[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 |
|
---|
| 22 |
|
---|
[933] | 23 | for (iit = iboxes.begin(); iit != iit_end; ++ iit)
|
---|
| 24 | {
|
---|
| 25 | const GtpVisibilityPreprocessor::AxisAlignedBox3 box = (*iit).second;
|
---|
| 26 | const AxisAlignedBox currentBox = OgreTypeConverter::ConvertToOgre(box);
|
---|
[944] | 27 |
|
---|
| 28 |
|
---|
[933] | 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 | //-------------------------------------------------------------------------
|
---|
| 40 | inline 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 | //-----------------------------------------------------------------------
|
---|
| 50 | Entity *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));
|
---|
[944] | 55 |
|
---|
[933] | 56 | //-- get intersecting scene nodes
|
---|
| 57 | mSceneMgr->findNodesIn(mybox, sceneNodeList, NULL);
|
---|
| 58 |
|
---|
| 59 |
|
---|
| 60 | list<SceneNode *>::const_iterator sit, sit_end = sceneNodeList.end();
|
---|
| 61 |
|
---|
[938] | 62 | // minimal overlap
|
---|
| 63 | float overlap = 0;//1e-6;
|
---|
[933] | 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 | } |
---|