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

     FONCTION :
     ----------
        Classe Graphic2d_SetOfSegments

     TEST :
     ------

        Voir TestG2D/TestG21

     REMARQUES:
     ----------

     HISTORIQUE DES MODIFICATIONS   :
     --------------------------------

      27-01-98 : GG ; OPTIMISATION LOADER
                    Transformer les variables static globales en static
                   locales.

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

#define G002    //GG_140400 Use SetPickedIndex method.
                // Add new DrawElement(), DrawVertex() methods

#define VERTEXMARKER 2
#define DEFAULTMARKERSIZE 3.0


#include <Graphic2d_SetOfSegments.ixx>

#include <TShort_Array1OfShortReal.hxx>

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

  : Graphic2d_Line (aGraphicObject) {

}

void Graphic2d_SetOfSegments::Add (
	const Standard_Real X1, const Standard_Real Y1,
	const Standard_Real X2, const Standard_Real Y2)
{
Standard_ShortReal x1 = Standard_ShortReal( X1 );
Standard_ShortReal y1 = Standard_ShortReal( Y1 );
Standard_ShortReal x2 = Standard_ShortReal( X2 );
Standard_ShortReal y2 = Standard_ShortReal( Y2 );

	if ((x1 == x2) && (y1 == y2)) return;

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

	myX1.Append(x1); 
    myY1.Append(y1); 
    myX2.Append(x2); 
    myY2.Append(y2);
    
}

Standard_Integer Graphic2d_SetOfSegments::Length () const {
   return myX1.Length();
}

void Graphic2d_SetOfSegments::Values (const Standard_Integer aRank,
				      Standard_Real &X1,Standard_Real &Y1,
				      Standard_Real &X2,Standard_Real &Y2) const {

	if( aRank < 1 || aRank > myX1.Length() ) 
                Standard_OutOfRange::Raise
			("the segment rank is out of bounds in the set");

	X1 = myX1(aRank);
	Y1 = myY1(aRank);
	X2 = myX2(aRank);
	Y2 = myY2(aRank);

}

void Graphic2d_SetOfSegments::Draw (const Handle(Graphic2d_Drawer)& aDrawer) {
  Standard_Integer i,ns = Length(),bufferize = 0;
  Standard_Boolean IsIn = Standard_False,transform = Standard_False;

  if( ns <= 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;
    DrawLineAttrib(aDrawer);
    if( transform ) theTrsf = myGOPtr->Transform ();

    if( ns > 2 ) bufferize = 1; 
    Standard_Real A,B;
    Standard_ShortReal x1,y1,x2,y2;
    A = myX1(1); B = myY1(1);
    if( transform ) theTrsf.Transforms(A,B);
    x1 = Standard_ShortReal( A ); 
    y1 = Standard_ShortReal( B );
    A = myX2(1); B = myY2(1);
    if( transform ) theTrsf.Transforms(A,B);
    x2 = Standard_ShortReal( A ); 
    y2 = Standard_ShortReal( B );
    aDrawer->MapSegmentFromTo(x1,y1,x2,y2,bufferize);
    for( i=2 ; i<ns ; i++ ) {
      A = myX1(i); B = myY1(i);
      if( transform ) theTrsf.Transforms(A,B);
      x1 = Standard_ShortReal( A ); 
      y1 = Standard_ShortReal( B );
      A = myX2(i); B = myY2(i);
      if( transform ) theTrsf.Transforms(A,B);
      x2 = Standard_ShortReal( A ); 
      y2 = Standard_ShortReal( B );
      aDrawer->MapSegmentFromTo(x1,y1,x2,y2,0);
    }
    A = myX1(ns); B = myY1(ns);
    if( transform ) theTrsf.Transforms(A,B);
    x1 = Standard_ShortReal( A ); 
    y1 = Standard_ShortReal( B );
    A = myX2(ns); B = myY2(ns);
    if( transform ) theTrsf.Transforms(A,B);
    x2 = Standard_ShortReal( A ); 
    y2 = Standard_ShortReal( B );
    aDrawer->MapSegmentFromTo(x1,y1,x2,y2,-bufferize);
  }
  myNumOfElem = ns;
  myNumOfVert = 2*ns;
}

#ifdef G002

