Debian Codegraph @

src/Imath/ImathVecAlgo.h

raw
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 <class Vec,
33 IMATH_ENABLE_IF(!std::is_integral<typename Vec::BaseType>::value)>
34 IMATH_CONSTEXPR14 inline Vec
35 project (const Vec& s, const Vec& t) IMATH_NOEXCEPT
36 {
37 Vec sNormalized = s.normalized();
38 return sNormalized * (sNormalized ^ t);
39 }
40
41 /// Find a vector that is perpendicular to `s` and
42 /// in the same plane as `s` and `t` (`Vec2`, `Vec3`, `Vec4`)
43 ///
44 /// Only defined for floating-point types, e.g. `V2f`, `V3d`, etc.
45 template <class Vec,
46 IMATH_ENABLE_IF(!std::is_integral<typename Vec::BaseType>::value)>
47 constexpr inline Vec
48 orthogonal (const Vec& s, const Vec& t) IMATH_NOEXCEPT
49 {
50 return t - project (s, t);
51 }
52
53 /// Find the direction of a ray `s` after reflection
54 /// off a plane with normal `t` (`Vec2`, `Vec3`, `Vec4`)
55 ///
56 /// Only defined for floating-point types, e.g. `V2f`, `V3d`, etc.
57 template <class Vec,
58 IMATH_ENABLE_IF(!std::is_integral<typename Vec::BaseType>::value)>
59 constexpr inline Vec
60 reflect (const Vec& s, const Vec& t) IMATH_NOEXCEPT
61 {
62 return s - typename Vec::BaseType (2) * (s - project (t, s));
63 }
64
65 /// @endcond
66
67 /// Find the vertex of triangle `(v0, v1, v2)` that is closest to point `p`
68 /// (`Vec2`, `Vec3`, `Vec4`)
69 template <class Vec>
70 IMATH_CONSTEXPR14 Vec
71 closestVertex (const Vec& v0, const Vec& v1, const Vec& v2, const Vec& p) IMATH_NOEXCEPT
72 {
73 Vec nearest = v0;
74 typename Vec::BaseType neardot = (v0 - p).length2();
75 typename Vec::BaseType tmp = (v1 - p).length2();
76
77 if (tmp < neardot)
78 {
79 neardot = tmp;
80 nearest = v1;
81 }
82
83 tmp = (v2 - p).length2();
84
85 if (tmp < neardot)
86 {
87 neardot = tmp;
88 nearest = v2;
89 }
90
91 return nearest;
92 }
93
94 IMATH_INTERNAL_NAMESPACE_HEADER_EXIT
95
96 #endif // INCLUDED_IMATHVECALGO_H