source: src/base/index.hpp@ facba0

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

Major vmg update.

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

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