7fd59977 |
1 | // File: Bnd_Box2d.cxx |
2 | // Created: Wed Oct 30 16:21:56 1991 |
3 | // Author: Modelisation |
4 | // <model@sdsun1> |
5 | |
6 | #include <Bnd_Box2d.ixx> |
7 | #include <Standard_Stream.hxx> |
8 | #include <gp.hxx> |
9 | //-- #include <Precision.hxx> Precision::Infinite() -> 1e+100 |
10 | |
11 | //======================================================================= |
12 | //function : Update |
13 | //purpose : |
14 | //======================================================================= |
15 | |
16 | void Bnd_Box2d::Update (const Standard_Real x, const Standard_Real y, |
17 | const Standard_Real X, const Standard_Real Y) |
18 | { |
19 | if (Flags & VoidMask) { |
20 | Xmin = x; |
21 | Ymin = y; |
22 | Xmax = X; |
23 | Ymax = Y; |
24 | Flags &= ~VoidMask; |
25 | } |
26 | else { |
27 | if (!(Flags & XminMask) && (x < Xmin)) Xmin = x; |
28 | if (!(Flags & XmaxMask) && (X > Xmax)) Xmax = X; |
29 | if (!(Flags & YminMask) && (y < Ymin)) Ymin = y; |
30 | if (!(Flags & YmaxMask) && (Y > Ymax)) Ymax = Y; |
31 | } |
32 | } |
33 | |
34 | //======================================================================= |
35 | //function : Update |
36 | //purpose : |
37 | //======================================================================= |
38 | |
39 | void Bnd_Box2d::Update (const Standard_Real X, const Standard_Real Y) |
40 | { |
41 | if (Flags & VoidMask) { |
42 | Xmin = X; |
43 | Ymin = Y; |
44 | Xmax = X; |
45 | Ymax = Y; |
46 | Flags &= ~VoidMask; |
47 | } |
48 | else { |
49 | if (!(Flags & XminMask) && (X < Xmin)) Xmin = X; |
50 | else if (!(Flags & XmaxMask) && (X > Xmax)) Xmax = X; |
51 | if (!(Flags & YminMask) && (Y < Ymin)) Ymin = Y; |
52 | else if (!(Flags & YmaxMask) && (Y > Ymax)) Ymax = Y; |
53 | } |
54 | } |
55 | |
56 | //======================================================================= |
57 | //function : Get |
58 | //purpose : |
59 | //======================================================================= |
60 | |
61 | void Bnd_Box2d::Get (Standard_Real& x, Standard_Real& y, |
62 | Standard_Real& Xm, Standard_Real& Ym) const |
63 | { |
64 | if(Flags & VoidMask) |
65 | Standard_ConstructionError::Raise("Bnd_Box is void"); |
66 | Standard_Real pinf = 1e+100; //-- Precision::Infinite(); |
67 | if (Flags & XminMask) x = -pinf; |
68 | else x = Xmin-Gap; |
69 | if (Flags & XmaxMask) Xm = pinf; |
70 | else Xm = Xmax+Gap; |
71 | if (Flags & YminMask) y = -pinf; |
72 | else y = Ymin-Gap; |
73 | if (Flags & YmaxMask) Ym = pinf; |
74 | else Ym = Ymax+Gap; |
75 | } |
76 | |
77 | //======================================================================= |
78 | //function : Transformed |
79 | //purpose : |
80 | //======================================================================= |
81 | |
82 | Bnd_Box2d Bnd_Box2d::Transformed (const gp_Trsf2d& T) const |
83 | { |
84 | gp_TrsfForm F = T.Form(); |
85 | Bnd_Box2d newb(*this); |
86 | if ( IsVoid() ) return newb; |
87 | |
88 | if (F == gp_Identity) {} |
89 | else if (F == gp_Translation) { |
90 | Standard_Real DX,DY; |
91 | (T.TranslationPart()).Coord(DX,DY); |
92 | if (!(Flags & XminMask)) newb.Xmin += DX; |
93 | if (!(Flags & XmaxMask)) newb.Xmax += DX; |
94 | if (!(Flags & YminMask)) newb.Ymin += DY; |
95 | if (!(Flags & YmaxMask)) newb.Ymax += DY; |
96 | } |
97 | else { |
98 | gp_Pnt2d P[4]; |
99 | Standard_Boolean Vertex[4]; |
100 | Standard_Integer i; |
101 | Vertex[0] = Standard_True; |
102 | Vertex[1] = Standard_True; |
103 | Vertex[2] = Standard_True; |
104 | Vertex[3] = Standard_True; |
105 | gp_Dir2d D[6]; |
106 | // Standard_Integer vertices = 0; |
107 | Standard_Integer directions = 0; |
108 | |
109 | if (Flags & XminMask) { |
110 | D[directions].SetCoord(-1., 0.); |
111 | directions++; |
112 | Vertex[0] = Vertex[2] = Standard_False; |
113 | } |
114 | if (Flags & XmaxMask) { |
115 | D[directions].SetCoord( 1., 0.); |
116 | directions++; |
117 | Vertex[1] = Vertex[3] = Standard_False; |
118 | } |
119 | if (Flags & YminMask) { |
120 | D[directions].SetCoord( 0.,-1.); |
121 | directions++; |
122 | Vertex[0] = Vertex[1] = Standard_False; |
123 | } |
124 | if (Flags & YmaxMask) { |
125 | D[directions].SetCoord( 0., 1.); |
126 | directions++; |
127 | Vertex[2] = Vertex[3] = Standard_False; |
128 | } |
129 | |
130 | newb.SetVoid(); |
131 | |
132 | for (i = 0; i < directions; i++) { |
133 | D[i].Transform(T); |
134 | newb.Add(D[i]); |
135 | } |
136 | P[0].SetCoord(Xmin,Ymin); |
137 | P[1].SetCoord(Xmax,Ymin); |
138 | P[2].SetCoord(Xmin,Ymax); |
139 | P[3].SetCoord(Xmax,Ymax); |
140 | if (Vertex[0]) { |
141 | P[0].Transform(T); |
142 | newb.Add(P[0]); |
143 | } |
144 | if (Vertex[1]) { |
145 | P[1].Transform(T); |
146 | newb.Add(P[1]); |
147 | } |
148 | if (Vertex[2]) { |
149 | P[2].Transform(T); |
150 | newb.Add(P[2]); |
151 | } |
152 | if (Vertex[3]) { |
153 | P[3].Transform(T); |
154 | newb.Add(P[3]); |
155 | } |
156 | newb.Gap=Gap; |
157 | } |
158 | return newb; |
159 | } |
160 | |
161 | //======================================================================= |
162 | //function : Add |
163 | //purpose : |
164 | //======================================================================= |
165 | |
166 | void Bnd_Box2d::Add (const Bnd_Box2d& Other) |
167 | { |
168 | if (IsWhole()) return; |
169 | else if (Other.IsVoid()) return; |
170 | else if (Other.IsWhole()) SetWhole(); |
171 | else if (IsVoid()) (*this) = Other; |
172 | else |
173 | { |
174 | if ( ! IsOpenXmin() ) |
175 | { |
176 | if (Other.IsOpenXmin()) OpenXmin(); |
177 | else if (Xmin > Other.Xmin) Xmin = Other.Xmin; |
178 | } |
179 | if ( ! IsOpenXmax() ) |
180 | { |
181 | if (Other.IsOpenXmax()) OpenXmax(); |
182 | else if (Xmax < Other.Xmax) Xmax = Other.Xmax; |
183 | } |
184 | if ( ! IsOpenYmin() ) |
185 | { |
186 | if (Other.IsOpenYmin()) OpenYmin(); |
187 | else if (Ymin > Other.Ymin) Ymin = Other.Ymin; |
188 | } |
189 | if ( ! IsOpenYmax() ) |
190 | { |
191 | if (Other.IsOpenYmax()) OpenYmax(); |
192 | else if (Ymax < Other.Ymax) Ymax = Other.Ymax; |
193 | } |
194 | Gap = Max (Gap, Other.Gap); |
195 | } |
196 | } |
197 | |
198 | //======================================================================= |
199 | //function : Add |
200 | //purpose : |
201 | //======================================================================= |
202 | |
203 | void Bnd_Box2d::Add (const gp_Dir2d& D) |
204 | { |
205 | Standard_Real DX = D.X(); |
206 | Standard_Real DY = D.Y(); |
207 | if (DX < 0) |
208 | if (DX < - gp::Resolution()) OpenXmin(); |
209 | else |
210 | if (DX > gp::Resolution()) OpenXmax(); |
211 | if (DY < 0) |
212 | if (DY < - gp::Resolution()) OpenYmin(); |
213 | else |
214 | if (DY > gp::Resolution()) OpenYmax(); |
215 | } |
216 | |
217 | //======================================================================= |
218 | //function : IsOut |
219 | //purpose : |
220 | //======================================================================= |
221 | |
222 | Standard_Boolean Bnd_Box2d::IsOut (const gp_Pnt2d& P) const |
223 | { |
224 | if (IsWhole()) return Standard_False; |
225 | else if (IsVoid()) return Standard_True; |
226 | else { |
227 | Standard_Real X = P.X(); |
228 | Standard_Real Y = P.Y(); |
229 | if (!(Flags & XminMask) && (X < (Xmin-Gap))) return Standard_True; |
230 | else if (!(Flags & XmaxMask) && (X > (Xmax+Gap))) return Standard_True; |
231 | else if (!(Flags & YminMask) && (Y < (Ymin-Gap))) return Standard_True; |
232 | else if (!(Flags & YmaxMask) && (Y > (Ymax+Gap))) return Standard_True; |
233 | else return Standard_False; |
234 | } |
235 | } |
236 | |
237 | //======================================================================= |
238 | //function : IsOut |
239 | //purpose : |
240 | //======================================================================= |
241 | |
242 | Standard_Boolean Bnd_Box2d::IsOut (const Bnd_Box2d& Other) const |
243 | { |
244 | if (IsWhole()) return Standard_False; |
245 | else if (IsVoid()) return Standard_True; |
246 | else if (Other.IsWhole()) return Standard_False; |
247 | else if (Other.IsVoid()) return Standard_True; |
248 | else { |
249 | Bnd_Box2d OtherBox2d = Other; // DownEqual |
250 | Standard_Real OXmin,OXmax,OYmin,OYmax; |
251 | OtherBox2d.Get(OXmin,OYmin,OXmax,OYmax); |
252 | if (!(Flags & XminMask) && (OXmax < (Xmin-Gap))) return Standard_True; |
253 | else if (!(Flags & XmaxMask) && (OXmin > (Xmax+Gap))) return Standard_True; |
254 | else if (!(Flags & YminMask) && (OYmax < (Ymin-Gap))) return Standard_True; |
255 | else if (!(Flags & YmaxMask) && (OYmin > (Ymax+Gap))) return Standard_True; |
256 | } |
257 | return Standard_False; |
258 | } |
259 | |
260 | //======================================================================= |
261 | //function : Dump |
262 | //purpose : |
263 | //======================================================================= |
264 | |
265 | void Bnd_Box2d::Dump () const |
266 | { |
267 | cout << "Box2d : "; |
268 | if (IsVoid()) cout << "Void"; |
269 | else if (IsWhole()) cout << "Whole"; |
270 | else { |
271 | cout << "\n Xmin : "; |
272 | if (IsOpenXmin()) cout << "Infinite"; |
273 | else cout << Xmin; |
274 | cout << "\n Xmax : "; |
275 | if (IsOpenXmax()) cout << "Infinite"; |
276 | else cout << Xmax; |
277 | cout << "\n Ymin : "; |
278 | if (IsOpenYmin()) cout << "Infinite"; |
279 | else cout << Ymin; |
280 | cout << "\n Ymax : "; |
281 | if (IsOpenYmax()) cout << "Infinite"; |
282 | else cout << Ymax; |
283 | } |
284 | cout << "\n Gap : " << Gap; |
285 | cout << "\n"; |
286 | } |