[933] | 1 | #include "OgreBoundingBoxConverter.h" |
---|
| 2 | #include "OgreTypeConverter.h" |
---|
| 3 | #include "OgreMeshInstance.h" |
---|
| 4 | #include "OgreOctreeSceneManager.h" |
---|
[1296] | 5 | #include "OgreKdTreeSceneManager.h" |
---|
[933] | 6 | #include <OgreLogManager.h> |
---|
| 7 | |
---|
| 8 | |
---|
| 9 | namespace Ogre |
---|
| 10 | {
|
---|
| 11 | //-------------------------------------------------------------------------
|
---|
| 12 | OgreBoundingBoxConverter::OgreBoundingBoxConverter(OctreeSceneManager *sm):
|
---|
[1296] | 13 | mOctSceneMgr(sm), mKdSceneMgr(0)
|
---|
[933] | 14 | {
|
---|
| 15 | }
|
---|
| 16 | //-------------------------------------------------------------------------
|
---|
[1296] | 17 | OgreBoundingBoxConverter::OgreBoundingBoxConverter(KdTreeSceneManager *sm):
|
---|
| 18 | mOctSceneMgr(0), mKdSceneMgr(sm)
|
---|
| 19 | {
|
---|
| 20 | }
|
---|
| 21 | //-------------------------------------------------------------------------
|
---|
[933] | 22 | bool OgreBoundingBoxConverter::IdentifyObjects(const GtpVisibilityPreprocessor::IndexedBoundingBoxContainer &iboxes,
|
---|
| 23 | GtpVisibilityPreprocessor::ObjectContainer &objects) const
|
---|
| 24 | {
|
---|
| 25 | GtpVisibilityPreprocessor::IndexedBoundingBoxContainer::
|
---|
| 26 | const_iterator iit, iit_end = iboxes.end();
|
---|
[944] | 27 |
|
---|
[933] | 28 | for (iit = iboxes.begin(); iit != iit_end; ++ iit)
|
---|
| 29 | {
|
---|
| 30 | const GtpVisibilityPreprocessor::AxisAlignedBox3 box = (*iit).second;
|
---|
| 31 | const AxisAlignedBox currentBox = OgreTypeConverter::ConvertToOgre(box);
|
---|
[944] | 32 |
|
---|
[933] | 33 | Entity *ent = FindCorrespondingObject(currentBox);
|
---|
| 34 |
|
---|
| 35 | // create new mesh instance
|
---|
| 36 | OgreMeshInstance *omi = new OgreMeshInstance(ent);
|
---|
| 37 | omi->SetId((*iit).first);
|
---|
| 38 | objects.push_back(omi);
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | return true;
|
---|
| 42 | } |
---|
| 43 | //-------------------------------------------------------------------------
|
---|
| 44 | inline static AxisAlignedBox EnlargeBox(const AxisAlignedBox &box)
|
---|
| 45 | {
|
---|
| 46 | const float eps = 1e-3f;
|
---|
| 47 | const Vector3 veps(eps, eps, eps);
|
---|
[1221] | 48 |
|
---|
[933] | 49 | Vector3 max = box.getMaximum();
|
---|
| 50 | Vector3 min = box.getMinimum();
|
---|
| 51 |
|
---|
| 52 | return AxisAlignedBox(min - veps, max + veps);
|
---|
| 53 | } |
---|
| 54 | //-----------------------------------------------------------------------
|
---|
| 55 | Entity *OgreBoundingBoxConverter::FindCorrespondingObject(const AxisAlignedBox &box) const
|
---|
| 56 | {
|
---|
| 57 | list<SceneNode *> sceneNodeList;
|
---|
| 58 | AxisAlignedBox mybox = EnlargeBox(box);
|
---|
| 59 | //AxisAlignedBox dummy(Vector3(-50000, -50000, -50000), Vector3(50000, 50000, 50000));
|
---|
[944] | 60 |
|
---|
[1221] | 61 | // get intersecting scene nodes
|
---|
[1296] | 62 | if (mOctSceneMgr)
|
---|
| 63 | mOctSceneMgr->findNodesIn(mybox, sceneNodeList, NULL);
|
---|
| 64 | else if (mKdSceneMgr)
|
---|
| 65 | mKdSceneMgr->findNodesIn(mybox, sceneNodeList, NULL);
|
---|
| 66 | else
|
---|
| 67 | OGRE_EXCEPT(Exception::ERR_INTERNAL_ERROR, "Bounding Box Converter cannot "
|
---|
| 68 | "find appropriate scene manager.", "OgreBoundingBoxConverter::FindCorrespondingObject");
|
---|
[933] | 69 |
|
---|
| 70 |
|
---|
| 71 | list<SceneNode *>::const_iterator sit, sit_end = sceneNodeList.end();
|
---|
| 72 |
|
---|
[938] | 73 | // minimal overlap
|
---|
| 74 | float overlap = 0;//1e-6;
|
---|
[933] | 75 |
|
---|
| 76 | Entity *bestFittingObj = NULL;
|
---|
| 77 | float bestFit = overlap;
|
---|
| 78 |
|
---|
| 79 | // perfect fit threshold
|
---|
| 80 | const float thresh = 1.0 - GtpVisibilityPreprocessor::Limits::Small;
|
---|
| 81 |
|
---|
| 82 |
|
---|
| 83 | // find the bbox which is closest to the current bbox
|
---|
| 84 | for (sit = sceneNodeList.begin(); sit != sceneNodeList.end(); ++ sit)
|
---|
| 85 | {
|
---|
| 86 | SceneNode *sn = *sit;
|
---|
| 87 | SceneNode::ObjectIterator oit = sn->getAttachedObjectIterator();
|
---|
| 88 |
|
---|
| 89 | while (oit.hasMoreElements())
|
---|
| 90 | {
|
---|
| 91 | MovableObject *mo = oit.getNext();
|
---|
| 92 |
|
---|
| 93 | // we are only interested in scene entities
|
---|
| 94 | if (mo->getMovableType() != "Entity")
|
---|
| 95 | {
|
---|
| 96 | continue;
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | const AxisAlignedBox bbox = EnlargeBox(mo->getWorldBoundingBox());
|
---|
| 100 |
|
---|
| 101 |
|
---|
| 102 | // compute measure how much aabbs overlap
|
---|
| 103 | overlap = RatioOfOverlap(OgreTypeConverter::ConvertFromOgre(mybox),
|
---|
| 104 | OgreTypeConverter::ConvertFromOgre(bbox));
|
---|
| 105 |
|
---|
| 106 | if (overlap > bestFit)
|
---|
| 107 | {
|
---|
| 108 | bestFit = overlap;
|
---|
| 109 |
|
---|
| 110 | bestFittingObj = static_cast<Entity *>(mo);
|
---|
| 111 |
|
---|
[1221] | 112 | // perfect fit => object found, early exit
|
---|
[933] | 113 | if (overlap >= thresh)
|
---|
| 114 | return bestFittingObj;
|
---|
| 115 | }
|
---|
| 116 | }
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | if (0)
|
---|
| 120 | {
|
---|
| 121 | std::stringstream d;
|
---|
| 122 | if (bestFittingObj)
|
---|
| 123 | d << "best fit: " << bestFit;
|
---|
| 124 | else
|
---|
| 125 | d << "warning, objects do not fit\n" << box;
|
---|
| 126 |
|
---|
| 127 | Ogre::LogManager::getSingleton().logMessage(d.str());
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | return bestFittingObj;
|
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | } |
---|