Honeycomb  0.1
Component-Model Framework
Vec1.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 
9 template<class Real, int Options>
10 struct matrix::priv::Traits<Vec<1,Real,Options>> : vec::priv::Traits<1,Real,Options,std::allocator<int8>>
11 {
13 };
14 
15 namespace vec { namespace priv
16 {
17  template<class Real, szt Align>
18  struct StorageFieldsMixin<Real, 1, Align>
19  {
20  operator Real() const { return x; }
22  };
23 } }
24 
25 namespace matrix { namespace priv
26 {
27  template<class R, int O>
28  void storageCopy(const R* a, Vec<1,R,O>& v) { v.x = a[0]; }
29  template<class R, int O>
30  void storageCopy(const Vec<1,R,O>& v, R* a) { a[0] = v.x; }
31 
32  template<class R, int O>
33  void storageFill(Vec<1,R,O>& v, R f) { v.x = f; }
34  template<class R, int O>
35  void storageFillZero(Vec<1,R,O>& v) { v.x = 0; }
36 
37  template<class R, int O>
38  bool storageEqual(const Vec<1,R,O>& lhs, const Vec<1,R,O>& rhs) { return lhs.x == rhs.x; }
39 } }
40 
42 template<class Real, int Options>
43 class Vec<1,Real,Options> : public VecBase<Vec<1,Real,Options>>
44 {
46 public:
47  using Super::dot;
48  using Super::operator*;
49  using Super::operator*=;
50  using Super::operator/;
51  using Super::operator/=;
52  using Super::x;
53 
55  Vec() {}
56  Vec(Real x) { this->x = x; }
58  template<class T>
59  Vec(const MatrixBase<T>& rhs) { operator=(rhs); }
60 
61  template<class T>
62  Vec& operator=(const MatrixBase<T>& rhs) { Super::operator=(rhs); return *this; }
63 
65  Vec operator* (int rhs) const { return operator*(Real(rhs)); }
66  Vec& operator*=(int rhs) { return operator*=(Real(rhs)); }
67  Vec operator/ (int rhs) const { return operator/(Real(rhs)); }
68  Vec& operator/=(int rhs) { return operator/=(Real(rhs)); }
69  friend Vec operator*(int lhs, const Vec& rhs) { return operator*(Real(lhs), rhs); }
70 
73  Real lengthSqr() const { return x*x; }
74  Real length() const { return x; }
75  Real dot(const Vec& v) const { return x*v.x; }
77 
78 public:
79  static const Vec zero;
80  static const Vec one;
81  static const Vec axisX;
82  static const Vec axis[1];
83 };
84 
85 template<class R, int O> const Vec<1,R,O> Vec<1,R,O>::zero (0);
86 template<class R, int O> const Vec<1,R,O> Vec<1,R,O>::one (1);
87 template<class R, int O> const Vec<1,R,O> Vec<1,R,O>::axisX (1);
88 template<class R, int O> const Vec<1,R,O> Vec<1,R,O>::axis[1] = { axisX };
89 
91 template<class R, int Opt>
94 struct priv::map_impl<Vec<1,R,Opt>, Vec<1,R,Opt>>
95 {
96  template<class T, class O, class Func>
97  static O&& func(T&& v, O&& o, Func&& f) { o.x = f(v.x); return forward<O>(o); }
98 };
99 
100 template<class R, int Opt>
101 struct priv::map_impl<Vec<1,R,Opt>, Vec<1,R,Opt>, Vec<1,R,Opt>>
102 {
103  template<class T, class T2, class O, class Func>
104  static O&& func(T&& v, T2&& rhs, O&& o, Func&& f) { o.x = f(v.x,rhs.x); return forward<O>(o); }
105 };
106 
107 template<class R, int O, class Accum_>
108 struct priv::reduce_impl<Vec<1,R,O>, Accum_>
109 {
110  template<class T, class Accum, class Func>
111  static Accum_ func(T&& v, Accum&& initVal, Func&& f) { return f(forward<Accum>(initVal), v.x); }
112 };
113 
114 template<class R, int O, class Accum_>
115 struct priv::reduce_impl<Vec<1,R,O>, Accum_, Vec<1,R,O>>
116 {
117  template<class T, class T2, class Accum, class Func>
118  static Accum_ func(T&& v, T2&& rhs, Accum&& initVal, Func&& f) { return f(forward<Accum>(initVal), v.x, rhs.x); }
119 };
121 
123 typedef Vec<1> Vec1;
127 
128 
130 template<class Real, int Options>
131 class Matrix<1,1,Real,Options> : public Vec<1,Real,Options>
132 {
133  typedef Vec<1,Real,Options> Super;
135 };
136 
137 }
(m x n)-dimensional matrix
Definition: Matrix.h:24
Automatic (stack-compatible) vector storage that allows direct access to dimension fields...
Definition: Storage.h:13
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
void storageFillZero(StorageBlock< T > &store)
Fill block storage with zeros.
Definition: Block.h:136
N-dimensional vector traits.
Definition: Vec.h:14
Real dot(const VecBase< T > &v) const
Vector dot product.
Definition: Base.h:91
bool storageEqual(const StorageBlock< T > &lhs, const StorageBlock< T2 > &rhs)
Test between block storages.
Definition: Block.h:157
static const Vec one
Definition: Vec1.h:80
Vec(Real x)
Definition: Vec1.h:56
Vec< 1, Float > Vec1_f
Definition: Vec1.h:125
static const Vec zero
Definition: Vec1.h:79
Vec< 1, Double > Vec1_d
Definition: Vec1.h:126
Real lengthSqr() const
Definition: Vec1.h:73
Real dot(const Vec &v) const
Definition: Vec1.h:75
Vec< 1 > Vec1
1D vector types
Definition: Vec1.h:124
Vec operator*(int rhs) const
Implicit conversion to real causes ambiguity with int.
Definition: Vec1.h:65
Real length() const
Definition: Vec1.h:74
MatrixS operator/(Real rhs) const
Definition: Base.h:184
N-dimensional vector.
Definition: Traits.h:12
Vec()
No init.
Definition: Vec1.h:55
friend Vec operator*(int lhs, const Vec &rhs)
Definition: Vec1.h:69
Definition: Traits.h:20
Vec & operator*=(int rhs)
Definition: Vec1.h:66
float Real
Real number type. See Real_ for real number operations and constants.
Definition: Real.h:21
Vector base class.
Definition: Base.h:11
Vec & 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: Vec.h:56
void storageFill(StorageBlock< T > &store, typename StorageBlock< T >::Real f)
Fill block storage with scalar.
Definition: Block.h:128
Vec operator/(int rhs) const
Definition: Vec1.h:67
Matrix< s_rows, T::s_cols, Real > operator*(const T &rhs) const
Multiply with another matrix. Returns a new matrix.
Definition: Base.h:178
1D vector
Definition: Vec1.h:43
Vec(const MatrixBase< T > &rhs)
Construct from vector of same dimension.
Definition: Vec1.h:59
void storageCopy(const StorageBlock< Src > &src, StorageBlock< Dst > &dst)
Copy between block and dense storages.
Definition: Block.h:84
static const Vec axisX
Definition: Vec1.h:81
#define MATRIX_VEC_ADAPTER
It's not possible to inherit ctors, so this macro is required.
Definition: Vec.h:71
Matrix base class.
Definition: Base.h:17
Vec & operator=(const MatrixBase< T > &rhs)
Definition: Vec1.h:62
Global Honeycomb namespace.
Vec & operator/=(int rhs)
Definition: Vec1.h:68
vec::priv::StorageFields< Vec< 1, Real, Options > > Storage
Definition: Vec1.h:12