1 // Created on: 1999-04-27
2 // Created by: Andrey BETENEV
3 // Copyright (c) 1999-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
6 // This file is part of Open CASCADE Technology software library.
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
17 // pdn 01.06.99 S4205: handling not-SameRange edges
18 // abv 22.07.99 implementing patch indices
19 // svv 10.01.00 porting on DEC
21 #include <ShapeFix_ComposeShell.ixx>
23 #include <Precision.hxx>
24 #include <gp_Pnt2d.hxx>
25 #include <gp_Lin2d.hxx>
26 #include <gp_Dir2d.hxx>
28 #include <TColStd_Array1OfInteger.hxx>
29 #include <TColStd_Array1OfBoolean.hxx>
30 #include <TColStd_Array1OfReal.hxx>
31 #include <TColStd_HArray1OfReal.hxx>
32 #include <TColgp_SequenceOfPnt2d.hxx>
33 #include <TColStd_SequenceOfReal.hxx>
35 #include <IntRes2d_IntersectionSegment.hxx>
36 #include <IntRes2d_IntersectionPoint.hxx>
37 #include <IntRes2d_Domain.hxx>
39 #include <Geom2dInt_GInter.hxx>
40 #include <Geom_Curve.hxx>
41 #include <Geom2d_Line.hxx>
42 #include <Geom2dAdaptor_Curve.hxx>
43 #include <GeomAdaptor_Surface.hxx>
46 #include <TopoDS_Vertex.hxx>
47 #include <TopoDS_Edge.hxx>
48 #include <TopoDS_Wire.hxx>
49 #include <TopoDS_Shell.hxx>
50 #include <TopoDS_Iterator.hxx>
51 #include <TopTools_MapOfShape.hxx>
52 #include <TopTools_DataMapOfShapeListOfShape.hxx>
54 #include <BRepTools.hxx>
55 #include <Bnd_Box2d.hxx>
56 #include <BndLib_Add2dCurve.hxx>
57 #include <BRepTopAdaptor_FClass2d.hxx>
58 #include <BRep_Tool.hxx>
59 #include <BRep_Builder.hxx>
61 #include <ShapeExtend.hxx>
62 #include <ShapeExtend_WireData.hxx>
63 #include <ShapeBuild_Vertex.hxx>
64 #include <ShapeBuild_Edge.hxx>
65 #include <ShapeAnalysis.hxx>
66 #include <ShapeAnalysis_Edge.hxx>
67 #include <ShapeAnalysis_WireOrder.hxx>
68 #include <ShapeFix_Wire.hxx>
69 #include <ShapeFix_Edge.hxx>
70 #include <ShapeFix_WireSegment.hxx>
71 #include <ShapeAnalysis_Curve.hxx>
72 #include <ShapeBuild_ReShape.hxx>
73 #include <ShapeAnalysis_TransferParametersProj.hxx>
74 #include <ShapeFix_Face.hxx>
75 #include <ShapeAnalysis_Surface.hxx>
77 #include <Extrema_ExtPC2d.hxx>
78 #include <ShapeAnalysis.hxx>
80 //=======================================================================
81 //function : ShapeFix_ComposeShell
83 //=======================================================================
85 ShapeFix_ComposeShell::ShapeFix_ComposeShell () :
86 myStatus(0), myClosedMode(Standard_False)
88 myTransferParamTool = new ShapeAnalysis_TransferParametersProj;
92 //=======================================================================
95 //=======================================================================
97 void ShapeFix_ComposeShell::Init (const Handle(ShapeExtend_CompositeSurface) &Grid,
98 const TopLoc_Location& L,
99 const TopoDS_Face &Face,
100 const Standard_Real Prec)
103 myUClosed = myGrid->IsUClosed();
104 myVClosed = myGrid->IsVClosed();
105 myUPeriod = myGrid->UJointValue(myGrid->NbUPatches()+1) - myGrid->UJointValue(1);
106 myVPeriod = myGrid->VJointValue(myGrid->NbVPatches()+1) - myGrid->VJointValue(1);
108 // DTK-CKY 100531 : protection against very thin face
109 // Test "isclosed" should be filtered on the overall (non trimmed) surface, must be closed
110 Handle(Geom_Surface) theSurface = BRep_Tool::Surface(Face,myLoc);
111 Standard_Real U0,U1,V0,V1;
112 theSurface->Bounds(U0,U1,V0,V1);
114 gp_Pnt P0 = theSurface->Value(U0,(V0+V1)/2.);
115 gp_Pnt P1 = theSurface->Value(U1,(V0+V1)/2.);
116 if (P0.Distance(P1) > Precision::Confusion()*10)
117 myUClosed = Standard_False;
120 gp_Pnt P0 = theSurface->Value((U0+U1)/2.,V0);
121 gp_Pnt P1 = theSurface->Value((U0+U1)/2.,V1);
122 if (P0.Distance(P1) > Precision::Confusion()*10)
123 myVClosed = Standard_False;
125 // DTK-CKY 100531 end
129 TopoDS_Shape tmpF = Face.Oriented ( TopAbs_FORWARD );
130 myFace = TopoDS::Face ( tmpF ); // for correct dealing with seams
131 myOrient = Face.Orientation();
133 myStatus = ShapeExtend::EncodeStatus ( ShapeExtend_OK );
135 // Compute resolution (checking in 2d is necessary for splitting
136 // degenerated edges and avoiding NotClosed)
137 myUResolution = myVResolution = RealLast();
138 for ( Standard_Integer i=1; i <= myGrid->NbUPatches(); i++ ) {
139 Standard_Real uRange = myGrid->UJointValue(i+1)-myGrid->UJointValue(i);
140 for ( Standard_Integer j=1; j <= myGrid->NbVPatches(); j++ ) {
141 Standard_Real vRange = myGrid->VJointValue(j+1)-myGrid->VJointValue(j);
142 Standard_Real u1,u2,v1,v2;
143 myGrid->Patch(i,j)->Bounds(u1,u2,v1,v2);
144 GeomAdaptor_Surface GAS ( myGrid->Patch(i,j) );
145 Standard_Real ures = GAS.UResolution ( 1. )*uRange/(u2-u1);
146 Standard_Real vres = GAS.VResolution ( 1. )*vRange/(v2-v1);
147 if ( ures >0. && myUResolution > ures ) myUResolution = ures;
148 if ( vres >0. && myVResolution > vres ) myVResolution = vres;
151 if ( myUResolution == RealLast() ) myUResolution = ::Precision::Parametric ( 1. );
152 if ( myVResolution == RealLast() ) myVResolution = ::Precision::Parametric ( 1. );
156 //=======================================================================
159 //=======================================================================
161 Standard_Boolean ShapeFix_ComposeShell::Perform ()
163 myStatus = ShapeExtend::EncodeStatus ( ShapeExtend_OK );
164 myInvertEdgeStatus = Standard_False;
166 ShapeFix_SequenceOfWireSegment seqw; // working data: wire segments
168 // Init seqw by initial set of wires (with corresponding orientation)
170 if(seqw.Length() == 0) {
171 myStatus = ShapeExtend::EncodeStatus ( ShapeExtend_FAIL6 );
172 return Standard_False;
175 // Split edges in the wires by grid and add internal segments of grid (parts of cutting lines)
176 SplitByGrid ( seqw );
178 // Split all the wires into segments by common vertices (intersections)
181 // Then, collect resulting wires
182 ShapeFix_SequenceOfWireSegment wires; // resulting wires
183 CollectWires ( wires, seqw );
185 // And construct resulting faces
186 TopTools_SequenceOfShape faces;
187 DispatchWires ( faces, wires );
189 // Finally, construct resulting shell
190 if ( faces.Length() !=1 ) {
194 for ( Standard_Integer i=1; i <= faces.Length(); i++ )
195 B.Add ( S, faces(i) );
198 else myResult = faces(1);
199 myResult.Orientation ( myOrient );
201 myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE1 );
202 return Standard_True;
206 //=======================================================================
207 //function : SplitEdges
209 //=======================================================================
211 void ShapeFix_ComposeShell::SplitEdges ()
213 myStatus = ShapeExtend::EncodeStatus ( ShapeExtend_OK );
215 ShapeFix_SequenceOfWireSegment seqw; // working data: wire segments
217 // Init seqw by initial set of wires (with corresponding orientation)
220 // Split edges in the wires by grid and add internal segments of grid (parts of cutting lines)
221 SplitByGrid ( seqw );
225 //=======================================================================
228 //=======================================================================
230 const TopoDS_Shape& ShapeFix_ComposeShell::Result () const
236 //=======================================================================
239 //=======================================================================
241 Standard_Boolean ShapeFix_ComposeShell::Status (const ShapeExtend_Status status) const
243 return ShapeExtend::DecodeStatus ( myStatus, status );
247 //=======================================================================
248 // PRIVATE (working) METHODS
249 //=======================================================================
251 #define TOLINT 1.e-10 // precision for intersection
253 // Local definitions: characteristics of intersection point
255 #define IOR_UNDEF 0 // undefined side
256 #define IOR_LEFT 1 // to left side of cutting line
257 #define IOR_RIGHT 2 // to right side of cutting line
258 #define IOR_BOTH 3 // crossing
259 #define IOR_POS 4 // in case of cycle on full period, whether first point is right
261 #define ITP_INTER 8 // crossing
262 #define ITP_BEGSEG 16 // start of tangential segment
263 #define ITP_ENDSEG 32 // stop of tangential segment
264 #define ITP_TANG 64 // tangential point
267 //=======================================================================
268 //function : PointLineDeviation
270 //=======================================================================
271 // Return (signed) deviation of point from line
272 static Standard_Real PointLineDeviation (const gp_Pnt2d &p, const gp_Lin2d &line)
274 gp_Dir2d dir = line.Direction();
275 gp_Dir2d n ( -dir.Y(), dir.X() );
276 return n.XY() * ( p.XY() - line.Location().XY() );
280 //=======================================================================
281 //function : PointLinePosition
283 //=======================================================================
284 // Define position of point relative to line
285 static Standard_Integer PointLinePosition (const gp_Pnt2d &p, const gp_Lin2d &line,
288 dev = PointLineDeviation ( p, line );
289 return ( dev > TOLINT ? IOR_LEFT : ( dev < -TOLINT ? IOR_RIGHT : IOR_UNDEF ) );
293 //=======================================================================
294 //function : PointLinePosition
296 //=======================================================================
297 // Define position of point relative to line
298 static Standard_Integer PointLinePosition (const gp_Pnt2d &p, const gp_Lin2d &line)
301 return PointLinePosition ( p, line, dev );
305 //=======================================================================
306 //function : ParamPointsOnLine
308 //=======================================================================
309 // Compute parameter of point on line
310 static inline Standard_Real ParamPointOnLine (const gp_Pnt2d &p, const gp_Lin2d &line)
312 return line.Direction().XY() * ( p.XY() - line.Location().XY() );
316 //=======================================================================
317 //function : ParamPointsOnLine
319 //=======================================================================
320 // Compute parameter of two points on line (as intersection of segment)
321 static Standard_Real ParamPointsOnLine (const gp_Pnt2d &p1, const gp_Pnt2d &p2,
322 const gp_Lin2d &line)
324 Standard_Real dist1 = PointLineDeviation ( p1, line );
325 Standard_Real dist2 = PointLineDeviation ( p2, line );
326 // in most cases, one of points is on line
327 if ( Abs ( dist1 ) < ::Precision::PConfusion() ) {
328 if ( Abs ( dist2 ) < ::Precision::PConfusion() )
329 return 0.5 * ( ParamPointOnLine ( p1, line ) + ParamPointOnLine ( p2, line ) );
330 return ParamPointOnLine ( p1, line );
332 if ( Abs ( dist2 ) < ::Precision::PConfusion() )
333 return ParamPointOnLine ( p2, line );
335 if ( dist2 * dist1 >0 )
336 return 0.5 * ( ParamPointOnLine ( p1, line ) + ParamPointOnLine ( p2, line ) );
337 // else compute intersection
338 return ( ParamPointOnLine ( p1, line ) * dist2 -
339 ParamPointOnLine ( p2, line ) * dist1 ) / ( dist2 - dist1 );
343 //=======================================================================
344 //function : ProjectPointOnLine
346 //=======================================================================
347 // Compute projection of point on line
348 static inline gp_Pnt2d ProjectPointOnLine (const gp_Pnt2d &p, const gp_Lin2d &line)
350 return line.Location().XY() + line.Direction().XY() * ParamPointOnLine ( p, line );
354 //=======================================================================
355 //function : ApplyContext
357 //=======================================================================
358 // Apply context to one edge in the wire and put result into this wire
359 static Standard_Integer ApplyContext (ShapeFix_WireSegment &wire,
360 const Standard_Integer iedge,
361 const Handle(ShapeBuild_ReShape) &context)
363 TopoDS_Edge edge = wire.Edge ( iedge );
364 TopoDS_Shape res = context->Apply ( edge );
366 if ( res.IsSame ( edge ) ) return 1;
368 if ( res.ShapeType() == TopAbs_EDGE ) {
369 wire.SetEdge ( iedge, TopoDS::Edge ( res ) );
373 Standard_Integer index = iedge;
375 Handle(ShapeExtend_WireData) segw = new ShapeExtend_WireData;
376 segw->ManifoldMode() = Standard_False;
377 for ( TopoDS_Iterator it(res); it.More(); it.Next() ) {
378 TopoDS_Edge E = TopoDS::Edge ( it.Value() );
379 if ( ! E.IsNull() ) segw->Add ( E );
381 else cout << "Error: ShapeFix_ComposeShell, ApplyContext: wrong mapping of edge" << endl;
385 // add edges into the wire in correct order
386 if ( segw->NbEdges() >0 ) {
387 Standard_Integer ind, iumin, iumax, ivmin, ivmax;
388 wire.GetPatchIndex ( iedge, iumin, iumax, ivmin, ivmax );
389 Standard_Integer nbEdges = segw->NbEdges();
390 for ( Standard_Integer i=1; i <= nbEdges; i++, index++ ) {
391 ind = ( edge.Orientation() == TopAbs_FORWARD || edge.Orientation() == TopAbs_INTERNAL ? i : segw->NbEdges()-i+1 );
392 TopoDS_Edge aE = segw->Edge ( ind );
393 if ( i==1 ) wire.SetEdge ( index, aE );
394 else wire.AddEdge ( index, aE, iumin, iumax, ivmin, ivmax );
398 else cout << "Warning: ShapeFix_ComposeShell, ApplyContext: edge is to remove - not implemented" << endl;
401 return index - iedge;
405 //=======================================================================
406 //function : IsCoincided
408 //=======================================================================
409 // check points coincidence
410 static inline Standard_Integer IsCoincided (const gp_Pnt2d &p1, const gp_Pnt2d &p2,
411 const Standard_Real UResolution,
412 const Standard_Real VResolution,
413 const Standard_Real tol)
415 //pdn Maximal accuracy is working precision of intersector.
416 Standard_Real UTolerance = UResolution * tol;
417 Standard_Real VTolerance = VResolution * tol;
418 return Abs ( p1.X() - p2.X() ) <= Max(TOLINT,UTolerance) &&
419 Abs ( p1.Y() - p2.Y() ) <= Max(TOLINT,VTolerance);
423 //=======================================================================
424 //function : GetPatchIndex
426 //=======================================================================
427 // computes index for the patch by given parameter Param
428 static Standard_Integer GetPatchIndex (const Standard_Real Param,
429 const Handle(TColStd_HArray1OfReal) &Params,
430 const Standard_Boolean isClosed)
432 Standard_Integer NP = Params->Upper();
433 Standard_Real period = Params->Value(NP) - Params->Value(1);
434 Standard_Real shift = 0;
436 shift = ShapeAnalysis::AdjustToPeriod ( Param, Params->Value(1), Params->Value(NP) );
437 Standard_Real p = Param + shift;
439 // locate patch: the same algo as in SE_CS::LocateParameter()
440 Standard_Integer i; // svv #1
441 for ( i = 2; i < NP; i++ ) {
442 // Standard_Real par = Params->Value(i);
443 if ( p < Params->Value(i) ) break;
447 Standard_Real ish = shift / period;
448 Standard_Integer ishift = (Standard_Integer)( ish <0 ? ish - 0.5 : ish + 0.5 );
449 return i - ishift * ( NP - 1 );
453 //=======================================================================
454 //function : LoadWires
456 //=======================================================================
458 void ShapeFix_ComposeShell::LoadWires (ShapeFix_SequenceOfWireSegment &seqw) const
462 // Init seqw by initial set of wires (with corresponding orientation)
463 for ( TopoDS_Iterator iw(myFace,Standard_False); iw.More(); iw.Next() )
465 TopoDS_Shape tmpW = Context()->Apply ( iw.Value() ) ;
466 if(tmpW.ShapeType() != TopAbs_WIRE)
468 if(tmpW.ShapeType() == TopAbs_VERTEX)
470 ShapeFix_WireSegment seg; //(( isOuter ? TopAbs_REVERSED : TopAbs_FORWARD ) );
471 seg.SetVertex(TopoDS::Vertex(tmpW));
472 seg.Orientation(tmpW.Orientation());
479 TopoDS_Wire wire = TopoDS::Wire ( tmpW );
481 Standard_Boolean isNonManifold = ( wire.Orientation() != TopAbs_REVERSED &&
482 wire.Orientation() != TopAbs_FORWARD );
484 // protect against INTERNAL/EXTERNAL wires
485 // if ( wire.Orientation() != TopAbs_REVERSED &&
486 // wire.Orientation() != TopAbs_FORWARD ) continue;
488 // determine orientation of the wire
489 // TopoDS_Face face = TopoDS::Face ( myFace.EmptyCopied() );
490 // B.Add ( face, wire );
491 // Standard_Boolean isOuter = ShapeAnalysis::IsOuterBound ( face );
495 Handle(ShapeExtend_WireData) sbwd = new ShapeExtend_WireData ( wire ,Standard_True,Standard_False);
496 //pdn protection againts of wires w/o edges
497 Standard_Integer nbEdges = sbwd->NbEdges();
500 //wire segments for non-manifold topology should have INTERNAL orientation
501 ShapeFix_WireSegment seg ( sbwd, TopAbs_INTERNAL);
507 //splitting wires containing manifold and non-manifold parts on a separate
509 Handle(ShapeExtend_WireData) sbwdM = new ShapeExtend_WireData();
510 Handle(ShapeExtend_WireData) sbwdNM = new ShapeExtend_WireData();
511 sbwdNM->ManifoldMode() = Standard_False;
512 TopoDS_Iterator aIt(wire);
513 for( ; aIt.More(); aIt.Next())
515 TopoDS_Edge E = TopoDS::Edge ( aIt.Value() );
516 if(E.Orientation() == TopAbs_FORWARD || E.Orientation() == TopAbs_REVERSED)
522 Standard_Integer nbMEdges = sbwdM->NbEdges();
523 Standard_Integer nbNMEdges = sbwdNM->NbEdges();
527 ShapeFix_WireSegment seg ( sbwdNM, TopAbs_INTERNAL); //(( isOuter ? TopAbs_REVERSED : TopAbs_FORWARD ) );
532 // Orientation is set so as to allow the segment to be traversed in only one direction
534 Handle(ShapeFix_Wire) sfw = new ShapeFix_Wire;
536 Standard_Integer stat=0;
537 Handle(Geom_Surface) gs = BRep_Tool::Surface(myFace);
538 if( gs->IsUPeriodic() && gs->IsVPeriodic() )
540 // For torus-like shapes, first reorder in 2d since reorder is indifferent in 3d
541 ShapeAnalysis_WireOrder sawo(Standard_False, 0);
542 ShapeAnalysis_Edge sae;
543 for(Standard_Integer i = 1; i <= nbMEdges; i++) {
545 Handle(Geom2d_Curve) c2d;
547 TopoDS_Shape tmpF = myFace.Oriented(TopAbs_FORWARD);
548 if(!sae.PCurve(sbwdM->Edge(i),TopoDS::Face(tmpF),c2d,f,l))
550 sawo.Add(c2d->Value(f).XY(),c2d->Value(l).XY());
554 stat = (sawo.Status() < 0 ? -1 : 1);
555 sfw->FixReorder(sawo);
559 if (sfw->StatusReorder(ShapeExtend_DONE3))
565 TopoDS_Shape dummy = myFace.EmptyCopied();
566 TopoDS_Face face = TopoDS::Face ( dummy );
567 B.Add ( face, wire );
568 Standard_Boolean isOuter = ShapeAnalysis::IsOuterBound ( face );
569 TopoDS_Wire w = sbwdM->Wire();
570 dummy = myFace.EmptyCopied();
571 face = TopoDS::Face ( dummy );
573 Standard_Boolean isOuterAfter = ShapeAnalysis::IsOuterBound ( face );
574 if(isOuter!=isOuterAfter)
575 sbwdM->Reverse(face);
578 ShapeFix_WireSegment seg ( sbwdM, TopAbs_REVERSED ); //(( isOuter ? TopAbs_REVERSED : TopAbs_FORWARD ) );
586 //=======================================================================
587 //function : ComputeCode
588 //purpose : compute code for wire segment between two intersections (by deviation)
589 //=======================================================================
591 Standard_Integer ShapeFix_ComposeShell::ComputeCode (const Handle(ShapeExtend_WireData) &wire,
592 const gp_Lin2d &line,
593 const Standard_Integer begInd,
594 const Standard_Integer endInd,
595 const Standard_Real begPar,
596 const Standard_Real endPar,
597 const Standard_Boolean isInternal)
599 Standard_Integer code = IOR_UNDEF;
601 ShapeAnalysis_Edge sae;
602 const Standard_Integer NPOINTS = 5; // number of points for measuring deviation
604 // track special closed case: segment starts at end of edge and ends at its beginning
605 Standard_Integer special = ( begInd == endInd &&
606 ( wire->Edge(begInd).Orientation() == TopAbs_FORWARD ||
607 wire->Edge(begInd).Orientation() == TopAbs_INTERNAL) ==
608 ( begPar > endPar ) ? 1 : 0);
609 if ( ! special && begInd == endInd && begPar == endPar &&
610 (myClosedMode || isInternal))
613 // for tracking cases in closed mode
614 Standard_Boolean begin=Standard_True;
615 Standard_Real shift=0;
618 // check if segment is tangency
619 // Segment is considered as tangency if deviation of pcurve from line
620 // (in 2d) measured by NPOINTS points is less than tolerance of edge
621 // (recomputed to 2d using Resolution).
623 Standard_Integer nb = wire->NbEdges();
625 Standard_Integer i; // svv #1
626 for ( i=begInd; ; i++ ) {
628 TopoDS_Edge edge = wire->Edge ( i );;
630 Handle(Geom2d_Curve) c2d;
632 if ( ! sae.PCurve ( edge, myFace, c2d, f, l, Standard_False ) ) {
633 myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL3 );
636 Standard_Real tol = LimitTolerance(BRep_Tool::Tolerance ( edge ));
637 Standard_Boolean isreversed = ( edge.Orientation() == TopAbs_REVERSED );
639 Standard_Real par1 = ( i == begInd && special >=0 ? begPar : ( isreversed ? l : f ) );
640 Standard_Real par2 = ( i == endInd && special <=0 ? endPar : ( isreversed ? f : l ) );
641 Standard_Real dpar = ( par2 - par1 ) / ( NPOINTS - 1 );
642 Standard_Integer np = ( Abs ( dpar ) < ::Precision::PConfusion() ? 1 : NPOINTS );
643 Standard_Integer j; // svv #1
644 for ( j=0; j < np; j++ ) {
645 Standard_Real par = par1 + dpar * j;
646 gp_Pnt2d p2d = c2d->Value ( par );
647 if ( myClosedMode ) {
648 if ( myUClosed && Abs ( line.Direction().X() ) < ::Precision::PConfusion() ) {
649 if ( begin ) shift = ShapeAnalysis::AdjustByPeriod ( p2d.X(), line.Location().X(), myUPeriod );
650 else if ( ! j ) shift = ShapeAnalysis::AdjustByPeriod ( p2d.X()-p2d0.X(), 0., myUPeriod );
651 p2d.SetX ( p2d.X() + shift );
653 if ( myVClosed && Abs ( line.Direction().Y() ) < ::Precision::PConfusion() ) {
654 if ( begin ) shift = ShapeAnalysis::AdjustByPeriod ( p2d.Y(), line.Location().Y(), myVPeriod );
655 else if ( ! j ) shift = ShapeAnalysis::AdjustByPeriod ( p2d.Y()-p2d0.Y(), 0., myVPeriod );
656 p2d.SetY ( p2d.Y() + shift );
658 begin = Standard_False;
661 Standard_Integer pos = PointLinePosition ( p2d, line );
662 if ( pos == IOR_UNDEF ) continue;
664 // analyse the deviation
665 gp_Pnt2d p2dl = ProjectPointOnLine ( p2d, line );
666 if(!IsCoincided ( p2d, p2dl, myUResolution, myVResolution, tol )) {
667 if(!myClosedMode) { code = pos; break; }
673 if ( j < np ) { i = 0; break; } // not tangency
675 if ( special <=0 ) break;
679 if ( myClosedMode ) {
680 if ( code != IOR_UNDEF && ! begin ) {
681 // in closed mode, if segment is of 2*pi length, it is BOTH
682 Standard_Real dev = PointLineDeviation ( p2d0, line );
683 if ( myUClosed && Abs ( line.Direction().X() ) < ::Precision::PConfusion() ) {
684 if ( Abs ( Abs ( dev ) - myUPeriod ) < 0.1 * myUPeriod ) {
686 if ( dev >0 ) code |= IOR_POS;
688 else if(code==IOR_BOTH)
691 if ( myVClosed && Abs ( line.Direction().Y() ) < ::Precision::PConfusion() ) {
692 if ( Abs ( Abs ( dev ) - myVPeriod ) < 0.1 * myVPeriod ) {
694 if ( dev >0 ) code |= IOR_POS;
696 else if(code==IOR_BOTH)
702 if ( i ) code = IOR_UNDEF; // tangency
703 else if ( code == IOR_BOTH ) { // parity error in intersector
705 myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL2 );
707 cout << "Warning: ShapeFix_ComposeShell::ComputeCode: lost intersection point" << endl;
714 //=======================================================================
715 //function : DistributeSplitPoints
717 //=======================================================================
718 // After applying context to (seam) edge, distribute its indices on new edges,
719 // according to their parameters on that edge
720 static void DistributeSplitPoints (const Handle(ShapeExtend_WireData) &sbwd,
721 const TopoDS_Face myFace,
722 const Standard_Integer index,
723 const Standard_Integer nsplit,
724 TColStd_SequenceOfInteger& indexes,
725 const TColStd_SequenceOfReal& values)
727 Standard_Boolean isreversed = ( nsplit >0 && sbwd->Edge(index).Orientation() == TopAbs_REVERSED );
729 TColStd_Array1OfReal params(0,nsplit);
730 Standard_Integer i; // svv #1
731 for ( i=0; i < nsplit; i++ ) {
733 BRep_Tool::Range ( sbwd->Edge(index+i), myFace, f, l );
734 params.SetValue ( i, ( isreversed ? l : f ) );
737 for ( i=1; i <= indexes.Length() && indexes(i) < index; i++ );
738 for ( Standard_Integer shift = 1; i <= indexes.Length() && indexes(i) == index; i++ ) {
739 while ( shift < nsplit && isreversed != (Standard_Boolean) ( values(i) > params(shift) ) ) shift++;
740 indexes.SetValue ( i, index + shift - 1 );
742 for ( ; i <= indexes.Length(); i++ )
743 indexes.SetValue ( i, indexes(i) + nsplit - 1 );
747 //=======================================================================
748 //function : CheckByCurve3d
750 //=======================================================================
751 static Standard_Integer CheckByCurve3d (const gp_Pnt &pos,
752 const Handle(Geom_Curve) &c3d,
753 const Standard_Real param,
755 const Standard_Real tol)
757 if ( c3d.IsNull() ) return Standard_True;
758 gp_Pnt p = c3d->Value(param);
759 if ( T.Form() != gp_Identity ) p.Transform ( T );
760 return pos.SquareDistance ( p ) <= tol * tol;
764 //=======================================================================
765 //function : DefinePatch
767 //=======================================================================
768 static void DefinePatch (ShapeFix_WireSegment &wire, const Standard_Integer code,
769 const Standard_Boolean isCutByU, const Standard_Integer cutIndex,
770 const Standard_Integer number = -1)
772 Standard_Integer nb = (number > 0 ? number : wire.NbEdges());
774 if ( ! ( code & IOR_LEFT ) ) wire.DefineIUMin ( nb, cutIndex );
775 if ( ! ( code & IOR_RIGHT ) ) wire.DefineIUMax ( nb, cutIndex );
778 if ( ! ( code & IOR_RIGHT ) ) wire.DefineIVMin ( nb, cutIndex );
779 if ( ! ( code & IOR_LEFT ) ) wire.DefineIVMax ( nb, cutIndex );
784 //=======================================================================
785 //function : DefinePatchForWire
787 //=======================================================================
788 static void DefinePatchForWire(ShapeFix_WireSegment &wire, const Standard_Integer code,
789 const Standard_Boolean isCutByU, const Standard_Integer cutIndex)
791 for(Standard_Integer i = 1; i <= wire.NbEdges(); i++)
792 DefinePatch(wire,code,isCutByU,cutIndex,i);
796 //=======================================================================
797 //function : GetGridResolution
799 //=======================================================================
800 static Standard_Real GetGridResolution(const Handle(TColStd_HArray1OfReal) SplitValues,
801 const Standard_Integer cutIndex)
803 Standard_Integer nb = SplitValues->Length();
804 Standard_Real leftLen = (cutIndex > 1 ? SplitValues->Value(cutIndex) - SplitValues->Value(cutIndex-1) :
805 SplitValues->Value(nb) -SplitValues->Value(nb-1));
806 Standard_Real rigthLen =(cutIndex < nb ? SplitValues->Value(cutIndex+1)-SplitValues->Value(cutIndex) :
807 SplitValues->Value(2) - SplitValues->Value(1));
808 return Min(leftLen,rigthLen)/3.;
812 //=======================================================================
813 //function : SplitWire
815 //=======================================================================
817 ShapeFix_WireSegment ShapeFix_ComposeShell::SplitWire (ShapeFix_WireSegment &wire,
818 TColStd_SequenceOfInteger& indexes,
819 const TColStd_SequenceOfReal& values,
820 TopTools_SequenceOfShape& vertices,
821 const TColStd_SequenceOfInteger &SegmentCodes,
822 const Standard_Boolean isCutByU,
823 const Standard_Integer cutIndex)
826 ShapeFix_WireSegment result;
827 Handle(ShapeAnalysis_Surface) aSurfTool =
828 new ShapeAnalysis_Surface ( BRep_Tool::Surface (myFace) );
829 Standard_Integer nbSplits = indexes.Length();
830 ShapeAnalysis_Edge sae;
831 Standard_Integer start = 1;
832 TopAbs_Orientation anWireOrient = wire.Orientation();
834 if ( ! myLoc.IsIdentity() ) T = myLoc.Inverted().Transformation();
836 // Processing edge by edge (assuming that split points are sorted along the wire)
837 for ( Standard_Integer i = 1; i <= wire.NbEdges(); i++ ) {
839 // for already splitted seam edge, redistribute its splitting points
840 Standard_Integer nsplit = ApplyContext ( wire, i, Context() );
842 DistributeSplitPoints ( wire.WireData(), myFace, i, nsplit, indexes, values );
845 cout << "Error: ShapeFix_ComposeShell::SplitWire: edge dismissed" << endl;
851 TopoDS_Edge edge = wire.Edge(i);
853 Standard_Integer iumin, iumax, ivmin, ivmax;
854 wire.GetPatchIndex ( i, iumin, iumax, ivmin, ivmax );
856 // Position code for first segment of edge
857 Standard_Integer code = SegmentCodes ( start >1 ? start-1 : SegmentCodes.Length() );
859 // Defining split parameters on edge
860 Standard_Integer stop = start;
861 while ( stop <= nbSplits && indexes(stop) == i ) stop++;
862 if ( stop == start ) {
863 result.AddEdge ( 0, edge, iumin, iumax, ivmin, ivmax );
864 if(code!=0 || wire.Orientation()!=TopAbs_EXTERNAL) // pdn 0 code handling for extertnal wires
865 DefinePatch ( result, code, isCutByU, cutIndex );
868 //find non-manifold vertices on edge
869 TopTools_SequenceOfShape aNMVertices;
870 TopoDS_Iterator aIt(edge,Standard_False);
871 for( ; aIt.More(); aIt.Next()) {
872 if(aIt.Value().Orientation() != TopAbs_FORWARD &&
873 aIt.Value().Orientation() != TopAbs_REVERSED)
874 aNMVertices.Append(aIt.Value());
877 // Collect data on edge
878 Standard_Real tolEdge = BRep_Tool::Tolerance(edge);
879 Standard_Real tol = LimitTolerance( tolEdge );
880 TopoDS_Vertex prevV = sae.FirstVertex(edge);
881 TopoDS_Vertex lastV = sae.LastVertex(edge);
882 Standard_Real prevVTol = LimitTolerance( BRep_Tool::Tolerance(prevV) );
883 Standard_Real lastVTol = LimitTolerance( BRep_Tool::Tolerance(lastV) );
884 gp_Pnt prevVPnt = BRep_Tool::Pnt(prevV);
885 gp_Pnt lastVPnt = BRep_Tool::Pnt(lastV);
886 if ( T.Form() != gp_Identity ) {
887 prevVPnt.Transform ( T );
888 lastVPnt.Transform ( T );
891 Handle(Geom_Curve) c3d;
892 Standard_Real f3d, l3d;
893 if ( ! sae.Curve3d ( edge, c3d, f3d, l3d ) ) { // not a crime
898 Standard_Real firstPar, lastPar;
899 Handle(Geom2d_Curve) C2d;
900 if ( ! sae.PCurve ( edge, myFace, C2d, firstPar, lastPar ) ) {
901 myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL2 );
903 //finding sequence of non-manifold parameters
904 Standard_Integer nbNMVert = aNMVertices.Length();
905 TColStd_SequenceOfReal aNMVertParams;
907 Geom2dAdaptor_Curve adc(C2d);
909 Standard_Integer n =1;
910 for( ; n<= nbNMVert; n++) {
911 gp_Pnt apV = BRep_Tool::Pnt(TopoDS::Vertex(aNMVertices.Value(n)));
912 Standard_Real apar =firstPar;
913 Standard_Real adist2 =RealLast();
916 ShapeAnalysis_Curve asae;
917 adist2 = asae.Project(c3d,apV,Precision::Confusion(),aPproj,apar);
922 gp_Pnt2d aP2d = aSurfTool->ValueOfUV(apV,Precision::Confusion());
923 Extrema_ExtPC2d aExtr(aP2d,adc);
924 if(aExtr.IsDone() && aExtr.NbExt()) {
925 adist2 = aExtr.SquareDistance(1);
926 Standard_Integer index =1;
927 Standard_Integer k =2;
928 for( ; k <= aExtr.NbExt();k++) {
929 Standard_Real ad2 = aExtr.SquareDistance(k);
935 apar = aExtr.Point(index).Parameter();
939 aNMVertParams.Append(apar);
943 //pdn Claculating parametric shift
944 Standard_Boolean sp = (f3d == firstPar && l3d == lastPar);
945 Standard_Real span2d = lastPar - firstPar;
946 // Standard_Real ln2d = lastPar-prevPar;
947 // Standard_Real ln3d = l3d - f3d;
948 // Standard_Real fact = ln2d/ln3d;
949 // Standard_Real shift = prevPar - f3d*fact;
950 Standard_Real prevPar = firstPar;
951 gp_Pnt2d prevPnt2d = C2d->Value(prevPar);
952 gp_Pnt2d lastPnt2d = C2d->Value(lastPar);
953 gp_Pnt prevPnt = myGrid->Value ( prevPnt2d );
954 gp_Pnt lastPnt = myGrid->Value ( lastPnt2d );
955 Standard_Boolean isPeriodic = C2d->IsPeriodic();
956 Standard_Real aPeriod = (isPeriodic ? C2d->Period() :0.);
959 Standard_Integer NbEdgesStart = result.NbEdges();
960 Standard_Boolean splitted = Standard_False;
961 Standard_Real currPar=lastPar; //SK
962 for ( Standard_Integer j = start; j <= stop; prevPar = currPar, j++ ) {
963 if ( ! splitted && j >= stop ) { // no splitting at all
964 // code = SegmentCodes ( j >1 ? j-1 : SegmentCodes.Length() ); // classification code
967 currPar = ( j < stop ? values.Value(j) : lastPar );
969 //fix for case when pcurve is periodic and first parameter of edge is more than 2P
970 //method ShapeBuild_Edge::CopyRanges shift pcurve to range 0-2P and parameters of cutting
971 //should be shifted too. gka SAMTECH 28.07.06
973 if (currPar > (Max(lastPar,firstPar) +Precision::PConfusion()) ||
974 currPar < (Min(firstPar,lastPar)- Precision::PConfusion())) {
975 Standard_Real aShift = ShapeAnalysis::AdjustByPeriod(currPar, (firstPar+lastPar)*0.5,aPeriod);
983 // Try to adjust current splitting point to previous or end of edge
984 Standard_Boolean doCut = Standard_True;
986 if ( Abs ( currPar - lastPar ) < ::Precision::PConfusion() ) {
988 doCut = Standard_False;
990 else if ( Abs ( currPar - prevPar ) < ::Precision::PConfusion() ) {
991 vertices.Append ( prevV );
992 code = SegmentCodes ( j ); // classification code - update for next segment
993 continue; // no splitting at this point, go to next one
996 currPnt2d = C2d->Value(currPar);
997 currPnt = myGrid->Value ( currPnt2d );
998 if ( currPnt.Distance ( lastVPnt ) <= lastVTol &&
999 lastPnt.Distance ( currPnt ) <= tol &&
1000 CheckByCurve3d ( lastVPnt, c3d, f3d+(currPar-firstPar)*(l3d-f3d)/span2d,
1002 lastPnt.Distance ( myGrid->Value ( C2d->Value(0.5*(currPar+lastPar)) ) ) <= tol ) {
1004 Standard_Real uRes = myUResolution;
1005 Standard_Real vRes = myVResolution;
1007 Standard_Real gridRes = GetGridResolution(myGrid->UJointValues(),cutIndex)/tol;
1008 uRes = Min(myUResolution,gridRes);
1011 Standard_Real gridRes = GetGridResolution(myGrid->VJointValues(),cutIndex)/tol;
1012 vRes = Min(myVResolution,gridRes);
1014 if ( IsCoincided ( lastPnt2d, currPnt2d, uRes, vRes, tol ) &&
1015 IsCoincided ( lastPnt2d, C2d->Value(0.5*(currPar+lastPar)),
1016 uRes, vRes, tol ) ) doCut = Standard_False;
1018 else if ( currPnt.Distance ( prevVPnt ) <= prevVTol &&
1019 prevPnt.Distance ( currPnt ) <= tol &&
1020 CheckByCurve3d ( prevVPnt, c3d, f3d+(currPar-firstPar)*(l3d-f3d)/span2d,
1022 prevPnt.Distance ( myGrid->Value ( C2d->Value(0.5*(currPar+prevPar)) ) ) <= tol ) {
1024 Standard_Real uRes = myUResolution;
1025 Standard_Real vRes = myVResolution;
1027 Standard_Real gridRes = GetGridResolution(myGrid->UJointValues(),cutIndex)/tol;
1028 uRes = Min(myUResolution,gridRes);
1031 Standard_Real gridRes = GetGridResolution(myGrid->VJointValues(),cutIndex)/tol;
1032 vRes = Min(myVResolution,gridRes);
1034 if ( IsCoincided ( prevPnt2d, currPnt2d, uRes, vRes, tol ) &&
1035 IsCoincided ( prevPnt2d, C2d->Value(0.5*(currPar+prevPar)),
1036 uRes, vRes, tol ) ) {
1037 vertices.Append ( prevV );
1038 code = SegmentCodes ( j ); // classification code - update for next segment
1039 continue; // no splitting at this point, go to next one
1042 //:abv 28.05.02: OCC320 Sample_2: if maxtol = 1e-7, the vertex tolerance
1043 // is actually ignored - protect against new vertex on degenerated edge
1044 else if ( BRep_Tool::Degenerated(edge) && prevV.IsSame(lastV) ) {
1048 // classification code for current segment
1049 if ( j > start ) code = SegmentCodes ( j >1 ? j-1 : SegmentCodes.Length() );
1051 // if not adjusted, make new vertex
1053 B.MakeVertex ( V, currPnt.Transformed(myLoc.Transformation()), tolEdge );
1054 vertices.Append ( V );
1056 // else adjusted to end, fill all resting vertices
1057 else if ( ! doCut ) {
1058 for ( ; j < stop; j++ ) vertices.Append ( lastV );
1059 if ( ! splitted ) break; // no splitting at all
1062 else vertices.Append ( V );
1064 // When edge is about to be splitted, copy end vertices to protect
1065 // original shape from increasing tolerance after fixing SameParameter
1068 TopoDS_Shape emptyCopiedfV = prevV.EmptyCopied();
1069 TopoDS_Vertex fV = TopoDS::Vertex (emptyCopiedfV );
1070 Context()->Replace ( prevV, fV );
1072 if ( prevV.IsSame ( lastV ) ) {
1074 TopoDS_Shape tmpV = fV.Oriented ( lastV.Orientation() ) ;
1075 lV = TopoDS::Vertex (tmpV);
1079 TopoDS_Shape emptyCopied = lastV.EmptyCopied();
1080 lV = TopoDS::Vertex (emptyCopied);
1081 Context()->Replace ( lastV, lV );
1083 if ( V.IsSame ( lastV ) ) V = lV;
1084 else if ( V.IsSame ( prevV ) ) V = fV;
1089 // Splitting of the edge
1090 splitted = Standard_True;
1091 prevV.Orientation ( TopAbs_FORWARD );
1092 V.Orientation ( TopAbs_REVERSED );
1093 ShapeBuild_Edge sbe;
1094 TopoDS_Edge anInitEdge = edge;
1095 Standard_Boolean ismanifold = (edge.Orientation() == TopAbs_FORWARD ||
1096 edge.Orientation() == TopAbs_REVERSED);
1098 anInitEdge.Orientation(TopAbs_FORWARD);
1099 TopoDS_Edge newEdge = sbe.CopyReplaceVertices (anInitEdge, prevV, V );
1102 //addition internal vertices if they exists on edge
1103 Standard_Integer n =1;
1104 for( ; n <= aNMVertParams.Length(); n++) {
1105 Standard_Real apar = aNMVertParams.Value(n);
1106 TopoDS_Vertex aNMVert =TopoDS::Vertex(aNMVertices.Value(n));
1107 TopoDS_Vertex atmpV = TopoDS::Vertex(Context()->Apply(aNMVert));
1108 if(fabs(apar - prevPar) <= Precision::PConfusion()) {
1109 Context()->Replace(atmpV,prevV);
1110 aNMVertParams.Remove(n);
1111 aNMVertices.Remove(n);
1114 else if(fabs(apar - currPar) <= Precision::PConfusion()) {
1115 Context()->Replace(atmpV,V);
1116 aNMVertParams.Remove(n);
1117 aNMVertices.Remove(n);
1120 if(apar > prevPar && apar < currPar) {
1121 B.Add(newEdge,atmpV);
1122 aNMVertParams.Remove(n);
1123 aNMVertices.Remove(n);
1129 sbe.CopyPCurves ( newEdge, anInitEdge );
1132 Handle(ShapeAnalysis_TransferParameters) theTransferParamtool = GetTransferParamTool();
1133 theTransferParamtool->SetMaxTolerance(MaxTolerance());
1134 theTransferParamtool->Init(anInitEdge,myFace);
1135 theTransferParamtool->TransferRange(newEdge,prevPar,currPar,Standard_True);
1139 if(code == IOR_UNDEF) //tangential segment
1140 newEdge.Orientation(TopAbs_EXTERNAL);
1142 newEdge.Orientation(edge.Orientation());
1145 if(!sp && !BRep_Tool::Degenerated(newEdge))
1146 B.SameRange(newEdge, Standard_False);
1147 //pdn take into account 0 codes (if ext)
1148 if(code == 0 && wire.Orientation()==TopAbs_EXTERNAL){
1149 code = ( ( isCutByU == (Standard_Boolean)( j == 1 ) ) ? 1 : 2 );
1152 result.AddEdge ( 0, newEdge, iumin, iumax, ivmin, ivmax );
1153 DefinePatch ( result, code, isCutByU, cutIndex );
1155 // Changing prev parameters
1157 prevVTol = LimitTolerance( BRep_Tool::Tolerance ( V ) );
1158 prevVPnt = BRep_Tool::Pnt ( V );
1160 prevPnt2d = currPnt2d;
1165 // record replacement in context
1166 // NOTE: order of edges in the replacing wire corresponds to FORWARD orientation of the edge
1167 TopoDS_Wire resWire;
1168 B.MakeWire ( resWire );
1169 for ( Standard_Integer k=NbEdgesStart; k < result.NbEdges(); k++ ) {
1170 if ( edge.Orientation() == TopAbs_FORWARD || edge.Orientation() == TopAbs_INTERNAL)
1171 B.Add ( resWire, result.Edge(k+1) );
1172 else B.Add ( resWire, result.Edge(result.NbEdges()-k+NbEdgesStart) );
1174 Context()->Replace ( edge, resWire );
1177 if(anWireOrient == TopAbs_INTERNAL && code ==0) {
1178 ShapeBuild_Edge sbe;
1179 if(edge.Orientation() == TopAbs_INTERNAL)
1180 edge.Orientation(TopAbs_FORWARD);
1181 TopoDS_Edge e1 = sbe.Copy(edge,Standard_False);
1182 Handle(Geom2d_Curve) C2d2 = Handle(Geom2d_Curve)::DownCast(C2d->Copy());
1183 B.UpdateEdge(e1,C2d,C2d2,myFace,0.);
1184 e1.Orientation(TopAbs_EXTERNAL);
1185 Context()->Replace ( edge,e1);
1186 result.AddEdge ( 0,e1 , iumin, iumax, ivmin, ivmax );
1189 result.AddEdge ( 0, edge, iumin, iumax, ivmin, ivmax );
1190 if(code == 0 && wire.Orientation()==TopAbs_EXTERNAL){
1191 //pdn defining code for intersection of two isos
1192 code = ( ( isCutByU == (Standard_Boolean)( Abs(firstPar-currPar) < Abs(lastPar-currPar) ) ) ? 2 : 1 );
1194 DefinePatch ( result, code, isCutByU, cutIndex );
1197 result.Orientation ( anWireOrient );
1202 //=======================================================================
1203 //function : SplitByLine
1205 //=======================================================================
1207 Standard_Boolean ShapeFix_ComposeShell::SplitByLine (ShapeFix_WireSegment &wire,
1208 const gp_Lin2d &line,
1209 const Standard_Boolean isCutByU,
1210 const Standard_Integer cutIndex,
1211 TColStd_SequenceOfReal &SplitLinePar,
1212 TColStd_SequenceOfInteger &SplitLineCode,
1213 TopTools_SequenceOfShape &SplitLineVertex)
1215 ShapeAnalysis_Edge sae;
1216 // prepare data on cutting line
1217 Handle(Geom2d_Line) jC2d = new Geom2d_Line ( line );
1218 Geom2dAdaptor_Curve jGAC(jC2d);
1220 TColStd_SequenceOfInteger IntEdgeInd; // index of intersecting edge
1221 TColStd_SequenceOfReal IntEdgePar; // parameter of intersection point on edge
1222 TColStd_SequenceOfReal IntLinePar; // parameter of intersection point on line
1224 Standard_Boolean isnonmanifold = (wire.Orientation() == TopAbs_INTERNAL);
1225 //gka correction for non-manifold vertices SAMTECH
1226 if(wire.IsVertex()) {
1227 Handle(ShapeAnalysis_Surface) aSurfTool = new ShapeAnalysis_Surface ( BRep_Tool::Surface (myFace) );
1228 TopoDS_Vertex aVert = wire.GetVertex();
1229 gp_Pnt aP3d = BRep_Tool::Pnt(aVert);
1230 gp_Pnt2d aP2d = aSurfTool->ValueOfUV(aP3d,Precision::Confusion());
1231 Standard_Real dev =0.;
1232 Standard_Integer code = PointLinePosition(aP2d,line,dev);
1233 if(code != IOR_UNDEF)
1234 return Standard_False;
1235 Standard_Real par = ParamPointOnLine (aP2d,line);
1236 SplitLinePar.Append ( par );
1237 //splitting codes for non-manifold topology should be tangential
1238 SplitLineCode.Append (ITP_TANG); //ITP_INTER);
1239 TopoDS_Vertex aVertNew;
1241 aB.MakeVertex(aVertNew,aP3d,BRep_Tool::Tolerance(aVert));
1242 aVertNew.Orientation(TopAbs_FORWARD);
1243 Context()->Replace(aVert,aVertNew);
1244 SplitLineVertex.Append (aVertNew);
1245 wire.SetVertex(aVertNew);
1246 return Standard_True;
1248 const Handle(ShapeExtend_WireData) sewd = wire.WireData();
1250 Standard_Integer nbe = sewd->NbEdges();
1252 //:abv 31.10.01: for closed mode
1253 Standard_Integer closedDir = 0;
1254 if ( myClosedMode ) {
1255 if ( myUClosed && Abs ( line.Direction().X() ) < ::Precision::PConfusion() )
1257 else if ( myVClosed && Abs ( line.Direction().Y() ) < ::Precision::PConfusion() )
1260 Standard_Real halfPeriod = 0.5 * ( closedDir ? closedDir <0 ? myUPeriod : myVPeriod : 0. );
1262 //============================================
1263 // make intersections and collect all data on intersection points
1264 Standard_Integer firstCode=0, prevCode=0;
1265 gp_Pnt2d firstPos, prevPos;
1266 Standard_Real firstDev=0., prevDev=0.;
1267 for (Standard_Integer iedge = 1; iedge <= nbe; iedge++) {
1269 TopoDS_Edge E= sewd->Edge ( iedge );
1270 Standard_Boolean isreversed = ( E.Orientation() == TopAbs_REVERSED );
1273 Handle(Geom2d_Curve) c2d;
1274 if ( ! sae.PCurve ( E, myFace, c2d, f, l, Standard_False ) ) continue;
1277 gp_Pnt2d posf = c2d->Value(f), posl = c2d->Value(l);
1278 gp_XY pppf = posf.XY(), pppl = posl.XY();
1280 // In case of ClosedMode, adjust curve and end points to period on closed surface
1281 //:abv 16.10.01: Ziegler_CADDY01.sat -18: if pcurve is longer than period,
1282 // ensure processing of all intersections
1283 Standard_Integer nbIter = 1;
1284 gp_Vec2d shiftNext(0.,0.);
1285 if ( myClosedMode ) {
1287 // get bounding box of pcurve
1288 ShapeAnalysis_Curve sac;
1290 sac.FillBndBox ( c2d, f, l, 11, Standard_True, box );
1291 Standard_Real umin, vmin, umax, vmax;
1292 box.Get ( umin, vmin, umax, vmax );
1294 // compute shifts and adjust points adjust
1295 if ( closedDir < 0 ) {
1296 Standard_Real x = line.Location().X();
1297 Standard_Real shift = ShapeAnalysis::AdjustToPeriod ( umin, x-myUPeriod, x );
1298 if ( shift != 0. ) {
1299 c2d = Handle(Geom2d_Curve)::DownCast ( c2d->Copy() );
1300 gp_Vec2d V ( shift, 0. );
1301 c2d->Translate ( V );
1302 pppf.SetX ( pppf.X() + shift );
1303 pppl.SetX ( pppl.X() + shift );
1305 shiftNext.SetX ( -myUPeriod );
1306 nbIter = (Standard_Integer)( 1 + Abs ( umax + shift - x ) / myUPeriod );
1307 shift = ShapeAnalysis::AdjustByPeriod ( posf.X(), x, myUPeriod );
1308 posf.SetX ( posf.X() + shift );
1309 shift = ShapeAnalysis::AdjustByPeriod ( posl.X(), x, myUPeriod );
1310 posl.SetX ( posl.X() + shift );
1312 else if ( closedDir > 0 ) {
1313 Standard_Real y = line.Location().Y();
1314 Standard_Real shift = ShapeAnalysis::AdjustToPeriod ( vmin, y-myVPeriod, y );
1315 if ( shift != 0. ) {
1316 c2d = Handle(Geom2d_Curve)::DownCast ( c2d->Copy() );
1317 gp_Vec2d V ( 0., shift );
1318 c2d->Translate ( V );
1319 pppf.SetY ( pppf.Y() + shift );
1320 pppl.SetY ( pppl.Y() + shift );
1322 shiftNext.SetY ( -myVPeriod );
1323 nbIter = (Standard_Integer)( 1 + Abs ( umax + shift - y ) / myVPeriod );
1324 shift = ShapeAnalysis::AdjustByPeriod ( posf.Y(), y, myVPeriod );
1325 posf.SetY ( posf.Y() + shift );
1326 shift = ShapeAnalysis::AdjustByPeriod ( posl.Y(), y, myVPeriod );
1327 posl.SetY ( posl.Y() + shift );
1331 // detect intersections at junction of two edges
1332 gp_Pnt2d pos = ( isreversed ? posl : posf );
1334 Standard_Integer code = PointLinePosition ( pos, line, dev );
1335 if ( iedge ==1 ) { firstCode = code; firstPos = pos; firstDev = dev; }
1336 else if ( code == IOR_UNDEF || code != prevCode ) {
1337 if ( ! closedDir || Abs ( dev - prevDev ) < halfPeriod ) {
1338 IntLinePar.Append ( ParamPointsOnLine ( pos, prevPos, line ) ); // !! - maybe compute exactly ?
1339 IntEdgePar.Append ( isreversed ? l : f );
1340 IntEdgeInd.Append ( iedge );
1344 // fill data on end point (for next edge)
1345 pos = ( isreversed ? posf : posl );
1346 prevCode = PointLinePosition ( pos, line, prevDev );
1349 // cycle with shift in order to track all possible intersections
1350 for ( Standard_Integer iter=1; iter <= nbIter; iter++ ) {
1352 // data for intersection
1353 IntRes2d_Domain iDom ( pppf, f, TOLINT, pppl, l, TOLINT );
1354 Geom2dAdaptor_Curve iGAC(c2d);
1357 Geom2dInt_GInter Inter;
1358 Inter.Perform ( jGAC, /*jDom,*/ iGAC, iDom, TOLINT, TOLINT );
1360 // Fill arrays with new intersection points
1361 if ( Inter.IsDone() ) {
1364 for ( i = 1; i <= Inter.NbPoints(); i++ ) {
1365 IntRes2d_IntersectionPoint IP = Inter.Point (i);
1366 IntLinePar.Append ( IP.ParamOnFirst() );
1367 IntEdgePar.Append ( IP.ParamOnSecond() );
1369 for ( i = 1; i <= Inter.NbSegments(); i++ ) {
1370 IntRes2d_IntersectionSegment IS = Inter.Segment (i);
1371 if ( IS.HasFirstPoint() ) {
1372 IntRes2d_IntersectionPoint IP = IS.FirstPoint();
1373 IntLinePar.Append ( IP.ParamOnFirst() );
1374 IntEdgePar.Append ( IP.ParamOnSecond() );
1376 if ( IS.HasLastPoint() ) {
1377 IntRes2d_IntersectionPoint IP = IS.LastPoint();
1378 IntLinePar.Append ( IP.ParamOnFirst() );
1379 IntEdgePar.Append ( IP.ParamOnSecond() );
1384 if ( iter < nbIter ) {
1385 if ( iter == 1 ) c2d = Handle(Geom2d_Curve)::DownCast ( c2d->Copy() );
1386 pppf += shiftNext.XY();
1387 pppl += shiftNext.XY();
1388 c2d->Translate ( shiftNext );
1392 Standard_Integer start = IntEdgeInd.Length() + 1; // first of the new points
1394 // Move all points into range [f,l] (intersector sometimes gives params out of range)
1396 for ( i = start; i <= IntEdgePar.Length(); i++ ) {
1397 if ( IntEdgePar(i) < f ) IntEdgePar.SetValue ( i, f );
1398 else if ( IntEdgePar(i) > l ) IntEdgePar.SetValue ( i, l );
1401 // Sort by parameter on edge
1402 for ( i = IntEdgePar.Length(); i > start; i-- )
1403 for ( Standard_Integer j = start; j < i; j++ ) {
1404 if ( isreversed == (Standard_Boolean) ( IntEdgePar(j+1) < IntEdgePar(j) ) ) continue;
1405 IntLinePar.Exchange ( j, j+1 );
1406 IntEdgePar.Exchange ( j, j+1 );
1410 for ( i = start; i <= IntEdgePar.Length(); i++ )
1411 IntEdgeInd.Append ( iedge );
1413 // Detect intersection at closing point
1414 // Only wires which are not EXTERNAL are considered (as closed)
1415 if ( iedge == nbe && wire.Orientation() != TopAbs_EXTERNAL &&
1416 wire.Orientation() != TopAbs_INTERNAL &&
1417 ( prevCode == IOR_UNDEF || prevCode != firstCode ) ) {
1418 if ( ! closedDir || Abs ( firstDev - prevDev ) < halfPeriod ) {
1419 IntLinePar.Append ( ParamPointsOnLine ( pos, firstPos, line ) );
1420 IntEdgePar.Append ( isreversed ? f : l );
1421 IntEdgeInd.Append ( iedge );
1426 if ( IntEdgePar.Length() <1 ) {
1427 //pdn Defining position of wire. There is no intersection, so by any point.
1428 DefinePatchForWire ( wire, firstCode, isCutByU, cutIndex );
1429 return Standard_False; //pdn ??
1432 //======================================
1433 // Fill sequence of transition codes for intersection points
1434 TColStd_SequenceOfInteger IntCode; // parameter of intersection point on line
1435 TColStd_SequenceOfInteger SegmentCodes; // classification codes for segments of wire
1437 // remove duplicated points to ensure correct results of ComputeCode
1438 Standard_Integer i, j = IntEdgePar.Length();
1439 if ( myClosedMode && j >1 ) {
1440 for ( i = 1; i <= IntEdgePar.Length(); ) {
1441 if ( i == j ) break;
1442 if ( IntEdgeInd(i) == IntEdgeInd(j) &&
1443 Abs ( IntEdgePar(i) - IntEdgePar(j) ) < ::Precision::PConfusion() ) {
1444 IntLinePar.Remove(i);
1445 IntEdgePar.Remove(i);
1446 IntEdgeInd.Remove(i);
1450 else if ( nbe ==1 || IntEdgeInd(i) == (IntEdgeInd(j)%nbe)+1 ) {
1451 TopoDS_Edge E1 = sewd->Edge ( IntEdgeInd(j) );
1452 TopoDS_Edge E2 = sewd->Edge ( IntEdgeInd(i) );
1453 Standard_Real a1, b1, a2, b2;
1454 BRep_Tool::Range ( E1, myFace, a1, b1 );
1455 BRep_Tool::Range ( E2, myFace, a2, b2 );
1456 if ( Abs ( IntEdgePar(j) - ( E1.Orientation() == TopAbs_FORWARD ? b1 : a1 ) ) < ::Precision::PConfusion() &&
1457 Abs ( IntEdgePar(i) - ( E2.Orientation() == TopAbs_FORWARD ? a2 : b2 ) ) < ::Precision::PConfusion() ) {
1458 IntLinePar.Remove(i);
1459 IntEdgePar.Remove(i);
1460 IntEdgeInd.Remove(i);
1469 // Compute segment codes (left side of line, right or tangential)
1470 for ( i=1; i <= IntEdgePar.Length(); i++ ) {
1471 j = ( i < IntEdgePar.Length() ? i + 1 : 1 );
1472 Standard_Integer code = ComputeCode ( sewd, line, IntEdgeInd(i), IntEdgeInd(j),
1473 IntEdgePar(i), IntEdgePar(j),isnonmanifold );
1474 SegmentCodes.Append ( code );
1477 // for EXTERNAL wire, i.e. another joint line, every point is double intersection
1478 if ( wire.Orientation() == TopAbs_EXTERNAL ) {
1479 for ( i=1; i <= IntEdgePar.Length(); i++ )
1480 IntCode.Append ( ITP_TANG | IOR_BOTH );
1482 // For real (closed) wire, analyze tangencies
1484 if(wire.Orientation() != TopAbs_INTERNAL) {
1485 // Two consecutive tangential segments are considered as one, merge them.
1486 for ( i=1; i <= IntEdgePar.Length(); i++ ) {
1487 j = ( i > 1 ? i-1 : IntEdgePar.Length() );
1489 int k = ( i < IntEdgePar.Length() ? i + 1 : 1 ); // [ACIS22539]
1491 if ( SegmentCodes(j) == IOR_UNDEF &&
1492 SegmentCodes(i) == IOR_UNDEF ) {
1494 // Very specific case when the constructed seam edge
1495 // overlaps with spur edge [ACIS22539]
1496 if (myClosedMode && (IntLinePar(i) - IntLinePar(j)) * (IntLinePar(k) - IntLinePar(i)) <= 0. )
1499 IntEdgeInd.Remove(i);
1500 IntEdgePar.Remove(i);
1501 IntLinePar.Remove(i);
1502 SegmentCodes.Remove(i);
1507 //pdn exit if all split points removed
1508 if ( IntEdgePar.Length() <1 ) {
1509 //DefinePatchForWire ( wire, firstCode, isCutByU, cutIndex );
1510 return Standard_False; //pdn ??
1513 // Analyze type of intersection point and encode it
1514 // Three kinds of points (ITP): clear intersection, tangency in-point,
1515 // beginning and end of tangential segment.
1516 // Orientation (IOR) tells on which side of line edge crosses it
1517 j = IntEdgePar.Length();
1518 for ( i=1; i <= IntEdgePar.Length(); j = i++ ) {
1519 Standard_Integer codej = SegmentCodes(j);
1520 Standard_Integer codei = SegmentCodes(i);
1521 if ( myClosedMode ) {
1522 if ( ( codej & IOR_BOTH ) == IOR_BOTH ) //IOR_LEFT : IOR_RIGHT
1523 codej = ( codej & IOR_POS ? IOR_RIGHT : IOR_LEFT );
1524 if ( ( codei & IOR_BOTH ) == IOR_BOTH ) //IOR_RIGHT : IOR_LEFT
1525 codei = ( codei & IOR_POS ? IOR_LEFT : IOR_RIGHT );
1527 Standard_Integer ipcode = ( codej | codei );
1528 if ( codej == IOR_UNDEF ) { // previous segment was tangency
1529 if ( IntLinePar(i) > IntLinePar (j) )
1530 ipcode |= ITP_ENDSEG; // end of segment
1531 else ipcode |= ITP_BEGSEG; // beginning of segment
1533 else if ( codei == IOR_UNDEF ) { // current segment is tangency
1534 if ( IntLinePar ( i < IntLinePar.Length() ? i+1 : 1 ) > IntLinePar(i) )
1535 ipcode |= ITP_BEGSEG; // beginning of segment
1536 else ipcode |= ITP_ENDSEG; // end of segment
1538 //internal wire can be only tangent
1539 else if ( i == j ) ipcode |= ( ( ipcode & IOR_BOTH ) == IOR_BOTH && !isnonmanifold ? ITP_INTER : ITP_TANG );
1540 else if ( codei == codej || isnonmanifold) ipcode |= ITP_TANG; // tangency in-point
1541 else ipcode |= ITP_INTER; // standard crossing
1542 IntCode.Append ( ipcode );
1546 //=======================================
1547 // Split edges in the wire by intersection points and fill vertices array
1548 TopTools_SequenceOfShape IntVertices;
1549 wire = SplitWire ( wire, IntEdgeInd, IntEdgePar, IntVertices,
1550 SegmentCodes, isCutByU, cutIndex );
1552 // add all data to input arrays
1553 for ( i=1; i <= IntLinePar.Length(); i++ ) {
1554 SplitLinePar.Append ( IntLinePar(i) );
1555 SplitLineCode.Append ( IntCode(i) );
1556 SplitLineVertex.Append ( IntVertices(i) );
1559 return Standard_True;
1563 //=======================================================================
1564 //function : SplitByLine
1566 //=======================================================================
1568 void ShapeFix_ComposeShell::SplitByLine (ShapeFix_SequenceOfWireSegment &wires,
1569 const gp_Lin2d &line,
1570 const Standard_Boolean isCutByU,
1571 const Standard_Integer cutIndex)
1573 TColStd_SequenceOfReal SplitLinePar;
1574 TColStd_SequenceOfInteger SplitLineCode;
1575 TopTools_SequenceOfShape SplitLineVertex;
1577 // split wires one by one, collecting data on intersection points
1578 Standard_Integer i; // svv #1
1579 for ( i=1; i <= wires.Length(); i++ ) {
1580 SplitByLine ( wires(i), line, isCutByU, cutIndex,
1581 SplitLinePar, SplitLineCode, SplitLineVertex );
1584 // sort intersection points along parameter on cutting line
1585 for ( i = SplitLinePar.Length(); i >1; i-- )
1586 for ( Standard_Integer j=1; j < i; j++ ) {
1587 if ( SplitLinePar(j) > SplitLinePar(j+1) ) {
1588 SplitLinePar.Exchange ( j, j+1 );
1589 SplitLineCode.Exchange ( j, j+1 );
1590 SplitLineVertex.Exchange ( j, j+1 );
1594 // merge null-length tangential segments into one-point tangencies or intersections
1595 for ( i = 1; i < SplitLinePar.Length(); i++ ) {
1596 if ( Abs ( SplitLinePar(i+1) - SplitLinePar(i) ) > ::Precision::PConfusion() ) continue;
1597 if ( ( SplitLineCode(i) & ITP_ENDSEG &&
1598 SplitLineCode(i+1) & ITP_BEGSEG ) ||
1599 ( SplitLineCode(i) & ITP_BEGSEG &&
1600 SplitLineCode(i+1) & ITP_ENDSEG ) ) {
1601 Standard_Integer code = ( SplitLineCode(i) | SplitLineCode(i+1) ) & IOR_BOTH;
1602 SplitLineCode.SetValue ( i, code | ( code == IOR_BOTH ? ITP_INTER : ITP_TANG ) );
1603 SplitLinePar.Remove(i+1);
1604 SplitLineCode.Remove(i+1);
1605 SplitLineVertex.Remove(i+1);
1609 // go along line, split it by intersection points and create edges
1610 // (only for internal parts, in particular not for tangential segments)
1612 Standard_Integer parity = 0; // 0 - out, 1 - in
1613 Standard_Integer halfparity = 0; // left/right for tangential segments
1614 Standard_Integer tanglevel = 0; // tangency nesting level
1615 for ( i = 1; i <= SplitLinePar.Length(); i++ ) {
1616 Standard_Integer code = SplitLineCode(i);
1617 Standard_Boolean interior = ( ! tanglevel && parity % 2 ); // create an edge
1618 if ( code & ITP_INTER ) { // crossing
1621 else if ( code & ITP_BEGSEG ) { // beginning of tangential segment
1623 if ( ! halfparity ) halfparity = ( code & IOR_BOTH );
1624 else if ( halfparity != ( code & IOR_BOTH ) ) parity++;
1626 else if ( code & ITP_ENDSEG ) { // end of tangential segment
1628 if ( ! halfparity ) halfparity = ( code & IOR_BOTH );
1629 else if ( halfparity != ( code & IOR_BOTH ) ) parity++;
1631 if ( tanglevel <0 ) {
1632 // myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL4 );
1634 cout << "Warning: ShapeFix_ComposeShell::SplitByLine: tangency level <0 !" << endl;
1637 if ( ! interior ) continue;
1639 // apply context to vertices (to perform replacing/merging vertices)
1641 TopoDS_Shape tmpV1 = Context()->Apply ( SplitLineVertex(i-1) );
1642 TopoDS_Shape tmpV2 = Context()->Apply ( SplitLineVertex(i) );
1643 TopoDS_Vertex V1 = TopoDS::Vertex ( tmpV1 );
1644 TopoDS_Vertex V2 = TopoDS::Vertex ( tmpV2 );
1645 // protection against creating null-length edges
1646 if ( SplitLinePar(i) - SplitLinePar(i-1) < ::Precision::PConfusion() ) {
1649 cout << "Info: ShapeFix_ComposeShell::SplitByLine: Short segment ignored" << endl;
1651 if ( ! V1.IsSame ( V2 ) ) { // merge coincident vertices
1652 ShapeBuild_Vertex sbv;
1653 TopoDS_Vertex V = sbv.CombineVertex ( V1, V2 );
1654 Context()->Replace ( V1, V.Oriented ( V1.Orientation() ) );
1655 Context()->Replace ( V2, V.Oriented ( V2.Orientation() ) );
1658 cout << "Info: ShapeFix_ComposeShell::SplitByLine: Coincided vertices merged" << endl;
1664 // create an edge (without 3d curve), put it in wire segment and add to sequence
1665 // NOTE: i here is always >1
1667 B.MakeEdge ( edge );
1668 V1.Orientation ( TopAbs_FORWARD );
1669 V2.Orientation ( TopAbs_REVERSED );
1672 Handle(Geom2d_Line) Lin1 = new Geom2d_Line ( line );
1673 Handle(Geom2d_Line) Lin2 = new Geom2d_Line ( line );
1674 B.UpdateEdge ( edge, Lin1, Lin2, myFace, ::Precision::Confusion() );
1675 B.Range ( edge, myFace, SplitLinePar(i-1), SplitLinePar(i) );
1677 Handle(ShapeExtend_WireData) sbwd = new ShapeExtend_WireData;
1679 ShapeFix_WireSegment seg ( sbwd, TopAbs_EXTERNAL );
1681 // set patch indices
1682 DefinePatch ( seg, IOR_UNDEF, isCutByU, cutIndex );
1684 seg.DefineIUMin ( 1, GetPatchIndex ( SplitLinePar(i-1)+::Precision::PConfusion(),
1685 myGrid->UJointValues(), myUClosed ) );
1686 seg.DefineIUMax ( 1, GetPatchIndex ( SplitLinePar(i)-::Precision::PConfusion(),
1687 myGrid->UJointValues(), myUClosed ) + 1 );
1690 seg.DefineIVMin ( 1, GetPatchIndex ( SplitLinePar(i-1)+::Precision::PConfusion(),
1691 myGrid->VJointValues(), myVClosed ) );
1692 seg.DefineIVMax ( 1, GetPatchIndex ( SplitLinePar(i)-::Precision::PConfusion(),
1693 myGrid->VJointValues(), myVClosed ) + 1 );
1696 wires.Append ( seg );
1699 myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL4 );
1701 cout << "Error: ShapeFix_ComposeShell::SplitByLine: parity error" << endl;
1705 // Apply context to all wires to perform all recorded replacements/merging
1706 for ( i=1; i <= wires.Length(); i++ ) {
1707 for ( Standard_Integer j=1; j <= wires(i).NbEdges(); )
1708 j += ApplyContext ( wires(i), j, Context() );
1713 //=======================================================================
1714 //function : SplitByGrid
1716 //=======================================================================
1718 void ShapeFix_ComposeShell::SplitByGrid (ShapeFix_SequenceOfWireSegment &seqw)
1720 // process splitting by U- anv V-seams (i.e. U=const and V=const curves)
1721 // closed composite surface is processed as periodic
1722 Standard_Real Uf,Ul,Vf,Vl;
1723 BRepTools::UVBounds(myFace,Uf,Ul,Vf,Vl);
1724 Standard_Real Umin,Umax,Vmin,Vmax;
1725 myGrid->Bounds(Umin,Umax,Vmin,Vmax);
1726 Standard_Real pprec = ::Precision::PConfusion();
1729 Standard_Integer i; // svv #1
1730 for ( i = ( myUClosed ? 1 : 2 ); i <= myGrid->NbUPatches(); i++ ) {
1731 gp_Pnt2d pos ( myGrid->UJointValue(i), 0. ); // 0. - for infinite ranges: myGrid->VJointValue(1) ;
1732 gp_Lin2d line ( pos, gp_Dir2d ( 0., 1. ) );
1733 if ( ! myClosedMode && myUClosed ) {
1734 Standard_Real period = Umax - Umin;
1735 Standard_Real X = pos.X();
1736 Standard_Real sh = ShapeAnalysis::AdjustToPeriod(X,Uf, Uf+period);
1737 for( ; X+sh <= Ul+pprec; sh += period ) {
1738 gp_Lin2d ln = line.Translated(gp_Vec2d(sh,0));
1739 Standard_Integer cutIndex = GetPatchIndex ( X+sh+pprec, myGrid->UJointValues(), myUClosed );
1740 SplitByLine ( seqw, ln, Standard_True, cutIndex );
1744 SplitByLine ( seqw, line, Standard_True, i );
1748 for ( i = ( myVClosed ? 1 : 2 ); i <= myGrid->NbVPatches(); i++ ) {
1749 gp_Pnt2d pos ( 0., myGrid->VJointValue(i) );
1750 gp_Lin2d line ( pos, gp_Dir2d ( 1., 0. ) );
1751 if ( ! myClosedMode && myVClosed ) {
1752 Standard_Real period = Vmax - Vmin;
1753 Standard_Real Y = pos.Y();
1754 Standard_Real sh = ShapeAnalysis::AdjustToPeriod(Y,Vf, Vf+period);
1755 for( ; Y+sh <= Vl+pprec; sh += period) {
1756 gp_Lin2d ln = line.Translated(gp_Vec2d(0,sh));
1757 Standard_Integer cutIndex = GetPatchIndex ( Y+sh+pprec, myGrid->VJointValues(), myVClosed );
1758 SplitByLine ( seqw, ln, Standard_False, cutIndex );
1762 SplitByLine ( seqw, line, Standard_False, i );
1765 // limit patch indices to be in range of grid (extended for periodic)
1766 Standard_Integer iumin = GetPatchIndex ( Uf+pprec, myGrid->UJointValues(), myUClosed );
1767 Standard_Integer iumax = GetPatchIndex ( Ul-pprec, myGrid->UJointValues(), myUClosed ) + 1;
1768 for ( i=1; i <= seqw.Length(); i++ ) {
1769 ShapeFix_WireSegment &wire = seqw(i);
1770 for ( Standard_Integer j=1; j <= wire.NbEdges(); j++ ) {
1771 wire.DefineIUMin ( j, iumin );
1772 wire.DefineIUMax ( j, iumax );
1775 Standard_Integer ivmin = GetPatchIndex ( Vf+pprec, myGrid->VJointValues(), myVClosed );
1776 Standard_Integer ivmax = GetPatchIndex ( Vl-pprec, myGrid->VJointValues(), myVClosed ) + 1;
1777 for ( i=1; i <= seqw.Length(); i++ ) {
1778 ShapeFix_WireSegment &wire = seqw(i);
1779 for ( Standard_Integer j=1; j <= wire.NbEdges(); j++ ) {
1780 wire.DefineIVMin ( j, ivmin );
1781 wire.DefineIVMax ( j, ivmax );
1787 //=======================================================================
1788 //function : BreakWires
1790 //=======================================================================
1792 void ShapeFix_ComposeShell::BreakWires (ShapeFix_SequenceOfWireSegment &seqw)
1795 // split all the wires by vertices
1796 TopTools_MapOfShape splitVertices;
1797 ShapeAnalysis_Edge sae;
1799 // first collect splitting vertices
1800 Standard_Integer i; // svv #1
1801 for ( i=1; i <= seqw.Length(); i++ ) {
1802 TopAbs_Orientation ori_wire = seqw(i).Orientation();
1803 if ( ori_wire != TopAbs_EXTERNAL &&
1804 ori_wire != TopAbs_INTERNAL) continue;
1806 Handle(ShapeExtend_WireData) sbwd = seqw(i).WireData();
1807 for ( Standard_Integer j=1; j <= sbwd->NbEdges(); j++ ) {
1808 TopoDS_Edge edge = sbwd->Edge ( j );
1809 TopAbs_Orientation ori_edge = (ori_wire == TopAbs_EXTERNAL ? ori_wire : edge.Orientation());
1810 if(ori_edge == TopAbs_EXTERNAL) {
1811 splitVertices.Add ( sae.FirstVertex ( edge ) );
1812 splitVertices.Add ( sae.LastVertex ( edge ) );
1817 // and then split each vire
1818 // Here each wire is supposed to be connected (while probably not closed)
1819 for ( i=1; i <= seqw.Length(); i++ ) {
1820 TopAbs_Orientation ori = seqw(i).Orientation();
1821 ShapeFix_WireSegment wire = seqw(i);
1824 Handle(ShapeExtend_WireData) sbwd = wire.WireData();
1826 // find first vertex for split
1827 Standard_Integer j; // svv #1
1828 for ( j=1; j <= sbwd->NbEdges(); j++ ) {
1829 TopoDS_Vertex V = sae.FirstVertex ( sbwd->Edge(j) );
1830 if ( splitVertices.Contains ( V ) ) break;
1832 if ( j > sbwd->NbEdges() ) continue; // splitting not needed
1834 // if first split of closed edge is not its start, make permutation
1835 Standard_Integer shift = 0;
1836 if ( j >1 && ! myClosedMode && wire.IsClosed() ) {
1837 TopoDS_Vertex V = sae.FirstVertex ( sbwd->Edge(1) );
1838 if ( ! splitVertices.Contains ( V ) )
1840 // wire.SetLast ( j-1 );
1843 // perform splitting
1844 Standard_Integer nbnew = 0;
1845 ShapeFix_WireSegment newwire;
1846 TopAbs_Orientation curOri = ori;
1847 for ( Standard_Integer ind=1; ind <= sbwd->NbEdges(); ind++ ) {
1848 j = 1 + ( ind - 1 + shift ) % sbwd->NbEdges();
1849 TopoDS_Edge edge = sbwd->Edge(j);
1850 TopoDS_Vertex V = sae.FirstVertex ( edge );
1851 if ( ind==1 || splitVertices.Contains ( V ) ) {
1852 if ( newwire.NbEdges() ) {
1853 newwire.Orientation ( curOri );
1854 // ShapeFix_WireSegment seg ( newwire, ori );
1855 seqw.InsertBefore ( i++, newwire );
1861 Standard_Integer iumin, iumax, ivmin, ivmax;
1862 wire.GetPatchIndex ( j, iumin, iumax, ivmin, ivmax );
1863 if(ori == TopAbs_INTERNAL && edge.Orientation() == TopAbs_EXTERNAL ) {
1864 curOri = TopAbs_EXTERNAL;
1865 edge.Orientation(TopAbs_FORWARD);
1869 newwire.AddEdge ( 0, edge, iumin, iumax, ivmin, ivmax );
1872 newwire.Orientation ( curOri );
1873 // ShapeFix_WireSegment seg ( newwire, ori );
1874 seqw.SetValue ( i, newwire );
1880 //=======================================================================
1881 //function : IsShortSegment
1882 //purpose : auxilary
1883 //=======================================================================
1884 // BUC60035 2053: check if wire segment is very short (in order not to skip it)
1886 // 1 - short even in 2d (to be taken always)
1887 // -1 - short in 3d but not in 2d (to be checked after algo and atteching to
1888 // another wire if alone)
1889 static Standard_Integer IsShortSegment (const ShapeFix_WireSegment &seg,
1890 const TopoDS_Face myFace,
1891 const Handle(Geom_Surface)& myGrid,
1892 const TopLoc_Location &myLoc,
1893 const Standard_Real UResolution,
1894 const Standard_Real VResolution)
1896 TopoDS_Vertex Vf = seg.FirstVertex();
1897 if ( ! Vf.IsSame ( seg.LastVertex() ) ) return Standard_False;
1899 gp_Pnt pnt = BRep_Tool::Pnt(Vf);
1900 Standard_Real tol = BRep_Tool::Tolerance(Vf);
1901 Standard_Real tol2 = tol*tol;
1903 Standard_Integer code = 1;
1904 ShapeAnalysis_Edge sae;
1905 Handle(ShapeExtend_WireData) sbwd = seg.WireData();
1906 for ( Standard_Integer i=1; i <= sbwd->NbEdges(); i++ ) {
1907 TopoDS_Edge edge = sbwd->Edge ( i );
1908 if ( ! Vf.IsSame ( sae.LastVertex ( edge ) ) ) return Standard_False;
1909 Handle(Geom2d_Curve) c2d;
1911 if ( ! sae.PCurve ( edge, myFace, c2d, f, l ) ) continue;
1914 gp_Pnt2d endPnt = c2d->Value(l);
1915 gp_Pnt2d midPnt = c2d->Value((f+l)/2);
1916 if ( ! IsCoincided ( endPnt, midPnt, UResolution, VResolution, tol ) ) code = -1;
1919 gp_Pnt midPnt3d = myGrid->Value(midPnt.X(),midPnt.Y());
1920 if ( ! myLoc.IsIdentity() ) midPnt3d.Transform ( myLoc.Transformation() );
1921 if ( midPnt3d.SquareDistance(pnt) > tol2) return 0;
1927 //=======================================================================
1928 //function : IsSamePatch
1929 //purpose : auxilary
1930 //=======================================================================
1931 static Standard_Boolean IsSamePatch (const ShapeFix_WireSegment wire,
1932 const Standard_Integer NU,
1933 const Standard_Integer NV,
1934 Standard_Integer &iumin,
1935 Standard_Integer &iumax,
1936 Standard_Integer &ivmin,
1937 Standard_Integer &ivmax,
1938 const Standard_Boolean extend=Standard_False)
1940 // get patch indices for current segment
1941 Standard_Integer jumin, jumax, jvmin, jvmax;
1942 wire.GetPatchIndex ( 1, jumin, jumax, jvmin, jvmax );
1944 // shift to the same period
1945 Standard_Integer du=0, dv=0;
1946 if ( jumin - iumin > NU ) du =-( jumin - iumin ) / NU;
1947 else if ( iumin - jumin > NU ) du = ( iumin - jumin ) / NU;
1948 if ( jvmin - ivmin > NV ) dv =-( jvmin - ivmin ) / NV;
1949 else if ( ivmin - jvmin > NV ) dv = ( ivmin - jvmin ) / NV;
1950 if ( du ) { jumin += du * NU; jumax += du * NU; }
1951 if ( dv ) { jvmin += dv * NV; jvmax += dv * NV; }
1953 // compute common (extended) indices
1954 Standard_Integer iun = Min ( iumin, jumin );
1955 Standard_Integer iux = Max ( iumax, jumax );
1956 Standard_Integer ivn = Min ( ivmin, jvmin );
1957 Standard_Integer ivx = Max ( ivmax, jvmax );
1958 Standard_Boolean ok = ( iun == iux || iun+1 == iux ) &&
1959 ( ivn == ivx || ivn+1 == ivx );
1960 if ( ok && extend ) { iumin = iun; iumax = iux; ivmin = ivn; ivmax = ivx; }
1965 //=======================================================================
1966 //function : CollectWires
1968 //=======================================================================
1970 void ShapeFix_ComposeShell::CollectWires (ShapeFix_SequenceOfWireSegment &wires,
1971 ShapeFix_SequenceOfWireSegment &seqw)
1974 ShapeAnalysis_Edge sae;
1975 Standard_Integer i; // svv #1
1976 // Collect information on short closed segments
1977 TColStd_Array1OfInteger shorts(1,seqw.Length());
1978 for ( i=1; i <= seqw.Length(); i++ ) {
1979 if(seqw(i).IsVertex() || seqw(i).Orientation() == TopAbs_INTERNAL) {
1980 wires.Append(seqw(i));
1986 for ( Standard_Integer k=1; ! myClosedMode && k <= seqw(i).NbEdges(); k++ )
1987 if ( ! seqw(i).CheckPatchIndex ( k ) ) {
1988 cout << "Warning: ShapeFix_ComposeShell::CollectWires: Wrong patch indices" << endl;
1992 Standard_Integer isshort = IsShortSegment ( seqw(i), myFace, myGrid, myLoc,
1993 myUResolution, myVResolution );
1994 shorts.SetValue ( i, isshort );
1996 ( seqw(i).Orientation() == TopAbs_EXTERNAL ||
1997 ( seqw(i).NbEdges() == 1 && //:abv 13.05.02: OCC320 - remove if degenerated
1998 BRep_Tool::Degenerated ( seqw(i).Edge(1) ) ) ) ) {
2000 cout << "Info: ShapeFix_ComposeShell::CollectWires: Short segment ignored" << endl;
2002 seqw(i).Orientation ( TopAbs_INTERNAL );
2006 Handle(ShapeExtend_WireData) sbwd;
2007 gp_Pnt2d endPnt, firstPnt;
2008 gp_Vec2d endTan, firstTan;
2009 TopoDS_Vertex firstV, endV;
2010 TopoDS_Edge firstEdge, lastEdge;
2011 Standard_Real tol = 0;
2012 Standard_Integer iumin = 0, iumax = 0, ivmin = 0, ivmax = 0;
2013 Standard_Real dsu=0., dsv=0.;
2014 Standard_Boolean canBeClosed = Standard_False;
2016 Standard_Integer index = 0;
2017 Standard_Boolean misoriented = Standard_True, samepatch = Standard_False;
2018 Standard_Boolean reverse = Standard_False, connected = Standard_False;
2019 Standard_Real angle = -M_PI, mindist = RealLast();
2020 Standard_Integer weigth = 0;
2021 Standard_Real shiftu=0., shiftv=0.;
2023 // find next segment to connect (or first if sbwd is NULL)
2024 for ( i = 1; i <= seqw.Length(); i++ ) {
2025 ShapeFix_WireSegment seg = seqw.Value(i);
2028 TopAbs_Orientation anOr = seg.Orientation();
2029 if ( anOr == TopAbs_INTERNAL ) continue;
2031 // for first segment, take any
2032 if ( sbwd.IsNull() ) {
2033 if ( shorts(i) >0 ) continue;
2034 if ( anOr == TopAbs_EXTERNAL ) continue;
2035 if ( anOr == TopAbs_FORWARD ) reverse = Standard_True;
2037 seg.GetPatchIndex ( 1, iumin, iumax, ivmin, ivmax );
2038 misoriented = Standard_False;
2043 // check whether current segment is on the same patch with previous
2044 Standard_Integer sp = ( myClosedMode || // no indexation in closed mode
2045 IsSamePatch ( seg, myGrid->NbUPatches(), myGrid->NbVPatches(),
2046 iumin, iumax, ivmin, ivmax ) );
2048 // not same patch has lowest priority
2049 if ( ! sp && ( canBeClosed || ( index && samepatch ) ) ) continue;
2051 // try to connect, with the following priorities:
2052 // The name of property Weigth:
2053 // sharing vertex auto
2056 // misorientation = 0 8
2057 // connected in 2d 4
2061 Handle(ShapeExtend_WireData) wire = seg.WireData();
2062 for ( Standard_Integer j=0; j <2; j++ ) {
2063 if ( ! endV.IsSame ( j ? seg.LastVertex() : seg.FirstVertex() ) ) continue;
2065 // check for misorientation only if nothing better is found
2066 Standard_Integer misor = ( anOr == ( j ? TopAbs_REVERSED : TopAbs_FORWARD ) );
2067 // if ( misor ) continue; // temporarily, to be improved
2069 // returning back by the same edge is lowest priority
2070 if ( lastEdge.IsSame ( wire->Edge ( j ? wire->NbEdges() : 1 ) ) ) {
2071 if ( ! index && ! canBeClosed ) { // || ( sp && ! samepatch ) ) {
2074 connected = Standard_True;
2075 misoriented = misor;
2077 weigth = ( sp ? 16 : 0 ) + ( connected ? 8 : 0 ) + (misor==0 ? 4 : 0);
2083 // compute starting tangent
2087 Standard_Real edgeTol = 0;
2088 for ( k=1; k <= wire->NbEdges(); k++ ) {
2089 TopoDS_Shape tmpE = wire->Edge(wire->NbEdges()-k+1).Reversed();
2090 TopoDS_Edge edge = ( j ? TopoDS::Edge ( tmpE ) :
2092 edgeTol = BRep_Tool::Tolerance ( edge );
2093 //if ( sae.GetEndTangent2d ( edge, myFace, Standard_False, lPnt, lVec ) ) break;
2094 if ( sae.GetEndTangent2d ( edge, myFace, Standard_False, lPnt, lVec, 1.e-3 ) ) break;
2096 if ( k > wire->NbEdges() ) myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL2 );
2098 if ( myClosedMode ) {
2100 shiftu = ShapeAnalysis::AdjustByPeriod ( lPnt.X(), endPnt.X(), myUPeriod );
2101 lPnt.SetX ( lPnt.X() + shiftu );
2104 shiftv = ShapeAnalysis::AdjustByPeriod ( lPnt.Y(), endPnt.Y(), myVPeriod );
2105 lPnt.SetY ( lPnt.Y() + shiftv );
2109 // short segment is to be taken with highest priority by angle
2110 Standard_Real ang = ( shorts(i) >0 ? M_PI : endTan.Angle ( lVec ) );
2111 if ( myClosedMode && shorts(i) <=0 && M_PI-ang < ::Precision::Angular() )
2112 ang = 0.; // abv 21 Mar 00: trj3_s1-md-214.stp #2471: avoid going back
2113 // abv 05 Feb 02: face from Parasolid: use tolerance of edges for check
2114 // for coincidence (instead of vertex tolerance) in order
2115 // this check to be in agreement with check for position of wire segments
2116 // thus avoiding bad effects on overlapping edges
2117 Standard_Real ctol = Max ( edgeTol, BRep_Tool::Tolerance(lastEdge) );
2118 Standard_Boolean conn = IsCoincided ( endPnt, lPnt, myUResolution,
2119 myVResolution, ctol );
2120 Standard_Real dist = endPnt.SquareDistance ( lPnt );
2122 // check if case is better than last found
2124 Standard_Integer w1 = ( sp ? 16 : 0 ) + ( conn ? 4 : 0 ) + (misor==0 ? 8 : 0);
2125 Standard_Integer tail1 = ( !conn && (dist < mindist) ? 2 : 0) + (ang > angle ? 1 : 0);
2126 Standard_Integer tail2 = ( !connected &&(dist > mindist) ? 2 : 0) + (ang < angle ? 1 : 0);
2127 if(w1+tail1 <= weigth+tail2)
2135 misoriented = misor;
2143 // if next segment found, connect it
2146 myInvertEdgeStatus = Standard_True;
2147 ShapeFix_WireSegment seg = seqw.Value(index);
2148 if ( sbwd.IsNull() ) sbwd = new ShapeExtend_WireData;
2149 else if ( samepatch ) { // extend patch indices
2150 IsSamePatch ( seg, myGrid->NbUPatches(), myGrid->NbVPatches(),
2151 iumin, iumax, ivmin, ivmax, Standard_True );
2153 // TopAbs_Orientation or = seg.Orientation();
2154 if ( ! reverse ) sbwd->Add ( seg.WireData() );
2156 Handle(ShapeExtend_WireData) wire = new ShapeExtend_WireData;
2157 wire->ManifoldMode() = Standard_False;
2158 wire->Add ( seg.WireData() );
2159 wire->Reverse ( myFace );
2162 if ( seg.Orientation() == TopAbs_EXTERNAL )
2163 seg.Orientation ( reverse ? TopAbs_REVERSED : TopAbs_FORWARD );
2164 else seg.Orientation ( TopAbs_INTERNAL );
2165 seqw.SetValue ( index, seg );
2167 else if ( sbwd.IsNull() ) break; // stop when no free segments available
2168 // for first segment, remember start point
2169 if ( endV.IsNull() ) {
2170 firstEdge = sbwd->Edge(1);
2171 firstV = sae.FirstVertex ( firstEdge );
2172 //sae.GetEndTangent2d ( firstEdge, myFace, Standard_False, firstPnt, firstTan );
2173 sae.GetEndTangent2d ( firstEdge, myFace, Standard_False, firstPnt, firstTan, 1.e-3 );
2176 // update last edge and vertex (only for not short segments)
2177 Standard_Boolean doupdate = ( index && ( shorts(index) <=0 || endV.IsNull() ) );
2179 lastEdge = sbwd->Edge ( sbwd->NbEdges() );
2180 endV = sae.LastVertex ( lastEdge );
2181 tol = BRep_Tool::Tolerance ( endV );
2182 // BUC60035 2053: iteration on edges is required
2183 Standard_Integer k; // svv #1
2184 for ( k=sbwd->NbEdges(); k >=1; k-- )
2185 //if ( sae.GetEndTangent2d ( sbwd->Edge ( k ), myFace, Standard_True, endPnt, endTan ) )
2186 if ( sae.GetEndTangent2d ( sbwd->Edge ( k ), myFace, Standard_True, endPnt, endTan, 1.e-3 ) )
2188 if ( k <1 ) myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL2 );
2189 if ( myUClosed ) endPnt.SetX ( endPnt.X() + dsu );
2190 if ( myVClosed ) endPnt.SetY ( endPnt.Y() + dsv );
2193 // if closed or no next segment found, add to wires
2194 canBeClosed = endV.IsSame ( firstV );
2195 if ( ! index || ( canBeClosed &&
2196 ! lastEdge.IsSame ( firstEdge ) && // cylinder (seam)
2197 IsCoincided ( endPnt, firstPnt, myUResolution, myVResolution, 2.* tol ) ) ) {
2198 if ( ! endV.IsSame ( sae.FirstVertex ( firstEdge ) ) ) {
2199 myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL5 );
2201 cout << "Warning: ShapeFix_ComposeShell::CollectWires: can't close wire" << endl;
2204 ShapeFix_WireSegment s ( sbwd, TopAbs_FORWARD );
2205 s.DefineIUMin(1,iumin);
2206 s.DefineIUMax(1,iumax);
2207 s.DefineIVMin(1,ivmin);
2208 s.DefineIVMax(1,ivmax);
2212 canBeClosed = Standard_False;
2216 // Check if some wires are short in 3d (lie entirely inside one vertex),
2217 // and if yes try to merge them with others
2218 //pdn The short seqments are stil plased in "in" sequence.
2220 for ( i=1; i <= seqw.Length(); i++ ) {
2221 if ( shorts(i) != 1 || seqw(i).IsVertex() || seqw(i).Orientation() == TopAbs_INTERNAL ||
2222 seqw(i).Orientation() == TopAbs_EXTERNAL ) continue;
2224 // find any other wire containing the same vertex
2225 Handle(ShapeExtend_WireData) wd = seqw(i).WireData();
2226 TopoDS_Vertex V = seqw(i).FirstVertex();
2227 Standard_Integer minj=0, mink=0;
2230 Standard_Real mindist=0;
2231 Standard_Boolean samepatch = Standard_False;
2232 // Standard_Integer iumin, iumax, ivmin, ivmax;
2233 seqw(i).GetPatchIndex ( 1, iumin, iumax, ivmin, ivmax );
2234 sae.GetEndTangent2d ( wd->Edge(1), myFace, Standard_False, p2d, vec );
2235 for ( Standard_Integer j=1; j <= wires.Length(); j++ ) {
2236 // if ( j == i ) continue;
2237 // Handle(ShapeExtend_WireData)
2238 sbwd = wires(j).WireData();
2239 for ( Standard_Integer k=1; k <= sbwd->NbEdges(); k++ ) {
2240 if ( !V.IsSame ( sae.FirstVertex ( sbwd->Edge(k) ) ) ) continue; //pdn I suppose that short segment should be inserted into the SAME vertex.
2242 Standard_Integer sp = IsSamePatch ( wires(j), myGrid->NbUPatches(), myGrid->NbVPatches(),
2243 iumin, iumax, ivmin, ivmax );
2244 if ( samepatch && !sp) continue;
2246 sae.GetEndTangent2d ( sbwd->Edge(k), myFace, Standard_False, pp, vec );
2247 Standard_Real dist = pp.SquareDistance ( p2d );
2248 if ( sp && ! samepatch ) { minj = j; mink = k; mindist = dist;samepatch=sp;}
2250 if ( ! minj || mindist > dist ) { minj = j; mink = k; mindist = dist;samepatch=sp; }
2254 //pdn add into resulting sequence!
2255 ShapeFix_WireSegment s ( wd, TopAbs_FORWARD );
2258 cout <<"Warning: Short segment processed as separate wire"<<endl;
2263 // and if found, merge
2264 // Handle(ShapeExtend_WireData)
2265 sbwd = wires(minj).WireData();
2266 for ( Standard_Integer n=1; n <= wd->NbEdges(); n++ )
2267 sbwd->Add ( wd->Edge(n), mink++ );
2269 // wires.Remove ( i );
2275 //=======================================================================
2276 //function : DispatchWires
2278 //=======================================================================
2280 static gp_Pnt2d GetMiddlePoint (const ShapeFix_WireSegment wire,
2281 const TopoDS_Face face)
2283 if(wire.IsVertex()) {
2284 TopoDS_Vertex aV = wire.GetVertex();
2285 gp_Pnt aP3D = BRep_Tool::Pnt(aV );
2286 Handle(Geom_Surface) surf = BRep_Tool::Surface(face);
2287 Handle(ShapeAnalysis_Surface) aSurfTool = new ShapeAnalysis_Surface(surf);
2288 return aSurfTool->ValueOfUV(aP3D,Precision::Confusion());
2291 ShapeAnalysis_Edge sae;
2292 ShapeAnalysis_Curve sac;
2293 Handle(ShapeExtend_WireData) wd = wire.WireData();
2294 for(Standard_Integer i = 1; i <= wd->NbEdges(); i++) {
2295 TopoDS_Edge E = wd->Edge (i);
2296 Standard_Real cf,cl;
2297 Handle(Geom2d_Curve) c2d;
2298 if(sae.PCurve (E,face,c2d,cf,cl,Standard_False)) {
2299 sac.FillBndBox ( c2d, cf, cl, 3, Standard_False, box );
2300 // box.Add(c2d->Value(cf));
2301 // box.Add(c2d->Value(cl));
2302 // box.Add(c2d->Value((cl+cf)/2.));
2305 if ( box.IsVoid() ) return gp_Pnt2d(0.,0.);
2306 Standard_Real aXmin, aYmin, aXmax, aYmax;
2307 box.Get(aXmin, aYmin, aXmax, aYmax);
2308 return gp_Pnt2d ( 0.5 * ( aXmax + aXmin ), 0.5 * ( aYmax + aYmin ) );
2311 //=======================================================================
2312 //function : MakeFacesOnPatch
2314 //=======================================================================
2316 void ShapeFix_ComposeShell::MakeFacesOnPatch (TopTools_SequenceOfShape &faces,
2317 const Handle(Geom_Surface)& surf,
2318 TopTools_SequenceOfShape &loops) const
2322 // Case of single loop: just add it to face
2323 if ( loops.Length() == 1 ) {
2324 TopoDS_Face newFace;
2325 B.MakeFace ( newFace, surf, myLoc, ::Precision::Confusion() );
2326 TopoDS_Shape aSH = loops.Value(1);
2327 if( aSH.ShapeType() != TopAbs_WIRE)
2329 TopoDS_Wire wire = TopoDS::Wire ( loops.Value(1) );
2331 B.Add ( newFace, wire );
2332 if(myInvertEdgeStatus) {
2333 Handle(ShapeFix_Face) sff = new ShapeFix_Face(newFace);
2334 sff->FixAddNaturalBoundMode() = Standard_False;
2335 TopTools_DataMapOfShapeListOfShape MapWires;
2337 sff->FixOrientation(MapWires);
2338 newFace = sff->Face();
2341 faces.Append ( newFace );
2345 // For several loops, first find roots
2346 // make pseudo-face,
2348 B.MakeFace ( pf, surf, myLoc, ::Precision::Confusion() );
2349 Handle(Geom_Surface) atSurf = BRep_Tool::Surface(pf);
2351 Handle(ShapeAnalysis_Surface) aSurfTool = new ShapeAnalysis_Surface(atSurf);
2352 TopTools_SequenceOfShape roots;
2353 Standard_Integer i; // svv #1
2354 for ( i = 1; i <= loops.Length(); i++ ) {
2357 TopoDS_Shape aShape = loops(i);
2358 if(aShape.ShapeType() != TopAbs_WIRE ||
2359 (aShape.Orientation() != TopAbs_FORWARD && aShape.Orientation() != TopAbs_REVERSED))
2362 wr = TopoDS::Wire ( loops(i) );
2363 TopoDS_Iterator ew (wr);
2364 if ( ! ew.More() ) continue;
2366 TopoDS_Edge ed = TopoDS::Edge ( ew.Value() );
2367 while(ed.Orientation() != TopAbs_FORWARD &&
2368 ed.Orientation() != TopAbs_REVERSED ) {
2371 ed = TopoDS::Edge ( ew.Value() );
2375 if ( ! ew.More() ) continue;
2376 Standard_Real cf, cl;
2377 Handle(Geom2d_Curve) cw = BRep_Tool::CurveOnSurface ( ed, pf, cf, cl );
2378 if ( cw.IsNull() ) continue;
2379 unp = cw->Value ( 0.5 * ( cf + cl ) );
2381 Standard_Integer j; // svv #1
2382 for ( j = 1; j <= loops.Length(); j++ ) {
2383 if ( i == j ) continue;
2384 TopoDS_Shape aShape2 = loops(j);
2385 if(aShape2.ShapeType() != TopAbs_WIRE ||
2386 (aShape2.Orientation() != TopAbs_FORWARD &&
2387 aShape2.Orientation() != TopAbs_REVERSED))
2389 TopoDS_Wire w1 = TopoDS::Wire (aShape2);
2392 awtmp.Orientation(TopAbs_FORWARD);
2393 TopoDS_Iterator aIt(w1);
2394 Standard_Integer nbe =0;
2395 for( ; aIt.More() ; aIt.Next()) {
2396 if(aIt.Value().Orientation() == TopAbs_FORWARD ||
2397 aIt.Value().Orientation() == TopAbs_REVERSED) {
2398 B.Add(awtmp,aIt.Value());
2406 B.MakeFace ( fc, surf, myLoc, ::Precision::Confusion() );
2407 B.Add ( fc, awtmp );
2408 BRepTopAdaptor_FClass2d clas ( fc, ::Precision::PConfusion() );
2409 TopAbs_State stPoint = clas.Perform (unp,Standard_False);
2410 if(stPoint == TopAbs_ON || stPoint == TopAbs_UNKNOWN) {
2412 TopoDS_Edge ed = TopoDS::Edge ( ew.Value() );
2413 Standard_Real cf, cl;
2414 Handle(Geom2d_Curve) cw = BRep_Tool::CurveOnSurface ( ed, pf, cf, cl );
2415 // handle tangential case (ON)
2416 while ( stPoint == TopAbs_ON || stPoint == TopAbs_UNKNOWN ) {
2417 stPoint = clas.Perform ( cw->Value(cl), Standard_False );
2418 if ( ! ew.More() ) break;
2420 if ( ! ew.More() ) break;
2421 TopoDS_Edge edge = TopoDS::Edge ( ew.Value() );
2422 if(edge.Orientation() !=TopAbs_FORWARD &&
2423 edge.Orientation() !=TopAbs_REVERSED)
2425 Handle(Geom2d_Curve) c2d = BRep_Tool::CurveOnSurface ( edge, pf, cf, cl );
2426 if ( ! c2d.IsNull() ) cw = c2d;
2429 TopAbs_State stInfin = clas.PerformInfinitePoint();
2430 if ( stPoint != stInfin ) break;
2432 if ( j > loops.Length()) {
2433 roots.Append ( wr );
2434 // loops.Remove ( i-- );
2438 // And remove them from the list of loops
2439 for ( i = 1; i <= loops.Length(); i++ )
2440 for ( Standard_Integer j = 1; j <= roots.Length(); j++ )
2441 if ( loops(i).IsSame ( roots(j) ) ) { loops.Remove(i--); break; }
2443 // check for lost wires, and if they are, make them roots
2444 if ( roots.Length() <=0 && loops.Length() >0 ) {
2446 cout << "Error: ShapeFix_ComposeShell::MakeFacesOnPatch: can't dispatch wires" << endl;
2448 for ( Standard_Integer j=1; j <= loops.Length(); j++ ) {
2449 roots.Append ( loops(j) );
2454 // Then iterate on loops
2455 for ( i=1; i <= roots.Length(); i++ ) {
2456 Standard_Boolean reverse = Standard_False;
2457 TopoDS_Wire wire = TopoDS::Wire ( roots(i) );
2459 B.MakeFace ( fc, surf, myLoc, ::Precision::Confusion() );
2461 BRepTopAdaptor_FClass2d clas ( fc, ::Precision::PConfusion() );
2462 if ( clas.PerformInfinitePoint() == TopAbs_IN ) {
2463 reverse = Standard_True;
2465 cout << "Warning: ShapeFix_ComposeShell::MakeFacesOnPatch: badly oriented wire" << endl;
2469 // find all holes for that loop
2470 TopTools_SequenceOfShape holes; // holes in holes not supported
2471 Standard_Integer j; // svv #1
2472 for ( j=1; j <= loops.Length(); j++ ) {
2474 if(loops(j).ShapeType() == TopAbs_WIRE) {
2475 TopoDS_Wire bw = TopoDS::Wire ( loops(j) );
2476 TopoDS_Iterator ew ( bw );
2477 if ( ! ew.More() ) continue;
2478 TopoDS_Edge ed = TopoDS::Edge ( ew.Value() );
2479 Standard_Real cf, cl;
2480 Handle(Geom2d_Curve) cw = BRep_Tool::CurveOnSurface ( ed, pf, cf, cl );
2481 if ( cw.IsNull() ) continue;
2482 unp = cw->Value ( 0.5 * ( cf + cl ) );
2484 else if(loops(j).ShapeType() == TopAbs_VERTEX) {
2485 TopoDS_Vertex aV = TopoDS::Vertex(loops(j));
2486 gp_Pnt aP = BRep_Tool::Pnt(aV);
2487 unp = aSurfTool->ValueOfUV(aP,Precision::Confusion());
2491 TopAbs_State state = clas.Perform (unp,Standard_False);
2492 if ( (Standard_Boolean) ( state == TopAbs_OUT ) == reverse ) {
2493 holes.Append ( loops(j) );
2494 loops.Remove ( j-- );
2498 // and add them to new face (no orienting is done)
2499 TopoDS_Face newFace;
2500 B.MakeFace ( newFace, surf, myLoc, ::Precision::Confusion() );
2501 B.Add ( newFace, wire );
2502 for ( j=1; j <= holes.Length(); j++ ) {
2503 TopoDS_Shape aSh = holes(j);
2504 if(aSh.ShapeType() == TopAbs_VERTEX) {
2505 TopoDS_Vertex aNewV = ShapeAnalysis_TransferParametersProj::CopyNMVertex(TopoDS::Vertex(aSh), newFace,myFace);
2506 Context()->Replace(aSh,aNewV);
2507 B.Add ( newFace,aNewV);
2510 B.Add ( newFace, holes(j) );
2512 faces.Append ( newFace );
2514 // check for lost wires, and if they are, make them roots
2515 if ( i == roots.Length() && loops.Length() >0 ) {
2517 cout << "Error: ShapeFix_ComposeShell::MakeFacesOnPatch: can't dispatch wires" << endl;
2519 for ( j=1; j <= loops.Length(); j++ ) {
2520 TopoDS_Shape aSh = loops(j);
2521 if(aSh.ShapeType() == TopAbs_WIRE && (aSh.Orientation() == TopAbs_FORWARD ||
2522 aSh.Orientation() == TopAbs_REVERSED))
2523 roots.Append ( loops(j) );
2530 //=======================================================================
2531 //function : DispatchWires
2533 //=======================================================================
2535 void ShapeFix_ComposeShell::DispatchWires (TopTools_SequenceOfShape &faces,
2536 ShapeFix_SequenceOfWireSegment& wires) const
2540 // in closed mode, apply FixShifted to all wires before dispatching them
2541 if ( myClosedMode ) {
2543 sfw.SetFace ( myFace );
2544 sfw.SetPrecision ( Precision() );
2546 // pdn: shift pcurves in the seam to make OK shape w/o fixshifted
2548 for ( i=1; i <= wires.Length(); i++ ) {
2549 if(wires(i).IsVertex())
2551 Handle(ShapeExtend_WireData) sbwd = wires(i).WireData();
2553 for(Standard_Integer jL=1; jL <= sbwd->NbEdges(); jL++ ) {
2554 TopoDS_Edge E = sbwd->Edge(jL);
2555 if ( E.Orientation() == TopAbs_REVERSED && BRep_Tool::IsClosed(E,myFace) ) {
2556 Standard_Real f1,l1, f2, l2;
2557 Handle(Geom2d_Curve) c21 = BRep_Tool::CurveOnSurface(E,myFace,f1,l1);
2558 TopoDS_Shape dummy = E.Reversed();
2559 Handle(Geom2d_Curve) c22 = BRep_Tool::CurveOnSurface(TopoDS::Edge(dummy),myFace,f2,l2);
2560 Standard_Real dPreci = ::Precision::PConfusion()*Precision::PConfusion();
2561 gp_Pnt2d pf1 = c21->Value(f1);
2562 gp_Pnt2d pl1 = c21->Value(l1);
2563 gp_Pnt2d pf2 = c22->Value(f2);
2564 gp_Pnt2d pl2 = c22->Value(l2);
2565 if ( c21 == c22 || pf1.SquareDistance(pf2) < dPreci ||
2566 pl1.SquareDistance(pl2) < dPreci ) {
2567 gp_Vec2d shift(0.,0.);
2568 if ( myUClosed && Abs ( pf2.X() - pl2.X() ) < ::Precision::PConfusion() )
2569 shift.SetX(myUPeriod);
2570 if ( myVClosed && Abs ( pf2.Y() - pl2.Y() ) < ::Precision::PConfusion() )
2571 shift.SetY(myVPeriod);
2572 c22->Translate(shift);
2578 for ( i=1; i <= wires.Length(); i++ ) {
2579 if(wires(i).IsVertex())
2581 Handle(ShapeExtend_WireData) sbwd = wires(i).WireData();
2583 //: abv 30.08.01: torHalf2.sat: if wire contains single degenerated
2584 // edge, skip that wire
2585 if ( sbwd->NbEdges() <=0 ||
2586 ( sbwd->NbEdges() ==1 && BRep_Tool::Degenerated(sbwd->Edge(1)) ) ) {
2594 // force recomputation of degenerated edges (clear pcurves)
2595 ShapeBuild_Edge sbe;
2596 for (Standard_Integer jL=1; jL <= sbwd->NbEdges(); jL++ ) {
2597 if ( BRep_Tool::Degenerated(sbwd->Edge(jL)) )
2598 sbe.RemovePCurve(sbwd->Edge(jL),myFace);
2599 // sfw.FixDegenerated(jL);
2601 sfw.FixDegenerated();
2605 // Compute center points for wires
2606 TColgp_SequenceOfPnt2d mPnts;
2607 Standard_Integer nb = wires.Length();
2609 // pdn protection on empty sequence
2613 Standard_Integer i; //svv #1
2614 for ( i = 1; i <= nb; i++ )
2615 mPnts.Append ( GetMiddlePoint ( wires(i), myFace ) );
2617 // Put each wire on its own surface patch (by reassigning pcurves)
2618 // and build 3d curve if necessary
2619 ShapeBuild_ReShape rs;
2620 ShapeBuild_Edge sbe;
2621 ShapeAnalysis_Edge sae;
2622 Handle(ShapeFix_Edge) sfe = new ShapeFix_Edge;
2624 Standard_Real U1,U2,V1,V2;
2625 myGrid->Bounds(U1,U2,V1,V2);
2626 for ( i = 1; i <= nb; i++ ) {
2628 gp_Pnt2d pnt = mPnts(i);
2629 Standard_Real ush =0., vsh=0.;
2631 ush = ShapeAnalysis::AdjustToPeriod(pnt.X(),U1,U2);
2632 pnt.SetX(pnt.X()+ush);
2635 vsh = ShapeAnalysis::AdjustToPeriod(pnt.Y(),V1,V2);
2636 pnt.SetY(pnt.Y()+vsh);
2639 Standard_Integer indU = myGrid->LocateUParameter ( pnt.X() );
2640 Standard_Integer indV = myGrid->LocateVParameter ( pnt.Y() );
2642 // compute parametric transformation
2644 Standard_Real uFact=1.;
2645 Standard_Boolean needT = myGrid->GlobalToLocalTransformation ( indU, indV, uFact, T );
2646 if ( ush != 0. || vsh != 0. ) {
2648 Sh.SetTranslation ( gp_Vec2d ( ush, vsh ) );
2650 needT = Standard_True;
2652 if(wires(i).IsVertex())
2654 Handle(Geom_Surface) surf = myGrid->Patch ( indU, indV );
2656 B.MakeFace ( face, surf, myLoc, ::Precision::Confusion() );
2657 Handle(ShapeExtend_WireData) sewd = wires(i).WireData();
2658 for ( Standard_Integer j = 1; j <= sewd->NbEdges(); j++ ) {
2659 // Standard_Integer nsplit = ApplyContext ( sewd, j, context );
2660 // if ( nsplit <1 ) { j--; continue; }
2662 TopoDS_Edge edge = sewd->Edge(j);
2664 // !! Accurately copy pcurves for SEAMS and SEAM-like edges !!
2666 // if edge is already copied, don`t copy any more
2667 TopoDS_Edge newEdge;
2668 TopoDS_Edge anInitEdge = edge;
2669 Standard_Boolean ismanifold = (edge.Orientation() == TopAbs_FORWARD ||
2670 edge.Orientation() == TopAbs_REVERSED);
2671 if ( rs.IsRecorded ( edge ) ) {
2673 TopoDS_Shape tmpNE = rs.Value(edge);
2674 newEdge = TopoDS::Edge ( tmpNE );
2679 anInitEdge.Orientation(TopAbs_FORWARD);
2681 newEdge = sbe.Copy ( anInitEdge, Standard_False );
2683 newEdge.Orientation(edge.Orientation());
2684 rs.Replace ( edge, newEdge );
2685 Context()->Replace ( edge, newEdge );
2688 sbe.ReassignPCurve ( newEdge, myFace, face );
2690 // transform pcurve to parametric space of patch
2693 Handle(Geom2d_Curve) c2d;
2694 if ( sae.PCurve ( newEdge, face, c2d, f, l, Standard_False ) ) {
2695 Standard_Real newf = f, newl = l;
2696 Handle(Geom2d_Curve) c2dnew = sbe.TransformPCurve ( c2d, T, uFact, newf, newl );
2697 if ( BRep_Tool::IsClosed ( newEdge, face ) ) {
2698 Standard_Real cf, cl;
2699 Handle(Geom2d_Curve) c2d2;
2701 TopoDS_Shape tmpE = newEdge.Reversed();
2702 TopoDS_Edge e2 = TopoDS::Edge (tmpE );
2703 if ( sae.PCurve ( e2, face, c2d2, cf, cl, Standard_False ) ) {
2704 if ( newEdge.Orientation() == TopAbs_FORWARD )
2705 B.UpdateEdge ( newEdge, c2dnew, c2d2, face, 0. );
2706 else B.UpdateEdge ( newEdge, c2d2, c2dnew, face, 0. );
2708 else B.UpdateEdge ( newEdge, c2dnew, face, 0. );
2710 else B.UpdateEdge ( newEdge, c2dnew, face, 0. );
2711 B.Range ( newEdge, face, newf, newl );
2712 if ( (newf != f || newl != l) && !BRep_Tool::Degenerated(newEdge) )
2713 B.SameRange ( newEdge, Standard_False );
2717 if(!BRep_Tool::SameRange(newEdge)) {
2720 TopoDS_Edge afe = TopoDS::Edge(newEdge.Oriented(TopAbs_FORWARD));
2721 etmp = sbe.Copy (afe , Standard_False );
2724 etmp = sbe.Copy ( newEdge, Standard_False );
2725 sfe->FixAddCurve3d ( etmp );
2726 Standard_Real cf, cl;
2727 Handle(Geom_Curve) c3d;
2728 if(sae.Curve3d(etmp,c3d,cf,cl,Standard_False)) {
2729 B.UpdateEdge ( newEdge, c3d, 0. );
2730 sbe.SetRange3d ( newEdge, cf, cl );
2734 sfe->FixAddCurve3d ( newEdge );
2735 sewd->Set ( newEdge, j );
2739 // Collect wires in packets lying on same surface and dispatch them
2740 TColStd_Array1OfBoolean used ( 1, nb );
2741 used.Init ( Standard_False );
2743 TopTools_SequenceOfShape loops;
2745 Handle(Geom_Surface) Surf;
2746 for ( i = 1; i <= nb; i++ ) {
2747 if ( used(i) ) continue;
2748 Handle(Geom_Surface) S = myGrid->Patch ( mPnts(i) );
2749 if ( Surf.IsNull() ) Surf = S;
2750 else if ( S != Surf ) continue;
2751 used(i) = Standard_True;
2752 ShapeFix_WireSegment aSeg = wires(i);
2753 if(aSeg.IsVertex()) {
2754 TopoDS_Vertex aVert = aSeg.GetVertex();
2755 if(aVert.Orientation() == TopAbs_INTERNAL)
2756 loops.Append(wires(i).GetVertex());
2759 Handle(ShapeExtend_WireData) aWD = aSeg.WireData();
2761 loops.Append ( aWD->Wire() );
2764 if ( Surf.IsNull() ) break;
2766 MakeFacesOnPatch ( faces, Surf, loops );
2770 //======================================================================
2771 //function : SetTransferParamTool
2773 //=======================================================================
2775 void ShapeFix_ComposeShell::SetTransferParamTool(const Handle(ShapeAnalysis_TransferParameters)& TransferParam)
2777 myTransferParamTool = TransferParam;
2780 //=======================================================================
2781 //function : GetTransferParamTool
2783 //=======================================================================
2785 Handle(ShapeAnalysis_TransferParameters) ShapeFix_ComposeShell::GetTransferParamTool() const
2787 return myTransferParamTool;
2790 //=======================================================================
2791 //function : ClosedMode
2793 //=======================================================================
2795 Standard_Boolean &ShapeFix_ComposeShell::ClosedMode()
2797 return myClosedMode;