// This is the default operation of the IntersectActor NewActionTypes::ResultE enterDefault(NodeCorePtr pNodeCore, ActorBase::FunctorArgumentType &funcArg) { IntersectActor *pIA = dynamic_cast<IntersectActor *>(funcArg.getActor()); pIA->setupChildrenPriorities(); return NewActionTypes::Continue; }
NewActionTypes::ResultE Joint::intersectActorEnter( ActorBase::FunctorArgumentType &funcArg) { IntersectActor *pIA = dynamic_cast<IntersectActor *>( funcArg.getActor()); Matrix matrix = this->getMatrix(); Line transLine; Pnt3f pos; Vec3f dir; matrix.mult(this->getJointTransformation()); matrix.invert(); matrix.multFull(pIA->getRay().getPosition (), pos); matrix.mult (pIA->getRay().getDirection(), dir); transLine.setValue(pos, dir); pIA->beginEditState(); { pIA->setRay (transLine ); pIA->setScaleFactor(pIA->getScaleFactor() / dir.length()); } pIA->endEditState (); pIA->setupChildrenPriorities(); return NewActionTypes::Continue; }
NewActionTypes::ResultE Joint::intersectActorLeave( ActorBase::FunctorArgumentType &funcArg) { IntersectActor *pIA = dynamic_cast<IntersectActor *>( funcArg.getActor()); const Matrix &matrix = this->getMatrix(); Pnt3f pos; Vec3f dir; matrix.mult(this->getJointTransformation()); matrix.multFull(pIA->getRay().getPosition (), pos); matrix.mult (pIA->getRay().getDirection(), dir); pIA->beginEditState(); { pIA->setRay (Line(pos, dir) ); pIA->setScaleFactor(pIA->getScaleFactor() / dir.length()); } pIA->endEditState (); return NewActionTypes::Continue; }
int main(int argc, char *argv[]) { osgLogP->setLogLevel(LOG_NOTICE); osgInit(argc, argv); int winid = setupGLUT(&argc, argv); // create a GLUT window GLUTWindowPtr gwin = GLUTWindow::create(); gwin->setId(winid); gwin->init(); osgLogP->setLogLevel(LOG_DEBUG); // build the test scene NodePtr pRoot = Node ::create(); GroupPtr pRootCore = Group::create(); NodePtr pRayGeo = Node ::create(); NodePtr pScene = buildGraph(); GroupPtr pSceneCore = Group::create(); Time tStart; Time tStop; Time tDFTotal = 0.0; Time tDFSTotal = 0.0; Time tPTotal = 0.0; Time tOTotal = 0.0; StatCollector statP; StatCollector statDF; StatCollector statDFS; beginEditCP(pRoot, Node::CoreFieldId | Node::ChildrenFieldId); pRoot->setCore (pRootCore ); pRoot->addChild(pScene ); pRoot->addChild(pRayGeo ); endEditCP (pRoot, Node::CoreFieldId | Node::ChildrenFieldId); createRays(uiNumRays, testRays); // build the geometry to visualize the rays pPoints = GeoPositions3f::create(); beginEditCP(pPoints); pPoints->addValue(Pnt3f(0.0, 0.0, 0.0)); pPoints->addValue(Pnt3f(0.0, 0.0, 0.0)); pPoints->addValue(Pnt3f(0.0, 0.0, 0.0)); pPoints->addValue(Pnt3f(0.0, 0.0, 0.0)); pPoints->addValue(Pnt3f(0.0, 0.0, 0.0)); endEditCP (pPoints); GeoIndicesUI32Ptr pIndices = GeoIndicesUI32::create(); beginEditCP(pIndices); pIndices->addValue(0); pIndices->addValue(1); pIndices->addValue(2); pIndices->addValue(3); pIndices->addValue(4); endEditCP (pIndices); GeoPLengthsPtr pLengths = GeoPLengthsUI32::create(); beginEditCP(pLengths); pLengths->addValue(2); pLengths->addValue(3); endEditCP (pLengths); GeoPTypesPtr pTypes = GeoPTypesUI8::create(); beginEditCP(pTypes); pTypes->addValue(GL_LINES ); pTypes->addValue(GL_TRIANGLES); endEditCP (pTypes); GeoColors3fPtr pColors = GeoColors3f::create(); beginEditCP(pColors); pColors->addValue(Color3f(1.0, 1.0, 1.0)); pColors->addValue(Color3f(1.0, 0.0, 0.0)); pColors->addValue(Color3f(1.0, 0.0, 0.0)); pColors->addValue(Color3f(1.0, 0.0, 0.0)); pColors->addValue(Color3f(1.0, 0.0, 0.0)); endEditCP (pColors); SimpleMaterialPtr pMaterial = SimpleMaterial::create(); beginEditCP(pMaterial); pMaterial->setLit(false); endEditCP (pMaterial); GeometryPtr pRayGeoCore = Geometry::create(); beginEditCP(pRayGeoCore); pRayGeoCore->setPositions(pPoints ); pRayGeoCore->setIndices (pIndices ); pRayGeoCore->setLengths (pLengths ); pRayGeoCore->setTypes (pTypes ); pRayGeoCore->setColors (pColors ); pRayGeoCore->setMaterial (pMaterial); endEditCP (pRayGeoCore); beginEditCP(pRayGeo, Node::CoreFieldId); pRayGeo->setCore(pRayGeoCore); endEditCP (pRayGeo, Node::CoreFieldId); IntersectActor::regDefaultClassEnter( osgTypedFunctionFunctor2CPtr< NewActionTypes::ResultE, NodeCorePtr, ActorBase::FunctorArgumentType & >(enterDefault)); NewActionBase *pDFAction = DepthFirstAction ::create(); NewActionBase *pDFSAction = DepthFirstStateAction::create(); NewActionBase *pPAction = PriorityAction ::create(); IntersectActor *pIActorDF = IntersectActor ::create(); IntersectActor *pIActorDFS = IntersectActor ::create(); IntersectActor *pIActorP = IntersectActor ::create(); pDFAction ->setStatistics(&statDF ); pDFSAction->setStatistics(&statDFS); pPAction ->setStatistics(&statP ); // IntersectActor with DFS-Action does not need leave calls pIActorDFS->setLeaveNodeFlag(false); pDFAction ->addActor(pIActorDF ); pDFSAction->addActor(pIActorDFS); pPAction ->addActor(pIActorP ); // create old action IntersectAction *pIntAction = IntersectAction ::create(); // make sure bv are up to date pScene->updateVolume(); SINFO << "-=< Intersect >=-" << endLog; std::vector<Line>::iterator itRays = testRays.begin(); std::vector<Line>::iterator endRays = testRays.end (); for(; itRays != endRays; ++itRays) { // DepthFirst tStart = getSystemTime(); pIActorDF->setRay (*itRays); pIActorDF->setMaxDistance(10000.0); pIActorDF->reset ( ); pDFAction->apply(pScene); tStop = getSystemTime(); tDFTotal += (tStop - tStart); if(pIActorDF->getHit() == true) { IntersectResult result; result._hit = true; result._pObj = pIActorDF->getHitObject (); result._tri = pIActorDF->getHitTriangleIndex(); result._dist = pIActorDF->getHitDistance (); result._time = (tStop - tStart); resultsDF.push_back(result); } else { IntersectResult result; result._hit = false; result._pObj = NullFC; result._tri = -1; result._dist = 0.0; result._time = (tStop - tStart); resultsDF.push_back(result); } std::string strStatDF; statDF.putToString(strStatDF); //SINFO << "stat DF: " << strStatDF << endLog; // Depth First State tStart = getSystemTime(); pIActorDFS->setRay (*itRays); pIActorDFS->setMaxDistance(10000.0); pIActorDFS->reset ( ); pDFSAction->apply(pScene); tStop = getSystemTime(); tDFSTotal += (tStop - tStart); if(pIActorDFS->getHit() == true) { IntersectResult result; result._hit = true; result._pObj = pIActorDFS->getHitObject (); result._tri = pIActorDFS->getHitTriangleIndex(); result._dist = pIActorDFS->getHitDistance (); result._time = (tStop - tStart); resultsDFS.push_back(result); } else { IntersectResult result; result._hit = false; result._pObj = NullFC; result._tri = -1; result._dist = 0.0; result._time = (tStop - tStart); resultsDFS.push_back(result); } std::string strStatDFS; statDFS.putToString(strStatDFS); //SINFO << "stat DFS: " << strStatDFS << endLog; // Priority tStart = getSystemTime(); pIActorP->setRay (*itRays); pIActorP->setMaxDistance(10000.0); pIActorP->reset ( ); pPAction->apply(pScene); tStop = getSystemTime(); tPTotal += (tStop - tStart); if(pIActorP->getHit() == true) { IntersectResult result; result._hit = true; result._pObj = pIActorP->getHitObject (); result._tri = pIActorP->getHitTriangleIndex(); result._dist = pIActorP->getHitDistance (); result._time = (tStop - tStart); resultsP.push_back(result); } else { IntersectResult result; result._hit = false; result._pObj = NullFC; result._tri = -1; result._dist = 0.0; result._time = (tStop - tStart); resultsP.push_back(result); } std::string strStatP; statP.putToString(strStatP); //SINFO << "stat P: " << strStatP << endLog; // Old tStart = getSystemTime(); pIntAction->setLine(*itRays, 100000); pIntAction->apply (pScene ); tStop = getSystemTime(); tOTotal += (tStop - tStart); if(pIntAction->didHit() == true) { IntersectResult result; result._hit = true; result._pObj = pIntAction->getHitObject (); result._tri = pIntAction->getHitTriangle(); result._dist = pIntAction->getHitT (); result._time = (tStop - tStart); resultsO.push_back(result); } else { IntersectResult result; result._hit = false; result._pObj = NullFC; result._tri = -1; result._dist = 0.0; result._time = (tStop - tStart); resultsO.push_back(result); } } UInt32 DFwins = 0; UInt32 DFwinsHit = 0; UInt32 DFwinsMiss = 0; UInt32 DFSwins = 0; UInt32 DFSwinsHit = 0; UInt32 DFSwinsMiss = 0; UInt32 Pwins = 0; UInt32 PwinsHit = 0; UInt32 PwinsMiss = 0; UInt32 Owins = 0; UInt32 OwinsHit = 0; UInt32 OwinsMiss = 0; UInt32 failCount = 0; UInt32 passCount = 0; UInt32 hitCount = 0; UInt32 missCount = 0; for(UInt32 i = 0; i < uiNumRays; ++i) { bool DFfastest = ((resultsDF [i]._time <= resultsDFS[i]._time) && (resultsDF [i]._time <= resultsP [i]._time) && (resultsDF [i]._time <= resultsO [i]._time) ); bool DFSfastest = ((resultsDFS[i]._time <= resultsDF [i]._time) && (resultsDFS[i]._time <= resultsP [i]._time) && (resultsDFS[i]._time <= resultsO [i]._time) ); bool Pfastest = ((resultsP [i]._time <= resultsDF [i]._time) && (resultsP [i]._time <= resultsDFS[i]._time) && (resultsP [i]._time <= resultsO [i]._time) ); bool Ofastest = ((resultsO [i]._time <= resultsDF [i]._time) && (resultsO [i]._time <= resultsDFS[i]._time) && (resultsO [i]._time <= resultsP [i]._time) ); if((resultsDF [i]._hit == resultsDFS[i]._hit) && (resultsDFS[i]._hit == resultsP [i]._hit) && (resultsP [i]._hit == resultsO [i]._hit) ) { if((osgabs(resultsDF [i]._dist - resultsDFS[i]._dist) >= 0.001) || (osgabs(resultsDFS[i]._dist - resultsP [i]._dist) >= 0.001) || (osgabs(resultsP [i]._dist - resultsO [i]._dist) >= 0.001) || (osgabs(resultsO [i]._dist - resultsDF [i]._dist) >= 0.001) ) { ++failCount; SINFO << "FAIL: df: " << resultsDF [i]._dist << " dfs: " << resultsDFS[i]._dist << " p: " << resultsP [i]._dist << " o: " << resultsO [i]._dist << endLog; SINFO << "FAIL: df: " << resultsDF [i]._tri << " dfs: " << resultsDFS[i]._tri << " p: " << resultsP [i]._tri << " o: " << resultsO [i]._tri << endLog; } else { ++passCount; } if(resultsDF[i]._hit == true) { ++hitCount; DFwinsHit = DFfastest ? DFwinsHit + 1 : DFwinsHit; DFSwinsHit = DFSfastest ? DFSwinsHit + 1 : DFSwinsHit; PwinsHit = Pfastest ? PwinsHit + 1 : PwinsHit; OwinsHit = Ofastest ? OwinsHit + 1 : OwinsHit; } else { ++missCount; DFwinsMiss = DFfastest ? DFwinsMiss + 1 : DFwinsMiss; DFSwinsMiss = DFSfastest ? DFSwinsMiss + 1 : DFSwinsMiss; PwinsMiss = Pfastest ? PwinsMiss + 1 : PwinsMiss; OwinsMiss = Ofastest ? OwinsMiss + 1 : OwinsMiss; } DFwins = DFfastest ? DFwins + 1 : DFwins; DFSwins = DFSfastest ? DFSwins + 1 : DFSwins; Pwins = Pfastest ? Pwins + 1 : Pwins; Owins = Ofastest ? Owins + 1 : Owins; } else { ++failCount; } //SINFO << i << " \t" << (DFfastest ? "D ->" : " ") << " hit: " << resultsDF [i]._hit << " time: " << resultsDF [i]._time << endLog; //SINFO << " \t" << (DFSfastest ? "S ->" : " ") << " hit: " << resultsDFS[i]._hit << " time: " << resultsDFS[i]._time << endLog; //SINFO << " \t" << (Pfastest ? "P ->" : " ") << " hit: " << resultsP [i]._hit << " time: " << resultsP [i]._time << endLog; //SINFO << " \t" << (Ofastest ? "O ->" : " ") << " hit: " << resultsO [i]._hit << " time: " << resultsO [i]._time << endLog; } SINFO << " df total: " << tDFTotal << (tDFTotal < tDFSTotal && tDFTotal < tPTotal && tDFTotal < tOTotal ? " *" : " ") << " wins: " << DFwins << " (" << (static_cast<Real32>(DFwins) / static_cast<Real32>(passCount)) * 100.0 << "%)\t" << " wins on hit: " << DFwinsHit << " (" << (static_cast<Real32>(DFwinsHit) / static_cast<Real32>(hitCount )) * 100.0 << "%)\t" << " wins on miss: " << DFwinsMiss << " (" << (static_cast<Real32>(DFwinsMiss) / static_cast<Real32>(missCount)) * 100.0 << "%)" << endLog; SINFO << " dfs total: " << tDFSTotal << (tDFSTotal < tDFTotal && tDFSTotal < tPTotal && tDFSTotal < tOTotal ? " *" : " ") << " wins: " << DFSwins << " (" << (static_cast<Real32>(DFSwins) / static_cast<Real32>(passCount)) * 100.0 << "%)\t" << " wins on hit: " << DFSwinsHit << " (" << (static_cast<Real32>(DFSwinsHit) / static_cast<Real32>(hitCount )) * 100.0 << "%)\t" << " wins on miss: " << DFSwinsMiss << " (" << (static_cast<Real32>(DFSwinsMiss) / static_cast<Real32>(missCount)) * 100.0 << "%)" << endLog; SINFO << " p total: " << tPTotal << (tPTotal < tDFTotal && tPTotal < tDFSTotal && tPTotal < tOTotal ? " *" : " ") << " wins: " << Pwins << " (" << (static_cast<Real32>(Pwins) / static_cast<Real32>(passCount)) * 100.0 << "%)\t" << " wins on hit: " << PwinsHit << " (" << (static_cast<Real32>(PwinsHit) / static_cast<Real32>(hitCount )) * 100.0 << "%)\t" << " wins on miss: " << PwinsMiss << " (" << (static_cast<Real32>(PwinsMiss) / static_cast<Real32>(missCount)) * 100.0 << "%)" << endLog; SINFO << " o total: " << tOTotal << (tOTotal < tDFTotal && tOTotal < tDFSTotal && tOTotal < tPTotal ? " *" : " ") << " wins: " << Owins << " (" << (static_cast<Real32>(Owins) / static_cast<Real32>(passCount)) * 100.0 << "%)\t" << " wins on hit: " << OwinsHit << " (" << (static_cast<Real32>(OwinsHit) / static_cast<Real32>(hitCount )) * 100.0 << "%)\t" << " wins on miss: " << OwinsMiss << " (" << (static_cast<Real32>(OwinsMiss) / static_cast<Real32>(missCount)) * 100.0 << "%)" << endLog; SINFO << "pass: "******" fail: " << failCount << " hit: " << hitCount << " miss: " << missCount << endLog; osgLogP->setLogLevel(LOG_NOTICE); #if 0 // create the SimpleSceneManager helper mgr = new SimpleSceneManager; // tell the manager what to manage mgr->setWindow(gwin ); mgr->setRoot (pRoot); // show the whole scene mgr->showAll(); // GLUT main loop glutMainLoop(); #endif return 0; }