File: explicitcurve2d.cpp

package info (click to toggle)
netgen 6.2.2601%2Bdfsg1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,076 kB
  • sloc: cpp: 166,627; tcl: 6,310; python: 2,868; sh: 528; makefile: 90
file content (160 lines) | stat: -rw-r--r-- 3,165 bytes parent folder | download | duplicates (13)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include <mystdlib.h>
#include <csg.hpp>

namespace netgen
{
ExplicitCurve2d :: ExplicitCurve2d ()
  {
    ;
  }
  
  
void ExplicitCurve2d :: Project (Point<2> & p) const
  {
  double t;
  t = ProjectParam (p);
  p = Eval (t);
  }

double ExplicitCurve2d :: NumericalProjectParam (const Point<2> & p, double lb, double ub) const
  {
  double t(-1);
  Vec<2> tan;
  Vec<2> curv;
  Point<2> cp;
  double f, fl, fu;
  int cnt;
  
  tan = EvalPrime (lb);
  cp = Eval (lb);
  fl = tan * (cp - p);
  if (fl > 0)			// changed by wmf, originally fl >= 0
    {
      //      cerr << "tan = " << tan << " cp - p = " << (cp - p) << endl;
      //      cerr << "ExplicitCurve2d::NumericalProject: lb wrong" << endl;
      return 0;
    }
  
  tan = EvalPrime (ub);
  cp = Eval (ub);
  fu = tan * (cp - p);
  if (fu < 0)			// changed by wmf, originally fu <= 0
    {
      //    cerr << "tan = " << tan << " cp - p = " << (cp - p) << endl;
      //    cerr << "ExplicitCurve2d::NumericalProject: ub wrong" << endl;
    return 0;
    }
    
  cnt = 0;
  while (ub - lb > 1e-12 && fu - fl > 1e-12)
    {
    cnt++;
    if (cnt > 50)
      {
      (*testout) << "Num Proj, cnt = " << cnt << endl;
      }
     
    t = (lb * fu - ub * fl) / (fu - fl);
    if (t > 0.9 * ub + 0.1 * lb) t = 0.9 * ub + 0.1 * lb;
    if (t < 0.1 * ub + 0.9 * lb) t = 0.1 * ub + 0.9 * lb;
    
    tan = EvalPrime (t);
    cp = Eval (t);
    f = tan * (cp - p);
    
    if (f >= 0)
      {
      ub = t;
      fu = f;
      }
    else
      {
      lb = t;
      fl = f;
      }
    }
    
  return t;
  }


Vec<2> ExplicitCurve2d :: Normal (double t) const
{
  Vec<2> tan = EvalPrime (t);
  tan.Normalize();
  return Vec<2> (tan(1), -tan(0));
}


void ExplicitCurve2d :: NormalVector (const Point<2> & p, Vec<2> & n) const
  {
  double t = ProjectParam (p);
  n = Normal (t);
  }


Point<2> ExplicitCurve2d :: CurvCircle (double t) const
  {
  Point<2> cp;
  Vec<2> tan, n, curv;
  double den;
  
  cp = Eval (t);
  tan = EvalPrime (t);
  n = Normal (t);
  curv = EvalPrimePrime (t);
  
  den = n * curv;
  if (fabs (den) < 1e-12)
    return cp + 1e12 * n;  
    
  return cp + (tan.Length2() / den) * n;  
  }


double ExplicitCurve2d :: MaxCurvature () const
  {
  double t, tmin, tmax, dt;
  double curv;
  Vec<2> tan;
  double maxcurv;

  maxcurv = 0;  
  
  tmin = MinParam ();
  tmax = MaxParam ();
  dt = (tmax - tmin) / 1000;
  for (t = tmin; t <= tmax+dt; t += dt)
    if (SectionUsed (t))
      {
      tan = EvalPrime (t);
      curv = fabs ( (Normal(t) * EvalPrimePrime(t)) / tan.Length2());
      if (curv > maxcurv) maxcurv = curv; 
      }
  return maxcurv;
  }  
  
double ExplicitCurve2d :: MaxCurvatureLoc (const Point<2> & p, double rad) const
  {
  double t, tmin, tmax, dt;
  double curv;
  Vec<2> tan;
  double maxcurv;

  maxcurv = 0;  
  
  tmin = MinParam ();
  tmax = MaxParam ();
  dt = (tmax - tmin) / 1000;
  for (t = tmin; t <= tmax+dt; t += dt)
    if (Dist (Eval(t), p) < rad)
      {
      tan = EvalPrime (t);
      curv = fabs ( (Normal(t) * EvalPrimePrime(t)) / tan.Length2());
      if (curv > maxcurv) maxcurv = curv; 
      }
    
  return maxcurv;
  }  
  
}