Free Electron
fe_math.h
Go to the documentation of this file.
1 /* Copyright (C) 2003-2021 Free Electron Organization
2  Any use of this software requires a license. If a valid license
3  was not distributed with this file, visit freeelectron.org. */
4 
5 /** @file */
6 
7 #ifndef __pyfe_fe_math_h__
8 #define __pyfe_fe_math_h__
9 
10 template<class rawtype>
11 class MathType
12 {
13  public:
14  MathType(void) { m_pRaw = &m_raw; }
15 virtual ~MathType(void) { /* NOOP */ }
16  MathType(const MathType<rawtype> &other)
17  {
18  m_pRaw = &m_raw;
19  *m_pRaw = *(other.m_pRaw);
20  }
21 
22  MathType<rawtype> &operator=(const MathType<rawtype> &other)
23  {
24  *m_pRaw = *(other.m_pRaw);
25  return *this;
26  }
27  void assign(rawtype &raw) { m_pRaw = &raw; }
28  rawtype *rawptr(void) { return m_pRaw; }
29  rawtype *rawptr(void) const { return m_pRaw; }
30 
31 
32  protected:
33  rawtype *m_pRaw;
34  rawtype m_raw;
35 };
36 
37 
38 template<class rawtype, int N>
39 class MathArrayType : public MathType<rawtype>
40 {
41  public:
42 virtual ~MathArrayType(void) { }
43  float getitem(unsigned long index)
44  {
45  if(index >= N)
46  {
47  feX(fe::e_invalidRange,
48  "pyfe",
49  "array index out of range");
50  }
51  return (*m_pRaw)[index];
52  }
53  void setitem(unsigned long index, float value)
54  {
55  if(index >= N)
56  {
57  feX(fe::e_invalidRange,
58  "pyfe",
59  "array index out of range");
60  }
61 // (*m_pRaw)[index] = value;
62  fe::setAt(*m_pRaw,index,value);
63  }
64 };
65 
66 class Vector2f : public MathArrayType<fe::Vector2f, 2>
67 {
68  public:
69 };
70 
71 Vector2f operator*(const float &lhs, const Vector2f &rhs)
72 {
73  Vector2f r;
74  *(r.rawptr()) = *(rhs.rawptr()) * lhs;
75  return r;
76 }
77 
78 Vector2f operator*(const Vector2f &lhs, const float &rhs)
79 {
80  Vector2f r;
81  *(r.rawptr()) = *(lhs.rawptr()) * rhs;
82  return r;
83 }
84 
85 Vector2f operator+(const Vector2f &lhs, const Vector2f &rhs)
86 {
87  Vector2f r;
88  *(r.rawptr()) = *(lhs.rawptr()) + *(rhs.rawptr());
89  return r;
90 }
91 
92 Vector2f operator-(const Vector2f &lhs, const Vector2f &rhs)
93 {
94  Vector2f r;
95  *(r.rawptr()) = *(lhs.rawptr()) - *(rhs.rawptr());
96  return r;
97 }
98 
99 class Vector3f : public MathArrayType<fe::Vector3f, 3>
100 {
101  public:
102  Vector3f cross(Vector3f &rhs)
103  {
104  Vector3f r;
105  fe::cross3(*(r.m_pRaw),*m_pRaw,*(rhs.m_pRaw));
106  return r;
107  }
108  float magnitude(void)
109  {
110  return fe::magnitude(*m_pRaw);
111  }
112  Vector3f normalize(void)
113  {
114  fe::normalize(*m_pRaw);
115  return *this;
116  }
117 
118 };
119 
120 Vector3f operator*(const float &lhs, const Vector3f &rhs)
121 {
122  Vector3f r;
123  *(r.rawptr()) = *(rhs.rawptr()) * lhs;
124  return r;
125 }
126 
127 Vector3f operator*(const Vector3f &lhs, const float &rhs)
128 {
129  Vector3f r;
130  *(r.rawptr()) = *(lhs.rawptr()) * rhs;
131  return r;
132 }
133 
134 Vector3f operator+(const Vector3f &lhs, const Vector3f &rhs)
135 {
136  Vector3f r;
137  *(r.rawptr()) = *(lhs.rawptr()) + *(rhs.rawptr());
138  return r;
139 }
140 
141 Vector3f operator-(const Vector3f &lhs, const Vector3f &rhs)
142 {
143  Vector3f r;
144  *(r.rawptr()) = *(lhs.rawptr()) - *(rhs.rawptr());
145  return r;
146 }
147 
148 class Vector4f : public MathArrayType<fe::Vector4f, 4>
149 {
150  public:
151  Vector4f cross(Vector4f &rhs)
152  {
153  Vector4f r;
154  fe::cross3(*(r.m_pRaw),*m_pRaw,*(rhs.m_pRaw));
155  return r;
156  }
157 };
158 
159 Vector4f operator*(const float &lhs, const Vector4f &rhs)
160 {
161  Vector4f r;
162  *(r.rawptr()) = *(rhs.rawptr()) * lhs;
163  return r;
164 }
165 
166 Vector4f operator*(const Vector4f &lhs, const float &rhs)
167 {
168  Vector4f r;
169  *(r.rawptr()) = *(lhs.rawptr()) * rhs;
170  return r;
171 }
172 
173 Vector4f operator+(const Vector4f &lhs, const Vector4f &rhs)
174 {
175  Vector4f r;
176  *(r.rawptr()) = *(lhs.rawptr()) + *(rhs.rawptr());
177  return r;
178 }
179 
180 Vector4f operator-(const Vector4f &lhs, const Vector4f &rhs)
181 {
182  Vector4f r;
183  *(r.rawptr()) = *(lhs.rawptr()) - *(rhs.rawptr());
184  return r;
185 }
186 
187 
188 
189 #endif /* __pyfe_fe_math_h__ */
190