1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
3 // This file is part of Open CASCADE Technology software library.
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
15 #include <BRep_Builder.hxx>
16 #include <BRep_Tool.hxx>
17 #include <BRepLib.hxx>
18 #include <BRepTools.hxx>
19 #include <Geom_BezierSurface.hxx>
20 #include <Geom_BSplineSurface.hxx>
21 #include <Geom_Curve.hxx>
22 #include <Geom_ElementarySurface.hxx>
23 #include <Geom_Surface.hxx>
24 #include <Geom_TrimmedCurve.hxx>
25 #include <GeomAdaptor_Curve.hxx>
26 #include <GeomAdaptor_Surface.hxx>
27 #include <GeomLib.hxx>
30 #include <Precision.hxx>
31 #include <ShapeAnalysis_CheckSmallFace.hxx>
32 #include <ShapeAnalysis_Curve.hxx>
33 #include <ShapeAnalysis_Wire.hxx>
34 #include <ShapeAnalysis_WireOrder.hxx>
35 #include <ShapeExtend.hxx>
36 #include <ShapeExtend_WireData.hxx>
37 #include <Standard_ErrorHandler.hxx>
38 #include <TColgp_Array1OfPnt.hxx>
39 #include <TColgp_Array2OfPnt.hxx>
40 #include <TColStd_Array1OfReal.hxx>
41 #include <TColStd_Array2OfReal.hxx>
42 #include <TColStd_ListOfReal.hxx>
44 #include <TopExp_Explorer.hxx>
46 #include <TopoDS_Builder.hxx>
47 #include <TopoDS_Compound.hxx>
48 #include <TopoDS_Edge.hxx>
49 #include <TopoDS_Face.hxx>
50 #include <TopoDS_Iterator.hxx>
51 #include <TopoDS_Vertex.hxx>
52 #include <TopoDS_Wire.hxx>
53 #include <TopTools_Array1OfShape.hxx>
54 #include <TopTools_HSequenceOfShape.hxx>
55 #include <TopTools_ListOfShape.hxx>
57 //#include <GeomLProp_SLProps.hxx>
58 //#include <ShapeFix_Wire.hxx>
59 //=======================================================
60 //function : ShapeAnalysis_CheckSmallFace
62 //=======================================================================
63 ShapeAnalysis_CheckSmallFace::ShapeAnalysis_CheckSmallFace()
65 myStatusSpot = ShapeExtend::EncodeStatus ( ShapeExtend_OK );
66 myStatusStrip = ShapeExtend::EncodeStatus ( ShapeExtend_OK );
67 myStatusPin = ShapeExtend::EncodeStatus ( ShapeExtend_OK );
68 myStatusTwisted = ShapeExtend::EncodeStatus ( ShapeExtend_OK );
69 myStatusSplitVert = ShapeExtend::EncodeStatus ( ShapeExtend_OK );
73 (const gp_Pnt& p, Standard_Integer& nb,
74 Standard_Real& minx, Standard_Real& miny, Standard_Real& minz,
75 Standard_Real& maxx, Standard_Real& maxy, Standard_Real& maxz)
79 if (nb < 1) { minx = maxx = x; miny = maxy = y; minz = maxz = z; }
82 if (minx > x) minx = x;
83 if (maxx < x) maxx = x;
84 if (miny > y) miny = y;
85 if (maxy < y) maxy = y;
86 if (minz > z) minz = z;
87 if (maxz < z) maxz = z;
92 static Standard_Boolean MinMaxSmall
93 (const Standard_Real minx, const Standard_Real miny, const Standard_Real minz, const Standard_Real maxx, const Standard_Real maxy, const Standard_Real maxz, const Standard_Real toler)
95 Standard_Real dx = maxx - minx;
96 Standard_Real dy = maxy - miny;
97 Standard_Real dz = maxz - minz;
99 if ((dx > toler && !Precision::IsInfinite (dx)) ||
100 (dy > toler && !Precision::IsInfinite (dy)) ||
101 (dz > toler && !Precision::IsInfinite (dz)))
102 return Standard_False;
103 return Standard_True;
106 //=======================================================================
107 //function : IsSpotFace
109 //=======================================================================
111 Standard_Integer ShapeAnalysis_CheckSmallFace::IsSpotFace(const TopoDS_Face& F,gp_Pnt& spot,Standard_Real& spotol,const Standard_Real tol) const
114 Standard_Real toler = tol; Standard_Real tolv = tol;
115 // Compute tolerance to get : from greatest tol of vertices
116 // In addition, also computes min-max of vertices
117 // To finally compare mini-max box with tolerance
118 // gka Mar2000 Protection against faces without wires
119 // but they occur due to bugs in the algorithm itself, it needs to be fixed
120 Standard_Boolean isWir = Standard_False;
121 for(TopoDS_Iterator itw(F,Standard_False) ; itw.More();itw.Next()) {
122 if(itw.Value().ShapeType() != TopAbs_WIRE)
124 TopoDS_Wire w1 = TopoDS::Wire(itw.Value());
125 if (!w1.IsNull()) {isWir = Standard_True; break;}
127 if(!isWir) return Standard_True;
128 Standard_Integer nbv = 0;
129 Standard_Real minx =0 ,miny = 0 ,minz = 0,maxx = Precision::Infinite(), maxy = Precision::Infinite(),maxz = Precision::Infinite();
131 Standard_Boolean same = Standard_True;
132 for (TopExp_Explorer iv(F,TopAbs_VERTEX); iv.More(); iv.Next()) {
133 TopoDS_Vertex V = TopoDS::Vertex (iv.Current());
134 if (V0.IsNull()) V0 = V;
135 else if (same) { if (!V0.IsSame(V)) same = Standard_False; }
137 gp_Pnt pnt = BRep_Tool::Pnt (V);
138 // Standard_Real x,y,z;
139 MinMaxPnt (pnt, nbv, minx,miny,minz, maxx,maxy,maxz);
142 tolv = BRep_Tool::Tolerance (V);
143 if (tolv > toler) toler = tolv;
148 if (!MinMaxSmall(minx,miny,minz,maxx,maxy,maxz,toler)) return 0;
150 // All vertices are confused
151 // Check edges (a closed edge may be a non-null length edge !)
152 // By picking intermediate point on each one
153 for (TopExp_Explorer ie(F,TopAbs_EDGE); ie.More(); ie.Next()) {
154 TopoDS_Edge E = TopoDS::Edge (ie.Current());
156 Handle(Geom_Curve) C3D = BRep_Tool::Curve (E,cf,cl);
157 if (C3D.IsNull()) continue;
158 gp_Pnt debut = C3D->Value (cf);
159 gp_Pnt milieu = C3D->Value ( (cf+cl)/2);
160 if (debut.SquareDistance(milieu) > toler*toler) return 0;
163 spot.SetCoord ( (minx+maxx)/2. , (miny+maxy)/2. , (minz+maxz)/2. );
165 spotol = Max (spotol, maxy-miny);
166 spotol = Max (spotol, maxz-minz);
169 return (same ? 2 : 1);
172 //=======================================================================
173 //function : CheckSpotFace
175 //=======================================================================
177 Standard_Boolean ShapeAnalysis_CheckSmallFace::CheckSpotFace(const TopoDS_Face& F,const Standard_Real tol)
180 Standard_Real spotol;
181 Standard_Integer stat = IsSpotFace (F,spot,spotol,tol);
182 if(!stat) return Standard_False;
184 case 1: myStatusSpot = ShapeExtend::EncodeStatus (ShapeExtend_DONE1); break;
185 case 2: myStatusSpot = ShapeExtend::EncodeStatus (ShapeExtend_DONE2); break;
189 return Standard_True;
192 //=======================================================================
193 //function : IsStripSupport
195 //=======================================================================
197 Standard_Boolean ShapeAnalysis_CheckSmallFace::IsStripSupport(const TopoDS_Face& F,const Standard_Real tol)
200 Standard_Real toler = tol;
201 if (toler < 0) toler = 1.e-07; // ?? better to compute tolerance zones
204 Handle(Geom_Surface) surf = BRep_Tool::Surface (F,loc);
205 if (surf.IsNull()) return 0;
207 // Checking on poles for bezier-bspline
208 // A more general way is to check Values by scanning ISOS (slower)
210 Handle(Geom_BSplineSurface) bs = Handle(Geom_BSplineSurface)::DownCast(surf);
211 Handle(Geom_BezierSurface) bz = Handle(Geom_BezierSurface)::DownCast(surf);
213 // Standard_Integer stat = 2; // 2 : small in V direction
214 if (!bs.IsNull() || !bz.IsNull()) {
215 Standard_Boolean cbz = (!bz.IsNull());
216 Standard_Integer iu,iv, nbu, nbv;
217 if (cbz) { nbu = bz->NbUPoles(), nbv = bz->NbVPoles(); }
218 else { nbu = bs->NbUPoles(), nbv = bs->NbVPoles(); }
219 // Standard_Real dx = 0, dy = 0, dz = 0;
220 // Standard_Real x,y,z;
221 Standard_Real minx = 0.,miny = 0.,minz = 0.,maxx = 0.,maxy = 0.,maxz = 0.;
222 Standard_Boolean issmall = Standard_True;
224 for (iu = 1; iu <= nbu; iu ++) {
225 // for each U line, scan poles in V (V direction)
226 Standard_Integer nb = 0;
227 for (iv = 1; iv <= nbv; iv ++) {
228 gp_Pnt unp = (cbz ? bz->Pole(iu,iv) : bs->Pole(iu,iv));
229 MinMaxPnt (unp, nb, minx,miny,minz, maxx,maxy,maxz);
231 if (!MinMaxSmall(minx,miny,minz,maxx,maxy,maxz,toler))
232 { issmall = Standard_False; break; } // small in V ?
235 myStatusStrip = ShapeExtend::EncodeStatus ( ShapeExtend_DONE2);
236 return issmall; // OK, small in V
238 issmall = Standard_True;
239 for (iv = 1; iv <= nbv; iv ++) {
240 // for each V line, scan poles in U (U direction)
241 Standard_Integer nb = 0;
242 for (iu = 1; iu <= nbu; iu ++) {
243 gp_Pnt unp = (cbz ? bz->Pole(iu,iv) : bs->Pole(iu,iv));
244 MinMaxPnt (unp, nb, minx,miny,minz, maxx,maxy,maxz);
246 if (!MinMaxSmall(minx,miny,minz,maxx,maxy,maxz,toler))
247 { issmall = Standard_False; break; } // small in U ?
250 myStatusStrip = ShapeExtend::EncodeStatus (ShapeExtend_DONE1);
255 return Standard_False;
258 //=======================================================================
259 //function : CheckStripEdges
261 //=======================================================================
263 Standard_Boolean ShapeAnalysis_CheckSmallFace::CheckStripEdges(const TopoDS_Edge& E1,const TopoDS_Edge& E2,const Standard_Real tol,Standard_Real& dmax) const
265 // We have the topological configuration OK : 2 edges, 2 vertices
266 // But, are these two edges well confused ?
267 Standard_Real toler = tol;
269 Standard_Real tole = BRep_Tool::Tolerance(E1) + BRep_Tool::Tolerance(E2);
270 if (toler < tole / 2.) toler = tole/2.;
273 // We project a list of points from each curve, on the opposite one,
274 // we check the distance
275 Standard_Integer nbint = 10;
277 ShapeAnalysis_Curve SAC;
278 Standard_Real cf1,cl1,cf2,cl2,u; dmax = 0;
279 Handle(Geom_Curve) C1,C2;
280 C1 = BRep_Tool::Curve (E1,cf1,cl1);
281 C2 = BRep_Tool::Curve (E2,cf2,cl2);
282 if(C1.IsNull() || C2.IsNull()) return Standard_False;
283 cf1 = Max(cf1, C1->FirstParameter());
284 cl1 = Min(cl1, C1->LastParameter());
285 Handle(Geom_TrimmedCurve) C1T = new Geom_TrimmedCurve(C1,cf1,cl1,Standard_True);
286 //pdn protection against feature in Trimmed_Curve
287 cf1 = C1T->FirstParameter();
288 cl1 = C1T->LastParameter();
289 Handle(Geom_TrimmedCurve) CC;
290 cf2 = Max(cf2, C2->FirstParameter());
291 cl2 = Min(cl2, C2->LastParameter());
292 Handle(Geom_TrimmedCurve) C2T = new Geom_TrimmedCurve(C2,cf2,cl2, Standard_True);
293 cf2 = C2T->FirstParameter();
294 cl2 = C2T->LastParameter();
296 Standard_Real cd1 = (cl1 - cf1)/nbint;
297 Standard_Real cd2 = (cl2 - cf2)/nbint;
300 for (int numcur = 0; numcur < 2; numcur ++) {
302 if (numcur) { CC = C1T; C1T = C2T; C2T = CC;
303 cd1 = cd2; //smh added replacing step and replacing first
307 for (int nump = 0; nump <= nbint; nump ++) {
308 gp_Pnt p2, p1 = C1T->Value (u);
310 //pdn Adaptor curve is used to avoid of enhancing of domain.
311 GeomAdaptor_Curve GAC(C2T);
312 Standard_Real dist = SAC.Project (GAC,p1,toler,p2,para);
313 //pdn check if parameter of projection is in the domain of the edge.
314 if (para < f || para > l) return Standard_False;
315 if (dist > dmax) dmax = dist;
316 if (dist > toler) return Standard_False;
320 return (dmax < toler);
323 //=======================================================================
324 //function : FindStripEdges
326 //=======================================================================
328 Standard_Boolean ShapeAnalysis_CheckSmallFace::FindStripEdges(const TopoDS_Face& F,TopoDS_Edge& E1,TopoDS_Edge& E2,const Standard_Real tol,Standard_Real& dmax)
330 E1.Nullify(); E2.Nullify();
331 Standard_Integer nb = 0;
332 for (TopExp_Explorer ex(F,TopAbs_EDGE); ex.More(); ex.Next()) {
333 TopoDS_Edge E = TopoDS::Edge (ex.Current());
334 if (nb == 1 && E.IsSame(E1))
335 continue; // ignore seam edge
337 TopExp::Vertices (E,V1,V2);
339 p1 = BRep_Tool::Pnt (V1);
340 p2 = BRep_Tool::Pnt (V2);
341 Standard_Real toler = tol;
342 if (toler <= 0) toler = (BRep_Tool::Tolerance(V1) + BRep_Tool::Tolerance(V2) ) / 2.;
345 Standard_Real dist = p1.Distance(p2);
348 Handle(Geom_Curve) CC;
349 CC = BRep_Tool::Curve (E,cf,cl);
350 Standard_Boolean isNullLength = Standard_True;
352 gp_Pnt pp = CC->Value ( (cf+cl)/2.);
353 if (pp.Distance(p1) < toler && pp.Distance(p2) < toler) continue;
354 isNullLength = Standard_False;
356 if (dist <= toler && isNullLength) continue; //smh
359 else if (nb == 2) E2 = E;
360 else return Standard_False;
362 // Now, check these two edge to define a strip !
363 if (!E1.IsNull()&&!E2.IsNull()) {
364 if(!CheckStripEdges (E1,E2,tol,dmax)) return Standard_False;
366 myStatusStrip = ShapeExtend::EncodeStatus (ShapeExtend_DONE3);
367 return Standard_True ;
370 return Standard_False;
373 //=======================================================================
374 //function : CheckSingleStrip
376 //=======================================================================
378 Standard_Boolean ShapeAnalysis_CheckSmallFace::CheckSingleStrip(const TopoDS_Face& F,
379 TopoDS_Edge& E1, TopoDS_Edge& E2,const Standard_Real tol)
381 Standard_Real toler = tol;
382 Standard_Real minx,miny,minz,maxx,maxy,maxz;
384 // In this case, we have 2 vertices and 2 great edges. Plus possibly 2 small
385 // edges, one on each vertex
387 Standard_Integer nb = 0;
388 for (TopExp_Explorer itv (F,TopAbs_VERTEX); itv.More(); itv.Next()) {
389 TopoDS_Vertex V = TopoDS::Vertex (itv.Current());
390 if (V1.IsNull()) V1 = V;
391 else if (V1.IsSame(V)) continue;
392 else if (V2.IsNull()) V2 = V;
393 else if (V2.IsSame(V)) continue;
400 for (TopExp_Explorer ite (F,TopAbs_EDGE); ite.More(); ite.Next()) {
401 TopoDS_Edge E = TopoDS::Edge (ite.Current());
402 if (nb == 1 && E.IsSame(E1))
403 continue; // ignore seam edge
405 TopExp::Vertices (E,VA,VB);
408 tolv = BRep_Tool::Tolerance (VA);
409 if (toler < tolv) toler = tolv;
410 tolv = BRep_Tool::Tolerance (VB);
411 if (toler < tolv) toler = tolv;
414 // Edge on same vertex : small one ?
416 Standard_Real cf = 0.,cl = 0.;
417 Handle(Geom_Curve) C3D;
418 if (!BRep_Tool::Degenerated(E)) C3D = BRep_Tool::Curve (E,cf,cl);
419 if (C3D.IsNull()) continue; // DGNR
420 Standard_Integer np = 0;
421 gp_Pnt deb = C3D->Value(cf);
422 MinMaxPnt (deb,np,minx,miny,minz,maxx,maxy,maxz);
423 gp_Pnt fin = C3D->Value(cl);
424 MinMaxPnt (fin,np,minx,miny,minz,maxx,maxy,maxz);
425 gp_Pnt mid = C3D->Value( (cf+cl)/2. );
426 MinMaxPnt (mid,np,minx,miny,minz,maxx,maxy,maxz);
427 if (!MinMaxSmall (minx,miny,minz,maxx,maxy,maxz,toler)) return Standard_False;
429 // Other case : two maximum allowed
431 if (nb > 2) return Standard_False;
432 if (nb == 1) { V1 = VA; V2 = VB; E1 = E; }
434 if (V1.IsSame(VA) && !V2.IsSame(VB)) return Standard_False;
435 if (V1.IsSame(VB) && !V2.IsSame(VA)) return Standard_False;
438 else return Standard_False;
442 if (nb < 2) return Standard_False; // only one vertex : cannot be a strip ...
444 // Checking if E1 and E2 define a Strip
446 if (!CheckStripEdges (E1,E2,tol,dmax)) return Standard_False;
447 myStatusStrip = ShapeExtend::EncodeStatus (ShapeExtend_DONE3);
448 return Standard_True;
451 //=======================================================================
452 //function : CheckStripFace
454 //=======================================================================
456 Standard_Boolean ShapeAnalysis_CheckSmallFace::CheckStripFace(const TopoDS_Face& F,
457 TopoDS_Edge& E1, TopoDS_Edge& E2,const Standard_Real tol)
460 // Standard_Integer stat;
461 if(CheckSingleStrip (F,E1,E2,tol)) return Standard_True ; // it is a strip
463 // IsStripSupport used as rejection. But this kind of test may be done
464 // on ANY face, once we are SURE that FindStripEdges is reliable (and fast
467 // ?? record a diagnostic StripFace, but without yet lists of edges
468 // ?? Record Diagnostic "StripFace", no data (should be "Edges1" "Edges2")
469 // but direction is known (1:U 2:V)
470 // TopoDS_Edge E1,E2;
472 if(FindStripEdges (F,E1,E2,tol,dmax)) return Standard_True;
474 // Now, trying edges : if there are 2 and only 2 edges greater than tolerance
475 // (given or sum of vertex tolerances), do they define a strip
476 // Warning : if yes, they bring different vertices ...
478 return Standard_False;
482 //=======================================================================
483 //function : CheckSplittingVertices
485 //=======================================================================
487 Standard_Integer ShapeAnalysis_CheckSmallFace::CheckSplittingVertices(const TopoDS_Face& F,
488 TopTools_DataMapOfShapeListOfShape& MapEdges,
489 ShapeAnalysis_DataMapOfShapeListOfReal& MapParam,
490 TopoDS_Compound& theAllVert)
493 // Prepare array of vertices with their locations //TopTools
494 Standard_Integer nbv = 0, nbp = 0;
495 //TopoDS_Compound theAllVert;
496 BRep_Builder theBuilder;
497 //theBuilder.MakeCompound(theAllVert);
498 TopExp_Explorer itv; // svv Jan11 2000 : porting on DEC
499 for (itv.Init(F,TopAbs_VERTEX); itv.More(); itv.Next()) nbv ++;
501 if (nbv == 0) return 0;
502 TopTools_Array1OfShape vtx (1,nbv);
503 TColgp_Array1OfPnt vtp (1,nbv);
504 TColStd_Array1OfReal vto (1,nbv);
507 for (itv.Init(F,TopAbs_VERTEX); itv.More(); itv.Next()) {
509 TopoDS_Vertex unv = TopoDS::Vertex (itv.Current());
510 vtx.SetValue (nbp,unv);
511 gp_Pnt unp = BRep_Tool::Pnt (unv);
512 vtp.SetValue (nbp,unp);
513 Standard_Real unt = myPrecision;
514 if (unt < 0) unt =BRep_Tool::Tolerance (unv);
515 vto.SetValue (nbp,unt);
517 nbv = nbp; nbp = 0; // now, counting splitting vertices
519 // Check edges : are vertices (other than extremities) confused with it ?
520 ShapeAnalysis_Curve SAC;
521 for (Standard_Integer iv = 1; iv <= nbv; iv ++) {
522 TopoDS_Vertex V = TopoDS::Vertex (vtx.Value(iv));
523 TopTools_ListOfShape listEdge;
524 TColStd_ListOfReal listParam;
525 Standard_Boolean issplit = Standard_False;
526 for (TopExp_Explorer ite(F,TopAbs_EDGE); ite.More(); ite.Next()) {
527 TopoDS_Edge E = TopoDS::Edge (ite.Current());
529 TopExp::Vertices (E,V1,V2);
531 Handle(Geom_Curve) C3D = BRep_Tool::Curve (E,cf,cl);
532 if (C3D.IsNull()) continue;
533 if (V.IsSame(V1) || V.IsSame(V2)) continue;
534 gp_Pnt unp = vtp.Value(iv);
535 Standard_Real unt = vto.Value(iv);
538 Standard_Real dist = SAC.Project (C3D,unp,unt*10.,proj,param,cf,cl);
539 if (dist == 0.0) continue; //smh
540 // Splitting Vertex to record ?
542 // If Split occurs at beginning or end, it is not a split ...
543 Standard_Real fpar, lpar, eps = 1.e-06;
544 if (param >=cl || param <= cf) continue; // Out of range
545 fpar = param - cf; lpar = param - cl;
546 if ((Abs(fpar) < eps) || (Abs(lpar) < eps)) continue; // Near end or start
548 listParam.Append(param);
549 issplit = Standard_True;
555 theBuilder.Add(theAllVert, V);
556 MapEdges.Bind(V,listEdge);
557 MapParam.Bind(V,listParam);
561 myStatusSplitVert = ShapeExtend::EncodeStatus (ShapeExtend_DONE);
566 static Standard_Integer IsoStat
567 (const TColgp_Array2OfPnt& poles,
568 const Standard_Integer uorv, const Standard_Integer rank,
569 const Standard_Real tolpin, const Standard_Real toler)
571 Standard_Integer i, np = 0;
572 Standard_Integer i0 = (uorv == 1 ? poles.LowerCol() : poles.LowerRow());
573 Standard_Integer i1 = (uorv == 1 ? poles.UpperCol() : poles.UpperRow());
574 Standard_Real xmin = 0.,ymin = 0.,zmin = 0., xmax = 0.,ymax = 0.,zmax = 0.;
575 for (i = i0; i <= i1; i ++) {
576 if (uorv == 1) MinMaxPnt (poles(rank,i),np,xmin,ymin,zmin, xmax,ymax,zmax);
577 else MinMaxPnt (poles(i,rank), np, xmin,ymin,zmin, xmax,ymax,zmax);
579 if (MinMaxSmall (xmin,ymin,zmin, xmax,ymax,zmax, tolpin)) return 0;
580 if (MinMaxSmall (xmin,ymin,zmin, xmax,ymax,zmax, toler)) return 1;
584 static Standard_Boolean CheckPoles(const TColgp_Array2OfPnt& poles, Standard_Integer uorv, Standard_Integer rank)
586 Standard_Integer i0 = (uorv == 1 ? poles.LowerCol() : poles.LowerRow());
587 Standard_Integer i1 = (uorv == 1 ? poles.UpperCol() : poles.UpperRow());
588 for (Standard_Integer i = i0; i <= i1-1; i ++) {
590 if(poles(rank,i).IsEqual(poles(rank, i+1), 1e-15)) return Standard_True;
592 if(poles(i,rank).IsEqual(poles(i+1,rank), 1e-15)) return Standard_True;
594 return Standard_False;
596 //=======================================================================
597 //function : CheckPin
599 //=======================================================================
601 Standard_Boolean ShapeAnalysis_CheckSmallFace::CheckPin (const TopoDS_Face& F, Standard_Integer& whatrow,Standard_Integer& sens)
604 Handle(Geom_Surface) surf = BRep_Tool::Surface (F,loc);
605 if (surf->IsKind(STANDARD_TYPE(Geom_ElementarySurface))) return Standard_False;
607 Standard_Real toler = myPrecision;
608 if (toler < 0) toler = 1.e-4;
609 Standard_Real tolpin = 1.e-9; // for sharp sharp pin
611 // Checking the poles
613 // Take the poles : they give good idea of sharpness of a pin
614 Standard_Integer nbu = 0 , nbv = 0;
615 Handle(Geom_BSplineSurface) bs = Handle(Geom_BSplineSurface)::DownCast(surf);
616 Handle(Geom_BezierSurface) bz = Handle(Geom_BezierSurface)::DownCast(surf);
617 if (!bs.IsNull()) { nbu = bs->NbUPoles(); nbv = bs->NbVPoles(); }
618 if (!bz.IsNull()) { nbu = bz->NbUPoles(); nbv = bz->NbVPoles(); }
619 if (nbu == 0 || nbv == 0) return Standard_False;
621 TColgp_Array2OfPnt allpoles (1,nbu,1,nbv);
622 if (!bs.IsNull()) bs->Poles (allpoles);
623 if (!bz.IsNull()) bz->Poles (allpoles);
625 // Check each natural bound if it is a singularity (i.e. a pin)
628 Standard_Integer stat = 0; // 0 none, 1 in U, 2 in V
629 whatrow = 0; // 0 no row, else rank of row
630 stat = IsoStat(allpoles,1, 1,tolpin,toler);
631 if (stat) { sens = 1; whatrow = nbu; }
633 stat = IsoStat(allpoles,1,nbu,tolpin,toler);
634 if (stat) { sens = 1; whatrow = nbu; }
636 stat = IsoStat(allpoles,2, 1,tolpin,toler);
637 if (stat) { sens = 2; whatrow = 1; }
639 stat = IsoStat(allpoles,2,nbv,tolpin,toler);
640 if (stat) { sens = 2; whatrow = nbv; }
642 if (!sens) return Standard_False; // no pin
645 case 1: myStatusPin = ShapeExtend::EncodeStatus (ShapeExtend_DONE1); break;
646 case 2: myStatusPin = ShapeExtend::EncodeStatus (ShapeExtend_DONE2); break;
649 // std::cout<<(whatstat == 1 ? "Smooth" : "Sharp")<<" Pin on "<<(sens == 1 ? "U" : "V")<<" Row n0 "<<whatrow<<std::endl;
652 // Standard_Boolean EqualPoles = Standard_False;
653 if(CheckPoles(allpoles, 2, nbv)|| CheckPoles(allpoles, 2, 1)
654 ||CheckPoles(allpoles, 1, nbu)|| CheckPoles(allpoles, 1, 1))
655 myStatusPin = ShapeExtend::EncodeStatus (ShapeExtend_DONE3);
659 return Standard_True;
662 static Standard_Real TwistedNorm
663 (const Standard_Real x1, const Standard_Real y1, const Standard_Real z1, const Standard_Real x2, const Standard_Real y2, const Standard_Real z2)
664 { return (x1*x2) + (y1*y2) + (z1*z2); }
666 //=======================================================================
667 //function : CheckTwisted
669 //=======================================================================
671 Standard_Boolean ShapeAnalysis_CheckSmallFace::CheckTwisted (const TopoDS_Face& F, Standard_Real& paramu,
672 Standard_Real& paramv)
675 Handle(Geom_Surface) surf = BRep_Tool::Surface (F,loc);
676 if (surf->IsKind(STANDARD_TYPE(Geom_ElementarySurface))) return Standard_False;
678 Standard_Real toler = myPrecision;
679 if (toler < 0) toler = 1.e-4;
680 //// GeomLProp_SLProps GLS (surf,2,toler);
681 GeomAdaptor_Surface GAS (surf);
683 // to be done : on isos of the surface
684 // and on edges, at least of outer wire
685 Standard_Integer nbint = 5;
686 TColStd_Array2OfReal nx (1,nbint+1,1,nbint+1);
687 TColStd_Array2OfReal ny (1,nbint+1,1,nbint+1);
688 TColStd_Array2OfReal nz (1,nbint+1,1,nbint+1);
689 Standard_Integer iu,iv;
690 Standard_Real umin,umax,vmin,vmax;
691 surf->Bounds (umin,umax,vmin,vmax);
692 Standard_Real u = umin, du = (umax-umin)/nbint;
693 Standard_Real v = vmin, dv = (umax-umin)/nbint;
696 for (iu = 1; iu <= nbint; iu ++) {
697 for (iv = 1; iv <= nbint; iv ++) {
698 // GLS.SetParameters (u,v);
699 // if (GLS.IsNormalDefined()) norm = GLS.Normal();
700 gp_Pnt curp; gp_Vec V1,V2,VXnorm;
701 GAS.D1 (u,v,curp,V1,V2);
702 VXnorm = V1.Crossed(V2);
703 nx.SetValue (iu,iv,VXnorm.X());
704 ny.SetValue (iu,iv,VXnorm.Y());
705 nz.SetValue (iu,iv,VXnorm.Z());
712 // Now, comparing normals on support surface, in both senses
713 // In principle, it suffuces to check within outer bound
715 for (iu = 1; iu < nbint; iu ++) {
716 for (iv = 1; iv < nbint; iv ++) {
717 // We here check each normal (iu,iv) with (iu,iv+1) and with (iu+1,iv)
718 // if for each test, we have negative scalar product, this means angle > 90deg
719 // it is the criterion to say it is twisted
720 if (TwistedNorm ( nx(iu,iv),ny(iu,iv),nz(iu,iv) , nx(iu,iv+1),ny(iu,iv+1),nz(iu,iv+1) ) < 0. ||
721 TwistedNorm ( nx(iu,iv),ny(iu,iv),nz(iu,iv) , nx(iu+1,iv),ny(iu+1,iv),nz(iu+1,iv) ) < 0. ) {
722 myStatusTwisted = ShapeExtend::EncodeStatus (ShapeExtend_DONE);
723 paramu = umin+du*iu-du/2;
724 paramv = vmin+dv*iv-dv/2;
725 return Standard_True;
730 // Now, comparing normals on edges ... to be done
732 return Standard_False;
736 //=======================================================================
737 //function : CheckPinFace
739 //=======================================================================
740 // Warning: This function not tested on many examples
742 Standard_Boolean ShapeAnalysis_CheckSmallFace::CheckPinFace(const TopoDS_Face& F,
743 TopTools_DataMapOfShapeShape& mapEdges,const Standard_Real toler)
746 TopExp_Explorer exp_w (F,TopAbs_WIRE);
748 Standard_Real coef1=0, coef2; // =0 for deleting warning (skl)
749 TopoDS_Wire theCurWire = TopoDS::Wire (exp_w.Current());
750 ShapeAnalysis_WireOrder wi;
751 ShapeAnalysis_Wire sfw;
752 Handle(ShapeExtend_WireData) sbwd = new ShapeExtend_WireData(theCurWire);
755 Handle(TopTools_HSequenceOfShape) newedges = new TopTools_HSequenceOfShape();
756 Standard_Integer nb = wi.NbEdges();
757 Standard_Integer i = 0;
758 for ( i=1; i <= nb; i++ )
759 newedges->Append ( sbwd->Edge ( wi.Ordered(i) ) );
760 for ( i=1; i <= nb; i++ )
761 sbwd->Set ( TopoDS::Edge ( newedges->Value(i) ), i );
762 //sfw.Init(theCurWire, F, Precision::Confusion());
764 //theCurWire = sfw.Wire();
765 theCurWire = sbwd->Wire();
767 Standard_Boolean done = Standard_False;
768 Standard_Real tol = Precision::Confusion();
769 TopoDS_Edge theFirstEdge, theSecondEdge;
770 Standard_Real d1=0,d2=0;
771 for (TopExp_Explorer exp_e (F,TopAbs_EDGE); exp_e.More(); exp_e.Next())
777 theFirstEdge = TopoDS::Edge (exp_e.Current());
778 V1 = TopExp::FirstVertex(theFirstEdge);
779 V2 = TopExp::LastVertex(theFirstEdge);
780 p1 = BRep_Tool::Pnt(V1);
781 p2 = BRep_Tool::Pnt(V2);
782 tol = Max(BRep_Tool::Tolerance(V1), BRep_Tool::Tolerance(V2));
783 if (toler > 0) //tol = Max(tol, toler); gka
785 d1 = p1.Distance(p2);
786 if (d1 == 0) return Standard_False;
787 if (d1/tol>=1) coef1 = d1/tol; else continue;
788 if (coef1<=3) continue;
792 //Check the length of edge
793 theSecondEdge = TopoDS::Edge (exp_e.Current());
794 V1 = TopExp::FirstVertex(theSecondEdge);
795 V2 = TopExp::LastVertex(theSecondEdge);
797 p1 = BRep_Tool::Pnt(V1);
798 p2 = BRep_Tool::Pnt(V2);
799 if (toler == -1) tol = Max(BRep_Tool::Tolerance(V1), BRep_Tool::Tolerance(V2));
801 if (p1.Distance(p2)> tol) continue;
802 //If there are two pin edges, record them in diagnostic
803 d2 = p1.Distance(p2); //gka
804 if (d2 == 0) return Standard_False;
805 if (d2/tol >= 1) coef2 = d2/tol; else continue;
806 if (coef2<=3) continue;
807 if (coef1>coef2*10) continue;
810 theFirstEdge = theSecondEdge;
815 if (CheckPinEdges(theFirstEdge, theSecondEdge, coef1, coef2,toler))
817 mapEdges.Bind(theFirstEdge,theSecondEdge);
818 myStatusPinFace = ShapeExtend::EncodeStatus (ShapeExtend_DONE);
819 done = Standard_True;
822 theFirstEdge = theSecondEdge;
830 //=======================================================================
831 //function : CheckPinEdges
833 //=======================================================================
834 // Warning: This function not tested on many examples
836 Standard_Boolean ShapeAnalysis_CheckSmallFace::CheckPinEdges(const TopoDS_Edge& theFirstEdge,const TopoDS_Edge& theSecondEdge,const Standard_Real coef1,
837 const Standard_Real coef2,const Standard_Real toler) const
840 Standard_Real cf1,cl1,cf2,cl2;
841 Handle(Geom_Curve) C1,C2,C3;
842 C1 = BRep_Tool::Curve (theFirstEdge,cf1,cl1);
843 C2 = BRep_Tool::Curve (theSecondEdge,cf2,cl2);
844 gp_Pnt p1, p2, pp1, pp2, pv;
845 Standard_Real d1 = (cf1-cl1)/coef1;
846 Standard_Real d2 = (cf2-cl2)/coef2;
847 //Standard_Real d1 = cf1-cl1/30; //10; gka
848 //Standard_Real d2 = cf2-cl2/30; //10;
851 pp1 = C2->Value(cf2);
852 pp2 = C2->Value(cl2);
854 Standard_Real paramc1=0, paramc2=0; // =0 for deleting warning (skl)
855 TopoDS_Vertex theSharedV = TopExp::LastVertex(theFirstEdge);
856 if (toler == -1) tol = BRep_Tool::Tolerance(theSharedV); else tol = toler;
857 pv = BRep_Tool::Pnt(theSharedV);
858 if (pv.Distance(p1)<=tol) paramc1 = cf1;
859 else if(pv.Distance(p2)<=tol) paramc1 = cl1;
860 if (pv.Distance(pp1)<=tol) paramc2 = cf2;
861 else if(pv.Distance(pp2)<=tol) paramc2 = cl2;
862 //Computing first derivative vectors and compare angle
863 // gp_Vec V11, V12, V21, V22;
865 // C1->D2(paramc1, tmp, V11, V21);
866 // C2->D2(paramc2, tmp, V12, V22);
867 // Standard_Real angle1, angle2;
869 // angle1 = V11.Angle(V12);
870 // angle2 = V21.Angle(V22);
872 // catch (Standard_Failure)
874 // std::cout << "Couldn't compute angle between derivative vectors" <<std::endl;
875 // return Standard_False;
877 // std::cout << "angle1 " << angle1<< std::endl;
878 // std::cout << "angle2 " << angle2<< std::endl;
879 // if (angle1<=0.0001) return Standard_True;
881 if (p1.Distance(p2)<pp1.Distance(pp2))
885 proj = C1->Value(paramc1 + (coef1-3)*d1);
886 else proj = C1->Value(paramc1-3*d1);
887 //proj = C1->Value(paramc1 + 9*d1);
888 //else proj = C1->Value(paramc1-d1);
894 proj = C2->Value(paramc2 + (coef2-3)*d2);
895 else proj = C2->Value(paramc2 -3*d2);
896 //proj = C2->Value(paramc2 + 9*d2);
897 //else proj = C2->Value(paramc2 -d2);
900 GeomAdaptor_Curve GAC(C3);
901 Standard_Real f = C3->FirstParameter();
902 Standard_Real l = C3->LastParameter();
904 ShapeAnalysis_Curve SAC;
905 Standard_Real dist = SAC.Project (GAC,proj,tol,result,param);
906 //pdn check if parameter of projection is in the domain of the edge.
907 if (param < f || param > l) return Standard_False;
908 if (dist > tol) return Standard_False;
910 //Computing first derivative vectors and compare angle
911 gp_Vec V11, V12, V21, V22;
913 C1->D2(paramc1, tmp, V11, V21);
914 C2->D2(paramc2, tmp, V12, V22);
915 Standard_Real angle1=0, angle2=0;
917 angle1 = V11.Angle(V12);
918 angle2 = V21.Angle(V22);
920 catch (Standard_Failure const&)
923 std::cout << "Couldn't compute angle between derivative vectors" <<std::endl;
925 return Standard_False;
927 // std::cout << "angle1 " << angle1<< std::endl;
928 // std::cout << "angle2 " << angle2<< std::endl;
929 if ((angle1<=0.001 && angle2<=0.01) || ((M_PI-angle2)<= 0.001 && (M_PI-angle2)<= 0.01)) return Standard_True;
930 else return Standard_False;
933 return Standard_False;