1 // File: ShapeFix_ComposeShell.cxx
2 // Created: Tue Apr 27 11:34:07 1999
3 // Author: Andrey BETENEV
4 // <abv@doomox.nnov.matra-dtv.fr>
5 // pdn 01.06.99 S4205: handling not-SameRange edges
6 // abv 22.07.99 implementing patch indices
7 // svv 10.01.00 porting on DEC
9 #include <ShapeFix_ComposeShell.ixx>
11 #include <Precision.hxx>
12 #include <gp_Pnt2d.hxx>
13 #include <gp_Lin2d.hxx>
14 #include <gp_Dir2d.hxx>
16 #include <TColStd_Array1OfInteger.hxx>
17 #include <TColStd_Array1OfBoolean.hxx>
18 #include <TColStd_Array1OfReal.hxx>
19 #include <TColStd_HArray1OfReal.hxx>
20 #include <TColgp_SequenceOfPnt2d.hxx>
21 #include <TColStd_SequenceOfReal.hxx>
23 #include <IntRes2d_IntersectionSegment.hxx>
24 #include <IntRes2d_IntersectionPoint.hxx>
25 #include <IntRes2d_Domain.hxx>
27 #include <Geom2dInt_GInter.hxx>
28 #include <Geom_Curve.hxx>
29 #include <Geom2d_Line.hxx>
30 #include <Geom2dAdaptor_Curve.hxx>
31 #include <GeomAdaptor_Surface.hxx>
34 #include <TopoDS_Vertex.hxx>
35 #include <TopoDS_Edge.hxx>
36 #include <TopoDS_Wire.hxx>
37 #include <TopoDS_Shell.hxx>
38 #include <TopoDS_Iterator.hxx>
39 #include <TopTools_MapOfShape.hxx>
40 #include <TopTools_DataMapOfShapeListOfShape.hxx>
42 #include <BRepTools.hxx>
43 #include <Bnd_Box2d.hxx>
44 #include <BndLib_Add2dCurve.hxx>
45 #include <BRepTopAdaptor_FClass2d.hxx>
46 #include <BRep_Tool.hxx>
47 #include <BRep_Builder.hxx>
49 #include <ShapeExtend.hxx>
50 #include <ShapeExtend_WireData.hxx>
51 #include <ShapeBuild_Vertex.hxx>
52 #include <ShapeBuild_Edge.hxx>
53 #include <ShapeAnalysis.hxx>
54 #include <ShapeAnalysis_Edge.hxx>
55 #include <ShapeAnalysis_WireOrder.hxx>
56 #include <ShapeFix_Wire.hxx>
57 #include <ShapeFix_Edge.hxx>
58 #include <ShapeFix_WireSegment.hxx>
59 #include <ShapeAnalysis_Curve.hxx>
60 #include <ShapeBuild_ReShape.hxx>
61 #include <ShapeAnalysis_TransferParametersProj.hxx>
62 #include <ShapeFix_Face.hxx>
63 #include <ShapeAnalysis_Surface.hxx>
65 #include <Extrema_ExtPC2d.hxx>
66 #include <ShapeAnalysis.hxx>
68 //=======================================================================
69 //function : ShapeFix_ComposeShell
71 //=======================================================================
73 ShapeFix_ComposeShell::ShapeFix_ComposeShell () :
74 myStatus(0), myClosedMode(Standard_False)
76 myTransferParamTool = new ShapeAnalysis_TransferParametersProj;
80 //=======================================================================
83 //=======================================================================
85 void ShapeFix_ComposeShell::Init (const Handle(ShapeExtend_CompositeSurface) &Grid,
86 const TopLoc_Location& L,
87 const TopoDS_Face &Face,
88 const Standard_Real Prec)
91 myUClosed = myGrid->IsUClosed();
92 myVClosed = myGrid->IsVClosed();
93 myUPeriod = myGrid->UJointValue(myGrid->NbUPatches()+1) - myGrid->UJointValue(1);
94 myVPeriod = myGrid->VJointValue(myGrid->NbVPatches()+1) - myGrid->VJointValue(1);
96 // DTK-CKY 100531 : protection against very thin face
97 // Test "isclosed" should be filtered on the overall (non trimmed) surface, must be closed
98 Handle(Geom_Surface) theSurface = BRep_Tool::Surface(Face,myLoc);
99 Standard_Real U0,U1,V0,V1;
100 theSurface->Bounds(U0,U1,V0,V1);
102 gp_Pnt P0 = theSurface->Value(U0,(V0+V1)/2.);
103 gp_Pnt P1 = theSurface->Value(U1,(V0+V1)/2.);
104 if (P0.Distance(P1) > Precision::Confusion()*10)
105 myUClosed = Standard_False;
108 gp_Pnt P0 = theSurface->Value((U0+U1)/2.,V0);
109 gp_Pnt P1 = theSurface->Value((U0+U1)/2.,V1);
110 if (P0.Distance(P1) > Precision::Confusion()*10)
111 myVClosed = Standard_False;
113 // DTK-CKY 100531 end
117 TopoDS_Shape tmpF = Face.Oriented ( TopAbs_FORWARD );
118 myFace = TopoDS::Face ( tmpF ); // for correct dealing with seams
119 myOrient = Face.Orientation();
121 myStatus = ShapeExtend::EncodeStatus ( ShapeExtend_OK );
123 // Compute resolution (checking in 2d is necessary for splitting
124 // degenerated edges and avoiding NotClosed)
125 myUResolution = myVResolution = RealLast();
126 for ( Standard_Integer i=1; i <= myGrid->NbUPatches(); i++ ) {
127 Standard_Real uRange = myGrid->UJointValue(i+1)-myGrid->UJointValue(i);
128 for ( Standard_Integer j=1; j <= myGrid->NbVPatches(); j++ ) {
129 Standard_Real vRange = myGrid->VJointValue(j+1)-myGrid->VJointValue(j);
130 Standard_Real u1,u2,v1,v2;
131 myGrid->Patch(i,j)->Bounds(u1,u2,v1,v2);
132 GeomAdaptor_Surface GAS ( myGrid->Patch(i,j) );
133 Standard_Real ures = GAS.UResolution ( 1. )*uRange/(u2-u1);
134 Standard_Real vres = GAS.VResolution ( 1. )*vRange/(v2-v1);
135 if ( ures >0. && myUResolution > ures ) myUResolution = ures;
136 if ( vres >0. && myVResolution > vres ) myVResolution = vres;
139 if ( myUResolution == RealLast() ) myUResolution = ::Precision::Parametric ( 1. );
140 if ( myVResolution == RealLast() ) myVResolution = ::Precision::Parametric ( 1. );
144 //=======================================================================
147 //=======================================================================
149 Standard_Boolean ShapeFix_ComposeShell::Perform ()
151 myStatus = ShapeExtend::EncodeStatus ( ShapeExtend_OK );
152 myInvertEdgeStatus = Standard_False;
154 ShapeFix_SequenceOfWireSegment seqw; // working data: wire segments
156 // Init seqw by initial set of wires (with corresponding orientation)
158 if(seqw.Length() == 0) {
159 myStatus = ShapeExtend::EncodeStatus ( ShapeExtend_FAIL6 );
160 return Standard_False;
163 // Split edges in the wires by grid and add internal segments of grid (parts of cutting lines)
164 SplitByGrid ( seqw );
166 // Split all the wires into segments by common vertices (intersections)
169 // Then, collect resulting wires
170 ShapeFix_SequenceOfWireSegment wires; // resulting wires
171 CollectWires ( wires, seqw );
173 // And construct resulting faces
174 TopTools_SequenceOfShape faces;
175 DispatchWires ( faces, wires );
177 // Finally, construct resulting shell
178 if ( faces.Length() !=1 ) {
182 for ( Standard_Integer i=1; i <= faces.Length(); i++ )
183 B.Add ( S, faces(i) );
186 else myResult = faces(1);
187 myResult.Orientation ( myOrient );
189 myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE1 );
190 return Standard_True;
194 //=======================================================================
195 //function : SplitEdges
197 //=======================================================================
199 void ShapeFix_ComposeShell::SplitEdges ()
201 myStatus = ShapeExtend::EncodeStatus ( ShapeExtend_OK );
203 ShapeFix_SequenceOfWireSegment seqw; // working data: wire segments
205 // Init seqw by initial set of wires (with corresponding orientation)
208 // Split edges in the wires by grid and add internal segments of grid (parts of cutting lines)
209 SplitByGrid ( seqw );
213 //=======================================================================
216 //=======================================================================
218 const TopoDS_Shape& ShapeFix_ComposeShell::Result () const
224 //=======================================================================
227 //=======================================================================
229 Standard_Boolean ShapeFix_ComposeShell::Status (const ShapeExtend_Status status) const
231 return ShapeExtend::DecodeStatus ( myStatus, status );
235 //=======================================================================
236 // PRIVATE (working) METHODS
237 //=======================================================================
239 #define TOLINT 1.e-10 // precision for intersection
241 // Local definitions: characteristics of intersection point
243 #define IOR_UNDEF 0 // undefined side
244 #define IOR_LEFT 1 // to left side of cutting line
245 #define IOR_RIGHT 2 // to right side of cutting line
246 #define IOR_BOTH 3 // crossing
247 #define IOR_POS 4 // in case of cycle on full period, whether first point is right
249 #define ITP_INTER 8 // crossing
250 #define ITP_BEGSEG 16 // start of tangential segment
251 #define ITP_ENDSEG 32 // stop of tangential segment
252 #define ITP_TANG 64 // tangential point
255 //=======================================================================
256 //function : PointLineDeviation
258 //=======================================================================
259 // Return (signed) deviation of point from line
260 static Standard_Real PointLineDeviation (const gp_Pnt2d &p, const gp_Lin2d &line)
262 gp_Dir2d dir = line.Direction();
263 gp_Dir2d n ( -dir.Y(), dir.X() );
264 return n.XY() * ( p.XY() - line.Location().XY() );
268 //=======================================================================
269 //function : PointLinePosition
271 //=======================================================================
272 // Define position of point relative to line
273 static Standard_Integer PointLinePosition (const gp_Pnt2d &p, const gp_Lin2d &line,
276 dev = PointLineDeviation ( p, line );
277 return ( dev > TOLINT ? IOR_LEFT : ( dev < -TOLINT ? IOR_RIGHT : IOR_UNDEF ) );
281 //=======================================================================
282 //function : PointLinePosition
284 //=======================================================================
285 // Define position of point relative to line
286 static Standard_Integer PointLinePosition (const gp_Pnt2d &p, const gp_Lin2d &line)
289 return PointLinePosition ( p, line, dev );
293 //=======================================================================
294 //function : ParamPointsOnLine
296 //=======================================================================
297 // Compute parameter of point on line
298 static inline Standard_Real ParamPointOnLine (const gp_Pnt2d &p, const gp_Lin2d &line)
300 return line.Direction().XY() * ( p.XY() - line.Location().XY() );
304 //=======================================================================
305 //function : ParamPointsOnLine
307 //=======================================================================
308 // Compute parameter of two points on line (as intersection of segment)
309 static Standard_Real ParamPointsOnLine (const gp_Pnt2d &p1, const gp_Pnt2d &p2,
310 const gp_Lin2d &line)
312 Standard_Real dist1 = PointLineDeviation ( p1, line );
313 Standard_Real dist2 = PointLineDeviation ( p2, line );
314 // in most cases, one of points is on line
315 if ( Abs ( dist1 ) < ::Precision::PConfusion() ) {
316 if ( Abs ( dist2 ) < ::Precision::PConfusion() )
317 return 0.5 * ( ParamPointOnLine ( p1, line ) + ParamPointOnLine ( p2, line ) );
318 return ParamPointOnLine ( p1, line );
320 if ( Abs ( dist2 ) < ::Precision::PConfusion() )
321 return ParamPointOnLine ( p2, line );
323 if ( dist2 * dist1 >0 )
324 return 0.5 * ( ParamPointOnLine ( p1, line ) + ParamPointOnLine ( p2, line ) );
325 // else compute intersection
326 return ( ParamPointOnLine ( p1, line ) * dist2 -
327 ParamPointOnLine ( p2, line ) * dist1 ) / ( dist2 - dist1 );
331 //=======================================================================
332 //function : ProjectPointOnLine
334 //=======================================================================
335 // Compute projection of point on line
336 static inline gp_Pnt2d ProjectPointOnLine (const gp_Pnt2d &p, const gp_Lin2d &line)
338 return line.Location().XY() + line.Direction().XY() * ParamPointOnLine ( p, line );
342 //=======================================================================
343 //function : ApplyContext
345 //=======================================================================
346 // Apply context to one edge in the wire and put result into this wire
347 static Standard_Integer ApplyContext (ShapeFix_WireSegment &wire,
348 const Standard_Integer iedge,
349 const Handle(ShapeBuild_ReShape) &context)
351 TopoDS_Edge edge = wire.Edge ( iedge );
352 TopoDS_Shape res = context->Apply ( edge );
354 if ( res.IsSame ( edge ) ) return 1;
356 if ( res.ShapeType() == TopAbs_EDGE ) {
357 wire.SetEdge ( iedge, TopoDS::Edge ( res ) );
361 Standard_Integer index = iedge;
363 Handle(ShapeExtend_WireData) segw = new ShapeExtend_WireData;
364 segw->ManifoldMode() = Standard_False;
365 for ( TopoDS_Iterator it(res); it.More(); it.Next() ) {
366 TopoDS_Edge E = TopoDS::Edge ( it.Value() );
367 if ( ! E.IsNull() ) segw->Add ( E );
369 else cout << "Error: ShapeFix_ComposeShell, ApplyContext: wrong mapping of edge" << endl;
373 // add edges into the wire in correct order
374 if ( segw->NbEdges() >0 ) {
375 Standard_Integer ind, iumin, iumax, ivmin, ivmax;
376 wire.GetPatchIndex ( iedge, iumin, iumax, ivmin, ivmax );
377 Standard_Integer nbEdges = segw->NbEdges();
378 for ( Standard_Integer i=1; i <= nbEdges; i++, index++ ) {
379 ind = ( edge.Orientation() == TopAbs_FORWARD || edge.Orientation() == TopAbs_INTERNAL ? i : segw->NbEdges()-i+1 );
380 TopoDS_Edge aE = segw->Edge ( ind );
381 if ( i==1 ) wire.SetEdge ( index, aE );
382 else wire.AddEdge ( index, aE, iumin, iumax, ivmin, ivmax );
386 else cout << "Warning: ShapeFix_ComposeShell, ApplyContext: edge is to remove - not implemented" << endl;
389 return index - iedge;
393 //=======================================================================
394 //function : IsCoincided
396 //=======================================================================
397 // check points coincidence
398 static inline Standard_Integer IsCoincided (const gp_Pnt2d &p1, const gp_Pnt2d &p2,
399 const Standard_Real UResolution,
400 const Standard_Real VResolution,
401 const Standard_Real tol)
403 //pdn Maximal accuracy is working precision of intersector.
404 Standard_Real UTolerance = UResolution * tol;
405 Standard_Real VTolerance = VResolution * tol;
406 return Abs ( p1.X() - p2.X() ) <= Max(TOLINT,UTolerance) &&
407 Abs ( p1.Y() - p2.Y() ) <= Max(TOLINT,VTolerance);
411 //=======================================================================
412 //function : GetPatchIndex
414 //=======================================================================
415 // computes index for the patch by given parameter Param
416 static Standard_Integer GetPatchIndex (const Standard_Real Param,
417 const Handle(TColStd_HArray1OfReal) &Params,
418 const Standard_Boolean isClosed)
420 Standard_Integer NP = Params->Upper();
421 Standard_Real period = Params->Value(NP) - Params->Value(1);
422 Standard_Real shift = 0;
424 shift = ShapeAnalysis::AdjustToPeriod ( Param, Params->Value(1), Params->Value(NP) );
425 Standard_Real p = Param + shift;
427 // locate patch: the same algo as in SE_CS::LocateParameter()
428 Standard_Integer i; // svv #1
429 for ( i = 2; i < NP; i++ ) {
430 // Standard_Real par = Params->Value(i);
431 if ( p < Params->Value(i) ) break;
435 Standard_Real ish = shift / period;
436 Standard_Integer ishift = (Standard_Integer)( ish <0 ? ish - 0.5 : ish + 0.5 );
437 return i - ishift * ( NP - 1 );
441 //=======================================================================
442 //function : LoadWires
444 //=======================================================================
446 void ShapeFix_ComposeShell::LoadWires (ShapeFix_SequenceOfWireSegment &seqw) const
450 // Init seqw by initial set of wires (with corresponding orientation)
451 for ( TopoDS_Iterator iw(myFace,Standard_False); iw.More(); iw.Next() ) {
453 TopoDS_Shape tmpW = Context()->Apply ( iw.Value() ) ;
454 if(tmpW.ShapeType() != TopAbs_WIRE) {
455 if(tmpW.ShapeType() == TopAbs_VERTEX) {
456 ShapeFix_WireSegment seg; //(( isOuter ? TopAbs_REVERSED : TopAbs_FORWARD ) );
457 seg.SetVertex(TopoDS::Vertex(tmpW));
458 seg.Orientation(tmpW.Orientation());
463 TopoDS_Wire wire = TopoDS::Wire ( tmpW );
465 Standard_Boolean isNonManifold = ( wire.Orientation() != TopAbs_REVERSED &&
466 wire.Orientation() != TopAbs_FORWARD );
470 // protect against INTERNAL/EXTERNAL wires
471 // if ( wire.Orientation() != TopAbs_REVERSED &&
472 // wire.Orientation() != TopAbs_FORWARD ) continue;
474 // determine orientation of the wire
475 // TopoDS_Face face = TopoDS::Face ( myFace.EmptyCopied() );
476 // B.Add ( face, wire );
477 // Standard_Boolean isOuter = ShapeAnalysis::IsOuterBound ( face );
481 Handle(ShapeExtend_WireData) sbwd = new ShapeExtend_WireData ( wire ,Standard_True,Standard_False);
482 //pdn protection againts of wires w/o edges
483 Standard_Integer nbEdges = sbwd->NbEdges();
486 //wire segments for non-manifold topology should have INTERNAL orientation
487 ShapeFix_WireSegment seg ( sbwd, TopAbs_INTERNAL);
492 //splitting wires containing manifold and non-manifold parts on a separate
495 Handle(ShapeExtend_WireData) sbwdM = new ShapeExtend_WireData();
496 Handle(ShapeExtend_WireData) sbwdNM = new ShapeExtend_WireData();
497 sbwdNM->ManifoldMode() = Standard_False;
498 TopoDS_Iterator aIt(wire);
499 for( ; aIt.More(); aIt.Next()) {
500 TopoDS_Edge E = TopoDS::Edge ( aIt.Value() );
501 if(E.Orientation() == TopAbs_FORWARD || E.Orientation() == TopAbs_REVERSED)
506 Standard_Integer nbMEdges = sbwdM->NbEdges();
507 Standard_Integer nbNMEdges = sbwdNM->NbEdges();
509 ShapeFix_WireSegment seg ( sbwdNM, TopAbs_INTERNAL); //(( isOuter ? TopAbs_REVERSED : TopAbs_FORWARD ) );
513 // Orientation is set so as to allow the segment to be traversed in only one direction
515 Handle(ShapeFix_Wire) sfw = new ShapeFix_Wire;
517 Standard_Integer stat=0;
518 Handle(Geom_Surface) gs = BRep_Tool::Surface(myFace);
519 if( gs->IsUPeriodic() && gs->IsVPeriodic() ) {
520 // For torus-like shapes, first reorder in 2d since reorder is indifferent in 3d
521 ShapeAnalysis_WireOrder sawo(Standard_False, 0);
522 ShapeAnalysis_Edge sae;
523 for(Standard_Integer i = 1; i <= nbMEdges; i++) {
525 Handle(Geom2d_Curve) c2d;
527 TopoDS_Shape tmpF = myFace.Oriented(TopAbs_FORWARD);
528 if(!sae.PCurve(sbwdM->Edge(i),TopoDS::Face(tmpF),c2d,f,l))
530 sawo.Add(c2d->Value(f).XY(),c2d->Value(l).XY());
533 stat = (sawo.Status() < 0 ? -1 : 1);
534 sfw->FixReorder(sawo);
538 if (sfw->StatusReorder(ShapeExtend_DONE3))
543 TopoDS_Shape dummy = myFace.EmptyCopied();
544 TopoDS_Face face = TopoDS::Face ( dummy );
545 B.Add ( face, wire );
546 Standard_Boolean isOuter = ShapeAnalysis::IsOuterBound ( face );
547 TopoDS_Wire w = sbwdM->Wire();
548 dummy = myFace.EmptyCopied();
549 face = TopoDS::Face ( dummy );
551 Standard_Boolean isOuterAfter = ShapeAnalysis::IsOuterBound ( face );
552 if(isOuter!=isOuterAfter)
553 sbwdM->Reverse(face);
556 ShapeFix_WireSegment seg ( sbwdM, TopAbs_REVERSED ); //(( isOuter ? TopAbs_REVERSED : TopAbs_FORWARD ) );
565 //=======================================================================
566 //function : ComputeCode
567 //purpose : compute code for wire segment between two intersections (by deviation)
568 //=======================================================================
570 Standard_Integer ShapeFix_ComposeShell::ComputeCode (const Handle(ShapeExtend_WireData) &wire,
571 const gp_Lin2d &line,
572 const Standard_Integer begInd,
573 const Standard_Integer endInd,
574 const Standard_Real begPar,
575 const Standard_Real endPar,
576 const Standard_Boolean isInternal)
578 Standard_Integer code = IOR_UNDEF;
580 ShapeAnalysis_Edge sae;
581 const Standard_Integer NPOINTS = 5; // number of points for measuring deviation
583 // track special closed case: segment starts at end of edge and ends at its beginning
584 Standard_Integer special = ( begInd == endInd &&
585 ( wire->Edge(begInd).Orientation() == TopAbs_FORWARD ||
586 wire->Edge(begInd).Orientation() == TopAbs_INTERNAL) ==
587 ( begPar > endPar ) ? 1 : 0);
588 if ( ! special && begInd == endInd && begPar == endPar &&
589 (myClosedMode || isInternal))
592 // for tracking cases in closed mode
593 Standard_Boolean begin=Standard_True;
594 Standard_Real shift=0;
597 // check if segment is tangency
598 // Segment is considered as tangency if deviation of pcurve from line
599 // (in 2d) measured by NPOINTS points is less than tolerance of edge
600 // (recomputed to 2d using Resolution).
602 Standard_Integer nb = wire->NbEdges();
604 Standard_Integer i; // svv #1
605 for ( i=begInd; ; i++ ) {
607 TopoDS_Edge edge = wire->Edge ( i );;
609 Handle(Geom2d_Curve) c2d;
611 if ( ! sae.PCurve ( edge, myFace, c2d, f, l, Standard_False ) ) {
612 myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL3 );
615 Standard_Real tol = LimitTolerance(BRep_Tool::Tolerance ( edge ));
616 Standard_Boolean isreversed = ( edge.Orientation() == TopAbs_REVERSED );
618 Standard_Real par1 = ( i == begInd && special >=0 ? begPar : ( isreversed ? l : f ) );
619 Standard_Real par2 = ( i == endInd && special <=0 ? endPar : ( isreversed ? f : l ) );
620 Standard_Real dpar = ( par2 - par1 ) / ( NPOINTS - 1 );
621 Standard_Integer np = ( Abs ( dpar ) < ::Precision::PConfusion() ? 1 : NPOINTS );
622 Standard_Integer j; // svv #1
623 for ( j=0; j < np; j++ ) {
624 Standard_Real par = par1 + dpar * j;
625 gp_Pnt2d p2d = c2d->Value ( par );
626 if ( myClosedMode ) {
627 if ( myUClosed && Abs ( line.Direction().X() ) < ::Precision::PConfusion() ) {
628 if ( begin ) shift = ShapeAnalysis::AdjustByPeriod ( p2d.X(), line.Location().X(), myUPeriod );
629 else if ( ! j ) shift = ShapeAnalysis::AdjustByPeriod ( p2d.X()-p2d0.X(), 0., myUPeriod );
630 p2d.SetX ( p2d.X() + shift );
632 if ( myVClosed && Abs ( line.Direction().Y() ) < ::Precision::PConfusion() ) {
633 if ( begin ) shift = ShapeAnalysis::AdjustByPeriod ( p2d.Y(), line.Location().Y(), myVPeriod );
634 else if ( ! j ) shift = ShapeAnalysis::AdjustByPeriod ( p2d.Y()-p2d0.Y(), 0., myVPeriod );
635 p2d.SetY ( p2d.Y() + shift );
637 begin = Standard_False;
640 Standard_Integer pos = PointLinePosition ( p2d, line );
641 if ( pos == IOR_UNDEF ) continue;
643 // analyse the deviation
644 gp_Pnt2d p2dl = ProjectPointOnLine ( p2d, line );
645 if(!IsCoincided ( p2d, p2dl, myUResolution, myVResolution, tol )) {
646 if(!myClosedMode) { code = pos; break; }
652 if ( j < np ) { i = 0; break; } // not tangency
654 if ( special <=0 ) break;
657 if ( myClosedMode ) {
658 if ( code != IOR_UNDEF && ! begin ) {
659 // in closed mode, if segment is of 2*pi length, it is BOTH
660 Standard_Real dev = PointLineDeviation ( p2d0, line );
661 if ( myUClosed && Abs ( line.Direction().X() ) < ::Precision::PConfusion() ) {
662 if ( Abs ( Abs ( dev ) - myUPeriod ) < 0.1 * myUPeriod ) {
664 if ( dev >0 ) code |= IOR_POS;
666 else if(code==IOR_BOTH)
669 if ( myVClosed && Abs ( line.Direction().Y() ) < ::Precision::PConfusion() ) {
670 if ( Abs ( Abs ( dev ) - myVPeriod ) < 0.1 * myVPeriod ) {
672 if ( dev >0 ) code |= IOR_POS;
674 else if(code==IOR_BOTH)
680 if ( i ) code = IOR_UNDEF; // tangency
681 else if ( code == IOR_BOTH ) { // parity error in intersector
683 myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL2 );
685 cout << "Warning: ShapeFix_ComposeShell::ComputeCode: lost intersection point" << cout;
692 //=======================================================================
693 //function : DistributeSplitPoints
695 //=======================================================================
696 // After applying context to (seam) edge, distribute its indices on new edges,
697 // according to their parameters on that edge
698 static void DistributeSplitPoints (const Handle(ShapeExtend_WireData) &sbwd,
699 const TopoDS_Face myFace,
700 const Standard_Integer index,
701 const Standard_Integer nsplit,
702 TColStd_SequenceOfInteger& indexes,
703 const TColStd_SequenceOfReal& values)
705 Standard_Boolean isreversed = ( nsplit >0 && sbwd->Edge(index).Orientation() == TopAbs_REVERSED );
707 TColStd_Array1OfReal params(0,nsplit);
708 Standard_Integer i; // svv #1
709 for ( i=0; i < nsplit; i++ ) {
711 BRep_Tool::Range ( sbwd->Edge(index+i), myFace, f, l );
712 params.SetValue ( i, ( isreversed ? l : f ) );
715 for ( i=1; i <= indexes.Length() && indexes(i) < index; i++ );
716 for ( Standard_Integer shift = 1; i <= indexes.Length() && indexes(i) == index; i++ ) {
717 while ( shift < nsplit && isreversed != (Standard_Boolean) ( values(i) > params(shift) ) ) shift++;
718 indexes.SetValue ( i, index + shift - 1 );
720 for ( ; i <= indexes.Length(); i++ )
721 indexes.SetValue ( i, indexes(i) + nsplit - 1 );
725 //=======================================================================
726 //function : CheckByCurve3d
728 //=======================================================================
729 static Standard_Integer CheckByCurve3d (const gp_Pnt &pos,
730 const Handle(Geom_Curve) &c3d,
731 const Standard_Real param,
733 const Standard_Real tol)
735 if ( c3d.IsNull() ) return Standard_True;
736 gp_Pnt p = c3d->Value(param);
737 if ( T.Form() != gp_Identity ) p.Transform ( T );
738 return pos.SquareDistance ( p ) <= tol * tol;
742 //=======================================================================
743 //function : DefinePatch
745 //=======================================================================
746 static void DefinePatch (ShapeFix_WireSegment &wire, const Standard_Integer code,
747 const Standard_Boolean isCutByU, const Standard_Integer cutIndex,
748 const Standard_Integer number = -1)
750 Standard_Integer nb = (number > 0 ? number : wire.NbEdges());
752 if ( ! ( code & IOR_LEFT ) ) wire.DefineIUMin ( nb, cutIndex );
753 if ( ! ( code & IOR_RIGHT ) ) wire.DefineIUMax ( nb, cutIndex );
756 if ( ! ( code & IOR_RIGHT ) ) wire.DefineIVMin ( nb, cutIndex );
757 if ( ! ( code & IOR_LEFT ) ) wire.DefineIVMax ( nb, cutIndex );
762 //=======================================================================
763 //function : DefinePatchForWire
765 //=======================================================================
766 static void DefinePatchForWire(ShapeFix_WireSegment &wire, const Standard_Integer code,
767 const Standard_Boolean isCutByU, const Standard_Integer cutIndex)
769 for(Standard_Integer i = 1; i <= wire.NbEdges(); i++)
770 DefinePatch(wire,code,isCutByU,cutIndex,i);
774 //=======================================================================
775 //function : GetGridResolution
777 //=======================================================================
778 static Standard_Real GetGridResolution(const Handle(TColStd_HArray1OfReal) SplitValues,
779 const Standard_Integer cutIndex)
781 Standard_Integer nb = SplitValues->Length();
782 Standard_Real leftLen = (cutIndex > 1 ? SplitValues->Value(cutIndex) - SplitValues->Value(cutIndex-1) :
783 SplitValues->Value(nb) -SplitValues->Value(nb-1));
784 Standard_Real rigthLen =(cutIndex < nb ? SplitValues->Value(cutIndex+1)-SplitValues->Value(cutIndex) :
785 SplitValues->Value(2) - SplitValues->Value(1));
786 return Min(leftLen,rigthLen)/3.;
790 //=======================================================================
791 //function : SplitWire
793 //=======================================================================
795 ShapeFix_WireSegment ShapeFix_ComposeShell::SplitWire (ShapeFix_WireSegment &wire,
796 TColStd_SequenceOfInteger& indexes,
797 const TColStd_SequenceOfReal& values,
798 TopTools_SequenceOfShape& vertices,
799 const TColStd_SequenceOfInteger &SegmentCodes,
800 const Standard_Boolean isCutByU,
801 const Standard_Integer cutIndex)
804 ShapeFix_WireSegment result;
805 Handle(ShapeAnalysis_Surface) aSurfTool =
806 new ShapeAnalysis_Surface ( BRep_Tool::Surface (myFace) );
807 Standard_Integer nbSplits = indexes.Length();
808 ShapeAnalysis_Edge sae;
809 Standard_Integer start = 1;
810 TopAbs_Orientation anWireOrient = wire.Orientation();
812 if ( ! myLoc.IsIdentity() ) T = myLoc.Inverted().Transformation();
814 // Processing edge by edge (assuming that split points are sorted along the wire)
815 for ( Standard_Integer i = 1; i <= wire.NbEdges(); i++ ) {
817 // for already splitted seam edge, redistribute its splitting points
818 Standard_Integer nsplit = ApplyContext ( wire, i, Context() );
820 DistributeSplitPoints ( wire.WireData(), myFace, i, nsplit, indexes, values );
823 cout << "Error: ShapeFix_ComposeShell::SplitWire: edge dismissed" << endl;
829 TopoDS_Edge edge = wire.Edge(i);
831 Standard_Integer iumin, iumax, ivmin, ivmax;
832 wire.GetPatchIndex ( i, iumin, iumax, ivmin, ivmax );
834 // Position code for first segment of edge
835 Standard_Integer code = SegmentCodes ( start >1 ? start-1 : SegmentCodes.Length() );
837 // Defining split parameters on edge
838 Standard_Integer stop = start;
839 while ( stop <= nbSplits && indexes(stop) == i ) stop++;
840 if ( stop == start ) {
841 result.AddEdge ( 0, edge, iumin, iumax, ivmin, ivmax );
842 if(code!=0 || wire.Orientation()!=TopAbs_EXTERNAL) // pdn 0 code handling for extertnal wires
843 DefinePatch ( result, code, isCutByU, cutIndex );
846 //find non-manifold vertices on edge
847 TopTools_SequenceOfShape aNMVertices;
848 TopoDS_Iterator aIt(edge,Standard_False);
849 for( ; aIt.More(); aIt.Next()) {
850 if(aIt.Value().Orientation() != TopAbs_FORWARD &&
851 aIt.Value().Orientation() != TopAbs_REVERSED)
852 aNMVertices.Append(aIt.Value());
855 // Collect data on edge
856 Standard_Real tolEdge = BRep_Tool::Tolerance(edge);
857 Standard_Real tol = LimitTolerance( tolEdge );
858 TopoDS_Vertex prevV = sae.FirstVertex(edge);
859 TopoDS_Vertex lastV = sae.LastVertex(edge);
860 Standard_Real prevVTol = LimitTolerance( BRep_Tool::Tolerance(prevV) );
861 Standard_Real lastVTol = LimitTolerance( BRep_Tool::Tolerance(lastV) );
862 gp_Pnt prevVPnt = BRep_Tool::Pnt(prevV);
863 gp_Pnt lastVPnt = BRep_Tool::Pnt(lastV);
864 if ( T.Form() != gp_Identity ) {
865 prevVPnt.Transform ( T );
866 lastVPnt.Transform ( T );
869 Handle(Geom_Curve) c3d;
870 Standard_Real f3d, l3d;
871 if ( ! sae.Curve3d ( edge, c3d, f3d, l3d ) ) { // not a crime
876 Standard_Real firstPar, lastPar;
877 Handle(Geom2d_Curve) C2d;
878 if ( ! sae.PCurve ( edge, myFace, C2d, firstPar, lastPar ) ) {
879 myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL2 );
881 //finding sequence of non-manifold parameters
882 Standard_Integer nbNMVert = aNMVertices.Length();
883 TColStd_SequenceOfReal aNMVertParams;
885 Geom2dAdaptor_Curve adc(C2d);
887 Standard_Integer n =1;
888 for( ; n<= nbNMVert; n++) {
889 gp_Pnt apV = BRep_Tool::Pnt(TopoDS::Vertex(aNMVertices.Value(n)));
890 Standard_Real apar =firstPar;
891 Standard_Real adist2 =RealLast();
894 ShapeAnalysis_Curve asae;
895 adist2 = asae.Project(c3d,apV,Precision::Confusion(),aPproj,apar);
900 gp_Pnt2d aP2d = aSurfTool->ValueOfUV(apV,Precision::Confusion());
901 Extrema_ExtPC2d aExtr(aP2d,adc);
902 if(aExtr.IsDone() && aExtr.NbExt()) {
903 adist2 = aExtr.SquareDistance(1);
904 Standard_Integer index =1;
905 Standard_Integer k =2;
906 for( ; k <= aExtr.NbExt();k++) {
907 Standard_Real ad2 = aExtr.SquareDistance(k);
913 apar = aExtr.Point(index).Parameter();
917 aNMVertParams.Append(apar);
921 //pdn Claculating parametric shift
922 Standard_Boolean sp = (f3d == firstPar && l3d == lastPar);
923 Standard_Real span2d = lastPar - firstPar;
924 // Standard_Real ln2d = lastPar-prevPar;
925 // Standard_Real ln3d = l3d - f3d;
926 // Standard_Real fact = ln2d/ln3d;
927 // Standard_Real shift = prevPar - f3d*fact;
928 Standard_Real prevPar = firstPar;
929 gp_Pnt2d prevPnt2d = C2d->Value(prevPar);
930 gp_Pnt2d lastPnt2d = C2d->Value(lastPar);
931 gp_Pnt prevPnt = myGrid->Value ( prevPnt2d );
932 gp_Pnt lastPnt = myGrid->Value ( lastPnt2d );
933 Standard_Boolean isPeriodic = C2d->IsPeriodic();
934 Standard_Real aPeriod = (isPeriodic ? C2d->Period() :0.);
937 Standard_Integer NbEdgesStart = result.NbEdges();
938 Standard_Boolean splitted = Standard_False;
939 Standard_Real currPar=lastPar; //SK
940 for ( Standard_Integer j = start; j <= stop; prevPar = currPar, j++ ) {
941 if ( ! splitted && j >= stop ) { // no splitting at all
942 // code = SegmentCodes ( j >1 ? j-1 : SegmentCodes.Length() ); // classification code
945 currPar = ( j < stop ? values.Value(j) : lastPar );
947 //fix for case when pcurve is periodic and first parameter of edge is more than 2P
948 //method ShapeBuild_Edge::CopyRanges shift pcurve to range 0-2P and parameters of cutting
949 //should be shifted too. gka SAMTECH 28.07.06
951 if (currPar > (Max(lastPar,firstPar) +Precision::PConfusion()) ||
952 currPar < (Min(firstPar,lastPar)- Precision::PConfusion())) {
953 Standard_Real aShift = ShapeAnalysis::AdjustByPeriod(currPar, (firstPar+lastPar)*0.5,aPeriod);
961 // Try to adjust current splitting point to previous or end of edge
962 Standard_Boolean doCut = Standard_True;
964 if ( Abs ( currPar - lastPar ) < ::Precision::PConfusion() ) {
966 doCut = Standard_False;
968 else if ( Abs ( currPar - prevPar ) < ::Precision::PConfusion() ) {
969 vertices.Append ( prevV );
970 code = SegmentCodes ( j ); // classification code - update for next segment
971 continue; // no splitting at this point, go to next one
974 currPnt2d = C2d->Value(currPar);
975 currPnt = myGrid->Value ( currPnt2d );
976 if ( currPnt.Distance ( lastVPnt ) <= lastVTol &&
977 lastPnt.Distance ( currPnt ) <= tol &&
978 CheckByCurve3d ( lastVPnt, c3d, f3d+(currPar-firstPar)*(l3d-f3d)/span2d,
980 lastPnt.Distance ( myGrid->Value ( C2d->Value(0.5*(currPar+lastPar)) ) ) <= tol ) {
982 Standard_Real uRes = myUResolution;
983 Standard_Real vRes = myVResolution;
985 Standard_Real gridRes = GetGridResolution(myGrid->UJointValues(),cutIndex)/tol;
986 uRes = Min(myUResolution,gridRes);
989 Standard_Real gridRes = GetGridResolution(myGrid->VJointValues(),cutIndex)/tol;
990 vRes = Min(myVResolution,gridRes);
992 if ( IsCoincided ( lastPnt2d, currPnt2d, uRes, vRes, tol ) &&
993 IsCoincided ( lastPnt2d, C2d->Value(0.5*(currPar+lastPar)),
994 uRes, vRes, tol ) ) doCut = Standard_False;
996 else if ( currPnt.Distance ( prevVPnt ) <= prevVTol &&
997 prevPnt.Distance ( currPnt ) <= tol &&
998 CheckByCurve3d ( prevVPnt, c3d, f3d+(currPar-firstPar)*(l3d-f3d)/span2d,
1000 prevPnt.Distance ( myGrid->Value ( C2d->Value(0.5*(currPar+prevPar)) ) ) <= tol ) {
1002 Standard_Real uRes = myUResolution;
1003 Standard_Real vRes = myVResolution;
1005 Standard_Real gridRes = GetGridResolution(myGrid->UJointValues(),cutIndex)/tol;
1006 uRes = Min(myUResolution,gridRes);
1009 Standard_Real gridRes = GetGridResolution(myGrid->VJointValues(),cutIndex)/tol;
1010 vRes = Min(myVResolution,gridRes);
1012 if ( IsCoincided ( prevPnt2d, currPnt2d, uRes, vRes, tol ) &&
1013 IsCoincided ( prevPnt2d, C2d->Value(0.5*(currPar+prevPar)),
1014 uRes, vRes, tol ) ) {
1015 vertices.Append ( prevV );
1016 code = SegmentCodes ( j ); // classification code - update for next segment
1017 continue; // no splitting at this point, go to next one
1020 //:abv 28.05.02: OCC320 Sample_2: if maxtol = 1e-7, the vertex tolerance
1021 // is actually ignored - protect against new vertex on degenerated edge
1022 else if ( BRep_Tool::Degenerated(edge) && prevV.IsSame(lastV) ) {
1026 // classification code for current segment
1027 if ( j > start ) code = SegmentCodes ( j >1 ? j-1 : SegmentCodes.Length() );
1029 // if not adjusted, make new vertex
1031 B.MakeVertex ( V, currPnt.Transformed(myLoc.Transformation()), tolEdge );
1032 vertices.Append ( V );
1034 // else adjusted to end, fill all resting vertices
1035 else if ( ! doCut ) {
1036 for ( ; j < stop; j++ ) vertices.Append ( lastV );
1037 if ( ! splitted ) break; // no splitting at all
1040 else vertices.Append ( V );
1042 // When edge is about to be splitted, copy end vertices to protect
1043 // original shape from increasing tolerance after fixing SameParameter
1046 TopoDS_Shape emptyCopiedfV = prevV.EmptyCopied();
1047 TopoDS_Vertex fV = TopoDS::Vertex (emptyCopiedfV );
1048 Context()->Replace ( prevV, fV );
1050 if ( prevV.IsSame ( lastV ) ) {
1052 TopoDS_Shape tmpV = fV.Oriented ( lastV.Orientation() ) ;
1053 lV = TopoDS::Vertex (tmpV);
1057 TopoDS_Shape emptyCopied = lastV.EmptyCopied();
1058 lV = TopoDS::Vertex (emptyCopied);
1059 Context()->Replace ( lastV, lV );
1061 if ( V.IsSame ( lastV ) ) V = lV;
1062 else if ( V.IsSame ( prevV ) ) V = fV;
1067 // Splitting of the edge
1068 splitted = Standard_True;
1069 prevV.Orientation ( TopAbs_FORWARD );
1070 V.Orientation ( TopAbs_REVERSED );
1071 ShapeBuild_Edge sbe;
1072 TopoDS_Edge anInitEdge = edge;
1073 Standard_Boolean ismanifold = (edge.Orientation() == TopAbs_FORWARD ||
1074 edge.Orientation() == TopAbs_REVERSED);
1076 anInitEdge.Orientation(TopAbs_FORWARD);
1077 TopoDS_Edge newEdge = sbe.CopyReplaceVertices (anInitEdge, prevV, V );
1080 //addition internal vertices if they exists on edge
1081 Standard_Integer n =1;
1082 for( ; n <= aNMVertParams.Length(); n++) {
1083 Standard_Real apar = aNMVertParams.Value(n);
1084 TopoDS_Vertex aNMVert =TopoDS::Vertex(aNMVertices.Value(n));
1085 TopoDS_Vertex atmpV = TopoDS::Vertex(Context()->Apply(aNMVert));
1086 if(fabs(apar - prevPar) <= Precision::PConfusion()) {
1087 Context()->Replace(atmpV,prevV);
1088 aNMVertParams.Remove(n);
1089 aNMVertices.Remove(n);
1092 else if(fabs(apar - currPar) <= Precision::PConfusion()) {
1093 Context()->Replace(atmpV,V);
1094 aNMVertParams.Remove(n);
1095 aNMVertices.Remove(n);
1098 if(apar > prevPar && apar < currPar) {
1099 B.Add(newEdge,atmpV);
1100 aNMVertParams.Remove(n);
1101 aNMVertices.Remove(n);
1107 sbe.CopyPCurves ( newEdge, anInitEdge );
1110 Handle(ShapeAnalysis_TransferParameters) theTransferParamtool = GetTransferParamTool();
1111 theTransferParamtool->SetMaxTolerance(MaxTolerance());
1112 theTransferParamtool->Init(anInitEdge,myFace);
1113 theTransferParamtool->TransferRange(newEdge,prevPar,currPar,Standard_True);
1117 if(code == IOR_UNDEF) //tangential segment
1118 newEdge.Orientation(TopAbs_EXTERNAL);
1120 newEdge.Orientation(edge.Orientation());
1123 if(!sp && !BRep_Tool::Degenerated(newEdge))
1124 B.SameRange(newEdge, Standard_False);
1125 //pdn take into account 0 codes (if ext)
1126 if(code == 0 && wire.Orientation()==TopAbs_EXTERNAL){
1127 code = ( ( isCutByU == (Standard_Boolean)( j == 1 ) ) ? 1 : 2 );
1130 result.AddEdge ( 0, newEdge, iumin, iumax, ivmin, ivmax );
1131 DefinePatch ( result, code, isCutByU, cutIndex );
1133 // Changing prev parameters
1135 prevVTol = LimitTolerance( BRep_Tool::Tolerance ( V ) );
1136 prevVPnt = BRep_Tool::Pnt ( V );
1138 prevPnt2d = currPnt2d;
1143 // record replacement in context
1144 // NOTE: order of edges in the replacing wire corresponds to FORWARD orientation of the edge
1145 TopoDS_Wire resWire;
1146 B.MakeWire ( resWire );
1147 for ( Standard_Integer k=NbEdgesStart; k < result.NbEdges(); k++ ) {
1148 if ( edge.Orientation() == TopAbs_FORWARD || edge.Orientation() == TopAbs_INTERNAL)
1149 B.Add ( resWire, result.Edge(k+1) );
1150 else B.Add ( resWire, result.Edge(result.NbEdges()-k+NbEdgesStart) );
1152 Context()->Replace ( edge, resWire );
1155 if(anWireOrient == TopAbs_INTERNAL && code ==0) {
1156 ShapeBuild_Edge sbe;
1157 if(edge.Orientation() == TopAbs_INTERNAL)
1158 edge.Orientation(TopAbs_FORWARD);
1159 TopoDS_Edge e1 = sbe.Copy(edge,Standard_False);
1160 Handle(Geom2d_Curve) C2d2 = Handle(Geom2d_Curve)::DownCast(C2d->Copy());
1161 B.UpdateEdge(e1,C2d,C2d2,myFace,0.);
1162 e1.Orientation(TopAbs_EXTERNAL);
1163 Context()->Replace ( edge,e1);
1164 result.AddEdge ( 0,e1 , iumin, iumax, ivmin, ivmax );
1167 result.AddEdge ( 0, edge, iumin, iumax, ivmin, ivmax );
1168 if(code == 0 && wire.Orientation()==TopAbs_EXTERNAL){
1169 //pdn defining code for intersection of two isos
1170 code = ( ( isCutByU == (Standard_Boolean)( Abs(firstPar-currPar) < Abs(lastPar-currPar) ) ) ? 2 : 1 );
1172 DefinePatch ( result, code, isCutByU, cutIndex );
1175 result.Orientation ( anWireOrient );
1180 //=======================================================================
1181 //function : SplitByLine
1183 //=======================================================================
1185 Standard_Boolean ShapeFix_ComposeShell::SplitByLine (ShapeFix_WireSegment &wire,
1186 const gp_Lin2d &line,
1187 const Standard_Boolean isCutByU,
1188 const Standard_Integer cutIndex,
1189 TColStd_SequenceOfReal &SplitLinePar,
1190 TColStd_SequenceOfInteger &SplitLineCode,
1191 TopTools_SequenceOfShape &SplitLineVertex)
1193 ShapeAnalysis_Edge sae;
1194 // prepare data on cutting line
1195 Handle(Geom2d_Line) jC2d = new Geom2d_Line ( line );
1196 Geom2dAdaptor_Curve jGAC(jC2d);
1198 TColStd_SequenceOfInteger IntEdgeInd; // index of intersecting edge
1199 TColStd_SequenceOfReal IntEdgePar; // parameter of intersection point on edge
1200 TColStd_SequenceOfReal IntLinePar; // parameter of intersection point on line
1202 Standard_Boolean isnonmanifold = (wire.Orientation() == TopAbs_INTERNAL);
1203 //gka correction for non-manifold vertices SAMTECH
1204 if(wire.IsVertex()) {
1205 Handle(ShapeAnalysis_Surface) aSurfTool = new ShapeAnalysis_Surface ( BRep_Tool::Surface (myFace) );
1206 TopoDS_Vertex aVert = wire.GetVertex();
1207 gp_Pnt aP3d = BRep_Tool::Pnt(aVert);
1208 gp_Pnt2d aP2d = aSurfTool->ValueOfUV(aP3d,Precision::Confusion());
1209 Standard_Real dev =0.;
1210 Standard_Integer code = PointLinePosition(aP2d,line,dev);
1211 if(code != IOR_UNDEF)
1212 return Standard_False;
1213 Standard_Real par = ParamPointOnLine (aP2d,line);
1214 SplitLinePar.Append ( par );
1215 //splitting codes for non-manifold topology should be tangential
1216 SplitLineCode.Append (ITP_TANG); //ITP_INTER);
1217 TopoDS_Vertex aVertNew;
1219 aB.MakeVertex(aVertNew,aP3d,BRep_Tool::Tolerance(aVert));
1220 aVertNew.Orientation(TopAbs_FORWARD);
1221 Context()->Replace(aVert,aVertNew);
1222 SplitLineVertex.Append (aVertNew);
1223 wire.SetVertex(aVertNew);
1224 return Standard_True;
1226 const Handle(ShapeExtend_WireData) sewd = wire.WireData();
1228 Standard_Integer nbe = sewd->NbEdges();
1230 //:abv 31.10.01: for closed mode
1231 Standard_Integer closedDir = 0;
1232 if ( myClosedMode ) {
1233 if ( myUClosed && Abs ( line.Direction().X() ) < ::Precision::PConfusion() )
1235 else if ( myVClosed && Abs ( line.Direction().Y() ) < ::Precision::PConfusion() )
1238 Standard_Real halfPeriod = 0.5 * ( closedDir ? closedDir <0 ? myUPeriod : myVPeriod : 0. );
1240 //============================================
1241 // make intersections and collect all data on intersection points
1242 Standard_Integer firstCode=0, prevCode=0;
1243 gp_Pnt2d firstPos, prevPos;
1244 Standard_Real firstDev=0., prevDev=0.;
1245 for (Standard_Integer iedge = 1; iedge <= nbe; iedge++) {
1247 TopoDS_Edge E= sewd->Edge ( iedge );
1248 Standard_Boolean isreversed = ( E.Orientation() == TopAbs_REVERSED );
1251 Handle(Geom2d_Curve) c2d;
1252 if ( ! sae.PCurve ( E, myFace, c2d, f, l, Standard_False ) ) continue;
1255 gp_Pnt2d posf = c2d->Value(f), posl = c2d->Value(l);
1256 gp_XY pppf = posf.XY(), pppl = posl.XY();
1258 // In case of ClosedMode, adjust curve and end points to period on closed surface
1259 //:abv 16.10.01: Ziegler_CADDY01.sat -18: if pcurve is longer than period,
1260 // ensure processing of all intersections
1261 Standard_Integer nbIter = 1;
1262 gp_Vec2d shiftNext(0.,0.);
1263 if ( myClosedMode ) {
1265 // get bounding box of pcurve
1266 ShapeAnalysis_Curve sac;
1268 sac.FillBndBox ( c2d, f, l, 11, Standard_True, box );
1269 Standard_Real umin, vmin, umax, vmax;
1270 box.Get ( umin, vmin, umax, vmax );
1272 // compute shifts and adjust points adjust
1273 if ( closedDir < 0 ) {
1274 Standard_Real x = line.Location().X();
1275 Standard_Real shift = ShapeAnalysis::AdjustToPeriod ( umin, x-myUPeriod, x );
1276 if ( shift != 0. ) {
1277 c2d = Handle(Geom2d_Curve)::DownCast ( c2d->Copy() );
1278 gp_Vec2d V ( shift, 0. );
1279 c2d->Translate ( V );
1280 pppf.SetX ( pppf.X() + shift );
1281 pppl.SetX ( pppl.X() + shift );
1283 shiftNext.SetX ( -myUPeriod );
1284 nbIter = (Standard_Integer)( 1 + Abs ( umax + shift - x ) / myUPeriod );
1285 shift = ShapeAnalysis::AdjustByPeriod ( posf.X(), x, myUPeriod );
1286 posf.SetX ( posf.X() + shift );
1287 shift = ShapeAnalysis::AdjustByPeriod ( posl.X(), x, myUPeriod );
1288 posl.SetX ( posl.X() + shift );
1290 else if ( closedDir > 0 ) {
1291 Standard_Real y = line.Location().Y();
1292 Standard_Real shift = ShapeAnalysis::AdjustToPeriod ( vmin, y-myVPeriod, y );
1293 if ( shift != 0. ) {
1294 c2d = Handle(Geom2d_Curve)::DownCast ( c2d->Copy() );
1295 gp_Vec2d V ( 0., shift );
1296 c2d->Translate ( V );
1297 pppf.SetY ( pppf.Y() + shift );
1298 pppl.SetY ( pppl.Y() + shift );
1300 shiftNext.SetY ( -myVPeriod );
1301 nbIter = (Standard_Integer)( 1 + Abs ( umax + shift - y ) / myVPeriod );
1302 shift = ShapeAnalysis::AdjustByPeriod ( posf.Y(), y, myVPeriod );
1303 posf.SetY ( posf.Y() + shift );
1304 shift = ShapeAnalysis::AdjustByPeriod ( posl.Y(), y, myVPeriod );
1305 posl.SetY ( posl.Y() + shift );
1309 // detect intersections at junction of two edges
1310 gp_Pnt2d pos = ( isreversed ? posl : posf );
1312 Standard_Integer code = PointLinePosition ( pos, line, dev );
1313 if ( iedge ==1 ) { firstCode = code; firstPos = pos; firstDev = dev; }
1314 else if ( code == IOR_UNDEF || code != prevCode ) {
1315 if ( ! closedDir || Abs ( dev - prevDev ) < halfPeriod ) {
1316 IntLinePar.Append ( ParamPointsOnLine ( pos, prevPos, line ) ); // !! - maybe compute exactly ?
1317 IntEdgePar.Append ( isreversed ? l : f );
1318 IntEdgeInd.Append ( iedge );
1322 // fill data on end point (for next edge)
1323 pos = ( isreversed ? posf : posl );
1324 prevCode = PointLinePosition ( pos, line, prevDev );
1327 // cycle with shift in order to track all possible intersections
1328 for ( Standard_Integer iter=1; iter <= nbIter; iter++ ) {
1330 // data for intersection
1331 IntRes2d_Domain iDom ( pppf, f, TOLINT, pppl, l, TOLINT );
1332 Geom2dAdaptor_Curve iGAC(c2d);
1335 Geom2dInt_GInter Inter;
1336 Inter.Perform ( jGAC, /*jDom,*/ iGAC, iDom, TOLINT, TOLINT );
1338 // Fill arrays with new intersection points
1339 if ( Inter.IsDone() ) {
1342 for ( i = 1; i <= Inter.NbPoints(); i++ ) {
1343 IntRes2d_IntersectionPoint IP = Inter.Point (i);
1344 IntLinePar.Append ( IP.ParamOnFirst() );
1345 IntEdgePar.Append ( IP.ParamOnSecond() );
1347 for ( i = 1; i <= Inter.NbSegments(); i++ ) {
1348 IntRes2d_IntersectionSegment IS = Inter.Segment (i);
1349 if ( IS.HasFirstPoint() ) {
1350 IntRes2d_IntersectionPoint IP = IS.FirstPoint();
1351 IntLinePar.Append ( IP.ParamOnFirst() );
1352 IntEdgePar.Append ( IP.ParamOnSecond() );
1354 if ( IS.HasLastPoint() ) {
1355 IntRes2d_IntersectionPoint IP = IS.LastPoint();
1356 IntLinePar.Append ( IP.ParamOnFirst() );
1357 IntEdgePar.Append ( IP.ParamOnSecond() );
1362 if ( iter < nbIter ) {
1363 if ( iter == 1 ) c2d = Handle(Geom2d_Curve)::DownCast ( c2d->Copy() );
1364 pppf += shiftNext.XY();
1365 pppl += shiftNext.XY();
1366 c2d->Translate ( shiftNext );
1370 Standard_Integer start = IntEdgeInd.Length() + 1; // first of the new points
1372 // Move all points into range [f,l] (intersector sometimes gives params out of range)
1374 for ( i = start; i <= IntEdgePar.Length(); i++ ) {
1375 if ( IntEdgePar(i) < f ) IntEdgePar.SetValue ( i, f );
1376 else if ( IntEdgePar(i) > l ) IntEdgePar.SetValue ( i, l );
1379 // Sort by parameter on edge
1380 for ( i = IntEdgePar.Length(); i > start; i-- )
1381 for ( Standard_Integer j = start; j < i; j++ ) {
1382 if ( isreversed == (Standard_Boolean) ( IntEdgePar(j+1) < IntEdgePar(j) ) ) continue;
1383 IntLinePar.Exchange ( j, j+1 );
1384 IntEdgePar.Exchange ( j, j+1 );
1388 for ( i = start; i <= IntEdgePar.Length(); i++ )
1389 IntEdgeInd.Append ( iedge );
1391 // Detect intersection at closing point
1392 // Only wires which are not EXTERNAL are considered (as closed)
1393 if ( iedge == nbe && wire.Orientation() != TopAbs_EXTERNAL &&
1394 wire.Orientation() != TopAbs_INTERNAL &&
1395 ( prevCode == IOR_UNDEF || prevCode != firstCode ) ) {
1396 if ( ! closedDir || Abs ( firstDev - prevDev ) < halfPeriod ) {
1397 IntLinePar.Append ( ParamPointsOnLine ( pos, firstPos, line ) );
1398 IntEdgePar.Append ( isreversed ? f : l );
1399 IntEdgeInd.Append ( iedge );
1404 if ( IntEdgePar.Length() <1 ) {
1405 //pdn Defining position of wire. There is no intersection, so by any point.
1406 DefinePatchForWire ( wire, firstCode, isCutByU, cutIndex );
1407 return Standard_False; //pdn ??
1410 //======================================
1411 // Fill sequence of transition codes for intersection points
1412 TColStd_SequenceOfInteger IntCode; // parameter of intersection point on line
1413 TColStd_SequenceOfInteger SegmentCodes; // classification codes for segments of wire
1415 // remove duplicated points to ensure correct results of ComputeCode
1416 Standard_Integer i, j = IntEdgePar.Length();
1417 if ( myClosedMode && j >1 ) {
1418 for ( i = 1; i <= IntEdgePar.Length(); ) {
1419 if ( i == j ) break;
1420 if ( IntEdgeInd(i) == IntEdgeInd(j) &&
1421 Abs ( IntEdgePar(i) - IntEdgePar(j) ) < ::Precision::PConfusion() ) {
1422 IntLinePar.Remove(i);
1423 IntEdgePar.Remove(i);
1424 IntEdgeInd.Remove(i);
1428 else if ( nbe ==1 || IntEdgeInd(i) == (IntEdgeInd(j)%nbe)+1 ) {
1429 TopoDS_Edge E1 = sewd->Edge ( IntEdgeInd(j) );
1430 TopoDS_Edge E2 = sewd->Edge ( IntEdgeInd(i) );
1431 Standard_Real a1, b1, a2, b2;
1432 BRep_Tool::Range ( E1, myFace, a1, b1 );
1433 BRep_Tool::Range ( E2, myFace, a2, b2 );
1434 if ( Abs ( IntEdgePar(j) - ( E1.Orientation() == TopAbs_FORWARD ? b1 : a1 ) ) < ::Precision::PConfusion() &&
1435 Abs ( IntEdgePar(i) - ( E2.Orientation() == TopAbs_FORWARD ? a2 : b2 ) ) < ::Precision::PConfusion() ) {
1436 IntLinePar.Remove(i);
1437 IntEdgePar.Remove(i);
1438 IntEdgeInd.Remove(i);
1447 // Compute segment codes (left side of line, right or tangential)
1448 for ( i=1; i <= IntEdgePar.Length(); i++ ) {
1449 j = ( i < IntEdgePar.Length() ? i + 1 : 1 );
1450 Standard_Integer code = ComputeCode ( sewd, line, IntEdgeInd(i), IntEdgeInd(j),
1451 IntEdgePar(i), IntEdgePar(j),isnonmanifold );
1452 SegmentCodes.Append ( code );
1455 // for EXTERNAL wire, i.e. another joint line, every point is double intersection
1456 if ( wire.Orientation() == TopAbs_EXTERNAL ) {
1457 for ( i=1; i <= IntEdgePar.Length(); i++ )
1458 IntCode.Append ( ITP_TANG | IOR_BOTH );
1460 // For real (closed) wire, analyze tangencies
1462 if(wire.Orientation() != TopAbs_INTERNAL) {
1463 // Two consecutive tangential segments are considered as one, merge them.
1464 for ( i=1; i <= IntEdgePar.Length(); i++ ) {
1465 j = ( i > 1 ? i-1 : IntEdgePar.Length() );
1466 if ( SegmentCodes(j) == IOR_UNDEF &&
1467 SegmentCodes(i) == IOR_UNDEF ) {
1468 IntEdgeInd.Remove(i);
1469 IntEdgePar.Remove(i);
1470 IntLinePar.Remove(i);
1471 SegmentCodes.Remove(i);
1476 //pdn exit if all split points removed
1477 if ( IntEdgePar.Length() <1 ) {
1478 //DefinePatchForWire ( wire, firstCode, isCutByU, cutIndex );
1479 return Standard_False; //pdn ??
1482 // Analyze type of intersection point and encode it
1483 // Three kinds of points (ITP): clear intersection, tangency in-point,
1484 // beginning and end of tangential segment.
1485 // Orientation (IOR) tells on which side of line edge crosses it
1486 j = IntEdgePar.Length();
1487 for ( i=1; i <= IntEdgePar.Length(); j = i++ ) {
1488 Standard_Integer codej = SegmentCodes(j);
1489 Standard_Integer codei = SegmentCodes(i);
1490 if ( myClosedMode ) {
1491 if ( ( codej & IOR_BOTH ) == IOR_BOTH ) //IOR_LEFT : IOR_RIGHT
1492 codej = ( codej & IOR_POS ? IOR_RIGHT : IOR_LEFT );
1493 if ( ( codei & IOR_BOTH ) == IOR_BOTH ) //IOR_RIGHT : IOR_LEFT
1494 codei = ( codei & IOR_POS ? IOR_LEFT : IOR_RIGHT );
1496 Standard_Integer ipcode = ( codej | codei );
1497 if ( codej == IOR_UNDEF ) { // previous segment was tangency
1498 if ( IntLinePar(i) > IntLinePar (j) )
1499 ipcode |= ITP_ENDSEG; // end of segment
1500 else ipcode |= ITP_BEGSEG; // beginning of segment
1502 else if ( codei == IOR_UNDEF ) { // current segment is tangency
1503 if ( IntLinePar ( i < IntLinePar.Length() ? i+1 : 1 ) > IntLinePar(i) )
1504 ipcode |= ITP_BEGSEG; // beginning of segment
1505 else ipcode |= ITP_ENDSEG; // end of segment
1507 //internal wire can be only tangent
1508 else if ( i == j ) ipcode |= ( ( ipcode & IOR_BOTH ) == IOR_BOTH && !isnonmanifold ? ITP_INTER : ITP_TANG );
1509 else if ( codei == codej || isnonmanifold) ipcode |= ITP_TANG; // tangency in-point
1510 else ipcode |= ITP_INTER; // standard crossing
1511 IntCode.Append ( ipcode );
1515 //=======================================
1516 // Split edges in the wire by intersection points and fill vertices array
1517 TopTools_SequenceOfShape IntVertices;
1518 wire = SplitWire ( wire, IntEdgeInd, IntEdgePar, IntVertices,
1519 SegmentCodes, isCutByU, cutIndex );
1521 // add all data to input arrays
1522 for ( i=1; i <= IntLinePar.Length(); i++ ) {
1523 SplitLinePar.Append ( IntLinePar(i) );
1524 SplitLineCode.Append ( IntCode(i) );
1525 SplitLineVertex.Append ( IntVertices(i) );
1528 return Standard_True;
1532 //=======================================================================
1533 //function : SplitByLine
1535 //=======================================================================
1537 void ShapeFix_ComposeShell::SplitByLine (ShapeFix_SequenceOfWireSegment &wires,
1538 const gp_Lin2d &line,
1539 const Standard_Boolean isCutByU,
1540 const Standard_Integer cutIndex)
1542 TColStd_SequenceOfReal SplitLinePar;
1543 TColStd_SequenceOfInteger SplitLineCode;
1544 TopTools_SequenceOfShape SplitLineVertex;
1546 // split wires one by one, collecting data on intersection points
1547 Standard_Integer i; // svv #1
1548 for ( i=1; i <= wires.Length(); i++ ) {
1549 SplitByLine ( wires(i), line, isCutByU, cutIndex,
1550 SplitLinePar, SplitLineCode, SplitLineVertex );
1553 // sort intersection points along parameter on cutting line
1554 for ( i = SplitLinePar.Length(); i >1; i-- )
1555 for ( Standard_Integer j=1; j < i; j++ ) {
1556 if ( SplitLinePar(j) > SplitLinePar(j+1) ) {
1557 SplitLinePar.Exchange ( j, j+1 );
1558 SplitLineCode.Exchange ( j, j+1 );
1559 SplitLineVertex.Exchange ( j, j+1 );
1563 // merge null-length tangential segments into one-point tangencies or intersections
1564 for ( i = 1; i < SplitLinePar.Length(); i++ ) {
1565 if ( Abs ( SplitLinePar(i+1) - SplitLinePar(i) ) > ::Precision::PConfusion() ) continue;
1566 if ( ( SplitLineCode(i) & ITP_ENDSEG &&
1567 SplitLineCode(i+1) & ITP_BEGSEG ) ||
1568 ( SplitLineCode(i) & ITP_BEGSEG &&
1569 SplitLineCode(i+1) & ITP_ENDSEG ) ) {
1570 Standard_Integer code = ( SplitLineCode(i) | SplitLineCode(i+1) ) & IOR_BOTH;
1571 SplitLineCode.SetValue ( i, code | ( code == IOR_BOTH ? ITP_INTER : ITP_TANG ) );
1572 SplitLinePar.Remove(i+1);
1573 SplitLineCode.Remove(i+1);
1574 SplitLineVertex.Remove(i+1);
1578 // go along line, split it by intersection points and create edges
1579 // (only for internal parts, in particular not for tangential segments)
1581 Standard_Integer parity = 0; // 0 - out, 1 - in
1582 Standard_Integer halfparity = 0; // left/right for tangential segments
1583 Standard_Integer tanglevel = 0; // tangency nesting level
1584 for ( i = 1; i <= SplitLinePar.Length(); i++ ) {
1585 Standard_Integer code = SplitLineCode(i);
1586 Standard_Boolean interior = ( ! tanglevel && parity % 2 ); // create an edge
1587 if ( code & ITP_INTER ) { // crossing
1590 else if ( code & ITP_BEGSEG ) { // beginning of tangential segment
1592 if ( ! halfparity ) halfparity = ( code & IOR_BOTH );
1593 else if ( halfparity != ( code & IOR_BOTH ) ) parity++;
1595 else if ( code & ITP_ENDSEG ) { // end of tangential segment
1597 if ( ! halfparity ) halfparity = ( code & IOR_BOTH );
1598 else if ( halfparity != ( code & IOR_BOTH ) ) parity++;
1600 if ( tanglevel <0 ) {
1601 // myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL4 );
1603 cout << "Warning: ShapeFix_ComposeShell::SplitByLine: tangency level <0 !" << endl;
1606 if ( ! interior ) continue;
1608 // apply context to vertices (to perform replacing/merging vertices)
1610 TopoDS_Shape tmpV1 = Context()->Apply ( SplitLineVertex(i-1) );
1611 TopoDS_Shape tmpV2 = Context()->Apply ( SplitLineVertex(i) );
1612 TopoDS_Vertex V1 = TopoDS::Vertex ( tmpV1 );
1613 TopoDS_Vertex V2 = TopoDS::Vertex ( tmpV2 );
1614 // protection against creating null-length edges
1615 if ( SplitLinePar(i) - SplitLinePar(i-1) < ::Precision::PConfusion() ) {
1618 cout << "Info: ShapeFix_ComposeShell::SplitByLine: Short segment ignored" << endl;
1620 if ( ! V1.IsSame ( V2 ) ) { // merge coincident vertices
1621 ShapeBuild_Vertex sbv;
1622 TopoDS_Vertex V = sbv.CombineVertex ( V1, V2 );
1623 Context()->Replace ( V1, V.Oriented ( V1.Orientation() ) );
1624 Context()->Replace ( V2, V.Oriented ( V2.Orientation() ) );
1627 cout << "Info: ShapeFix_ComposeShell::SplitByLine: Coincided vertices merged" << endl;
1633 // create an edge (without 3d curve), put it in wire segment and add to sequence
1634 // NOTE: i here is always >1
1636 B.MakeEdge ( edge );
1637 V1.Orientation ( TopAbs_FORWARD );
1638 V2.Orientation ( TopAbs_REVERSED );
1641 Handle(Geom2d_Line) Lin1 = new Geom2d_Line ( line );
1642 Handle(Geom2d_Line) Lin2 = new Geom2d_Line ( line );
1643 B.UpdateEdge ( edge, Lin1, Lin2, myFace, ::Precision::Confusion() );
1644 B.Range ( edge, myFace, SplitLinePar(i-1), SplitLinePar(i) );
1646 Handle(ShapeExtend_WireData) sbwd = new ShapeExtend_WireData;
1648 ShapeFix_WireSegment seg ( sbwd, TopAbs_EXTERNAL );
1650 // set patch indices
1651 DefinePatch ( seg, IOR_UNDEF, isCutByU, cutIndex );
1653 seg.DefineIUMin ( 1, GetPatchIndex ( SplitLinePar(i-1)+::Precision::PConfusion(),
1654 myGrid->UJointValues(), myUClosed ) );
1655 seg.DefineIUMax ( 1, GetPatchIndex ( SplitLinePar(i)-::Precision::PConfusion(),
1656 myGrid->UJointValues(), myUClosed ) + 1 );
1659 seg.DefineIVMin ( 1, GetPatchIndex ( SplitLinePar(i-1)+::Precision::PConfusion(),
1660 myGrid->VJointValues(), myVClosed ) );
1661 seg.DefineIVMax ( 1, GetPatchIndex ( SplitLinePar(i)-::Precision::PConfusion(),
1662 myGrid->VJointValues(), myVClosed ) + 1 );
1665 wires.Append ( seg );
1668 myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL4 );
1670 cout << "Error: ShapeFix_ComposeShell::SplitByLine: parity error" << endl;
1674 // Apply context to all wires to perform all recorded replacements/merging
1675 for ( i=1; i <= wires.Length(); i++ ) {
1676 for ( Standard_Integer j=1; j <= wires(i).NbEdges(); )
1677 j += ApplyContext ( wires(i), j, Context() );
1682 //=======================================================================
1683 //function : SplitByGrid
1685 //=======================================================================
1687 void ShapeFix_ComposeShell::SplitByGrid (ShapeFix_SequenceOfWireSegment &seqw)
1689 // process splitting by U- anv V-seams (i.e. U=const and V=const curves)
1690 // closed composite surface is processed as periodic
1691 Standard_Real Uf,Ul,Vf,Vl;
1692 BRepTools::UVBounds(myFace,Uf,Ul,Vf,Vl);
1693 Standard_Real Umin,Umax,Vmin,Vmax;
1694 myGrid->Bounds(Umin,Umax,Vmin,Vmax);
1695 Standard_Real pprec = ::Precision::PConfusion();
1698 Standard_Integer i; // svv #1
1699 for ( i = ( myUClosed ? 1 : 2 ); i <= myGrid->NbUPatches(); i++ ) {
1700 gp_Pnt2d pos ( myGrid->UJointValue(i), 0. ); // 0. - for infinite ranges: myGrid->VJointValue(1) ;
1701 gp_Lin2d line ( pos, gp_Dir2d ( 0., 1. ) );
1702 if ( ! myClosedMode && myUClosed ) {
1703 Standard_Real period = Umax - Umin;
1704 Standard_Real X = pos.X();
1705 Standard_Real sh = ShapeAnalysis::AdjustToPeriod(X,Uf, Uf+period);
1706 for( ; X+sh <= Ul+pprec; sh += period ) {
1707 gp_Lin2d ln = line.Translated(gp_Vec2d(sh,0));
1708 Standard_Integer cutIndex = GetPatchIndex ( X+sh+pprec, myGrid->UJointValues(), myUClosed );
1709 SplitByLine ( seqw, ln, Standard_True, cutIndex );
1713 SplitByLine ( seqw, line, Standard_True, i );
1717 for ( i = ( myVClosed ? 1 : 2 ); i <= myGrid->NbVPatches(); i++ ) {
1718 gp_Pnt2d pos ( 0., myGrid->VJointValue(i) );
1719 gp_Lin2d line ( pos, gp_Dir2d ( 1., 0. ) );
1720 if ( ! myClosedMode && myVClosed ) {
1721 Standard_Real period = Vmax - Vmin;
1722 Standard_Real Y = pos.Y();
1723 Standard_Real sh = ShapeAnalysis::AdjustToPeriod(Y,Vf, Vf+period);
1724 for( ; Y+sh <= Vl+pprec; sh += period) {
1725 gp_Lin2d ln = line.Translated(gp_Vec2d(0,sh));
1726 Standard_Integer cutIndex = GetPatchIndex ( Y+sh+pprec, myGrid->VJointValues(), myVClosed );
1727 SplitByLine ( seqw, ln, Standard_False, cutIndex );
1731 SplitByLine ( seqw, line, Standard_False, i );
1734 // limit patch indices to be in range of grid (extended for periodic)
1735 Standard_Integer iumin = GetPatchIndex ( Uf+pprec, myGrid->UJointValues(), myUClosed );
1736 Standard_Integer iumax = GetPatchIndex ( Ul-pprec, myGrid->UJointValues(), myUClosed ) + 1;
1737 for ( i=1; i <= seqw.Length(); i++ ) {
1738 ShapeFix_WireSegment &wire = seqw(i);
1739 for ( Standard_Integer j=1; j <= wire.NbEdges(); j++ ) {
1740 wire.DefineIUMin ( j, iumin );
1741 wire.DefineIUMax ( j, iumax );
1744 Standard_Integer ivmin = GetPatchIndex ( Vf+pprec, myGrid->VJointValues(), myVClosed );
1745 Standard_Integer ivmax = GetPatchIndex ( Vl-pprec, myGrid->VJointValues(), myVClosed ) + 1;
1746 for ( i=1; i <= seqw.Length(); i++ ) {
1747 ShapeFix_WireSegment &wire = seqw(i);
1748 for ( Standard_Integer j=1; j <= wire.NbEdges(); j++ ) {
1749 wire.DefineIVMin ( j, ivmin );
1750 wire.DefineIVMax ( j, ivmax );
1756 //=======================================================================
1757 //function : BreakWires
1759 //=======================================================================
1761 void ShapeFix_ComposeShell::BreakWires (ShapeFix_SequenceOfWireSegment &seqw)
1764 // split all the wires by vertices
1765 TopTools_MapOfShape splitVertices;
1766 ShapeAnalysis_Edge sae;
1768 // first collect splitting vertices
1769 Standard_Integer i; // svv #1
1770 for ( i=1; i <= seqw.Length(); i++ ) {
1771 TopAbs_Orientation ori_wire = seqw(i).Orientation();
1772 if ( ori_wire != TopAbs_EXTERNAL &&
1773 ori_wire != TopAbs_INTERNAL) continue;
1775 Handle(ShapeExtend_WireData) sbwd = seqw(i).WireData();
1776 for ( Standard_Integer j=1; j <= sbwd->NbEdges(); j++ ) {
1777 TopoDS_Edge edge = sbwd->Edge ( j );
1778 TopAbs_Orientation ori_edge = (ori_wire == TopAbs_EXTERNAL ? ori_wire : edge.Orientation());
1779 if(ori_edge == TopAbs_EXTERNAL) {
1780 splitVertices.Add ( sae.FirstVertex ( edge ) );
1781 splitVertices.Add ( sae.LastVertex ( edge ) );
1786 // and then split each vire
1787 // Here each wire is supposed to be connected (while probably not closed)
1788 for ( i=1; i <= seqw.Length(); i++ ) {
1789 TopAbs_Orientation ori = seqw(i).Orientation();
1790 ShapeFix_WireSegment wire = seqw(i);
1793 Handle(ShapeExtend_WireData) sbwd = wire.WireData();
1795 // find first vertex for split
1796 Standard_Integer j; // svv #1
1797 for ( j=1; j <= sbwd->NbEdges(); j++ ) {
1798 TopoDS_Vertex V = sae.FirstVertex ( sbwd->Edge(j) );
1799 if ( splitVertices.Contains ( V ) ) break;
1801 if ( j > sbwd->NbEdges() ) continue; // splitting not needed
1803 // if first split of closed edge is not its start, make permutation
1804 Standard_Integer shift = 0;
1805 if ( j >1 && ! myClosedMode && wire.IsClosed() ) {
1806 TopoDS_Vertex V = sae.FirstVertex ( sbwd->Edge(1) );
1807 if ( ! splitVertices.Contains ( V ) )
1809 // wire.SetLast ( j-1 );
1812 // perform splitting
1813 Standard_Integer nbnew = 0;
1814 ShapeFix_WireSegment newwire;
1815 TopAbs_Orientation curOri = ori;
1816 for ( Standard_Integer ind=1; ind <= sbwd->NbEdges(); ind++ ) {
1817 j = 1 + ( ind - 1 + shift ) % sbwd->NbEdges();
1818 TopoDS_Edge edge = sbwd->Edge(j);
1819 TopoDS_Vertex V = sae.FirstVertex ( edge );
1820 if ( ind==1 || splitVertices.Contains ( V ) ) {
1821 if ( newwire.NbEdges() ) {
1822 newwire.Orientation ( curOri );
1823 // ShapeFix_WireSegment seg ( newwire, ori );
1824 seqw.InsertBefore ( i++, newwire );
1830 Standard_Integer iumin, iumax, ivmin, ivmax;
1831 wire.GetPatchIndex ( j, iumin, iumax, ivmin, ivmax );
1832 if(ori == TopAbs_INTERNAL && edge.Orientation() == TopAbs_EXTERNAL ) {
1833 curOri = TopAbs_EXTERNAL;
1834 edge.Orientation(TopAbs_FORWARD);
1838 newwire.AddEdge ( 0, edge, iumin, iumax, ivmin, ivmax );
1841 newwire.Orientation ( curOri );
1842 // ShapeFix_WireSegment seg ( newwire, ori );
1843 seqw.SetValue ( i, newwire );
1849 //=======================================================================
1850 //function : IsShortSegment
1851 //purpose : auxilary
1852 //=======================================================================
1853 // BUC60035 2053: check if wire segment is very short (in order not to skip it)
1855 // 1 - short even in 2d (to be taken always)
1856 // -1 - short in 3d but not in 2d (to be checked after algo and atteching to
1857 // another wire if alone)
1858 static Standard_Integer IsShortSegment (const ShapeFix_WireSegment &seg,
1859 const TopoDS_Face myFace,
1860 const Handle(Geom_Surface)& myGrid,
1861 const TopLoc_Location &myLoc,
1862 const Standard_Real UResolution,
1863 const Standard_Real VResolution)
1865 TopoDS_Vertex Vf = seg.FirstVertex();
1866 if ( ! Vf.IsSame ( seg.LastVertex() ) ) return Standard_False;
1868 gp_Pnt pnt = BRep_Tool::Pnt(Vf);
1869 Standard_Real tol = BRep_Tool::Tolerance(Vf);
1870 Standard_Real tol2 = tol*tol;
1872 Standard_Integer code = 1;
1873 ShapeAnalysis_Edge sae;
1874 Handle(ShapeExtend_WireData) sbwd = seg.WireData();
1875 for ( Standard_Integer i=1; i <= sbwd->NbEdges(); i++ ) {
1876 TopoDS_Edge edge = sbwd->Edge ( i );
1877 if ( ! Vf.IsSame ( sae.LastVertex ( edge ) ) ) return Standard_False;
1878 Handle(Geom2d_Curve) c2d;
1880 if ( ! sae.PCurve ( edge, myFace, c2d, f, l ) ) continue;
1883 gp_Pnt2d endPnt = c2d->Value(l);
1884 gp_Pnt2d midPnt = c2d->Value((f+l)/2);
1885 if ( ! IsCoincided ( endPnt, midPnt, UResolution, VResolution, tol ) ) code = -1;
1888 gp_Pnt midPnt3d = myGrid->Value(midPnt.X(),midPnt.Y());
1889 if ( ! myLoc.IsIdentity() ) midPnt3d.Transform ( myLoc.Transformation() );
1890 if ( midPnt3d.SquareDistance(pnt) > tol2) return 0;
1896 //=======================================================================
1897 //function : IsSamePatch
1898 //purpose : auxilary
1899 //=======================================================================
1900 static Standard_Boolean IsSamePatch (const ShapeFix_WireSegment wire,
1901 const Standard_Integer NU,
1902 const Standard_Integer NV,
1903 Standard_Integer &iumin,
1904 Standard_Integer &iumax,
1905 Standard_Integer &ivmin,
1906 Standard_Integer &ivmax,
1907 const Standard_Boolean extend=Standard_False)
1909 // get patch indices for current segment
1910 Standard_Integer jumin, jumax, jvmin, jvmax;
1911 wire.GetPatchIndex ( 1, jumin, jumax, jvmin, jvmax );
1913 // shift to the same period
1914 Standard_Integer du=0, dv=0;
1915 if ( jumin - iumin > NU ) du =-( jumin - iumin ) / NU;
1916 else if ( iumin - jumin > NU ) du = ( iumin - jumin ) / NU;
1917 if ( jvmin - ivmin > NV ) dv =-( jvmin - ivmin ) / NV;
1918 else if ( ivmin - jvmin > NV ) dv = ( ivmin - jvmin ) / NV;
1919 if ( du ) { jumin += du * NU; jumax += du * NU; }
1920 if ( dv ) { jvmin += dv * NV; jvmax += dv * NV; }
1922 // compute common (extended) indices
1923 Standard_Integer iun = Min ( iumin, jumin );
1924 Standard_Integer iux = Max ( iumax, jumax );
1925 Standard_Integer ivn = Min ( ivmin, jvmin );
1926 Standard_Integer ivx = Max ( ivmax, jvmax );
1927 Standard_Boolean ok = ( iun == iux || iun+1 == iux ) &&
1928 ( ivn == ivx || ivn+1 == ivx );
1929 if ( ok && extend ) { iumin = iun; iumax = iux; ivmin = ivn; ivmax = ivx; }
1934 //=======================================================================
1935 //function : CollectWires
1937 //=======================================================================
1939 void ShapeFix_ComposeShell::CollectWires (ShapeFix_SequenceOfWireSegment &wires,
1940 ShapeFix_SequenceOfWireSegment &seqw)
1943 ShapeAnalysis_Edge sae;
1944 Standard_Integer i; // svv #1
1945 // Collect information on short closed segments
1946 TColStd_Array1OfInteger shorts(1,seqw.Length());
1947 for ( i=1; i <= seqw.Length(); i++ ) {
1948 if(seqw(i).IsVertex() || seqw(i).Orientation() == TopAbs_INTERNAL) {
1949 wires.Append(seqw(i));
1955 for ( Standard_Integer k=1; ! myClosedMode && k <= seqw(i).NbEdges(); k++ )
1956 if ( ! seqw(i).CheckPatchIndex ( k ) ) {;} //break;
1957 // cout << "Warning: ShapeFix_ComposeShell::CollectWires: Wrong patch indices" << endl;
1959 Standard_Integer isshort = IsShortSegment ( seqw(i), myFace, myGrid, myLoc,
1960 myUResolution, myVResolution );
1961 shorts.SetValue ( i, isshort );
1963 ( seqw(i).Orientation() == TopAbs_EXTERNAL ||
1964 ( seqw(i).NbEdges() == 1 && //:abv 13.05.02: OCC320 - remove if degenerated
1965 BRep_Tool::Degenerated ( seqw(i).Edge(1) ) ) ) ) {
1967 cout << "Info: ShapeFix_ComposeShell::CollectWires: Short segment ignored" << endl;
1969 seqw(i).Orientation ( TopAbs_INTERNAL );
1973 Handle(ShapeExtend_WireData) sbwd;
1974 gp_Pnt2d endPnt, firstPnt;
1975 gp_Vec2d endTan, firstTan;
1976 TopoDS_Vertex firstV, endV;
1977 TopoDS_Edge firstEdge, lastEdge;
1978 Standard_Real tol = 0;
1979 Standard_Integer iumin, iumax, ivmin, ivmax;
1980 Standard_Real dsu=0., dsv=0.;
1981 Standard_Boolean canBeClosed = Standard_False;
1983 Standard_Integer index = 0;
1984 Standard_Boolean misoriented = Standard_True, samepatch = Standard_False;
1985 Standard_Boolean reverse = Standard_False, connected = Standard_False;
1986 Standard_Real angle = -PI, mindist = RealLast();
1987 Standard_Integer weigth = 0;
1988 Standard_Real shiftu=0., shiftv=0.;
1990 // find next segment to connect (or first if sbwd is NULL)
1991 for ( i = 1; i <= seqw.Length(); i++ ) {
1992 ShapeFix_WireSegment seg = seqw.Value(i);
1995 TopAbs_Orientation anOr = seg.Orientation();
1996 if ( anOr == TopAbs_INTERNAL ) continue;
1998 // for first segment, take any
1999 if ( sbwd.IsNull() ) {
2000 if ( shorts(i) >0 ) continue;
2001 if ( anOr == TopAbs_EXTERNAL ) continue;
2002 if ( anOr == TopAbs_FORWARD ) reverse = Standard_True;
2004 seg.GetPatchIndex ( 1, iumin, iumax, ivmin, ivmax );
2005 misoriented = Standard_False;
2010 // check whether current segment is on the same patch with previous
2011 Standard_Integer sp = ( myClosedMode || // no indexation in closed mode
2012 IsSamePatch ( seg, myGrid->NbUPatches(), myGrid->NbVPatches(),
2013 iumin, iumax, ivmin, ivmax ) );
2015 // not same patch has lowest priority
2016 if ( ! sp && ( canBeClosed || ( index && samepatch ) ) ) continue;
2018 // try to connect, with the following priorities:
2019 // The name of property Weigth:
2020 // sharing vertex auto
2023 // misorientation = 0 8
2024 // connected in 2d 4
2028 Handle(ShapeExtend_WireData) wire = seg.WireData();
2029 for ( Standard_Integer j=0; j <2; j++ ) {
2030 if ( ! endV.IsSame ( j ? seg.LastVertex() : seg.FirstVertex() ) ) continue;
2032 // check for misorientation only if nothing better is found
2033 Standard_Integer misor = ( anOr == ( j ? TopAbs_REVERSED : TopAbs_FORWARD ) );
2034 // if ( misor ) continue; // temporarily, to be improved
2036 // returning back by the same edge is lowest priority
2037 if ( lastEdge.IsSame ( wire->Edge ( j ? wire->NbEdges() : 1 ) ) ) {
2038 if ( ! index && ! canBeClosed ) { // || ( sp && ! samepatch ) ) {
2041 connected = Standard_True;
2042 misoriented = misor;
2044 weigth = ( sp ? 16 : 0 ) + ( connected ? 8 : 0 ) + (misor==0 ? 4 : 0);
2050 // compute starting tangent
2054 Standard_Real edgeTol = 0;
2055 for ( k=1; k <= wire->NbEdges(); k++ ) {
2056 TopoDS_Shape tmpE = wire->Edge(wire->NbEdges()-k+1).Reversed();
2057 TopoDS_Edge edge = ( j ? TopoDS::Edge ( tmpE ) :
2059 edgeTol = BRep_Tool::Tolerance ( edge );
2060 //if ( sae.GetEndTangent2d ( edge, myFace, Standard_False, lPnt, lVec ) ) break;
2061 if ( sae.GetEndTangent2d ( edge, myFace, Standard_False, lPnt, lVec, 1.e-3 ) ) break;
2063 if ( k > wire->NbEdges() ) myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL2 );
2065 if ( myClosedMode ) {
2067 shiftu = ShapeAnalysis::AdjustByPeriod ( lPnt.X(), endPnt.X(), myUPeriod );
2068 lPnt.SetX ( lPnt.X() + shiftu );
2071 shiftv = ShapeAnalysis::AdjustByPeriod ( lPnt.Y(), endPnt.Y(), myVPeriod );
2072 lPnt.SetY ( lPnt.Y() + shiftv );
2076 // short segment is to be taken with highest priority by angle
2077 Standard_Real ang = ( shorts(i) >0 ? PI : endTan.Angle ( lVec ) );
2078 if ( myClosedMode && shorts(i) <=0 && PI-ang < ::Precision::Angular() )
2079 ang = 0.; // abv 21 Mar 00: trj3_s1-md-214.stp #2471: avoid going back
2080 // abv 05 Feb 02: face from Parasolid: use tolerance of edges for check
2081 // for coincidence (instead of vertex tolerance) in order
2082 // this check to be in agreement with check for position of wire segments
2083 // thus avoiding bad effects on overlapping edges
2084 Standard_Real ctol = Max ( edgeTol, BRep_Tool::Tolerance(lastEdge) );
2085 Standard_Boolean conn = IsCoincided ( endPnt, lPnt, myUResolution,
2086 myVResolution, ctol );
2087 Standard_Real dist = endPnt.SquareDistance ( lPnt );
2089 // check if case is better than last found
2091 Standard_Integer w1 = ( sp ? 16 : 0 ) + ( conn ? 4 : 0 ) + (misor==0 ? 8 : 0);
2092 Standard_Integer tail1 = ( !conn && (dist < mindist) ? 2 : 0) + (ang > angle ? 1 : 0);
2093 Standard_Integer tail2 = ( !connected &&(dist > mindist) ? 2 : 0) + (ang < angle ? 1 : 0);
2094 if(w1+tail1 <= weigth+tail2)
2102 misoriented = misor;
2110 // if next segment found, connect it
2113 myInvertEdgeStatus = Standard_True;
2114 ShapeFix_WireSegment seg = seqw.Value(index);
2115 if ( sbwd.IsNull() ) sbwd = new ShapeExtend_WireData;
2116 else if ( samepatch ) { // extend patch indices
2117 IsSamePatch ( seg, myGrid->NbUPatches(), myGrid->NbVPatches(),
2118 iumin, iumax, ivmin, ivmax, Standard_True );
2120 // TopAbs_Orientation or = seg.Orientation();
2121 if ( ! reverse ) sbwd->Add ( seg.WireData() );
2123 Handle(ShapeExtend_WireData) wire = new ShapeExtend_WireData;
2124 wire->ManifoldMode() = Standard_False;
2125 wire->Add ( seg.WireData() );
2126 wire->Reverse ( myFace );
2129 if ( seg.Orientation() == TopAbs_EXTERNAL )
2130 seg.Orientation ( reverse ? TopAbs_REVERSED : TopAbs_FORWARD );
2131 else seg.Orientation ( TopAbs_INTERNAL );
2132 seqw.SetValue ( index, seg );
2134 else if ( sbwd.IsNull() ) break; // stop when no free segments available
2135 // for first segment, remember start point
2136 if ( endV.IsNull() ) {
2137 firstEdge = sbwd->Edge(1);
2138 firstV = sae.FirstVertex ( firstEdge );
2139 //sae.GetEndTangent2d ( firstEdge, myFace, Standard_False, firstPnt, firstTan );
2140 sae.GetEndTangent2d ( firstEdge, myFace, Standard_False, firstPnt, firstTan, 1.e-3 );
2143 // update last edge and vertex (only for not short segments)
2144 Standard_Boolean doupdate = ( index && ( shorts(index) <=0 || endV.IsNull() ) );
2146 lastEdge = sbwd->Edge ( sbwd->NbEdges() );
2147 endV = sae.LastVertex ( lastEdge );
2148 tol = BRep_Tool::Tolerance ( endV );
2149 // BUC60035 2053: iteration on edges is required
2150 Standard_Integer k; // svv #1
2151 for ( k=sbwd->NbEdges(); k >=1; k-- )
2152 //if ( sae.GetEndTangent2d ( sbwd->Edge ( k ), myFace, Standard_True, endPnt, endTan ) )
2153 if ( sae.GetEndTangent2d ( sbwd->Edge ( k ), myFace, Standard_True, endPnt, endTan, 1.e-3 ) )
2155 if ( k <1 ) myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL2 );
2156 if ( myUClosed ) endPnt.SetX ( endPnt.X() + dsu );
2157 if ( myVClosed ) endPnt.SetY ( endPnt.Y() + dsv );
2160 // if closed or no next segment found, add to wires
2161 canBeClosed = endV.IsSame ( firstV );
2162 if ( ! index || ( canBeClosed &&
2163 ! lastEdge.IsSame ( firstEdge ) && // cylinder (seam)
2164 IsCoincided ( endPnt, firstPnt, myUResolution, myVResolution, 2.* tol ) ) ) {
2165 if ( ! endV.IsSame ( sae.FirstVertex ( firstEdge ) ) ) {
2166 myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL5 );
2168 cout << "Warning: ShapeFix_ComposeShell::CollectWires: can't close wire" << endl;
2171 ShapeFix_WireSegment s ( sbwd, TopAbs_FORWARD );
2172 s.DefineIUMin(1,iumin);
2173 s.DefineIUMax(1,iumax);
2174 s.DefineIVMin(1,ivmin);
2175 s.DefineIVMax(1,ivmax);
2179 canBeClosed = Standard_False;
2183 // Check if some wires are short in 3d (lie entirely inside one vertex),
2184 // and if yes try to merge them with others
2185 //pdn The short seqments are stil plased in "in" sequence.
2187 for ( i=1; i <= seqw.Length(); i++ ) {
2188 if ( shorts(i) != 1 || seqw(i).IsVertex() || seqw(i).Orientation() == TopAbs_INTERNAL ||
2189 seqw(i).Orientation() == TopAbs_EXTERNAL ) continue;
2191 // find any other wire containing the same vertex
2192 Handle(ShapeExtend_WireData) wd = seqw(i).WireData();
2193 TopoDS_Vertex V = seqw(i).FirstVertex();
2194 Standard_Integer minj=0, mink=0;
2197 Standard_Real mindist=0;
2198 Standard_Boolean samepatch = Standard_False;
2199 // Standard_Integer iumin, iumax, ivmin, ivmax;
2200 seqw(i).GetPatchIndex ( 1, iumin, iumax, ivmin, ivmax );
2201 sae.GetEndTangent2d ( wd->Edge(1), myFace, Standard_False, p2d, vec );
2202 for ( Standard_Integer j=1; j <= wires.Length(); j++ ) {
2203 // if ( j == i ) continue;
2204 // Handle(ShapeExtend_WireData)
2205 sbwd = wires(j).WireData();
2206 for ( Standard_Integer k=1; k <= sbwd->NbEdges(); k++ ) {
2207 if ( !V.IsSame ( sae.FirstVertex ( sbwd->Edge(k) ) ) ) continue; //pdn I suppose that short segment should be inserted into the SAME vertex.
2209 Standard_Integer sp = IsSamePatch ( wires(j), myGrid->NbUPatches(), myGrid->NbVPatches(),
2210 iumin, iumax, ivmin, ivmax );
2211 if ( samepatch && !sp) continue;
2213 sae.GetEndTangent2d ( sbwd->Edge(k), myFace, Standard_False, pp, vec );
2214 Standard_Real dist = pp.SquareDistance ( p2d );
2215 if ( sp && ! samepatch ) { minj = j; mink = k; mindist = dist;samepatch=sp;}
2217 if ( ! minj || mindist > dist ) { minj = j; mink = k; mindist = dist;samepatch=sp; }
2221 //pdn add into resulting sequence!
2222 ShapeFix_WireSegment s ( wd, TopAbs_FORWARD );
2225 cout <<"Warning: Short segment processed as separate wire"<<endl;
2230 // and if found, merge
2231 // Handle(ShapeExtend_WireData)
2232 sbwd = wires(minj).WireData();
2233 for ( Standard_Integer n=1; n <= wd->NbEdges(); n++ )
2234 sbwd->Add ( wd->Edge(n), mink++ );
2236 // wires.Remove ( i );
2242 //=======================================================================
2243 //function : DispatchWires
2245 //=======================================================================
2247 static gp_Pnt2d GetMiddlePoint (const ShapeFix_WireSegment wire,
2248 const TopoDS_Face face)
2250 if(wire.IsVertex()) {
2251 TopoDS_Vertex aV = wire.GetVertex();
2252 gp_Pnt aP3D = BRep_Tool::Pnt(aV );
2253 Handle(Geom_Surface) surf = BRep_Tool::Surface(face);
2254 Handle(ShapeAnalysis_Surface) aSurfTool = new ShapeAnalysis_Surface(surf);
2255 return aSurfTool->ValueOfUV(aP3D,Precision::Confusion());
2258 ShapeAnalysis_Edge sae;
2259 ShapeAnalysis_Curve sac;
2260 Handle(ShapeExtend_WireData) wd = wire.WireData();
2261 for(Standard_Integer i = 1; i <= wd->NbEdges(); i++) {
2262 TopoDS_Edge E = wd->Edge (i);
2263 Standard_Real cf,cl;
2264 Handle(Geom2d_Curve) c2d;
2265 if(sae.PCurve (E,face,c2d,cf,cl,Standard_False)) {
2266 sac.FillBndBox ( c2d, cf, cl, 3, Standard_False, box );
2267 // box.Add(c2d->Value(cf));
2268 // box.Add(c2d->Value(cl));
2269 // box.Add(c2d->Value((cl+cf)/2.));
2272 if ( box.IsVoid() ) return gp_Pnt2d(0.,0.);
2273 Standard_Real aXmin, aYmin, aXmax, aYmax;
2274 box.Get(aXmin, aYmin, aXmax, aYmax);
2275 return gp_Pnt2d ( 0.5 * ( aXmax + aXmin ), 0.5 * ( aYmax + aYmin ) );
2278 //=======================================================================
2279 //function : MakeFacesOnPatch
2281 //=======================================================================
2283 void ShapeFix_ComposeShell::MakeFacesOnPatch (TopTools_SequenceOfShape &faces,
2284 const Handle(Geom_Surface)& surf,
2285 TopTools_SequenceOfShape &loops) const
2289 // Case of single loop: just add it to face
2290 if ( loops.Length() == 1 ) {
2291 TopoDS_Face newFace;
2292 B.MakeFace ( newFace, surf, myLoc, ::Precision::Confusion() );
2293 TopoDS_Shape aSH = loops.Value(1);
2294 if( aSH.ShapeType() != TopAbs_WIRE)
2296 TopoDS_Wire wire = TopoDS::Wire ( loops.Value(1) );
2298 B.Add ( newFace, wire );
2299 if(myInvertEdgeStatus) {
2300 Handle(ShapeFix_Face) sff = new ShapeFix_Face(newFace);
2301 sff->FixAddNaturalBoundMode() = Standard_False;
2302 TopTools_DataMapOfShapeListOfShape MapWires;
2304 sff->FixOrientation(MapWires);
2305 newFace = sff->Face();
2308 faces.Append ( newFace );
2312 // For several loops, first find roots
2313 // make pseudo-face,
2315 B.MakeFace ( pf, surf, myLoc, ::Precision::Confusion() );
2316 Handle(Geom_Surface) atSurf = BRep_Tool::Surface(pf);
2318 Handle(ShapeAnalysis_Surface) aSurfTool = new ShapeAnalysis_Surface(atSurf);
2319 TopTools_SequenceOfShape roots;
2320 Standard_Integer i; // svv #1
2321 for ( i = 1; i <= loops.Length(); i++ ) {
2324 TopoDS_Shape aShape = loops(i);
2325 if(aShape.ShapeType() != TopAbs_WIRE ||
2326 (aShape.Orientation() != TopAbs_FORWARD && aShape.Orientation() != TopAbs_REVERSED))
2329 wr = TopoDS::Wire ( loops(i) );
2330 TopoDS_Iterator ew (wr);
2331 if ( ! ew.More() ) continue;
2333 TopoDS_Edge ed = TopoDS::Edge ( ew.Value() );
2334 while(ed.Orientation() != TopAbs_FORWARD &&
2335 ed.Orientation() != TopAbs_REVERSED ) {
2338 ed = TopoDS::Edge ( ew.Value() );
2342 if ( ! ew.More() ) continue;
2343 Standard_Real cf, cl;
2344 Handle(Geom2d_Curve) cw = BRep_Tool::CurveOnSurface ( ed, pf, cf, cl );
2345 if ( cw.IsNull() ) continue;
2346 unp = cw->Value ( 0.5 * ( cf + cl ) );
2348 Standard_Integer j; // svv #1
2349 for ( j = 1; j <= loops.Length(); j++ ) {
2350 if ( i == j ) continue;
2351 TopoDS_Shape aShape2 = loops(j);
2352 if(aShape2.ShapeType() != TopAbs_WIRE ||
2353 (aShape2.Orientation() != TopAbs_FORWARD &&
2354 aShape2.Orientation() != TopAbs_REVERSED))
2356 TopoDS_Wire w1 = TopoDS::Wire (aShape2);
2359 awtmp.Orientation(TopAbs_FORWARD);
2360 TopoDS_Iterator aIt(w1);
2361 Standard_Integer nbe =0;
2362 for( ; aIt.More() ; aIt.Next()) {
2363 if(aIt.Value().Orientation() == TopAbs_FORWARD ||
2364 aIt.Value().Orientation() == TopAbs_REVERSED) {
2365 B.Add(awtmp,aIt.Value());
2373 B.MakeFace ( fc, surf, myLoc, ::Precision::Confusion() );
2374 B.Add ( fc, awtmp );
2375 BRepTopAdaptor_FClass2d clas ( fc, ::Precision::PConfusion() );
2376 TopAbs_State stPoint = clas.Perform (unp,Standard_False);
2377 if(stPoint == TopAbs_ON || stPoint == TopAbs_UNKNOWN) {
2379 TopoDS_Edge ed = TopoDS::Edge ( ew.Value() );
2380 Standard_Real cf, cl;
2381 Handle(Geom2d_Curve) cw = BRep_Tool::CurveOnSurface ( ed, pf, cf, cl );
2382 // handle tangential case (ON)
2383 while ( stPoint == TopAbs_ON || stPoint == TopAbs_UNKNOWN ) {
2384 stPoint = clas.Perform ( cw->Value(cl), Standard_False );
2385 if ( ! ew.More() ) break;
2387 if ( ! ew.More() ) break;
2388 TopoDS_Edge edge = TopoDS::Edge ( ew.Value() );
2389 if(edge.Orientation() !=TopAbs_FORWARD &&
2390 edge.Orientation() !=TopAbs_REVERSED)
2392 Handle(Geom2d_Curve) c2d = BRep_Tool::CurveOnSurface ( edge, pf, cf, cl );
2393 if ( ! c2d.IsNull() ) cw = c2d;
2396 TopAbs_State stInfin = clas.PerformInfinitePoint();
2397 if ( stPoint != stInfin ) break;
2399 if ( j > loops.Length()) {
2400 roots.Append ( wr );
2401 // loops.Remove ( i-- );
2405 // And remove them from the list of loops
2406 for ( i = 1; i <= loops.Length(); i++ )
2407 for ( Standard_Integer j = 1; j <= roots.Length(); j++ )
2408 if ( loops(i).IsSame ( roots(j) ) ) { loops.Remove(i--); break; }
2410 // check for lost wires, and if they are, make them roots
2411 if ( roots.Length() <=0 && loops.Length() >0 ) {
2413 cout << "Error: ShapeFix_ComposeShell::MakeFacesOnPatch: can't dispatch wires" << endl;
2415 for ( Standard_Integer j=1; j <= loops.Length(); j++ ) {
2416 roots.Append ( loops(j) );
2421 // Then iterate on loops
2422 for ( i=1; i <= roots.Length(); i++ ) {
2423 Standard_Boolean reverse = Standard_False;
2424 TopoDS_Wire wire = TopoDS::Wire ( roots(i) );
2426 B.MakeFace ( fc, surf, myLoc, ::Precision::Confusion() );
2428 BRepTopAdaptor_FClass2d clas ( fc, ::Precision::PConfusion() );
2429 if ( clas.PerformInfinitePoint() == TopAbs_IN ) {
2430 reverse = Standard_True;
2432 cout << "Warning: ShapeFix_ComposeShell::MakeFacesOnPatch: badly oriented wire" << endl;
2436 // find all holes for that loop
2437 TopTools_SequenceOfShape holes; // holes in holes not supported
2438 Standard_Integer j; // svv #1
2439 for ( j=1; j <= loops.Length(); j++ ) {
2441 if(loops(j).ShapeType() == TopAbs_WIRE) {
2442 TopoDS_Wire bw = TopoDS::Wire ( loops(j) );
2443 TopoDS_Iterator ew ( bw );
2444 if ( ! ew.More() ) continue;
2445 TopoDS_Edge ed = TopoDS::Edge ( ew.Value() );
2446 Standard_Real cf, cl;
2447 Handle(Geom2d_Curve) cw = BRep_Tool::CurveOnSurface ( ed, pf, cf, cl );
2448 if ( cw.IsNull() ) continue;
2449 unp = cw->Value ( 0.5 * ( cf + cl ) );
2451 else if(loops(j).ShapeType() == TopAbs_VERTEX) {
2452 TopoDS_Vertex aV = TopoDS::Vertex(loops(j));
2453 gp_Pnt aP = BRep_Tool::Pnt(aV);
2454 unp = aSurfTool->ValueOfUV(aP,Precision::Confusion());
2458 TopAbs_State state = clas.Perform (unp,Standard_False);
2459 if ( (Standard_Boolean) ( state == TopAbs_OUT ) == reverse ) {
2460 holes.Append ( loops(j) );
2461 loops.Remove ( j-- );
2465 // and add them to new face (no orienting is done)
2466 TopoDS_Face newFace;
2467 B.MakeFace ( newFace, surf, myLoc, ::Precision::Confusion() );
2468 B.Add ( newFace, wire );
2469 for ( j=1; j <= holes.Length(); j++ ) {
2470 TopoDS_Shape aSh = holes(j);
2471 if(aSh.ShapeType() == TopAbs_VERTEX) {
2472 TopoDS_Vertex aNewV = ShapeAnalysis_TransferParametersProj::CopyNMVertex(TopoDS::Vertex(aSh), newFace,myFace);
2473 Context()->Replace(aSh,aNewV);
2474 B.Add ( newFace,aNewV);
2477 B.Add ( newFace, holes(j) );
2479 faces.Append ( newFace );
2481 // check for lost wires, and if they are, make them roots
2482 if ( i == roots.Length() && loops.Length() >0 ) {
2484 cout << "Error: ShapeFix_ComposeShell::MakeFacesOnPatch: can't dispatch wires" << endl;
2486 for ( j=1; j <= loops.Length(); j++ ) {
2487 TopoDS_Shape aSh = loops(j);
2488 if(aSh.ShapeType() == TopAbs_WIRE && (aSh.Orientation() == TopAbs_FORWARD ||
2489 aSh.Orientation() == TopAbs_REVERSED))
2490 roots.Append ( loops(j) );
2497 //=======================================================================
2498 //function : DispatchWires
2500 //=======================================================================
2502 void ShapeFix_ComposeShell::DispatchWires (TopTools_SequenceOfShape &faces,
2503 ShapeFix_SequenceOfWireSegment& wires) const
2507 // in closed mode, apply FixShifted to all wires before dispatching them
2508 if ( myClosedMode ) {
2510 sfw.SetFace ( myFace );
2511 sfw.SetPrecision ( Precision() );
2513 // pdn: shift pcurves in the seam to make OK shape w/o fixshifted
2515 for ( i=1; i <= wires.Length(); i++ ) {
2516 if(wires(i).IsVertex())
2518 Handle(ShapeExtend_WireData) sbwd = wires(i).WireData();
2520 for(Standard_Integer jL=1; jL <= sbwd->NbEdges(); jL++ ) {
2521 TopoDS_Edge E = sbwd->Edge(jL);
2522 if ( E.Orientation() == TopAbs_REVERSED && BRep_Tool::IsClosed(E,myFace) ) {
2523 Standard_Real f1,l1, f2, l2;
2524 Handle(Geom2d_Curve) c21 = BRep_Tool::CurveOnSurface(E,myFace,f1,l1);
2525 TopoDS_Shape dummy = E.Reversed();
2526 Handle(Geom2d_Curve) c22 = BRep_Tool::CurveOnSurface(TopoDS::Edge(dummy),myFace,f2,l2);
2527 Standard_Real dPreci = ::Precision::PConfusion()*Precision::PConfusion();
2528 gp_Pnt2d pf1 = c21->Value(f1);
2529 gp_Pnt2d pl1 = c21->Value(l1);
2530 gp_Pnt2d pf2 = c22->Value(f2);
2531 gp_Pnt2d pl2 = c22->Value(l2);
2532 if ( c21 == c22 || pf1.SquareDistance(pf2) < dPreci ||
2533 pl1.SquareDistance(pl2) < dPreci ) {
2534 gp_Vec2d shift(0.,0.);
2535 if ( myUClosed && Abs ( pf2.X() - pl2.X() ) < ::Precision::PConfusion() )
2536 shift.SetX(myUPeriod);
2537 if ( myVClosed && Abs ( pf2.Y() - pl2.Y() ) < ::Precision::PConfusion() )
2538 shift.SetY(myVPeriod);
2539 c22->Translate(shift);
2545 for ( i=1; i <= wires.Length(); i++ ) {
2546 if(wires(i).IsVertex())
2548 Handle(ShapeExtend_WireData) sbwd = wires(i).WireData();
2550 //: abv 30.08.01: torHalf2.sat: if wire contains single degenerated
2551 // edge, skip that wire
2552 if ( sbwd->NbEdges() <=0 ||
2553 ( sbwd->NbEdges() ==1 && BRep_Tool::Degenerated(sbwd->Edge(1)) ) ) {
2561 // force recomputation of degenerated edges (clear pcurves)
2562 ShapeBuild_Edge sbe;
2563 for (Standard_Integer jL=1; jL <= sbwd->NbEdges(); jL++ ) {
2564 if ( BRep_Tool::Degenerated(sbwd->Edge(jL)) )
2565 sbe.RemovePCurve(sbwd->Edge(jL),myFace);
2566 // sfw.FixDegenerated(jL);
2568 sfw.FixDegenerated();
2572 // Compute center points for wires
2573 TColgp_SequenceOfPnt2d mPnts;
2574 Standard_Integer nb = wires.Length();
2576 // pdn protection on empty sequence
2580 Standard_Integer i; //svv #1
2581 for ( i = 1; i <= nb; i++ )
2582 mPnts.Append ( GetMiddlePoint ( wires(i), myFace ) );
2584 // Put each wire on its own surface patch (by reassigning pcurves)
2585 // and build 3d curve if necessary
2586 ShapeBuild_ReShape rs;
2587 ShapeBuild_Edge sbe;
2588 ShapeAnalysis_Edge sae;
2589 Handle(ShapeFix_Edge) sfe = new ShapeFix_Edge;
2591 Standard_Real U1,U2,V1,V2;
2592 myGrid->Bounds(U1,U2,V1,V2);
2593 for ( i = 1; i <= nb; i++ ) {
2595 gp_Pnt2d pnt = mPnts(i);
2596 Standard_Real ush =0., vsh=0.;
2598 ush = ShapeAnalysis::AdjustToPeriod(pnt.X(),U1,U2);
2599 pnt.SetX(pnt.X()+ush);
2602 vsh = ShapeAnalysis::AdjustToPeriod(pnt.Y(),V1,V2);
2603 pnt.SetY(pnt.Y()+vsh);
2606 Standard_Integer indU = myGrid->LocateUParameter ( pnt.X() );
2607 Standard_Integer indV = myGrid->LocateVParameter ( pnt.Y() );
2609 // compute parametric transformation
2611 Standard_Real uFact=1.;
2612 Standard_Boolean needT = myGrid->GlobalToLocalTransformation ( indU, indV, uFact, T );
2613 if ( ush != 0. || vsh != 0. ) {
2615 Sh.SetTranslation ( gp_Vec2d ( ush, vsh ) );
2617 needT = Standard_True;
2619 if(wires(i).IsVertex())
2621 Handle(Geom_Surface) surf = myGrid->Patch ( indU, indV );
2623 B.MakeFace ( face, surf, myLoc, ::Precision::Confusion() );
2624 Handle(ShapeExtend_WireData) sewd = wires(i).WireData();
2625 for ( Standard_Integer j = 1; j <= sewd->NbEdges(); j++ ) {
2626 // Standard_Integer nsplit = ApplyContext ( sewd, j, context );
2627 // if ( nsplit <1 ) { j--; continue; }
2629 TopoDS_Edge edge = sewd->Edge(j);
2631 // !! Accurately copy pcurves for SEAMS and SEAM-like edges !!
2633 // if edge is already copied, don`t copy any more
2634 TopoDS_Edge newEdge;
2635 TopoDS_Edge anInitEdge = edge;
2636 Standard_Boolean ismanifold = (edge.Orientation() == TopAbs_FORWARD ||
2637 edge.Orientation() == TopAbs_REVERSED);
2638 if ( rs.IsRecorded ( edge ) ) {
2640 TopoDS_Shape tmpNE = rs.Value(edge);
2641 newEdge = TopoDS::Edge ( tmpNE );
2646 anInitEdge.Orientation(TopAbs_FORWARD);
2648 newEdge = sbe.Copy ( anInitEdge, Standard_False );
2650 newEdge.Orientation(edge.Orientation());
2651 rs.Replace ( edge, newEdge );
2652 Context()->Replace ( edge, newEdge );
2655 sbe.ReassignPCurve ( newEdge, myFace, face );
2657 // transform pcurve to parametric space of patch
2660 Handle(Geom2d_Curve) c2d;
2661 if ( sae.PCurve ( newEdge, face, c2d, f, l, Standard_False ) ) {
2662 Standard_Real newf = f, newl = l;
2663 Handle(Geom2d_Curve) c2dnew = sbe.TransformPCurve ( c2d, T, uFact, newf, newl );
2664 if ( BRep_Tool::IsClosed ( newEdge, face ) ) {
2665 Standard_Real cf, cl;
2666 Handle(Geom2d_Curve) c2d2;
2668 TopoDS_Shape tmpE = newEdge.Reversed();
2669 TopoDS_Edge e2 = TopoDS::Edge (tmpE );
2670 if ( sae.PCurve ( e2, face, c2d2, cf, cl, Standard_False ) ) {
2671 if ( newEdge.Orientation() == TopAbs_FORWARD )
2672 B.UpdateEdge ( newEdge, c2dnew, c2d2, face, 0. );
2673 else B.UpdateEdge ( newEdge, c2d2, c2dnew, face, 0. );
2675 else B.UpdateEdge ( newEdge, c2dnew, face, 0. );
2677 else B.UpdateEdge ( newEdge, c2dnew, face, 0. );
2678 B.Range ( newEdge, face, newf, newl );
2679 if ( (newf != f || newl != l) && !BRep_Tool::Degenerated(newEdge) )
2680 B.SameRange ( newEdge, Standard_False );
2684 if(!BRep_Tool::SameRange(newEdge)) {
2687 TopoDS_Edge afe = TopoDS::Edge(newEdge.Oriented(TopAbs_FORWARD));
2688 etmp = sbe.Copy (afe , Standard_False );
2691 etmp = sbe.Copy ( newEdge, Standard_False );
2692 sfe->FixAddCurve3d ( etmp );
2693 Standard_Real cf, cl;
2694 Handle(Geom_Curve) c3d;
2695 if(sae.Curve3d(etmp,c3d,cf,cl,Standard_False)) {
2696 B.UpdateEdge ( newEdge, c3d, 0. );
2697 sbe.SetRange3d ( newEdge, cf, cl );
2701 sfe->FixAddCurve3d ( newEdge );
2702 sewd->Set ( newEdge, j );
2706 // Collect wires in packets lying on same surface and dispatch them
2707 TColStd_Array1OfBoolean used ( 1, nb );
2708 used.Init ( Standard_False );
2710 TopTools_SequenceOfShape loops;
2712 Handle(Geom_Surface) Surf;
2713 for ( i = 1; i <= nb; i++ ) {
2714 if ( used(i) ) continue;
2715 Handle(Geom_Surface) S = myGrid->Patch ( mPnts(i) );
2716 if ( Surf.IsNull() ) Surf = S;
2717 else if ( S != Surf ) continue;
2718 used(i) = Standard_True;
2719 ShapeFix_WireSegment aSeg = wires(i);
2720 if(aSeg.IsVertex()) {
2721 TopoDS_Vertex aVert = aSeg.GetVertex();
2722 if(aVert.Orientation() == TopAbs_INTERNAL)
2723 loops.Append(wires(i).GetVertex());
2726 Handle(ShapeExtend_WireData) aWD = aSeg.WireData();
2728 loops.Append ( aWD->Wire() );
2731 if ( Surf.IsNull() ) break;
2733 MakeFacesOnPatch ( faces, Surf, loops );
2737 //======================================================================
2738 //function : SetTransferParamTool
2740 //=======================================================================
2742 void ShapeFix_ComposeShell::SetTransferParamTool(const Handle(ShapeAnalysis_TransferParameters)& TransferParam)
2744 myTransferParamTool = TransferParam;
2747 //=======================================================================
2748 //function : GetTransferParamTool
2750 //=======================================================================
2752 Handle(ShapeAnalysis_TransferParameters) ShapeFix_ComposeShell::GetTransferParamTool() const
2754 return myTransferParamTool;
2757 //=======================================================================
2758 //function : ClosedMode
2760 //=======================================================================
2762 Standard_Boolean &ShapeFix_ComposeShell::ClosedMode()
2764 return myClosedMode;