Free Electron
TrianglePower.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 __geometry_TrianglePower_h__
8 #define __geometry_TrianglePower_h__
9 
10 namespace fe
11 {
12 namespace ext
13 {
14 
15 /**************************************************************************//**
16  @brief Evaluate barycenter on triangle of surface using MatrixPower
17 
18  @ingroup geometry
19 *//***************************************************************************/
20 template <typename T>
22 {
23  public:
24  TrianglePower(void) {}
25 
26  void configure(const Vector<3,T>& v0,
27  const Vector<3,T>& v1,
28  const Vector<3,T>& v2,
29  const Vector<3,T>& n0,
30  const Vector<3,T>& n1,
31  const Vector<3,T>& n2);
32 
33  void solve(const Barycenter<T>& barycenter,
34  Vector<3,T>& v,Vector<3,T>& n) const;
35 
36  void setIterations(U32 iterations)
37  { m_matrixPower.setIterations(iterations); }
38 
39  private:
40  void calcFrames(
41  Matrix<3,4,T>& a_rTransform1,
42  Matrix<3,4,T>& a_rTransform2,
43  const Vector<3,T>& a_rDirection,
44  const Vector<3,T>& a_rVertex1,
45  const Vector<3,T>& a_rNormal1,
46  const Vector<3,T>& a_rVertex2,
47  const Vector<3,T>& a_rNormal2) const;
48 
49  MatrixPower< Matrix<3,4,T> > m_matrixPower;
50 
51  Matrix<3,4,T> m_frame0;
52  Matrix<3,4,T> m_delta01;
53  Vector<3,T> m_direction;
54  Vector<3,T> m_v2;
55  Vector<3,T> m_n2;
56 };
57 
58 template <typename T>
59 inline void TrianglePower<T>::configure(
60  const Vector<3,T>& v0,
61  const Vector<3,T>& v1,
62  const Vector<3,T>& v2,
63  const Vector<3,T>& n0,
64  const Vector<3,T>& n1,
65  const Vector<3,T>& n2)
66 {
67  m_direction=unit(v1-v0);
68 
69  Matrix<3,4,T> frame1;
70  calcFrames(m_frame0,frame1,m_direction,v0,n0,v1,n1);
71  Matrix<3,4,T> inv0;
72  invert(inv0,m_frame0);
73  m_delta01=inv0*frame1;
74 
75  m_v2=v2;
76  m_n2=n2;
77 }
78 
79 template <typename T>
80 inline void TrianglePower<T>::solve(
81  const Barycenter<T>& barycenter,
82  Vector<3,T>& vertex,Vector<3,T>& normal) const
83 {
84  const T f01=(barycenter[0]+barycenter[1] > T(0))?
85  barycenter[1]/(barycenter[0]+barycenter[1]): T(0);
86  const T f2=T(1)-barycenter[0]-barycenter[1];
87 
88  Matrix<3,4,T> partial;
89  m_matrixPower.solve(partial,m_delta01,f01);
90  Matrix<3,4,T> mid=m_frame0*partial;
91  const Vector<3,T> mid01=mid.translation();
92  const Vector<3,T> norm01=mid.up();
93 
94  FEASSERT(magnitude(norm01)>0.99);
95  FEASSERT(magnitude(norm01)<1.01);
96 
97  Matrix<3,4,T> frame2;
98  Matrix<3,4,T> frame3;
99  calcFrames(frame2,frame3,m_direction,mid01,norm01,m_v2,m_n2);
100  Matrix<3,4,T> inv2;
101  invert(inv2,frame2);
102  Matrix<3,4,T> delta2=inv2*frame3;
103  m_matrixPower.solve(partial,delta2,f2);
104  mid=frame2*partial;
105  vertex=mid.translation();
106  normal=mid.up();
107 
108  FEASSERT(magnitude(normal)>0.99);
109  FEASSERT(magnitude(normal)<1.01);
110 }
111 
112 template <typename T>
113 inline void TrianglePower<T>::calcFrames(
114  Matrix<3,4,T>& a_rTransform1,Matrix<3,4,T>& a_rTransform2,
115  const Vector<3,T>& a_rDirection,
116  const Vector<3,T>& a_rVertex1,const Vector<3,T>& a_rNormal1,
117  const Vector<3,T>& a_rVertex2,const Vector<3,T>& a_rNormal2) const
118 {
119  a_rTransform1.left()=unit(cross(a_rNormal1,a_rDirection));
120  a_rTransform2.left()=unit(cross(a_rNormal2,a_rDirection));
121  a_rTransform1.direction()=cross(a_rTransform1.left(),a_rNormal1);
122  a_rTransform2.direction()=cross(a_rTransform2.left(),a_rNormal2);
123  a_rTransform1.up()=a_rNormal1;
124  a_rTransform2.up()=a_rNormal2;
125  a_rTransform1.translation()=a_rVertex1;
126  a_rTransform2.translation()=a_rVertex2;
127 
128 #if FALSE
129  feLog("a_rDirection\n%s\n",c_print(a_rDirection));
130  feLog("frame1\n%s\n",c_print(a_rTransform1));
131  feLog("frame2\n%s\n",c_print(a_rTransform2));
132 #endif
133 }
134 
135 } /* namespace ext */
136 } /* namespace fe */
137 
138 #endif /* __geometry_TrianglePower_h__ */
Matrix for 3D transformations.
Definition: Matrix3x4.h:47
kernel
Definition: namespace.dox:3
Matrix< 4, 4, T > & invert(Matrix< 4, 4, T > &a_inverted, const Matrix< 4, 4, T > &a_matrix)
4x4 full matrix inversion
Definition: Matrix.h:1033
Evaluate barycenter on triangle of surface using MatrixPower.
Definition: TrianglePower.h:21
Barycentric coordinates for a triangle.
Definition: Barycenter.h:26
solve B = A^^power, where A is a matrix
Definition: MatrixPower.h:37