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