Isis 3 Programmer Reference
BulletClosestRayCallback.cpp
1 
6 /* SPDX-License-Identifier: CC0-1.0 */
7 
8 #include <cmath>
9 
10 #include <QtGlobal>
11 #include <QScopedPointer>
12 #include <QString>
13 #include <QVector>
14 
15 #include "BulletClosestRayCallback.h"
16 #include "BulletTargetShape.h"
17 #include "Constants.h"
18 #include "IException.h"
19 #include "IsisBullet.h"
20 #include "SurfacePoint.h"
21 
22 #include "BulletCollision/NarrowPhaseCollision/btRaycastCallback.h"
23 
24 namespace Isis {
25 
26 
32  btCollisionWorld::ClosestRayResultCallback(btVector3(0,0,0), btVector3(0,0,0)),
33  m_point(0.0, 0.0, 0.0), m_normal(0.0, 0.0, 0.0),
34  m_triangleIndex(-1), m_partId(-1) {
35 
36  // Set default ray tracing flags
37  m_flags = defaultFlags();
38  }
39 
40 
51  const btVector3 &point,
52  const btVector3 &normal) :
53  btCollisionWorld::ClosestRayResultCallback(result.observer(), result.lookdir()) {
54  *this = result;
55  m_point = point;
56  m_normal = normal;
57  return;
58  }
59 
60 
68  const btVector3 &lookdir) :
69  btCollisionWorld::ClosestRayResultCallback(observer, lookdir),
70  m_point(0.0, 0.0, 0.0), m_normal(0.0, 0.0, 0.0),
71  m_triangleIndex(-1), m_partId(-1) {
72 
73  // Set default ray tracing flags
74  m_flags = defaultFlags();
75  }
76 
77 
95  const btVector3 &lookdir,
96  const btCollisionWorld::RayResultCallback &source,
97  btCollisionWorld::LocalRayResult &rayResult,
98  bool normalInWorldSpace) :
99  btCollisionWorld::ClosestRayResultCallback(observer, lookdir),
100  m_point(0.0, 0.0, 0.0), m_normal(0.0, 0.0, 0.0),
101  m_triangleIndex(-1), m_partId(-1) {
102  copyRayResult(*this, source);
103  addSingleResult(rayResult, normalInWorldSpace);
104  }
105 
106 
111 
112 
121  return ( hasHit() );
122  }
123 
124 
131  return ( ClosestRayResultCallback::m_rayFromWorld );
132  }
133 
134 
141  return ( ClosestRayResultCallback::m_rayToWorld );
142  }
143 
144 
158  return ( RayResultCallback::m_closestHitFraction );
159  }
160 
161 
168  if ( hasHit() ) {
169  return ( m_point );
170  }
172  "No hits in ray trace so no surface point!",
173  _FILEINFO_);
174  return ( btVector3(0.0, 0.0, 0.0) );
175  }
176 
177 
184  if ( hasHit() ) {
185  return ( m_normal );
186  }
187 
189  "No hits in ray trace so no normal!",
190  _FILEINFO_);
191  return btVector3(0.0, 0.0, 0.0);
192  }
193 
194 
201  return ( m_triangleIndex );
202  }
203 
204 
211  return ( m_partId );
212  }
213 
214 
222  if ( !isValid() ) return (DBL_MAX);
223  return ( observer().distance(point()) );
224  }
225 
226 
239  if ( !(isValid() && other.isValid()) ) return (DBL_MAX);
240  return ( point().distance( other.point() ));
241  }
242 
243 
252  btScalar BulletClosestRayCallback::distance(const btVector3 &other) const {
253  if ( !isValid() ) return (DBL_MAX);
254  return ( point().distance(other) );
255  }
256 
257 
267  if ( !(isValid() && other.isValid()) ) return (false);
268  if ( triangleIndex() != other.triangleIndex() ) return (false);
269  if ( partId() != other.partId() ) return (false);
270  return ( true );
271  }
272 
273 
274  //TODO This needs a better name
292  const btScalar tolerance) const {
293  if ( !(isValid() && other.isValid()) ) return (false);
294  if ( triangleIndex() != other.triangleIndex() ) return (false);
295  if ( partId() != other.partId() ) return (false);
296 
297  // How close are the two intercept points?
298  if ( distance( other ) > tolerance ) return ( false );
299 
300  return (true); // Its visable!
301  }
302 
314  if ( !isValid() ) return (0);
315  return ( static_cast<const BulletTargetShape *> (m_collisionObject->getUserPointer()) );
316  }
317 
318 
333  btScalar BulletClosestRayCallback::addSingleResult(btCollisionWorld::LocalRayResult &rayResult,
334  bool normalInWorldSpace) {
335  btScalar hitFraction = ClosestRayResultCallback::addSingleResult(rayResult, normalInWorldSpace);
336 
337  m_point = m_hitPointWorld;
338  m_normal = m_hitNormalWorld;
339 
340  // Triangle information
341  m_triangleIndex = rayResult.m_localShapeInfo->m_triangleIndex;
342  m_partId = rayResult.m_localShapeInfo->m_shapePart;
343  return (hitFraction);
344  }
345 
346 
354  return ( (btTriangleRaycastCallback::kF_FilterBackfaces |
355  btTriangleRaycastCallback::kF_KeepUnflippedNormal |
356  btTriangleRaycastCallback::kF_UseGjkConvexCastRaytest) );
357  }
358 
365  void BulletClosestRayCallback::copyRayResult(btCollisionWorld::RayResultCallback &dest,
366  const btCollisionWorld::RayResultCallback &source) {
367  dest = source;
368  return;
369  }
370 
371 } // namespace Isis
372 
373 
374 
Isis::BulletClosestRayCallback::partId
int partId() const
Return the Bullet ID of the intersected collision object.
Definition: BulletClosestRayCallback.cpp:210
Isis::BulletClosestRayCallback::normal
btVector3 normal() const
Return the local surface normal at the intersection, if an intersection exists.
Definition: BulletClosestRayCallback.cpp:183
Isis::BulletClosestRayCallback::m_normal
btVector3 m_normal
! The intersection point in body fixed (x, y, z) kilometers.
Definition: BulletClosestRayCallback.h:68
Isis::BulletClosestRayCallback::m_triangleIndex
int m_triangleIndex
! The local surface normal at the intersection point in body fixed (x, y, z).
Definition: BulletClosestRayCallback.h:70
Isis::BulletClosestRayCallback::point
btVector3 point() const
Return the intersection point, if one exists.
Definition: BulletClosestRayCallback.cpp:167
Isis::BulletClosestRayCallback::copyRayResult
void copyRayResult(btCollisionWorld::RayResultCallback &dest, const btCollisionWorld::RayResultCallback &source)
Easy way to copy one callback into another.
Definition: BulletClosestRayCallback.cpp:365
Isis::BulletClosestRayCallback::fraction
btScalar fraction() const
Return the intersection hit fraction or fractional distance along the ray of the intersection.
Definition: BulletClosestRayCallback.cpp:157
Isis::BulletClosestRayCallback::BulletClosestRayCallback
BulletClosestRayCallback()
Default constructor.
Definition: BulletClosestRayCallback.cpp:31
Isis::BulletClosestRayCallback::defaultFlags
unsigned int defaultFlags() const
Return the default ray cast flags.
Definition: BulletClosestRayCallback.cpp:353
Isis::BulletClosestRayCallback::addSingleResult
virtual btScalar addSingleResult(btCollisionWorld::LocalRayResult &rayResult, bool normalInWorldSpace)
! The Bullet ID of the intersected collision object.
Definition: BulletClosestRayCallback.cpp:333
Isis::BulletClosestRayCallback::body
const BulletTargetShape * body() const
@breif Return pointer to target shape
Definition: BulletClosestRayCallback.cpp:313
Isis::BulletClosestRayCallback::~BulletClosestRayCallback
virtual ~BulletClosestRayCallback()
Destory a callback.
Definition: BulletClosestRayCallback.cpp:110
Isis::IException
Isis exception class.
Definition: IException.h:91
Isis::BulletClosestRayCallback::isValid
bool isValid() const
Checks if the callback is valid/has a valid intersection.
Definition: BulletClosestRayCallback.cpp:120
Isis::BulletClosestRayCallback::triangleIndex
int triangleIndex() const
Return the 0-based index of the intersected triangle.
Definition: BulletClosestRayCallback.cpp:200
Isis::IException::Programmer
@ Programmer
This error is for when a programmer made an API call that was illegal.
Definition: IException.h:146
Isis::BulletTargetShape
Bullet Target Shape for planetary bodies.
Definition: BulletTargetShape.h:33
Isis::BulletClosestRayCallback::distance
btScalar distance() const
Returns the distance from the intersection point to the beginning of the ray.
Definition: BulletClosestRayCallback.cpp:221
Isis::BulletClosestRayCallback::isVisible
bool isVisible(const BulletClosestRayCallback &other, const btScalar tolerance=DBL_MAX) const
Check if the intersection in this is visible based on another callback.
Definition: BulletClosestRayCallback.cpp:291
Isis::BulletClosestRayCallback::operator==
bool operator==(const BulletClosestRayCallback &other) const
Equality operator to check if this callback is equivalent to another callback.
Definition: BulletClosestRayCallback.cpp:266
Isis::BulletClosestRayCallback
Bullet ray tracing callback for closest hit on target surface.
Definition: BulletClosestRayCallback.h:33
Isis::BulletClosestRayCallback::observer
btVector3 observer() const
Return the beginning of the ray.
Definition: BulletClosestRayCallback.cpp:130
Isis::BulletClosestRayCallback::lookdir
btVector3 lookdir() const
Return the end of the ray.
Definition: BulletClosestRayCallback.cpp:140
Isis
This is free and unencumbered software released into the public domain.
Definition: Apollo.h:16
Isis::BulletClosestRayCallback::m_partId
int m_partId
! The 0-based index of the intersected triangle.
Definition: BulletClosestRayCallback.h:71