AGX Dynamics 2.38.0.2
Loading...
Searching...
No Matches
Common.h
Go to the documentation of this file.
1/*
2Copyright 2007-2024. 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#ifndef AGX_HIERARCHICAL_GRID_COMMON_H
18#define AGX_HIERARCHICAL_GRID_COMMON_H
19
20#include <agx/Vec2.h>
21
22#include <agx/HashTable.h>
27
28#include <agxData/LocalVector.h>
29
30#include "Util.h"
31#include "GridTables.h"
32
33#define POST_FILTER_OBB_TESTS 1
34
35#define GRID_SUBSYSTEM_GEOMETRY 0
36#define GRID_SUBSYSTEM_PARTICLE 1
37#define GRID_NUM_SUBSYSTEMS 2
38
39namespace agx
40{
41
48
50
52 {
54 AGX_FORCE_INLINE LocalOverlap(UInt32 object1In, UInt32 object2In) { this->set(object1In, object2In); }
55 AGX_FORCE_INLINE void set(UInt32 object1In, UInt32 object2In) { this->object1 = object1In; this->object2 = object2In; }
56
59 };
60
62
64
66 {
67 inline CellSortEntry() {}
68 inline CellSortEntry(UInt64 _linearId, UInt8 _tier, UInt32 _cellIndex) : linearId(_linearId), cellIndex(_cellIndex), tier(_tier) {}
69
73 };
74
76 {
77 // Tier as primary sort key, use linearId if same tier
78 return p1.tier == p2.tier ? (p1.linearId < p2.linearId) : (p1.tier < p2.tier);
79 }
80
81
83 {
84 static Vec3 cornerOffsets[4] = {
85 Vec3(-radius, radius, 0.0 ), Vec3( radius, radius, 0),
86 Vec3( radius, -radius, 0.0 ), Vec3(-radius, -radius, 0)};
87 return cornerOffsets[corner];
88 }
89
90 template< typename CellIdT>
91 AGX_FORCE_INLINE CellIdT getCornerId( size_t /*corner*/, Real32 /*radius*/, Vec3f /*centerPosition*/, Real32 /*invCellSize*/ )
92 {
93 agxAbortN( "Need getCornerId template specialization for %s.", typeid(CellIdT).name() );
94 return CellIdT();
95 }
96
97 template<>
98 AGX_FORCE_INLINE Vec2i getCornerId( size_t corner, Real32 radius, Vec3f centerPosition, Real32 invCellSize )
99 {
100 Vec3f cornerPosition = centerPosition + (Vec3f)cornerOffsets2D( radius, corner );
101 return agx::calculateCellId( cornerPosition.x(), cornerPosition.y(), invCellSize );
102 }
103
105
106 // typedef HashTable< Vec3i, agx::UInt32 > GridCellTable;
107 // typedef HashTable< Vec2i, agx::UInt32 > GridCell2DTable;
108
111
114
116
117 // Special wrappers due to 2d implementation using 3d particle positions
118 AGX_FORCE_INLINE void calculateCellId(const Vec3& pos, Real invCellSize, Vec3i& id)
119 {
120 id = agx::calculateCellId(pos, invCellSize);
121 }
122
123 AGX_FORCE_INLINE void calculateCellId(const Vec3& pos, Real invCellSize, Vec2i& id)
124 {
125 id = agx::calculateCellId(pos[0], pos[1], invCellSize);
126 }
127
129
131 {
132 return (cellId[0] & 0x1) | ((cellId[1] & 0x1) << 1) | ((cellId[2] & 0x1) << 2);
133 }
134
136 {
137 return (cellId[0] & 0x1) | ((cellId[1] & 0x1) << 1);
138 }
139
140
142
144 {
145 return (UInt(cellId[0] >= 0) & 0x1) | ((UInt(cellId[1] >= 0) & 0x1) << 1) | ((UInt(cellId[2] >= 0) & 0x1) << 2);
146 }
147
149 {
150 return (UInt(cellId[0] >= 0) & 0x1) | ((UInt(cellId[1] >= 0) & 0x1) << 1);
151 }
152
154
156 {
157 Vec3i result = cellId * 2;
158 if (childIndex & 0x1) result[0]++;
159 if (childIndex & 0x2) result[1]++;
160 if (childIndex & 0x4) result[2]++;
161 return result;
162 }
163
165 {
166 Vec2i result = cellId * 2;
167 if (childIndex & 0x1) result[0]++;
168 if (childIndex & 0x2) result[1]++;
169 return result;
170 }
171
173
175 {
176 // TODO Use offset to make sure id becomes positive, some power of two (bit shift) based on tier id
177 return cellId;
178 }
179
181 {
182 // TODO Use offset to make sure id becomes positive, some power of two (bit shift) based on tier id
183 return cellId;
184 }
185
187
189 {
190 Vec3i parentId = cellId / 2;
191
192 if (cellId[0] < 0 && (cellId[0] & 0x1)) parentId[0]--;
193 if (cellId[1] < 0 && (cellId[1] & 0x1)) parentId[1]--;
194 if (cellId[2] < 0 && (cellId[2] & 0x1)) parentId[2]--;
195
196 return parentId;
197 }
198
200 {
201 Vec2i parentId = cellId / 2;
202
203 if (cellId[0] < 0 && (cellId[0] & 0x1)) parentId[0]--;
204 if (cellId[1] < 0 && (cellId[1] & 0x1)) parentId[1]--;
205
206 return parentId;
207 }
208
210
216 AGX_FORCE_INLINE UInt8 calculateTier( Real minCellSize, const agxData::Array<Real>& gridTier_size )
217 {
218 size_t numTiers = gridTier_size.size();
219 if ( numTiers == 0 )
220 return InvalidIndex;
221
222 UInt8 tier;
223 #if 0
224 // Experimental log2 way of finding the correct tier. Might be slower than
225 // the short loop below and I'm worried about precision problems. Also, if
226 // the required minimum cell size is larger than the currently largest
227 // tier, then this code will (probably) return the smallest (numTiers-1)
228 // tier.
229 // Don't think we should use this. Comments?
230 tier = (UInt8)( log2(largestCellSize) - log2(minCellSize) );
231 if ( tier >= numTiers )
232 tier = numTiers-1;
233 #else
234 // Safe loop for finding the correct tier. Searches down the hierarchy.
235 // May produce the largest tier even if even it is too small. Also,
236 // will produce the smallest tier even if the object really should be
237 // in a even smaller tier.
238 for ( tier = 0; tier < numTiers-1; ++tier )
239 {
240 // Test if the NEXT tier would be too small.
241 if ( gridTier_size[ tier+1 ] < minCellSize )
242 break;
243 }
244 #endif
245
246 // Make sure we found a tier that isn't too small.
247 if ( minCellSize > gridTier_size[ tier ] )
248 tier = InvalidIndex;
249
250 // Make sure the object shouldn't fit better in a even smaller tier.
251 // NOTE: Important that there is only one valid tier for each requested cell size.
252 // We must therefore use '<=' instead of '<' to prevent ambiguous tier assignments.
253 else if ( Real(2) * minCellSize <= gridTier_size[ tier ] )
254 tier = InvalidIndex;
255
256 return tier;
257 }
258}
259
260
261#endif /* AGX_HIERARCHICAL_GRID_COMMON_H */
Only use as member allocated variable.
Definition Value.h:285
Inheritance with partial specialization due to bug with ref_ptr containers.
#define agxAbortN(format,...)
Definition debug.h:166
#define AGX_FORCE_INLINE
Definition macros.h:58
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
Vec3 cornerOffsets2D(Real32 radius, size_t corner)
Definition Common.h:82
uint32_t UInt32
Definition Integer.h:32
Vec3i calculateChildCellId(const Vec3i &cellId, UInt8 childIndex)
Definition Common.h:155
Vec3T< Real > Vec3
The object holding 3 dimensional vectors and providing basic arithmetic.
Definition agx/Vec3.h:36
Int32 log2(size_t val)
Definition Math.h:393
agx::HashTable< Vec3i, agx::Physics::HierarchicalGrid::ContactZonePtr > ContactZoneTable
Definition Common.h:112
Vec3i calculateNonNegativeId(const Vec3i &cellId, UInt8)
Definition Common.h:174
uint64_t UInt64
Definition Integer.h:33
agx::HashTable< UInt64, agx::Physics::HierarchicalGrid::ContactZoneDependencyPtr > ZoneDependencyTable
Definition Common.h:113
CellTable< Vec2i > GridCell2DTable
Definition Common.h:110
agxData::LocalVector< LocalOverlap > LocalOverlapVector
Definition Common.h:61
uint64_t UInt
Definition Integer.h:27
CellTable< Vec3i > GridCellTable
Definition Common.h:109
CellState
Definition Common.h:43
@ CELL_UNINITIALIZED
Definition Common.h:45
@ CELL_ACTIVE
Definition Common.h:46
@ CELL_DEAD
Definition Common.h:44
double Real
Definition Real.h:42
UInt calculateRootCellIndex(const Vec3i &cellId)
Definition Common.h:143
uint8_t UInt8
Definition Integer.h:30
float Real32
Definition Real.h:44
CellIdT getCornerId(size_t, Real32, Vec3f, Real32)
Definition Common.h:91
bool compareCellSortEntries(const CellSortEntry &p1, const CellSortEntry &p2)
Definition Common.h:75
Vec2i calculateCellId(Real x, Real y, Real invCellSize)
Definition Vec2.h:72
AGXCORE_EXPORT const InvalidIndexStruct InvalidIndex
UInt calculateCellChildIndex(const Vec3i &cellId)
Definition Common.h:130
Vec3T< Real32 > Vec3f
Definition agx/Vec3.h:39
Vec3i calculateParentCellId(const Vec3i &cellId)
Definition Common.h:188
UInt8 calculateTier(Real minCellSize, const agxData::Array< Real > &gridTier_size)
Calculates the tier where the given object size will fit.
Definition Common.h:216
UInt64 linearId
Definition Common.h:70
UInt32 cellIndex
Definition Common.h:71
CellSortEntry(UInt64 _linearId, UInt8 _tier, UInt32 _cellIndex)
Definition Common.h:68
LocalOverlap(UInt32 object1In, UInt32 object2In)
Definition Common.h:54
UInt32 object1
Definition Common.h:57
UInt32 object2
Definition Common.h:58
void set(UInt32 object1In, UInt32 object2In)
Definition Common.h:55