VR-Vantage
Loading...
Searching...
No Matches
DtOctree.inl
Go to the documentation of this file.
1/******************************************************************************
2** Copyright (c) 2023 MAK Technologies, Inc.
3** All rights reserved.
4******************************************************************************/
5
9#pragma once
10
12#include <vrvMath/DtCamera.h>
13#include <vrvMath/DtCommonUtils.h> // for SAFE_DELETE
14
15namespace makVrv {
16
19 template<class T>
21 {
22 // infinite boxes never fit in a child - always root node
23 if (box.isInfinite())
24 return false;
25
26 DtVector3<T> halfm_BoxSize = myBox.halfSize();
27 DtVector3<T> boxSize = box.size();
28 return ((boxSize.x <= halfm_BoxSize.x)
29 && (boxSize.y <= halfm_BoxSize.y)
30 && (boxSize.z <= halfm_BoxSize.z));
31
32 }
33
38 template<class T>
40 , int *x, int *y, int *z) const
41 {
42 DtVector3<T> max = myBox.max();
43 DtVector3<T> min = box.min();
44
45 DtVector3<T> center = myBox.max().midPoint(myBox.min());
46
47 DtVector3<T> ncenter = box.max().midPoint(box.min());
48
49 if (ncenter.x > center.x)
50 * x = 1;
51 else
52 *x = 0;
53
54 if (ncenter.y > center.y)
55 * y = 1;
56 else
57 *y = 0;
58
59 if (ncenter.z > center.z)
60 * z = 1;
61 else
62 *z = 0;
63
64 }
65
66 template<class T>
67 DtOctree<T>::DtOctree(DtOctree * parent, DtOctree* treeRoot, size_t maxDepth)
68 : myHalfSize(0, 0, 0)
69 {
70 //initialize all children to null.
71 for (int i = 0; i < 2; i++)
72 {
73 for (int j = 0; j < 2; j++)
74 {
75 for (int k = 0; k < 2; k++)
76 {
77 myChildren[i][j][k] = 0;
78 }
79 }
80 }
81
82 if (parent == 0 && treeRoot == 0)
83 {
84 myOctreeRoot = this;
85 }
86 else
87 {
88 ASSERT_PREDICATE(parent && treeRoot);
89 myOctreeRoot = treeRoot;
90 }
91
92 myMaxDepth = maxDepth;
93
94 myParent = parent;
95 myNumNodes = 0;
96 }
97
98 template<class T>
100 {
101 //initialize all children to null.
102 for (int i = 0; i < 2; i++)
103 {
104 for (int j = 0; j < 2; j++)
105 {
106 for (int k = 0; k < 2; k++)
107 {
108 if (myChildren[i][j][k] != 0)
109 SAFE_DELETE(myChildren[i][j][k]);
110 }
111 }
112 }
113 myParent = 0;
114 }
115 template<class T>
117 {
118 // switch to push_back for a vector..
119 myNodes.push_front(n);
120 n->setOctant(this);
121
122 //update total counts.
123 ref();
124
125 }
126
127 template<class T>
129 {
130 typename DtOctree<T>::NodeList::iterator iter = std::find(myNodes.begin(), myNodes.end(), n);
131 if (iter != myNodes.end()) {
132 myNodes.erase(iter);
133 }
134
135 n->setOctant(0);
136
137 //update total counts.
138 unref();
139 }
140
141 template<class T>
146
147 template<class T>
149 {
150 const DtAxisAlignedBoundingBox<T>& box = pOctreeNode->myWorldAABB;
151 if (box.isNull())
152 return;
153
154 // octree deleted (shutdown condition)
155 if (myOctreeRoot == 0)
156 return;
157
158 DtOctree<T> * container = pOctreeNode->getOctant();
159 if (container == 0)
160 {
161 //if outside the octree, force into the root node.
162 if (!pOctreeNode->isIn(myOctreeRoot->myBox))
163 myOctreeRoot->addNode(pOctreeNode);
164 else
165 addOctreeNode(pOctreeNode, myOctreeRoot);
166 return;
167 }
168
169 if (!pOctreeNode->isIn(container->myBox))
170 {
171 container->removeNode(pOctreeNode);
172 pOctreeNode->setOctant(NULL);
173
174 //if outside the octree, force into the root node.
175 if (!pOctreeNode->isIn(myOctreeRoot->myBox))
176 {
177 myOctreeRoot->addNode(pOctreeNode);
178 }
179 else
180 {
181 addOctreeNode(pOctreeNode, myOctreeRoot);
182 }
183 }
184 }
185
186 template<class T>
188 , DtOctree<T>* octant, size_t depth)
189 {
190 // octree deleted (shutdown condition)
191 if (myOctreeRoot == 0)
192 return;
193
194 const DtAxisAlignedBoundingBox<T>& box = pOctreeNode->myWorldAABB;
195
196 //if the octree is twice as big as the scene node,
197 //we will add it to a child.
198 if ((depth < myMaxDepth) && octant->isTwiceSize(box))
199 {
200 int x, y, z;
201 octant->getChildIndexes(box, &x, &y, &z);
202
203 if (octant->myChildren[x][y][z] == 0)
204 {
205 octant->myChildren[x][y][z]
206 = new DtOctree<T>(octant, myOctreeRoot, myMaxDepth);
207
208 const DtVector3<T>& octantMin = octant->myBox.min();
209 const DtVector3<T>& octantMax = octant->myBox.max();
210
211 DtVector3<T> min, max;
212
213 if (x == 0)
214 {
215 min.x = octantMin.x;
216 max.x = (octantMin.x + octantMax.x) / 2;
217 }
218
219 else
220 {
221 min.x = (octantMin.x + octantMax.x) / 2;
222 max.x = octantMax.x;
223 }
224
225 if (y == 0)
226 {
227 min.y = octantMin.y;
228 max.y = (octantMin.y + octantMax.y) / 2;
229 }
230
231 else
232 {
233 min.y = (octantMin.y + octantMax.y) / 2;
234 max.y = octantMax.y;
235 }
236
237 if (z == 0)
238 {
239 min.z = octantMin.z;
240 max.z = (octantMin.z + octantMax.z) / 2;
241 }
242
243 else
244 {
245 min.z = (octantMin.z + octantMax.z) / 2;
246 max.z = octantMax.z;
247 }
248
249 octant->myChildren[x][y][z]->myBox.setMinMax(min, max);
250 octant->myChildren[x][y][z]->myHalfSize = (max - min) / 2;
251 }
252
253 addOctreeNode(pOctreeNode, octant->myChildren[x][y][z], ++depth);
254 }
255 else
256 {
257 //std::cout<<"Node: "<<pOctreeNode<<" Added at level: "<<depth<<std::endl;
258 octant->addNode(pOctreeNode);
259 }
260 }
261 template<class T>
263 {
264 // octree deleted (shutdown condition)
265 if (myOctreeRoot == 0)
266 return;
267
268 DtOctree<T> * oct = pOctreeNode->getOctant();
269
270 if (oct)
271 {
272 oct->removeNode(pOctreeNode);
273 }
274
275 pOctreeNode->setOctant(0);
276 }
277
278 template<class T>
280 , DtOctree<T>* octant, const DtCamera<T>* camera, bool foundvisible) const
281 {
282 //return immediately if nothing is in the node.
283 if (octant->numNodes() == 0)
284 return;
285
287
288 if (foundvisible)
289 {
290 v = VISIBILITY_FULL;
291 }
292
293 else if (octant == myOctreeRoot)
294 {
296 }
297
298 else
299 {
301 octant->getCullBounds(&box);
302 v = camera->getVisibility(box);
303 }
304
305 // if the octant is visible, or if it's the root node...
306 if (v != VISIBILITY_NONE)
307 {
308
309 //Add stuff to be rendered;
310 typename DtOctree<T>::NodeList::iterator it = octant->myNodes.begin();
311
312#if 0
313 if (mShowBoxes)
314 {
315 mBoxes.push_back(octant->getWireBoundingBox());
316 }
317#endif
318
319 bool vis = true;
320
321 while (it != octant->myNodes.end())
322 {
323 DtOctreeNode<T> * sn = *it;
324
325 // if this octree is partially visible, manually cull all
326 // scene nodes attached directly to this level.
327
328 if (v == VISIBILITY_PARTIAL)
329 vis = camera->IsVisible(sn->worldAABB());
330
331 if (vis)
332 {
333
334#if 0
335 mNumObjects++;
336 sn->_addToRenderQueue(camera, queue, onlyShadowCasters);
337#endif
338
339 visibleNodes.push_back(sn);
340
341#if 0
342 if (mDisplayNodes)
343 queue->addRenderable(sn->getDebugRenderable());
344
345 // check if the scene manager or this node wants the bounding box shown.
346 if (sn->getShowBoundingBox() || mShowBoundingBoxes)
347 sn->_addBoundingBoxToQueue(queue);
348#endif
349 }
350
351 ++it;
352 }
353
354 DtOctree<T>* child;
355 bool childfoundvisible = (v == VISIBILITY_FULL);
356 if ((child = octant->myChildren[0][0][0]) != 0)
357 findVisibleObjects(visibleNodes, child, camera, childfoundvisible);
358
359 if ((child = octant->myChildren[1][0][0]) != 0)
360 findVisibleObjects(visibleNodes, child, camera, childfoundvisible);
361
362 if ((child = octant->myChildren[0][1][0]) != 0)
363 findVisibleObjects(visibleNodes, child, camera, childfoundvisible);
364
365 if ((child = octant->myChildren[1][1][0]) != 0)
366 findVisibleObjects(visibleNodes, child, camera, childfoundvisible);
367
368 if ((child = octant->myChildren[0][0][1]) != 0)
369 findVisibleObjects(visibleNodes, child, camera, childfoundvisible);
370
371 if ((child = octant->myChildren[1][0][1]) != 0)
372 findVisibleObjects(visibleNodes, child, camera, childfoundvisible);
373
374 if ((child = octant->myChildren[0][1][1]) != 0)
375 findVisibleObjects(visibleNodes, child, camera, childfoundvisible);
376
377 if ((child = octant->myChildren[1][1][1]) != 0)
378 findVisibleObjects(visibleNodes, child, camera, childfoundvisible);
379 }
380 }
381
382 template<class T>
384 , DtOctree<T>* octant, const Frustum<T>* frustum, bool foundvisible) const
385 {
386 //return immediately if nothing is in the node.
387 if (octant->numNodes() == 0)
388 return;
389
391
392 if (foundvisible)
393 {
394 v = VISIBILITY_FULL;
395 }
396
397 else if (octant == myOctreeRoot)
398 {
400 }
401
402 else
403 {
405 octant->getCullBounds(&box);
406 v = frustum->visibility(box);
407 }
408
409 // if the octant is visible, or if it's the root node...
410 if (v != VISIBILITY_NONE)
411 {
412
413 //Add stuff to be rendered;
414 typename DtOctree<T>::NodeList::iterator it = octant->myNodes.begin();
415
416 bool vis = true;
417
418 while (it != octant->myNodes.end())
419 {
420 DtOctreeNode<T>* sn = *it;
421
422 // if this octree is partially visible, manually cull all
423 // scene nodes attached directly to this level.
424
425 if (v == VISIBILITY_PARTIAL)
426 vis = frustum->isVisible(sn->worldAABB());
427
428 if (vis)
429 {
430 visibleNodes.push_back(sn);
431 }
432
433 ++it;
434 }
435
436 DtOctree<T>* child;
437 bool childfoundvisible = (v == VISIBILITY_FULL);
438 if ((child = octant->myChildren[0][0][0]) != 0)
439 findVisibleObjects(visibleNodes, child, frustum, childfoundvisible);
440
441 if ((child = octant->myChildren[1][0][0]) != 0)
442 findVisibleObjects(visibleNodes, child, frustum, childfoundvisible);
443
444 if ((child = octant->myChildren[0][1][0]) != 0)
445 findVisibleObjects(visibleNodes, child, frustum, childfoundvisible);
446
447 if ((child = octant->myChildren[1][1][0]) != 0)
448 findVisibleObjects(visibleNodes, child, frustum, childfoundvisible);
449
450 if ((child = octant->myChildren[0][0][1]) != 0)
451 findVisibleObjects(visibleNodes, child, frustum, childfoundvisible);
452
453 if ((child = octant->myChildren[1][0][1]) != 0)
454 findVisibleObjects(visibleNodes, child, frustum, childfoundvisible);
455
456 if ((child = octant->myChildren[0][1][1]) != 0)
457 findVisibleObjects(visibleNodes, child, frustum, childfoundvisible);
458
459 if ((child = octant->myChildren[1][1][1]) != 0)
460 findVisibleObjects(visibleNodes, child, frustum, childfoundvisible);
461 }
462 }
463
464
465} //namespace makVrv
#define ASSERT_PREDICATE(p)
Definition DtAssert.h:28
Camera class.
Common types.
#define SAFE_DELETE(p)
Definition DtCommonUtils.h:21
OctreeNode class.
Template for 32/64 bit Axis Aligned bounding boxesnot virtual because its stored as is on the GPU.
Definition DtAxisAlignedBoundingBox.h:28
bool isNull(void) const
is null?
Definition DtAxisAlignedBoundingBox.inl:203
void setMinMax(const DtVector3< T > &vMin, const DtVector3< T > &vMax)
set the min max values
Definition DtAxisAlignedBoundingBox.inl:79
bool isInfinite(void) const
is infinite extents?
Definition DtAxisAlignedBoundingBox.inl:193
const DtVector3< T > & min(void) const
get the min
Definition DtAxisAlignedBoundingBox.inl:55
const DtVector3< T > & max(void) const
get the max
Definition DtAxisAlignedBoundingBox.inl:61
DtVector3< T > size(void) const
full size
Definition DtAxisAlignedBoundingBox.inl:147
Template for 32/64 bit Cameras.
Definition DtCamera.h:44
VisibilityFromCamera getVisibility(const DtAxisAlignedBoundingBox< T > &box) const
Returns visibility from this camera?
Definition DtCamera.inl:278
bool IsVisible(const DtAxisAlignedBoundingBox< T > &box) const
Is a box visible from this camera?
Definition DtCamera.inl:241
Definition DtOctree.h:28
void findVisibleObjects(NodeVector &visibleNodes, DtOctree *pOctree, const DtCamera< T > *pCamera, bool foundVisible) const
bool isTwiceSize(const DtAxisAlignedBoundingBox< T > &box) const
Returns true is the box will fit in a child.
Definition DtOctree.inl:20
size_t myMaxDepth
Definition DtOctree.h:125
void removeNode(DtOctreeNode< T > *)
Removes an Octree scene node to this octree level.
Definition DtOctree.inl:128
void getChildIndexes(const DtAxisAlignedBoundingBox< T > &, int *x, int *y, int *z) const
It's assumed the the given box has already been proven to fit into a child.
Definition DtOctree.inl:39
DtAxisAlignedBoundingBox< T > myBox
Definition DtOctree.h:52
void unref()
Decrements the overall node count of this octree and all its parents.
Definition DtOctree.h:106
std::vector< DtOctreeNode< T > * > NodeVector
Definition DtOctree.h:79
void ref()
Increments the overall node count of this octree and all its parents.
Definition DtOctree.h:95
void updateOctreeNode(DtOctreeNode< T > *pOctreeNode)
Definition DtOctree.inl:148
DtOctree(DtOctree *parent, DtOctree *treeRoot, size_t maxDepth)
CTOR.
Definition DtOctree.inl:67
int numNodes()
Returns the number of scene nodes attached to this octree.
Definition DtOctree.h:45
int myNumNodes
number of SceneNodes in this octree and all its children.
Definition DtOctree.h:117
DtVector3< T > myHalfSize
Vector containing the dimensions of this octree / 2.
Definition DtOctree.h:55
DtOctree * myChildren[2][2][2]
Definition DtOctree.h:60
NodeList myNodes
Public list of SceneNodes attached to this particular octree.
Definition DtOctree.h:82
~DtOctree()
DTOR.
Definition DtOctree.inl:99
void removeOctreeNode(DtOctreeNode< T > *pOctreeNode)
Definition DtOctree.inl:262
void getCullBounds(DtAxisAlignedBoundingBox< T > *) const
Definition DtOctree.inl:142
void addOctreeNode(DtOctreeNode< T > *pOctreeNode, DtOctree< T > *pOctree, size_t depth=0)
Definition DtOctree.inl:187
DtOctree * myParent
parent octree
Definition DtOctree.h:120
void addNode(DtOctreeNode< T > *)
Definition DtOctree.inl:116
DtOctree * myOctreeRoot
tree root
Definition DtOctree.h:123
OctreeNode class.
Definition DtOctreeNode.h:21
DtOctree< T > * getOctant(void)
Get the octant which this node is in.
Definition DtOctreeNode.inl:33
void setOctant(DtOctree< T > *)
Set the octant which this node is in.
Definition DtOctreeNode.inl:27
bool isIn(const DtAxisAlignedBoundingBox< T > &box)
Determine whether a box is inside this node.
Definition DtOctreeNode.inl:40
DtAxisAlignedBoundingBox< T > myWorldAABB
Definition DtOctreeNode.h:41
2 dimensional vector
Definition DtVector3.h:27
T y
Definition DtVector3.h:135
T z
Definition DtVector3.h:135
T x
Definition DtVector3.h:135
Template for 32/64 bit bit frustums.
Definition DtCamera.h:160
Contains DtCreateSensorFieldOfViewCommand and DtCreateSensorFieldOfViewCommandCreator class declarati...
Definition DtGridLinesSettingsRecord.h:16
VisibilityFromCamera
Definition DtCamera.h:36
@ VISIBILITY_PARTIAL
Definition DtCamera.h:37
@ VISIBILITY_FULL
Definition DtCamera.h:37
@ VISIBILITY_NONE
Definition DtCamera.h:37