summaryrefslogtreecommitdiff
path: root/src/GGraphic2d/GGraphic2d_Curve.cxx
blob: b8cd08f002832cbb0f4c629446950d820d6c2419 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
#define WTO0001         //GG_140596
//                      Calcul des min-max faux apres transformation.
// G002 TCL : Drawing of descriptor of BSpline and Bezier curves in display mode 1

#include <GGraphic2d_Curve.ixx>
#include <BndLib_Add2dCurve.hxx>
#include <Bnd_Box2d.hxx>
#include <Geom2dAdaptor_Curve.hxx>
#include <GCPnts_UniformDeflection.hxx>
#include <gp_Pnt2d.hxx>
#include <gp_Dir2d.hxx>
#include <gp_Circ2d.hxx>
#include <gp_Elips2d.hxx>
#include <gp_Parab2d.hxx>
#include <gp_Hypr2d.hxx>
#include <gp_Pnt.hxx>
#include <TShort_Array1OfShortReal.hxx>
#include <Geom2dAPI_ProjectPointOnCurve.hxx>
#include <Standard_ErrorHandler.hxx>
#include <TColgp_Array1OfPnt2d.hxx>
#include <Geom2d_BezierCurve.hxx>
#include <Geom2d_BSplineCurve.hxx>
#include <Geom2d_Line.hxx>
#include <Geom2d_Circle.hxx>
#include <Geom2d_Ellipse.hxx>
#include <Geom2d_Parabola.hxx>
#include <Geom2d_Hyperbola.hxx>
#include <Geom2d_TrimmedCurve.hxx>
#include <Geom2d_OffsetCurve.hxx>

#define MAXPOINTS 1023
#define VERTEXMARKER 4
#define DEFAULTMARKERSIZE 3.0

static  TShort_Array1OfShortReal Xpoint(1,MAXPOINTS);
static  TShort_Array1OfShortReal Ypoint(1,MAXPOINTS);

GGraphic2d_Curve::GGraphic2d_Curve
  (const Handle(Graphic2d_GraphicObject)& aGraphicObject,
   const Handle(Geom2d_Curve)& aCurve)
    : Graphic2d_Line(aGraphicObject) , myCurve(aCurve) {

    Bnd_Box2d Box;
    BndLib_Add2dCurve::Add(Geom2dAdaptor_Curve(aCurve),0.,Box);
    Standard_Real aXmin, aYmin, aXmax, aYmax;
    Box.Get( aXmin, aYmin, aXmax, aYmax);
    myMinX = Standard_ShortReal(aXmin);
    myMinY = Standard_ShortReal(aYmin);
    myMaxX = Standard_ShortReal(aXmax);
    myMaxY = Standard_ShortReal(aYmax);
}

Handle(Geom2d_Curve) GGraphic2d_Curve::GeomCurve() const {
   return myCurve;
}

