| 1 | // groundcache.hxx -- carries a small subset of the scenegraph near the vehicle |
| 2 | // |
| 3 | // Written by Mathias Froehlich, started Nov 2004. |
| 4 | // |
| 5 | // Copyright (C) 2004 Mathias Froehlich - [email protected] |
| 6 | // |
| 7 | // This program is free software; you can redistribute it and/or |
| 8 | // modify it under the terms of the GNU General Public License as |
| 9 | // published by the Free Software Foundation; either version 2 of the |
| 10 | // License, or (at your option) any later version. |
| 11 | // |
| 12 | // This program is distributed in the hope that it will be useful, but |
| 13 | // WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | // General Public License for more details. |
| 16 | // |
| 17 | // You should have received a copy of the GNU General Public License |
| 18 | // along with this program; if not, write to the Free Software |
| 19 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 20 | // |
| 21 | // $Id$ |
| 22 | |
| 23 | #ifndef _GROUNDCACHE_HXX |
| 24 | #define _GROUNDCACHE_HXX |
| 25 | |
| 26 | #include <simgear/compiler.h> |
| 27 | #include <simgear/constants.h> |
| 28 | #include <simgear/math/SGMath.hxx> |
| 29 | #include <simgear/math/SGGeometry.hxx> |
| 30 | #include <simgear/bvh/BVHNode.hxx> |
| 31 | #include <simgear/structure/SGSharedPtr.hxx> |
| 32 | |
| 33 | // #define GROUNDCACHE_DEBUG |
| 34 | #ifdef GROUNDCACHE_DEBUG |
| 35 | #include <osg/Group> |
| 36 | #include <osg/ref_ptr> |
| 37 | #include <simgear/timing/timestamp.hxx> |
| 38 | #endif |
| 39 | |
| 40 | namespace simgear { |
| 41 | class BVHLineGeometry; |
| 42 | class BVHMaterial; |
| 43 | } |
| 44 | |
| 45 | class FGGroundCache { |
| 46 | public: |
| 47 | FGGroundCache(); |
| 48 | ~FGGroundCache(); |
| 49 | |
| 50 | ////////////////////////////////////////////////////////////////////////// |
| 51 | // Ground handling routines |
| 52 | ////////////////////////////////////////////////////////////////////////// |
| 53 | |
| 54 | // Prepare the ground cache for the wgs84 position pt_*. |
| 55 | // That is take all vertices in the ball with radius rad around the |
| 56 | // position given by the pt_* and store them in a local scene graph. |
| 57 | bool prepare_ground_cache(double startSimTime, double endSimTime, |
| 58 | const SGVec3d& pt, double rad); |
| 59 | |
| 60 | // Returns true if the cache is valid. |
| 61 | // Also the reference time, point and radius values where the cache |
| 62 | // is valid for are returned. |
| 63 | bool is_valid(double& ref_time, SGVec3d& pt, double& rad); |
| 64 | |
| 65 | // Returns the unit down vector at the ground cache |
| 66 | const SGVec3d& get_down() const |
| 67 | { return down; } |
| 68 | |
| 69 | // The time offset that originates from a simtime different than zero |
| 70 | // at initialization time of the fdm. |
| 71 | double get_cache_time_offset() const |
| 72 | { return cache_time_offset; } |
| 73 | void set_cache_time_offset(double time_offset) |
| 74 | { cache_time_offset = time_offset; } |
| 75 | |
| 76 | bool get_body(double t, SGMatrixd& bodyToWorld, SGVec3d& linearVel, |
| 77 | SGVec3d& angularVel, simgear::BVHNode::Id id); |
| 78 | |
| 79 | // Return the nearest catapult to the given point |
| 80 | // pt in wgs84 coordinates. |
| 81 | double get_cat(double t, const SGVec3d& pt, |
| 82 | SGVec3d end[2], SGVec3d vel[2]); |
| 83 | |
| 84 | |
| 85 | // Return the altitude above ground below the wgs84 point pt |
| 86 | // Search for highest triangle not higher than pt + max_altoff. |
| 87 | // Return ground properties like the maximum load |
| 88 | // this kind kind of ground can carry, the friction factor between |
| 89 | // 0 and 1 which can be used to model lower friction with wet runways. |
| 90 | bool get_agl(double t, const SGVec3d& pt, SGVec3d& contact, |
| 91 | SGVec3d& normal, SGVec3d& linearVel, SGVec3d& angularVel, |
| 92 | simgear::BVHNode::Id& id, |
| 93 | const simgear::BVHMaterial*& material); |
| 94 | |
| 95 | bool get_nearest(double t, const SGVec3d& pt, double maxDist, |
| 96 | SGVec3d& contact, SGVec3d& linearVel, SGVec3d& angularVel, |
| 97 | simgear::BVHNode::Id& id, |
| 98 | const simgear::BVHMaterial*& material); |
| 99 | |
| 100 | // Return 1 if the hook intersects with a wire. |
| 101 | // That test is done by checking if the quad spanned by the points pt* |
| 102 | // intersects with the line representing the wire. |
| 103 | // If the wire is caught, the cache will trace this wires endpoints until |
| 104 | // the FDM calls release_wire(). |
| 105 | bool caught_wire(double t, const SGVec3d pt[4]); |
| 106 | |
| 107 | // Return the location and speed of the wire endpoints. |
| 108 | bool get_wire_ends(double t, SGVec3d end[2], SGVec3d vel[2]); |
| 109 | |
| 110 | // Tell the cache code that it does no longer need to care for |
| 111 | // the wire end position. |
| 112 | void release_wire(void); |
| 113 | |
| 114 | private: |
| 115 | class CacheFill; |
| 116 | class BodyFinder; |
| 117 | class CatapultFinder; |
| 118 | class WireIntersector; |
| 119 | class WireFinder; |
| 120 | |
| 121 | // Approximate ground radius. |
| 122 | // In case the aircraft is too high above ground. |
| 123 | double _altitude; |
| 124 | // the simgear material reference, contains friction coeficients ... |
| 125 | const simgear::BVHMaterial* _material; |
| 126 | // The time reference for later call to intersection test routines. |
| 127 | // Is required since we will have moving triangles in carriers. |
| 128 | double cache_ref_time; |
| 129 | // The time the cache was initialized. |
| 130 | double cache_time_offset; |
| 131 | // The wire to track. |
| 132 | const simgear::BVHLineGeometry* _wire; |
| 133 | |
| 134 | // The point and radius where the cache is built around. |
| 135 | // That are the arguments that were given to prepare_ground_cache. |
| 136 | SGVec3d reference_wgs84_point; |
| 137 | double reference_vehicle_radius; |
| 138 | SGVec3d down; |
| 139 | bool found_ground; |
| 140 | |
| 141 | SGSharedPtr<simgear::BVHNode> _localBvhTree; |
| 142 | |
| 143 | #ifdef GROUNDCACHE_DEBUG |
| 144 | SGTimeStamp _lookupTime; |
| 145 | unsigned _lookupCount; |
| 146 | SGTimeStamp _buildTime; |
| 147 | unsigned _buildCount; |
| 148 | |
| 149 | osg::ref_ptr<osg::Group> _group; |
| 150 | #endif |
| 151 | }; |
| 152 | |
| 153 | #endif |