Changeset 338
- Timestamp:
- 10/21/06 13:43:40 (2 years ago)
- Files:
-
- branches/Dirk_RenderTraversalWork/Source/System/NodeCores/Drawables/Geometry/Util/OSGGeoStatsAttachment.cpp (modified) (12 diffs)
- branches/Dirk_RenderTraversalWork/Source/System/NodeCores/Drawables/Geometry/Util/OSGGeoStatsAttachment.h (modified) (1 diff)
- branches/Dirk_RenderTraversalWork/Source/System/NodeCores/Drawables/Geometry/Util/OSGGeoStatsAttachmentTest.cpp (modified) (11 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/Dirk_RenderTraversalWork/Source/System/NodeCores/Drawables/Geometry/Util/OSGGeoStatsAttachment.cpp
r329 r338 59 59 The GeoStatsAttachment keeps track of some core Geometry Characteristics like 60 60 the number of vertices used, the number of points, lines or triangles created 61 and some information about memory consumption. It can be calculated61 and some information about memory consumption. 62 62 63 63 It is designed as an attachment so that it can be kept inside the graph. It 64 64 can also be used to keep aggregated information about subtrees, by adding up 65 the contributions of the underlying nodes. 66 67 68 \warning 65 the contributions of the underlying nodes. It can invalidate itself by using 66 changed callbacks, so that no manual bookkeeping is necessary. 67 68 \warning To use the automatic update you have to use the 69 osg::GeoStatsAttachment::addTo() or osg::GeoStatsAttachment::attachTo() 70 methods! Otherwise the necessary callbacks are not set! 71 72 \warning Before accessing the data osg::GeoStatsAttachment::validate() needs 73 to be called to calculate and aggregate the results. 69 74 70 75 */ … … 128 133 void GeoStatsAttachment::reset(void) 129 134 { 135 // Don't change it unless it's valid. 136 // Needed to protect intermediate results 137 if(!getValid()) 138 return; 139 130 140 setVertices (0); 131 141 setPoints (0); … … 135 145 setStoredAttributeBytes (0); 136 146 137 setValid( true);147 setValid(false); 138 148 } 139 149 … … 260 270 } 261 271 272 /*! Create a GeoStatsAttachment for the given Geometry 273 */ 262 274 GeoStatsAttachmentPtr GeoStatsAttachment::calcStatic(GeometryPtrArg geo) 263 275 { … … 267 279 268 280 return st; 281 } 282 283 284 /*! Access the GeoStatsAttachemnt in \a arg, if it has one. Return NullFC 285 otherwise. 286 */ 287 GeoStatsAttachmentPtr GeoStatsAttachment::get(AttachmentContainerPtr arg) 288 { 289 return cast_dynamic<GeoStatsAttachmentPtr>( 290 arg->findAttachment(GeoStatsAttachment::getClassType())); 291 292 } 293 294 /*! Access the GeoStatsAttachemnt in \a arg, if it has one. Return NullFC 295 otherwise. 296 */ 297 GeoStatsAttachmentPtr GeoStatsAttachment::get(AttachmentContainer *arg) 298 { 299 return cast_dynamic<GeoStatsAttachmentPtr>( 300 arg->findAttachment(GeoStatsAttachment::getClassType())); 301 269 302 } 270 303 … … 272 305 /* Updates */ 273 306 274 /*! Add GeoStats to the given307 /*! Add a new GeoStatsAttachment to the given \a obj. 275 308 */ 276 voidGeoStatsAttachment::addTo(AttachmentContainerPtr obj)309 GeoStatsAttachmentPtr GeoStatsAttachment::addTo(AttachmentContainerPtr obj) 277 310 { 278 311 GeoStatsAttachmentPtr st = GeoStatsAttachment::create(); 279 312 280 313 st->attachTo(obj); 281 } 282 283 /*! 314 315 return st; 316 } 317 318 /*! Attach the current GeoStatsAttachment to the given \a obj. 284 319 */ 285 320 void GeoStatsAttachment::attachTo(AttachmentContainerPtr obj) … … 303 338 obj->addAttachment(st); 304 339 305 setValid(false);340 reset(); 306 341 307 342 obj->addChangedFunctor(GeoStatsAttachment::invalidateFunctor, … … 311 346 void GeoStatsAttachment::validate(void) 312 347 { 348 commitChanges(); 349 313 350 // Still valid? Do nothing. 314 351 if(getValid()) 315 352 return; 316 353 317 commitChanges();318 319 354 AttachmentContainerPtr cont = 320 355 cast_dynamic<AttachmentContainerPtr>(getParents()[0]); … … 337 372 if(n != NullFC) 338 373 { 339 // Checkthe core374 // Validate the core 340 375 GeometryPtr g = cast_dynamic<GeometryPtr>(n->getCore()); 341 376 if(g != NullFC) 342 377 { 343 calc(g); 344 } 345 378 GeoStatsAttachmentPtr s = get(g); 379 380 if(s == NullFC) 381 { 382 s = GeoStatsAttachment::addTo(g); 383 } 384 385 s->validate(); 386 387 *this += s; 388 setValid(false); // Not done yet. 389 } 390 391 // Validate all the children 346 392 for(UInt32 i = 0; i < n->getNChildren(); ++i) 347 393 { 348 394 NodePtr c = n->getChild(i); 349 395 350 FieldContainerAttachmentPtr a = 351 c->findAttachment(GeoStatsAttachment::getClassType()); 352 353 GeoStatsAttachmentPtr s; 354 355 if(a != NullFC) 396 GeoStatsAttachmentPtr s = get(c); 397 398 if(s == NullFC) 356 399 { 357 s = cast_dynamic<GeoStatsAttachmentPtr>(a);400 s = GeoStatsAttachment::addTo(c); 358 401 } 359 else // Need to add an attachment to it360 {361 s = GeoStatsAttachment::create();362 c->addAttachment(s);363 }364 402 365 403 s->validate(); 366 404 367 405 *this += s; 368 } 369 } 406 setValid(false); // Not done yet. 407 } 408 } 409 setValid(true); // Done! 370 410 } 371 411 … … 388 428 389 429 // Find the attachment 390 FieldContainerAttachmentPtr a = 391 cont->findAttachment(GeoStatsAttachment::getClassType()); 392 393 GeoStatsAttachmentPtr st; 394 395 if(a != NullFC) 396 { 397 st = cast_dynamic<GeoStatsAttachmentPtr>(a); 398 } 399 else 400 { 401 st = GeoStatsAttachment::create(); 402 cont->addAttachment(st); 403 } 404 405 st->setValid(false); 406 430 GeoStatsAttachmentPtr st = get(cont); 431 432 if(st == NullFC) // Found the end of the chain 433 return; 434 435 // Invalidate it 436 st->reset(); 437 438 // Traverse upwards 407 439 if(st->getParents().size()) 408 440 { 409 441 FieldContainerPtr p = st->getParents()[0]; // Can't have more than 1 410 442 411 NodePtr n = cast_dynamic<NodePtr>(p); 412 443 // Is this attached to a NodeCore? 444 NodeCorePtr c = cast_dynamic<NodeCorePtr>(p); 445 if(c != NullFC) 446 { 447 MFParentFieldContainerPtr::const_iterator pnI; 448 449 for( pnI = c->getMFParents()->begin(); 450 pnI != c->getMFParents()->end (); 451 ++pnI) 452 { 453 NodePtr node = cast_dynamic<NodePtr>(*pnI); 454 invalidate(node); 455 } 456 } 457 458 // Is this attached to a Node? 459 NodePtr n = cast_dynamic<NodePtr>(p); 413 460 if(n != NullFC) 414 461 { 415 n= n->getParent();416 invalidate( n);462 NodePtr par = n->getParent(); 463 invalidate(par); 417 464 } 418 465 } … … 429 476 setStoredAttributeBytes (getStoredAttributeBytes() + 430 477 arg->getStoredAttributeBytes()); 478 setValid(true); 431 479 } 432 480 … … 441 489 setStoredAttributeBytes (getStoredAttributeBytes() - 442 490 arg->getStoredAttributeBytes()); 491 setValid(true); 443 492 } 444 493 branches/Dirk_RenderTraversalWork/Source/System/NodeCores/Drawables/Geometry/Util/OSGGeoStatsAttachment.h
r329 r338 101 101 /*! \} */ 102 102 /*---------------------------------------------------------------------*/ 103 /*! \name Access */ 104 /*! \{ */ 105 106 static GeoStatsAttachmentPtr get(AttachmentContainerPtr arg); 107 108 static GeoStatsAttachmentPtr get(AttachmentContainer *arg); 109 110 111 /*! \} */ 112 /*---------------------------------------------------------------------*/ 103 113 /*! \name Updating */ 104 114 /*! \{ */ 105 115 106 static voidaddTo(AttachmentContainerPtr obj);116 static GeoStatsAttachmentPtr addTo(AttachmentContainerPtr obj); 107 117 108 118 void attachTo(AttachmentContainerPtr obj); branches/Dirk_RenderTraversalWork/Source/System/NodeCores/Drawables/Geometry/Util/OSGGeoStatsAttachmentTest.cpp
r329 r338 93 93 94 94 commitChanges(); 95 96 sumVertices = sumPoints = sumLines = sumTriangles = 97 sumProcessedAttributeBytes = sumStoredAttributeBytes = 0; 98 99 for(UInt32 i = 0; i < nGeos; ++i) 100 { 101 sumVertices += geoVertices[i]; 102 sumPoints += geoPoints[i]; 103 sumLines += geoLines[i]; 104 sumTriangles += geoTriangles[i]; 105 sumProcessedAttributeBytes += geoProcessedAttributeBytes[i]; 106 sumStoredAttributeBytes += geoStoredAttributeBytes[i]; 107 108 } 95 109 } 96 110 … … 113 127 UInt32 geoStoredAttributeBytes[nGeos]; 114 128 129 // Summed stats data 130 UInt32 sumVertices; 131 UInt32 sumPoints; 132 UInt32 sumLines; 133 UInt32 sumTriangles; 134 UInt32 sumProcessedAttributeBytes; 135 UInt32 sumStoredAttributeBytes; 136 115 137 // Nodes for tree test 116 138 RefPtr<NodePtr> nodes[nGeos]; … … 119 141 }; 120 142 121 #if 0122 143 TEST_FIXTURE(GSAFixture, BasicCreationAndCalculation) 123 144 { … … 182 203 183 204 RefPtr<GeoStatsAttachmentPtr> st; 184 st = cast_dynamic<GeoStatsAttachmentPtr>( 185 geos[0]->findAttachment(GeoStatsAttachment::getClassType())); 186 187 CHECK(st != NullFC); 188 189 st->validate(); 190 191 CHECK_EQUAL(geoVertices[0], st->getVertices()); 192 CHECK_EQUAL(geoPoints[0], st->getPoints()); 193 CHECK_EQUAL(geoLines[0], st->getLines()); 194 CHECK_EQUAL(geoTriangles[0], st->getTriangles()); 195 CHECK_EQUAL(geoProcessedAttributeBytes[0], st->getProcessedAttributeBytes()); 196 CHECK_EQUAL(geoStoredAttributeBytes[0], st->getStoredAttributeBytes()); 205 st = GeoStatsAttachment::get(geos[0]); 206 207 CHECK(st != NullFC); 208 209 st->validate(); 210 211 CHECK_EQUAL(geoVertices[0], st->getVertices()); 212 CHECK_EQUAL(geoPoints[0], st->getPoints()); 213 CHECK_EQUAL(geoLines[0], st->getLines()); 214 CHECK_EQUAL(geoTriangles[0], st->getTriangles()); 215 CHECK_EQUAL(geoProcessedAttributeBytes[0], st->getProcessedAttributeBytes()); 216 CHECK_EQUAL(geoStoredAttributeBytes[0], st->getStoredAttributeBytes()); 217 } 218 219 TEST_FIXTURE(GSAFixture, CreationAndAccess) 220 { 221 GeoStatsAttachmentPtr st; 222 223 st = GeoStatsAttachment::get(geos[0]); 224 CHECK(st == NullFC); 225 226 GeoStatsAttachment::addTo(geos[0]); 227 228 st = GeoStatsAttachment::get(geos[0]); 229 230 CHECK(st != NullFC); 197 231 } 198 232 … … 202 236 203 237 RefPtr<GeoStatsAttachmentPtr> st; 204 st = cast_dynamic<GeoStatsAttachmentPtr>( 205 geos[0]->findAttachment(GeoStatsAttachment::getClassType())); 238 st = GeoStatsAttachment::get(geos[0]); 206 239 207 240 CHECK(st != NullFC); … … 209 242 st->attachTo(geos[1]); 210 243 211 st = cast_dynamic<GeoStatsAttachmentPtr>( 212 geos[0]->findAttachment(GeoStatsAttachment::getClassType())); 244 st = GeoStatsAttachment::get(geos[0]); 213 245 214 246 CHECK(st == NullFC); // Removed from old one? 215 247 216 st = cast_dynamic<GeoStatsAttachmentPtr>( 217 geos[1]->findAttachment(GeoStatsAttachment::getClassType())); 248 st = GeoStatsAttachment::get(geos[1]); 218 249 219 250 CHECK(st != NullFC); // Attached to new one? … … 238 269 239 270 RefPtr<GeoStatsAttachmentPtr> st; 240 st = cast_dynamic<GeoStatsAttachmentPtr>( 241 nodes[0]->findAttachment(GeoStatsAttachment::getClassType())); 271 st = GeoStatsAttachment::get(nodes[0]); 242 272 243 273 CHECK(st != NullFC); … … 260 290 261 291 RefPtr<GeoStatsAttachmentPtr> st; 262 st = cast_dynamic<GeoStatsAttachmentPtr>( 263 geos0Node->findAttachment(GeoStatsAttachment::getClassType())); 292 st = GeoStatsAttachment::get(geos0Node); 264 293 265 294 CHECK(st != NullFC); … … 281 310 282 311 RefPtr<GeoStatsAttachmentPtr> st; 283 st = cast_dynamic<GeoStatsAttachmentPtr>( 284 geos0Node->findAttachment(GeoStatsAttachment::getClassType())); 285 286 CHECK(st != NullFC); 287 288 st->validate(); 289 290 CHECK_EQUAL(geoVertices[0], st->getVertices()); 291 CHECK_EQUAL(geoPoints[0], st->getPoints()); 292 CHECK_EQUAL(geoLines[0], st->getLines()); 293 CHECK_EQUAL(geoTriangles[0], st->getTriangles()); 294 CHECK_EQUAL(geoProcessedAttributeBytes[0], st->getProcessedAttributeBytes()); 295 CHECK_EQUAL(geoStoredAttributeBytes[0], st->getStoredAttributeBytes()); 296 } 297 #endif 312 st = GeoStatsAttachment::get(geos0Node); 313 314 CHECK(st != NullFC); 315 316 st->validate(); 317 318 CHECK_EQUAL(geoVertices[0], st->getVertices()); 319 CHECK_EQUAL(geoPoints[0], st->getPoints()); 320 CHECK_EQUAL(geoLines[0], st->getLines()); 321 CHECK_EQUAL(geoTriangles[0], st->getTriangles()); 322 CHECK_EQUAL(geoProcessedAttributeBytes[0], st->getProcessedAttributeBytes()); 323 CHECK_EQUAL(geoStoredAttributeBytes[0], st->getStoredAttributeBytes()); 324 } 298 325 299 326 // Automatic Invalidation 300 TEST_FIXTURE(GSAFixture, AutoInvalidate OnGeometryChange)327 TEST_FIXTURE(GSAFixture, AutoInvalidateGeometryOnChange) 301 328 { 302 329 GeoStatsAttachment::addTo(geos[0]); 303 330 304 331 RefPtr<GeoStatsAttachmentPtr> st; 305 st = cast_dynamic<GeoStatsAttachmentPtr>( 306 geos[0]->findAttachment(GeoStatsAttachment::getClassType())); 332 st = GeoStatsAttachment::get(geos[0]); 307 333 308 334 CHECK(st != NullFC); … … 319 345 CHECK_EQUAL(geoStoredAttributeBytes[0], st->getStoredAttributeBytes()); 320 346 321 geos[0]->getTypes()->push_back(GL_LINE S);347 geos[0]->getTypes()->push_back(GL_LINE_STRIP); 322 348 geos[0]->getLengths()->push_back(3); 323 349 geos[0]->getIndex(0)->push_back(0); … … 342 368 } 343 369 344 } 370 // Automatic Invalidation 371 TEST_FIXTURE(GSAFixture, AutoInvalidateNodeOnChange) 372 { 373 GeoStatsAttachment::addTo(nodes[0]); 374 375 RefPtr<GeoStatsAttachmentPtr> st; 376 st = GeoStatsAttachment::get(nodes[0]); 377 378 CHECK(st != NullFC); 379 380 st->validate(); 381 382 commitChanges(); 383 384 CHECK_EQUAL(geoVertices[0], st->getVertices()); 385 CHECK_EQUAL(geoPoints[0], st->getPoints()); 386 CHECK_EQUAL(geoLines[0], st->getLines()); 387 CHECK_EQUAL(geoTriangles[0], st->getTriangles()); 388 CHECK_EQUAL(geoProcessedAttributeBytes[0], st->getProcessedAttributeBytes()); 389 CHECK_EQUAL(geoStoredAttributeBytes[0], st->getStoredAttributeBytes()); 390 391 geos[0]->getTypes()->push_back(GL_LINE_STRIP); 392 geos[0]->getLengths()->push_back(3); 393 geos[0]->getIndex(0)->push_back(0); 394 geos[0]->getIndex(0)->push_back(1); 395 geos[0]->getIndex(0)->push_back(2); 396 397 commitChanges(); 398 399 CHECK_EQUAL(st->getValid(), false); 400 401 st->validate(); 402 403 CHECK_EQUAL(st->getValid(), true); 404 405 CHECK_EQUAL(geoVertices[0] + 3, st->getVertices()); 406 CHECK_EQUAL(geoPoints[0], st->getPoints()); 407 CHECK_EQUAL(geoLines[0] + 2, st->getLines()); 408 CHECK_EQUAL(geoTriangles[0], st->getTriangles()); 409 CHECK_EQUAL(geoProcessedAttributeBytes[0] + sizeof(Vec3f) * 3, 410 st->getProcessedAttributeBytes()); 411 CHECK_EQUAL(geoStoredAttributeBytes[0], st->getStoredAttributeBytes()); 412 } 413 414 // Hierarchical Invalidation 415 TEST_FIXTURE(GSAFixture, AutoInvalidateHierarchy) 416 { 417 RefPtr<GeoStatsAttachmentPtr> st; 418 st = GeoStatsAttachment::get(root); 419 420 CHECK(st == NullFC); 421 422 GeoStatsAttachment::addTo(root); 423 st = GeoStatsAttachment::get(root); 424 425 CHECK(st != NullFC); 426 427 st->validate(); 428 429 CHECK_EQUAL(sumVertices, st->getVertices()); 430 CHECK_EQUAL(sumPoints, st->getPoints()); 431 CHECK_EQUAL(sumLines, st->getLines()); 432 CHECK_EQUAL(sumTriangles, st->getTriangles()); 433 CHECK_EQUAL(sumProcessedAttributeBytes, st->getProcessedAttributeBytes()); 434 CHECK_EQUAL(sumStoredAttributeBytes, st->getStoredAttributeBytes()); 435 436 geos[0]->getTypes()->push_back(GL_LINE_STRIP); 437 geos[0]->getLengths()->push_back(3); 438 geos[0]->getIndex(0)->push_back(0); 439 geos[0]->getIndex(0)->push_back(1); 440 geos[0]->getIndex(0)->push_back(2); 441 442 commitChanges(); 443 444 CHECK_EQUAL(false, st->getValid()); 445 446 st->validate(); 447 448 CHECK_EQUAL(true, st->getValid()); 449 450 CHECK_EQUAL(sumVertices + 3, st->getVertices()); 451 CHECK_EQUAL(sumPoints, st->getPoints()); 452 CHECK_EQUAL(sumLines + 2, st->getLines()); 453 CHECK_EQUAL(sumTriangles, st->getTriangles()); 454 CHECK_EQUAL(sumProcessedAttributeBytes + sizeof(Vec3f) * 3, 455 st->getProcessedAttributeBytes()); 456 CHECK_EQUAL(sumStoredAttributeBytes, st->getStoredAttributeBytes()); 457 } 458 459 }
