source: src/base/vector.hpp@ 2a5451

Last change on this file since 2a5451 was fcf7f6, checked in by Julian Iseringhausen <isering@…>, 14 years ago

vmg: Added license files and headers (GPLv3).

git-svn-id: https://svn.version.fz-juelich.de/scafacos/trunk@1812 5161e1c8-67bf-11de-9fd5-51895aff932f

  • Property mode set to 100644
File size: 6.1 KB
Line 
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
36namespace VMG
37{
38
39class Index;
40
41class Vector
42{
43public:
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
109 Vector MaxComponentwise(const Vector& rhs) const
110 {
111 return Vector(std::max(i[0], rhs.i[0]), std::max(i[1], rhs.i[1]), std::max(i[2], rhs.i[2]));
112 }
113
114 Vector MinComponentwise(const Vector& rhs) const
115 {
116 return Vector(std::min(i[0], rhs.i[0]), std::min(i[1], rhs.i[1]), std::min(i[2], rhs.i[2]));
117 }
118
119 vmg_float* vec() {return i;}
120 const vmg_float* vec() const {return i;}
121
122 Vector& operator=(const Vector& rhs)
123 {
124 i[0] = rhs.X();
125 i[1] = rhs.Y();
126 i[2] = rhs.Z();
127 return *this;
128 }
129
130 Vector& operator=(const vmg_float& rhs)
131 {
132 i[0] = rhs;
133 i[1] = rhs;
134 i[2] = rhs;
135 return *this;
136 }
137
138 bool operator==(const Vector& other) const
139 {
140 return (i[0]==other.X() && i[1]==other.Y() && i[2]==other.Z());
141 }
142
143 bool operator!=(const Vector& other) const
144 {
145 return !(*this == other);
146 }
147
148 Vector& operator+=(const Vector& rhs)
149 {
150 i[0] += rhs.X();
151 i[1] += rhs.Y();
152 i[2] += rhs.Z();
153 return *this;
154 }
155
156 Vector& operator-=(const Vector& rhs)
157 {
158 i[0] -= rhs.X();
159 i[1] -= rhs.Y();
160 i[2] -= rhs.Z();
161 return *this;
162 }
163
164 Vector& operator*=(const Vector& rhs)
165 {
166 i[0] *= rhs.X();
167 i[1] *= rhs.Y();
168 i[2] *= rhs.Z();
169 return *this;
170 }
171
172 Vector& operator/=(const Vector& rhs)
173 {
174 i[0] /= rhs.X();
175 i[1] /= rhs.Y();
176 i[2] /= rhs.Z();
177 return *this;
178 }
179
180 Vector& operator+=(const vmg_float& rhs)
181 {
182 i[0] += rhs;
183 i[1] += rhs;
184 i[2] += rhs;
185 return *this;
186 }
187
188 Vector& operator-=(const vmg_float& rhs)
189 {
190 i[0] -= rhs;
191 i[1] -= rhs;
192 i[2] -= rhs;
193 return *this;
194 }
195
196 Vector& operator*=(const vmg_float& rhs)
197 {
198 i[0] *= rhs;
199 i[1] *= rhs;
200 i[2] *= rhs;
201 return *this;
202 }
203
204 Vector& operator/=(const vmg_float& rhs)
205 {
206 i[0] /= rhs;
207 i[1] /= rhs;
208 i[2] /= rhs;
209 return *this;
210 }
211
212 Vector operator+(const Vector& rhs) const
213 {
214 return Vector(*this) += rhs;
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 vmg_float& 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
252private:
253 vmg_float i[3];
254};
255
256inline Vector operator+(const vmg_float& lhs, const Vector& rhs)
257{
258 return Vector(lhs) += rhs;
259}
260
261inline Vector operator-(const vmg_float& lhs, const Vector& rhs)
262{
263 return Vector(lhs) -= rhs;
264}
265
266inline Vector operator*(const vmg_float& lhs, const Vector& rhs)
267{
268 return Vector(lhs) *= rhs;
269}
270
271inline Vector operator/(const vmg_float& lhs, const Vector& rhs)
272{
273 return Vector(lhs) /= rhs;
274}
275
276inline vmg_float Norm_2(const Vector& vec)
277{
278 return sqrt(vec.X()*vec.X()+vec.Y()*vec.Y()+vec.Z()*vec.Z());
279}
280
281inline vmg_float InnerProduct(const Vector& vec1, const Vector& vec2)
282{
283 return vec1.X()*vec2.X()+vec1.Y()*vec2.Y()+vec1.Z()*vec2.Z();
284}
285
286std::ostream& operator<<(std::ostream& out, const Vector& vector);
287
288}
289
290#endif /* VECTOR_HPP_ */
Note: See TracBrowser for help on using the repository browser.