1 | /*
|
---|
2 | * vmg - a versatile multigrid solver
|
---|
3 | * Copyright (C) 2012 Institute for Numerical Simulation, University of Bonn
|
---|
4 | *
|
---|
5 | * vmg is free software: you can redistribute it and/or modify
|
---|
6 | * it under the terms of the GNU General Public License as published by
|
---|
7 | * the Free Software Foundation, either version 3 of the License, or
|
---|
8 | * (at your option) any later version.
|
---|
9 | *
|
---|
10 | * vmg is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | * GNU General Public License for more details.
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU General Public License
|
---|
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
17 | */
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * @file vector.hpp
|
---|
21 | * @author Julian Iseringhausen <isering@ins.uni-bonn.de>
|
---|
22 | * @date Mon Apr 18 12:26:13 2011
|
---|
23 | *
|
---|
24 | * @brief VMG::Vector
|
---|
25 | *
|
---|
26 | */
|
---|
27 |
|
---|
28 | #ifndef VECTOR_HPP_
|
---|
29 | #define VECTOR_HPP_
|
---|
30 |
|
---|
31 | #include <algorithm>
|
---|
32 | #include <cmath>
|
---|
33 | #include <cstring>
|
---|
34 | #include <iostream>
|
---|
35 |
|
---|
36 | namespace VMG
|
---|
37 | {
|
---|
38 |
|
---|
39 | class Index;
|
---|
40 |
|
---|
41 | class Vector
|
---|
42 | {
|
---|
43 | public:
|
---|
44 | Vector(const Index& index);
|
---|
45 | Vector(const vmg_float& x, const vmg_float& y, const vmg_float& z);
|
---|
46 | Vector(const vmg_float& val);
|
---|
47 | Vector(const vmg_float* arr);
|
---|
48 | Vector();
|
---|
49 |
|
---|
50 | Vector(const Vector& other)
|
---|
51 | {
|
---|
52 | std::memcpy(this->i, other.i, 3*sizeof(vmg_float));
|
---|
53 | }
|
---|
54 |
|
---|
55 | vmg_float& operator[](const int& index) {return i[index];}
|
---|
56 | const vmg_float& operator[](const int& index) const {return i[index];}
|
---|
57 |
|
---|
58 | vmg_float& X() {return i[0];}
|
---|
59 | vmg_float& Y() {return i[1];}
|
---|
60 | vmg_float& Z() {return i[2];}
|
---|
61 |
|
---|
62 | const vmg_float& X() const {return i[0];}
|
---|
63 | const vmg_float& Y() const {return i[1];}
|
---|
64 | const vmg_float& Z() const {return i[2];}
|
---|
65 |
|
---|
66 | Vector Abs() const {return Vector(fabs(i[0]), fabs(i[1]), fabs(i[2]));}
|
---|
67 | vmg_float Length() const {return sqrt(i[0]*i[0]+i[1]*i[1]+i[2]*i[2]);}
|
---|
68 | vmg_float Max() const {return std::max(std::max(i[0],i[1]),i[2]);}
|
---|
69 | vmg_float Min() const {return std::min(std::min(i[0],i[1]),i[2]);}
|
---|
70 | vmg_float Product() const {return i[0]*i[1]*i[2];}
|
---|
71 |
|
---|
72 | Vector& Floor()
|
---|
73 | {
|
---|
74 | i[0] = std::floor(i[0]);
|
---|
75 | i[1] = std::floor(i[1]);
|
---|
76 | i[2] = std::floor(i[2]);
|
---|
77 | return *this;
|
---|
78 | }
|
---|
79 |
|
---|
80 | bool IsComponentwiseLess(const Vector& other) const
|
---|
81 | {
|
---|
82 | return this->i[0] < other.i[0]
|
---|
83 | && this->i[1] < other.i[1]
|
---|
84 | && this->i[2] < other.i[2];
|
---|
85 | }
|
---|
86 |
|
---|
87 | bool IsComponentwiseLessOrEqual(const Vector& other) const
|
---|
88 | {
|
---|
89 | return this->i[0] <= other.i[0]
|
---|
90 | && this->i[1] <= other.i[1]
|
---|
91 | && this->i[2] <= other.i[2];
|
---|
92 | }
|
---|
93 |
|
---|
94 | bool IsComponentwiseGreater(const Vector& other) const
|
---|
95 | {
|
---|
96 | return this->i[0] > other.i[0]
|
---|
97 | && this->i[1] > other.i[1]
|
---|
98 | && this->i[2] > other.i[2];
|
---|
99 | }
|
---|
100 |
|
---|
101 | bool IsComponentwiseGreaterOrEqual(const Vector& other) const
|
---|
102 | {
|
---|
103 | return this->i[0] >= other.i[0]
|
---|
104 | && this->i[1] >= other.i[1]
|
---|
105 | && this->i[2] >= other.i[2];
|
---|
106 | }
|
---|
107 |
|
---|
108 | bool IsInBounds(const Vector& begin, const Vector& end) const
|
---|
109 | {
|
---|
110 | return this->IsComponentwiseGreaterOrEqual(begin) &&
|
---|
111 | this->IsComponentwiseLessOrEqual(end);
|
---|
112 | }
|
---|
113 |
|
---|
114 | Vector MaxComponentwise(const Vector& rhs) const
|
---|
115 | {
|
---|
116 | return Vector(std::max(i[0], rhs.i[0]), std::max(i[1], rhs.i[1]), std::max(i[2], rhs.i[2]));
|
---|
117 | }
|
---|
118 |
|
---|
119 | Vector MinComponentwise(const Vector& rhs) const
|
---|
120 | {
|
---|
121 | return Vector(std::min(i[0], rhs.i[0]), std::min(i[1], rhs.i[1]), std::min(i[2], rhs.i[2]));
|
---|
122 | }
|
---|
123 |
|
---|
124 | vmg_float* vec() {return i;}
|
---|
125 | const vmg_float* vec() const {return i;}
|
---|
126 |
|
---|
127 | Vector& operator=(const Vector& rhs)
|
---|
128 | {
|
---|
129 | i[0] = rhs.X();
|
---|
130 | i[1] = rhs.Y();
|
---|
131 | i[2] = rhs.Z();
|
---|
132 | return *this;
|
---|
133 | }
|
---|
134 |
|
---|
135 | Vector& operator=(const vmg_float& rhs)
|
---|
136 | {
|
---|
137 | i[0] = rhs;
|
---|
138 | i[1] = rhs;
|
---|
139 | i[2] = rhs;
|
---|
140 | return *this;
|
---|
141 | }
|
---|
142 |
|
---|
143 | bool operator==(const Vector& other) const
|
---|
144 | {
|
---|
145 | return (i[0]==other.X() && i[1]==other.Y() && i[2]==other.Z());
|
---|
146 | }
|
---|
147 |
|
---|
148 | bool operator!=(const Vector& other) const
|
---|
149 | {
|
---|
150 | return !(*this == other);
|
---|
151 | }
|
---|
152 |
|
---|
153 | Vector& operator+=(const Vector& rhs)
|
---|
154 | {
|
---|
155 | i[0] += rhs.X();
|
---|
156 | i[1] += rhs.Y();
|
---|
157 | i[2] += rhs.Z();
|
---|
158 | return *this;
|
---|
159 | }
|
---|
160 |
|
---|
161 | Vector& operator-=(const Vector& rhs)
|
---|
162 | {
|
---|
163 | i[0] -= rhs.X();
|
---|
164 | i[1] -= rhs.Y();
|
---|
165 | i[2] -= rhs.Z();
|
---|
166 | return *this;
|
---|
167 | }
|
---|
168 |
|
---|
169 | Vector& operator*=(const Vector& rhs)
|
---|
170 | {
|
---|
171 | i[0] *= rhs.X();
|
---|
172 | i[1] *= rhs.Y();
|
---|
173 | i[2] *= rhs.Z();
|
---|
174 | return *this;
|
---|
175 | }
|
---|
176 |
|
---|
177 | Vector& operator/=(const Vector& rhs)
|
---|
178 | {
|
---|
179 | i[0] /= rhs.X();
|
---|
180 | i[1] /= rhs.Y();
|
---|
181 | i[2] /= rhs.Z();
|
---|
182 | return *this;
|
---|
183 | }
|
---|
184 |
|
---|
185 | Vector& operator+=(const vmg_float& rhs)
|
---|
186 | {
|
---|
187 | i[0] += rhs;
|
---|
188 | i[1] += rhs;
|
---|
189 | i[2] += rhs;
|
---|
190 | return *this;
|
---|
191 | }
|
---|
192 |
|
---|
193 | Vector& operator-=(const vmg_float& rhs)
|
---|
194 | {
|
---|
195 | i[0] -= rhs;
|
---|
196 | i[1] -= rhs;
|
---|
197 | i[2] -= rhs;
|
---|
198 | return *this;
|
---|
199 | }
|
---|
200 |
|
---|
201 | Vector& operator*=(const vmg_float& rhs)
|
---|
202 | {
|
---|
203 | i[0] *= rhs;
|
---|
204 | i[1] *= rhs;
|
---|
205 | i[2] *= rhs;
|
---|
206 | return *this;
|
---|
207 | }
|
---|
208 |
|
---|
209 | Vector& operator/=(const vmg_float& rhs)
|
---|
210 | {
|
---|
211 | i[0] /= rhs;
|
---|
212 | i[1] /= rhs;
|
---|
213 | i[2] /= rhs;
|
---|
214 | return *this;
|
---|
215 | }
|
---|
216 |
|
---|
217 | Vector operator+(const Vector& rhs) const
|
---|
218 | {
|
---|
219 | return Vector(*this) += rhs;
|
---|
220 | }
|
---|
221 |
|
---|
222 | Vector operator-(const Vector& rhs) const
|
---|
223 | {
|
---|
224 | return Vector(*this) -= rhs;
|
---|
225 | }
|
---|
226 |
|
---|
227 | Vector operator*(const Vector& rhs) const
|
---|
228 | {
|
---|
229 | return Vector(*this) *= rhs;
|
---|
230 | }
|
---|
231 |
|
---|
232 | Vector operator/(const Vector& rhs) const
|
---|
233 | {
|
---|
234 | return Vector(*this) /= rhs;
|
---|
235 | }
|
---|
236 |
|
---|
237 | Vector operator+(const vmg_float& rhs) const
|
---|
238 | {
|
---|
239 | return Vector(*this) += rhs;
|
---|
240 | }
|
---|
241 |
|
---|
242 | Vector operator-(const vmg_float& rhs) const
|
---|
243 | {
|
---|
244 | return Vector(*this) -= rhs;
|
---|
245 | }
|
---|
246 |
|
---|
247 | Vector operator*(const vmg_float& rhs) const
|
---|
248 | {
|
---|
249 | return Vector(*this) *= rhs;
|
---|
250 | }
|
---|
251 |
|
---|
252 | Vector operator/(const vmg_float& rhs) const
|
---|
253 | {
|
---|
254 | return Vector(*this) /= rhs;
|
---|
255 | }
|
---|
256 |
|
---|
257 | private:
|
---|
258 | vmg_float i[3];
|
---|
259 | };
|
---|
260 |
|
---|
261 | inline Vector operator+(const vmg_float& lhs, const Vector& rhs)
|
---|
262 | {
|
---|
263 | return Vector(lhs) += rhs;
|
---|
264 | }
|
---|
265 |
|
---|
266 | inline Vector operator-(const vmg_float& lhs, const Vector& rhs)
|
---|
267 | {
|
---|
268 | return Vector(lhs) -= rhs;
|
---|
269 | }
|
---|
270 |
|
---|
271 | inline Vector operator*(const vmg_float& lhs, const Vector& rhs)
|
---|
272 | {
|
---|
273 | return Vector(lhs) *= rhs;
|
---|
274 | }
|
---|
275 |
|
---|
276 | inline Vector operator/(const vmg_float& lhs, const Vector& rhs)
|
---|
277 | {
|
---|
278 | return Vector(lhs) /= rhs;
|
---|
279 | }
|
---|
280 |
|
---|
281 | inline vmg_float Norm_2(const Vector& vec)
|
---|
282 | {
|
---|
283 | return sqrt(vec.X()*vec.X()+vec.Y()*vec.Y()+vec.Z()*vec.Z());
|
---|
284 | }
|
---|
285 |
|
---|
286 | inline vmg_float InnerProduct(const Vector& vec1, const Vector& vec2)
|
---|
287 | {
|
---|
288 | return vec1.X()*vec2.X()+vec1.Y()*vec2.Y()+vec1.Z()*vec2.Z();
|
---|
289 | }
|
---|
290 |
|
---|
291 | std::ostream& operator<<(std::ostream& out, const Vector& vector);
|
---|
292 |
|
---|
293 | }
|
---|
294 |
|
---|
295 | #endif /* VECTOR_HPP_ */
|
---|