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.
17 #include <BRepOffset_Analyse.ixx>
18 #include <BRepOffset_Interval.hxx>
19 #include <BRepOffset_Tool.hxx>
20 #include <BRepOffset_ListIteratorOfListOfInterval.hxx>
21 #include <BRep_Tool.hxx>
22 #include <BRepTools.hxx>
23 #include <BRep_Builder.hxx>
25 #include <BRepAdaptor_Curve.hxx>
31 #include <gp_Pnt2d.hxx>
33 #include <Geom_Surface.hxx>
34 #include <Geom_Curve.hxx>
35 #include <Geom2d_Curve.hxx>
38 #include <TopExp_Explorer.hxx>
41 #include <TopoDS_Face.hxx>
42 #include <TopoDS_Edge.hxx>
43 #include <TopTools_ListIteratorOfListOfShape.hxx>
44 #include <TopTools_MapOfShape.hxx>
46 #include <Precision.hxx>
50 static void CorrectOrientationOfTangent(gp_Vec& TangVec,
51 const TopoDS_Vertex& aVertex,
52 const TopoDS_Edge& anEdge)
54 TopoDS_Vertex Vlast = TopExp::LastVertex(anEdge);
55 if (aVertex.IsSame(Vlast))
59 //=======================================================================
60 //function : BRepOffset_Analyse
62 //=======================================================================
64 BRepOffset_Analyse::BRepOffset_Analyse()
65 :myDone(Standard_False)
70 //=======================================================================
71 //function : BRepOffset_Analyse
73 //=======================================================================
75 BRepOffset_Analyse::BRepOffset_Analyse(const TopoDS_Shape& S,
76 const Standard_Real Angle)
77 :myDone(Standard_False)
82 //=======================================================================
83 //function : EdgeAnlyse
85 //=======================================================================
87 static void EdgeAnalyse(const TopoDS_Edge& E,
88 const TopoDS_Face& F1,
89 const TopoDS_Face& F2,
90 const Standard_Real SinTol,
91 BRepOffset_ListOfInterval& LI)
98 Handle (Geom_Surface) S1 = BRep_Tool::Surface(F1);
99 Handle (Geom_Surface) S2 = BRep_Tool::Surface(F2);
100 Handle (Geom2d_Curve) C1 = BRep_Tool::CurveOnSurface(E,F1,f,l);
101 Handle (Geom2d_Curve) C2 = BRep_Tool::CurveOnSurface(E,F2,f,l);
103 BRepAdaptor_Curve C(E);
104 f = C.FirstParameter();
105 l = C.LastParameter();
107 // Tangent if the regularity is at least G1.
108 if (BRep_Tool::HasContinuity(E,F1,F2)) {
109 if (BRep_Tool::Continuity(E,F1,F2) > GeomAbs_C0) {
110 BRepOffset_Interval I;
111 I.First(f); I.Last(l);
112 I.Type(BRepOffset_Tangent);
117 // First stage : Type determined by one of ends.
118 // Calculate normals and tangents on the curves and surface.
119 // normals are oriented outwards.
121 Standard_Real ParOnC = 0.5*(f+l);
122 gp_Vec T1 = C.DN(ParOnC,1).Transformed(L.Transformation());
123 if (T1.SquareMagnitude() > gp::Resolution()) {
127 if (BRepOffset_Tool::OriEdgeInFace(E,F1) == TopAbs_REVERSED) {
130 if (F1.Orientation() == TopAbs_REVERSED) T1.Reverse();
132 gp_Pnt2d P = C1->Value(ParOnC);
136 S1->D1(P.X(),P.Y(),P3,D1U,D1V);
138 if (F1.Orientation() == TopAbs_REVERSED) DN1.Reverse();
140 P = C2->Value(ParOnC);
141 S2->D1(P.X(),P.Y(),P3,D1U,D1V);
143 if (F2.Orientation() == TopAbs_REVERSED) DN2.Reverse();
148 gp_Vec ProVec = DN1^DN2;
149 Standard_Real NormProVec = ProVec.Magnitude();
151 BRepOffset_Interval I;
152 I.First(f); I.Last(l);
154 if (Abs(NormProVec) < SinTol) {
156 if (DN1.Dot(DN2) > 0) {
158 I.Type(BRepOffset_Tangent);
161 //Mixed not finished!
163 cout <<" faces locally mixed"<<endl;
165 I.Type(BRepOffset_Convex);
169 if (NormProVec > gp::Resolution())
171 Standard_Real Prod = T1.Dot(DN1^DN2);
174 I.Type(BRepOffset_Convex);
178 I.Type(BRepOffset_Concave);
184 //=======================================================================
185 //function : BuildAncestors
187 //=======================================================================
189 static void BuildAncestors (const TopoDS_Shape& S,
190 TopTools_IndexedDataMapOfShapeListOfShape& MA)
193 TopExp::MapShapesAndAncestors(S,TopAbs_VERTEX,TopAbs_EDGE,MA);
194 TopExp::MapShapesAndAncestors(S,TopAbs_EDGE ,TopAbs_FACE,MA);
197 TopTools_MapOfShape Map;
198 for (Standard_Integer i = 1; i <= MA.Extent(); i++) {
200 TopTools_ListOfShape& L = MA(i);
201 TopTools_ListIteratorOfListOfShape it(L);
203 if (!Map.Add(it.Value())) {
213 //=======================================================================
216 //=======================================================================
218 Standard_Boolean BRepOffset_Analyse::IsDone() const
224 //=======================================================================
227 //=======================================================================
229 void BRepOffset_Analyse::Perform (const TopoDS_Shape& S,
230 const Standard_Real Angle)
235 Standard_Real SinTol = sin(Angle);
238 BuildAncestors (S,ancestors);
241 TopExp_Explorer Exp(S.Oriented(TopAbs_FORWARD),TopAbs_EDGE);
242 for ( ; Exp.More(); Exp.Next()) {
243 const TopoDS_Edge& E = TopoDS::Edge(Exp.Current());
244 if (!mapEdgeType.IsBound(E)) {
245 BRepOffset_ListOfInterval LI;
246 mapEdgeType.Bind(E,LI);
248 const TopTools_ListOfShape& L = Ancestors(E);
252 if (L.Extent() == 2) {
253 const TopoDS_Face& F1 = TopoDS::Face(L.First());
254 const TopoDS_Face& F2 = TopoDS::Face(L.Last ());
255 EdgeAnalyse(E,F1,F2,SinTol,mapEdgeType(E));
257 else if (L.Extent() == 1) {
259 const TopoDS_Face& F = TopoDS::Face(L.First());
260 BRep_Tool::Range(E,F,U1,U2);
261 BRepOffset_Interval Inter(U1,U2,BRepOffset_Other);
263 if (! BRepTools::IsReallyClosed(E,F)) {
264 Inter.Type(BRepOffset_FreeBoundary);
266 mapEdgeType(E).Append(Inter);
270 cout <<"edge shared by more than two faces"<<endl;
275 myDone = Standard_True;
278 //=======================================================================
281 //=======================================================================
283 void BRepOffset_Analyse::Clear()
285 myDone = Standard_False;
295 //=======================================================================
296 //function : BRepOffset_ListOfInterval&
298 //=======================================================================
300 const BRepOffset_ListOfInterval& BRepOffset_Analyse::Type(const TopoDS_Edge& E)
303 return mapEdgeType (E);
307 //=======================================================================
310 //=======================================================================
312 void BRepOffset_Analyse::Edges(const TopoDS_Vertex& V,
313 const BRepOffset_Type T,
314 TopTools_ListOfShape& LE)
318 const TopTools_ListOfShape& L = Ancestors (V);
319 TopTools_ListIteratorOfListOfShape it(L);
321 for ( ;it.More(); it.Next()) {
322 const TopoDS_Edge& E = TopoDS::Edge(it.Value());
324 BRepOffset_Tool::EdgeVertices (E,V1,V2);
326 if (mapEdgeType(E).Last().Type() == T)
330 if (mapEdgeType(E).First().Type() == T)
337 //=======================================================================
340 //=======================================================================
342 void BRepOffset_Analyse::Edges(const TopoDS_Face& F,
343 const BRepOffset_Type T,
344 TopTools_ListOfShape& LE)
348 TopExp_Explorer exp(F, TopAbs_EDGE);
350 for ( ;exp.More(); exp.Next()) {
351 const TopoDS_Edge& E = TopoDS::Edge(exp.Current());
353 const BRepOffset_ListOfInterval& Lint = Type(E);
354 BRepOffset_ListIteratorOfListOfInterval it(Lint);
355 for ( ;it.More(); it.Next()) {
356 if (it.Value().Type() == T) LE.Append(E);
361 //=======================================================================
362 //function : TangentEdges
364 //=======================================================================
366 void BRepOffset_Analyse::TangentEdges(const TopoDS_Edge& Edge ,
367 const TopoDS_Vertex& Vertex,
368 TopTools_ListOfShape& Edges ) const
373 Standard_Real U,URef;
374 BRepAdaptor_Curve C3d, C3dRef;
376 URef = BRep_Tool::Parameter(Vertex,Edge);
377 C3dRef = BRepAdaptor_Curve(Edge);
378 VRef = C3dRef.DN(URef,1);
379 CorrectOrientationOfTangent(VRef, Vertex, Edge);
380 if (VRef.SquareMagnitude() < gp::Resolution()) return;
384 const TopTools_ListOfShape& Anc = Ancestors(Vertex);
385 TopTools_ListIteratorOfListOfShape it(Anc);
386 for ( ; it.More(); it.Next()) {
387 const TopoDS_Edge& CurE = TopoDS::Edge(it.Value());
388 if ( CurE.IsSame(Edge)) continue;
389 U = BRep_Tool::Parameter(Vertex,CurE);
390 C3d = BRepAdaptor_Curve(CurE);
392 CorrectOrientationOfTangent(V, Vertex, CurE);
393 if (V.SquareMagnitude() < gp::Resolution()) continue;
394 if (V.IsOpposite(VRef,angle)) {
402 //=======================================================================
403 //function : HasAncestor
405 //=======================================================================
407 Standard_Boolean BRepOffset_Analyse::HasAncestor (const TopoDS_Shape& S) const
409 return ancestors.Contains(S);
413 //=======================================================================
414 //function : Ancestors
416 //=======================================================================
418 const TopTools_ListOfShape& BRepOffset_Analyse::Ancestors
419 (const TopoDS_Shape& S) const
421 return ancestors.FindFromKey(S);
425 //=======================================================================
428 //=======================================================================
430 void BRepOffset_Analyse::Explode( TopTools_ListOfShape& List,
431 const BRepOffset_Type T ) const
435 TopTools_MapOfShape Map;
437 TopExp_Explorer Fexp;
438 for (Fexp.Init(myShape,TopAbs_FACE); Fexp.More(); Fexp.Next()) {
439 if ( Map.Add(Fexp.Current())) {
440 TopoDS_Face Face = TopoDS::Face(Fexp.Current());
444 // add to Co all faces from the cloud of faces
445 // G1 created from <Face>
446 AddFaces(Face,Co,Map,T);
452 //=======================================================================
455 //=======================================================================
457 void BRepOffset_Analyse::Explode( TopTools_ListOfShape& List,
458 const BRepOffset_Type T1,
459 const BRepOffset_Type T2) const
463 TopTools_MapOfShape Map;
465 TopExp_Explorer Fexp;
466 for (Fexp.Init(myShape,TopAbs_FACE); Fexp.More(); Fexp.Next()) {
467 if ( Map.Add(Fexp.Current())) {
468 TopoDS_Face Face = TopoDS::Face(Fexp.Current());
472 // add to Co all faces from the cloud of faces
473 // G1 created from <Face>
474 AddFaces(Face,Co,Map,T1,T2);
481 //=======================================================================
482 //function : AddFaces
484 //=======================================================================
486 void BRepOffset_Analyse::AddFaces (const TopoDS_Face& Face,
488 TopTools_MapOfShape& Map,
489 const BRepOffset_Type T) const
492 TopExp_Explorer exp(Face,TopAbs_EDGE);
493 for ( ; exp.More(); exp.Next()) {
494 const TopoDS_Edge& E = TopoDS::Edge(exp.Current());
495 const BRepOffset_ListOfInterval& LI = Type(E);
496 if (!LI.IsEmpty() && LI.First().Type() == T) {
497 // so <NewFace> is attached to G1 by <Face>
498 const TopTools_ListOfShape& L = Ancestors(E);
499 if (L.Extent() == 2) {
500 TopoDS_Face F1 = TopoDS::Face(L.First());
501 if ( F1.IsSame(Face))
502 F1 = TopoDS::Face(L.Last ());
505 AddFaces(F1,Co,Map,T);
511 //=======================================================================
512 //function : AddFaces
514 //=======================================================================
516 void BRepOffset_Analyse::AddFaces (const TopoDS_Face& Face,
518 TopTools_MapOfShape& Map,
519 const BRepOffset_Type T1,
520 const BRepOffset_Type T2) const
523 TopExp_Explorer exp(Face,TopAbs_EDGE);
524 for ( ; exp.More(); exp.Next()) {
525 const TopoDS_Edge& E = TopoDS::Edge(exp.Current());
526 const BRepOffset_ListOfInterval& LI = Type(E);
528 (LI.First().Type() == T1 || LI.First().Type() == T2)) {
529 // so <NewFace> is attached to G1 by <Face>
530 const TopTools_ListOfShape& L = Ancestors(E);
531 if (L.Extent() == 2) {
532 TopoDS_Face F1 = TopoDS::Face(L.First());
533 if ( F1.IsSame(Face))
534 F1 = TopoDS::Face(L.Last ());
537 AddFaces(F1,Co,Map,T1,T2);