Ravelin
SharedVectorN.h
1 /****************************************************************************
2  * Copyright 2013 Evan Drumwright
3  * This library is distributed under the terms of the Apache V2.0
4  * License (obtainable from http://www.apache.org/licenses/LICENSE-2.0).
5  ****************************************************************************/
6 
7 #ifndef SHAREDVECTORN
8 #error This class is not to be included by the user directly. Use SharedVectorNff.h or SharedVectorNd.h instead.
9 #endif
10 
12 class VECTORN;
13 
16 {
17  friend class VECTORN;
18  friend class MATRIXN;
19  friend class MATRIX2;
20  friend class MATRIX3;
21  friend class CONST_SHAREDVECTORN;
22  friend class SHAREDMATRIXN;
23  friend class CONST_SHAREDMATRIXN;
24 
25  public:
26  SHAREDVECTORN();
27  SHAREDVECTORN(const SHAREDVECTORN& source) { reset_from(source); }
28  SHAREDVECTORN(unsigned len, unsigned inc, unsigned start, SharedResizable<REAL> data);
29  void reset_from(const SHAREDVECTORN& source);
30  virtual ~SHAREDVECTORN() {}
31  SHAREDVECTORN& normalize() { assert(norm() > EPS); operator*=((REAL) 1.0/norm()); return *this; }
32  unsigned size() const { return _len; }
33  static REAL norm(const SHAREDVECTORN& v) { return std::sqrt(norm_sq(v)); }
34  static REAL norm_sq(const SHAREDVECTORN& v) { return SHAREDVECTORN::dot(v, v); }
35  REAL norm_inf() const { return norm_inf(*this); }
36  REAL norm1() const { return norm1(*this); }
37  REAL norm() const { return norm(*this); }
38  REAL norm_sq() const { return norm_sq(*this); }
39  SHAREDVECTORN segment(unsigned start, unsigned end);
40  CONST_SHAREDVECTORN segment(unsigned start, unsigned end) const;
41  SHAREDVECTORN& resize(unsigned N, bool preserve = false);
42  SHAREDVECTORN& operator=(const ORIGIN2& source);
43  SHAREDVECTORN& operator=(const ORIGIN3& source);
44  SHAREDVECTORN& operator=(const VECTOR3& source);
45  SHAREDVECTORN& operator=(const SHAREDVECTORN& source);
47  SHAREDVECTORN& operator=(const VECTORN& source);
48  SHAREDVECTORN& operator/=(REAL scalar) { return operator*=((REAL) 1.0/scalar); }
49  REAL* data() { return _data.get() + _start; }
50  const REAL* data() const { return _data.get() + _start; }
51  SHAREDVECTORN& resize(unsigned m, unsigned n, bool preserve = false) { if (n != 1) throw MissizeException(); return resize(m, preserve); }
52 
54  void reset() { _data.reset(); _start = _inc = _len = 0; }
55 
56  unsigned rows() const { return _len; }
57  unsigned columns() const { return 1; }
58  unsigned leading_dim() const { return _len; }
59  unsigned inc() const { return _inc; }
60 
61  // inline code specific to SharedVectorN
62  #include "SharedVectorN.inl"
63 
64  // inline code for VectorN/SharedVectorN
65  #define XVECTORN SHAREDVECTORN
66  #include "XVectorN.inl"
67  #undef XVECTORN
68 
69  protected:
70  SharedResizable<REAL> _data;
71  unsigned _start;
72  unsigned _inc;
73  unsigned _len;
74 }; // end class
75 
78 {
79  friend class VECTORN;
80  friend class MATRIXN;
81  friend class MATRIX2;
82  friend class MATRIX3;
83  friend class SHAREDVECTORN;
84  friend class SHAREDMATRIXN;
85  friend class CONST_SHAREDMATRIXN;
86 
87  public:
89  CONST_SHAREDVECTORN(unsigned len, unsigned inc, unsigned start, SharedResizable<REAL> data);
90  CONST_SHAREDVECTORN(const SHAREDVECTORN& source) { reset_from(source); }
91  CONST_SHAREDVECTORN(const CONST_SHAREDVECTORN& source) { reset_from(source); }
92  const SHAREDVECTORN get() const;
93  void reset_from(const SHAREDVECTORN& source);
94  void reset_from(const CONST_SHAREDVECTORN& source);
95  virtual ~CONST_SHAREDVECTORN() {}
96  unsigned size() const { return _len; }
97  static REAL norm(const CONST_SHAREDVECTORN& v) { return std::sqrt(norm_sq(v)); }
98  static REAL norm_sq(const CONST_SHAREDVECTORN& v) { return CONST_SHAREDVECTORN::dot(v, v); }
99  REAL norm_inf() const { return norm_inf(*this); }
100  REAL norm1() const { return norm1(*this); }
101  REAL norm() const { return norm(*this); }
102  REAL norm_sq() const { return norm_sq(*this); }
103  CONST_SHAREDVECTORN segment(unsigned start, unsigned end) const;
104  CONST_SHAREDVECTORN& resize(unsigned N, bool preserve = false);
105  const REAL* data() const { return _data.get() + _start; }
106  CONST_SHAREDVECTORN& resize(unsigned m, unsigned n, bool preserve = false)
107  {
108  #ifndef NEXCEPT
109  if (n != 1)
110  throw MissizeException();
111  #endif
112  return resize(m, preserve);
113  }
114 
116  void reset() { _data.reset(); _start = _inc = _len = 0; }
117  unsigned rows() const { return _len; }
118  unsigned columns() const { return 1; }
119  unsigned leading_dim() const { return _len; }
120  unsigned inc() const { return _inc; }
121 
122  // inline code specific to SharedVectorN
123  #include "ConstSharedVectorN.inl"
124 
125  protected:
126  SharedResizable<REAL> _data;
127  unsigned _start;
128  unsigned _inc;
129  unsigned _len;
130 }; // end class
131 
132 std::ostream& operator<<(std::ostream& out, const SHAREDVECTORN& v);
133 std::istream& operator>>(std::istream& in, SHAREDVECTORN& v);
134 std::ostream& operator<<(std::ostream& out, const CONST_SHAREDVECTORN& v);
135 
static REAL dot(const V1 &v1, const V2 &v2)
Computes the dot-product between two vectors.
Definition: SharedVectorN.h:247
SHAREDVECTORN & operator=(const ORIGIN2 &source)
Copies another vector.
Definition: SharedVectorN.cpp:80
void reset_from(const SHAREDVECTORN &source)
Constructs a shared vector from another shared vector.
Definition: SharedVectorN.cpp:27
A general 2x2 matrix.
Definition: Matrix2.h:16
SHAREDVECTORN segment(unsigned start, unsigned end)
Gets a shared subvector of this subvector.
Definition: SharedVectorN.cpp:36
CONST_SHAREDVECTORN & resize(unsigned N, bool preserve=false)
Does nothing.
Definition: SharedVectorN.cpp:246
SHAREDVECTORN & resize(unsigned N, bool preserve=false)
Does nothing.
Definition: SharedVectorN.cpp:68
A generic, possibly non-square matrix.
Definition: MatrixN.h:18
CONST_SHAREDVECTORN()
Default constructor - constructs an empty vector.
Definition: SharedVectorN.cpp:181
A generic N-dimensional floating point vector.
Definition: SharedVectorN.h:77
A generic, possibly non-square matrix using shared data.
Definition: SharedMatrixN.h:59
CONST_SHAREDVECTORN segment(unsigned start, unsigned end) const
Gets a shared subvector of this subvector.
Definition: SharedVectorN.cpp:230
A generic N-dimensional floating point vector.
Definition: SharedVectorN.h:15
void reset_from(const SHAREDVECTORN &source)
Constructs a shared vector from another shared vector.
Definition: SharedVectorN.cpp:221
static REAL dot(const V1 &v1, const V2 &v2)
Computes the dot-product between two vectors.
Definition: SharedVectorN.h:606
void reset()
Resets the shared structure.
Definition: SharedVectorN.h:116
A generic N-dimensional floating point vector.
Definition: VectorN.h:16
COLUMN_ITERATOR end()
Returns the column iterator.
Definition: SharedVectorN.h:330
A three-dimensional floating point vector used for representing points and vectors in 3D with associa...
Definition: Vector3.h:15
A three-dimensional floating point vector used for representing points and vectors in 3D and without ...
Definition: Origin3.h:16
A generic, possibly non-square matrix using constant shared data.
Definition: SharedMatrixN.h:19
A two-dimensional floating point vector used for computational geometry calculations and without asso...
Definition: Origin2.h:14
SHAREDVECTORN()
Default constructor - constructs an empty vector.
Definition: SharedVectorN.cpp:10
A 3x3 matrix that may be used for orientation, inertia tensors, etc.
Definition: Matrix3.h:20
void reset()
Resets the shared structure.
Definition: SharedVectorN.h:54
XVECTORN & operator*=(REAL scalar)
Multiplies this vector in place by a scalar.
Definition: SharedVectorN.h:393