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