Rise
The Vieneo Province
Loading...
Searching...
No Matches
Tests/CloudBaseGeometryTests.cpp

Source at documentation-generation time. See Test catalog for fixture associations.

1#include "../Viewscreen/CloudBaseGeometry.h"
2
3#include <cstdlib>
4#include <iostream>
5
6struct Vector
7{
8 float x = 0, y = 0, z = 0;
9 Vector() = default;
10 Vector(float x, float y, float z) : x(x), y(y), z(z) {}
11 Vector operator+(Vector b) const { return {x + b.x, y + b.y, z + b.z}; }
12 Vector operator-(Vector b) const { return {x - b.x, y - b.y, z - b.z}; }
13 Vector operator*(float b) const { return {x * b, y * b, z * b}; }
14};
15
16static int checks = 0;
17static void Check(bool condition, const char* message)
18{
19 ++checks;
20 if (!condition)
21 {
22 std::cerr << "FAIL: " << message << '\n';
23 std::exit(1);
24 }
25}
26
27static double Dot(Vector a, Vector b)
28{
29 return double(a.x) * b.x + double(a.y) * b.y + double(a.z) * b.z;
30}
31
32static Vector Cross(Vector a, Vector b)
33{
34 return {a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x};
35}
36
37static double Length(Vector a) { return std::sqrt(Dot(a, a)); }
38static constexpr float Radius = 2939.6f;
39
40// An independent ray/sphere reference using the six faces' center and axes.
41// It also checks the sign/orientation against the existing terrain convention.
42static Vector Reference(int face, int u, int v)
43{
44 static const Vector normals[] = {{0,-1,0}, {1,0,0}, {-1,0,0}, {0,1,0}, {0,0,-1}, {0,0,1}};
45 static const Vector east[] = {{1,0,0}, {0,1,0}, {0,-1,0}, {-1,0,0}, {1,0,0}, {1,0,0}};
46 static const Vector down[] = {{0,0,1}, {0,0,1}, {0,0,1}, {0,0,1}, {0,-1,0}, {0,1,0}};
47 static const int centerU[] = {1019, 3057, -1019, 5095, 1019, 1019};
48 static const int centerV[] = {1019, 1019, 1019, 1019, -1019, 3057};
49 const double uOffset = (u - centerU[face]) / 1019.0;
50 const double vOffset = (v - centerV[face]) / 1019.0;
51 const double x = normals[face].x + east[face].x * uOffset + down[face].x * vOffset;
52 const double y = normals[face].y + east[face].y * uOffset + down[face].y * vOffset;
53 const double z = normals[face].z + east[face].z * uOffset + down[face].z * vOffset;
54 const double scale = -Radius / std::sqrt(x*x + y*y + z*z);
55 return {float(x * scale), float(y * scale), float(z * scale)};
56}
57
58static Vector Position(int face, int u, int v)
59{
60 return CloudBaseGeometry::Position<Vector>(face, u, v, Radius);
61}
62
63static void CheckTriangle(Vector a, Vector b, Vector c)
64{
65 const Vector normal = Cross(b - a, c - a);
66 Check(Length(normal) > 0.5, "cloud triangle collapsed at a face edge/corner");
67 Check(Dot(normal, a) < 0, "cloud triangle winding no longer faces the surface");
68}
69
70static void CheckPatch(int face, int centerU, int centerV)
71{
72 // Same 64x64 patch and triangle winding as the cloud vertex/index buffers.
73 for (int t = 0; t < 64; ++t)
74 for (int s = 0; s < 64; ++s)
75 {
76 const int u = centerU + t - 31, v = centerV + s - 31;
77 const Vector p = Position(face, u, v);
78 Check(std::abs(Length(p) - Radius) < 0.0006,
79 "cloud vertex left the sphere (terrain void placeholder)");
80 Check(Length(p - Reference(face, u, v)) < 0.0006,
81 "cloud projection changed the face orientation or grid scale");
82 if (t < 63 && s < 63)
83 {
84 const Vector right = Position(face, u+1, v), down = Position(face, u, v+1);
85 const Vector diagonal = Position(face, u+1, v+1);
86 Check(Length(right - p) < 3.0 && Length(down - p) < 3.0,
87 "cloud edge spans more than a local grid cell");
88 CheckTriangle(p, diagonal, down);
89 CheckTriangle(p, right, diagonal);
90 }
91 if (t > 0 && t < 63 && s > 0 && s < 63)
92 {
93 // Smooth geometry means uniform texture steps cannot produce
94 // the old abrupt change in UV scale where a cube face folds.
95 Check(Length(Position(face, u-1, v) + Position(face, u+1, v) - p*2) < 0.01,
96 "cloud texture direction has a kink across the U seam");
97 Check(Length(Position(face, u, v-1) + Position(face, u, v+1) - p*2) < 0.01,
98 "cloud texture direction has a kink across the V seam");
99 }
100 }
101}
102
103int main()
104{
105 static const int originU[] = {0, 2038, -2038, 4076, 0, 0};
106 static const int originV[] = {0, 0, 0, 0, -2038, 2038};
107 for (int face = 0; face < 6; ++face)
108 for (int u : {0, 1019, 2038})
109 for (int v : {0, 1019, 2038})
110 CheckPatch(face, originU[face] + u, originV[face] + v);
111
112 // Reported surface location: N35.264 E44.998, close to PM/E/N corner.
113 const double radians = std::acos(-1.0) / 180.0;
114 const double latitude = 35.264 * radians, longitude = 44.998 * radians;
115 const int u = int((std::tan(longitude) + 1.0) * 1019.0);
116 const int v = int((1.0 - std::tan(latitude) / std::cos(longitude)) * 1019.0);
117 CheckPatch(0, u, v);
118 std::cout << "Passed " << checks << " cloud geometry checks across all 6 faces, "
119 "12 edges, 8 corners, and N35.264 E44.998 (grid " << u << ',' << v << ").\n";
120}
SCRIPT_CONTROL SCRIPT_STATE const void int
Reference
Definition ServerTypes.h:40
Vector Position(int cube, int u, int v, float radius)
float Dot(const Vector3 &a, const Vector3 &b)
Vec operator*(Vec a, double b)
Definition OrbitMath.h:14
Vec Cross(Vec a, Vec b)
Definition OrbitMath.h:17
DirectX::XMFLOAT3 Vector
Definition Math11.h:7
double Length(Point a)