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

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