summaryrefslogtreecommitdiff
path: root/src/Graphic2d/Graphic2d_SetOfMarkers.cxx
blob: b0bf61349804b82d7a1d15657bab76cae0e50b43 (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
470
471
472
473
474
475
476
477
478
479
480
#define G002    //GG_140400 Use SetPickedIndex method 
                // Add new DrawElement(), DrawVertex() methods

#define VERTEXMARKER 2
#define DEFAULTMARKERSIZE 3.0

#include <Graphic2d_SetOfMarkers.ixx>


//SAV
#include <TColStd_HSequenceOfInteger.hxx>
#include <TShort_HArray1OfShortReal.hxx>
#include <Aspect_WindowDriver.hxx>
#include <TColStd_MapOfInteger.hxx>
#include <TColStd_MapIteratorOfMapOfInteger.hxx>
#include <gp_Circ2d.hxx> 
#include <gp_Ax2d.hxx>
#include <gp_Pnt2d.hxx>
#include <gp_Dir2d.hxx>
#include <Aspect_Units.hxx>

#define OCC402

Graphic2d_SetOfMarkers
::Graphic2d_SetOfMarkers( const Handle(Graphic2d_GraphicObject)& aGraphicObject )
: Graphic2d_Line (aGraphicObject), myHLimit( -1 ), mySuppressHigh( Standard_False )
{

  	SetFamily(Graphic2d_TOP_MARKER);
    
}

void Graphic2d_SetOfMarkers::Add ( const Standard_Real X, const Standard_Real Y) {

  Standard_ShortReal x = Standard_ShortReal( X );
  Standard_ShortReal y = Standard_ShortReal( Y );

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

  myType.Append(0);
  myX.Append(x); 
  myY.Append(y); 
  myWidth.Append(0.);
  myHeight.Append(0.);
  myAngle.Append(0.);
  myScaledWidth = 0.;

}

void Graphic2d_SetOfMarkers::Add (
        const Standard_Integer anIndex,
        const Standard_Real X, const Standard_Real Y,
        const Standard_Real aWidth, const Standard_Real anHeight,
        const Quantity_PlaneAngle anAngle)
{ 
Standard_ShortReal x  = Standard_ShortReal( X );
Standard_ShortReal y  = Standard_ShortReal( Y );
Standard_ShortReal w  = Standard_ShortReal( aWidth );
Standard_ShortReal h  = Standard_ShortReal( anHeight );
Standard_ShortReal a  = Standard_ShortReal( anAngle );
Standard_ShortReal x1 = Standard_ShortReal( x - w/2. );
Standard_ShortReal x2 = Standard_ShortReal( x + w/2. );
Standard_ShortReal y1 = Standard_ShortReal( y - h/2. );
Standard_ShortReal y2 = Standard_ShortReal( y + h/2. );

        if( anIndex <= 0 )
                Graphic2d_MarkerDefinitionError::Raise
                        ("The marker index must be > 0");
 
        if (aWidth <= 0.0)
                Graphic2d_MarkerDefinitionError::Raise ("The width = 0.");
 
        if (anHeight <= 0.0)
                Graphic2d_MarkerDefinitionError::Raise ("The height = 0.");
 
	myMinX = Min(myMinX,x1);
	myMinY = Min(myMinY,y1);
	myMaxX = Max(myMaxX,x2);
	myMaxY = Max(myMaxY,y2);

	myType.Append(anIndex);
	myX.Append(x); 
        myY.Append(y); 
	myWidth.Append(w);
	myHeight.Append(h);
	myAngle.Append(a);
        myScaledWidth = w;
}

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

void Graphic2d_SetOfMarkers::Values(
                      const Standard_Integer aRank,
				      Standard_Integer    &anIndex,
				      Quantity_Length     & X,
                      Quantity_Length     & Y,
                      Quantity_Length     & aW, 
                      Quantity_Length     & aH,
                      Quantity_PlaneAngle & anAngle ) const
{
	if( aRank < 1 || aRank > myX.Length() ) 
                Standard_OutOfRange::Raise
			("the marker rank is out of bounds in the set");

	anIndex = myType( aRank );
	X       = Quantity_Length( myX( aRank ) );
	Y       = Quantity_Length( myY( aRank ) );
    aW      = Quantity_Length( myWidth( aRank ) );
    aH      = Quantity_Length( myHeight( aRank ) );
    anAngle = Quantity_PlaneAngle( myAngle( aRank ) );
}

void Graphic2d_SetOfMarkers::Draw (const Handle(Graphic2d_Drawer)& aDrawer) {
 
  myNumOfElem = Length();
  myNumOfVert = Length();
  Standard_Integer i,l = Length(),bufferize = 0;

  if ( (l > 0) && aDrawer->IsIn (myMinX,myMaxX,myMinY,myMaxY)){
    DrawMarkerAttrib(aDrawer);

    if( l > 2 ) bufferize = 1; 
    aDrawer->MapMarkerFromTo
	(myType(1), myX(1), myY(1), myWidth(1), myHeight(1), 
						myAngle(1),bufferize);
    for( i=2 ; i<l ; i++ ) {
      aDrawer->MapMarkerFromTo
	(myType(i), myX(i), myY(i), myWidth(i), myHeight(i), myAngle(i),0);
    }
    aDrawer->MapMarkerFromTo
    	(myType(l), myX(l), myY(l), myWidth(l), myHeight(l), 
						myAngle(l),-bufferize);
  }
}

#ifdef G002

void Graphic2d_SetOfMarkers::DrawElement( const Handle(Graphic2d_Drawer)& aDrawer,
                                      const Standard_Integer anIndex) {
  Standard_Integer l = Length(), bufferize = 0;

  if ( (l > 0) && aDrawer->IsIn( myMinX,myMaxX,myMinY,myMaxY) ) {
   if ( anIndex > 0 && anIndex <= l ) { //Draw element
    DrawMarkerAttrib(aDrawer);
    if ( l > 2 ) bufferize = 1; 
    if ( anIndex == 1 )     
     aDrawer->MapMarkerFromTo
	 ( myType(1), myX(1), myY(1), myWidth(1), myHeight(1), 
						myAngle(1),bufferize );
    else if ( anIndex == l )
      aDrawer->MapMarkerFromTo
      (myType(l), myX(l), myY(l), myWidth(l), myHeight(l), 
						myAngle(l),-bufferize);
    else 
      aDrawer->MapMarkerFromTo
	  ( myType(anIndex), myX(anIndex), myY(anIndex), myWidth(anIndex), 
        myHeight(anIndex), myAngle(anIndex),0);
    }    
  }

}

void Graphic2d_SetOfMarkers::DrawVertex( const Handle(Graphic2d_Drawer)& /*aDrawer*/,
                                         const Standard_Integer /*anIndex*/) {
}
#endif

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


#ifdef OCC402
  myPickedIndices.Clear();
#endif

  Standard_Integer i,l = Length();
  Standard_ShortReal SRX = X, SRY = Y;
 
  if ( (l > 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 <= myX.Length(); i++ ) {
       if ( Graphic2d_Primitive::IsOn ( SRX, SRY, myX(i), myY(i), aPrecision) ) {
           SetPickedIndex(-i);
           return Standard_True;
       } else if ( (i < myX.Length() ) &&  
                   SRX >= myX(i)-aPrecision &&
	               SRX <= myX(i)+aPrecision &&
	               SRY >= myY(i)-aPrecision &&
	               SRY <= myY(i)+aPrecision ) {
            SetPickedIndex(i);
            return Standard_True;
       }
    }
#else

    for ( i=1; i<=myX.Length(); i++) {
	  if (SRX >= myX(i)-aPrecision &&
	      SRX <= myX(i)+aPrecision &&
	      SRY >= myY(i)-aPrecision &&
	      SRY <= myY(i)+aPrecision) {
        myPickedIndex = i;
		return Standard_True;
      }
    }

#endif

   TShort_Array1OfShortReal arr1X( 1, myX.Length() );
   TShort_Array1OfShortReal arr1Y( 1, myY.Length() );
   for ( i = 1; i < myX.Length(); i++ ) {
      arr1X.SetValue( i, myX(i) );
      arr1Y.SetValue( i, myY(i) );
   }

   if ( IsIn( SRX, SRY,  arr1X, arr1Y, aPrecision ) ) {
#ifdef G002
	      SetPickedIndex(0);
#else
	      myPickedIndex = 0;
#endif
	      return Standard_True;
   }

    return Standard_False;
  } //end if it's in MinMax
  return Standard_False;
}

