]> git.mxchange.org Git - flightgear.git/blob - src/FDM/groundcache.hxx
e881463bb9fddcf0da0fd1daec74aa17faf00880
[flightgear.git] / src / FDM / groundcache.hxx
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 - Mathias.Froehlich@web.de
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., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22
23 #ifndef _GROUNDCACHE_HXX
24 #define _GROUNDCACHE_HXX
25
26 #include <plib/sg.h>
27 #include <plib/ssg.h>
28 #include <simgear/compiler.h>
29 #include <simgear/constants.h>
30
31 class FGGroundCache {
32 public:
33     FGGroundCache();
34     ~FGGroundCache();
35
36     //////////////////////////////////////////////////////////////////////////
37     // Ground handling routines
38     //////////////////////////////////////////////////////////////////////////
39
40     // Prepare the ground cache for the wgs84 position pt_*.
41     // That is take all vertices in the ball with radius rad around the
42     // position given by the pt_* and store them in a local scene graph.
43     bool prepare_ground_cache(double ref_time, const double pt[3],
44                               double rad);
45
46     // Returns true if the cache is valid.
47     // Also the reference time, point and radius values where the cache
48     // is valid for are returned.
49     bool is_valid(double *ref_time, double pt[3], double *rad);
50       
51
52     // Return the nearest catapult to the given point
53     // pt in wgs84 coordinates.
54     double get_cat(double t, const double pt[3],
55                    double end[2][3], double vel[2][3]);
56   
57
58     // Return the altitude above ground below the wgs84 point pt
59     // Search for highest triangle not higher than pt + max_altoff.
60     // Return ground properties like the ground type, the maximum load
61     // this kind kind of ground can carry, the friction factor between
62     // 0 and 1 which can be used to model lower friction with wet runways
63     // and finally the altitude above ground.
64     bool get_agl(double t, const double pt[3], double max_altoff,
65                  double contact[3], double normal[3], double vel[3],
66                  int *type, double *loadCapacity,
67                  double *frictionFactor, double *agl);
68
69     // Return 1 if the hook intersects with a wire.
70     // That test is done by checking if the quad spanned by the points pt*
71     // intersects with the line representing the wire.
72     // If the wire is caught, the cache will trace this wires endpoints until
73     // the FDM calls release_wire().
74     bool caught_wire(double t, const double pt[4][3]);
75   
76     // Return the location and speed of the wire endpoints.
77     bool get_wire_ends(double t, double end[2][3], double vel[2][3]);
78
79     // Tell the cache code that it does no longer need to care for
80     // the wire end position.
81     void release_wire(void);
82
83 private:
84     struct Triangle {
85       // The edge vertices.
86       sgdVec3 vertices[3];
87       // The surface normal.
88       sgdVec4 plane;
89       // The bounding shpere.
90       sgdSphere sphere;
91       // The linear velocity.
92       sgdVec3 velocity;
93       // Ground type
94       int type;
95     };
96     struct Catapult {
97       sgdVec3 start;
98       sgdVec3 end;
99       sgdVec3 velocity;
100     };
101     struct Wire {
102       sgdVec3 ends[2];
103       sgdVec3 velocity;
104       int wire_id;
105     };
106
107
108     // The center of the cache.
109     sgdVec3 cache_center;
110     // Approximate ground radius.
111     // In case the aircraft is too high above ground.
112     double ground_radius;
113     // The time reference for later call to intersection test routines.
114     // Is required since we will have moving triangles in carriers.
115     double cache_ref_time;
116     // The wire identifier to track.
117     int wire_id;
118
119     // Containers which hold all the essential information about this cache.
120     std::vector<Triangle> triangles;
121     std::vector<Catapult> catapults;
122     std::vector<Wire> wires;
123
124     // The point and radius where the cache is built around.
125     // That are the arguments that were given to prepare_ground_cache.
126     sgdVec3 reference_wgs84_point;
127     double reference_vehicle_radius;
128     bool found_ground;
129
130
131     // Fills the environment cache with everything inside the sphere sp.
132     void cache_fill(ssgBranch *branch, sgdMat4 xform,
133                     sgdSphere* sp, sgdVec3 down, sgdSphere* wsp);
134
135     // compute the ground property of this leaf.
136     void putSurfaceLeafIntoCache(const sgdSphere *sp, const sgdMat4 xform,
137                                  bool sphIsec, sgdVec3 down, ssgLeaf *l);
138
139     void putLineLeafIntoCache(const sgdSphere *wsp, const sgdMat4 xform,
140                               ssgLeaf *l);
141
142     // Helper class to hold some properties of the ground triangle.
143     struct GroundProperty {
144       GroundProperty() : type(0) {}
145       int type;
146       int wire_id;
147       sgVec3 vel;
148       // not yet implemented ...
149 //       double loadCapacity;
150     };
151
152     // compute the ground property of this leaf.
153     static GroundProperty extractGroundProperty( ssgLeaf* leaf );
154
155
156     static void velocityTransformTriangle(double dt, Triangle& dst,
157                                           const Triangle& src);
158 };
159
160 #endif