]> git.mxchange.org Git - simgear.git/commitdiff
Remove the StaticLeaf visitor slot.
authorfrohlich <frohlich>
Mon, 2 Mar 2009 18:02:50 +0000 (18:02 +0000)
committerTim Moore <timoore@redhat.com>
Thu, 5 Mar 2009 09:32:06 +0000 (10:32 +0100)
Add a nearest point visitor.

Modified Files:
BVHBoundingBoxVisitor.hxx BVHDebugCollectVisitor.hxx
BVHLineSegmentVisitor.cxx BVHLineSegmentVisitor.hxx
BVHStaticGeometryBuilder.hxx BVHStaticLeaf.cxx
BVHStaticLeaf.hxx BVHSubTreeCollector.cxx
BVHSubTreeCollector.hxx BVHVisitor.hxx Makefile.am bvhtest.cxx
Added Files:
BVHNearestPointVisitor.hxx

12 files changed:
simgear/scene/bvh/BVHBoundingBoxVisitor.hxx
simgear/scene/bvh/BVHDebugCollectVisitor.hxx
simgear/scene/bvh/BVHLineSegmentVisitor.cxx
simgear/scene/bvh/BVHLineSegmentVisitor.hxx
simgear/scene/bvh/BVHNearestPointVisitor.hxx [new file with mode: 0644]
simgear/scene/bvh/BVHStaticLeaf.cxx
simgear/scene/bvh/BVHStaticLeaf.hxx
simgear/scene/bvh/BVHSubTreeCollector.cxx
simgear/scene/bvh/BVHSubTreeCollector.hxx
simgear/scene/bvh/BVHVisitor.hxx
simgear/scene/bvh/Makefile.am
simgear/scene/bvh/bvhtest.cxx

index 8fc9db9ffc95f8d2a1aec94c2bfb613f56d641a7..d37779c367b519fde3374bccd3f7ef056354b853 100644 (file)
@@ -30,7 +30,6 @@
 #include "BVHStaticData.hxx"
 
 #include "BVHStaticNode.hxx"
-#include "BVHStaticLeaf.hxx"
 #include "BVHStaticTriangle.hxx"
 #include "BVHStaticBinary.hxx"
 #include "BVHStaticGeometry.hxx"
@@ -57,8 +56,6 @@ public:
     
     virtual void apply(const BVHStaticBinary& node, const BVHStaticData& data)
     { expandBy(node.getBoundingBox()); }
-    virtual void apply(const BVHStaticLeaf& node, const BVHStaticData& data)
-    { expandBy(node.computeBoundingBox(data)); }
     virtual void apply(const BVHStaticTriangle& node, const BVHStaticData& data)
     { expandBy(node.computeBoundingBox(data)); }
     
index 540ee45f12b4cc28e139ebe5f800787d84402e7e..11bbd9f18d6b94c9ceb5abcfabf80c97b3b15d7e 100644 (file)
@@ -25,7 +25,6 @@
 #include <osg/PolygonOffset>
 #include <osg/PrimitiveSet>
 #include <osg/MatrixTransform>
-#include <osg/PositionAttitudeTransform>
 #include <osg/ShapeDrawable>
 #include <osg/Shape>
 #include <osg/Depth>
@@ -44,7 +43,6 @@
 #include "BVHStaticData.hxx"
 
 #include "BVHStaticNode.hxx"
-#include "BVHStaticLeaf.hxx"
 #include "BVHStaticTriangle.hxx"
 #include "BVHStaticBinary.hxx"
 
@@ -101,16 +99,9 @@ public:
     {
         addNodeSphere(node);
         osg::ref_ptr<osg::Group> oldGroup = _group;
-        osg::ref_ptr<osg::PositionAttitudeTransform> transform;
-        transform = new osg::PositionAttitudeTransform;
+        osg::ref_ptr<osg::MatrixTransform> transform = new osg::MatrixTransform;
+        transform->setMatrix(osg::Matrix(node.getToWorldReferenceTransform().data()));
         double tt = node.getReferenceTime() - node.getEndTime();
-        tt = 100*tt;
-        tt += node.getReferenceTime();
-//     transform->setPosition(node.getPosition(node.getEndTime()).osg());
-        transform->setPosition(node.getPosition().osg());
-        transform->setAttitude(inverse(node.getOrientation(tt)).osg());
-//     transform->setPosition(node.getPosition().osg());
-//     transform->setAttitude(inverse(node.getOrientation()).osg());
         _group = transform;
         ++_currentLevel;
         node.traverse(*this);
@@ -137,10 +128,6 @@ public:
         node.traverse(*this, data);
         --_currentLevel;
     }
