]>
Dogcows Code - chaz/yoink/blob - src/Moof/Octree.cc
2 /*******************************************************************************
4 Copyright (c) 2009, Charles McGarvey
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
10 * Redistributions of source code must retain the above copyright notice,
11 this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *******************************************************************************/
39 stlplus::ntree
<OctreeNode
>::prefix_iterator it
;
41 for (it
= tree_
.prefix_begin(); it
!= tree_
.prefix_end(); ++it
)
48 stlplus::ntree
<OctreeNode
>::iterator
Octree::insert(stlplus::ntree
<OctreeNode
>::iterator node
,
51 ASSERT(node
.valid() && "invalid node passed");
52 ASSERT(entity
&& "null entity passed");
54 Plane::Halfspace halfspace
;
57 Plane xy
= node
->getAabb().getPlaneXY();
58 halfspace
= xy
.intersectsSphere(entity
->getSphere());
59 if (halfspace
== Plane::INTERSECT
)
61 halfspace
= xy
.intersectsAabb(entity
->getAabb());
64 if (halfspace
== Plane::POSITIVE
)
66 Plane xz
= node
->getAabb().getPlaneXZ();
67 halfspace
= xz
.intersectsSphere(entity
->getSphere());
68 if (halfspace
== Plane::INTERSECT
)
70 halfspace
= xz
.intersectsAabb(entity
->getAabb());
73 if (halfspace
== Plane::POSITIVE
)
75 Plane yz
= node
->getAabb().getPlaneYZ();
76 halfspace
= yz
.intersectsSphere(entity
->getSphere());
77 if (halfspace
== Plane::INTERSECT
)
79 halfspace
= yz
.intersectsAabb(entity
->getAabb());
82 if (halfspace
== Plane::POSITIVE
)
86 else if (halfspace
== Plane::NEGATIVE
)
91 else if (halfspace
== Plane::NEGATIVE
)
93 Plane yz
= node
->getAabb().getPlaneYZ();
94 halfspace
= yz
.intersectsSphere(entity
->getSphere());
95 if (halfspace
== Plane::INTERSECT
)
97 halfspace
= yz
.intersectsAabb(entity
->getAabb());
100 if (halfspace
== Plane::POSITIVE
)
104 else if (halfspace
== Plane::NEGATIVE
)
110 else if (halfspace
== Plane::NEGATIVE
)
112 Plane xz
= node
->getAabb().getPlaneXZ();
113 halfspace
= xz
.intersectsSphere(entity
->getSphere());
114 if (halfspace
== Plane::INTERSECT
)
116 halfspace
= xz
.intersectsAabb(entity
->getAabb());
119 if (halfspace
== Plane::POSITIVE
)
121 Plane yz
= node
->getAabb().getPlaneYZ();
122 halfspace
= yz
.intersectsSphere(entity
->getSphere());
123 if (halfspace
== Plane::INTERSECT
)
125 halfspace
= yz
.intersectsAabb(entity
->getAabb());
128 if (halfspace
== Plane::POSITIVE
)
132 else if (halfspace
== Plane::NEGATIVE
)
137 else if (halfspace
== Plane::NEGATIVE
)
139 Plane yz
= node
->getAabb().getPlaneYZ();
140 halfspace
= yz
.intersectsSphere(entity
->getSphere());
141 if (halfspace
== Plane::INTERSECT
)
143 halfspace
= yz
.intersectsAabb(entity
->getAabb());
146 if (halfspace
== Plane::POSITIVE
)
150 else if (halfspace
== Plane::NEGATIVE
)
159 node
->objects
.push_front(entity
);
164 if ((int)tree_
.children(node
) <= octantNum
)
166 addChild(node
, octantNum
);
169 stlplus::ntree
<OctreeNode
>::iterator child
= tree_
.child(node
, octantNum
);
170 ASSERT(child
.valid() && "expected valid child node");
172 return insert(child
, entity
);
176 stlplus::ntree
<OctreeNode
>::iterator
Octree::reinsert(EntityP entity
,
177 stlplus::ntree
<OctreeNode
>::iterator node
)
179 ASSERT(entity
&& "null entity passed");
180 ASSERT(node
.valid() && "invalid node passed");
182 std::list
<EntityP
>::iterator it
;
183 it
= std::find(node
->objects
.begin(), node
->objects
.end(), entity
);
185 if (it
!= node
->objects
.end())
187 node
->objects
.erase(it
);
190 return insert(entity
);
194 void Octree::addChild(stlplus::ntree
<OctreeNode
>::iterator node
, int index
)
196 ASSERT(node
.valid() && "invalid node passed");
200 for (int i
= tree_
.children(node
); i
<= index
; ++i
)
202 node
->getAabb().getOctant(octant
, i
);
203 tree_
.append(node
, octant
);
208 void Octree::draw(stlplus::ntree
<OctreeNode
>::iterator node
, Scalar alpha
)
210 ASSERT(node
.valid() && "invalid node passed");
214 for (unsigned i
= 0; i
< tree_
.children(node
); ++i
)
216 stlplus::ntree
<OctreeNode
>::iterator child
= tree_
.child(node
, i
);
217 ASSERT(child
.valid() && "expected valid child node");
223 void Octree::drawIfVisible(stlplus::ntree
<OctreeNode
>::iterator node
,
224 Scalar alpha
, const Camera
& cam
)
226 ASSERT(node
.valid() && "invalid node passed");
228 // try to cull by sphere
229 Frustum::Collision collision
=
230 cam
.getFrustum().containsSphere(node
->getSphere());
231 if (collision
== Frustum::OUTSIDE
) return;
233 // try to cull by aabb
234 collision
= cam
.getFrustum().containsAabb(node
->getAabb());
235 if (collision
== Frustum::OUTSIDE
) return;
238 if (collision
== Frustum::INSIDE
)
242 else // collision == Frustum::INTERSECT
244 node
->drawIfVisible(alpha
, cam
);
247 if (tree_
.children(node
) > 0)
249 if (collision
== Frustum::INSIDE
)
251 for (unsigned i
= 0; i
< tree_
.children(node
); ++i
)
253 stlplus::ntree
<OctreeNode
>::iterator child
= tree_
.child(node
, i
);
254 ASSERT(child
.valid() && "expected valid child node");
259 else // collision == Frustum::INTERSECT
261 for (unsigned i
= 0; i
< tree_
.children(node
); ++i
)
263 stlplus::ntree
<OctreeNode
>::iterator child
= tree_
.child(node
, i
);
264 ASSERT(child
.valid() && "expected valid child node");
266 drawIfVisible(child
, alpha
, cam
);
275 /** vim: set ts=4 sw=4 tw=80: *************************************************/
This page took 0.04655 seconds and 5 git commands to generate.