Standard_Boolean Graphic2d_SetOfMarkers::Pick( const Standard_ShortReal X1,
					       const Standard_ShortReal Y1,
					       const Standard_ShortReal X2,
					       const Standard_ShortReal Y2,
					       const Handle(Graphic2d_Drawer)& drawer,
					       const Graphic2d_PickMode pMode )
{
#ifdef OCC402
  mySuppressHigh = Standard_False;
  Standard_ShortReal minX = Min( X2, X1 ),
                     minY = Min( Y2, Y1 ),
                     maxX = Max( X2, X1 ),
                     maxY = Max( Y2, Y1 );


  // no check for transformation...
  // pick mode - INCLUDE only.
  
  switch( pMode ) {

    case Graphic2d_PM_INCLUDE : {
	
      TColStd_MapOfInteger toHighlight; 

      for (Standard_Integer i=1; i<=myX.Length(); i++)
	if ( myX(i) >= minX && myX(i) <= maxX &&
	     myY(i) >= minY && myY(i) <= maxY ) {

	  if ( myMapOfSelected.Extent() && myMapOfSelected.Contains( i ) )
	    continue;
	  if ( myPickedIndices.Contains( i ) )
	    myPickedIndices.Remove( i );
	  toHighlight.Add( i );
	}
      
      if ( myPickedIndices.Extent() && drawer->IsWindowDriver() ) {
	Handle(Aspect_WindowDriver) theDriver = drawer->WindowDriver();
	theDriver->BeginDraw( Standard_True );
	drawer->SetOverride( Standard_True );
	drawer->SetOverrideColor( ColorIndex() );
	DrawPickedElements( drawer );
	drawer->SetOverride( Standard_False );
	theDriver->EndDraw();
      }
      
      myPickedIndices = toHighlight;

      if ( myPickedIndices.Extent() )
	return Standard_True;
      break;
    }
    default: /* Do nothing */ break;
  }
#endif
  return Standard_False;
}