void Graphic2d_SetOfSegments::DrawElement( const Handle(Graphic2d_Drawer)& aDrawer,
                                      const Standard_Integer anIndex) {
  Standard_Integer ns = Length(),bufferize = 0;
  Standard_Boolean IsIn = Standard_False,transform = Standard_False;

  if ( ns <= 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 ) {
   if ( anIndex > 0 && anIndex <= ns ) { //Draw element
    static gp_GTrsf2d theTrsf;
    DrawLineAttrib(aDrawer);
    if( transform ) theTrsf = myGOPtr->Transform ();

    if( ns > 2 ) bufferize = 1; 
    Standard_Real A,B;
    Standard_ShortReal x1,y1,x2,y2;
    if ( anIndex == 1) {
      A = myX1(1); B = myY1(1);
      if( transform ) theTrsf.Transforms(A,B);
      x1 = Standard_ShortReal( A ); 
      y1 = Standard_ShortReal( B );
      A = myX2(1); B = myY2(1);
      if( transform ) theTrsf.Transforms(A,B);
      x2 = Standard_ShortReal( A ); 
      y2 = Standard_ShortReal( B );
      aDrawer->MapSegmentFromTo(x1,y1,x2,y2,bufferize);
    } else if ( anIndex == ns ) {
      A = myX1(ns); B = myY1(ns);
      if( transform ) theTrsf.Transforms(A,B);
      x1 = Standard_ShortReal( A ); 
      y1 = Standard_ShortReal( B );
      A = myX2(ns); B = myY2(ns);
      if( transform ) theTrsf.Transforms(A,B);
      x2 = Standard_ShortReal( A ); 
      y2 = Standard_ShortReal( B );
      aDrawer->MapSegmentFromTo(x1,y1,x2,y2,-bufferize);
    } else {
      A = myX1(anIndex); B = myY1(anIndex);
      if( transform ) theTrsf.Transforms(A,B);
      x1 = Standard_ShortReal( A ); 
      y1 = Standard_ShortReal( B );
      A = myX2(anIndex); B = myY2(anIndex);
      if( transform ) theTrsf.Transforms(A,B);
      x2 = Standard_ShortReal( A ); 
      y2 = Standard_ShortReal( B );
      aDrawer->MapSegmentFromTo(x1,y1,x2,y2,0);
    }
   }
  }

}

void Graphic2d_SetOfSegments::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 = myX1.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 (myX1(anIndex));
        B = Standard_Real (myY1(anIndex));
        aTrsf.Transforms (A, B);
        X = Standard_ShortReal (A);
        Y = Standard_ShortReal (B);
      } else {
        X = myX1(anIndex);
        Y = myY1(anIndex);
      }
      aDrawer->MapMarkerFromTo(VERTEXMARKER,X,Y,
			DEFAULTMARKERSIZE,DEFAULTMARKERSIZE,0.0);
    } else if ( anIndex > 0 && anIndex > nbpoints &&  anIndex <= 2*nbpoints) {
      Standard_ShortReal X,Y;
      DrawMarkerAttrib (aDrawer);
      
      if (myGOPtr->IsTransformed ()) {
        gp_GTrsf2d aTrsf = myGOPtr->Transform ();
        Standard_Real A, B;
        A = Standard_Real (myX2(anIndex-nbpoints));
        B = Standard_Real (myY2(anIndex-nbpoints));
        aTrsf.Transforms (A, B);
        X = Standard_ShortReal (A);
        Y = Standard_ShortReal (B);
      } else {
        X = myX2(anIndex-nbpoints);
        Y = myY2(anIndex-nbpoints);
      }
      aDrawer->MapMarkerFromTo(VERTEXMARKER,X,Y,
			DEFAULTMARKERSIZE,DEFAULTMARKERSIZE,0.0);
     }
  }
}
#endif

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

   Standard_Integer i,ns = Length();
   Standard_ShortReal SRX = X, SRY = Y;
 
   if ( (ns > 0) && 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 = 1; i <= myX1.Length(); i++ ) {
       if ( Graphic2d_Primitive::IsOn( SRX, SRY, myX1(i), myY1(i), aPrecision) ) {
           SetPickedIndex(-i);
           return Standard_True;
       } else if ( Graphic2d_Primitive::IsOn( SRX, SRY, myX2(i), myY2(i), aPrecision) ) {
           SetPickedIndex(-ns-i);
           return Standard_True;
       } else if (IsOn( SRX, SRY, myX1(i), myY1(i), myX2(i), myY2(i), aPrecision) )  {
           SetPickedIndex(i);
           return Standard_True;
       }
    }
#else

    for (i=1; i<=myX1.Length(); i++) {
        if (IsOn (SRX, SRY, myX1 (i), myY1 (i), 
				myX2 (i), myY2 (i), aPrecision) ) {
                myPickedIndex = i;                       
                return Standard_True;
        }
    }

#endif
   
    return Standard_False;
  } else 
   return Standard_False;

}


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