Itasca C++ Interface
avect.h
Go to the documentation of this file.
1 #pragma once
9 #include "vect.h"
10 
12 
29 namespace AVector2Util {
30  static const double nullValue_ = 0.0;
31 }
32 
34 
43 template <class T> class AVector2 {
44 public:
45  // Creators
47 #ifdef _DEBUG
48  AVector2() { z_ = std::numeric_limits<T>::max(); }
49 #else
50 PUSHWARNING
51 VSWARNING(26495)// Uninitialize in VS code analysis - on purpose
52  AVector2() {}
53 POPWARNING
54 #endif
56  AVector2(const AVector2 &av) : z_(av.z_) { }
58  explicit AVector2(const T &t) : z_(t) { }
60  explicit AVector2(const Vector2<T> &) : z_(0) { }
62  explicit AVector2(const Vector3<T> &v) : z_(v.z_) { }
63  constexpr const AVector2<T> &operator=(const AVector2<T> &v) { z_ = v.z_; return *this; }
64 
66  const T &x() const { return AVector2Util::nullValue_; }
68  const T &y() const { return AVector2Util::nullValue_; }
70  const T &z() const { return z_; }
72  const T &dof(uint32 u) const {
73 #ifdef _DEBUG
74  if (u>2) throw std::runtime_error("Vector index out of range.");
75 #endif
76  if (u==2) return z_;
77  return AVector2Util::nullValue_;
78  }
79 
81  T &rz() { return z_; }
83  T &rdof([[maybe_unused]] uint32 u) {
84 #ifdef _DEBUG
85  if (u!=2) throw std::runtime_error("Vector index out of range.");
86 #endif
87  return z_;
88  }
89 
90  const T &operator[](uint32 u) const { return dof(u); }
91  T &operator[](uint32 u) { return rdof(u); }
92 
93  T fsum() const { return std::abs(z_); }
94  T mag2() const { return z_*z_; }
95  T mag() const { return std::abs(z_); }
96  T area() const { return 0; }
97  T volume() const { return 0; }
98  AVector2<T> unit() const { AVector2<T> v(*this); if (z_) { v.z_ = z_ < static_cast<T>(0) ? static_cast<T>(-1) : static_cast<T>(1); } return v; }
99  AVector2<T> abs() const { return std::abs(z_); }
100  void fill(const T &d) { z_=d; }
101  T maxComp() const { return std::max(0.0,z_); }
102  T minComp() const { return std::min(0.0,z_); }
103 
104  bool operator==(const AVector2<T> &av) const { return z_==av.z_; }
105  bool operator!=(const AVector2<T> &av) const { return z_!=av.z_; }
106  bool operator<(const AVector2<T> &av) const { return z_ < av.z_; }
107  bool operator>(const AVector2<T> &av) const { return z_ > av.z_; }
108 
109  const AVector2<T> &operator+=(const AVector2<T> &v) { z_ += v.z_; return *this; }
110  const AVector2<T> &operator-=(const AVector2<T> &v) { z_ -= v.z_; return *this; }
111  const AVector2<T> &operator*=(const AVector2<T> &v) { z_ *= v.z_; return *this; }
112  const AVector2<T> &operator*=(const T &t) { z_ *= t; return *this; }
113  const AVector2<T> &operator/=(const AVector2<T> &v) { z_ /= v.z_; return *this; }
114  const AVector2<T> &operator/=(const T &t) { z_ /= t; return *this; }
115 
116  AVector2<T> operator+(const AVector2<T> &av) const { AVector2<T> out(z_+av.z_); return out; }
117  AVector2<T> operator-(const AVector2<T> &av) const { AVector2<T> out(z_-av.z_); return out; }
118  AVector2<T> operator*(const AVector2<T> &av) const { AVector2<T> out(z_*av.z_); return out; }
119  AVector2<T> operator*(const T &t) const { AVector2<T> out(z_*t); return out; }
120  AVector2<T> operator/(const AVector2<T> &av) const { AVector2<T> out(z_/av.z_); return out; }
121  AVector2<T> operator/(const T &t) const { AVector2<T> out(z_/t); return out; }
122 
123  T operator&(const AVector2<T> &) const { return 0; }
124  Vector2<T> operator&(const Vector2<T> &v) const { Vector2<T> out(-z_*v.y(),z_*v.x()); return out; }
125  Vector3<T> operator&(const Vector3<T> &v) const { Vector3<T> out(-z_*v.y(),z_*v.x(),0); return out; }
126 
127  T operator|(const AVector2<T> &v) const { return z_*v.z(); }
128  T operator|(const Vector2<T> &) const { return 0; }
129  T operator|(const Vector3<T> &v) const { return z_*v.z(); }
130 
131  Vector2<T> toVector2() const { return Vector2<T>(0); }
132  Vector3<T> toVector3() const { return Vector3<T>(0,0,z_); }
133 
134 private:
135  T z_;
136 };
137 
140 template <class T> inline Vector2<T> operator&(const Vector2<T> &v,const AVector2<T> &av) {
141  Vector2<T> out(v.y()*av.z(),-v.x()*av.z());
142  return out;
143 }
144 
147 template <class T> inline Vector3<T> operator&(const Vector3<T> &v,const AVector2<T> &av) {
148  Vector3<T> out(v.y()*av.z(),-v.x()*av.z(),0);
149  return out;
150 }
151 
154 template <class T> inline AVector2<T> operator&(const Vector2<T> &v1,const Vector2<T> &v2) {
155  AVector2<T> out((v1.x()*v2.y()) - (v1.y()*v2.x()));
156  return out;
157 }
158 
161 template <class T> inline T operator|(const Vector2<T> &,const AVector2<T> &) { return 0; }
162 
163 
164 
165 
167 
176 template <class T> class AVector3 : public Vector3<T> {
177 public:
178  using Vector3<T>::Vector3;
179  AVector3() : Vector3<T>() { }
180  AVector3(const Vector3<T> &v) : Vector3<T>(v) { }
181  AVector3(const AVector3<T> &v) : Vector3<T>(v) { }
183  explicit AVector3(const AVector2<T> &v) : Vector3<T>(0,0,v.z()) { }
184  constexpr const AVector3<T> &operator=(const AVector3<T> &v) { Vector3<T>::operator=(v); return *this; }
185 
187  Vector2<T> toVector2() const { return Vector2<T>(this->x(),this->y()); }
189  const Vector3<T> &toVector3() const { return *this; }
190 };
191 
192 // Naming convention for most commonly used types.
193 typedef AVector2<double> DAVect2;
194 typedef AVector2<float> FAVect2;
195 typedef AVector2<int32> IAVect2;
196 typedef AVector2<uint32> UAVect2;
197 
198 typedef AVector3<double> DAVect3;
199 typedef AVector3<float> FAVect3;
200 typedef AVector3<int32> IAVect3;
201 typedef AVector3<uint32> UAVect3;
202 
203 // Conversion routines.
206 template <class T> inline DAVect2 toDAVect2(const AVector2<T> &v) { DAVect2 dv2(to<double>(v.z())); return dv2; }
209 template <class T> inline FAVect2 toFAVect2(const AVector2<T> &v) { FAVect2 fv2(to<float>(v.z())); return fv2; }
212 template <class T> inline IAVect2 toIAVect2(const AVector2<T> &v) { IAVect2 iv2(to<int32>(v.z())); return iv2; }
215 template <class T> inline UAVect2 toUAVect2(const AVector2<T> &v) { UAVect2 uv2(to<uint32>(v.z())); return uv2; }
216 
219 template <class T> inline DAVect3 toDAVect3(const Vector3<T> &v) {
220  DAVect3 dv3(to<double>(v.x()),to<double>(v.y()),to<double>(v.z()));
221  return dv3;
222 }
223 
226 template <class T> inline FAVect3 toFAVect3(const Vector3<T> &v) {
227  FAVect3 fv3(to<float>(v.x()),to<float>(v.y()),to<float>(v.z()));
228  return fv3;
229 }
230 
233 template <class T> inline IAVect3 toAIVect3(const Vector3<T> &v) {
234  IAVect3 iv3(to<int32>(v.x()),to<int32>(v.y()),to<int32>(v.z()));
235  return iv3;
236 }
237 
240 template <class T> inline UAVect3 toAUVect3(const Vector3<T> &v) {
241  UAVect3 uv3(to<uint32>(v.x()),to<uint32>(v.y()),to<uint32>(v.z()));
242  return uv3;
243 }
244 
247 template <class T> inline Vector2<T> toVect2(const AVector2<T> &) { return Vector2<T>(0); }
250 template <class T> inline Vector3<T> toVect3(const AVector2<T> &v) { return Vector3<T>(0,0,v.z()); }
253 template <class T> inline Vector2<T> toVect2(const AVector3<T> &v) { return Vector2<T>(v.x(),v.y()); }
256 template <class T> inline const Vector3<T> &toVect3(const AVector3<T> &v) { return v; }
257 
260 template <class T> inline const AVector2<T> &toAVect2(const AVector2<T> &v) { return v; }
264 template <class T> inline AVector2<T> toAVect2(const AVector3<T> &v) { return AVector2<T>(v.z()); }
268 template <class T> inline AVector3<T> toAVect3(const AVector2<T> &v,const T &x=0,const T &y=0) { return Vector3<T>(x,y,v.z()); }
271 template <class T> inline const AVector3<T> &toAVect3(const AVector3<T> &v) { return v; }
272 
275 template <class T> inline AVector3<T> vmax(const AVector3<T> &v1,const AVector3<T> &v2) {
276  AVector3<T> out(std::max<T>(v1.x(),v2.x()),std::max<T>(v1.y(),v2.y()),std::max<T>(v1.z(),v2.z()));
277  return out;
278 }
279 
282 template <class T> inline AVector3<T> vmin(const AVector3<T> &v1,const AVector3<T> &v2) {
283  AVector3<T> out(std::min<T>(v1.x(),v2.x()),std::min<T>(v1.y(),v2.y()),std::min<T>(v1.z(),v2.z()));
284  return out;
285 }
286 
289 template <class T> inline AVector3<T> vsign(const AVector3<T> &v1,const AVector3<T> &v2) {
290  AVector3<T> out(v2.x() < 0 ? -qAbs(v1.x()) : qAbs(v1.x()), v2.y() < 0 ? -qAbs(v1.y()) : qAbs(v1.y()), v2.z() < 0 ? -qAbs(v1.z()) : qAbs(v1.z()));
291  return out;
292 }
293 
296 template <class T> inline AVector3<T> vceil(const AVector3<T> &v) {
297  AVector3<T> out(ceil(v.x()),ceil(v.y()),ceil(v.z()));
298  return out;
299 }
300 
303 template <class T> inline AVector2<T> vmax(const AVector2<T> &v1,const AVector2<T> &v2) {
304  AVector2<T> out(std::max<T>(v1.z(),v2.z()));
305  return out;
306 }
307 
310 template <class T> inline AVector2<T> vmin(const AVector2<T> &v1,const AVector2<T> &v2) {
311  AVector2<T> out(std::min<T>(v1.z(),v2.z()));
312  return out;
313 }
314 
317 template <class T> inline AVector2<T> vsign(const AVector2<T> &v1,const AVector2<T> &v2) {
318  AVector2<T> out(v2.z() < 0 ? -qAbs(v1.z()) : qAbs(v1.z()));
319  return out;
320 }
321 
324 template <class T> inline AVector2<T> vceil(const AVector2<T> &v) {
325  AVector2<T> out(ceil(v.z()));
326  return out;
327 }
328 
329 namespace base {
330  template <class T>
331  inline string ts(const AVector2<T> &t, int width=0, char notation = '\0', int precision = -1, char fill = ' ') {
332  return ts(t.z(), width, notation, precision, fill);
333  }
334 
335  template <class T>
336  inline string ts(const AVector3<T> &t, int width=0, char notation = '\0', int precision = -1, char fill = ' ') {
337  return ts(toVect3(t), width, notation, precision, fill);
338  }
339 }
340 
342 // EOF
2D Angular vector class.
Definition: avect.h:43
AVector2< T > operator-(const AVector2< T > &av) const
Binary math operator - require temp (till C++0x)
Definition: avect.h:117
T operator|(const Vector3< T > &v) const
Dot product with a Vector3.
Definition: avect.h:129
AVector2< T > operator+(const AVector2< T > &av) const
Binary math operator - require temp (till C++0x)
Definition: avect.h:116
T operator|(const Vector2< T > &) const
Dot product with a Vector2 returns 0.0.
Definition: avect.h:128
T fsum() const
The Manhatten norm.
Definition: avect.h:93
bool operator>(const AVector2< T > &av) const
comparison operator
Definition: avect.h:107
T & rz()
Returns a reference to the z component, cannot reference access x or y.
Definition: avect.h:81
AVector2(const T &t)
Explicit conversion constructor, the z component is initialized to t.
Definition: avect.h:58
const AVector2< T > & operator-=(const AVector2< T > &v)
in-place math operator
Definition: avect.h:110
const AVector2< T > & operator*=(const AVector2< T > &v)
in-place math operator
Definition: avect.h:111
bool operator==(const AVector2< T > &av) const
comparison operator
Definition: avect.h:104
const T & operator[](uint32 u) const
General degree-of-freedom access. If u is 2 returns the z component, otherwise returns 0....
Definition: avect.h:90
bool operator<(const AVector2< T > &av) const
comparison operator
Definition: avect.h:106
AVector2(const Vector2< T > &)
Explicit conversion constructor, the z component is set to 0, the x and y values of v are lost.
Definition: avect.h:60
T operator|(const AVector2< T > &v) const
Dot product.
Definition: avect.h:127
T mag() const
The magnitude of the vector.
Definition: avect.h:95
T & operator[](uint32 u)
Returns a reference to the z component if u=2, any other value of u is an error.
Definition: avect.h:91
const AVector2< T > & operator/=(const T &t)
in-place math operator
Definition: avect.h:114
const AVector2< T > & operator/=(const AVector2< T > &v)
in-place math operator
Definition: avect.h:113
AVector2(const Vector3< T > &v)
Explicit convertion contructor, the z component or v is copied but he x and y values are lost.
Definition: avect.h:62
POPWARNING AVector2(const AVector2 &av)
Copy constructor.
Definition: avect.h:56
PUSHWARNING VSWARNING(26495) AVector2()
Default constructor, no data initialization.
Definition: avect.h:51
bool operator!=(const AVector2< T > &av) const
comparison operator
Definition: avect.h:105
AVector2< T > operator*(const T &t) const
Binary math operator - require temp (till C++0x)
Definition: avect.h:119
const T & x() const
Member access - the x and y values are always uniquely 0 for a 2D angular vector.
Definition: avect.h:66
const T & y() const
Member access - the x and y values are always uniquely 0 for a 2D angular vector.
Definition: avect.h:68
T mag2() const
The square of the magnitude, or the vector dotted with itself.
Definition: avect.h:94
const T & dof(uint32 u) const
General degree-of-freedom access. If u is 2 returns the z component, otherwise returns 0....
Definition: avect.h:72
void fill(const T &d)
Fill all components with d.
Definition: avect.h:100
Vector3< T > toVector3() const
Converts to a Vector3(0,0,z)
Definition: avect.h:132
Vector3< T > operator&(const Vector3< T > &v) const
Cross product with a Vector3 produces a Vector3 (z=0)
Definition: avect.h:125
Vector2< T > toVector2() const
Converts to a Vector2(0,0)
Definition: avect.h:131
T operator&(const AVector2< T > &) const
Cross product with another AVector2 will always return 0.0.
Definition: avect.h:123
AVector2< T > operator*(const AVector2< T > &av) const
Binary math operator - require temp (till C++0x)
Definition: avect.h:118
const AVector2< T > & operator+=(const AVector2< T > &v)
in-place math operator
Definition: avect.h:109
T & rdof([[maybe_unused]] uint32 u)
Returns a reference to the z component if u=2, any other value of u is an error.
Definition: avect.h:83
T volume() const
Volume represented by x*y*z, always returns 0.0.
Definition: avect.h:97
Vector2< T > operator&(const Vector2< T > &v) const
Cross product with a Vector2 produces a Vector2.
Definition: avect.h:124
const AVector2< T > & operator*=(const T &t)
in-place math operator
Definition: avect.h:112
const T & z() const
Member access - returns the z component of the 2D angular vector.
Definition: avect.h:70
AVector2< T > operator/(const T &t) const
Binary math operator - require temp (till C++0x)
Definition: avect.h:121
T minComp() const
Returns the minimum component (assuming x and y are 0.0)
Definition: avect.h:102
AVector2< T > operator/(const AVector2< T > &av) const
Binary math operator - require temp (till C++0x)
Definition: avect.h:120
T maxComp() const
Returns the maximum component (assuming x and y are 0.0)
Definition: avect.h:101
T area() const
Area represented by x*y, always returns 0.0.
Definition: avect.h:96
AVector2< T > unit() const
returns a unit vector.
Definition: avect.h:98
AVector2< T > abs() const
Returns a vector of absolute values of components.
Definition: avect.h:99
3D Angular vector class.
Definition: avect.h:176
const Vector3< T > & toVector3() const
Converts to a Vector3 directly.
Definition: avect.h:189
AVector3(const AVector2< T > &v)
Explicit conversion contructor from an AVector2, x=0, y=0, z=v.z.
Definition: avect.h:183
Vector2< T > toVector2() const
Converts to a Vector2, the z component is lost.
Definition: avect.h:187
Vector2< T > toVect2(const AVector3< T > &v)
Definition: avect.h:253
AVector2< T > vceil(const AVector2< T > &v)
Definition: avect.h:324
IAVect2 toIAVect2(const AVector2< T > &v)
Definition: avect.h:212
DAVect3 toDAVect3(const Vector3< T > &v)
Definition: avect.h:219
AVector3< T > vsign(const AVector3< T > &v1, const AVector3< T > &v2)
Definition: avect.h:289
const AVector2< T > & toAVect2(const AVector2< T > &v)
Definition: avect.h:260
UAVect2 toUAVect2(const AVector2< T > &v)
Definition: avect.h:215
AVector2< T > vsign(const AVector2< T > &v1, const AVector2< T > &v2)
Definition: avect.h:317
UAVect3 toAUVect3(const Vector3< T > &v)
Definition: avect.h:240
FAVect2 toFAVect2(const AVector2< T > &v)
Definition: avect.h:209
Vector3< T > operator&(const Vector3< T > &v, const AVector2< T > &av)
Definition: avect.h:147
AVector3< T > vceil(const AVector3< T > &v)
Definition: avect.h:296
AVector3< T > toAVect3(const AVector2< T > &v, const T &x=0, const T &y=0)
Definition: avect.h:268
Vector2< T > operator&(const Vector2< T > &v, const AVector2< T > &av)
Definition: avect.h:140
AVector2< T > vmin(const AVector2< T > &v1, const AVector2< T > &v2)
Definition: avect.h:310
DAVect2 toDAVect2(const AVector2< T > &v)
Definition: avect.h:206
FAVect3 toFAVect3(const Vector3< T > &v)
Definition: avect.h:226
AVector2< T > toAVect2(const AVector3< T > &v)
Definition: avect.h:264
constexpr Vector3< T > toVect3(const Vector2< T > &v, const T &t=0)
Conversion between vectors of different dimension.
Definition: vect.h:341
Vector2< T > toVect2(const AVector2< T > &)
Definition: avect.h:247
AVector2< T > vmax(const AVector2< T > &v1, const AVector2< T > &v2)
Definition: avect.h:303
AVector3< T > vmax(const AVector3< T > &v1, const AVector3< T > &v2)
Definition: avect.h:275
Vector3< T > toVect3(const AVector2< T > &v)
Definition: avect.h:250
const Vector3< T > & toVect3(const AVector3< T > &v)
Definition: avect.h:256
IAVect3 toAIVect3(const Vector3< T > &v)
Definition: avect.h:233
AVector3< T > vmin(const AVector3< T > &v1, const AVector3< T > &v2)
Definition: avect.h:282
const AVector3< T > & toAVect3(const AVector3< T > &v)
Definition: avect.h:271
T operator|(const Vector2< T > &, const AVector2< T > &)
Definition: avect.h:161
AVector2< T > operator&(const Vector2< T > &v1, const Vector2< T > &v2)
Definition: avect.h:154
2D Angular vector class.
Definition: avect.h:29
2D and 3D vector utility classes.