Standard_Boolean Graphic2d_SetOfMarkers
::PickByCircle( const Standard_ShortReal x,
	        const Standard_ShortReal y,
	        const Standard_ShortReal radius,
	        const Handle(Graphic2d_Drawer)& drawer,
	        const Graphic2d_PickMode pMode )
{
#ifdef OCC402

  mySuppressHigh = Standard_True;
  myPickedIndices.Clear();

  // no check for transformation...
  // pick mode - INCLUDE only.
  
  switch( pMode ) 
  {

    case Graphic2d_PM_INCLUDE : {
	
      Standard_Real xdist, ydist;
      Standard_Real rad;
      for (Standard_Integer i=1; i<=myX.Length(); i++) {
	xdist = Abs( myX(i) - x );
	ydist = Abs( myY(i) - y );
	rad = radius - myScaledWidth / 2;
	if ( xdist <= rad && ydist <= rad )
	  myPickedIndices.Add( i );
      }
      
      if ( myPickedIndices.Extent() )
	return Standard_True;
      break;
    }
    default: /* Do nothing */ break;
  }
#endif
  return Standard_False;
}


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

//SAV
void Graphic2d_SetOfMarkers
::DrawPickedElements( const Handle(Graphic2d_Drawer)& aDrawer )
{
#ifdef OCC402
  if ( !mySuppressHigh ) {
    Standard_Integer length = myPickedIndices.Extent();
    if ( length ) {
      
      if ( myHLimit > 0 && length > myHLimit )
	length = myHLimit;
      
      Handle(TShort_HArray1OfShortReal) x = 
	new TShort_HArray1OfShortReal( 1, length );
      Handle(TShort_HArray1OfShortReal) y = 
	new TShort_HArray1OfShortReal( 1, length );
      
      TColStd_MapIteratorOfMapOfInteger it( myPickedIndices );
      for ( Standard_Integer i = 1; i <= length; it.Next(), i++ ) {
	x->SetValue( i, myX( it.Key() ) );
	y->SetValue( i, myY( it.Key() ) );
      }
      
      DrawElements( aDrawer, x, y );
    }
  }
#endif
}

