1 // File: TopOpeBRepTool_faulty.cxx
2 // Created: Tue Nov 24 11:15:13 1998
3 // Author: Xuan PHAM PHU
4 // <xpu@poulopox.paris1.matra-dtv.fr>
6 #include <TopOpeBRepTool_define.hxx>
7 #include <TopOpeBRepTool_EXPORT.hxx>
8 #include <TopOpeBRepTool_2d.hxx>
9 #include <TopExp_Explorer.hxx>
10 #include <BRep_Tool.hxx>
11 #include <GeomAdaptor_Surface.hxx>
12 #include <TopTools_Array1OfShape.hxx>
16 #define M_FORWARD(sta) (sta == TopAbs_FORWARD)
17 #define M_REVERSED(sta) (sta == TopAbs_REVERSED)
18 #define M_INTERNAL(sta) (sta == TopAbs_INTERNAL)
19 #define M_EXTERNAL(sta) (sta == TopAbs_EXTERNAL)
22 #include <TopOpeBRepTool_DRAW.hxx>
26 //Standard_IMPORT extern TopTools_IndexedMapOfShape STATIC_PURGE_mapv;
27 Standard_IMPORT TopTools_IndexedMapOfShape STATIC_PURGE_mapv;
28 //Standard_IMPORT extern TopTools_IndexedMapOfOrientedShape STATIC_PURGE_mapeds;
29 Standard_IMPORT TopTools_IndexedMapOfOrientedShape STATIC_PURGE_mapeds;
30 Standard_IMPORT Standard_Boolean TopOpeBRepTool_GettracePURGE();
31 Standard_EXPORT void FUN_REINIT()
32 {STATIC_PURGE_mapv.Clear(); STATIC_PURGE_mapeds.Clear();}
33 static void FUN_RaiseError()
36 Standard_Boolean trc = TopOpeBRepTool_GettracePURGE();
37 // Standard_Failure::Raise("TopOpeBRepTool::PurgeClosingEdges");
39 if (trc) cout <<"*********failure in TopOpeBRepTool::PurgeClosingEdges***********\n";
42 Standard_EXPORT Standard_Integer FUN_addepc(const TopoDS_Shape& ed,const TopoDS_Shape& f){
43 Standard_Integer ie = STATIC_PURGE_mapeds.Add(ed);
45 // TCollection_AsciiString aa = TCollection_AsciiString("pc_"); FUN_tool_draw(aa,TopoDS::Edge(ed),TopoDS::Face(f),ie);
46 // TCollection_AsciiString bb = TCollection_AsciiString("ed_"); FUN_tool_draw(bb,ed,ie);
50 Standard_EXPORT Standard_Integer FUN_addcheckepc(const TopoDS_Shape& ed,const TopoDS_Shape& f) {
51 Standard_Integer ie = 0;
52 ie = STATIC_PURGE_mapeds.FindIndex(ed);
53 if (ie == 0) ie = FUN_addepc(ed,f);
56 /*Standard_EXPORT Standard_Integer FUN_adds(const TopoDS_Shape& s) {
57 TopAbs_ShapeEnum typ = s.ShapeType();
58 TCollection_AsciiString aa; Standard_Integer is;
59 if (typ == TopAbs_VERTEX) {aa = TCollection_AsciiString("v_"); is = STATIC_PURGE_mapv.Add(s);}
61 Standard_Boolean trc = TopOpeBRepTool_GettracePURGE();
62 if (trc) FUN_tool_draw(aa,s,is);
66 Standard_IMPORT Standard_Integer FUN_adds(const TopoDS_Shape& s);
67 Standard_EXPORT void FUN_tool_tori(const TopAbs_Orientation Or)
82 Standard_EXPORT void FUN_tool_trace(const Standard_Integer Index)
83 {if (Index == 1) cout <<"FORWARD ";
84 if (Index == 2) cout <<"REVERSED ";}
85 Standard_EXPORT void FUN_tool_trace(const gp_Pnt2d p2d)
86 {cout<<" = ("<<p2d.X()<<" "<<p2d.Y()<<")"<<endl;}
88 /*Standard_IMPORT Standard_Real FUN_tool_parOnE(const Standard_Integer Index,const TopoDS_Edge& E,const TopoDS_Face& F);
89 Standard_IMPORT void FUN_tool_addTomap
90 (const TopoDS_Shape& key,const TopoDS_Shape& elementOfitem,TopTools_DataMapOfShapeListOfShape& map,const Standard_Integer check);
91 Standard_IMPORT void FUN_tool_addTomap(const TopoDS_Shape& key,const TopoDS_Shape& elementOfitem,
92 TopTools_IndexedDataMapOfShapeListOfShape& map,const Standard_Integer check);
94 // ----------------------------------------------------------------------
95 Standard_EXPORT gp_Pnt2d FUN_GetVParonF(const TopoDS_Edge& E, const TopoDS_Face& F, const Standard_Integer Index)
96 // ----------------------------------------------------------------------
98 Standard_Real par = FUN_tool_parOnE(Index, E, F);
99 Standard_Real pf,pl,tol; Handle(Geom2d_Curve) PC = FUNTOOLC2D_CurveOnSurface(E,F,pf,pl,tol);
100 if (PC.IsNull()) Standard_Failure::Raise(" FUN_GetVParonF : no 2d curve");
101 gp_Pnt2d p2d; PC->D0(par,p2d);
105 // ----------------------------------------------------------------------
106 Standard_EXPORT Standard_Real FUN_toluv(const GeomAdaptor_Surface& GAS, const Standard_Real& tol3d)
107 // ----------------------------------------------------------------------
109 Standard_Real tolu = GAS.UResolution(tol3d);
110 Standard_Real tolv = GAS.VResolution(tol3d);
111 Standard_Real toluv = Max(tolu,tolv);
115 // ----------------------------------------------------------------------
116 Standard_EXPORT void FUN_tool_Vertices(const TopoDS_Edge& E, TopTools_Array1OfShape& vertices)
117 // ----------------------------------------------------------------------
119 // Returns vertices (F,R) if E is FORWARD
120 // (R,V) if E is REVERSED
121 TopAbs_Orientation oriE = E.Orientation();
122 TopoDS_Vertex vF, vR; TopExp::Vertices(E,vF,vR);
123 Standard_Integer iF, iR;
124 if (oriE == TopAbs_FORWARD) {iF = 1; iR = 2;}
125 else {iF = 2; iR = 1;}
126 vertices.ChangeValue(iF) = vF;
127 vertices.ChangeValue(iR) = vR;
129 // ----------------------------------------------------------------------
130 Standard_EXPORT void FUN_mapVloe(const TopoDS_Shape& F, TopTools_IndexedDataMapOfShapeListOfShape& mapVloe)
131 // ----------------------------------------------------------------------
134 // TopExp::MapShapesAndAncestors(F,TopAbs_VERTEX,TopAbs_EDGE,mapVloe);
135 TopExp_Explorer exe(F, TopAbs_EDGE);
136 for (; exe.More(); exe.Next()){
137 const TopoDS_Shape e = exe.Current();
138 TopExp_Explorer exv(e, TopAbs_VERTEX);
139 for (; exv.More(); exv.Next()){
140 const TopoDS_Shape& v = exv.Current();
141 FUN_tool_addTomap(v, e, mapVloe, 0);
146 //======================================================================
148 /*Standard_EXPORT Standard_Boolean FUN_DetectEdgeswithfaultyUV
149 (const TopoDS_Face& Fin, const TopoDS_Shape& fF, const TopTools_ListOfShape& ISOEds,
150 const Standard_Boolean has2fybounds, TopTools_ListOfShape& lfyE, Standard_Integer& Ivfaulty,
151 const Standard_Boolean& stopatfirst=Standard_False)
152 // purpose : finding out edges <lfyE> / its UV rep. on <Fin>
153 // is unconnected to the other pcurves.
154 // if (has2fybounds), the 2 bounds of the pcurve are faulty.
155 // else : Ivfaulty is index of faulty vertex.
156 // return True if at least one edge is faulty
158 // <ISOEds> : list of pcurves to check among <fF>'s 2d rep. edges.
159 // <ISOEds> are edges of <fF>
162 Ivfaulty = 0; // dummy if has2faultybounds
163 TopTools_ListOfShape Eds;
164 TopExp_Explorer exe(fF, TopAbs_EDGE);
165 for (; exe.More(); exe.Next()) Eds.Append(exe.Current());
169 // key = a vertex boundary of a closing edge,
170 // item = the list of edges connexed to the vertex.
171 TopTools_IndexedDataMapOfShapeListOfShape mapVloe; ::FUN_mapVloe(fF,mapVloe);
173 const Handle(Geom_Surface)& SU = BRep_Tool::Surface(Fin);
174 GeomAdaptor_Surface GAS(SU);
175 Standard_Real tttol = 1.e-8;
176 Standard_Real tttolF = BRep_Tool::Tolerance(Fin);
177 Standard_Real tttuvF = FUN_toluv(GAS,tttolF);
181 // An edge is valid if the first and last vertices are valid:
182 // vertex <vISOe> is valid if there is an edge with bound <ve> such that :
183 // <vISOe> and <ve> share same UV geometry
184 // <vISOe> and <ve> are of opposite orientation.
185 TopTools_ListIteratorOfListOfShape itISOEd(ISOEds);
186 for (; itISOEd.More(); itISOEd.Next()) {
187 const TopoDS_Edge& ISOe = TopoDS::Edge(itISOEd.Value());
188 TopAbs_Orientation oriISO = ISOe.Orientation();
189 if (M_INTERNAL(oriISO) || M_EXTERNAL(oriISO)) continue;
191 Standard_Real tttolISOe = BRep_Tool::Tolerance(ISOe);
192 Standard_Real tttuvISOe = FUN_toluv(GAS,tttolISOe);
194 TopTools_Array1OfShape vISOE(1,2); FUN_tool_Vertices(ISOe, vISOE);
196 Standard_Boolean closed = vISOE(1).IsSame(vISOE(2));
197 if (closed) continue; // closed edge is ass
199 Standard_Integer nfyv = 0;
200 for (Standard_Integer ivISOe = 1; ivISOe <=2; ivISOe++) {
202 // <vISOe> (boundary of <ISOe>):
203 const TopoDS_Vertex& vISOe = TopoDS::Vertex(vISOE(ivISOe));
204 gp_Pnt2d UVvISOe = FUN_GetVParonF(ISOe, Fin, ivISOe);
206 Standard_Real tttolvISOe = BRep_Tool::Tolerance(vISOe);
207 Standard_Real tttuvvISOe = FUN_toluv(GAS,tttolvISOe);
209 Standard_Boolean isbound = mapVloe.Contains(vISOe);
210 if (!isbound) {FUN_RaiseError(); return Standard_False;}
213 Standard_Boolean vISOeok = Standard_False;
214 const TopTools_ListOfShape& loe = mapVloe.FindFromKey(vISOe);
215 Standard_Integer nloe = loe.Extent();
216 for (TopTools_ListIteratorOfListOfShape ite(loe); ite.More(); ite.Next()) {
217 const TopoDS_Edge& E = TopoDS::Edge(ite.Value());
218 if (E.IsSame(ISOe)) continue;
219 Standard_Real tttolE = BRep_Tool::Tolerance(E);
220 Standard_Real tttuvE = FUN_toluv(GAS,tttolE);
222 TopTools_Array1OfShape vE(1,2); FUN_tool_Vertices(E,vE);
223 for (Standard_Integer ive = 1; ive <=2; ive++) {
224 const TopoDS_Vertex& ve = TopoDS::Vertex(vE(ive));
225 Standard_Boolean samev = ve.IsSame(vISOe);
226 if (!samev) continue;
228 if (ive == ivISOe) continue;
229 gp_Pnt2d UVve = FUN_GetVParonF(E, Fin, ive);
231 Standard_Real tttolve = BRep_Tool::Tolerance(ve);
232 Standard_Real tttuvve = FUN_toluv(GAS,tttolve);
234 tttol = Max(tttol,Max(tttuvF,Max(tttuvE,Max(tttuvISOe,Max(tttuvve,tttuvvISOe)))));
235 Standard_Boolean isequal = UVvISOe.IsEqual(UVve,tttol);
236 if (isequal) {vISOeok = Standard_True; break;}
241 if (!vISOeok) nfyv++;
243 Standard_Boolean stop = (!has2fybounds && (nfyv > 0));
245 if (Ivfaulty == 0) Ivfaulty = ivISOe;//xpu121098 (cto900J4,f4ou) : first faulty edge
250 Standard_Boolean found = has2fybounds && (nfyv == 2);
251 found = found || (!has2fybounds && (nfyv == 1));
257 Standard_Integer n = lfyE.Extent();
258 Standard_Boolean det = (n != 0);
260 } // FUN_DetectEdgeswithfaultyUV
262 Standard_EXPORT Standard_Boolean FUN_DetectEdgewithfaultyUV
263 (const TopoDS_Face& Fin, const TopoDS_Shape& fF, const TopTools_ListOfShape& ISOEds,
264 const Standard_Boolean has2fybounds, TopoDS_Shape& fyE, Standard_Integer& Ivfaulty)
265 // purpose : finding out first edge <fyE> / its UV rep. on <Fin>
266 // is unconnected to the other pcurves.
267 // if (has2fybounds), the 2 bounds of the pcurve are faulty.
268 // else : Ivfaulty is index of faulty vertex.
269 // return True if at least one edge is faulty
271 // <ISOEds> : list of pcurves to check among <fF>'s 2d rep. edges.
272 // <ISOEds> are edges of <fF>
273 TopTools_ListOfShape lfyE;
274 Standard_Boolean det = FUN_DetectEdgeswithfaultyUV(Fin,fF,ISOEds,has2fybounds,lfyE,Ivfaulty);
275 if (det) fyE = lfyE.First();
279 // ----------------------------------------------------------------------
280 Standard_EXPORT Standard_Boolean FUN_DetectFaultyClosingEdge
281 (const TopoDS_Face& Fin,const TopTools_ListOfShape& Eds,const TopTools_ListOfShape& cEds,TopTools_ListOfShape& fyE)
282 // ----------------------------------------------------------------------
285 Standard_Boolean trc = TopOpeBRepTool_GettracePURGE();
286 for (TopTools_ListIteratorOfListOfShape it(Eds); it.More(); it.Next()){
287 const TopoDS_Shape& ed = it.Value();
289 for (ex.Init(ed, TopAbs_VERTEX); ex.More(); ex.Next())
290 // for (TopExp_Explorer ex(ed, TopAbs_VERTEX); ex.More(); ex.Next())
291 FUN_adds(ex.Current());}
294 // <cEds> and <Eds> are the lists of closed and non-"closing" edges
295 // describing a wire boundary on face <Fin>.
296 // ("closing" edge = edge built on the geometry of a closing edge of <Fin> )
298 // If <W> has UV non-connexed edges, returns <True> if <fyE> is the list of faulty cEds
299 // fyE = {e / e has its 2 bounds non-UVconnexed}
300 // Returns <False> elsewhere.
301 // prequesitory : wire <W> is 3d-connexed, so a faulty edge must be a
306 // key = a vertex boundary of a closing edge,
307 // item = the list of edges connexed to the vertex.
308 TopTools_DataMapOfShapeListOfShape mapVloe;
309 for (TopTools_ListIteratorOfListOfShape itEd(Eds); itEd.More(); itEd.Next()) {
310 const TopoDS_Edge& E = TopoDS::Edge(itEd.Value());
312 for (ex.Init(E, TopAbs_VERTEX); ex.More(); ex.Next()) {
313 // for (TopExp_Explorer ex(E, TopAbs_VERTEX); ex.More(); ex.Next()) {
314 const TopoDS_Shape& v = ex.Current();
315 FUN_tool_addTomap(v, E, mapVloe, 0);
318 const Handle(Geom_Surface)& SU = BRep_Tool::Surface(TopoDS::Face(Fin));
319 GeomAdaptor_Surface GAS(SU);
320 Standard_Real tttol = 1.e-8;
321 Standard_Real tttolF = BRep_Tool::Tolerance(TopoDS::Face(Fin));
322 Standard_Real tttuvF = FUN_toluv(GAS,tttolF);
324 Standard_Boolean uclosed,vclosed; Standard_Real uperiod,vperiod;
325 Standard_Boolean uvclosed = FUN_tool_closedS(Fin,uclosed,uperiod,vclosed,vperiod);
329 // An edge is valid if the first and last vertices are valid:
330 // vertex <vce> is valid if there is an edge with bound <ve> verifying :
331 // <vce> and <ve> share same UV geometry
332 // <vce> and <ve> are of opposite orientation.
333 TopTools_ListIteratorOfListOfShape itCEd(cEds);
334 for (; itCEd.More(); itCEd.Next()) {
335 const TopoDS_Edge& cE = TopoDS::Edge(itCEd.Value());
336 Standard_Boolean isou,isov; gp_Pnt2d o2d; gp_Dir2d d2d;
337 Standard_Real f,l,tol; Handle(Geom2d_Curve) PC = FUNTOOLC2D_CurveOnSurface(cE,Fin,f,l,tol);
338 Standard_Boolean isouv = FUN_tool_IsUViso(PC,isou,isov,d2d,o2d);
341 Standard_Integer icE = FUN_addcheckepc(cE,Fin);
342 if (trc) {cout<<"closing edge "<<icE<<" "; TopAbs::Print(cE.Orientation(),cout);
346 Standard_Real tttolcE = BRep_Tool::Tolerance(cE);
347 Standard_Real tttuvcE = FUN_toluv(GAS,tttolcE);
349 Standard_Boolean cEKO = Standard_True;
350 TopTools_Array1OfShape vcE(1,2); FUN_tool_Vertices(cE, vcE);
351 for (Standard_Integer ivce = 1; ivce <=2; ivce++) {
352 // <vce> (boundary of <cE>):
353 const TopoDS_Vertex& vce = TopoDS::Vertex(vcE(ivce));
354 gp_Pnt2d UVvce = FUN_GetVParonF(cE, Fin, ivce);
356 // recall in one wire, there are 2 vertices for one non-degenerated closing edge
357 Standard_Integer ivmapv = STATIC_PURGE_mapv.Add(vce);
359 cout<<"v"<<ivmapv<<" ";FUN_tool_trace(ivce);cout<<" in closing edge "<<icE;
360 FUN_tool_trace(UVvce);
362 TCollection_AsciiString bb("uv_");bb += TCollection_AsciiString(ivmapv);FUN_tool_draw(bb,UVvce);
367 Standard_Real tttolvce = BRep_Tool::Tolerance(vce);
368 Standard_Real tttuvvce = FUN_toluv(GAS,tttolvce);
370 Standard_Boolean isbound = mapVloe.IsBound(vce);
371 if (!isbound) continue; //xpu201198 cto016E2 (e3on)
374 Standard_Boolean vceok = Standard_False;
375 const TopTools_ListOfShape& loe = mapVloe.Find(vce);
376 Standard_Integer nloe = loe.Extent();
378 Standard_Boolean tdr = Standard_False;
380 Standard_Integer inde = 1;
381 for (TopTools_ListIteratorOfListOfShape ite(loe); ite.More(); ite.Next()) {
382 const TopoDS_Shape& eee = ite.Value(); FUN_drawe(eee,inde); inde++;
386 for (TopTools_ListIteratorOfListOfShape ite(loe); ite.More(); ite.Next()) {
387 const TopoDS_Edge& E = TopoDS::Edge(ite.Value());
388 // if (E.IsSame(cE)) continue; // closing FORWARD and REVERSED EDGES are UV disjoint
390 Standard_Integer iE = FUN_addcheckepc(E,Fin);
393 Standard_Real tttolE = BRep_Tool::Tolerance(E);
394 Standard_Real tttuvE = FUN_toluv(GAS,tttolE);
396 TopTools_Array1OfShape vE(1,2); FUN_tool_Vertices(E,vE);
397 for (Standard_Integer ive = 1; ive <=2; ive++) {
398 const TopoDS_Vertex& ve = TopoDS::Vertex(vE(ive));
399 Standard_Boolean samev = ve.IsSame(vce);
400 if (!samev) continue;
402 if (ive == ivce) continue;
403 gp_Pnt2d UVve = FUN_GetVParonF(E, Fin, ive);
405 Standard_Real tttolve = BRep_Tool::Tolerance(ve);
406 Standard_Real tttuvve = FUN_toluv(GAS,tttolve);
408 tttol = Max(tttol,Max(tttuvF,Max(tttuvE,Max(tttuvcE,Max(tttuvve,tttuvvce)))));
409 // Standard_Boolean isequal = UVvce.IsEqual(UVve,tttol);
410 Standard_Real dd=0., xperiod=0.;
411 if (uclosed && isou) {dd = UVve.X()-UVvce.X(); xperiod = uperiod;}
412 if (vclosed && isov) {dd = UVve.Y()-UVvce.Y(); xperiod = vperiod;}
413 Standard_Boolean xok = (Abs(dd)<tttol) || (Abs(Abs(dd)-xperiod)<tttol);
414 Standard_Boolean isequal = xok;
417 vceok = Standard_True;
420 {cout<<"vertex ";FUN_tool_trace(ivce);cout<<" of closing edge "<<icE;
421 cout<<" is connexed to vertex ";FUN_tool_trace(ive);cout<<" of edge "<<iE<<endl;}
429 if (trc && !vceok) {cout<<" vertex ";FUN_tool_trace(ivce);
430 cout<<"of closing edge "<<icE<<" is faulty"<<endl;}
432 cEKO = cEKO && (!vceok);
436 if (trc) {if (!cEKO) cout<<"-> valid edge"<<endl;
437 else cout<<"-> faulty edge"<<endl;}
439 if (cEKO) {fyE.Append(cE);}
441 return (!fyE.IsEmpty());
444 Standard_EXPORT Standard_Boolean FUN_isUVClosed(const TopoDS_Face& Fin, const TopoDS_Face& fF)
445 // purpose : returns true is fF is UV connexed
447 TopTools_ListOfShape efF;
448 TopExp_Explorer ex(fF, TopAbs_EDGE);
449 for (; ex.More(); ex.Next()) efF.Append(ex.Current());
450 Standard_Integer Ivfaulty; TopTools_ListOfShape lfyE; Standard_Boolean stopatfirst = Standard_True;
451 Standard_Boolean foundfaulty = FUN_DetectEdgeswithfaultyUV(Fin,fF,efF,Standard_False,lfyE,Ivfaulty,stopatfirst);