summaryrefslogtreecommitdiff
path: root/src/ShapeAnalysis/ShapeAnalysis_ShapeTolerance.cxx
blob: 6808a88fb048a47c683abb8a113faa1d011cc0a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
//#76 rln 11.03.99 S4135: compute average without weights according to tolerances
//szv#4 S4163
#include <ShapeAnalysis_ShapeTolerance.ixx>

#include <BRep_Tool.hxx>

#include <TopoDS.hxx>
#include <TopoDS_Vertex.hxx>
#include <TopoDS_Edge.hxx>
#include <TopoDS_Face.hxx>

#include <TopExp_Explorer.hxx>

#include <TopTools_MapOfShape.hxx>


//=======================================================================
//function : ShapeAnalysis_ShapeTolerance
//purpose  : 
//=======================================================================

ShapeAnalysis_ShapeTolerance::ShapeAnalysis_ShapeTolerance() : myNbTol (0)
{
}

static  void  AddTol (const Standard_Real tol, Standard_Integer&   nbt,
	      Standard_Real&  cmin, Standard_Real& cmoy, Standard_Real& cmax)
{
  nbt ++;
  if      (nbt == 1)  cmin = cmoy = cmax = tol;
  else {
    if (cmin > tol) cmin = tol;
    if (cmax < tol) cmax = tol;
//    cmoy += tol;
//  Calcul en moyenne geometrique  entre 1 et 1e-7
    Standard_Integer mult = 1;
    //#76 rln 11.03.99 S4135: compute average without weights according to tolerances
/*    if      (tol < 1.e-07) mult = 10000;
    else if (tol < 1.e-06) mult =  3000;
    else if (tol < 1.e-05) mult =  1000;
    else if (tol < 1.e-04) mult =   300;
    else if (tol < 1.e-03) mult =   100;
    else if (tol < 1.e-02) mult =    30;
    else if (tol < 1.e-01) mult =    10;
    else if (tol < 1.    ) mult =     3;
*/
    nbt += (mult-1);
    cmoy += (tol * mult);
  }
}


//=======================================================================
//function : Tolerance
//purpose  : 
//=======================================================================

Standard_Real ShapeAnalysis_ShapeTolerance::Tolerance(const TopoDS_Shape& shape,const Standard_Integer mode,const TopAbs_ShapeEnum type) 
{
  InitTolerance();
  AddTolerance (shape, type);
  return GlobalTolerance (mode);
}

//=======================================================================
//function : OverTolerance
//purpose  : 
//=======================================================================

 Handle(TopTools_HSequenceOfShape) ShapeAnalysis_ShapeTolerance::OverTolerance(const TopoDS_Shape& shape,const Standard_Real value,const TopAbs_ShapeEnum type) const
{
  if (value >= 0) return InTolerance (shape,value,0.,type);
  else return InTolerance (shape,0.,value,type);
}