void GGraphic2d_Curve::Draw (const Handle(Graphic2d_Drawer)& aDrawer) { 

  Standard_Boolean IsIn = Standard_False;

  if (! myGOPtr->IsTransformed ())
    IsIn = aDrawer->IsIn (myMinX,myMaxX,myMinY,myMaxY);
  else {
    Standard_ShortReal minx, miny, maxx, maxy;
#ifdef WTO0001
    MinMax(minx,maxx,miny,maxy);
#else
    gp_GTrsf2d aTrsf = myGOPtr->Transform ();
    Standard_Real MINX, MINY, MAXX, MAXY;

    MINX = Standard_Real (myMinX);
    MINY = Standard_Real (myMinY);
    MAXX = Standard_Real (myMaxX);
    MAXY = Standard_Real (myMaxY);
    aTrsf.Transforms (MINX, MINY);
    aTrsf.Transforms (MAXX, MAXY);
    minx = Standard_ShortReal (MINX);
    miny = Standard_ShortReal (MINY);
    maxx = Standard_ShortReal (MAXX);
    maxy = Standard_ShortReal (MAXY);
#endif
    IsIn = aDrawer->IsIn (minx,maxx,miny,maxy);
  }

  if (IsIn) {
   switch ( myDisplayMode ) {
   case 0:
   default: {
        
    gp_GTrsf2d aTrsf = myGOPtr->Transform ();

    Standard_ShortReal Def;
    Standard_ShortReal Coeff;
    Aspect_TypeOfDeflection Type;
    aDrawer->DrawPrecision(Def,Coeff,Type);
    Standard_Boolean Controll = Standard_True;
    if (Type == Aspect_TOD_RELATIVE)
      Def = ( Abs(myMaxX-myMinX) + Abs(myMaxY-myMinY) ) * Coeff;
    Geom2dAdaptor_Curve GAC(myCurve);
    GCPnts_UniformDeflection UD(GAC,Def,Controll);
    if (UD.IsDone() ) {
      Standard_Integer i,j = 1,nbp = UD.NbPoints();
      gp_Pnt p2;
      Standard_Real x,y,z;
      
      while (nbp > 0) {
	    Standard_Integer n = Min(MAXPOINTS,nbp);
        for (i=1; i<=n; i++) {
	      p2 = UD.Value(j); j++;
	      p2.Coord(x,y,z);
	      Xpoint(i) = Standard_ShortReal( x );
	      Ypoint(i) = Standard_ShortReal( y );
        }

        if (myGOPtr->IsTransformed ()) {
          Standard_Real A, B;
          for (i=1; i<=n; i++) {
		    A = Standard_Real (Xpoint(i));
		    B = Standard_Real (Ypoint(i));
		    aTrsf.Transforms (A, B);
		    Xpoint(i) = Standard_ShortReal (A);
		    Ypoint(i) = Standard_ShortReal (B);
          }
        }
        DrawLineAttrib(aDrawer);
        aDrawer->MapPolylineFromTo(Xpoint,Ypoint,n);
        nbp -= MAXPOINTS; --j;
      }  // end while
    } // end UD.IsDone
   } // end case 0
   break;
   case 1: {
    if ( myCurve->IsKind(STANDARD_TYPE(Geom2d_BezierCurve) ) ) {    
       Handle(Geom2d_BezierCurve) theBC = Handle(Geom2d_BezierCurve)::DownCast(myCurve); 
       TColgp_Array1OfPnt2d thePnts(1, theBC->NbPoles());
       theBC->Poles(thePnts);
       TShort_Array1OfShortReal XDpoint(thePnts.Lower(), thePnts.Upper());
       TShort_Array1OfShortReal YDpoint(thePnts.Lower(), thePnts.Upper());
       int i;
       for ( i = thePnts.Lower(); i <= thePnts.Upper(); ++i ) {
             XDpoint.SetValue( i, Standard_ShortReal( thePnts(i).X() ) );
             YDpoint.SetValue( i, Standard_ShortReal( thePnts(i).Y() ) );
       }
       
       if ( myGOPtr->IsTransformed() ) {
         gp_GTrsf2d aTrsf = myGOPtr->Transform();
         Standard_Real A, B;
         for ( i = XDpoint.Lower(); i <= XDpoint.Upper(); ++i ) {
            A = Standard_Real(XDpoint(i));
		    B = Standard_Real(YDpoint(i));
		    aTrsf.Transforms( A, B );
		    XDpoint(i) = Standard_ShortReal( A );
		    YDpoint(i) = Standard_ShortReal( B );
         }
       }
       DrawLineAttrib(aDrawer);
       aDrawer->MapPolylineFromTo(XDpoint,YDpoint,thePnts.Length());
       
       DrawMarkerAttrib(aDrawer);
       for ( i = XDpoint.Lower(); i <= XDpoint.Upper(); ++i ) 
         aDrawer->MapMarkerFromTo(VERTEXMARKER,XDpoint(i),YDpoint(i), DEFAULTMARKERSIZE,DEFAULTMARKERSIZE,0.0);

    } else if ( myCurve->IsKind(STANDARD_TYPE(Geom2d_BSplineCurve) ) ) {                     
       Handle(Geom2d_BSplineCurve) theBC = Handle(Geom2d_BSplineCurve)::DownCast(myCurve); 
       TColgp_Array1OfPnt2d thePnts(1, theBC->NbPoles());
       theBC->Poles(thePnts);
       TShort_Array1OfShortReal XDpoint(thePnts.Lower(), thePnts.Upper());
       TShort_Array1OfShortReal YDpoint(thePnts.Lower(), thePnts.Upper());
       int i;
       for ( i = thePnts.Lower(); i <= thePnts.Upper(); ++i ) {
             XDpoint.SetValue( i, Standard_ShortReal( thePnts(i).X() ) );
             YDpoint.SetValue( i, Standard_ShortReal( thePnts(i).Y() ) );
       }
       
       if ( myGOPtr->IsTransformed() ) {
         gp_GTrsf2d aTrsf = myGOPtr->Transform();
         Standard_Real A, B;
         for ( i = XDpoint.Lower(); i <= XDpoint.Upper(); ++i ) {
            A = Standard_Real(XDpoint(i));
		    B = Standard_Real(YDpoint(i));
		    aTrsf.Transforms( A, B );
		    XDpoint(i) = Standard_ShortReal( A );
		    YDpoint(i) = Standard_ShortReal( B );
         }
       }
       DrawLineAttrib(aDrawer);
       aDrawer->MapPolylineFromTo(XDpoint,YDpoint,thePnts.Length());
       DrawMarkerAttrib(aDrawer);
       for ( i = XDpoint.Lower(); i <= XDpoint.Upper(); ++i ) 
         aDrawer->MapMarkerFromTo( VERTEXMARKER, XDpoint(i), YDpoint(i), DEFAULTMARKERSIZE, DEFAULTMARKERSIZE, 0.0 );
    }
       
   } //end case 1
   
   }  //end switch
  } // end IsIn
}

