0033661: Data Exchange, Step Import - Tessellated GDTs are not imported
[occt.git] / src / Bnd / Bnd_Box2d.cxx
1 // Created on: 1991-10-30
2 // Created by: Modelisation
3 // Copyright (c) 1991-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17
18 #include <Bnd_Box2d.hxx>
19 #include <gp_Dir2d.hxx>
20 #include <gp_Trsf2d.hxx>
21 #include <Standard_ConstructionError.hxx>
22 #include <Standard_Stream.hxx>
23
24 //-- #include <Precision.hxx> Precision::Infinite() -> 1e+100
25 //=======================================================================
26 //function : Update
27 //purpose  : 
28 //=======================================================================
29 void Bnd_Box2d::Update (const Standard_Real x, const Standard_Real y, 
30                         const Standard_Real X, const Standard_Real Y)
31 {
32   if (Flags & VoidMask) {
33     Xmin = x;
34     Ymin = y;
35     Xmax = X;
36     Ymax = Y;
37     Flags &= ~VoidMask;
38   }
39   else {
40     if (!(Flags & XminMask) && (x < Xmin)) Xmin = x;
41     if (!(Flags & XmaxMask) && (X > Xmax)) Xmax = X;
42     if (!(Flags & YminMask) && (y < Ymin)) Ymin = y;
43     if (!(Flags & YmaxMask) && (Y > Ymax)) Ymax = Y;
44   }
45 }
46
47 //=======================================================================
48 //function : Update
49 //purpose  : 
50 //=======================================================================
51
52 void Bnd_Box2d::Update (const Standard_Real X, const Standard_Real Y)
53 {
54   if (Flags & VoidMask) {
55     Xmin = X;
56     Ymin = Y;
57     Xmax = X;
58     Ymax = Y;
59     Flags &= ~VoidMask;
60   }
61   else {
62     if      (!(Flags & XminMask) && (X < Xmin)) Xmin = X;
63     else if (!(Flags & XmaxMask) && (X > Xmax)) Xmax = X;
64     if      (!(Flags & YminMask) && (Y < Ymin)) Ymin = Y;
65     else if (!(Flags & YmaxMask) && (Y > Ymax)) Ymax = Y;
66   }
67 }
68
69 //=======================================================================
70 //function : Get
71 //purpose  : 
72 //=======================================================================
73
74 void Bnd_Box2d::Get (Standard_Real& x, Standard_Real& y,
75                      Standard_Real& Xm, Standard_Real& Ym) const
76 {
77   if(Flags & VoidMask)
78     throw Standard_ConstructionError("Bnd_Box is void");
79   Standard_Real pinf = 1e+100; //-- Precision::Infinite();
80   if (Flags & XminMask) x = -pinf;
81   else                  x =  Xmin-Gap;
82   if (Flags & XmaxMask) Xm =  pinf;
83   else                  Xm =  Xmax+Gap;
84   if (Flags & YminMask) y = -pinf;
85   else                  y =  Ymin-Gap;
86   if (Flags & YmaxMask) Ym =  pinf;
87   else                  Ym =  Ymax+Gap;
88 }
89
90 //=======================================================================
91 //function : Transformed
92 //purpose  : 
93 //=======================================================================
94
95 Bnd_Box2d Bnd_Box2d::Transformed (const gp_Trsf2d& T) const
96 {
97   gp_TrsfForm F = T.Form();
98   Bnd_Box2d newb(*this);
99   if ( IsVoid() ) return newb;
100
101   if      (F == gp_Identity) {}
102   else if (F == gp_Translation) {
103     Standard_Real DX,DY;
104     (T.TranslationPart()).Coord(DX,DY);
105     if (!(Flags & XminMask))  newb.Xmin += DX;
106     if (!(Flags & XmaxMask))  newb.Xmax += DX;
107     if (!(Flags & YminMask))  newb.Ymin += DY;
108     if (!(Flags & YmaxMask))  newb.Ymax += DY;
109   }
110   else {
111     gp_Pnt2d P[4];
112     Standard_Boolean Vertex[4];
113     Standard_Integer i;
114     Vertex[0] = Standard_True;
115     Vertex[1] = Standard_True;
116     Vertex[2] = Standard_True;
117     Vertex[3] = Standard_True;
118     gp_Dir2d D[6];
119 //    Standard_Integer vertices = 0;
120     Standard_Integer directions = 0;
121
122     if (Flags & XminMask) {
123       D[directions].SetCoord(-1., 0.);
124       directions++;
125       Vertex[0] = Vertex[2] = Standard_False;
126     }
127     if (Flags & XmaxMask) {
128       D[directions].SetCoord( 1., 0.);
129       directions++;
130       Vertex[1] = Vertex[3] = Standard_False;
131     }
132     if (Flags & YminMask) {
133       D[directions].SetCoord( 0.,-1.);
134       directions++;
135       Vertex[0] = Vertex[1] = Standard_False;
136     }
137     if (Flags & YmaxMask) {
138       D[directions].SetCoord( 0., 1.);
139       directions++;
140       Vertex[2] = Vertex[3] = Standard_False;
141     }
142
143     newb.SetVoid();
144
145     for (i = 0; i < directions; i++) {
146       D[i].Transform(T);
147       newb.Add(D[i]);
148     }
149     P[0].SetCoord(Xmin,Ymin);
150     P[1].SetCoord(Xmax,Ymin);
151     P[2].SetCoord(Xmin,Ymax);
152     P[3].SetCoord(Xmax,Ymax);
153     if (Vertex[0]) {
154       P[0].Transform(T);
155       newb.Add(P[0]);
156     }
157     if (Vertex[1]) {
158       P[1].Transform(T);
159       newb.Add(P[1]);
160     }
161     if (Vertex[2]) {
162       P[2].Transform(T);
163       newb.Add(P[2]);
164     }
165     if (Vertex[3]) {
166       P[3].Transform(T);
167       newb.Add(P[3]);
168     }
169     newb.Gap=Gap;
170   }
171   return newb;
172 }
173
174 //=======================================================================
175 //function : Add
176 //purpose  : 
177 //=======================================================================
178
179 void Bnd_Box2d::Add (const Bnd_Box2d& Other)
180 {
181   if (IsWhole()) return;
182   else if (Other.IsVoid()) return; 
183   else if (Other.IsWhole()) SetWhole();
184   else if (IsVoid()) (*this) = Other;
185   else
186   {
187     if ( ! IsOpenXmin() )
188     {
189       if (Other.IsOpenXmin()) OpenXmin();
190       else if (Xmin > Other.Xmin) Xmin = Other.Xmin;
191     }
192     if ( ! IsOpenXmax() )
193     {
194       if (Other.IsOpenXmax()) OpenXmax();
195       else if (Xmax < Other.Xmax) Xmax = Other.Xmax;
196     }
197     if ( ! IsOpenYmin() )
198     {
199       if (Other.IsOpenYmin()) OpenYmin();
200       else if (Ymin > Other.Ymin) Ymin = Other.Ymin;
201     }
202     if ( ! IsOpenYmax() )
203     {
204       if (Other.IsOpenYmax()) OpenYmax();
205       else if (Ymax < Other.Ymax) Ymax = Other.Ymax;
206     }
207     Gap = Max (Gap, Other.Gap);
208   }
209 }
210
211 //=======================================================================
212 //function : Add
213 //purpose  : 
214 //=======================================================================
215
216 void Bnd_Box2d::Add (const gp_Dir2d& D)
217 {
218   Standard_Real DX = D.X();
219   Standard_Real DY = D.Y();
220
221   if (DX < -RealEpsilon()) 
222     OpenXmin();
223   else if (DX > RealEpsilon()) 
224     OpenXmax();
225
226   if (DY < -RealEpsilon())
227     OpenYmin();
228   else if (DY > RealEpsilon())
229     OpenYmax();
230 }
231
232 //=======================================================================
233 //function : IsOut
234 //purpose  : 
235 //=======================================================================
236
237 Standard_Boolean Bnd_Box2d::IsOut (const gp_Pnt2d& P) const
238 {
239   if        (IsWhole())  return Standard_False;
240   else if   (IsVoid())   return Standard_True;
241   else {
242     Standard_Real X = P.X();
243     Standard_Real Y = P.Y();
244     if      (!(Flags & XminMask) && (X < (Xmin-Gap))) return Standard_True;
245     else if (!(Flags & XmaxMask) && (X > (Xmax+Gap))) return Standard_True;
246     else if (!(Flags & YminMask) && (Y < (Ymin-Gap))) return Standard_True;
247     else if (!(Flags & YmaxMask) && (Y > (Ymax+Gap))) return Standard_True;
248     else return Standard_False;
249   }
250 }
251
252 //=======================================================================
253 //function : IsOut
254 //purpose  :
255 //=======================================================================
256
257 Standard_Boolean Bnd_Box2d::IsOut(const gp_Lin2d& theL) const
258 {
259   if (IsWhole())
260   {
261     return Standard_False;
262   }
263   if (IsVoid())
264   {
265     return Standard_True;
266   }
267   Standard_Real aXMin, aXMax, aYMin, aYMax;
268   Get(aXMin, aYMin, aXMax, aYMax);
269
270   gp_XY aCenter((aXMin + aXMax) / 2, (aYMin + aYMax) / 2);
271   gp_XY aHeigh(Abs(aXMax - aCenter.X()), Abs(aYMax - aCenter.Y()));
272
273   const Standard_Real aProd[3] = {
274     theL.Direction().XY() ^ (aCenter - theL.Location().XY()),
275     theL.Direction().X() * aHeigh.Y(),
276     theL.Direction().Y() * aHeigh.X()
277   };
278   Standard_Boolean aStatus = (Abs(aProd[0]) > (Abs(aProd[1]) + Abs(aProd[2])));
279   return aStatus;
280 }
281
282 //=======================================================================
283 //function : IsOut
284 //purpose  :
285 //=======================================================================
286
287 Standard_Boolean Bnd_Box2d::IsOut(const gp_Pnt2d& theP0, const gp_Pnt2d& theP1) const
288 {
289   if (IsWhole())
290   {
291     return Standard_False;
292   }
293   if (IsVoid())
294   {
295     return Standard_True;
296   }
297   
298   Standard_Boolean aStatus = Standard_True;
299   Standard_Real aLocXMin, aLocXMax, aLocYMin, aLocYMax;
300   Get(aLocXMin, aLocYMin, aLocXMax, aLocYMax);
301
302   //// Intersect the line containing the segment.
303   const gp_XY aSegDelta(theP1.XY() - theP0.XY());
304
305   gp_XY aCenter((aLocXMin + aLocXMax) / 2, (aLocYMin + aLocYMax) / 2);
306   gp_XY aHeigh(Abs(aLocXMax - aCenter.X()), Abs(aLocYMax - aCenter.Y()));
307
308   const Standard_Real aProd[3] = {
309     aSegDelta ^ (aCenter - theP0.XY()),
310     aSegDelta.X() * aHeigh.Y(),
311     aSegDelta.Y() * aHeigh.X()
312   };
313
314   if((Abs(aProd[0]) <= (Abs(aProd[1]) + Abs(aProd[2]))))
315   {
316     // Intersection with line detected; check the segment as bounding box
317     const gp_XY aHSeg(0.5 * aSegDelta.X(), 0.5 * aSegDelta.Y());
318     const gp_XY aHSegAbs(Abs(aHSeg.X()), Abs(aHSeg.Y()));
319     aStatus = ((Abs((theP0.XY() + aHSeg - aCenter).X()) >
320       (aHeigh + aHSegAbs).X()) || (Abs((theP0.XY() + aHSeg - aCenter).Y()) >
321       (aHeigh + aHSegAbs).Y()));
322   }
323   return aStatus;
324 }
325
326 //=======================================================================
327 //function : IsOut
328 //purpose  : 
329 //=======================================================================
330
331 Standard_Boolean Bnd_Box2d::IsOut (const Bnd_Box2d& Other) const
332 {
333   if        (IsWhole())  return Standard_False;
334   else if   (IsVoid())   return Standard_True;
335   else if   (Other.IsWhole())  return Standard_False;
336   else if   (Other.IsVoid())   return Standard_True;
337   else {
338     Standard_Real OXmin,OXmax,OYmin,OYmax;
339     Other.Get(OXmin,OYmin,OXmax,OYmax);
340     if      (!(Flags & XminMask) && (OXmax < (Xmin-Gap))) return Standard_True;
341     else if (!(Flags & XmaxMask) && (OXmin > (Xmax+Gap))) return Standard_True;
342     else if (!(Flags & YminMask) && (OYmax < (Ymin-Gap))) return Standard_True;
343     else if (!(Flags & YmaxMask) && (OYmin > (Ymax+Gap))) return Standard_True;
344   }
345   return Standard_False;
346 }
347
348 //=======================================================================
349 //function : Dump
350 //purpose  : 
351 //=======================================================================
352
353 void Bnd_Box2d::Dump () const
354 {
355   std::cout << "Box2d : ";
356   if      (IsVoid())  std::cout << "Void";
357   else if (IsWhole()) std::cout << "Whole";
358   else {
359     std::cout << "\n Xmin : ";
360     if (IsOpenXmin()) std::cout << "Infinite";
361     else              std::cout << Xmin;
362     std::cout << "\n Xmax : ";
363     if (IsOpenXmax()) std::cout << "Infinite";
364     else              std::cout << Xmax;
365     std::cout << "\n Ymin : ";
366     if (IsOpenYmin()) std::cout << "Infinite";
367     else              std::cout << Ymin;
368     std::cout << "\n Ymax : ";
369     if (IsOpenYmax()) std::cout << "Infinite";
370     else              std::cout << Ymax;
371   }
372   std::cout << "\n Gap : " << Gap;
373   std::cout << "\n";
374 }