1 // Created on: 1998-07-02
2 // Created by: Stephanie HUMEAU
3 // Copyright (c) 1998-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
6 // This file is part of Open CASCADE Technology software library.
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
17 #include <GeomFill_GuideTrihedronPlan.ixx>
20 #include <gp_Pnt2d.hxx>
21 //#include <gp_Trsf2d.hxx>
22 //#include <Bnd_Box2d.hxx>
25 #include <Adaptor3d_Curve.hxx>
26 #include <GeomAdaptor_HCurve.hxx>
27 #include <GeomAdaptor_HSurface.hxx>
29 #include <Geom_Plane.hxx>
31 #include <IntCurveSurface_IntersectionPoint.hxx>
32 #include <IntCurveSurface_HInter.hxx>
34 #include <GeomFill_Frenet.hxx>
35 #include <GeomFill_PlanFunc.hxx>
37 #include <math_Vector.hxx>
39 #include <math_FunctionRoot.hxx>
40 #include <math_Matrix.hxx>
42 #include <Precision.hxx>
45 #include <DrawTrSurf.hxx>
49 static void TracePlan(const Handle(Geom_Surface)& /*Plan*/)
51 cout << "Pas d'intersection Guide/Plan" << endl;
53 char* Temp = "ThePlan" ;
54 DrawTrSurf::Set(Temp, Plan);
55 // DrawTrSurf::Set("ThePlan", Plan);
60 //==================================================================
61 //Function: InGoodPeriod
62 //Purpose : Recadre un paramtere
63 //==================================================================
64 static void InGoodPeriod(const Standard_Real Prec,
65 const Standard_Real Period,
66 Standard_Real& Current)
68 Standard_Real Diff=Current-Prec;
69 Standard_Integer nb = (Standard_Integer ) IntegerPart(Diff/Period);
72 if (Diff > Period/2) Current -= Period;
73 else if (Diff < -Period/2) Current += Period;
76 //=======================================================================
77 //function : GuideTrihedronPlan
78 //purpose : Constructor
79 //=======================================================================
80 GeomFill_GuideTrihedronPlan::GeomFill_GuideTrihedronPlan (const Handle(Adaptor3d_HCurve)& theGuide) :
84 myStatus(GeomFill_PipeOk)
87 myGuide = theGuide; // guide
89 myNbPts = 20; // nb points pour calculs
90 Pole = new (TColgp_HArray2OfPnt2d)(1,1,1,myNbPts);//tab pr stocker Pprime (pt sur guide)
91 frenet = new (GeomFill_Frenet)();
93 XTol(1) = myGuide->Resolution(1.e-6);
96 //=======================================================================
98 //purpose : calcule myNbPts points sur la courbe guide (<=> normale)
99 //=======================================================================
100 void GeomFill_GuideTrihedronPlan::Init()
102 myStatus = GeomFill_PipeOk;
105 // Box.Update(-0.1, -0.1, 0.1, 0.1); // Taille minimal
106 gp_Vec Tangent,Normal,BiNormal;
108 Standard_Real t, DeltaG, w = 0.;
109 Standard_Real f = myCurve->FirstParameter();
110 Standard_Real l = myCurve->LastParameter();
114 Handle(Geom_Plane) Plan;
115 Handle(GeomAdaptor_HSurface) Pl;
116 IntCurveSurface_IntersectionPoint PInt;
117 IntCurveSurface_HInter Int;
118 frenet->SetCurve(myCurve);
119 DeltaG = (myGuide->LastParameter() - myGuide->FirstParameter())/2;
121 Inf(1) = myGuide->FirstParameter() - DeltaG;
122 Sup(1) = myGuide->LastParameter() + DeltaG;
124 if (!myGuide->IsPeriodic()) {
125 myTrimG = myGuide->Trim(myGuide->FirstParameter()- DeltaG/100,
126 myGuide->LastParameter() + DeltaG/100,
132 // Standard_Real Step = DeltaG/100;
134 for (ii=1; ii<=myNbPts; ii++)
136 t = Standard_Real(myNbPts - ii)*f + Standard_Real(ii - 1)*l;
139 frenet->D0(t, Tangent, Normal, BiNormal);
140 Plan = new (Geom_Plane) (P, Tangent);
141 Pl = new(GeomAdaptor_HSurface) (Plan);
143 Int.Perform(myTrimG, Pl); // intersection plan / guide
144 if (Int.NbPoints() == 0) {
148 w = (fabs(myGuide->LastParameter() -w) > fabs(myGuide->FirstParameter()-w) ? myGuide->FirstParameter() : myGuide->LastParameter());
150 myStatus = GeomFill_PlaneNotIntersectGuide;
158 Standard_Real Dmin = P.Distance(Pmin);
159 for (Standard_Integer jj=2;jj<=Int.NbPoints();jj++)
161 Pmin = Int.Point(jj).Pnt();
162 if (P.Distance(Pmin) < Dmin)
164 PInt = Int.Point(jj);
165 Dmin = P.Distance(Pmin);
172 Standard_Real Diff = w - Pole->Value(1, ii-1).Y();
173 if (Abs(Diff) > DeltaG) {
174 if (myGuide->IsPeriodic()) {
175 InGoodPeriod (Pole->Value(1, ii-1).Y(),
176 myGuide->Period(), w);
178 Diff = w - Pole->Value(1, ii-1).Y();
183 if (Abs(Diff) > DeltaG) {
184 cout << "Trihedron Plan Diff on Guide : " <<
190 gp_Pnt2d p1(t, w); // on stocke les parametres
191 Pole->SetValue(1, ii, p1);
196 //=======================================================================
197 //function : SetCurve
198 //purpose : calculation of trihedron
199 //=======================================================================
200 void GeomFill_GuideTrihedronPlan::SetCurve(const Handle(Adaptor3d_HCurve)& C)
203 if (!myCurve.IsNull()) Init();
206 //=======================================================================
208 //purpose : calculation of trihedron
209 //=======================================================================
211 Handle(Adaptor3d_HCurve) GeomFill_GuideTrihedronPlan::Guide()const
216 //=======================================================================
218 //purpose : calculation of trihedron
219 //=======================================================================
220 Standard_Boolean GeomFill_GuideTrihedronPlan::D0(const Standard_Real Param,
228 myCurve->D0(Param, P);
230 frenet->D0(Param,Tangent,Normal,BiNormal);
232 //initialisation de la recherche
235 Standard_Integer Iter = 50;
237 // fonction dont il faut trouver la racine : G(W)-Pl(U,V)=0
238 GeomFill_PlanFunc E(P, Tangent, myGuide);
241 math_FunctionRoot Result(E, X(1), XTol(1),
242 Inf(1), Sup(1), Iter);
246 Standard_Real Res = Result.Root();
247 // R = Result.Root(); // solution
249 Pprime = myTrimG->Value(Res); // pt sur courbe guide
250 gp_Vec n (P, Pprime); // vecteur definissant la normale du triedre
252 Normal = n.Normalized();
253 BiNormal = Tangent.Crossed(Normal);
254 BiNormal.Normalized();
259 // plan ortho a la trajectoire pour determiner Pprime
260 Handle(Geom_Plane) Plan = new (Geom_Plane)(P, Tangent);
263 myStatus = GeomFill_PlaneNotIntersectGuide;
264 return Standard_False;
267 return Standard_True;
270 //=======================================================================
272 //purpose : calculation of trihedron and first derivative
273 //=======================================================================
274 Standard_Boolean GeomFill_GuideTrihedronPlan::D1(const Standard_Real Param,
282 // return Standard_False;
288 // triedre de frenet sur la trajectoire
289 myCurve->D1(Param, P, To);
290 frenet->D1(Param,Tangent,DTangent,Normal,DNormal,BiNormal,DBiNormal);
294 Standard_Integer Iter = 50;
296 // fonction dont il faut trouver la racine : G(W)-Pl(U,V)=0
298 GeomFill_PlanFunc E(P, Tangent, myGuide);
301 math_FunctionRoot Result(E, X(1), XTol(1),
302 Inf(1), Sup(1), Iter);
306 Standard_Real Res = Result.Root();
307 // R = Result.Root(); // solution
308 myTrimG->D1(Res, PG, TG);
309 gp_Vec n (P, PG), dn; // vecteur definissant la normale du triedre
310 Standard_Real Norm = n.Magnitude();
318 BiNormal = Tangent.Crossed(Normal);
320 // derivee premiere du triedre
321 Standard_Real dedx, dedt, dtg_dt;
322 E.Derivative(Res, dedx);
323 E.DEDT(Res, To, DTangent, dedt);
327 /* Standard_Real h=1.e-7, e, etg, etc;
330 if ( Abs( (etg-e)/h - dedx) > 1.e-4) {
331 cout << "err :" << (etg-e)/h - dedx << endl;
335 myCurve->D0(Param+h, pdbg);
336 frenet->D0(Param+h,td, nb, bnb);
338 GeomFill_PlanFunc Edeb(pdbg, td, myGuide);
339 Edeb.Value(Res, etc);
340 if ( Abs( (etc-e)/h - dedt) > 1.e-4) {
341 cout << "err :" << (etc-e)/h - dedt << endl;
344 dn.SetLinearForm(dtg_dt, TG, -1, To);
346 DNormal.SetLinearForm(-(n*dn), n, dn);
348 DBiNormal.SetLinearForm(Tangent.Crossed(DNormal),
349 DTangent.Crossed(Normal));
354 // plan ortho a la trajectoire
355 Handle(Geom_Plane) Plan = new (Geom_Plane)(P, Tangent);
358 myStatus = GeomFill_PlaneNotIntersectGuide;
359 return Standard_False;
362 return Standard_True;
366 //=======================================================================
368 //purpose : calculation of trihedron and derivatives
369 //=======================================================================
370 Standard_Boolean GeomFill_GuideTrihedronPlan::D2(const Standard_Real Param,
383 // gp_Vec To,DTo,TG,DTG;
386 myCurve->D2(Param, P, To, DTo);
388 // triedre de Frenet sur la trajectoire
389 frenet->D2(Param,Tangent,DTangent,D2Tangent,
390 Normal,DNormal,D2Normal,
391 BiNormal,DBiNormal,D2BiNormal);
394 // plan ortho a Tangent pour trouver la pt Pprime sur le guide
395 Handle(Geom_Plane) Plan = new (Geom_Plane)(P, Tangent);
396 Handle(GeomAdaptor_HSurface) Pl= new(GeomAdaptor_HSurface)(Plan);
399 Standard_Integer Iter = 50;
400 // fonction dont il faut trouver la racine : G(W) - Pl(U,V)=0
401 GeomFill_FunctionPipe E(Pl , myGuide);
405 math_FunctionSetRoot Result(E, X, XTol,
410 R = Result.Root(); // solution
411 myTrimG->D2(R(1), PG, TG, DTG);
413 gp_Vec n (P, PG); // vecteur definissant la normale du triedre
414 Standard_Real Norm = n.Magnitude();
416 Normal = n.Normalized();
417 BiNormal = Tangent.Crossed(Normal);
421 // derivee premiere du triedre
422 Standard_Real dtp_dt;
423 dtp_dt = (To*Tangent - Norm*(n*DTangent))/(Tangent*TG);
425 dn.SetLinearForm(dtp_dt, TG, -1, To);
427 DNormal.SetLinearForm(-(n*dn), n, dn);
429 DBiNormal = Tangent.Crossed(DNormal) + DTangent.Crossed(Normal);
431 // derivee seconde du triedre
432 Standard_Real d2tp_dt2;
433 d2tp_dt2 = (DTo*Tangent+To*DTangent - dn*DTangent-Norm*n*D2Tangent)/(TG*Tangent)
434 - (To*Tangent-Norm*n*DTangent) * (DTG*dtp_dt*Tangent+TG*DTangent)
435 / ((TG*Tangent)*(TG*Tangent));
438 d2n.SetLinearForm(dtp_dt*dtp_dt, DTG, d2tp_dt2, TG, -DTo);
442 D2Normal.SetLinearForm(3*Pow(n*dn,2)- (dn.SquareMagnitude() + n*d2n), n,
446 D2BiNormal.SetLinearForm(1, D2Tangent.Crossed(Normal),
447 2, DTangent.Crossed(DNormal),
448 Tangent.Crossed(D2Normal));
455 myStatus = GeomFill_PlaneNotIntersectGuide;
456 return Standard_False;
459 // return Standard_True;
460 return Standard_False;
464 //=======================================================================
467 //=======================================================================
468 Handle(GeomFill_TrihedronLaw) GeomFill_GuideTrihedronPlan::Copy() const
470 Handle(GeomFill_GuideTrihedronPlan) copy =
471 new (GeomFill_GuideTrihedronPlan) (myGuide);
472 copy->SetCurve(myCurve);
476 //=======================================================================
477 //function : ErrorStatus
479 //=======================================================================
480 GeomFill_PipeError GeomFill_GuideTrihedronPlan::ErrorStatus() const
486 //=======================================================================
487 //function : NbIntervals
488 //purpose : Version provisoire : Il faut tenir compte du guide
489 //=======================================================================
490 Standard_Integer GeomFill_GuideTrihedronPlan::NbIntervals(const GeomAbs_Shape S)const
495 case GeomAbs_C0: tmpS = GeomAbs_C1; break;
496 case GeomAbs_C1: tmpS = GeomAbs_C2; break;
497 case GeomAbs_C2: tmpS = GeomAbs_C3; break;
498 default: tmpS = GeomAbs_CN;
501 Nb = myCurve->NbIntervals(tmpS);
504 //======================================================================
505 //function :Intervals
507 //=======================================================================
508 void GeomFill_GuideTrihedronPlan::Intervals(TColStd_Array1OfReal& TT,
509 const GeomAbs_Shape S) const
513 case GeomAbs_C0: tmpS = GeomAbs_C1; break;
514 case GeomAbs_C1: tmpS = GeomAbs_C2; break;
515 case GeomAbs_C2: tmpS = GeomAbs_C3; break;
516 default: tmpS = GeomAbs_CN;
518 myCurve->Intervals(TT, tmpS);
521 //======================================================================
522 //function :SetInterval
524 //=======================================================================
525 void GeomFill_GuideTrihedronPlan::SetInterval(const Standard_Real First,
526 const Standard_Real Last)
528 myTrimmed = myCurve->Trim(First, Last, Precision::Confusion());
532 //=======================================================================
533 //function : GetAverageLaw
535 //=======================================================================
536 void GeomFill_GuideTrihedronPlan::GetAverageLaw(gp_Vec& ATangent,
541 Standard_Real t, Delta = (myCurve->LastParameter() -
542 myCurve->FirstParameter())/20.001;
544 ATangent.SetCoord(0.,0.,0.);
545 ANormal.SetCoord(0.,0.,0.);
546 ABiNormal.SetCoord(0.,0.,0.);
549 for (ii=1; ii<=20; ii++) {
550 t = myCurve->FirstParameter() +(ii-1)*Delta;
561 //=======================================================================
562 //function : IsConstant
564 //=======================================================================
565 Standard_Boolean GeomFill_GuideTrihedronPlan::IsConstant() const
567 if ((myCurve->GetType() == GeomAbs_Line) &&
568 (myGuide->GetType() == GeomAbs_Line)) {
570 Angle = myCurve->Line().Angle(myGuide->Line());
571 if ((Angle<1.e-12) || ((2*M_PI-Angle)<1.e-12) )
572 return Standard_True;
575 return Standard_False;
578 //=======================================================================
579 //function : IsOnlyBy3dCurve
581 //=======================================================================
582 Standard_Boolean GeomFill_GuideTrihedronPlan::IsOnlyBy3dCurve() const
584 return Standard_False;
587 //=======================================================================
589 //purpose : Nothing!!
590 //=======================================================================
591 void GeomFill_GuideTrihedronPlan::Origine(const Standard_Real ,
592 const Standard_Real )
596 //==================================================================
598 //Purpose : recherche par interpolation d'une valeur initiale
599 //==================================================================
600 void GeomFill_GuideTrihedronPlan::InitX(const Standard_Real Param)
603 Standard_Integer Ideb = 1, Ifin = Pole->RowLength(), Idemi;
604 Standard_Real Valeur, t1, t2;
607 Valeur = Pole->Value(1, Ideb).X();
608 if (Param == Valeur) {
612 Valeur = Pole->Value(1, Ifin).X();
613 if (Param == Valeur) {
617 while ( Ideb+1 != Ifin) {
618 Idemi = (Ideb+Ifin)/2;
619 Valeur = Pole->Value(1, Idemi).X();
620 if (Valeur < Param) {
624 if ( Valeur > Param) { Ifin = Idemi;}
632 t1 = Pole->Value(1,Ideb).X();
633 t2 = Pole->Value(1,Ifin).X();
634 Standard_Real diff = t2-t1;
636 Standard_Real b = (Param-t1) / diff,
637 a = (t2-Param) / diff;
639 X(1) = Pole->Value(1,Ideb).Coord(2) * a
640 + Pole->Value(1,Ifin).Coord(2) * b; //param guide
643 X(1) = (Pole->Value(1, Ideb).Coord(2) +
644 Pole->Value(1, Ifin).Coord(2)) / 2;
646 if (myGuide->IsPeriodic()) {
647 X(1) = ElCLib::InPeriod(X(1), myGuide->FirstParameter(),
648 myGuide->LastParameter());