Changeset 338

Show
Ignore:
Timestamp:
10/21/06 13:43:40 (2 years ago)
Author:
dirk
Message:

Fixed hierarchical update for GeoStatsAttachment?

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/Dirk_RenderTraversalWork/Source/System/NodeCores/Drawables/Geometry/Util/OSGGeoStatsAttachment.cpp

    r329 r338  
    5959The GeoStatsAttachment keeps track of some core Geometry Characteristics like 
    6060the number of vertices used, the number of points, lines or triangles created 
    61 and some information about memory consumption. It can be calculated  
     61and some information about memory consumption. 
    6262 
    6363It is designed as an attachment so that it can be kept inside the graph. It 
    6464can also be used to keep aggregated information about subtrees, by adding up 
    65 the contributions of the underlying nodes. 
    66  
    67  
    68 \warning  
     65the contributions of the underlying nodes. It can invalidate itself by using 
     66changed callbacks, so that no manual bookkeeping is necessary. 
     67 
     68\warning To use the automatic update you have to use the 
     69osg::GeoStatsAttachment::addTo() or osg::GeoStatsAttachment::attachTo() 
     70methods! Otherwise the necessary callbacks are not set! 
     71 
     72\warning Before accessing the data osg::GeoStatsAttachment::validate() needs 
     73to be called to calculate and aggregate the results. 
    6974 
    7075*/ 
     
    128133void GeoStatsAttachment::reset(void) 
    129134{ 
     135    // Don't change it unless it's valid. 
     136    // Needed to protect intermediate results 
     137    if(!getValid()) 
     138        return;  
     139         
    130140    setVertices               (0); 
    131141    setPoints                 (0); 
     
    135145    setStoredAttributeBytes   (0); 
    136146 
    137     setValid(true);     
     147    setValid(false);     
    138148} 
    139149 
     
    260270} 
    261271 
     272/*! Create a GeoStatsAttachment for the given Geometry 
     273*/ 
    262274GeoStatsAttachmentPtr GeoStatsAttachment::calcStatic(GeometryPtrArg geo) 
    263275{ 
     
    267279     
    268280    return st; 
     281} 
     282 
     283 
     284/*! Access the GeoStatsAttachemnt in \a arg, if it has one. Return NullFC 
     285otherwise. 
     286*/ 
     287GeoStatsAttachmentPtr 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 
     295otherwise. 
     296*/ 
     297GeoStatsAttachmentPtr GeoStatsAttachment::get(AttachmentContainer *arg) 
     298{ 
     299    return cast_dynamic<GeoStatsAttachmentPtr>( 
     300            arg->findAttachment(GeoStatsAttachment::getClassType())); 
     301 
    269302} 
    270303 
     
    272305/*                              Updates                                   */ 
    273306 
    274 /*! Add GeoStats to the given  
     307/*! Add a new GeoStatsAttachment to the given \a obj. 
    275308*/ 
    276 void GeoStatsAttachment::addTo(AttachmentContainerPtr obj) 
     309GeoStatsAttachmentPtr GeoStatsAttachment::addTo(AttachmentContainerPtr obj) 
    277310{ 
    278311    GeoStatsAttachmentPtr st = GeoStatsAttachment::create(); 
    279312     
    280313    st->attachTo(obj); 
    281 
    282  
    283 /*! 
     314     
     315    return st; 
     316
     317 
     318/*! Attach the current GeoStatsAttachment to the given \a obj. 
    284319*/ 
    285320void GeoStatsAttachment::attachTo(AttachmentContainerPtr obj) 
     
    303338    obj->addAttachment(st); 
    304339     
    305     setValid(false); 
     340    reset(); 
    306341     
    307342    obj->addChangedFunctor(GeoStatsAttachment::invalidateFunctor,  
     
    311346void GeoStatsAttachment::validate(void) 
    312347{ 
     348    commitChanges(); 
     349     
    313350    // Still valid? Do nothing. 
    314351    if(getValid()) 
    315352        return;  
    316353         
    317     commitChanges(); 
    318      
    319354    AttachmentContainerPtr cont =  
    320355        cast_dynamic<AttachmentContainerPtr>(getParents()[0]); 
     
    337372    if(n != NullFC) 
    338373    { 
    339         // Check the core 
     374        // Validate the core 
    340375        GeometryPtr g = cast_dynamic<GeometryPtr>(n->getCore()); 
    341376        if(g != NullFC) 
    342377        { 
    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 
    346392        for(UInt32 i = 0; i < n->getNChildren(); ++i) 
    347393        { 
    348394            NodePtr c = n->getChild(i); 
    349395             
    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) 
    356399            { 
    357                 s = cast_dynamic<GeoStatsAttachmentPtr>(a); 
     400                s = GeoStatsAttachment::addTo(c); 
    358401            } 
    359             else // Need to add an attachment to it 
    360             { 
    361                 s = GeoStatsAttachment::create(); 
    362                 c->addAttachment(s); 
    363             } 
    364402             
    365403            s->validate(); 
    366404             
    367405            *this += s; 
    368         } 
    369     } 
     406            setValid(false); // Not done yet. 
     407        } 
     408    } 
     409    setValid(true); // Done! 
    370410} 
    371411 
     
    388428     
    389429    // 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 
    407439    if(st->getParents().size()) 
    408440    { 
    409441        FieldContainerPtr p = st->getParents()[0]; // Can't have more than 1 
    410442         
    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);         
    413460        if(n != NullFC) 
    414461        { 
    415             n = n->getParent(); 
    416             invalidate(n); 
     462            NodePtr par = n->getParent(); 
     463            invalidate(par); 
    417464        } 
    418465    } 
     
    429476    setStoredAttributeBytes   (getStoredAttributeBytes() +  
    430477                                   arg->getStoredAttributeBytes()); 
     478    setValid(true); 
    431479} 
    432480 
     
    441489    setStoredAttributeBytes   (getStoredAttributeBytes() -  
    442490                                   arg->getStoredAttributeBytes()); 
     491    setValid(true); 
    443492} 
    444493 
  • branches/Dirk_RenderTraversalWork/Source/System/NodeCores/Drawables/Geometry/Util/OSGGeoStatsAttachment.h

    r329 r338  
    101101    /*! \}                                                                 */ 
    102102    /*---------------------------------------------------------------------*/ 
     103    /*! \name                     Access                                   */ 
     104    /*! \{                                                                 */ 
     105 
     106    static GeoStatsAttachmentPtr    get(AttachmentContainerPtr  arg); 
     107 
     108    static GeoStatsAttachmentPtr    get(AttachmentContainer    *arg); 
     109 
     110 
     111    /*! \}                                                                 */ 
     112    /*---------------------------------------------------------------------*/ 
    103113    /*! \name                    Updating                                  */ 
    104114    /*! \{                                                                 */ 
    105115 
    106     static void addTo(AttachmentContainerPtr obj); 
     116    static GeoStatsAttachmentPtr addTo(AttachmentContainerPtr obj); 
    107117 
    108118    void attachTo(AttachmentContainerPtr obj); 
  • branches/Dirk_RenderTraversalWork/Source/System/NodeCores/Drawables/Geometry/Util/OSGGeoStatsAttachmentTest.cpp

    r329 r338  
    9393         
    9494        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        } 
    95109    } 
    96110     
     
    113127    UInt32 geoStoredAttributeBytes[nGeos]; 
    114128     
     129    // Summed stats data 
     130    UInt32 sumVertices; 
     131    UInt32 sumPoints; 
     132    UInt32 sumLines; 
     133    UInt32 sumTriangles; 
     134    UInt32 sumProcessedAttributeBytes; 
     135    UInt32 sumStoredAttributeBytes; 
     136     
    115137    // Nodes for tree test 
    116138    RefPtr<NodePtr> nodes[nGeos]; 
     
    119141}; 
    120142 
    121 #if 0 
    122143TEST_FIXTURE(GSAFixture, BasicCreationAndCalculation) 
    123144{ 
     
    182203     
    183204    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 
     219TEST_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); 
    197231} 
    198232 
     
    202236     
    203237    RefPtr<GeoStatsAttachmentPtr> st; 
    204     st = cast_dynamic<GeoStatsAttachmentPtr>( 
    205             geos[0]->findAttachment(GeoStatsAttachment::getClassType())); 
     238    st = GeoStatsAttachment::get(geos[0]); 
    206239 
    207240    CHECK(st != NullFC); 
     
    209242    st->attachTo(geos[1]); 
    210243 
    211     st = cast_dynamic<GeoStatsAttachmentPtr>( 
    212             geos[0]->findAttachment(GeoStatsAttachment::getClassType())); 
     244    st = GeoStatsAttachment::get(geos[0]); 
    213245 
    214246    CHECK(st == NullFC);   // Removed from old one?  
    215247 
    216     st = cast_dynamic<GeoStatsAttachmentPtr>( 
    217             geos[1]->findAttachment(GeoStatsAttachment::getClassType())); 
     248    st = GeoStatsAttachment::get(geos[1]); 
    218249 
    219250    CHECK(st != NullFC);    // Attached to new one?  
     
    238269     
    239270    RefPtr<GeoStatsAttachmentPtr> st; 
    240     st = cast_dynamic<GeoStatsAttachmentPtr>( 
    241             nodes[0]->findAttachment(GeoStatsAttachment::getClassType())); 
     271    st = GeoStatsAttachment::get(nodes[0]); 
    242272 
    243273    CHECK(st != NullFC); 
     
    260290     
    261291    RefPtr<GeoStatsAttachmentPtr> st; 
    262     st = cast_dynamic<GeoStatsAttachmentPtr>( 
    263             geos0Node->findAttachment(GeoStatsAttachment::getClassType())); 
     292    st = GeoStatsAttachment::get(geos0Node); 
    264293 
    265294    CHECK(st != NullFC); 
     
    281310     
    282311    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
    298325 
    299326// Automatic Invalidation 
    300 TEST_FIXTURE(GSAFixture, AutoInvalidateOnGeometryChange) 
     327TEST_FIXTURE(GSAFixture, AutoInvalidateGeometryOnChange) 
    301328{     
    302329    GeoStatsAttachment::addTo(geos[0]); 
    303330     
    304331    RefPtr<GeoStatsAttachmentPtr> st; 
    305     st = cast_dynamic<GeoStatsAttachmentPtr>( 
    306             geos[0]->findAttachment(GeoStatsAttachment::getClassType())); 
     332    st = GeoStatsAttachment::get(geos[0]); 
    307333 
    308334    CHECK(st != NullFC); 
     
    319345    CHECK_EQUAL(geoStoredAttributeBytes[0], st->getStoredAttributeBytes()); 
    320346     
    321     geos[0]->getTypes()->push_back(GL_LINES); 
     347    geos[0]->getTypes()->push_back(GL_LINE_STRIP); 
    322348    geos[0]->getLengths()->push_back(3); 
    323349    geos[0]->getIndex(0)->push_back(0); 
     
    342368} 
    343369 
    344 
     370// Automatic Invalidation 
     371TEST_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 
     415TEST_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