Isis 3 Programmer Reference
BulletClosestRayCallback.cpp
Go to the documentation of this file.
1 
24 #include <cmath>
25 
26 #include <QtGlobal>
27 #include <QScopedPointer>
28 #include <QString>
29 #include <QVector>
30 
32 #include "BulletTargetShape.h"
33 #include "Constants.h"
34 #include "IException.h"
35 #include "IsisBullet.h"
36 #include "SurfacePoint.h"
37 
38 #include "BulletCollision/NarrowPhaseCollision/btRaycastCallback.h"
39 
40 namespace Isis {
41 
42 
48  btCollisionWorld::ClosestRayResultCallback(btVector3(0,0,0), btVector3(0,0,0)),
49  m_point(0.0, 0.0, 0.0), m_normal(0.0, 0.0, 0.0),
50  m_triangleIndex(-1), m_partId(-1) {
51 
52  // Set default ray tracing flags
53  m_flags = defaultFlags();
54  }
55 
56 
67  const btVector3 &point,
68  const btVector3 &normal) :
69  btCollisionWorld::ClosestRayResultCallback(result.observer(), result.lookdir()) {
70  *this = result;
71  m_point = point;
72  m_normal = normal;
73  return;
74  }
75 
76 
84  const btVector3 &lookdir) :
85  btCollisionWorld::ClosestRayResultCallback(observer, lookdir),
86  m_point(0.0, 0.0, 0.0), m_normal(0.0, 0.0, 0.0),
87  m_triangleIndex(-1), m_partId(-1) {
88 
89  // Set default ray tracing flags
90  m_flags = defaultFlags();
91  }
92 
93 
111  const btVector3 &lookdir,
112  const btCollisionWorld::RayResultCallback &source,
113  btCollisionWorld::LocalRayResult &rayResult,
114  bool normalInWorldSpace) :
115  btCollisionWorld::ClosestRayResultCallback(observer, lookdir),
116  m_point(0.0, 0.0, 0.0), m_normal(0.0, 0.0, 0.0),
117  m_triangleIndex(-1), m_partId(-1) {
118  copyRayResult(*this, source);
119  addSingleResult(rayResult, normalInWorldSpace);
120  }
121 
122 
127 
128 
137  return ( hasHit() );
138  }
139 
140 
147  return ( ClosestRayResultCallback::m_rayFromWorld );
148  }
149 
150 
157  return ( ClosestRayResultCallback::m_rayToWorld );
158  }
159 
160 
174  return ( RayResultCallback::m_closestHitFraction );
175  }
176 
177 
184  if ( hasHit() ) {
185  return ( m_point );
186  }
188  "No hits in ray trace so no surface point!",
189  _FILEINFO_);
190  return ( btVector3(0.0, 0.0, 0.0) );
191  }
192 
193 
200  if ( hasHit() ) {
201  return ( m_normal );
202  }
203 
205  "No hits in ray trace so no normal!",
206  _FILEINFO_);
207  return btVector3(0.0, 0.0, 0.0);
208  }
209 
210 
217  return ( m_triangleIndex );
218  }
219 
220 
227  return ( m_partId );
228  }
229 
230 
238  if ( !isValid() ) return (DBL_MAX);
239  return ( observer().distance(point()) );
240  }
241 
242 
255  if ( !(isValid() && other.isValid()) ) return (DBL_MAX);
256  return ( point().distance( other.point() ));
257  }
258 
259 
268  btScalar BulletClosestRayCallback::distance(const btVector3 &other) const {
269  if ( !isValid() ) return (DBL_MAX);
270  return ( point().distance(other) );
271  }
272 
273 
283  if ( !(isValid() && other.isValid()) ) return (false);
284  if ( triangleIndex() != other.triangleIndex() ) return (false);
285  if ( partId() != other.partId() ) return (false);
286  return ( true );
287  }
288 
289 
290  //TODO This needs a better name
308  const btScalar tolerance) const {
309  if ( !(isValid() && other.isValid()) ) return (false);
310  if ( triangleIndex() != other.triangleIndex() ) return (false);
311  if ( partId() != other.partId() ) return (false);
312 
313  // How close are the two intercept points?
314  if ( distance( other ) > tolerance ) return ( false );
315 
316  return (true); // Its visable!
317  }
318 
330  if ( !isValid() ) return (0);
331  return ( static_cast<const BulletTargetShape *> (m_collisionObject->getUserPointer()) );
332  }
333 
334 
349  btScalar BulletClosestRayCallback::addSingleResult(btCollisionWorld::LocalRayResult &rayResult,
350  bool normalInWorldSpace) {
351  btScalar hitFraction = ClosestRayResultCallback::addSingleResult(rayResult, normalInWorldSpace);
352 
353  m_point = m_hitPointWorld;
354  m_normal = m_hitNormalWorld;
355 
356  // Triangle information
357  m_triangleIndex = rayResult.m_localShapeInfo->m_triangleIndex;
358  m_partId = rayResult.m_localShapeInfo->m_shapePart;
359  return (hitFraction);
360  }
361 
362 
370  return ( (btTriangleRaycastCallback::kF_FilterBackfaces |
371  btTriangleRaycastCallback::kF_KeepUnflippedNormal |
372  btTriangleRaycastCallback::kF_UseGjkConvexCastRaytest) );
373  }
374 
381  void BulletClosestRayCallback::copyRayResult(btCollisionWorld::RayResultCallback &dest,
382  const btCollisionWorld::RayResultCallback &source) {
383  dest = source;
384  return;
385  }
386 
387 } // namespace Isis
388 
389 
390 
btScalar distance() const
Returns the distance from the intersection point to the beginning of the ray.
virtual ~BulletClosestRayCallback()
Destory a callback.
unsigned int defaultFlags() const
Return the default ray cast flags.
const BulletTargetShape * body() const
Return pointer to target shape
int m_partId
! The 0-based index of the intersected triangle.
int m_triangleIndex
! The local surface normal at the intersection point in body fixed (x, y, z).
bool operator==(const BulletClosestRayCallback &other) const
Equality operator to check if this callback is equivalent to another callback.
This error is for when a programmer made an API call that was illegal.
Definition: IException.h:162
int partId() const
Return the Bullet ID of the intersected collision object.
btVector3 m_normal
! The intersection point in body fixed (x, y, z) kilometers.
btVector3 point() const
Return the intersection point, if one exists.
int triangleIndex() const
Return the 0-based index of the intersected triangle.
btVector3 normal() const
Return the local surface normal at the intersection, if an intersection exists.
#define _FILEINFO_
Macro for the filename and line number.
Definition: IException.h:40
Bullet ray tracing callback for closest hit on target surface.
void copyRayResult(btCollisionWorld::RayResultCallback &dest, const btCollisionWorld::RayResultCallback &source)
Easy way to copy one callback into another.
btVector3 observer() const
Return the beginning of the ray.
virtual btScalar addSingleResult(btCollisionWorld::LocalRayResult &rayResult, bool normalInWorldSpace)
! The Bullet ID of the intersected collision object.
btVector3 lookdir() const
Return the end of the ray.
Isis exception class.
Definition: IException.h:107
bool isVisible(const BulletClosestRayCallback &other, const btScalar tolerance=DBL_MAX) const
Check if the intersection in this is visible based on another callback.
Namespace for ISIS/Bullet specific routines.
Definition: Apollo.h:31
btScalar fraction() const
Return the intersection hit fraction or fractional distance along the ray of the intersection.
bool isValid() const
Checks if the callback is valid/has a valid intersection.
Bullet Target Shape for planetary bodies.
BulletClosestRayCallback()
Default constructor.