fixed Box::Extend(Vec3f&)
[MicroTrace.git] / Vec3f.cxx
1 #include <cassert>
2
3 #include "Vec3f.hxx"
4
5 Vec3f::Vec3f()
6 {
7 for(int i = 0; i < 3; ++i)
8 {
9 m_values[i] = 0.0f;
10 }
11 }
12
13 Vec3f::Vec3f(float x, float y, float z)
14 {
15 m_values[0] = x;
16 m_values[1] = y;
17 m_values[2] = z;
18 }
19
20 Vec3f::~Vec3f()
21 {
22 }
23
24 Vec3f::Vec3f(const Vec3f& o)
25 {
26 for(int i = 0; i < 3; ++i)
27 {
28 m_values[i] = o.m_values[i];
29 }
30 }
31
32 Vec3f&
33 Vec3f::operator=(const Vec3f& o)
34 {
35 if(this != &o)
36 {
37 for(int i = 0; i < 3; ++i)
38 {
39 m_values[i] = o.m_values[i];
40 }
41 }
42 return *this;
43 }
44
45 float
46 Vec3f::operator|(const Vec3f& o) const
47 {
48 return this->dot(o);
49 }
50
51 float
52 Vec3f::dot(const Vec3f& o) const
53 {
54 float dot = 0.0f;
55 for(int i = 0; i < 3; ++i)
56 {
57 dot += m_values[i]*o.m_values[i];
58 }
59 return dot;
60 }
61
62 Vec3f
63 Vec3f::operator%(const Vec3f& o) const
64 {
65 return this->cross(o);
66 }
67
68 Vec3f
69 Vec3f::cross(const Vec3f& o) const
70 {
71 float x = m_values[1]*o.m_values[2] - m_values[2]*o.m_values[1];
72 float y = m_values[2]*o.m_values[0] - m_values[0]*o.m_values[2];
73 float z = m_values[0]*o.m_values[1] - m_values[1]*o.m_values[0];
74
75 return Vec3f(x,y,z);
76 }
77
78 void
79 Vec3f::normalize()
80 {
81 float norm = this->norm();
82 *this *= 1.0f/norm;
83 }
84
85 float
86 Vec3f::norm() const
87 {
88 float n = this->dot(*this);
89 return sqrtf(n);
90 }
91
92 Vec3f
93 Vec3f::operator*(const float t) const
94 {
95 Vec3f v(*this);
96 return v *= t;
97 }
98
99 Vec3f&
100 Vec3f::operator*=(const float t)
101 {
102 for(int i = 0; i < 3; ++i)
103 {
104 m_values[i] *= t;
105 }
106 return *this;
107 }
108
109 Vec3f
110 Vec3f::operator/(const float t) const
111 {
112 Vec3f v(*this);
113 return v /= t;
114 }
115
116 Vec3f&
117 Vec3f::operator/=(const float t)
118 {
119 for(int i = 0; i < 3; ++i)
120 {
121 m_values[i] /= t;
122 }
123 return *this;
124 }
125
126 // example implementation of a standard operator
127 Vec3f
128 Vec3f::operator+(const Vec3f& o) const
129 {
130 Vec3f v(*this);
131
132 return v += o;
133 }
134
135 // example implementation of an in-place operator
136 Vec3f&
137 Vec3f::operator+=(const Vec3f& o)
138 {
139 for(unsigned int i = 0; i < 3; ++i)
140 {
141 m_values[i] += o.m_values[i];
142 }
143
144 return *this;
145 }
146
147 Vec3f
148 Vec3f::operator-(const Vec3f& o) const
149 {
150 Vec3f v(*this);
151
152 return v -= o;
153 }
154
155 Vec3f&
156 Vec3f::operator-=(const Vec3f& o)
157 {
158 for(unsigned int i = 0; i < 3; ++i)
159 {
160 m_values[i] -= o.m_values[i];
161 }
162 return *this;
163 }
164
165 Vec3f
166 Vec3f::operator*(const Vec3f& o) const
167 {
168 Vec3f v(*this);
169
170 return v *= o;
171 }
172
173 Vec3f&
174 Vec3f::operator*=(const Vec3f& o)
175 {
176 for(unsigned int i = 0; i < 3; ++i)
177 {
178 m_values[i] *= o.m_values[i];
179 }
180 return *this;
181 }
182
183 Vec3f
184 Vec3f::operator/(const Vec3f& o) const
185 {
186 Vec3f v(*this);
187
188 return v /= o;
189 }
190
191 Vec3f&
192 Vec3f::operator/=(const Vec3f& o)
193 {
194 for(unsigned int i = 0; i < 3; ++i)
195 {
196 m_values[i] /= o.m_values[i];
197 }
198 return *this;
199 }
200
201 float
202 Vec3f::operator[](unsigned int i) const
203 {
204 assert( i < 3 );
205 return m_values[i];
206 }
207
208 float&
209 Vec3f::operator[](unsigned int i)
210 {
211 assert(i < 3);
212 return m_values[i];
213 }
214
215 void
216 Vec3f::clamp()
217 {
218 for(unsigned int i = 0; i < 3; ++i)
219 {
220 if(m_values[i] < 0.0)
221 m_values[i] = 0.0;
222 if(m_values[i] > 1.0)
223 m_values[i] = 1.0;
224 }
225 }
This page took 0.080667 seconds and 5 git commands to generate.