Stellarium  0.90.0
Heightmap.hpp
1 /*
2  * Stellarium Scenery3d Plug-in
3  *
4  * Copyright (C) 2011-2015 Simon Parzer, Peter Neubauer, Georg Zotti, Andrei Borza, Florian Schaukowitsch
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
19  */
20 
21 #ifndef HEIGHTMAP_HPP
22 #define HEIGHTMAP_HPP
23 
24 #include "StelOBJ.hpp"
25 
27 class Heightmap
28 {
29 
30 public:
31  typedef QVector<unsigned int> IdxList;
32  typedef QVector<Vec3f> PosList;
33 
37  Heightmap();
38  virtual ~Heightmap();
39 
41  void setMeshData(const IdxList& indexList, const PosList& posList, const AABBox *bbox = Q_NULLPTR);
42 
48  float getHeight(const float x, const float y) const;
49 
51  void setNullHeight(float h){nullHeight=h;}
52  float getNullHeight() const {return nullHeight;}
53 
54 private:
55  IdxList indexList;
56  PosList posList;
57 
58  static const int GRID_LENGTH = 60; // # of grid spaces is GRID_LENGTH^2
59 
60  typedef QVector<const unsigned int*> FaceVector; //points to first index in Index list for a face
61 
62  struct QuadTreeNode
63  {
65  QuadTreeNode(const Vec2f& min, const Vec2f& max);
66  //deletes all child nodes
67  ~QuadTreeNode();
68 
69  //indexes the child nodes, INVALID means outside the current node
70  enum Quadrant { MinMin, MinMax, MaxMin, MaxMax, INVALID };
71 
72  //Stores the index of the given triangle in the QuadTree
73  void putTriangle(const unsigned int* pTriangle, const PosList& posList, int maxLevel = 7);
74 
75  float getHeightAtPoint(const Vec2f& point, const PosList &posList) const;
76 
77  //Recursively retrieves all triangles which *potentially* lie at the given point
78  FaceVector getTriangleCandidatesAtPoint(const Vec2f& point) const;
79 
80  private:
82  QuadTreeNode();
83  void init(QuadTreeNode* parent, const Vec2f& min, const Vec2f& max);
84 
85  //turns this node from a leaf node into a normal node by adding 4 children
86  void subdivide();
87 
88  //gets the subquadrant in which the specified point lies
89  Quadrant getQuadrantForPoint(const Vec2f& point) const;
90 
91  //the parent node in case of a non-root node
92  QuadTreeNode* parent;
93  //either Q_NULLPTR in case of a leaf, or array of size 4
94  QuadTreeNode* children;
95 
97  int level;
99  int depth;
101  int nodecount;
102  //contains faces which do not fit wholly in a subnode
103  FaceVector faces;
104 
105  Vec2f min, max, mid;
106  } *rootNode;
107 
108  struct GridSpace {
109  FaceVector faces;
110  float getHeight(const PosList &posList, const float x, const float y) const;
111  };
112 
113  GridSpace* grid;
114  Vec2f min, max, range;
115  float nullHeight; // return value for areas outside grid
116 
117  void initQuadtree();
118  void initGrid();
119  GridSpace* getSpace(const float x, const float y) const ;
120  static bool triangle_intersects_bbox(const Vec2f &t1, const Vec2f &t2, const Vec2f &t3, const Vec2f &rMin, const Vec2f &rMax);
122  inline static bool sameSide(const Vec2f& p, const Vec2f& q, const Vec2f& a, const Vec2f& b);
123  static bool line_intersects_triangle(const Vec2f &t1, const Vec2f &t2, const Vec2f &t3, const Vec2f &p0, const Vec2f &p1);
125  static void cartesian_to_barycentric(const Vec2f& t1, const Vec2f& t2, const Vec2f& t3, const Vec2f& p, float* l1, float* l2, float* l3);
126  static float face_height_at(const PosList &obj, const unsigned int *pTriangle, const float x, const float y);
127 };
128 
129 #endif // HEIGHTMAP_HPP
This represents a heightmap for viewer-ground collision.
Definition: Heightmap.hpp:27
float getHeight(const float x, const float y) const
Get z Value at (x,y) coordinates.
void setMeshData(const IdxList &indexList, const PosList &posList, const AABBox *bbox=Q_NULLPTR)
Sets the mesh data to use. If the bbox is given, min/max calculation is skipped and its values are ta...
Heightmap()
Construct a heightmap from a loaded OBJ mesh.
An axis-aligned bounding-box class.
Definition: GeomMath.hpp:31
void setNullHeight(float h)
set/retrieve default height
Definition: Heightmap.hpp:51