1 // groundcache.cxx -- carries a small subset of the scenegraph near the vehicle
3 // Written by Mathias Froehlich, started Nov 2004.
5 // Copyright (C) 2004, 2009 Mathias Froehlich - Mathias.Froehlich@web.de
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.
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.
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.
27 #include "groundcache.hxx"
31 #include <osg/Drawable>
33 #include <osg/Geometry>
35 #include <osg/Transform>
36 #include <osg/MatrixTransform>
37 #include <osg/PositionAttitudeTransform>
38 #include <osg/CameraView>
40 #include <simgear/sg_inlines.h>
41 #include <simgear/constants.h>
42 #include <simgear/debug/logstream.hxx>
43 #include <simgear/math/SGMisc.hxx>
44 #include <simgear/scene/util/SGNodeMasks.hxx>
45 #include <simgear/scene/util/SGSceneUserData.hxx>
46 #include <simgear/scene/util/OsgMath.hxx>
48 #include <simgear/scene/bvh/BVHNode.hxx>
49 #include <simgear/scene/bvh/BVHGroup.hxx>
50 #include <simgear/scene/bvh/BVHTransform.hxx>
51 #include <simgear/scene/bvh/BVHMotionTransform.hxx>
52 #include <simgear/scene/bvh/BVHLineGeometry.hxx>
53 #include <simgear/scene/bvh/BVHStaticGeometry.hxx>
54 #include <simgear/scene/bvh/BVHStaticData.hxx>
55 #include <simgear/scene/bvh/BVHStaticNode.hxx>
56 #include <simgear/scene/bvh/BVHStaticTriangle.hxx>
57 #include <simgear/scene/bvh/BVHStaticBinary.hxx>
58 #include <simgear/scene/bvh/BVHSubTreeCollector.hxx>
59 #include <simgear/scene/bvh/BVHLineSegmentVisitor.hxx>
60 #include <simgear/scene/bvh/BVHNearestPointVisitor.hxx>
62 #ifdef GROUNDCACHE_DEBUG
63 #include <simgear/scene/bvh/BVHDebugCollectVisitor.hxx>
64 #include <Main/fg_props.hxx>
67 #include <Main/globals.hxx>
68 #include <Scenery/scenery.hxx>
69 #include <Scenery/tilemgr.hxx>
73 using namespace simgear;
75 class FGGroundCache::CacheFill : public osg::NodeVisitor {
77 CacheFill(const SGVec3d& center, const SGVec3d& down, const double& radius,
78 const double& startTime, const double& endTime) :
79 osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ACTIVE_CHILDREN),
83 _startTime(startTime),
86 _maxDown(SGGeod::fromCart(center).getElevationM() + 9999),
90 setTraversalMask(SG_NODEMASK_TERRAIN_BIT);
92 virtual void apply(osg::Node& node)
94 if (!testBoundingSphere(node.getBound()))
97 addBoundingVolume(node);
100 virtual void apply(osg::Group& group)
102 if (!testBoundingSphere(group.getBound()))
105 simgear::BVHSubTreeCollector::NodeList parentNodeList;
106 mSubTreeCollector.pushNodeList(parentNodeList);
109 addBoundingVolume(group);
111 mSubTreeCollector.popNodeList(parentNodeList);
114 virtual void apply(osg::Transform& transform)
115 { handleTransform(transform); }
116 virtual void apply(osg::Camera& camera)
118 if (camera.getRenderOrder() != osg::Camera::NESTED_RENDER)
120 handleTransform(camera);
122 virtual void apply(osg::CameraView& transform)
123 { handleTransform(transform); }
124 virtual void apply(osg::MatrixTransform& transform)
125 { handleTransform(transform); }
126 virtual void apply(osg::PositionAttitudeTransform& transform)
127 { handleTransform(transform); }
129 void handleTransform(osg::Transform& transform)
131 // Hmm, may be this needs to be refined somehow ...
132 if (transform.getReferenceFrame() != osg::Transform::RELATIVE_RF)
135 if (!testBoundingSphere(transform.getBound()))
138 osg::Matrix inverseMatrix;
139 if (!transform.computeWorldToLocalMatrix(inverseMatrix, this))
142 if (!transform.computeLocalToWorldMatrix(matrix, this))
145 // Look for a velocity note
146 const SGSceneUserData::Velocity* velocity = getVelocity(transform);
148 SGVec3d center = _center;
149 SGVec3d down = _down;
150 double radius = _radius;
151 bool haveHit = _haveHit;
152 const SGMaterial* material = _material;
155 _center = toSG(inverseMatrix.preMult(toOsg(_center)));
156 _down = toSG(osg::Matrix::transform3x3(toOsg(_down), inverseMatrix));
158 SGVec3d staticCenter(_center);
160 double dtStart = velocity->referenceTime - _startTime;
161 SGVec3d startCenter = staticCenter + dtStart*velocity->linear;
162 SGQuatd startOr(SGQuatd::fromAngleAxis(dtStart*velocity->angular));
163 startCenter = startOr.transform(startCenter);
165 double dtEnd = velocity->referenceTime - _endTime;
166 SGVec3d endCenter = staticCenter + dtEnd*velocity->linear;
167 SGQuatd endOr(SGQuatd::fromAngleAxis(dtEnd*velocity->angular));
168 endCenter = endOr.transform(endCenter);
170 _center = 0.5*(startCenter + endCenter);
171 _down = startOr.transform(_down);
172 _radius += 0.5*dist(startCenter, endCenter);
175 simgear::BVHSubTreeCollector::NodeList parentNodeList;
176 mSubTreeCollector.pushNodeList(parentNodeList);
178 addBoundingVolume(transform);
181 if (mSubTreeCollector.haveChildren()) {
183 simgear::BVHMotionTransform* bvhTransform;
184 bvhTransform = new simgear::BVHMotionTransform;
185 bvhTransform->setToWorldTransform(SGMatrixd(matrix.ptr()));
186 bvhTransform->setLinearVelocity(velocity->linear);
187 bvhTransform->setAngularVelocity(velocity->angular);
188 bvhTransform->setReferenceTime(velocity->referenceTime);
189 bvhTransform->setStartTime(_startTime);
190 bvhTransform->setEndTime(_endTime);
191 bvhTransform->setId(velocity->id);
193 mSubTreeCollector.popNodeList(parentNodeList, bvhTransform);
195 simgear::BVHTransform* bvhTransform;
196 bvhTransform = new simgear::BVHTransform;
197 bvhTransform->setToWorldTransform(SGMatrixd(matrix.ptr()));
199 mSubTreeCollector.popNodeList(parentNodeList, bvhTransform);
202 mSubTreeCollector.popNodeList(parentNodeList);
207 double dt = _startTime - velocity->referenceTime;
208 SGQuatd ori(SGQuatd::fromAngleAxis(dt*velocity->angular));
209 _sceneryHit = ori.transform(_sceneryHit);
210 _sceneryHit += dt*velocity->linear;
212 _sceneryHit = toSG(matrix.preMult(toOsg(_sceneryHit)));
214 _material = material;
223 const SGSceneUserData::Velocity* getVelocity(osg::Node& node)
225 SGSceneUserData* userData = SGSceneUserData::getSceneUserData(&node);
228 return userData->getVelocity();
230 simgear::BVHNode* getNodeBoundingVolume(osg::Node& node)
232 SGSceneUserData* userData = SGSceneUserData::getSceneUserData(&node);
235 return userData->getBVHNode();
237 void addBoundingVolume(osg::Node& node)
239 simgear::BVHNode* bvNode = getNodeBoundingVolume(node);
243 // Find a croase ground intersection
244 SGLineSegmentd line(_center + _radius*_down, _center + _maxDown*_down);
245 simgear::BVHLineSegmentVisitor lineSegmentVisitor(line, _startTime);
246 bvNode->accept(lineSegmentVisitor);
247 if (!lineSegmentVisitor.empty()) {
248 _sceneryHit = lineSegmentVisitor.getPoint();
249 _material = lineSegmentVisitor.getMaterial();
250 _maxDown = SGMiscd::max(_radius, dot(_down, _sceneryHit - _center));
254 // Get that part of the local bv tree that intersects our sphere
256 mSubTreeCollector.setSphere(SGSphered(_center, _radius));
257 bvNode->accept(mSubTreeCollector);
260 bool testBoundingSphere(const osg::BoundingSphere& bound) const
265 SGLineSegmentd downSeg(_center, _center + _maxDown*_down);
266 double maxDist = bound._radius + _radius;
267 SGVec3d boundCenter(toVec3d(toSG(bound._center)));
268 return distSqr(downSeg, boundCenter) <= maxDist*maxDist;
271 SGSharedPtr<simgear::BVHNode> getBVHNode() const
272 { return mSubTreeCollector.getNode(); }
274 bool getHaveElevationBelowCache() const
276 double getElevationBelowCache() const
277 { return SGGeod::fromCart(_sceneryHit).getElevationM(); }
278 const SGMaterial* getMaterialBelowCache() const
279 { return _material; }
288 simgear::BVHSubTreeCollector mSubTreeCollector;
291 const SGMaterial* _material;
295 FGGroundCache::FGGroundCache() :
299 cache_time_offset(0),
301 reference_wgs84_point(SGVec3d(0, 0, 0)),
302 reference_vehicle_radius(0),
306 #ifdef GROUNDCACHE_DEBUG
307 _lookupTime = SGTimeStamp::fromSec(0.0);
309 _buildTime = SGTimeStamp::fromSec(0.0);
314 FGGroundCache::~FGGroundCache()
319 FGGroundCache::prepare_ground_cache(double startSimTime, double endSimTime,
320 const SGVec3d& pt, double rad)
322 #ifdef GROUNDCACHE_DEBUG
323 SGTimeStamp t0 = SGTimeStamp::now();
327 found_ground = false;
329 SGGeod geodPt = SGGeod::fromCart(pt);
330 // Don't blow away the cache ground_radius and stuff if there's no
332 if (!globals->get_tile_mgr()->schedule_scenery(geodPt, rad, 1.0)) {
333 SG_LOG(SG_FLIGHT, SG_WARN, "prepare_ground_cache(): scenery_available "
334 "returns false at " << geodPt << " " << pt << " " << rad);
339 // If we have an active wire, get some more area into the groundcache
341 rad = SGMiscd::max(200, rad);
343 // Store the parameters we used to build up that cache.
344 reference_wgs84_point = pt;
345 reference_vehicle_radius = rad;
346 // Store the time reference used to compute movements of moving triangles.
347 cache_ref_time = startSimTime;
349 // Get a normalized down vector valid for the whole cache
350 SGQuatd hlToEc = SGQuatd::fromLonLat(geodPt);
351 down = hlToEc.rotate(SGVec3d(0, 0, 1));
353 // Get the ground cache, that is a local collision tree of the environment
354 startSimTime += cache_time_offset;
355 endSimTime += cache_time_offset;
356 CacheFill subtreeCollector(pt, down, rad, startSimTime, endSimTime);
357 globals->get_scenery()->get_scene_graph()->accept(subtreeCollector);
358 _localBvhTree = subtreeCollector.getBVHNode();
360 if (subtreeCollector.getHaveElevationBelowCache()) {
361 // Use the altitude value below the cache that we gathered during
363 _altitude = subtreeCollector.getElevationBelowCache();
364 _material = subtreeCollector.getMaterialBelowCache();
366 } else if (_localBvhTree) {
367 // We have nothing below us, so try starting with the lowest point
368 // upwards for a croase altitude value
369 SGLineSegmentd line(pt + reference_vehicle_radius*down, pt - 1e3*down);
370 simgear::BVHLineSegmentVisitor lineSegmentVisitor(line, startSimTime);
371 _localBvhTree->accept(lineSegmentVisitor);
373 if (!lineSegmentVisitor.empty()) {
374 SGGeod geodPt = SGGeod::fromCart(lineSegmentVisitor.getPoint());
375 _altitude = geodPt.getElevationM();
376 _material = lineSegmentVisitor.getMaterial();
382 // Ok, still nothing here?? Last resort ...
384 found_ground = globals->get_scenery()->
385 get_elevation_m(SGGeod::fromGeodM(geodPt, 10000), alt, &_material);
390 // Still not sucessful??
392 SG_LOG(SG_FLIGHT, SG_WARN, "prepare_ground_cache(): trying to build "
393 "cache without any scenery below the aircraft");
395 #ifdef GROUNDCACHE_DEBUG
396 t0 = SGTimeStamp::now() - t0;
400 if (_buildCount > 60) {
401 double buildTime = 0;
403 buildTime = _buildTime.toSecs()/_buildCount;
404 double lookupTime = 0;
406 lookupTime = _lookupTime.toSecs()/_lookupCount;
407 _buildTime = SGTimeStamp::fromSec(0.0);
409 _lookupTime = SGTimeStamp::fromSec(0.0);
411 SG_LOG(SG_FLIGHT, SG_ALERT, "build time = " << buildTime
412 << ", lookup Time = " << lookupTime);
415 if (!_group.valid()) {
416 _group = new osg::Group;
417 globals->get_scenery()->get_scene_graph()->addChild(_group);
418 fgSetInt("/fdm/groundcache-debug-level", -3);
420 _group->removeChildren(0, _group->getNumChildren());
422 int level = fgGetInt("/fdm/groundcache-debug-level");
424 simgear::BVHDebugCollectVisitor debug(endSimTime, level);
425 _localBvhTree->accept(debug);
426 _group->addChild(debug.getNode());
435 FGGroundCache::is_valid(double& ref_time, SGVec3d& pt, double& rad)
437 pt = reference_wgs84_point;
438 rad = reference_vehicle_radius;
439 ref_time = cache_ref_time;
443 class FGGroundCache::BodyFinder : public BVHVisitor {
445 BodyFinder(BVHNode::Id id, const double& t) :
447 _bodyToWorld(SGMatrixd::unit()),
448 _linearVelocity(0, 0, 0),
449 _angularVelocity(0, 0, 0),
453 virtual void apply(BVHGroup& leaf)
457 leaf.traverse(*this);
459 virtual void apply(BVHTransform& transform)
464 transform.traverse(*this);
467 _linearVelocity = transform.vecToWorld(_linearVelocity);
468 _angularVelocity = transform.vecToWorld(_angularVelocity);
469 _bodyToWorld = transform.getToWorldTransform()*_bodyToWorld;
472 virtual void apply(BVHMotionTransform& transform)
477 if (_id == transform.getId()) {
480 transform.traverse(*this);
484 SGMatrixd toWorld = transform.getToWorldTransform(_time);
485 SGVec3d referencePoint = _bodyToWorld.xformPt(SGVec3d::zeros());
486 _linearVelocity += transform.getLinearVelocityAt(referencePoint);
487 _angularVelocity += transform.getAngularVelocity();
488 _linearVelocity = toWorld.xformVec(_linearVelocity);
489 _angularVelocity = toWorld.xformVec(_angularVelocity);
490 _bodyToWorld = toWorld*_bodyToWorld;
493 virtual void apply(BVHLineGeometry& node) { }
494 virtual void apply(BVHStaticGeometry& node) { }
496 virtual void apply(const BVHStaticBinary&, const BVHStaticData&) { }
497 virtual void apply(const BVHStaticTriangle&, const BVHStaticData&) { }
499 const SGMatrixd& getBodyToWorld() const
500 { return _bodyToWorld; }
501 const SGVec3d& getLinearVelocity() const
502 { return _linearVelocity; }
503 const SGVec3d& getAngularVelocity() const
504 { return _angularVelocity; }
507 { return !_foundId; }
510 simgear::BVHNode::Id _id;
512 SGMatrixd _bodyToWorld;
514 SGVec3d _linearVelocity;
515 SGVec3d _angularVelocity;
523 FGGroundCache::get_body(double t, SGMatrixd& bodyToWorld, SGVec3d& linearVel,
524 SGVec3d& angularVel, simgear::BVHNode::Id id)
526 // Get the transform matrix and velocities of a moving body with id at t.
529 t += cache_time_offset;
530 BodyFinder bodyFinder(id, t);
531 _localBvhTree->accept(bodyFinder);
532 if (bodyFinder.empty())
535 bodyToWorld = bodyFinder.getBodyToWorld();
536 linearVel = bodyFinder.getLinearVelocity();
537 angularVel = bodyFinder.getAngularVelocity();
542 class FGGroundCache::CatapultFinder : public BVHVisitor {
544 CatapultFinder(const SGSphered& sphere, const double& t) :
545 _haveLineSegment(false),
550 virtual void apply(BVHGroup& leaf)
552 if (!intersects(_sphere, leaf.getBoundingSphere()))
554 leaf.traverse(*this);
556 virtual void apply(BVHTransform& transform)
558 if (!intersects(_sphere, transform.getBoundingSphere()))
561 SGSphered sphere = _sphere;
562 _sphere = transform.sphereToLocal(sphere);
563 bool haveLineSegment = _haveLineSegment;
564 _haveLineSegment = false;
566 transform.traverse(*this);
568 if (_haveLineSegment) {
569 _lineSegment = transform.lineSegmentToWorld(_lineSegment);
570 _linearVelocity = transform.vecToWorld(_linearVelocity);
571 _angularVelocity = transform.vecToWorld(_angularVelocity);
573 _haveLineSegment |= haveLineSegment;
574 _sphere.setCenter(sphere.getCenter());
576 virtual void apply(BVHMotionTransform& transform)
578 if (!intersects(_sphere, transform.getBoundingSphere()))
581 SGSphered sphere = _sphere;
582 _sphere = transform.sphereToLocal(sphere, _time);
583 bool haveLineSegment = _haveLineSegment;
584 _haveLineSegment = false;
586 transform.traverse(*this);
588 if (_haveLineSegment) {
589 SGMatrixd toWorld = transform.getToWorldTransform(_time);
591 += transform.getLinearVelocityAt(_lineSegment.getStart());
592 _angularVelocity += transform.getAngularVelocity();
593 _linearVelocity = toWorld.xformVec(_linearVelocity);
594 _angularVelocity = toWorld.xformVec(_angularVelocity);
595 _lineSegment = _lineSegment.transform(toWorld);
597 _haveLineSegment |= haveLineSegment;
598 _sphere.setCenter(sphere.getCenter());
600 virtual void apply(BVHLineGeometry& node)
602 if (node.getType() != BVHLineGeometry::CarrierCatapult)
605 SGLineSegmentd lineSegment(node.getLineSegment());
606 if (!intersects(_sphere, lineSegment))
609 _lineSegment = lineSegment;
610 double dist = distSqr(lineSegment, getSphere().getCenter());
611 _sphere.setRadius(sqrt(dist));
612 _linearVelocity = SGVec3d::zeros();
613 _angularVelocity = SGVec3d::zeros();
614 _haveLineSegment = true;
616 virtual void apply(BVHStaticGeometry& node)
619 virtual void apply(const BVHStaticBinary&, const BVHStaticData&) { }
620 virtual void apply(const BVHStaticTriangle&, const BVHStaticData&) { }
622 void setSphere(const SGSphered& sphere)
623 { _sphere = sphere; }
624 const SGSphered& getSphere() const
627 const SGLineSegmentd& getLineSegment() const
628 { return _lineSegment; }
629 const SGVec3d& getLinearVelocity() const
630 { return _linearVelocity; }
631 const SGVec3d& getAngularVelocity() const
632 { return _angularVelocity; }
634 bool getHaveLineSegment() const
635 { return _haveLineSegment; }
638 SGLineSegmentd _lineSegment;
639 SGVec3d _linearVelocity;
640 SGVec3d _angularVelocity;
642 bool _haveLineSegment;
649 FGGroundCache::get_cat(double t, const SGVec3d& pt,
650 SGVec3d end[2], SGVec3d vel[2])
652 double maxDistance = 1000;
654 // Get the wire in question
655 t += cache_time_offset;
656 CatapultFinder catapultFinder(SGSphered(pt, maxDistance), t);
658 _localBvhTree->accept(catapultFinder);
660 if (!catapultFinder.getHaveLineSegment())
663 // prepare the returns
664 end[0] = catapultFinder.getLineSegment().getStart();
665 end[1] = catapultFinder.getLineSegment().getEnd();
667 // The linear velocity is the one at the start of the line segment ...
668 vel[0] = catapultFinder.getLinearVelocity();
669 // ... so the end point has the additional cross product.
670 vel[1] = catapultFinder.getLinearVelocity();
671 vel[1] += cross(catapultFinder.getAngularVelocity(),
672 catapultFinder.getLineSegment().getDirection());
674 // Return the distance to the cat
675 return sqrt(distSqr(catapultFinder.getLineSegment(), pt));
679 FGGroundCache::get_agl(double t, const SGVec3d& pt, SGVec3d& contact,
680 SGVec3d& normal, SGVec3d& linearVel, SGVec3d& angularVel,
681 simgear::BVHNode::Id& id, const SGMaterial*& material)
683 #ifdef GROUNDCACHE_DEBUG
684 SGTimeStamp t0 = SGTimeStamp::now();
687 // Just set up a ground intersection query for the given point
688 SGLineSegmentd line(pt, pt + 10*reference_vehicle_radius*down);
689 t += cache_time_offset;
690 simgear::BVHLineSegmentVisitor lineSegmentVisitor(line, t);
692 _localBvhTree->accept(lineSegmentVisitor);
694 #ifdef GROUNDCACHE_DEBUG
695 t0 = SGTimeStamp::now() - t0;
700 if (!lineSegmentVisitor.empty()) {
701 // Have an intersection
702 contact = lineSegmentVisitor.getPoint();
703 normal = lineSegmentVisitor.getNormal();
704 if (0 < dot(normal, down))
706 linearVel = lineSegmentVisitor.getLinearVelocity();
707 angularVel = lineSegmentVisitor.getAngularVelocity();
708 material = lineSegmentVisitor.getMaterial();
709 id = lineSegmentVisitor.getId();
713 // Whenever we did not have a ground triangle for the requested point,
714 // take the ground level we found during the current cache build.
715 // This is as good as what we had before for agl.
716 SGGeod geodPt = SGGeod::fromCart(pt);
717 geodPt.setElevationM(_altitude);
718 contact = SGVec3d::fromGeod(geodPt);
720 linearVel = SGVec3d(0, 0, 0);
721 angularVel = SGVec3d(0, 0, 0);
722 material = _material;
731 FGGroundCache::get_nearest(double t, const SGVec3d& pt, double maxDist,
732 SGVec3d& contact, SGVec3d& linearVel,
733 SGVec3d& angularVel, simgear::BVHNode::Id& id,
734 const SGMaterial*& material)
739 #ifdef GROUNDCACHE_DEBUG
740 SGTimeStamp t0 = SGTimeStamp::now();
743 // Just set up a ground intersection query for the given point
744 SGSphered sphere(pt, maxDist);
745 t += cache_time_offset;
746 simgear::BVHNearestPointVisitor nearestPointVisitor(sphere, t);
747 _localBvhTree->accept(nearestPointVisitor);
749 #ifdef GROUNDCACHE_DEBUG
750 t0 = SGTimeStamp::now() - t0;
755 if (nearestPointVisitor.empty())
758 // Have geometry in the range of maxDist
759 contact = nearestPointVisitor.getPoint();
760 linearVel = nearestPointVisitor.getLinearVelocity();
761 angularVel = nearestPointVisitor.getAngularVelocity();
762 material = nearestPointVisitor.getMaterial();
763 id = nearestPointVisitor.getId();
769 class FGGroundCache::WireIntersector : public BVHVisitor {
771 WireIntersector(const SGVec3d pt[4], const double& t) :
772 _linearVelocity(SGVec3d::zeros()),
773 _angularVelocity(SGVec3d::zeros()),
777 // Build the two triangles spanning the area where the hook has moved
778 // during the past step.
779 _triangles[0].set(pt[0], pt[1], pt[2]);
780 _triangles[1].set(pt[0], pt[2], pt[3]);
783 virtual void apply(BVHGroup& leaf)
785 if (!_intersects(leaf.getBoundingSphere()))
788 leaf.traverse(*this);
790 virtual void apply(BVHTransform& transform)
792 if (!_intersects(transform.getBoundingSphere()))
795 SGTriangled triangles[2] = { _triangles[0], _triangles[1] };
796 _triangles[0] = triangles[0].transform(transform.getToLocalTransform());
797 _triangles[1] = triangles[1].transform(transform.getToLocalTransform());
799 transform.traverse(*this);
802 _lineSegment = transform.lineSegmentToWorld(_lineSegment);
803 _linearVelocity = transform.vecToWorld(_linearVelocity);
804 _angularVelocity = transform.vecToWorld(_angularVelocity);
806 _triangles[0] = triangles[0];
807 _triangles[1] = triangles[1];
809 virtual void apply(BVHMotionTransform& transform)
811 if (!_intersects(transform.getBoundingSphere()))
814 SGMatrixd toLocal = transform.getToLocalTransform(_time);
816 SGTriangled triangles[2] = { _triangles[0], _triangles[1] };
817 _triangles[0] = triangles[0].transform(toLocal);
818 _triangles[1] = triangles[1].transform(toLocal);
820 transform.traverse(*this);
823 SGMatrixd toWorld = transform.getToWorldTransform(_time);
825 += transform.getLinearVelocityAt(_lineSegment.getStart());
826 _angularVelocity += transform.getAngularVelocity();
827 _linearVelocity = toWorld.xformVec(_linearVelocity);
828 _angularVelocity = toWorld.xformVec(_angularVelocity);
829 _lineSegment = _lineSegment.transform(toWorld);
831 _triangles[0] = triangles[0];
832 _triangles[1] = triangles[1];
834 virtual void apply(BVHLineGeometry& node)
836 if (node.getType() != BVHLineGeometry::CarrierWire)
838 SGLineSegmentd lineSegment(node.getLineSegment());
839 if (!_intersects(lineSegment))
842 _lineSegment = lineSegment;
843 _linearVelocity = SGVec3d::zeros();
844 _angularVelocity = SGVec3d::zeros();
847 virtual void apply(BVHStaticGeometry& node)
850 virtual void apply(const BVHStaticBinary&, const BVHStaticData&) { }
851 virtual void apply(const BVHStaticTriangle&, const BVHStaticData&) { }
853 bool _intersects(const SGSphered& sphere) const
857 if (intersects(_triangles[0], sphere))
859 if (intersects(_triangles[1], sphere))
863 bool _intersects(const SGLineSegmentd& lineSegment) const
867 if (intersects(_triangles[0], lineSegment))
869 if (intersects(_triangles[1], lineSegment))
874 const SGLineSegmentd& getLineSegment() const
875 { return _lineSegment; }
876 const SGVec3d& getLinearVelocity() const
877 { return _linearVelocity; }
878 const SGVec3d& getAngularVelocity() const
879 { return _angularVelocity; }
881 const BVHLineGeometry* getWire() const
885 SGLineSegmentd _lineSegment;
886 SGVec3d _linearVelocity;
887 SGVec3d _angularVelocity;
888 const BVHLineGeometry* _wire;
890 SGTriangled _triangles[2];
894 bool FGGroundCache::caught_wire(double t, const SGVec3d pt[4])
896 // Get the wire in question
897 t += cache_time_offset;
898 WireIntersector wireIntersector(pt, t);
900 _localBvhTree->accept(wireIntersector);
902 _wire = wireIntersector.getWire();
906 class FGGroundCache::WireFinder : public BVHVisitor {
908 WireFinder(const BVHLineGeometry* wire, const double& t) :
911 _lineSegment(SGVec3d::zeros(), SGVec3d::zeros()),
912 _linearVelocity(SGVec3d::zeros()),
913 _angularVelocity(SGVec3d::zeros()),
914 _haveLineSegment(false)
917 virtual void apply(BVHGroup& leaf)
919 if (_haveLineSegment)
921 leaf.traverse(*this);
923 virtual void apply(BVHTransform& transform)
925 if (_haveLineSegment)
928 transform.traverse(*this);
930 if (_haveLineSegment) {
931 _linearVelocity = transform.vecToWorld(_linearVelocity);
932 _angularVelocity = transform.vecToWorld(_angularVelocity);
933 _lineSegment = transform.lineSegmentToWorld(_lineSegment);
936 virtual void apply(BVHMotionTransform& transform)
938 if (_haveLineSegment)
941 transform.traverse(*this);
943 if (_haveLineSegment) {
944 SGMatrixd toWorld = transform.getToWorldTransform(_time);
946 += transform.getLinearVelocityAt(_lineSegment.getStart());
947 _angularVelocity += transform.getAngularVelocity();
948 _linearVelocity = toWorld.xformVec(_linearVelocity);
949 _angularVelocity = toWorld.xformVec(_angularVelocity);
950 _lineSegment = _lineSegment.transform(toWorld);
953 virtual void apply(BVHLineGeometry& node)
955 if (_haveLineSegment)
959 if (node.getType() != BVHLineGeometry::CarrierWire)
961 _lineSegment = SGLineSegmentd(node.getLineSegment());
962 _linearVelocity = SGVec3d::zeros();
963 _angularVelocity = SGVec3d::zeros();
964 _haveLineSegment = true;
966 virtual void apply(BVHStaticGeometry&) { }
968 virtual void apply(const BVHStaticBinary&, const BVHStaticData&) { }
969 virtual void apply(const BVHStaticTriangle&, const BVHStaticData&) { }
971 const SGLineSegmentd& getLineSegment() const
972 { return _lineSegment; }
974 bool getHaveLineSegment() const
975 { return _haveLineSegment; }
977 const SGVec3d& getLinearVelocity() const
978 { return _linearVelocity; }
979 const SGVec3d& getAngularVelocity() const
980 { return _angularVelocity; }
983 const BVHLineGeometry* _wire;
986 SGLineSegmentd _lineSegment;
987 SGVec3d _linearVelocity;
988 SGVec3d _angularVelocity;
990 bool _haveLineSegment;
993 bool FGGroundCache::get_wire_ends(double t, SGVec3d end[2], SGVec3d vel[2])
995 // Fast return if we do not have an active wire.
999 // Get the wire in question
1000 t += cache_time_offset;
1001 WireFinder wireFinder(_wire, t);
1003 _localBvhTree->accept(wireFinder);
1005 if (!wireFinder.getHaveLineSegment())
1008 // prepare the returns
1009 end[0] = wireFinder.getLineSegment().getStart();
1010 end[1] = wireFinder.getLineSegment().getEnd();
1012 // The linear velocity is the one at the start of the line segment ...
1013 vel[0] = wireFinder.getLinearVelocity();
1014 // ... so the end point has the additional cross product.
1015 vel[1] = wireFinder.getLinearVelocity();
1016 vel[1] += cross(wireFinder.getAngularVelocity(),
1017 wireFinder.getLineSegment().getDirection());
1022 void FGGroundCache::release_wire(void)