00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include <simgear/math/SGMath.hxx>
00019 #include "BVHLineSegmentVisitor.hxx"
00020
00021 #include <simgear/math/SGGeometry.hxx>
00022
00023 #include "BVHVisitor.hxx"
00024
00025 #include "BVHNode.hxx"
00026 #include "BVHGroup.hxx"
00027 #include "BVHTransform.hxx"
00028 #include "BVHMotionTransform.hxx"
00029 #include "BVHLineGeometry.hxx"
00030 #include "BVHStaticGeometry.hxx"
00031
00032 #include "BVHStaticData.hxx"
00033
00034 #include "BVHStaticNode.hxx"
00035 #include "BVHStaticTriangle.hxx"
00036 #include "BVHStaticBinary.hxx"
00037
00038 namespace simgear {
00039
00040 void
00041 BVHLineSegmentVisitor::apply(BVHGroup& group)
00042 {
00043 if (!intersects(_lineSegment, group.getBoundingSphere()))
00044 return;
00045 group.traverse(*this);
00046 }
00047
00048 void
00049 BVHLineSegmentVisitor::apply(BVHTransform& transform)
00050 {
00051 if (!intersects(_lineSegment, transform.getBoundingSphere()))
00052 return;
00053
00054 bool haveHit = _haveHit;
00055 _haveHit = false;
00056
00057
00058 SGLineSegmentd lineSegment = getLineSegment();
00059 _lineSegment = transform.lineSegmentToLocal(lineSegment);
00060
00061 transform.traverse(*this);
00062
00063 if (_haveHit) {
00064 _linearVelocity = transform.vecToWorld(_linearVelocity);
00065 _angularVelocity = transform.vecToWorld(_angularVelocity);
00066 SGVec3d point(transform.ptToWorld(_lineSegment.getEnd()));
00067 _lineSegment.set(lineSegment.getStart(), point);
00068 _normal = transform.vecToWorld(_normal);
00069 } else {
00070 _lineSegment = lineSegment;
00071 _haveHit = haveHit;
00072 }
00073 }
00074
00075 void
00076 BVHLineSegmentVisitor::apply(BVHMotionTransform& transform)
00077 {
00078 if (!intersects(_lineSegment, transform.getBoundingSphere()))
00079 return;
00080
00081 bool haveHit = _haveHit;
00082 _haveHit = false;
00083
00084
00085 SGLineSegmentd lineSegment = getLineSegment();
00086 SGMatrixd toLocal = transform.getToLocalTransform(_time);
00087 _lineSegment = lineSegment.transform(toLocal);
00088
00089 transform.traverse(*this);
00090
00091 if (_haveHit) {
00092 SGMatrixd toWorld = transform.getToWorldTransform(_time);
00093 SGVec3d localStart = _lineSegment.getStart();
00094 _linearVelocity += transform.getLinearVelocityAt(localStart);
00095 _angularVelocity += transform.getAngularVelocity();
00096 _linearVelocity = toWorld.xformVec(_linearVelocity);
00097 _angularVelocity = toWorld.xformVec(_angularVelocity);
00098 SGVec3d localEnd = _lineSegment.getEnd();
00099 _lineSegment.set(lineSegment.getStart(), toWorld.xformPt(localEnd));
00100 _normal = toWorld.xformVec(_normal);
00101 if (!_id)
00102 _id = transform.getId();
00103 } else {
00104 _lineSegment = lineSegment;
00105 _haveHit = haveHit;
00106 }
00107 }
00108
00109 void
00110 BVHLineSegmentVisitor::apply(BVHLineGeometry&)
00111 {
00112 }
00113
00114 void
00115 BVHLineSegmentVisitor::apply(BVHStaticGeometry& node)
00116 {
00117 if (!intersects(_lineSegment, node.getBoundingSphere()))
00118 return;
00119 node.traverse(*this);
00120 }
00121
00122 void
00123 BVHLineSegmentVisitor::apply(const BVHStaticBinary& node,
00124 const BVHStaticData& data)
00125 {
00126 if (!intersects(SGLineSegmentf(_lineSegment), node.getBoundingBox()))
00127 return;
00128
00129
00130
00131
00132
00133 node.traverse(*this, data, _lineSegment.getStart());
00134 }
00135
00136 void
00137 BVHLineSegmentVisitor::apply(const BVHStaticTriangle& triangle,
00138 const BVHStaticData& data)
00139 {
00140 SGTrianglef tri = triangle.getTriangle(data);
00141 SGVec3f point;
00142 if (!intersects(point, tri, SGLineSegmentf(_lineSegment), 1e-4f))
00143 return;
00144 setLineSegmentEnd(SGVec3d(point));
00145 _normal = SGVec3d(tri.getNormal());
00146 _linearVelocity = SGVec3d::zeros();
00147 _angularVelocity = SGVec3d::zeros();
00148 _material = data.getMaterial(triangle.getMaterialIndex());
00149 _id = 0;
00150 _haveHit = true;
00151 }
00152
00153
00154 }