gwnavruntime/collision/heightfield.h Source File

heightfield.h
Go to the documentation of this file.
1 /*
2 * Copyright 2015 Autodesk, Inc. All rights reserved.
3 * Use of this software is subject to the terms of the Autodesk license agreement and any attachments or Appendices thereto provided at the time of installation or download,
4 * or which otherwise accompanies this software in either electronic or hard copy form, or which is signed by you and accepted by Autodesk.
5 */
6 
7 #ifndef Navigation_HeightField_H
8 #define Navigation_HeightField_H
9 
17 
18 namespace Kaim
19 {
20 
21 class World;
22 class FileOpenerBase;
23 class IndexedMesh;
24 class HeightFieldBlob;
25 
26 typedef Vec2i HeightfieldTilePos;
28 
31 
32 struct HeightFieldConfig
33 {
34  HeightFieldConfig() : m_xAltitudeCount(0), m_yAltitudeCount(0), m_tileSize(0.f) {}
35 
36  KyUInt32 m_xAltitudeCount;
37  KyUInt32 m_yAltitudeCount;
38  KyFloat32 m_tileSize;
39  Vec2f m_origin;
40 };
41 
47 
48 class HeightField : public RefCountBase<HeightField, MemStat_CollisionData>
49 {
50  KY_CLASS_WITHOUT_COPY(HeightField)
51 public:
52  HeightField() : m_blob(KY_NULL), m_altitudes(KY_NULL), m_tileSizeInv(1.f) {}
53  ~HeightField() { Clear(); }
54 
55  KyResult Initialize(const HeightFieldConfig& config);
56  KyResult InitFromBlob(HeightFieldBlob* blob);
57  KyResult InitFromBlobHandler(Ptr<BlobHandler<HeightFieldBlob> > handler);
58  void Clear();
59 
60  // RayCast on this heightfield. Returns RayHit if any triangles are intersected.
61  CollisionRayCastResult RayCast(const Vec3f& startPoint, const Vec3f& endPoint) const;
62 
63  KY_INLINE KyUInt32 GetXTileCount() const { return m_tileBox.CountX(); }
64  KY_INLINE KyUInt32 GetYTileCount() const { return m_tileBox.CountY(); }
65  KY_INLINE KyUInt32 GetTileCount() const { return GetTileCount() * GetYTileCount(); }
66 
67  KY_INLINE KyUInt32 GetXAltitudeCount() const { return m_vertexBox.CountX(); }
68  KY_INLINE KyUInt32 GetYAltitudeCount() const { return m_vertexBox.CountY(); }
69  KY_INLINE KyUInt32 GetAltitudeCount() const { return GetTileCount() * GetYTileCount(); }
70 
71  KY_INLINE KyFloat32 GetXExtents() const { return GetXTileCount() * m_blob->m_tileSize; }
72  KY_INLINE KyFloat32 GetYExtents() const { return GetYTileCount() * m_blob->m_tileSize; }
73 
74  KY_INLINE void SetAltitude(const HeightfieldVertexPos& pixelPos, KyFloat32 altitude)
75  {
76  m_altitudes[m_vertexBox.GetRowMajorIndex(pixelPos)] = altitude;
77  }
78 
79  KY_INLINE Vec3f GetVertex(const HeightfieldVertexPos& vertexPos) const
80  {
81  const KyFloat32 altitude = GetAltitude(vertexPos);
82  return m_blob->m_origin + Vec3f((KyFloat32)vertexPos.x * m_blob->m_tileSize, (KyFloat32)vertexPos.y * m_blob->m_tileSize, altitude);
83  }
84 
85  KY_INLINE KyResult GetTileAtPos(const Vec3f& pos, HeightFieldTile& tile) const
86  {
87  // Transform pos, snap it to grid.
88  const Vec3f localPos = pos - m_blob->m_origin;
89  const CellPos cellPos(SnapFloat(localPos.x), SnapFloat(localPos.y));
90  return GetTileAtPos(cellPos, tile);
91  }
92 
93  KY_INLINE KyResult GetTileAtPos(const HeightfieldTilePos& pixelPos, HeightFieldTile& tile) const
94  {
95  if (m_tileBox.IsInside(pixelPos) == false)
96  return KY_ERROR;
97 
98  const KyFloat32 southEastX = ((KyFloat32)pixelPos.x * m_blob->m_tileSize) + m_blob->m_origin.x + m_blob->m_tileSize;
99  const KyFloat32 southEastY = ((KyFloat32)pixelPos.y * m_blob->m_tileSize) + m_blob->m_origin.y;
100 
101  tile.m_vertices[0].x = southEastX;
102  tile.m_vertices[0].y = southEastY;
103  tile.m_vertices[0].z = GetAltitude(pixelPos.NeighborEast());
104 
105  tile.m_vertices[1].x = southEastX;
106  tile.m_vertices[1].y = southEastY + m_blob->m_tileSize;
107  tile.m_vertices[1].z = GetAltitude(pixelPos.NeighborNorthEast());
108 
109  tile.m_vertices[2].x = southEastX - m_blob->m_tileSize;
110  tile.m_vertices[2].y = southEastY + m_blob->m_tileSize;
111  tile.m_vertices[2].z = GetAltitude(pixelPos.NeighborNorth());
112 
113  tile.m_vertices[3].x = southEastX - m_blob->m_tileSize;
114  tile.m_vertices[3].y = southEastY;
115  tile.m_vertices[3].z = GetAltitude(pixelPos);
116 
117  tile.m_cellPos = pixelPos;
118 
119  return KY_SUCCESS;
120  }
121 
122  Box2f GetAABB2D() const
123  {
124  return Box2f(m_blob->m_origin.Get2d(), m_blob->m_origin.Get2d() +
125  Vec2f(m_blob->m_xAltitudeCount * m_blob->m_tileSize, m_blob->m_yAltitudeCount * m_blob->m_tileSize));
126  }
127 
128  void VisualDebug(World* world);
129 
130  KyResult ConvertToIndexedMesh(IndexedMesh& mesh) const;
131 
132  KyResult WriteToObj(File* file) const;
133 
134  BlobHandler<HeightFieldBlob>* GetBlobHandler() { return m_blobHandler; }
135 
136 private:
137 
138  KY_INLINE KyFloat32 GetAltitude(const HeightfieldVertexPos& vertexPos) const
139  {
140  return m_altitudes[m_vertexBox.GetRowMajorIndex(vertexPos)];
141  }
142 
143  KyResult GetStartTile(const Vec3f& startPoint, const Vec3f& endPoint, HeightFieldTile& tile) const;
144  KyResult GetNextTile(const HeightFieldTile& tile, const Vec2f& rayA, const Vec2f& rayB, HeightFieldTile& outTile, const HeightFieldTile* destTile = KY_NULL) const;
145  CollisionRayCastResult RayVsTriangle(const Triangle3f& tri, const Vec3f& from, const Vec3f& to) const;
146 
147  KY_INLINE KyUInt32 SnapFloat(KyFloat32 inputValue) const
148  {
149  return KyUInt32(inputValue * m_tileSizeInv);
150  }
151 
152  KY_INLINE Vec3f Interpolate(const Vec3f& v0, const Vec3f& v1, KyFloat32 rt) const
153  {
154  const KyFloat32 s = 1.0f - rt;
155  return Vec3f(s * v0[0] + rt * v1[0],
156  s * v0[1] + rt * v1[1],
157  s * v0[2] + rt * v1[2]);
158  }
159 
160  void OnSetBlob();
161 
162  Ptr<BlobHandler<HeightFieldBlob> > m_blobHandler;
163  HeightFieldBlob* m_blob;
164  KyFloat32* m_altitudes; // Shortcut
165  KyFloat32 m_tileSizeInv;
166  HeightfieldTileBox m_tileBox;
167  HeightfieldVertexBox m_vertexBox;
168  Vec3f m_verts[4];
169 };
170 
171 }
172 
173 #endif // Navigation_HeightField_H
This class represents a two-dimensional axis-aligned bounding box whose dimensions are stored using 3...
Definition: box2i.h:119
KyInt32 KyResult
Defines a type that can be returned by methods or functions in the Gameware Navigation SDK to indicat...
Definition: types.h:254
Vec2i CellPos
A type that represents the position of a cell within a 2D grid.
Definition: navmeshtypes.h:33
Box2i HeightfieldTileBox
A type that represents a bounding box around the Tiles of the Heigtfield.
Definition: heightfield.h:27
Box2i HeightfieldVertexBox
A type that represents a bounding box around the Vertices of the Heighfield.
Definition: heightfield.h:30
#define KY_NULL
Null value.
Definition: types.h:247
Vec2i HeightfieldVertexPos
A type that represents one of the vertices of a Tile within a 2D grid.
Definition: heightfield.h:29
#define KY_CLASS_WITHOUT_COPY(ClassName)
Define to forbid copy constructor and copy assignment.
Definition: types.h:387
Vec2i HeightfieldTilePos
A type that represents the position of a Tile within a 2D grid.
Definition: heightfield.h:24
KyInt32 GetRowMajorIndex(const Vec2i &pos) const
Retrieves the index of the grid cell at the specified (X,Y) position, counting cells row-by-row...
Definition: box2i.h:297
This class defines a two-dimensional vector whose coordinates are stored using floating-point numbers...
Definition: vec2f.h:24
Heightfield with a uniform grid of sampled altitudes.
Definition: heightfield.h:51
This class defines a two-dimensional vector whose coordinates are stored using 32-bit integers...
Definition: vec2i.h:26
Definition: gamekitcrowddispersion.h:20
#define KY_SUCCESS
Shorthand for Kaim::Result::Success.
Definition: types.h:273
KyInt32 CountY() const
Retrieves the number of grid cells contained within this box along its Y axis.
Definition: box2i.h:210
#define KY_ERROR
Shorthand for Kaim::Result::Failure.
Definition: types.h:272
bool IsInside(const Vec2i &pos) const
Returns true if the specified position is contained within the extents of the bounding box or if the ...
Definition: box2i.h:214
KyInt32 CountX() const
Retrieves the number of grid cells contained within this box along its X axis.
Definition: box2i.h:207
unsigned int KyUInt32
Type used internally to represent an unsigned 32-bit integer.
Definition: types.h:36
float KyFloat32
Type used internally to represent a 32-bit floating-point number.
Definition: types.h:43