-    virtual void apply(const BVHStaticLeaf& node, const BVHStaticData& data)
-    {
-        addNodeBox(node, data);
-    }
     virtual void apply(const BVHStaticTriangle& node, const BVHStaticData& data)
     {
         addNodeBox(node, data);
index d4dbd194f6b5652223cca01496b8bb409451574e..6475ce2b1472743926f7d804a8b79251ee5be374 100644 (file)
@@ -31,7 +31,6 @@
 #include "BVHStaticData.hxx"
 
 #include "BVHStaticNode.hxx"
-#include "BVHStaticLeaf.hxx"
 #include "BVHStaticTriangle.hxx"
 #include "BVHStaticBinary.hxx"
 
@@ -131,12 +130,6 @@ BVHLineSegmentVisitor::apply(const BVHStaticBinary& node,
     node.traverse(*this, data, _lineSegment.getStart());
 }
 
-void
-BVHLineSegmentVisitor::apply(const BVHStaticLeaf& node,
-                             const BVHStaticData& data)
-{
-}
-
 void
 BVHLineSegmentVisitor::apply(const BVHStaticTriangle& triangle,
                              const BVHStaticData& data)
index db3508bc67a89e83d958241d78a4e57c61d1e066..5801e5d37c5fafeb8da06fc0a0a93c6557036567 100644 (file)
@@ -62,7 +62,6 @@ public:
     virtual void apply(BVHStaticGeometry& node);
     
     virtual void apply(const BVHStaticBinary&, const BVHStaticData&);
-    virtual void apply(const BVHStaticLeaf&, const BVHStaticData&);
     virtual void apply(const BVHStaticTriangle&, const BVHStaticData&);
     
 protected:
diff --git a/simgear/scene/bvh/BVHNearestPointVisitor.hxx b/simgear/scene/bvh/BVHNearestPointVisitor.hxx
new file mode 100644 (file)
index 0000000..c5f1add
--- /dev/null
@@ -0,0 +1,161 @@
+// Copyright (C) 2008 - 2009  Mathias Froehlich - Mathias.Froehlich@web.de
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Library General Public
+// License as published by the Free Software Foundation; either
+// version 2 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Library General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+//
+
+#ifndef BVHNearestPointVisitor_hxx
+#define BVHNearestPointVisitor_hxx
+
+#include <simgear/math/SGGeometry.hxx>
+
+#include "BVHVisitor.hxx"
+
+#include "BVHNode.hxx"
+#include "BVHGroup.hxx"
+#include "BVHTransform.hxx"
+#include "BVHLineGeometry.hxx"
+#include "BVHStaticGeometry.hxx"
+
+#include "BVHStaticData.hxx"
+
+#include "BVHStaticNode.hxx"
+#include "BVHStaticTriangle.hxx"
+#include "BVHStaticBinary.hxx"
+
+namespace simgear {
+
+class BVHNearestPointVisitor : public BVHVisitor {
+public:
+    BVHNearestPointVisitor(const SGSphered& sphere, const double& t) :
+        _sphere(sphere),
+        _time(t),
+        _material(0),
+        _havePoint(false)
+    { }
+    
+    virtual void apply(BVHGroup& leaf)
+    {
+        if (!intersects(_sphere, leaf.getBoundingSphere()))
+            return;
+        leaf.traverse(*this);
+    }
+    virtual void apply(BVHTransform& transform)
+    {
+        if (!intersects(_sphere, transform.getBoundingSphere()))
+            return;
+        
+        SGSphered sphere = _sphere;
+        _sphere = transform.sphereToLocal(sphere);
+        bool havePoint = _havePoint;
+        _havePoint = false;
+        
+        transform.traverse(*this);
+        
+        if (_havePoint) {
+            _point = transform.ptToWorld(_point);
+            _linearVelocity = transform.vecToWorld(_linearVelocity);
+            _angularVelocity = transform.vecToWorld(_angularVelocity);
+        }
+        _havePoint |= havePoint;
+        _sphere.setCenter(sphere.getCenter());
+    }
+    virtual void apply(BVHMotionTransform& transform)
+    {
+        if (!intersects(_sphere, transform.getBoundingSphere()))
+            return;
+        
+        SGSphered sphere = _sphere;
+        _sphere = transform.sphereToLocal(sphere, _time);
+        bool havePoint = _havePoint;
+        _havePoint = false;
+        
+        transform.traverse(*this);
+        
+        if (_havePoint) {
+            SGMatrixd toWorld = transform.getToWorldTransform(_time);
+            SGVec3d localCenter = _sphere.getCenter();
+            _linearVelocity += transform.getLinearVelocityAt(localCenter);
+            _angularVelocity += transform.getAngularVelocity();
+            _linearVelocity = toWorld.xformVec(_linearVelocity);
+            _angularVelocity = toWorld.xformVec(_angularVelocity);
+            _point = toWorld.xformPt(_point);
+        }
+        _havePoint |= havePoint;
+        _sphere.setCenter(sphere.getCenter());
+    }
+    virtual void apply(BVHLineGeometry& node)
+    { }
+    virtual void apply(BVHStaticGeometry& node)
+    {
+        if (!intersects(_sphere, node.getBoundingSphere()))
+            return;
+        node.traverse(*this);
+    }
+    
+    virtual void apply(const BVHStaticBinary& node, const BVHStaticData& data)
+    {
+        if (!intersects(_sphere, node.getBoundingBox()))
+            return;
+        node.traverse(*this, data, _sphere.getCenter());
+    }
+    virtual void apply(const BVHStaticTriangle& node, const BVHStaticData& data)
+    {
+        SGVec3f center(_sphere.getCenter());
+        SGVec3d closest(closestPoint(node.getTriangle(data), center));
+        if (!intersects(_sphere, closest))
+            return;
+        _point = closest;
+        _linearVelocity = SGVec3d::zeros();
+        _angularVelocity = SGVec3d::zeros();
+        _material = data.getMaterial(node.getMaterialIndex());
+        // The trick is to decrease the radius of the search sphere.
+        _sphere.setRadius(length(closest - _sphere.getCenter()));
+        _havePoint = true;
+    }
+    
+    void setSphere(const SGSphered& sphere)
+    { _sphere = sphere; }
+    const SGSphered& getSphere() const
+    { return _sphere; }
+    
+    const SGVec3d& getPoint() const
+    { return _point; }
+    const SGVec3d& getLinearVelocity() const
+    { return _linearVelocity; }
+    const SGVec3d& getAngularVelocity() const
+    { return _angularVelocity; }
+    const SGMaterial* getMaterial() const
+    { return _material; }
+    
+    bool getHavePoint() const
+    { return _havePoint; }
+    bool empty() const
+    { return !_havePoint; }
+    
+private:
+    SGSphered _sphere;
+    double _time;
+
+    SGVec3d _point;
+    SGVec3d _linearVelocity;
+    SGVec3d _angularVelocity;
+    const SGMaterial* _material;
+    
+    bool _havePoint;
+};
+
+}
+
+#endif
index 6713753073633b84bc401bd85aedb6dd69a046a9..29c2e04ffafe38d4489df2edcc0ba4fd1922ff04 100644 (file)
@@ -24,10 +24,4 @@ BVHStaticLeaf::~BVHStaticLeaf()
 {
 }
 
-void
-BVHStaticLeaf::accept(BVHVisitor& visitor, const BVHStaticData& data) const
-{
-  visitor.apply(*this, data);
-}
-
 }
