AGX Dynamics 2.42.1.1
Loading...
Searching...
No Matches
VoxelGridUtils.h
Go to the documentation of this file.
1/*
2Copyright 2007-2025. Algoryx Simulation AB.
3
4All AGX source code, intellectual property, documentation, sample code,
5tutorials, scene files and technical white papers, are copyrighted, proprietary
6and confidential material of Algoryx Simulation AB. You may not download, read,
7store, distribute, publish, copy or otherwise disseminate, use or expose this
8material unless having a written signed agreement with Algoryx Simulation AB, or having been
9advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10valid commercial license from Algoryx Simulation AB.
11
12Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13from using this software, unless otherwise stated in written agreements with
14Algoryx Simulation AB.
15*/
16
17#pragma once
18
19#include <agx/Vec3.h>
22
23namespace agxTerrain
24{
25 namespace VoxelGridUtils
26 {
27 /*
28 This namespace deals with position-index conversions INSIDE the voxel grid.
29 A position in the grid, aka grid position or voxel space position, is not the
30 same as a local position in the terrain, which has it origo in the middle of
31 the terrain. The voxel space origo is situated in the "bottomleft" corner of
32 the terrain, coinciding with the terrain index (0, 0).
33 */
34
35 /*
36 \param gridPosition - The position in voxel space.
37 \param voxelSize - The side length of a voxel in the grid.
38 \return the voxel index of the specified voxel space position.
39 */
41 {
42 gridPosition *= (1.0 / voxelSize);
43 return agx::Vec3i( (agx::Int)std::floor(gridPosition.x() + 0.5),
44 (agx::Int)std::floor(gridPosition.y() + 0.5),
45 (agx::Int)std::floor(gridPosition.z() + 0.5) );
46 }
47
48 /*
49 \param voxelIndex - the index of a voxel in grid.
50 \param voxelSize - The side length of a voxel in the grid.
51 \return the center position of the specified voxel in voxel space coordinates.
52 */
54 {
55 return agx::Vec3( static_cast<agx::Real>((voxelIndex.x()))* voxelSize,
56 static_cast<agx::Real>((voxelIndex.y()))* voxelSize,
57 static_cast<agx::Real>((voxelIndex.z()))* voxelSize );
58 }
59
67 const agx::AffineMatrix4x4& gridInvTransform,
68 agx::Real extend = agx::Real( 0 ) )
69 {
70 return agxCollide::BoundingAABB{ agx::Bound3{ bound.min() - agx::Vec3( extend ),
71 bound.max() + agx::Vec3( extend ) },
72 gridInvTransform };
73 }
74
75 inline void renderVoxel(
76 const agx::Vec3i voxel, const agxRender::Color& color, agx::Real voxelSize,
77 const agx::AffineMatrix4x4& gridTransform, agx::Real size)
78 {
79 agx::Vec3 voxelCenter = getGridPositionFromVoxelIndex(voxel, voxelSize);
80
81 // Find eight other corner points on the voxel and test them.
82 agx::Real h = voxelSize * 0.5;
83 agx::Vec3 corners[8];
84 // Bottom
85 corners[0] = voxelCenter + agx::Vec3(-h, -h, -h);
86 corners[1] = voxelCenter + agx::Vec3(+h, -h, -h);
87 corners[2] = voxelCenter + agx::Vec3(-h, +h, -h);
88 corners[3] = voxelCenter + agx::Vec3(+h, +h, -h);
89 // Top
90 corners[4] = voxelCenter + agx::Vec3(-h, -h, +h);
91 corners[5] = voxelCenter + agx::Vec3(+h, -h, +h);
92 corners[6] = voxelCenter + agx::Vec3(-h, +h, +h);
93 corners[7] = voxelCenter + agx::Vec3(+h, +h, +h);
94
95 static const int edges[12][2] = {
96 // Bottom square
97 {0, 1},
98 {1, 3},
99 {3, 2},
100 {2, 0},
101
102 // Top square
103 {4, 5},
104 {5, 7},
105 {7, 6},
106 {6, 4},
107
108 // Vertical connections
109 {0, 4},
110 {1, 5},
111 {2, 6},
112 {3, 7}
113 };
114
115 for (int e = 0; e < 12; ++e) {
116 int a = edges[e][0];
117 int b = edges[e][1];
118
119 // Put line in world coordinates
120 const agx::Vec3& p0 = corners[a] * gridTransform;
121 const agx::Vec3& p1 = corners[b] * gridTransform;
122
123 agxRender::RenderSingleton::instance()->add(p0, p1, size, color);
124 }
125 }
126
127 inline void renderVoxels(
128 const agx::Vec3iVector& voxels, const agxRender::Color& color, agx::Real voxelSize,
129 const agx::AffineMatrix4x4& gridTransform, agx::Real size)
130 {
131 for (const auto& v : voxels)
132 renderVoxel(v, color, voxelSize, gridTransform, size);
133 }
134 }
135}
Axis aligned bounding box implementation.
Definition: BoundingAABB.h:38
Utility color class with "common colors".
Definition: Color.h:48
static RenderSingleton * instance()
void add(const Point &point)
Add temporary point (sphere).
const T & min() const
Definition: Bound.h:252
const T & max() const
Definition: Bound.h:255
Vector containing 'raw' data.
Definition: agx/Vector.h:246
agx::Vec3 getGridPositionFromVoxelIndex(agx::Vec3i voxelIndex, agx::Real voxelSize)
agx::Vec3i getVoxelIndexFromGridPosition(agx::Vec3 gridPosition, agx::Real voxelSize)
void renderVoxels(const agx::Vec3iVector &voxels, const agxRender::Color &color, agx::Real voxelSize, const agx::AffineMatrix4x4 &gridTransform, agx::Real size)
agx::Bound3 transformBound(const agx::Bound3 &bound, const agx::AffineMatrix4x4 &gridInvTransform, agx::Real extend=agx::Real(0))
Transforms a world bound to grid given grid transform and additional/extended size.
void renderVoxel(const agx::Vec3i voxel, const agxRender::Color &color, agx::Real voxelSize, const agx::AffineMatrix4x4 &gridTransform, agx::Real size)
The agxTerrain namespace contains a 3D model for a dynamic deformable Terrain and related classes.
Definition: Geometry.h:59
Vec3T< Real > Vec3
The object holding 3 dimensional vectors and providing basic arithmetic.
Definition: agx/Vec3.h:36
Vec3T< Int > Vec3i
Definition: agx/Vec3.h:43
double Real
Definition: Real.h:41
int64_t Int
Definition: Integer.h:28