0024947: Redesign OCCT legacy type system -- automatic
[occt.git] / src / BinTools / BinTools_CurveSet.cxx
CommitLineData
b311480e 1// Created on: 2004-05-20
2// Created by: Sergey ZARITCHNY
973c2be1 3// Copyright (c) 2004-2014 OPEN CASCADE SAS
b311480e 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
b311480e 6//
d5f74e42 7// This library is free software; you can redistribute it and/or modify it under
8// the terms of the GNU Lesser General Public License version 2.1 as published
973c2be1 9// by the Free Software Foundation, with special exception defined in the file
10// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11// distribution for complete text of the license and disclaimer of any warranty.
b311480e 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
7fd59977 15
16#include <BinTools_CurveSet.ixx>
17#include <BinTools.hxx>
18#include <Geom_Circle.hxx>
19#include <Geom_Line.hxx>
20#include <Geom_Ellipse.hxx>
21#include <Geom_Parabola.hxx>
22#include <Geom_Hyperbola.hxx>
23#include <Geom_BezierCurve.hxx>
24#include <Geom_BSplineCurve.hxx>
25#include <Geom_TrimmedCurve.hxx>
26#include <Geom_OffsetCurve.hxx>
27
28#include <gp_Lin.hxx>
29#include <gp_Circ.hxx>
30#include <gp_Elips.hxx>
31#include <gp_Parab.hxx>
32#include <gp_Hypr.hxx>
33
34#include <TColStd_Array1OfReal.hxx>
35#include <TColStd_Array1OfInteger.hxx>
36#include <TColgp_Array1OfPnt.hxx>
37#include <Standard_Failure.hxx>
38#include <Standard_ErrorHandler.hxx>
ec357c5c 39#include <Geom_Curve.hxx>
7fd59977 40
41#define LINE 1
42#define CIRCLE 2
43#define ELLIPSE 3
44#define PARABOLA 4
45#define HYPERBOLA 5
46#define BEZIER 6
47#define BSPLINE 7
48#define TRIMMED 8
49#define OFFSET 9
50
51//=======================================================================
52//function : BinTools_CurveSet
53//purpose :
54//=======================================================================
55
56BinTools_CurveSet::BinTools_CurveSet()
57{
58}
59
60
61//=======================================================================
62//function : Clear
63//purpose :
64//=======================================================================
65
66void BinTools_CurveSet::Clear()
67{
68 myMap.Clear();
69}
70
71
72//=======================================================================
73//function : Add
74//purpose :
75//=======================================================================
76
77Standard_Integer BinTools_CurveSet::Add(const Handle(Geom_Curve)& C)
78{
79 return (C.IsNull()) ? 0 : myMap.Add(C);
80}
81
82
83//=======================================================================
84//function : Curve
85//purpose :
86//=======================================================================
87
88Handle(Geom_Curve) BinTools_CurveSet::Curve
89 (const Standard_Integer I)const
90{
91 if (I == 0) {
92 Handle(Geom_Curve) dummy;
93 return dummy;
94 }
95 else
96 return Handle(Geom_Curve)::DownCast(myMap(I));
97}
98
99
100//=======================================================================
101//function : Index
102//purpose :
103//=======================================================================
104
105Standard_Integer BinTools_CurveSet::Index
106 (const Handle(Geom_Curve)& S)const
107{
108 return S.IsNull() ? 0 : myMap.FindIndex(S);
109}
110
111
112//=======================================================================
113//function : operator << (gp_Pnt)
114//purpose :
115//=======================================================================
116
117static Standard_OStream& operator <<(Standard_OStream& OS, const gp_Pnt P)
118{
119 BinTools::PutReal(OS, P.X());
120 BinTools::PutReal(OS, P.Y());
121 BinTools::PutReal(OS, P.Z());
122 return OS;
123}
124
125//=======================================================================
126//function : operator << (gp_Dir)
127//purpose :
128//=======================================================================
129
130static Standard_OStream& operator <<(Standard_OStream& OS, const gp_Dir D)
131{
132 BinTools::PutReal(OS, D.X());
133 BinTools::PutReal(OS, D.Y());
134 BinTools::PutReal(OS, D.Z());
135 return OS;
136}
137
138
139//=======================================================================
140//function : operator << (Geom_Line)
141//purpose :
142//=======================================================================
143
144static Standard_OStream& operator <<(Standard_OStream& OS, const Handle(Geom_Line)& L)
145{
146 OS << (Standard_Byte)LINE;
147 gp_Lin C = L->Lin();
148 OS << C.Location();//Pnt
149 OS << C.Direction();//Dir
150 return OS;
151
152}
153
154//=======================================================================
155//function : operator <<(Geom_Circle)
156//purpose :
157//=======================================================================
158
159static Standard_OStream& operator <<(Standard_OStream& OS, const Handle(Geom_Circle)& CC)
160{
161 OS << (Standard_Byte)CIRCLE;
162 gp_Circ C = CC->Circ();
163 OS << C.Location();
164 OS << C.Axis().Direction();
165 OS << C.XAxis().Direction();
166 OS << C.YAxis().Direction();
167 BinTools::PutReal(OS, C.Radius());
168 return OS;
169}
170
171//=======================================================================
172//function : operator <<(Geom_Ellipse)
173//purpose :
174//=======================================================================
175
176static Standard_OStream& operator <<(Standard_OStream& OS, const Handle(Geom_Ellipse)& E)
177{
178 OS << (Standard_Byte)ELLIPSE;
179 gp_Elips C = E->Elips();
180 OS << C.Location();
181 OS << C.Axis().Direction();
182 OS << C.XAxis().Direction();
183 OS << C.YAxis().Direction();
184 BinTools::PutReal(OS, C.MajorRadius());
185 BinTools::PutReal(OS, C.MinorRadius());
186 return OS;
187}
188
189//=======================================================================
190//function : operator <<(Geom_Parabola)
191//purpose :
192//=======================================================================
193
194static Standard_OStream& operator <<(Standard_OStream& OS, const Handle(Geom_Parabola)& P)
195{
196 OS << (Standard_Byte)PARABOLA;
197 gp_Parab C = P->Parab();
198 OS << C.Location();
199 OS << C.Axis().Direction();
200 OS << C.XAxis().Direction();
201 OS << C.YAxis().Direction();
202 BinTools::PutReal(OS, C.Focal());
203 return OS;
204}
205
206//=======================================================================
207//function : operator <<(Geom_Hyperbola)
208//purpose :
209//=======================================================================
210
211static Standard_OStream& operator <<(Standard_OStream& OS, const Handle(Geom_Hyperbola)& H)
212{
213 OS << (Standard_Byte)HYPERBOLA;
214 gp_Hypr C = H->Hypr();
215 OS << C.Location();
216 OS << C.Axis().Direction();
217 OS << C.XAxis().Direction();
218 OS << C.YAxis().Direction();
219 BinTools::PutReal(OS, C.MajorRadius());
220 BinTools::PutReal(OS, C.MinorRadius());
221 return OS;
222}
223
224//=======================================================================
225//function : operator <<(Geom_BezierCurve)
226//purpose :
227//=======================================================================
228
229static Standard_OStream& operator <<(Standard_OStream& OS, const Handle(Geom_BezierCurve)& B)
230{
231 OS << (Standard_Byte)BEZIER;
232 Standard_Boolean aRational = B->IsRational() ? 1:0;
233 BinTools::PutBool(OS, aRational); //rational
234 // poles and weights
235 Standard_Integer i,aDegree = B->Degree();
236 BinTools::PutExtChar(OS, (Standard_ExtCharacter)aDegree); //<< Degree
237 for (i = 1; i <= aDegree+1; i++) {
238 OS << B->Pole(i); //Pnt
239 if (aRational)
240 BinTools::PutReal(OS, B->Weight(i));//Real
241 }
242 return OS;
243}
244
245//=======================================================================
246//function : operator <<(Geom_BSplineCurve)
247//purpose :
248//=======================================================================
249
250static Standard_OStream& operator <<(Standard_OStream& OS, const Handle(Geom_BSplineCurve)& B)
251{
252 OS << (Standard_Byte)BSPLINE;
253 Standard_Boolean aRational = B->IsRational() ? 1:0;
254 BinTools::PutBool(OS, aRational); //rational
255 Standard_Boolean aPeriodic = B->IsPeriodic() ? 1:0;
256 BinTools::PutBool(OS, aPeriodic); //periodic
257 // poles and weights
258 Standard_Integer i,aDegree,aNbPoles,aNbKnots;
259 aDegree = B->Degree();
260 aNbPoles = B->NbPoles();
261 aNbKnots = B->NbKnots();
262 BinTools::PutExtChar(OS, (Standard_ExtCharacter) aDegree);
263 BinTools::PutInteger(OS, aNbPoles);
264 BinTools::PutInteger(OS, aNbKnots);
265 for (i = 1; i <= aNbPoles; i++) {
266 OS << B->Pole(i); // Pnt
267 if (aRational)
268 BinTools::PutReal(OS, B->Weight(i));
269 }
270
271 for (i = 1; i <= aNbKnots; i++) {
272 BinTools::PutReal(OS, B->Knot(i));
273 BinTools::PutInteger(OS, B->Multiplicity(i));
274 }
275 return OS;
276}
277
278//=======================================================================
279//function : operator <<(Geom_TrimmedCurve)
280//purpose :
281//=======================================================================
282
283static Standard_OStream& operator <<(Standard_OStream& OS, const Handle(Geom_TrimmedCurve)& C)
284{
285 OS << (Standard_Byte)TRIMMED;
286 BinTools::PutReal(OS, C->FirstParameter());
287 BinTools::PutReal(OS, C->LastParameter());
288 BinTools_CurveSet::WriteCurve(C->BasisCurve(),OS);
289 return OS;
290}
291
292//=======================================================================
293//function : operator <<(Geom_OffsetCurve)
294//purpose :
295//=======================================================================
296
297static Standard_OStream& operator <<(Standard_OStream& OS, const Handle(Geom_OffsetCurve)& C)
298{
299 OS << (Standard_Byte)OFFSET;
300 BinTools::PutReal(OS,C->Offset());//Offset
301 OS << C->Direction();
302 BinTools_CurveSet::WriteCurve(C->BasisCurve(),OS);
303 return OS;
304}
305
306//=======================================================================
307//function :
308//purpose :
309//=======================================================================
310
311void BinTools_CurveSet::WriteCurve(const Handle(Geom_Curve)& C,
312 Standard_OStream& OS)
313{
314 Standard_SStream aMsg;
315 Handle(Standard_Type) TheType = C->DynamicType();
316 try {
317 OCC_CATCH_SIGNALS
318 if ( TheType ==STANDARD_TYPE(Geom_Line)) {
319 OS << Handle(Geom_Line)::DownCast(C);
320 }
321 else if ( TheType == STANDARD_TYPE(Geom_Circle)) {
322 OS << Handle(Geom_Circle)::DownCast(C);
323 }
324 else if ( TheType == STANDARD_TYPE(Geom_Ellipse)) {
325 OS << Handle(Geom_Ellipse)::DownCast(C);
326 }
327 else if ( TheType == STANDARD_TYPE(Geom_Parabola)) {
328 OS << Handle(Geom_Parabola)::DownCast(C);
329 }
330 else if ( TheType == STANDARD_TYPE(Geom_Hyperbola)) {
331 OS << Handle(Geom_Hyperbola)::DownCast(C);
332 }
333 else if ( TheType == STANDARD_TYPE(Geom_BezierCurve)) {
334 OS << Handle(Geom_BezierCurve)::DownCast(C);
335 }
336 else if ( TheType == STANDARD_TYPE(Geom_BSplineCurve)) {
337 OS << Handle(Geom_BSplineCurve)::DownCast(C);
338 }
339 else if ( TheType == STANDARD_TYPE(Geom_TrimmedCurve)) {
340 OS << Handle(Geom_TrimmedCurve)::DownCast(C);
341 }
342 else if ( TheType == STANDARD_TYPE(Geom_OffsetCurve)) {
343 OS << Handle(Geom_OffsetCurve)::DownCast(C);
344 }
345 else {
346 aMsg << "UNKNOWN CURVE TYPE" <<endl;
347 Standard_Failure::Raise(aMsg);
348 }
349 }
350 catch(Standard_Failure) {
351 aMsg << "EXCEPTION in BinTools_CurveSet::WriteCurve(..)" << endl;
352 Handle(Standard_Failure) anExc = Standard_Failure::Caught();
353 aMsg << anExc << endl;
354 Standard_Failure::Raise(aMsg);
355 }
356}
357
358//=======================================================================
359//function : Write
360//purpose :
361//=======================================================================
362
363void BinTools_CurveSet::Write(Standard_OStream& OS)const
364{
365 Standard_Integer i, nbsurf = myMap.Extent();
366 OS << "Curves "<< nbsurf << "\n";
367 for (i = 1; i <= nbsurf; i++) {
368 WriteCurve(Handle(Geom_Curve)::DownCast(myMap(i)),OS);
369 }
370}
371
372
373//=======================================================================
374//function : ReadPnt
375//purpose :
376//=======================================================================
377
378static Standard_IStream& operator>>(Standard_IStream& IS, gp_Pnt& P)
379{
380 Standard_Real X=0.,Y=0.,Z=0.;
381 BinTools::GetReal(IS, X);
382 BinTools::GetReal(IS, Y);
383 BinTools::GetReal(IS, Z);
384 P.SetCoord(X,Y,Z);
385 return IS;
386}
387
388//=======================================================================
389//function : ReadDir
390//purpose :
391//=======================================================================
392
393static Standard_IStream& operator>>(Standard_IStream& IS, gp_Dir& D)
394{
395 Standard_Real X=0.,Y=0.,Z=0.;
396 BinTools::GetReal(IS, X);
397 BinTools::GetReal(IS, Y);
398 BinTools::GetReal(IS, Z);
399 D.SetCoord(X,Y,Z);
400 return IS;
401}
402
403
404//=======================================================================
405//function : ReadCurve
406//purpose :
407//=======================================================================
408
409static Standard_IStream& operator>>(Standard_IStream& IS,
410 Handle(Geom_Line)& L)
411{
412 gp_Pnt P(0.,0.,0.);
413 gp_Dir AX(1.,0.,0.);
414 IS >> P >> AX;
415 L = new Geom_Line(P,AX);
416 return IS;
417}
418
419//=======================================================================
420//function : ReadCurve
421//purpose :
422//=======================================================================
423
424static Standard_IStream& operator>>(Standard_IStream& IS,
425 Handle(Geom_Circle)& C)
426{
427 gp_Pnt P(0.,0.,0.);
428 gp_Dir A(1.,0.,0.),AX(1.,0.,0.),AY(1.,0.,0.);
429 Standard_Real R=0.;
430 IS >> P >> A >> AX >> AY;
431 BinTools::GetReal(IS, R);
432 C = new Geom_Circle(gp_Ax2(P,A,AX),R);
433 return IS;
434}
435
436//=======================================================================
437//function : ReadCurve
438//purpose :
439//=======================================================================
440
441static Standard_IStream& operator>>(Standard_IStream& IS,
442 Handle(Geom_Ellipse)& E)
443{
444 gp_Pnt P(0.,0.,0.);
445 gp_Dir A(1.,0.,0.),AX(1.,0.,0.),AY(1.,0.,0.);
446 Standard_Real R1=0.,R2=0.;
447 IS >> P >> A >> AX >> AY;
448 BinTools::GetReal(IS, R1);
449 BinTools::GetReal(IS, R2);
450 E = new Geom_Ellipse(gp_Ax2(P,A,AX),R1,R2);
451 return IS;
452}
453
454//=======================================================================
455//function : ReadCurve
456//purpose :
457//=======================================================================
458
459static Standard_IStream& operator>>(Standard_IStream& IS,
460 Handle(Geom_Parabola)& C)
461{
462 gp_Pnt P(0.,0.,0.);
463 gp_Dir A(1.,0.,0.),AX(1.,0.,0.),AY(1.,0.,0.);
464 Standard_Real R1=0.;
465 IS >> P >> A >> AX >> AY;
466 BinTools::GetReal(IS, R1);
467 C = new Geom_Parabola(gp_Ax2(P,A,AX),R1);
468 return IS;
469}
470
471//=======================================================================
472//function : ReadCurve
473//purpose :
474//=======================================================================
475
476static Standard_IStream& operator>>(Standard_IStream& IS,
477 Handle(Geom_Hyperbola)& H)
478{
479 gp_Pnt P(0.,0.,0.);
480 gp_Dir A(1.,0.,0.),AX(1.,0.,0.),AY(1.,0.,0);
481 Standard_Real R1=0.,R2=0.;
482 IS >> P >> A >> AX >> AY;
483 BinTools::GetReal(IS, R1);
484 BinTools::GetReal(IS, R2);
485 H = new Geom_Hyperbola(gp_Ax2(P,A,AX),R1,R2);
486 return IS;
487}
488
489//=======================================================================
490//function : ReadCurve
491//purpose :
492//=======================================================================
493
494static Standard_IStream& operator>>(Standard_IStream& IS,
495 Handle(Geom_BezierCurve)& B)
496{
497 Standard_Boolean rational=Standard_False;
498 BinTools::GetBool(IS, rational);
499
500// poles and weights
501 Standard_Integer i=0,degree=0;
502// degree;
503 Standard_ExtCharacter aVal='\0';
504 BinTools::GetExtChar(IS, aVal);
505 degree = (Standard_Integer)aVal;
506
507 TColgp_Array1OfPnt poles(1,degree+1);
508 TColStd_Array1OfReal weights(1,degree+1);
509
510 for (i = 1; i <= degree+1; i++) {
511 IS >> poles(i);//Pnt
512 if (rational)
513// weights(i);
514 BinTools::GetReal(IS, weights(i));
515 }
516
517 if (rational)
518 B = new Geom_BezierCurve(poles,weights);
519 else
520 B = new Geom_BezierCurve(poles);
521
522 return IS;
523}
524
525//=======================================================================
526//function : ReadCurve
527//purpose :
528//=======================================================================
529
530static Standard_IStream& operator>>(Standard_IStream& IS,
531 Handle(Geom_BSplineCurve)& B)
532{
533
534 Standard_Boolean rational=Standard_False,periodic=Standard_False;
535 BinTools::GetBool(IS, rational);
536 BinTools::GetBool(IS, periodic);
537
538// poles and weights
539 Standard_Integer i=0,degree=0,nbpoles=0,nbknots=0;
540 Standard_ExtCharacter aVal='\0';
541 BinTools::GetExtChar(IS, aVal);
542 degree = (Standard_Integer)aVal;
543
544 BinTools::GetInteger(IS, nbpoles);
545
546 BinTools::GetInteger(IS, nbknots);
547
548 TColgp_Array1OfPnt poles(1,nbpoles);
549 TColStd_Array1OfReal weights(1,nbpoles);
550
551 for (i = 1; i <= nbpoles; i++) {
552 IS >> poles(i);//Pnt
553 if (rational)
554 BinTools::GetReal(IS, weights(i));
555 }
556
557 TColStd_Array1OfReal knots(1,nbknots);
558 TColStd_Array1OfInteger mults(1,nbknots);
559
560 for (i = 1; i <= nbknots; i++) {
561 BinTools::GetReal(IS, knots(i));
562 BinTools::GetInteger(IS, mults(i));
563 }
564
565 if (rational)
566 B = new Geom_BSplineCurve(poles,weights,knots,mults,degree,periodic);
567 else
568 B = new Geom_BSplineCurve(poles,knots,mults,degree,periodic);
569
570 return IS;
571}
572
573//=======================================================================
574//function : ReadCurve
575//purpose :
576//=======================================================================
577
578static Standard_IStream& operator>>(Standard_IStream& IS,
579 Handle(Geom_TrimmedCurve)& C)
580{
581 Standard_Real p1=0.,p2=0.;
582 BinTools::GetReal(IS, p1);//FirstParameter
583 BinTools::GetReal(IS, p2);//LastParameter
584 Handle(Geom_Curve) BC;
585 BinTools_CurveSet::ReadCurve(IS,BC);
586 C = new Geom_TrimmedCurve(BC,p1,p2);
587 return IS;
588}
589
590//=======================================================================
591//function : ReadCurve
592//purpose :
593//=======================================================================
594
595static Standard_IStream& operator>>(Standard_IStream& IS,
596 Handle(Geom_OffsetCurve)& C)
597{
598 Standard_Real p=0.;
599 BinTools::GetReal(IS, p);//Offset
600 gp_Dir D(1.,0.,0.);
601 IS >> D;
602 Handle(Geom_Curve) BC;
603 BinTools_CurveSet::ReadCurve(IS,BC);
604 C = new Geom_OffsetCurve(BC,p,D);
605 return IS;
606}
607
608//=======================================================================
609//function : ReadCurve
610//purpose :
611//=======================================================================
612
613Standard_IStream& BinTools_CurveSet::ReadCurve(Standard_IStream& IS,
614 Handle(Geom_Curve)& C)
615{
616 Standard_SStream aMsg;
617 try {
618 OCC_CATCH_SIGNALS
619 const Standard_Byte ctype = (Standard_Byte) IS.get();
620
621 switch (ctype) {
622
623 case LINE :
624 {
625 Handle(Geom_Line) CC;
626 IS >> CC;
627 C = CC;
628 }
629 break;
630
631 case CIRCLE :
632 {
633 Handle(Geom_Circle) CC;
634 IS >> CC;
635 C = CC;
636 }
637 break;
638
639 case ELLIPSE :
640 {
641 Handle(Geom_Ellipse) CC;
642 IS >> CC;
643 C = CC;
644 }
645 break;
646
647 case PARABOLA :
648 {
649 Handle(Geom_Parabola) CC;
650 IS >> CC;
651 C = CC;
652 }
653 break;
654
655 case HYPERBOLA :
656 {
657 Handle(Geom_Hyperbola) CC;
658 IS >> CC;
659 C = CC;
660 }
661 break;
662
663 case BEZIER :
664 {
665 Handle(Geom_BezierCurve) CC;
666 IS >> CC;
667 C = CC;
668 }
669 break;
670
671 case BSPLINE :
672 {
673 Handle(Geom_BSplineCurve) CC;
674 IS >> CC;
675 C = CC;
676 }
677 break;
678
679 case TRIMMED :
680 {
681 Handle(Geom_TrimmedCurve) CC;
682 IS >> CC;
683 C = CC;
684 }
685 break;
686
687 case OFFSET :
688 {
689 Handle(Geom_OffsetCurve) CC;
690 IS >> CC;
691 C = CC;
692 }
693 break;
694
695 default:
696 {
697 C = NULL;
698 aMsg << "UNKNOWN CURVE TYPE" << endl;
699 Standard_Failure::Raise(aMsg);
700 }
701 }
702 }
703 catch(Standard_Failure) {
704 C = NULL;
705 aMsg <<"EXCEPTION in BinTools_CurveSet::ReadCurve(..)" << endl;
706 Handle(Standard_Failure) anExc = Standard_Failure::Caught();
707 Standard_Failure::Raise(aMsg);
708 }
709 return IS;
710}
711
712//=======================================================================
713//function : Read
714//purpose :
715//=======================================================================
716
717void BinTools_CurveSet::Read(Standard_IStream& IS)
718{
719 char buffer[255];
720 IS >> buffer;
721 if (IS.fail() || strcmp(buffer,"Curves")) {
722 Standard_SStream aMsg;
723 aMsg << "BinTools_CurveSet::Read: Not a Curve table"<<endl;
0797d9d3 724#ifdef OCCT_DEBUG
7fd59977 725 cout <<"CurveSet buffer: " << buffer << endl;
726#endif
727 Standard_Failure::Raise(aMsg);
728 return;
729 }
730
731 Handle(Geom_Curve) C;
732 Standard_Integer i, nbcurve;
733 IS >> nbcurve;
734
735 IS.get();//remove <lf>
736 for (i = 1; i <= nbcurve; i++) {
737 BinTools_CurveSet::ReadCurve(IS,C);
738 myMap.Add(C);
739 }
740}
741
742