]> Dogcows Code - chaz/yoink/blob - src/Moof/Octree.cc
fixed stack overflow of entities outside octree
[chaz/yoink] / src / Moof / Octree.cc
1
2 /*******************************************************************************
3
4 Copyright (c) 2009, Charles McGarvey
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9
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.
15
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.
26
27 *******************************************************************************/
28
29 #include "Camera.hh"
30 #include "Log.hh"
31 #include "Octree.hh"
32
33
34 namespace Mf {
35
36
37 void Octree::sort()
38 {
39 stlplus::ntree<OctreeNode>::prefix_iterator it;
40
41 for (it = tree_.prefix_begin(); it != tree_.prefix_end(); ++it)
42 {
43 it->sort();
44 }
45 }
46
47
48 OctreeNodeP Octree::insert(EntityP entity, OctreeNodeP node)
49 {
50 ASSERT(node.valid() && "invalid node passed");
51 ASSERT(entity && "null entity passed");
52
53 int octantNum = -1;
54
55 Plane::Halfspace halfspace;
56
57 // TODO this method needs a lot of work
58 Plane xy = node->getAabb().getPlaneXY();
59
60
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]))
68 {
69 goto done;
70 }
71
72 halfspace = xy.intersectsSphere(entity->getSphere());
73 if (halfspace == Plane::INTERSECT)
74 {
75 halfspace = xy.intersectsAabb(entity->getAabb());
76 }
77
78 if (halfspace == Plane::POSITIVE)
79 {
80 Plane xz = node->getAabb().getPlaneXZ();
81 halfspace = xz.intersectsSphere(entity->getSphere());
82 if (halfspace == Plane::INTERSECT)
83 {
84 halfspace = xz.intersectsAabb(entity->getAabb());
85 }
86
87 if (halfspace == Plane::POSITIVE)
88 {
89 Plane yz = node->getAabb().getPlaneYZ();
90 halfspace = yz.intersectsSphere(entity->getSphere());
91 if (halfspace == Plane::INTERSECT)
92 {
93 halfspace = yz.intersectsAabb(entity->getAabb());
94 }
95
96 if (halfspace == Plane::POSITIVE)
97 {
98 octantNum = 2;
99 }
100 else if (halfspace == Plane::NEGATIVE)
101 {
102 octantNum = 3;
103 }
104 }
105 else if (halfspace == Plane::NEGATIVE)
106 {
107 Plane yz = node->getAabb().getPlaneYZ();
108 halfspace = yz.intersectsSphere(entity->getSphere());
109 if (halfspace == Plane::INTERSECT)
110 {
111 halfspace = yz.intersectsAabb(entity->getAabb());
112 }
113
114 if (halfspace == Plane::POSITIVE)
115 {
116 octantNum = 1;
117 }
118 else if (halfspace == Plane::NEGATIVE)
119 {
120 octantNum = 0;
121 }
122 }
123 }
124 else if (halfspace == Plane::NEGATIVE)
125 {
126 Plane xz = node->getAabb().getPlaneXZ();
127 halfspace = xz.intersectsSphere(entity->getSphere());
128 if (halfspace == Plane::INTERSECT)
129 {
130 halfspace = xz.intersectsAabb(entity->getAabb());
131 }
132
133 if (halfspace == Plane::POSITIVE)
134 {
135 Plane yz = node->getAabb().getPlaneYZ();
136 halfspace = yz.intersectsSphere(entity->getSphere());
137 if (halfspace == Plane::INTERSECT)
138 {
139 halfspace = yz.intersectsAabb(entity->getAabb());
140 }
141
142 if (halfspace == Plane::POSITIVE)
143 {
144 octantNum = 6;
145 }
146 else if (halfspace == Plane::NEGATIVE)
147 {
148 octantNum = 7;
149 }
150 }
151 else if (halfspace == Plane::NEGATIVE)
152 {
153 Plane yz = node->getAabb().getPlaneYZ();
154 halfspace = yz.intersectsSphere(entity->getSphere());
155 if (halfspace == Plane::INTERSECT)
156 {
157 halfspace = yz.intersectsAabb(entity->getAabb());
158 }
159
160 if (halfspace == Plane::POSITIVE)
161 {
162 octantNum = 5;
163 }
164 else if (halfspace == Plane::NEGATIVE)
165 {
166 octantNum = 4;
167 }
168 }
169 }
170
171 done:
172
173 if (octantNum == -1)
174 {
175 node->objects.push_front(entity);
176 return node;
177 }
178 else
179 {
180 if ((int)tree_.children(node) <= octantNum)
181 {
182 addChild(node, octantNum);
183 }
184
185 OctreeNodeP child = tree_.child(node, octantNum);
186 ASSERT(child.valid() && "expected valid child node");
187
188 return insert(entity, child);
189 }
190 }
191
192 OctreeNodeP Octree::reinsert(EntityP entity, OctreeNodeP node)
193 {
194 ASSERT(entity && "null entity passed");
195 ASSERT(node.valid() && "invalid node passed");
196
197 std::list<EntityP>::iterator it;
198 it = std::find(node->objects.begin(), node->objects.end(), entity);
199
200 if (it != node->objects.end())
201 {
202 node->objects.erase(it);
203 }
204
205 return insert(entity);
206 }
207
208
209 void Octree::addChild(OctreeNodeP node, int index)
210 {
211 ASSERT(node.valid() && "invalid node passed");
212
213 Aabb octant;
214
215 for (int i = tree_.children(node); i <= index; ++i)
216 {
217 node->getAabb().getOctant(octant, i);
218 tree_.append(node, octant);
219 }
220 }
221
222
223 void Octree::draw(Scalar alpha, OctreeNodeP node)
224 {
225 ASSERT(node.valid() && "invalid node passed");
226
227 node->draw(alpha);
228
229 for (unsigned i = 0; i < tree_.children(node); ++i)
230 {
231 OctreeNodeP child = tree_.child(node, i);
232 ASSERT(child.valid() && "expected valid child node");
233
234 draw(alpha, child);
235 }
236 }
237
238 void Octree::drawIfVisible(Scalar alpha, const Camera& cam, OctreeNodeP node)
239 {
240 ASSERT(node.valid() && "invalid node passed");
241
242 // try to cull by sphere
243 Frustum::Collision collision =
244 cam.getFrustum().containsSphere(node->getSphere());
245 if (collision == Frustum::OUTSIDE) return;
246
247 // try to cull by aabb
248 collision = cam.getFrustum().containsAabb(node->getAabb());
249 if (collision == Frustum::OUTSIDE) return;
250
251
252 if (collision == Frustum::INSIDE)
253 {
254 node->draw(alpha);
255 }
256 else // collision == Frustum::INTERSECT
257 {
258 node->drawIfVisible(alpha, cam);
259 }
260
261 if (tree_.children(node) > 0)
262 {
263 if (collision == Frustum::INSIDE)
264 {
265 for (unsigned i = 0; i < tree_.children(node); ++i)
266 {
267 OctreeNodeP child = tree_.child(node, i);
268 ASSERT(child.valid() && "expected valid child node");
269
270 draw(alpha, child);
271 }
272 }
273 else // collision == Frustum::INTERSECT
274 {
275 for (unsigned i = 0; i < tree_.children(node); ++i)
276 {
277 OctreeNodeP child = tree_.child(node, i);
278 ASSERT(child.valid() && "expected valid child node");
279
280 drawIfVisible(alpha, cam, child);
281 }
282 }
283 }
284 }
285
286
287 } // namespace Mf
288
289 /** vim: set ts=4 sw=4 tw=80: *************************************************/
290
This page took 0.049762 seconds and 5 git commands to generate.