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 <Geom_BezierCurve.hxx>
19 #include <Geom_BSplineCurve.hxx>
20 #include <Geom_Circle.hxx>
21 #include <Geom_Curve.hxx>
22 #include <Geom_Ellipse.hxx>
23 #include <Geom_Hyperbola.hxx>
24 #include <Geom_Line.hxx>
25 #include <Geom_OffsetCurve.hxx>
26 #include <Geom_Parabola.hxx>
27 #include <Geom_TrimmedCurve.hxx>
28 #include <GeomTools.hxx>
29 #include <GeomTools_CurveSet.hxx>
30 #include <GeomTools_UndefinedTypeHandler.hxx>
31 #include <gp_Circ.hxx>
32 #include <gp_Elips.hxx>
33 #include <gp_Hypr.hxx>
35 #include <gp_Parab.hxx>
36 #include <Message_ProgressScope.hxx>
37 #include <Standard_ErrorHandler.hxx>
38 #include <Standard_Failure.hxx>
39 #include <Standard_OutOfRange.hxx>
40 #include <Standard_Stream.hxx>
41 #include <TColgp_Array1OfPnt.hxx>
42 #include <TColStd_Array1OfInteger.hxx>
43 #include <TColStd_Array1OfReal.hxx>
55 //=======================================================================
56 //function : GeomTools_CurveSet
58 //=======================================================================
60 GeomTools_CurveSet::GeomTools_CurveSet()
65 //=======================================================================
68 //=======================================================================
70 void GeomTools_CurveSet::Clear()
76 //=======================================================================
79 //=======================================================================
81 Standard_Integer GeomTools_CurveSet::Add(const Handle(Geom_Curve)& C)
83 return (C.IsNull()) ? 0 : myMap.Add(C);
87 //=======================================================================
90 //=======================================================================
92 Handle(Geom_Curve) GeomTools_CurveSet::Curve
93 (const Standard_Integer I)const
95 if (I <= 0 || I > myMap.Extent())
96 return Handle(Geom_Curve)();
97 return Handle(Geom_Curve)::DownCast(myMap(I));
101 //=======================================================================
104 //=======================================================================
106 Standard_Integer GeomTools_CurveSet::Index
107 (const Handle(Geom_Curve)& S)const
109 return S.IsNull() ? 0 : myMap.FindIndex(S);
113 //=======================================================================
116 //=======================================================================
118 static void Print(const gp_Pnt P,
119 Standard_OStream& OS,
120 const Standard_Boolean compact)
123 if (!compact) OS << ",";
126 if (!compact) OS << ",";
132 //=======================================================================
135 //=======================================================================
137 static void Print(const gp_Dir D,
138 Standard_OStream& OS,
139 const Standard_Boolean compact)
142 if (!compact) OS << ",";
145 if (!compact) OS << ",";
152 //=======================================================================
155 //=======================================================================
157 static void Print(const Handle(Geom_Line)& L,
158 Standard_OStream& OS,
159 const Standard_Boolean compact)
167 if (!compact) OS << "\n Origin :";
168 Print(C.Location(),OS,compact);
169 if (!compact) OS << "\n Axis :";
170 Print(C.Direction(),OS,compact);
171 if (!compact) OS << "\n";
175 //=======================================================================
178 //=======================================================================
180 static void Print(const Handle(Geom_Circle)& CC,
181 Standard_OStream& OS,
182 const Standard_Boolean compact)
189 gp_Circ C = CC->Circ();
190 if (!compact) OS << "\n Center :";
191 Print(C.Location(),OS,compact);
192 if (!compact) OS << "\n Axis :";
193 Print(C.Axis().Direction(),OS,compact);
194 if (!compact) OS << "\n XAxis :";
195 Print(C.XAxis().Direction(),OS,compact);
196 if (!compact) OS << "\n YAxis :";
197 Print(C.YAxis().Direction(),OS,compact);
198 if (!compact) OS << "\n Radius :";
200 if (!compact) OS << "\n";
204 //=======================================================================
207 //=======================================================================
209 static void Print(const Handle(Geom_Ellipse)& E,
210 Standard_OStream& OS,
211 const Standard_Boolean compact)
214 OS << ELLIPSE << " ";
218 gp_Elips C = E->Elips();
219 if (!compact) OS << "\n Center :";
220 Print(C.Location(),OS,compact);
221 if (!compact) OS << "\n Axis :";
222 Print(C.Axis().Direction(),OS,compact);
223 if (!compact) OS << "\n XAxis :";
224 Print(C.XAxis().Direction(),OS,compact);
225 if (!compact) OS << "\n YAxis :";
226 Print(C.YAxis().Direction(),OS,compact);
227 if (!compact) OS << "\n Radii :";
228 OS << C.MajorRadius();
229 if (!compact) OS << ",";
231 OS << C.MinorRadius();
232 if (!compact) OS << "\n";
236 //=======================================================================
239 //=======================================================================
241 static void Print(const Handle(Geom_Parabola)& P,
242 Standard_OStream& OS,
243 const Standard_Boolean compact)
246 OS << PARABOLA << " ";
250 gp_Parab C = P->Parab();
251 if (!compact) OS << "\n Center :";
252 Print(C.Location(),OS,compact);
253 if (!compact) OS << "\n Axis :";
254 Print(C.Axis().Direction(),OS,compact);
255 if (!compact) OS << "\n XAxis :";
256 Print(C.XAxis().Direction(),OS,compact);
257 if (!compact) OS << "\n YAxis :";
258 Print(C.YAxis().Direction(),OS,compact);
259 if (!compact) OS << "\n Focal :";
261 if (!compact) OS << "\n";
265 //=======================================================================
268 //=======================================================================
270 static void Print(const Handle(Geom_Hyperbola)& H,
271 Standard_OStream& OS,
272 const Standard_Boolean compact)
275 OS << HYPERBOLA << " ";
279 gp_Hypr C = H->Hypr();
280 if (!compact) OS << "\n Center :";
281 Print(C.Location(),OS,compact);
282 if (!compact) OS << "\n Axis :";
283 Print(C.Axis().Direction(),OS,compact);
284 if (!compact) OS << "\n XAxis :";
285 Print(C.XAxis().Direction(),OS,compact);
286 if (!compact) OS << "\n YAxis :";
287 Print(C.YAxis().Direction(),OS,compact);
288 if (!compact) OS << "\n Radii :";
289 OS << C.MajorRadius();
290 if (!compact) OS << ",";
292 OS << C.MinorRadius();
293 if (!compact) OS << "\n";
297 //=======================================================================
300 //=======================================================================
302 static void Print(const Handle(Geom_BezierCurve)& B,
303 Standard_OStream& OS,
304 const Standard_Boolean compact)
311 Standard_Boolean rational = B->IsRational();
313 OS << (rational ? 1 : 0) << " ";
320 Standard_Integer i,degree = B->Degree();
321 if (!compact) OS << "\n Degree :";
324 for (i = 1; i <= degree+1; i++) {
325 if (!compact) OS << "\n "<<std::setw(2)<<i<<" : ";
326 Print(B->Pole(i),OS,compact);
328 OS << " " << B->Weight(i);
333 if (!compact) OS << "\n";
336 //=======================================================================
339 //=======================================================================
341 static void Print(const Handle(Geom_BSplineCurve)& B,
342 Standard_OStream& OS,
343 const Standard_Boolean compact)
346 OS << BSPLINE << " ";
348 OS << "BSplineCurve";
351 Standard_Boolean rational = B->IsRational();
353 OS << (rational ? 1 : 0) << " ";
359 Standard_Boolean periodic = B->IsPeriodic();
361 OS << (periodic ? 1 : 0)<< " ";
368 Standard_Integer i,degree,nbpoles,nbknots;
369 degree = B->Degree();
370 nbpoles = B->NbPoles();
371 nbknots = B->NbKnots();
372 if (!compact) OS << "\n Degree ";
375 if (!compact) OS << ",";
378 if (!compact) OS << " Poles,";
380 OS << nbknots << " ";
381 if (!compact) OS << " Knots\n";
383 if (!compact) OS << "Poles :\n";
384 for (i = 1; i <= nbpoles; i++) {
385 if (!compact) OS << "\n "<<std::setw(2)<<i<<" : ";
387 Print(B->Pole(i),OS,compact);
389 OS << " " << B->Weight(i);
393 if (!compact) OS << "Knots :\n";
394 for (i = 1; i <= nbknots; i++) {
395 if (!compact) OS << "\n "<<std::setw(2)<<i<<" : ";
396 OS << " " << B->Knot(i) << " " << B->Multiplicity(i);
400 if (!compact) OS << "\n";
403 //=======================================================================
406 //=======================================================================
408 static void Print(const Handle(Geom_TrimmedCurve)& C,
409 Standard_OStream& OS,
410 const Standard_Boolean compact)
413 OS << TRIMMED << " ";
415 OS << "Trimmed curve\n";
416 if (!compact) OS << "Parameters : ";
417 OS << C->FirstParameter() << " " << C->LastParameter() << "\n";
418 if (!compact) OS << "Basis curve :\n";
419 GeomTools_CurveSet::PrintCurve(C->BasisCurve(),OS,compact);
422 //=======================================================================
425 //=======================================================================
427 static void Print(const Handle(Geom_OffsetCurve)& C,
428 Standard_OStream& OS,
429 const Standard_Boolean compact)
435 if (!compact) OS << "Offset : ";
436 OS << C->Offset() << "\n";
437 if (!compact) OS << "Direction : ";
438 Print(C->Direction(),OS,compact);
440 if (!compact) OS << "Basis curve :\n";
441 GeomTools_CurveSet::PrintCurve(C->BasisCurve(),OS,compact);
444 //=======================================================================
447 //=======================================================================
449 void GeomTools_CurveSet::PrintCurve(const Handle(Geom_Curve)& C,
450 Standard_OStream& OS,
451 const Standard_Boolean compact)
453 Handle(Standard_Type) TheType = C->DynamicType();
455 if ( TheType ==STANDARD_TYPE(Geom_Line)) {
456 Print(Handle(Geom_Line)::DownCast(C),OS,compact);
458 else if ( TheType == STANDARD_TYPE(Geom_Circle)) {
459 Print(Handle(Geom_Circle)::DownCast(C),OS,compact);
461 else if ( TheType == STANDARD_TYPE(Geom_Ellipse)) {
462 Print(Handle(Geom_Ellipse)::DownCast(C),OS,compact);
464 else if ( TheType == STANDARD_TYPE(Geom_Parabola)) {
465 Print(Handle(Geom_Parabola)::DownCast(C),OS,compact);
467 else if ( TheType == STANDARD_TYPE(Geom_Hyperbola)) {
468 Print(Handle(Geom_Hyperbola)::DownCast(C),OS,compact);
470 else if ( TheType == STANDARD_TYPE(Geom_BezierCurve)) {
471 Print(Handle(Geom_BezierCurve)::DownCast(C),OS,compact);
473 else if ( TheType == STANDARD_TYPE(Geom_BSplineCurve)) {
474 Print(Handle(Geom_BSplineCurve)::DownCast(C),OS,compact);
476 else if ( TheType == STANDARD_TYPE(Geom_TrimmedCurve)) {
477 Print(Handle(Geom_TrimmedCurve)::DownCast(C),OS,compact);
479 else if ( TheType == STANDARD_TYPE(Geom_OffsetCurve)) {
480 Print(Handle(Geom_OffsetCurve)::DownCast(C),OS,compact);
483 GeomTools::GetUndefinedTypeHandler()->PrintCurve(C,OS,compact);
485 // OS << "****** UNKNOWN CURVE TYPE ******\n";
487 // std::cout << "****** UNKNOWN CURVE TYPE ******" << std::endl;
491 //=======================================================================
494 //=======================================================================
496 void GeomTools_CurveSet::Dump(Standard_OStream& OS)const
498 Standard_Integer i, nbsurf = myMap.Extent();
499 OS << "\n -------\n";
500 OS << "Dump of "<< nbsurf << " Curves ";
501 OS << "\n -------\n\n";
503 for (i = 1; i <= nbsurf; i++) {
504 OS << std::setw(4) << i << " : ";
505 PrintCurve(Handle(Geom_Curve)::DownCast(myMap(i)),OS,Standard_False);
510 //=======================================================================
513 //=======================================================================
515 void GeomTools_CurveSet::Write(Standard_OStream& OS, const Message_ProgressRange& theProgress)const
517 std::streamsize prec = OS.precision(17);
519 Standard_Integer i, nbcurve = myMap.Extent();
520 OS << "Curves "<< nbcurve << "\n";
521 Message_ProgressScope aPS(theProgress, "3D Curves", nbcurve);
522 for (i = 1; i <= nbcurve && aPS.More(); i++, aPS.Next()) {
523 PrintCurve(Handle(Geom_Curve)::DownCast(myMap(i)),OS,Standard_True);
529 //=======================================================================
532 //=======================================================================
534 static Standard_IStream& operator>>(Standard_IStream& IS, gp_Pnt& P)
536 Standard_Real X=0.,Y=0.,Z=0.;
537 GeomTools::GetReal(IS, X);
538 GeomTools::GetReal(IS, Y);
539 GeomTools::GetReal(IS, Z);
544 //=======================================================================
547 //=======================================================================
549 static Standard_IStream& operator>>(Standard_IStream& IS, gp_Dir& D)
551 Standard_Real X=0.,Y=0.,Z=0.;
552 GeomTools::GetReal(IS, X);
553 GeomTools::GetReal(IS, Y);
554 GeomTools::GetReal(IS, Z);
560 //=======================================================================
561 //function : ReadCurve
563 //=======================================================================
565 static Standard_IStream& operator>>(Standard_IStream& IS,
566 Handle(Geom_Line)& L)
571 L = new Geom_Line(P,AX);
575 //=======================================================================
576 //function : ReadCurve
578 //=======================================================================
580 static Standard_IStream& operator>>(Standard_IStream& IS,
581 Handle(Geom_Circle)& C)
584 gp_Dir A(1.,0.,0.),AX(1.,0.,0.),AY(1.,0.,0.);
586 IS >> P >> A >> AX >> AY;
587 GeomTools::GetReal(IS, R);
588 C = new Geom_Circle(gp_Ax2(P,A,AX),R);
592 //=======================================================================
593 //function : ReadCurve
595 //=======================================================================
597 static Standard_IStream& operator>>(Standard_IStream& IS,
598 Handle(Geom_Ellipse)& E)
601 gp_Dir A(1.,0.,0.),AX(1.,0.,0.),AY(1.,0.,0.);
602 Standard_Real R1=0.,R2=0.;
603 IS >> P >> A >> AX >> AY;
604 GeomTools::GetReal(IS, R1);
605 GeomTools::GetReal(IS, R2);
606 E = new Geom_Ellipse(gp_Ax2(P,A,AX),R1,R2);
610 //=======================================================================
611 //function : ReadCurve
613 //=======================================================================
615 static Standard_IStream& operator>>(Standard_IStream& IS,
616 Handle(Geom_Parabola)& C)
619 gp_Dir A(1.,0.,0.),AX(1.,0.,0.),AY(1.,0.,0.);
621 IS >> P >> A >> AX >> AY;
622 GeomTools::GetReal(IS, R1);
623 C = new Geom_Parabola(gp_Ax2(P,A,AX),R1);
627 //=======================================================================
628 //function : ReadCurve
630 //=======================================================================
632 static Standard_IStream& operator>>(Standard_IStream& IS,
633 Handle(Geom_Hyperbola)& H)
636 gp_Dir A(1.,0.,0.),AX(1.,0.,0.),AY(1.,0.,0.);
637 Standard_Real R1=0.,R2=0.;
638 IS >> P >> A >> AX >> AY;
639 GeomTools::GetReal(IS, R1);
640 GeomTools::GetReal(IS, R2);
641 H = new Geom_Hyperbola(gp_Ax2(P,A,AX),R1,R2);
645 //=======================================================================
646 //function : ReadCurve
648 //=======================================================================
650 static Standard_IStream& operator>>(Standard_IStream& IS,
651 Handle(Geom_BezierCurve)& B)
653 Standard_Boolean rational=Standard_False;
657 Standard_Integer i=0,degree=0;
660 TColgp_Array1OfPnt poles(1,degree+1);
661 TColStd_Array1OfReal weights(1,degree+1);
663 for (i = 1; i <= degree+1; i++) {
666 GeomTools::GetReal(IS, weights(i));
670 B = new Geom_BezierCurve(poles,weights);
672 B = new Geom_BezierCurve(poles);
677 //=======================================================================
678 //function : ReadCurve
680 //=======================================================================
682 static Standard_IStream& operator>>(Standard_IStream& IS,
683 Handle(Geom_BSplineCurve)& B)
686 Standard_Boolean rational=Standard_False,periodic=Standard_False;
687 IS >> rational >> periodic;
690 Standard_Integer i=0,degree=0,nbpoles=0,nbknots=0;
691 IS >> degree >> nbpoles >> nbknots;
693 TColgp_Array1OfPnt poles(1,nbpoles);
694 TColStd_Array1OfReal weights(1,nbpoles);
696 for (i = 1; i <= nbpoles; i++) {
699 GeomTools::GetReal(IS, weights(i));
702 TColStd_Array1OfReal knots(1,nbknots);
703 TColStd_Array1OfInteger mults(1,nbknots);
705 for (i = 1; i <= nbknots; i++) {
706 GeomTools::GetReal(IS, knots(i));
711 B = new Geom_BSplineCurve(poles,weights,knots,mults,degree,periodic);
713 B = new Geom_BSplineCurve(poles,knots,mults,degree,periodic);
718 //=======================================================================
719 //function : ReadCurve
721 //=======================================================================
723 static Standard_IStream& operator>>(Standard_IStream& IS,
724 Handle(Geom_TrimmedCurve)& C)
726 Standard_Real p1=0.,p2=0.;
727 GeomTools::GetReal(IS, p1);
728 GeomTools::GetReal(IS, p2);
729 Handle(Geom_Curve) BC = GeomTools_CurveSet::ReadCurve(IS);
730 C = new Geom_TrimmedCurve(BC,p1,p2);
734 //=======================================================================
735 //function : ReadCurve
737 //=======================================================================
739 static Standard_IStream& operator>>(Standard_IStream& IS,
740 Handle(Geom_OffsetCurve)& C)
743 GeomTools::GetReal(IS, p);
746 Handle(Geom_Curve) BC = GeomTools_CurveSet::ReadCurve(IS);
747 C = new Geom_OffsetCurve(BC,p,D);
751 //=======================================================================
752 //function : ReadCurve
754 //=======================================================================
756 Handle(Geom_Curve) GeomTools_CurveSet::ReadCurve (Standard_IStream& IS)
758 Standard_Integer ctype;
760 Handle(Geom_Curve) C;
768 Handle(Geom_Line) CC;
776 Handle(Geom_Circle) CC;
784 Handle(Geom_Ellipse) CC;
792 Handle(Geom_Parabola) CC;
800 Handle(Geom_Hyperbola) CC;
808 Handle(Geom_BezierCurve) CC;
816 Handle(Geom_BSplineCurve) CC;
824 Handle(Geom_TrimmedCurve) CC;
832 Handle(Geom_OffsetCurve) CC;
840 Handle(Geom_Curve) CC;
841 GeomTools::GetUndefinedTypeHandler()->ReadCurve(ctype,IS,CC);
846 catch(Standard_Failure const& anException) {
848 std::cout <<"EXCEPTION in GeomTools_CurveSet::ReadCurve(..)!!!" << std::endl;
849 std::cout << anException << std::endl;
856 //=======================================================================
859 //=======================================================================
861 void GeomTools_CurveSet::Read(Standard_IStream& IS, const Message_ProgressRange& theProgress)
865 if (strcmp(buffer,"Curves")) {
866 std::cout << "Not a Curve table"<<std::endl;
870 Standard_Integer i, nbcurve;
872 Message_ProgressScope aPS(theProgress, "3D Curves", nbcurve);
873 for (i = 1; i <= nbcurve && aPS.More(); i++, aPS.Next()) {
874 Handle(Geom_Curve) C = GeomTools_CurveSet::ReadCurve (IS);