Mercator  0.4.0
Segment.h
1 // This file may be redistributed and modified only under the terms of
2 // the GNU General Public License (See COPYING for details).
3 // Copyright (C) 2003 Alistair Riddoch, Damien McGinnes
4 
5 #ifndef MERCATOR_SEGMENT_H
6 #define MERCATOR_SEGMENT_H
7 
8 #include "Mercator.h"
9 #include "Matrix.h"
10 #include "BasePoint.h"
11 #include "HeightMap.h"
12 
13 #include <wfmath/vector.h>
14 #include <wfmath/axisbox.h>
15 
16 #include <set>
17 #include <map>
18 
19 namespace WFMath {
20 class MTRand;
21 }
22 
23 namespace Mercator {
24 
25 class Terrain;
26 class Surface;
27 class TerrainMod;
28 class Area;
29 
30 // This class will need to be reference counted if we want the code to
31 // be able to hold onto it, as currently they get deleted internally
32 // whenever height points are asserted.
33 
36 class Segment {
37  public:
39  typedef std::map<int, Surface *> Surfacestore;
40 
42  typedef std::multimap<int, const Area *> Areastore;
43  private:
45  const int m_res;
47  const int m_size;
49  const int m_xRef;
51  const int m_zRef;
53  Matrix<2, 2, BasePoint> m_controlPoints;
55  HeightMap m_heightMap;
57  float * m_normals;
58 
60  Surfacestore m_surfaces;
61 
63  Areastore m_areas;
64 
66  std::map<long, const TerrainMod*> m_terrainMods;
67  public:
68  explicit Segment(int x, int z, unsigned int resolution);
69  ~Segment();
70 
72  int getResolution() const {
73  return m_res;
74  }
75 
77  int getSize() const {
78  return m_size;
79  }
80 
82  int getXRef() const {
83  return m_xRef;
84  }
85 
87  int getZRef() const {
88  return m_zRef;
89  }
90 
94  bool isValid() const {
95  return m_heightMap.isValid();
96  }
97 
98  void invalidate(bool points = true);
99 
106  void setCornerPoint(unsigned int x, unsigned int z, const BasePoint & bp) {
107  m_controlPoints(x, z) = bp;
108  invalidate();
109  }
110 
113  return m_controlPoints;
114  }
115 
118  return m_controlPoints;
119  }
120 
122  const Surfacestore & getSurfaces() const {
123  return m_surfaces;
124  }
125 
127  Surfacestore & getSurfaces() {
128  return m_surfaces;
129  }
130 
132  const float * getPoints() const {
133  return m_heightMap.getData();
134  }
135 
137  float * getPoints() {
138  return m_heightMap.getData();
139  }
140 
142  const HeightMap& getHeightMap() const {
143  return m_heightMap;
144  }
145 
148  return m_heightMap;
149  }
150 
152  const float * getNormals() const {
153  return m_normals;
154  }
155 
157  float * getNormals() {
158  return m_normals;
159  }
160 
162  float get(int x, int z) const {
163  return m_heightMap.get(x, z);
164  }
165 
166  void getHeight(float x, float y, float &h) const;
167  void getHeightAndNormal(float x, float z, float &h,
168  WFMath::Vector<3> &normal) const;
169  bool clipToSegment(const WFMath::AxisBox<2> &bbox, int &lx, int &hx, int &lz, int &hz) const;
170 
171 
172  void populate();
173  void populateNormals();
174  void populateSurfaces();
175  void populateHeightMap(HeightMap& heightMap);
176 
178  float getMax() const { return m_heightMap.getMax(); }
180  float getMin() const { return m_heightMap.getMin(); }
181 
183  WFMath::AxisBox<2> getRect() const;
184 
185  void updateMod(long id, const TerrainMod *t);
186 
187  void clearMods();
188 
190  const Areastore& getAreas() const
191  { return m_areas; }
192 
193  const std::map<long, const TerrainMod*>& getMods() const
194  { return m_terrainMods; }
195 
196  int addArea(const Area* a);
197  int updateArea(const Area* a);
198  int removeArea(const Area* a);
199  private:
200 
201  void applyMod(const TerrainMod *t);
202 
203  void invalidateSurfaces();
204 
205 };
206 
207 } // namespace Mercator
208 
209 #endif // MERCATOR_SEGMENT_H
Definition: HeightMap.h:13
int getZRef() const
Accessor for Global y reference of this segment.
Definition: Segment.h:87
Class storing heightfield and other data for a single fixed size square area of terrain defined by fo...
Definition: HeightMap.h:21
DataType * getData()
Accessor for a pointer to buffer containing data values.
Definition: Buffer.h:61
const Surfacestore & getSurfaces() const
Accessor for list of attached Surface objects.
Definition: Segment.h:122
const float * getNormals() const
Accessor for buffer containing surface normals.
Definition: Segment.h:152
Definition: Area.cpp:20
bool isValid() const
Check whether this Segment contains valid point data.
Definition: Segment.h:94
float get(int x, int z) const
Get the height at a relative integer position in the Segment.
Definition: HeightMap.h:40
int getSize() const
Accessor for array size of this segment.
Definition: Segment.h:77
std::map< int, Surface * > Surfacestore
STL map of pointers to Surface objects.
Definition: Segment.h:39
Matrix< 2, 2, BasePoint > & getControlPoints()
Accessor for modifying 2D matrix of base points.
Definition: Segment.h:117
Class storing heightfield and other data for a single fixed size square area of terrain defined by fo...
Definition: Segment.h:36
float getMin() const
Accessor for the minimum height value in this Segment.
Definition: HeightMap.h:51
void setCornerPoint(unsigned int x, unsigned int z, const BasePoint &bp)
Set the BasePoint data for one of the four that define this Segment.
Definition: Segment.h:106
const Areastore & getAreas() const
Accessor for multimap of Area objects.
Definition: Segment.h:190
int getXRef() const
Accessor for Global x reference of this segment.
Definition: Segment.h:82
Point on the fundamental grid that is used as the basis for terrain.
Definition: BasePoint.h:19
A fixed sized array of objects.
Definition: Matrix.h:14
const Matrix< 2, 2, BasePoint > & getControlPoints() const
Accessor for 2D matrix of base points.
Definition: Segment.h:112
float getMax() const
Accessor for the maximum height value in this Segment.
Definition: Segment.h:178
bool isValid() const
Determine if this buffer has valid allocated storage.
Definition: Buffer.h:81
float getMin() const
Accessor for the minimum height value in this Segment.
Definition: Segment.h:180
std::multimap< int, const Area * > Areastore
STL multimap of pointers to Area objects affecting this segment.
Definition: Segment.h:42
float * getNormals()
Accessor for write access to buffer containing surface normals.
Definition: Segment.h:157
float * getPoints()
Accessor for write access to buffer containing height points.
Definition: Segment.h:137
const float * getPoints() const
Accessor for buffer containing height points.
Definition: Segment.h:132
int getResolution() const
Accessor for resolution of this segment.
Definition: Segment.h:72
const HeightMap & getHeightMap() const
Accessor for height map.
Definition: Segment.h:142
Surfacestore & getSurfaces()
Accessor for modifying list of attached Surface objects.
Definition: Segment.h:127
Region of terrain surface which is modified.
Definition: Area.h:28
HeightMap & getHeightMap()
Accessor for write access to height map.
Definition: Segment.h:147
float getMax() const
Accessor for the maximum height value in this Segment.
Definition: HeightMap.h:49
Base class for modifiers to the procedurally generated terrain.
Definition: TerrainMod.h:20