summaryrefslogtreecommitdiff
path: root/src/Graphic2d/Graphic2d_Polyline.cxx
blob: c5bc5e57b5ada66aa7264454b80133bd3b50be19 (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
// WTO0001         GG_140596
//                      Calcul des min-max faux apres transformation.

#define G002	//GG_140400 Use SetPickedIndex() method
//                GG_050500 Add new DrawElement(), DrawVertex() methods
//		   	    Returns a negative picked index when the point
//			    is very closed to a polyline point.

#define VERTEXMARKER 2
#define DEFAULTMARKERSIZE 3.0

#include <Graphic2d_Polyline.ixx>
#include <Graphic2d_Vertex.hxx>

#include <TShort_Array1OfShortReal.hxx>

Graphic2d_Polyline::Graphic2d_Polyline (
	const Handle(Graphic2d_GraphicObject)& aGraphicObject,
	const Graphic2d_Array1OfVertex& aListVertex)

	: Graphic2d_Line (aGraphicObject),
	myX (1, aListVertex.Length ()),
	myY (1, aListVertex.Length ()) {

	if (aListVertex.Length () < 2)
		Graphic2d_PolylineDefinitionError::Raise
			("A polyline with a length less than 2 points.");

Standard_ShortReal X, Y;
Standard_Integer i, j;

	// Recherche des MinMax
Standard_Integer Lower, Upper;
	Lower	= aListVertex.Lower ();
	Upper	= aListVertex.Upper ();

	for (j=1, i=Lower; i<=Upper; i++, j++) {
		X	= Standard_ShortReal(aListVertex (i).X ());
		Y	= Standard_ShortReal(aListVertex (i).Y ());
		myX (j)	= X;
		myY (j)	= Y;
		if (X > myMaxX) myMaxX = X;
		if (X < myMinX) myMinX = X;
		if (Y > myMaxY) myMaxY = Y;
		if (Y < myMinY) myMinY = Y;
	}

    myNumOfElem = myX.Length()-1;
    myNumOfVert = myX.Length();

}

Graphic2d_Polyline::Graphic2d_Polyline (
	const Handle(Graphic2d_GraphicObject)& aGraphicObject,
	const TColStd_Array1OfReal& aListX,
	const TColStd_Array1OfReal& aListY)

	: Graphic2d_Line (aGraphicObject),
	myX (1, aListX.Length ()),
	myY (1, aListY.Length ()) {

	if (aListX.Length () < 2)
		Graphic2d_PolylineDefinitionError::Raise
			("polyline : length < 2.");

	if (aListX.Length () != aListY.Length ())
		Graphic2d_PolylineDefinitionError::Raise
			("polyline : ListX and ListY have different lengths.");

Standard_ShortReal X, Y;
Standard_Integer i, j;

	// Recherche des MinMax
Standard_Integer Lower, Upper;
	Lower	= aListX.Lower ();
	Upper	= aListX.Upper ();

	for (j=1, i=Lower; i<=Upper; i++, j++) {
		X	= Standard_ShortReal(aListX (i));
		Y	= Standard_ShortReal(aListY (i));
		myX (j)	= X;
		myY (j)	= Y;
		if (X > myMaxX) myMaxX = X;
		if (X < myMinX) myMinX = X;
		if (Y > myMaxY) myMaxY = Y;
		if (Y < myMinY) myMinY = Y;
	}
    myNumOfElem = myX.Length()-1;
    myNumOfVert = myX.Length();
}


Standard_Integer Graphic2d_Polyline::Length () const {
   return myX.Length();
}

void Graphic2d_Polyline::Values( const Standard_Integer aRank,
                                 Standard_Real &X,Standard_Real &Y ) const
{
        if( aRank < 1 || aRank > myX.Length() )
                Standard_OutOfRange::Raise
                        ("the point rank is out of bounds in the line");

        X = myX(aRank);
        Y = myY(aRank);

}

void Graphic2d_Polyline::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;
    MinMax(minx,maxx,miny,maxy);
    IsIn = aDrawer->IsIn (minx,maxx,miny,maxy);
  }

  if (IsIn) {
    DrawLineAttrib (aDrawer);

    if (myGOPtr->IsTransformed ()) {
      Standard_Integer nbpoints = myX.Length ();
      TShort_Array1OfShortReal Xpoint (1, nbpoints);
      TShort_Array1OfShortReal Ypoint (1, nbpoints);
      gp_GTrsf2d aTrsf = myGOPtr->Transform ();
      Standard_Real A, B;
	  for (Standard_Integer j=1; j<= nbpoints; j++) {
		     A = Standard_Real (myX(j));
		     B = Standard_Real (myY(j));
		     aTrsf.Transforms (A, B);
		     Xpoint(j) = Standard_ShortReal (A);
		     Ypoint(j) = Standard_ShortReal (B);
	  }
      if ( myTypeOfPolygonFilling == Graphic2d_TOPF_EMPTY ) {
          aDrawer->MapPolylineFromTo(Xpoint, Ypoint);
      } else {
          aDrawer->MapPolygonFromTo(Xpoint, Ypoint); 
      }
    } else {
       if(myTypeOfPolygonFilling == Graphic2d_TOPF_EMPTY) {
          aDrawer->MapPolylineFromTo(myX, myY);
       } else {
          aDrawer->MapPolygonFromTo(myX, myY); 
       }
    }
  }
}