index 3a7682f7449a8848bdb348ffd2a3d5399f717bd9..99d2959d913f0ead6ddb24856cdf4e3774238c8c 100644 (file)
@@ -27,7 +27,7 @@ class BVHStaticLeaf : public BVHStaticNode {
 public:
   virtual ~BVHStaticLeaf();
 
-  virtual void accept(BVHVisitor& visitor, const BVHStaticData& data) const;
+  virtual void accept(BVHVisitor& visitor, const BVHStaticData& data) const = 0;
 
   virtual SGBoxf computeBoundingBox(const BVHStaticData&) const = 0;
   virtual SGVec3f computeCenter(const BVHStaticData&) const = 0;
index cc7596be384bf0e08b6fb97ef273282eaf80c9e7..d8c6017f072dba6ae2e792594142ec493a229285 100644 (file)
@@ -26,7 +26,6 @@
 #include "BVHStaticData.hxx"
 
 #include "BVHStaticNode.hxx"
-#include "BVHStaticLeaf.hxx"
 #include "BVHStaticTriangle.hxx"
 #include "BVHStaticBinary.hxx"
 #include "BVHStaticGeometry.hxx"
@@ -180,15 +179,6 @@ BVHSubTreeCollector::apply(const BVHStaticBinary& node,
     }
 }
 
