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