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>
20 #include <gp_Dir2d.hxx>
21 #include <gp_Pnt2d.hxx>
22 #include <gp_Trsf2d.hxx>
23 #include <Standard_ConstructionError.hxx>
24 #include <Standard_Stream.hxx>
26 //-- #include <Precision.hxx> Precision::Infinite() -> 1e+100
27 //=======================================================================
30 //=======================================================================
31 void Bnd_Box2d::Update (const Standard_Real x, const Standard_Real y,
32 const Standard_Real X, const Standard_Real Y)
34 if (Flags & VoidMask) {
42 if (!(Flags & XminMask) && (x < Xmin)) Xmin = x;
43 if (!(Flags & XmaxMask) && (X > Xmax)) Xmax = X;
44 if (!(Flags & YminMask) && (y < Ymin)) Ymin = y;
45 if (!(Flags & YmaxMask) && (Y > Ymax)) Ymax = Y;
49 //=======================================================================
52 //=======================================================================
54 void Bnd_Box2d::Update (const Standard_Real X, const Standard_Real Y)
56 if (Flags & VoidMask) {
64 if (!(Flags & XminMask) && (X < Xmin)) Xmin = X;
65 else if (!(Flags & XmaxMask) && (X > Xmax)) Xmax = X;
66 if (!(Flags & YminMask) && (Y < Ymin)) Ymin = Y;
67 else if (!(Flags & YmaxMask) && (Y > Ymax)) Ymax = Y;
71 //=======================================================================
74 //=======================================================================
76 void Bnd_Box2d::Get (Standard_Real& x, Standard_Real& y,
77 Standard_Real& Xm, Standard_Real& Ym) const
80 Standard_ConstructionError::Raise("Bnd_Box is void");
81 Standard_Real pinf = 1e+100; //-- Precision::Infinite();
82 if (Flags & XminMask) x = -pinf;
84 if (Flags & XmaxMask) Xm = pinf;
86 if (Flags & YminMask) y = -pinf;
88 if (Flags & YmaxMask) Ym = pinf;
92 //=======================================================================
93 //function : Transformed
95 //=======================================================================
97 Bnd_Box2d Bnd_Box2d::Transformed (const gp_Trsf2d& T) const
99 gp_TrsfForm F = T.Form();
100 Bnd_Box2d newb(*this);
101 if ( IsVoid() ) return newb;
103 if (F == gp_Identity) {}
104 else if (F == gp_Translation) {
106 (T.TranslationPart()).Coord(DX,DY);
107 if (!(Flags & XminMask)) newb.Xmin += DX;
108 if (!(Flags & XmaxMask)) newb.Xmax += DX;
109 if (!(Flags & YminMask)) newb.Ymin += DY;
110 if (!(Flags & YmaxMask)) newb.Ymax += DY;
114 Standard_Boolean Vertex[4];
116 Vertex[0] = Standard_True;
117 Vertex[1] = Standard_True;
118 Vertex[2] = Standard_True;
119 Vertex[3] = Standard_True;
121 // Standard_Integer vertices = 0;
122 Standard_Integer directions = 0;
124 if (Flags & XminMask) {
125 D[directions].SetCoord(-1., 0.);
127 Vertex[0] = Vertex[2] = Standard_False;
129 if (Flags & XmaxMask) {
130 D[directions].SetCoord( 1., 0.);
132 Vertex[1] = Vertex[3] = Standard_False;
134 if (Flags & YminMask) {
135 D[directions].SetCoord( 0.,-1.);
137 Vertex[0] = Vertex[1] = Standard_False;
139 if (Flags & YmaxMask) {
140 D[directions].SetCoord( 0., 1.);
142 Vertex[2] = Vertex[3] = Standard_False;
147 for (i = 0; i < directions; i++) {
151 P[0].SetCoord(Xmin,Ymin);
152 P[1].SetCoord(Xmax,Ymin);
153 P[2].SetCoord(Xmin,Ymax);
154 P[3].SetCoord(Xmax,Ymax);
176 //=======================================================================
179 //=======================================================================
181 void Bnd_Box2d::Add (const Bnd_Box2d& Other)
183 if (IsWhole()) return;
184 else if (Other.IsVoid()) return;
185 else if (Other.IsWhole()) SetWhole();
186 else if (IsVoid()) (*this) = Other;
189 if ( ! IsOpenXmin() )
191 if (Other.IsOpenXmin()) OpenXmin();
192 else if (Xmin > Other.Xmin) Xmin = Other.Xmin;
194 if ( ! IsOpenXmax() )
196 if (Other.IsOpenXmax()) OpenXmax();
197 else if (Xmax < Other.Xmax) Xmax = Other.Xmax;
199 if ( ! IsOpenYmin() )
201 if (Other.IsOpenYmin()) OpenYmin();
202 else if (Ymin > Other.Ymin) Ymin = Other.Ymin;
204 if ( ! IsOpenYmax() )
206 if (Other.IsOpenYmax()) OpenYmax();
207 else if (Ymax < Other.Ymax) Ymax = Other.Ymax;
209 Gap = Max (Gap, Other.Gap);
213 //=======================================================================
216 //=======================================================================
218 void Bnd_Box2d::Add (const gp_Dir2d& D)
220 Standard_Real DX = D.X();
221 Standard_Real DY = D.Y();
223 if (DX < -RealEpsilon())
225 else if (DX > RealEpsilon())
228 if (DY < -RealEpsilon())
230 else if (DY > RealEpsilon())
234 //=======================================================================
237 //=======================================================================
239 Standard_Boolean Bnd_Box2d::IsOut (const gp_Pnt2d& P) const
241 if (IsWhole()) return Standard_False;
242 else if (IsVoid()) return Standard_True;
244 Standard_Real X = P.X();
245 Standard_Real Y = P.Y();
246 if (!(Flags & XminMask) && (X < (Xmin-Gap))) return Standard_True;
247 else if (!(Flags & XmaxMask) && (X > (Xmax+Gap))) return Standard_True;
248 else if (!(Flags & YminMask) && (Y < (Ymin-Gap))) return Standard_True;
249 else if (!(Flags & YmaxMask) && (Y > (Ymax+Gap))) return Standard_True;
250 else return Standard_False;
254 //=======================================================================
257 //=======================================================================
259 Standard_Boolean Bnd_Box2d::IsOut (const Bnd_Box2d& Other) const
261 if (IsWhole()) return Standard_False;
262 else if (IsVoid()) return Standard_True;
263 else if (Other.IsWhole()) return Standard_False;
264 else if (Other.IsVoid()) return Standard_True;
266 Standard_Real OXmin,OXmax,OYmin,OYmax;
267 Other.Get(OXmin,OYmin,OXmax,OYmax);
268 if (!(Flags & XminMask) && (OXmax < (Xmin-Gap))) return Standard_True;
269 else if (!(Flags & XmaxMask) && (OXmin > (Xmax+Gap))) return Standard_True;
270 else if (!(Flags & YminMask) && (OYmax < (Ymin-Gap))) return Standard_True;
271 else if (!(Flags & YmaxMask) && (OYmin > (Ymax+Gap))) return Standard_True;
273 return Standard_False;
276 //=======================================================================
279 //=======================================================================
281 void Bnd_Box2d::Dump () const
284 if (IsVoid()) cout << "Void";
285 else if (IsWhole()) cout << "Whole";
287 cout << "\n Xmin : ";
288 if (IsOpenXmin()) cout << "Infinite";
290 cout << "\n Xmax : ";
291 if (IsOpenXmax()) cout << "Infinite";
293 cout << "\n Ymin : ";
294 if (IsOpenYmin()) cout << "Infinite";
296 cout << "\n Ymax : ";
297 if (IsOpenYmax()) cout << "Infinite";
300 cout << "\n Gap : " << Gap;