]>
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 OctreeNodeP
Octree::insert(EntityP entity
, OctreeNodeP node
)
50 ASSERT(node
.valid() && "invalid node passed");
51 ASSERT(entity
&& "null entity passed");
55 Plane::Halfspace halfspace
;
57 // TODO this method needs a lot of work
58 Plane xy
= node
->getAabb().getPlaneXY();
61 // make sure the entity is fully inside the volume
62 if (!(entity
->getAabb().max
[0] < node
->getAabb().max
[0] &&
63 entity
->getAabb().min
[0] > node
->getAabb().min
[0] &&
64 entity
->getAabb().max
[1] < node
->getAabb().max
[1] &&
65 entity
->getAabb().min
[1] > node
->getAabb().min
[1] &&
66 entity
->getAabb().max
[2] < node
->getAabb().max
[2] &&
67 entity
->getAabb().min
[2] > node
->getAabb().min
[2]))
69 // TODO this check is only needed for the root node, if we're inside the
70 // volume of the root node, we'll be fully inside the child as
71 // determined by trying to insert the parent node
75 halfspace
= xy
.intersectsSphere(entity
->getSphere());
76 if (halfspace
== Plane::INTERSECT
)
78 halfspace
= xy
.intersectsAabb(entity
->getAabb());
81 if (halfspace
== Plane::POSITIVE
)
83 Plane xz
= node
->getAabb().getPlaneXZ();
84 halfspace
= xz
.intersectsSphere(entity
->getSphere());
85 if (halfspace
== Plane::INTERSECT
)
87 halfspace
= xz
.intersectsAabb(entity
->getAabb());
90 if (halfspace
== Plane::POSITIVE
)
92 Plane yz
= node
->getAabb().getPlaneYZ();
93 halfspace
= yz
.intersectsSphere(entity
->getSphere());
94 if (halfspace
== Plane::INTERSECT
)
96 halfspace
= yz
.intersectsAabb(entity
->getAabb());
99 if (halfspace
== Plane::POSITIVE
)
103 else if (halfspace
== Plane::NEGATIVE
)
108 else if (halfspace
== Plane::NEGATIVE
)
110 Plane yz
= node
->getAabb().getPlaneYZ();
111 halfspace
= yz
.intersectsSphere(entity
->getSphere());
112 if (halfspace
== Plane::INTERSECT
)
114 halfspace
= yz
.intersectsAabb(entity
->getAabb());
117 if (halfspace
== Plane::POSITIVE
)
121 else if (halfspace
== Plane::NEGATIVE
)
127 else if (halfspace
== Plane::NEGATIVE
)
129 Plane xz
= node
->getAabb().getPlaneXZ();
130 halfspace
= xz
.intersectsSphere(entity
->getSphere());
131 if (halfspace
== Plane::INTERSECT
)
133 halfspace
= xz
.intersectsAabb(entity
->getAabb());
136 if (halfspace
== Plane::POSITIVE
)
138 Plane yz
= node
->getAabb().getPlaneYZ();
139 halfspace
= yz
.intersectsSphere(entity
->getSphere());
140 if (halfspace
== Plane::INTERSECT
)
142 halfspace
= yz
.intersectsAabb(entity
->getAabb());
145 if (halfspace
== Plane::POSITIVE
)
149 else if (halfspace
== Plane::NEGATIVE
)
154 else if (halfspace
== Plane::NEGATIVE
)
156 Plane yz
= node
->getAabb().getPlaneYZ();
157 halfspace
= yz
.intersectsSphere(entity
->getSphere());
158 if (halfspace
== Plane::INTERSECT
)
160 halfspace
= yz
.intersectsAabb(entity
->getAabb());
163 if (halfspace
== Plane::POSITIVE
)
167 else if (halfspace
== Plane::NEGATIVE
)
178 node
->objects
.push_front(entity
);
183 if ((int)tree_
.children(node
) <= octantNum
)
185 addChild(node
, octantNum
);
188 OctreeNodeP child
= tree_
.child(node
, octantNum
);
189 ASSERT(child
.valid() && "expected valid child node");
191 return insert(entity
, child
);
195 OctreeNodeP
Octree::reinsert(EntityP entity
, OctreeNodeP node
)
197 ASSERT(entity
&& "null entity passed");
198 ASSERT(node
.valid() && "invalid node passed");
200 std::list
<EntityP
>::iterator it
;
201 it
= std::find(node
->objects
.begin(), node
->objects
.end(), entity
);
203 if (it
!= node
->objects
.end())
205 node
->objects
.erase(it
);
208 return insert(entity
);
212 void Octree::addChild(OctreeNodeP node
, int index
)
214 ASSERT(node
.valid() && "invalid node passed");
218 for (int i
= tree_
.children(node
); i
<= index
; ++i
)
220 node
->getAabb().getOctant(octant
, i
);
221 tree_
.append(node
, octant
);
226 void Octree::draw(Scalar alpha
, OctreeNodeP node
)
228 ASSERT(node
.valid() && "invalid node passed");
232 for (unsigned i
= 0; i
< tree_
.children(node
); ++i
)
234 OctreeNodeP child
= tree_
.child(node
, i
);
235 ASSERT(child
.valid() && "expected valid child node");
241 void Octree::drawIfVisible(Scalar alpha
, const Camera
& cam
, OctreeNodeP node
)
243 ASSERT(node
.valid() && "invalid node passed");
245 // try to cull by sphere
246 Frustum::Collision collision
=
247 cam
.getFrustum().containsSphere(node
->getSphere());
248 if (collision
== Frustum::OUTSIDE
) return;
250 // try to cull by aabb
251 collision
= cam
.getFrustum().containsAabb(node
->getAabb());
252 if (collision
== Frustum::OUTSIDE
) return;
255 if (collision
== Frustum::INSIDE
)
259 else // collision == Frustum::INTERSECT
261 node
->drawIfVisible(alpha
, cam
);
264 if (tree_
.children(node
) > 0)
266 if (collision
== Frustum::INSIDE
)
268 for (unsigned i
= 0; i
< tree_
.children(node
); ++i
)
270 OctreeNodeP child
= tree_
.child(node
, i
);
271 ASSERT(child
.valid() && "expected valid child node");
276 else // collision == Frustum::INTERSECT
278 for (unsigned i
= 0; i
< tree_
.children(node
); ++i
)
280 OctreeNodeP child
= tree_
.child(node
, i
);
281 ASSERT(child
.valid() && "expected valid child node");
283 drawIfVisible(alpha
, cam
, child
);
292 /** vim: set ts=4 sw=4 tw=80: *************************************************/
This page took 0.047405 seconds and 5 git commands to generate.