0023663: Removing 2D viewer library
[occt.git] / src / Graphic2d / Graphic2d_Polyline.cxx
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
19 // WTO0001         GG_140596
20 //                      Calcul des min-max faux apres transformation.
21
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.
26
27 #define VERTEXMARKER 2
28 #define DEFAULTMARKERSIZE 3.0
29
30 #include <Graphic2d_Polyline.ixx>
31 #include <Graphic2d_Vertex.hxx>
32
33 #include <TShort_Array1OfShortReal.hxx>
34
35 Graphic2d_Polyline::Graphic2d_Polyline (
36         const Handle(Graphic2d_GraphicObject)& aGraphicObject,
37         const Graphic2d_Array1OfVertex& aListVertex)
38
39         : Graphic2d_Line (aGraphicObject),
40         myX (1, aListVertex.Length ()),
41         myY (1, aListVertex.Length ()) {
42
43         if (aListVertex.Length () < 2)
44                 Graphic2d_PolylineDefinitionError::Raise
45                         ("A polyline with a length less than 2 points.");
46
47 Standard_ShortReal X, Y;
48 Standard_Integer i, j;
49
50         // Recherche des MinMax
51 Standard_Integer Lower, Upper;
52         Lower   = aListVertex.Lower ();
53         Upper   = aListVertex.Upper ();
54
55         for (j=1, i=Lower; i<=Upper; i++, j++) {
56                 X       = Standard_ShortReal(aListVertex (i).X ());
57                 Y       = Standard_ShortReal(aListVertex (i).Y ());
58                 myX (j) = X;
59                 myY (j) = 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;
64         }
65
66     myNumOfElem = myX.Length()-1;
67     myNumOfVert = myX.Length();
68
69 }
70
71 Graphic2d_Polyline::Graphic2d_Polyline (
72         const Handle(Graphic2d_GraphicObject)& aGraphicObject,
73         const TColStd_Array1OfReal& aListX,
74         const TColStd_Array1OfReal& aListY)
75
76         : Graphic2d_Line (aGraphicObject),
77         myX (1, aListX.Length ()),
78         myY (1, aListY.Length ()) {
79
80         if (aListX.Length () < 2)
81                 Graphic2d_PolylineDefinitionError::Raise
82                         ("polyline : length < 2.");
83
84         if (aListX.Length () != aListY.Length ())
85                 Graphic2d_PolylineDefinitionError::Raise
86                         ("polyline : ListX and ListY have different lengths.");
87
88 Standard_ShortReal X, Y;
89 Standard_Integer i, j;
90
91         // Recherche des MinMax
92 Standard_Integer Lower, Upper;
93         Lower   = aListX.Lower ();
94         Upper   = aListX.Upper ();
95
96         for (j=1, i=Lower; i<=Upper; i++, j++) {
97                 X       = Standard_ShortReal(aListX (i));
98                 Y       = Standard_ShortReal(aListY (i));
99                 myX (j) = X;
100                 myY (j) = Y;
101                 if (X > myMaxX) myMaxX = X;
102                 if (X < myMinX) myMinX = X;
103                 if (Y > myMaxY) myMaxY = Y;
104                 if (Y < myMinY) myMinY = Y;
105         }
106     myNumOfElem = myX.Length()-1;
107     myNumOfVert = myX.Length();
108 }
109
110
111 Standard_Integer Graphic2d_Polyline::Length () const {
112    return myX.Length();
113 }
114
115 void Graphic2d_Polyline::Values( const Standard_Integer aRank,
116                                  Standard_Real &X,Standard_Real &Y ) const
117 {
118         if( aRank < 1 || aRank > myX.Length() )
119                 Standard_OutOfRange::Raise
120                         ("the point rank is out of bounds in the line");
121
122         X = myX(aRank);
123         Y = myY(aRank);
124
125 }
126
127 void Graphic2d_Polyline::Draw (const Handle(Graphic2d_Drawer)& aDrawer) {
128
129 Standard_Boolean IsIn = Standard_False;
130
131   if (! myGOPtr->IsTransformed ())
132     IsIn = aDrawer->IsIn (myMinX,myMaxX,myMinY,myMaxY);
133   else {
134     Standard_ShortReal minx, miny, maxx, maxy;
135     MinMax(minx,maxx,miny,maxy);
136     IsIn = aDrawer->IsIn (minx,maxx,miny,maxy);
137   }
138
139   if (IsIn) {
140     DrawLineAttrib (aDrawer);
141
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 ();
147       Standard_Real A, B;
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);
154           }
155       if ( myTypeOfPolygonFilling == Graphic2d_TOPF_EMPTY ) {
156           aDrawer->MapPolylineFromTo(Xpoint, Ypoint);
157       } else {
158           aDrawer->MapPolygonFromTo(Xpoint, Ypoint); 
159       }
160     } else {
161        if(myTypeOfPolygonFilling == Graphic2d_TOPF_EMPTY) {
162           aDrawer->MapPolylineFromTo(myX, myY);
163        } else {
164           aDrawer->MapPolygonFromTo(myX, myY); 
165        }
166     }
167   }
168 }
169
170 #ifdef G002
171 void Graphic2d_Polyline::DrawElement( const Handle(Graphic2d_Drawer)& aDrawer,
172                                       const Standard_Integer anIndex) {
173 Standard_Boolean IsIn = Standard_False;
174
175   if (! myGOPtr->IsTransformed ())
176     IsIn = aDrawer->IsIn (myMinX,myMaxX,myMinY,myMaxY);
177   else {
178     Standard_ShortReal minx, miny, maxx, maxy;
179     MinMax(minx,maxx,miny,maxy);
180     IsIn = aDrawer->IsIn (minx,maxx,miny,maxy);
181   }
182
183   Standard_Integer nbpoints = myX.Length ();
184   if (IsIn ) {
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 ();
190         Standard_Real A, B;
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);
201       } else {
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));
206       }
207       aDrawer->MapSegmentFromTo(X1,Y1,X2,Y2);
208     }
209   }
210 }
211
212 void Graphic2d_Polyline::DrawVertex( const Handle(Graphic2d_Drawer)& aDrawer,
213                                      const Standard_Integer anIndex) {
214  Standard_Boolean IsIn = Standard_False;
215
216  if (! myGOPtr->IsTransformed ())
217     IsIn = aDrawer->IsIn (myMinX,myMaxX,myMinY,myMaxY);
218  else {
219     Standard_ShortReal minx, miny, maxx, maxy;
220     MinMax(minx,maxx,miny,maxy);
221     IsIn = aDrawer->IsIn (minx,maxx,miny,maxy);
222  }
223
224  Standard_Integer nbpoints = myX.Length ();
225  if (IsIn ) {
226     if( anIndex > 0 && anIndex <= nbpoints ) { 
227       Standard_ShortReal X,Y;
228       DrawMarkerAttrib (aDrawer);
229       if (myGOPtr->IsTransformed ()) {
230         gp_GTrsf2d aTrsf = myGOPtr->Transform ();
231         Standard_Real A, B;
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);
237       } else {
238         X = Standard_ShortReal (myX(anIndex));
239         Y = Standard_ShortReal (myY(anIndex));
240       }
241       aDrawer->MapMarkerFromTo(VERTEXMARKER,X,Y,
242                         DEFAULTMARKERSIZE,DEFAULTMARKERSIZE,0.0);
243     }
244   }
245 }
246 #endif
247
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*/)
252 {
253
254   Standard_ShortReal SRX = X, SRY = Y;
255
256   Standard_Integer i;
257
258   Standard_Integer Lower, Upper;
259   Lower = myX.Lower ();
260   Upper = myX.Upper ();
261
262    if (IsInMinMax (X, Y, aPrecision)) {
263
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);
269         }
270
271 #ifdef G002
272    for ( i = Lower; i <= Upper; i++ ) {
273      if( Graphic2d_Primitive::IsOn ( SRX, SRY, myX (i), myY (i), aPrecision) ) {
274         SetPickedIndex(-i);
275         return Standard_True;
276      } else if( (i < Upper) && IsOn (SRX, SRY, myX (i), myY (i),
277                                 myX (i+1), myY (i+1), aPrecision) ) {
278            SetPickedIndex(i);
279            return Standard_True;
280      }
281     }
282 #else
283         for (i=Lower; i<Upper ; i++) {
284             if( IsOn (SRX, SRY, myX (i), myY (i), 
285                                 myX (i+1), myY (i+1), aPrecision) ) {
286               myPickedIndex = i;
287               return Standard_True;
288             }
289         }
290 #endif
291
292         if (myTypeOfPolygonFilling != Graphic2d_TOPF_EMPTY) {
293           if ( IsIn (SRX  , SRY ,  myX , myY , aPrecision) ) {
294 #ifdef G002
295               SetPickedIndex(0);
296 #else
297               myPickedIndex = 0;
298 #endif
299               return Standard_True;
300           }
301         }
302    }
303    return Standard_False;
304
305 }
306
307 void Graphic2d_Polyline::Save(Aspect_FStream& aFStream) const
308 {
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);
314 }
315
316 void Graphic2d_Polyline::Retrieve(Aspect_IFStream& anIFStream,
317                         const Handle(Graphic2d_GraphicObject)& aGraphicObject)
318 {
319         Standard_Integer numOfVert;
320         Standard_ShortReal X, Y;
321
322         *anIFStream >> numOfVert;
323         Graphic2d_Array1OfVertex listVertex(1, numOfVert);
324         for (Standard_Integer i=1; i<=numOfVert; i++)
325         {
326                 *anIFStream >> X >> Y;
327                 listVertex(i).SetCoord(X, Y);
328         }
329         Handle(Graphic2d_Polyline)
330                 thePLin = new Graphic2d_Polyline(aGraphicObject, listVertex);
331         ((Handle (Graphic2d_Line))thePLin)->Retrieve(anIFStream);
332 }
333