]>
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]))
72 halfspace
= xy
.intersectsSphere(entity
->getSphere());
73 if (halfspace
== Plane::INTERSECT
)
75 halfspace
= xy
.intersectsAabb(entity
->getAabb());
78 if (halfspace
== Plane::POSITIVE
)
80 Plane xz
= node
->getAabb().getPlaneXZ();
81 halfspace
= xz
.intersectsSphere(entity
->getSphere());
82 if (halfspace
== Plane::INTERSECT
)
84 halfspace
= xz
.intersectsAabb(entity
->getAabb());
87 if (halfspace
== Plane::POSITIVE
)
89 Plane yz
= node
->getAabb().getPlaneYZ();
90 halfspace
= yz
.intersectsSphere(entity
->getSphere());
91 if (halfspace
== Plane::INTERSECT
)
93 halfspace
= yz
.intersectsAabb(entity
->getAabb());
96 if (halfspace
== Plane::POSITIVE
)
100 else if (halfspace
== Plane::NEGATIVE
)
105 else if (halfspace
== Plane::NEGATIVE
)
107 Plane yz
= node
->getAabb().getPlaneYZ();
108 halfspace
= yz
.intersectsSphere(entity
->getSphere());
109 if (halfspace
== Plane::INTERSECT
)
111 halfspace
= yz
.intersectsAabb(entity
->getAabb());
114 if (halfspace
== Plane::POSITIVE
)
118 else if (halfspace
== Plane::NEGATIVE
)
124 else if (halfspace
== Plane::NEGATIVE
)
126 Plane xz
= node
->getAabb().getPlaneXZ();
127 halfspace
= xz
.intersectsSphere(entity
->getSphere());
128 if (halfspace
== Plane::INTERSECT
)
130 halfspace
= xz
.intersectsAabb(entity
->getAabb());
133 if (halfspace
== Plane::POSITIVE
)
135 Plane yz
= node
->getAabb().getPlaneYZ();
136 halfspace
= yz
.intersectsSphere(entity
->getSphere());
137 if (halfspace
== Plane::INTERSECT
)
139 halfspace
= yz
.intersectsAabb(entity
->getAabb());
142 if (halfspace
== Plane::POSITIVE
)
146 else if (halfspace
== Plane::NEGATIVE
)
151 else if (halfspace
== Plane::NEGATIVE
)
153 Plane yz
= node
->getAabb().getPlaneYZ();
154 halfspace
= yz
.intersectsSphere(entity
->getSphere());
155 if (halfspace
== Plane::INTERSECT
)
157 halfspace
= yz
.intersectsAabb(entity
->getAabb());
160 if (halfspace
== Plane::POSITIVE
)
164 else if (halfspace
== Plane::NEGATIVE
)
175 node
->objects
.push_front(entity
);
180 if ((int)tree_
.children(node
) <= octantNum
)
182 addChild(node
, octantNum
);
185 OctreeNodeP child
= tree_
.child(node
, octantNum
);
186 ASSERT(child
.valid() && "expected valid child node");
188 return insert(entity
, child
);
192 OctreeNodeP
Octree::reinsert(EntityP entity
, OctreeNodeP node
)
194 ASSERT(entity
&& "null entity passed");
195 ASSERT(node
.valid() && "invalid node passed");
197 std::list
<EntityP
>::iterator it
;
198 it
= std::find(node
->objects
.begin(), node
->objects
.end(), entity
);
200 if (it
!= node
->objects
.end())
202 node
->objects
.erase(it
);
205 return insert(entity
);
209 void Octree::addChild(OctreeNodeP node
, int index
)
211 ASSERT(node
.valid() && "invalid node passed");
215 for (int i
= tree_
.children(node
); i
<= index
; ++i
)
217 node
->getAabb().getOctant(octant
, i
);
218 tree_
.append(node
, octant
);
223 void Octree::draw(Scalar alpha
, OctreeNodeP node
)
225 ASSERT(node
.valid() && "invalid node passed");
229 for (unsigned i
= 0; i
< tree_
.children(node
); ++i
)
231 OctreeNodeP child
= tree_
.child(node
, i
);
232 ASSERT(child
.valid() && "expected valid child node");
238 void Octree::drawIfVisible(Scalar alpha
, const Camera
& cam
, OctreeNodeP node
)
240 ASSERT(node
.valid() && "invalid node passed");
242 // try to cull by sphere
243 Frustum::Collision collision
=
244 cam
.getFrustum().containsSphere(node
->getSphere());
245 if (collision
== Frustum::OUTSIDE
) return;
247 // try to cull by aabb
248 collision
= cam
.getFrustum().containsAabb(node
->getAabb());
249 if (collision
== Frustum::OUTSIDE
) return;
252 if (collision
== Frustum::INSIDE
)
256 else // collision == Frustum::INTERSECT
258 node
->drawIfVisible(alpha
, cam
);
261 if (tree_
.children(node
) > 0)
263 if (collision
== Frustum::INSIDE
)
265 for (unsigned i
= 0; i
< tree_
.children(node
); ++i
)
267 OctreeNodeP child
= tree_
.child(node
, i
);
268 ASSERT(child
.valid() && "expected valid child node");
273 else // collision == Frustum::INTERSECT
275 for (unsigned i
= 0; i
< tree_
.children(node
); ++i
)
277 OctreeNodeP child
= tree_
.child(node
, i
);
278 ASSERT(child
.valid() && "expected valid child node");
280 drawIfVisible(alpha
, cam
, child
);
289 /** vim: set ts=4 sw=4 tw=80: *************************************************/
This page took 0.045895 seconds and 4 git commands to generate.