source: src/base/vector.hpp@ c2f74f

Last change on this file since c2f74f was 48b662, checked in by Olaf Lenz <olenz@…>, 14 years ago

Moved files in scafacos_fcs one level up.

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

  • Property mode set to 100644
File size: 4.2 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 <iostream>
17
18namespace VMG
19{
20
21class Index;
22
23class Vector
24{
25public:
26 Vector(const Index& index);
27 Vector(vmg_float x, vmg_float y, vmg_float z);
28 Vector(vmg_float val);
29 Vector(vmg_float* arr) {i[0]=arr[0];i[1]=arr[1];i[2]=arr[2];}
30 Vector();
31
32 vmg_float& operator[](const int& index) {return i[index];}
33 const vmg_float& operator[](const int& index) const {return i[index];}
34
35 vmg_float& X() {return i[0];}
36 vmg_float& Y() {return i[1];}
37 vmg_float& Z() {return i[2];}
38
39 const vmg_float& X() const {return i[0];}
40 const vmg_float& Y() const {return i[1];}
41 const vmg_float& Z() const {return i[2];}
42
43 const Vector Abs() {return Vector(fabs(i[0]), fabs(i[1]), fabs(i[2]));}
44 vmg_float Length() const {return sqrt(i[0]*i[0]+i[1]*i[1]+i[2]*i[2]);}
45 vmg_float Max() const {return std::max(std::max(i[0],i[1]),i[2]);}
46 vmg_float Min() const {return std::min(std::min(i[0],i[1]),i[2]);}
47 vmg_float Product() const {return i[0]*i[1]*i[2];}
48
49 vmg_float* vec() {return i;}
50 const vmg_float* vec() const {return i;}
51
52 Vector& operator=(const Vector& rhs)
53 {
54 i[0] = rhs.X();
55 i[1] = rhs.Y();
56 i[2] = rhs.Z();
57 return *this;
58 }
59
60 Vector& operator=(const vmg_float& rhs)
61 {
62 i[0] = rhs;
63 i[1] = rhs;
64 i[2] = rhs;
65 return *this;
66 }
67
68 bool operator==(const Vector& other) const
69 {
70 return (i[0]==other.X() && i[1]==other.Y() && i[2]==other.Z());
71 }
72
73 bool operator!=(const Vector& other) const
74 {
75 return !(*this == other);
76 }
77
78 Vector& operator+=(const Vector& rhs)
79 {
80 i[0] += rhs.X();
81 i[1] += rhs.Y();
82 i[2] += rhs.Z();
83 return *this;
84 }
85
86 Vector& operator-=(const Vector& rhs)
87 {
88 i[0] -= rhs.X();
89 i[1] -= rhs.Y();
90 i[2] -= rhs.Z();
91 return *this;
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 vmg_float& rhs)
111 {
112 i[0] += rhs;
113 i[1] += rhs;
114 i[2] += rhs;
115 return *this;
116 }
117
118 Vector& operator-=(const vmg_float& rhs)
119 {
120 i[0] -= rhs;
121 i[1] -= rhs;
122 i[2] -= rhs;
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 const Vector operator+(const Vector& rhs) const
143 {
144 return Vector(*this) += rhs;
145 }
146
147 const Vector operator-(const Vector& rhs) const
148 {
149 return Vector(*this) -= rhs;
150 }
151
152 const Vector operator*(const Vector& rhs) const
153 {
154 return Vector(*this) *= rhs;
155 }
156
157 const Vector operator/(const Vector& rhs) const
158 {
159 return Vector(*this) /= rhs;
160 }
161
162 const Vector operator+(const vmg_float& rhs) const
163 {
164 return Vector(*this) += rhs;
165 }
166
167 const Vector operator-(const vmg_float& rhs) const
168 {
169 return Vector(*this) -= rhs;
170 }
171
172 const Vector operator*(const vmg_float& rhs) const
173 {
174 return Vector(*this) *= rhs;
175 }
176
177 const Vector operator/(const vmg_float& rhs) const
178 {
179 return Vector(*this) /= rhs;
180 }
181
182private:
183 vmg_float i[3];
184};
185
186inline const Vector operator+(const vmg_float& lhs, const Vector& rhs)
187{
188 return Vector(lhs) += rhs;
189}
190
191inline const Vector operator-(const vmg_float& lhs, const Vector& rhs)
192{
193 return Vector(lhs) -= rhs;
194}
195
196inline const Vector operator*(const vmg_float& lhs, const Vector& rhs)
197{
198 return Vector(lhs) *= rhs;
199}
200
201inline const Vector operator/(const vmg_float& lhs, const Vector& rhs)
202{
203 return Vector(lhs) /= rhs;
204}
205
206inline vmg_float Norm_2(const Vector& vec)
207{
208 return sqrt(vec.X()*vec.X()+vec.Y()*vec.Y()+vec.Z()*vec.Z());
209}
210
211inline vmg_float InnerProduct(const Vector& vec1, const Vector& vec2)
212{
213 return vec1.X()*vec2.X()+vec1.Y()*vec2.Y()+vec1.Z()*vec2.Z();
214}
215
216std::ostream& operator<<(std::ostream& out, const Vector& base);
217
218}
219
220#endif /* VECTOR_HPP_ */
Note: See TracBrowser for help on using the repository browser.