Mercator  0.4.0
Public Types | Public Member Functions | List of all members
Mercator::Segment Class Reference

Class storing heightfield and other data for a single fixed size square area of terrain defined by four adjacent BasePoint objects. More...

#include <Segment.h>

Public Types

typedef std::map< int, Surface * > Surfacestore
 STL map of pointers to Surface objects.
 
typedef std::multimap< int, const Area * > Areastore
 STL multimap of pointers to Area objects affecting this segment.
 

Public Member Functions

 Segment (int x, int z, unsigned int resolution)
 Construct an empty segment with the given resolution. More...
 
 ~Segment ()
 Destruct the Segment. More...
 
int getResolution () const
 Accessor for resolution of this segment.
 
int getSize () const
 Accessor for array size of this segment.
 
int getXRef () const
 Accessor for Global x reference of this segment.
 
int getZRef () const
 Accessor for Global y reference of this segment.
 
bool isValid () const
 Check whether this Segment contains valid point data. More...
 
void invalidate (bool points=true)
 Mark the contents of this Segment as stale. More...
 
void setCornerPoint (unsigned int x, unsigned int z, const BasePoint &bp)
 Set the BasePoint data for one of the four that define this Segment. More...
 
const Matrix< 2, 2, BasePoint > & getControlPoints () const
 Accessor for 2D matrix of base points.
 
Matrix< 2, 2, BasePoint > & getControlPoints ()
 Accessor for modifying 2D matrix of base points.
 
const SurfacestoregetSurfaces () const
 Accessor for list of attached Surface objects.
 
SurfacestoregetSurfaces ()
 Accessor for modifying list of attached Surface objects.
 
const float * getPoints () const
 Accessor for buffer containing height points.
 
float * getPoints ()
 Accessor for write access to buffer containing height points.
 
const HeightMapgetHeightMap () const
 Accessor for height map.
 
HeightMapgetHeightMap ()
 Accessor for write access to height map.
 
const float * getNormals () const
 Accessor for buffer containing surface normals.
 
float * getNormals ()
 Accessor for write access to buffer containing surface normals.
 
float get (int x, int z) const
 Get the height at a relative integer position in the Segment.
 
void getHeight (float x, float y, float &h) const
 
void getHeightAndNormal (float x, float z, float &h, WFMath::Vector< 3 > &normal) const
 Get an accurate height and normal vector at a given coordinate relative to this segment. More...
 
bool clipToSegment (const WFMath::AxisBox< 2 > &bbox, int &lx, int &hx, int &lz, int &hz) const
 Determine the intersection between an axis aligned box and this segment. More...
 
void populate ()
 Populate the Segment with heightfield data. More...
 
void populateNormals ()
 Populate the Segment with surface normal data. More...
 
void populateSurfaces ()
 Populate the surfaces associated with this Segment. More...
 
void populateHeightMap (HeightMap &heightMap)
 
float getMax () const
 Accessor for the maximum height value in this Segment.
 
float getMin () const
 Accessor for the minimum height value in this Segment.
 
WFMath::AxisBox< 2 > getRect () const
 The 2d area covered by this segment.
 
void updateMod (long id, const TerrainMod *t)
 
void clearMods ()
 Delete all the modifications applied to this Segment. More...
 
const AreastoregetAreas () const
 Accessor for multimap of Area objects.
 
const std::map< long, const TerrainMod * > & getMods () const
 
int addArea (const Area *a)
 Add an area to those that affect this segment. More...
 
int updateArea (const Area *a)
 
int removeArea (const Area *a)
 Remove an area from those that affect this segment.
 

Detailed Description

Class storing heightfield and other data for a single fixed size square area of terrain defined by four adjacent BasePoint objects.

Constructor & Destructor Documentation

◆ Segment()

Mercator::Segment::Segment ( int  x,
int  z,
unsigned int  resolution 
)
explicit

Construct an empty segment with the given resolution.

