Integration of OCCT 6.5.0 from SVN
[occt.git] / src / Graphic2d / Graphic2d_Primitive.cxx
1 //
2 // ARCH         //GG_240298
3 //              Nouvelle methode privee ResetIndex() qui sert de relais
4 //              a SetIndex() permettant de reinitialiser l'index
5 //              courant d'exploration des primitives.
6 //
7 //              //GG_200898
8 //              Nouvelle methode privee ComputeMinMax() qui peut etre redefinie
9 //              dans chaque primitive de maniere a recalculer les minmax
10 //              au moment ou l'on en a besoin.
11 //
12 // S3593        //GG_270298
13 //              Nouvelle methodes privees SetFamily() appellee par
14 //              Line(),Marker(),Text(),Image() et Family()
15 //              qui retourne la famille a laquelle appartient la primitive
16 //              Voir son utilisation dans Graphic2d_GraphicObject.
17 //              Optimisation de la methode MinMax qui retourne maintenant
18 //              un BOOLEAN pour signale que la primitive est vide.
19 //
20 // G002     //GG_140400
21 //              Add SetPickedIndex() method
22 //              Add DrawElement() and DrawVertex() methods
23 //              Add IsOn() method
24 //              Add Highlight(),Unhighlight() IsHighlighted() method.
25 //
26
27 #include <Graphic2d_Primitive.ixx>
28 #include <Graphic2d_View.hxx>
29
30 Graphic2d_Primitive::Graphic2d_Primitive (const Handle(Graphic2d_GraphicObject)& aGraphicObject):
31         myMinX (ShortRealLast ()),
32         myMinY (ShortRealLast ()),
33         myMaxX (ShortRealFirst()),
34         myMaxY (ShortRealFirst()),
35         myColorIndex (1),
36         myNumOfElem(0),
37         myNumOfVert(0),
38         myDisplayMode(0),
39         myPickedIndex (0),
40         myTypeOfPrimitive(Graphic2d_TOP_UNKNOWN) ,
41         mySeqOfHighInd( new TColStd_HSequenceOfInteger() ) {
42
43         myGOPtr = aGraphicObject.operator->();
44         aGraphicObject->AddPrimitive (this);
45
46 }
47
48 Standard_Boolean Graphic2d_Primitive::ComputeMinMax () {
49 // cout << "*Graphic2d_Primitive::ComputeMinMax() must be redefined in the primitive '" << this->DynamicType()->Name() << "'" << endl;
50         return Standard_False;
51 }
52
53 Standard_Boolean Graphic2d_Primitive::MinMax (Standard_ShortReal & Minx, Standard_ShortReal & Maxx, Standard_ShortReal & Miny, Standard_ShortReal & Maxy) {
54 Standard_Boolean status = Standard_True;
55
56         if( (myMaxX < myMinX) || (myMaxY < myMinY) ) {
57           this->ComputeMinMax();
58         }
59
60         if( (myMaxX >= myMinX) && (myMaxY >= myMinY) ) {
61           if (myGOPtr->IsTransformed () ) {
62             Standard_Real MINX,MAXX,MINY,MAXY,X,Y;
63             gp_GTrsf2d aTrsf = myGOPtr->Transform ();
64             X = myMinX; Y = myMinY;
65             aTrsf.Transforms (X, Y);
66             MINX = MAXX = X; MINY = MAXY = Y;
67
68             X = myMaxX; Y = myMaxY;
69             aTrsf.Transforms (X, Y);
70             MINX = Min(MINX,X); MINY = Min(MINY,Y);
71             MAXX = Max(MAXX,X); MAXY = Max(MAXY,Y);
72
73             X = myMinX; Y = myMaxY;
74             aTrsf.Transforms (X, Y);
75             MINX = Min(MINX,X); MINY = Min(MINY,Y);
76             MAXX = Max(MAXX,X); MAXY = Max(MAXY,Y);
77
78             X = myMaxX; Y = myMinY;
79             aTrsf.Transforms (X, Y);
80             Minx = Standard_ShortReal(Min(MINX,X)); 
81         Miny = Standard_ShortReal(Min(MINY,Y));
82             Maxx = Standard_ShortReal(Max(MAXX,X)); 
83         Maxy = Standard_ShortReal(Max(MAXY,Y));
84           } else {
85             Minx = myMinX; Miny = myMinY;
86             Maxx = myMaxX; Maxy = myMaxY;
87           }
88         } else {
89           status = Standard_False;
90           Minx = myMinX; Miny = myMinY;
91           Maxx = myMaxX; Maxy = myMaxY;
92         }
93
94
95         return status;
96
97 }
98
99 void Graphic2d_Primitive::SetColorIndex (const Standard_Integer anIndex) {
100
101         myColorIndex    = anIndex;
102         ResetIndex();
103
104 }
105
106 Standard_Integer Graphic2d_Primitive::ColorIndex () const {
107
108         return myColorIndex;
109
110 }
111
112 Standard_Integer Graphic2d_Primitive::PickedIndex () const {
113
114         return myPickedIndex;
115
116 }
117
118 Standard_Integer Graphic2d_Primitive::NumOfElemIndices() const {
119   return myNumOfElem;
120 }
121
122 Standard_Integer Graphic2d_Primitive::NumOfVertIndices() const {
123  return myNumOfVert;
124 }
125
126 void Graphic2d_Primitive::SetPickedIndex (const Standard_Integer anIndex) {
127
128         myPickedIndex = anIndex;
129 }
130
131 Standard_Boolean Graphic2d_Primitive::IsOn( const Standard_ShortReal aX1,
132                                             const Standard_ShortReal aY1,
133                                             const Standard_ShortReal aX2,
134                                             const Standard_ShortReal aY2,
135                                             const Standard_ShortReal aPrecision) {
136
137   Standard_ShortReal DX = aX2 - aX1, DY = aY2 - aY1, 
138         dd = Standard_ShortReal(Sqrt(DX*DX + DY*DY));
139
140         return dd < aPrecision;
141 }
142
143 Standard_Boolean Graphic2d_Primitive::IsInMinMax (const Standard_ShortReal X,
144                                          const Standard_ShortReal Y,
145                                          const Standard_ShortReal aPrecision)
146  {
147 Standard_Boolean Result = Standard_False;
148
149         if( (myMaxX < myMinX) || (myMaxY < myMinY) ) {
150           this->ComputeMinMax();
151         }
152
153         if (! myGOPtr->IsTransformed ())
154                 Result =
155                         X >= myMinX - aPrecision && 
156                         X <= myMaxX + aPrecision && 
157                         Y >= myMinY - aPrecision && 
158                         Y <= myMaxY + aPrecision;
159         else {
160                 Standard_ShortReal minx, miny, maxx, maxy;
161                 MinMax(minx,maxx,miny,maxy);
162                 Result =
163                         X >= minx - aPrecision && 
164                         X <= maxx + aPrecision && 
165                         Y >= miny - aPrecision && 
166                         Y <= maxy + aPrecision;
167         }
168
169         return Result;
170
171 }
172
173 Handle(Graphic2d_Drawer) Graphic2d_Primitive::Drawer() const {
174
175         return myGOPtr->View()->Drawer();
176 }
177
178 Graphic2d_GOPtr Graphic2d_Primitive::PGraphicObject() const {
179
180         return myGOPtr;
181 }
182
183 void Graphic2d_Primitive::ResetIndex() {
184
185         myGOPtr->SetIndex(this);
186 }
187
188 void Graphic2d_Primitive::SetFamily( const Graphic2d_TypeOfPrimitive aFamily) {
189
190         myTypeOfPrimitive = aFamily;
191 }
192
193 Graphic2d_TypeOfPrimitive Graphic2d_Primitive::Family() const {
194
195         return myTypeOfPrimitive;
196 }
197
198 void Graphic2d_Primitive::DrawElement( const Handle(Graphic2d_Drawer)& /*aDrawer*/,
199                                        const Standard_Integer /*anElement*/) {
200    cout << " *** Graphic2d_Primitive::DrawElement() method must be redefined ***" << endl;
201 }
202
203 //SAV
204 void Graphic2d_Primitive
205 ::DrawPickedElements( const Handle(Graphic2d_Drawer)& /*aDrawer*/ )
206 {
207 #ifdef DEB
208   cout << " *** Graphic2d_Primitive::DrawPickedElements() method must be redefined ***"
209     << endl;
210 #endif
211 }
212
213 // SAV          
214 void Graphic2d_Primitive
215 ::DrawSelectedElements( const Handle(Graphic2d_Drawer)& /*aDrawer*/ )
216 {
217 #ifdef DEB
218   cout << " *** Graphic2d_Primitive::DrawSelectedElements() method must be redefined ***"
219     << endl;
220 #endif
221 }
222
223 // SAV
224 Standard_Boolean Graphic2d_Primitive::SetElementsSelected()
225 {
226   return Standard_False;
227 }
228
229 // SAV
230 Standard_Boolean Graphic2d_Primitive::HasSelectedElements()
231 {
232   /* if this method is not redefined we should return false
233      to achieve the standard behavior of Graphic2d_GraphicObject::Draw() method.
234      already redefined for SetOfMarkers
235   */
236   return Standard_False;
237 }
238
239 // SAV
240 void Graphic2d_Primitive::ClearSelectedElements()
241 {
242   // parent does nothing! redefine please
243 #ifdef DEB
244   cout << " *** Graphic2d_Primitive::ClearSelectedElements() method must be redefined ***"
245     << endl;
246 #endif
247 }
248
249 // SAV
250 Standard_Boolean Graphic2d_Primitive
251 ::PickByCircle( const Standard_ShortReal X,
252                 const Standard_ShortReal Y,
253                 const Standard_ShortReal Radius,
254                 const Handle(Graphic2d_Drawer)& /*aDrawer*/,
255                 const Graphic2d_PickMode aPickMode )
256 {
257   // parent does nothing! redefine please
258 #ifdef SAV_DEB
259   cout << " *** Graphic2d_Primitive::PickByCircle() method must be redefined ***"
260     << endl;
261 #endif
262   return Standard_False;
263 }
264
265
266 void Graphic2d_Primitive::DrawVertex( const Handle(Graphic2d_Drawer)& /*aDrawer*/,
267                                        const Standard_Integer /*aVertex*/) {
268    cout << " *** Graphic2d_Primitive::DrawVertex() method must be redefined ***" << endl;
269 }
270
271 void Graphic2d_Primitive::Highlight( const Standard_Integer anIndex) {
272
273      mySeqOfHighInd->Append(anIndex);
274 }
275
276 void Graphic2d_Primitive::Unhighlight () {
277     if ( ! mySeqOfHighInd->IsEmpty() )
278         mySeqOfHighInd->Clear();
279 }
280
281 Standard_Boolean Graphic2d_Primitive::IsHighlighted () const {
282     return ( ! mySeqOfHighInd->IsEmpty() );
283 }
284
285 Handle(TColStd_HSequenceOfInteger) Graphic2d_Primitive::HighlightIndices() const {
286
287     return mySeqOfHighInd;
288 }
289
290 const TColStd_MapOfInteger& Graphic2d_Primitive::PickedIndices() const
291 {
292   return myPickedIndices;
293 }
294
295
296 void Graphic2d_Primitive::SetDisplayMode( const Standard_Integer aMode )  {
297
298  myDisplayMode = aMode;
299
300
301
302 Standard_Integer Graphic2d_Primitive::DisplayMode() const {
303
304  return myDisplayMode;
305 }
306
307 Standard_Boolean Graphic2d_Primitive::Pick( const Standard_ShortReal Xmin,
308                                             const Standard_ShortReal Ymin,
309                                             const Standard_ShortReal Xmax,
310                                             const Standard_ShortReal Ymax,
311                                             const Handle(Graphic2d_Drawer)& /*aDrawer*/,
312                                             const Graphic2d_PickMode aPickMode ) {
313
314  
315   Standard_ShortReal X1 = Min( Xmax, Xmin );
316   Standard_ShortReal Y1 = Min( Ymax, Ymin );
317   Standard_ShortReal X2 = Max( Xmax, Xmin );
318   Standard_ShortReal Y2 = Max( Ymax, Ymin );
319
320   Standard_Boolean Result = Standard_False;
321
322   if ( ( myMaxX < myMinX ) || ( myMaxY < myMinY ) )
323     ComputeMinMax();
324   Standard_ShortReal minx = myMinX, miny = myMinY, 
325                      maxx = myMaxX, maxy = myMaxY;
326   if ( myGOPtr->IsTransformed() )
327      MinMax(minx,maxx,miny,maxy);
328   switch ( aPickMode )  {
329   case Graphic2d_PM_INCLUDE: 
330
331     Result =
332             minx >= X1 &&
333             miny >= Y1 &&
334             maxx <= X2 &&
335             maxy <= Y2;
336     break;
337   case Graphic2d_PM_EXCLUDE: 
338
339     Result =
340                         maxx < X1 || 
341                         maxy < Y1 || 
342                         minx > X2 || 
343                         miny > Y2;
344     break;
345   case Graphic2d_PM_INTERSECT: {
346
347       Standard_ShortReal a = Max( minx, X1 );
348       Standard_ShortReal b = Max( miny, Y1 );
349       Standard_ShortReal c = Min( maxx, X2 );
350       Standard_ShortReal d = Min( maxy, Y2 );
351
352       Result = ( a >= c || d <= b ) ? 0 : 1;
353
354       if ( Result && 
355            ( X1 >= minx &&
356              Y1 >= miny  &&
357              X2 <= maxx  &&
358              Y2 <= maxy ) ) Result = 0;;
359
360       
361   }
362   break;                      
363   default: 
364          Result = Standard_False;
365   }
366      
367   return Result;
368
369 }