Standard_Boolean GGraphic2d_Curve::Pick (const Standard_ShortReal X,
					 const Standard_ShortReal Y,
					 const Standard_ShortReal aPrecision,
					 const Handle(Graphic2d_Drawer)& /*aDrawer*/) 

{
//Standard_ShortReal SRX = X, SRY = Y;
static Standard_ShortReal SRX , SRY ;
SRX = X;
SRY = Y;

if (IsInMinMax (X, Y, aPrecision)) {
  if (myGOPtr->IsTransformed ()) {
gp_GTrsf2d aTrsf = (myGOPtr->Transform ()).Inverted ();
Standard_Real RX = Standard_Real (SRX), RY = Standard_Real (SRY);
	aTrsf.Transforms (RX, RY);
	SRX = Standard_ShortReal (RX); SRY = Standard_ShortReal (RY);
  }
  try { 
    OCC_CATCH_SIGNALS
    Geom2dAPI_ProjectPointOnCurve PC(gp_Pnt2d(SRX, SRY),
				     myCurve, 
				     myCurve->FirstParameter(),
				     myCurve->LastParameter());
    if(PC.NbPoints() == 0) {
      return Standard_False;
    }
    else {
      gp_Pnt2d P = PC.NearestPoint();
      return (( Abs ( SRX - P.X() ) +  Abs ( SRY - P.Y() ) ) < aPrecision);    
    }
    
  }
  catch(Standard_Failure)
    {
      return Standard_False;          
    }
}
return Standard_False;          //WNT
}

