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
24namespace 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;
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
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.
btScalar fraction() const
Return the intersection hit fraction or fractional distance along the ray of the intersection.
btScalar distance() const
Returns the distance from the intersection point to the beginning of the ray.
btVector3 m_normal
! The intersection point in body fixed (x, y, z) kilometers.
btVector3 lookdir() const
Return the end of the ray.
int m_partId
! The 0-based index of the intersected triangle.
bool isValid() const
Checks if the callback is valid/has a valid intersection.
virtual ~BulletClosestRayCallback()
Destory a callback.
int triangleIndex() const
Return the 0-based index of the intersected triangle.
btVector3 point() const
Return the intersection point, if one exists.
bool operator==(const BulletClosestRayCallback &other) const
Equality operator to check if this callback is equivalent to another callback.
unsigned int defaultFlags() const
Return the default ray cast flags.
bool isVisible(const BulletClosestRayCallback &other, const btScalar tolerance=DBL_MAX) const
Check if the intersection in this is visible based on another callback.
btVector3 normal() const
Return the local surface normal at the intersection, if an intersection exists.
int partId() const
Return the Bullet ID of the intersected collision object.
virtual btScalar addSingleResult(btCollisionWorld::LocalRayResult &rayResult, bool normalInWorldSpace)
! The Bullet ID of the intersected collision object.
const BulletTargetShape * body() const
@breif Return pointer to target shape
int m_triangleIndex
! The local surface normal at the intersection point in body fixed (x, y, z).
btVector3 observer() const
Return the beginning of the ray.
Bullet Target Shape for planetary bodies.
Isis exception class.
Definition IException.h:91
@ Programmer
This error is for when a programmer made an API call that was illegal.
Definition IException.h:146
This is free and unencumbered software released into the public domain.
Definition Apollo.h:16