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 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.
29 #include <osg/Drawable>
31 #include <osg/Geometry>
33 #include <osg/Transform>
34 #include <osg/MatrixTransform>
35 #include <osg/PositionAttitudeTransform>
36 #include <osg/CameraView>
38 #include <simgear/sg_inlines.h>
39 #include <simgear/constants.h>
40 #include <simgear/debug/logstream.hxx>
41 #include <simgear/math/sg_geodesy.hxx>
42 #include <simgear/scene/material/mat.hxx>
43 #include <simgear/scene/util/SGNodeMasks.hxx>
44 #include <simgear/scene/util/SGSceneUserData.hxx>
45 #include <simgear/scene/model/placementtrans.hxx>
47 #include <simgear/scene/bvh/BVHNode.hxx>
48 #include <simgear/scene/bvh/BVHGroup.hxx>
49 #include <simgear/scene/bvh/BVHTransform.hxx>
50 #include <simgear/scene/bvh/BVHMotionTransform.hxx>
51 #include <simgear/scene/bvh/BVHLineGeometry.hxx>
52 #include <simgear/scene/bvh/BVHStaticGeometry.hxx>
53 #include <simgear/scene/bvh/BVHStaticData.hxx>
54 #include <simgear/scene/bvh/BVHStaticNode.hxx>
55 #include <simgear/scene/bvh/BVHStaticTriangle.hxx>
56 #include <simgear/scene/bvh/BVHStaticBinary.hxx>
57 #include <simgear/scene/bvh/BVHSubTreeCollector.hxx>
58 #include <simgear/scene/bvh/BVHLineSegmentVisitor.hxx>
60 #include <Main/globals.hxx>
61 #include <Scenery/scenery.hxx>
62 #include <Scenery/tilemgr.hxx>
65 #include "groundcache.hxx"
67 using namespace simgear;
69 static FGInterface::GroundType
70 materialToGroundType(const SGMaterial* material)
73 return FGInterface::Solid;
74 if (material->get_solid())
75 return FGInterface::Solid;
76 return FGInterface::Water;
79 class FGGroundCache::CacheFill : public osg::NodeVisitor {
81 CacheFill(const SGVec3d& center, const double& radius,
82 const double& startTime, const double& endTime) :
83 osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ACTIVE_CHILDREN),
86 _startTime(startTime),
89 setTraversalMask(SG_NODEMASK_TERRAIN_BIT);
91 virtual void apply(osg::Node& node)
93 if (!testBoundingSphere(node.getBound()))
96 addBoundingVolume(node);
99 virtual void apply(osg::Group& group)
101 if (!testBoundingSphere(group.getBound()))
104 simgear::BVHSubTreeCollector::NodeList parentNodeList;
105 mSubTreeCollector.pushNodeList(parentNodeList);
108 addBoundingVolume(group);
110 mSubTreeCollector.popNodeList(parentNodeList);
113 virtual void apply(osg::Transform& transform)
114 { handleTransform(transform); }
115 virtual void apply(osg::Camera& camera)
117 if (camera.getRenderOrder() != osg::Camera::NESTED_RENDER)
119 handleTransform(camera);
121 virtual void apply(osg::CameraView& transform)
122 { handleTransform(transform); }
123 virtual void apply(osg::MatrixTransform& transform)
124 { handleTransform(transform); }
125 virtual void apply(osg::PositionAttitudeTransform& transform)
126 { handleTransform(transform); }
128 void handleTransform(osg::Transform& transform)
130 // Hmm, may be this needs to be refined somehow ...
131 if (transform.getReferenceFrame() != osg::Transform::RELATIVE_RF)
134 if (!testBoundingSphere(transform.getBound()))
137 osg::Matrix inverseMatrix;
138 if (!transform.computeWorldToLocalMatrix(inverseMatrix, this))
141 if (!transform.computeLocalToWorldMatrix(matrix, this))
144 // Look for a velocity note
145 const SGSceneUserData::Velocity* velocity = getVelocity(transform);
147 SGVec3d center = _center;
148 _center = SGVec3d(inverseMatrix.preMult(_center.osg()));
149 double radius = _radius;
151 _radius += (_endTime - _startTime)*norm(velocity->linear);
153 simgear::BVHSubTreeCollector::NodeList parentNodeList;
154 mSubTreeCollector.pushNodeList(parentNodeList);
156 addBoundingVolume(transform);
159 if (mSubTreeCollector.haveChildren()) {
161 simgear::BVHMotionTransform* bvhTransform;
162 bvhTransform = new simgear::BVHMotionTransform;
163 bvhTransform->setToWorldTransform(SGMatrixd(matrix.ptr()));
164 bvhTransform->setLinearVelocity(velocity->linear);
165 bvhTransform->setAngularVelocity(velocity->angular);
166 bvhTransform->setReferenceTime(_startTime);
167 bvhTransform->setEndTime(_endTime);
168 bvhTransform->setId(velocity->id);
170 mSubTreeCollector.popNodeList(parentNodeList, bvhTransform);
172 simgear::BVHTransform* bvhTransform;
173 bvhTransform = new simgear::BVHTransform;
174 bvhTransform->setToWorldTransform(SGMatrixd(matrix.ptr()));
176 mSubTreeCollector.popNodeList(parentNodeList, bvhTransform);
179 mSubTreeCollector.popNodeList(parentNodeList);
185 const SGSceneUserData::Velocity* getVelocity(osg::Node& node)
187 SGSceneUserData* userData = SGSceneUserData::getSceneUserData(&node);
190 return userData->getVelocity();
192 simgear::BVHNode* getNodeBoundingVolume(osg::Node& node)
194 SGSceneUserData* userData = SGSceneUserData::getSceneUserData(&node);
197 return userData->getBVHNode();
199 void addBoundingVolume(osg::Node& node)
201 simgear::BVHNode* bvNode = getNodeBoundingVolume(node);
205 // Get that part of the local bv tree that intersects our sphere
207 mSubTreeCollector.setSphere(SGSphered(_center, _radius));
208 bvNode->accept(mSubTreeCollector);
211 bool testBoundingSphere(const osg::BoundingSphere& bound) const
216 double maxDist = bound._radius + _radius;
217 return distSqr(SGVec3d(bound._center), _center) <= maxDist*maxDist;
220 SGSharedPtr<simgear::BVHNode> getBVHNode() const
221 { return mSubTreeCollector.getNode(); }
230 simgear::BVHSubTreeCollector mSubTreeCollector;
233 FGGroundCache::FGGroundCache() :
239 reference_wgs84_point(SGVec3d(0, 0, 0)),
240 reference_vehicle_radius(0),
246 FGGroundCache::~FGGroundCache()
251 FGGroundCache::prepare_ground_cache(double ref_time, const SGVec3d& pt,
255 found_ground = false;
257 SGGeod geodPt = SGGeod::fromCart(pt);
258 // Don't blow away the cache ground_radius and stuff if there's no
260 if (!globals->get_tile_mgr()->scenery_available(geodPt.getLatitudeDeg(),
261 geodPt.getLongitudeDeg(),
266 // If we have an active wire, get some more area into the groundcache
268 rad = SGMiscd::max(200, rad);
270 // Store the parameters we used to build up that cache.
271 reference_wgs84_point = pt;
272 reference_vehicle_radius = rad;
273 // Store the time reference used to compute movements of moving triangles.
274 cache_ref_time = ref_time;
276 // Get a normalized down vector valid for the whole cache
277 SGQuatd hlToEc = SGQuatd::fromLonLat(geodPt);
278 down = hlToEc.rotate(SGVec3d(0, 0, 1));
280 // Get the ground cache, that is a local collision tree of the environment
281 double endTime = cache_ref_time + 1; //FIXME??
282 CacheFill subtreeCollector(pt, rad, cache_ref_time, endTime);
283 globals->get_scenery()->get_scene_graph()->accept(subtreeCollector);
284 _localBvhTree = subtreeCollector.getBVHNode();
286 // Try to get a croase altitude value for the ground cache
287 SGLineSegmentd line(pt, pt + 1e4*down);
288 simgear::BVHLineSegmentVisitor lineSegmentVisitor(line, ref_time);
290 _localBvhTree->accept(lineSegmentVisitor);
292 // If this is successful, store this altitude for croase altitude values
293 if (!lineSegmentVisitor.empty()) {
294 SGGeod geodPt = SGGeod::fromCart(lineSegmentVisitor.getPoint());
295 _altitude = geodPt.getElevationM();
296 _material = lineSegmentVisitor.getMaterial();
297 _type = materialToGroundType(_material);
300 // Else do a crude scene query for the current point
301 found_ground = globals->get_scenery()->
302 get_cart_elevation_m(pt, rad, _altitude, &_material);
303 _type = materialToGroundType(_material);
306 // Still not sucessful??
308 SG_LOG(SG_FLIGHT, SG_WARN, "prepare_ground_cache(): trying to build "
309 "cache without any scenery below the aircraft");
315 FGGroundCache::is_valid(double& ref_time, SGVec3d& pt, double& rad)
317 pt = reference_wgs84_point;
318 rad = reference_vehicle_radius;
319 ref_time = cache_ref_time;
323 class FGGroundCache::CatapultFinder : public BVHVisitor {
325 CatapultFinder(const SGSphered& sphere, const double& t) :
328 _haveLineSegment(false)
331 virtual void apply(BVHGroup& leaf)
333 if (!intersects(_sphere, leaf.getBoundingSphere()))
335 leaf.traverse(*this);
337 virtual void apply(BVHTransform& transform)
339 if (!intersects(_sphere, transform.getBoundingSphere()))
342 SGSphered sphere = _sphere;
343 _sphere = transform.sphereToLocal(sphere);
344 bool haveLineSegment = _haveLineSegment;
345 _haveLineSegment = false;
347 transform.traverse(*this);
349 if (_haveLineSegment) {
350 _lineSegment = transform.lineSegmentToWorld(_lineSegment);
351 _linearVelocity = transform.vecToWorld(_linearVelocity);
352 _angularVelocity = transform.vecToWorld(_angularVelocity);
354 _haveLineSegment |= haveLineSegment;
355 _sphere.setCenter(sphere.getCenter());
357 virtual void apply(BVHMotionTransform& transform)
359 if (!intersects(_sphere, transform.getBoundingSphere()))
362 SGSphered sphere = _sphere;
363 _sphere = transform.sphereToLocal(sphere, _time);
364 bool haveLineSegment = _haveLineSegment;
365 _haveLineSegment = false;
367 transform.traverse(*this);
369 if (_haveLineSegment) {
370 SGMatrixd toWorld = transform.getToWorldTransform(_time);
372 += transform.getLinearVelocityAt(_lineSegment.getStart());
373 _angularVelocity += transform.getAngularVelocity();
374 _linearVelocity = toWorld.xformVec(_linearVelocity);
375 _angularVelocity = toWorld.xformVec(_angularVelocity);
376 _lineSegment = _lineSegment.transform(toWorld);
378 _haveLineSegment |= haveLineSegment;
379 _sphere.setCenter(sphere.getCenter());
381 virtual void apply(BVHLineGeometry& node)
383 if (node.getType() != BVHLineGeometry::CarrierCatapult)
386 SGLineSegmentd lineSegment(node.getLineSegment());
387 if (!intersects(_sphere, lineSegment))
390 _lineSegment = lineSegment;
391 double dist = distSqr(lineSegment, getSphere().getCenter());
392 _sphere.setRadius(sqrt(dist));
393 _linearVelocity = SGVec3d::zeros();
394 _angularVelocity = SGVec3d::zeros();
395 _haveLineSegment = true;
397 virtual void apply(BVHStaticGeometry& node)
400 virtual void apply(const BVHStaticBinary&, const BVHStaticData&) { }
401 virtual void apply(const BVHStaticTriangle&, const BVHStaticData&) { }
403 void setSphere(const SGSphered& sphere)
404 { _sphere = sphere; }
405 const SGSphered& getSphere() const
408 const SGLineSegmentd& getLineSegment() const
409 { return _lineSegment; }
410 const SGVec3d& getLinearVelocity() const
411 { return _linearVelocity; }
412 const SGVec3d& getAngularVelocity() const
413 { return _angularVelocity; }
415 bool getHaveLineSegment() const
416 { return _haveLineSegment; }
419 SGLineSegmentd _lineSegment;
420 SGVec3d _linearVelocity;
421 SGVec3d _angularVelocity;
423 bool _haveLineSegment;
430 FGGroundCache::get_cat(double t, const SGVec3d& pt,
431 SGVec3d end[2], SGVec3d vel[2])
433 double maxDistance = 1000;
435 // Get the wire in question
436 CatapultFinder catapultFinder(SGSphered(pt, maxDistance), t);
438 _localBvhTree->accept(catapultFinder);
440 if (!catapultFinder.getHaveLineSegment())
443 // prepare the returns
444 end[0] = catapultFinder.getLineSegment().getStart();
445 end[1] = catapultFinder.getLineSegment().getEnd();
447 // The linear velocity is the one at the start of the line segment ...
448 vel[0] = catapultFinder.getLinearVelocity();
449 // ... so the end point has the additional cross product.
450 vel[1] = catapultFinder.getLinearVelocity();
451 vel[1] += cross(catapultFinder.getAngularVelocity(),
452 catapultFinder.getLineSegment().getDirection());
454 // Return the distance to the cat
455 return sqrt(distSqr(catapultFinder.getLineSegment(), pt));
459 FGGroundCache::get_agl(double t, const SGVec3d& pt, double max_altoff,
460 SGVec3d& contact, SGVec3d& normal, SGVec3d& vel,
461 int *type, const SGMaterial** material, double *agl)
463 // Just set up a ground intersection query for the given point
464 SGLineSegmentd line(pt - max_altoff*down, pt + 1e4*down);
465 simgear::BVHLineSegmentVisitor lineSegmentVisitor(line, t);
467 _localBvhTree->accept(lineSegmentVisitor);
469 if (!lineSegmentVisitor.empty()) {
470 // Have an intersection
471 contact = lineSegmentVisitor.getPoint();
472 normal = lineSegmentVisitor.getNormal();
473 if (0 < dot(normal, down))
475 *agl = dot(down, contact - pt);
476 vel = lineSegmentVisitor.getLinearVelocity();
477 // correct the linear velocity, since the line intersector delivers
478 // values for the start point and the get_agl function should
479 // traditionally deliver for the contact point
480 vel += cross(lineSegmentVisitor.getAngularVelocity(),
481 contact - line.getStart());
482 *type = materialToGroundType(lineSegmentVisitor.getMaterial());
484 *material = lineSegmentVisitor.getMaterial();
488 // Whenever we did not have a ground triangle for the requested point,
489 // take the ground level we found during the current cache build.
490 // This is as good as what we had before for agl.
491 SGGeod geodPt = SGGeod::fromCart(pt);
492 *agl = geodPt.getElevationM() - _altitude;
493 geodPt.setElevationM(_altitude);
494 contact = SGVec3d::fromGeod(geodPt);
496 vel = SGVec3d(0, 0, 0);
499 *material = _material;
505 class FGGroundCache::WireIntersector : public BVHVisitor {
507 WireIntersector(const SGVec3d pt[4], const double& t) :
508 _linearVelocity(SGVec3d::zeros()),
509 _angularVelocity(SGVec3d::zeros()),
513 // Build the two triangles spanning the area where the hook has moved
514 // during the past step.
515 _triangles[0].set(pt[0], pt[1], pt[2]);
516 _triangles[1].set(pt[0], pt[2], pt[3]);
519 virtual void apply(BVHGroup& leaf)
521 if (!_intersects(leaf.getBoundingSphere()))
524 leaf.traverse(*this);
526 virtual void apply(BVHTransform& transform)
528 if (!_intersects(transform.getBoundingSphere()))
531 SGTriangled triangles[2] = { _triangles[0], _triangles[1] };
532 _triangles[0] = triangles[0].transform(transform.getToLocalTransform());
533 _triangles[1] = triangles[1].transform(transform.getToLocalTransform());
535 transform.traverse(*this);
538 _lineSegment = transform.lineSegmentToWorld(_lineSegment);
539 _linearVelocity = transform.vecToWorld(_linearVelocity);
540 _angularVelocity = transform.vecToWorld(_angularVelocity);
542 _triangles[0] = triangles[0];
543 _triangles[1] = triangles[1];
545 virtual void apply(BVHMotionTransform& transform)
547 if (!_intersects(transform.getBoundingSphere()))
550 SGMatrixd toLocal = transform.getToLocalTransform(_time);
552 SGTriangled triangles[2] = { _triangles[0], _triangles[1] };
553 _triangles[0] = triangles[0].transform(toLocal);
554 _triangles[1] = triangles[1].transform(toLocal);
556 transform.traverse(*this);
559 SGMatrixd toWorld = transform.getToWorldTransform(_time);
561 += transform.getLinearVelocityAt(_lineSegment.getStart());
562 _angularVelocity += transform.getAngularVelocity();
563 _linearVelocity = toWorld.xformVec(_linearVelocity);
564 _angularVelocity = toWorld.xformVec(_angularVelocity);
565 _lineSegment = _lineSegment.transform(toWorld);
567 _triangles[0] = triangles[0];
568 _triangles[1] = triangles[1];
570 virtual void apply(BVHLineGeometry& node)
572 if (node.getType() != BVHLineGeometry::CarrierWire)
574 SGLineSegmentd lineSegment(node.getLineSegment());
575 if (!_intersects(lineSegment))
578 _lineSegment = lineSegment;
579 _linearVelocity = SGVec3d::zeros();
580 _angularVelocity = SGVec3d::zeros();
583 virtual void apply(BVHStaticGeometry& node)
586 virtual void apply(const BVHStaticBinary&, const BVHStaticData&) { }
587 virtual void apply(const BVHStaticTriangle&, const BVHStaticData&) { }
589 bool _intersects(const SGSphered& sphere) const
593 if (intersects(_triangles[0], sphere))
595 if (intersects(_triangles[1], sphere))
599 bool _intersects(const SGLineSegmentd& lineSegment) const
603 if (intersects(_triangles[0], lineSegment))
605 if (intersects(_triangles[1], lineSegment))
610 const SGLineSegmentd& getLineSegment() const
611 { return _lineSegment; }
612 const SGVec3d& getLinearVelocity() const
613 { return _linearVelocity; }
614 const SGVec3d& getAngularVelocity() const
615 { return _angularVelocity; }
617 const BVHLineGeometry* getWire() const
621 SGLineSegmentd _lineSegment;
622 SGVec3d _linearVelocity;
623 SGVec3d _angularVelocity;
624 const BVHLineGeometry* _wire;
626 SGTriangled _triangles[2];
630 bool FGGroundCache::caught_wire(double t, const SGVec3d pt[4])
632 // Get the wire in question
633 WireIntersector wireIntersector(pt, t);
635 _localBvhTree->accept(wireIntersector);
637 _wire = wireIntersector.getWire();
641 class FGGroundCache::WireFinder : public BVHVisitor {
643 WireFinder(const BVHLineGeometry* wire, const double& t) :
646 _lineSegment(SGVec3d::zeros(), SGVec3d::zeros()),
647 _linearVelocity(SGVec3d::zeros()),
648 _angularVelocity(SGVec3d::zeros()),
649 _haveLineSegment(false)
652 virtual void apply(BVHGroup& leaf)
654 if (_haveLineSegment)
656 leaf.traverse(*this);
658 virtual void apply(BVHTransform& transform)
660 if (_haveLineSegment)
663 transform.traverse(*this);
665 if (_haveLineSegment) {
666 _linearVelocity = transform.vecToWorld(_linearVelocity);
667 _angularVelocity = transform.vecToWorld(_angularVelocity);
668 _lineSegment = transform.lineSegmentToWorld(_lineSegment);
671 virtual void apply(BVHMotionTransform& transform)
673 if (_haveLineSegment)
676 transform.traverse(*this);
678 if (_haveLineSegment) {
679 SGMatrixd toWorld = transform.getToWorldTransform(_time);
681 += transform.getLinearVelocityAt(_lineSegment.getStart());
682 _angularVelocity += transform.getAngularVelocity();
683 _linearVelocity = toWorld.xformVec(_linearVelocity);
684 _angularVelocity = toWorld.xformVec(_angularVelocity);
685 _lineSegment = _lineSegment.transform(toWorld);
688 virtual void apply(BVHLineGeometry& node)
690 if (_haveLineSegment)
694 if (node.getType() != BVHLineGeometry::CarrierWire)
696 _lineSegment = SGLineSegmentd(node.getLineSegment());
697 _linearVelocity = SGVec3d::zeros();
698 _angularVelocity = SGVec3d::zeros();
699 _haveLineSegment = true;
701 virtual void apply(BVHStaticGeometry&) { }
703 virtual void apply(const BVHStaticBinary&, const BVHStaticData&) { }
704 virtual void apply(const BVHStaticTriangle&, const BVHStaticData&) { }
706 const SGLineSegmentd& getLineSegment() const
707 { return _lineSegment; }
709 bool getHaveLineSegment() const
710 { return _haveLineSegment; }
712 const SGVec3d& getLinearVelocity() const
713 { return _linearVelocity; }
714 const SGVec3d& getAngularVelocity() const
715 { return _angularVelocity; }
718 const BVHLineGeometry* _wire;
721 SGLineSegmentd _lineSegment;
722 SGVec3d _linearVelocity;
723 SGVec3d _angularVelocity;
725 bool _haveLineSegment;
728 bool FGGroundCache::get_wire_ends(double t, SGVec3d end[2], SGVec3d vel[2])
730 // Fast return if we do not have an active wire.
734 // Get the wire in question
735 WireFinder wireFinder(_wire, t);
737 _localBvhTree->accept(wireFinder);
739 if (!wireFinder.getHaveLineSegment())
742 // prepare the returns
743 end[0] = wireFinder.getLineSegment().getStart();
744 end[1] = wireFinder.getLineSegment().getEnd();
746 // The linear velocity is the one at the start of the line segment ...
747 vel[0] = wireFinder.getLinearVelocity();
748 // ... so the end point has the additional cross product.
749 vel[1] = wireFinder.getLinearVelocity();
750 vel[1] += cross(wireFinder.getAngularVelocity(),
751 wireFinder.getLineSegment().getDirection());
756 void FGGroundCache::release_wire(void)