Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ElmerCSC
GitHub Repository: ElmerCSC/elmerfem
Path: blob/devel/ElmerGUI/netgen/libsrc/gprim/geomfuncs.hpp
3206 views
1
#ifndef FILE_GEOMFUNCS
2
#define FILE_GEOMFUNCS
3
4
/* *************************************************************************/
5
/* File: geomfuncs.hpp */
6
/* Author: Joachim Schoeberl */
7
/* Date: 20. Jul. 02 */
8
/* *************************************************************************/
9
10
11
template <int D>
12
inline double Abs (const Vec<D> & v)
13
{
14
double sum = 0;
15
for (int i = 0; i < D; i++)
16
sum += v(i) * v(i);
17
return sqrt (sum);
18
}
19
20
21
template <int D>
22
inline double Abs2 (const Vec<D> & v)
23
{
24
double sum = 0;
25
for (int i = 0; i < D; i++)
26
sum += v(i) * v(i);
27
return sum;
28
}
29
30
31
32
template <int D>
33
inline double Dist (const Point<D> & a, const Point<D> & b)
34
{
35
return Abs (a-b);
36
}
37
38
template <int D>
39
inline double Dist2 (const Point<D> & a, const Point<D> & b)
40
{
41
return Abs2 (a-b);
42
}
43
44
45
template <int D>
46
inline Point<D> Center (const Point<D> & a, const Point<D> & b)
47
{
48
Point<D> res;
49
for (int i = 0; i < D; i++)
50
res(i) = 0.5 * (a(i) + b(i));
51
return res;
52
}
53
54
template <int D>
55
inline Point<D> Center (const Point<D> & a, const Point<D> & b, const Point<D> & c)
56
{
57
Point<D> res;
58
for (int i = 0; i < D; i++)
59
res(i) = (1.0/3.0) * (a(i) + b(i) + c(i));
60
return res;
61
}
62
63
template <int D>
64
inline Point<D> Center (const Point<D> & a, const Point<D> & b, const Point<D> & c, const Point<D> & d)
65
{
66
Point<D> res;
67
for (int i = 0; i < D; i++)
68
res(i) = (1.0/4.0) * (a(i) + b(i) + c(i) + d(i));
69
return res;
70
}
71
72
73
74
inline Vec<3> Cross (const Vec<3> & v1, const Vec<3> & v2)
75
{
76
return Vec<3>
77
( v1(1) * v2(2) - v1(2) * v2(1),
78
v1(2) * v2(0) - v1(0) * v2(2),
79
v1(0) * v2(1) - v1(1) * v2(0) );
80
}
81
82
83
inline double Determinant (const Vec<3> & col1,
84
const Vec<3> & col2,
85
const Vec<3> & col3)
86
{
87
return
88
col1(0) * ( col2(1) * col3(2) - col2(2) * col3(1)) +
89
col1(1) * ( col2(2) * col3(0) - col2(0) * col3(2)) +
90
col1(2) * ( col2(0) * col3(1) - col2(1) * col3(0));
91
}
92
93
94
95
template <>
96
inline Vec<2> Vec<2> :: GetNormal () const
97
{
98
return Vec<2> (-x[1], x[0]);
99
}
100
101
template <>
102
inline Vec<3> Vec<3> :: GetNormal () const
103
{
104
if (fabs (x[0]) > fabs (x[2]))
105
return Vec<3> (-x[1], x[0], 0);
106
else
107
return Vec<3> (0, x[2], -x[1]);
108
}
109
110
111
112
// template <int H, int W>
113
inline void CalcInverse (const Mat<2,2> & m, Mat<2,2> & inv)
114
{
115
double det = m(0,0) * m(1,1) - m(0,1) * m(1,0);
116
if (det == 0)
117
{
118
inv = 0;
119
return;
120
}
121
122
double idet = 1.0 / det;
123
inv(0,0) = idet * m(1,1);
124
inv(0,1) = -idet * m(0,1);
125
inv(1,0) = -idet * m(1,0);
126
inv(1,1) = idet * m(0,0);
127
}
128
129
void CalcInverse (const Mat<3,3> & m, Mat<3,3> & inv);
130
131
inline void CalcInverse (const Mat<2,3> & m, Mat<3,2> & inv)
132
{
133
Mat<2,2> a = m * Trans (m);
134
Mat<2,2> ainv;
135
CalcInverse (a, ainv);
136
inv = Trans (m) * ainv;
137
}
138
139
void CalcInverse (const Mat<3,2> & m, Mat<2,3> & inv);
140
141
inline void CalcInverse (const Mat<3,2> & m, Mat<2,3> & inv)
142
{
143
Mat<2,2> a = Trans (m) * m;
144
Mat<2,2> ainv;
145
CalcInverse (a, ainv);
146
inv = ainv * Trans (m);
147
}
148
149
150
double Det (const Mat<2,2> & m);
151
double Det (const Mat<3,3> & m);
152
153
// eigenvalues of a symmetric matrix
154
void EigenValues (const Mat<3,3> & m, Vec<3> & ev);
155
void EigenValues (const Mat<2,2> & m, Vec<3> & ev);
156
157
#endif
158
159