void GGraphic2d_Curve::Save(Aspect_FStream& aFStream) const
{
	*aFStream << "GGraphic2d_Curve" << endl;

	if ( myCurve->IsKind(STANDARD_TYPE(Geom2d_Line)) )
	{
		Handle(Geom2d_Line) theLine = Handle(Geom2d_Line)::DownCast( myCurve );
		gp_Dir2d tDir = theLine->Direction();
		gp_Pnt2d tPnt = theLine->Location();

		*aFStream << "Geom2d_Line" << endl;
		*aFStream << tPnt.X() << ' ' << tPnt.Y() << endl;
		*aFStream << tDir.X() << ' ' << tDir.Y() << endl;
	}
	else if ( myCurve->IsKind(STANDARD_TYPE(Geom2d_Circle)) )
	{
		Handle(Geom2d_Circle) theCirc = Handle(Geom2d_Circle)::DownCast( myCurve );
		gp_Circ2d tCirc  = theCirc->Circ2d();
		gp_Ax22d tAx22d = tCirc.Axis();
		gp_Pnt2d tLoc  = tAx22d.Location();
		gp_Dir2d tDirX = tAx22d.XDirection(),
		       tDirY = tAx22d.YDirection();

		Standard_Real tRad = tCirc.Radius();

		*aFStream << "Geom2d_Circle" << endl;
		*aFStream << tRad << endl;
		*aFStream << tLoc.X() << ' ' << tLoc.Y() << endl;
		*aFStream << tDirX.X() << ' ' << tDirX.Y() << endl;
		*aFStream << tDirY.X() << ' ' << tDirY.Y() << endl;
	}
	else if ( myCurve->IsKind(STANDARD_TYPE(Geom2d_Parabola)) )
	{
		Handle(Geom2d_Parabola) theParab = Handle(Geom2d_Parabola)::DownCast( myCurve );
		gp_Parab2d tParab = theParab->Parab2d();
		gp_Ax22d tAx22d = tParab.Axis();
		gp_Pnt2d tLoc  = tAx22d.Location();
		gp_Dir2d tDirX = tAx22d.XDirection(),
			tDirY = tAx22d.YDirection();

		Standard_Real tFocal = theParab->Focal();

		*aFStream << "Geom2d_Parabola" << endl;
		*aFStream << tFocal << endl;
		*aFStream << tLoc.X() << ' ' << tLoc.Y() << endl;
		*aFStream << tDirX.X() << ' ' << tDirX.Y() << endl;
		*aFStream << tDirY.X() << ' ' << tDirY.Y() << endl;
	}
	else if ( myCurve->IsKind(STANDARD_TYPE(Geom2d_Ellipse)) )
	{
		Handle(Geom2d_Ellipse) theEllipse = Handle(Geom2d_Ellipse)::DownCast(myCurve);
		gp_Elips2d tElips = theEllipse->Elips2d();
		Standard_Real tMin = tElips.MinorRadius(),
			     tMaj = tElips.MajorRadius();
		gp_Ax22d tAx22d = tElips.Axis();
		gp_Pnt2d tLoc  = tAx22d.Location();
		gp_Dir2d tDirX = tAx22d.XDirection(),
			tDirY = tAx22d.YDirection();

		*aFStream << "Geom2d_Ellipse" << endl;

		*aFStream << tMin << ' ' << tMaj << endl;
		*aFStream << tLoc.X() << ' ' << tLoc.Y() << endl;
		*aFStream << tDirX.X() << ' ' << tDirX.Y() << endl;
		*aFStream << tDirY.X() << ' ' << tDirY.Y() << endl;
	}
	else if ( myCurve->IsKind(STANDARD_TYPE(Geom2d_Hyperbola)) )
	{
		Handle(Geom2d_Hyperbola) theHypr = Handle(Geom2d_Hyperbola)::DownCast(myCurve);
		gp_Hypr2d tHypr = theHypr->Hypr2d();
		Standard_Real tMin = tHypr.MinorRadius(),
			      tMaj = tHypr.MajorRadius();
		gp_Ax22d tAx22d = tHypr.Axis();
		gp_Pnt2d tLoc  = tAx22d.Location();
		gp_Dir2d tDirX = tAx22d.XDirection(),
			 tDirY = tAx22d.YDirection();

		*aFStream << "Geom2d_Hyperbola" << endl;

		*aFStream << tMin << ' ' << tMaj << endl;
		*aFStream << tLoc.X() << ' ' << tLoc.Y() << endl;
		*aFStream << tDirX.X() << ' ' << tDirX.Y() << endl;
		*aFStream << tDirY.X() << ' ' << tDirY.Y() << endl;
	}
	else if ( myCurve->IsKind(STANDARD_TYPE(Geom2d_BezierCurve)) )
	{
		Handle(Geom2d_BezierCurve) theBC = Handle(Geom2d_BezierCurve)::DownCast(myCurve);
		Standard_Integer nbPoles = theBC->NbPoles();
		TColgp_Array1OfPnt2d tpoles( 1, nbPoles );
		theBC->Poles( tpoles );
		*aFStream << "Geom2d_BezierCurve" << endl;
		*aFStream << nbPoles << endl;
		for ( int i = 1; i <= nbPoles; i++ ) 
			*aFStream << tpoles(i).X() << ' ' << tpoles(i).Y() << endl;

	}
	else if ( myCurve->IsKind(STANDARD_TYPE(Geom2d_BSplineCurve)) )
	{
//		Handle(Geom2d_BSplineCurve) theBSC = Handle(Geom2d_BSplineCurve)::DownCast(myCurve);
	}
	else if ( myCurve->IsKind(STANDARD_TYPE(Geom2d_OffsetCurve)) )
	{
//		Handle(Geom2d_OffsetCurve) theOC = Handle(Geom2d_OffsetCurve)::DownCast(myCurve);
	}
	else if ( myCurve->IsKind(STANDARD_TYPE(Geom2d_TrimmedCurve)) )
	{
//		Handle(Geom2d_TrimmedCurve) theTC = Handle(Geom2d_TrimmedCurve)::DownCast(myCurve);
	}
	Graphic2d_Line::Save(aFStream);
}

