summaryrefslogtreecommitdiff
path: root/src/Graphic2d/Graphic2d_SetOfPolylines.cxx
blob: d236729fa6f0d9a7d8ae70186a571d71eedeb483 (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
/*=====================================================================

     FONCTION :
     ----------
        Classe Graphic2d_SetOfPolylines

     TEST :
     ------

        Voir TestG2D/TestG21



=====================================================================*/

// PRO13369	//GG_280498
//			MapPolylineFromTo() should be called with exact number of points

#define G002    //GG_140400 Use SetPickedIndex method 

#include <Graphic2d_SetOfPolylines.ixx>
#include <Graphic2d_Vertex.hxx>
#include <Graphic2d_HSequenceOfVertex.hxx>

Graphic2d_SetOfPolylines::Graphic2d_SetOfPolylines (
	const Handle(Graphic2d_GraphicObject)& aGraphicObject)

  : Graphic2d_Line (aGraphicObject) {

}

void Graphic2d_SetOfPolylines::Add( const Standard_Real X, 
                                    const Standard_Real Y,
	                                const Standard_Boolean NewPolyline ) {
  Standard_Integer n = myPolylines.Length();
  Standard_ShortReal x = Standard_ShortReal( X ),
                     y = Standard_ShortReal( Y );
  Graphic2d_Vertex V( x, y );

	if( NewPolyline || (n == 0) ) {
	  Handle(Graphic2d_HSequenceOfVertex) P = 
					new Graphic2d_HSequenceOfVertex();
	  P->Append(V);
	  myPolylines.Append(P);
	} else {
	  Handle(Graphic2d_HSequenceOfVertex) P = myPolylines.Last();
	  if( !V.IsEqual(P->Value(P->Length())) ) {
	    P->Append(V);
	  }
	}

	myMinX	= Min(myMinX,x);
	myMinY	= Min(myMinY,y);
	myMaxX	= Max(myMaxX,x);
	myMaxY	= Max(myMaxY,y);
}

void Graphic2d_SetOfPolylines::Add( const Standard_Real X1, 
                                    const Standard_Real Y1,
	                                const Standard_Real X2, 
                                    const Standard_Real Y2 ) {

  Standard_ShortReal x1 = Standard_ShortReal( X1 ),
                     y1 = Standard_ShortReal( Y1 ),
                     x2 = Standard_ShortReal( X2 ),
                     y2 = Standard_ShortReal( Y2 );
  Graphic2d_Vertex V1( x1, y1 ),
                   V2( x2, y2 );
  Standard_Integer np = myPolylines.Length();

	if( !V1.IsEqual(V2) ) {
	  if( np == 0 ) {
	    Handle(Graphic2d_HSequenceOfVertex) P = 
					new Graphic2d_HSequenceOfVertex();
	    if( (x1 > x2) || (y1 > y2) ) {	
	      V1.SetCoord(x2,y2);	// seg orientation
	      V2.SetCoord(x1,y1);
	    }
	    P->Append(V1); P->Append(V2);
	    myPolylines.Append(P);
	  } else {		// Try to build a polyline 
				// Warning,take an aspirine before continue reading...
	    Handle(Graphic2d_HSequenceOfVertex) P,PP;
	    Standard_Integer ip,ipp,lv;
	    for( ip=np ; ip>0 ; ip-- ) {
	      P = myPolylines.Value(ip);//ref line to check
	      lv = P->Length();		//number of vertex in the ref line
	      if( V1.IsEqual(P->Value(lv)) ) {
		//first vertex of segment is the same that the last vertex of the
		//ref line
		if( ip > 1 ) { 	//Try to concatenate ref line with others
		  for( ipp=1 ; ipp<ip ; ipp++ ) {
	      	    PP = myPolylines.Value(ipp);	//other polyline 
		    if( V2.IsEqual(PP->Value(1)) ) {
		      PP->Prepend(P); 		// Move the ref line before this
		      myPolylines.Remove(ip);	// and destroy the ref line
		      ip = -1; break;
		    } else if( V2.IsEqual(PP->Value(PP->Length())) ) {
		      P->Reverse();
		      PP->Append(P); 		// Reverse & Move the ref line after this
		      myPolylines.Remove(ip);	// and destroy the ref line
		      ip = -1; break;
		    }
		  }
		}
	        if( ip > 0 ) {
		  P->Append(V2);		// Append new vertex to ref line
		  ip = -1;
		}
	      } else if( V2.IsEqual(P->Value(lv)) ) {
		//second vertex of segment is the same that the last vertex of the
		//ref line
		if( ip > 1 ) { 	//Try to concatenate ref line with others
		  for( ipp=1 ; ipp<ip ; ipp++ ) {
	      	    PP = myPolylines.Value(ipp);	//other polyline 
		    if( V1.IsEqual(PP->Value(1)) ) {
		      PP->Prepend(P); 		// Move the ref line before this
		      myPolylines.Remove(ip);	// and destroy the ref line
		      ip = -1; break;
		    } else if( V1.IsEqual(PP->Value(PP->Length())) ) {
		      P->Reverse();
		      PP->Append(P); 		// Reverse & Move the ref line after this
		      myPolylines.Remove(ip);	// and destroy the ref line
		      ip = -1; break;
		    }
		  }
		}
	        if( ip > 0 ) {
		  P->Append(V1);		// Append new vertex to ref line
		  ip = -1;
		}
	      } else if( V1.IsEqual(P->Value(1)) ) {
		//first vertex of segment is the same that the first vertex of the
		//ref line
		if( ip > 1 ) { 	//Try to concatenate ref line with others
		  for( ipp=1 ; ipp<ip ; ipp++ ) {
	      	    PP = myPolylines.Value(ipp);	//other polyline 
		    if( V2.IsEqual(PP->Value(PP->Length())) ) {
		      P->Prepend(PP); 		// Move this line before the ref line
		      myPolylines.Remove(ipp);	// and destroy this line
		      ip = -1; break;
		    } else if( V2.IsEqual(PP->Value(1)) ) {
		      PP->Reverse();
		      P->Prepend(PP); 		// Reverse & Move this line before the ref line 
		      myPolylines.Remove(ipp);	// and destroy this line
		      ip = -1; break;
		    }
		  }
		}
	        if( ip > 0 ) {
		  P->Prepend(V2);		// Prepend new vertex to ref line
		  ip = -1;
		}
	      } else if( V2.IsEqual(P->Value(1)) ) {
		//second vertex of segment is the same that the first vertex of the
		//ref line
		if( ip > 1 ) { 	//Try to concatenate ref line with others
		  for( ipp=1 ; ipp<ip ; ipp++ ) {
	      	    PP = myPolylines.Value(ipp);	//other polyline 
		    if( V1.IsEqual(PP->Value(PP->Length())) ) {
		      P->Prepend(PP); 		// Move this line before the ref line
		      myPolylines.Remove(ipp);	// and destroy this line
		      ip = -1; break;
		    } else if( V1.IsEqual(PP->Value(1)) ) {
		      PP->Reverse();
		      P->Prepend(PP); 		// Reverse & Move this line before the ref line
		      myPolylines.Remove(ipp);	// and destroy this line
		      ip = -1; break;
		    }
		  }
		}
	        if( ip > 0 ) {
		  P->Prepend(V1);		// Append new vertex to ref line
		  ip = -1;
		}
	      }
	    }
				//create new line
	    if( ip >= 0 ) {
	      Handle(Graphic2d_HSequenceOfVertex) P = 
					new Graphic2d_HSequenceOfVertex();
	      if( (x1 > x2) || (y1 > y2) ) {	
	        V1.SetCoord(x2,y2);	// seg orientation
	        V2.SetCoord(x1,y1);
	      }
	      P->Append(V1);
	      P->Append(V2);
	      myPolylines.Append(P);
	    }
	  }

	  myMinX = Min(myMinX,Min(x1,x2));
	  myMinY = Min(myMinY,Min(y1,y2));
	  myMaxX = Max(myMaxX,Max(x1,x2));
	  myMaxY = Max(myMaxY,Max(y1,y2));
	}
}

Standard_Integer Graphic2d_SetOfPolylines::Length () const {
   return myPolylines.Length();
}

Standard_Integer Graphic2d_SetOfPolylines::Length (const Standard_Integer aPrank) const {
    if( aPrank < 1 || aPrank > Length() ) 
                Standard_OutOfRange::Raise
			("the polyline rank is out of bounds in the set");
    return myPolylines.Value(aPrank)->Length();
}

void Graphic2d_SetOfPolylines::Values( const Standard_Integer aPrank,
				                       const Standard_Integer aVrank,
				                       Standard_Real &X,
                                       Standard_Real &Y ) const {

    if( aPrank < 1 || aPrank > Length() ) 
                Standard_OutOfRange::Raise
			("the polyline rank is out of bounds in the set");
    if( aVrank < 1 || aVrank > Length(aPrank) ) 
                Standard_OutOfRange::Raise
			("the point rank is out of bounds in the polyline");

    Graphic2d_Vertex V = myPolylines.Value(aPrank)->Value(aVrank);
    X = V.X(); Y = V.Y();
}

void Graphic2d_SetOfPolylines::Draw (const Handle(Graphic2d_Drawer)& aDrawer) {
Standard_Integer np = myPolylines.Length();
Standard_Boolean IsIn = Standard_False,transform = Standard_False;

  if( np <= 0 ) return;

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

  if ( IsIn ) {
    static gp_GTrsf2d theTrsf;
    Handle(Graphic2d_HSequenceOfVertex) P;
    Standard_Real A, B;
    Standard_ShortReal x1,y1,x2,y2;
    Standard_Integer ip,lp,iv;
    DrawLineAttrib(aDrawer);
    if( transform ) theTrsf = myGOPtr->Transform ();
    for( ip=1 ; ip<=np ; ip++ ) { 
      P = myPolylines.Value(ip);
      lp = P->Length();
      if( lp > 2 ) {
	P->Value(1).Coord(A,B);
	if( transform ) theTrsf.Transforms(A,B); 
	x1 = Standard_ShortReal( A ); 
    y1 = Standard_ShortReal( B );
	aDrawer->MapPolylineFromTo(x1,y1,lp); 
	for( iv=2 ; iv<lp ; iv++ ) {
	  P->Value(iv).Coord(A,B);
	  if( transform ) theTrsf.Transforms(A,B); 
	  x1 = Standard_ShortReal( A ); 
      y1 = Standard_ShortReal( B );
	  aDrawer->MapPolylineFromTo(x1,y1,0); 
	}
	P->Value(lp).Coord(A,B);
	if( transform ) theTrsf.Transforms(A,B); 
	x1 = Standard_ShortReal( A ); 
    y1 = Standard_ShortReal( B );
	aDrawer->MapPolylineFromTo(x1,y1,-1); 
      } else if( lp > 1 ) {
	P->Value(1).Coord(A,B);
	if( transform ) theTrsf.Transforms(A,B); 
	x1 = Standard_ShortReal( A ); 
    y1 = Standard_ShortReal( B );
	P->Value(2).Coord(A,B);
	if( transform ) theTrsf.Transforms(A,B); 
	x2 = Standard_ShortReal( A ); 
    y2 = Standard_ShortReal( B );
    aDrawer->MapSegmentFromTo(x1,y1,x2,y2);
      }
    }
  }
}

Standard_Boolean Graphic2d_SetOfPolylines::Pick (const Standard_ShortReal X,
					  const Standard_ShortReal Y,
					  const Standard_ShortReal aPrecision,
					  const Handle(Graphic2d_Drawer)& aDrawer)
{
Standard_Integer np = myPolylines.Length();
Standard_ShortReal SRX = X, SRY = Y;

    if ( (np > 0) && IsInMinMax (X, Y, aPrecision)) {
      if (myGOPtr->IsTransformed ()) {
        gp_GTrsf2d theTrsf((myGOPtr->Transform ()).Inverted ());
        Standard_Real RX = Standard_Real (SRX), RY = Standard_Real (SRY);
        theTrsf.Transforms (RX, RY);
        SRX = Standard_ShortReal (RX); SRY = Standard_ShortReal (RY);
      }

      Handle(Graphic2d_HSequenceOfVertex) P;
      Standard_Integer ip,iv=0,lp;
      Standard_ShortReal x1,y1,x2,y2;
      Standard_Real A,B;
      for( ip=1 ; ip<=np ; ip++ ) { 
        P = myPolylines.Value(ip);
        lp = P->Length();
        if( lp > 1 ) {
	     for( iv=1 ; iv<lp ; iv++ ) {
	       P->Value(iv).Coord(A,B);
	       x1 = Standard_ShortReal( A ); 
           y1 = Standard_ShortReal( B );
	       P->Value(iv+1).Coord(A,B);
	       x2 = Standard_ShortReal( A ); 
           y2 = Standard_ShortReal( B );
           if (IsOn (SRX, SRY, x1, y1, x2, y2, aPrecision) ) { 
#ifdef G002
	      SetPickedIndex((ip << 16) | iv);
#else
	      myPickedIndex = (ip << 16) | iv;
#endif
	      return Standard_True;
	    }
	  }
	}
      }
      return Standard_False;
    } 
    return Standard_False;
}

void Graphic2d_SetOfPolylines::Save(Aspect_FStream& aFStream) const
{
}