-void
-BVHSubTreeCollector::apply(const BVHStaticLeaf& node,
-                           const BVHStaticData& data)
-{
-    if (!intersects(_sphere, node.computeBoundingBox(data)))
-        return;
-    _staticNode = &node;
-}
-
 void
 BVHSubTreeCollector::apply(const BVHStaticTriangle& node,
                            const BVHStaticData& data)
index b29d62740fed73ba928dcf9326146609cadd076b..aa3e1124ecb43f82d4e4212fdb52240bea2eb4eb 100644 (file)
@@ -49,7 +49,6 @@ public:
     virtual void apply(BVHStaticGeometry&);
     
     virtual void apply(const BVHStaticBinary&, const BVHStaticData&);
-    virtual void apply(const BVHStaticLeaf&, const BVHStaticData&);
     virtual void apply(const BVHStaticTriangle&, const BVHStaticData&);
     
     void setSphere(const SGSphered& sphere)
index 058cf27c4487318a3d69d6e4d65c8732e8486c02..f74f445691bd508052759ea905af3fe2a3bf0a3d 100644 (file)
@@ -29,7 +29,6 @@ class BVHStaticGeometry;
 class BVHLineGeometry;
 
 class BVHStaticBinary;
-class BVHStaticLeaf;
 class BVHStaticTriangle;
 
 class BVHVisitor {
@@ -49,7 +48,6 @@ public:
     
     // Static tree nodes to handle
     virtual void apply(const BVHStaticBinary&, const BVHStaticData&) = 0;
-    virtual void apply(const BVHStaticLeaf&, const BVHStaticData&) = 0;
     virtual void apply(const BVHStaticTriangle&, const BVHStaticData&) = 0;
 };
 
index 0b1be9854f5b4316ba3229ed2b70d6e018b671a7..c880a313ba87b42683891d2c3ab7184168ced22e 100644 (file)
@@ -17,6 +17,7 @@ include_HEADERS = \
        BVHLineSegmentVisitor.hxx \
        BVHLineGeometry.hxx \
        BVHMotionTransform.hxx \
+       BVHNearestPointVisitor.hxx \
        BVHNode.hxx \
        BVHStaticBinary.hxx \
        BVHStaticData.hxx \
index 797299cb7f05a1ca2b3f47f32045c7bf38d847f6..0f01e7f002888eb274a477bf5afec73ee3966203 100644 (file)
@@ -33,6 +33,7 @@
 #include "BVHBoundingBoxVisitor.hxx"
 #include "BVHSubTreeCollector.hxx"
 #include "BVHLineSegmentVisitor.hxx"
+#include "BVHNearestPointVisitor.hxx"
 
 using namespace simgear;
 
@@ -109,10 +110,33 @@ testLineIntersections()
     return true;
 }
 
+bool
+testNearestPoint()
+{
+    SGVec3f v1(-1, -1, 0);
+    SGVec3f v2(1, -1, 0);
+    SGVec3f v3(-1, 1, 0);
+    SGSharedPtr<BVHNode> node = buildSingleTriangle(v1, v2, v3);
+
+    SGSphered sphere(SGVec3d(0, 0, -1), 2);
+    {
+      BVHNearestPointVisitor nearestPointVisitor(sphere, 0);
+        node->accept(nearestPointVisitor);
+        if (nearestPointVisitor.empty())
+            return false;
+        if (!equivalent(nearestPointVisitor.getPoint(), SGVec3d(0, 0, 0)))
+            return false;
+    }
+
+    return true;
+}
+
 int
 main(int argc, char** argv)
 {
     if (!testLineIntersections())
         return EXIT_FAILURE;
+    if (!testNearestPoint())
+        return EXIT_FAILURE;
     return EXIT_SUCCESS;
 }