Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ElmerCSC
GitHub Repository: ElmerCSC/elmerfem
Path: blob/devel/meshgen2d/src/include/coreGeometry.h
3203 views
1
#if !defined( MESH_GEOMETRY_H )
2
#define MESH_GEOMETRY_H
3
#include <math.h>
4
#include <float.h>
5
#include <algorithm>
6
7
#include "../../config.h"
8
#if defined(_NO_STD_MINMAX) || defined(WIN32)
9
#include "minmaxpatch.h"
10
#endif
11
12
inline
13
int inCircle(double x[4], double y[4])
14
{
15
double x10 = x[1] - x[0];
16
double x20 = x[2] - x[0];
17
double x30 = x[3] - x[0];
18
double x21 = x[2] - x[1];
19
double x31 = x[3] - x[1];
20
double y10 = y[1] - y[0];
21
double y20 = y[2] - y[0];
22
double y30 = y[3] - y[0];
23
double y21 = y[2] - y[1];
24
double y31 = y[3] - y[1];
25
26
double a1 = x10 * y20, b1 = x20 * y10;
27
double a2 = x10 * y30, b2 = x30 * y10;
28
double a3 = x20 * x21, b3 = y20 * y21;
29
double a4 = x30 * x31, b4 = y30 * y31;
30
31
double t1 = a1 - b1;
32
double t2 = a2 - b2;
33
double t3 = a3 + b3;
34
double t4 = a4 + b4;
35
double t14 = t1 * t4, t23 = t2 * t3;
36
double det = t14 - t23;
37
38
if (det >= 0.0)
39
return 1;
40
41
// rounding error evaluation:
42
// sum: a = b + c -> a_error <= b_error + c_error + max(|a|, |b|, |c|) * DBL_EPSILON
43
// product: a = b * c -> a_error <= b_error * |c| + c_error * |b| + b_error * c_error + |a| * DBL_EPSILON
44
// the term (b_error * c_error) is very small and can therefore be ignored
45
46
// maximum rounding error of coordinate differences
47
double maxcoord = std::max(std::max(std::max(fabs(x[0]), fabs(x[1])),
48
std::max(fabs(x[2]), fabs(x[3]))),
49
std::max(std::max(fabs(y[0]), fabs(y[1])),
50
std::max(fabs(y[2]), fabs(y[3]))));
51
52
// maximum rounding error of t1..t4
53
x10 = fabs(x10);
54
y10 = fabs(y10);
55
56
x20 = fabs(x20); y20 = fabs(y20);
57
a1 = fabs(a1); b1 = fabs(b1); t1 = fabs(t1);
58
59
double e1 = maxcoord * (x10 + x20 + y10 + y20) +
60
a1 + b1 + std::max(std::max(a1, b1), t1);
61
62
x30 = fabs(x30); y30 = fabs(y30);
63
a2 = fabs(a2); b2 = fabs(b2); t2 = fabs(t2);
64
65
double e2 = maxcoord * (x10 + x30 + y10 + y30) +
66
a2 + b2 + std::max(std::max(a2, b2), t2);
67
68
x21 = fabs(x21); y21 = fabs(y21);
69
a3 = fabs(a3); b3 = fabs(b3); t3 = fabs(t3);
70
71
double e3 = maxcoord * (x21 + x20 + y21 + y20) +
72
a3 + b3 + std::max(std::max(a3, b3), t3);
73
74
x31 = fabs(x31); y31 = fabs(y31);
75
a4 = fabs(a4); b4 = fabs(b4); t4 = fabs(t4);
76
77
double e4 = maxcoord * (x31 + x30 + y31 + y30) +
78
a4 + b4 + std::max(std::max(a4, b4), t4);
79
80
t14 = fabs(t14); t23 = fabs(t23);
81
// maximum rounding error of det
82
double eps = e1 * t4 + e4 * t1 + e2 * t3 + e3 * t2 +
83
t14 + t23 + std::max(std::max(t14, t23), fabs(det));
84
85
eps *= 2.0 * DBL_EPSILON;
86
87
if(det < -eps)
88
return 0;
89
90
return 1;
91
}
92
93
inline
94
double sideDet(double x1,double y1,double x2,double y2,double x3,double y3, double *err = NULL)
95
{
96
double d23 = x2 * y3, d32 = x3 * y2;
97
double d13 = x1 * y3, d31 = x3 * y1;
98
double d12 = x1 * y2, d21 = x2 * y1;
99
double d1 = d23 - d32, d2 = d13 - d31, d3 = d12 - d21;
100
101
if (err)
102
{
103
// maximum rounding errors of d1, d2 and d3
104
// product 1 product 2 difference
105
double e1 = fabs(d23) + fabs(d32) + std::max(std::max(fabs(d23), fabs(d32)), fabs(d1));
106
double e2 = fabs(d13) + fabs(d31) + std::max(std::max(fabs(d13), fabs(d31)), fabs(d2));
107
double e3 = fabs(d12) + fabs(d21) + std::max(std::max(fabs(d12), fabs(d21)), fabs(d3));
108
109
// maximum rounding error of sideDet
110
*err = (e1 + e2 + e3 + std::max(std::max(fabs(d1), fabs(d2)), fabs(d1 - d2)) +
111
std::max(std::max(fabs(d1 - d2), fabs(d3)), d1 - d2 + d3)) * 2.0 * DBL_EPSILON;
112
}
113
114
return d1 - d2 + d3;
115
}
116
117
inline
118
int orientation(double x1,double y1,double x2,double y2,double x3,double y3)
119
{
120
double val;
121
double eps;
122
val = sideDet(x1,y1,x2,y2,x3,y3, &eps);
123
124
if(val <= eps)
125
return 0;
126
return 1;
127
}
128
129
inline
130
double distance(double x1, double y1, double x2, double y2)
131
{
132
double dx = x2 - x1;
133
double dy = y2 - y1;
134
return sqrt(dx*dx + dy*dy);
135
}
136
137
inline
138
void vertexLocation(double p1x, double p1y, double p2x, double p2y,
139
double p3x, double p3y, double *vx, double *vy)
140
{
141
double a,b,c,d;
142
double r1,r2;
143
double det;
144
145
a = 2 * (p2x - p1x);
146
b = 2 * (p2y - p1y);
147
c = 2 * (p3x - p2x);
148
d = 2 * (p3y - p2y);
149
r1 = -(-p2x * p2x + p1x * p1x - p2y * p2y + p1y * p1y);
150
r2 = -(-p3x * p3x + p2x * p2x - p3y * p3y + p2y * p2y);
151
det = a * d - b * c;
152
*vx = (1 / det) * (d * r1 - b * r2);
153
*vy = (1 / det) * (-c * r1 + a * r2);
154
}
155
156
//#define MAP(a) (a)
157
//#define UNMAP(a) (a)
158
#define MAP(a) log(a)
159
#define UNMAP(a) exp(a)
160
161
#endif /* MESH_GEOMETRY_H */
162
163