//=======================================================================
//function : InTolerance
//purpose  : 
//=======================================================================

 Handle(TopTools_HSequenceOfShape) ShapeAnalysis_ShapeTolerance::InTolerance(const TopoDS_Shape& shape,const Standard_Real valmin,const Standard_Real valmax,const TopAbs_ShapeEnum type) const
{
  Standard_Real tol;
  Standard_Boolean over = (valmax < valmin);  // pas de liminite max
  Handle(TopTools_HSequenceOfShape) sl = new TopTools_HSequenceOfShape ();

  TopExp_Explorer myExp;

  // Iteration sur les Faces

  if (type == TopAbs_FACE || type == TopAbs_SHAPE) {
    myExp.Init(shape, TopAbs_FACE);
    while(myExp.More()) {
      tol = BRep_Tool::Tolerance(TopoDS::Face(myExp.Current()));
      if (tol >= valmin && (over || (tol <= valmax)) ) sl->Append (myExp.Current());
      myExp.Next();
    }
  }

  // Iteration sur les Edges

  if (type == TopAbs_EDGE || type == TopAbs_SHAPE) {
    myExp.Init(shape, TopAbs_EDGE);
    while(myExp.More()) {
      tol = BRep_Tool::Tolerance(TopoDS::Edge(myExp.Current()));
      if (tol >= valmin && (over || (tol <= valmax)) ) sl->Append (myExp.Current());
      myExp.Next();
    }
  }

  // Iteration sur les Vertex

  if (type == TopAbs_VERTEX || type == TopAbs_SHAPE) {
    myExp.Init(shape, TopAbs_VERTEX);
    while(myExp.More()) {
      tol = BRep_Tool::Tolerance(TopoDS::Vertex(myExp.Current()));
      if (tol >= valmin && (over || (tol >= valmax)) ) sl->Append (myExp.Current());
      myExp.Next();
    }
  }

  // Iteration combinee (cumul) SHELL+FACE+EDGE+VERTEX, on retourne SHELL+FACE

  if (type == TopAbs_SHELL) {
    //  Exploration des shells
    TopTools_MapOfShape mapface;
    myExp.Init (shape, TopAbs_SHELL);
    while(myExp.More()) {
      Standard_Boolean iashell = Standard_False;
      TopoDS_Shape ash = myExp.Current();
      for (TopExp_Explorer face (ash,TopAbs_FACE); face.More(); face.Next()) {
	mapface.Add (face.Current());
	Handle(TopTools_HSequenceOfShape) fc =
	  InTolerance (face.Current(),valmin,valmax,type);
	if (fc->Length() > 0) {
	  sl->Append(fc);
	  iashell = Standard_True;
	}
      }
      if (iashell) sl->Append (ash);
      myExp.Next();
    }

    //  Les faces (libres ou sous shell)
    myExp.Init (shape, TopAbs_FACE);
    for (; myExp.More(); myExp.Next()) {
      Standard_Boolean iaface = Standard_False;
      if (mapface.Contains (myExp.Current()) ) continue;
      tol = BRep_Tool::Tolerance(TopoDS::Face(myExp.Current()));
      if (tol >= valmin && (over || (tol <= valmax)) ) iaface = Standard_True;
      else {
	// les edges contenues ?
	Handle(TopTools_HSequenceOfShape) fl =
	  InTolerance (myExp.Current(),valmin,valmax,TopAbs_EDGE);
	if (fl->Length() > 0) iaface = Standard_True;
	else {
	  fl = InTolerance (myExp.Current(),valmin,valmax,TopAbs_VERTEX);
	  if (fl->Length() > 0) iaface = Standard_True;
	}
      }
      if (iaface) sl->Append (myExp.Current());
    }

  }

  return sl;
}

//=======================================================================
//function : InitTolerance
//purpose  : 
//=======================================================================

 void ShapeAnalysis_ShapeTolerance::InitTolerance() 
{
  myNbTol = 0;
  myTols[1] = 0;
}

//=======================================================================
//function : AddTolerance
//purpose  : 
//=======================================================================

 void ShapeAnalysis_ShapeTolerance::AddTolerance(const TopoDS_Shape& shape,const TopAbs_ShapeEnum type) 
{
  Standard_Integer nbt   = 0;
  Standard_Real tol, cmin = 0,cmoy = 0,cmax = 0;

  TopExp_Explorer myExp;

  // Iteration sur les Faces

  if (type == TopAbs_FACE || type == TopAbs_SHAPE) {
    myExp.Init(shape, TopAbs_FACE);
    while(myExp.More()) {
      tol = BRep_Tool::Tolerance(TopoDS::Face(myExp.Current()));
      AddTol (tol,nbt,cmin,cmoy,cmax);
      myExp.Next();
    }
  }

  // Iteration sur les Edges

  if (type == TopAbs_EDGE || type == TopAbs_SHAPE) {
    myExp.Init(shape, TopAbs_EDGE);
    while(myExp.More()) {
      tol = BRep_Tool::Tolerance(TopoDS::Edge(myExp.Current()));
      AddTol (tol,nbt,cmin,cmoy,cmax);
      myExp.Next();
    }
  }

  // Iteration sur les Vertices

  if (type == TopAbs_VERTEX || type == TopAbs_SHAPE) {
    myExp.Init(shape, TopAbs_VERTEX);
    while(myExp.More()) {
      tol = BRep_Tool::Tolerance(TopoDS::Vertex(myExp.Current()));
      AddTol (tol,nbt,cmin,cmoy,cmax);
      myExp.Next();
    }
  }

//  Resultat : attention en mode cumul
  if (nbt == 0) return;
  if (myNbTol == 0 || myTols[0] >  cmin) myTols[0] = cmin;
  if (myNbTol == 0 || myTols[2] <  cmax) myTols[2] = cmax;
  myTols[1] += cmoy;
  myNbTol   += nbt;
}

//=======================================================================
//function : GlobalTolerance
//purpose  : 
//=======================================================================

 Standard_Real ShapeAnalysis_ShapeTolerance::GlobalTolerance(const Standard_Integer mode) const
{
  //szv#4:S4163:12Mar99 optimized
  Standard_Real result = 0.;
  if (myNbTol != 0.) {
    if (mode < 0) result = myTols[0];
    else if (mode == 0) {
      if (myTols[0] == myTols[2]) result = myTols[0];
      else result =  myTols[1] / myNbTol;
    }
    else result = myTols[2];
  }

  return result;
}