]>
Dogcows Code - chaz/yoink/blob - src/Moof/Octree.hh
1c57ff2d3317fe83d10aa6972093683cb634556f
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 *******************************************************************************/
29 #ifndef _MOOF_OCTREE_HH_
30 #define _MOOF_OCTREE_HH_
34 #include <boost/shared_ptr.hpp>
36 #include <stlplus/ntree.hpp>
38 #include <Moof/Aabb.hh>
39 #include <Moof/Drawable.hh>
40 #include <Moof/Entity.hh>
41 #include <Moof/Math.hh>
42 #include <Moof/Sphere.hh>
48 struct OctreeNode
: public Entity
50 std::list
<EntityPtr
> objects
;
54 aabb_
.min
= Vector3(-1.0, -1.0, -1.0);
55 aabb_
.max
= Vector3(1.0, 1.0, 1.0);
56 sphere_
.init(Vector3(0.0, 0.0, 0.0), 1.41421);
59 OctreeNode(const Aabb
& aabb
)
62 sphere_
.point
= aabb
.getCenter();
63 sphere_
.radius
= (aabb
.min
- sphere_
.point
).length();
66 void draw(Scalar alpha
) const
68 std::list
<EntityPtr
>::const_iterator it
;
70 for (it
= objects
.begin(); it
!= objects
.end(); ++it
)
75 aabb_
.draw(); // temporary
78 void drawIfVisible(Scalar alpha
, const Camera
& cam
) const
80 std::list
<EntityPtr
>::const_iterator it
;
82 for (it
= objects
.begin(); it
!= objects
.end(); ++it
)
84 (*it
)->drawIfVisible(alpha
, cam
);
91 bool isVisible(const Camera
& cam
) const
93 if (sphere_
.isVisible(cam
))
95 return aabb_
.isVisible(cam
);
104 typedef boost::shared_ptr
<Octree
> OctreePtr
;
109 stlplus::ntree
<OctreeNode
> root_
;
113 explicit Octree(const OctreeNode
& rootNode
)
115 root_
.insert(rootNode
);
118 void insert(EntityPtr entity
)
120 insert(root_
.root(), entity
);
123 void insert(stlplus::ntree
<OctreeNode
>::iterator node
, EntityPtr entity
)
125 Plane::Halfspace halfspace
;
130 std::cerr
<< "cannot insert into invalid node" << std::endl
;
134 Plane xy
= node
->getAabb().getPlaneXY();
135 halfspace
= xy
.intersectsSphere(entity
->getSphere());
137 if (halfspace
== Plane::POSITIVE
)
139 Plane xz
= node
->getAabb().getPlaneXZ();
140 halfspace
= xz
.intersectsSphere(entity
->getSphere());
142 if (halfspace
== Plane::POSITIVE
)
144 Plane yz
= node
->getAabb().getPlaneYZ();
145 halfspace
= yz
.intersectsSphere(entity
->getSphere());
147 if (halfspace
== Plane::POSITIVE
)
151 else if (halfspace
== Plane::NEGATIVE
)
156 else if (halfspace
== Plane::NEGATIVE
)
158 Plane yz
= node
->getAabb().getPlaneYZ();
159 halfspace
= yz
.intersectsSphere(entity
->getSphere());
161 if (halfspace
== Plane::POSITIVE
)
165 else if (halfspace
== Plane::NEGATIVE
)
171 else if (halfspace
== Plane::NEGATIVE
)
173 Plane xz
= node
->getAabb().getPlaneXZ();
174 halfspace
= xz
.intersectsSphere(entity
->getSphere());
176 if (halfspace
== Plane::POSITIVE
)
178 Plane yz
= node
->getAabb().getPlaneYZ();
179 halfspace
= yz
.intersectsSphere(entity
->getSphere());
181 if (halfspace
== Plane::POSITIVE
)
185 else if (halfspace
== Plane::NEGATIVE
)
190 else if (halfspace
== Plane::NEGATIVE
)
192 Plane yz
= node
->getAabb().getPlaneYZ();
193 halfspace
= yz
.intersectsSphere(entity
->getSphere());
195 if (halfspace
== Plane::POSITIVE
)
199 else if (halfspace
== Plane::NEGATIVE
)
208 node
->objects
.push_front(entity
);
213 if (root_
.children(node
) == 0)
218 stlplus::ntree
<OctreeNode
>::iterator child
= root_
.child(node
, octantNum
);
222 return insert(child
, entity
);
226 std::cerr
<< "expected but found no child at index " << octantNum
<< std::endl
;
227 //return stlplus::ntree<OctreeNode>::iterator();
233 void addChildren(stlplus::ntree
<OctreeNode
>::iterator node
)
239 std::cerr
<< "cannot add children to invalid node" << std::endl
;
243 for (int i
= 0; i
< 8; ++i
)
245 node
->getAabb().getOctant(octant
, i
);
246 //OctreeNode octantNode(octant);
248 root_
.append(node
, octant
);
252 void draw(stlplus::ntree
<OctreeNode
>::iterator node
, Scalar alpha
)
256 std::cerr
<< "cannot draw null child node :-(" << std::endl
;
262 for (unsigned i
= 0; i
< root_
.children(node
); ++i
)
264 stlplus::ntree
<OctreeNode
>::iterator child
= root_
.child(node
, i
);
272 std::cerr
<< "node is not a leaf, but has an invalid child" << std::endl
;
278 void drawIfVisible(stlplus::ntree
<OctreeNode
>::iterator node
,
279 Scalar alpha
, const Camera
& cam
)
281 //node.drawIfVisible(alpha, cam);
285 std::cerr
<< "invalid child while drawing :-(" << std::endl
;
289 Frustum::Collision collision
=
290 cam
.getFrustum().containsSphere(node
->getSphere());
291 if (collision
== Frustum::OUTSIDE
) return;
293 collision
= cam
.getFrustum().containsAabb(node
->getAabb());
294 if (collision
== Frustum::OUTSIDE
) return;
297 if (collision
== Frustum::INSIDE
)
301 else // collision == Frustum::INTERSECT
303 node
->drawIfVisible(alpha
, cam
);
306 if (root_
.children(node
) > 0)
308 if (collision
== Frustum::INSIDE
)
310 for (unsigned i
= 0; i
< root_
.children(node
); ++i
)
312 stlplus::ntree
<OctreeNode
>::iterator child
= root_
.child(node
, i
);
320 std::cerr
<< "node is not a leaf, but has an invalid child" << std::endl
;
325 else // collision == Frustum::INTERSECT
327 for (unsigned i
= 0; i
< root_
.children(node
); ++i
)
329 stlplus::ntree
<OctreeNode
>::iterator child
= root_
.child(node
, i
);
333 drawIfVisible(child
, alpha
, cam
);
337 std::cerr
<< "node is not a leaf, but has an invalid child" << std::endl
;
344 void drawIfVisible(Scalar alpha
, const Camera
& cam
)
346 drawIfVisible(root_
.root(), alpha
, cam
);
355 #endif // _MOOF_OCTREE_HH_
357 /** vim: set ts=4 sw=4 tw=80: *************************************************/
This page took 0.054242 seconds and 4 git commands to generate.