| [48b662] | 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>
|
|---|
| [dfed1c] | 16 | #include <cstring>
|
|---|
| [48b662] | 17 | #include <iostream>
|
|---|
| 18 |
|
|---|
| 19 | namespace VMG
|
|---|
| 20 | {
|
|---|
| 21 |
|
|---|
| 22 | class Vector;
|
|---|
| 23 |
|
|---|
| 24 | class Index
|
|---|
| 25 | {
|
|---|
| 26 | public:
|
|---|
| [dfed1c] | 27 | Index(const Index& rhs);
|
|---|
| [48b662] | 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 |
|
|---|
| [dfed1c] | 45 | Index Abs() {return Index(abs(i[0]), abs(i[1]), abs(i[2]));}
|
|---|
| [48b662] | 46 | int Max() const {return std::max(std::max(i[0],i[1]),i[2]);}
|
|---|
| 47 | int Min() const {return std::min(std::min(i[0],i[1]),i[2]);}
|
|---|
| 48 | int Product() const {return i[0]*i[1]*i[2];}
|
|---|
| [dfed1c] | 49 | int Sum() const {return i[0]+i[1]+i[2];}
|
|---|
| 50 |
|
|---|
| 51 | bool IsInBounds(const Index& begin, const Index& end) const
|
|---|
| 52 | {
|
|---|
| 53 | return i[0] >= begin[0] && i[0] < end[0] &&
|
|---|
| 54 | i[1] >= begin[1] && i[1] < end[1] &&
|
|---|
| 55 | i[2] >= begin[2] && i[2] < end[2];
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| [ac6d04] | 58 | bool IsComponentwiseLess(const Index& other) const
|
|---|
| 59 | {
|
|---|
| 60 | return this->i[0] < other.i[0]
|
|---|
| 61 | && this->i[1] < other.i[1]
|
|---|
| 62 | && this->i[2] < other.i[2];
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | bool IsComponentwiseLessOrEqual(const Index& other) const
|
|---|
| 66 | {
|
|---|
| 67 | return this->i[0] <= other.i[0]
|
|---|
| 68 | && this->i[1] <= other.i[1]
|
|---|
| 69 | && this->i[2] <= other.i[2];
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | bool IsComponentwiseGreater(const Index& other) const
|
|---|
| 73 | {
|
|---|
| 74 | return this->i[0] > other.i[0]
|
|---|
| 75 | && this->i[1] > other.i[1]
|
|---|
| 76 | && this->i[2] > other.i[2];
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | bool IsComponentwiseGreaterOrEqual(const Index& other) const
|
|---|
| 80 | {
|
|---|
| 81 | return this->i[0] >= other.i[0]
|
|---|
| 82 | && this->i[1] >= other.i[1]
|
|---|
| 83 | && this->i[2] >= other.i[2];
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| [dfed1c] | 86 | Index MaxComponentwise(const Index& rhs) const
|
|---|
| 87 | {
|
|---|
| 88 | return Index(std::max(i[0], rhs.i[0]), std::max(i[1], rhs.i[1]), std::max(i[2], rhs.i[2]));
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | Index MinComponentwise(const Index& rhs) const
|
|---|
| 92 | {
|
|---|
| 93 | return Index(std::min(i[0], rhs.i[0]), std::min(i[1], rhs.i[1]), std::min(i[2], rhs.i[2]));
|
|---|
| 94 | }
|
|---|
| [48b662] | 95 |
|
|---|
| [ac6d04] | 96 | Index Clamp(const Index& lower_bound, const Index& upper_bound) const
|
|---|
| [48b662] | 97 | {
|
|---|
| [ac6d04] | 98 | Index index(*this);
|
|---|
| [48b662] | 99 | for (int j=0; j<3; ++j) {
|
|---|
| [ac6d04] | 100 | index.i[j] = std::max(index.i[j], lower_bound[j]);
|
|---|
| 101 | index.i[j] = std::min(index.i[j], upper_bound[j]);
|
|---|
| [48b662] | 102 | }
|
|---|
| [ac6d04] | 103 | return index;
|
|---|
| [48b662] | 104 | }
|
|---|
| 105 |
|
|---|
| 106 | int* vec() {return i;}
|
|---|
| 107 | const int* vec() const {return i;}
|
|---|
| 108 |
|
|---|
| 109 | Index& operator=(const Index& rhs)
|
|---|
| 110 | {
|
|---|
| 111 | i[0] = rhs.X();
|
|---|
| 112 | i[1] = rhs.Y();
|
|---|
| 113 | i[2] = rhs.Z();
|
|---|
| 114 | return *this;
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | Index& operator=(const int& rhs)
|
|---|
| 118 | {
|
|---|
| 119 | i[0] = rhs;
|
|---|
| 120 | i[1] = rhs;
|
|---|
| 121 | i[2] = rhs;
|
|---|
| 122 | return *this;
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | bool operator==(const Index& other) const
|
|---|
| 126 | {
|
|---|
| 127 | return (i[0]==other.X() && i[1]==other.Y() && i[2]==other.Z());
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| [894a5f] | 130 | bool operator<(const Index& other) const
|
|---|
| 131 | {
|
|---|
| 132 | for (int j=0; j<3; ++j) {
|
|---|
| 133 | if (this->i[j] < other.i[j]) return true;
|
|---|
| 134 | if (this->i[j] != other.i[j]) return false;
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | return false;
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| [48b662] | 140 | bool operator!=(const Index& other) const
|
|---|
| 141 | {
|
|---|
| 142 | return !(*this == other);
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | Index& operator+=(const Index& rhs)
|
|---|
| 146 | {
|
|---|
| 147 | i[0] += rhs.X();
|
|---|
| 148 | i[1] += rhs.Y();
|
|---|
| 149 | i[2] += rhs.Z();
|
|---|
| 150 | return *this;
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | Index& operator-=(const Index& rhs)
|
|---|
| 154 | {
|
|---|
| 155 | i[0] -= rhs.X();
|
|---|
| 156 | i[1] -= rhs.Y();
|
|---|
| 157 | i[2] -= rhs.Z();
|
|---|
| 158 | return *this;
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | Index& operator*=(const Index& rhs)
|
|---|
| 162 | {
|
|---|
| 163 | i[0] *= rhs.X();
|
|---|
| 164 | i[1] *= rhs.Y();
|
|---|
| 165 | i[2] *= rhs.Z();
|
|---|
| 166 | return *this;
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 | Index& operator/=(const Index& rhs)
|
|---|
| 170 | {
|
|---|
| 171 | i[0] /= rhs.X();
|
|---|
| 172 | i[1] /= rhs.Y();
|
|---|
| 173 | i[2] /= rhs.Z();
|
|---|
| 174 | return *this;
|
|---|
| 175 | }
|
|---|
| 176 |
|
|---|
| 177 | Index& operator%=(const Index& rhs)
|
|---|
| 178 | {
|
|---|
| 179 | i[0] %= rhs.X();
|
|---|
| 180 | i[1] %= rhs.Y();
|
|---|
| 181 | i[2] %= rhs.Z();
|
|---|
| 182 | return *this;
|
|---|
| 183 | }
|
|---|
| 184 |
|
|---|
| 185 | Index& operator+=(const int& rhs)
|
|---|
| 186 | {
|
|---|
| 187 | i[0] += rhs;
|
|---|
| 188 | i[1] += rhs;
|
|---|
| 189 | i[2] += rhs;
|
|---|
| 190 | return *this;
|
|---|
| 191 | }
|
|---|
| 192 |
|
|---|
| 193 | Index& operator-=(const int& rhs)
|
|---|
| 194 | {
|
|---|
| 195 | i[0] -= rhs;
|
|---|
| 196 | i[1] -= rhs;
|
|---|
| 197 | i[2] -= rhs;
|
|---|
| 198 | return *this;
|
|---|
| 199 | }
|
|---|
| 200 |
|
|---|
| 201 | Index& operator*=(const int& rhs)
|
|---|
| 202 | {
|
|---|
| 203 | i[0] *= rhs;
|
|---|
| 204 | i[1] *= rhs;
|
|---|
| 205 | i[2] *= rhs;
|
|---|
| 206 | return *this;
|
|---|
| 207 | }
|
|---|
| 208 |
|
|---|
| 209 | Index& operator/=(const int& rhs)
|
|---|
| 210 | {
|
|---|
| 211 | i[0] /= rhs;
|
|---|
| 212 | i[1] /= rhs;
|
|---|
| 213 | i[2] /= rhs;
|
|---|
| 214 | return *this;
|
|---|
| 215 | }
|
|---|
| 216 |
|
|---|
| 217 | Index& operator%=(const int& rhs)
|
|---|
| 218 | {
|
|---|
| 219 | i[0] %= rhs;
|
|---|
| 220 | i[1] %= rhs;
|
|---|
| 221 | i[2] %= rhs;
|
|---|
| 222 | return *this;
|
|---|
| 223 | }
|
|---|
| 224 |
|
|---|
| [dfed1c] | 225 | Index operator+(const Index& rhs) const
|
|---|
| [48b662] | 226 | {
|
|---|
| 227 | return Index(*this) += rhs;
|
|---|
| 228 | }
|
|---|
| 229 |
|
|---|
| [dfed1c] | 230 | Index operator-(const Index& rhs) const
|
|---|
| [48b662] | 231 | {
|
|---|
| 232 | return Index(*this) -= rhs;
|
|---|
| 233 | }
|
|---|
| 234 |
|
|---|
| [dfed1c] | 235 | Index operator*(const Index& rhs) const
|
|---|
| [48b662] | 236 | {
|
|---|
| 237 | return Index(*this) *= rhs;
|
|---|
| 238 | }
|
|---|
| 239 |
|
|---|
| [dfed1c] | 240 | Index operator/(const Index& rhs) const
|
|---|
| [48b662] | 241 | {
|
|---|
| 242 | return Index(*this) /= rhs;
|
|---|
| 243 | }
|
|---|
| 244 |
|
|---|
| [dfed1c] | 245 | Index operator%(const Index& rhs) const
|
|---|
| [48b662] | 246 | {
|
|---|
| 247 | return Index(*this) %= rhs;
|
|---|
| 248 | }
|
|---|
| 249 |
|
|---|
| [dfed1c] | 250 | Index operator+(const int& rhs) const
|
|---|
| [48b662] | 251 | {
|
|---|
| 252 | return Index(*this) += rhs;
|
|---|
| 253 | }
|
|---|
| 254 |
|
|---|
| [dfed1c] | 255 | Index operator-(const int& rhs) const
|
|---|
| [48b662] | 256 | {
|
|---|
| 257 | return Index(*this) -= rhs;
|
|---|
| 258 | }
|
|---|
| 259 |
|
|---|
| [dfed1c] | 260 | Index operator*(const int& rhs) const
|
|---|
| [48b662] | 261 | {
|
|---|
| 262 | return Index(*this) *= rhs;
|
|---|
| 263 | }
|
|---|
| 264 |
|
|---|
| [dfed1c] | 265 | Index operator/(const int& rhs) const
|
|---|
| [48b662] | 266 | {
|
|---|
| 267 | return Index(*this) /= rhs;
|
|---|
| 268 | }
|
|---|
| 269 |
|
|---|
| [dfed1c] | 270 | Index operator%(const int& rhs) const
|
|---|
| [48b662] | 271 | {
|
|---|
| 272 | return Index(*this) %= rhs;
|
|---|
| 273 | }
|
|---|
| 274 |
|
|---|
| [dfed1c] | 275 | Vector operator+(const Vector& rhs) const;
|
|---|
| 276 | Vector operator-(const Vector& rhs) const;
|
|---|
| 277 | Vector operator*(const Vector& rhs) const;
|
|---|
| 278 | Vector operator/(const Vector& rhs) const;
|
|---|
| [48b662] | 279 |
|
|---|
| [dfed1c] | 280 | Vector operator+(const vmg_float& rhs) const;
|
|---|
| 281 | Vector operator-(const vmg_float& rhs) const;
|
|---|
| 282 | Vector operator*(const vmg_float& rhs) const;
|
|---|
| 283 | Vector operator/(const vmg_float& rhs) const;
|
|---|
| [48b662] | 284 |
|
|---|
| 285 | private:
|
|---|
| 286 | int i[3];
|
|---|
| 287 | };
|
|---|
| 288 |
|
|---|
| [dfed1c] | 289 | inline Index operator+(const int& lhs, const Index& rhs)
|
|---|
| [48b662] | 290 | {
|
|---|
| 291 | return Index(lhs) += rhs;
|
|---|
| 292 | }
|
|---|
| 293 |
|
|---|
| [dfed1c] | 294 | inline Index operator-(const int& lhs, const Index& rhs)
|
|---|
| [48b662] | 295 | {
|
|---|
| 296 | return Index(lhs) -= rhs;
|
|---|
| 297 | }
|
|---|
| 298 |
|
|---|
| [dfed1c] | 299 | inline Index operator*(const int& lhs, const Index& rhs)
|
|---|
| [48b662] | 300 | {
|
|---|
| 301 | return Index(lhs) *= rhs;
|
|---|
| 302 | }
|
|---|
| 303 |
|
|---|
| [dfed1c] | 304 | inline Index operator/(const int& lhs, const Index& rhs)
|
|---|
| [48b662] | 305 | {
|
|---|
| 306 | return Index(lhs) /= rhs;
|
|---|
| 307 | }
|
|---|
| 308 |
|
|---|
| [dfed1c] | 309 | inline Index operator%(const int& lhs, const Index& rhs)
|
|---|
| [48b662] | 310 | {
|
|---|
| 311 | return Index(lhs) %= rhs;
|
|---|
| 312 | }
|
|---|
| 313 |
|
|---|
| 314 | const Index Max(const Index& index1, const Index& index2);
|
|---|
| [dfed1c] | 315 | std::ostream& operator<<(std::ostream& out, const Index& index);
|
|---|
| [48b662] | 316 |
|
|---|
| 317 | }
|
|---|
| 318 |
|
|---|
| 319 | #endif /* INDEX_HPP_ */
|
|---|