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 | |
---|
10 | namespace Ogre {
|
---|
11 |
|
---|
12 | class Entity; |
---|
13 | class OctreeSceneManager;
|
---|
14 | class KdTreeSceneManager;
|
---|
15 | class BvHierarchySceneManager;
|
---|
16 |
|
---|
17 |
|
---|
18 | /** Class which converts preprocessor types to OGRE types
|
---|
19 | */
|
---|
20 | template<typename T>
|
---|
21 | class __declspec(dllexport) PlatFormBoundingBoxConverter: public GtpVisibilityPreprocessor::BoundingBoxConverter
|
---|
22 | {
|
---|
23 | public:
|
---|
24 | PlatFormBoundingBoxConverter(T *sm);
|
---|
25 |
|
---|
26 | bool IdentifyObjects(const GtpVisibilityPreprocessor::IndexedBoundingBoxContainer &iboxes,
|
---|
27 | GtpVisibilityPreprocessor::ObjectContainer &objects) const;
|
---|
28 |
|
---|
29 |
|
---|
30 | protected:
|
---|
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 | //-------------------------------------------------------------------------
|
---|
50 | template<typename T>
|
---|
51 | PlatFormBoundingBoxConverter<T>::PlatFormBoundingBoxConverter(T *sm):
|
---|
52 | mSceneMgr(sm)
|
---|
53 | {
|
---|
54 | }
|
---|
55 | //-----------------------------------------------------------------------
|
---|
56 | template<typename T>
|
---|
57 | Entity *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 | //-------------------------------------------------------------------------
|
---|
125 | template<typename T>
|
---|
126 | bool 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 |
|
---|
153 | typedef PlatFormBoundingBoxConverter<OctreeSceneManager> OctreeBoundingBoxConverter;
|
---|
154 | typedef PlatFormBoundingBoxConverter<BvHierarchySceneManager> BvhBoundingBoxConverter;
|
---|
155 | typedef PlatFormBoundingBoxConverter<KdTreeSceneManager> KdTreeBoundingBoxConverter;
|
---|
156 |
|
---|
157 | } // namespace Ogre
|
---|
158 |
|
---|
159 | #endif // PlatFormBoundingBoxConverter
|
---|