BaseVector.hpp
Go to the documentation of this file.
1 
28 /*
29  * BaseVector.hpp
30  *
31  * @date 02.06.2017
32  * @author Lukas Kalbertodt <lukas.kalbertodt@gmail.com>
33  */
34 
35 #ifndef LVR2_GEOMETRY_BASEVECTOR_H_
36 #define LVR2_GEOMETRY_BASEVECTOR_H_
37 
38 // Eigen sometimes produces errors when compiled with CUDA. Disables
39 // all Eigen related function for CUDA code (which is currently fine).
40 #ifndef __NVCC__
41 #include <Eigen/Dense>
42 #endif
43 
44 #include <iostream>
45 
46 namespace lvr2
47 {
48 
49 template <typename> struct Normal;
50 
59 template <typename CoordT>
60 struct BaseVector
61 {
62 public:
63  using CoordType = CoordT;
64 
65  CoordT x;
66  CoordT y;
67  CoordT z;
68 
70  BaseVector() : x(0), y(0), z(0) {}
71 
73  BaseVector(const CoordT &x, const CoordT &y, const CoordT &z)
74  : x(x), y(y), z(z)
75  {
76  }
77 
78  BaseVector(const BaseVector& o) : x(o.x), y(o.y), z(o.z)
79  {
80  }
81  // ========================================================================
82  // === Named operations
83  // ========================================================================
84 
88  CoordT length() const;
89 
96  CoordT length2() const;
97 
101  CoordT distance(const BaseVector &other) const;
102 
109  CoordT distance2(const BaseVector &other) const;
110 
115  BaseVector<CoordT> cross(const BaseVector &other) const;
116 
121  BaseVector<CoordT> rotated(const BaseVector &n, const double &alpha) const;
122 
127  CoordT dot(const BaseVector &other) const;
128 
129 
130  void normalize()
131  {
132  // Check for invalid vector. This check can be disabled in release mode,
133  // which will probably lead to +inf and -inf values. In the documentation
134  // for this function we require the vector to not be the null-vector.
135  // assert(!(this->x == 0 && this->y == 0 && this->z == 0));
136  if(!(this->x == 0 && this->y == 0 && this->z == 0))
137  {
138  auto len = this->length();
139  this->x /= len;
140  this->y /= len;
141  this->z /= len;
142  }
143  }
144 
145 
146  // ========================================================================
147  // === Operator overloads
148  // ========================================================================
149 
151  BaseVector<CoordT> operator*(const CoordT &scale) const;
153  BaseVector<CoordT> operator/(const CoordT &scale) const;
154 
156  BaseVector<CoordT>& operator*=(const CoordT &scale);
158  BaseVector<CoordT>& operator/=(const CoordT &scale);
159 
161  BaseVector<CoordT> operator+(const BaseVector &other) const;
163  BaseVector<CoordT> operator-(const BaseVector &other) const;
164 
169 
170  CoordType distanceFrom(const BaseVector<CoordT> &other) const;
172 
179  template<typename CollectionT>
180  static BaseVector<CoordT> centroid(const CollectionT& points);
181 
182 
189  template<typename CollectionT>
190  static BaseVector<CoordT> average(const CollectionT& vecs);
191 
198  Normal<CoordT> normalized() const;
199 
200  bool operator==(const BaseVector &other) const;
201  bool operator!=(const BaseVector &other) const;
202 
203  CoordT operator*(const BaseVector<CoordType> &other) const;
204 
208  CoordT operator[](const unsigned& index) const;
209 
213  CoordT& operator[](const unsigned& index);
214 
215 // Eigen sometimes produces errors when compiled with CUDA. Disables
216 // all Eigen related function for CUDA code (which is currently fine).
217 #ifndef __NVCC__
218  // Friend declaration for Eigen multiplication
219  template<typename T, typename S>
220  friend BaseVector<T> operator*(const Eigen::Matrix<S, 4, 4>& mat, const BaseVector<T>& normal);
221 
222 #endif // ifndef __NVCC__
223 
224 };
225 
226 template<typename T>
227 std::ostream& operator<<( std::ostream& os, const BaseVector<T>& v)
228 {
229  os << "Vec: [" << v.x << " " << v.y << " " << v.z << "]" << std::endl;
230  return os;
231 }
232 
233 // Eigen sometimes produces errors when compiled with CUDA. Disables
234 // all Eigen related function for CUDA code (which is currently fine).
235 #ifndef __NVCC__
236 
248 template<typename CoordType, typename Scalar = CoordType>
249 inline BaseVector<CoordType> operator*(const Eigen::Matrix<Scalar, 4, 4>& mat, const BaseVector<CoordType>& normal)
250 {
251  // TODO: CHECK IF THIS IS CORRECT
252  CoordType x = mat(0, 0) * normal.x + mat(1, 0) * normal.y + mat(2, 0) * normal.z;
253  CoordType y = mat(0, 1) * normal.x + mat(1, 1) * normal.y + mat(2, 1) * normal.z;
254  CoordType z = mat(0, 2) * normal.x + mat(1, 2) * normal.y + mat(2, 2) * normal.z;
255 
256  x += mat(0, 3);
257  y += mat(1, 3);
258  z += mat(2, 3);
259 
260  return BaseVector<CoordType>(x,y,z);
261 }
262 
263 #endif // ifndef __NVCC__
264 
265 } // namespace lvr
266 
267 #include "lvr2/geometry/BaseVector.tcc"
268 
269 #endif /* LVR2_GEOMETRY_BASEVECTOR_H_ */
CoordT length() const
Returns the length of this vector.
BaseVector< CoordT > & operator-=(const BaseVector< CoordT > &other)
Element-wise subtraction.
static BaseVector< CoordT > centroid(const CollectionT &points)
Returns the centroid of all points in the given collection.
BaseVector(const BaseVector &o)
Definition: BaseVector.hpp:78
BaseVector< CoordT > operator-(const BaseVector &other) const
Element-wise subtraction.
BaseVector< CoordT > operator*(const CoordT &scale) const
Scalar multiplication.
BaseVector< CoordT > operator/(const CoordT &scale) const
Scalar division.
BaseVector< CoordT > & operator*=(const CoordT &scale)
Scalar multiplication.
BaseVector< CoordT > cross(const BaseVector &other) const
Calculates the cross product between this and the given vector. Returns a new BaseVector instance...
bool operator==(const BaseVector &other) const
A vector guaranteed to be normalized (length = 1).
Definition: BaseVector.hpp:49
BaseVector()
Default constructs a null-vector.
Definition: BaseVector.hpp:70
CoordT distance2(const BaseVector &other) const
Calculates the squared distance to another vector.
CoordT length2() const
Returns the squared length of this vector.
Normal< CoordT > normalized() const
Returns a normalized version of this vector.
CoordType squaredDistanceFrom(const BaseVector< CoordType > &other) const
bool operator!=(const BaseVector &other) const
BaseVector< CoordT > operator+(const BaseVector &other) const
Element-wise addition.
BaseVector(const CoordT &x, const CoordT &y, const CoordT &z)
Builds a BaseVector with the given coordinates.
Definition: BaseVector.hpp:73
CoordT distance(const BaseVector &other) const
Calculates the distance to another vector.
BaseVector< CoordT > & operator/=(const CoordT &scale)
Scalar division.
BaseVector< CoordT > rotated(const BaseVector &n, const double &alpha) const
Calculates the rotated vector around an normal vector n with the rotation angle alpha.
CoordT dot(const BaseVector &other) const
Calculates the dot product between this and the given vector.
static BaseVector< CoordT > average(const CollectionT &vecs)
Returns the average of all vectors in the given collection.
A generic, weakly-typed vector.
Definition: BaseVector.hpp:60
CoordType distanceFrom(const BaseVector< CoordT > &other) const
CoordT operator[](const unsigned &index) const
Indexed coordinate access (reading)
BaseVector< CoordT > & operator+=(const BaseVector< CoordT > &other)
Element-wise addition.


lvr2
Author(s): Thomas Wiemann , Sebastian Pütz , Alexander Mock , Lars Kiesow , Lukas Kalbertodt , Tristan Igelbrink , Johan M. von Behren , Dominik Feldschnieders , Alexander Löhr
autogenerated on Mon Feb 28 2022 22:46:06