1 // Created on: 1995-10-20
2 // Created by: Yves FRICAUD
3 // Copyright (c) 1995-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.
18 #include <Adaptor3d_Surface.hxx>
19 #include <BRep_Builder.hxx>
20 #include <BRep_Tool.hxx>
21 #include <BRepAdaptor_Curve.hxx>
22 #include <BRepAdaptor_Surface.hxx>
23 #include <BRepOffset_Analyse.hxx>
24 #include <BRepOffset_Interval.hxx>
25 #include <BRepOffset_ListIteratorOfListOfInterval.hxx>
26 #include <BRepOffset_Tool.hxx>
27 #include <BRepTools.hxx>
28 #include <Geom2d_Curve.hxx>
29 #include <Geom_Curve.hxx>
30 #include <Geom_Surface.hxx>
34 #include <gp_Pnt2d.hxx>
36 #include <Precision.hxx>
38 #include <TopExp_Explorer.hxx>
40 #include <TopoDS_Compound.hxx>
41 #include <TopoDS_Edge.hxx>
42 #include <TopoDS_Face.hxx>
43 #include <TopoDS_Shape.hxx>
44 #include <TopoDS_Vertex.hxx>
45 #include <TopTools_ListIteratorOfListOfShape.hxx>
46 #include <TopTools_MapOfShape.hxx>
49 static void Correct2dPoint(const Adaptor3d_Surface& theS, gp_Pnt2d& theP2d);
51 static BRepOffset_Type DefineConnectType(const TopoDS_Edge& E,
52 const TopoDS_Face& F1,
53 const TopoDS_Face& F2,
54 const Standard_Real SinTol,
55 const Standard_Boolean CorrectPoint);
57 static void CorrectOrientationOfTangent(gp_Vec& TangVec,
58 const TopoDS_Vertex& aVertex,
59 const TopoDS_Edge& anEdge)
61 TopoDS_Vertex Vlast = TopExp::LastVertex(anEdge);
62 if (aVertex.IsSame(Vlast))
65 //=======================================================================
66 //function : BRepOffset_Analyse
68 //=======================================================================
70 BRepOffset_Analyse::BRepOffset_Analyse()
71 :myDone(Standard_False)
76 //=======================================================================
77 //function : BRepOffset_Analyse
79 //=======================================================================
81 BRepOffset_Analyse::BRepOffset_Analyse(const TopoDS_Shape& S,
82 const Standard_Real Angle)
83 :myDone(Standard_False)
88 //=======================================================================
89 //function : EdgeAnlyse
91 //=======================================================================
93 static void EdgeAnalyse(const TopoDS_Edge& E,
94 const TopoDS_Face& F1,
95 const TopoDS_Face& F2,
96 const Standard_Real SinTol,
97 BRepOffset_ListOfInterval& LI)
101 BRep_Tool::Range(E, F1, f, l);
102 BRepOffset_Interval I;
103 I.First(f); I.Last(l);
105 // Tangent if the regularity is at least G1.
106 if (BRep_Tool::HasContinuity(E,F1,F2)) {
107 if (BRep_Tool::Continuity(E,F1,F2) > GeomAbs_C0) {
108 I.Type(BRepOffset_Tangent);
114 BRepOffset_Type aType = DefineConnectType(E, F1, F2, SinTol, Standard_False);
115 if(aType != BRepOffset_Tangent)
117 aType = DefineConnectType(E, F1, F2, SinTol, Standard_True);
123 //=======================================================================
124 //function : BuildAncestors
126 //=======================================================================
128 static void BuildAncestors (const TopoDS_Shape& S,
129 TopTools_IndexedDataMapOfShapeListOfShape& MA)
132 TopExp::MapShapesAndAncestors(S,TopAbs_VERTEX,TopAbs_EDGE,MA);
133 TopExp::MapShapesAndAncestors(S,TopAbs_EDGE ,TopAbs_FACE,MA);
136 TopTools_MapOfShape Map;
137 for (Standard_Integer i = 1; i <= MA.Extent(); i++) {
139 TopTools_ListOfShape& L = MA(i);
140 TopTools_ListIteratorOfListOfShape it(L);
142 if (!Map.Add(it.Value())) {
152 //=======================================================================
155 //=======================================================================
157 Standard_Boolean BRepOffset_Analyse::IsDone() const
163 //=======================================================================
166 //=======================================================================
168 void BRepOffset_Analyse::Perform (const TopoDS_Shape& S,
169 const Standard_Real Angle)
174 Standard_Real SinTol = Sin(Angle);
177 BuildAncestors (S,ancestors);
180 TopExp_Explorer Exp(S.Oriented(TopAbs_FORWARD),TopAbs_EDGE);
181 for ( ; Exp.More(); Exp.Next()) {
182 const TopoDS_Edge& E = TopoDS::Edge(Exp.Current());
183 if (!mapEdgeType.IsBound(E)) {
184 BRepOffset_ListOfInterval LI;
185 mapEdgeType.Bind(E,LI);
187 const TopTools_ListOfShape& L = Ancestors(E);
191 if (L.Extent() == 2) {
192 const TopoDS_Face& F1 = TopoDS::Face(L.First());
193 const TopoDS_Face& F2 = TopoDS::Face(L.Last ());
194 EdgeAnalyse(E,F1,F2,SinTol,mapEdgeType(E));
196 else if (L.Extent() == 1) {
198 const TopoDS_Face& F = TopoDS::Face(L.First());
199 BRep_Tool::Range(E,F,U1,U2);
200 BRepOffset_Interval Inter(U1,U2,BRepOffset_Other);
202 if (! BRepTools::IsReallyClosed(E,F)) {
203 Inter.Type(BRepOffset_FreeBoundary);
205 mapEdgeType(E).Append(Inter);
209 cout <<"edge shared by more than two faces"<<endl;
214 myDone = Standard_True;
217 //=======================================================================
220 //=======================================================================
222 void BRepOffset_Analyse::Clear()
224 myDone = Standard_False;
234 //=======================================================================
235 //function : BRepOffset_ListOfInterval&
237 //=======================================================================
239 const BRepOffset_ListOfInterval& BRepOffset_Analyse::Type(const TopoDS_Edge& E)
242 return mapEdgeType (E);
246 //=======================================================================
249 //=======================================================================
251 void BRepOffset_Analyse::Edges(const TopoDS_Vertex& V,
252 const BRepOffset_Type T,
253 TopTools_ListOfShape& LE)
257 const TopTools_ListOfShape& L = Ancestors (V);
258 TopTools_ListIteratorOfListOfShape it(L);
260 for ( ;it.More(); it.Next()) {
261 const TopoDS_Edge& E = TopoDS::Edge(it.Value());
263 BRepOffset_Tool::EdgeVertices (E,V1,V2);
265 if (mapEdgeType(E).Last().Type() == T)
269 if (mapEdgeType(E).First().Type() == T)
276 //=======================================================================
279 //=======================================================================
281 void BRepOffset_Analyse::Edges(const TopoDS_Face& F,
282 const BRepOffset_Type T,
283 TopTools_ListOfShape& LE)
287 TopExp_Explorer exp(F, TopAbs_EDGE);
289 for ( ;exp.More(); exp.Next()) {
290 const TopoDS_Edge& E = TopoDS::Edge(exp.Current());
292 const BRepOffset_ListOfInterval& Lint = Type(E);
293 BRepOffset_ListIteratorOfListOfInterval it(Lint);
294 for ( ;it.More(); it.Next()) {
295 if (it.Value().Type() == T) LE.Append(E);
300 //=======================================================================
301 //function : TangentEdges
303 //=======================================================================
305 void BRepOffset_Analyse::TangentEdges(const TopoDS_Edge& Edge ,
306 const TopoDS_Vertex& Vertex,
307 TopTools_ListOfShape& Edges ) const
312 Standard_Real U,URef;
313 BRepAdaptor_Curve C3d, C3dRef;
315 URef = BRep_Tool::Parameter(Vertex,Edge);
316 C3dRef = BRepAdaptor_Curve(Edge);
317 VRef = C3dRef.DN(URef,1);
318 CorrectOrientationOfTangent(VRef, Vertex, Edge);
319 if (VRef.SquareMagnitude() < gp::Resolution()) return;
323 const TopTools_ListOfShape& Anc = Ancestors(Vertex);
324 TopTools_ListIteratorOfListOfShape it(Anc);
325 for ( ; it.More(); it.Next()) {
326 const TopoDS_Edge& CurE = TopoDS::Edge(it.Value());
327 if ( CurE.IsSame(Edge)) continue;
328 U = BRep_Tool::Parameter(Vertex,CurE);
329 C3d = BRepAdaptor_Curve(CurE);
331 CorrectOrientationOfTangent(V, Vertex, CurE);
332 if (V.SquareMagnitude() < gp::Resolution()) continue;
333 if (V.IsOpposite(VRef,angle)) {
341 //=======================================================================
342 //function : HasAncestor
344 //=======================================================================
346 Standard_Boolean BRepOffset_Analyse::HasAncestor (const TopoDS_Shape& S) const
348 return ancestors.Contains(S);
352 //=======================================================================
353 //function : Ancestors
355 //=======================================================================
357 const TopTools_ListOfShape& BRepOffset_Analyse::Ancestors
358 (const TopoDS_Shape& S) const
360 return ancestors.FindFromKey(S);
364 //=======================================================================
367 //=======================================================================
369 void BRepOffset_Analyse::Explode( TopTools_ListOfShape& List,
370 const BRepOffset_Type T ) const
374 TopTools_MapOfShape Map;
376 TopExp_Explorer Fexp;
377 for (Fexp.Init(myShape,TopAbs_FACE); Fexp.More(); Fexp.Next()) {
378 if ( Map.Add(Fexp.Current())) {
379 TopoDS_Face Face = TopoDS::Face(Fexp.Current());
383 // add to Co all faces from the cloud of faces
384 // G1 created from <Face>
385 AddFaces(Face,Co,Map,T);
391 //=======================================================================
394 //=======================================================================
396 void BRepOffset_Analyse::Explode( TopTools_ListOfShape& List,
397 const BRepOffset_Type T1,
398 const BRepOffset_Type T2) const
402 TopTools_MapOfShape Map;
404 TopExp_Explorer Fexp;
405 for (Fexp.Init(myShape,TopAbs_FACE); Fexp.More(); Fexp.Next()) {
406 if ( Map.Add(Fexp.Current())) {
407 TopoDS_Face Face = TopoDS::Face(Fexp.Current());
411 // add to Co all faces from the cloud of faces
412 // G1 created from <Face>
413 AddFaces(Face,Co,Map,T1,T2);
420 //=======================================================================
421 //function : AddFaces
423 //=======================================================================
425 void BRepOffset_Analyse::AddFaces (const TopoDS_Face& Face,
427 TopTools_MapOfShape& Map,
428 const BRepOffset_Type T) const
431 TopExp_Explorer exp(Face,TopAbs_EDGE);
432 for ( ; exp.More(); exp.Next()) {
433 const TopoDS_Edge& E = TopoDS::Edge(exp.Current());
434 const BRepOffset_ListOfInterval& LI = Type(E);
435 if (!LI.IsEmpty() && LI.First().Type() == T) {
436 // so <NewFace> is attached to G1 by <Face>
437 const TopTools_ListOfShape& L = Ancestors(E);
438 if (L.Extent() == 2) {
439 TopoDS_Face F1 = TopoDS::Face(L.First());
440 if ( F1.IsSame(Face))
441 F1 = TopoDS::Face(L.Last ());
444 AddFaces(F1,Co,Map,T);
450 //=======================================================================
451 //function : AddFaces
453 //=======================================================================
455 void BRepOffset_Analyse::AddFaces (const TopoDS_Face& Face,
457 TopTools_MapOfShape& Map,
458 const BRepOffset_Type T1,
459 const BRepOffset_Type T2) const
462 TopExp_Explorer exp(Face,TopAbs_EDGE);
463 for ( ; exp.More(); exp.Next()) {
464 const TopoDS_Edge& E = TopoDS::Edge(exp.Current());
465 const BRepOffset_ListOfInterval& LI = Type(E);
467 (LI.First().Type() == T1 || LI.First().Type() == T2)) {
468 // so <NewFace> is attached to G1 by <Face>
469 const TopTools_ListOfShape& L = Ancestors(E);
470 if (L.Extent() == 2) {
471 TopoDS_Face F1 = TopoDS::Face(L.First());
472 if ( F1.IsSame(Face))
473 F1 = TopoDS::Face(L.Last ());
476 AddFaces(F1,Co,Map,T1,T2);
483 //=======================================================================
484 //function : Correct2dPoint
486 //=======================================================================
487 void Correct2dPoint(const Adaptor3d_Surface& theS, gp_Pnt2d& theP2d)
489 const Standard_Real coeff = 0.01;
491 Standard_Real u1, u2, v1, v2;
492 if(theS.GetType() >= GeomAbs_BezierSurface)
494 u1 = theS.FirstUParameter();
495 u2 = theS.LastUParameter();
496 v1 = theS.FirstVParameter();
497 v2 = theS.LastVParameter();
498 if(!(Precision::IsInfinite(u1) || Precision::IsInfinite(u2)))
500 eps = Max(coeff*(u2-u1), Precision::PConfusion());
501 if(Abs(theP2d.X()-u1) < eps)
503 theP2d.SetX(u1 + eps);
505 if(Abs(theP2d.X()-u2) < eps)
507 theP2d.SetX(u2 - eps);
510 if(!(Precision::IsInfinite(v1) || Precision::IsInfinite(v2)))
512 eps = Max(coeff*(v2-v1), Precision::PConfusion());
513 if(Abs(theP2d.Y()-v1) < eps)
515 theP2d.SetY(v1 + eps);
517 if(Abs(theP2d.Y()-v2) < eps)
519 theP2d.SetY(v2 - eps);
525 //=======================================================================
526 //function : DefineConnectType
528 //=======================================================================
529 BRepOffset_Type DefineConnectType(const TopoDS_Edge& E,
530 const TopoDS_Face& F1,
531 const TopoDS_Face& F2,
532 const Standard_Real SinTol,
533 const Standard_Boolean CorrectPoint)
538 BRepAdaptor_Surface S1(F1), S2(F2);
539 Handle (Geom2d_Curve) C1 = BRep_Tool::CurveOnSurface(E,F1,f,l);
540 Handle (Geom2d_Curve) C2 = BRep_Tool::CurveOnSurface(E,F2,f,l);
542 BRepAdaptor_Curve C(E);
543 f = C.FirstParameter();
544 l = C.LastParameter();
546 Standard_Real ParOnC = 0.5*(f+l);
547 gp_Vec T1 = C.DN(ParOnC,1).Transformed(L.Transformation());
548 if (T1.SquareMagnitude() > gp::Resolution()) {
552 if (BRepOffset_Tool::OriEdgeInFace(E,F1) == TopAbs_REVERSED) {
555 if (F1.Orientation() == TopAbs_REVERSED) T1.Reverse();
557 gp_Pnt2d P = C1->Value(ParOnC);
562 Correct2dPoint(S1, P);
564 S1.D1(P.X(),P.Y(),P3,D1U,D1V);
566 if (F1.Orientation() == TopAbs_REVERSED) DN1.Reverse();
568 P = C2->Value(ParOnC);
570 Correct2dPoint(S2, P);
571 S2.D1(P.X(),P.Y(),P3,D1U,D1V);
573 if (F2.Orientation() == TopAbs_REVERSED) DN2.Reverse();
578 gp_Vec ProVec = DN1^DN2;
579 Standard_Real NormProVec = ProVec.Magnitude();
581 if (Abs(NormProVec) < SinTol) {
583 if (DN1.Dot(DN2) > 0) {
585 return BRepOffset_Tangent;
588 //Mixed not finished!
590 cout <<" faces locally mixed"<<endl;
592 return BRepOffset_Convex;
596 if (NormProVec > gp::Resolution())
598 Standard_Real Prod = T1.Dot(DN1^DN2);
601 return BRepOffset_Convex;
605 return BRepOffset_Concave;