1 // Created on: 1993-07-19
2 // Created by: Remi LEQUETTE
3 // Copyright (c) 1993-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 <Geom2d_BezierCurve.hxx>
19 #include <Geom2d_BSplineCurve.hxx>
20 #include <Geom2d_Circle.hxx>
21 #include <Geom2d_Curve.hxx>
22 #include <Geom2d_Ellipse.hxx>
23 #include <Geom2d_Hyperbola.hxx>
24 #include <Geom2d_Line.hxx>
25 #include <Geom2d_OffsetCurve.hxx>
26 #include <Geom2d_Parabola.hxx>
27 #include <Geom2d_TrimmedCurve.hxx>
28 #include <GeomTools.hxx>
29 #include <GeomTools_Curve2dSet.hxx>
30 #include <GeomTools_UndefinedTypeHandler.hxx>
31 #include <gp_Circ2d.hxx>
32 #include <gp_Elips2d.hxx>
33 #include <gp_Hypr2d.hxx>
34 #include <gp_Lin2d.hxx>
35 #include <gp_Parab2d.hxx>
36 #include <Message_ProgressScope.hxx>
37 #include <Standard_ErrorHandler.hxx>
38 #include <Standard_Failure.hxx>
39 #include <Standard_Stream.hxx>
40 #include <TColgp_Array1OfPnt2d.hxx>
41 #include <TColStd_Array1OfInteger.hxx>
42 #include <TColStd_Array1OfReal.hxx>
55 //=======================================================================
56 //function : GeomTools_Curve2dSet
58 //=======================================================================
60 GeomTools_Curve2dSet::GeomTools_Curve2dSet()
65 //=======================================================================
68 //=======================================================================
70 void GeomTools_Curve2dSet::Clear()
76 //=======================================================================
79 //=======================================================================
81 Standard_Integer GeomTools_Curve2dSet::Add(const Handle(Geom2d_Curve)& S)
87 //=======================================================================
90 //=======================================================================
92 Handle(Geom2d_Curve) GeomTools_Curve2dSet::Curve2d
93 (const Standard_Integer I)const
95 if (I <= 0 || I > myMap.Extent())
96 return Handle(Geom2d_Curve)();
98 return Handle(Geom2d_Curve)::DownCast(myMap(I));
102 //=======================================================================
105 //=======================================================================
107 Standard_Integer GeomTools_Curve2dSet::Index
108 (const Handle(Geom2d_Curve)& S)const
110 return myMap.FindIndex(S);
113 //=======================================================================
116 //=======================================================================
118 static void Print(const gp_Pnt2d P,
119 Standard_OStream& OS,
120 const Standard_Boolean compact)
123 if (!compact) OS << ",";
129 //=======================================================================
132 //=======================================================================
134 static void Print(const gp_Dir2d D,
135 Standard_OStream& OS,
136 const Standard_Boolean compact)
139 if (!compact) OS << ",";
145 //=======================================================================
148 //=======================================================================
150 static void Print(const Handle(Geom2d_Line)& L,
151 Standard_OStream& OS,
152 const Standard_Boolean compact)
159 gp_Lin2d C2d = L->Lin2d();
160 if (!compact) OS << "\n Origin :";
161 Print(C2d.Location(),OS,compact);
162 if (!compact) OS << "\n Axis :";
163 Print(C2d.Direction(),OS,compact);
164 if (!compact) OS << "\n";
168 //=======================================================================
171 //=======================================================================
173 static void Print(const Handle(Geom2d_Circle)& C,
174 Standard_OStream& OS,
175 const Standard_Boolean compact)
182 gp_Circ2d C2d = C->Circ2d();
183 if (!compact) OS << "\n Center :";
184 Print(C2d.Location(),OS,compact);
185 if (!compact) OS << "\n XAxis :";
186 Print(C2d.XAxis().Direction(),OS,compact);
187 if (!compact) OS << "\n YAxis :";
188 Print(C2d.YAxis().Direction(),OS,compact);
189 if (!compact) OS << "\n Radius :";
191 if (!compact) OS << "\n";
195 //=======================================================================
198 //=======================================================================
200 static void Print(const Handle(Geom2d_Ellipse)& E,
201 Standard_OStream& OS,
202 const Standard_Boolean compact)
205 OS << ELLIPSE << " ";
209 gp_Elips2d C2d = E->Elips2d();
210 if (!compact) OS << "\n Center :";
211 Print(C2d.Location(),OS,compact);
212 if (!compact) OS << "\n XAxis :";
213 Print(C2d.XAxis().Direction(),OS,compact);
214 if (!compact) OS << "\n YAxis :";
215 Print(C2d.YAxis().Direction(),OS,compact);
216 if (!compact) OS << "\n Radii :";
217 OS << C2d.MajorRadius();
218 if (!compact) OS << ",";
220 OS << C2d.MinorRadius();
221 if (!compact) OS << "\n";
225 //=======================================================================
228 //=======================================================================
230 static void Print(const Handle(Geom2d_Parabola)& P,
231 Standard_OStream& OS,
232 const Standard_Boolean compact)
235 OS << PARABOLA << " ";
239 gp_Parab2d C2d = P->Parab2d();
240 if (!compact) OS << "\n Center :";
241 Print(C2d.Location(),OS,compact);
242 if (!compact) OS << "\n XAxis :";
243 Print(C2d.Axis().XAxis().Direction(),OS,compact);
244 if (!compact) OS << "\n YAxis :";
245 Print(C2d.Axis().YAxis().Direction(),OS,compact);
246 if (!compact) OS << "\n Focal :";
248 if (!compact) OS << "\n";
252 //=======================================================================
255 //=======================================================================
257 static void Print(const Handle(Geom2d_Hyperbola)& H,
258 Standard_OStream& OS,
259 const Standard_Boolean compact)
262 OS << HYPERBOLA << " ";
266 gp_Hypr2d C2d = H->Hypr2d();
267 if (!compact) OS << "\n Center :";
268 Print(C2d.Location(),OS,compact);
269 if (!compact) OS << "\n XAxis :";
270 Print(C2d.XAxis().Direction(),OS,compact);
271 if (!compact) OS << "\n YAxis :";
272 Print(C2d.YAxis().Direction(),OS,compact);
273 if (!compact) OS << "\n Radii :";
274 OS << C2d.MajorRadius();
275 if (!compact) OS << ",";
277 OS << C2d.MinorRadius();
278 if (!compact) OS << "\n";
282 //=======================================================================
285 //=======================================================================
287 static void Print(const Handle(Geom2d_BezierCurve)& B,
288 Standard_OStream& OS,
289 const Standard_Boolean compact)
296 Standard_Boolean rational = B->IsRational();
298 OS << (rational ? 1 : 0) << " ";
305 Standard_Integer i,degree = B->Degree();
306 if (!compact) OS << "\n Degree :";
309 for (i = 1; i <= degree+1; i++) {
310 if (!compact) OS << "\n "<<std::setw(2)<<i<<" : ";
311 Print(B->Pole(i),OS,compact);
313 OS << " " << B->Weight(i);
317 if (!compact) OS << "\n";
321 //=======================================================================
324 //=======================================================================
326 static void Print(const Handle(Geom2d_BSplineCurve)& B,
327 Standard_OStream& OS,
328 const Standard_Boolean compact)
331 OS << BSPLINE << " ";
333 OS << "BSplineCurve";
336 Standard_Boolean rational = B->IsRational();
338 OS << (rational ? 1 : 0) << " ";
344 Standard_Boolean periodic = B->IsPeriodic();
346 OS << (periodic ? 1 : 0) << " ";
353 Standard_Integer i,degree,nbpoles,nbknots;
354 degree = B->Degree();
355 nbpoles = B->NbPoles();
356 nbknots = B->NbKnots();
357 if (!compact) OS << "\n Degree ";
360 if (!compact) OS << ",";
363 if (!compact) OS << " Poles,";
365 OS << nbknots << " ";
366 if (!compact) OS << " Knots";
368 if (!compact) OS << "Poles :\n";
369 for (i = 1; i <= nbpoles; i++) {
370 if (!compact) OS << "\n "<<std::setw(2)<<i<<" : ";
372 Print(B->Pole(i),OS,compact);
374 OS << " " << B->Weight(i);
378 if (!compact) OS << "Knots :\n";
379 for (i = 1; i <= nbknots; i++) {
380 if (!compact) OS << "\n "<<std::setw(2)<<i<<" : ";
382 OS << B->Knot(i) << " " << B->Multiplicity(i);
384 if (!compact) OS << "\n";
388 //=======================================================================
391 //=======================================================================
393 static void Print(const Handle(Geom2d_TrimmedCurve)& C,
394 Standard_OStream& OS,
395 const Standard_Boolean compact)
398 OS << TRIMMED << " ";
400 OS << "Trimmed curve\n";
401 if (!compact) OS << "Parameters : ";
402 OS << C->FirstParameter() << " " << C->LastParameter() << "\n";
403 if (!compact) OS << "Basis curve :\n";
404 GeomTools_Curve2dSet::PrintCurve2d(C->BasisCurve(),OS,compact);
407 //=======================================================================
410 //=======================================================================
412 static void Print(const Handle(Geom2d_OffsetCurve)& C,
413 Standard_OStream& OS,
414 const Standard_Boolean compact)
420 if (!compact) OS << "Offset : ";
421 OS << C->Offset() << "\n";
422 if (!compact) OS << "Basis curve :\n";
423 GeomTools_Curve2dSet::PrintCurve2d(C->BasisCurve(),OS,compact);
426 //=======================================================================
427 //function : PrintCurve2d
429 //=======================================================================
431 void GeomTools_Curve2dSet::PrintCurve2d(const Handle(Geom2d_Curve)& C,
432 Standard_OStream& OS,
433 const Standard_Boolean compact)
435 Handle(Standard_Type) TheType = C->DynamicType();
437 if ( TheType == STANDARD_TYPE(Geom2d_Circle)) {
438 Print(Handle(Geom2d_Circle)::DownCast(C),OS,compact);
440 else if ( TheType ==STANDARD_TYPE(Geom2d_Line)) {
441 Print(Handle(Geom2d_Line)::DownCast(C),OS,compact);
443 else if ( TheType == STANDARD_TYPE(Geom2d_Ellipse)) {
444 Print(Handle(Geom2d_Ellipse)::DownCast(C),OS,compact);
446 else if ( TheType == STANDARD_TYPE(Geom2d_Parabola)) {
447 Print(Handle(Geom2d_Parabola)::DownCast(C),OS,compact);
449 else if ( TheType == STANDARD_TYPE(Geom2d_Hyperbola)) {
450 Print(Handle(Geom2d_Hyperbola)::DownCast(C),OS,compact);
452 else if ( TheType == STANDARD_TYPE(Geom2d_BezierCurve)) {
453 Print(Handle(Geom2d_BezierCurve)::DownCast(C),OS,compact);
455 else if ( TheType == STANDARD_TYPE(Geom2d_BSplineCurve)) {
456 Print(Handle(Geom2d_BSplineCurve)::DownCast(C),OS,compact);
458 else if ( TheType == STANDARD_TYPE(Geom2d_TrimmedCurve)) {
459 Print(Handle(Geom2d_TrimmedCurve)::DownCast(C),OS,compact);
461 else if ( TheType == STANDARD_TYPE(Geom2d_OffsetCurve)) {
462 Print(Handle(Geom2d_OffsetCurve)::DownCast(C),OS,compact);
465 GeomTools::GetUndefinedTypeHandler()->PrintCurve2d(C,OS,compact);
467 // OS << "****** UNKNOWN CURVE2d TYPE ******\n";
469 // std::cout << "****** UNKNOWN CURVE2d TYPE ******" << std::endl;
473 //=======================================================================
476 //=======================================================================
478 void GeomTools_Curve2dSet::Dump(Standard_OStream& OS)const
480 Standard_Integer i, nbsurf = myMap.Extent();
481 OS << "\n -------\n";
482 OS << "Dump of "<< nbsurf << " Curve2ds ";
483 OS << "\n -------\n\n";
485 for (i = 1; i <= nbsurf; i++) {
486 OS << std::setw(4) << i << " : ";
487 PrintCurve2d(Handle(Geom2d_Curve)::DownCast(myMap(i)),OS,Standard_False);
492 //=======================================================================
495 //=======================================================================
497 void GeomTools_Curve2dSet::Write(Standard_OStream& OS, const Message_ProgressRange& theProgress)const
499 std::streamsize prec = OS.precision(17);
501 Standard_Integer i, nbsurf = myMap.Extent();
502 OS << "Curve2ds "<< nbsurf << "\n";
503 Message_ProgressScope aPS(theProgress, "2D Curves", nbsurf);
504 for (i = 1; i <= nbsurf && aPS.More(); i++, aPS.Next()) {
505 PrintCurve2d(Handle(Geom2d_Curve)::DownCast(myMap(i)),OS,Standard_True);
511 //=======================================================================
512 //function : ReadPnt2d
514 //=======================================================================
516 static Standard_IStream& operator>>(Standard_IStream& IS, gp_Pnt2d& P)
518 Standard_Real X=0.,Y=0.;
519 GeomTools::GetReal(IS, X);
520 GeomTools::GetReal(IS, Y);
525 //=======================================================================
526 //function : ReadDir2d
528 //=======================================================================
530 static Standard_IStream& operator>>(Standard_IStream& IS, gp_Dir2d& D)
532 Standard_Real X=0.,Y=0.;
533 GeomTools::GetReal(IS, X);
534 GeomTools::GetReal(IS, Y);
540 //=======================================================================
541 //function : ReadCurve2d
543 //=======================================================================
545 static Standard_IStream& operator>>(Standard_IStream& IS,
546 Handle(Geom2d_Line)& L)
551 L = new Geom2d_Line(P,AX);
555 //=======================================================================
556 //function : ReadCurve2d
558 //=======================================================================
560 static Standard_IStream& operator>>(Standard_IStream& IS,
561 Handle(Geom2d_Circle)& C)
564 gp_Dir2d AX(1.,0.),AY(1.,0.);
567 GeomTools::GetReal(IS, R);
568 C = new Geom2d_Circle(gp_Ax22d(P,AX,AY),R);
572 //=======================================================================
573 //function : ReadCurve2d
575 //=======================================================================
577 static Standard_IStream& operator>>(Standard_IStream& IS,
578 Handle(Geom2d_Ellipse)& E)
581 gp_Dir2d AX(1.,0.),AY(1.,0.);
582 Standard_Real R1=0.,R2=0.;
584 GeomTools::GetReal(IS, R1);
585 GeomTools::GetReal(IS, R2);
586 E = new Geom2d_Ellipse(gp_Ax22d(P,AX,AY),R1,R2);
590 //=======================================================================
591 //function : ReadCurve2d
593 //=======================================================================
595 static Standard_IStream& operator>>(Standard_IStream& IS,
596 Handle(Geom2d_Parabola)& C)
599 gp_Dir2d AX(1.,0.),AY(1.,0.);
602 GeomTools::GetReal(IS, R1);
603 C = new Geom2d_Parabola(gp_Ax22d(P,AX,AY),R1);
607 //=======================================================================
608 //function : ReadCurve2d
610 //=======================================================================
612 static Standard_IStream& operator>>(Standard_IStream& IS,
613 Handle(Geom2d_Hyperbola)& H)
616 gp_Dir2d AX(1.,0.),AY(1.,0.);
617 Standard_Real R1=0.,R2=0.;
619 GeomTools::GetReal(IS, R1);
620 GeomTools::GetReal(IS, R2);
621 H = new Geom2d_Hyperbola(gp_Ax22d(P,AX,AY),R1,R2);
625 //=======================================================================
626 //function : ReadCurve2d
628 //=======================================================================
630 static Standard_IStream& operator>>(Standard_IStream& IS,
631 Handle(Geom2d_BezierCurve)& B)
633 Standard_Boolean rational=Standard_False;
637 Standard_Integer i=0,degree=0;
640 TColgp_Array1OfPnt2d poles(1,degree+1);
641 TColStd_Array1OfReal weights(1,degree+1);
643 for (i = 1; i <= degree+1; i++) {
646 GeomTools::GetReal(IS, weights(i));
650 B = new Geom2d_BezierCurve(poles,weights);
652 B = new Geom2d_BezierCurve(poles);
657 //=======================================================================
658 //function : ReadCurve2d
660 //=======================================================================
662 static Standard_IStream& operator>>(Standard_IStream& IS,
663 Handle(Geom2d_BSplineCurve)& B)
665 Standard_Boolean rational=Standard_False,periodic=Standard_False;
666 IS >> rational >> periodic;
669 Standard_Integer i=0,degree=0,nbpoles=0,nbknots=0;
670 IS >> degree >> nbpoles >> nbknots;
672 TColgp_Array1OfPnt2d poles(1,nbpoles);
673 TColStd_Array1OfReal weights(1,nbpoles);
675 for (i = 1; i <= nbpoles; i++) {
678 GeomTools::GetReal(IS, weights(i));
681 TColStd_Array1OfReal knots(1,nbknots);
682 TColStd_Array1OfInteger mults(1,nbknots);
684 for (i = 1; i <= nbknots; i++) {
685 GeomTools::GetReal(IS, knots(i));
690 B = new Geom2d_BSplineCurve(poles,weights,knots,mults,degree,periodic);
692 B = new Geom2d_BSplineCurve(poles,knots,mults,degree,periodic);
697 //=======================================================================
698 //function : ReadCurve2d
700 //=======================================================================
702 static Standard_IStream& operator>>(Standard_IStream& IS,
703 Handle(Geom2d_TrimmedCurve)& C)
705 Standard_Real p1=0.,p2=0.;
706 GeomTools::GetReal(IS, p1);
707 GeomTools::GetReal(IS, p2);
708 Handle(Geom2d_Curve) BC = GeomTools_Curve2dSet::ReadCurve2d(IS);
709 C = new Geom2d_TrimmedCurve(BC,p1,p2);
713 //=======================================================================
714 //function : ReadCurve2d
716 //=======================================================================
718 static Standard_IStream& operator>>(Standard_IStream& IS,
719 Handle(Geom2d_OffsetCurve)& C)
722 GeomTools::GetReal(IS, p);
723 Handle(Geom2d_Curve) BC = GeomTools_Curve2dSet::ReadCurve2d(IS);
724 C = new Geom2d_OffsetCurve(BC,p);
728 //=======================================================================
729 //function : ReadCurve2d
731 //=======================================================================
733 Handle(Geom2d_Curve) GeomTools_Curve2dSet::ReadCurve2d(Standard_IStream& IS)
735 Standard_Integer ctype;
737 Handle(Geom2d_Curve) C;
745 Handle(Geom2d_Line) CC;
753 Handle(Geom2d_Circle) CC;
761 Handle(Geom2d_Ellipse) CC;
769 Handle(Geom2d_Parabola) CC;
777 Handle(Geom2d_Hyperbola) CC;
785 Handle(Geom2d_BezierCurve) CC;
793 Handle(Geom2d_BSplineCurve) CC;
801 Handle(Geom2d_TrimmedCurve) CC;
809 Handle(Geom2d_OffsetCurve) CC;
817 Handle(Geom2d_Curve) CC;
818 GeomTools::GetUndefinedTypeHandler()->ReadCurve2d(ctype,IS,CC);
824 catch(Standard_Failure const& anException) {
826 std::cout <<"EXCEPTION in GeomTools_Curve2dSet::ReadCurve2d(..)!!!" << std::endl;
827 std::cout << anException << std::endl;
834 //=======================================================================
837 //=======================================================================
839 void GeomTools_Curve2dSet::Read(Standard_IStream& IS, const Message_ProgressRange& theProgress)
843 if (strcmp(buffer,"Curve2ds")) {
844 std::cout << "Not a Curve2d table"<<std::endl;
848 Standard_Integer i, nbcurve;
850 Message_ProgressScope aPS(theProgress, "2D Curves", nbcurve);
851 for (i = 1; i <= nbcurve && aPS.More(); i++, aPS.Next()) {
852 Handle(Geom2d_Curve) C = GeomTools_Curve2dSet::ReadCurve2d (IS);