0022792: Globally defined symbol PI conflicts with VTK definition (Intel compiler)
[occt.git] / src / Graphic2d / Graphic2d_CircleMarker.cxx
1 // Modified     23/02/98 : FMN ; Remplacement PI par Standard_PI
2
3 #define G002    //TCL: Add new DrawElement(), DrawVertex() methods
4
5 #define VERTEXMARKER 2
6 #define DEFAULTMARKERSIZE 3.0
7 #define MAXPOINTS 1023
8 #define IMPLEMENTED
9
10 #include <Graphic2d_CircleMarker.ixx>
11 #include <Quantity_PlaneAngle.hxx>
12 #include <TShort_Array1OfShortReal.hxx>
13
14 Graphic2d_CircleMarker::Graphic2d_CircleMarker (
15         const Handle(Graphic2d_GraphicObject)& aGraphicObject,
16         const Quantity_Length aXPosition,
17         const Quantity_Length aYPosition,
18         const Quantity_Length X,
19         const Quantity_Length Y,
20         const Quantity_Length Radius)
21
22         :Graphic2d_VectorialMarker (aGraphicObject, aXPosition, aYPosition),
23          myX (Standard_ShortReal (X)),
24          myY (Standard_ShortReal (Y)),
25          myRadius (Standard_ShortReal (Radius)),
26          myFirstAngle (0.0),
27          mySecondAngle (Standard_ShortReal (2. * M_PI)) {
28
29         if (myRadius <= RealEpsilon ())
30                 Graphic2d_CircleDefinitionError::Raise ("The radius = 0.");
31
32 #ifdef IMPLEMENTED
33         myMinX = myX + Standard_ShortReal (aXPosition) - myRadius;
34         myMinY = myY + Standard_ShortReal (aYPosition) - myRadius;
35         myMaxX = myX + Standard_ShortReal (aXPosition) + myRadius;
36         myMaxY = myY + Standard_ShortReal (aYPosition) + myRadius;
37 #else
38         myMinX = myX;
39         myMinY = myY;
40         myMaxX = myX;
41         myMaxY = myY;
42 #endif /* NOT IMPLEMENTED */
43    myisArc = Standard_False;
44    myNumOfElem = MAXPOINTS + 1;
45    myNumOfVert = 3;
46 }
47
48 Graphic2d_CircleMarker::Graphic2d_CircleMarker (
49         const Handle(Graphic2d_GraphicObject)& aGraphicObject,
50         const Quantity_Length aXPosition,
51         const Quantity_Length aYPosition,
52         const Quantity_Length X,
53         const Quantity_Length Y,
54         const Quantity_Length Radius,
55         const Quantity_PlaneAngle Alpha,
56         const Quantity_PlaneAngle Beta)
57
58         :Graphic2d_VectorialMarker (aGraphicObject, aXPosition, aYPosition),
59          myX (Standard_ShortReal (X)),
60          myY (Standard_ShortReal (Y)),
61          myRadius (Standard_ShortReal (Radius)),
62          myFirstAngle (Standard_ShortReal (Alpha)),
63          mySecondAngle (Standard_ShortReal (Beta)) {
64
65         if (myRadius <= RealEpsilon ())
66                 Graphic2d_CircleDefinitionError::Raise ("The radius = 0.");
67
68 #ifdef IMPLEMENTED
69         myMinX = myX + Standard_ShortReal (aXPosition) - myRadius;
70         myMinY = myY + Standard_ShortReal (aYPosition) - myRadius;
71         myMaxX = myX + Standard_ShortReal (aXPosition) + myRadius;
72         myMaxY = myY + Standard_ShortReal (aYPosition) + myRadius;
73 #else
74         myMinX = myX;
75         myMinY = myY;
76         myMaxX = myX;
77         myMaxY = myY;
78 #endif /* NOT IMPLEMENTED */
79     myisArc = Standard_True;
80     myNumOfElem = MAXPOINTS + 1;
81     myNumOfVert = 3;
82 }
83
84 void Graphic2d_CircleMarker::Draw (const Handle(Graphic2d_Drawer)& aDrawer) {
85
86     DrawLineAttrib(aDrawer);
87
88     Standard_ShortReal xp,yp;
89     aDrawer->GetMapFromTo(Standard_ShortReal (XPosition ()),
90                           Standard_ShortReal (YPosition ()),xp,yp);
91     xp += myX;
92     yp += myY;
93
94     if (myTypeOfPolygonFilling == Graphic2d_TOPF_EMPTY) {
95       aDrawer->DrawArc(xp,yp,myRadius,myFirstAngle,mySecondAngle);
96     } else {
97       aDrawer->DrawPolyArc(xp,yp,myRadius,myFirstAngle,mySecondAngle);
98     }
99
100 }
101
102 #ifdef G002
103
104 void Graphic2d_CircleMarker::DrawElement( const Handle(Graphic2d_Drawer)& aDrawer,
105                                           const Standard_Integer anIndex ) {
106    
107     
108  if ( anIndex > 0 && anIndex <= MAXPOINTS + 1 ) { 
109      DrawLineAttrib(aDrawer);
110      Standard_ShortReal rd = aDrawer->ConvertMapToFrom(myRadius);
111      Standard_ShortReal x1 = aDrawer->ConvertMapToFrom(myX) + myXPosition;
112      Standard_ShortReal y1 = aDrawer->ConvertMapToFrom(myY) + myYPosition;
113
114      Standard_ShortReal teta = Abs( mySecondAngle - myFirstAngle ) / MAXPOINTS;
115      Standard_ShortReal 
116                x2 = Standard_ShortReal(x1 + rd * Cos(myFirstAngle + teta*(anIndex-1))),
117                y2 = Standard_ShortReal(y1 + rd * Sin(myFirstAngle + teta*(anIndex-1)));
118     
119      aDrawer->MapSegmentFromTo( x1, y1, x2, y2 );
120  }
121
122 }
123
124 void Graphic2d_CircleMarker::DrawVertex( const Handle(Graphic2d_Drawer)& aDrawer,
125                                          const Standard_Integer anIndex) {
126
127   if ( anIndex > 0 && anIndex < 4 ) {
128      DrawMarkerAttrib( aDrawer );
129      Standard_ShortReal x,y;
130      aDrawer->GetMapFromTo(Standard_ShortReal( XPosition() ),
131                           Standard_ShortReal( YPosition() ), x, y );
132      x += myX;
133      y += myY;
134      
135      Standard_Real alpha = ( anIndex == 2 ) ? myFirstAngle : mySecondAngle;
136        x += Standard_ShortReal( myRadius * Cos( alpha ) );
137        y += Standard_ShortReal( myRadius * Sin( alpha ) );
138      aDrawer->DrawMarker( VERTEXMARKER, x, y, DEFAULTMARKERSIZE,DEFAULTMARKERSIZE, 0.0 );
139   }
140 }
141
142 #endif
143
144 Standard_Boolean Graphic2d_CircleMarker::Pick (const Standard_ShortReal X,
145                                          const Standard_ShortReal Y,
146                                          const Standard_ShortReal aPrecision,
147                                          const Handle(Graphic2d_Drawer)& aDrawer) {
148
149      Standard_ShortReal SRX = X, SRY = Y;
150      Standard_ShortReal rd = aDrawer->ConvertMapToFrom(myRadius);
151      Standard_ShortReal x =  aDrawer->ConvertMapToFrom(myX) + myXPosition;
152      Standard_ShortReal y =  aDrawer->ConvertMapToFrom(myY) + myYPosition;
153      Standard_Boolean found = Standard_False;
154
155      if (myGOPtr->IsTransformed ()) {
156        gp_GTrsf2d aTrsf = (myGOPtr->Transform ()).Inverted ();
157        Standard_Real RX = Standard_Real (SRX), RY = Standard_Real (SRY);
158            aTrsf.Transforms (RX, RY);
159            SRX = Standard_ShortReal (RX); SRY = Standard_ShortReal (RY);
160      }
161
162  #ifdef G002
163
164      if ( Graphic2d_Primitive::IsOn( SRX, SRY, x, y, aPrecision) ) {
165         SetPickedIndex(-1);
166         return Standard_True;
167      } else {
168        if ( myisArc ) {
169          Standard_ShortReal x1 = Standard_ShortReal(rd * Cos( myFirstAngle )  + x),
170                             y1 = Standard_ShortReal(rd * Sin( myFirstAngle )  + y),
171                             x2 = Standard_ShortReal(rd * Cos( mySecondAngle ) + x),
172                             y2 = Standard_ShortReal(rd * Sin( mySecondAngle ) + y);
173
174         if ( Graphic2d_Primitive::IsOn( SRX, SRY, x1, y1, aPrecision) ) {
175             SetPickedIndex(-2);
176             return Standard_True;
177         } else if ( Graphic2d_Primitive::IsOn( SRX, SRY, x2, y2, aPrecision) ) {
178             SetPickedIndex(-3);
179             return Standard_True;
180         }
181        } // end if is Arc
182        
183        TShort_Array1OfShortReal Xpoint( 1, MAXPOINTS + 1 ); 
184        TShort_Array1OfShortReal Ypoint( 1, MAXPOINTS + 1 ); 
185           
186        Standard_ShortReal teta = Abs( mySecondAngle - myFirstAngle ) / MAXPOINTS;
187     
188        for ( Standard_Integer i = 1; i <= MAXPOINTS + 1; i++ ) {
189               Xpoint(i) = Standard_ShortReal(x + rd * Cos( myFirstAngle + teta*(i-1) ));
190               Ypoint(i) = Standard_ShortReal(y + rd * Sin( myFirstAngle + teta*(i-1) ));
191           if ( Graphic2d_Primitive::IsOn( SRX, SRY, Xpoint(i), Ypoint(i), aPrecision) ) {
192             SetPickedIndex(i);
193             return Standard_True;
194           }
195        }
196
197        if ( myTypeOfPolygonFilling != Graphic2d_TOPF_EMPTY )
198               found = Abs (Sqrt ((x - SRX)*(x - SRX) + (y - SRY)*(y - SRY))) < rd;
199        if ( !found )
200               found  = Abs (Sqrt ((x - SRX)*(x - SRX) + (y - SRY)*(y - SRY)) - rd) < aPrecision;
201        if ( found ) {
202            SetPickedIndex( 0 );
203            return Standard_True;
204        }                                                      
205      }
206  #else   
207
208      if ( myTypeOfPolygonFilling != Graphic2d_TOPF_EMPTY ) 
209            found = Abs (Sqrt ((x - SRX)*(x - SRX) + (y - SRY)*(y - SRY))) < rd;
210      if ( !found ) 
211              found  = Abs (Sqrt ((x - SRX)*(x - SRX) + (y - SRY)*(y - SRY)) - rd) < aPrecision;
212      if ( found ) return Standard_True;
213      
214  #endif
215
216   return Standard_False;
217
218 }
219
220 void Graphic2d_CircleMarker::Center( Quantity_Length& X,Quantity_Length& Y ) const {
221      
222     X = Quantity_Length( myX );
223     Y = Quantity_Length( myY );
224 }
225
226 Quantity_Length Graphic2d_CircleMarker::Radius() const {
227
228     return Quantity_Length( myRadius );
229 }
230
231 Quantity_PlaneAngle Graphic2d_CircleMarker::FirstAngle() const {
232  
233     return Quantity_PlaneAngle( myFirstAngle );
234 }
235
236 Quantity_PlaneAngle Graphic2d_CircleMarker::SecondAngle() const {
237
238     return Quantity_PlaneAngle( mySecondAngle );
239 }
240
241 void Graphic2d_CircleMarker::Save(Aspect_FStream& aFStream) const
242 {
243         *aFStream << "Graphic2d_CircleMarker" << endl;
244         *aFStream << myXPosition << ' ' << myYPosition << endl;
245         *aFStream << myX << ' ' << myY << endl;
246         *aFStream << myRadius << endl;
247         *aFStream << myisArc << endl;
248         if (myisArc)
249                 *aFStream << myFirstAngle << ' ' << mySecondAngle << endl;
250         Graphic2d_Line::Save(aFStream);
251 }
252
253 void Graphic2d_CircleMarker::Retrieve(Aspect_IFStream& anIFStream,
254                         const Handle(Graphic2d_GraphicObject)& aGraphicObject)
255 {
256         Quantity_Length XPos, YPos, X, Y, Rad;
257         Quantity_PlaneAngle Ang1, Ang2;
258         int isArc;
259         Handle(Graphic2d_CircleMarker) theCirM;
260
261         *anIFStream >> XPos >> YPos;
262         *anIFStream >> X >> Y;
263         *anIFStream >> Rad;
264         *anIFStream >> isArc;
265         if (isArc)
266         {
267                 *anIFStream >> Ang1 >> Ang2;
268                 theCirM = new Graphic2d_CircleMarker(aGraphicObject, XPos, YPos, X, Y, Rad, Ang1, Ang2);
269         }
270         else
271                 theCirM = new Graphic2d_CircleMarker(aGraphicObject, XPos, YPos, X, Y, Rad);
272         ((Handle (Graphic2d_Line))theCirM)->Retrieve(anIFStream);
273 }
274