| 1 |
// |
| 2 |
// SPDX-License-Identifier: BSD-3-Clause |
| 3 |
// Copyright Contributors to the OpenEXR Project. |
| 4 |
// |
| 5 |
|
| 6 |
// |
| 7 |
// A 3D sphere class template |
| 8 |
// |
| 9 |
|
| 10 |
#ifndef INCLUDED_IMATHSPHERE_H |
| 11 |
#define INCLUDED_IMATHSPHERE_H |
| 12 |
|
| 13 |
#include "ImathExport.h" |
| 14 |
#include "ImathNamespace.h" |
| 15 |
|
| 16 |
#include "ImathBox.h" |
| 17 |
#include "ImathLine.h" |
| 18 |
#include "ImathVec.h" |
| 19 |
|
| 20 |
IMATH_INTERNAL_NAMESPACE_HEADER_ENTER |
| 21 |
|
| 22 |
/// |
| 23 |
/// A 3D sphere |
| 24 |
/// |
| 25 |
|
| 26 |
template <class T> class IMATH_EXPORT_TEMPLATE_TYPE Sphere3 |
| 27 |
{ |
| 28 |
public: |
| 29 |
|
| 30 |
/// @{ |
| 31 |
/// @name Direct access to member fields |
| 32 |
|
| 33 |
/// Center |
| 34 |
Vec3<T> center; |
| 35 |
|
| 36 |
/// Radius |
| 37 |
T radius; |
| 38 |
|
| 39 |
/// @} |
| 40 |
|
| 41 |
/// @{ |
| 42 |
/// @name Constructors |
| 43 |
|
| 44 |
/// Default is center at (0,0,0) and radius of 0. |
| 45 |
IMATH_HOSTDEVICE constexpr Sphere3() : center (0, 0, 0), radius (0) {} |
| 46 |
|
| 47 |
/// Initialize to a given center and radius |
| 48 |
IMATH_HOSTDEVICE constexpr Sphere3 (const Vec3<T>& c, T r) : center (c), radius (r) {} |
| 49 |
|
| 50 |
/// @} |
| 51 |
|
| 52 |
/// @{ |
| 53 |
/// @name Manipulation |
| 54 |
|
| 55 |
/// Set the center and radius of the sphere so that it tightly |
| 56 |
/// encloses Box b. |
| 57 |
IMATH_HOSTDEVICE void circumscribe (const Box<Vec3<T>>& box); |
| 58 |
|
| 59 |
/// @} |
| 60 |
|
| 61 |
/// @{ |
| 62 |
/// @name Utility Methods |
| 63 |
|
| 64 |
/// If the sphere and line `l` intersect, then compute the |
| 65 |
/// smallest `t` with `t>=0` so that `l(t)` is a point on the sphere. |
| 66 |
/// |
| 67 |
/// @param[in] l The line |
| 68 |
/// @param[out] intersection The point of intersection |
| 69 |
/// @return True if the sphere and line intersect, false if they |
| 70 |
/// do not. |
| 71 |
IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool |
| 72 |
intersect (const Line3<T>& l, Vec3<T>& intersection) const; |
| 73 |
|
| 74 |
/// If the sphere and line `l` intersect, then compute the |
| 75 |
/// smallest `t` with `t>=0` so that `l(t)` is a point on the sphere. |
| 76 |
/// |
| 77 |
/// @param[in] l The line |
| 78 |
/// @param[out] t The parameter of the line at the intersection point |
| 79 |
/// @return True if the sphere and line intersect, false if they |
| 80 |
/// do not. |
| 81 |
IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool intersectT (const Line3<T>& l, T& t) const; |
| 82 |
|
| 83 |
/// @} |
| 84 |
}; |
| 85 |
|
| 86 |
/// Sphere of type float |
| 87 |
typedef Sphere3<float> Sphere3f; |
| 88 |
|
| 89 |
/// Sphere of type double |
| 90 |
typedef Sphere3<double> Sphere3d; |
| 91 |
|
| 92 |
//--------------- |
| 93 |
// Implementation |
| 94 |
//--------------- |
| 95 |
|
| 96 |
template <class T> |
| 97 |
IMATH_HOSTDEVICE inline void |
| 98 |
Sphere3<T>::circumscribe (const Box<Vec3<T>>& box) |
| 99 |
{ |
| 100 |
center = T (0.5) * (box.min + box.max); |
| 101 |
radius = (box.max - center).length(); |
| 102 |
} |
| 103 |
|
| 104 |
template <class T> |
| 105 |
IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool |
| 106 |
Sphere3<T>::intersectT (const Line3<T>& line, T& t) const |
| 107 |
{ |
| 108 |
bool doesIntersect = true; |
| 109 |
|
| 110 |
Vec3<T> v = line.pos - center; |
| 111 |
T B = T (2.0) * (line.dir ^ v); |
| 112 |
T C = (v ^ v) - (radius * radius); |
| 113 |
|
| 114 |
// compute discriminant |
| 115 |
// if negative, there is no intersection |
| 116 |
|
| 117 |
T discr = B * B - T (4.0) * C; |
| 118 |
|
| 119 |
if (discr < 0.0) |
| 120 |
{ |
| 121 |
// line and Sphere3 do not intersect |
| 122 |
|
| 123 |
doesIntersect = false; |
| 124 |
} |
| 125 |
else |
| 126 |
{ |
| 127 |
// t0: (-B - sqrt(B^2 - 4AC)) / 2A (A = 1) |
| 128 |
|
| 129 |
T sqroot = std::sqrt (discr); |
| 130 |
t = (-B - sqroot) * T (0.5); |
| 131 |
|
| 132 |
if (t < 0.0) |
| 133 |
{ |
| 134 |
// no intersection, try t1: (-B + sqrt(B^2 - 4AC)) / 2A (A = 1) |
| 135 |
|
| 136 |
t = (-B + sqroot) * T (0.5); |
| 137 |
} |
| 138 |
|
| 139 |
if (t < 0.0) |
| 140 |
doesIntersect = false; |
| 141 |
} |
| 142 |
|
| 143 |
return doesIntersect; |
| 144 |
} |
| 145 |
|
| 146 |
template <class T> |
| 147 |
IMATH_CONSTEXPR14 bool |
| 148 |
Sphere3<T>::intersect (const Line3<T>& line, Vec3<T>& intersection) const |
| 149 |
{ |
| 150 |
T t (0); |
| 151 |
|
| 152 |
if (intersectT (line, t)) |
| 153 |
{ |
| 154 |
intersection = line (t); |
| 155 |
return true; |
| 156 |
} |
| 157 |
else |
| 158 |
{ |
| 159 |
return false; |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
IMATH_INTERNAL_NAMESPACE_HEADER_EXIT |
| 164 |
|
| 165 |
#endif // INCLUDED_IMATHSPHERE_H |