source: src/base/vector.hpp@ e3dbbf

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

Merge recent changes of the vmg library into ScaFaCos.

Includes a fix for the communication problems on Jugene.

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

  • Property mode set to 100644
File size: 4.6 KB
Line 
1/**
2 * @file vector.hpp
3 * @author Julian Iseringhausen <isering@ins.uni-bonn.de>
4 * @date Mon Apr 18 12:26:13 2011
5 *
6 * @brief VMG::Vector
7 *
8 *
9 */
10
11#ifndef VECTOR_HPP_
12#define VECTOR_HPP_
13
14#include <algorithm>
15#include <cmath>
16#include <cstring>
17#include <iostream>
18
19namespace VMG
20{
21
22class Index;
23
24class Vector
25{
26public:
27 Vector(const Index& index);
28 Vector(const vmg_float& x, const vmg_float& y, const vmg_float& z);
29 Vector(const vmg_float& val);
30 Vector(const vmg_float* arr);
31 Vector();
32
33 Vector(const Vector& other)
34 {
35 std::memcpy(this->i, other.i, 3*sizeof(vmg_float));
36 }
37
38 vmg_float& operator[](const int& index) {return i[index];}
39 const vmg_float& operator[](const int& index) const {return i[index];}
40
41 vmg_float& X() {return i[0];}
42 vmg_float& Y() {return i[1];}
43 vmg_float& Z() {return i[2];}
44
45 const vmg_float& X() const {return i[0];}
46 const vmg_float& Y() const {return i[1];}
47 const vmg_float& Z() const {return i[2];}
48
49 Vector Abs() const {return Vector(fabs(i[0]), fabs(i[1]), fabs(i[2]));}
50 vmg_float Length() const {return sqrt(i[0]*i[0]+i[1]*i[1]+i[2]*i[2]);}
51 vmg_float Max() const {return std::max(std::max(i[0],i[1]),i[2]);}
52 vmg_float Min() const {return std::min(std::min(i[0],i[1]),i[2]);}
53 vmg_float Product() const {return i[0]*i[1]*i[2];}
54
55 Vector MaxComponentwise(const Vector& rhs) const
56 {
57 return Vector(std::max(i[0], rhs.i[0]), std::max(i[1], rhs.i[1]), std::max(i[2], rhs.i[2]));
58 }
59
60 Vector MinComponentwise(const Vector& rhs) const
61 {
62 return Vector(std::min(i[0], rhs.i[0]), std::min(i[1], rhs.i[1]), std::min(i[2], rhs.i[2]));
63 }
64
65 vmg_float* vec() {return i;}
66 const vmg_float* vec() const {return i;}
67
68 Vector& operator=(const Vector& rhs)
69 {
70 i[0] = rhs.X();
71 i[1] = rhs.Y();
72 i[2] = rhs.Z();
73 return *this;
74 }
75
76 Vector& operator=(const vmg_float& rhs)
77 {
78 i[0] = rhs;
79 i[1] = rhs;
80 i[2] = rhs;
81 return *this;
82 }
83
84 bool operator==(const Vector& other) const
85 {
86 return (i[0]==other.X() && i[1]==other.Y() && i[2]==other.Z());
87 }
88
89 bool operator!=(const Vector& other) const
90 {
91 return !(*this == other);
92 }
93
94 Vector& operator+=(const Vector& rhs)
95 {
96 i[0] += rhs.X();
97 i[1] += rhs.Y();
98 i[2] += rhs.Z();
99 return *this;
100 }
101
102 Vector& operator-=(const Vector& rhs)
103 {
104 i[0] -= rhs.X();
105 i[1] -= rhs.Y();
106 i[2] -= rhs.Z();
107 return *this;
108 }
109
110 Vector& operator*=(const Vector& rhs)
111 {
112 i[0] *= rhs.X();
113 i[1] *= rhs.Y();
114 i[2] *= rhs.Z();
115 return *this;
116 }
117
118 Vector& operator/=(const Vector& rhs)
119 {
120 i[0] /= rhs.X();
121 i[1] /= rhs.Y();
122 i[2] /= rhs.Z();
123 return *this;
124 }
125
126 Vector& operator+=(const vmg_float& rhs)
127 {
128 i[0] += rhs;
129 i[1] += rhs;
130 i[2] += rhs;
131 return *this;
132 }
133
134 Vector& operator-=(const vmg_float& rhs)
135 {
136 i[0] -= rhs;
137 i[1] -= rhs;
138 i[2] -= rhs;
139 return *this;
140 }
141
142 Vector& operator*=(const vmg_float& rhs)
143 {
144 i[0] *= rhs;
145 i[1] *= rhs;
146 i[2] *= rhs;
147 return *this;
148 }
149
150 Vector& operator/=(const vmg_float& rhs)
151 {
152 i[0] /= rhs;
153 i[1] /= rhs;
154 i[2] /= rhs;
155 return *this;
156 }
157
158 const Vector operator+(const Vector& rhs) const
159 {
160 return Vector(*this) += rhs;
161 }
162
163 const Vector operator-(const Vector& rhs) const
164 {
165 return Vector(*this) -= rhs;
166 }
167
168 const Vector operator*(const Vector& rhs) const
169 {
170 return Vector(*this) *= rhs;
171 }
172
173 const Vector operator/(const Vector& rhs) const
174 {
175 return Vector(*this) /= rhs;
176 }
177
178 const Vector operator+(const vmg_float& rhs) const
179 {
180 return Vector(*this) += rhs;
181 }
182
183 const Vector operator-(const vmg_float& rhs) const
184 {
185 return Vector(*this) -= rhs;
186 }
187
188 const Vector operator*(const vmg_float& rhs) const
189 {
190 return Vector(*this) *= rhs;
191 }
192
193 const Vector operator/(const vmg_float& rhs) const
194 {
195 return Vector(*this) /= rhs;
196 }
197
198private:
199 vmg_float i[3];
200};
201
202inline const Vector operator+(const vmg_float& lhs, const Vector& rhs)
203{
204 return Vector(lhs) += rhs;
205}
206
207inline const Vector operator-(const vmg_float& lhs, const Vector& rhs)
208{
209 return Vector(lhs) -= rhs;
210}
211
212inline const Vector operator*(const vmg_float& lhs, const Vector& rhs)
213{
214 return Vector(lhs) *= rhs;
215}
216
217inline const Vector operator/(const vmg_float& lhs, const Vector& rhs)
218{
219 return Vector(lhs) /= rhs;
220}
221
222inline vmg_float Norm_2(const Vector& vec)
223{
224 return sqrt(vec.X()*vec.X()+vec.Y()*vec.Y()+vec.Z()*vec.Z());
225}
226
227inline vmg_float InnerProduct(const Vector& vec1, const Vector& vec2)
228{
229 return vec1.X()*vec2.X()+vec1.Y()*vec2.Y()+vec1.Z()*vec2.Z();
230}
231
232std::ostream& operator<<(std::ostream& out, const Vector& vector);
233
234}
235
236#endif /* VECTOR_HPP_ */
Note: See TracBrowser for help on using the repository browser.