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

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

1#include "../Viewscreen/RtsTerrainSurface.h"
2
3#include <algorithm>
4#include <cmath>
5#include <cstdlib>
6#include <iostream>
7
8struct Vector
9{
10 float x, y, z;
11 Vector operator+(const Vector& b) const { return {x + b.x, y + b.y, z + b.z}; }
12 Vector operator-(const 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 Vector operator-() const { return {-x, -y, -z}; }
15};
16
17// Model the stored grid vertices, which are produced before rendering. Keep
18// /fp:fast from folding fixture construction through later local subtraction.
19__declspec(noinline) static Vector Stored(Vector value)
20{
21 volatile float x=value.x,y=value.y,z=value.z;
22 return {x,y,z};
23}
24
25struct PlayerOrigin
26{
27 Vector position;
28 double precisionx, precisiony, precisionz;
29};
30static PlayerOrigin playerships[1];
31struct TerrainOrigin
32{
33 Vector terpos;
34 double terprecisionx, terprecisiony, terprecisionz;
35 void Rebuild()
36 {
37 auto* viewscreen = this;
38#include "TerrainOrigin.generated.h"
39 }
40 Vector Translation() const
41 {
42 return {float(playerships[0].precisionx + terprecisionx),
43 float(playerships[0].precisiony + terprecisiony),
44 float(playerships[0].precisionz + terprecisionz)};
45 }
46};
47
48static int checks = 0;
49static void Check(bool condition, const char* message)
50{
51 ++checks;
52 if (!condition) { std::cerr << "FAIL: " << message << '\n'; std::exit(1); }
53}
54
55static double Distance(Vector a, Vector b)
56{
57 return std::sqrt(std::pow(double(a.x) - b.x, 2) +
58 std::pow(double(a.y) - b.y, 2) + std::pow(double(a.z) - b.z, 2));
59}
60
61// Independent reference: locate a point in grid.cpp's eight triangles using
62// double-precision barycentric coordinates in the terrain UV plane.
63static Vector Reference(const RtsTerrain::Surface<Vector>& surface, double u, double v)
64{
65 constexpr int triangles[8][3] = {
66 {0, 1, 3}, {1, 4, 3}, {1, 2, 4}, {2, 5, 4},
67 {3, 4, 6}, {4, 7, 6}, {4, 5, 7}, {5, 8, 7}
68 };
69 for (const auto& triangle : triangles)
70 {
71 double x[3], y[3];
72 Vector p[3];
73 for (int k = 0; k < 3; ++k)
74 {
75 x[k] = (triangle[k] % 3) * 0.5;
76 y[k] = (triangle[k] / 3) * 0.5;
77 p[k] = surface.points[triangle[k] / 3][triangle[k] % 3];
78 }
79 const double determinant = (y[1] - y[2]) * (x[0] - x[2]) +
80 (x[2] - x[1]) * (y[0] - y[2]);
81 const double a = ((y[1] - y[2]) * (u - x[2]) +
82 (x[2] - x[1]) * (v - y[2])) / determinant;
83 const double b = ((y[2] - y[0]) * (u - x[2]) +
84 (x[0] - x[2]) * (v - y[2])) / determinant;
85 const double c = 1.0 - a - b;
86 if (a >= -1e-9 && b >= -1e-9 && c >= -1e-9)
87 return {float(p[0].x*a + p[1].x*b + p[2].x*c),
88 float(p[0].y*a + p[1].y*b + p[2].y*c),
89 float(p[0].z*a + p[1].z*b + p[2].z*c)};
90 }
91 Check(false, "reference point outside terrain");
92 return {};
93}
94
95static void VerifySurface(const RtsTerrain::Surface<Vector>& surface)
96{
97 for (int north = 0; north <= 26; ++north)
98 for (int east = 0; east <= 26; ++east)
99 Check(Distance(surface.Sample(east, north),
100 Reference(surface, east / 26.0, (26 - north) / 26.0)) < 0.000002,
101 "RTS vertex does not lie on the terrain (including plot edges)");
102
103 // Testing interiors is essential: matching tile vertices alone still
104 // leaves a seam if RTS splits the quad along the opposite diagonal.
105 constexpr double weights[][3] = {{0.2, 0.3, 0.5}, {0.7, 0.1, 0.2}, {0.1, 0.8, 0.1}};
106 for (int north = 0; north < 26; ++north)
107 for (int east = 0; east < 26; ++east)
108 for (int triangle = 0; triangle < 2; ++triangle)
109 {
110 const auto* corners = RtsTerrain::TileCorners + triangle * 3;
111 const int winding = (corners[1].east - corners[0].east) * (corners[2].north - corners[0].north) -
112 (corners[1].north - corners[0].north) * (corners[2].east - corners[0].east);
113 Check(winding > 0, "RTS triangle winding changed");
114 for (const auto& weight : weights)
115 {
116 Vector interpolated = {};
117 double u = 0, v = 0;
118 for (int k = 0; k < 3; ++k)
119 {
120 const int e = east + corners[k].east;
121 const int n = north + corners[k].north;
122 interpolated = interpolated + surface.Sample(e, n) * float(weight[k]);
123 u += e / 26.0 * weight[k];
124 v += (26 - n) / 26.0 * weight[k];
125 }
126 Check(Distance(interpolated, Reference(surface, u, v)) < 0.000002,
127 "RTS triangle bridges a terrain fold");
128 }
129 }
130}
131
132static void VerifyRenderOrigins()
133{
134 double oldGap = 0, newGap = 0;
135 // Non-representable camera positions, at planet scale. Rebuild in both
136 // directions and move between rebuilds, as driving across a plot does.
137 for (int rebuild = -4; rebuild <= 4; ++rebuild)
138 {
139 auto& player = playerships[0];
140 player.precisionx = -2654.123456789 - rebuild * .137;
141 player.precisiony = 1298.987654321 + rebuild * .081;
142 player.precisionz = 215.234567891 - rebuild * .019;
143 player.position = {float(player.precisionx),float(player.precisiony),float(player.precisionz)};
144 const double originalX = -player.precisionx, originalY = -player.precisiony, originalZ = -player.precisionz;
145 TerrainOrigin terrain; terrain.Rebuild();
146 Check(terrain.terprecisionx == double(terrain.terpos.x)
147 && terrain.terprecisiony == double(terrain.terpos.y)
148 && terrain.terprecisionz == double(terrain.terpos.z),
149 "terrain drawing and collision must restore the origin subtracted from vertices");
151 const Vector zoneOrigin = Stored(terrain.terpos + Vector{.73f,-.41f,.22f});
153 for(int y=0;y<3;++y)for(int x=0;x<3;++x)
154 {
155 const Vector world = Stored(zoneOrigin + Vector{float(x-1),float(y-1),.03f*float(x*y)});
156 city.points[y][x] = world-zoneOrigin;
157 ground.points[y][x] = world-terrain.terpos;
158 }
159 for(int step=-10;step<=10;++step)
160 {
161 player.precisionx = -originalX + step*.027;
162 player.precisiony = -originalY - step*.018;
163 player.precisionz = -originalZ + step*.009;
164 const Vector cityTranslation = {float(player.precisionx+double(zoneOrigin.x)),
165 float(player.precisiony+double(zoneOrigin.y)),float(player.precisionz+double(zoneOrigin.z))};
166 const Vector oldTranslation = {float(player.precisionx+originalX),
167 float(player.precisiony+originalY),float(player.precisionz+originalZ)};
168 for(int side=0;side<4;++side)for(int edge=0;edge<=26;++edge)
169 {
170 const int east=side<2?edge:side==2?0:26;
171 const int north=side<2?(side==0?0:26):edge;
172 const Vector expected=city.Sample(east,north)+cityTranslation;
173 const Vector local=ground.Sample(east,north);
174 oldGap=std::max(oldGap,Distance(local+oldTranslation,expected));
175 newGap=std::max(newGap,Distance(local+terrain.Translation(),expected));
176
177 }
178 // CalculateAGLd casts from the inverse of the same translation
179 // into vertices localized by terpos. Test the ray origin as well.
180 const Vector rayOrigin = -terrain.Translation();
181 const Vector expectedRay = {float(-player.precisionx-double(terrain.terpos.x)),
182 float(-player.precisiony-double(terrain.terpos.y)),float(-player.precisionz-double(terrain.terpos.z))};
183 Check(Distance(rayOrigin,expectedRay)<.000002,"ground collision ray uses a different origin");
184 }
185 }
186 Check(oldGap>.00005,"fixture must reproduce the draw-origin road gap");
187 std::cout << "Origin mismatch: before=" << oldGap*1000 << " m; corrected=" << newGap*1000 << " m\n";
188 Check(newGap<.000002,"terrain and city road separate after camera motion or grid rebuild");
189}
190
191int main()
192{
193 VerifyRenderOrigins();
194 // Distort all nine points so both planar-only and bilinear implementations
195 // fail. This covers the curved plots used by non-Deois RTS cities too.
197 {{-1.05f, -1.1f, 0.01f}, {0.02f, -1.0f, 0.03f}, {1.0f, -1.05f, -0.01f}},
198 {{-1.1f, 0.03f, -0.04f}, {0.0f, 0.0f, 0.08f}, {1.02f, 0.01f, 0.02f}},
199 {{-1.0f, 1.04f, 0.03f}, {-0.02f, 1.05f, -0.02f}, {1.05f, 1.0f, 0.0f}}
200 }};
201 VerifySurface(curved);
202
203 // A Deois-style flat plot at planet-scale coordinates. The midpoints are
204 // computed exactly as CalculateInsideArray does, including float rounding.
205 const Vector nw = {1700.1123f, 1699.0817f, 1701.3456f};
206 const Vector ne = nw + Vector{1.434f, -1.434f, 0.0f};
207 const Vector sw = nw + Vector{0.828f, 0.828f, -1.656f};
208 const Vector se = ne + (sw - nw);
209 const Vector origin = nw * 0.25f + se * 0.25f + ne * 0.25f + sw * 0.25f;
211 {nw - origin, nw * 0.5f + ne * 0.5f - origin, ne - origin},
212 {nw * 0.5f + sw * 0.5f - origin, {}, ne * 0.5f + se * 0.5f - origin},
213 {sw - origin, sw * 0.5f + se * 0.5f - origin, se - origin}
214 }};
215 VerifySurface(flat);
216
217 double oldMaximumGap = 0;
218 for (int n = 0; n <= 26; ++n)
219 for (int e = 0; e <= 26; ++e)
220 {
221 const Vector left = sw + (nw - sw) * (float(n) / 26.0f);
222 const Vector right = se + (ne - se) * (float(n) / 26.0f);
223 const Vector old = left + (right - left) * (float(e) / 26.0f) - origin;
224 oldMaximumGap = std::max(oldMaximumGap,
225 Distance(old, Reference(flat, e / 26.0, (26 - n) / 26.0)));
226 }
227 Check(oldMaximumGap > 0.00005, "fixture must reproduce the old planet-space rounding gap");
228
229 // A neighboring plot has a different local origin but must use the same
230 // shared boundary. Compare the full edge, not only its three control points.
231 auto neighbor = curved;
232 const Vector offset = {1.8f, -0.1f, 0.05f};
233 for (int row = 0; row < 3; ++row)
234 neighbor.points[row][0] = curved.points[row][2] - offset;
235 for (int n = 0; n <= 26; ++n)
236 Check(Distance(curved.Sample(26, n), neighbor.Sample(0, n) + offset) < 0.000002,
237 "neighboring plot origins separate a shared edge");
238
239 // Original texture corner values, independently tabulated from buildings.cpp.
240 constexpr RtsTerrain::TexCoord expected[4][4] = {
241 {{1,0}, {0,0}, {1,1}, {0,1}}, {{0,0}, {0,1}, {1,0}, {1,1}},
242 {{0,1}, {1,1}, {0,0}, {1,0}}, {{1,1}, {1,0}, {0,1}, {0,0}}
243 };
244 for (unsigned char rotation = 0; rotation < 8; ++rotation)
245 for (const auto corner : RtsTerrain::TileCorners)
246 {
247 auto want = expected[rotation % 4][corner.north * 2 + corner.east];
248 if (rotation > 3) want.u = 1.0f - want.u;
249 const auto got = RtsTerrain::TextureCoordinate(corner, rotation);
250 Check(got.u == want.u && got.v == want.v, "tile rotation/mirroring changed");
251 }
252
253 std::cout << "PASS: " << checks << " RTS surface checks; old flat-plot gap reproduced: "
254 << oldMaximumGap * 1000.0 << " meters; new surface tolerance: 0.002 meters\n";
255}
Reference
Definition ServerTypes.h:40
s_network_objects playerships[MAX_SCAN]
Definition globals.cpp:112
Vec operator*(Vec a, double b)
Definition OrbitMath.h:14
Vec operator-(Vec a, Vec b)
Definition OrbitMath.h:13
DirectX::XMFLOAT3 Vector
Definition Math11.h:7
constexpr TileCorner TileCorners[6]
TexCoord TextureCoordinate(TileCorner corner, unsigned char rotation)
float Distance(Point a, Point b)
Vector Sample(int east, int north) const
double precisionx
Definition globals.h:512