#ifdef G002
void Graphic2d_Polyline::DrawElement( const Handle(Graphic2d_Drawer)& aDrawer,
                                      const Standard_Integer anIndex) {
Standard_Boolean IsIn = Standard_False;

  if (! myGOPtr->IsTransformed ())
    IsIn = aDrawer->IsIn (myMinX,myMaxX,myMinY,myMaxY);
  else {
    Standard_ShortReal minx, miny, maxx, maxy;
    MinMax(minx,maxx,miny,maxy);
    IsIn = aDrawer->IsIn (minx,maxx,miny,maxy);
  }

  Standard_Integer nbpoints = myX.Length ();
  if (IsIn ) {
    if( anIndex > 0 && anIndex < nbpoints ) { //Draw edge
      Standard_ShortReal X1,Y1,X2,Y2;
      DrawLineAttrib (aDrawer);
      if (myGOPtr->IsTransformed ()) {
        gp_GTrsf2d aTrsf = myGOPtr->Transform ();
        Standard_Real A, B;
        A = Standard_Real (myX(anIndex));
        B = Standard_Real (myY(anIndex));
        aTrsf.Transforms (A, B);
        X1 = Standard_ShortReal (A);
        Y1 = Standard_ShortReal (B);
        A = Standard_Real (myX(anIndex+1));
        B = Standard_Real (myY(anIndex+1));
        aTrsf.Transforms (A, B);
        X2 = Standard_ShortReal (A);
        Y2 = Standard_ShortReal (B);
      } else {
        X1 = Standard_ShortReal (myX(anIndex));
        Y1 = Standard_ShortReal (myY(anIndex));
        X2 = Standard_ShortReal (myX(anIndex+1));
        Y2 = Standard_ShortReal (myY(anIndex+1));
      }
      aDrawer->MapSegmentFromTo(X1,Y1,X2,Y2);
    }
  }
}

void Graphic2d_Polyline::DrawVertex( const Handle(Graphic2d_Drawer)& aDrawer,
                                     const Standard_Integer anIndex) {
 Standard_Boolean IsIn = Standard_False;

 if (! myGOPtr->IsTransformed ())
    IsIn = aDrawer->IsIn (myMinX,myMaxX,myMinY,myMaxY);
 else {
    Standard_ShortReal minx, miny, maxx, maxy;
    MinMax(minx,maxx,miny,maxy);
    IsIn = aDrawer->IsIn (minx,maxx,miny,maxy);
 }

 Standard_Integer nbpoints = myX.Length ();
 if (IsIn ) {
    if( anIndex > 0 && anIndex <= nbpoints ) { 
      Standard_ShortReal X,Y;
      DrawMarkerAttrib (aDrawer);
      if (myGOPtr->IsTransformed ()) {
        gp_GTrsf2d aTrsf = myGOPtr->Transform ();
        Standard_Real A, B;
        A = Standard_Real (myX(anIndex));
        B = Standard_Real (myY(anIndex));
        aTrsf.Transforms (A, B);
        X = Standard_ShortReal (A);
        Y = Standard_ShortReal (B);
      } else {
        X = Standard_ShortReal (myX(anIndex));
        Y = Standard_ShortReal (myY(anIndex));
      }
      aDrawer->MapMarkerFromTo(VERTEXMARKER,X,Y,
			DEFAULTMARKERSIZE,DEFAULTMARKERSIZE,0.0);
    }
  }
}
#endif

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

  Standard_ShortReal SRX = X, SRY = Y;

  Standard_Integer i;

  Standard_Integer Lower, Upper;
  Lower	= myX.Lower ();
  Upper	= myX.Upper ();

   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);
	}

#ifdef G002
   for ( i = Lower; i <= Upper; i++ ) {
     if( Graphic2d_Primitive::IsOn ( SRX, SRY, myX (i), myY (i), aPrecision) ) {
        SetPickedIndex(-i);
        return Standard_True;
     } else if( (i < Upper) && IsOn (SRX, SRY, myX (i), myY (i),
                                myX (i+1), myY (i+1), aPrecision) ) {
           SetPickedIndex(i);
           return Standard_True;
     }
    }
#else
	for (i=Lower; i<Upper ; i++) {
	    if( IsOn (SRX, SRY, myX (i), myY (i), 
				myX (i+1), myY (i+1), aPrecision) ) {
	      myPickedIndex = i;
	      return Standard_True;
	    }
	}
#endif

	if (myTypeOfPolygonFilling != Graphic2d_TOPF_EMPTY) {
	  if ( IsIn (SRX  , SRY ,  myX , myY , aPrecision) ) {
#ifdef G002
	      SetPickedIndex(0);
#else
	      myPickedIndex = 0;
#endif
	      return Standard_True;
	  }
	}
   }
   return Standard_False;

}

void Graphic2d_Polyline::Save(Aspect_FStream& aFStream) const
{
	*aFStream << "Graphic2d_Polyline" << endl;
	*aFStream << myNumOfVert << endl;
	for (Standard_Integer i=1; i<=myNumOfVert; i++)
		*aFStream << myX(i) << ' ' << myY(i) << endl;
	Graphic2d_Line::Save(aFStream);
}

void Graphic2d_Polyline::Retrieve(Aspect_IFStream& anIFStream,
			const Handle(Graphic2d_GraphicObject)& aGraphicObject)
{
	Standard_Integer numOfVert;
	Standard_ShortReal X, Y;

	*anIFStream >> numOfVert;
	Graphic2d_Array1OfVertex listVertex(1, numOfVert);
	for (Standard_Integer i=1; i<=numOfVert; i++)
	{
		*anIFStream >> X >> Y;
		listVertex(i).SetCoord(X, Y);
	}
	Handle(Graphic2d_Polyline)
		thePLin = new Graphic2d_Polyline(aGraphicObject, listVertex);
	((Handle (Graphic2d_Line))thePLin)->Retrieve(anIFStream);
}