]> git.mxchange.org Git - flightgear.git/blob - src/FDM/groundcache.cxx
6ade97accdb010f36f131f9713586ff8db518116
[flightgear.git] / src / FDM / groundcache.cxx
1 // groundcache.cxx -- 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
24 #include <float.h>
25
26 #include <plib/sg.h>
27
28 #include <simgear/constants.h>
29 #include <simgear/debug/logstream.hxx>
30 #include <simgear/math/sg_geodesy.hxx>
31
32 #include <Main/globals.hxx>
33 #include <Scenery/scenery.hxx>
34 #include <Scenery/tilemgr.hxx>
35 #include <AIModel/AICarrier.hxx>
36
37 #include "flight.hxx"
38 #include "groundcache.hxx"
39
40 FGGroundCache::FGGroundCache()
41 {
42   sgdSetVec3(cache_center, 0.0, 0.0, 0.0);
43   ground_radius = 0.0;
44   cache_ref_time = 0.0;
45   wire_id = 0;
46   sgdSetVec3(reference_wgs84_point, 0.0, 0.0, 0.0);
47   reference_vehicle_radius = 0.0;
48   found_ground = false;
49 }
50
51 FGGroundCache::~FGGroundCache()
52 {
53   cache_root.removeAllKids();
54 }
55
56 FGGroundCache::GroundProperty*
57 FGGroundCache::extractGroundProperty( ssgLeaf* l )
58 {
59   // FIXME: Do more ...
60   // Idea: have a get_globals() function which knows about that stuff.
61   // Or most propably read that from a configuration file,
62   // from property tree or whatever ...
63   
64   // Get ground dependent data.
65   GroundProperty *gp = new GroundProperty;
66   gp->wire_id = -1;
67   
68   FGAICarrierHardware *ud =
69     dynamic_cast<FGAICarrierHardware*>(l->getUserData());
70   if (ud) {
71     switch (ud->type) {
72     case FGAICarrierHardware::Wire:
73       gp->type = FGInterface::Wire;
74       gp->wire_id = ud->id;
75       break;
76     case FGAICarrierHardware::Catapult:
77       gp->type = FGInterface::Catapult;
78       break;
79     default:
80       gp->type = FGInterface::Solid;
81       break;
82     }
83
84     // Copy the velocity from the carrier class.
85     ud->carrier->getVelocityWrtEarth( gp->vel );
86   }
87
88   else {
89
90     // Initialize velocity field.
91     sgSetVec3( gp->vel, 0.0, 0.0, 0.0 );
92   }
93   
94   // Get the texture name and decide what ground type we have.
95   ssgState *st = l->getState();
96   if (st != NULL && st->isAKindOf(ssgTypeSimpleState())) {
97     ssgSimpleState *ss = (ssgSimpleState*)st;
98     SGPath fullPath( ss->getTextureFilename() ? ss->getTextureFilename(): "" );
99     string file = fullPath.file();
100     SGPath dirPath(fullPath.dir());
101     string category = dirPath.file();
102     
103     SG_LOG(SG_FLIGHT,SG_INFO,
104            "New triangle in cache: " << category << " " << file );
105     
106     if (category == "Runway")
107       gp->type = FGInterface::Solid;
108     else {
109       if (file == "asphault.rgb" || file == "airport.rgb")
110         gp->type = FGInterface::Solid;
111       else if (file == "water.rgb" || file == "water-lake.rgb")
112         gp->type = FGInterface::Water;
113       else if (file == "forest.rgb" || file == "cropwood.rgb")
114         gp->type = FGInterface::Forest;
115     }
116   }
117   
118   return gp;
119 }
120
121 // Test if the line given by the point on the line pt_on_line and the
122 // line direction dir intersects the sphere sp.
123 // Adapted from plib.
124 static bool
125 sgIsectSphereInfLine(const sgSphere *sp,
126                      const sgVec3 pt_on_line, const sgVec3 dir)
127 {
128   sgVec3 r ;
129   sgSubVec3 ( r, sp->getCenter(), pt_on_line ) ;
130
131   SGfloat projectedDistance = sgScalarProductVec3(r, dir);
132  
133   SGfloat dist = sgScalarProductVec3 ( r, r ) -
134     projectedDistance * projectedDistance; 
135
136   SGfloat radius = sp->getRadius();
137   return dist < radius*radius;
138 }
139
140 void
141 FGGroundCache::addAndFlattenLeaf(GLenum ty, ssgLeaf *l, ssgIndexArray *ia,
142                                  const sgMat4 xform)
143 {
144   // Extract data from the leaf which is just copied.
145   ssgVertexArray *va = ((ssgVtxTable *)l)->getVertices();
146   ssgNormalArray *na = ((ssgVtxTable *)l)->getNormals();
147   // Create a new leaf.
148   ssgVtxArray *vtxa = new ssgVtxArray( ty, va, na, 0, 0, ia );
149   // Clones data ...
150   vtxa->removeUnusedVertices();
151   // Apply transform. We won't store transforms in our cache.
152   vtxa->transform( xform );
153   // Check for magic texture names object names and such ...
154   vtxa->setUserData( extractGroundProperty( l ) );
155   vtxa->setCullFace( l->getCullFace() );
156   // Finally append to cache.
157   cache_root.addKid((ssgEntity*)vtxa);
158 }
159
160 void
161 FGGroundCache::putLineLeafIntoCache(const sgSphere *wsp, const sgMat4 xform,
162                                     ssgLeaf *l)
163 {
164   ssgIndexArray *ia = 0;
165   
166   // Lines must have special meanings.
167   // Wires and catapults are done with lines.
168   int nl = l->getNumLines();
169   for (int i = 0; i < nl; ++i) {
170     sgSphere tmp;
171     short v[2];
172     l->getLine(i, v, v+1 );
173     for (int k=0; k<2; ++k)
174       tmp.extend( l->getVertex( v[k] ) );
175     tmp.orthoXform(xform);
176     
177     if (wsp->intersects( &tmp )) {
178       if (ia == 0)
179         ia = new ssgIndexArray();
180         
181       ia->add( v[0] );
182       ia->add( v[1] );
183     }
184   }
185   if (!ia)
186     return;
187   
188   addAndFlattenLeaf(GL_LINES, l, ia, xform);
189 }
190
191 void
192 FGGroundCache::putSurfaceLeafIntoCache(const sgSphere *sp, const sgMat4 xform,
193                                        bool sphIsec, sgVec3 down, ssgLeaf *l)
194 {
195   ssgIndexArray *ia = 0;
196   
197   int nt = l->getNumTriangles();
198   for (int i = 0; i < nt; ++i) {
199     // Build up a sphere around that particular triangle-
200     sgSphere tmp;
201     short v[3];
202     l->getTriangle(i, v, v+1, v+2 );
203     for (int k=0; k<3; ++k)
204       tmp.extend( l->getVertex( v[k] ) );
205     tmp.orthoXform(xform);
206
207     // Check if the sphere around the vehicle intersects the sphere
208     // around that triangle. If so, put that triangle into the cache.
209     if (sphIsec && sp->intersects( &tmp )) {
210       if (ia == 0)
211         ia = new ssgIndexArray();
212       
213       ia->add( v[0] );
214       ia->add( v[1] );
215       ia->add( v[2] );
216     }
217     
218     // In case the cache is empty, we still provide agl computations.
219     // But then we use the old way of having a fixed elevation value for
220     // the whole lifetime of this cache.
221     if ( sgIsectSphereInfLine(&tmp, sp->getCenter(), down) ) {
222       sgVec3 tri[3];
223       for (int k=0; k<3; ++k) {
224         sgCopyVec3( tri[k], l->getVertex( v[k] ) );
225         sgXformPnt3( tri[k], xform );
226       }
227       
228       sgVec4 plane;
229       sgMakePlane( plane, tri[0], tri[1], tri[2]  );
230       sgVec3 ac_cent;
231       sgCopyVec3(ac_cent, sp->getCenter());
232       sgVec3 dst;
233       sgIsectInfLinePlane( dst, ac_cent, down, plane );
234       if ( sgPointInTriangle ( dst, tri ) ) {
235         found_ground = true;
236         sgdVec3 ddst;
237         sgdSetVec3(ddst, dst);
238         sgdAddVec3(ddst, cache_center);
239         double this_radius = sgdLengthVec3(ddst);
240         if (ground_radius < this_radius)
241           ground_radius = this_radius;
242       }
243     }
244   }
245   if (!ia)
246     return;
247   
248   addAndFlattenLeaf(GL_TRIANGLES, l, ia, xform);
249 }
250
251 // Here is the point where rotation should be handled
252 void
253 FGGroundCache::extractCacheRelativeVertex(double t, ssgVtxArray *va,
254                                           GroundProperty *gp,
255                                           short i, sgVec3 rel_pos,
256                                           sgdVec3 wgs84_vel)
257 {
258   sgCopyVec3( rel_pos, va->getVertex( i ) );
259   sgAddScaledVec3( rel_pos, gp->vel, t );
260
261   // Set velocity.
262   sgdSetVec3( wgs84_vel, gp->vel );
263 }
264
265 void
266 FGGroundCache::extractWgs84Vertex(double t, ssgVtxArray *va,
267                                   GroundProperty *gp, short i,
268                                   sgdVec3 wgs84_pos, sgdVec3 wgs84_vel)
269 {
270   sgVec3 rel_pos;
271   extractCacheRelativeVertex(t, va, gp, i, rel_pos, wgs84_vel);
272   sgdSetVec3( wgs84_pos, rel_pos );
273   sgdAddVec3( wgs84_pos, cache_center );
274 }
275
276
277 void
278 FGGroundCache::cache_fill(ssgBranch *branch, sgMat4 xform,
279                           sgSphere* sp, sgVec3 down, sgSphere* wsp)
280 {
281   // Travel through all kids.
282   ssgEntity *e;
283   for ( e = branch->getKid(0); e != NULL ; e = branch->getNextKid() ) {
284     if ( !( e->getTraversalMask() & SSGTRAV_HOT) )
285       continue;
286     if ( e->getBSphere()->isEmpty() )
287       continue;
288     
289     // Wee need to check further if either the sphere around the branch
290     // intersects the sphere around the aircraft or the line downwards from
291     // the aircraft intersects the branchs sphere.
292     sgSphere esphere = *(e->getBSphere());
293     esphere.orthoXform(xform);
294     bool wspIsec = wsp->intersects(&esphere);
295     bool downIsec = sgIsectSphereInfLine(&esphere, sp->getCenter(), down);
296     if (!wspIsec && !downIsec)
297       continue;
298       
299     // For branches collect up the transforms to reach that branch and
300     // call cache_fill recursively.
301     if ( e->isAKindOf( ssgTypeBranch() ) ) {
302       ssgBranch *b = (ssgBranch *)e;
303       if ( b->isAKindOf( ssgTypeTransform() ) ) {
304         // Collect up the transfors required to reach that part of
305         // the branch.
306         sgMat4 xform2;
307         sgMakeIdentMat4( xform2 );
308         ssgTransform *t = (ssgTransform*)b;
309         t->getTransform( xform2 );
310         sgPostMultMat4( xform2, xform );
311         cache_fill( b, xform2, sp, down, wsp );
312       } else
313         cache_fill( b, xform, sp, down, wsp );
314     }
315    
316     // For leafs, check each triangle for intersection.
317     // This will minimize the number of vertices/triangles in the cache.
318     else if (e->isAKindOf(ssgTypeLeaf())) {
319       // Since we reach that leaf if we have an intersection with the
320       // most propably bigger wire/catapult cache sphere, we need to check
321       // that here, if the smaller cache for the surface has a chance for hits.
322       // Also, if the spheres do not intersect compute a croase agl value
323       // by following the line downwards originating at the aircraft.
324       bool spIsec = sp->intersects(&esphere);
325       putSurfaceLeafIntoCache(sp, xform, spIsec, down, (ssgLeaf *)e);
326
327       // If we are here, we need to put all special hardware here into
328       // the cache.
329       if (wspIsec)
330         putLineLeafIntoCache(wsp, xform, (ssgLeaf *)e);
331     }
332   }
333 }
334
335 bool
336 FGGroundCache::prepare_ground_cache(double ref_time, const double pt[3],
337                                       double rad)
338 {
339   // Empty cache.
340   cache_root.removeAllKids();
341   ground_radius = 0.0;
342   found_ground = false;
343
344   // Store the parameters we used to build up that cache.
345   sgdCopyVec3(reference_wgs84_point, pt);
346   reference_vehicle_radius = rad;
347   // Store the time reference used to compute movements of moving triangles.
348   cache_ref_time = ref_time;
349
350   // The center of the cache.
351   Point3D psc = globals->get_tile_mgr()->get_current_center();
352   sgdSetVec3(cache_center, psc[0], psc[1], psc[2]);
353   
354   // Prepare sphere around the aircraft.
355   sgSphere acSphere;
356   acSphere.setRadius(rad);
357
358   // Compute the postion of the aircraft relative to the scenery center.
359   sgdVec3 doffset;
360   sgdSubVec3( doffset, pt, cache_center );
361   sgVec3 offset;
362   sgSetVec3( offset, doffset[0], doffset[1], doffset[2] );
363   acSphere.setCenter( offset );
364
365   // Prepare bigger sphere around the aircraft.
366   // This one is required for reliably finding wires we have caught but
367   // have already left the hopefully smaller sphere for the ground reactions.
368   const double max_wire_dist = 300.0;
369   sgSphere wireSphere;
370   wireSphere.setRadius( max_wire_dist < rad ? rad : max_wire_dist );
371   wireSphere.setCenter( offset );
372
373   // Down vector. Is used for croase agl computations when we are far enough
374   // from ground that we have an empty cache.
375   sgVec3 down;
376   sgSetVec3(down, -pt[0], -pt[1], -pt[2]);
377   sgNormalizeVec3(down);
378
379   // We collaps all transforms we need to reach a particular leaf.
380   // The leafs itself will be then transformed later.
381   // So our cache is just flat.
382   // For leafs which are moving (carriers surface, etc ...)
383   // we will later store a speed in the GroundType class. We can then apply
384   // some translations to that nodes according to the time which has passed
385   // compared to that snapshot.
386   sgMat4 xform;
387   sgMakeIdentMat4(xform);
388
389   // Walk the terrain branch for now.
390   ssgBranch *terrain = globals->get_scenery()->get_scene_graph();
391   cache_fill(terrain, xform, &acSphere, down, &wireSphere);
392
393   // some stats
394   SG_LOG(SG_FLIGHT,SG_INFO, "prepare_ground_cache(): ac radius = " << rad
395          << ", # leafs = " << cache_root.getNumKids()
396          << ", ground_radius = " << ground_radius );
397
398   // If the ground radius is still below 5e6 meters, then we do not yet have
399   // any scenery.
400   found_ground = found_ground && 5e6 < ground_radius;
401   if (!found_ground)
402     SG_LOG(SG_FLIGHT, SG_WARN, "prepare_ground_cache(): trying to build cache "
403            "without any scenery below the aircraft" );
404
405   return found_ground;
406 }
407
408 bool
409 FGGroundCache::is_valid(double *ref_time, double pt[3], double *rad)
410 {
411   sgdCopyVec3(pt, reference_wgs84_point);
412   *rad = reference_vehicle_radius;
413   *ref_time = cache_ref_time;
414   return found_ground;
415 }
416
417 double
418 FGGroundCache::get_cat(double t, const double dpt[3],
419                          double end[2][3], double vel[2][3])
420 {
421   // start with a distance of 1e10 meters...
422   double dist = 1e10;
423
424   // Time difference to the reference time.
425   t -= cache_ref_time;
426
427   // We know that we have a flat cache ...
428   ssgEntity *e;
429   for ( e = cache_root.getKid(0); e != NULL ; e = cache_root.getNextKid() ) {
430     // We just know that, because we build that ourselfs ...
431     ssgVtxArray *va = (ssgVtxArray *)e;
432     // Only lines are interresting ...
433     if (va->getPrimitiveType() != GL_LINES)
434       continue;
435     GroundProperty *gp = dynamic_cast<GroundProperty*>(va->getUserData());
436     // Assertation???
437     if ( !gp )
438       continue;
439     // Check if we have a catapult ...
440     if ( gp->type != FGInterface::Catapult )
441       continue;
442
443     int nl = va->getNumLines();
444     for (int i=0; i < nl; ++i) {
445       sgdLineSegment3 ls;
446       sgdVec3 lsVel[2];
447       short vi[2];
448       va->getLine(i, vi, vi+1 );
449       extractWgs84Vertex(t, va, gp, vi[0], ls.a, lsVel[0]);
450       extractWgs84Vertex(t, va, gp, vi[1], ls.b, lsVel[1]);
451       
452       double this_dist = sgdDistSquaredToLineSegmentVec3( ls, dpt );
453       if (this_dist < dist) {
454         dist = this_dist;
455         
456         // end[0] is the end where the cat starts.
457         // end[1] is the end where the cat ends.
458         // The carrier code takes care of that ordering.
459         sgdCopyVec3( end[0], ls.a );
460         sgdCopyVec3( end[1], ls.b );
461         sgdCopyVec3( vel[0], lsVel[0] );
462         sgdCopyVec3( vel[1], lsVel[1] );
463       }
464     }
465   }
466
467   // At the end take the root, we only computed squared distances ...
468   return sqrt(dist);
469 }
470
471 bool
472 FGGroundCache::get_agl(double t, const double dpt[3],
473                          double contact[3], double normal[3], double vel[3],
474                          int *type, double *loadCapacity,
475                          double *frictionFactor, double *agl)
476 {
477   bool ret = false;
478
479   *type = FGInterface::Unknown;
480 //   *agl = 0.0;
481   *loadCapacity = DBL_MAX;
482   *frictionFactor = 1.0;
483   sgdSetVec3( vel, 0.0, 0.0, 0.0 );
484   sgdSetVec3( contact, 0.0, 0.0, 0.0 );
485   sgdSetVec3( normal, 0.0, 0.0, 0.0 );
486
487   // Time difference to th reference time.
488   t -= cache_ref_time;
489
490   // The double valued point we start to search for intersection.
491   sgdVec3 tmp;
492   sgdSubVec3( tmp, dpt, cache_center );
493   sgVec3 pt;
494   sgSetVec3( pt, tmp );
495
496   // The search direction
497   sgVec3 dir;
498   sgSetVec3( dir, -dpt[0], -dpt[1], -dpt[2] );
499
500   // Initialize to something sensible
501   double sqdist = DBL_MAX;
502
503   // We know that we have a flat cache ...
504   // We just know that, because we build that ourselfs ...
505   ssgEntity *e;
506   for ( e = cache_root.getKid(0) ; e != NULL ; e = cache_root.getNextKid() ) {
507     // We just know that, because we build that ourselfs ...
508     ssgVtxArray *va = (ssgVtxArray *)e;
509     // AGL computations are done with triangle/surface leafs.
510     if (va->getPrimitiveType() != GL_TRIANGLES)
511       continue;
512     GroundProperty *gp = dynamic_cast<GroundProperty*>(va->getUserData());
513     // Assertation???
514     if ( !gp )
515       continue;
516
517     int nt = va->getNumTriangles();
518     for (int i=0; i < nt; ++i) {
519       short vi[3];
520       va->getTriangle( i, vi, vi+1, vi+2 );
521       
522       sgVec3 tri[3];
523       sgdVec3 dvel[3];
524       for (int k=0; k<3; ++k)
525         extractCacheRelativeVertex(t, va, gp, vi[k], tri[k], dvel[k]);
526       sgVec4 plane;
527       sgMakePlane( plane, tri[0], tri[1], tri[2] );
528       
529       // Check for intersection.
530       sgVec3 isecpoint;
531       if ( sgIsectInfLinePlane( isecpoint, pt, dir, plane ) &&
532            sgPointInTriangle3( isecpoint, tri ) ) {
533         // Only accept surfaces with the normal pointing upwards.
534         // For double sided surfaces flip the normal in this case.
535         float dirDot = sgScalarProductVec3(plane, dir);
536         if ( dirDot >= 0 && va->getCullFace() == 1 ) {
537           sgScaleVec4( plane, -1 );
538           dirDot = -dirDot;
539         }
540
541         // Check for the closest intersection point.
542         // FIXME: is this the right one?
543         double newSqdist = sgDistanceSquaredVec3( isecpoint, pt );
544         if ( newSqdist < sqdist && dirDot < 0 ) {
545           sqdist = newSqdist;
546           ret = true;
547           // Save the new potential intersection point.
548           sgdSetVec3( contact, isecpoint );
549           sgdAddVec3( contact, cache_center );
550           // The first three values in the vector are the plane normal.
551           sgdSetVec3( normal, plane );
552           // The velocity wrt earth.
553           /// FIXME: only true for non rotating objects!!!!
554           sgdCopyVec3( vel, dvel[0] );
555           // Save the ground type.
556           *type = gp->type;
557           // FIXME: figure out how to get that sign ...
558 //           *agl = sqrt(sqdist);
559           *agl = sgdLengthVec3( dpt ) - sgdLengthVec3( contact );
560 //           *loadCapacity = DBL_MAX;
561 //           *frictionFactor = 1.0;
562         }
563       }
564     }
565   }
566
567   if (ret)
568     return true;
569
570   // Whenever we did not have a ground triangle for the requested point,
571   // take the ground level we found during the current cache build.
572   // This is as good as what we had before for agl.
573   double r = sgdLengthVec3( dpt );
574   sgdCopyVec3( contact, dpt );
575   sgdScaleVec3( contact, ground_radius/r );
576   sgdCopyVec3( normal, dpt );
577   sgdNormaliseVec3( normal );
578   sgdSetVec3( vel, 0.0, 0.0, 0.0 );
579   
580   // The altitude is the distance of the requested point from the
581   // contact point.
582   *agl = sgdLengthVec3( dpt ) - sgdLengthVec3( contact );
583   *type = FGInterface::Unknown;
584   *loadCapacity = DBL_MAX;
585   *frictionFactor = 1.0;
586
587   return ret;
588 }
589
590 bool FGGroundCache::caught_wire(double t, const double cpt[4][3])
591 {
592   bool ret = false;
593
594   // Time difference to the reference time.
595   t -= cache_ref_time;
596
597   bool firsttime = true;
598   sgVec4 plane[2];
599   sgVec3 tri[2][3];
600
601   // We know that we have a flat cache ...
602   ssgEntity *e;
603   for ( e = cache_root.getKid(0); e != NULL ; e = cache_root.getNextKid() ) {
604     // We just know that, because we build that ourselfs ...
605     ssgVtxArray *va = (ssgVtxArray *)e;
606     // Only lines are interresting ...
607     if (va->getPrimitiveType() != GL_LINES)
608       continue;
609     GroundProperty *gp = dynamic_cast<GroundProperty*>(va->getUserData());
610     // Assertation???
611     if ( !gp )
612       continue;
613     // Check if we have a catapult ...
614     if ( gp->type != FGInterface::Wire )
615       continue;
616
617     // Lazy compute the values required for intersectiion tests.
618     // Since we normally do not have wires in the cache this is a
619     // huge benefit.
620     if (firsttime) {
621       firsttime = false;
622       sgVec3 pt[4];
623       for (int k=0; k<4; ++k) {
624         sgdVec3 tmp;
625         sgdSubVec3( tmp, cpt[k], cache_center );
626         sgSetVec3( pt[k], tmp );
627       }
628       sgMakePlane( plane[0], pt[0], pt[1], pt[2] );
629       sgCopyVec3( tri[0][0], pt[0] );
630       sgCopyVec3( tri[0][1], pt[1] );
631       sgCopyVec3( tri[0][2], pt[2] );
632       sgMakePlane( plane[1], pt[0], pt[2], pt[3] );
633       sgCopyVec3( tri[1][0], pt[0] );
634       sgCopyVec3( tri[1][1], pt[2] );
635       sgCopyVec3( tri[1][2], pt[3] );
636     }
637     
638     int nl = va->getNumLines();
639     for (int i=0; i < nl; ++i) {
640       short vi[2];
641       va->getLine(i, vi, vi+1 );
642       sgVec3 le[2];
643       sgdVec3 dummy;
644       extractCacheRelativeVertex(t, va, gp, vi[0], le[0], dummy);
645       extractCacheRelativeVertex(t, va, gp, vi[1], le[1], dummy);
646       
647       for (int k=0; k<2; ++k) {
648         sgVec3 isecpoint;
649         float isecval = sgIsectLinesegPlane( isecpoint, le[0], le[1], plane[k] );
650         
651         if ( 0.0 <= isecval && isecval <= 1.0 &&
652              sgPointInTriangle( isecpoint, tri[k] ) ) {
653           // Store the wire id.
654           wire_id = gp->wire_id;
655           ret = true;
656         }
657       }
658     }
659   }
660
661   return ret;
662 }
663
664 bool FGGroundCache::get_wire_ends(double t, double end[2][3], double vel[2][3])
665 {
666   // Fast return if we do not have an active wire.
667   if (wire_id < 0)
668     return false;
669
670   bool ret = false;
671
672   // Time difference to th reference time.
673   t -= cache_ref_time;
674
675   // We know that we have a flat cache ...
676   ssgEntity *e;
677   for ( e = cache_root.getKid(0); e != NULL ; e = cache_root.getNextKid() ) {
678     // We just know that, because we build that ourselfs ...
679     ssgVtxArray *va = (ssgVtxArray *)e;
680     // Only lines are interresting ...
681     if (va->getPrimitiveType() != GL_LINES)
682       continue;
683     GroundProperty *gp = dynamic_cast<GroundProperty*>(va->getUserData());
684     // Assertation???
685     if ( !gp )
686       continue;
687     // Check if we have a catapult ...
688     if ( gp->type != FGInterface::Wire )
689       continue;
690     if ( gp->wire_id != wire_id )
691       continue;
692
693     // Get the line ends, that are the wire endpoints.
694     short vi[2];
695     va->getLine(0, vi, vi+1 );
696     extractWgs84Vertex(t, va, gp, vi[0], end[0], vel[0]);
697     extractWgs84Vertex(t, va, gp, vi[1], end[1], vel[1]);
698
699     ret = true;
700   }
701
702   return ret;
703 }
704
705 void FGGroundCache::release_wire(void)
706 {
707   wire_id = -1;
708 }