0031642: Visualization - crash in Graphic3d_Structure::SetVisual() on redisplaying...
[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
42cf5bc1 16
7fd59977 17#include <BinTools.hxx>
42cf5bc1 18#include <BinTools_CurveSet.hxx>
19#include <Geom_BezierCurve.hxx>
20#include <Geom_BSplineCurve.hxx>
7fd59977 21#include <Geom_Circle.hxx>
42cf5bc1 22#include <Geom_Curve.hxx>
7fd59977 23#include <Geom_Ellipse.hxx>
7fd59977 24#include <Geom_Hyperbola.hxx>
42cf5bc1 25#include <Geom_Line.hxx>
7fd59977 26#include <Geom_OffsetCurve.hxx>
42cf5bc1 27#include <Geom_Parabola.hxx>
28#include <Geom_TrimmedCurve.hxx>
7fd59977 29#include <gp_Circ.hxx>
30#include <gp_Elips.hxx>
7fd59977 31#include <gp_Hypr.hxx>
42cf5bc1 32#include <gp_Lin.hxx>
33#include <gp_Parab.hxx>
7fd59977 34#include <Standard_ErrorHandler.hxx>
42cf5bc1 35#include <Standard_Failure.hxx>
36#include <Standard_OutOfRange.hxx>
37#include <TColgp_Array1OfPnt.hxx>
38#include <TColStd_Array1OfInteger.hxx>
39#include <TColStd_Array1OfReal.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{
7fd59977 314 Handle(Standard_Type) TheType = C->DynamicType();
315 try {
316 OCC_CATCH_SIGNALS
317 if ( TheType ==STANDARD_TYPE(Geom_Line)) {
318 OS << Handle(Geom_Line)::DownCast(C);
319 }
320 else if ( TheType == STANDARD_TYPE(Geom_Circle)) {
321 OS << Handle(Geom_Circle)::DownCast(C);
322 }
323 else if ( TheType == STANDARD_TYPE(Geom_Ellipse)) {
324 OS << Handle(Geom_Ellipse)::DownCast(C);
325 }
326 else if ( TheType == STANDARD_TYPE(Geom_Parabola)) {
327 OS << Handle(Geom_Parabola)::DownCast(C);
328 }
329 else if ( TheType == STANDARD_TYPE(Geom_Hyperbola)) {
330 OS << Handle(Geom_Hyperbola)::DownCast(C);
331 }
332 else if ( TheType == STANDARD_TYPE(Geom_BezierCurve)) {
333 OS << Handle(Geom_BezierCurve)::DownCast(C);
334 }
335 else if ( TheType == STANDARD_TYPE(Geom_BSplineCurve)) {
336 OS << Handle(Geom_BSplineCurve)::DownCast(C);
337 }
338 else if ( TheType == STANDARD_TYPE(Geom_TrimmedCurve)) {
339 OS << Handle(Geom_TrimmedCurve)::DownCast(C);
340 }
341 else if ( TheType == STANDARD_TYPE(Geom_OffsetCurve)) {
342 OS << Handle(Geom_OffsetCurve)::DownCast(C);
343 }
344 else {
9775fa61 345 throw Standard_Failure("UNKNOWN CURVE TYPE");
7fd59977 346 }
347 }
9775fa61 348 catch(Standard_Failure const& anException) {
4525373b 349 Standard_SStream aMsg;
04232180 350 aMsg << "EXCEPTION in BinTools_CurveSet::WriteCurve(..)" << std::endl;
351 aMsg << anException << std::endl;
9775fa61 352 throw Standard_Failure(aMsg.str().c_str());
7fd59977 353 }
354}
355
356//=======================================================================
357//function : Write
358//purpose :
359//=======================================================================
360
361void BinTools_CurveSet::Write(Standard_OStream& OS)const
362{
363 Standard_Integer i, nbsurf = myMap.Extent();
364 OS << "Curves "<< nbsurf << "\n";
365 for (i = 1; i <= nbsurf; i++) {
366 WriteCurve(Handle(Geom_Curve)::DownCast(myMap(i)),OS);
367 }
368}
369
370
371//=======================================================================
372//function : ReadPnt
373//purpose :
374//=======================================================================
375
376static Standard_IStream& operator>>(Standard_IStream& IS, gp_Pnt& P)
377{
378 Standard_Real X=0.,Y=0.,Z=0.;
379 BinTools::GetReal(IS, X);
380 BinTools::GetReal(IS, Y);
381 BinTools::GetReal(IS, Z);
382 P.SetCoord(X,Y,Z);
383 return IS;
384}
385
386//=======================================================================
387//function : ReadDir
388//purpose :
389//=======================================================================
390
391static Standard_IStream& operator>>(Standard_IStream& IS, gp_Dir& D)
392{
393 Standard_Real X=0.,Y=0.,Z=0.;
394 BinTools::GetReal(IS, X);
395 BinTools::GetReal(IS, Y);
396 BinTools::GetReal(IS, Z);
397 D.SetCoord(X,Y,Z);
398 return IS;
399}
400
401
402//=======================================================================
403//function : ReadCurve
404//purpose :
405//=======================================================================
406
407static Standard_IStream& operator>>(Standard_IStream& IS,
408 Handle(Geom_Line)& L)
409{
410 gp_Pnt P(0.,0.,0.);
411 gp_Dir AX(1.,0.,0.);
412 IS >> P >> AX;
413 L = new Geom_Line(P,AX);
414 return IS;
415}
416
417//=======================================================================
418//function : ReadCurve
419//purpose :
420//=======================================================================
421
422static Standard_IStream& operator>>(Standard_IStream& IS,
423 Handle(Geom_Circle)& C)
424{
425 gp_Pnt P(0.,0.,0.);
426 gp_Dir A(1.,0.,0.),AX(1.,0.,0.),AY(1.,0.,0.);
427 Standard_Real R=0.;
428 IS >> P >> A >> AX >> AY;
429 BinTools::GetReal(IS, R);
430 C = new Geom_Circle(gp_Ax2(P,A,AX),R);
431 return IS;
432}
433
434//=======================================================================
435//function : ReadCurve
436//purpose :
437//=======================================================================
438
439static Standard_IStream& operator>>(Standard_IStream& IS,
440 Handle(Geom_Ellipse)& E)
441{
442 gp_Pnt P(0.,0.,0.);
443 gp_Dir A(1.,0.,0.),AX(1.,0.,0.),AY(1.,0.,0.);
444 Standard_Real R1=0.,R2=0.;
445 IS >> P >> A >> AX >> AY;
446 BinTools::GetReal(IS, R1);
447 BinTools::GetReal(IS, R2);
448 E = new Geom_Ellipse(gp_Ax2(P,A,AX),R1,R2);
449 return IS;
450}
451
452//=======================================================================
453//function : ReadCurve
454//purpose :
455//=======================================================================
456
457static Standard_IStream& operator>>(Standard_IStream& IS,
458 Handle(Geom_Parabola)& C)
459{
460 gp_Pnt P(0.,0.,0.);
461 gp_Dir A(1.,0.,0.),AX(1.,0.,0.),AY(1.,0.,0.);
462 Standard_Real R1=0.;
463 IS >> P >> A >> AX >> AY;
464 BinTools::GetReal(IS, R1);
465 C = new Geom_Parabola(gp_Ax2(P,A,AX),R1);
466 return IS;
467}
468
469//=======================================================================
470//function : ReadCurve
471//purpose :
472//=======================================================================
473
474static Standard_IStream& operator>>(Standard_IStream& IS,
475 Handle(Geom_Hyperbola)& H)
476{
477 gp_Pnt P(0.,0.,0.);
478 gp_Dir A(1.,0.,0.),AX(1.,0.,0.),AY(1.,0.,0);
479 Standard_Real R1=0.,R2=0.;
480 IS >> P >> A >> AX >> AY;
481 BinTools::GetReal(IS, R1);
482 BinTools::GetReal(IS, R2);
483 H = new Geom_Hyperbola(gp_Ax2(P,A,AX),R1,R2);
484 return IS;
485}
486
487//=======================================================================
488//function : ReadCurve
489//purpose :
490//=======================================================================
491
492static Standard_IStream& operator>>(Standard_IStream& IS,
493 Handle(Geom_BezierCurve)& B)
494{
495 Standard_Boolean rational=Standard_False;
496 BinTools::GetBool(IS, rational);
497
498// poles and weights
499 Standard_Integer i=0,degree=0;
500// degree;
501 Standard_ExtCharacter aVal='\0';
502 BinTools::GetExtChar(IS, aVal);
503 degree = (Standard_Integer)aVal;
504
505 TColgp_Array1OfPnt poles(1,degree+1);
506 TColStd_Array1OfReal weights(1,degree+1);
507
508 for (i = 1; i <= degree+1; i++) {
509 IS >> poles(i);//Pnt
510 if (rational)
511// weights(i);
512 BinTools::GetReal(IS, weights(i));
513 }
514
515 if (rational)
516 B = new Geom_BezierCurve(poles,weights);
517 else
518 B = new Geom_BezierCurve(poles);
519
520 return IS;
521}
522
523//=======================================================================
524//function : ReadCurve
525//purpose :
526//=======================================================================
527
528static Standard_IStream& operator>>(Standard_IStream& IS,
529 Handle(Geom_BSplineCurve)& B)
530{
531
532 Standard_Boolean rational=Standard_False,periodic=Standard_False;
533 BinTools::GetBool(IS, rational);
534 BinTools::GetBool(IS, periodic);
535
536// poles and weights
537 Standard_Integer i=0,degree=0,nbpoles=0,nbknots=0;
538 Standard_ExtCharacter aVal='\0';
539 BinTools::GetExtChar(IS, aVal);
540 degree = (Standard_Integer)aVal;
541
542 BinTools::GetInteger(IS, nbpoles);
543
544 BinTools::GetInteger(IS, nbknots);
545
546 TColgp_Array1OfPnt poles(1,nbpoles);
547 TColStd_Array1OfReal weights(1,nbpoles);
548
549 for (i = 1; i <= nbpoles; i++) {
550 IS >> poles(i);//Pnt
551 if (rational)
552 BinTools::GetReal(IS, weights(i));
553 }
554
555 TColStd_Array1OfReal knots(1,nbknots);
556 TColStd_Array1OfInteger mults(1,nbknots);
557
558 for (i = 1; i <= nbknots; i++) {
559 BinTools::GetReal(IS, knots(i));
560 BinTools::GetInteger(IS, mults(i));
561 }
562
563 if (rational)
564 B = new Geom_BSplineCurve(poles,weights,knots,mults,degree,periodic);
565 else
566 B = new Geom_BSplineCurve(poles,knots,mults,degree,periodic);
567
568 return IS;
569}
570
571//=======================================================================
572//function : ReadCurve
573//purpose :
574//=======================================================================
575
576static Standard_IStream& operator>>(Standard_IStream& IS,
577 Handle(Geom_TrimmedCurve)& C)
578{
579 Standard_Real p1=0.,p2=0.;
580 BinTools::GetReal(IS, p1);//FirstParameter
581 BinTools::GetReal(IS, p2);//LastParameter
582 Handle(Geom_Curve) BC;
583 BinTools_CurveSet::ReadCurve(IS,BC);
584 C = new Geom_TrimmedCurve(BC,p1,p2);
585 return IS;
586}
587
588//=======================================================================
589//function : ReadCurve
590//purpose :
591//=======================================================================
592
593static Standard_IStream& operator>>(Standard_IStream& IS,
594 Handle(Geom_OffsetCurve)& C)
595{
596 Standard_Real p=0.;
597 BinTools::GetReal(IS, p);//Offset
598 gp_Dir D(1.,0.,0.);
599 IS >> D;
600 Handle(Geom_Curve) BC;
601 BinTools_CurveSet::ReadCurve(IS,BC);
602 C = new Geom_OffsetCurve(BC,p,D);
603 return IS;
604}
605
606//=======================================================================
607//function : ReadCurve
608//purpose :
609//=======================================================================
610
611Standard_IStream& BinTools_CurveSet::ReadCurve(Standard_IStream& IS,
612 Handle(Geom_Curve)& C)
613{
7fd59977 614 try {
615 OCC_CATCH_SIGNALS
616 const Standard_Byte ctype = (Standard_Byte) IS.get();
617
618 switch (ctype) {
619
620 case LINE :
621 {
622 Handle(Geom_Line) CC;
623 IS >> CC;
624 C = CC;
625 }
626 break;
627
628 case CIRCLE :
629 {
630 Handle(Geom_Circle) CC;
631 IS >> CC;
632 C = CC;
633 }
634 break;
635
636 case ELLIPSE :
637 {
638 Handle(Geom_Ellipse) CC;
639 IS >> CC;
640 C = CC;
641 }
642 break;
643
644 case PARABOLA :
645 {
646 Handle(Geom_Parabola) CC;
647 IS >> CC;
648 C = CC;
649 }
650 break;
651
652 case HYPERBOLA :
653 {
654 Handle(Geom_Hyperbola) CC;
655 IS >> CC;
656 C = CC;
657 }
658 break;
659
660 case BEZIER :
661 {
662 Handle(Geom_BezierCurve) CC;
663 IS >> CC;
664 C = CC;
665 }
666 break;
667
668 case BSPLINE :
669 {
670 Handle(Geom_BSplineCurve) CC;
671 IS >> CC;
672 C = CC;
673 }
674 break;
675
676 case TRIMMED :
677 {
678 Handle(Geom_TrimmedCurve) CC;
679 IS >> CC;
680 C = CC;
681 }
682 break;
683
684 case OFFSET :
685 {
686 Handle(Geom_OffsetCurve) CC;
687 IS >> CC;
688 C = CC;
689 }
690 break;
691
692 default:
693 {
694 C = NULL;
9775fa61 695 throw Standard_Failure("UNKNOWN CURVE TYPE");
7fd59977 696 }
697 }
698 }
9775fa61 699 catch(Standard_Failure const& anException) {
7fd59977 700 C = NULL;
4525373b 701 Standard_SStream aMsg;
04232180 702 aMsg <<"EXCEPTION in BinTools_CurveSet::ReadCurve(..)" << std::endl;
703 aMsg << anException << std::endl;
9775fa61 704 throw Standard_Failure(aMsg.str().c_str());
7fd59977 705 }
706 return IS;
707}
708
709//=======================================================================
710//function : Read
711//purpose :
712//=======================================================================
713
714void BinTools_CurveSet::Read(Standard_IStream& IS)
715{
716 char buffer[255];
717 IS >> buffer;
718 if (IS.fail() || strcmp(buffer,"Curves")) {
719 Standard_SStream aMsg;
04232180 720 aMsg << "BinTools_CurveSet::Read: Not a Curve table"<<std::endl;
0797d9d3 721#ifdef OCCT_DEBUG
04232180 722 std::cout <<"CurveSet buffer: " << buffer << std::endl;
7fd59977 723#endif
9775fa61 724 throw Standard_Failure(aMsg.str().c_str());
7fd59977 725 return;
726 }
727
728 Handle(Geom_Curve) C;
729 Standard_Integer i, nbcurve;
730 IS >> nbcurve;
731
732 IS.get();//remove <lf>
733 for (i = 1; i <= nbcurve; i++) {
734 BinTools_CurveSet::ReadCurve(IS,C);
735 myMap.Add(C);
736 }
737}
738
739