1 // Created on: 1998-11-24
2 // Created by: Xuan PHAM PHU
3 // Copyright (c) 1998-1999 Matra Datavision
4 // Copyright (c) 1999-2012 OPEN CASCADE SAS
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.
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.
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.
22 #include <TopOpeBRepTool_define.hxx>
23 #include <TopOpeBRepTool_EXPORT.hxx>
24 #include <TopOpeBRepTool_2d.hxx>
25 #include <TopExp_Explorer.hxx>
26 #include <BRep_Tool.hxx>
27 #include <GeomAdaptor_Surface.hxx>
28 #include <TopTools_Array1OfShape.hxx>
32 #define M_FORWARD(sta) (sta == TopAbs_FORWARD)
33 #define M_REVERSED(sta) (sta == TopAbs_REVERSED)
34 #define M_INTERNAL(sta) (sta == TopAbs_INTERNAL)
35 #define M_EXTERNAL(sta) (sta == TopAbs_EXTERNAL)
38 #include <TopOpeBRepTool_DRAW.hxx>
42 extern TopTools_IndexedMapOfShape STATIC_PURGE_mapv;
43 extern TopTools_IndexedMapOfOrientedShape STATIC_PURGE_mapeds;
44 extern Standard_Boolean TopOpeBRepTool_GettracePURGE();
45 Standard_EXPORT void FUN_REINIT()
46 {STATIC_PURGE_mapv.Clear(); STATIC_PURGE_mapeds.Clear();}
47 static void FUN_RaiseError()
50 Standard_Boolean trc = TopOpeBRepTool_GettracePURGE();
51 // Standard_Failure::Raise("TopOpeBRepTool::PurgeClosingEdges");
53 if (trc) cout <<"*********failure in TopOpeBRepTool::PurgeClosingEdges***********\n";
56 Standard_EXPORT Standard_Integer FUN_addepc(const TopoDS_Shape& ed,const TopoDS_Shape& f){
57 Standard_Integer ie = STATIC_PURGE_mapeds.Add(ed);
59 // TCollection_AsciiString aa = TCollection_AsciiString("pc_"); FUN_tool_draw(aa,TopoDS::Edge(ed),TopoDS::Face(f),ie);
60 // TCollection_AsciiString bb = TCollection_AsciiString("ed_"); FUN_tool_draw(bb,ed,ie);
64 Standard_EXPORT Standard_Integer FUN_addcheckepc(const TopoDS_Shape& ed,const TopoDS_Shape& f) {
65 Standard_Integer ie = 0;
66 ie = STATIC_PURGE_mapeds.FindIndex(ed);
67 if (ie == 0) ie = FUN_addepc(ed,f);
70 /*Standard_EXPORT Standard_Integer FUN_adds(const TopoDS_Shape& s) {
71 TopAbs_ShapeEnum typ = s.ShapeType();
72 TCollection_AsciiString aa; Standard_Integer is;
73 if (typ == TopAbs_VERTEX) {aa = TCollection_AsciiString("v_"); is = STATIC_PURGE_mapv.Add(s);}
75 Standard_Boolean trc = TopOpeBRepTool_GettracePURGE();
76 if (trc) FUN_tool_draw(aa,s,is);
80 Standard_IMPORT Standard_Integer FUN_adds(const TopoDS_Shape& s);
81 Standard_EXPORT void FUN_tool_tori(const TopAbs_Orientation Or)
96 Standard_EXPORT void FUN_tool_trace(const Standard_Integer Index)
97 {if (Index == 1) cout <<"FORWARD ";
98 if (Index == 2) cout <<"REVERSED ";}
99 Standard_EXPORT void FUN_tool_trace(const gp_Pnt2d p2d)
100 {cout<<" = ("<<p2d.X()<<" "<<p2d.Y()<<")"<<endl;}
102 /*Standard_IMPORT Standard_Real FUN_tool_parOnE(const Standard_Integer Index,const TopoDS_Edge& E,const TopoDS_Face& F);
103 Standard_IMPORT void FUN_tool_addTomap
104 (const TopoDS_Shape& key,const TopoDS_Shape& elementOfitem,TopTools_DataMapOfShapeListOfShape& map,const Standard_Integer check);
105 Standard_IMPORT void FUN_tool_addTomap(const TopoDS_Shape& key,const TopoDS_Shape& elementOfitem,
106 TopTools_IndexedDataMapOfShapeListOfShape& map,const Standard_Integer check);
108 // ----------------------------------------------------------------------
109 Standard_EXPORT gp_Pnt2d FUN_GetVParonF(const TopoDS_Edge& E, const TopoDS_Face& F, const Standard_Integer Index)
110 // ----------------------------------------------------------------------
112 Standard_Real par = FUN_tool_parOnE(Index, E, F);
113 Standard_Real pf,pl,tol; Handle(Geom2d_Curve) PC = FUNTOOLC2D_CurveOnSurface(E,F,pf,pl,tol);
114 if (PC.IsNull()) Standard_Failure::Raise(" FUN_GetVParonF : no 2d curve");
115 gp_Pnt2d p2d; PC->D0(par,p2d);
119 // ----------------------------------------------------------------------
120 Standard_EXPORT Standard_Real FUN_toluv(const GeomAdaptor_Surface& GAS, const Standard_Real& tol3d)
121 // ----------------------------------------------------------------------
123 Standard_Real tolu = GAS.UResolution(tol3d);
124 Standard_Real tolv = GAS.VResolution(tol3d);
125 Standard_Real toluv = Max(tolu,tolv);
129 // ----------------------------------------------------------------------
130 Standard_EXPORT void FUN_tool_Vertices(const TopoDS_Edge& E, TopTools_Array1OfShape& vertices)
131 // ----------------------------------------------------------------------
133 // Returns vertices (F,R) if E is FORWARD
134 // (R,V) if E is REVERSED
135 TopAbs_Orientation oriE = E.Orientation();
136 TopoDS_Vertex vF, vR; TopExp::Vertices(E,vF,vR);
137 Standard_Integer iF, iR;
138 if (oriE == TopAbs_FORWARD) {iF = 1; iR = 2;}
139 else {iF = 2; iR = 1;}
140 vertices.ChangeValue(iF) = vF;
141 vertices.ChangeValue(iR) = vR;
143 // ----------------------------------------------------------------------
144 Standard_EXPORT void FUN_mapVloe(const TopoDS_Shape& F, TopTools_IndexedDataMapOfShapeListOfShape& mapVloe)
145 // ----------------------------------------------------------------------
148 // TopExp::MapShapesAndAncestors(F,TopAbs_VERTEX,TopAbs_EDGE,mapVloe);
149 TopExp_Explorer exe(F, TopAbs_EDGE);
150 for (; exe.More(); exe.Next()){
151 const TopoDS_Shape e = exe.Current();
152 TopExp_Explorer exv(e, TopAbs_VERTEX);
153 for (; exv.More(); exv.Next()){
154 const TopoDS_Shape& v = exv.Current();
155 FUN_tool_addTomap(v, e, mapVloe, 0);
160 //======================================================================
162 /*Standard_EXPORT Standard_Boolean FUN_DetectEdgeswithfaultyUV
163 (const TopoDS_Face& Fin, const TopoDS_Shape& fF, const TopTools_ListOfShape& ISOEds,
164 const Standard_Boolean has2fybounds, TopTools_ListOfShape& lfyE, Standard_Integer& Ivfaulty,
165 const Standard_Boolean& stopatfirst=Standard_False)
166 // purpose : finding out edges <lfyE> / its UV rep. on <Fin>
167 // is unconnected to the other pcurves.
168 // if (has2fybounds), the 2 bounds of the pcurve are faulty.
169 // else : Ivfaulty is index of faulty vertex.
170 // return True if at least one edge is faulty
172 // <ISOEds> : list of pcurves to check among <fF>'s 2d rep. edges.
173 // <ISOEds> are edges of <fF>
176 Ivfaulty = 0; // dummy if has2faultybounds
177 TopTools_ListOfShape Eds;
178 TopExp_Explorer exe(fF, TopAbs_EDGE);
179 for (; exe.More(); exe.Next()) Eds.Append(exe.Current());
183 // key = a vertex boundary of a closing edge,
184 // item = the list of edges connexed to the vertex.
185 TopTools_IndexedDataMapOfShapeListOfShape mapVloe; ::FUN_mapVloe(fF,mapVloe);
187 const Handle(Geom_Surface)& SU = BRep_Tool::Surface(Fin);
188 GeomAdaptor_Surface GAS(SU);
189 Standard_Real tttol = 1.e-8;
190 Standard_Real tttolF = BRep_Tool::Tolerance(Fin);
191 Standard_Real tttuvF = FUN_toluv(GAS,tttolF);
195 // An edge is valid if the first and last vertices are valid:
196 // vertex <vISOe> is valid if there is an edge with bound <ve> such that :
197 // <vISOe> and <ve> share same UV geometry
198 // <vISOe> and <ve> are of opposite orientation.
199 TopTools_ListIteratorOfListOfShape itISOEd(ISOEds);
200 for (; itISOEd.More(); itISOEd.Next()) {
201 const TopoDS_Edge& ISOe = TopoDS::Edge(itISOEd.Value());
202 TopAbs_Orientation oriISO = ISOe.Orientation();
203 if (M_INTERNAL(oriISO) || M_EXTERNAL(oriISO)) continue;
205 Standard_Real tttolISOe = BRep_Tool::Tolerance(ISOe);
206 Standard_Real tttuvISOe = FUN_toluv(GAS,tttolISOe);
208 TopTools_Array1OfShape vISOE(1,2); FUN_tool_Vertices(ISOe, vISOE);
210 Standard_Boolean closed = vISOE(1).IsSame(vISOE(2));
211 if (closed) continue; // closed edge is ass
213 Standard_Integer nfyv = 0;
214 for (Standard_Integer ivISOe = 1; ivISOe <=2; ivISOe++) {
216 // <vISOe> (boundary of <ISOe>):
217 const TopoDS_Vertex& vISOe = TopoDS::Vertex(vISOE(ivISOe));
218 gp_Pnt2d UVvISOe = FUN_GetVParonF(ISOe, Fin, ivISOe);
220 Standard_Real tttolvISOe = BRep_Tool::Tolerance(vISOe);
221 Standard_Real tttuvvISOe = FUN_toluv(GAS,tttolvISOe);
223 Standard_Boolean isbound = mapVloe.Contains(vISOe);
224 if (!isbound) {FUN_RaiseError(); return Standard_False;}
227 Standard_Boolean vISOeok = Standard_False;
228 const TopTools_ListOfShape& loe = mapVloe.FindFromKey(vISOe);
229 Standard_Integer nloe = loe.Extent();
230 for (TopTools_ListIteratorOfListOfShape ite(loe); ite.More(); ite.Next()) {
231 const TopoDS_Edge& E = TopoDS::Edge(ite.Value());
232 if (E.IsSame(ISOe)) continue;
233 Standard_Real tttolE = BRep_Tool::Tolerance(E);
234 Standard_Real tttuvE = FUN_toluv(GAS,tttolE);
236 TopTools_Array1OfShape vE(1,2); FUN_tool_Vertices(E,vE);
237 for (Standard_Integer ive = 1; ive <=2; ive++) {
238 const TopoDS_Vertex& ve = TopoDS::Vertex(vE(ive));
239 Standard_Boolean samev = ve.IsSame(vISOe);
240 if (!samev) continue;
242 if (ive == ivISOe) continue;
243 gp_Pnt2d UVve = FUN_GetVParonF(E, Fin, ive);
245 Standard_Real tttolve = BRep_Tool::Tolerance(ve);
246 Standard_Real tttuvve = FUN_toluv(GAS,tttolve);
248 tttol = Max(tttol,Max(tttuvF,Max(tttuvE,Max(tttuvISOe,Max(tttuvve,tttuvvISOe)))));
249 Standard_Boolean isequal = UVvISOe.IsEqual(UVve,tttol);
250 if (isequal) {vISOeok = Standard_True; break;}
255 if (!vISOeok) nfyv++;
257 Standard_Boolean stop = (!has2fybounds && (nfyv > 0));
259 if (Ivfaulty == 0) Ivfaulty = ivISOe;//xpu121098 (cto900J4,f4ou) : first faulty edge
264 Standard_Boolean found = has2fybounds && (nfyv == 2);
265 found = found || (!has2fybounds && (nfyv == 1));
271 Standard_Integer n = lfyE.Extent();
272 Standard_Boolean det = (n != 0);
274 } // FUN_DetectEdgeswithfaultyUV
276 Standard_EXPORT Standard_Boolean FUN_DetectEdgewithfaultyUV
277 (const TopoDS_Face& Fin, const TopoDS_Shape& fF, const TopTools_ListOfShape& ISOEds,
278 const Standard_Boolean has2fybounds, TopoDS_Shape& fyE, Standard_Integer& Ivfaulty)
279 // purpose : finding out first edge <fyE> / its UV rep. on <Fin>
280 // is unconnected to the other pcurves.
281 // if (has2fybounds), the 2 bounds of the pcurve are faulty.
282 // else : Ivfaulty is index of faulty vertex.
283 // return True if at least one edge is faulty
285 // <ISOEds> : list of pcurves to check among <fF>'s 2d rep. edges.
286 // <ISOEds> are edges of <fF>
287 TopTools_ListOfShape lfyE;
288 Standard_Boolean det = FUN_DetectEdgeswithfaultyUV(Fin,fF,ISOEds,has2fybounds,lfyE,Ivfaulty);
289 if (det) fyE = lfyE.First();
293 // ----------------------------------------------------------------------
294 Standard_EXPORT Standard_Boolean FUN_DetectFaultyClosingEdge
295 (const TopoDS_Face& Fin,const TopTools_ListOfShape& Eds,const TopTools_ListOfShape& cEds,TopTools_ListOfShape& fyE)
296 // ----------------------------------------------------------------------
299 Standard_Boolean trc = TopOpeBRepTool_GettracePURGE();
300 for (TopTools_ListIteratorOfListOfShape it(Eds); it.More(); it.Next()){
301 const TopoDS_Shape& ed = it.Value();
303 for (ex.Init(ed, TopAbs_VERTEX); ex.More(); ex.Next())
304 // for (TopExp_Explorer ex(ed, TopAbs_VERTEX); ex.More(); ex.Next())
305 FUN_adds(ex.Current());}
308 // <cEds> and <Eds> are the lists of closed and non-"closing" edges
309 // describing a wire boundary on face <Fin>.
310 // ("closing" edge = edge built on the geometry of a closing edge of <Fin> )
312 // If <W> has UV non-connexed edges, returns <True> if <fyE> is the list of faulty cEds
313 // fyE = {e / e has its 2 bounds non-UVconnexed}
314 // Returns <False> elsewhere.
315 // prequesitory : wire <W> is 3d-connexed, so a faulty edge must be a
320 // key = a vertex boundary of a closing edge,
321 // item = the list of edges connexed to the vertex.
322 TopTools_DataMapOfShapeListOfShape mapVloe;
323 for (TopTools_ListIteratorOfListOfShape itEd(Eds); itEd.More(); itEd.Next()) {
324 const TopoDS_Edge& E = TopoDS::Edge(itEd.Value());
326 for (ex.Init(E, TopAbs_VERTEX); ex.More(); ex.Next()) {
327 // for (TopExp_Explorer ex(E, TopAbs_VERTEX); ex.More(); ex.Next()) {
328 const TopoDS_Shape& v = ex.Current();
329 FUN_tool_addTomap(v, E, mapVloe, 0);
332 const Handle(Geom_Surface)& SU = BRep_Tool::Surface(TopoDS::Face(Fin));
333 GeomAdaptor_Surface GAS(SU);
334 Standard_Real tttol = 1.e-8;
335 Standard_Real tttolF = BRep_Tool::Tolerance(TopoDS::Face(Fin));
336 Standard_Real tttuvF = FUN_toluv(GAS,tttolF);
338 Standard_Boolean uclosed,vclosed; Standard_Real uperiod,vperiod;
339 Standard_Boolean uvclosed = FUN_tool_closedS(Fin,uclosed,uperiod,vclosed,vperiod);
343 // An edge is valid if the first and last vertices are valid:
344 // vertex <vce> is valid if there is an edge with bound <ve> verifying :
345 // <vce> and <ve> share same UV geometry
346 // <vce> and <ve> are of opposite orientation.
347 TopTools_ListIteratorOfListOfShape itCEd(cEds);
348 for (; itCEd.More(); itCEd.Next()) {
349 const TopoDS_Edge& cE = TopoDS::Edge(itCEd.Value());
350 Standard_Boolean isou,isov; gp_Pnt2d o2d; gp_Dir2d d2d;
351 Standard_Real f,l,tol; Handle(Geom2d_Curve) PC = FUNTOOLC2D_CurveOnSurface(cE,Fin,f,l,tol);
352 Standard_Boolean isouv = FUN_tool_IsUViso(PC,isou,isov,d2d,o2d);
355 Standard_Integer icE = FUN_addcheckepc(cE,Fin);
356 if (trc) {cout<<"closing edge "<<icE<<" "; TopAbs::Print(cE.Orientation(),cout);
360 Standard_Real tttolcE = BRep_Tool::Tolerance(cE);
361 Standard_Real tttuvcE = FUN_toluv(GAS,tttolcE);
363 Standard_Boolean cEKO = Standard_True;
364 TopTools_Array1OfShape vcE(1,2); FUN_tool_Vertices(cE, vcE);
365 for (Standard_Integer ivce = 1; ivce <=2; ivce++) {
366 // <vce> (boundary of <cE>):
367 const TopoDS_Vertex& vce = TopoDS::Vertex(vcE(ivce));
368 gp_Pnt2d UVvce = FUN_GetVParonF(cE, Fin, ivce);
370 // recall in one wire, there are 2 vertices for one non-degenerated closing edge
371 Standard_Integer ivmapv = STATIC_PURGE_mapv.Add(vce);
373 cout<<"v"<<ivmapv<<" ";FUN_tool_trace(ivce);cout<<" in closing edge "<<icE;
374 FUN_tool_trace(UVvce);
376 TCollection_AsciiString bb("uv_");bb += TCollection_AsciiString(ivmapv);FUN_tool_draw(bb,UVvce);
381 Standard_Real tttolvce = BRep_Tool::Tolerance(vce);
382 Standard_Real tttuvvce = FUN_toluv(GAS,tttolvce);
384 Standard_Boolean isbound = mapVloe.IsBound(vce);
385 if (!isbound) continue; //xpu201198 cto016E2 (e3on)
388 Standard_Boolean vceok = Standard_False;
389 const TopTools_ListOfShape& loe = mapVloe.Find(vce);
390 Standard_Integer nloe = loe.Extent();
392 Standard_Boolean tdr = Standard_False;
394 Standard_Integer inde = 1;
395 for (TopTools_ListIteratorOfListOfShape ite(loe); ite.More(); ite.Next()) {
396 const TopoDS_Shape& eee = ite.Value(); FUN_drawe(eee,inde); inde++;
400 for (TopTools_ListIteratorOfListOfShape ite(loe); ite.More(); ite.Next()) {
401 const TopoDS_Edge& E = TopoDS::Edge(ite.Value());
402 // if (E.IsSame(cE)) continue; // closing FORWARD and REVERSED EDGES are UV disjoint
404 Standard_Integer iE = FUN_addcheckepc(E,Fin);
407 Standard_Real tttolE = BRep_Tool::Tolerance(E);
408 Standard_Real tttuvE = FUN_toluv(GAS,tttolE);
410 TopTools_Array1OfShape vE(1,2); FUN_tool_Vertices(E,vE);
411 for (Standard_Integer ive = 1; ive <=2; ive++) {
412 const TopoDS_Vertex& ve = TopoDS::Vertex(vE(ive));
413 Standard_Boolean samev = ve.IsSame(vce);
414 if (!samev) continue;
416 if (ive == ivce) continue;
417 gp_Pnt2d UVve = FUN_GetVParonF(E, Fin, ive);
419 Standard_Real tttolve = BRep_Tool::Tolerance(ve);
420 Standard_Real tttuvve = FUN_toluv(GAS,tttolve);
422 tttol = Max(tttol,Max(tttuvF,Max(tttuvE,Max(tttuvcE,Max(tttuvve,tttuvvce)))));
423 // Standard_Boolean isequal = UVvce.IsEqual(UVve,tttol);
424 Standard_Real dd=0., xperiod=0.;
425 if (uclosed && isou) {dd = UVve.X()-UVvce.X(); xperiod = uperiod;}
426 if (vclosed && isov) {dd = UVve.Y()-UVvce.Y(); xperiod = vperiod;}
427 Standard_Boolean xok = (Abs(dd)<tttol) || (Abs(Abs(dd)-xperiod)<tttol);
428 Standard_Boolean isequal = xok;
431 vceok = Standard_True;
434 {cout<<"vertex ";FUN_tool_trace(ivce);cout<<" of closing edge "<<icE;
435 cout<<" is connexed to vertex ";FUN_tool_trace(ive);cout<<" of edge "<<iE<<endl;}
443 if (trc && !vceok) {cout<<" vertex ";FUN_tool_trace(ivce);
444 cout<<"of closing edge "<<icE<<" is faulty"<<endl;}
446 cEKO = cEKO && (!vceok);
450 if (trc) {if (!cEKO) cout<<"-> valid edge"<<endl;
451 else cout<<"-> faulty edge"<<endl;}
453 if (cEKO) {fyE.Append(cE);}
455 return (!fyE.IsEmpty());
458 Standard_EXPORT Standard_Boolean FUN_isUVClosed(const TopoDS_Face& Fin, const TopoDS_Face& fF)
459 // purpose : returns true is fF is UV connexed
461 TopTools_ListOfShape efF;
462 TopExp_Explorer ex(fF, TopAbs_EDGE);
463 for (; ex.More(); ex.Next()) efF.Append(ex.Current());
464 Standard_Integer Ivfaulty; TopTools_ListOfShape lfyE; Standard_Boolean stopatfirst = Standard_True;
465 Standard_Boolean foundfaulty = FUN_DetectEdgeswithfaultyUV(Fin,fF,efF,Standard_False,lfyE,Ivfaulty,stopatfirst);