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 | // 10/11/98 : GG ; Protection sur methode IsOn() lorsque |
22 | // les points sont confondus. |
23 | |
24 | #define G002 //GG_100500 |
25 | // Change IsOn method with a best computation on short segments. |
7fd59977 |
26 | |
27 | #include <Graphic2d_Line.ixx> |
28 | |
29 | Graphic2d_Line::Graphic2d_Line (const Handle(Graphic2d_GraphicObject)& aGraphicObject) |
30 | :Graphic2d_Primitive (aGraphicObject), |
31 | myTypeOfPolygonFilling (Graphic2d_TOPF_EMPTY), |
32 | myDrawEdge (Standard_True), |
33 | myWidthIndex (0), |
34 | myTypeIndex (0), |
35 | myPatternIndex (0), |
36 | myInteriorColorIndex (1) { |
37 | |
38 | SetFamily(Graphic2d_TOP_LINE); |
39 | } |
40 | |
41 | void Graphic2d_Line::SetWidthIndex (const Standard_Integer anIndex) { |
42 | |
43 | myWidthIndex = anIndex; |
44 | ResetIndex (); |
45 | |
46 | } |
47 | |
48 | void Graphic2d_Line::SetTypeIndex (const Standard_Integer anIndex) { |
49 | |
50 | myTypeIndex = anIndex; |
51 | ResetIndex (); |
52 | |
53 | } |
54 | |
55 | void Graphic2d_Line::SetInteriorColorIndex (const Standard_Integer anIndex) { |
56 | |
57 | myInteriorColorIndex = anIndex; |
58 | ResetIndex (); |
59 | |
60 | } |
61 | |
62 | void Graphic2d_Line::SetDrawEdge (const Standard_Boolean aDraw) { |
63 | |
64 | myDrawEdge = aDraw; |
65 | ResetIndex (); |
66 | |
67 | } |
68 | |
69 | void Graphic2d_Line::SetInteriorPattern (const Standard_Integer anIndex) { |
70 | |
71 | myPatternIndex = anIndex; |
72 | ResetIndex (); |
73 | |
74 | } |
75 | |
76 | void Graphic2d_Line::SetTypeOfPolygonFilling (const Graphic2d_TypeOfPolygonFilling aType) { |
77 | |
78 | myTypeOfPolygonFilling = aType; |
79 | ResetIndex (); |
80 | |
81 | } |
82 | |
83 | Standard_Integer Graphic2d_Line::WidthIndex () const { |
84 | |
85 | return myWidthIndex; |
86 | |
87 | } |
88 | |
89 | Standard_Integer Graphic2d_Line::InteriorColorIndex () const { |
90 | |
91 | return myInteriorColorIndex; |
92 | |
93 | } |
94 | |
95 | Standard_Integer Graphic2d_Line::InteriorPattern () const { |
96 | |
97 | return myPatternIndex; |
98 | |
99 | } |
100 | |
101 | Graphic2d_TypeOfPolygonFilling Graphic2d_Line::TypeOfPolygonFilling () const { |
102 | |
103 | return myTypeOfPolygonFilling; |
104 | |
105 | } |
106 | |
107 | Standard_Integer Graphic2d_Line::TypeIndex () const { |
108 | |
109 | return myTypeIndex; |
110 | |
111 | } |
112 | |
113 | void Graphic2d_Line::DrawLineAttrib (const Handle(Graphic2d_Drawer)& aDrawer) |
114 | const { |
115 | |
116 | aDrawer->SetLineAttrib (myColorIndex,myTypeIndex,myWidthIndex); |
117 | |
118 | switch (myTypeOfPolygonFilling) { |
119 | case Graphic2d_TOPF_FILLED: |
120 | aDrawer->SetPolyAttrib (myInteriorColorIndex,0,myDrawEdge); |
121 | break; |
122 | case Graphic2d_TOPF_PATTERNED: |
123 | aDrawer->SetPolyAttrib (myInteriorColorIndex, |
124 | myPatternIndex,myDrawEdge); |
125 | break; |
126 | default: break; |
127 | } |
128 | |
129 | } |
130 | |
131 | void Graphic2d_Line::DrawMarkerAttrib (const Handle(Graphic2d_Drawer)& aDrawer) |
132 | const { |
133 | |
134 | Standard_Boolean filled = (myTypeOfPolygonFilling != Graphic2d_TOPF_EMPTY); |
135 | |
136 | switch (myTypeOfPolygonFilling) { |
137 | case Graphic2d_TOPF_FILLED: |
138 | aDrawer->SetPolyAttrib (myInteriorColorIndex,0,Standard_False); |
139 | break; |
140 | case Graphic2d_TOPF_PATTERNED: |
141 | aDrawer->SetPolyAttrib (myInteriorColorIndex,myPatternIndex,Standard_False); |
142 | break; |
143 | default: break; |
144 | } |
145 | |
146 | aDrawer->SetMarkerAttrib (myColorIndex,myWidthIndex,filled); |
147 | |
148 | } |
149 | |
150 | Standard_Boolean Graphic2d_Line::IsIn ( const Standard_ShortReal aX, |
151 | const Standard_ShortReal aY, |
152 | const TShort_Array1OfShortReal& X, |
153 | const TShort_Array1OfShortReal& Y, |
154 | const Standard_ShortReal aPrecision) |
155 | { |
156 | Standard_Integer i1=0,i2=0,n; |
157 | Standard_Real dx1,dy1,dx2,dy2,anglesum=0.,angle; |
158 | Standard_Real prosca,provec,norme1,norme2,cosin; |
159 | n = X.Length (); |
160 | for (Standard_Integer m = 1; m <= n; m++) { |
161 | i1++; |
162 | i2 = (i1 == n) ? 1 : i1 + 1; |
163 | dx1 = X(i1) - aX; dy1 = Y(i1) - aY; |
164 | dx2 = X(i2) - aX; dy2 = Y(i2) - aY; |
165 | prosca = dx1 * dx2 + dy1 * dy2; |
166 | provec = dx1 * dy2 - dx2 * dy1; |
167 | norme1 = Sqrt ( dx1 * dx1 + dy1 * dy1 ); |
168 | norme2 = Sqrt ( dx2 * dx2 + dy2 * dy2 ); |
169 | if ( norme1 <= aPrecision || norme2 <= aPrecision ) return Standard_True; |
170 | |
171 | cosin = prosca / norme1 / norme2; |
172 | if ( cosin >= 1 ) angle = 0.; |
173 | else { |
c6541a0c |
174 | if ( cosin <= -1) angle = - M_PI; |
7fd59977 |
175 | else |
176 | angle = Sign ( ACos ( cosin ) , provec ); |
177 | } |
178 | anglesum = anglesum + angle;} |
179 | return (Abs (anglesum) > 1.) ; |
180 | } |
181 | |
182 | Standard_Boolean Graphic2d_Line::IsOn ( const Standard_ShortReal aX, |
183 | const Standard_ShortReal aY, |
184 | const Standard_ShortReal aX1, |
185 | const Standard_ShortReal aY1, |
186 | const Standard_ShortReal aX2, |
187 | const Standard_ShortReal aY2, |
188 | const Standard_ShortReal aPrecision) |
189 | { |
190 | |
191 | Standard_ShortReal DX = aX2 - aX1, DY = aY2 - aY1, dd = DX*DX + DY*DY; |
192 | |
193 | |
194 | #ifdef G002 |
195 | if( Sqrt(dd) < aPrecision ) |
196 | #else |
197 | if( dd < aPrecision ) |
198 | #endif |
199 | return (Abs (aX - aX1) + Abs (aY - aY1)) < aPrecision; |
200 | |
201 | Standard_ShortReal lambda = (DX*(aX-aX1) + DY*(aY-aY1)) / dd; |
202 | |
203 | if ( lambda >= 0. && lambda <= 1. ) { |
204 | |
205 | // On prend comme norme la somme des valeurs absolues: |
206 | Standard_ShortReal Xproj = aX1 + lambda * DX; |
207 | Standard_ShortReal Yproj = aY1 + lambda * DY; |
208 | return (Abs (aX - Xproj) + Abs (aY - Yproj)) < aPrecision; |
209 | |
210 | } |
211 | else |
212 | return Standard_False; |
213 | } |
214 | |
215 | void Graphic2d_Line::Save(Aspect_FStream& aFStream) const |
216 | { |
217 | *aFStream << myColorIndex << ' ' << myWidthIndex << endl; |
218 | *aFStream << myTypeIndex << ' ' << myPatternIndex << endl; |
219 | *aFStream << myInteriorColorIndex << ' ' << myTypeOfPolygonFilling << ' ' << myDrawEdge << endl; |
220 | } |
221 | |
222 | void Graphic2d_Line::Retrieve(Aspect_IFStream& anIFStream) |
223 | { |
224 | int topf; |
225 | *anIFStream >> myColorIndex >> myWidthIndex; |
226 | *anIFStream >> myTypeIndex >> myPatternIndex; |
227 | *anIFStream >> myInteriorColorIndex; |
228 | *anIFStream >> topf; |
229 | myTypeOfPolygonFilling=Graphic2d_TypeOfPolygonFilling(topf); |
230 | *anIFStream >> myDrawEdge; |
231 | } |
232 | |