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

Revision 933, 3.4 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        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        Vector3 max = box.getMaximum();
43        Vector3 min = box.getMinimum();
44
45        return AxisAlignedBox(min - veps, max + veps);
46}
47//-----------------------------------------------------------------------
48Entity *OgreBoundingBoxConverter::FindCorrespondingObject(const AxisAlignedBox &box) const
49{
50        list<SceneNode *> sceneNodeList;
51        AxisAlignedBox mybox = EnlargeBox(box);
52        //AxisAlignedBox dummy(Vector3(-50000, -50000, -50000), Vector3(50000, 50000, 50000));
53       
54        //-- get intersecting scene nodes
55        mSceneMgr->findNodesIn(mybox, sceneNodeList, NULL);
56       
57
58        list<SceneNode *>::const_iterator sit, sit_end = sceneNodeList.end();
59
60        float overlap = 0;//GtpVisibilityPreprocessor::Limits::Small;
61
62        Entity *bestFittingObj = NULL;
63        float bestFit = overlap;
64
65        // perfect fit threshold
66        const float thresh = 1.0 - GtpVisibilityPreprocessor::Limits::Small;
67
68
69        // find the bbox which is closest to the current bbox
70        for (sit = sceneNodeList.begin(); sit != sceneNodeList.end(); ++ sit)
71        {
72                SceneNode *sn = *sit;
73                SceneNode::ObjectIterator oit = sn->getAttachedObjectIterator();
74
75        while (oit.hasMoreElements())
76                {
77                        MovableObject *mo = oit.getNext();
78
79                        // we are only interested in scene entities
80                        if (mo->getMovableType() != "Entity")
81                        {
82                                continue;
83                        }
84                         
85                        const AxisAlignedBox bbox = EnlargeBox(mo->getWorldBoundingBox());
86                                       
87
88                        // compute measure how much aabbs overlap
89                        overlap = RatioOfOverlap(OgreTypeConverter::ConvertFromOgre(mybox),
90                                                                         OgreTypeConverter::ConvertFromOgre(bbox));
91       
92                         if (overlap > bestFit)
93                         {
94                                 bestFit = overlap;
95                       
96                                 bestFittingObj = static_cast<Entity *>(mo);
97
98                                 // perfect fit => object found, eraly exit
99                                 if (overlap >= thresh)
100                                         return bestFittingObj;                         
101                         }
102                }
103        }
104
105        if (0)
106        {
107                std::stringstream d;
108                if (bestFittingObj)
109                        d << "best fit: " << bestFit;     
110                else
111                        d << "warning, objects do not fit\n" << box;
112       
113                Ogre::LogManager::getSingleton().logMessage(d.str());
114        }
115
116        return bestFittingObj;
117}
118
119}
Note: See TracBrowser for help on using the repository browser.