-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathONB.cpp
More file actions
138 lines (119 loc) · 2.59 KB
/
ONB.cpp
File metadata and controls
138 lines (119 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include"ONB.h"
#include"mathUtil.h"
#define ONB_EPSILON 0.01f
ONB::ONB():_U(1.0f, .0f, .0f), _V(.0f, 1.0f, .0f), _W(.0f, .0f, 1.0f)
{}
ONB::ONB(const Vector3& v1, const Vector3& v2, const Vector3& v3)
: _U(v1), _V(v2), _W(v3)
{}
void ONB::initFromU(const Vector3& u)
{
Vector3 n(1.0f, .0f, .0f);
Vector3 m(.0f, 1.0f, .0f);
_U = unitVector(u);
_V = cross(_U, n);
if (_V.length() < ONB_EPSILON)
{
_V = cross(_U, m);
}
_W = cross(_U, _V);
}
void ONB::initFromV(const Vector3& v)
{
Vector3 n(1.0f, .0f, .0f);
Vector3 m(.0f, 1.0f, .0f);
_V = unitVector(v);
_U = cross(_V, n);
if (_U.length() < ONB_EPSILON)
{
_U = cross(_V, m);
}
_W = cross(_U, _V);
}
void ONB::initFromW(const Vector3& w)
{
Vector3 n(1.0f, .0f, .0f);
Vector3 m(.0f, 1.0f, .0f);
_W = unitVector(w);
_U = cross(_W, n);
if (_U.length() < ONB_EPSILON)
{
_U = cross(_W, m);
}
_V = cross(_W, _U);
}
void ONB::set(const Vector3& v1, const Vector3& v2, const Vector3& v3)
{
_U = v1;
_V = v2;
_W = v3;
}
void ONB::initFromUV(const Vector3& u, const Vector3& v)
{
_U = unitVector(u);
_W = unitVector(cross(u, v));
_V = cross(_W, _U);
}
void ONB::initFromVU(const Vector3& v, const Vector3& u)
{
_V = unitVector(v);
_W = unitVector(cross(u, v));
_U = cross(_V, _W);
}
void ONB::initFromUW(const Vector3& u, const Vector3& w)
{
_U = unitVector(u);
_V = unitVector(cross(_U, _V));
_U = cross(_V, _W);
}
void ONB::initFromWU(const Vector3& w, const Vector3& u)
{
_W = unitVector(w);
_V = unitVector(cross(w, u));
_U = cross(_V, _W);
}
void ONB::initFromVW(const Vector3& v, const Vector3& w)
{
_V = unitVector(v);
_U = unitVector(cross(v, w));
_W = cross(_U, _V);
}
void ONB::initFromWV(const Vector3& w, const Vector3& v)
{
_W = unitVector(w);
_U = unitVector(cross(v, w));
_V = cross(_W, _U);
}
std::istream& operator>>(std::istream& is, ONB& t)
{
Vector3 u, v, w;
is >> u >> v >> w;
t.set(u, v, w);
return is;
}
std::ostream& operator<<(std::ostream& os, const ONB& t)
{
os << t.u() << "\n" << t.v() << "\n" << t.w() << "\n";
return os;
}
bool operator==(const ONB& o1, const ONB& o2)
{
Vector3 u = o1.u() - o2.u();
Vector3 v = o1.v() - o2.v();
Vector3 w = o1.w() - o2.w();
return (isZero(u.length(), ONB_EPSILON)
&& isZero(v.length(), ONB_EPSILON)
&& isZero(w.length(), ONB_EPSILON));
}
Vector3 ONB::u() const
{
return _U;
}
Vector3 ONB::v() const
{
return _V;
}
Vector3 ONB::w() const
{
return _W;
}