0022627: Change OCCT memory management defaults
[occt.git] / src / GGraphic2d / GGraphic2d_SetOfCurves.cxx
1 #include <GGraphic2d_SetOfCurves.ixx>
2 #include <BndLib_Add2dCurve.hxx>
3 #include <Bnd_Box2d.hxx>
4 #include <Geom2dAdaptor_Curve.hxx>
5 #include <TColgp_Array1OfPnt2d.hxx>
6 #include <Geom2d_BezierCurve.hxx>
7 #include <Geom2d_BSplineCurve.hxx>
8 #include <Geom2d_Line.hxx>
9 #include <Geom2d_Circle.hxx>
10 #include <Geom2d_Ellipse.hxx>
11 #include <Geom2d_Parabola.hxx>
12 #include <Geom2d_Hyperbola.hxx>
13 #include <Geom2d_TrimmedCurve.hxx>
14 #include <Geom2d_OffsetCurve.hxx>
15 #include <GCPnts_UniformDeflection.hxx>
16 #include <gp_Pnt2d.hxx>
17 #include <gp_Dir2d.hxx>
18 #include <gp_Circ2d.hxx>
19 #include <gp_Elips2d.hxx>
20 #include <gp_Parab2d.hxx>
21 #include <gp_Hypr2d.hxx>
22
23 #include <gp_Pnt.hxx>
24 #include <TShort_Array1OfShortReal.hxx>
25 #include <Geom2dAPI_ProjectPointOnCurve.hxx>
26 #include <Standard_ErrorHandler.hxx>
27
28 #define G002
29 #define VERTEXMARKER 2
30 #define DEFAULTMARKERSIZE 3.0
31
32 GGraphic2d_SetOfCurves::GGraphic2d_SetOfCurves (
33         const Handle(Graphic2d_GraphicObject)& aGraphicObject)
34
35   : Graphic2d_Line (aGraphicObject) {
36
37 }
38
39 void GGraphic2d_SetOfCurves::Add (const Handle(Geom2d_Curve) &aCurve)
40 {
41         Bnd_Box2d Box;
42         BndLib_Add2dCurve::Add(Geom2dAdaptor_Curve(aCurve),0.,Box);
43         Standard_Real Xmin, Ymin, Xmax, Ymax;
44         Box.Get( Xmin, Ymin, Xmax, Ymax);
45         Standard_ShortReal minX = Standard_ShortReal(Xmin);
46         Standard_ShortReal minY = Standard_ShortReal(Ymin);
47         Standard_ShortReal maxX = Standard_ShortReal(Xmax);
48         Standard_ShortReal maxY = Standard_ShortReal(Ymax);
49         myMinX  = Min(myMinX,minX);
50         myMinY  = Min(myMinY,minY);
51         myMaxX  = Max(myMaxX,maxX);
52         myMaxY  = Max(myMaxY,maxY);
53
54         myCurves.Append(aCurve);
55 }
56
57 Standard_Integer GGraphic2d_SetOfCurves::Length () const {
58    return myCurves.Length();
59 }
60
61 Handle(Geom2d_Curve) GGraphic2d_SetOfCurves::Values(const Standard_Integer aRank) const {
62
63         if( aRank < 1 || aRank > myCurves.Length() ) 
64                 Standard_OutOfRange::Raise
65                         ("the curve rank is out of bounds in the set");
66
67         return myCurves.Value(aRank);
68 }
69
70 void GGraphic2d_SetOfCurves::Draw (const Handle(Graphic2d_Drawer)& aDrawer) {
71 //Standard_Integer i,length = myCurves.Length(),bufferize = 1;
72  Standard_Integer length = myCurves.Length(), bufferize = 1;
73  Standard_Boolean IsIn = Standard_False;
74  Standard_ShortReal minX,minY,maxX,maxY;
75  Standard_Real Xmin,Ymin,Xmax,Ymax;
76
77  if( length <= 0 ) return ;
78
79  gp_GTrsf2d aTrsf = myGOPtr->Transform ();
80  
81   if (! myGOPtr->IsTransformed ())
82     IsIn = aDrawer->IsIn (myMinX,myMaxX,myMinY,myMaxY);
83   else {
84     MinMax(minX,maxX,minY,maxY);
85     IsIn = aDrawer->IsIn (minX,maxX,minY,maxY);
86   }
87
88   if ( IsIn ) {
89     Standard_ShortReal Def;
90     Standard_ShortReal Coeff;
91     Aspect_TypeOfDeflection Type;
92     Standard_Boolean Controll = Standard_True;
93     Handle(Geom2d_Curve) aCurve;
94     Standard_Integer i,typeindex = TypeIndex();
95
96     aDrawer->DrawPrecision(Def,Coeff,Type);
97     DrawLineAttrib(aDrawer);
98     for( i=1 ; i<=length ; i++ ) {
99       aCurve = myCurves.Value(i);
100
101       if (Type == Aspect_TOD_RELATIVE) {
102         Bnd_Box2d Box;
103         BndLib_Add2dCurve::Add(Geom2dAdaptor_Curve(aCurve),0.,Box);
104         Box.Get( Xmin,Ymin,Xmax,Ymax );
105         Def = Standard_ShortReal( (Abs(Xmax-Xmin) + Abs(Ymax-Ymin) ) * Coeff);
106       }
107
108       Geom2dAdaptor_Curve GAC(aCurve);
109       GCPnts_UniformDeflection UD(GAC,Def,Controll);
110       if(UD.IsDone()) {
111         Standard_Integer j,nbp = UD.NbPoints();
112         gp_Pnt p2;
113         Standard_Real x,y,z;
114         Standard_ShortReal x1=0,y1=0,x2,y2;
115         for (j=1; j<=nbp; j++) {
116           p2 = UD.Value(j);
117           p2.Coord(x,y,z);
118           if (myGOPtr->IsTransformed ()) {
119             aTrsf.Transforms(x, y);
120           }
121           if( typeindex > 0 ) {
122             if( j > 1 ) {
123               if( j < nbp ) bufferize = 0;
124               else          bufferize = -1;
125             } else {
126               bufferize = nbp;
127             }
128           aDrawer->MapPolylineFromTo( Standard_ShortReal(x), Standard_ShortReal(y), bufferize);
129           } else {
130             if( j > 1 ) {
131               if( (i == length) && (j == nbp) ) bufferize = -1;
132               x2 = Standard_ShortReal(x); y2 = Standard_ShortReal(y);
133           aDrawer->MapSegmentFromTo(x1,y1,x2,y2,bufferize);
134               x1 = x2; y1 = y2;
135               bufferize = 0; 
136             } else {
137               x1 = Standard_ShortReal(x); y1 = Standard_ShortReal(y);
138             }
139           }
140         }
141       }
142     }
143   }
144 }
145
146 #ifdef G002
147
148 void GGraphic2d_SetOfCurves::DrawElement( const Handle(Graphic2d_Drawer)& aDrawer,
149                                           const Standard_Integer anIndex ) {
150
151  Standard_Integer length = myCurves.Length(), bufferize = 1;
152  Standard_Boolean IsIn = Standard_False;
153  Standard_ShortReal minX,minY,maxX,maxY;
154  Standard_Real Xmin,Ymin,Xmax,Ymax;
155
156  if ( length <= 0 ) return ;
157
158  gp_GTrsf2d aTrsf = myGOPtr->Transform ();
159  
160  if ( ! myGOPtr->IsTransformed() )
161     IsIn = aDrawer->IsIn( myMinX,myMaxX,myMinY,myMaxY );
162  else {
163     MinMax(minX,maxX,minY,maxY);
164     IsIn = aDrawer->IsIn (minX,maxX,minY,maxY);
165   }
166
167   if ( IsIn ) {
168
169    if ( anIndex > 0 && anIndex <= length ) { 
170
171     Standard_ShortReal Def, Coeff;
172     Aspect_TypeOfDeflection Type;
173     Standard_Boolean Controll = Standard_True;
174     Handle(Geom2d_Curve) aCurve;
175     Standard_Integer typeindex = TypeIndex();
176
177     aDrawer->DrawPrecision( Def, Coeff, Type );
178     DrawLineAttrib( aDrawer );
179     aCurve = myCurves.Value( anIndex );
180
181     if ( Type == Aspect_TOD_RELATIVE ) {
182       Bnd_Box2d Box;
183       BndLib_Add2dCurve::Add(Geom2dAdaptor_Curve(aCurve),0.,Box);
184       Box.Get( Xmin,Ymin,Xmax,Ymax  );
185       Def = Standard_ShortReal( (Abs(Xmax-Xmin) + Abs(Ymax-Ymin) ) * Coeff);
186     }
187
188     Geom2dAdaptor_Curve GAC(aCurve);
189     GCPnts_UniformDeflection UD(GAC,Def,Controll);
190     if ( UD.IsDone() ) {
191       Standard_Integer j,nbp = UD.NbPoints();
192       gp_Pnt p2;
193       Standard_Real x,y,z;
194       Standard_ShortReal x1=0,y1=0,x2,y2;
195       for ( j = 1; j <= nbp; j++ ) {
196           p2 = UD.Value( j );
197           p2.Coord( x, y, z );
198           if ( myGOPtr->IsTransformed() ) {
199             aTrsf.Transforms (x, y);
200           }
201               if ( typeindex > 0 ) {
202                 if ( j > 1 ) {
203               bufferize = ( j < nbp ) ? 0 : -1;
204             } else {
205                    bufferize = nbp;
206             }
207             aDrawer->MapPolylineFromTo( Standard_ShortReal(x), Standard_ShortReal(y), bufferize );
208           } else {
209                 if ( j > 1 ) {
210                  if ( ( anIndex == length ) && (j == nbp) ) 
211                  bufferize = -1;
212                  x2 = Standard_ShortReal(x); y2 = Standard_ShortReal(y);
213              aDrawer->MapSegmentFromTo( x1, y1, x2, y2, bufferize );
214                  x1 = x2; y1 = y2;
215                  bufferize = 0; 
216             } else {
217                  x1 = Standard_ShortReal(x); y1 = Standard_ShortReal(y);
218             }
219           }
220       } // end for
221     }
222   }
223  
224  } // end if IsIn
225 }
226
227 void GGraphic2d_SetOfCurves::DrawVertex( const Handle(Graphic2d_Drawer)& aDrawer,
228                                          const Standard_Integer anIndex ) {
229
230  Standard_Integer length = myCurves.Length();
231  Standard_Boolean IsIn = Standard_False;
232  Standard_ShortReal minX,minY,maxX,maxY;
233
234  if ( length <= 0 ) return ;
235
236  if ( ! myGOPtr->IsTransformed() )
237     IsIn = aDrawer->IsIn( myMinX,myMaxX,myMinY,myMaxY );
238  else {
239     MinMax(minX,maxX,minY,maxY);
240     IsIn = aDrawer->IsIn (minX,maxX,minY,maxY);
241   }
242
243   if ( IsIn ) {
244
245    if ( anIndex > 0 && anIndex <= 2*length ) { 
246       
247       DrawMarkerAttrib (aDrawer);
248       Handle(Geom2d_Curve) aCurve; 
249       Standard_Real theParam; 
250       if ( anIndex <= length ) { 
251        aCurve = myCurves.Value( anIndex );
252        theParam = aCurve->FirstParameter();
253       } else {
254        aCurve = myCurves.Value( anIndex - length);
255        theParam = aCurve->LastParameter();
256       }
257       gp_Pnt2d thePnt = aCurve->Value(theParam);
258       Standard_ShortReal a = Standard_ShortReal(thePnt.X()),
259                          b = Standard_ShortReal(thePnt.Y());
260       if ( myGOPtr->IsTransformed() ) {
261         gp_GTrsf2d aTrsf = myGOPtr->Transform ();
262         Standard_Real a1, b1;
263         a1 = Standard_Real(a);
264         b1 = Standard_Real(b);
265         aTrsf.Transforms( a1, b1 );
266         a = Standard_ShortReal( a1 );
267         b = Standard_ShortReal( b1 );
268       } 
269       aDrawer->MapMarkerFromTo( VERTEXMARKER, a, b,
270                         DEFAULTMARKERSIZE,DEFAULTMARKERSIZE,0.0);
271    } 
272   } // end if IsIn
273 }
274
275 #endif
276
277 Standard_Boolean GGraphic2d_SetOfCurves::Pick( 
278                       const Standard_ShortReal X,
279                       const Standard_ShortReal Y,
280                       const Standard_ShortReal aPrecision,
281                       const Handle(Graphic2d_Drawer)& /*aDrawer*/) {
282
283  Standard_Integer i,length = myCurves.Length();
284  //Standard_ShortReal SRX = X, SRY = Y;
285  static Standard_ShortReal SRX , SRY ;
286  SRX = X;
287  SRY = Y;
288  //Standard_Boolean isPicked = Standard_False;
289   
290  if ( (length > 0) && IsInMinMax (X, Y, aPrecision)) {
291   if (myGOPtr->IsTransformed ()) {
292     gp_GTrsf2d aTrsf = (myGOPtr->Transform()).Inverted();
293     Standard_Real RX = Standard_Real( SRX ), RY = Standard_Real( SRY );
294     aTrsf.Transforms( RX, RY );
295     SRX = Standard_ShortReal( RX ); SRY = Standard_ShortReal( RY );
296    }
297    try {
298      OCC_CATCH_SIGNALS
299       Handle(Geom2d_Curve) aCurve;
300       for ( i = 1 ; i <= length; i++ ) {
301         aCurve = myCurves.Value(i);
302         Standard_Real FParam = aCurve->FirstParameter(), 
303                       LParam = aCurve->LastParameter();
304         gp_Pnt2d FP = aCurve->Value(FParam), 
305                  LP = aCurve->Value(LParam);
306         Geom2dAPI_ProjectPointOnCurve PC(gp_Pnt2d(SRX, SRY),
307                                      aCurve,
308                                      FParam,
309                                      LParam );
310         if ( PC.NbPoints() > 0 ) {
311           gp_Pnt2d P = PC.NearestPoint();
312
313 #ifdef G002
314
315           if ( ( Abs( SRX - P.X() ) +  Abs( SRY - P.Y() ) ) < aPrecision ) {
316             SetPickedIndex( i );
317             return Standard_True;
318           } else if ( Graphic2d_Primitive::IsOn( SRX, SRY, 
319               Standard_ShortReal(FP.X()), Standard_ShortReal(FP.Y()), aPrecision) ) {
320             SetPickedIndex( -i );
321             return Standard_True;
322           } else if ( Graphic2d_Primitive::IsOn( SRX, SRY, 
323               Standard_ShortReal(LP.X()), Standard_ShortReal(LP.Y()), aPrecision) ) {
324             SetPickedIndex( -i - length );
325             return Standard_True;
326           }
327 #else
328           if ( ( Abs( SRX - P.X() ) +  Abs( SRY - P.Y() ) ) < aPrecision ) {
329                 myPickedIndex = i;
330             return Standard_True;
331           }
332 #endif
333         } // end if NbPoints() > 0
334       } // end for
335     } catch( Standard_Failure )  {
336       return Standard_False;
337     }
338   }
339
340   return Standard_False;
341  
342 }
343
344 void GGraphic2d_SetOfCurves::Save(Aspect_FStream& aFStream) const
345 {
346         int i,lngth = Length();
347         if ( ! lngth ) return;
348
349         *aFStream << "GGraphic2d_SetOfCurves" << endl;
350         *aFStream << lngth << endl;     
351
352
353         Handle(Geom2d_Curve) myCurve;
354
355         for ( i = 1; i <= lngth; i++ ) {
356
357         myCurve = myCurves.Value( i );
358
359         if ( myCurve->IsKind(STANDARD_TYPE(Geom2d_Line)) ) {
360
361         Handle(Geom2d_Line) theLine = Handle(Geom2d_Line)::DownCast( myCurve );
362         gp_Dir2d tDir = theLine->Direction();
363         gp_Pnt2d tPnt = theLine->Location();
364
365         *aFStream << "Geom2d_Line" << endl;
366         *aFStream << tDir.X() << ' ' << tDir.Y() << endl;
367         *aFStream << tPnt.X() << ' ' << tPnt.Y() << endl;
368
369         } else if ( myCurve->IsKind(STANDARD_TYPE(Geom2d_Circle)) ) {
370
371         Handle(Geom2d_Circle) theCirc = Handle(Geom2d_Circle)::DownCast( myCurve );
372         gp_Circ2d tCirc  = theCirc->Circ2d();
373         gp_Ax22d tAx22d = tCirc.Axis();
374         gp_Pnt2d tLoc  = tAx22d.Location();
375         gp_Dir2d tDirX = tAx22d.XDirection(),
376                tDirY = tAx22d.YDirection();
377
378         Standard_Real tRad = tCirc.Radius();
379
380         *aFStream << "Geom2d_Circle" << endl;
381         *aFStream << tLoc.X() << ' ' << tLoc.Y() << endl;
382         *aFStream << tDirX.X() << ' ' << tDirX.Y() << endl;
383         *aFStream << tDirY.X() << ' ' << tDirY.Y() << endl;
384         *aFStream << tRad << endl;
385
386         } else if ( myCurve->IsKind(STANDARD_TYPE(Geom2d_Parabola)) ) {
387
388         Handle(Geom2d_Parabola) theParab = Handle(Geom2d_Parabola)::DownCast( myCurve );
389         gp_Parab2d tParab = theParab->Parab2d();
390         gp_Ax22d tAx22d = tParab.Axis();
391         gp_Pnt2d tLoc  = tAx22d.Location();
392         gp_Dir2d tDirX = tAx22d.XDirection(),
393                 tDirY = tAx22d.YDirection();
394
395         Standard_Real tFocal = theParab->Focal();
396
397         *aFStream << "Geom2d_Parabola" << endl;
398         *aFStream << tLoc.X() << ' ' << tLoc.Y() << endl;
399         *aFStream << tDirX.X() << ' ' << tDirX.Y() << endl;
400         *aFStream << tDirY.X() << ' ' << tDirY.Y() << endl;
401         *aFStream << tFocal << endl;
402
403         } else if ( myCurve->IsKind(STANDARD_TYPE(Geom2d_Ellipse)) ) {
404         Handle(Geom2d_Ellipse) theEllipse = Handle(Geom2d_Ellipse)::DownCast(myCurve);
405         gp_Elips2d tElips = theEllipse->Elips2d();
406         Standard_Real tMin = tElips.MinorRadius(),
407                      tMaj = tElips.MajorRadius();
408         gp_Ax22d tAx22d = tElips.Axis();
409         gp_Pnt2d tLoc  = tAx22d.Location();
410         gp_Dir2d tDirX = tAx22d.XDirection(),
411                 tDirY = tAx22d.YDirection();
412
413         *aFStream << "Geom2d_Ellipse" << endl;
414
415         *aFStream << tMin << ' ' << tMaj << endl;
416         *aFStream << tLoc.X() << ' ' << tLoc.Y() << endl;
417         *aFStream << tDirX.X() << ' ' << tDirX.Y() << endl;
418         *aFStream << tDirY.X() << ' ' << tDirY.Y() << endl;
419
420         } else if ( myCurve->IsKind(STANDARD_TYPE(Geom2d_Hyperbola)) ) {
421
422         Handle(Geom2d_Hyperbola) theHypr = Handle(Geom2d_Hyperbola)::DownCast(myCurve);
423         gp_Hypr2d tHypr = theHypr->Hypr2d();
424         Standard_Real tMin = tHypr.MinorRadius(),
425                       tMaj = tHypr.MajorRadius();
426         gp_Ax22d tAx22d = tHypr.Axis();
427         gp_Pnt2d tLoc  = tAx22d.Location();
428         gp_Dir2d tDirX = tAx22d.XDirection(),
429                  tDirY = tAx22d.YDirection();
430
431         *aFStream << "Geom2d_Hyperbola" << endl;
432
433         *aFStream << tMin << ' ' << tMaj << endl;
434         *aFStream << tLoc.X() << ' ' << tLoc.Y() << endl;
435         *aFStream << tDirX.X() << ' ' << tDirX.Y() << endl;
436         *aFStream << tDirY.X() << ' ' << tDirY.Y() << endl;
437
438         } else if ( myCurve->IsKind(STANDARD_TYPE(Geom2d_BezierCurve)) ) {
439
440         Handle(Geom2d_BezierCurve) theBC = Handle(Geom2d_BezierCurve)::DownCast(myCurve);
441         Standard_Integer nbPoles = theBC->NbPoles();
442         TColgp_Array1OfPnt2d tpoles( 1, nbPoles );
443         theBC->Poles( tpoles );
444         *aFStream << "Geom2d_BezierCurve" << endl;
445
446         for ( i = 1; i <= nbPoles; i++ ) 
447           *aFStream << tpoles(i).X() << ' ' << tpoles(i).Y() << endl;
448
449         } else if ( myCurve->IsKind(STANDARD_TYPE(Geom2d_BSplineCurve)) ) {
450         Handle(Geom2d_BSplineCurve) theBSC = Handle(Geom2d_BSplineCurve)::DownCast(myCurve);
451
452         } else if ( myCurve->IsKind(STANDARD_TYPE(Geom2d_OffsetCurve)) ) {
453         Handle(Geom2d_OffsetCurve) theOC = Handle(Geom2d_OffsetCurve)::DownCast(myCurve);
454
455         } else if ( myCurve->IsKind(STANDARD_TYPE(Geom2d_TrimmedCurve)) ) {    
456         Handle(Geom2d_TrimmedCurve) theTC = Handle(Geom2d_TrimmedCurve)::DownCast(myCurve);
457         }
458
459         } // end for 
460         Graphic2d_Line::Save(aFStream);
461 }