b311480e |
1 | // Copyright (c) 1995-1999 Matra Datavision |
2 | // Copyright (c) 1999-2012 OPEN CASCADE SAS |
3 | // |
4 | // The content of this file is subject to the Open CASCADE Technology Public |
5 | // License Version 6.5 (the "License"). You may not use the content of this file |
6 | // except in compliance with the License. Please obtain a copy of the License |
7 | // at http://www.opencascade.org and read it completely before using this file. |
8 | // |
9 | // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its |
10 | // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France. |
11 | // |
12 | // The Original Code and all software distributed under the License is |
13 | // distributed on an "AS IS" basis, without warranty of any kind, and the |
14 | // Initial Developer hereby disclaims all such warranties, including without |
15 | // limitation, any warranties of merchantability, fitness for a particular |
16 | // purpose or non-infringement. Please see the License for the specific terms |
17 | // and conditions governing the rights and limitations under the License. |
18 | |
7fd59977 |
19 | // Modified 23/02/98 : FMN ; Remplacement PI par Standard_PI |
20 | |
21 | #define G002 //TCL: Add new DrawElement(), DrawVertex() methods |
22 | |
23 | #define VERTEXMARKER 2 |
24 | #define DEFAULTMARKERSIZE 3.0 |
25 | #define MAXPOINTS 1023 |
26 | #define IMPLEMENTED |
27 | |
28 | #include <Graphic2d_CircleMarker.ixx> |
29 | #include <Quantity_PlaneAngle.hxx> |
30 | #include <TShort_Array1OfShortReal.hxx> |
31 | |
32 | Graphic2d_CircleMarker::Graphic2d_CircleMarker ( |
33 | const Handle(Graphic2d_GraphicObject)& aGraphicObject, |
34 | const Quantity_Length aXPosition, |
35 | const Quantity_Length aYPosition, |
36 | const Quantity_Length X, |
37 | const Quantity_Length Y, |
38 | const Quantity_Length Radius) |
39 | |
40 | :Graphic2d_VectorialMarker (aGraphicObject, aXPosition, aYPosition), |
41 | myX (Standard_ShortReal (X)), |
42 | myY (Standard_ShortReal (Y)), |
43 | myRadius (Standard_ShortReal (Radius)), |
44 | myFirstAngle (0.0), |
c6541a0c |
45 | mySecondAngle (Standard_ShortReal (2. * M_PI)) { |
7fd59977 |
46 | |
47 | if (myRadius <= RealEpsilon ()) |
48 | Graphic2d_CircleDefinitionError::Raise ("The radius = 0."); |
49 | |
50 | #ifdef IMPLEMENTED |
51 | myMinX = myX + Standard_ShortReal (aXPosition) - myRadius; |
52 | myMinY = myY + Standard_ShortReal (aYPosition) - myRadius; |
53 | myMaxX = myX + Standard_ShortReal (aXPosition) + myRadius; |
54 | myMaxY = myY + Standard_ShortReal (aYPosition) + myRadius; |
55 | #else |
56 | myMinX = myX; |
57 | myMinY = myY; |
58 | myMaxX = myX; |
59 | myMaxY = myY; |
60 | #endif /* NOT IMPLEMENTED */ |
61 | myisArc = Standard_False; |
62 | myNumOfElem = MAXPOINTS + 1; |
63 | myNumOfVert = 3; |
64 | } |
65 | |
66 | Graphic2d_CircleMarker::Graphic2d_CircleMarker ( |
67 | const Handle(Graphic2d_GraphicObject)& aGraphicObject, |
68 | const Quantity_Length aXPosition, |
69 | const Quantity_Length aYPosition, |
70 | const Quantity_Length X, |
71 | const Quantity_Length Y, |
72 | const Quantity_Length Radius, |
73 | const Quantity_PlaneAngle Alpha, |
74 | const Quantity_PlaneAngle Beta) |
75 | |
76 | :Graphic2d_VectorialMarker (aGraphicObject, aXPosition, aYPosition), |
77 | myX (Standard_ShortReal (X)), |
78 | myY (Standard_ShortReal (Y)), |
79 | myRadius (Standard_ShortReal (Radius)), |
80 | myFirstAngle (Standard_ShortReal (Alpha)), |
81 | mySecondAngle (Standard_ShortReal (Beta)) { |
82 | |
83 | if (myRadius <= RealEpsilon ()) |
84 | Graphic2d_CircleDefinitionError::Raise ("The radius = 0."); |
85 | |
86 | #ifdef IMPLEMENTED |
87 | myMinX = myX + Standard_ShortReal (aXPosition) - myRadius; |
88 | myMinY = myY + Standard_ShortReal (aYPosition) - myRadius; |
89 | myMaxX = myX + Standard_ShortReal (aXPosition) + myRadius; |
90 | myMaxY = myY + Standard_ShortReal (aYPosition) + myRadius; |
91 | #else |
92 | myMinX = myX; |
93 | myMinY = myY; |
94 | myMaxX = myX; |
95 | myMaxY = myY; |
96 | #endif /* NOT IMPLEMENTED */ |
97 | myisArc = Standard_True; |
98 | myNumOfElem = MAXPOINTS + 1; |
99 | myNumOfVert = 3; |
100 | } |
101 | |
102 | void Graphic2d_CircleMarker::Draw (const Handle(Graphic2d_Drawer)& aDrawer) { |
103 | |
104 | DrawLineAttrib(aDrawer); |
105 | |
106 | Standard_ShortReal xp,yp; |
107 | aDrawer->GetMapFromTo(Standard_ShortReal (XPosition ()), |
108 | Standard_ShortReal (YPosition ()),xp,yp); |
109 | xp += myX; |
110 | yp += myY; |
111 | |
112 | if (myTypeOfPolygonFilling == Graphic2d_TOPF_EMPTY) { |
113 | aDrawer->DrawArc(xp,yp,myRadius,myFirstAngle,mySecondAngle); |
114 | } else { |
115 | aDrawer->DrawPolyArc(xp,yp,myRadius,myFirstAngle,mySecondAngle); |
116 | } |
117 | |
118 | } |
119 | |
120 | #ifdef G002 |
121 | |
122 | void Graphic2d_CircleMarker::DrawElement( const Handle(Graphic2d_Drawer)& aDrawer, |
123 | const Standard_Integer anIndex ) { |
124 | |
125 | |
126 | if ( anIndex > 0 && anIndex <= MAXPOINTS + 1 ) { |
127 | DrawLineAttrib(aDrawer); |
128 | Standard_ShortReal rd = aDrawer->ConvertMapToFrom(myRadius); |
129 | Standard_ShortReal x1 = aDrawer->ConvertMapToFrom(myX) + myXPosition; |
130 | Standard_ShortReal y1 = aDrawer->ConvertMapToFrom(myY) + myYPosition; |
131 | |
132 | Standard_ShortReal teta = Abs( mySecondAngle - myFirstAngle ) / MAXPOINTS; |
133 | Standard_ShortReal |
134 | x2 = Standard_ShortReal(x1 + rd * Cos(myFirstAngle + teta*(anIndex-1))), |
135 | y2 = Standard_ShortReal(y1 + rd * Sin(myFirstAngle + teta*(anIndex-1))); |
136 | |
137 | aDrawer->MapSegmentFromTo( x1, y1, x2, y2 ); |
138 | } |
139 | |
140 | } |
141 | |
142 | void Graphic2d_CircleMarker::DrawVertex( const Handle(Graphic2d_Drawer)& aDrawer, |
143 | const Standard_Integer anIndex) { |
144 | |
145 | if ( anIndex > 0 && anIndex < 4 ) { |
146 | DrawMarkerAttrib( aDrawer ); |
147 | Standard_ShortReal x,y; |
148 | aDrawer->GetMapFromTo(Standard_ShortReal( XPosition() ), |
149 | Standard_ShortReal( YPosition() ), x, y ); |
150 | x += myX; |
151 | y += myY; |
152 | |
153 | Standard_Real alpha = ( anIndex == 2 ) ? myFirstAngle : mySecondAngle; |
154 | x += Standard_ShortReal( myRadius * Cos( alpha ) ); |
155 | y += Standard_ShortReal( myRadius * Sin( alpha ) ); |
156 | aDrawer->DrawMarker( VERTEXMARKER, x, y, DEFAULTMARKERSIZE,DEFAULTMARKERSIZE, 0.0 ); |
157 | } |
158 | } |
159 | |
160 | #endif |
161 | |
162 | Standard_Boolean Graphic2d_CircleMarker::Pick (const Standard_ShortReal X, |
163 | const Standard_ShortReal Y, |
164 | const Standard_ShortReal aPrecision, |
165 | const Handle(Graphic2d_Drawer)& aDrawer) { |
166 | |
167 | Standard_ShortReal SRX = X, SRY = Y; |
168 | Standard_ShortReal rd = aDrawer->ConvertMapToFrom(myRadius); |
169 | Standard_ShortReal x = aDrawer->ConvertMapToFrom(myX) + myXPosition; |
170 | Standard_ShortReal y = aDrawer->ConvertMapToFrom(myY) + myYPosition; |
171 | Standard_Boolean found = Standard_False; |
172 | |
173 | if (myGOPtr->IsTransformed ()) { |
174 | gp_GTrsf2d aTrsf = (myGOPtr->Transform ()).Inverted (); |
175 | Standard_Real RX = Standard_Real (SRX), RY = Standard_Real (SRY); |
176 | aTrsf.Transforms (RX, RY); |
177 | SRX = Standard_ShortReal (RX); SRY = Standard_ShortReal (RY); |
178 | } |
179 | |
180 | #ifdef G002 |
181 | |
182 | if ( Graphic2d_Primitive::IsOn( SRX, SRY, x, y, aPrecision) ) { |
183 | SetPickedIndex(-1); |
184 | return Standard_True; |
185 | } else { |
186 | if ( myisArc ) { |
187 | Standard_ShortReal x1 = Standard_ShortReal(rd * Cos( myFirstAngle ) + x), |
188 | y1 = Standard_ShortReal(rd * Sin( myFirstAngle ) + y), |
189 | x2 = Standard_ShortReal(rd * Cos( mySecondAngle ) + x), |
190 | y2 = Standard_ShortReal(rd * Sin( mySecondAngle ) + y); |
191 | |
192 | if ( Graphic2d_Primitive::IsOn( SRX, SRY, x1, y1, aPrecision) ) { |
193 | SetPickedIndex(-2); |
194 | return Standard_True; |
195 | } else if ( Graphic2d_Primitive::IsOn( SRX, SRY, x2, y2, aPrecision) ) { |
196 | SetPickedIndex(-3); |
197 | return Standard_True; |
198 | } |
199 | } // end if is Arc |
200 | |
201 | TShort_Array1OfShortReal Xpoint( 1, MAXPOINTS + 1 ); |
202 | TShort_Array1OfShortReal Ypoint( 1, MAXPOINTS + 1 ); |
203 | |
204 | Standard_ShortReal teta = Abs( mySecondAngle - myFirstAngle ) / MAXPOINTS; |
205 | |
206 | for ( Standard_Integer i = 1; i <= MAXPOINTS + 1; i++ ) { |
207 | Xpoint(i) = Standard_ShortReal(x + rd * Cos( myFirstAngle + teta*(i-1) )); |
208 | Ypoint(i) = Standard_ShortReal(y + rd * Sin( myFirstAngle + teta*(i-1) )); |
209 | if ( Graphic2d_Primitive::IsOn( SRX, SRY, Xpoint(i), Ypoint(i), aPrecision) ) { |
210 | SetPickedIndex(i); |
211 | return Standard_True; |
212 | } |
213 | } |
214 | |
215 | if ( myTypeOfPolygonFilling != Graphic2d_TOPF_EMPTY ) |
216 | found = Abs (Sqrt ((x - SRX)*(x - SRX) + (y - SRY)*(y - SRY))) < rd; |
217 | if ( !found ) |
218 | found = Abs (Sqrt ((x - SRX)*(x - SRX) + (y - SRY)*(y - SRY)) - rd) < aPrecision; |
219 | if ( found ) { |
220 | SetPickedIndex( 0 ); |
221 | return Standard_True; |
222 | } |
223 | } |
224 | #else |
225 | |
226 | if ( myTypeOfPolygonFilling != Graphic2d_TOPF_EMPTY ) |
227 | found = Abs (Sqrt ((x - SRX)*(x - SRX) + (y - SRY)*(y - SRY))) < rd; |
228 | if ( !found ) |
229 | found = Abs (Sqrt ((x - SRX)*(x - SRX) + (y - SRY)*(y - SRY)) - rd) < aPrecision; |
230 | if ( found ) return Standard_True; |
231 | |
232 | #endif |
233 | |
234 | return Standard_False; |
235 | |
236 | } |
237 | |
238 | void Graphic2d_CircleMarker::Center( Quantity_Length& X,Quantity_Length& Y ) const { |
239 | |
240 | X = Quantity_Length( myX ); |
241 | Y = Quantity_Length( myY ); |
242 | } |
243 | |
244 | Quantity_Length Graphic2d_CircleMarker::Radius() const { |
245 | |
246 | return Quantity_Length( myRadius ); |
247 | } |
248 | |
249 | Quantity_PlaneAngle Graphic2d_CircleMarker::FirstAngle() const { |
250 | |
251 | return Quantity_PlaneAngle( myFirstAngle ); |
252 | } |
253 | |
254 | Quantity_PlaneAngle Graphic2d_CircleMarker::SecondAngle() const { |
255 | |
256 | return Quantity_PlaneAngle( mySecondAngle ); |
257 | } |
258 | |
259 | void Graphic2d_CircleMarker::Save(Aspect_FStream& aFStream) const |
260 | { |
261 | *aFStream << "Graphic2d_CircleMarker" << endl; |
262 | *aFStream << myXPosition << ' ' << myYPosition << endl; |
263 | *aFStream << myX << ' ' << myY << endl; |
264 | *aFStream << myRadius << endl; |
265 | *aFStream << myisArc << endl; |
266 | if (myisArc) |
267 | *aFStream << myFirstAngle << ' ' << mySecondAngle << endl; |
268 | Graphic2d_Line::Save(aFStream); |
269 | } |
270 | |
271 | void Graphic2d_CircleMarker::Retrieve(Aspect_IFStream& anIFStream, |
272 | const Handle(Graphic2d_GraphicObject)& aGraphicObject) |
273 | { |
274 | Quantity_Length XPos, YPos, X, Y, Rad; |
275 | Quantity_PlaneAngle Ang1, Ang2; |
276 | int isArc; |
277 | Handle(Graphic2d_CircleMarker) theCirM; |
278 | |
279 | *anIFStream >> XPos >> YPos; |
280 | *anIFStream >> X >> Y; |
281 | *anIFStream >> Rad; |
282 | *anIFStream >> isArc; |
283 | if (isArc) |
284 | { |
285 | *anIFStream >> Ang1 >> Ang2; |
286 | theCirM = new Graphic2d_CircleMarker(aGraphicObject, XPos, YPos, X, Y, Rad, Ang1, Ang2); |
287 | } |
288 | else |
289 | theCirM = new Graphic2d_CircleMarker(aGraphicObject, XPos, YPos, X, Y, Rad); |
290 | ((Handle (Graphic2d_Line))theCirM)->Retrieve(anIFStream); |
291 | } |
292 | |