0023510: Integration of test grid "vis" into the new testing system
[occt.git] / src / Graphic2d / Graphic2d_PolylineMarker.cxx
CommitLineData
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#include <Graphic2d_PolylineMarker.ixx>
20#include <Graphic2d_Vertex.hxx>
21
22#define G002 //TCL : Use SetPickedIndex() method
23// TCL : Add new DrawElement(), DrawVertex() methods
24
25#define VERTEXMARKER 2
26#define DEFAULTMARKERSIZE 3.0
27
28Graphic2d_PolylineMarker::Graphic2d_PolylineMarker (
29 const Handle(Graphic2d_GraphicObject)& aGraphicObject,
30 const Standard_Real aXPosition,
31 const Standard_Real aYPosition,
32 const Graphic2d_Array1OfVertex& aListVertex)
33
34 : Graphic2d_VectorialMarker (aGraphicObject, aXPosition, aYPosition),
35 myX (1, aListVertex.Length ()),
36 myY (1, aListVertex.Length ()) {
37
38 if (aListVertex.Length () < 2)
39 Graphic2d_PolylineDefinitionError::Raise
40 ("A polyline with a length less than 2 points.");
41
42 Standard_Integer i, j;
43
44 Standard_Integer Lower = aListVertex.Lower ();
45 Standard_Integer Upper = aListVertex.Upper ();
46
47 // Recopie de tableaux avec recherche du minmax
48 myMinX = Standard_ShortReal( aListVertex (Lower).X () );
49 myMinY = Standard_ShortReal( aListVertex (Lower).Y () );
50 myMaxX = Standard_ShortReal( aListVertex (Lower).X () );
51 myMaxY = Standard_ShortReal( aListVertex (Lower).Y () );
52
53 for (j=1, i=Lower; i<=Upper; i++, j++) {
54 myX (j) = Standard_ShortReal( aListVertex (i).X () );
55 myY (j) = Standard_ShortReal( aListVertex (i).Y () );
56 if (myX (j) < myMinX) myMinX = myX (j);
57 if (myY (j) < myMinY) myMinY = myY (j);
58 if (myX (j) > myMaxX) myMaxX = myX (j);
59 if (myY (j) > myMaxY) myMaxY = myY (j);
60 }
61
62#ifdef IMPLEMENTED
63 myMinX += aXPosition;
64 myMinY += aYPosition;
65 myMaxX += aXPosition;
66 myMaxY += aYPosition;
67#else
68#endif /* NOT IMPLEMENTED */
69
70 myNumOfElem = myX.Length()-1;
71 myNumOfVert = myX.Length();
72
73}
74
75Graphic2d_PolylineMarker::Graphic2d_PolylineMarker (
76 const Handle(Graphic2d_GraphicObject)& aGraphicObject,
77 const Standard_Real aXPosition, const Standard_Real aYPosition,
78 const TColStd_Array1OfReal& aListX,
79 const TColStd_Array1OfReal& aListY)
80
81 : Graphic2d_VectorialMarker (aGraphicObject,aXPosition,aYPosition),
82 myX (1, aListX.Length ()),
83 myY (1, aListY.Length ()) {
84
85 if (aListX.Length () < 2)
86 Graphic2d_PolylineDefinitionError::Raise
87 ("polyline : length < 2.");
88
89 if (aListX.Length () != aListY.Length ())
90 Graphic2d_PolylineDefinitionError::Raise
91 ("polyline : ListX and ListY have different lengths.");
92
93 Standard_Integer i, j;
94
95 Standard_Integer Lower = aListX.Lower ();
96 Standard_Integer Upper = aListX.Upper ();
97
98 // Recopie de tableaux avec recherche du minmax
99 myMinX = Standard_ShortReal( aListX (Lower) );
100 myMinY = Standard_ShortReal( aListY (Lower) );
101 myMaxX = Standard_ShortReal( aListX (Lower) );
102 myMaxY = Standard_ShortReal( aListY (Lower) );
103
104 for (j=1, i=Lower; i<=Upper; i++, j++) {
105 myX (j) = Standard_ShortReal( aListX (i) );
106 myY (j) = Standard_ShortReal( aListY (i) );
107 if (myX (j) < myMinX) myMinX = myX (j);
108 if (myY (j) < myMinY) myMinY = myY (j);
109 if (myX (j) > myMaxX) myMaxX = myX (j);
110 if (myY (j) > myMaxY) myMaxY = myY (j);
111 }
112
113#ifdef IMPLEMENTED
114 myMinX += aXPosition;
115 myMinY += aYPosition;
116 myMaxX += aXPosition;
117 myMaxY += aYPosition;
118#else
119#endif /* NOT IMPLEMENTED */
120
121 myNumOfElem = myX.Length()-1;
122 myNumOfVert = myX.Length();
123
124}
125
126Standard_Integer Graphic2d_PolylineMarker::Length () const {
127 return myX.Length();
128}
129
130void Graphic2d_PolylineMarker::Values( const Standard_Integer aRank,
131 Standard_Real &X,Standard_Real &Y ) const {
132
133 if ( aRank < 1 || aRank > myX.Length() )
134 Standard_OutOfRange::Raise( "The renk of point is out of bounds in the line");
135
136 X = myX(aRank);
137 Y = myY(aRank);
138
139}
140
141void Graphic2d_PolylineMarker::Draw (const Handle(Graphic2d_Drawer)& aDrawer) {
142
143 Standard_ShortReal xp,yp;
144 aDrawer->GetMapFromTo(Standard_ShortReal (XPosition ()),
145 Standard_ShortReal (YPosition ()),xp,yp);
146 DrawLineAttrib (aDrawer);
147 if(myTypeOfPolygonFilling == Graphic2d_TOPF_EMPTY) {
148 aDrawer->DrawPolyline(xp,yp,myX, myY);
149 } else {
150 aDrawer->DrawPolygon(xp,yp,myX, myY);
151 }
152}
153
154#ifdef G002
155
156void Graphic2d_PolylineMarker::DrawElement( const Handle(Graphic2d_Drawer)& aDrawer,
157 const Standard_Integer anIndex) {
158
159 if ( anIndex > 0 && anIndex < myX.Length() ) {
160 Standard_ShortReal xp, yp;
161 DrawLineAttrib( aDrawer );
162 aDrawer->GetMapFromTo(Standard_ShortReal(XPosition()),
163 Standard_ShortReal(YPosition()), xp, yp );
164 aDrawer->DrawSegment( xp + myX(anIndex), yp + myY(anIndex),
165 xp + myX(anIndex+1), yp + myY(anIndex+1));
166 }
167
168}
169
170void Graphic2d_PolylineMarker::DrawVertex( const Handle(Graphic2d_Drawer)& aDrawer,
171 const Standard_Integer anIndex) {
172
173 if( anIndex > 0 && anIndex <= myX.Length() ) {
174 Standard_ShortReal xp, yp;
175 aDrawer->GetMapFromTo(Standard_ShortReal(XPosition()),
176 Standard_ShortReal(YPosition()), xp, yp );
177 DrawMarkerAttrib( aDrawer );
178 aDrawer->DrawMarker( VERTEXMARKER, myX(anIndex) + xp, myY(anIndex) + yp,
179 DEFAULTMARKERSIZE, DEFAULTMARKERSIZE, 0.0 );
180 }
181}
182
183#endif
184
185Standard_Boolean Graphic2d_PolylineMarker::Pick( const Standard_ShortReal X,
186 const Standard_ShortReal Y,
187 const Standard_ShortReal aPrecision,
188 const Handle(Graphic2d_Drawer)& aDrawer) {
189
190 Standard_Integer i, Lower, Upper;
191
192 Standard_ShortReal SRX = X, SRY = Y;
193
194 Lower = myX.Lower();
195 Upper = myX.Upper();
196
197 TShort_Array1OfShortReal x(Lower,Upper);
198 TShort_Array1OfShortReal y(Lower,Upper);
199 for ( i = Lower; i <= Upper; i++ ) {
200 x(i) = aDrawer->ConvertMapToFrom( myX(i) );
201 y(i) = aDrawer->ConvertMapToFrom( myY(i) );
202 }
203
204 if ( myGOPtr->IsTransformed() ) {
205 gp_GTrsf2d aTrsf = ( myGOPtr->Transform() ).Inverted();
206 Standard_Real RX = Standard_Real( SRX ), RY = Standard_Real( SRY );
207 aTrsf.Transforms( RX, RY );
208 SRX = Standard_ShortReal( RX ); SRY = Standard_ShortReal( RY );
209 }
210
211#ifdef G002
212
213 for ( i = Lower; i <= Upper; i++ ) {
214 if ( Graphic2d_Primitive::IsOn ( SRX, SRY, myXPosition + x(i), myYPosition + y(i), aPrecision ) ) {
215 SetPickedIndex(-i);
216 return Standard_True;
217 } else if ( ( i < Upper ) && IsOn( SRX, SRY, myXPosition + x(i), myYPosition + y(i),
218 myXPosition + x(i+1), myYPosition + y(i+1), aPrecision ) ) {
219 SetPickedIndex(i);
220 return Standard_True;
221 }
222 }
223#else
224
225 for ( i = Lower; i < Upper; i++ ) {
226 if ( IsOn( SRX, SRY, myXPosition + x(i), myYPosition + y(i),
227 myXPosition + x(i+1), myYPosition + y(i+1), aPrecision ) ) {
228 myPickedIndex = i;
229 return Standard_True;
230 }
231 }
232#endif
233
234 if ( myTypeOfPolygonFilling != Graphic2d_TOPF_EMPTY )
235 if ( IsIn( SRX - myXPosition, SRY - myYPosition, x, y, aPrecision ) ) {
236#ifdef G002
237 SetPickedIndex(0);
238#else
239 myPickedIndex = 0;
240#endif
241 return Standard_True;
242 }
243
244 return Standard_False;
245
246}
247void Graphic2d_PolylineMarker::Save(Aspect_FStream& aFStream) const
248{
249 *aFStream << "Graphic2d_PolylineMarker" << endl;
250 *aFStream << myNumOfVert << endl;
251 *aFStream << myXPosition << ' ' << myYPosition << endl;
252 for (Standard_Integer i=1; i<=myNumOfVert; i++)
253 *aFStream << myX(i) << ' ' << myY(i) << endl;
254 Graphic2d_Line::Save(aFStream);
255}
256
257void Graphic2d_PolylineMarker::Retrieve(Aspect_IFStream& anIFStream,
258 const Handle(Graphic2d_GraphicObject)& aGraphicObject)
259{
260 Standard_Integer numOfVert;
261 Standard_ShortReal XPos, YPos, X, Y;
262
263 *anIFStream >> numOfVert;
264 *anIFStream >> XPos >> YPos;
265 Graphic2d_Array1OfVertex listVertex(1, numOfVert);
266 for (Standard_Integer i=1; i<=numOfVert; i++)
267 {
268 *anIFStream >> X >> Y;
269 listVertex(i).SetCoord(X, Y);
270 }
271 Handle(Graphic2d_PolylineMarker)
272 thePLinM = new Graphic2d_PolylineMarker(aGraphicObject, XPos, YPos, listVertex);
273 ((Handle (Graphic2d_Line))thePLinM)->Retrieve(anIFStream);
274}
275
276