mercator  0.4.0
A terrain generation library for the Worldforge system.
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>

Classes

struct  AreaEntry
 

Public Types

typedef std::map< int, std::unique_ptr< Surface > > Surfacestore
 STL map of pointers to Surface objects. More...
 
typedef std::multimap< int, AreaEntryAreastore
 STL multimap of pointers to Area objects affecting this segment. More...
 

Public Member Functions

 Segment (int x, int z, 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. More...
 
int getSize () const
 Accessor for array size of this segment. More...
 
int getXRef () const
 Accessor for Global x reference of this segment. More...
 
int getZRef () const
 Accessor for Global y reference of this segment. More...
 
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. More...
 
Matrix< 2, 2, BasePoint > & getControlPoints ()
 Accessor for modifying 2D matrix of base points. More...
 
const SurfacestoregetSurfaces () const
 Accessor for list of attached Surface objects. More...
 
SurfacestoregetSurfaces ()
 Accessor for modifying list of attached Surface objects. More...
 
const float * getPoints () const
 Accessor for buffer containing height points. More...
 
float * getPoints ()
 Accessor for write access to buffer containing height points. More...
 
const HeightMapgetHeightMap () const
 Accessor for height map. More...
 
HeightMapgetHeightMap ()
 Accessor for write access to height map. More...
 
const float * getNormals () const
 Accessor for buffer containing surface normals. More...
 
float * getNormals ()
 Accessor for write access to buffer containing surface normals. More...
 
float get (int x, int z) const
 Get the height at a relative integer position in the Segment. More...
 
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. More...
 
float getMin () const
 Accessor for the minimum height value in this Segment. More...
 
WFMath::AxisBox< 2 > getRect () const
 The 2d area covered by this segment. More...
 
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. More...
 
const std::map< long, const TerrainMod * > & getMods () const
 
void updateArea (long id, const Area *area, const Shader *shader)
 

Detailed Description

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

Definition at line 37 of file Segment.h.

Member Typedef Documentation

◆ Areastore

typedef std::multimap<int, AreaEntry> Mercator::Segment::Areastore

STL multimap of pointers to Area objects affecting this segment.

Definition at line 48 of file Segment.h.

◆ Surfacestore

typedef std::map<int, std::unique_ptr<Surface> > Mercator::Segment::Surfacestore

STL map of pointers to Surface objects.

Definition at line 40 of file Segment.h.

Constructor & Destructor Documentation

◆ Segment()

Mercator::Segment::Segment ( int  x,
int  z,
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.

Definition at line 37 of file Segment.cpp.

◆ ~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.

Definition at line 50 of file Segment.cpp.

References clearMods().

Member Function Documentation

◆ 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.

Definition at line 274 of file Segment.cpp.

References invalidate().

Referenced by ~Segment().

◆ 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.

Definition at line 238 of file Segment.cpp.

◆ get()

float Mercator::Segment::get ( int  x,
int  z 
) const
inline

Get the height at a relative integer position in the Segment.

Definition at line 173 of file Segment.h.

References Mercator::HeightMap::get().

Referenced by Mercator::Terrain::get(), populateNormals(), and Mercator::GrassShader::shade().

◆ getAreas()

const Areastore& Mercator::Segment::getAreas ( ) const
inline

Accessor for multimap of Area objects.

Definition at line 201 of file Segment.h.

Referenced by Mercator::AreaShader::checkIntersect(), and Mercator::AreaShader::shade().

◆ getControlPoints() [1/2]

Matrix<2, 2, BasePoint>& Mercator::Segment::getControlPoints ( )
inline

Accessor for modifying 2D matrix of base points.

Definition at line 128 of file Segment.h.

◆ getControlPoints() [2/2]

const Matrix<2, 2, BasePoint>& Mercator::Segment::getControlPoints ( ) const
inline

Accessor for 2D matrix of base points.

Definition at line 123 of file Segment.h.

◆ 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).

Definition at line 223 of file Segment.cpp.

References Mercator::HeightMap::getHeightAndNormal().

Referenced by Mercator::Terrain::getHeightAndNormal().

◆ getHeightMap() [1/2]

HeightMap& Mercator::Segment::getHeightMap ( )
inline

Accessor for write access to height map.

Definition at line 158 of file Segment.h.

◆ getHeightMap() [2/2]

const HeightMap& Mercator::Segment::getHeightMap ( ) const
inline

Accessor for height map.

Definition at line 153 of file Segment.h.

◆ getMax()

float Mercator::Segment::getMax ( ) const
inline

Accessor for the maximum height value in this Segment.

Definition at line 189 of file Segment.h.

References Mercator::HeightMap::getMax().

Referenced by Mercator::GrassShader::checkIntersect(), Mercator::HighShader::checkIntersect(), and Mercator::BandShader::checkIntersect().

◆ getMin()

float Mercator::Segment::getMin ( ) const
inline

◆ getNormals() [1/2]

float* Mercator::Segment::getNormals ( )
inline

Accessor for write access to buffer containing surface normals.

Definition at line 168 of file Segment.h.

◆ getNormals() [2/2]

const float* Mercator::Segment::getNormals ( ) const
inline

Accessor for buffer containing surface normals.

Definition at line 163 of file Segment.h.

◆ getPoints() [1/2]

float* Mercator::Segment::getPoints ( )
inline

Accessor for write access to buffer containing height points.

Definition at line 148 of file Segment.h.

References Mercator::Buffer< DataType >::getData().

◆ getPoints() [2/2]

const float* Mercator::Segment::getPoints ( ) const
inline

◆ getRect()

WFMath::AxisBox< 2 > Mercator::Segment::getRect ( ) const

The 2d area covered by this segment.

Definition at line 337 of file Segment.cpp.

Referenced by Mercator::Area::checkIntersects(), and Mercator::Area::clipToSegment().

◆ getResolution()

int Mercator::Segment::getResolution ( ) const
inline

Accessor for resolution of this segment.

Definition at line 83 of file Segment.h.

Referenced by Mercator::GrassShader::shade().

◆ getSize()

int Mercator::Segment::getSize ( ) const
inline

◆ getSurfaces() [1/2]

Surfacestore& Mercator::Segment::getSurfaces ( )
inline

Accessor for modifying list of attached Surface objects.

Definition at line 138 of file Segment.h.

◆ getSurfaces() [2/2]

const Surfacestore& Mercator::Segment::getSurfaces ( ) const
inline

Accessor for list of attached Surface objects.

Definition at line 133 of file Segment.h.

◆ getXRef()

int Mercator::Segment::getXRef ( ) const
inline

Accessor for Global x reference of this segment.

Definition at line 93 of file Segment.h.

Referenced by Mercator::Terrain::get(), Mercator::Terrain::getHeight(), and Mercator::Terrain::getHeightAndNormal().

◆ getZRef()

int Mercator::Segment::getZRef ( ) const
inline

Accessor for Global y reference of this segment.

Definition at line 98 of file Segment.h.

Referenced by Mercator::Terrain::get(), Mercator::Terrain::getHeight(), and Mercator::Terrain::getHeightAndNormal().

◆ 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.

Definition at line 83 of file Segment.cpp.

References Mercator::Buffer< DataType >::invalidate().

Referenced by clearMods(), and setCornerPoint().

◆ isValid()

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

Check whether this Segment contains valid point data.

Returns
true if this Segment is valid, false otherwise.

Definition at line 105 of file Segment.h.

References Mercator::Buffer< DataType >::isValid().

Referenced by Mercator::Terrain::get(), Mercator::Terrain::getHeight(), and Mercator::Terrain::getHeightAndNormal().

◆ 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.

Definition at line 60 of file Segment.cpp.

References Mercator::Buffer< DataType >::allocate().

◆ 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.

Definition at line 111 of file Segment.cpp.

References Mercator::HeightMap::get(), get(), and Mercator::Buffer< DataType >::isValid().

◆ populateSurfaces()

void Mercator::Segment::populateSurfaces ( )

Populate the surfaces associated with this Segment.

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

Definition at line 197 of file Segment.cpp.

◆ 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.

Definition at line 117 of file Segment.h.

References invalidate().

Referenced by Mercator::Terrain::setBasePoint().


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