]> git.mxchange.org Git - flightgear.git/blobdiff - src/FDM/groundcache.hxx
FGPUIDialog: fix reading from already free'd memory.
[flightgear.git] / src / FDM / groundcache.hxx
index 9568129b4c6afd19c544f79f761baeb874272bf0..9319666a6ba83508f9f1c364ab8d9e624ba740ed 100644 (file)
 //
 // 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., 675 Mass Ave, Cambridge, MA 02139, USA.
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 //
 // $Id$
 
 #ifndef _GROUNDCACHE_HXX
 #define _GROUNDCACHE_HXX
 
-#include <plib/sg.h>
-#include <plib/ssg.h>
 #include <simgear/compiler.h>
 #include <simgear/constants.h>
+#include <simgear/math/SGMath.hxx>
+#include <simgear/math/SGGeometry.hxx>
+#include <simgear/bvh/BVHNode.hxx>
+#include <simgear/structure/SGSharedPtr.hxx>
+
+// #define GROUNDCACHE_DEBUG
+#ifdef GROUNDCACHE_DEBUG
+#include <osg/Group>
+#include <osg/ref_ptr>
+#include <simgear/timing/timestamp.hxx>
+#endif
+
+namespace simgear {
+class BVHLineGeometry;
+class BVHMaterial;
+}
 
 class FGGroundCache {
 public:
@@ -40,108 +54,100 @@ public:
     // Prepare the ground cache for the wgs84 position pt_*.
     // That is take all vertices in the ball with radius rad around the
     // position given by the pt_* and store them in a local scene graph.
-    bool prepare_ground_cache(double ref_time, const double pt[3],
-                              double rad);
+    bool prepare_ground_cache(double startSimTime, double endSimTime,
+                              const SGVec3d& pt, double rad);
 
     // Returns true if the cache is valid.
     // Also the reference time, point and radius values where the cache
     // is valid for are returned.
-    bool is_valid(double *ref_time, double pt[3], double *rad);
-      
+    bool is_valid(double& ref_time, SGVec3d& pt, double& rad);
+
+    // Returns the unit down vector at the ground cache
+    const SGVec3d& get_down() const
+    { return down; }
+
+    // The time offset that originates from a simtime different than zero
+    // at initialization time of the fdm.
+    double get_cache_time_offset() const
+    { return cache_time_offset; }
+    void set_cache_time_offset(double time_offset)
+    { cache_time_offset = time_offset; }
+
+    bool get_body(double t, SGMatrixd& bodyToWorld, SGVec3d& linearVel,
+                  SGVec3d& angularVel, simgear::BVHNode::Id id);
 
     // Return the nearest catapult to the given point
     // pt in wgs84 coordinates.
-    double get_cat(double t, const double pt[3],
-                   double end[2][3], double vel[2][3]);
+    double get_cat(double t, const SGVec3d& pt,
+                   SGVec3d end[2], SGVec3d vel[2]);
   
 
     // Return the altitude above ground below the wgs84 point pt
-    // Search for the nearest triangle to pt.
-    // Return ground properties like the ground type, the maximum load
+    // Search for highest triangle not higher than pt + max_altoff.
+    // Return ground properties like the maximum load
     // this kind kind of ground can carry, the friction factor between
-    // 0 and 1 which can be used to model lower friction with wet runways
-    // and finally the altitude above ground.
-    bool get_agl(double t, const double pt[3],
-                 double contact[3], double normal[3], double vel[3],
-                 int *type, double *loadCapacity,
-                 double *frictionFactor, double *agl);
+    // 0 and 1 which can be used to model lower friction with wet runways.
+    bool get_agl(double t, const SGVec3d& pt, SGVec3d& contact,
+                 SGVec3d& normal, SGVec3d& linearVel, SGVec3d& angularVel,
+                 simgear::BVHNode::Id& id,
+                 const simgear::BVHMaterial*& material);
 
+    bool get_nearest(double t, const SGVec3d& pt, double maxDist,
+                     SGVec3d& contact, SGVec3d& linearVel, SGVec3d& angularVel,
+                     simgear::BVHNode::Id& id,
+                     const simgear::BVHMaterial*& material);
 
     // Return 1 if the hook intersects with a wire.
     // That test is done by checking if the quad spanned by the points pt*
     // intersects with the line representing the wire.
     // If the wire is caught, the cache will trace this wires endpoints until
     // the FDM calls release_wire().
-    bool caught_wire(double t, const double pt[4][3]);
+    bool caught_wire(double t, const SGVec3d pt[4]);
   
     // Return the location and speed of the wire endpoints.
-    bool get_wire_ends(double t, double end[2][3], double vel[2][3]);
+    bool get_wire_ends(double t, SGVec3d end[2], SGVec3d vel[2]);
 
     // Tell the cache code that it does no longer need to care for
     // the wire end position.
     void release_wire(void);
 
 private:
-    // Holds the private ground triangle cache ...
-    // The surface cache itself.
-    ssgRoot cache_root;
-    // The center of the cache.
-    sgdVec3 cache_center;
+    class CacheFill;
+    class BodyFinder;
+    class CatapultFinder;
+    class WireIntersector;
+    class WireFinder;
+
     // Approximate ground radius.
     // In case the aircraft is too high above ground.
-    double ground_radius;
+    double _altitude;
+    // the simgear material reference, contains friction coeficients ...
+    const simgear::BVHMaterial* _material;
     // The time reference for later call to intersection test routines.
     // Is required since we will have moving triangles in carriers.
     double cache_ref_time;
-    // The wire identifier to track.
-    int wire_id;
+    // The time the cache was initialized.
+    double cache_time_offset;
+    // The wire to track.
+    const simgear::BVHLineGeometry* _wire;
 
     // The point and radius where the cache is built around.
     // That are the arguments that were given to prepare_ground_cache.
-    sgdVec3 reference_wgs84_point;
+    SGVec3d reference_wgs84_point;
     double reference_vehicle_radius;
+    SGVec3d down;
     bool found_ground;
 
+    SGSharedPtr<simgear::BVHNode> _localBvhTree;
 
-    // Fills the environment cache with everything inside the sphere sp.
-    void cache_fill(ssgBranch *branch, sgMat4 xform,
-                    sgSphere* sp, sgVec3 down, sgSphere* wsp);
-
-    // Helper class to hold some properties of the ground triangle.
-    class GroundProperty
-      : public ssgBase {
-    public:
-      GroundProperty() : type(0) {}
-      int type;
-      int wire_id;
-      sgVec3 vel;
-      // not yet implemented ...
-//       double loadCapacity;
-    };
-
-    // compute the ground property of this leaf.
-    static GroundProperty *extractGroundProperty( ssgLeaf* leaf );
+#ifdef GROUNDCACHE_DEBUG
+    SGTimeStamp _lookupTime;
+    unsigned _lookupCount;
+    SGTimeStamp _buildTime;
+    unsigned _buildCount;
 
-
-    // compute the ground property of this leaf.
-    void putSurfaceLeafIntoCache(const sgSphere *sp, const sgMat4 xform,
-                                 bool sphIsec, sgVec3 down, ssgLeaf *l);
-
-    void putLineLeafIntoCache(const sgSphere *wsp, const sgMat4 xform,
-                              ssgLeaf *l);
-
-    void addAndFlattenLeaf(GLenum ty, ssgLeaf *l, ssgIndexArray *ia,
-                           const sgMat4 xform);
-
-
-    void extractCacheRelativeVertex(double t, ssgVtxArray *va,
-                                    GroundProperty *gp,
-                                    short i, sgVec3 rel_pos,
-                                    sgdVec3 wgs84_vel);
-
-    void extractWgs84Vertex(double t, ssgVtxArray *va,
-                            GroundProperty *gp, short i,
-                            sgdVec3 wgs84_pos, sgdVec3 wgs84_vel);
+    osg::ref_ptr<osg::Group> _group;
+#endif
 };
 
 #endif