Loading...
Searching...
No Matches
vec_transform.h
Go to the documentation of this file.
1#ifndef _BBM_VEC_TRANSFORM_H_
2#define _BBM_VEC_TRANSFORM_H_
3
4#include "core/spherical.h"
5
6/************************************************************************/
7/*! \file vec_transform.h
8
9 \brief Vector transformation operations
10
11*************************************************************************/
12
13namespace bbm {
14
15 /********************************************************************/
16 /*! \brief Returns the lefthand (clockwise) perpendicular vector of a 2D
17 vector.
18
19 \param v Input vector
20 \returns (y, -x)
21 *********************************************************************/
22 template<typename T>
23 inline vec2d<T> perp(const vec2d<T>& v) { return vec2d<T>(vec::y(v), -vec::x(v)); }
24
25
26 /********************************************************************/
27 /*! \brief Returns the righthand (counterclockwise) perpendicular vector of a 2D
28 vector.
29
30 \param v Input vector
31 \returns (-y, x)
32 *********************************************************************/
33 template<typename T>
34 inline vec2d<T> cperp(const vec2d<T>& v) { return vec2d<T>(-vec::y(v), vec::x(v)); }
35
36
37 /********************************************************************/
38 /*! \brief Reflects a 3D vector.
39
40 \param v Input vector to reflect
41 \param normal Normal to reflect around
42 *********************************************************************/
43 template<typename T>
44 inline vec3d<T> reflect(const vec3d<T>& v, const vec3d<T>& normal) { return normal * dot(normal, v) * 2.0 - v; }
45
46 /********************************************************************/
47 /*! \brief Reflects a 3D vector around Z=1.
48
49 \param v Input vector to reflect
50 *********************************************************************/
51 template<typename T>
52 inline vec3d<T> reflect(const vec3d<T>& v) { return vec3d<T>(-vec::x(v), -vec::y(v), vec::z(v)); }
53
54
55 /********************************************************************/
56 /*! \brief Cross product of two 3D vectors.
57
58 \param a = first vector
59 \param b = second vector
60 ********************************************************************/
61 template<typename T>
62 inline constexpr vec3d<T> cross(const vec3d<T>& a, const vec3d<T>& b)
63 {
64 return vec3d<T>( a[1]*b[2] - a[2]*b[1],
65 a[2]*b[0] - a[0]*b[2],
66 a[0]*b[1] - a[1]*b[0] );
67 }
68
69
70 /********************************************************************/
71 /*! \brief Halfway vector (3D)
72
73 \param a = first vector
74 \param b = second vector
75 ********************************************************************/
76 template<typename T>
77 vec3d<T> halfway(const vec3d<T>& a, const vec3d<T>& b)
78 {
79 return bbm::normalize(a + b);
80 }
81
82
83 /********************************************************************/
84 /*! \brief Convert fro Canonical to the Halfway-Difference
85 parameterization
86
87 \param a = first vector
88 \param b = second vector
89 *********************************************************************/
90 template<typename T>
91 std::pair<vec3d<T>, vec3d<T>> convertToHalfwayDifference(const vec3d<T>& a, const vec3d<T>& b)
92 {
93 vec3d<T> half = halfway(a,b);
94 vec2d<T> half_sph = spherical::convert(half);
95 vec3d<T> diff = rotationY(-spherical::theta(half_sph)) * (rotationZ(-spherical::phi(half_sph)) * a);
96 return {half, diff};
97 }
98
99 /********************************************************************/
100 /*! \brief Difference vector
101
102 \param a = first vector
103 \param b = second vector
104 *********************************************************************/
105 template<typename T>
106 vec3d<T> difference(const vec3d<T>& a, const vec3d<T>& b)
107 {
108 return convertToHalfwayDifference(a,b).second;
109 }
110
111 /********************************************************************/
112 /*! \brief Convert from Halfway-Difference to Canonical parameterization
113
114 \param a = first vector
115 \param b = second vector
116 *********************************************************************/
117 template<typename T>
118 std::pair<vec3d<T>, vec3d<T>> convertFromHalfwayDifference(const vec3d<T>& half, const vec3d<T>& diff)
119 {
120 auto in = rotationZ(phi(half)) * (rotationY(theta(half)) * diff);
121 auto out = rotationZ(phi(half)) * (rotationY(theta(half)) * vec3d<T>(-vec::x(diff), -vec::y(diff), vec::z(diff)));
122 return {in, out};
123 }
124
125
126} // bbm namespace
127
128#endif /* _BBM_VEC_TRANSFORM_H_ */
T & phi(vec2d< T > &v)
Definition: spherical.h:39
vec2d< T > convert(const vec3d< T > &v)
Definition: spherical.h:53
T & theta(vec2d< T > &v)
Definition: spherical.h:23
constexpr decltype(auto) y(bbm::vec3d< T > &v)
Definition: vec.h:23
constexpr decltype(auto) z(bbm::vec3d< T > &v)
Definition: vec.h:26
constexpr decltype(auto) x(bbm::vec3d< T > &v)
Definition: vec.h:20
Definition: aggregatebsdf.h:29
mat3d< std::decay_t< T > > rotationY(const vec2d< T > &cossin)
Rotation around the Y-axis.
Definition: transform.h:47
vec2d< T > perp(const vec2d< T > &v)
Returns the lefthand (clockwise) perpendicular vector of a 2D vector.
Definition: vec_transform.h:23
mat3d< std::decay_t< T > > rotationZ(const vec2d< T > &cossin)
Rotation around the Z-axis.
Definition: transform.h:74
vec3d< T > halfway(const vec3d< T > &a, const vec3d< T > &b)
Halfway vector (3D)
Definition: vec_transform.h:77
vec3d< T > difference(const vec3d< T > &a, const vec3d< T > &b)
Difference vector.
Definition: vec_transform.h:106
std::pair< vec3d< T >, vec3d< T > > convertFromHalfwayDifference(const vec3d< T > &half, const vec3d< T > &diff)
Convert from Halfway-Difference to Canonical parameterization.
Definition: vec_transform.h:118
vec2d< T > cperp(const vec2d< T > &v)
Returns the righthand (counterclockwise) perpendicular vector of a 2D vector.
Definition: vec_transform.h:34
std::pair< vec3d< T >, vec3d< T > > convertToHalfwayDifference(const vec3d< T > &a, const vec3d< T > &b)
Convert fro Canonical to the Halfway-Difference parameterization.
Definition: vec_transform.h:91
constexpr vec3d< T > cross(const vec3d< T > &a, const vec3d< T > &b)
Cross product of two 3D vectors.
Definition: vec_transform.h:62
vec3d< T > reflect(const vec3d< T > &v, const vec3d< T > &normal)
Reflects a 3D vector.
Definition: vec_transform.h:44
Methods for handling spherical coordinates.