d13569d823b4cb4cbc560dacdc39834d9d959b87
[occt.git] / src / TopOpeBRepTool / TopOpeBRepTool_tol.cxx
1 // Created on: 1997-04-01
2 // Created by: Jean Yves LEBEY
3 // Copyright (c) 1997-1999 Matra Datavision
4 // Copyright (c) 1999-2012 OPEN CASCADE SAS
5 //
6 // The content of this file is subject to the Open CASCADE Technology Public
7 // License Version 6.5 (the "License"). You may not use the content of this file
8 // except in compliance with the License. Please obtain a copy of the License
9 // at http://www.opencascade.org and read it completely before using this file.
10 //
11 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
12 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 //
14 // The Original Code and all software distributed under the License is
15 // distributed on an "AS IS" basis, without warranty of any kind, and the
16 // Initial Developer hereby disclaims all such warranties, including without
17 // limitation, any warranties of merchantability, fitness for a particular
18 // purpose or non-infringement. Please see the License for the specific terms
19 // and conditions governing the rights and limitations under the License.
20
21
22 #include <TopOpeBRepTool_tol.hxx>
23
24 #include <TopoDS.hxx>
25 #include <TopExp_Explorer.hxx>
26 #include <Precision.hxx>
27 #include <BRep_Tool.hxx>
28 #include <TopOpeBRepTool_HBoxTool.hxx>
29 #include <Bnd_Box.hxx>
30 #include <TopOpeBRepTool_box.hxx>
31 #include <TopOpeBRepTool_define.hxx>
32
33 Standard_EXPORT void FTOL_FaceTolerances
34 (const Bnd_Box& B1, const Bnd_Box& B2,
35  const TopoDS_Face& myFace1, const TopoDS_Face& myFace2,
36  const BRepAdaptor_Surface& mySurface1, const BRepAdaptor_Surface& mySurface2,
37  Standard_Real& myTol1, Standard_Real& myTol2,
38  Standard_Real& Deflection, Standard_Real& MaxUV)
39 {
40   // ============================================================
41   // ==   t o l e r a n c e s   d   I n t e r s e c t i o n    ==
42   // ============================================================
43   Standard_Real x0,y0,z0,x1,y1,z1,dx,dy,dz;
44   Standard_Boolean Box1OK,Box2OK;
45
46   if(   !B1.IsOpenXmin() && !B1.IsOpenXmax() 
47      && !B1.IsOpenYmin() && !B1.IsOpenYmax() 
48      && !B1.IsOpenZmin() && !B1.IsOpenZmax()
49      && !B1.IsVoid()) { 
50     Box1OK=Standard_True;
51   }
52   else { 
53     Box1OK=Standard_False;
54   }
55
56   if(   !B2.IsOpenXmin() && !B2.IsOpenXmax() 
57      && !B2.IsOpenYmin() && !B2.IsOpenYmax() 
58      && !B2.IsOpenZmin() && !B2.IsOpenZmax() 
59      && !B2.IsVoid()) { 
60     Box2OK=Standard_True;
61   }
62   else { 
63     Box2OK=Standard_False;
64   }
65
66   if(Box1OK) { 
67     B1.Get(x0,y0,z0,x1,y1,z1);
68     dx=x1-x0;
69     dy=y1-y0;
70     dz=z1-z0;
71     
72     if(Box2OK) { 
73       B2.Get(x0,y0,z0,x1,y1,z1);
74       Standard_Real _dx=x1-x0;
75       Standard_Real _dy=y1-y0;
76       Standard_Real _dz=z1-z0;
77       if(_dx>dx) dx=_dx;
78       if(_dy>dy) dy=_dy;
79       if(_dz>dz) dz=_dz;
80     }
81   }
82   else { 
83     if(Box2OK) { 
84       B2.Get(x0,y0,z0,x1,y1,z1);
85       dx=x1-x0;
86       dy=y1-y0;
87       dz=z1-z0;
88     }
89     else { 
90       dx=dy=dz=1.0;
91     }
92   }
93   if(dx<dy) dx=dy;
94   if(dx<dz) dx=dz;
95   if(dx>1000000.0) dx=1000000.0; //-- if(dx>10000.0) dx=10000.0;
96
97   TopExp_Explorer ex;
98   Standard_Real tolef1 = Precision::Confusion();
99   for (ex.Init(myFace1,TopAbs_EDGE);ex.More();ex.Next()) {
100     Standard_Real tole=BRep_Tool::Tolerance(TopoDS::Edge(ex.Current()));
101     if (tole>tolef1) tolef1=tole;
102   }
103   Standard_Real tolef2 = Precision::Confusion();
104   for (ex.Init(myFace2,TopAbs_EDGE);ex.More();ex.Next()) {
105     Standard_Real tole=BRep_Tool::Tolerance(TopoDS::Edge(ex.Current()));
106     if (tole>tolef2) tolef2=tole;
107   }
108   Standard_Real tolef= tolef1;
109   if (tolef2>tolef) tolef=tolef2;
110  //jmb le 30 juillet 99. on ne multiplie pas la tolerance par la dimension de la piece
111  // Standard_Real tolbox = tolef*dx;
112   Standard_Real tolbox = tolef;
113   myTol1 = tolbox;
114   myTol2 = tolbox;
115
116   Standard_Real tolmin = Precision::Confusion();
117   if (tolmin>myTol1) myTol1 = tolmin;
118   if (tolmin>myTol2) myTol2 = tolmin;
119
120   Deflection=0.01;
121   MaxUV=0.01;
122   Deflection*=dx;
123
124   Standard_Real MTOL1 = Max(myTol1,1e-8);
125   Standard_Real MTOL2 = Max(myTol2,1e-8);
126   Standard_Real MDEFLECTION = Deflection;
127   Standard_Real MMAXUV=0.01;
128
129   Standard_Real MU0,MU1,MV0,MV1,DP1,DP2,DP;
130   MU0=mySurface1.FirstUParameter();
131   MU1=mySurface1.LastUParameter();
132   MV0=mySurface1.FirstVParameter();
133   MV1=mySurface1.LastVParameter();
134   DP1 = MU1-MU0;
135   DP2 = MV1-MV0;
136   if(DP2<DP1) DP=DP2; else DP=DP1;  //-- DP + petit
137
138   MU0=mySurface2.FirstUParameter();
139   MU1=mySurface2.LastUParameter();
140   MV0=mySurface2.FirstVParameter();
141   MV1=mySurface2.LastVParameter();
142   DP1 = MU1-MU0;
143   DP2 = MV1-MV0;
144   if(DP2>DP1) DP1=DP2;    //-- DP1 + petit
145   if(DP1<DP) DP=DP1;    //-- DP + petit
146
147   DP*=0.01;
148 //jmb le 30 juillet 99
149 // non ! laisser MMAXUV a 0.01
150 //  MMAXUV=DP;
151
152 #ifdef DEB
153 //  printf("\n FaceTolerances3d : TOL1 = %5.5eg TOL2=%5.5eg DEFL=%5.5eg MAXUV=%5.5eg\n",MTOL1,MTOL2,MDEFLECTION,0.01);
154 #endif
155
156   if(MTOL1>1e-4*dx) MTOL1=1e-4*dx;
157   if(MTOL2>1e-4*dx) MTOL2=1e-4*dx;
158   if(MTOL1<1e-8) MTOL1=1e-8;
159   if(MTOL2<1e-8) MTOL2=1e-8;
160   if(MMAXUV<1e-3) MMAXUV=1e-3;
161   if(MDEFLECTION<1e-3) MDEFLECTION = 1e-3;
162
163   if(MTOL1>0.5) MTOL1=0.5;
164   if(MTOL2>0.5) MTOL2=0.5;
165   if(MMAXUV>0.01) MMAXUV=0.01;
166   if(MDEFLECTION>0.1) MDEFLECTION = 0.1;
167
168 #ifdef DEB
169 //  printf("TOL1 = %5.5eg TOL2=%5.5eg DEFL=%5.5eg MAXUV=%5.5eg\n",MTOL1,MTOL2,MDEFLECTION,MMAXUV);
170 #endif
171
172   myTol1 = MTOL1;
173   myTol2 = MTOL2;
174   Deflection = MDEFLECTION;
175   MaxUV = MMAXUV;
176
177 } // FTOL_FaceTolerances
178  
179  Standard_EXPORT void FTOL_FaceTolerances3d
180 (const TopoDS_Face& myFace1,
181  const TopoDS_Face& myFace2,
182  Standard_Real& Tol)
183 {
184   const Handle(TopOpeBRepTool_HBoxTool)& hbt = FBOX_GetHBoxTool();
185   Bnd_Box B1, B2;
186   if(hbt->HasBox(myFace1)) {
187     B1 = hbt->Box(myFace1);
188   }
189   else {
190     B1.Update(0., 0., 0., 1., 1., 1.);
191   }
192   if(hbt->HasBox(myFace2)) {
193     B2 = hbt->Box(myFace2);
194   }
195   else {
196     B2.Update(0., 0., 0., 1., 1., 1.);
197   }
198   BRepAdaptor_Surface mySurface1;
199   BRepAdaptor_Surface mySurface2;
200   mySurface1.Initialize(myFace1);
201   mySurface2.Initialize(myFace2);
202   Standard_Real Deflection=0.01,MaxUV=0.01;
203   Standard_Real myTol1,myTol2;
204   FTOL_FaceTolerances(B1,B2,
205                             myFace1,myFace2,
206                             mySurface1,mySurface2,
207                             myTol1,myTol2,Deflection,MaxUV);  
208   myTol1 = (myTol1 > 1.e-4)? 1.e-4: myTol1;
209   myTol2 = (myTol2 > 1.e-4)? 1.e-4: myTol2;
210   Tol = Max(myTol1,myTol2);
211 }
212
213 Standard_EXPORT void FTOL_FaceTolerances3d
214 (const Bnd_Box& B1, const Bnd_Box& B2,
215  const TopoDS_Face& myFace1, const TopoDS_Face& myFace2,
216  const BRepAdaptor_Surface& mySurface1, const BRepAdaptor_Surface& mySurface2,
217  Standard_Real& myTol1, Standard_Real& myTol2,
218  Standard_Real& Deflection, Standard_Real& MaxUV)
219 {
220   FTOL_FaceTolerances(B1,B2,
221                             myFace1,myFace2,
222                             mySurface1,mySurface2,
223                             myTol1,myTol2,
224                             Deflection,MaxUV);
225 }
226
227 Standard_EXPORT void FTOL_FaceTolerances2d
228 (const Bnd_Box& B1, 
229  const Bnd_Box& B2,
230  const TopoDS_Face& myFace1, 
231  const TopoDS_Face& myFace2,
232  const BRepAdaptor_Surface& mySurface1,
233  const BRepAdaptor_Surface& mySurface2,
234  Standard_Real& myTol1, Standard_Real& myTol2)
235 {
236   Standard_Real BIDDeflection,BIDMaxUV;
237   FTOL_FaceTolerances(B1,B2,
238                             myFace1,myFace2,
239                             mySurface1,mySurface2,
240                             myTol1,myTol2,
241                             BIDDeflection,BIDMaxUV);  
242 }