| 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 |
|
|---|
| 11 | #ifndef INDEX_HPP_
|
|---|
| 12 | #define INDEX_HPP_
|
|---|
| 13 |
|
|---|
| 14 | #include <algorithm>
|
|---|
| 15 | #include <cmath>
|
|---|
| 16 | #include <cstdlib>
|
|---|
| 17 | #include <iostream>
|
|---|
| 18 |
|
|---|
| 19 | #include "base/stencil.hpp"
|
|---|
| 20 |
|
|---|
| 21 | namespace VMG
|
|---|
| 22 | {
|
|---|
| 23 |
|
|---|
| 24 | class Vector;
|
|---|
| 25 |
|
|---|
| 26 | class Index
|
|---|
| 27 | {
|
|---|
| 28 | public:
|
|---|
| 29 | Index(const Vector& vec);
|
|---|
| 30 | Index(const Stencil::iterator& iter);
|
|---|
| 31 | Index(int x, int y, int z) {i[0]=x;i[1]=y;i[2]=z;}
|
|---|
| 32 | Index(int val) {i[0]=val;i[1]=val;i[2]=val;}
|
|---|
| 33 | Index(int* arr) {i[0]=arr[0];i[1]=arr[1];i[2]=arr[2];}
|
|---|
| 34 | Index() {i[0]=0;i[1]=0;i[2]=0;}
|
|---|
| 35 |
|
|---|
| 36 | int& operator[](const int& index) {return i[index];}
|
|---|
| 37 | const int& operator[](const int& index) const {return i[index];}
|
|---|
| 38 |
|
|---|
| 39 | int& X() {return i[0];}
|
|---|
| 40 | int& Y() {return i[1];}
|
|---|
| 41 | int& Z() {return i[2];}
|
|---|
| 42 |
|
|---|
| 43 | const int& X() const {return i[0];}
|
|---|
| 44 | const int& Y() const {return i[1];}
|
|---|
| 45 | const int& Z() const {return i[2];}
|
|---|
| 46 |
|
|---|
| 47 | const Index Abs() {return Index(abs(i[0]), abs(i[1]), abs(i[2]));}
|
|---|
| 48 | int Length() const {return sqrt(i[0]*i[0]+i[1]*i[1]+i[2]*i[2]);}
|
|---|
| 49 | int Max() const {return std::max(std::max(i[0],i[1]),i[2]);}
|
|---|
| 50 | int Min() const {return std::min(std::min(i[0],i[1]),i[2]);}
|
|---|
| 51 | int Product() const {return i[0]*i[1]*i[2];}
|
|---|
| 52 |
|
|---|
| 53 | Index& Clamp(const Index& lower_bound, const Index& upper_bound)
|
|---|
| 54 | {
|
|---|
| 55 | for (int j=0; j<3; ++j) {
|
|---|
| 56 | i[j] = std::max(i[j], lower_bound[j]);
|
|---|
| 57 | i[j] = std::min(i[j], upper_bound[j]);
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | return *this;
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | int* vec() {return i;}
|
|---|
| 64 | const int* vec() const {return i;}
|
|---|
| 65 |
|
|---|
| 66 | Index& operator=(const Index& rhs)
|
|---|
| 67 | {
|
|---|
| 68 | i[0] = rhs.X();
|
|---|
| 69 | i[1] = rhs.Y();
|
|---|
| 70 | i[2] = rhs.Z();
|
|---|
| 71 | return *this;
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | Index& operator=(const int& rhs)
|
|---|
| 75 | {
|
|---|
| 76 | i[0] = rhs;
|
|---|
| 77 | i[1] = rhs;
|
|---|
| 78 | i[2] = rhs;
|
|---|
| 79 | return *this;
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | bool operator==(const Index& other) const
|
|---|
| 83 | {
|
|---|
| 84 | return (i[0]==other.X() && i[1]==other.Y() && i[2]==other.Z());
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | bool operator!=(const Index& other) const
|
|---|
| 88 | {
|
|---|
| 89 | return !(*this == other);
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | Index& operator+=(const Index& rhs)
|
|---|
| 93 | {
|
|---|
| 94 | i[0] += rhs.X();
|
|---|
| 95 | i[1] += rhs.Y();
|
|---|
| 96 | i[2] += rhs.Z();
|
|---|
| 97 | return *this;
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | Index& operator-=(const Index& rhs)
|
|---|
| 101 | {
|
|---|
| 102 | i[0] -= rhs.X();
|
|---|
| 103 | i[1] -= rhs.Y();
|
|---|
| 104 | i[2] -= rhs.Z();
|
|---|
| 105 | return *this;
|
|---|
| 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 int& rhs)
|
|---|
| 133 | {
|
|---|
| 134 | i[0] += rhs;
|
|---|
| 135 | i[1] += rhs;
|
|---|
| 136 | i[2] += rhs;
|
|---|
| 137 | return *this;
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | Index& operator-=(const int& rhs)
|
|---|
| 141 | {
|
|---|
| 142 | i[0] -= rhs;
|
|---|
| 143 | i[1] -= rhs;
|
|---|
| 144 | i[2] -= rhs;
|
|---|
| 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 | const Index operator+(const Index& rhs) const
|
|---|
| 173 | {
|
|---|
| 174 | return Index(*this) += rhs;
|
|---|
| 175 | }
|
|---|
| 176 |
|
|---|
| 177 | const Index operator-(const Index& rhs) const
|
|---|
| 178 | {
|
|---|
| 179 | return Index(*this) -= rhs;
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | const Index operator*(const Index& rhs) const
|
|---|
| 183 | {
|
|---|
| 184 | return Index(*this) *= rhs;
|
|---|
| 185 | }
|
|---|
| 186 |
|
|---|
| 187 | const Index operator/(const Index& rhs) const
|
|---|
| 188 | {
|
|---|
| 189 | return Index(*this) /= rhs;
|
|---|
| 190 | }
|
|---|
| 191 |
|
|---|
| 192 | const Index operator%(const Index& rhs) const
|
|---|
| 193 | {
|
|---|
| 194 | return Index(*this) %= rhs;
|
|---|
| 195 | }
|
|---|
| 196 |
|
|---|
| 197 | const Index operator+(const int& rhs) const
|
|---|
| 198 | {
|
|---|
| 199 | return Index(*this) += rhs;
|
|---|
| 200 | }
|
|---|
| 201 |
|
|---|
| 202 | const Index operator-(const int& rhs) const
|
|---|
| 203 | {
|
|---|
| 204 | return Index(*this) -= rhs;
|
|---|
| 205 | }
|
|---|
| 206 |
|
|---|
| 207 | const Index operator*(const int& rhs) const
|
|---|
| 208 | {
|
|---|
| 209 | return Index(*this) *= rhs;
|
|---|
| 210 | }
|
|---|
| 211 |
|
|---|
| 212 | const Index operator/(const int& rhs) const
|
|---|
| 213 | {
|
|---|
| 214 | return Index(*this) /= rhs;
|
|---|
| 215 | }
|
|---|
| 216 |
|
|---|
| 217 | const Index operator%(const int& rhs) const
|
|---|
| 218 | {
|
|---|
| 219 | return Index(*this) %= rhs;
|
|---|
| 220 | }
|
|---|
| 221 |
|
|---|
| 222 | const Vector operator+(const Vector& rhs) const;
|
|---|
| 223 | const Vector operator-(const Vector& rhs) const;
|
|---|
| 224 | const Vector operator*(const Vector& rhs) const;
|
|---|
| 225 | const Vector operator/(const Vector& rhs) const;
|
|---|
| 226 |
|
|---|
| 227 | const Vector operator+(const vmg_float& rhs) const;
|
|---|
| 228 | const Vector operator-(const vmg_float& rhs) const;
|
|---|
| 229 | const Vector operator*(const vmg_float& rhs) const;
|
|---|
| 230 | const Vector operator/(const vmg_float& rhs) const;
|
|---|
| 231 |
|
|---|
| 232 | private:
|
|---|
| 233 | int i[3];
|
|---|
| 234 | };
|
|---|
| 235 |
|
|---|
| 236 | inline const Index operator+(const int& lhs, const Index& rhs)
|
|---|
| 237 | {
|
|---|
| 238 | return Index(lhs) += rhs;
|
|---|
| 239 | }
|
|---|
| 240 |
|
|---|
| 241 | inline const Index operator-(const int& lhs, const Index& rhs)
|
|---|
| 242 | {
|
|---|
| 243 | return Index(lhs) -= rhs;
|
|---|
| 244 | }
|
|---|
| 245 |
|
|---|
| 246 | inline const Index operator*(const int& lhs, const Index& rhs)
|
|---|
| 247 | {
|
|---|
| 248 | return Index(lhs) *= rhs;
|
|---|
| 249 | }
|
|---|
| 250 |
|
|---|
| 251 | inline const Index operator/(const int& lhs, const Index& rhs)
|
|---|
| 252 | {
|
|---|
| 253 | return Index(lhs) /= rhs;
|
|---|
| 254 | }
|
|---|
| 255 |
|
|---|
| 256 | inline const Index operator%(const int& lhs, const Index& rhs)
|
|---|
| 257 | {
|
|---|
| 258 | return Index(lhs) %= rhs;
|
|---|
| 259 | }
|
|---|
| 260 |
|
|---|
| 261 | bool operator<(const Index& lhs, const Index& rhs);
|
|---|
| 262 | const Index Max(const Index& index1, const Index& index2);
|
|---|
| 263 | std::ostream& operator<<(std::ostream& out, const Index& base);
|
|---|
| 264 |
|
|---|
| 265 | }
|
|---|
| 266 |
|
|---|
| 267 | #endif /* INDEX_HPP_ */
|
|---|