]> git.mxchange.org Git - flightgear.git/blobdiff - src/FDM/groundcache.cxx
Fix line endings
[flightgear.git] / src / FDM / groundcache.cxx
index 6c2654083a74e13d8812dfebc8974db71849d0f4..3680ef366dbd2e4b9a271160a63a958cac5a99b3 100644 (file)
@@ -20,6 +20,9 @@
 //
 // $Id$
 
+#ifdef HAVE_CONFIG_H
+#  include "config.h"
+#endif
 
 #include <float.h>
 
@@ -202,7 +205,7 @@ FGGroundCache::extractGroundProperty( ssgLeaf* l )
 {
   // FIXME: Do more ...
   // Idea: have a get_globals() function which knows about that stuff.
-  // Or most propably read that from a configuration file,
+  // Or most probably read that from a configuration file,
   // from property tree or whatever ...
   
   // Get ground dependent data.
@@ -226,13 +229,15 @@ FGGroundCache::extractGroundProperty( ssgLeaf* l )
     }
 
     // Copy the velocity from the carrier class.
-    ud->carrier->getVelocityWrtEarth( gp.vel );
+    ud->carrier->getVelocityWrtEarth( gp.vel, gp.rot, gp.pivot );
   }
 
   else {
 
     // Initialize velocity field.
-    sgSetVec3( gp.vel, 0.0, 0.0, 0.0 );
+    sgdSetVec3( gp.vel, 0.0, 0.0, 0.0 );
+    sgdSetVec3( gp.rot, 0.0, 0.0, 0.0 );
+    sgdSetVec3( gp.pivot, 0.0, 0.0, 0.0 );
   }
   
   // Get the texture name and decide what ground type we have.
