0030581: Modeling Data - Standard_OutOfRange within Geom_BSplineSurface::LocateV()
[occt.git] / src / BRepFill / BRepFill_OffsetWire.cxx
CommitLineData
b311480e 1// Created on: 1995-04-20
2// Created by: Bruno DUMORTIER
3// Copyright (c) 1995-1999 Matra Datavision
973c2be1 4// Copyright (c) 1999-2014 OPEN CASCADE SAS
b311480e 5//
973c2be1 6// This file is part of Open CASCADE Technology software library.
b311480e 7//
d5f74e42 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
973c2be1 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.
b311480e 13//
973c2be1 14// Alternatively, this file may be used under the terms of Open CASCADE
15// commercial license or contractual agreement.
7fd59977 16
17// Modified by skv - Fri Jul 8 11:21:38 2005 OCC9145
18
42cf5bc1 19#include <Adaptor3d_Curve.hxx>
5201d3e6 20#include <Adaptor2d_OffsetCurve.hxx>
42cf5bc1 21#include <Bisector_Bisec.hxx>
22#include <BRep_Builder.hxx>
23#include <BRep_CurveRepresentation.hxx>
24#include <BRep_GCurve.hxx>
25#include <BRep_TEdge.hxx>
26#include <BRep_Tool.hxx>
27#include <BRep_TVertex.hxx>
7fd59977 28#include <BRepAdaptor_Curve.hxx>
29#include <BRepAdaptor_Surface.hxx>
7fd59977 30#include <BRepFill_DataMapOfNodeShape.hxx>
42cf5bc1 31#include <BRepFill_DataMapOfOrientedShapeListOfShape.hxx>
7fd59977 32#include <BRepFill_DataMapOfShapeSequenceOfPnt.hxx>
42cf5bc1 33#include <BRepFill_DataMapOfShapeSequenceOfReal.hxx>
34#include <BRepFill_OffsetWire.hxx>
7fd59977 35#include <BRepFill_TrimEdgeTool.hxx>
36#include <BRepLib.hxx>
42cf5bc1 37#include <BRepLib_MakeEdge.hxx>
7fd59977 38#include <BRepLib_MakeFace.hxx>
42cf5bc1 39#include <BRepLib_MakeVertex.hxx>
7fd59977 40#include <BRepLib_MakeWire.hxx>
42cf5bc1 41#include <BRepMAT2d_BisectingLocus.hxx>
42#include <BRepMAT2d_Explorer.hxx>
43#include <BRepMAT2d_LinkTopoBilo.hxx>
7fd59977 44#include <BRepTools.hxx>
42cf5bc1 45#include <BRepTools_Substitution.hxx>
7fd59977 46#include <BRepTools_WireExplorer.hxx>
42cf5bc1 47#include <Geom2d_BSplineCurve.hxx>
7fd59977 48#include <Geom2d_Circle.hxx>
42cf5bc1 49#include <Geom2d_Curve.hxx>
7fd59977 50#include <Geom2d_Line.hxx>
7fd59977 51#include <Geom2d_OffsetCurve.hxx>
42cf5bc1 52#include <Geom2d_TrimmedCurve.hxx>
53#include <Geom2dAdaptor_Curve.hxx>
54#include <Geom2dAdaptor_HCurve.hxx>
55#include <Geom2dConvert_CompCurveToBSplineCurve.hxx>
56#include <Geom2dLProp_CLProps2d.hxx>
7fd59977 57#include <Geom_Circle.hxx>
6a442250 58#include <Geom_Line.hxx>
7fd59977 59#include <Geom_OffsetCurve.hxx>
42cf5bc1 60#include <Geom_Plane.hxx>
61#include <Geom_Surface.hxx>
62#include <Geom_TrimmedCurve.hxx>
63#include <GeomAPI.hxx>
64#include <gp.hxx>
65#include <gp_Ax2.hxx>
66#include <gp_Dir2d.hxx>
67#include <gp_Pln.hxx>
68#include <gp_Vec.hxx>
69#include <MAT2d_CutCurve.hxx>
7fd59977 70#include <MAT_Arc.hxx>
7fd59977 71#include <MAT_Graph.hxx>
42cf5bc1 72#include <MAT_Node.hxx>
7fd59977 73#include <Precision.hxx>
42cf5bc1 74#include <Standard_ConstructionError.hxx>
75#include <Standard_ErrorHandler.hxx>
76#include <Standard_NoSuchObject.hxx>
7fd59977 77#include <Standard_NotImplemented.hxx>
42cf5bc1 78#include <TColgp_Array1OfPnt2d.hxx>
7fd59977 79#include <TColgp_SequenceOfPnt.hxx>
42cf5bc1 80#include <TColStd_Array1OfInteger.hxx>
81#include <TColStd_Array1OfReal.hxx>
82#include <TColStd_SequenceOfReal.hxx>
83#include <TopAbs.hxx>
7fd59977 84#include <TopExp.hxx>
85#include <TopExp_Explorer.hxx>
86#include <TopoDS.hxx>
7fd59977 87#include <TopoDS_Compound.hxx>
42cf5bc1 88#include <TopoDS_Face.hxx>
7fd59977 89#include <TopoDS_Iterator.hxx>
42cf5bc1 90#include <TopoDS_Shape.hxx>
91#include <TopoDS_Wire.hxx>
7fd59977 92#include <TopTools_DataMapIteratorOfDataMapOfShapeListOfShape.hxx>
93#include <TopTools_DataMapIteratorOfDataMapOfShapeShape.hxx>
42cf5bc1 94#include <TopTools_DataMapOfShapeListOfShape.hxx>
7fd59977 95#include <TopTools_DataMapOfShapeSequenceOfShape.hxx>
42cf5bc1 96#include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
7fd59977 97#include <TopTools_IndexedMapOfShape.hxx>
42cf5bc1 98#include <TopTools_ListIteratorOfListOfShape.hxx>
99#include <TopTools_ListOfShape.hxx>
100#include <TopTools_MapIteratorOfMapOfShape.hxx>
101#include <TopTools_MapOfShape.hxx>
102#include <TopTools_SequenceOfShape.hxx>
103
104#include <stdio.h>
873c119f 105#ifdef OCCT_DEBUG
106//#define DRAW
7fd59977 107#ifdef DRAW
108#include <Draw.hxx>
109#include <DrawTrSurf.hxx>
110#include <DrawTrSurf_Curve2d.hxx>
111#include <DBRep.hxx>
ec357c5c 112#include <Geom_Curve.hxx>
7fd59977 113static Standard_Boolean AffichGeom = Standard_False;
114static Standard_Boolean Affich2d = Standard_False;
115static Standard_Boolean AffichEdge = Standard_False;
116static Standard_Integer NbTRIMEDGES = 0;
117static Standard_Integer NbOFFSET = 0;
118static Standard_Integer NbEDGES = 0;
119static Standard_Integer NbBISSEC = 0;
7fd59977 120#endif
873c119f 121#endif
7fd59977 122
123// Modified by Sergey KHROMOV - Thu Nov 16 17:24:39 2000 Begin
124
125static void QuasiFleche(const Adaptor3d_Curve& C,
873c119f 126 const Standard_Real Deflection2,
127 const Standard_Real Udeb,
128 const gp_Pnt& Pdeb,
129 const gp_Vec& Vdeb,
130 const Standard_Real Ufin,
131 const gp_Pnt& Pfin,
132 const gp_Vec& Vfin,
133 const Standard_Integer Nbmin,
134 const Standard_Real Eps,
135 TColStd_SequenceOfReal& Parameters,
136 TColgp_SequenceOfPnt& Points);
7fd59977 137
138static Standard_Boolean PerformCurve (TColStd_SequenceOfReal& Parameters,
873c119f 139 TColgp_SequenceOfPnt& Points,
140 const Adaptor3d_Curve& C,
141 const Standard_Real Deflection,
142 const Standard_Real U1,
143 const Standard_Real U2,
144 const Standard_Real EPSILON,
145 const Standard_Integer Nbmin);
7fd59977 146
147static void CheckBadEdges(const TopoDS_Face& Spine, const Standard_Real Offset,
873c119f 148 const BRepMAT2d_BisectingLocus& Locus,
149 const BRepMAT2d_LinkTopoBilo& Link,
150 TopTools_ListOfShape& BadEdges);
7fd59977 151
152static Standard_Integer CutEdge (const TopoDS_Edge& E,
873c119f 153 const TopoDS_Face& F,
154 Standard_Integer ForceCut,
155 TopTools_ListOfShape& Cuts);
7fd59977 156
157
158static void CutCurve (const Handle(Geom2d_TrimmedCurve)& C,
873c119f 159 const Standard_Integer nbParts,
160 TColGeom2d_SequenceOfCurve& theCurves);
7fd59977 161// Modified by Sergey KHROMOV - Thu Nov 16 17:24:47 2000 End
162
163
164static void EdgeVertices (const TopoDS_Edge& E,
873c119f 165 TopoDS_Vertex& V1,
166 TopoDS_Vertex& V2)
7fd59977 167{
168 if (E.Orientation() == TopAbs_REVERSED) {
169 TopExp::Vertices(E,V2,V1);
170 }
171 else {
172 TopExp::Vertices(E,V1,V2);
173 }
174}
7fd59977 175static Standard_Boolean VertexFromNode
873c119f 176 (const Handle(MAT_Node)& aNode,
177 const Standard_Real Offset,
178 gp_Pnt2d& PN,
179 BRepFill_DataMapOfNodeShape& MapNodeVertex,
180 TopoDS_Vertex& VN);
7fd59977 181
182static void StoreInMap (const TopoDS_Shape& V1,
873c119f 183 const TopoDS_Shape& V2,
184 TopTools_IndexedDataMapOfShapeShape& MapVV);
7fd59977 185
186static void TrimEdge (const TopoDS_Edge& CurrentEdge,
e11c1bc4 187 const TopoDS_Shape& CurrentSpine,
188 const TopoDS_Face& AllSpine,
873c119f 189 const TopTools_ListOfShape& D,
6a442250 190 TopTools_SequenceOfShape& Sv,
191 TColStd_SequenceOfReal& MapverPar,
192 TopTools_SequenceOfShape& S,
193 TopTools_IndexedDataMapOfShapeShape& MapVV,
194 const Standard_Integer IndOfE);
7fd59977 195
e11c1bc4 196static Standard_Boolean IsInnerEdge(const TopoDS_Shape& ProE,
197 const TopoDS_Face& AllSpine,
198 Standard_Real& TrPar1,
199 Standard_Real& TrPar2);
200
7fd59977 201static Standard_Boolean DoubleOrNotInside (const TopTools_ListOfShape& EC,
873c119f 202 const TopoDS_Vertex& V);
7fd59977 203
204static Standard_Boolean IsSmallClosedEdge(const TopoDS_Edge& anEdge,
873c119f 205 const TopoDS_Vertex& aVertex);
7fd59977 206
207static void MakeCircle
873c119f 208 (const TopoDS_Edge& E,
209 const TopoDS_Vertex& V,
210 const TopoDS_Face& F,
211 const Standard_Real Offset,
212 BRepFill_IndexedDataMapOfOrientedShapeListOfShape& Map,
213 const Handle(Geom_Plane)& RefPlane);
7fd59977 214
215static void MakeOffset
873c119f 216 (const TopoDS_Edge& E,
217 const TopoDS_Face& F,
218 const Standard_Real Offset,
219 BRepFill_IndexedDataMapOfOrientedShapeListOfShape& Map,
220 const Handle(Geom_Plane)& RefPlane,
221 const Standard_Boolean IsOpenResult,
c7806cb6 222 const GeomAbs_JoinType theJoinType,
873c119f 223 const TopoDS_Vertex * Ends);
7fd59977 224
7642e2d0 225Standard_Boolean CheckSmallParamOnEdge(const TopoDS_Edge& anEdge);
226
7fd59977 227//=======================================================================
228//function : KPartCircle
229//purpose :
230//=======================================================================
231
232static Standard_Boolean KPartCircle
873c119f 233 (const TopoDS_Face& mySpine,
234 const Standard_Real myOffset,
235 const Standard_Boolean IsOpenResult,
236 const Standard_Real Alt,
237 TopoDS_Shape& myShape,
238 BRepFill_IndexedDataMapOfOrientedShapeListOfShape& myMap,
239 Standard_Boolean& myIsDone)
7fd59977 240{
7fd59977 241 TopExp_Explorer exp(mySpine,TopAbs_EDGE);
242 Standard_Integer NbEdges = 0;
243 TopoDS_Edge E;
244
245 for (; exp.More(); exp.Next()) {
246 NbEdges++;
247 E = TopoDS::Edge(exp.Current());
248 if (NbEdges > 1) return Standard_False;
249 }
7fd59977 250
251 Standard_Real f,l;
252 TopLoc_Location L;
253 Handle(Geom_Curve) C = BRep_Tool::Curve(E,L,f,l);
254
255 if (C->IsKind(STANDARD_TYPE(Geom_TrimmedCurve))) {
256 Handle(Geom_TrimmedCurve) Ct = Handle(Geom_TrimmedCurve)::DownCast(C);
257 C = Ct->BasisCurve();
258 }
259
da72a17c 260 if ((C->IsKind(STANDARD_TYPE(Geom_Circle)) && BRep_Tool::IsClosed(E)) || //closed circle
e11c1bc4 261 IsOpenResult)
6a442250 262 {
6a442250 263 Standard_Real anOffset = myOffset;
6a442250 264
0e0f7a7c 265 Handle(Geom2d_Curve) aPCurve = BRep_Tool::CurveOnSurface(E, mySpine, f, l);
6a442250 266 Handle(Geom2dAdaptor_HCurve) AHC = new Geom2dAdaptor_HCurve(aPCurve, f, l);
0e0f7a7c 267 Handle(Geom2d_Curve) OC;
268 if (AHC->GetType() == GeomAbs_Line)
269 {
ef237d73 270 if (E.Orientation() == TopAbs_REVERSED)
271 anOffset *= -1;
5201d3e6 272 Adaptor2d_OffsetCurve Off(AHC,anOffset);
0e0f7a7c 273 OC = new Geom2d_Line(Off.Line());
274 }
275 else if (AHC->GetType() == GeomAbs_Circle)
276 {
ef237d73 277 if (E.Orientation() == TopAbs_FORWARD)
278 anOffset *= -1;
0e0f7a7c 279 gp_Circ2d theCirc = AHC->Circle();
c7806cb6 280 if (anOffset > 0. || Abs(anOffset) < theCirc.Radius())
281 OC = new Geom2d_Circle (theCirc.Position(), theCirc.Radius() + anOffset);
282 else
283 {
284 myIsDone = Standard_False;
285 return Standard_False;
873c119f 286 }
0e0f7a7c 287 }
288 else
289 {
ef237d73 290 if (E.Orientation() == TopAbs_FORWARD)
291 anOffset *= -1;
0e0f7a7c 292 Handle(Geom2d_TrimmedCurve) G2dT = new Geom2d_TrimmedCurve(aPCurve, f, l);
293 OC = new Geom2d_OffsetCurve( G2dT, anOffset);
294 }
295 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(mySpine);
6a442250 296 Handle(Geom_Plane) aPlane = Handle(Geom_Plane)::DownCast(aSurf);
0e0f7a7c 297 myShape = BRepLib_MakeEdge(OC, aPlane, f, l);
6a442250 298 BRepLib::BuildCurve3d(TopoDS::Edge(myShape));
299
300 myShape.Orientation(E.Orientation());
301 myShape.Location(L);
a2ff7810 302 if (fabs(Alt) > gp::Resolution()) {
6a442250 303 BRepAdaptor_Surface S(mySpine,0);
304 gp_Ax1 Nor = S.Plane().Axis();
305 gp_Trsf T;
306 gp_Vec Trans(Nor.Direction());
307 Trans = Alt*Trans;
308 T.SetTranslation(Trans);
309 myShape.Move(TopLoc_Location(T));
310 }
311
312 TopTools_ListOfShape LL;
313 LL.Append(myShape);
314 myMap.Add(E,LL);
315
316 TopoDS_Edge myEdge = TopoDS::Edge(myShape);
317 myShape = BRepLib_MakeWire(myEdge);
318
319 myIsDone = Standard_True;
320 return Standard_True;
321 }
7fd59977 322
0e0f7a7c 323 return Standard_False;
7fd59977 324}
325
326//=======================================================================
327//function : BRepFill_OffsetWire
328//purpose :
329//=======================================================================
330
331BRepFill_OffsetWire::BRepFill_OffsetWire()
6a442250 332 : myIsOpenResult(Standard_False),
333 myIsDone(Standard_False)
7fd59977 334{
335}
336
337
338//=======================================================================
339//function : BRepFill_OffsetWire
340//purpose :
341//=======================================================================
342
343BRepFill_OffsetWire::BRepFill_OffsetWire(const TopoDS_Face& Spine,
873c119f 344 const GeomAbs_JoinType Join,
345 const Standard_Boolean IsOpenResult)
7fd59977 346{
6a442250 347 Init(Spine,Join,IsOpenResult);
7fd59977 348}
349
7fd59977 350//=======================================================================
351//function : Init
352//purpose :
353//=======================================================================
354
355void BRepFill_OffsetWire::Init(const TopoDS_Face& Spine,
873c119f 356 const GeomAbs_JoinType Join,
357 const Standard_Boolean IsOpenResult)
7fd59977 358{
7fd59977 359 myIsDone = Standard_False;
360 TopoDS_Shape aLocalShape = Spine.Oriented(TopAbs_FORWARD);
361 mySpine = TopoDS::Face(aLocalShape);
873c119f 362 // mySpine = TopoDS::Face(Spine.Oriented(TopAbs_FORWARD));
7fd59977 363 myJoinType = Join;
6a442250 364 myIsOpenResult = IsOpenResult;
873c119f 365
7fd59977 366 myMap.Clear();
367 myMapSpine.Clear();
368 //------------------------------------------------------------------
0d969553 369 // cut the spine for bissectors.
7fd59977 370 //------------------------------------------------------------------
873c119f 371 // Modified by Sergey KHROMOV - Tue Nov 26 17:39:03 2002 Begin
7fd59977 372 static BRepMAT2d_Explorer Exp;
373
374 Exp.Perform(mySpine);
375
873c119f 376 // TopoDS_Face anOldSpine = mySpine;
7fd59977 377
378 mySpine = TopoDS::Face(Exp.ModifiedShape(mySpine));
379 PrepareSpine ();
380
873c119f 381 // Modified by Sergey KHROMOV - Tue Nov 26 17:39:03 2002 End
7fd59977 382 TopoDS_Shape aShape;
86b3805f 383 BRepFill_IndexedDataMapOfOrientedShapeListOfShape aMap;
7fd59977 384 Standard_Boolean Done;
e11c1bc4 385 if (KPartCircle(myWorkSpine,1.,myIsOpenResult,0.,aShape,aMap,Done)) return;
873c119f 386
7fd59977 387
388 //-----------------------------------------------------
0d969553
Y
389 // Calculate the map of bissectors to the left.
390 // and Links Topology -> base elements of the map.
7fd59977 391 //-----------------------------------------------------
392
873c119f 393 // Modified by Sergey KHROMOV - Tue Nov 26 17:39:03 2002 Begin
394 // static BRepMAT2d_Explorer Exp;
395 // Modified by Sergey KHROMOV - Tue Nov 26 17:39:03 2002 End
7fd59977 396 Exp.Perform(myWorkSpine);
c19dd45e 397 myBilo.Compute(Exp, 1 ,MAT_Left, myJoinType, myIsOpenResult);
873c119f 398 myLink.Perform(Exp,myBilo);
7fd59977 399}
400
401
402//=======================================================================
403//function : IsDone
404//purpose :
405//=======================================================================
406
407Standard_Boolean BRepFill_OffsetWire::IsDone() const
408{
409 return myIsDone;
410}
411
412
413//=======================================================================
414//function : Spine
415//purpose :
416//=======================================================================
417
418const TopoDS_Face& BRepFill_OffsetWire::Spine() const
419{
420 return mySpine;
421}
422
423
424//=======================================================================
425//function : Shape
426//purpose :
427//=======================================================================
428
429const TopoDS_Shape& BRepFill_OffsetWire::Shape() const
430{
431 return myShape;
432}
433
434
435//=======================================================================
436//function : GeneratedShapes
437//purpose :
438//=======================================================================
439
440const TopTools_ListOfShape& BRepFill_OffsetWire::GeneratedShapes
873c119f 441 (const TopoDS_Shape& SpineShape)
7fd59977 442{
443 if (!myCallGen) {
444 if (!myMapSpine.IsEmpty()) {
0d969553 445 // myMapSpine can be empty if passed by PerformWithBilo.
7fd59977 446 TopTools_DataMapIteratorOfDataMapOfShapeShape it(myMapSpine);
447 for (; it.More(); it.Next()) {
86b3805f
J
448 if (myMap.Contains(it.Key())) {
449 if (!myMap.Contains(it.Value())) {
7fd59977 450 TopTools_ListOfShape L;
86b3805f 451 myMap.Add(it.Value(),L);
7fd59977 452 }
453 if ( !it.Value().IsSame(it.Key())) {
86b3805f 454 myMap.ChangeFromKey(it.Value()).Append(myMap.ChangeFromKey(it.Key()));
3f5aa017 455 myMap.RemoveKey(it.Key());
7fd59977 456 }
457 }
86b3805f
J
458 if (myMap.Contains(it.Key().Reversed())) {
459 if (!myMap.Contains(it.Value().Reversed())) {
7fd59977 460 TopTools_ListOfShape L;
86b3805f 461 myMap.Add(it.Value().Reversed(),L);
7fd59977 462 }
463 if ( !it.Value().IsSame(it.Key())) {
86b3805f 464 myMap.ChangeFromKey(it.Value().Reversed()).Append(myMap.ChangeFromKey(it.Key().Reversed()));
3f5aa017 465 myMap.RemoveKey(it.Key().Reversed());
7fd59977 466 }
467 }
468 }
469 }
470 myCallGen = Standard_True;
471 }
873c119f 472
86b3805f
J
473 if (myMap.Contains(SpineShape)) {
474 return myMap.FindFromKey(SpineShape);
7fd59977 475 }
476 else {
477 static TopTools_ListOfShape Empty;
478 return Empty;
479 }
480}
481
482
483//=======================================================================
484//function : JoinType
485//purpose :
486//=======================================================================
487
488GeomAbs_JoinType BRepFill_OffsetWire::JoinType() const
489{
490 return myJoinType;
491}
492
493//=======================================================================
494//function : Perform
495//purpose :
496//=======================================================================
497
498void BRepFill_OffsetWire::Perform (const Standard_Real Offset,
873c119f 499 const Standard_Real Alt)
7fd59977 500{
91bb31f3 501 // Modified by skv - Fri Jul 8 11:21:38 2005 OCC9145 Begin
502 try
503 {
7fd59977 504 OCC_CATCH_SIGNALS
91bb31f3 505 myCallGen = Standard_False;
e11c1bc4 506 if (KPartCircle(myWorkSpine,Offset,myIsOpenResult,Alt,myShape,myMap,myIsDone)) return;
7fd59977 507
508 TopoDS_Face oldWorkSpain = myWorkSpine;
509
510 TopTools_ListOfShape BadEdges;
511 CheckBadEdges(myWorkSpine,Offset,myBilo,myLink,BadEdges);
512
91bb31f3 513 if(!BadEdges.IsEmpty())
514 {
7fd59977 515 // Modification of myWorkSpine;
516 //cout << "Modification of myWorkSpine : " << BadEdges.Extent() << endl;
517 BRepTools_Substitution aSubst;
518 TopTools_ListIteratorOfListOfShape it(BadEdges);
519 TopTools_ListOfShape aL;
520 Standard_Real aDefl = .01 * Abs(Offset);
521 TColStd_SequenceOfReal Parameters;
522 TColgp_SequenceOfPnt Points;
523
524 for(; it.More(); it.Next()) {
91bb31f3 525 aL.Clear();
526 Parameters.Clear();
527 Points.Clear();
528 const TopoDS_Shape& anE = it.Value();
529
530 TopoDS_Vertex Vf, Vl;
531 TopExp::Vertices(TopoDS::Edge(anE), Vf, Vl);
532
533 Standard_Real f, l;
534 Handle(Geom_Curve) G3d = BRep_Tool::Curve(TopoDS::Edge(anE),f,l);
535 GeomAdaptor_Curve AC(G3d,f,l);
536
537 PerformCurve(Parameters, Points, AC, aDefl, f,
873c119f 538 l, Precision::Confusion(), 2);
91bb31f3 539
540 Standard_Integer NPnts = Points.Length();
541 if(NPnts > 2)
542 {
543 //cout << NPnts << " points " << endl;
544 TopoDS_Vertex FV = Vf;
545 TopoDS_Vertex LV;
546 TopoDS_Edge newE;
547 Standard_Integer np;
548 for(np = 2; np < NPnts; np++) {
549 gp_Pnt LP = Points(np);
550 LV = BRepLib_MakeVertex(LP);
551 newE = BRepLib_MakeEdge(FV, LV);
552 aL.Append(newE);
553 FV = LV;
554 }
555 LV = Vl;
556 newE = BRepLib_MakeEdge(FV, LV);
557 aL.Append(newE);
558 }
559 else
560 {
561 //cout << " 2 points " << endl;
562 TopoDS_Edge newE = BRepLib_MakeEdge(Vf, Vl);
563 aL.Append(newE);
564 }
565 //Update myMapSpine
566 if (myMapSpine.IsBound( anE ))
567 {
568 TopTools_ListIteratorOfListOfShape newit( aL );
569 for (; newit.More(); newit.Next())
570 {
571 TopoDS_Edge NewEdge = TopoDS::Edge( newit.Value() );
572 myMapSpine.Bind( NewEdge, myMapSpine(anE) );
573 TopoDS_Vertex NewV1, NewV2;
574 EdgeVertices( NewEdge, NewV1, NewV2 );
575 if (!myMapSpine.IsBound(NewV1)) myMapSpine.Bind( NewV1, myMapSpine(anE) );
576 if (!myMapSpine.IsBound(NewV2)) myMapSpine.Bind( NewV2, myMapSpine(anE) );
577 }
578 myMapSpine.UnBind( anE );
579 }
580 ///////////////////
581 aSubst.Substitute(anE, aL);
7fd59977 582 }
583
584 TopTools_DataMapOfShapeListOfShape wwmap;
585 TopoDS_Iterator itws( myWorkSpine );
586 for (; itws.More(); itws.Next())
91bb31f3 587 {
588 TopoDS_Shape aWire = itws.Value();
589 aSubst.Build( aWire );
590 if (aSubst.IsCopied(aWire))
591 {
592 TopoDS_Wire NewWire = TopoDS::Wire( aSubst.Copy(aWire).First() );
593 NewWire.Closed( aWire.Closed() );
594 TopTools_ListOfShape Lw;
595 Lw.Append( NewWire );
596 wwmap.Bind( aWire, Lw );
597 }
598 }
7fd59977 599 aSubst.Clear();
600 TopTools_DataMapIteratorOfDataMapOfShapeListOfShape itmap( wwmap );
601 for (; itmap.More(); itmap.Next())
91bb31f3 602 aSubst.Substitute( itmap.Key(), itmap.Value() );
873c119f 603
7fd59977 604 aSubst.Build(myWorkSpine);
873c119f 605
7fd59977 606 if(aSubst.IsCopied(myWorkSpine)) {
91bb31f3 607 myWorkSpine = TopoDS::Face(aSubst.Copy(myWorkSpine).First());
ab87e6fc 608
91bb31f3 609 BRepMAT2d_Explorer newExp;
610 newExp.Perform(myWorkSpine);
611 BRepMAT2d_BisectingLocus newBilo;
612 BRepMAT2d_LinkTopoBilo newLink;
c19dd45e 613 newBilo.Compute(newExp, 1, MAT_Left, myJoinType, myIsOpenResult);
ab87e6fc 614
91bb31f3 615 if(!newBilo.IsDone())
616 {
617 myShape.Nullify();
618 myIsDone = Standard_False;
619 return;
620 }
7fd59977 621
91bb31f3 622 newLink.Perform(newExp,newBilo);
623 PerformWithBiLo(myWorkSpine,Offset,newBilo,newLink,myJoinType,Alt);
624 myWorkSpine = oldWorkSpain;
7fd59977 625 }
626 else {
91bb31f3 627 PerformWithBiLo(myWorkSpine,Offset,myBilo,myLink,myJoinType,Alt);
7fd59977 628 }
7fd59977 629 }
91bb31f3 630 else
631 {
7fd59977 632 PerformWithBiLo(myWorkSpine,Offset,myBilo,myLink,myJoinType,Alt);
633 }
ab87e6fc 634 }
9775fa61 635 catch (Standard_Failure const& anException) {
0797d9d3 636#ifdef OCCT_DEBUG
ab87e6fc 637 cout<<"An exception was caught in BRepFill_OffsetWire::Perform : ";
9775fa61 638 anException.Print(cout);
ab87e6fc 639 cout<<endl;
63c629aa 640#endif
9775fa61 641 (void)anException;
642 myShape.Nullify();
643 myIsDone = Standard_False;
91bb31f3 644
7fd59977 645 return;
646 }
ab87e6fc 647
91bb31f3 648 // Modified by skv - Fri Jul 8 11:21:38 2005 OCC9145 End
649 // Modified by Sergey KHROMOV - Thu Mar 14 10:48:15 2002 Begin
6a442250 650 if (!myIsOpenResult)
651 {
652 TopExp_Explorer anExp(myShape, TopAbs_WIRE);
873c119f 653
6a442250 654 for (; anExp.More(); anExp.Next()) {
655 const TopoDS_Shape &aWire = anExp.Current();
873c119f 656
6a442250 657 if (!aWire.Closed()) {
658 myShape.Nullify();
659 myIsDone = Standard_False;
9775fa61 660 throw Standard_ConstructionError("Offset wire is not closed.");
6a442250 661 }
7fd59977 662 }
663 }
91bb31f3 664 // Modified by Sergey KHROMOV - Thu Mar 14 10:48:16 2002 End
7fd59977 665}
666
667//=======================================================================
668//function : Compute
669//purpose :
670//=======================================================================
671
672void Compute (const TopoDS_Face& Spine,
873c119f 673 TopoDS_Shape& aShape,
674 BRepFill_IndexedDataMapOfOrientedShapeListOfShape& Map,
675 const Standard_Real Alt)
7fd59977 676{
677 BRep_Builder B;
678 B.MakeCompound(TopoDS::Compound(aShape));
679 Standard_Real ALT = Alt;
680 if ( Spine.Orientation() == TopAbs_REVERSED) ALT = -Alt;
681 gp_Trsf T;
682 T.SetTranslation(gp_Vec(0.,0.,ALT));
683 TopLoc_Location L(T);
684
685 for ( TopExp_Explorer exp(Spine,TopAbs_WIRE); exp.More(); exp.Next()) {
686 const TopoDS_Wire& CurW = TopoDS::Wire(exp.Current());
687 TopoDS_Shape aLocalShape = CurW.Moved(L);
688 TopoDS_Wire NewW = TopoDS::Wire(aLocalShape);
873c119f 689 // TopoDS_Wire NewW = TopoDS::Wire(CurW.Moved(L));
7fd59977 690 B.Add(aShape,NewW);
0d969553 691 // update Map.
7fd59977 692 TopoDS_Iterator it1( CurW);
693 TopoDS_Iterator it2( NewW);
694 for ( ; it1.More(); it1.Next(), it2.Next()) {
695 TopTools_ListOfShape List;
696 List.Append(it2.Value());
86b3805f 697 Map.Add(it1.Value(), List);
7fd59977 698 }
699 }
700}
701
702//=======================================================================
703//function : PerformWithBiLo
704//purpose :
705//=======================================================================
706
707void BRepFill_OffsetWire::PerformWithBiLo
873c119f 708 (const TopoDS_Face& Spine,
709 const Standard_Real Offset,
710 const BRepMAT2d_BisectingLocus& Locus,
711 BRepMAT2d_LinkTopoBilo& Link,
712 const GeomAbs_JoinType Join,
713 const Standard_Real Alt)
7fd59977 714{
7fd59977 715 myIsDone = Standard_False;
51740958 716 TopoDS_Shape aLocalShapeWork = Spine.Oriented(TopAbs_FORWARD);
717 myWorkSpine = TopoDS::Face(aLocalShapeWork);
873c119f 718 // myWorkSpine = TopoDS::Face(Spine.Oriented(TopAbs_FORWARD));
7fd59977 719 myJoinType = Join;
720 myOffset = Offset ;
721 myShape.Nullify();
722
723
724 if (mySpine.IsNull()) {
725 TopoDS_Shape aLocalShape = Spine.Oriented(TopAbs_FORWARD);
726 mySpine = TopoDS::Face(aLocalShape);
873c119f 727 // mySpine = TopoDS::Face(Spine.Oriented(TopAbs_FORWARD));
728 }
7fd59977 729 myMap.Clear();
730
7fd59977 731 if ( Abs(myOffset) < Precision::Confusion()) {
732 Compute(mySpine,myShape,myMap,Alt);
733 myIsDone = Standard_True;
734 return;
735 }
736
0d969553
Y
737 //********************************
738 // Calculate for a non null offset
739 //********************************
e11c1bc4 740 if (KPartCircle(myWorkSpine,Offset,myIsOpenResult,Alt,myShape,myMap,myIsDone))
91bb31f3 741 return;
7fd59977 742
743 BRep_Builder myBuilder;
744 myBuilder.MakeCompound(TopoDS::Compound(myShape));
873c119f 745
7fd59977 746 //---------------------------------------------------------------------
0d969553
Y
747 // MapNodeVertex : associate to each node of the map (key1) and to
748 // each element of the profile (key2) a vertex (item).
749 // MapBis : all edges or vertices (item) generated by
750 // a bisectrice on a face or an edge (key) of revolution tubes.
751 // MapVerPar : Map of parameters of vertices on parallel edges
752 // the list contained in MapVerPar (E) corresponds to
753 // parameters on E of vertices contained in MapBis(E);
7fd59977 754 //---------------------------------------------------------------------
755
756
757 BRepFill_DataMapOfNodeShape MapNodeVertex;
758 TopTools_DataMapOfShapeSequenceOfShape MapBis;
759 BRepFill_DataMapOfShapeSequenceOfReal MapVerPar;
760
761 TopTools_DataMapOfShapeShape EmptyMap;
762 TopTools_SequenceOfShape EmptySeq;
763 TopTools_ListOfShape EmptyList;
764 TColStd_SequenceOfReal EmptySeqOfReal;
765
7fd59977 766 Handle(Geom_Plane) RefPlane =
767 Handle(Geom_Plane)::DownCast(BRep_Tool::Surface(myWorkSpine));
a2ff7810 768 if (fabs(Alt) > gp::Resolution()) {
769 Standard_Real anAlt = Alt;
770 if ( myWorkSpine.Orientation() == TopAbs_REVERSED) anAlt = -Alt;
771 RefPlane = Handle(Geom_Plane)::DownCast
772 (RefPlane->Translated( anAlt * gp_Vec(RefPlane->Axis().Direction() )));
773 }
7fd59977 774
775 //---------------------------------------------------------------
0d969553 776 // Construction of Circles and OffsetCurves
7fd59977 777 //---------------------------------------------------------------
6a442250 778
779 TopoDS_Vertex Ends [2];
780 if (myIsOpenResult)
781 {
782 TopoDS_Wire theWire;
783 TopoDS_Iterator iter(mySpine);
784 theWire = TopoDS::Wire(iter.Value());
785 TopExp::Vertices(theWire, Ends[0], Ends[1]);
786 }
873c119f 787
7fd59977 788 for (Standard_Integer ic = 1; ic <= Locus.NumberOfContours(); ic++) {
789 TopoDS_Shape PEE = Link.GeneratingShape(Locus.BasicElt(ic,Locus.NumberOfElts(ic)));
790 TopoDS_Shape& PE = PEE ;
791 for (Standard_Integer ie = 1; ie <= Locus.NumberOfElts(ic); ie++) {
792 const TopoDS_Shape& SE = Link.GeneratingShape(Locus.BasicElt(ic,ie));
793 if (SE.ShapeType() == TopAbs_VERTEX) {
0e0f7a7c 794 if (!SE.IsSame(Ends[0]) && !SE.IsSame(Ends[1]))
795 MakeCircle (TopoDS::Edge(PE),TopoDS::Vertex(SE),
796 myWorkSpine,myOffset,myMap,RefPlane);
7fd59977 797 }
798 else {
873c119f 799 MakeOffset (TopoDS::Edge(SE),myWorkSpine,myOffset,myMap,RefPlane,
c7806cb6 800 myIsOpenResult, myJoinType, Ends);
873c119f 801 PE = SE;
7fd59977 802 }
803 }
804 }
805
016dfb16 806 //Remove possible hanging arcs on vertices
807 if (myIsOpenResult && myJoinType == GeomAbs_Arc)
808 {
809 if (!myMap.IsEmpty() &&
810 myMap.FindKey(1).ShapeType() == TopAbs_VERTEX)
811 {
3f5aa017 812 myMap.RemoveFromIndex(1);
016dfb16 813 }
814 if (!myMap.IsEmpty() &&
815 myMap.FindKey(myMap.Extent()).ShapeType() == TopAbs_VERTEX)
816 myMap.RemoveLast();
817 }
7fd59977 818
873c119f 819#ifdef OCCT_DEBUG
1896126e 820#ifdef DRAW
7fd59977 821 if (AffichEdge) {
0d969553 822 cout << " End Construction of geometric primitives "<<endl;
7fd59977 823 }
824#endif
873c119f 825#endif
7fd59977 826
827
828 //---------------------------------------------------
0d969553 829 // Construction of offset vertices.
7fd59977 830 //---------------------------------------------------
831 BRepFill_DataMapOfOrientedShapeListOfShape Detromp;
832 Handle(MAT_Arc) CurrentArc;
833 Handle(Geom2d_Curve) Bis, PCurve1, PCurve2 ;
834 Handle(Geom_Curve) CBis;
835 Standard_Boolean Reverse;
836 TopoDS_Edge CurrentEdge;
837 TopoDS_Shape S [2];
838 TopoDS_Edge E [2];
839 TopLoc_Location L;
86b3805f 840 Standard_Integer j, k;
7fd59977 841
842 for (Standard_Integer i = 1; i <= Locus.Graph()->NumberOfArcs(); i++) {
843
844 CurrentArc = Locus.Graph()->Arc(i);
845 Bisector_Bisec Bisec = Locus.GeomBis(CurrentArc,Reverse);
873c119f 846#ifdef OCCT_DEBUG
7fd59977 847#ifdef DRAW
873c119f 848
849 if ( AffichGeom) {
850 char name[256];
851 sprintf(name,"BISSEC_%d",NbBISSEC++);
852 DrawTrSurf::Set(name,Bisec.Value());
853 }
854#endif
7fd59977 855#endif
856
0d969553
Y
857 //-------------------------------------------------------------------
858 // Return elements of the spine corresponding to separate basicElts.
859 //-------------------------------------------------------------------
7fd59977 860 S [0] = Link.GeneratingShape(CurrentArc->FirstElement());
861 S [1] = Link.GeneratingShape(CurrentArc->SecondElement());
862
863 TopTools_SequenceOfShape Vertices;
864 TColgp_SequenceOfPnt Params;
873c119f 865
7fd59977 866 TopTools_DataMapOfShapeSequenceOfShape MapSeqVer;
867 BRepFill_DataMapOfShapeSequenceOfPnt MapSeqPar;
868
869 //-----------------------------------------------------------
0d969553
Y
870 // Return parallel edges on each face.
871 // If no offset generated => move to the next bissectrice.
7fd59977 872 //--------------------------------------------------------------
86b3805f
J
873 if (myMap.Contains(S[0]) && myMap.Contains(S[1])) {
874 E [0] = TopoDS::Edge(myMap.FindFromKey(S[0]).First());
875 E [1] = TopoDS::Edge(myMap.FindFromKey(S[1]).First());
7fd59977 876 }
877 else continue;
878
879 //-----------------------------------------------------------
0d969553
Y
880 // Construction of vertices corresponding to the node of the map.
881 // if they are on the offset.
7fd59977 882 //-----------------------------------------------------------
883 TopoDS_Vertex VS,VE;
884 Handle(MAT_Node) Node1, Node2;
885
886 if (Reverse) {
887 Node1 = CurrentArc->SecondNode();
888 Node2 = CurrentArc->FirstNode();
889 }
890 else {
891 Node1 = CurrentArc->FirstNode();
892 Node2 = CurrentArc->SecondNode();
893 }
873c119f 894
7fd59977 895 Standard_Boolean StartOnEdge = 0, EndOnEdge = 0;
873c119f 896
7fd59977 897 if (!Node1->Infinite()) {
898 gp_Pnt2d aLocalPnt2d = Locus.GeomElt(Node1);
899 StartOnEdge = VertexFromNode(Node1, myOffset, aLocalPnt2d ,MapNodeVertex,VS);
873c119f 900 // StartOnEdge = VertexFromNode(Node1, myOffset, Locus.GeomElt(Node1),
901 // MapNodeVertex,VS);
7fd59977 902 }
903 if (!Node2->Infinite()) {
904 gp_Pnt2d aLocalPnt2d = Locus.GeomElt(Node2) ;
905 EndOnEdge = VertexFromNode(Node2, myOffset, aLocalPnt2d ,MapNodeVertex,VE);
873c119f 906 // EndOnEdge = VertexFromNode(Node2, myOffset, Locus.GeomElt(Node2),
907 // MapNodeVertex,VE);
7fd59977 908 }
909
9eb68d38 910 if (myJoinType == GeomAbs_Intersection)
911 StartOnEdge = EndOnEdge = 0;
912
7fd59977 913 //---------------------------------------------
0d969553 914 // Construction of geometries.
7fd59977 915 //---------------------------------------------
916 BRepFill_TrimEdgeTool Trim (Bisec,
873c119f 917 Locus.GeomElt(CurrentArc->FirstElement()),
918 Locus.GeomElt(CurrentArc->SecondElement()),
919 myOffset);
7fd59977 920
921 //-----------------------------------------------------------
0d969553 922 // Construction of vertices on edges parallel to the spine.
7fd59977 923 //-----------------------------------------------------------
924
46767247 925 Trim.IntersectWith(E[0], E[1], S[0], S[1], Ends[0], Ends[1],
926 myJoinType, myIsOpenResult, Params);
7fd59977 927
928 for (Standard_Integer s = 1; s <= Params.Length(); s++) {
929 TopoDS_Vertex VC;
930 myBuilder.MakeVertex (VC);
931 gp_Pnt2d P2 = Bisec.Value()->Value(Params.Value(s).X());
932 gp_Pnt PVC(P2.X(),P2.Y(),0.);
933
934 myBuilder.UpdateVertex(VC,PVC,Precision::Confusion());
935 Vertices.Append(VC);
936 }
937 if (StartOnEdge) {
938 Standard_Boolean Start = 1;
939 Trim.AddOrConfuse(Start, E[0], E[1], Params);
940 if (Params.Length() == Vertices.Length())
873c119f 941 Vertices.SetValue(1,VS);
7fd59977 942
943 else
873c119f 944 // the point was not found by IntersectWith
945 Vertices.Prepend(VS);
7fd59977 946 }
947 if (EndOnEdge) {
948 Standard_Boolean Start = 0;
949 Trim.AddOrConfuse(Start, E[0], E[1], Params);
950 if (Params.Length() == Vertices.Length())
873c119f 951 Vertices.SetValue(Params.Length(),VE);
7fd59977 952
953 else
873c119f 954 // the point was not found by IntersectWith
955 Vertices.Append(VE);
7fd59977 956 }
957
958 //------------------------------------------------------------
0d969553
Y
959 // Update Detromp.
960 // Detromp allows to remove vertices on the offset
961 // corresponding to tangency zones
962 // Detromp ranks the vertices that limit
963 // the parts of the bissectrices located between the spine and the
964 // offset.
7fd59977 965 //------------------------------------------------------------
966 if (!Detromp.IsBound(S[0])) Detromp.Bind(S[0],EmptyList);
967 if (!Detromp.IsBound(S[1])) Detromp.Bind(S[1],EmptyList);
968
969
c19dd45e 970 UpdateDetromp (Detromp, S[0], S[1], Vertices, Params,
873c119f 971 Bisec, StartOnEdge, EndOnEdge, Trim);
7fd59977 972 //----------------------------------------------
0d969553
Y
973 // Storage of vertices on parallel edges.
974 // fill MapBis and MapVerPar.
7fd59977 975 //----------------------------------------------
976 if (!Vertices.IsEmpty()) {
977 for (k = 0; k <= 1; k++) {
873c119f 978 if (!MapBis.IsBound(E[k])) {
979 MapBis .Bind(E[k],EmptySeq);
980 MapVerPar.Bind(E[k],EmptySeqOfReal);
981 }
982 for (Standard_Integer ii = 1; ii <= Vertices.Length(); ii++) {
983 MapBis (E[k]).Append(Vertices.Value(ii));
984 if (k == 0) MapVerPar (E[k]).Append(Params.Value(ii).Y());
985 else MapVerPar (E[k]).Append(Params.Value(ii).Z());
986 }
7fd59977 987 }
988 }
989 else {
990 //------------------------------------------------------------
0d969553
Y
991 // FOR COMPLETE CIRCLES. the parallel line can be contained
992 // in the zone without intersection with the border
993 // no intersection
994 // if myoffset is < distance of nodes the parallel can be valid.
7fd59977 995 //-------------------------------------------------------------
996 for (k = 0; k <= 1; k++) {
873c119f 997 if (!MapBis.IsBound(E[k])) {
998 if (Node1->Distance() > myOffset && Node2->Distance() > myOffset) {
999 MapBis .Bind(E[k],EmptySeq);
1000 MapVerPar.Bind(E[k],EmptySeqOfReal);
1001 }
1002 }
7fd59977 1003 }
1004 }
1005 }
873c119f 1006
1007#ifdef OCCT_DEBUG
1896126e 1008#ifdef DRAW
7fd59977 1009 if (AffichEdge) {
0d969553 1010 cout << " End Construction of vertices on offsets"<<endl;
7fd59977 1011 }
1012#endif
873c119f 1013#endif
7fd59977 1014
1015 //----------------------------------
0d969553 1016 // Construction of parallel edges.
7fd59977 1017 //----------------------------------
1018 TopTools_IndexedDataMapOfShapeShape MapVV;
1019
1020 TopoDS_Shape CurrentSpine;
1021
86b3805f 1022 //BRepFill_DataMapIteratorOfDataMapOfOrientedShapeListOfShape ite1;
7fd59977 1023
86b3805f
J
1024 for (j = 1; j <= myMap.Extent(); j++) {
1025 CurrentSpine = myMap.FindKey(j);
1026 CurrentEdge = TopoDS::Edge(myMap(j).First());
7fd59977 1027
86b3805f 1028 myMap(j).Clear();
7fd59977 1029 if (MapBis.IsBound(CurrentEdge)) {
51740958 1030 TopTools_SequenceOfShape aSeqOfShape;
7fd59977 1031 if (!MapBis(CurrentEdge).IsEmpty()) {
6a442250 1032 Standard_Integer IndOfE = 0;
1033 if (myIsOpenResult)
1034 {
1035 if (j == 1)
1036 IndOfE = 1;
1037 else if (j == myMap.Extent())
1038 IndOfE = -1;
1039 }
873c119f 1040 TrimEdge (CurrentEdge,
1041 CurrentSpine,
1042 mySpine,
1043 Detromp (CurrentSpine),
1044 MapBis (CurrentEdge) ,
1045 MapVerPar(CurrentEdge) ,
51740958 1046 aSeqOfShape, MapVV, IndOfE);
1047 for ( k = 1; k <= aSeqOfShape.Length(); k++) {
1048 myMap(j).Append(aSeqOfShape.Value(k));
873c119f 1049 }
7fd59977 1050 }
1051 else {
873c119f 1052 //-----------------
1053 // Complete circles
1054 //-----------------
1055 myMap(j).Append(CurrentEdge);
7fd59977 1056 }
1057 }
1058 }
873c119f 1059
7fd59977 1060 Standard_Integer ind;
1061 for (ind = 1; ind <= MapVV.Extent(); ind++)
873c119f 1062 {
1063 TopoDS_Vertex OldV = TopoDS::Vertex(MapVV.FindKey(ind));
1064 TopoDS_Vertex NewV = TopoDS::Vertex(MapVV(ind));
1065 gp_Pnt P1 = BRep_Tool::Pnt(OldV);
1066 gp_Pnt P2 = BRep_Tool::Pnt(NewV);
1067 myBuilder.UpdateVertex(NewV, P1.Distance(P2));
1068 TopTools_ListOfShape LV;
1069 LV.Append( NewV.Oriented(TopAbs_FORWARD) );
1070 BRepTools_Substitution aSubst;
1071 aSubst.Substitute( OldV, LV );
1072 for (j = 1; j <= myMap.Extent(); j++)
7fd59977 1073 {
873c119f 1074 TopTools_ListIteratorOfListOfShape itl(myMap(j));
1075 for (; itl.More(); itl.Next())
1076 {
1077 aSubst.Build(itl.Value());
1078 if (aSubst.IsCopied(itl.Value()))
1079 {
1080 const TopTools_ListOfShape& listSh = aSubst.Copy(itl.Value());
1081 TopAbs_Orientation SaveOr = itl.Value().Orientation();
1082 itl.Value() = listSh.First();
1083 itl.Value().Orientation(SaveOr);
1084 }
1085 }
7fd59977 1086 }
873c119f 1087 }
7fd59977 1088
1089 //----------------------------------
0d969553 1090 // Construction of offset wires.
7fd59977 1091 //----------------------------------
1092 MakeWires ();
1093
0d969553 1094 // Update vertices ( Constructed in the plane Z = 0) !!!
7fd59977 1095 TopTools_MapOfShape MapVertex;
1096 for ( TopExp_Explorer exp(myShape,TopAbs_VERTEX); exp.More(); exp.Next()) {
1097 TopoDS_Vertex V = TopoDS::Vertex(exp.Current());
1098 if ( MapVertex.Add(V)) {
1099 gp_Pnt P = BRep_Tool::Pnt(V);
1100 P = RefPlane->Value(P.X(),P.Y());
1101 myBuilder.UpdateVertex(V,P, Precision::Confusion());
1102 }
1103 }
1104
0d969553 1105 // Construction of curves 3d.
7fd59977 1106 BRepLib::BuildCurves3d(myShape);
1107 MapVertex.Clear();
1108 TopExp_Explorer Explo( myShape, TopAbs_EDGE );
1109 for (; Explo.More(); Explo.Next())
873c119f 1110 {
51740958 1111 TopoDS_Edge anEdge = TopoDS::Edge( Explo.Current() );
873c119f 1112 TopoDS_Vertex V1, V2;
51740958 1113 TopExp::Vertices(anEdge, V1, V2 );
873c119f 1114 Handle(BRep_TVertex)& TV1 = *((Handle(BRep_TVertex)*) &(V1).TShape());
1115 Handle(BRep_TVertex)& TV2 = *((Handle(BRep_TVertex)*) &(V2).TShape());
1116
1117 TopLoc_Location loc;
1118 Standard_Real f, l;
51740958 1119 Handle( Geom_Curve ) theCurve = BRep_Tool::Curve(anEdge, loc, f, l );
873c119f 1120 theCurve = Handle( Geom_Curve )::DownCast( theCurve->Copy() );
1121 theCurve->Transform( loc.Transformation() );
1122 gp_Pnt f3d = theCurve->Value( f );
1123 gp_Pnt l3d = theCurve->Value( l );
1124
1125 Standard_Real dist1, dist2;
1126 dist1 = f3d.Distance( TV1->Pnt() );
1127 dist2 = l3d.Distance( TV2->Pnt() );
1128 if (! MapVertex.Contains( V1 ))
7fd59977 1129 {
7fd59977 1130
873c119f 1131 TV1->Pnt( f3d );
1132 MapVertex.Add( V1 );
7fd59977 1133 }
873c119f 1134 else
1135 TV1->UpdateTolerance( 1.5*dist1 );
1136 if (! MapVertex.Contains( V2 ))
1137 {
1138 TV2->Pnt( l3d );
1139 MapVertex.Add( V2 );
1140 }
1141 else
1142 TV2->UpdateTolerance( 1.5*dist2 );
1143 }
7fd59977 1144
1867dc32 1145 if (!myIsOpenResult)
1146 FixHoles();
7fd59977 1147
1148 myIsDone = Standard_True;
1149}
1150
1151
1152//=======================================================================
1153//function : Generated
1154//purpose :
1155//=======================================================================
1156
86b3805f 1157BRepFill_IndexedDataMapOfOrientedShapeListOfShape&
873c119f 1158 BRepFill_OffsetWire::Generated()
7fd59977 1159{
1160 return myMap;
1161}
1162
1163
1164//=======================================================================
1165//function : PrepareSpine
1166//purpose :
1167//=======================================================================
1168
1169void BRepFill_OffsetWire::PrepareSpine()
1170{
1171 BRep_Builder B;
1172 TopTools_ListOfShape Cuts;
1173 TopTools_ListIteratorOfListOfShape IteCuts;
1174 TopoDS_Vertex V1,V2;
1175
1176 myWorkSpine.Nullify();
1177 myMapSpine.Clear();
1178
1179 TopLoc_Location L;
1180 const Handle(Geom_Surface)& S = BRep_Tool::Surface (mySpine,L);
1181 Standard_Real TolF = BRep_Tool::Tolerance(mySpine);
1182 B.MakeFace(myWorkSpine,S,L,TolF);
1183
1184 for (TopoDS_Iterator IteF(mySpine) ; IteF.More(); IteF.Next()) {
1185
1186 TopoDS_Wire NW;
1187 B.MakeWire (NW);
1188
873c119f 1189 // Modified by Sergey KHROMOV - Thu Nov 16 17:29:55 2000 Begin
7fd59977 1190 Standard_Integer ForcedCut = 0;
1191 Standard_Integer nbResEdges = -1;
1192 TopTools_IndexedMapOfShape EdgeMap;
1193
1194 TopExp::MapShapes(IteF.Value(), TopAbs_EDGE, EdgeMap);
1195 Standard_Integer nbEdges = EdgeMap.Extent();
1196
6a442250 1197 if (nbEdges == 1 && !myIsOpenResult) //in case of open wire there's no need to do it
7fd59977 1198 ForcedCut = 2;
873c119f 1199 // Modified by Sergey KHROMOV - Thu Nov 16 17:29:48 2000 End
7fd59977 1200
1201 for (TopoDS_Iterator IteW(IteF.Value()); IteW.More(); IteW.Next()) {
1202
1203 const TopoDS_Edge& E = TopoDS::Edge(IteW.Value());
1204 EdgeVertices(E,V1,V2);
1205 myMapSpine.Bind(V1,V1);
1206 myMapSpine.Bind(V2,V2);
1207 Cuts.Clear();
1208
0d969553 1209 // Cut
7fd59977 1210 TopoDS_Shape aLocalShape = E.Oriented(TopAbs_FORWARD);
873c119f 1211 // Modified by Sergey KHROMOV - Thu Nov 16 17:29:29 2000 Begin
7fd59977 1212 if (nbEdges == 2 && nbResEdges == 0)
873c119f 1213 ForcedCut = 1;
1214 // Modified by Sergey KHROMOV - Thu Nov 16 17:29:33 2000 End
7fd59977 1215 nbResEdges = CutEdge (TopoDS::Edge(aLocalShape), mySpine, ForcedCut, Cuts);
1216
1217 if (Cuts.IsEmpty()) {
873c119f 1218 B.Add(NW,E);
1219 myMapSpine.Bind(E,E);
7fd59977 1220 }
1221 else {
873c119f 1222 for (IteCuts.Initialize(Cuts); IteCuts.More(); IteCuts.Next()) {
1223 TopoDS_Edge NE = TopoDS::Edge(IteCuts.Value());
1224 NE.Orientation(E.Orientation());
1225 B.Add(NW,NE);
1226 myMapSpine.Bind(NE,E);
1227 EdgeVertices(NE,V1,V2);
1228 if (!myMapSpine.IsBound(V1)) myMapSpine.Bind(V1,E);
1229 if (!myMapSpine.IsBound(V2)) myMapSpine.Bind(V2,E);
1230 }
7fd59977 1231 }
1232 }
873c119f 1233 // Modified by Sergey KHROMOV - Thu Mar 7 09:17:41 2002 Begin
7fd59977 1234 TopoDS_Vertex aV1;
1235 TopoDS_Vertex aV2;
1236
1237 TopExp::Vertices(NW, aV1, aV2);
1238
1239 NW.Closed(aV1.IsSame(aV2));
873c119f 1240
1241 // Modified by Sergey KHROMOV - Thu Mar 7 09:17:43 2002 End
7fd59977 1242 B.Add(myWorkSpine, NW);
1243 }
1244
873c119f 1245#ifdef OCCT_DEBUG
7fd59977 1246#ifdef DRAW
1247 if ( AffichEdge) {
1896126e 1248 DBRep::Set("WS",myWorkSpine);
873c119f 1249 DBRep::Set("MS",mySpine);
1250 BRepTools::Write(myWorkSpine, "WS");
1251 BRepTools::Write(mySpine, "MS");
7fd59977 1252 }
1253#endif
873c119f 1254#endif
7fd59977 1255
1256}
1257
1258//=======================================================================
c19dd45e 1259//function : UpdateDetromp
1260//purpose : For each interval on bissectrice defined by parameters
1261// test if the medium point is at a distance > offset
1262// in this case vertices corresponding to the extremities of the interval
1263// are ranked in the proofing.
1264// => If the same vertex appears in the proofing, the
1265// border of the zone of proximity is tangent to the offset .
1266//=======================================================================
1267
1268void BRepFill_OffsetWire::UpdateDetromp (BRepFill_DataMapOfOrientedShapeListOfShape& Detromp,
1269 const TopoDS_Shape& Shape1,
1270 const TopoDS_Shape& Shape2,
1271 const TopTools_SequenceOfShape& Vertices,
1272 const TColgp_SequenceOfPnt& Params,
1273 const Bisector_Bisec& Bisec,
1274 const Standard_Boolean SOnE,
1275 const Standard_Boolean EOnE,
1276 const BRepFill_TrimEdgeTool& Trim) const
1277{
c19dd45e 1278 Standard_Integer ii = 1;
c19dd45e 1279
46767247 1280 if (myJoinType == GeomAbs_Intersection)
1281 {
1282 for (; ii <= Vertices.Length(); ii++)
1283 {
1284 const TopoDS_Vertex& aVertex = TopoDS::Vertex(Vertices.Value(ii));
1285 Detromp(Shape1).Append(aVertex);
1286 Detromp(Shape2).Append(aVertex);
c19dd45e 1287 }
c19dd45e 1288 }
46767247 1289 else //myJoinType == GeomAbs_Arc
1290 {
1291 Standard_Real U1,U2;
1292 TopoDS_Vertex V1,V2;
1293
1294 const Handle(Geom2d_Curve)& Bis = Bisec.Value();
c65f8201 1295 Standard_Boolean ForceAdd = Standard_False;
1296 Handle(Geom2d_TrimmedCurve) aTC = Handle(Geom2d_TrimmedCurve)::DownCast(Bis);
1297 if(!aTC.IsNull() && aTC->BasisCurve()->IsPeriodic())
1298 {
1299 gp_Pnt2d Pf = Bis->Value(Bis->FirstParameter());
1300 gp_Pnt2d Pl = Bis->Value(Bis->LastParameter());
1301 ForceAdd = Pf.Distance(Pl) <= Precision::Confusion();
1302 }
1303
46767247 1304 U1 = Bis->FirstParameter();
1305
1306 if (SOnE) {
1307 // the first point of the bissectrice is on the offset
1308 V1 = TopoDS::Vertex(Vertices.Value(ii));
1309 ii++;
1310 }
1311
1312 while (ii <= Vertices.Length()) {
1313 U2 = Params.Value(ii).X();
1314 V2 = TopoDS::Vertex(Vertices.Value(ii));
1315
1316 gp_Pnt2d P = Bis->Value((U2 + U1)*0.5);
c65f8201 1317 if (!Trim.IsInside(P) || ForceAdd) {
873c119f 1318 if (!V1.IsNull()) {
1319 Detromp(Shape1).Append(V1);
1320 Detromp(Shape2).Append(V1);
1321 }
46767247 1322 Detromp(Shape1).Append(V2);
1323 Detromp(Shape2).Append(V2);
c19dd45e 1324 }
46767247 1325 U1 = U2;
1326 V1 = V2;
1327 ii ++;
c19dd45e 1328 }
46767247 1329
1330 // test medium point between the last parameter and the end of the bissectrice.
1331 U2 = Bis->LastParameter();
1332 if (!EOnE) {
1333 if (!Precision::IsInfinite(U2)) {
1334 gp_Pnt2d P = Bis->Value((U2 + U1)*0.5);
c65f8201 1335 if (!Trim.IsInside(P) || ForceAdd) {
46767247 1336 if (!V1.IsNull()) {
1337 Detromp(Shape1).Append(V1);
1338 Detromp(Shape2).Append(V1);
1339 }
1340 }
c19dd45e 1341 }
46767247 1342 else {
1343 if (!V1.IsNull()) {
1344 Detromp(Shape1).Append(V1);
1345 Detromp(Shape2).Append(V1);
1346 }
1347 }
1348 }
1349 //else if(myJoinType != GeomAbs_Arc)
1350 //{
1351 // if (!V1.IsNull()) {
1352 // Detromp(Shape1).Append(V1);
1353 // Detromp(Shape2).Append(V1);
1354 // }
1355 //}
1356 } //end of else (myJoinType==GeomAbs_Arc)
c19dd45e 1357}
1358
1359//=======================================================================
7fd59977 1360//function : MakeWires
1361//purpose :
1362//=======================================================================
1363
1364void BRepFill_OffsetWire::MakeWires()
1365{
1366 //--------------------------------------------------------
0d969553 1367 // creation of a single list of created parallel edges.
7fd59977 1368 //--------------------------------------------------------
1369 TopTools_SequenceOfShape TheEdges;
1370 TopTools_ListOfShape TheWires;
1371 TopTools_ListIteratorOfListOfShape itl;
86b3805f
J
1372 //BRepFill_DataMapIteratorOfDataMapOfOrientedShapeListOfShape ite;
1373 TopTools_IndexedDataMapOfShapeListOfShape MVE;
1374 //TopTools_DataMapIteratorOfDataMapOfShapeListOfShape MVEit;
7fd59977 1375 TopoDS_Vertex V1,V2,VF,CV;
86b3805f 1376 Standard_Integer i;
7fd59977 1377
86b3805f
J
1378 for (i = 1; i <= myMap.Extent(); i++) {
1379 for (itl.Initialize(myMap(i)); itl.More(); itl.Next()) {
7fd59977 1380 const TopoDS_Edge& E = TopoDS::Edge(itl.Value());
1381 TopExp::Vertices (E,V1,V2);
1382 if (V1.IsSame(V2) && IsSmallClosedEdge(E, V1))
873c119f 1383 continue; //remove small closed edges
7642e2d0 1384 if (!CheckSmallParamOnEdge(E))
1385 continue;
86b3805f 1386 if (!MVE.Contains(V1)) {
873c119f 1387 TopTools_ListOfShape empty;
1388 MVE.Add(V1,empty);
7fd59977 1389 }
86b3805f
J
1390 MVE.ChangeFromKey(V1).Append(E);
1391 if (!MVE.Contains(V2)) {
873c119f 1392 TopTools_ListOfShape empty;
1393 MVE.Add(V2,empty);
7fd59977 1394 }
86b3805f 1395 MVE.ChangeFromKey(V2).Append(E);
7fd59977 1396 }
1397 }
1398
1399 //--------------------------------------
0d969553 1400 // Creation of parallel wires.
7fd59977 1401 //--------------------------------------
1402 BRep_Builder B;
1403
873c119f 1404
1405 // Standard_Integer NbEdges;
1406 // Standard_Boolean NewWire = Standard_True;
1407 // Standard_Boolean AddEdge = Standard_False;
7fd59977 1408
1409 TopoDS_Wire NW;
1410 Standard_Boolean End;
1411 TopoDS_Edge CE;
1412
1413 while (!MVE.IsEmpty()) {
1414 B.MakeWire(NW);
1415
86b3805f
J
1416 //MVEit.Initialize(MVE);
1417 for (i = 1; i <= MVE.Extent(); i++)
1418 if(MVE(i).Extent() == 1)
1419 break;
7fd59977 1420
86b3805f
J
1421 //if(!MVEit.More()) MVEit.Initialize(MVE);
1422 if (i > MVE.Extent())
1423 i = 1;
7fd59977 1424
86b3805f
J
1425 CV = VF = TopoDS::Vertex(MVE.FindKey(i));
1426 CE = TopoDS::Edge(MVE(i).First());
7fd59977 1427 End = Standard_False;
1867dc32 1428 MVE.ChangeFromKey(CV).RemoveFirst();
1429
1430 if (myIsOpenResult && MVE.FindFromKey(CV).IsEmpty())
1431 {
3f5aa017 1432 MVE.RemoveKey(CV);
1867dc32 1433 }
1434
7fd59977 1435
873c119f 1436 // Modified by Sergey KHROMOV - Thu Mar 14 11:29:59 2002 Begin
7fd59977 1437 Standard_Boolean isClosed = Standard_False;
873c119f 1438
1439 // Modified by Sergey KHROMOV - Thu Mar 14 11:30:00 2002 End
7fd59977 1440
1441 while(!End) {
1442 //-------------------------------
0d969553 1443 // Construction of a wire.
7fd59977 1444 //-------------------------------
1445 TopExp::Vertices(CE,V1,V2);
1446 if (!CV.IsSame(V1)) CV = V1; else CV = V2;
1447
1448 B.Add (NW,CE);
1449
86b3805f 1450 if (VF.IsSame(CV) || !MVE.Contains(CV)) {
873c119f 1451 // Modified by Sergey KHROMOV - Thu Mar 14 11:30:14 2002 Begin
1452 isClosed = VF.IsSame(CV);
1453 // Modified by Sergey KHROMOV - Thu Mar 14 11:30:15 2002 End
1454 End = Standard_True;
3f5aa017 1455 MVE.RemoveKey(VF);
7fd59977 1456 }
1457
1458 if (!End) {
873c119f 1459 if (MVE.FindFromKey(CV).Extent() > 2) {
1460 //cout <<"vertex on more that 2 edges in a face."<<endl;
1461 }
1462 for ( itl.Initialize(MVE.FindFromKey(CV)); itl.More(); itl.Next()) {
1463 if (itl.Value().IsSame(CE)) {
1464 MVE.ChangeFromKey(CV).Remove(itl);
1465 break;
1466 }
1467 }
1468 if (!MVE.FindFromKey(CV).IsEmpty()) {
1469 CE = TopoDS::Edge(MVE.FindFromKey(CV).First());
1470 MVE.ChangeFromKey(CV).RemoveFirst();
1471 }
1867dc32 1472 else if (myIsOpenResult)//CV was a vertex with one edge
1473 End = Standard_True;
1474
873c119f 1475 if (MVE.FindFromKey(CV).IsEmpty())
86b3805f 1476 {
3f5aa017 1477 MVE.RemoveKey(CV);
86b3805f 1478 }
7fd59977 1479 }
1480 }
873c119f 1481 // Modified by Sergey KHROMOV - Thu Mar 14 11:29:31 2002 Begin
1482 // NW.Closed(Standard_True);
7fd59977 1483 NW.Closed(isClosed);
873c119f 1484 // Modified by Sergey KHROMOV - Thu Mar 14 11:29:37 2002 End
7fd59977 1485 TheWires.Append(NW);
1486 }
1487
0d969553
Y
1488 // update myShape :
1489 // -- if only one wire : myShape is a Wire
1490 // -- if several wires : myShape is a Compound.
7fd59977 1491 if ( TheWires.Extent() == 1) {
1492 myShape = TheWires.First();
1493 }
1494 else {
1495 TopoDS_Compound R;
1496 B.MakeCompound(R);
1497 TopTools_ListIteratorOfListOfShape it(TheWires);
1498 for ( ; it.More(); it.Next()) {
1499 B.Add(R, it.Value());
1500 }
1501 myShape = R;
1502 }
1503
1504}
1505
1506//=======================================================================
1507//function : FixHoles
1508//purpose :
1509//=======================================================================
1510
1511void BRepFill_OffsetWire::FixHoles()
1512{
1513 TopTools_SequenceOfShape ClosedWires, UnclosedWires, IsolatedWires;
1514
1515 Standard_Real MaxTol = 0.;
7fd59977 1516 BRep_Builder BB;
1517
1518 TopExp_Explorer Explo( mySpine, TopAbs_VERTEX );
1519 for (; Explo.More(); Explo.Next())
91bb31f3 1520 {
1521 const TopoDS_Vertex& aVertex = TopoDS::Vertex( Explo.Current() );
1522 Standard_Real Tol = BRep_Tool::Tolerance(aVertex);
1523 if (Tol > MaxTol)
1524 MaxTol = Tol;
1525 }
7fd59977 1526 MaxTol *= 100.;
1527
1528 Explo.Init( myShape, TopAbs_WIRE );
1529 for (; Explo.More(); Explo.Next())
91bb31f3 1530 {
1531 TopoDS_Shape aWire = Explo.Current();
1532 // Remove duplicated edges
1533 TopTools_DataMapOfShapeListOfShape EEmap;
1534 TopoDS_Iterator it( aWire );
1535 for (; it.More(); it.Next())
7fd59977 1536 {
91bb31f3 1537 const TopoDS_Shape& anEdge = it.Value();
1538 if (! EEmap.IsBound( anEdge ))
1539 {
1540 TopTools_ListOfShape LE;
1541 EEmap.Bind( anEdge, LE );
1542 }
7fd59977 1543 else
91bb31f3 1544 EEmap(anEdge).Append( anEdge );
7fd59977 1545 }
91bb31f3 1546 aWire.Free( Standard_True );
1547 TopTools_DataMapIteratorOfDataMapOfShapeListOfShape mapit( EEmap );
1548 for (; mapit.More(); mapit.Next())
1549 {
1550 const TopTools_ListOfShape& LE = mapit.Value();
1551 TopTools_ListIteratorOfListOfShape itl( LE );
1552 for (; itl.More(); itl.Next())
1553 BB.Remove( aWire, itl.Value() );
1554 }
1555 // Sorting
1556 if (aWire.Closed())
1557 ClosedWires.Append( aWire );
1558 else
1559 UnclosedWires.Append( aWire );
1560 }
1561
7fd59977 1562 while (!UnclosedWires.IsEmpty())
91bb31f3 1563 {
1564 TopoDS_Wire& Base = TopoDS::Wire( UnclosedWires(1) );
1565 TopoDS_Vertex Vf, Vl;
1566 TopExp::Vertices( Base, Vf, Vl );
1567 if(Vf.IsNull() || Vl.IsNull())
873c119f 1568 {
9775fa61 1569 throw Standard_Failure("BRepFill_OffsetWire::FixHoles(): Wrong wire.");
873c119f 1570 }
91bb31f3 1571 gp_Pnt Pf, Pl;
1572 Pf = BRep_Tool::Pnt(Vf);
1573 Pl = BRep_Tool::Pnt(Vl);
1574 Standard_Real DistF = RealLast(), DistL = RealLast();
1232a766 1575 Standard_Integer IndexF = 0, IndexL = 0;
91bb31f3 1576 Standard_Boolean IsFirstF = Standard_False, IsFirstL = Standard_False;
1577 for (Standard_Integer i = 2; i <= UnclosedWires.Length(); i++)
7fd59977 1578 {
91bb31f3 1579 TopoDS_Wire aWire = TopoDS::Wire( UnclosedWires(i) );
1580 TopoDS_Vertex V1, V2;
1581 TopExp::Vertices( aWire, V1, V2 );
1582
1583 if(V1.IsNull() || V2.IsNull())
873c119f 1584 {
9775fa61 1585 throw Standard_Failure("BRepFill_OffsetWire::FixHoles(): Wrong wire.");
873c119f 1586 }
91bb31f3 1587
1588 gp_Pnt P1, P2;
1589 P1 = BRep_Tool::Pnt(V1);
1590 P2 = BRep_Tool::Pnt(V2);
1591 Standard_Real dist = Pf.Distance( P1 );
1592 if (dist < DistF)
1593 {
1594 DistF = dist;
1595 IndexF = i;
1596 IsFirstF = Standard_True;
1597 }
1598 dist = Pf.Distance( P2 );
1599 if (dist < DistF)
1600 {
1601 DistF = dist;
1602 IndexF = i;
1603 IsFirstF = Standard_False;
1604 }
1605 dist = Pl.Distance( P1 );
1606 if (dist < DistL)
1607 {
1608 DistL = dist;
1609 IndexL = i;
1610 IsFirstL = Standard_True;
1611 }
1612 dist = Pl.Distance( P2 );
1613 if (dist < DistL)
1614 {
1615 DistL = dist;
1616 IndexL = i;
1617 IsFirstL = Standard_False;
1618 }
1619 }
1232a766 1620 if (DistF > MaxTol)
1621 IndexF = 0;
1622 if (DistL > MaxTol)
1623 IndexL = 0;
91bb31f3 1624 TopoDS_Wire theWire;
1625 TopoDS_Edge theEdge;
1626 TopoDS_Vertex theVertex;
1627 Standard_Real CommonTol;
1628 Standard_Boolean TryToClose = Standard_True;
1629 if (DistF <= MaxTol && DistL <= MaxTol && IndexF == IndexL && IsFirstF == IsFirstL)
1630 {
1631 if (DistF < DistL)
1632 {
1633 DistL = RealLast();
1634 IndexL++;
1635 }
1636 else
1637 {
1638 DistF = RealLast();
1639 IndexF++;
1640 }
1641 TryToClose = Standard_False;
1642 }
1643 if (DistF <= MaxTol)
1644 {
1645 theWire = TopoDS::Wire( UnclosedWires(IndexF) );
1646 TopoDS_Vertex V1, V2;
1647 TopExp::Vertices( theWire, V1, V2 );
1648 TopTools_IndexedDataMapOfShapeListOfShape VEmap;
1649 TopExp::MapShapesAndAncestors( theWire, TopAbs_VERTEX, TopAbs_EDGE, VEmap );
1650 theEdge = (IsFirstF)? TopoDS::Edge(VEmap.FindFromKey( V1 ).First()) :
1651 TopoDS::Edge(VEmap.FindFromKey( V2 ).First());
1652 TopoDS_Iterator it( theWire );
1653 for (; it.More(); it.Next())
1654 {
1655 TopoDS_Edge anEdge = TopoDS::Edge( it.Value() );
1656 if (IsFirstF) anEdge.Reverse();
1657 if (!anEdge.IsSame( theEdge ))
1658 BB.Add( Base, anEdge );
1659 }
1660 theVertex = (IsFirstF)? V1 : V2;
1661 CommonTol = Max( BRep_Tool::Tolerance(Vf), BRep_Tool::Tolerance(theVertex) );
1662 if (DistF <= CommonTol)
1663 {
1664 theEdge.Free( Standard_True );
1665 Vf.Orientation( theVertex.Orientation() );
1666 BB.Remove( theEdge, theVertex );
1667 BB.Add( theEdge, Vf );
1668 BB.UpdateVertex( Vf, CommonTol );
1669 if (IsFirstF) theEdge.Reverse();
1670 BB.Add( Base, theEdge );
1671 }
1672 else
1673 {
1674 if (IsFirstF) theEdge.Reverse();
1675 BB.Add( Base, theEdge );
1676 // Creating new edge from theVertex to Vf
1677 TopoDS_Edge NewEdge = BRepLib_MakeEdge( theVertex, Vf );
1678 BB.Add( Base, NewEdge );
1679 }
1680 }
1681 if (DistL <= MaxTol && IndexL != IndexF)
1682 {
1683 theWire = TopoDS::Wire( UnclosedWires(IndexL) );
1684 TopoDS_Vertex V1, V2;
1685 TopExp::Vertices( theWire, V1, V2 );
1686 TopTools_IndexedDataMapOfShapeListOfShape VEmap;
1687 TopExp::MapShapesAndAncestors( theWire, TopAbs_VERTEX, TopAbs_EDGE, VEmap );
1688 theEdge = (IsFirstL)? TopoDS::Edge(VEmap.FindFromKey( V1 ).First()) :
1689 TopoDS::Edge(VEmap.FindFromKey( V2 ).First());
1690 TopoDS_Iterator it( theWire );
1691 for (; it.More(); it.Next())
1692 {
1693 TopoDS_Edge anEdge = TopoDS::Edge( it.Value() );
1694 if (!IsFirstL) anEdge.Reverse();
1695 if (!anEdge.IsSame( theEdge ))
1696 BB.Add( Base, anEdge );
1697 }
1698 theVertex = (IsFirstL)? V1 : V2;
1699 CommonTol = Max( BRep_Tool::Tolerance(Vl), BRep_Tool::Tolerance(theVertex) );
1700 if (DistL <= CommonTol)
1701 {
1702 theEdge.Free( Standard_True );
1703 Vl.Orientation( theVertex.Orientation() );
1704 BB.Remove( theEdge, theVertex );
1705 BB.Add( theEdge, Vl );
1706 BB.UpdateVertex( Vl, CommonTol );
1707 if (!IsFirstL) theEdge.Reverse();
1708 BB.Add( Base, theEdge );
1709 }
1710 else
1711 {
1712 if (!IsFirstL) theEdge.Reverse();
1713 BB.Add( Base, theEdge );
1714 // Creating new edge from Vl to theVertex
1715 TopoDS_Edge NewEdge = BRepLib_MakeEdge( Vl, theVertex );
1716 BB.Add( Base, NewEdge );
1717 }
1718 }
1719 // Check if it is possible to close resulting wire
1720 if (TryToClose)
1721 {
1722 TopExp::Vertices( Base, Vf, Vl );
1723 CommonTol = Max( BRep_Tool::Tolerance(Vf), BRep_Tool::Tolerance(Vl) );
1724 TopTools_IndexedDataMapOfShapeListOfShape VEmap;
1725 TopExp::MapShapesAndAncestors( Base, TopAbs_VERTEX, TopAbs_EDGE, VEmap );
1726 TopoDS_Edge Efirst, Elast;
1727 Efirst = TopoDS::Edge(VEmap.FindFromKey( Vf ).First());
1728 Elast = TopoDS::Edge(VEmap.FindFromKey( Vl ).First());
7fd59977 1729 Pf = BRep_Tool::Pnt(Vf);
1730 Pl = BRep_Tool::Pnt(Vl);
91bb31f3 1731 Standard_Real Dist = Pf.Distance(Pl);
1732 if (Dist <= CommonTol)
1733 {
1734 Elast.Free( Standard_True );
1735 Vf.Orientation( Vl.Orientation() );
1736 BB.Remove( Elast, Vl );
1737 BB.Add( Elast, Vf );
1738 BB.UpdateVertex( Vf, CommonTol );
1739 Base.Closed( Standard_True );
1740 }
1741 else if (Dist <= MaxTol)
1742 {
1743 // Creating new edge from Vl to Vf
1744 TopoDS_Edge NewEdge = BRepLib_MakeEdge( Vf, Vl );
1745 BB.Add( Base, NewEdge );
1746 Base.Closed( Standard_True );
1747 }
7fd59977 1748 }
91bb31f3 1749 // Updating sequences ClosedWires and UnclosedWires
1750 if (DistF <= MaxTol)
1751 UnclosedWires.Remove( IndexF );
1752 if (DistL <= MaxTol && IndexL != IndexF)
7fd59977 1753 {
91bb31f3 1754 if (DistF <= MaxTol && IndexL > IndexF)
1755 IndexL--;
1756 UnclosedWires.Remove( IndexL );
7fd59977 1757 }
91bb31f3 1758 if (Base.Closed())
7fd59977 1759 {
91bb31f3 1760 ClosedWires.Append( Base );
1761 UnclosedWires.Remove( 1 );
7fd59977 1762 }
91bb31f3 1763 else if (DistF > MaxTol && DistL > MaxTol)
1764 {
1765 IsolatedWires.Append( Base );
1766 UnclosedWires.Remove( 1 );
1767 }
1768 }
1769
1770 // Updating myShape
1771 if (ClosedWires.Length() + IsolatedWires.Length() == 1)
1772 {
1773 if (!ClosedWires.IsEmpty())
1774 myShape = ClosedWires.First();
1775 else
1776 myShape = IsolatedWires.First();
1777 }
1778 else
1779 {
1780 TopoDS_Compound R;
1781 BB.MakeCompound( R );
51740958 1782 Standard_Integer i;
91bb31f3 1783 for (i = 1; i <= ClosedWires.Length(); i++)
1784 BB.Add( R, ClosedWires(i) );
1785 for (i = 1; i <= IsolatedWires.Length(); i++)
1786 BB.Add( R, IsolatedWires(i) );
1787 myShape = R;
1788 }
7fd59977 1789}
1790
1791//=======================================================================
1792//function : CutEdge
0d969553
Y
1793//purpose : Cut edge at the extrema of curvatures and points of inflexion.
1794// So, closed circles are cut in two.
1795// If <Cuts> is empty, the edge is not modified.
1796// The first and the last vertex of the initial edge
1797// belong to the first and the last parts respectively.
7fd59977 1798//=======================================================================
1799Standard_Integer CutEdge (const TopoDS_Edge& E,
873c119f 1800 const TopoDS_Face& F,
1801 Standard_Integer ForceCut,
1802 TopTools_ListOfShape& Cuts)
7fd59977 1803{
1804 Cuts.Clear();
1805 MAT2d_CutCurve Cuter;
1806 TColGeom2d_SequenceOfCurve theCurves;
1807 Standard_Real f,l;
1808 Handle(Geom2d_Curve) C2d;
1809 Handle(Geom2d_TrimmedCurve) CT2d;
873c119f 1810 // Modified by Sergey KHROMOV - Wed Mar 6 17:36:25 2002 Begin
7fd59977 1811 Standard_Real aTol = BRep_Tool::Tolerance(E);
1812 Handle(Geom_Curve) aC;
873c119f 1813 // Modified by Sergey KHROMOV - Wed Mar 6 17:36:25 2002 End
7fd59977 1814
1815 TopoDS_Vertex V1,V2,VF,VL;
1816 TopExp::Vertices (E,V1,V2);
1817 BRep_Builder B;
1818
1819 C2d = BRep_Tool::CurveOnSurface (E,F,f,l);
873c119f 1820 // Modified by Sergey KHROMOV - Wed Mar 6 17:36:54 2002 Begin
7fd59977 1821 aC = BRep_Tool::Curve(E,f,l);
873c119f 1822 // Modified by Sergey KHROMOV - Wed Mar 6 17:36:54 2002 End
7fd59977 1823 CT2d = new Geom2d_TrimmedCurve(C2d,f,l);
1824 //if (E.Orientation() == TopAbs_REVERSED) CT2d->Reverse();
1825
1826 if (CT2d->BasisCurve()->IsKind(STANDARD_TYPE(Geom2d_Circle)) &&
873c119f 1827 ( Abs(f-l) >= M_PI) ) {
1828 return 0;
7fd59977 1829 }
1830
1831 //-------------------------
0d969553 1832 // Cut curve.
7fd59977 1833 //-------------------------
1834 Cuter.Perform(CT2d);
1835
873c119f 1836 // Modified by Sergey KHROMOV - Thu Nov 16 17:28:29 2000 Begin
7fd59977 1837 if (ForceCut == 0) {
1838 if (Cuter.UnModified()) {
873c119f 1839 //-----------------------------
1840 // edge not modified => return.
1841 //-----------------------------
7fd59977 1842 return 0;
1843 } else {
1844 for (Standard_Integer k = 1; k <= Cuter.NbCurves(); k++)
873c119f 1845 theCurves.Append(Cuter.Value(k));
7fd59977 1846 }
1847 } else if (ForceCut == 1) {
1848 if (Cuter.UnModified()) {
1849 CutCurve (CT2d, 2, theCurves);
1850 } else {
1851 for (Standard_Integer k = 1; k <= Cuter.NbCurves(); k++)
873c119f 1852
1853 theCurves.Append(Cuter.Value(k));
7fd59977 1854 }
1855 } else if (ForceCut == 2) {
1856 if (Cuter.UnModified()) {
1857 CutCurve (CT2d, 3, theCurves);
1858 } else {
1859 if (Cuter.NbCurves() == 2) {
873c119f 1860
1861 Handle(Geom2d_TrimmedCurve)CC = Cuter.Value(1);
1862
1863 if (CC->LastParameter() > (l+f)/2.) {
1864 CutCurve (CC, 2, theCurves);
1865 theCurves.Append(Cuter.Value(2));
1866 } else {
1867 theCurves.Append(CC);
1868 CutCurve (Cuter.Value(2), 2, theCurves);
1869 }
7fd59977 1870 } else {
873c119f 1871 for (Standard_Integer k = 1; k <= Cuter.NbCurves(); k++)
1872 theCurves.Append(Cuter.Value(k));
7fd59977 1873 }
1874 }
1875 }
873c119f 1876
1877 // Modified by Sergey KHROMOV - Thu Nov 16 17:28:37 2000 End
7fd59977 1878
1879 //--------------------------------------
0d969553 1880 // Creation of cut edges.
7fd59977 1881 //--------------------------------------
1882 VF = V1;
1883
1884 for (Standard_Integer k = 1; k <= theCurves.Length(); k++) {
1885
1886 Handle(Geom2d_TrimmedCurve)CC = Handle(Geom2d_TrimmedCurve)::DownCast(theCurves.Value(k));
1887
1888 if (k == theCurves.Length()) {VL = V2;}
1889 else {
873c119f 1890 // Modified by Sergey KHROMOV - Wed Mar 6 17:38:02 2002 Begin
7fd59977 1891 gp_Pnt P = aC->Value(CC->LastParameter());
1892
1893 VL = BRepLib_MakeVertex(P);
1894 B.UpdateVertex(VL, aTol);
873c119f 1895 // Modified by Sergey KHROMOV - Wed Mar 6 17:38:05 2002 End
7fd59977 1896 }
1897 TopoDS_Shape aLocalShape = E.EmptyCopied();
1898 TopoDS_Edge NE = TopoDS::Edge(aLocalShape);
873c119f 1899 // TopoDS_Edge NE = TopoDS::Edge(E.EmptyCopied());
7fd59977 1900 NE.Orientation(TopAbs_FORWARD);
1901 B.Add (NE,VF.Oriented(TopAbs_FORWARD));
1902 B.Add (NE,VL.Oriented(TopAbs_REVERSED));
1903 B.Range(NE,CC->FirstParameter(),CC->LastParameter());
1904 Cuts.Append(NE.Oriented(E.Orientation()));
1905 VF = VL;
1906 }
1907
1908 return theCurves.Length();
1909}
1910
1911// Modified by Sergey KHROMOV - Thu Nov 16 17:27:56 2000 Begin
1912//=======================================================================
1913//function : CutCurve
1914//purpose :
1915//=======================================================================
1916
1917void CutCurve (const Handle(Geom2d_TrimmedCurve)& C,
873c119f 1918 const Standard_Integer nbParts,
1919 TColGeom2d_SequenceOfCurve& theCurves)
7fd59977 1920{
1921 Handle(Geom2d_TrimmedCurve) TrimC;
1922 Standard_Real UF,UL,UC;
1923 Standard_Real Step;
1924 gp_Pnt2d PF,PL,PC;
1925 Standard_Real PTol = Precision::PConfusion()*10;
1926 Standard_Real Tol = Precision::Confusion()*10;
1927 Standard_Boolean YaCut = Standard_False;
1928
1929 UF = C->FirstParameter();
1930 UL = C->LastParameter ();
1931 PF = C->Value(UF);
1932 PL = C->Value(UL);
1933
1934 Step = (UL - UF)/nbParts;
1935
1936 for (Standard_Integer i = 1; i < nbParts; i++) {
1937
1938 UC = UF + i*Step;
1939 PC = C->Value(UC);
1940
1941 if (UC - UF > PTol && PC.Distance(PF) > Tol) {
1942 if ( UL - UC < PTol || PL.Distance(PC) < Tol)
873c119f 1943 continue;
7fd59977 1944
1945 TrimC = new Geom2d_TrimmedCurve(C,UF,UC);
1946 theCurves.Append(TrimC);
1947 UF = UC;
1948 PF = PC;
1949 YaCut = Standard_True;
1950 }
1951 }
1952 if (YaCut) {
1953 TrimC = new Geom2d_TrimmedCurve(C,UF,UL);
1954 theCurves.Append(TrimC);
1955 } else
1956 theCurves.Append(C);
1957}
1958// Modified by Sergey KHROMOV - Thu Nov 16 17:28:13 2000 End
1959
1960//=======================================================================
1961//function : MakeCircle
1962//purpose :
1963//=======================================================================
1964
1965void MakeCircle (const TopoDS_Edge& E,
873c119f 1966 const TopoDS_Vertex& V,
1967 const TopoDS_Face& F,
1968 const Standard_Real Offset,
1969 BRepFill_IndexedDataMapOfOrientedShapeListOfShape& Map,
1970 const Handle(Geom_Plane)& RefPlane)
7fd59977 1971{
0d969553 1972 // evaluate the Axis of the Circle.
7fd59977 1973 Standard_Real f,l;
1974 Handle(Geom2d_Curve) GC = BRep_Tool::CurveOnSurface(E,F,f,l);
1975 gp_Vec2d DX;
1976 gp_Pnt2d P;
1977
1978 if (E.Orientation() == TopAbs_FORWARD) {
1979 GC->D1(l,P,DX);
1980 DX.Reverse();
1981 }
1982 else GC->D1(f,P,DX);
1983
1984 gp_Ax2d Axis(P,gp_Dir2d(DX));
1985 Handle(Geom2d_Circle) Circ
1986 = new Geom2d_Circle(Axis, Abs(Offset), Offset < 0.);
1987
1988 // Bind the edges in my Map.
1989 TopoDS_Edge OE = BRepLib_MakeEdge(Circ, RefPlane);
1990 TopTools_ListOfShape LL;
1991
1992 LL.Append(OE);
86b3805f 1993 Map.Add(V,LL);
7fd59977 1994
873c119f 1995#ifdef OCCT_DEBUG
7fd59977 1996#ifdef DRAW
1997 if ( AffichGeom && !OE.IsNull()) {
1896126e 1998 char name[256];
7fd59977 1999 sprintf(name,"OFFSET_%d",++NbOFFSET);
2000 DBRep::Set(name,OE);
2001 }
2002#endif
873c119f 2003#endif
7fd59977 2004}
2005
2006//=======================================================================
2007//function : MakeOffset
2008//purpose :
2009//=======================================================================
2010
2011void MakeOffset (const TopoDS_Edge& E,
873c119f 2012 const TopoDS_Face& F,
2013 const Standard_Real Offset,
2014 BRepFill_IndexedDataMapOfOrientedShapeListOfShape& Map,
2015 const Handle(Geom_Plane)& RefPlane,
2016 const Standard_Boolean IsOpenResult,
c7806cb6 2017 const GeomAbs_JoinType theJoinType,
873c119f 2018 const TopoDS_Vertex * Ends)
7fd59977 2019{
2020 Standard_Real f,l;
2021 Standard_Real anOffset = Offset;
2022
2023 if (E.Orientation() == TopAbs_REVERSED) anOffset *= -1;
2024
2025 Handle(Geom2d_Curve) G2d = BRep_Tool::CurveOnSurface(E,F,f,l);
2026 Handle(Geom2d_Curve) G2dOC;
2027
6a442250 2028 Standard_Boolean ToExtendFirstPar = Standard_True;
2029 Standard_Boolean ToExtendLastPar = Standard_True;
2030 if (IsOpenResult)
2031 {
2032 TopoDS_Vertex V1, V2;
2033 TopExp::Vertices(E, V1, V2);
2034 if (V1.IsSame(Ends[0]) ||
873c119f 2035 V1.IsSame(Ends[1]))
6a442250 2036 ToExtendFirstPar = Standard_False;
2037 if (V2.IsSame(Ends[0]) ||
873c119f 2038 V2.IsSame(Ends[1]))
6a442250 2039 ToExtendLastPar = Standard_False;
2040 }
2041
7fd59977 2042 Geom2dAdaptor_Curve AC(G2d,f,l);
2043 if ( AC.GetType() == GeomAbs_Circle) {
0d969553
Y
2044 // if the offset is greater otr equal to the radius and the side of the
2045 // concavity of the circle => edge null.
7fd59977 2046 gp_Circ2d C1(AC.Circle());
7fd59977 2047 gp_Ax22d axes( C1.Axis());
2048 gp_Dir2d Xd = axes.XDirection();
2049 gp_Dir2d Yd = axes.YDirection();
2050 Standard_Real Crossed = Xd.X()*Yd.Y()-Xd.Y()*Yd.X();
2051 Standard_Real Signe = ( Crossed > 0.) ? 1. : -1.;
2052
9ffd84ad 2053 if (anOffset*Signe < AC.Circle().Radius() - Precision::Confusion()) {
0d969553 2054
7fd59977 2055 Handle(Geom2dAdaptor_HCurve) AHC =
873c119f 2056 new Geom2dAdaptor_HCurve(G2d);
5201d3e6 2057 Adaptor2d_OffsetCurve Off(AHC,-anOffset);
7fd59977 2058 Handle(Geom2d_Circle) CC = new Geom2d_Circle(Off.Circle());
2059
c6541a0c 2060 Standard_Real Delta = 2*M_PI - l + f;
c7806cb6 2061 if (theJoinType == GeomAbs_Arc)
2062 {
873c119f 2063 if (ToExtendFirstPar)
2064 f -= 0.2*Delta;
2065 if (ToExtendLastPar)
2066 l += 0.2*Delta;
c7806cb6 2067 }
2068 else //GeomAbs_Intersection
2069 {
2070 if (ToExtendFirstPar && ToExtendLastPar)
2071 {
2072 Standard_Real old_l = l;
2073 f = old_l + Delta/2.;
2074 l = f + 2*M_PI;
2075 }
2076 else if (ToExtendFirstPar)
2077 {
2078 f = l;
2079 l = f + 2*M_PI;
2080 }
2081 else if (ToExtendLastPar)
2082 {
2083 l = f + 2*M_PI;
2084 }
2085 }
7fd59977 2086 G2dOC = new Geom2d_TrimmedCurve(CC,f,l);
2087 }
2088 }
2089 else if (AC.GetType() == GeomAbs_Line) {
2090 Handle(Geom2dAdaptor_HCurve) AHC =
2091 new Geom2dAdaptor_HCurve(G2d);
5201d3e6 2092 Adaptor2d_OffsetCurve Off(AHC,anOffset);
7fd59977 2093 Handle(Geom2d_Line) CC = new Geom2d_Line(Off.Line());
2094 Standard_Real Delta = (l - f);
6a442250 2095 if (ToExtendFirstPar)
c7806cb6 2096 {
2097 if (theJoinType == GeomAbs_Arc)
873c119f 2098 f -= Delta;
c7806cb6 2099 else //GeomAbs_Intersection
2100 f = -Precision::Infinite();
2101 }
6a442250 2102 if (ToExtendLastPar)
c7806cb6 2103 {
2104 if (theJoinType == GeomAbs_Arc)
873c119f 2105 l += Delta;
c7806cb6 2106 else //GeomAbs_Intersection
2107 l = Precision::Infinite();
2108 }
7fd59977 2109 G2dOC = new Geom2d_TrimmedCurve(CC,f,l);
2110 }
2111 else {
2112
2113 anOffset = -anOffset;
2114 Handle(Geom2d_TrimmedCurve) G2dT = new Geom2d_TrimmedCurve(G2d,f,l);
2115 G2dOC = new Geom2d_OffsetCurve( G2dT, anOffset);
2116
2117 }
2118
2119 // Bind the edges in my Map.
2120 if (!G2dOC.IsNull()) {
2121 TopoDS_Edge OE = BRepLib_MakeEdge(G2dOC, RefPlane);
2122 OE.Orientation(E.Orientation());
2123 TopTools_ListOfShape LL;
2124 LL.Append(OE);
86b3805f 2125 Map.Add(E,LL);
7fd59977 2126
873c119f 2127#ifdef OCCT_DEBUG
7fd59977 2128#ifdef DRAW
2129 if (AffichGeom && !OE.IsNull()) {
1896126e 2130 char name[256];
7fd59977 2131 sprintf(name,"OFFSET_%d",++NbOFFSET);
2132 DBRep::Set(name,OE);
873c119f 2133 //Standard_Real ii = 0;
7fd59977 2134 }
2135#endif
873c119f 2136#endif
2137
7fd59977 2138 }
2139}
2140
7fd59977 2141
2142//=======================================================================
2143//function : VertexFromNode
2144//purpose :
2145//=======================================================================
2146
2147Standard_Boolean VertexFromNode (const Handle(MAT_Node)& aNode,
873c119f 2148 const Standard_Real Offset,
2149 gp_Pnt2d& PN,
2150 BRepFill_DataMapOfNodeShape& MapNodeVertex,
2151 TopoDS_Vertex& VN)
7fd59977 2152{
2153 Standard_Boolean Status;
2154 Standard_Real Tol = Precision::Confusion();
2155 BRep_Builder B;
2156
2157 if (!aNode->Infinite() && Abs(aNode->Distance()-Offset) < Tol) {
2158 //------------------------------------------------
0d969553 2159 // the Node gives a vertex on the offset
7fd59977 2160 //------------------------------------------------
2161 if (MapNodeVertex.IsBound(aNode)) {
2162 VN = TopoDS::Vertex(MapNodeVertex(aNode));
2163 }
2164 else {
2165 gp_Pnt P(PN.X(),PN.Y(),0.);
2166 B.MakeVertex (VN);
2167 B.UpdateVertex(VN,P, Precision::Confusion());
2168 MapNodeVertex.Bind(aNode,VN);
2169 }
2170 Status = Standard_True;
2171 }
2172 else Status = Standard_False;
2173
2174 return Status;
2175}
2176
2177
2178//=======================================================================
2179//function : StoreInMap
2180//purpose :
2181//=======================================================================
2182
2183void StoreInMap (const TopoDS_Shape& V1,
873c119f 2184 const TopoDS_Shape& V2,
2185 TopTools_IndexedDataMapOfShapeShape& MapVV)
7fd59977 2186{
2187 TopoDS_Shape OldV = V1, NewV = V2;
2188 Standard_Integer i;
2189
2190 if (MapVV.Contains(V2))
2191 NewV = MapVV.FindFromKey(V2);
2192
2193 if (MapVV.Contains(V1))
2194 MapVV.ChangeFromKey(V1) = NewV;
2195
2196 for (i = 1; i <= MapVV.Extent(); i++)
2197 if (MapVV(i).IsSame(V1))
2198 MapVV(i) = NewV;
2199
2200 MapVV.Add(V1, NewV);
2201}
2202
2203//=======================================================================
2204//function : TrimEdge
2205//purpose :
2206//=======================================================================
2207
2208void TrimEdge (const TopoDS_Edge& E,
e11c1bc4 2209 const TopoDS_Shape& ProE,
2210 const TopoDS_Face& AllSpine,
7fd59977 2211 const TopTools_ListOfShape& Detromp,
6a442250 2212 TopTools_SequenceOfShape& TheVer,
2213 TColStd_SequenceOfReal& ThePar,
2214 TopTools_SequenceOfShape& S,
2215 TopTools_IndexedDataMapOfShapeShape& MapVV,
2216 const Standard_Integer IndOfE)
7fd59977 2217{
2218 Standard_Boolean Change = Standard_True;
2219 BRep_Builder TheBuilder;
2220 S.Clear();
2221
2222 //-----------------------------------------------------------
0d969553 2223 // Parse two sequences depending on the parameter on the edge.
7fd59977 2224 //-----------------------------------------------------------
2225 while (Change) {
2226 Change = Standard_False;
2227 for (Standard_Integer i = 1; i < ThePar.Length(); i++) {
2228 if (ThePar.Value(i) > ThePar.Value(i+1)) {
873c119f 2229 ThePar.Exchange(i,i+1);
2230 TheVer.Exchange(i,i+1);
2231 Change = Standard_True;
7fd59977 2232 }
2233 }
2234 }
2235
2236 //----------------------------------------------------------
0d969553 2237 // If a vertex is not in the proofing, it is eliminated.
7fd59977 2238 //----------------------------------------------------------
2239 if (!BRep_Tool::Degenerated(E)) {
2240 for (Standard_Integer k = 1; k <= TheVer.Length(); k ++) {
2241 if ( DoubleOrNotInside (Detromp,
873c119f 2242 TopoDS::Vertex(TheVer.Value(k)))) {
2243 TheVer.Remove(k);
2244 ThePar.Remove(k);
2245 k--;
7fd59977 2246 }
2247 }
2248 }
2249
2250 //----------------------------------------------------------
0d969553
Y
2251 // If a vertex_double appears twice in the proofing
2252 // the vertex is removed.
2253 // otherwise preserve only one of its representations.
7fd59977 2254 //----------------------------------------------------------
2255 if (!BRep_Tool::Degenerated(E)) {
94f71cad 2256 Standard_Real aParTol = 2.0 * Precision::PConfusion();
7fd59977 2257 for (Standard_Integer k = 1; k < TheVer.Length(); k ++) {
2258 if (TheVer.Value(k).IsSame(TheVer.Value(k+1)) ||
94f71cad 2259 Abs(ThePar.Value(k)-ThePar.Value(k+1)) <= aParTol) {
873c119f 2260
2261 if(k+1 == TheVer.Length()) {
2262 StoreInMap(TheVer(k), TheVer(k+1), MapVV);
2263 TheVer.Remove(k);
2264 ThePar.Remove(k);
2265 }
2266 else {
2267 StoreInMap(TheVer(k+1), TheVer(k), MapVV);
2268 TheVer.Remove(k+1);
2269 ThePar.Remove(k+1);
2270 }
2271 /*
2272 if ( DoubleOrNotInside (Detromp,
2273 TopoDS::Vertex(TheVer.Value(k)))) {
2274 TheVer.Remove(k);
2275 ThePar.Remove(k);
2276 k--;
2277 }
2278 */
2279 k--;
7fd59977 2280 }
2281 }
2282 }
2283 //-----------------------------------------------------------
0d969553
Y
2284 // Creation of edges.
2285 // the number of vertices should be even. The created edges
2286 // go from a vertex with uneven index i to vertex i+1;
7fd59977 2287 //-----------------------------------------------------------
6a442250 2288 if (IndOfE == 1 || IndOfE == -1) //open result and extreme edges of result
2289 {
7fd59977 2290 TopoDS_Shape aLocalShape = E.EmptyCopied();
2291 TopoDS_Edge NewEdge = TopoDS::Edge(aLocalShape);
6a442250 2292 TopoDS_Vertex V1, V2;
2293 TopExp::Vertices(E, V1, V2);
2294 Standard_Real fpar, lpar;
e11c1bc4 2295 Handle(Geom_Surface) aPlane;
2296 TopLoc_Location aLoc;
2297 Handle(Geom2d_Curve) PCurve;
2298 BRep_Tool::CurveOnSurface(E, PCurve, aPlane, aLoc, fpar, lpar);
2299 //BRep_Tool::Range(E, fpar, lpar);
2300
2301 Standard_Real TrPar1, TrPar2;
2302 Standard_Boolean ToTrimAsOrigin = IsInnerEdge(ProE, AllSpine, TrPar1, TrPar2);
2303
6a442250 2304 if (IndOfE == 1) //first edge of open wire
2305 {
2306 if (NewEdge.Orientation() == TopAbs_FORWARD)
2307 {
e11c1bc4 2308 if (ToTrimAsOrigin)
2309 {
2310 fpar = TrPar1;
2311 gp_Pnt2d TrPnt2d = PCurve->Value(fpar);
2312 gp_Pnt TrPnt(TrPnt2d.X(), TrPnt2d.Y(), 0.);
2313 V1 = BRepLib_MakeVertex(TrPnt);
2314 }
6a442250 2315 TheBuilder.Add(NewEdge, V1.Oriented(TopAbs_FORWARD));
2316 TheBuilder.Add(NewEdge, TheVer.First().Oriented(TopAbs_REVERSED));
2317 TheBuilder.Range(NewEdge, fpar, ThePar.First());
2318 }
2319 else
2320 {
e11c1bc4 2321 if (ToTrimAsOrigin)
2322 {
2323 lpar = TrPar2;
2324 gp_Pnt2d TrPnt2d = PCurve->Value(lpar);
2325 gp_Pnt TrPnt(TrPnt2d.X(), TrPnt2d.Y(), 0.);
2326 V2 = BRepLib_MakeVertex(TrPnt);
2327 }
6a442250 2328 TheBuilder.Add(NewEdge, TheVer.First().Oriented(TopAbs_REVERSED));
2329 TheBuilder.Add(NewEdge, V2.Oriented(TopAbs_FORWARD));
2330 TheBuilder.Range(NewEdge, ThePar.First(), lpar);
2331 }
7fd59977 2332 }
6a442250 2333 else //last edge of open wire
2334 {
2335 if (NewEdge.Orientation() == TopAbs_FORWARD)
2336 {
e11c1bc4 2337 if (ToTrimAsOrigin)
2338 {
2339 lpar = TrPar2;
2340 gp_Pnt2d TrPnt2d = PCurve->Value(lpar);
2341 gp_Pnt TrPnt(TrPnt2d.X(), TrPnt2d.Y(), 0.);
2342 V2 = BRepLib_MakeVertex(TrPnt);
2343 }
6a442250 2344 TheBuilder.Add(NewEdge, TheVer.First().Oriented(TopAbs_FORWARD));
2345 TheBuilder.Add(NewEdge, V2.Oriented(TopAbs_REVERSED));
2346 TheBuilder.Range(NewEdge, ThePar.First(), lpar);
2347 }
2348 else
2349 {
e11c1bc4 2350 if (ToTrimAsOrigin)
2351 {
2352 fpar = TrPar1;
2353 gp_Pnt2d TrPnt2d = PCurve->Value(fpar);
2354 gp_Pnt TrPnt(TrPnt2d.X(), TrPnt2d.Y(), 0.);
2355 V1 = BRepLib_MakeVertex(TrPnt);
2356 }
6a442250 2357 TheBuilder.Add(NewEdge, V1.Oriented(TopAbs_REVERSED));
2358 TheBuilder.Add(NewEdge, TheVer.First().Oriented(TopAbs_FORWARD));
2359 TheBuilder.Range(NewEdge, fpar, ThePar.First());
2360 }
7fd59977 2361 }
6a442250 2362 S.Append(NewEdge);
2363 }
2364 else
2365 {
2366 for (Standard_Integer k = 1; k < TheVer.Length(); k = k+2) {
2367 TopoDS_Shape aLocalShape = E.EmptyCopied();
2368 TopoDS_Edge NewEdge = TopoDS::Edge(aLocalShape);
2369 // TopoDS_Edge NewEdge = TopoDS::Edge(E.EmptyCopied());
2370
2371 if (NewEdge.Orientation() == TopAbs_REVERSED) {
2372 TheBuilder.Add (NewEdge,TheVer.Value(k) .Oriented(TopAbs_REVERSED));
2373 TheBuilder.Add (NewEdge,TheVer.Value(k+1).Oriented(TopAbs_FORWARD));
2374 }
2375 else {
2376 TheBuilder.Add (NewEdge,TheVer.Value(k) .Oriented(TopAbs_FORWARD));
2377 TheBuilder.Add (NewEdge,TheVer.Value(k+1).Oriented(TopAbs_REVERSED));
2378 }
2379
2380
2381 TheBuilder.Range(NewEdge,ThePar.Value(k),ThePar.Value(k+1));
2382
873c119f 2383#ifdef OCCT_DEBUG
7fd59977 2384#ifdef DRAW
6a442250 2385 if ( AffichEdge) {
2386 char name[256];
2387 sprintf(name,"TRIMEDGE_%d",NbTRIMEDGES);
2388 DBRep::Set(name,NewEdge);
2389 }
2390 if (Affich2d) {
2391 TopLoc_Location L;
2392 Standard_Real f,l;
2393 Handle(Geom_Surface) Surf;
2394 Handle(Geom2d_Curve) C;
2395 BRep_Tool::CurveOnSurface(NewEdge,C,Surf,L,f,l);
2396 char name[256];
2397 sprintf(name,"OFFSET2d_%d",NbTRIMEDGES++);
2398 Handle(Geom2d_TrimmedCurve) C2d = new Geom2d_TrimmedCurve(C,f,l);
2399 Handle(DrawTrSurf_Curve2d) dr =
2400 new DrawTrSurf_Curve2d(C2d,Standard_False);
2401 dr->SetColor(Draw_bleu);
2402 Draw::Set(name,dr);
2403 }
7fd59977 2404#endif
873c119f 2405#endif
6a442250 2406 S.Append(NewEdge);
2407 }
7fd59977 2408 }
2409}
2410
2411//=======================================================================
e11c1bc4 2412//function : IsInnerEdge
2413//purpose :
2414//=======================================================================
2415
2416static Standard_Boolean IsInnerEdge(const TopoDS_Shape& ProE,
2417 const TopoDS_Face& AllSpine,
2418 Standard_Real& TrPar1,
2419 Standard_Real& TrPar2)
2420{
2421 if (ProE.ShapeType() != TopAbs_EDGE)
2422 return Standard_False;
2423
2424 TopoDS_Edge anEdge = TopoDS::Edge(ProE);
2425
2426 TopTools_IndexedDataMapOfShapeListOfShape VEmap;
2427 TopExp::MapShapesAndAncestors(AllSpine, TopAbs_VERTEX, TopAbs_EDGE, VEmap);
2428 for (Standard_Integer i = 1; i <= VEmap.Extent(); i++)
2429 {
2430 const TopTools_ListOfShape& LE = VEmap(i);
2431 if (LE.Extent() == 1 && anEdge.IsSame(LE.First()))
2432 return Standard_False;
2433 }
2434
2435 BRep_Tool::Range(anEdge, TrPar1, TrPar2);
2436 return Standard_True;
2437}
2438
2439
2440
2441//=======================================================================
7fd59977 2442//function : DoubleOrNotInside
0d969553 2443//purpose : return True if V appears twice in LV or is not inside.
7fd59977 2444//=======================================================================
2445
2446Standard_Boolean DoubleOrNotInside (const TopTools_ListOfShape& LV,
873c119f 2447 const TopoDS_Vertex& V)
7fd59977 2448{
2449 Standard_Boolean Vu = Standard_False;
2450 TopTools_ListIteratorOfListOfShape it(LV);
2451
2452 for ( ; it.More(); it.Next()) {
2453 if (V.IsSame(it.Value())) {
2454 if (Vu) return Standard_True;
2455 else Vu = Standard_True;
2456 }
2457 }
2458 if (Vu) return Standard_False;
2459 else return Standard_True;
2460}
2461
2462Standard_Boolean IsSmallClosedEdge(const TopoDS_Edge& anEdge,
873c119f 2463 const TopoDS_Vertex& aVertex)
7fd59977 2464{
2465 gp_Pnt PV = BRep_Tool::Pnt(aVertex);
2466 gp_Pnt2d PV2d, Pfirst, Plast, Pmid;
2467 PV2d.SetCoord( PV.X(), PV.Y() );
2468
2469 Handle(Geom2d_Curve) PCurve;
2470 Handle( BRep_CurveRepresentation ) CurveRep =
2471 ((Handle(BRep_TEdge)::DownCast(anEdge.TShape()))->Curves()).First();
2472 PCurve = CurveRep->PCurve();
2473
2474 Standard_Real fpar = (Handle(BRep_GCurve)::DownCast(CurveRep))->First();
2475 Standard_Real lpar = (Handle(BRep_GCurve)::DownCast(CurveRep))->Last();
2476 Pfirst = PCurve->Value(fpar);
2477 Plast = PCurve->Value(lpar);
2478 Pmid = PCurve->Value((fpar + lpar)*0.5);
2479
2480 Standard_Real theTol = BRep_Tool::Tolerance(aVertex);
2481 theTol *= 1.5;
2482
2483 Standard_Real dist1 = Pfirst.Distance(PV2d);
2484 Standard_Real dist2 = Plast.Distance(PV2d);
2485 Standard_Real dist3 = Pmid.Distance(PV2d);
2486
2487 if (dist1 <= theTol && dist2 <= theTol && dist3 <= theTol)
2488 return Standard_True;
2489
2490 return Standard_False;
2491}
2492
2493static void CheckBadEdges(const TopoDS_Face& Spine, const Standard_Real Offset,
873c119f 2494 const BRepMAT2d_BisectingLocus& Locus,
2495 const BRepMAT2d_LinkTopoBilo& Link,
2496 TopTools_ListOfShape& BadEdges)
7fd59977 2497{
2498
2499 TopoDS_Face F = TopoDS::Face(Spine.Oriented(TopAbs_FORWARD));
2500 Standard_Real eps = Precision::Confusion();
2501 Standard_Real LimCurv = 1./Offset;
2502
2503 TopTools_MapOfShape aMap;
2504
2505 for (Standard_Integer ic = 1; ic <= Locus.NumberOfContours(); ic++) {
2506 for (Standard_Integer ie = 1; ie <= Locus.NumberOfElts(ic); ie++) {
2507 const TopoDS_Shape& SE = Link.GeneratingShape(Locus.BasicElt(ic,ie));
2508 if (SE.ShapeType() == TopAbs_EDGE) {
2509
873c119f 2510
2511 if (aMap.Contains(SE)) {
2512 //cout << "Edge is treated second time" << endl;
2513 continue;
2514 }
2515
2516
2517 TopoDS_Edge E = TopoDS::Edge(SE);
2518
2519
2520 Standard_Real f,l;
2521
2522
2523 Handle(Geom2d_Curve) G2d = BRep_Tool::CurveOnSurface(E,F,f,l);
2524
2525
2526 Geom2dAdaptor_Curve AC(G2d,f,l);
2527 GeomAbs_CurveType aCType = AC.GetType();
2528
2529
2530 if(aCType != GeomAbs_Line && aCType != GeomAbs_Circle) {
2531
2532
2533 Standard_Boolean reverse = Standard_False;
2534 if (E.Orientation() == TopAbs_FORWARD) reverse = Standard_True;
2535
2536
2537 gp_Pnt2d P, Pc;
2538 gp_Dir2d N;
2539
2540
2541 Geom2dLProp_CLProps2d aCLProps(G2d, 2, eps);
2542
2543
2544 aCLProps.SetParameter(f);
2545 if(!aCLProps.IsTangentDefined()) {
2546 BadEdges.Append(SE);
2547 aMap.Add(SE);
2548 continue;
2549 }
2550
2551
2552 P = aCLProps.Value();
2553 Standard_Real Crv = aCLProps.Curvature();
2554
2555
2556 if(Crv >= eps) {
2557 aCLProps.Tangent(N);
2558 Standard_Real x = N.Y(), y = -N.X();
2559 N.SetCoord(x, y);
2560 if (reverse) N.Reverse();
2561 aCLProps.CentreOfCurvature(Pc);
2562 gp_Vec2d Dir( P, Pc );
2563 if (N.Dot(Dir) > 0.) {
2564 if (LimCurv <= Crv + eps) {
2565 BadEdges.Append(SE);
2566 aMap.Add(SE);
2567 continue;
2568 }
2569 }
2570 }
2571
2572
2573 aCLProps.SetParameter(l);
2574 if(!aCLProps.IsTangentDefined()) {
2575 BadEdges.Append(SE);
2576 aMap.Add(SE);
2577 continue;
2578 }
2579
2580
2581 P = aCLProps.Value();
2582 Crv = aCLProps.Curvature();
2583
2584
2585 if(Crv >= eps) {
2586 aCLProps.Tangent(N);
2587 Standard_Real x = N.Y(), y = -N.X();
2588 N.SetCoord(x, y);
2589 if (reverse) N.Reverse();
2590 aCLProps.CentreOfCurvature(Pc);
2591 gp_Vec2d Dir( P, Pc );
2592 if (N.Dot(Dir) > 0.) {
2593 if (LimCurv <= Crv + eps) {
2594 BadEdges.Append(SE);
2595 aMap.Add(SE);
2596 continue;
2597 }
2598 }
2599 }
2600 }
7fd59977 2601 }
2602 }
2603 }
2604}
2605
2606
2607//=======================================================================
2608//function : PerformCurve
2609//purpose :
2610//=======================================================================
2611
2612static Standard_Boolean PerformCurve (TColStd_SequenceOfReal& Parameters,
873c119f 2613
2614 TColgp_SequenceOfPnt& Points,
2615 const Adaptor3d_Curve& C,
2616 const Standard_Real Deflection,
2617 const Standard_Real U1,
2618 const Standard_Real U2,
2619 const Standard_Real EPSILON,
2620 const Standard_Integer Nbmin)
7fd59977 2621{
2622 Standard_Real UU1 = Min(U1, U2);
2623 Standard_Real UU2 = Max(U1, U2);
2624
2625 gp_Pnt Pdeb, Pfin;
2626 gp_Vec Ddeb,Dfin;
2627 C.D1(UU1,Pdeb,Ddeb);
2628 Parameters.Append(UU1);
2629 Points.Append(Pdeb);
2630
2631 C.D1(UU2,Pfin,Dfin);
91bb31f3 2632
2633 const Standard_Real aDelta = UU2 - UU1;
2634 const Standard_Real aDist = Pdeb.Distance(Pfin);
2635
2636 if((aDelta/aDist) > 5.0e-14)
2637 {
2638 QuasiFleche(C,Deflection*Deflection,
873c119f 2639
2640 UU1,Pdeb,
2641 Ddeb,
2642 UU2,Pfin,
2643 Dfin,
2644 Nbmin,
2645 EPSILON*EPSILON,
2646 Parameters,Points);
91bb31f3 2647 }
2648
7fd59977 2649 return Standard_True;
2650}
2651//=======================================================================
2652//function : QuasiFleche
2653//purpose :
2654//=======================================================================
2655
2656static void QuasiFleche(const Adaptor3d_Curve& C,
873c119f 2657
2658 const Standard_Real Deflection2,
2659 const Standard_Real Udeb,
2660 const gp_Pnt& Pdeb,
2661 const gp_Vec& Vdeb,
2662 const Standard_Real Ufin,
2663 const gp_Pnt& Pfin,
2664 const gp_Vec& Vfin,
2665 const Standard_Integer Nbmin,
2666 const Standard_Real Eps,
2667 TColStd_SequenceOfReal& Parameters,
2668 TColgp_SequenceOfPnt& Points)
7fd59977 2669{
2670 Standard_Integer Ptslength = Points.Length();
2671 Standard_Real Udelta = Ufin-Udeb;
2672 gp_Pnt Pdelta;
2673 gp_Vec Vdelta;
2674 if (Nbmin > 2) {
2675 Udelta /=(Nbmin-1);
2676 C.D1(Udeb+Udelta,Pdelta,Vdelta);
2677 }
2678 else {
2679 Pdelta = Pfin;
2680 Vdelta = Vfin;
2681 }
2682
2683
2684 Standard_Real Norme = gp_Vec(Pdeb,Pdelta).SquareMagnitude();
2685 Standard_Real theFleche=0;
2686 Standard_Boolean flecheok = Standard_False;
2687 if (Norme > Eps) {
0d969553 2688 // Evaluation of the arrow by interpolation. See IntWalk_IWalking_5.gxx
7fd59977 2689 Standard_Real N1 = Vdeb.SquareMagnitude();
2690 Standard_Real N2 = Vdelta.SquareMagnitude();
2691 if (N1 > Eps && N2 > Eps) {
2692 Standard_Real Normediff =
873c119f 2693
2694 (Vdeb.Normalized().XYZ()-Vdelta.Normalized().XYZ()).SquareModulus();
7fd59977 2695 if (Normediff > Eps) {
873c119f 2696
2697 theFleche = Normediff*Norme/64.;
2698 flecheok = Standard_True;
7fd59977 2699 }
2700 }
2701 }
2702 if (!flecheok) {
2703 gp_Pnt Pmid((Pdeb.XYZ()+Pdelta.XYZ())/2.);
2704 gp_Pnt Pverif(C.Value(Udeb+Udelta/2.));
2705 theFleche = Pmid.SquareDistance(Pverif);
2706 }
2707
2708 if (theFleche < Deflection2) {
2709 Parameters.Append(Udeb+Udelta);
2710 Points.Append(Pdelta);
2711 }
2712 else {
2713 QuasiFleche(C,Deflection2,Udeb,Pdeb,
873c119f 2714
2715 Vdeb,
2716 Udeb+Udelta,Pdelta,
2717 Vdelta,
2718 3,
2719 Eps,
2720 Parameters,Points);
7fd59977 2721
2722 }
2723
2724 if (Nbmin > 2) {
2725 QuasiFleche(C,Deflection2,Udeb+Udelta,Pdelta,
873c119f 2726
2727 Vdelta,
2728 Ufin,Pfin,
2729 Vfin,
2730 Nbmin-(Points.Length()-Ptslength),
2731 Eps,
2732 Parameters,Points);
7fd59977 2733 }
2734}
ab87e6fc 2735
7642e2d0 2736Standard_Boolean CheckSmallParamOnEdge(const TopoDS_Edge& anEdge)
2737{
2738 const BRep_ListOfCurveRepresentation& aList = ((Handle(BRep_TEdge)::DownCast(anEdge.TShape()))->Curves());
2739 if (!aList.IsEmpty())
2740 {
2741 Handle( BRep_CurveRepresentation ) CRep = ((Handle(BRep_TEdge)::DownCast(anEdge.TShape()))->Curves()).First();
2742 Standard_Real f = (Handle(BRep_GCurve)::DownCast(CRep))->First();
2743 Standard_Real l = (Handle(BRep_GCurve)::DownCast(CRep))->Last();
2744 if (Abs (l - f) < Precision::PConfusion())
2745 return Standard_False;
2746 }
2747 return Standard_True;
2748}
2749
2750