1 | |
---|
2 | |
---|
3 | |
---|
4 | |
---|
5 | |
---|
6 | |
---|
7 | |
---|
8 | |
---|
9 | |
---|
10 | |
---|
11 | |
---|
12 | |
---|
13 | |
---|
14 | |
---|
15 | |
---|
16 | |
---|
17 | |
---|
18 | |
---|
19 | #include "DistanceAccumulator.h" |
---|
20 | #include <osg/Geode> |
---|
21 | #include <osg/Transform> |
---|
22 | #include <osg/Projection> |
---|
23 | #include <algorithm> |
---|
24 | #include <math.h> |
---|
25 | #include <limits.h> |
---|
26 | |
---|
27 | |
---|
28 | |
---|
29 | bool precedes(const DistanceAccumulator::DistancePair &a, |
---|
30 | const DistanceAccumulator::DistancePair &b) |
---|
31 | { |
---|
32 | |
---|
33 | if(a.second > b.second) return true; |
---|
34 | else return false; |
---|
35 | } |
---|
36 | |
---|
37 | |
---|
38 | |
---|
39 | double distance(const osg::Vec3 &coord, const osg::Matrix& matrix) |
---|
40 | { |
---|
41 | |
---|
42 | |
---|
43 | |
---|
44 | return -( coord[0]*matrix(0,2) + coord[1]*matrix(1,2) + |
---|
45 | coord[2]*matrix(2,2) + matrix(3,2) ); |
---|
46 | } |
---|
47 | |
---|
48 | #define CURRENT_CLASS DistanceAccumulator |
---|
49 | CURRENT_CLASS::CURRENT_CLASS() |
---|
50 | : osg::NodeVisitor(TRAVERSE_ALL_CHILDREN), |
---|
51 | _nearFarRatio(0.0005), _maxDepth(UINT_MAX) |
---|
52 | { |
---|
53 | setMatrices(osg::Matrix::identity(), osg::Matrix::identity()); |
---|
54 | reset(); |
---|
55 | } |
---|
56 | |
---|
57 | CURRENT_CLASS::~CURRENT_CLASS() {} |
---|
58 | |
---|
59 | void CURRENT_CLASS::pushLocalFrustum() |
---|
60 | { |
---|
61 | osg::Matrix& currMatrix = _viewMatrices.back(); |
---|
62 | |
---|
63 | |
---|
64 | osg::Polytope localFrustum; |
---|
65 | localFrustum.setToUnitFrustum(false, false); |
---|
66 | localFrustum.transformProvidingInverse(currMatrix*_projectionMatrices.back()); |
---|
67 | _localFrusta.push_back(localFrustum); |
---|
68 | |
---|
69 | |
---|
70 | bbCornerPair corner; |
---|
71 | corner.second = (currMatrix(0,2)<=0?1:0) | |
---|
72 | (currMatrix(1,2)<=0?2:0) | |
---|
73 | (currMatrix(2,2)<=0?4:0); |
---|
74 | corner.first = (~corner.second)&7; |
---|
75 | _bbCorners.push_back(corner); |
---|
76 | } |
---|
77 | |
---|
78 | void CURRENT_CLASS::pushDistancePair(double zNear, double zFar) |
---|
79 | { |
---|
80 | if(zFar > 0.0) |
---|
81 | { |
---|
82 | |
---|
83 | if(zNear <= 0.0) |
---|
84 | { |
---|
85 | zNear = zFar*_nearFarRatio; |
---|
86 | if(zNear >= 1.0) zNear = 1.0; |
---|
87 | } |
---|
88 | |
---|
89 | |
---|
90 | _distancePairs.push_back(DistancePair(zNear, zFar)); |
---|
91 | |
---|
92 | |
---|
93 | if(zNear < _limits.first) _limits.first = zNear; |
---|
94 | if(zFar > _limits.second) _limits.second = zFar; |
---|
95 | } |
---|
96 | } |
---|
97 | |
---|
98 | |
---|
99 | |
---|
100 | |
---|
101 | bool CURRENT_CLASS::shouldContinueTraversal(osg::Node &node) |
---|
102 | { |
---|
103 | |
---|
104 | bool keepTraversing = (_currentDepth < _maxDepth); |
---|
105 | |
---|
106 | osg::BoundingSphere bs = node.getBound(); |
---|
107 | double zNear = 0.0, zFar = 0.0; |
---|
108 | |
---|
109 | |
---|
110 | if(bs.valid()) |
---|
111 | { |
---|
112 | |
---|
113 | if(!_localFrusta.back().contains(bs)) keepTraversing = false; |
---|
114 | |
---|
115 | else |
---|
116 | { |
---|
117 | |
---|
118 | |
---|
119 | |
---|
120 | const osg::Matrix &l2w = _viewMatrices.back(); |
---|
121 | |
---|
122 | |
---|
123 | osg::Vec3d newX = bs._center; |
---|
124 | newX.x() += bs._radius; |
---|
125 | newX = newX * l2w; |
---|
126 | |
---|
127 | |
---|
128 | osg::Vec3d newY = bs._center; |
---|
129 | newY.y() += bs._radius; |
---|
130 | newY = newY * l2w; |
---|
131 | |
---|
132 | |
---|
133 | osg::Vec3d newZ = bs._center; |
---|
134 | newZ.z() += bs._radius; |
---|
135 | newZ = newZ * l2w; |
---|
136 | |
---|
137 | |
---|
138 | bs._center = bs._center * l2w; |
---|
139 | |
---|
140 | |
---|
141 | double newXLen = (newX - bs._center).length(); |
---|
142 | double newYLen = (newY - bs._center).length(); |
---|
143 | double newZLen = (newZ - bs._center).length(); |
---|
144 | |
---|
145 | |
---|
146 | bs._radius = newXLen; |
---|
147 | if(newYLen > bs._radius) bs._radius = newYLen; |
---|
148 | if(newZLen > bs._radius) bs._radius = newZLen; |
---|
149 | |
---|
150 | |
---|
151 | |
---|
152 | |
---|
153 | |
---|
154 | |
---|
155 | zNear = -bs._center.z() - bs._radius; |
---|
156 | zFar = -bs._center.z() + bs._radius; |
---|
157 | |
---|
158 | |
---|
159 | |
---|
160 | if(zNear >= zFar*_nearFarRatio) keepTraversing = false; |
---|
161 | } |
---|
162 | } |
---|
163 | |
---|
164 | |
---|
165 | if(!keepTraversing) pushDistancePair(zNear, zFar); |
---|
166 | |
---|
167 | return keepTraversing; |
---|
168 | } |
---|
169 | |
---|
170 | void CURRENT_CLASS::apply(osg::Node &node) |
---|
171 | { |
---|
172 | if(shouldContinueTraversal(node)) |
---|
173 | { |
---|
174 | |
---|
175 | ++_currentDepth; |
---|
176 | traverse(node); |
---|
177 | --_currentDepth; |
---|
178 | } |
---|
179 | } |
---|
180 | |
---|
181 | void CURRENT_CLASS::apply(osg::Projection &proj) |
---|
182 | { |
---|
183 | if(shouldContinueTraversal(proj)) |
---|
184 | { |
---|
185 | |
---|
186 | _projectionMatrices.push_back(proj.getMatrix()); |
---|
187 | pushLocalFrustum(); |
---|
188 | |
---|
189 | |
---|
190 | ++_currentDepth; |
---|
191 | traverse(proj); |
---|
192 | --_currentDepth; |
---|
193 | |
---|
194 | |
---|
195 | _localFrusta.pop_back(); |
---|
196 | _bbCorners.pop_back(); |
---|
197 | _projectionMatrices.pop_back(); |
---|
198 | } |
---|
199 | } |
---|
200 | |
---|
201 | void CURRENT_CLASS::apply(osg::Transform &transform) |
---|
202 | { |
---|
203 | if(shouldContinueTraversal(transform)) |
---|
204 | { |
---|
205 | |
---|
206 | osg::Matrix currMatrix = _viewMatrices.back(); |
---|
207 | bool pushMatrix = transform.computeLocalToWorldMatrix(currMatrix, this); |
---|
208 | |
---|
209 | if(pushMatrix) |
---|
210 | { |
---|
211 | |
---|
212 | _viewMatrices.push_back(currMatrix); |
---|
213 | pushLocalFrustum(); |
---|
214 | } |
---|
215 | |
---|
216 | ++_currentDepth; |
---|
217 | traverse(transform); |
---|
218 | --_currentDepth; |
---|
219 | |
---|
220 | if(pushMatrix) |
---|
221 | { |
---|
222 | |
---|
223 | _localFrusta.pop_back(); |
---|
224 | _bbCorners.pop_back(); |
---|
225 | _viewMatrices.pop_back(); |
---|
226 | } |
---|
227 | } |
---|
228 | } |
---|
229 | |
---|
230 | void CURRENT_CLASS::apply(osg::Geode &geode) |
---|
231 | { |
---|
232 | |
---|
233 | |
---|
234 | if(shouldContinueTraversal(geode)) |
---|
235 | { |
---|
236 | osg::Drawable *drawable; |
---|
237 | double zNear, zFar; |
---|
238 | |
---|
239 | |
---|
240 | for(unsigned int i = 0; i < geode.getNumDrawables(); ++i) |
---|
241 | { |
---|
242 | drawable = geode.getDrawable(i); |
---|
243 | |
---|
244 | const osg::BoundingBox &bb = drawable->getBound(); |
---|
245 | if(bb.valid()) |
---|
246 | { |
---|
247 | |
---|
248 | if(!_localFrusta.back().contains(bb)) continue; |
---|
249 | |
---|
250 | |
---|
251 | zNear = distance(bb.corner(_bbCorners.back().first), |
---|
252 | _viewMatrices.back()); |
---|
253 | zFar = distance(bb.corner(_bbCorners.back().second), |
---|
254 | _viewMatrices.back()); |
---|
255 | if(zNear > zFar) std::swap(zNear, zFar); |
---|
256 | pushDistancePair(zNear, zFar); |
---|
257 | } |
---|
258 | } |
---|
259 | } |
---|
260 | } |
---|
261 | |
---|
262 | void CURRENT_CLASS::setMatrices(const osg::Matrix &modelview, |
---|
263 | const osg::Matrix &projection) |
---|
264 | { |
---|
265 | _modelview = modelview; |
---|
266 | _projection = projection; |
---|
267 | } |
---|
268 | |
---|
269 | void CURRENT_CLASS::reset() |
---|
270 | { |
---|
271 | |
---|
272 | _distancePairs.clear(); |
---|
273 | _cameraPairs.clear(); |
---|
274 | _limits.first = DBL_MAX; |
---|
275 | _limits.second = 0.0; |
---|
276 | _currentDepth = 0; |
---|
277 | |
---|
278 | |
---|
279 | _viewMatrices.clear(); |
---|
280 | _viewMatrices.push_back(_modelview); |
---|
281 | |
---|
282 | |
---|
283 | _projectionMatrices.clear(); |
---|
284 | _projectionMatrices.push_back(_projection); |
---|
285 | |
---|
286 | |
---|
287 | _localFrusta.clear(); |
---|
288 | _bbCorners.clear(); |
---|
289 | pushLocalFrustum(); |
---|
290 | } |
---|
291 | |
---|
292 | void CURRENT_CLASS::computeCameraPairs() |
---|
293 | { |
---|
294 | |
---|
295 | if(_distancePairs.empty()) return; |
---|
296 | |
---|
297 | |
---|
298 | if(_limits.first >= _limits.second*_nearFarRatio) |
---|
299 | { |
---|
300 | _cameraPairs.push_back(_limits); |
---|
301 | return; |
---|
302 | } |
---|
303 | |
---|
304 | PairList::iterator i,j; |
---|
305 | |
---|
306 | |
---|
307 | std::sort(_distancePairs.begin(), _distancePairs.end(), precedes); |
---|
308 | |
---|
309 | |
---|
310 | |
---|
311 | PairList combinedPairs; |
---|
312 | DistancePair currPair = _distancePairs.front(); |
---|
313 | for(i = _distancePairs.begin(); i != _distancePairs.end(); ++i) |
---|
314 | { |
---|
315 | |
---|
316 | |
---|
317 | if(i->second < 0.99*currPair.first) |
---|
318 | { |
---|
319 | combinedPairs.push_back(currPair); |
---|
320 | currPair = *i; |
---|
321 | } |
---|
322 | |
---|
323 | |
---|
324 | |
---|
325 | else |
---|
326 | currPair.first = std::min(i->first, currPair.first); |
---|
327 | } |
---|
328 | combinedPairs.push_back(currPair); |
---|
329 | |
---|
330 | |
---|
331 | |
---|
332 | double currNearLimit, numSegs, new_ratio; |
---|
333 | double ratio_invlog = 1.0/log(_nearFarRatio); |
---|
334 | unsigned int temp; |
---|
335 | for(i = combinedPairs.begin(); i != combinedPairs.end(); ++i) |
---|
336 | { |
---|
337 | currPair = *i; |
---|
338 | |
---|
339 | |
---|
340 | |
---|
341 | currNearLimit = currPair.second*_nearFarRatio; |
---|
342 | if(currPair.first >= currNearLimit) numSegs = 1.0; |
---|
343 | else |
---|
344 | { |
---|
345 | numSegs = log(currPair.first/currPair.second)*ratio_invlog; |
---|
346 | |
---|
347 | |
---|
348 | |
---|
349 | for(temp = (unsigned int)(-floor(-numSegs)); temp > 1; temp--) |
---|
350 | { |
---|
351 | currNearLimit *= _nearFarRatio; |
---|
352 | } |
---|
353 | } |
---|
354 | |
---|
355 | |
---|
356 | for(j = i+1; j != combinedPairs.end(); ++j) |
---|
357 | { |
---|
358 | |
---|
359 | if(j->first < currNearLimit) break; |
---|
360 | } |
---|
361 | |
---|
362 | |
---|
363 | |
---|
364 | if(i != j-1) |
---|
365 | { |
---|
366 | i = j-1; |
---|
367 | currPair.first = i->first; |
---|
368 | if(currPair.first >= currPair.second*_nearFarRatio) numSegs = 1.0; |
---|
369 | else numSegs = log(currPair.first/currPair.second)*ratio_invlog; |
---|
370 | } |
---|
371 | |
---|
372 | |
---|
373 | |
---|
374 | |
---|
375 | |
---|
376 | |
---|
377 | |
---|
378 | |
---|
379 | if(numSegs < 10.0) numSegs = floor(numSegs + 1.0 - 0.1*floor(numSegs)); |
---|
380 | else numSegs = floor(numSegs); |
---|
381 | |
---|
382 | |
---|
383 | |
---|
384 | |
---|
385 | new_ratio = pow(currPair.first/currPair.second, 1.0/numSegs); |
---|
386 | |
---|
387 | |
---|
388 | for(temp = (unsigned int)numSegs; temp > 0; temp--) |
---|
389 | { |
---|
390 | currPair.first = currPair.second*new_ratio; |
---|
391 | _cameraPairs.push_back(currPair); |
---|
392 | currPair.second = currPair.first; |
---|
393 | } |
---|
394 | } |
---|
395 | } |
---|
396 | |
---|
397 | void CURRENT_CLASS::setNearFarRatio(double ratio) |
---|
398 | { |
---|
399 | if(ratio <= 0.0 || ratio >= 1.0) return; |
---|
400 | _nearFarRatio = ratio; |
---|
401 | } |
---|
402 | #undef CURRENT_CLASS |
---|