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
6 // This file is part of Open CASCADE Technology software library.
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.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
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>
24 //-- #include <Precision.hxx> Precision::Infinite() -> 1e+100
25 //=======================================================================
28 //=======================================================================
29 void Bnd_Box2d::Update (const Standard_Real x, const Standard_Real y,
30 const Standard_Real X, const Standard_Real Y)
32 if (Flags & VoidMask) {
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;
47 //=======================================================================
50 //=======================================================================
52 void Bnd_Box2d::Update (const Standard_Real X, const Standard_Real Y)
54 if (Flags & VoidMask) {
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;
69 //=======================================================================
72 //=======================================================================
74 void Bnd_Box2d::Get (Standard_Real& x, Standard_Real& y,
75 Standard_Real& Xm, Standard_Real& Ym) const
78 throw Standard_ConstructionError("Bnd_Box is void");
79 Standard_Real pinf = 1e+100; //-- Precision::Infinite();
80 if (Flags & XminMask) x = -pinf;
82 if (Flags & XmaxMask) Xm = pinf;
84 if (Flags & YminMask) y = -pinf;
86 if (Flags & YmaxMask) Ym = pinf;
90 //=======================================================================
91 //function : Transformed
93 //=======================================================================
95 Bnd_Box2d Bnd_Box2d::Transformed (const gp_Trsf2d& T) const
97 gp_TrsfForm F = T.Form();
98 Bnd_Box2d newb(*this);
99 if ( IsVoid() ) return newb;
101 if (F == gp_Identity) {}
102 else if (F == gp_Translation) {
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;
112 Standard_Boolean Vertex[4];
114 Vertex[0] = Standard_True;
115 Vertex[1] = Standard_True;
116 Vertex[2] = Standard_True;
117 Vertex[3] = Standard_True;
119 // Standard_Integer vertices = 0;
120 Standard_Integer directions = 0;
122 if (Flags & XminMask) {
123 D[directions].SetCoord(-1., 0.);
125 Vertex[0] = Vertex[2] = Standard_False;
127 if (Flags & XmaxMask) {
128 D[directions].SetCoord( 1., 0.);
130 Vertex[1] = Vertex[3] = Standard_False;
132 if (Flags & YminMask) {
133 D[directions].SetCoord( 0.,-1.);
135 Vertex[0] = Vertex[1] = Standard_False;
137 if (Flags & YmaxMask) {
138 D[directions].SetCoord( 0., 1.);
140 Vertex[2] = Vertex[3] = Standard_False;
145 for (i = 0; i < directions; i++) {
149 P[0].SetCoord(Xmin,Ymin);
150 P[1].SetCoord(Xmax,Ymin);
151 P[2].SetCoord(Xmin,Ymax);
152 P[3].SetCoord(Xmax,Ymax);
174 //=======================================================================
177 //=======================================================================
179 void Bnd_Box2d::Add (const Bnd_Box2d& Other)
181 if (IsWhole()) return;
182 else if (Other.IsVoid()) return;
183 else if (Other.IsWhole()) SetWhole();
184 else if (IsVoid()) (*this) = Other;
187 if ( ! IsOpenXmin() )
189 if (Other.IsOpenXmin()) OpenXmin();
190 else if (Xmin > Other.Xmin) Xmin = Other.Xmin;
192 if ( ! IsOpenXmax() )
194 if (Other.IsOpenXmax()) OpenXmax();
195 else if (Xmax < Other.Xmax) Xmax = Other.Xmax;
197 if ( ! IsOpenYmin() )
199 if (Other.IsOpenYmin()) OpenYmin();
200 else if (Ymin > Other.Ymin) Ymin = Other.Ymin;
202 if ( ! IsOpenYmax() )
204 if (Other.IsOpenYmax()) OpenYmax();
205 else if (Ymax < Other.Ymax) Ymax = Other.Ymax;
207 Gap = Max (Gap, Other.Gap);
211 //=======================================================================
214 //=======================================================================
216 void Bnd_Box2d::Add (const gp_Dir2d& D)
218 Standard_Real DX = D.X();
219 Standard_Real DY = D.Y();
221 if (DX < -RealEpsilon())
223 else if (DX > RealEpsilon())
226 if (DY < -RealEpsilon())
228 else if (DY > RealEpsilon())
232 //=======================================================================
235 //=======================================================================
237 Standard_Boolean Bnd_Box2d::IsOut (const gp_Pnt2d& P) const
239 if (IsWhole()) return Standard_False;
240 else if (IsVoid()) return Standard_True;
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;
252 //=======================================================================
255 //=======================================================================
257 Standard_Boolean Bnd_Box2d::IsOut(const gp_Lin2d& theL) const
261 return Standard_False;
265 return Standard_True;
267 Standard_Real aXMin, aXMax, aYMin, aYMax;
268 Get(aXMin, aYMin, aXMax, aYMax);
270 gp_XY aCenter((aXMin + aXMax) / 2, (aYMin + aYMax) / 2);
271 gp_XY aHeigh(Abs(aXMax - aCenter.X()), Abs(aYMax - aCenter.Y()));
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()
278 Standard_Boolean aStatus = (Abs(aProd[0]) > (Abs(aProd[1]) + Abs(aProd[2])));
282 //=======================================================================
285 //=======================================================================
287 Standard_Boolean Bnd_Box2d::IsOut(const gp_Pnt2d& theP0, const gp_Pnt2d& theP1) const
291 return Standard_False;
295 return Standard_True;
298 Standard_Boolean aStatus = Standard_True;
299 Standard_Real aLocXMin, aLocXMax, aLocYMin, aLocYMax;
300 Get(aLocXMin, aLocYMin, aLocXMax, aLocYMax);
302 //// Intersect the line containing the segment.
303 const gp_XY aSegDelta(theP1.XY() - theP0.XY());
305 gp_XY aCenter((aLocXMin + aLocXMax) / 2, (aLocYMin + aLocYMax) / 2);
306 gp_XY aHeigh(Abs(aLocXMax - aCenter.X()), Abs(aLocYMax - aCenter.Y()));
308 const Standard_Real aProd[3] = {
309 aSegDelta ^ (aCenter - theP0.XY()),
310 aSegDelta.X() * aHeigh.Y(),
311 aSegDelta.Y() * aHeigh.X()
314 if((Abs(aProd[0]) <= (Abs(aProd[1]) + Abs(aProd[2]))))
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()));
326 //=======================================================================
329 //=======================================================================
331 Standard_Boolean Bnd_Box2d::IsOut (const Bnd_Box2d& Other) const
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;
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;
345 return Standard_False;
348 //=======================================================================
351 //=======================================================================
353 void Bnd_Box2d::Dump () const
355 std::cout << "Box2d : ";
356 if (IsVoid()) std::cout << "Void";
357 else if (IsWhole()) std::cout << "Whole";
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;
372 std::cout << "\n Gap : " << Gap;