Honeycomb  0.1
Component-Model Framework
Base.h
Go to the documentation of this file.
1 // Honeycomb, Copyright (C) 2015 NewGamePlus Inc. Distributed under the Boost Software License v1.0.
2 #pragma once
3 
5 
6 namespace honey
7 {
8 
10 template<class Subclass>
11 class VecBase : public MatrixBase<Subclass>
12 {
14 protected:
15  using typename Super::Alge;
16 
17 public:
18  typedef Subclass VecS;
19  using typename Super::Real;
20  using typename Super::Real_;
21  using Super::s_rows;
22  using Super::s_cols;
23  using Super::s_size;
24  using Super::subc;
25  using Super::fromZero;
26  using Super::m;
27  using Super::size;
28  using Super::resize;
29 
31  VecS& fromAxis(sdt i) { this->assertIndex(i); fromZero(); m(i) = 1; return subc(); }
32 
34  template<class T>
36  {
37  typedef MatrixBase<T> Rhs;
38  static_assert( Rhs::s_rows == matrix::dynamic || Rhs::s_rows == 1 ||
39  Rhs::s_cols == matrix::dynamic || Rhs::s_cols == 1, "Can only assign a row or column vector");
40  static_assert( s_size == matrix::dynamic || Rhs::s_size == matrix::dynamic || s_size == Rhs::s_size,
41  "Can only assign to vector of the same size");
42  assert(rhs.rows() == 1 || rhs.cols() == 1, "Can only assign a row or column vector");
43 
44  resize(rhs.size());
45  matrix::priv::storageCopy(rhs.subc(), subc());
46  return *this;
47  }
48 
50  template<sdt Dim>
52  segment(sdt i, sdt dim = -1) { return vec::priv::Segment<VecS,Dim>::create(subc(), i, dim); }
53 
54  template<sdt Dim>
56  segment(sdt i, sdt dim = -1) const { return vec::priv::Segment<const VecS,Dim>::create(subc(), i, dim); }
57 
60  segment(sdt i, sdt dim) { return vec::priv::Segment<VecS>::create(subc(), i, dim); }
61 
63  segment(sdt i, sdt dim) const { return vec::priv::Segment<const VecS>::create(subc(), i, dim); }
64 
66 
69  VecS& resize(sdt dim) { return s_cols == 1 ? resize(dim, 1) : resize(1, dim); }
70 
72  Real lengthSqr() const { return reduce(subc(), Real(0), [](Real a, Real e) { return a + Alge::sqr(e); }); }
74  Real length() const { return Alge::sqrt(subc().lengthSqr()); }
75 
77  VecS normalize(optional<Real&> len = optnull) const
78  {
79  Real l = length();
80  if (l > Real_::zeroTol)
81  {
82  if (len) len = l;
83  return subc() / l;
84  }
85  if (len) len = 0;
86  return VecS().resize(size()).fromZero();
87  }
88 
90  template<class T>
91  Real dot(const VecBase<T>& v) const { return reduce(subc(), v.subc(), Real(0), [](Real a, Real e, Real rhs) { return a + e*rhs; }); }
92 
93  friend ostream& operator<<(ostream& os, const VecBase& val)
94  {
95  if (val.size() > 1) os << "[";
96  for (sdt i = 0; i < val.size(); ++i)
97  {
98  if (i != 0) os << ", ";
99  os << val[i];
100  }
101  return val.size() > 1 ? os << "]" : os;
102  }
103 };
104 
105 
106 namespace matrix { namespace priv
107 {
109  template<class Vec, sdt Cols>
110  struct Traits<Block<Vec,1,Cols>> : BlockTraits<Vec,1,Cols>
111  {
113  using typename Super::Subclass;
115  };
116 
118  template<class Vec, sdt Rows>
119  struct Traits<Block<Vec,Rows,1>> : BlockTraits<Vec,Rows,1>
120  {
122  using typename Super::Subclass;
124  };
125 
127  template<class Vec>
128  struct Traits<Block<Vec,1,1>> : BlockTraits<Vec,1,1>
129  {
131  using typename Super::Subclass;
133  };
134 } }
135 
136 }
137 
static const sdt dynamic
Definition: Traits.h:23
VecBase & operator=(const MatrixBase< T > &rhs)
Assign to row or column vector of any dimension. Asserts that if this vector has a fixed dimension th...
Definition: Base.h:35
VecBase< Subclass > Base
Definition: Base.h:123
static type create(Vec &v, sdt i, sdt dim=-1)
Definition: Block.h:220
Real dot(const VecBase< T > &v) const
Vector dot product.
Definition: Base.h:91
BlockTraits< Vec, 1, 1 > Super
Definition: Base.h:130
static optnull_t optnull
Null optional, use to reset an optional to an uninitialized state or test for initialization.
Definition: Optional.h:12
const Real & m(sdt i) const
Access matrix element at index. Wrapper for convenience only, more readable than (*this)(i) ...
Definition: Base.h:567
VecS normalize(optional< Real & > len=optnull) const
Get unit vector. The pre-normalized length will be returned in len if specified.
Definition: Base.h:77
ptrdiff_t sdt
Size difference type, shorthand for ptrdiff_t.
Definition: Core.h:92
Numeral< Real >::Real_ Real_
Definition: Base.h:25
VecBase< Subclass > Base
Definition: Base.h:132
Definition: Block.h:166
VecS & fromAxis(sdt i)
Initialize with unit axis (all zeros except for a one at index i)
Definition: Base.h:31
Accum reduce(Range &&, Seqs &&..., Accum &&initVal, Func &&)
Accumulate a series of sequences into an output.
static Real sqrt(Real x)
Square Root.
Definition: Alge.h:63
Real length() const
Get length (magnitude) of vector.
Definition: Base.h:74
Alge_< Real > Alge
Definition: Base.h:28
Matrix block view.
Definition: Block.h:188
friend ostream & operator<<(ostream &os, const VecBase &val)
Definition: Base.h:93
VecBase< Subclass > Base
Definition: Base.h:114
vec::priv::Segment< VecS >::type segment(sdt i, sdt dim)
Get dynamic segment at offset i with dimension dim
Definition: Base.h:60
#define assert(...)
Forwards to assert_#args. See assert_1(), assert_2().
Definition: Debug.h:24
N-dimensional vector.
Definition: Traits.h:12
vec::priv::Segment< const VecS, Dim >::type segment(sdt i, sdt dim=-1) const
Definition: Base.h:56
Definition: Traits.h:20
BlockTraits< Vec, 1, Cols > Super
Definition: Base.h:112
BlockTraits< Vec, Rows, 1 > Super
Definition: Base.h:121
float Real
Real number type. See Real_ for real number operations and constants.
Definition: Real.h:21
MatrixS & resize(sdt rows, sdt cols)
Sets number of rows/columns and reallocates only if the size has changed (rows*cols). All previous data is lost on reallocation. Returns self.
Definition: Base.h:277
Enables any type to be optional so it can exist in an uninitialized null state.
Definition: Optional.h:52
MatrixS & fromZero()
Zero all elements.
Definition: Base.h:70
vec::priv::Segment< VecS, Dim >::type segment(sdt i, sdt dim=-1)
Get segment at offset i with dimension dim. If dimension is fixed then dim may be left unspecified...
Definition: Base.h:52
Vector base class.
Definition: Base.h:11
vec::priv::Segment< const VecS >::type segment(sdt i, sdt dim) const
Definition: Base.h:63
static Num sqr(Num x)
Square.
Definition: Alge.h:60
int size(const StdContainer &cont)
Safely get the size of a std container as a signed integer.
Definition: StdUtil.h:19
Subclass VecS
Definition: Base.h:18
void storageCopy(const StorageBlock< Src > &src, StorageBlock< Dst > &dst)
Copy between block and dense storages.
Definition: Block.h:84
Real lengthSqr() const
Get the square of the length.
Definition: Base.h:72
Matrix base class.
Definition: Base.h:17
Global Honeycomb namespace.
VecS & resize(sdt dim)
Sets number of dimensions and reallocates only if different. All previous data is lost on reallocatio...
Definition: Base.h:69