Integration of OCCT 6.5.0 from SVN
[occt.git] / src / Graphic2d / Graphic2d_HidingGraphicObject.cxx
1 #define xTRACE
2
3 // S3569        //Gestion des zones cachantes
4
5 #define G002        //GG_140400
6 //              Manage highlighting.
7
8 #include <Graphic2d_HidingGraphicObject.ixx>
9 #include <Graphic2d_Vertex.hxx>
10 #include <Aspect.hxx>
11
12 #define BASE_PRIORITY 1000
13 #define MAX_PRIORITY 999
14 #define MAX_POINTS 1024
15
16 Graphic2d_HidingGraphicObject::Graphic2d_HidingGraphicObject
17        (const Handle(Graphic2d_View)& aView,
18         const Graphic2d_Array1OfVertex& aFrame) :
19 Graphic2d_GraphicObject(aView) ,
20 myXmin(RealLast()),myYmin(RealLast()),myXmax(RealFirst()),myYmax(RealFirst())
21 {
22
23   SetFrame(aFrame);
24   SetHidingColorIndex();
25   SetFrameColorIndex();
26   SetFrameTypeIndex();
27   SetFrameWidthIndex();
28
29 }
30
31 Graphic2d_HidingGraphicObject::Graphic2d_HidingGraphicObject
32        (const Handle(Graphic2d_View)& aView,
33         const Graphic2d_TypeOfFrame aType,
34         const Quantity_Length aMargin1,
35         const Quantity_Length aMargin2) :
36 Graphic2d_GraphicObject(aView), 
37 myXmin(RealLast()),myYmin(RealLast()),myXmax(RealFirst()),myYmax(RealFirst())
38 {
39
40   SetFrame(aType,aMargin1,aMargin2);
41   SetHidingColorIndex();
42   SetFrameColorIndex();
43   SetFrameTypeIndex();
44   SetFrameWidthIndex();
45
46 }
47
48 void Graphic2d_HidingGraphicObject::SetFrame(const Graphic2d_Array1OfVertex& aFrame) {
49 static Graphic2d_Vertex v;
50 Standard_Integer l = aFrame.Length();
51 Standard_Real x,y;
52
53         myFrame = new Graphic2d_HArray1OfVertex(1,l);
54         for( Standard_Integer i=1,j=aFrame.Lower() ; i<=l ; i++,j++) {
55           v = aFrame.Value(j); v.Coord(x,y);
56           myXmin = Min(myXmin,x); myYmin = Min(myYmin,y);
57           myXmax = Max(myXmax,x); myYmax = Max(myYmax,y);
58           myFrame->SetValue(i,v);
59         }
60         myTypeOfFrame = Graphic2d_TOF_UNKNOWN;
61         myIsComputed = Standard_True;
62 }
63
64 void Graphic2d_HidingGraphicObject::SetFrame(
65                         const Graphic2d_TypeOfFrame aType,
66                         const Quantity_Length aMargin1,
67                         const Quantity_Length aMargin2) {
68         myFrame.Nullify();
69         myTypeOfFrame = aType;
70         myFrameMargin1 = aMargin1;
71         myFrameMargin2 = aMargin2;
72         if( myTypeOfFrame != Graphic2d_TOF_UNKNOWN )
73                                 myIsComputed = Standard_False;
74         else myIsComputed = Standard_True;
75 }
76
77 void Graphic2d_HidingGraphicObject::SetHidingColorIndex(const Standard_Integer anIndex) {
78         myHidingColorIndex = anIndex;
79 }
80
81 void Graphic2d_HidingGraphicObject::SetFrameColorIndex(const Standard_Integer anIndex) {
82         myFrameColorIndex = anIndex;
83 }
84
85 void Graphic2d_HidingGraphicObject::SetFrameTypeIndex(const Standard_Integer anIndex) {
86         myFrameTypeIndex = anIndex;
87 }
88
89 void Graphic2d_HidingGraphicObject::SetFrameWidthIndex(const Standard_Integer anIndex) {
90         myFrameWidthIndex = anIndex;
91 }
92
93 Standard_Integer Graphic2d_HidingGraphicObject::BasePriority () const {
94
95         return BASE_PRIORITY;
96
97 }
98
99 Standard_Integer Graphic2d_HidingGraphicObject::MaxPriority () const {
100
101         return MAX_PRIORITY;
102
103 }
104
105 void Graphic2d_HidingGraphicObject::Draw (const Handle(Graphic2d_Drawer)& aDrawer, const Standard_Boolean Reset) {
106
107         if( !IsDrawable() ) return;
108
109         if( !IsDisplayed() && !IsHighlighted() ) return;
110
111         if( !myIsUpToDate || !myIsComputed  ) {
112           static Graphic2d_Vertex v;
113           myIsComputed = 
114                 Graphic2d_GraphicObject::MinMax(myXmin,myXmax,myYmin,myYmax);
115
116           if( myIsComputed ) switch (myTypeOfFrame) {
117             case Graphic2d_TOF_RECTANGULAR :
118               myXmin -= myFrameMargin1; myYmin -= myFrameMargin2;
119               myXmax += myFrameMargin1; myYmax += myFrameMargin2;
120               if( myFrame.IsNull() || (myFrame->Length() != 4) ) {
121                 myFrame = new Graphic2d_HArray1OfVertex(1,4);
122               }
123               v.SetCoord(myXmin,myYmin);
124               myFrame->SetValue(1,v);
125               v.SetCoord(myXmin,myYmax);
126               myFrame->SetValue(2,v);
127               v.SetCoord(myXmax,myYmax);
128               myFrame->SetValue(3,v);
129               v.SetCoord(myXmax,myYmin);
130               myFrame->SetValue(4,v);
131               break;
132             case Graphic2d_TOF_CIRCULAR :
133               {
134                 Standard_ShortReal precis,coef,radius;
135                 radius = Standard_ShortReal(Sqrt( (myXmax - myXmin) * (myXmax - myXmin) +
136                              (myYmax - myYmin) * (myYmax - myYmin) ) / 2.);
137                 radius += Standard_ShortReal(myFrameMargin1);
138                 Standard_ShortReal x0 = Standard_ShortReal((myXmax + myXmin)/2.);
139                 Standard_ShortReal y0 = Standard_ShortReal((myYmax + myYmin)/2.);
140                 myXmin = x0 - radius; 
141                 myYmin = y0 - radius; 
142                 myXmax = x0 + radius; 
143                 myYmax = y0 + radius; 
144                 Aspect_TypeOfDeflection type;
145                 aDrawer->DrawPrecision(precis,coef,type);
146                 if( type == Aspect_TOD_RELATIVE) {
147                   precis = radius * coef;
148                 }
149                 Standard_ShortReal value = Standard_ShortReal(Standard_PI/4.);
150                 if( radius > precis )
151                   value = Standard_ShortReal(Max( 0.0044 ,
152                                 Min (0.7854 , 2. * ACos(1.-precis/radius))));
153                 Standard_Integer n = Min(MAX_POINTS,
154                                 Standard_Integer(2.*Standard_PI/value)+2);
155                 Standard_ShortReal da = Standard_ShortReal(2.*Standard_PI/n);
156                 Standard_ShortReal cosin = Standard_ShortReal(Cos(da));
157                 Standard_ShortReal x1 = radius;
158                 Standard_ShortReal y1 = 0.;
159                 Standard_ShortReal x2 = radius * cosin;
160                 Standard_ShortReal y2 = Standard_ShortReal(radius * Sin(da));
161                 Standard_ShortReal x,y;
162
163                 if( myFrame.IsNull() || (myFrame->Length() != n) ) {
164                   myFrame = new Graphic2d_HArray1OfVertex(1,n);
165                 }
166                 v.SetCoord(x0+x1,y0+y1);
167                 myFrame->SetValue(1,v);
168                 v.SetCoord(x0+x2,y0+y2);
169                 myFrame->SetValue(2,v);
170                 for (Standard_Integer i=3; i<=n; i++) {
171                   x = Standard_ShortReal(2.*x2*cosin - x1); x1 = x2; x2 = x;
172                   y = Standard_ShortReal(2.*y2*cosin - y1); y1 = y2; y2 = y;
173                   v.SetCoord(x0+x,y0+y);
174                   myFrame->SetValue(i,v);
175                 }
176                 break;
177               }
178 #ifndef DEB
179             default:
180               break;
181 #endif
182           } else {
183             myXmin = myYmin = RealLast();
184             myXmax = myYmax = RealFirst();
185           }
186         }
187
188         if( !myFrame.IsNull() && (myXmax > myXmin) && (myYmax > myYmin) ) {
189           Standard_Real xmin,ymin,xmax,ymax;
190           Standard_Boolean transform = Standard_False; 
191           gp_GTrsf2d theTrsf;
192           xmin = myXmin; ymin = myYmin;
193           xmax = myXmax; ymax = myYmax;
194           if( IsTransformed() ) {
195             transform = Standard_True;
196             theTrsf = Transform();
197             TransformMinMax(theTrsf,xmin,xmax,ymin,ymax);
198           }
199
200           if( aDrawer->IsIn (
201                 Standard_ShortReal(xmin), Standard_ShortReal(xmax),
202                         Standard_ShortReal(ymin),Standard_ShortReal(ymax)) ) { 
203             if( IsHighlighted() ) {
204               aDrawer->SetOverride ( Standard_True );
205               aDrawer->SetOverrideColor ( myOverrideColor );
206             } else {
207               aDrawer->SetOffSet ( myOffSet );
208             }
209
210             aDrawer->SetHidingPolyAttrib (myHidingColorIndex,
211                 myFrameColorIndex,myFrameTypeIndex,myFrameWidthIndex);
212
213             Standard_Real x,y;
214             Standard_Integer i,l = myFrame->Length(),mode = l;
215             for( i=1 ; i<=l ; i++ ) {
216               (myFrame->Value(i)).Coord(x,y);
217               if( transform ) {
218                 theTrsf.Transforms(x,y);
219               }
220               if( i >= l ) mode = -1;
221 #ifdef TRACE
222 cout << " Graphic2d_HidingGraphicObject::Draw(" << x << "," << y << "," << mode << endl;
223 #endif
224               aDrawer->MapPolygonFromTo(Standard_ShortReal(x),
225                                         Standard_ShortReal(y),mode);
226               mode = 0;
227             }
228           }
229         }
230
231         Graphic2d_GraphicObject::Draw(aDrawer,Reset);
232 }
233
234 void Graphic2d_HidingGraphicObject::Draw(const Handle_Graphic2d_Drawer& aDrawer, const Handle_Graphic2d_Primitive& aPrim)
235 {
236  Graphic2d_GraphicObject::Draw(aDrawer,aPrim);
237 }
238
239 Standard_Boolean Graphic2d_HidingGraphicObject::Pick (const Standard_Real X, const Standard_Real Y, const Standard_Real aPrecision, const Handle(Graphic2d_Drawer)& aDrawer) {
240 Standard_Boolean status = Standard_False;
241
242 #ifndef G002
243   myPickedIndex = 0;
244 #endif
245   if( IsPickable() && (IsDisplayed() || IsHighlighted()) ) {
246
247     if( myIsComputed ) {
248       Standard_Real xx = X, yy = Y;
249       Standard_Real xmin = myXmin - aPrecision;
250       Standard_Real ymin = myYmin - aPrecision;
251       Standard_Real xmax = myXmax + aPrecision;
252       Standard_Real ymax = myYmax + aPrecision;
253       if( IsTransformed() ) {
254         gp_GTrsf2d aTrsf = (Transform ()).Inverted ();
255         aTrsf.Transforms (xx, yy);
256       }
257       if( (xx > xmin) && (xx < xmax) &&
258                 (yy > ymin) && (yy < ymax) ) {
259         switch (myTypeOfFrame) {
260           case Graphic2d_TOF_UNKNOWN :
261             status = (myFrame.IsNull()) ? Standard_False : Standard_True;
262             break;
263
264           case Graphic2d_TOF_RECTANGULAR :
265             status = Standard_True;
266             break;
267
268           case Graphic2d_TOF_CIRCULAR :
269             {
270               Standard_Real x = (xmin + xmax)/2.;
271               Standard_Real y = (ymin + ymax)/2.;
272               Standard_Real r = xmax - x;
273               if( Sqrt ((x - xx)*(x - xx) + (y - yy)*(y - yy)) < r )
274                         status = Standard_True;
275             }
276             break;
277         }
278       } 
279     }
280
281     if( !status )
282         status = Graphic2d_GraphicObject::Pick(X,Y,aPrecision,aDrawer);
283 #ifdef G002
284     else SetPickedIndex(0);
285 #endif
286   }
287
288   return status;
289 }
290
291 Standard_Boolean Graphic2d_HidingGraphicObject::FrameMinMax (Quantity_Length & Minx, Quantity_Length &Maxx, Quantity_Length & Miny, Quantity_Length & Maxy) const {
292 Standard_Boolean status = Standard_False;
293
294         if( !myFrame.IsNull() && (myXmax > myXmin) && (myYmax > myYmin) ) {
295           Standard_Real xmin,ymin,xmax,ymax;
296           xmin = myXmin; ymin = myYmin;
297           xmax = myXmax; ymax = myYmax;
298           if( IsTransformed() ) {
299             gp_GTrsf2d theTrsf = Transform();
300             TransformMinMax(theTrsf,xmin,xmax,ymin,ymax);
301           }
302           status = Standard_True;
303           Minx = xmin;
304           Miny = ymin;
305           Maxx = xmax;
306           Maxy = ymax;
307         } else {
308           Minx = RealFirst();
309           Miny = RealFirst();
310           Maxx = RealLast();
311           Maxy = RealLast();
312         }
313
314         return status;
315 }
316
317 Standard_Boolean Graphic2d_HidingGraphicObject::MinMax (Quantity_Length & Minx, Quantity_Length &Maxx, Quantity_Length & Miny, Quantity_Length & Maxy) const {
318
319         Standard_Boolean status = 
320                 Graphic2d_GraphicObject::MinMax(Minx,Maxx,Miny,Maxy);
321
322         Standard_Real xmin,ymin,xmax,ymax;
323         if( this->FrameMinMax(xmin,ymin,xmax,ymax) ) {
324           if( status ) {
325             Minx = Min(Minx,xmin);
326             Miny = Min(Miny,ymin);
327             Maxx = Min(Maxx,xmax);
328             Maxy = Min(Maxy,ymax);
329           } else {
330             status = Standard_True;
331             Minx = xmin;
332             Miny = ymin;
333             Maxx = xmax;
334             Maxy = ymax;
335           }
336         }
337
338         return status;
339 }
340
341 Graphic2d_TypeOfFrame Graphic2d_HidingGraphicObject::Frame( Graphic2d_Array1OfVertex& aFrame) const {
342 //Graphic2d_Vertex v;
343 //Standard_Real x,y;
344
345         aFrame = myFrame->Array1();
346         return myTypeOfFrame;
347 }
348
349 Standard_Integer Graphic2d_HidingGraphicObject::HidingColorIndex() const {
350
351         return myHidingColorIndex;
352 }
353
354 Standard_Integer Graphic2d_HidingGraphicObject::FrameColorIndex() const {
355
356         return myFrameColorIndex;
357 }
358
359 Standard_Integer Graphic2d_HidingGraphicObject::FrameTypeIndex() const {
360
361         return myFrameTypeIndex;
362 }
363
364 Standard_Integer Graphic2d_HidingGraphicObject::FrameWidthIndex() const {
365
366         return myFrameWidthIndex;
367 }
368
369 void Graphic2d_HidingGraphicObject::TransformMinMax (const gp_GTrsf2d& aTrsf,Standard_Real & Minx, Standard_Real &Maxx, Standard_Real & Miny, Standard_Real & Maxy) const {
370
371           Standard_Real xx,yy,xmin,ymin,xmax,ymax;
372           xmin = ymin = RealLast(); 
373           xmax = ymax = RealFirst();
374
375           xx = Minx; yy = Miny; 
376           aTrsf.Transforms (xx,yy);
377           xmin = Min(xmin,xx) ; 
378           ymin = Min(ymin,yy);
379           xmax = Max(xmax,xx) ; 
380           ymax = Max(ymax,yy);
381
382           xx = Minx; yy = Maxy; 
383           aTrsf.Transforms (xx,yy);
384           xmin = Min(xmin,xx) ; 
385           ymin = Min(ymin,yy);
386           xmax = Max(xmax,xx) ; 
387           ymax = Max(ymax,yy);
388
389           xx = Maxx; yy = Maxy; 
390           aTrsf.Transforms (xx,yy);
391           xmin = Min(xmin,xx) ; 
392           ymin = Min(ymin,yy);
393           xmax = Max(xmax,xx) ; 
394           ymax = Max(ymax,yy);
395
396           xx = Maxx; yy = Miny; 
397           aTrsf.Transforms (xx,yy);
398           Minx = Min(xmin,xx) ; 
399           Miny = Min(ymin,yy);
400           Maxx = Max(xmax,xx) ; 
401           Maxy = Max(ymax,yy);
402 }