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