1 // Created on: 1994-02-28
2 // Created by: Bruno DUMORTIER
3 // Copyright (c) 1994-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.
18 #include <Adaptor3d_HCurve.hxx>
20 #include <GCPnts_QuasiUniformDeflection.hxx>
21 #include <Geom_BSplineCurve.hxx>
22 #include <Geom_Circle.hxx>
23 #include <Geom_Curve.hxx>
24 #include <Geom_TrimmedCurve.hxx>
25 #include <GeomAdaptor.hxx>
26 #include <GeomAdaptor_Curve.hxx>
27 #include <GeomConvert.hxx>
28 #include <GeomFill_Profiler.hxx>
29 #include <GeomFill_SweepSectionGenerator.hxx>
34 #include <gp_Trsf.hxx>
36 #include <Precision.hxx>
37 #include <Standard_RangeError.hxx>
38 #include <TColStd_Array1OfReal.hxx>
42 #include <DrawTrSurf.hxx>
43 #include <Geom_BSplineCurve.hxx>
44 static Standard_Boolean Affich = Standard_False;
45 static Standard_Integer NbSECTIONS = 0;
48 //=======================================================================
49 //function : GeomFill_SweepSectionGenerator
51 //=======================================================================
53 GeomFill_SweepSectionGenerator::GeomFill_SweepSectionGenerator()
55 myIsDone = Standard_False;
59 //=======================================================================
60 //function : GeomFill_SweepSectionGenerator
62 //=======================================================================
64 GeomFill_SweepSectionGenerator::GeomFill_SweepSectionGenerator
65 (const Handle(Geom_Curve)& Path,
66 const Standard_Real Radius)
72 //=======================================================================
73 //function : GeomFill_SweepSectionGenerator
75 //=======================================================================
77 GeomFill_SweepSectionGenerator::GeomFill_SweepSectionGenerator
78 (const Handle(Geom_Curve)& Path,
79 const Handle(Geom_Curve)& FirstSect)
85 //=======================================================================
86 //function : GeomFill_SweepSectionGenerator
88 //=======================================================================
90 GeomFill_SweepSectionGenerator::GeomFill_SweepSectionGenerator
91 (const Handle(Geom_Curve)& Path,
92 const Handle(Geom_Curve)& FirstSect,
93 const Handle(Geom_Curve)& LastSect )
95 Init(Path,FirstSect,LastSect);
99 //=======================================================================
100 //function : GeomFill_SweepSectionGenerator
102 //=======================================================================
104 GeomFill_SweepSectionGenerator::GeomFill_SweepSectionGenerator
105 (const Handle(Adaptor3d_HCurve)& Path,
106 const Handle(Adaptor3d_HCurve)& Curve1,
107 const Handle(Adaptor3d_HCurve)& Curve2,
108 const Standard_Real Radius)
110 Init(Path,Curve1,Curve2,Radius);
114 //=======================================================================
117 //=======================================================================
119 void GeomFill_SweepSectionGenerator::Init(const Handle(Geom_Curve)& Path,
120 const Standard_Real Radius)
122 myIsDone = Standard_False;
124 GeomAdaptor_Curve ThePath(Path);
126 if (ThePath.GetType() == GeomAbs_Circle) {
128 myCircPathAxis = ThePath.Circle().Axis();
132 if ( Path->IsKind(STANDARD_TYPE(Geom_BSplineCurve))) {
133 myPath = Handle(Geom_BSplineCurve)::DownCast(Path->Copy());
136 myPath = GeomConvert::CurveToBSplineCurve(Path);
141 //=======================================================================
144 //=======================================================================
145 void GeomFill_SweepSectionGenerator::Init
146 (const Handle(Geom_Curve)& Path,
147 const Handle(Geom_Curve)& FirstSect)
149 myIsDone = Standard_False;
151 GeomAdaptor_Curve ThePath(Path);
153 if (ThePath.GetType() == GeomAbs_Circle) {
154 myCircPathAxis = ThePath.Circle().Axis();
160 if ( Path->IsKind(STANDARD_TYPE(Geom_BSplineCurve))) {
161 myPath = Handle(Geom_BSplineCurve)::DownCast(Path->Copy());
164 myPath = GeomConvert::CurveToBSplineCurve(Path);
166 if ( FirstSect->IsKind(STANDARD_TYPE(Geom_BSplineCurve))) {
167 myFirstSect = Handle(Geom_BSplineCurve)::DownCast(FirstSect->Copy());
171 myFirstSect = GeomConvert::CurveToBSplineCurve(FirstSect,
172 Convert_QuasiAngular);
174 if ( myFirstSect->IsPeriodic()) myFirstSect->SetNotPeriodic();
178 //=======================================================================
181 //=======================================================================
183 void GeomFill_SweepSectionGenerator::Init
184 (const Handle(Geom_Curve)& Path,
185 const Handle(Geom_Curve)& FirstSect,
186 const Handle(Geom_Curve)& LastSect )
188 myIsDone = Standard_False;
190 GeomAdaptor_Curve ThePath(Path);
192 if (ThePath.GetType() == GeomAbs_Circle) {
194 myCircPathAxis = ThePath.Circle().Axis();
199 if ( Path->IsKind(STANDARD_TYPE(Geom_BSplineCurve))) {
200 myPath = Handle(Geom_BSplineCurve)::DownCast(Path->Copy());
203 myPath = GeomConvert::CurveToBSplineCurve(Path);
207 if ( FirstSect->IsKind(STANDARD_TYPE(Geom_BSplineCurve))) {
208 myFirstSect = Handle(Geom_BSplineCurve)::DownCast(FirstSect->Copy());
211 myFirstSect = GeomConvert::CurveToBSplineCurve(FirstSect,
212 Convert_QuasiAngular);
214 if ( LastSect->IsKind(STANDARD_TYPE(Geom_BSplineCurve))) {
215 myLastSect = Handle(Geom_BSplineCurve)::DownCast(LastSect->Copy());
218 myLastSect = GeomConvert::CurveToBSplineCurve(LastSect,
219 Convert_QuasiAngular);
222 if ( myFirstSect->IsPeriodic()) myFirstSect->SetNotPeriodic();
223 if ( myLastSect->IsPeriodic()) myLastSect->SetNotPeriodic();
228 GeomFill_Profiler Profil;
229 Profil.AddCurve(myFirstSect);
230 Profil.AddCurve(myLastSect);
231 Profil.Perform(Precision::Confusion());
233 myFirstSect = Handle(Geom_BSplineCurve)::DownCast(Profil.Curve(1));
234 myLastSect = Handle(Geom_BSplineCurve)::DownCast(Profil.Curve(2));
238 //=======================================================================
241 //=======================================================================
243 void GeomFill_SweepSectionGenerator::Init
244 (const Handle(Adaptor3d_HCurve)& Path,
245 const Handle(Adaptor3d_HCurve)& Curve1,
246 const Handle(Adaptor3d_HCurve)& Curve2,
247 const Standard_Real Radius)
249 myIsDone = Standard_False;
253 Handle(Geom_Curve) CC = GeomAdaptor::MakeCurve(Path->Curve());
254 myPath = GeomConvert::CurveToBSplineCurve(CC);
256 myAdpFirstSect = Curve1;
257 myAdpLastSect = Curve2;
261 //=======================================================================
264 //=======================================================================
266 void GeomFill_SweepSectionGenerator::Perform(const Standard_Boolean Polynomial)
268 myPolynomial = Polynomial;
270 // eval myNbSections.
271 Standard_Integer NSpans = myPath->NbKnots()-1;
273 myNbSections = 21 * NSpans;
277 Standard_Real U1 = myPath->FirstParameter();
278 Standard_Real U2 = myPath->LastParameter();
280 GCPnts_QuasiUniformDeflection Samp;
281 // Calcul de la longueur approximative de la courbe
282 GeomAdaptor_Curve AdpPath(myPath);
283 gp_Pnt P1 = AdpPath.Value(U1);
284 gp_Pnt P2 = AdpPath.Value((U1+U2)/2.);
285 gp_Pnt P3 = AdpPath.Value(U2);
286 Standard_Real Length =
287 P1.Distance(P2) + P2.Distance(P3);
288 Standard_Real Fleche = 1.e-5 * Length;
289 Samp.Initialize(AdpPath,Fleche);
291 if ( Samp.IsDone() && (Samp.NbPoints() > myNbSections) ) {
292 myNbSections = Samp.NbPoints();
294 // the transformations are calculate on differents points of <myPath>
295 // corresponding to the path parameter uniformly reparted.
296 Standard_Real DeltaU = (U2-U1)/(Standard_Real)(myNbSections-1);
297 TColStd_Array1OfReal Parameters(1,myNbSections);
298 // Parameters(1) = U1;
299 // for (Standard_Integer i = 2; i < myNbSections; i++) {
300 // Parameters(i) = U1 + (i-1) * DeltaU;
302 // Parameters(myNbSections) = U2;
305 for (Standard_Integer i = 2; i < myNbSections; i++) {
306 Parameters(i) = (i-1) * DeltaU;
308 Parameters(myNbSections) = U2 - U1;
312 gp_Trsf TR, cumulTR, Trans;
314 myPath->D1( U1, PRef, D1Ref);
316 if ( ( myType == 1) || (myType == 4) ) {
317 // We create a circle with radius <myRadius>. This axis is create with
318 // main direction <DRef> (first derivate vector of <myPath> on the first
319 // point <PRef> ). This circle is, after transform to BSpline curve,
320 // put in <myFirstSect>.
322 gp_Ax2 CircleAxis (PRef,D1Ref);
324 Handle(Geom_Circle) Circ = new Geom_Circle( CircleAxis, myRadius);
326 myFirstSect = GeomConvert::CurveToBSplineCurve(Circ);
327 // le cercle est segmente car AppBlend_AppSurf ne gere
328 // pas les courbes periodiques.
329 myFirstSect->Segment(0., 2.*M_PI);
331 Handle(Geom_TrimmedCurve) Circ =
332 new Geom_TrimmedCurve(new Geom_Circle( CircleAxis, myRadius),
335 myFirstSect = GeomConvert::CurveToBSplineCurve(Circ,Convert_QuasiAngular);
338 if (myType <= 3 && myType >=1 ) {
340 for (Standard_Integer i = 2; i <= myNbSections; i++) {
342 U = Parameters(i) + U1;
343 if (i == myNbSections) U = U2;
345 myPath->D1( U, P, D1);
347 // Eval the translation between the (i-1) section and the i-th.
348 Trans.SetTranslation(PRef, P);
351 if (! D1Ref.IsParallel(D1, Precision::Angular())) {
352 // Eval the Rotation between (i-1) section and the i-th.
353 Rot.SetRotation(gp_Ax1(P, gp_Dir(D1Ref^D1)),
354 D1Ref.AngleWithRef(D1, D1Ref^D1));
357 if (D1Ref.IsOpposite(D1, Precision::Angular()))
359 std::cout <<"Que fais-je ???? " << std::endl;
362 // TR is the transformation between (i-1) section and the i-th.
364 // cumulTR is the transformation between <myFirstSec> and
366 cumulTR = TR * cumulTR;
368 myTrsfs.Append(cumulTR);
374 else if ( myType != 0) {
375 for (Standard_Integer i = 2; i<= myNbSections; i++) {
376 cumulTR.SetRotation(myCircPathAxis, Parameters(i));
377 myTrsfs.Append(cumulTR);
381 myIsDone = Standard_True;
384 //=======================================================================
385 //function : GetShape
387 //=======================================================================
389 void GeomFill_SweepSectionGenerator::GetShape
390 (Standard_Integer& NbPoles,
391 Standard_Integer& NbKnots,
392 Standard_Integer& Degree,
393 Standard_Integer& NbPoles2d) const
404 NbPoles = myFirstSect->NbPoles();
405 NbKnots = myFirstSect->NbKnots();
406 Degree = myFirstSect->Degree();
408 else { // myType == 0
417 //=======================================================================
420 //=======================================================================
422 void GeomFill_SweepSectionGenerator::Knots(TColStd_Array1OfReal& TKnots) const
426 Standard_Real U = 2.*M_PI/3.;
427 for ( Standard_Integer i = 1; i <= 4; i++)
428 TKnots(i) = ( i-1) * U;
433 myFirstSect->Knots(TKnots);
443 //=======================================================================
446 //=======================================================================
448 void GeomFill_SweepSectionGenerator::Mults(TColStd_Array1OfInteger& TMults)
453 TMults( 1) = TMults( 4) = 3;
454 TMults( 2) = TMults( 3) = 2;
459 myFirstSect->Multiplicities(TMults);
462 TMults( 1) = TMults( 2) = 7;
468 //=======================================================================
471 //=======================================================================
473 Standard_Boolean GeomFill_SweepSectionGenerator::Section
474 (const Standard_Integer P,
475 TColgp_Array1OfPnt& Poles,
476 TColgp_Array1OfVec& DPoles,
477 TColgp_Array1OfPnt2d& Poles2d,
478 TColgp_Array1OfVec2d& , //DPoles2d,
479 TColStd_Array1OfReal& Weigths,
480 TColStd_Array1OfReal& DWeigths
483 Section( P, Poles, Poles2d, Weigths);
485 // pour les tuyaux sur aretes pour l'instant on ne calcule pas les derivees
486 if ( myType == 0 ) return Standard_False; // a voir pour mieux.
488 // calcul des derivees sur la surface
489 // on calcule les derivees en approximant le path au voisinage du point
490 // P(u) par le cercle osculateur au path .
492 // calcul du cercle osculateur.
496 U = myPath->FirstParameter();
498 else if ( P == myNbSections ) {
499 U = myPath->LastParameter();
502 return Standard_False;
507 myPath->D2(U,Pt,D1,D2);
508 Standard_Real l = D1.Magnitude();
510 if ( l < Epsilon(1.))
511 return Standard_False;
514 Standard_Real m = D2.Dot(T);
515 gp_Vec D = D2 - m * T;
516 Standard_Real c = D.Magnitude() / (l*l);
518 if ( c < Epsilon(1.)) {
519 // null curvature : equivalent to a translation of the section
520 for (Standard_Integer i = 1; i <= myFirstSect->NbPoles(); i++) {
526 gp_Pnt Q = Pt.Translated( (1./c) * gp_Vec(N));
529 for ( Standard_Integer i = 1; i <= myFirstSect->NbPoles(); i++) {
530 V = gp_Vec(Q, Poles(i));
533 DPoles(i) = x * gp_Vec(N) - y * gp_Vec(T);
534 if ( DPoles(i).Magnitude() > Epsilon(1.)) {
535 DPoles(i).Normalize();
536 DPoles(i) *= Sqrt( x*x + y*y);
541 for ( Standard_Integer i = 1; i <= myFirstSect->NbPoles(); i++) {
545 return Standard_True;
549 //=======================================================================
552 //=======================================================================
554 void GeomFill_SweepSectionGenerator::Section
555 (const Standard_Integer P,
556 TColgp_Array1OfPnt& Poles,
557 TColgp_Array1OfPnt2d& , //Poles2d,
558 TColStd_Array1OfReal& Weigths) const
561 myFirstSect->Poles(Poles);
562 myFirstSect->Weights(Weigths);
565 cumulTR = myTrsfs(P - 1);
566 // <cumulTR> transform <myFirstSect> to the P ieme Section. In fact
567 // each points of the array <poles> will be transformed.
569 if ( (myType == 3 ) || (myType == 6) ){
570 for (Standard_Integer i = 1; i <= myFirstSect->NbPoles(); i++) {
571 Poles(i).SetXYZ( (myNbSections - P) * myFirstSect->Pole(i).XYZ() +
572 (P - 1) * myLastSect->Pole(i).XYZ() );
573 Poles(i).SetXYZ( Poles(i).XYZ() / (myNbSections - 1));
575 Weigths(i) = (myNbSections - P) * myFirstSect->Weight(i) +
576 (P - 1) * myLastSect->Weight(i);
577 Weigths(i) /= myNbSections - 1;
581 for (Standard_Integer i = 1; i<=Poles.Length(); i++)
582 Poles(i).Transform(cumulTR);
587 sprintf(name,"SECTION_%d",++NbSECTIONS);
588 DrawTrSurf::Set(name,myFirstSect->Transformed(cumulTR));
594 Standard_Real Coef = (P -1. ) / ( myNbSections - 1.);
596 ( 1- Coef) * myAdpPath->FirstParameter() +
597 Coef * myAdpPath->LastParameter();
599 gp_Pnt PPath = myAdpPath->Value(U);
601 Standard_Real Alpha = U - myAdpPath->FirstParameter();
602 Alpha /= myAdpPath->LastParameter() - myAdpPath->FirstParameter();
605 ( 1- Alpha) * myAdpFirstSect->FirstParameter() +
606 Alpha * myAdpFirstSect->LastParameter();
608 gp_Pnt P1 = myAdpFirstSect->Value(U1);
611 ( 1- Alpha) * myAdpLastSect->FirstParameter() +
612 Alpha * myAdpLastSect->LastParameter();
614 gp_Pnt P2 = myAdpLastSect->Value(U2);
618 if ( P1.Distance(P2) < Precision::Confusion()) {
623 gp_Vec(PPath,P1) ^ gp_Vec(PPath,P2),
625 Angle = ElCLib::CircleParameter(Axis,P2);
629 if (Standard_False) {
630 gp_Vec dummyD1 = myAdpPath->DN(U,1);
631 gp_Vec dummyTg = Axis.Direction();
632 Standard_Real Cos = dummyD1.Dot(dummyTg);
633 if ( Cos > 0.) std::cout << "+" ;
634 else std::cout << "-" ;
638 if ( Angle < Precision::Angular()) {
639 for ( Standard_Integer i = 1; i <= Poles.Upper(); i++) {
645 Handle(Geom_Circle) Circ =
646 new Geom_Circle( Axis, myRadius);
647 Handle(Geom_TrimmedCurve) CT =
648 new Geom_TrimmedCurve(Circ, 0., Angle);
649 Handle(Geom_BSplineCurve) BS;
651 BS = GeomConvert::CurveToBSplineCurve( CT, Convert_Polynomial);
653 BS = GeomConvert::CurveToBSplineCurve( CT, Convert_QuasiAngular);
658 sprintf(name,"SECTION_%d",++NbSECTIONS);
659 DrawTrSurf::Set(name,BS);
664 BS->Weights(Weigths);
669 //=======================================================================
670 //function : Transformation
672 //=======================================================================
673 const gp_Trsf& GeomFill_SweepSectionGenerator::Transformation
674 (const Standard_Integer Index) const
676 if (Index > myTrsfs.Length())
677 throw Standard_RangeError("GeomFill_SweepSectionGenerator::Transformation");
679 return myTrsfs(Index);
683 //=======================================================================
684 //function : Parameter
686 //=======================================================================
688 Standard_Real GeomFill_SweepSectionGenerator::Parameter
689 (const Standard_Integer P) const
692 return myPath->FirstParameter();
694 else if (P == myNbSections) {
695 return myPath->LastParameter();
698 Standard_Real U1 = myPath->FirstParameter();
699 Standard_Real U2 = myPath->LastParameter();
700 Standard_Real prm = ((myNbSections-P)*U1 + (P-1)*U2)/
701 (Standard_Real)(myNbSections-1);