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