[933] | 1 | #include "OgreBoundingBoxConverter.h" |
---|
| 2 | #include "OgreTypeConverter.h" |
---|
| 3 | #include "OgreMeshInstance.h" |
---|
| 4 | #include "OgreOctreeSceneManager.h" |
---|
| 5 | #include <OgreLogManager.h> |
---|
| 6 | |
---|
| 7 | |
---|
| 8 | namespace Ogre |
---|
| 9 | {
|
---|
| 10 | //-------------------------------------------------------------------------
|
---|
| 11 | OgreBoundingBoxConverter::OgreBoundingBoxConverter(OctreeSceneManager *sm):
|
---|
| 12 | mSceneMgr(sm)
|
---|
| 13 | {
|
---|
| 14 | }
|
---|
| 15 | //-------------------------------------------------------------------------
|
---|
| 16 | bool OgreBoundingBoxConverter::IdentifyObjects(const GtpVisibilityPreprocessor::IndexedBoundingBoxContainer &iboxes,
|
---|
| 17 | GtpVisibilityPreprocessor::ObjectContainer &objects) const
|
---|
| 18 | {
|
---|
| 19 | GtpVisibilityPreprocessor::IndexedBoundingBoxContainer::
|
---|
| 20 | const_iterator iit, iit_end = iboxes.end();
|
---|
[944] | 21 |
|
---|
[933] | 22 | for (iit = iboxes.begin(); iit != iit_end; ++ iit)
|
---|
| 23 | {
|
---|
| 24 | const GtpVisibilityPreprocessor::AxisAlignedBox3 box = (*iit).second;
|
---|
| 25 | const AxisAlignedBox currentBox = OgreTypeConverter::ConvertToOgre(box);
|
---|
[944] | 26 |
|
---|
[933] | 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 | //-------------------------------------------------------------------------
|
---|
| 38 | inline static AxisAlignedBox EnlargeBox(const AxisAlignedBox &box)
|
---|
| 39 | {
|
---|
| 40 | const float eps = 1e-3f;
|
---|
| 41 | const Vector3 veps(eps, eps, eps);
|
---|
[1221] | 42 |
|
---|
[933] | 43 | Vector3 max = box.getMaximum();
|
---|
| 44 | Vector3 min = box.getMinimum();
|
---|
| 45 |
|
---|
| 46 | return AxisAlignedBox(min - veps, max + veps);
|
---|
| 47 | } |
---|
| 48 | //-----------------------------------------------------------------------
|
---|
| 49 | Entity *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));
|
---|
[944] | 54 |
|
---|
[1221] | 55 | // get intersecting scene nodes
|
---|
[933] | 56 | mSceneMgr->findNodesIn(mybox, sceneNodeList, NULL);
|
---|
| 57 |
|
---|
| 58 |
|
---|
| 59 | list<SceneNode *>::const_iterator sit, sit_end = sceneNodeList.end();
|
---|
| 60 |
|
---|
[938] | 61 | // minimal overlap
|
---|
| 62 | float overlap = 0;//1e-6;
|
---|
[933] | 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 |
|
---|
[1221] | 100 | // perfect fit => object found, early exit
|
---|
[933] | 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 | } |
---|