void GGraphic2d_Curve::Retrieve(Aspect_IFStream& anIFStream,
			const Handle(Graphic2d_GraphicObject)& aGraphicObject)
{
	char buf[100];

	anIFStream->getline(buf, 100);
	if (! strcmp(buf, "Geom2d_Line"))
	{
		Standard_Real X, Y, dX, dY;
		
		*anIFStream >> X >> Y;
		*anIFStream >> dX >> dY;
		gp_Pnt2d tPnt(X, Y);
		gp_Dir2d tDir(dX, dY);
		Handle(Geom2d_Line)
			theLine = new Geom2d_Line(tPnt, tDir);
		Handle(GGraphic2d_Curve)
			theGGC = new GGraphic2d_Curve(aGraphicObject, theLine);
		((Handle (Graphic2d_Line))theGGC)->Retrieve(anIFStream);
	}
	else if (! strcmp(buf, "Geom2d_Circle"))
	{
		Standard_Real tRad, X, Y, dXX, dXY, dYX, dYY;
		
		*anIFStream >> tRad;
		*anIFStream >> X >> Y;
		*anIFStream >> dXX >> dXY;
		*anIFStream >> dYX >> dYY;
		gp_Pnt2d tPnt(X, Y);
		gp_Dir2d tDirX(dXX, dXY);
		gp_Dir2d tDirY(dYX, dYY);
		gp_Ax22d tAx22d(tPnt, tDirX, tDirY);
		Handle(Geom2d_Circle)
			tCirc = new Geom2d_Circle(tAx22d, tRad);
		Handle(GGraphic2d_Curve)
			theGGC = new GGraphic2d_Curve(aGraphicObject, tCirc);
		((Handle (Graphic2d_Line))theGGC)->Retrieve(anIFStream);
	}
	else if (! strcmp(buf, "Geom2d_Parabola"))
	{
		Standard_Real tFocal, X, Y, dXX, dXY, dYX, dYY;
		
		*anIFStream >> tFocal;
		*anIFStream >> X >> Y;
		*anIFStream >> dXX >> dXY;
		*anIFStream >> dYX >> dYY;
		gp_Pnt2d tPnt(X, Y);
		gp_Dir2d tDirX(dXX, dXY);
		gp_Dir2d tDirY(dYX, dYY);
		gp_Ax22d tAx22d(tPnt, tDirX, tDirY);
		Handle(Geom2d_Parabola)
			gp_Parab2d = new Geom2d_Parabola(tAx22d, tFocal);
		Handle(GGraphic2d_Curve)
			theGGC = new GGraphic2d_Curve(aGraphicObject, gp_Parab2d);
		((Handle (Graphic2d_Line))theGGC)->Retrieve(anIFStream);
	}
	else if (! strcmp(buf, "Geom2d_Ellipse"))
	{
		Standard_Real tMin, tMaj, X, Y, dXX, dXY, dYX, dYY;
		
		*anIFStream >> tMin >> tMaj;
		*anIFStream >> X >> Y;
		*anIFStream >> dXX >> dXY;
		*anIFStream >> dYX >> dYY;
		gp_Pnt2d tPnt(X, Y);
		gp_Dir2d tDirX(dXX, dXY);
		gp_Dir2d tDirY(dYX, dYY);
		gp_Ax22d tAx22d(tPnt, tDirX, tDirY);
		Handle(Geom2d_Ellipse)
			theEllipse = new Geom2d_Ellipse(tAx22d, tMaj, tMin);
		Handle(GGraphic2d_Curve)
			theGGC = new GGraphic2d_Curve(aGraphicObject, theEllipse);
		((Handle (Graphic2d_Line))theGGC)->Retrieve(anIFStream);
	}
	else if (! strcmp(buf, "Geom2d_Hyperbola"))
	{
		Standard_Real tMin, tMaj, X, Y, dXX, dXY, dYX, dYY;
		
		*anIFStream >> tMin >> tMaj;
		*anIFStream >> X >> Y;
		*anIFStream >> dXX >> dXY;
		*anIFStream >> dYX >> dYY;
		gp_Pnt2d tPnt(X, Y);
		gp_Dir2d tDirX(dXX, dXY);
		gp_Dir2d tDirY(dYX, dYY);
		gp_Ax22d tAx22d(tPnt, tDirX, tDirY);
		Handle(Geom2d_Hyperbola)
			theHypr = new Geom2d_Hyperbola(tAx22d, tMaj, tMin);
		Handle(GGraphic2d_Curve)
			theGGC = new GGraphic2d_Curve(aGraphicObject, theHypr);
		((Handle (Graphic2d_Line))theGGC)->Retrieve(anIFStream);
	}
	else if (! strcmp(buf, "Geom2d_BezierCurve"))
	{
		Standard_Integer numPoles;
		Standard_Real X, Y;
		
		*anIFStream >> numPoles;
		TColgp_Array1OfPnt2d listPoles(1, numPoles);
		for (Standard_Integer i=1; i<=numPoles; i++)
		{
			*anIFStream >> X >> Y;
			listPoles.SetValue(i, gp_Pnt2d(X, Y));
		}
		Handle(Geom2d_BezierCurve)
			theBC = new Geom2d_BezierCurve(listPoles);
		Handle(GGraphic2d_Curve)
			theGGC = new GGraphic2d_Curve(aGraphicObject, theBC);
		((Handle (Graphic2d_Line))theGGC)->Retrieve(anIFStream);
	}
	else if (! strcmp(buf, "Geom2d_BSplineCurve"))
		;
	else if (! strcmp(buf, "Geom2d_OffsetCurve"))
		;
	else if (! strcmp(buf, "Geom2d_TrimmedCurve"))
		;
	else 
	  {}
}