| 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 | 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 | for (int j=0; j<3; ++j) {
|
|---|
| 106 | if (this->i[j] < other.i[j]) return true;
|
|---|
| 107 | if (this->i[j] != other.i[j]) return false;
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | return false;
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | bool operator!=(const Index& other) const
|
|---|
| 114 | {
|
|---|
| 115 | return !(*this == other);
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | Index& operator+=(const Index& rhs)
|
|---|
| 119 | {
|
|---|
| 120 | i[0] += rhs.X();
|
|---|
| 121 | i[1] += rhs.Y();
|
|---|
| 122 | i[2] += rhs.Z();
|
|---|
| 123 | return *this;
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | Index& operator-=(const Index& rhs)
|
|---|
| 127 | {
|
|---|
| 128 | i[0] -= rhs.X();
|
|---|
| 129 | i[1] -= rhs.Y();
|
|---|
| 130 | i[2] -= rhs.Z();
|
|---|
| 131 | return *this;
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | Index& operator*=(const Index& rhs)
|
|---|
| 135 | {
|
|---|
| 136 | i[0] *= rhs.X();
|
|---|
| 137 | i[1] *= rhs.Y();
|
|---|
| 138 | i[2] *= rhs.Z();
|
|---|
| 139 | return *this;
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | Index& operator/=(const Index& rhs)
|
|---|
| 143 | {
|
|---|
| 144 | i[0] /= rhs.X();
|
|---|
| 145 | i[1] /= rhs.Y();
|
|---|
| 146 | i[2] /= rhs.Z();
|
|---|
| 147 | return *this;
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | Index& operator%=(const Index& rhs)
|
|---|
| 151 | {
|
|---|
| 152 | i[0] %= rhs.X();
|
|---|
| 153 | i[1] %= rhs.Y();
|
|---|
| 154 | i[2] %= rhs.Z();
|
|---|
| 155 | return *this;
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | Index& operator+=(const int& rhs)
|
|---|
| 159 | {
|
|---|
| 160 | i[0] += rhs;
|
|---|
| 161 | i[1] += rhs;
|
|---|
| 162 | i[2] += rhs;
|
|---|
| 163 | return *this;
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | Index& operator-=(const int& rhs)
|
|---|
| 167 | {
|
|---|
| 168 | i[0] -= rhs;
|
|---|
| 169 | i[1] -= rhs;
|
|---|
| 170 | i[2] -= rhs;
|
|---|
| 171 | return *this;
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| 174 | Index& operator*=(const int& rhs)
|
|---|
| 175 | {
|
|---|
| 176 | i[0] *= rhs;
|
|---|
| 177 | i[1] *= rhs;
|
|---|
| 178 | i[2] *= rhs;
|
|---|
| 179 | return *this;
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | Index& operator/=(const int& rhs)
|
|---|
| 183 | {
|
|---|
| 184 | i[0] /= rhs;
|
|---|
| 185 | i[1] /= rhs;
|
|---|
| 186 | i[2] /= rhs;
|
|---|
| 187 | return *this;
|
|---|
| 188 | }
|
|---|
| 189 |
|
|---|
| 190 | Index& operator%=(const int& rhs)
|
|---|
| 191 | {
|
|---|
| 192 | i[0] %= rhs;
|
|---|
| 193 | i[1] %= rhs;
|
|---|
| 194 | i[2] %= rhs;
|
|---|
| 195 | return *this;
|
|---|
| 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 Index& rhs) const
|
|---|
| 214 | {
|
|---|
| 215 | return Index(*this) /= rhs;
|
|---|
| 216 | }
|
|---|
| 217 |
|
|---|
| 218 | Index operator%(const Index& 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 | Index operator/(const int& rhs) const
|
|---|
| 239 | {
|
|---|
| 240 | return Index(*this) /= rhs;
|
|---|
| 241 | }
|
|---|
| 242 |
|
|---|
| 243 | Index operator%(const int& rhs) const
|
|---|
| 244 | {
|
|---|
| 245 | return Index(*this) %= rhs;
|
|---|
| 246 | }
|
|---|
| 247 |
|
|---|
| 248 | Vector operator+(const Vector& rhs) const;
|
|---|
| 249 | Vector operator-(const Vector& rhs) const;
|
|---|
| 250 | Vector operator*(const Vector& rhs) const;
|
|---|
| 251 | Vector operator/(const Vector& rhs) const;
|
|---|
| 252 |
|
|---|
| 253 | Vector operator+(const vmg_float& rhs) const;
|
|---|
| 254 | Vector operator-(const vmg_float& rhs) const;
|
|---|
| 255 | Vector operator*(const vmg_float& rhs) const;
|
|---|
| 256 | Vector operator/(const vmg_float& rhs) const;
|
|---|
| 257 |
|
|---|
| 258 | private:
|
|---|
| 259 | int i[3];
|
|---|
| 260 | };
|
|---|
| 261 |
|
|---|
| 262 | inline Index operator+(const int& lhs, const Index& rhs)
|
|---|
| 263 | {
|
|---|
| 264 | return Index(lhs) += rhs;
|
|---|
| 265 | }
|
|---|
| 266 |
|
|---|
| 267 | inline Index operator-(const int& lhs, const Index& rhs)
|
|---|
| 268 | {
|
|---|
| 269 | return Index(lhs) -= rhs;
|
|---|
| 270 | }
|
|---|
| 271 |
|
|---|
| 272 | inline Index operator*(const int& lhs, const Index& rhs)
|
|---|
| 273 | {
|
|---|
| 274 | return Index(lhs) *= rhs;
|
|---|
| 275 | }
|
|---|
| 276 |
|
|---|
| 277 | inline Index operator/(const int& lhs, const Index& rhs)
|
|---|
| 278 | {
|
|---|
| 279 | return Index(lhs) /= rhs;
|
|---|
| 280 | }
|
|---|
| 281 |
|
|---|
| 282 | inline Index operator%(const int& lhs, const Index& rhs)
|
|---|
| 283 | {
|
|---|
| 284 | return Index(lhs) %= rhs;
|
|---|
| 285 | }
|
|---|
| 286 |
|
|---|
| 287 | const Index Max(const Index& index1, const Index& index2);
|
|---|
| 288 | std::ostream& operator<<(std::ostream& out, const Index& index);
|
|---|
| 289 |
|
|---|
| 290 | }
|
|---|
| 291 |
|
|---|
| 292 | #endif /* INDEX_HPP_ */
|
|---|