Path: blob/devel/ElmerGUI/netgen/libsrc/csg/explicitcurve2d.cpp
3206 views
#include <mystdlib.h>1#include <csg.hpp>23namespace netgen4{5ExplicitCurve2d :: ExplicitCurve2d ()6{7;8}91011void ExplicitCurve2d :: Project (Point<2> & p) const12{13double t;14t = ProjectParam (p);15p = Eval (t);16}1718double ExplicitCurve2d :: NumericalProjectParam (const Point<2> & p, double lb, double ub) const19{20double t(-1);21Vec<2> tan;22Vec<2> curv;23Point<2> cp;24double f, fl, fu;25int cnt;2627tan = EvalPrime (lb);28cp = Eval (lb);29fl = tan * (cp - p);30if (fl > 0) // changed by wmf, originally fl >= 031{32// cerr << "tan = " << tan << " cp - p = " << (cp - p) << endl;33// cerr << "ExplicitCurve2d::NumericalProject: lb wrong" << endl;34return 0;35}3637tan = EvalPrime (ub);38cp = Eval (ub);39fu = tan * (cp - p);40if (fu < 0) // changed by wmf, originally fu <= 041{42// cerr << "tan = " << tan << " cp - p = " << (cp - p) << endl;43// cerr << "ExplicitCurve2d::NumericalProject: ub wrong" << endl;44return 0;45}4647cnt = 0;48while (ub - lb > 1e-12 && fu - fl > 1e-12)49{50cnt++;51if (cnt > 50)52{53(*testout) << "Num Proj, cnt = " << cnt << endl;54}5556t = (lb * fu - ub * fl) / (fu - fl);57if (t > 0.9 * ub + 0.1 * lb) t = 0.9 * ub + 0.1 * lb;58if (t < 0.1 * ub + 0.9 * lb) t = 0.1 * ub + 0.9 * lb;5960tan = EvalPrime (t);61cp = Eval (t);62f = tan * (cp - p);6364if (f >= 0)65{66ub = t;67fu = f;68}69else70{71lb = t;72fl = f;73}74}7576return t;77}787980Vec<2> ExplicitCurve2d :: Normal (double t) const81{82Vec<2> tan = EvalPrime (t);83tan.Normalize();84return Vec<2> (tan(1), -tan(0));85}868788void ExplicitCurve2d :: NormalVector (const Point<2> & p, Vec<2> & n) const89{90double t = ProjectParam (p);91n = Normal (t);92}939495Point<2> ExplicitCurve2d :: CurvCircle (double t) const96{97Point<2> cp;98Vec<2> tan, n, curv;99double den;100101cp = Eval (t);102tan = EvalPrime (t);103n = Normal (t);104curv = EvalPrimePrime (t);105106den = n * curv;107if (fabs (den) < 1e-12)108return cp + 1e12 * n;109110return cp + (tan.Length2() / den) * n;111}112113114double ExplicitCurve2d :: MaxCurvature () const115{116double t, tmin, tmax, dt;117double curv;118Vec<2> tan;119double maxcurv;120121maxcurv = 0;122123tmin = MinParam ();124tmax = MaxParam ();125dt = (tmax - tmin) / 1000;126for (t = tmin; t <= tmax+dt; t += dt)127if (SectionUsed (t))128{129tan = EvalPrime (t);130curv = fabs ( (Normal(t) * EvalPrimePrime(t)) / tan.Length2());131if (curv > maxcurv) maxcurv = curv;132}133return maxcurv;134}135136double ExplicitCurve2d :: MaxCurvatureLoc (const Point<2> & p, double rad) const137{138double t, tmin, tmax, dt;139double curv;140Vec<2> tan;141double maxcurv;142143maxcurv = 0;144145tmin = MinParam ();146tmax = MaxParam ();147dt = (tmax - tmin) / 1000;148for (t = tmin; t <= tmax+dt; t += dt)149if (Dist (Eval(t), p) < rad)150{151tan = EvalPrime (t);152curv = fabs ( (Normal(t) * EvalPrimePrime(t)) / tan.Length2());153if (curv > maxcurv) maxcurv = curv;154}155156return maxcurv;157}158159}160161162