Mercator
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/Mercator.h>
9 #include <Mercator/Matrix.h>
10 #include <Mercator/BasePoint.h>
11 
12 #include <wfmath/vector.h>
13 #include <wfmath/axisbox.h>
14 
15 #include <set>
16 #include <map>
17 
18 namespace WFMath {
19 class MTRand;
20 }
21 
22 namespace Mercator {
23 
24 class Terrain;
25 class Surface;
26 class TerrainMod;
27 typedef std::set<const TerrainMod *> ModList;
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_yRef;
55  float * m_points;
57  float * m_normals;
59  float m_max;
61  float m_min;
62 
64  Surfacestore m_surfaces;
65 
67  Areastore m_areas;
68 
70  ModList m_modList;
71  public:
72  explicit Segment(int x, int y, unsigned int resolution);
73  ~Segment();
74 
76  const int getResolution() const {
77  return m_res;
78  }
79 
81  const int getSize() const {
82  return m_size;
83  }
84 
86  const int getXRef() const {
87  return m_xRef;
88  }
89 
91  const int getYRef() const {
92  return m_yRef;
93  }
94 
98  const bool isValid() const {
99  return (m_points != 0);
100  }
101 
106  void setMinMax(float min, float max) {
107  m_min = min;
108  m_max = max;
109  }
110 
111  void invalidate(bool points = true);
112 
119  void setCornerPoint(unsigned int x, unsigned int y, const BasePoint & bp) {
120  m_controlPoints(x, y) = bp;
121  invalidate();
122  }
123 
126  return m_controlPoints;
127  }
128 
131  return m_controlPoints;
132  }
133 
135  const Surfacestore & getSurfaces() const {
136  return m_surfaces;
137  }
138 
140  Surfacestore & getSurfaces() {
141  return m_surfaces;
142  }
143 
145  const float * getPoints() const {
146  return m_points;
147  }
148 
150  float * getPoints() {
151  return m_points;
152  }
153 
155  const float * getNormals() const {
156  return m_normals;
157  }
158 
160  float * getNormals() {
161  return m_normals;
162  }
163 
165  float get(int x, int y) const {
166  return m_points[y * (m_res + 1) + x];
167  }
168 
169  void getHeightAndNormal(float x, float y, float &h,
170  WFMath::Vector<3> &normal) const;
171  bool clipToSegment(const WFMath::AxisBox<2> &bbox, int &lx, int &hx, int &ly, int &hy) const;
172 
173 
174  void populate();
175  void populateNormals();
176  void populateSurfaces();
177 
179  float getMax() const { return m_max; }
181  float getMin() const { return m_min; }
182 
184  WFMath::AxisBox<2> getRect() const;
185 
187  WFMath::AxisBox<3> getBox() const;
188 
189  int addMod(const TerrainMod *t);
190  int updateMod(const TerrainMod *t);
191  int removeMod(const TerrainMod *t);
192  void clearMods();
193 
195  const Areastore& getAreas() const
196  { return m_areas; }
197 
198  const ModList& getMods() const
199  { return m_modList; }
200 
201  int addArea(const Area* a);
202  int updateArea(const Area* a);
203  int removeArea(const Area* a);
204  private:
205  void checkMaxMin(float h);
206 
207  void fill1d(const BasePoint& l, const BasePoint &h, float *array) const;
208 
209  void fill2d(const BasePoint& p1, const BasePoint& p2,
210  const BasePoint& p3, const BasePoint& p4);
211 
212  float qRMD(WFMath::MTRand& rng, float nn, float fn, float ff, float nf,
213  float roughness, float falloff, float depth) const;
214 
215  void applyMod(const TerrainMod *t);
216 
217  void invalidateSurfaces();
218 
219 };
220 
221 } // namespace Mercator
222 
223 #endif // MERCATOR_SEGMENT_H
Definition: Segment.h:18
const int m_yRef
Global y reference of this segment.
Definition: Segment.h:51
float m_max
Maximum height of any point in this segment.
Definition: Segment.h:59
Areastore m_areas
Areas which intersect this segment.
Definition: Segment.h:67
const int getYRef() const
Accessor for Global y reference of this segment.
Definition: Segment.h:91
ModList m_modList
List of TerrainMod objects that are applied to this Segment.
Definition: Segment.h:70
Definition: Area.cpp:20
const int m_res
Distance between segments.
Definition: Segment.h:45
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:130
void setMinMax(float min, float max)
Set min and max height values for this Segment.
Definition: Segment.h:106
const Surfacestore & getSurfaces() const
Accessor for list of attached Surface objects.
Definition: Segment.h:135
Matrix< 2, 2, BasePoint > m_controlPoints
2x2 matrix of points which control this segment
Definition: Segment.h:53
const int getResolution() const
Accessor for resolution of this segment.
Definition: Segment.h:76
float getMin() const
Accessor for the minimum height value in this Segment.
Definition: Segment.h:181
Class storing heightfield and other data for a single fixed size square area of terrain defined by fo...
Definition: Segment.h:36
float * m_normals
Pointer to buffer containing normals for height points.
Definition: Segment.h:57
const Areastore & getAreas() const
Accessor for multimap of Area objects.
Definition: Segment.h:195
void setCornerPoint(unsigned int x, unsigned int y, const BasePoint &bp)
Set the BasePoint data for one of the four that define this Segment.
Definition: Segment.h:119
const int getXRef() const
Accessor for Global x reference of this segment.
Definition: Segment.h:86
const int m_xRef
Global x reference of this segment.
Definition: Segment.h:49
const float * getNormals() const
Accessor for buffer containing surface normals.
Definition: Segment.h:155
Surfacestore m_surfaces
Store of surfaces which can be rendered on this terrain.
Definition: Segment.h:64
float getMax() const
Accessor for the maximum height value in this Segment.
Definition: Segment.h:179
float m_min
Minimum height of any point in this segment.
Definition: Segment.h:61
const bool isValid() const
Check whether this Segment contains valid point data.
Definition: Segment.h:98
Point on the fundamental grid that is used as the basis for terrain.
Definition: BasePoint.h:19
float * m_points
Pointer to buffer containing height points.
Definition: Segment.h:55
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:125
const int getSize() const
Accessor for array size of this segment.
Definition: Segment.h:81
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:160
const float * getPoints() const
Accessor for buffer containing height points.
Definition: Segment.h:145
float * getPoints()
Accessor for write access to buffer containing height points.
Definition: Segment.h:150
const int m_size
Size of segment, m_res + 1.
Definition: Segment.h:47
Surfacestore & getSurfaces()
Accessor for modifying list of attached Surface objects.
Definition: Segment.h:140
Region of terrain surface which is modified.
Definition: Area.h:28
Base class for modifiers to the procedurally generated terrain.
Definition: TerrainMod.h:20