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