//SAV
void Graphic2d_SetOfMarkers
::DrawSelectedElements( const Handle(Graphic2d_Drawer)& aDrawer )
{
#ifdef OCC402
  Standard_Integer length = myMapOfSelected.Extent();
  if ( length ) {
    Handle(TShort_HArray1OfShortReal) x = 
      new TShort_HArray1OfShortReal( 1, length );
    Handle(TShort_HArray1OfShortReal) y = 
      new TShort_HArray1OfShortReal( 1, length );
    TColStd_MapIteratorOfMapOfInteger it( myMapOfSelected );
    Standard_Integer index = 1;
    for (; it.More(); it.Next(), index++ ) {
      x->SetValue( index, myX( it.Key() ) );
      y->SetValue( index, myY( it.Key() ) );
    }
  
    DrawElements( aDrawer, x, y );
    //myPickedIndices.Clear();
  }
#endif
}

//SAV
void Graphic2d_SetOfMarkers
::DrawElements( const Handle(Graphic2d_Drawer)& aDrawer,
	        const Handle(TShort_HArray1OfShortReal)& x,
	        const Handle(TShort_HArray1OfShortReal)& y )
{
#ifdef OCC402
  DrawMarkerAttrib(aDrawer);

  Standard_Integer type = myType( 1 );
  Standard_ShortReal width = myWidth( 1 ), 
                     height = myHeight( 1 ),
                     angle = myAngle( 1 );
  aDrawer->MapMarkersFromTo( type, x, y, width, height, angle, 0);
#endif
}

//SAV
Standard_Boolean Graphic2d_SetOfMarkers::SetElementsSelected()
{
#ifdef OCC402
  if ( myPickedIndices.Extent() ) {
    myMapOfSelected.Clear();
    myMapOfSelected = myPickedIndices;
    //myPickedIndices.Clear();
  }
#endif
  return Standard_True;
}

//SAV
Standard_Boolean Graphic2d_SetOfMarkers::HasSelectedElements()
{
#ifdef OCC402
  return (myMapOfSelected.Extent() > 0);
#else
  return Standard_False;
#endif
}

//SAV
void Graphic2d_SetOfMarkers::ClearSelectedElements()
{
#ifdef OCC402
  myMapOfSelected.Clear();
#endif
}


//SAV
const TColStd_MapOfInteger& Graphic2d_SetOfMarkers::GetSelectedElements() const
{
 return myMapOfSelected;
}

//SAV
void Graphic2d_SetOfMarkers::AddOrRemoveSelected(const Standard_Integer index)
{
#ifdef OCC402
  if ( myMapOfSelected.Contains( index ) )
    myMapOfSelected.Remove( index );
  else
    myMapOfSelected.Add( index );
#endif
}

//SAV
void Graphic2d_SetOfMarkers::SetHighlightedLimit(const Standard_Integer number)
{
#ifdef OCC402
  if ( number > 0 )
    myHLimit = number;
#endif
}

void Graphic2d_SetOfMarkers::SetScaledWidth( const Standard_ShortReal width )
{
#ifdef OCC402
 myScaledWidth = width;
#endif
}