source: GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/include/OgreBoundingBoxConverter.h @ 1595

Revision 1595, 4.4 KB checked in by mattausch, 18 years ago (diff)
Line 
1#ifndef _PlatFormBoundingBoxConverter_H__
2#define _PlatFormBoundingBoxConverter_H__
3 
4#include "OgreAxisAlignedBox.h"
5#include "AxisAlignedBox3.h"
6#include "Vector3.h"
7#include "BoundingBoxConverter.h"
8#include "Containers.h"
9
10namespace Ogre {
11
12class Entity;
13class OctreeSceneManager;
14class KdTreeSceneManager;
15class BvHierarchySceneManager;
16
17
18/**     Class which converts preprocessor types to OGRE types
19*/
20template<typename T>
21class __declspec(dllexport) PlatFormBoundingBoxConverter: public GtpVisibilityPreprocessor::BoundingBoxConverter
22{
23public:
24        PlatFormBoundingBoxConverter(T *sm);
25       
26        bool IdentifyObjects(const GtpVisibilityPreprocessor::IndexedBoundingBoxContainer &iboxes,
27                                                 GtpVisibilityPreprocessor::ObjectContainer &objects) const;
28
29
30protected:
31
32        //-------------------------------------------------------------------------
33        inline static AxisAlignedBox EnlargeBox(const AxisAlignedBox &box)
34        {
35                const float eps = 1e-3f;
36                const Vector3 veps(eps, eps, eps);
37               
38                Vector3 max = box.getMaximum();
39        Vector3 min = box.getMinimum();
40
41                return AxisAlignedBox(min - veps, max + veps);
42        }
43       
44        Entity *FindBestFittingObject(const AxisAlignedBox &box) const;
45
46        T *mSceneMgr;
47};
48
49//-------------------------------------------------------------------------
50template<typename T>
51PlatFormBoundingBoxConverter<T>::PlatFormBoundingBoxConverter(T *sm):
52mSceneMgr(sm)
53{
54}
55//-----------------------------------------------------------------------
56template<typename T>
57Entity *PlatFormBoundingBoxConverter<T>::FindBestFittingObject(const AxisAlignedBox &box) const
58{
59        list<SceneNode *> sceneNodeList;
60        AxisAlignedBox mybox = EnlargeBox(box);
61               
62        // get intersecting scene nodes
63        mSceneMgr->findNodesIn(mybox, sceneNodeList, NULL);
64               
65        // minimal overlap
66        float overlap = 0;//1e-6;
67
68        Entity *bestFittingObj = NULL;
69        float bestFit = overlap;
70
71        // perfect fit threshold
72        const float thresh = 1.0 - GtpVisibilityPreprocessor::Limits::Small;
73
74        list<SceneNode *>::const_iterator sit, sit_end = sceneNodeList.end();
75        // find the bbox which is closest to the current bbox
76        for (sit = sceneNodeList.begin(); sit != sceneNodeList.end(); ++ sit)
77        {
78                SceneNode *sn = *sit;
79                SceneNode::ObjectIterator oit = sn->getAttachedObjectIterator();
80
81        while (oit.hasMoreElements())
82                {
83                        MovableObject *mo = oit.getNext();
84
85                        // we are only interested in scene entities
86                        if (mo->getMovableType() != "Entity")
87                        {
88                                continue;
89                        }
90                         
91                        const AxisAlignedBox bbox = EnlargeBox(mo->getWorldBoundingBox());
92                                       
93                        // compute measure how much aabbs overlap
94                        overlap = RatioOfOverlap(OgreTypeConverter::ConvertFromOgre(mybox),
95                                                                         OgreTypeConverter::ConvertFromOgre(bbox));
96       
97                         if (overlap > bestFit)
98                         {
99                                 bestFit = overlap;
100                                 bestFittingObj = static_cast<Entity *>(mo);
101
102                                 // perfect fit => object found, early exit
103                                 if (overlap >= thresh)
104                                 {
105                                         return bestFittingObj;
106                                 }
107                         }
108                }
109        }
110
111        if (0)
112        {
113                std::stringstream d;
114                if (bestFittingObj)
115                        d << "best fit: " << bestFit;     
116                else
117                        d << "warning, no best fitting object\n" << box;
118       
119                Ogre::LogManager::getSingleton().logMessage(d.str());
120        }
121
122        return bestFittingObj;
123}
124//-------------------------------------------------------------------------
125template<typename T>
126bool PlatFormBoundingBoxConverter<T>::IdentifyObjects(
127        const GtpVisibilityPreprocessor::IndexedBoundingBoxContainer &iboxes,
128        GtpVisibilityPreprocessor::ObjectContainer &objects) const
129{
130        GtpVisibilityPreprocessor::IndexedBoundingBoxContainer::
131                const_iterator iit, iit_end = iboxes.end();
132 
133        for (iit = iboxes.begin(); iit != iit_end; ++ iit)
134        {
135                const GtpVisibilityPreprocessor::AxisAlignedBox3 box = (*iit).second;
136                const AxisAlignedBox currentBox = OgreTypeConverter::ConvertToOgre(box);
137   
138                Entity *ent = FindBestFittingObject(currentBox);
139
140                // create new mesh instance
141                if (ent)
142                {
143                        OgreMeshInstance *omi = new OgreMeshInstance(ent);
144                        omi->SetId((*iit).first);
145                        objects.push_back(omi);
146                }
147        }
148
149        return true;
150}
151
152
153typedef PlatFormBoundingBoxConverter<OctreeSceneManager> OctreeBoundingBoxConverter;
154typedef PlatFormBoundingBoxConverter<BvHierarchySceneManager> BvhBoundingBoxConverter;
155typedef PlatFormBoundingBoxConverter<KdTreeSceneManager> KdTreeBoundingBoxConverter;
156
157} // namespace Ogre
158
159#endif // PlatFormBoundingBoxConverter
Note: See TracBrowser for help on using the repository browser.