Rise
The Vieneo Province
Loading...
Searching...
No Matches
TerrainMapProjection.h
Go to the documentation of this file.
1#pragma once
2
4#include <cmath>
5
7{
8 // Terrain vertices use render coordinates (the opposite sign to waypoints).
9 // Remove elevation before mapping: a sectional chart shows ground distance.
10 inline RiseMath::Vector2 Project(const RiseMath::Vector3& terrainPosition, float radius,
11 const RiseMath::Matrix& mapView, float pixelsPerKmX, float pixelsPerKmY,
12 RiseMath::Vector2 center)
13 {
14 RiseMath::Vector3 surface;
15 RiseMath::Vec3Normalize(&surface, &terrainPosition);
16 surface *= -radius;
17 RiseMath::Vec3TransformCoord(&surface, &surface, &mapView);
18 return { center.x - surface.x * pixelsPerKmX, center.y + surface.y * pixelsPerKmY };
19 }
20
21 // Map a texture rectangle to the four projected cell corners, in perimeter
22 // order. A homography keeps shared edges coincident, unlike rotated squares
23 // or affine parallelograms. D3DXSprite applies the homogeneous transform.
24 inline bool QuadTransform(const RiseMath::Vector2 (&p)[4], float width, float height,
25 float depth, RiseMath::Matrix& transform)
26 {
27 const double dx1 = double(p[1].x) - p[2].x, dy1 = double(p[1].y) - p[2].y;
28 const double dx2 = double(p[3].x) - p[2].x, dy2 = double(p[3].y) - p[2].y;
29 const double dx3 = double(p[0].x) - p[1].x + p[2].x - p[3].x;
30 const double dy3 = double(p[0].y) - p[1].y + p[2].y - p[3].y;
31 const double determinant = dx1 * dy2 - dx2 * dy1;
32 if (!std::isfinite(determinant) || std::fabs(determinant) < 1e-8)
33 return false;
34 const double g = (dx3 * dy2 - dx2 * dy3) / determinant;
35 const double h = (dx1 * dy3 - dx3 * dy1) / determinant;
36 if (1 + g <= 0 || 1 + h <= 0 || 1 + g + h <= 0)
37 return false;
38
39 transform = RiseMath::Matrix(
40 float((p[1].x - p[0].x + g * p[1].x) / width),
41 float((p[1].y - p[0].y + g * p[1].y) / width), float(depth * g / width), float(g / width),
42 float((p[3].x - p[0].x + h * p[3].x) / height),
43 float((p[3].y - p[0].y + h * p[3].y) / height), float(depth * h / height), float(h / height),
44 0, 0, 0, 0,
45 p[0].x, p[0].y, depth, 1);
46 return true;
47 }
48
49 template<class Vertex>
50 bool CellTransform(const Vertex (&grid)[64][64], int u, int v, float radius,
51 const RiseMath::Matrix& mapView, float pixelsPerKmX, float pixelsPerKmY,
52 RiseMath::Vector2 center, RiseMath::Matrix& transform, float minimumPixels = 0)
53 {
54 if (u < 0 || u >= 63 || v < 0 || v >= 63)
55 return false;
56 RiseMath::Vector2 corners[4];
57 const int du[] = { 0, 1, 1, 0 }, dv[] = { 0, 0, 1, 1 };
58 for (int corner = 0; corner < 4; ++corner)
59 {
60 const auto& vertex = grid[u + du[corner]][v + dv[corner]];
61 if (vertex.pri_landform == 255 || RiseMath::Vec3LengthSq(&vertex.position) < 1.0f)
62 return false;
63 corners[corner] = Project(vertex.position, radius, mapView, pixelsPerKmX, pixelsPerKmY, center);
64 }
65 if (minimumPixels > 0)
66 {
67 float left = corners[0].x, right = left, top = corners[0].y, bottom = top;
68 for (const auto& p : corners) { left = (std::min)(left,p.x); right = (std::max)(right,p.x); top = (std::min)(top,p.y); bottom = (std::max)(bottom,p.y); }
69 const float sx = (std::max)(1.f,minimumPixels/(std::max)(.001f,right-left));
70 const float sy = (std::max)(1.f,minimumPixels/(std::max)(.001f,bottom-top));
71 for (auto& p : corners) { p.x = (left+right)*.5f+(p.x-(left+right)*.5f)*sx; p.y = (top+bottom)*.5f+(p.y-(top+bottom)*.5f)*sy; }
72 }
73 return QuadTransform(corners, 32, 32, 0.1f, transform);
74 }
75}
Definition grid.h:14
Vector3 * Vec3TransformCoord(Vector3 *out, const Vector3 *a, const Matrix *m)
Definition ClientMath.h:109
float Vec3LengthSq(const Vector3 *a)
Definition ClientMath.h:94
Vector3 * Vec3Normalize(Vector3 *out, const Vector3 *a)
Definition ClientMath.h:103
RiseMath::Vector2 Project(const RiseMath::Vector3 &terrainPosition, float radius, const RiseMath::Matrix &mapView, float pixelsPerKmX, float pixelsPerKmY, RiseMath::Vector2 center)
bool QuadTransform(const RiseMath::Vector2(&p)[4], float width, float height, float depth, RiseMath::Matrix &transform)
bool CellTransform(const Vertex(&grid)[64][64], int u, int v, float radius, const RiseMath::Matrix &mapView, float pixelsPerKmX, float pixelsPerKmY, RiseMath::Vector2 center, RiseMath::Matrix &transform, float minimumPixels=0)