1 // Copyright (c) 1995-1999 Matra Datavision
2 // Copyright (c) 1999-2012 OPEN CASCADE SAS
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.
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.
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.
20 // Calcul des min-max faux apres transformation.
22 #define G002 //GG_140400 Use SetPickedIndex() method
23 // GG_050500 Add new DrawElement(), DrawVertex() methods
24 // Returns a negative picked index when the point
25 // is very closed to a polyline point.
27 #define VERTEXMARKER 2
28 #define DEFAULTMARKERSIZE 3.0
30 #include <Graphic2d_Polyline.ixx>
31 #include <Graphic2d_Vertex.hxx>
33 #include <TShort_Array1OfShortReal.hxx>
35 Graphic2d_Polyline::Graphic2d_Polyline (
36 const Handle(Graphic2d_GraphicObject)& aGraphicObject,
37 const Graphic2d_Array1OfVertex& aListVertex)
39 : Graphic2d_Line (aGraphicObject),
40 myX (1, aListVertex.Length ()),
41 myY (1, aListVertex.Length ()) {
43 if (aListVertex.Length () < 2)
44 Graphic2d_PolylineDefinitionError::Raise
45 ("A polyline with a length less than 2 points.");
47 Standard_ShortReal X, Y;
48 Standard_Integer i, j;
50 // Recherche des MinMax
51 Standard_Integer Lower, Upper;
52 Lower = aListVertex.Lower ();
53 Upper = aListVertex.Upper ();
55 for (j=1, i=Lower; i<=Upper; i++, j++) {
56 X = Standard_ShortReal(aListVertex (i).X ());
57 Y = Standard_ShortReal(aListVertex (i).Y ());
60 if (X > myMaxX) myMaxX = X;
61 if (X < myMinX) myMinX = X;
62 if (Y > myMaxY) myMaxY = Y;
63 if (Y < myMinY) myMinY = Y;
66 myNumOfElem = myX.Length()-1;
67 myNumOfVert = myX.Length();
71 Graphic2d_Polyline::Graphic2d_Polyline (
72 const Handle(Graphic2d_GraphicObject)& aGraphicObject,
73 const TColStd_Array1OfReal& aListX,
74 const TColStd_Array1OfReal& aListY)
76 : Graphic2d_Line (aGraphicObject),
77 myX (1, aListX.Length ()),
78 myY (1, aListY.Length ()) {
80 if (aListX.Length () < 2)
81 Graphic2d_PolylineDefinitionError::Raise
82 ("polyline : length < 2.");
84 if (aListX.Length () != aListY.Length ())
85 Graphic2d_PolylineDefinitionError::Raise
86 ("polyline : ListX and ListY have different lengths.");
88 Standard_ShortReal X, Y;
89 Standard_Integer i, j;
91 // Recherche des MinMax
92 Standard_Integer Lower, Upper;
93 Lower = aListX.Lower ();
94 Upper = aListX.Upper ();
96 for (j=1, i=Lower; i<=Upper; i++, j++) {
97 X = Standard_ShortReal(aListX (i));
98 Y = Standard_ShortReal(aListY (i));
101 if (X > myMaxX) myMaxX = X;
102 if (X < myMinX) myMinX = X;
103 if (Y > myMaxY) myMaxY = Y;
104 if (Y < myMinY) myMinY = Y;
106 myNumOfElem = myX.Length()-1;
107 myNumOfVert = myX.Length();
111 Standard_Integer Graphic2d_Polyline::Length () const {
115 void Graphic2d_Polyline::Values( const Standard_Integer aRank,
116 Standard_Real &X,Standard_Real &Y ) const
118 if( aRank < 1 || aRank > myX.Length() )
119 Standard_OutOfRange::Raise
120 ("the point rank is out of bounds in the line");
127 void Graphic2d_Polyline::Draw (const Handle(Graphic2d_Drawer)& aDrawer) {
129 Standard_Boolean IsIn = Standard_False;
131 if (! myGOPtr->IsTransformed ())
132 IsIn = aDrawer->IsIn (myMinX,myMaxX,myMinY,myMaxY);
134 Standard_ShortReal minx, miny, maxx, maxy;
135 MinMax(minx,maxx,miny,maxy);
136 IsIn = aDrawer->IsIn (minx,maxx,miny,maxy);
140 DrawLineAttrib (aDrawer);
142 if (myGOPtr->IsTransformed ()) {
143 Standard_Integer nbpoints = myX.Length ();
144 TShort_Array1OfShortReal Xpoint (1, nbpoints);
145 TShort_Array1OfShortReal Ypoint (1, nbpoints);
146 gp_GTrsf2d aTrsf = myGOPtr->Transform ();
148 for (Standard_Integer j=1; j<= nbpoints; j++) {
149 A = Standard_Real (myX(j));
150 B = Standard_Real (myY(j));
151 aTrsf.Transforms (A, B);
152 Xpoint(j) = Standard_ShortReal (A);
153 Ypoint(j) = Standard_ShortReal (B);
155 if ( myTypeOfPolygonFilling == Graphic2d_TOPF_EMPTY ) {
156 aDrawer->MapPolylineFromTo(Xpoint, Ypoint);
158 aDrawer->MapPolygonFromTo(Xpoint, Ypoint);
161 if(myTypeOfPolygonFilling == Graphic2d_TOPF_EMPTY) {
162 aDrawer->MapPolylineFromTo(myX, myY);
164 aDrawer->MapPolygonFromTo(myX, myY);
171 void Graphic2d_Polyline::DrawElement( const Handle(Graphic2d_Drawer)& aDrawer,
172 const Standard_Integer anIndex) {
173 Standard_Boolean IsIn = Standard_False;
175 if (! myGOPtr->IsTransformed ())
176 IsIn = aDrawer->IsIn (myMinX,myMaxX,myMinY,myMaxY);
178 Standard_ShortReal minx, miny, maxx, maxy;
179 MinMax(minx,maxx,miny,maxy);
180 IsIn = aDrawer->IsIn (minx,maxx,miny,maxy);
183 Standard_Integer nbpoints = myX.Length ();
185 if( anIndex > 0 && anIndex < nbpoints ) { //Draw edge
186 Standard_ShortReal X1,Y1,X2,Y2;
187 DrawLineAttrib (aDrawer);
188 if (myGOPtr->IsTransformed ()) {
189 gp_GTrsf2d aTrsf = myGOPtr->Transform ();
191 A = Standard_Real (myX(anIndex));
192 B = Standard_Real (myY(anIndex));
193 aTrsf.Transforms (A, B);
194 X1 = Standard_ShortReal (A);
195 Y1 = Standard_ShortReal (B);
196 A = Standard_Real (myX(anIndex+1));
197 B = Standard_Real (myY(anIndex+1));
198 aTrsf.Transforms (A, B);
199 X2 = Standard_ShortReal (A);
200 Y2 = Standard_ShortReal (B);
202 X1 = Standard_ShortReal (myX(anIndex));
203 Y1 = Standard_ShortReal (myY(anIndex));
204 X2 = Standard_ShortReal (myX(anIndex+1));
205 Y2 = Standard_ShortReal (myY(anIndex+1));
207 aDrawer->MapSegmentFromTo(X1,Y1,X2,Y2);
212 void Graphic2d_Polyline::DrawVertex( const Handle(Graphic2d_Drawer)& aDrawer,
213 const Standard_Integer anIndex) {
214 Standard_Boolean IsIn = Standard_False;
216 if (! myGOPtr->IsTransformed ())
217 IsIn = aDrawer->IsIn (myMinX,myMaxX,myMinY,myMaxY);
219 Standard_ShortReal minx, miny, maxx, maxy;
220 MinMax(minx,maxx,miny,maxy);
221 IsIn = aDrawer->IsIn (minx,maxx,miny,maxy);
224 Standard_Integer nbpoints = myX.Length ();
226 if( anIndex > 0 && anIndex <= nbpoints ) {
227 Standard_ShortReal X,Y;
228 DrawMarkerAttrib (aDrawer);
229 if (myGOPtr->IsTransformed ()) {
230 gp_GTrsf2d aTrsf = myGOPtr->Transform ();
232 A = Standard_Real (myX(anIndex));
233 B = Standard_Real (myY(anIndex));
234 aTrsf.Transforms (A, B);
235 X = Standard_ShortReal (A);
236 Y = Standard_ShortReal (B);
238 X = Standard_ShortReal (myX(anIndex));
239 Y = Standard_ShortReal (myY(anIndex));
241 aDrawer->MapMarkerFromTo(VERTEXMARKER,X,Y,
242 DEFAULTMARKERSIZE,DEFAULTMARKERSIZE,0.0);
248 Standard_Boolean Graphic2d_Polyline::Pick (const Standard_ShortReal X,
249 const Standard_ShortReal Y,
250 const Standard_ShortReal aPrecision,
251 const Handle(Graphic2d_Drawer)& /*aDrawer*/)
254 Standard_ShortReal SRX = X, SRY = Y;
258 Standard_Integer Lower, Upper;
259 Lower = myX.Lower ();
260 Upper = myX.Upper ();
262 if (IsInMinMax (X, Y, aPrecision)) {
264 if (myGOPtr->IsTransformed ()) {
265 gp_GTrsf2d aTrsf = (myGOPtr->Transform ()).Inverted ();
266 Standard_Real RX = Standard_Real (SRX), RY = Standard_Real (SRY);
267 aTrsf.Transforms (RX, RY);
268 SRX = Standard_ShortReal (RX); SRY = Standard_ShortReal (RY);
272 for ( i = Lower; i <= Upper; i++ ) {
273 if( Graphic2d_Primitive::IsOn ( SRX, SRY, myX (i), myY (i), aPrecision) ) {
275 return Standard_True;
276 } else if( (i < Upper) && IsOn (SRX, SRY, myX (i), myY (i),
277 myX (i+1), myY (i+1), aPrecision) ) {
279 return Standard_True;
283 for (i=Lower; i<Upper ; i++) {
284 if( IsOn (SRX, SRY, myX (i), myY (i),
285 myX (i+1), myY (i+1), aPrecision) ) {
287 return Standard_True;
292 if (myTypeOfPolygonFilling != Graphic2d_TOPF_EMPTY) {
293 if ( IsIn (SRX , SRY , myX , myY , aPrecision) ) {
299 return Standard_True;
303 return Standard_False;
307 void Graphic2d_Polyline::Save(Aspect_FStream& aFStream) const
309 *aFStream << "Graphic2d_Polyline" << endl;
310 *aFStream << myNumOfVert << endl;
311 for (Standard_Integer i=1; i<=myNumOfVert; i++)
312 *aFStream << myX(i) << ' ' << myY(i) << endl;
313 Graphic2d_Line::Save(aFStream);
316 void Graphic2d_Polyline::Retrieve(Aspect_IFStream& anIFStream,
317 const Handle(Graphic2d_GraphicObject)& aGraphicObject)
319 Standard_Integer numOfVert;
320 Standard_ShortReal X, Y;
322 *anIFStream >> numOfVert;
323 Graphic2d_Array1OfVertex listVertex(1, numOfVert);
324 for (Standard_Integer i=1; i<=numOfVert; i++)
326 *anIFStream >> X >> Y;
327 listVertex(i).SetCoord(X, Y);
329 Handle(Graphic2d_Polyline)
330 thePLin = new Graphic2d_Polyline(aGraphicObject, listVertex);
331 ((Handle (Graphic2d_Line))thePLin)->Retrieve(anIFStream);