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>
24 #include <TShort_Array1OfShortReal.hxx>
25 #include <Geom2dAPI_ProjectPointOnCurve.hxx>
26 #include <Standard_ErrorHandler.hxx>
29 #define VERTEXMARKER 2
30 #define DEFAULTMARKERSIZE 3.0
32 GGraphic2d_SetOfCurves::GGraphic2d_SetOfCurves (
33 const Handle(Graphic2d_GraphicObject)& aGraphicObject)
35 : Graphic2d_Line (aGraphicObject) {
39 void GGraphic2d_SetOfCurves::Add (const Handle(Geom2d_Curve) &aCurve)
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);
54 myCurves.Append(aCurve);
57 Standard_Integer GGraphic2d_SetOfCurves::Length () const {
58 return myCurves.Length();
61 Handle(Geom2d_Curve) GGraphic2d_SetOfCurves::Values(const Standard_Integer aRank) const {
63 if( aRank < 1 || aRank > myCurves.Length() )
64 Standard_OutOfRange::Raise
65 ("the curve rank is out of bounds in the set");
67 return myCurves.Value(aRank);
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;
77 if( length <= 0 ) return ;
79 gp_GTrsf2d aTrsf = myGOPtr->Transform ();
81 if (! myGOPtr->IsTransformed ())
82 IsIn = aDrawer->IsIn (myMinX,myMaxX,myMinY,myMaxY);
84 MinMax(minX,maxX,minY,maxY);
85 IsIn = aDrawer->IsIn (minX,maxX,minY,maxY);
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();
96 aDrawer->DrawPrecision(Def,Coeff,Type);
97 DrawLineAttrib(aDrawer);
98 for( i=1 ; i<=length ; i++ ) {
99 aCurve = myCurves.Value(i);
101 if (Type == Aspect_TOD_RELATIVE) {
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);
108 Geom2dAdaptor_Curve GAC(aCurve);
109 GCPnts_UniformDeflection UD(GAC,Def,Controll);
111 Standard_Integer j,nbp = UD.NbPoints();
114 Standard_ShortReal x1=0,y1=0,x2,y2;
115 for (j=1; j<=nbp; j++) {
118 if (myGOPtr->IsTransformed ()) {
119 aTrsf.Transforms(x, y);
121 if( typeindex > 0 ) {
123 if( j < nbp ) bufferize = 0;
128 aDrawer->MapPolylineFromTo( Standard_ShortReal(x), Standard_ShortReal(y), bufferize);
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);
137 x1 = Standard_ShortReal(x); y1 = Standard_ShortReal(y);
148 void GGraphic2d_SetOfCurves::DrawElement( const Handle(Graphic2d_Drawer)& aDrawer,
149 const Standard_Integer anIndex ) {
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;
156 if ( length <= 0 ) return ;
158 gp_GTrsf2d aTrsf = myGOPtr->Transform ();
160 if ( ! myGOPtr->IsTransformed() )
161 IsIn = aDrawer->IsIn( myMinX,myMaxX,myMinY,myMaxY );
163 MinMax(minX,maxX,minY,maxY);
164 IsIn = aDrawer->IsIn (minX,maxX,minY,maxY);
169 if ( anIndex > 0 && anIndex <= length ) {
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();
177 aDrawer->DrawPrecision( Def, Coeff, Type );
178 DrawLineAttrib( aDrawer );
179 aCurve = myCurves.Value( anIndex );
181 if ( Type == Aspect_TOD_RELATIVE ) {
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);
188 Geom2dAdaptor_Curve GAC(aCurve);
189 GCPnts_UniformDeflection UD(GAC,Def,Controll);
191 Standard_Integer j,nbp = UD.NbPoints();
194 Standard_ShortReal x1=0,y1=0,x2,y2;
195 for ( j = 1; j <= nbp; j++ ) {
198 if ( myGOPtr->IsTransformed() ) {
199 aTrsf.Transforms (x, y);
201 if ( typeindex > 0 ) {
203 bufferize = ( j < nbp ) ? 0 : -1;
207 aDrawer->MapPolylineFromTo( Standard_ShortReal(x), Standard_ShortReal(y), bufferize );
210 if ( ( anIndex == length ) && (j == nbp) )
212 x2 = Standard_ShortReal(x); y2 = Standard_ShortReal(y);
213 aDrawer->MapSegmentFromTo( x1, y1, x2, y2, bufferize );
217 x1 = Standard_ShortReal(x); y1 = Standard_ShortReal(y);
227 void GGraphic2d_SetOfCurves::DrawVertex( const Handle(Graphic2d_Drawer)& aDrawer,
228 const Standard_Integer anIndex ) {
230 Standard_Integer length = myCurves.Length();
231 Standard_Boolean IsIn = Standard_False;
232 Standard_ShortReal minX,minY,maxX,maxY;
234 if ( length <= 0 ) return ;
236 if ( ! myGOPtr->IsTransformed() )
237 IsIn = aDrawer->IsIn( myMinX,myMaxX,myMinY,myMaxY );
239 MinMax(minX,maxX,minY,maxY);
240 IsIn = aDrawer->IsIn (minX,maxX,minY,maxY);
245 if ( anIndex > 0 && anIndex <= 2*length ) {
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();
254 aCurve = myCurves.Value( anIndex - length);
255 theParam = aCurve->LastParameter();
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 );
269 aDrawer->MapMarkerFromTo( VERTEXMARKER, a, b,
270 DEFAULTMARKERSIZE,DEFAULTMARKERSIZE,0.0);
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*/) {
283 Standard_Integer i,length = myCurves.Length();
284 //Standard_ShortReal SRX = X, SRY = Y;
285 static Standard_ShortReal SRX , SRY ;
288 //Standard_Boolean isPicked = Standard_False;
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 );
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),
310 if ( PC.NbPoints() > 0 ) {
311 gp_Pnt2d P = PC.NearestPoint();
315 if ( ( Abs( SRX - P.X() ) + Abs( SRY - P.Y() ) ) < aPrecision ) {
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;
328 if ( ( Abs( SRX - P.X() ) + Abs( SRY - P.Y() ) ) < aPrecision ) {
330 return Standard_True;
333 } // end if NbPoints() > 0
335 } catch( Standard_Failure ) {
336 return Standard_False;
340 return Standard_False;
344 void GGraphic2d_SetOfCurves::Save(Aspect_FStream& aFStream) const
346 int i,lngth = Length();
347 if ( ! lngth ) return;
349 *aFStream << "GGraphic2d_SetOfCurves" << endl;
350 *aFStream << lngth << endl;
353 Handle(Geom2d_Curve) myCurve;
355 for ( i = 1; i <= lngth; i++ ) {
357 myCurve = myCurves.Value( i );
359 if ( myCurve->IsKind(STANDARD_TYPE(Geom2d_Line)) ) {
361 Handle(Geom2d_Line) theLine = Handle(Geom2d_Line)::DownCast( myCurve );
362 gp_Dir2d tDir = theLine->Direction();
363 gp_Pnt2d tPnt = theLine->Location();
365 *aFStream << "Geom2d_Line" << endl;
366 *aFStream << tDir.X() << ' ' << tDir.Y() << endl;
367 *aFStream << tPnt.X() << ' ' << tPnt.Y() << endl;
369 } else if ( myCurve->IsKind(STANDARD_TYPE(Geom2d_Circle)) ) {
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();
378 Standard_Real tRad = tCirc.Radius();
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;
386 } else if ( myCurve->IsKind(STANDARD_TYPE(Geom2d_Parabola)) ) {
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();
395 Standard_Real tFocal = theParab->Focal();
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;
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();
413 *aFStream << "Geom2d_Ellipse" << endl;
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;
420 } else if ( myCurve->IsKind(STANDARD_TYPE(Geom2d_Hyperbola)) ) {
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();
431 *aFStream << "Geom2d_Hyperbola" << endl;
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;
438 } else if ( myCurve->IsKind(STANDARD_TYPE(Geom2d_BezierCurve)) ) {
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;
446 for ( i = 1; i <= nbPoles; i++ )
447 *aFStream << tpoles(i).X() << ' ' << tpoles(i).Y() << endl;
449 } else if ( myCurve->IsKind(STANDARD_TYPE(Geom2d_BSplineCurve)) ) {
450 Handle(Geom2d_BSplineCurve) theBSC = Handle(Geom2d_BSplineCurve)::DownCast(myCurve);
452 } else if ( myCurve->IsKind(STANDARD_TYPE(Geom2d_OffsetCurve)) ) {
453 Handle(Geom2d_OffsetCurve) theOC = Handle(Geom2d_OffsetCurve)::DownCast(myCurve);
455 } else if ( myCurve->IsKind(STANDARD_TYPE(Geom2d_TrimmedCurve)) ) {
456 Handle(Geom2d_TrimmedCurve) theTC = Handle(Geom2d_TrimmedCurve)::DownCast(myCurve);
460 Graphic2d_Line::Save(aFStream);