Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ElmerCSC
GitHub Repository: ElmerCSC/elmerfem
Path: blob/devel/ElmerGUI/netgen/libsrc/csg/explicitcurve2d.cpp
3206 views
1
#include <mystdlib.h>
2
#include <csg.hpp>
3
4
namespace netgen
5
{
6
ExplicitCurve2d :: ExplicitCurve2d ()
7
{
8
;
9
}
10
11
12
void ExplicitCurve2d :: Project (Point<2> & p) const
13
{
14
double t;
15
t = ProjectParam (p);
16
p = Eval (t);
17
}
18
19
double ExplicitCurve2d :: NumericalProjectParam (const Point<2> & p, double lb, double ub) const
20
{
21
double t(-1);
22
Vec<2> tan;
23
Vec<2> curv;
24
Point<2> cp;
25
double f, fl, fu;
26
int cnt;
27
28
tan = EvalPrime (lb);
29
cp = Eval (lb);
30
fl = tan * (cp - p);
31
if (fl > 0) // changed by wmf, originally fl >= 0
32
{
33
// cerr << "tan = " << tan << " cp - p = " << (cp - p) << endl;
34
// cerr << "ExplicitCurve2d::NumericalProject: lb wrong" << endl;
35
return 0;
36
}
37
38
tan = EvalPrime (ub);
39
cp = Eval (ub);
40
fu = tan * (cp - p);
41
if (fu < 0) // changed by wmf, originally fu <= 0
42
{
43
// cerr << "tan = " << tan << " cp - p = " << (cp - p) << endl;
44
// cerr << "ExplicitCurve2d::NumericalProject: ub wrong" << endl;
45
return 0;
46
}
47
48
cnt = 0;
49
while (ub - lb > 1e-12 && fu - fl > 1e-12)
50
{
51
cnt++;
52
if (cnt > 50)
53
{
54
(*testout) << "Num Proj, cnt = " << cnt << endl;
55
}
56
57
t = (lb * fu - ub * fl) / (fu - fl);
58
if (t > 0.9 * ub + 0.1 * lb) t = 0.9 * ub + 0.1 * lb;
59
if (t < 0.1 * ub + 0.9 * lb) t = 0.1 * ub + 0.9 * lb;
60
61
tan = EvalPrime (t);
62
cp = Eval (t);
63
f = tan * (cp - p);
64
65
if (f >= 0)
66
{
67
ub = t;
68
fu = f;
69
}
70
else
71
{
72
lb = t;
73
fl = f;
74
}
75
}
76
77
return t;
78
}
79
80
81
Vec<2> ExplicitCurve2d :: Normal (double t) const
82
{
83
Vec<2> tan = EvalPrime (t);
84
tan.Normalize();
85
return Vec<2> (tan(1), -tan(0));
86
}
87
88
89
void ExplicitCurve2d :: NormalVector (const Point<2> & p, Vec<2> & n) const
90
{
91
double t = ProjectParam (p);
92
n = Normal (t);
93
}
94
95
96
Point<2> ExplicitCurve2d :: CurvCircle (double t) const
97
{
98
Point<2> cp;
99
Vec<2> tan, n, curv;
100
double den;
101
102
cp = Eval (t);
103
tan = EvalPrime (t);
104
n = Normal (t);
105
curv = EvalPrimePrime (t);
106
107
den = n * curv;
108
if (fabs (den) < 1e-12)
109
return cp + 1e12 * n;
110
111
return cp + (tan.Length2() / den) * n;
112
}
113
114
115
double ExplicitCurve2d :: MaxCurvature () const
116
{
117
double t, tmin, tmax, dt;
118
double curv;
119
Vec<2> tan;
120
double maxcurv;
121
122
maxcurv = 0;
123
124
tmin = MinParam ();
125
tmax = MaxParam ();
126
dt = (tmax - tmin) / 1000;
127
for (t = tmin; t <= tmax+dt; t += dt)
128
if (SectionUsed (t))
129
{
130
tan = EvalPrime (t);
131
curv = fabs ( (Normal(t) * EvalPrimePrime(t)) / tan.Length2());
132
if (curv > maxcurv) maxcurv = curv;
133
}
134
return maxcurv;
135
}
136
137
double ExplicitCurve2d :: MaxCurvatureLoc (const Point<2> & p, double rad) const
138
{
139
double t, tmin, tmax, dt;
140
double curv;
141
Vec<2> tan;
142
double maxcurv;
143
144
maxcurv = 0;
145
146
tmin = MinParam ();
147
tmax = MaxParam ();
148
dt = (tmax - tmin) / 1000;
149
for (t = tmin; t <= tmax+dt; t += dt)
150
if (Dist (Eval(t), p) < rad)
151
{
152
tan = EvalPrime (t);
153
curv = fabs ( (Normal(t) * EvalPrimePrime(t)) / tan.Length2());
154
if (curv > maxcurv) maxcurv = curv;
155
}
156
157
return maxcurv;
158
}
159
160
}
161
162