@@ -285,7 +290,9 @@ FGGroundCache::putLineLeafIntoCache(const sgdSphere *wsp, const sgdMat4 xform,
         Wire wire;
         sgdCopyVec3(wire.ends[0], ends[0]);
         sgdCopyVec3(wire.ends[1], ends[1]);
-        sgdSetVec3(wire.velocity, gp.vel);
+        sgdCopyVec3(wire.velocity, gp.vel);
+        sgdCopyVec3(wire.rotation, gp.rot);
+        sgdSubVec3(wire.rotation_pivot, gp.pivot, cache_center);
         wire.wire_id = gp.wire_id;
 
         wires.push_back(wire);
@@ -294,7 +301,9 @@ FGGroundCache::putLineLeafIntoCache(const sgdSphere *wsp, const sgdMat4 xform,
         Catapult cat;
         sgdCopyVec3(cat.start, ends[0]);
         sgdCopyVec3(cat.end, ends[1]);
-        sgdSetVec3(cat.velocity, gp.vel);
+        sgdCopyVec3(cat.velocity, gp.vel);
+        sgdCopyVec3(cat.rotation, gp.rot);
+        sgdSubVec3(cat.rotation_pivot, gp.pivot, cache_center);
 
         catapults.push_back(cat);
       }
@@ -334,7 +343,9 @@ FGGroundCache::putSurfaceLeafIntoCache(const sgdSphere *sp,
     // Check if the sphere around the vehicle intersects the sphere
     // around that triangle. If so, put that triangle into the cache.
     if (sphIsec && sp->intersects(&t.sphere)) {
-      sgdSetVec3(t.velocity, gp.vel);
+      sgdCopyVec3(t.velocity, gp.vel);
+      sgdCopyVec3(t.rotation, gp.rot);
+      sgdSubVec3(t.rotation_pivot, gp.pivot, cache_center);
       t.type = gp.type;
       triangles.push_back(t);
     }
@@ -348,11 +359,18 @@ FGGroundCache::putSurfaceLeafIntoCache(const sgdSphere *sp,
       sgdVec3 isectpoint;
       if ( sgdIsectInfLinePlane( isectpoint, tmp, down, t.plane ) &&
            fgdPointInTriangle( isectpoint, t.vertices ) ) {
-        found_ground = true;
-        sgdAddVec3(isectpoint, cache_center);
-        double this_radius = sgdLengthVec3(isectpoint);
-        if (ground_radius < this_radius)
-          ground_radius = this_radius;
+        // Compute the offset to the ground cache midpoint
+        sgdVec3 off;
+        sgdSubVec3(off, isectpoint, tmp);
+        // Only accept the altitude if the intersection point is below the
+        // ground cache midpoint
+        if (0 < sgdScalarProductVec3( off, down )) {
+          found_ground = true;
+          sgdAddVec3(isectpoint, cache_center);
+          double this_radius = sgdLengthVec3(isectpoint);
+          if (ground_radius < this_radius)
+            ground_radius = this_radius;
+        }
       }
     }
   }
@@ -373,15 +391,25 @@ FGGroundCache::velocityTransformTriangle(double dt,
   dst.sphere.radius = src.sphere.radius;
 
   sgdCopyVec3(dst.velocity, src.velocity);
+  sgdCopyVec3(dst.rotation, src.rotation);
+  sgdCopyVec3(dst.rotation_pivot, src.rotation_pivot);
 
   dst.type = src.type;
 
   if (dt*sgdLengthSquaredVec3(src.velocity) != 0) {
-    sgdAddScaledVec3(dst.vertices[0], src.velocity, dt);
-    sgdAddScaledVec3(dst.vertices[1], src.velocity, dt);
-    sgdAddScaledVec3(dst.vertices[2], src.velocity, dt);
+    sgdVec3 pivotoff, vel;
+    for (int i = 0; i < 3; ++i) {
+      sgdSubVec3(pivotoff, src.vertices[i], src.rotation_pivot);
+      sgdVectorProductVec3(vel, src.rotation, pivotoff);
+      sgdAddVec3(vel, src.velocity);
+      sgdAddScaledVec3(dst.vertices[i], vel, dt);
+    }
     
-    dst.plane[3] += dt*sgdScalarProductVec3(dst.plane, src.velocity);
+    // Transform the plane equation
+    sgdSubVec3(pivotoff, dst.plane, src.rotation_pivot);
+    sgdVectorProductVec3(vel, src.rotation, pivotoff);
+    sgdAddVec3(vel, src.velocity);
+    dst.plane[3] += dt*sgdScalarProductVec3(dst.plane, vel);
 
     sgdAddScaledVec3(dst.sphere.center, src.velocity, dt);
   }
@@ -416,7 +444,7 @@ FGGroundCache::cache_fill(ssgBranch *branch, sgdMat4 xform,
     if ( e->isAKindOf( ssgTypeBranch() ) ) {
       ssgBranch *b = (ssgBranch *)e;
       if ( b->isAKindOf( ssgTypeTransform() ) ) {
-        // Collect up the transfors required to reach that part of
+        // Collect up the transforms required to reach that part of
         // the branch.
         sgMat4 xform2;
         sgMakeIdentMat4( xform2 );
@@ -433,9 +461,9 @@ FGGroundCache::cache_fill(ssgBranch *branch, sgdMat4 xform,
     // This will minimize the number of vertices/triangles in the cache.
     else if (e->isAKindOf(ssgTypeLeaf())) {
       // Since we reach that leaf if we have an intersection with the
-      // most propably bigger wire/catapult cache sphere, we need to check
+      // most probably bigger wire/catapult cache sphere, we need to check
       // that here, if the smaller cache for the surface has a chance for hits.
-      // Also, if the spheres do not intersect compute a croase agl value
+      // Also, if the spheres do not intersect compute a coarse agl value
       // by following the line downwards originating at the aircraft.
       bool spIsec = sp->intersects(&esphere);
       putSurfaceLeafIntoCache(sp, xform, spIsec, down, (ssgLeaf *)e);
@@ -468,7 +496,7 @@ FGGroundCache::prepare_ground_cache(double ref_time, const double pt[3],
   // Decide where we put the scenery center.
   Point3D old_cntr = globals->get_scenery()->get_center();
   Point3D cntr(pt[0], pt[1], pt[2]);
-  // Only move the cache center if it is unaccaptable far away.
+  // Only move the cache center if it is unacceptable far away.
   if (40*40 < old_cntr.distance3Dsquared(cntr))
     globals->get_scenery()->set_center(cntr);
   else
@@ -498,7 +526,7 @@ FGGroundCache::prepare_ground_cache(double ref_time, const double pt[3],
   sgdSetVec3(down, -pt[0], -pt[1], -pt[2]);
   sgdNormalizeVec3(down);
 
-  // We collaps all transforms we need to reach a particular leaf.
+  // We collapse all transforms we need to reach a particular leaf.
   // The leafs itself will be then transformed later.
   // So our cache is just flat.
   // For leafs which are moving (carriers surface, etc ...)
@@ -514,7 +542,7 @@ FGGroundCache::prepare_ground_cache(double ref_time, const double pt[3],
   cache_fill(terrain, xform, &acSphere, down, &wireSphere);
 
   // some stats
-  SG_LOG(SG_FLIGHT,SG_INFO, "prepare_ground_cache(): ac radius = " << rad
+  SG_LOG(SG_FLIGHT,SG_DEBUG, "prepare_ground_cache(): ac radius = " << rad
          << ", # triangles = " << triangles.size()
          << ", # wires = " << wires.size()
          << ", # catapults = " << catapults.size()
@@ -554,15 +582,23 @@ FGGroundCache::get_cat(double t, const double dpt[3],
 
   size_t sz = catapults.size();
   for (size_t i = 0; i < sz; ++i) {
+    sgdVec3 pivotoff, rvel[2];
     sgdLineSegment3 ls;
     sgdCopyVec3(ls.a, catapults[i].start);
     sgdCopyVec3(ls.b, catapults[i].end);
 
+    sgdSubVec3(pivotoff, ls.a, catapults[i].rotation_pivot);
+    sgdVectorProductVec3(rvel[0], catapults[i].rotation, pivotoff);
+    sgdAddVec3(rvel[0], catapults[i].velocity);
+    sgdSubVec3(pivotoff, ls.b, catapults[i].rotation_pivot);
+    sgdVectorProductVec3(rvel[1], catapults[i].rotation, pivotoff);
+    sgdAddVec3(rvel[1], catapults[i].velocity);
+
     sgdAddVec3(ls.a, cache_center);
     sgdAddVec3(ls.b, cache_center);
 
-    sgdAddScaledVec3(ls.a, catapults[i].velocity, t);
-    sgdAddScaledVec3(ls.b, catapults[i].velocity, t);
+    sgdAddScaledVec3(ls.a, rvel[0], t);
+    sgdAddScaledVec3(ls.b, rvel[1], t);
     
     double this_dist = sgdDistSquaredToLineSegmentVec3( ls, dpt );
     if (this_dist < dist) {
@@ -573,8 +609,8 @@ FGGroundCache::get_cat(double t, const double dpt[3],
       // The carrier code takes care of that ordering.
       sgdCopyVec3( end[0], ls.a );
       sgdCopyVec3( end[1], ls.b );
-      sgdCopyVec3( vel[0], catapults[i].velocity );
-      sgdCopyVec3( vel[1], catapults[i].velocity );
+      sgdCopyVec3( vel[0], rvel[0] );
+      sgdCopyVec3( vel[1], rvel[1] );
     }
   }
 
@@ -583,10 +619,10 @@ FGGroundCache::get_cat(double t, const double dpt[3],
 }
 
 bool
-FGGroundCache::get_agl(double t, const double dpt[3],
-                         double contact[3], double normal[3], double vel[3],
-                         int *type, double *loadCapacity,
-                         double *frictionFactor, double *agl)
+FGGroundCache::get_agl(double t, const double dpt[3], double max_altoff,
+                       double contact[3], double normal[3], double vel[3],
+                       int *type, double *loadCapacity,
+                       double *frictionFactor, double *agl)
 {
   bool ret = false;
 
@@ -608,9 +644,10 @@ FGGroundCache::get_agl(double t, const double dpt[3],
   // The search direction
   sgdVec3 dir;
   sgdSetVec3( dir, -dpt[0], -dpt[1], -dpt[2] );
+  sgdNormaliseVec3( dir );
 
   // Initialize to something sensible
-  double sqdist = DBL_MAX;
+  double current_radius = 0.0;
 
   size_t sz = triangles.size();
   for (size_t i = 0; i < sz; ++i) {
@@ -623,28 +660,35 @@ FGGroundCache::get_agl(double t, const double dpt[3],
     sgdVec3 isecpoint;
     if ( sgdIsectInfLinePlane( isecpoint, pt, dir, triangle.plane ) &&
          sgdPointInTriangle( isecpoint, triangle.vertices ) ) {
-
-      // Check for the closest intersection point.
-      // FIXME: is this the right one?
-      SGDfloat newSqdist = sgdDistanceSquaredVec3( isecpoint, pt );
-      if ( newSqdist < sqdist ) {
-        sqdist = newSqdist;
-        ret = true;
-        // Save the new potential intersection point.
-        sgdCopyVec3( contact, isecpoint );
-        sgdAddVec3( contact, cache_center );
-        // The first three values in the vector are the plane normal.
-        sgdCopyVec3( normal, triangle.plane );
-        // The velocity wrt earth.
-        /// FIXME: only true for non rotating objects!!!!
-        sgdCopyVec3( vel, triangle.velocity );
-        // Save the ground type.
-        *type = triangle.type;
-        // FIXME: figure out how to get that sign ...
+      // Compute the vector from pt to the intersection point ...
+      sgdVec3 off;
+      sgdSubVec3(off, isecpoint, pt);
+      // ... and check if it is too high or not
+      if (-max_altoff < sgdScalarProductVec3( off, dir )) {
+        // Transform to the wgs system
+        sgdAddVec3( isecpoint, cache_center );
+        // compute the radius, good enough approximation to take the geocentric radius
+        SGDfloat radius = sgdLengthSquaredVec3(isecpoint);
+        if (current_radius < radius) {
+          current_radius = radius;
+          ret = true;
+          // Save the new potential intersection point.
+          sgdCopyVec3( contact, isecpoint );
+          // The first three values in the vector are the plane normal.
+          sgdCopyVec3( normal, triangle.plane );
+          // The velocity wrt earth.
+          sgdVec3 pivotoff;
+          sgdSubVec3(pivotoff, pt, triangle.rotation_pivot);
+          sgdVectorProductVec3(vel, triangle.rotation, pivotoff);
+          sgdAddVec3(vel, triangle.velocity);
+          // Save the ground type.
+          *type = triangle.type;
+          // FIXME: figure out how to get that sign ...
 //           *agl = sqrt(sqdist);
-        *agl = sgdLengthVec3( dpt ) - sgdLengthVec3( contact );
+          *agl = sgdLengthVec3( dpt ) - sgdLengthVec3( contact );
 //           *loadCapacity = DBL_MAX;
 //           *frictionFactor = 1.0;
+        }
       }
     }
   }
@@ -695,17 +739,18 @@ bool FGGroundCache::caught_wire(double t, const double pt[4][3])
   sgdCopyVec3( tri[1][2], pt[3] );
 
   // Intersect the wire lines with each of these triangles.
-  // You have cautght a wire if they intersect.
+  // You have caught a wire if they intersect.
   for (size_t i = 0; i < sz; ++i) {
     sgdVec3 le[2];
-    sgdCopyVec3(le[0], wires[i].ends[0]);
-    sgdCopyVec3(le[1], wires[i].ends[1]);
-
-    sgdAddVec3(le[0], cache_center);
-    sgdAddVec3(le[1], cache_center);
-
-    sgdAddScaledVec3(le[0], wires[i].velocity, t);
-    sgdAddScaledVec3(le[1], wires[i].velocity, t);
+    for (int k = 0; k < 2; ++k) {
+      sgdVec3 pivotoff, vel;
+      sgdCopyVec3(le[k], wires[i].ends[k]);
+      sgdSubVec3(pivotoff, le[k], wires[i].rotation_pivot);
+      sgdVectorProductVec3(vel, wires[i].rotation, pivotoff);
+      sgdAddVec3(vel, wires[i].velocity);
+      sgdAddScaledVec3(le[k], vel, t);
+      sgdAddVec3(le[k], cache_center);
+    }
     
     for (int k=0; k<2; ++k) {
       sgdVec3 isecpoint;
@@ -736,17 +781,15 @@ bool FGGroundCache::get_wire_ends(double t, double end[2][3], double vel[2][3])
   size_t sz = wires.size();
   for (size_t i = 0; i < sz; ++i) {
     if (wires[i].wire_id == wire_id) {
-      sgdCopyVec3(end[0], wires[i].ends[0]);
-      sgdCopyVec3(end[1], wires[i].ends[1]);
-      
-      sgdAddVec3(end[0], cache_center);
-      sgdAddVec3(end[1], cache_center);
-      
-      sgdAddScaledVec3(end[0], wires[i].velocity, t);
-      sgdAddScaledVec3(end[1], wires[i].velocity, t);
-      
-      sgdCopyVec3(vel[0], wires[i].velocity);
-      sgdCopyVec3(vel[1], wires[i].velocity);
+      for (size_t k = 0; k < 2; ++k) {
+        sgdVec3 pivotoff;
+        sgdCopyVec3(end[k], wires[i].ends[k]);
+        sgdSubVec3(pivotoff, end[k], wires[i].rotation_pivot);
+        sgdVectorProductVec3(vel[k], wires[i].rotation, pivotoff);
+        sgdAddVec3(vel[k], wires[i].velocity);
+        sgdAddScaledVec3(end[k], vel[k], t);
+        sgdAddVec3(end[k], cache_center);
+      }
       return true;
     }
   }