source: GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/src/OgreBoundingBoxConverter.cpp @ 1221

Revision 1221, 3.3 KB checked in by mattausch, 18 years ago (diff)

added intel ray tracing

Line 
1#include "OgreBoundingBoxConverter.h"
2#include "OgreTypeConverter.h"
3#include "OgreMeshInstance.h"
4#include "OgreOctreeSceneManager.h"
5#include <OgreLogManager.h>
6
7
8namespace Ogre
9{
10//-------------------------------------------------------------------------
11OgreBoundingBoxConverter::OgreBoundingBoxConverter(OctreeSceneManager *sm):
12mSceneMgr(sm)
13{
14}
15//-------------------------------------------------------------------------
16bool OgreBoundingBoxConverter::IdentifyObjects(const GtpVisibilityPreprocessor::IndexedBoundingBoxContainer &iboxes,
17                                                                                           GtpVisibilityPreprocessor::ObjectContainer &objects) const
18{
19        GtpVisibilityPreprocessor::IndexedBoundingBoxContainer::
20                const_iterator iit, iit_end = iboxes.end();
21 
22        for (iit = iboxes.begin(); iit != iit_end; ++ iit)
23        {
24                const GtpVisibilityPreprocessor::AxisAlignedBox3 box = (*iit).second;
25                const AxisAlignedBox currentBox = OgreTypeConverter::ConvertToOgre(box);
26   
27                Entity *ent = FindCorrespondingObject(currentBox);
28
29                // create new mesh instance
30                OgreMeshInstance *omi = new OgreMeshInstance(ent);
31                omi->SetId((*iit).first);
32                objects.push_back(omi);
33        }
34
35        return true;
36}
37//-------------------------------------------------------------------------
38inline static AxisAlignedBox EnlargeBox(const AxisAlignedBox &box)
39{
40        const float eps = 1e-3f;
41        const Vector3 veps(eps, eps, eps);
42
43        Vector3 max = box.getMaximum();
44        Vector3 min = box.getMinimum();
45
46        return AxisAlignedBox(min - veps, max + veps);
47}
48//-----------------------------------------------------------------------
49Entity *OgreBoundingBoxConverter::FindCorrespondingObject(const AxisAlignedBox &box) const
50{
51        list<SceneNode *> sceneNodeList;
52        AxisAlignedBox mybox = EnlargeBox(box);
53        //AxisAlignedBox dummy(Vector3(-50000, -50000, -50000), Vector3(50000, 50000, 50000));
54       
55        // get intersecting scene nodes
56        mSceneMgr->findNodesIn(mybox, sceneNodeList, NULL);
57       
58
59        list<SceneNode *>::const_iterator sit, sit_end = sceneNodeList.end();
60
61        // minimal overlap
62        float overlap = 0;//1e-6;
63
64        Entity *bestFittingObj = NULL;
65        float bestFit = overlap;
66
67        // perfect fit threshold
68        const float thresh = 1.0 - GtpVisibilityPreprocessor::Limits::Small;
69
70
71        // find the bbox which is closest to the current bbox
72        for (sit = sceneNodeList.begin(); sit != sceneNodeList.end(); ++ sit)
73        {
74                SceneNode *sn = *sit;
75                SceneNode::ObjectIterator oit = sn->getAttachedObjectIterator();
76
77        while (oit.hasMoreElements())
78                {
79                        MovableObject *mo = oit.getNext();
80
81                        // we are only interested in scene entities
82                        if (mo->getMovableType() != "Entity")
83                        {
84                                continue;
85                        }
86                         
87                        const AxisAlignedBox bbox = EnlargeBox(mo->getWorldBoundingBox());
88                                       
89
90                        // compute measure how much aabbs overlap
91                        overlap = RatioOfOverlap(OgreTypeConverter::ConvertFromOgre(mybox),
92                                                                         OgreTypeConverter::ConvertFromOgre(bbox));
93       
94                         if (overlap > bestFit)
95                         {
96                                 bestFit = overlap;
97                       
98                                 bestFittingObj = static_cast<Entity *>(mo);
99
100                                 // perfect fit => object found, early exit
101                                 if (overlap >= thresh)
102                                         return bestFittingObj;                         
103                         }
104                }
105        }
106
107        if (0)
108        {
109                std::stringstream d;
110                if (bestFittingObj)
111                        d << "best fit: " << bestFit;     
112                else
113                        d << "warning, objects do not fit\n" << box;
114       
115                Ogre::LogManager::getSingleton().logMessage(d.str());
116        }
117
118        return bestFittingObj;
119}
120
121}
Note: See TracBrowser for help on using the repository browser.