HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ImathVecAlgo.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 // Algorithms applied to or in conjunction with points (Imath::Vec2
8 // and Imath::Vec3).
9 //
10 // The assumption made is that these functions are called much
11 // less often than the basic point functions or these functions
12 // require more support classes.
13 //
14 
15 #ifndef INCLUDED_IMATHVECALGO_H
16 #define INCLUDED_IMATHVECALGO_H
17 
18 #include "ImathNamespace.h"
19 #include "ImathVec.h"
20 
21 IMATH_INTERNAL_NAMESPACE_HEADER_ENTER
22 
23 /// @cond Doxygen_Suppress
24 //
25 // Note: doxygen doesn't understand these templates, so omit these
26 // functions from the docs.
27 //
28 
29 /// Find the projection of vector `t` onto vector `s` (`Vec2`, `Vec3`, `Vec4`)
30 ///
31 /// Only defined for floating-point types, e.g. `V2f`, `V3d`, etc.
32 template <
33  class Vec,
35 IMATH_CONSTEXPR14 inline Vec
36 project (const Vec& s, const Vec& t) IMATH_NOEXCEPT
37 {
38  Vec sNormalized = s.normalized ();
39  return sNormalized * (sNormalized ^ t);
40 }
41 
42 /// Find a vector that is perpendicular to `s` and
43 /// in the same plane as `s` and `t` (`Vec2`, `Vec3`, `Vec4`)
44 ///
45 /// Only defined for floating-point types, e.g. `V2f`, `V3d`, etc.
46 template <
47  class Vec,
49 constexpr inline Vec
50 orthogonal (const Vec& s, const Vec& t) IMATH_NOEXCEPT
51 {
52  return t - project (s, t);
53 }
54 
55 /// Find the direction of a ray `s` after reflection
56 /// off a plane with normal `t` (`Vec2`, `Vec3`, `Vec4`)
57 ///
58 /// Only defined for floating-point types, e.g. `V2f`, `V3d`, etc.
59 template <
60  class Vec,
62 constexpr inline Vec
63 reflect (const Vec& s, const Vec& t) IMATH_NOEXCEPT
64 {
65  return s - typename Vec::BaseType (2) * (s - project (t, s));
66 }
67 
68 /// @endcond
69 
70 /// Find the vertex of triangle `(v0, v1, v2)` that is closest to point `p`
71 /// (`Vec2`, `Vec3`, `Vec4`)
72 template <class Vec>
73 IMATH_CONSTEXPR14 Vec
74 closestVertex (const Vec& v0, const Vec& v1, const Vec& v2, const Vec& p)
76 {
77  Vec nearest = v0;
78  typename Vec::BaseType neardot = (v0 - p).length2 ();
79  typename Vec::BaseType tmp = (v1 - p).length2 ();
80 
81  if (tmp < neardot)
82  {
83  neardot = tmp;
84  nearest = v1;
85  }
86 
87  tmp = (v2 - p).length2 ();
88 
89  if (tmp < neardot)
90  {
91  neardot = tmp;
92  nearest = v2;
93  }
94 
95  return nearest;
96 }
97 
98 IMATH_INTERNAL_NAMESPACE_HEADER_EXIT
99 
100 #endif // INCLUDED_IMATHVECALGO_H
#define IMATH_NOEXCEPT
Definition: ImathConfig.h:78
GLsizei const GLfloat * value
Definition: glcorearb.h:824
GLdouble s
Definition: glad.h:3009
GLfloat GLfloat GLfloat v2
Definition: glcorearb.h:818
UT_Vector2T< T > project(const UT_Vector2T< T > &u, const UT_Vector2T< T > &v)
The orthogonal projection of a vector u onto a vector v.
Definition: UT_Vector2.h:703
#define IMATH_ENABLE_IF(...)
GLdouble t
Definition: glad.h:2397
GLfloat v0
Definition: glcorearb.h:816
GLfloat GLfloat v1
Definition: glcorearb.h:817
IMATH_INTERNAL_NAMESPACE_HEADER_ENTER IMATH_CONSTEXPR14 Vec closestVertex(const Vec &v0, const Vec &v1, const Vec &v2, const Vec &p) IMATH_NOEXCEPT
Definition: ImathVecAlgo.h:74