Generally it is not necessary to call this from outside the Mercator library Segment objects are created as required. The Segment is constructed without allocating any storage for heightfield or surface normal data. The m_min and m_max members are initialised to extreme values, and should be set to appropriate using setMinMax() as soon as possible after construction. Similarly the control points should be set soon after construction.

◆ ~Segment()

Mercator::Segment::~Segment ( )

Destruct the Segment.

Generally it is not necessary to delete Segment objects from application code, as Segment instances are owned by the Terrain object. Storage allocated for heightfield and surface normals is implicitly deleted as well as all surfaces.

Member Function Documentation

◆ addArea()

int Mercator::Segment::addArea ( const Area ar)

Add an area to those that affect this segment.

Call from Terrain when an Area is added which is found to intersect this segment.

Parameters
arthe area to be added.
Returns
zero if the area was added, non-zero otherwise

◆ clearMods()

void Mercator::Segment::clearMods ( )

Delete all the modifications applied to this Segment.

Usually called from the destructor. It is not normally necessary to call this function from the application.

◆ clipToSegment()

bool Mercator::Segment::clipToSegment ( const WFMath::AxisBox< 2 > &  bbox,
int &  lx,
int &  hx,
int &  lz,
int &  hz 
) const

Determine the intersection between an axis aligned box and this segment.

Parameters
bboxaxis aligned box to be tested.
lxlower x coordinate of intersection area.
hxupper x coordinate of intersection area.
lzlower z coordinate of intersection area.
hzupper z coordinate of intersection area.
Returns
true if the box intersects with this Segment, false otherwise.

◆ getHeightAndNormal()

void Mercator::Segment::getHeightAndNormal ( float  x,
float  z,
float &  h,
WFMath::Vector< 3 > &  normal 
) const

Get an accurate height and normal vector at a given coordinate relative to this segment.

The height and surface normal are determined by finding the four adjacent height points nearest to the coordinate, and interpolating between those height values. The square area defined by the 4 height points is considered as two triangles for the purposes of interpolation to ensure that the calculated height falls on the surface rendered by a 3D graphics engine from the same heightfield data. The line used to divide the area is defined by the gradient y = x, so the first triangle has relative vertex coordinates (0,0) (1,0) (1,1) and the second triangle has vertex coordinates (0,0) (0,1) (1,1).

◆ invalidate()

void Mercator::Segment::invalidate ( bool  points = true)

Mark the contents of this Segment as stale.

This is called internally whenever changes occur that mean that the heightfield and surface normal data are no longer valid. If surface normal storage is deallocated, and if the points argument is true the heightfield storage is also deallocated.

◆ isValid()

bool Mercator::Segment::isValid ( ) const
inline

Check whether this Segment contains valid point data.

Returns
true if this Segment is valid, false otherwise.

◆ populate()

void Mercator::Segment::populate ( )

Populate the Segment with heightfield data.

Storage for the heightfield data is allocated if necessary, the qRMD algorithm is used to calculate the heightfield data, and required modifications are applied.

◆ populateNormals()

void Mercator::Segment::populateNormals ( )

Populate the Segment with surface normal data.

Storage for the normals is allocated if necessary, and the average normal at each heightpoint is calculated. The middle normals are calculated first, followed by the boundaries which are done in 2 dimensions to ensure that there is no visible seam between segments.

◆ populateSurfaces()

void Mercator::Segment::populateSurfaces ( )

Populate the surfaces associated with this Segment.

Call Surface::populate() for each Surface in turn.

◆ setCornerPoint()

void Mercator::Segment::setCornerPoint ( unsigned int  x,
unsigned int  z,
const BasePoint bp 
)
inline

Set the BasePoint data for one of the four that define this Segment.

Parameters
xrelative x coord of base point. Must be 0 or 1.
zrelative z coord of base point. Must be 0 or 1.
bpBasePoint data to be used.

The documentation for this class was generated from the following files: