HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ImathSphere.h
Go to the documentation of this file.
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  /// @name Direct access to member fields
31 
32  /// Center
34 
35  /// Radius
37 
38  /// @}
39 
40  /// @{
41  /// @name Constructors
42 
43  /// Default is center at (0,0,0) and radius of 0.
44  IMATH_HOSTDEVICE constexpr Sphere3 () : center (0, 0, 0), radius (0) {}
45 
46  /// Initialize to a given center and radius
47  IMATH_HOSTDEVICE constexpr Sphere3 (const Vec3<T>& c, T r)
48  : center (c), radius (r)
49  {}
50 
51  /// @}
52 
53  /// @{
54  /// @name Manipulation
55 
56  /// Set the center and radius of the sphere so that it tightly
57  /// encloses Box b.
58  IMATH_HOSTDEVICE void circumscribe (const Box<Vec3<T>>& box);
59 
60  /// @}
61 
62  /// @{
63  /// @name Utility Methods
64 
65  /// If the sphere and line `l` intersect, then compute the
66  /// smallest `t` with `t>=0` so that `l(t)` is a point on the sphere.
67  ///
68  /// @param[in] l The line
69  /// @param[out] intersection The point of intersection
70  /// @return True if the sphere and line intersect, false if they
71  /// do not.
72  IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool
73  intersect (const Line3<T>& l, Vec3<T>& intersection) const;
74 
75  /// If the sphere and line `l` intersect, then compute the
76  /// smallest `t` with `t>=0` so that `l(t)` is a point on the sphere.
77  ///
78  /// @param[in] l The line
79  /// @param[out] t The parameter of the line at the intersection point
80  /// @return True if the sphere and line intersect, false if they
81  /// do not.
82  IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool
83  intersectT (const Line3<T>& l, T& t) const;
84 
85  /// @}
86 };
87 
88 /// Sphere of type float
90 
91 /// Sphere of type double
93 
94 //---------------
95 // Implementation
96 //---------------
97 
98 template <class T>
99 IMATH_HOSTDEVICE inline void
101 {
102  center = T (0.5) * (box.min + box.max);
103  radius = (box.max - center).length ();
104 }
105 
106 template <class T>
107 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool
108 Sphere3<T>::intersectT (const Line3<T>& line, T& t) const
109 {
110  bool doesIntersect = true;
111 
112  Vec3<T> v = line.pos - center;
113  T B = T (2.0) * (line.dir ^ v);
114  T C = (v ^ v) - (radius * radius);
115 
116  // compute discriminant
117  // if negative, there is no intersection
118 
119  T discr = B * B - T (4.0) * C;
120 
121  if (discr < 0.0)
122  {
123  // line and Sphere3 do not intersect
124 
125  doesIntersect = false;
126  }
127  else
128  {
129  // t0: (-B - sqrt(B^2 - 4AC)) / 2A (A = 1)
130 
131  T sqroot = std::sqrt (discr);
132  t = (-B - sqroot) * T (0.5);
133 
134  if (t < 0.0)
135  {
136  // no intersection, try t1: (-B + sqrt(B^2 - 4AC)) / 2A (A = 1)
137 
138  t = (-B + sqroot) * T (0.5);
139  }
140 
141  if (t < 0.0) doesIntersect = false;
142  }
143 
144  return doesIntersect;
145 }
146 
147 template <class T>
148 IMATH_CONSTEXPR14 bool
149 Sphere3<T>::intersect (const Line3<T>& line, Vec3<T>& intersection) const
150 {
151  T t (0);
152 
153  if (intersectT (line, t))
154  {
155  intersection = line (t);
156  return true;
157  }
158  else
159  {
160  return false;
161  }
162 }
163 
164 IMATH_INTERNAL_NAMESPACE_HEADER_EXIT
165 
166 #endif // INCLUDED_IMATHSPHERE_H
Vec3< T > pos
A point on the line.
Definition: ImathLine.h:31
IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool intersectT(const Line3< T > &l, T &t) const
Definition: ImathSphere.h:108
Definition: ImathVec.h:40
IMATH_HOSTDEVICE constexpr Sphere3(const Vec3< T > &c, T r)
Initialize to a given center and radius.
Definition: ImathSphere.h:47
const GLdouble * v
Definition: glcorearb.h:837
vfloat4 sqrt(const vfloat4 &a)
Definition: simd.h:7694
GLuint GLsizei GLsizei * length
Definition: glcorearb.h:795
#define IMATH_HOSTDEVICE
Definition: ImathConfig.h:108
IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool intersect(const Line3< T > &l, Vec3< T > &intersection) const
Definition: ImathSphere.h:149
Vec3< T > center
Center.
Definition: ImathSphere.h:33
T radius
Radius.
Definition: ImathSphere.h:36
IMATH_HOSTDEVICE constexpr Sphere3()
Default is center at (0,0,0) and radius of 0.
Definition: ImathSphere.h:44
Vec3< T > dir
The direction of the line.
Definition: ImathLine.h:34
GLdouble t
Definition: glad.h:2397
#define IMATH_EXPORT_TEMPLATE_TYPE
Definition: ImathExport.h:61
IMATH_CONSTEXPR14 bool intersect(const Line3< T > &line, const Vec3< T > &v0, const Vec3< T > &v1, const Vec3< T > &v2, Vec3< T > &pt, Vec3< T > &barycentric, bool &front) IMATH_NOEXCEPT
Definition: ImathLineAlgo.h:85
Definition: ImathBox.h:37
GLboolean r
Definition: glcorearb.h:1222
Sphere3< float > Sphere3f
Sphere of type float.
Definition: ImathSphere.h:89
IMATH_HOSTDEVICE void circumscribe(const Box< Vec3< T >> &box)
Definition: ImathSphere.h:100
Sphere3< double > Sphere3d
Sphere of type double.
Definition: ImathSphere.h:92