0030675: Visualization - remove redundant proxy classes in hierarchy of PrsMgr_Presen...
[occt.git] / src / TopOpeBRepBuild / TopOpeBRepBuild_Tools2d.cxx
1 // Created on: 1999-11-29
2 // Created by: Peter KURNEV
3 // Copyright (c) 1999-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
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.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17
18 #include <TopExp.hxx>
19 #include <TopExp_Explorer.hxx>
20 #include <TopoDS.hxx>
21 #include <TopoDS_Vertex.hxx>
22 #include <TopoDS_Wire.hxx>
23 #include <TopOpeBRepBuild_Tools2d.hxx>
24 #include <TopOpeBRepBuild_VertexInfo.hxx>
25 #include <TopTools_IndexedMapOfShape.hxx>
26 #include <TopTools_ListIteratorOfListOfShape.hxx>
27 #include <TopTools_ListOfShape.hxx>
28
29 #include <stdio.h>
30 static 
31   void BuildPath (const TopoDS_Vertex& myVertex0,
32                   const TopoDS_Edge&   myEdge,
33                   const TopoDS_Vertex& myVertex,
34                   const Standard_Integer aNbEdges,
35                   TopOpeBRepBuild_IndexedDataMapOfShapeVertexInfo& M,
36                   Standard_Integer anEdgesCount,
37                   Standard_Integer& aBreakFlag,
38                   TopTools_ListOfShape& myResList);
39
40 //=======================================================================
41 // function :TopOpeBRepBuild_Tools2d::Path
42 // purpose: 
43 //=======================================================================
44   void TopOpeBRepBuild_Tools2d::Path(const TopoDS_Wire& aWire, TopTools_ListOfShape& aResList)
45 {
46   Standard_Integer anEdgesCount=0, aNbEdges=0, aBreakFlag=0;
47   TopTools_ListOfShape myResList;
48   TopoDS_Vertex  myVertex, myVertex0;
49   TopoDS_Edge    myEdge, aNullEdge;
50
51   TopExp_Explorer ex(aWire,TopAbs_EDGE);
52   for (; ex.More(); ex.Next()) aNbEdges++;
53   
54   myResList.Clear();
55
56   TopOpeBRepBuild_IndexedDataMapOfShapeVertexInfo M;
57   TopOpeBRepBuild_Tools2d::MakeMapOfShapeVertexInfo(aWire, M);
58   
59   
60   myEdge=aNullEdge;
61   myVertex0=TopoDS::Vertex(M.FindKey(1));
62   myVertex=myVertex0;
63
64   BuildPath (myVertex0, myEdge, myVertex, aNbEdges, M, anEdgesCount, aBreakFlag, myResList);
65   //
66   aResList.Clear();
67   aResList=myResList;
68 }
69 //=======================================================================
70 // function :BuildPath
71 // purpose: 
72 //=======================================================================
73   void BuildPath (const TopoDS_Vertex& myVertex0,
74                   const TopoDS_Edge&   myInputEdge,
75                   const TopoDS_Vertex& myInputVertex,
76                   const Standard_Integer aNbEdges,
77                   TopOpeBRepBuild_IndexedDataMapOfShapeVertexInfo& M,
78                   Standard_Integer anEdgesCount,
79                   Standard_Integer& aBreakFlag, 
80                   TopTools_ListOfShape& myResList)
81 {
82   Standard_Integer  j = 1, aFoundOut, aNbCases, stopFlag=0;
83   TopoDS_Edge myEdge;
84   TopoDS_Vertex myVertex;
85
86   if (aBreakFlag==1) return;
87   
88   TopOpeBRepBuild_VertexInfo& aVInfo=M.ChangeFromKey(myInputVertex);
89   //
90   aVInfo.SetCurrentIn (myInputEdge);
91   aVInfo.Prepare(myResList);
92   aNbCases=aVInfo.NbCases();
93   if (!aNbCases) aBreakFlag=2; 
94
95   for (j=1; j<=aNbCases; j++) {
96
97     myEdge=aVInfo.CurrentOut();
98
99     aFoundOut=aVInfo.FoundOut();
100     if (!aFoundOut) { //FondOut=0 TUPICK
101       aBreakFlag=2;
102       return;
103     }
104
105     else {
106       if (stopFlag) { // if previous path was wrong
107         aVInfo.RemovePassed();
108         myResList.RemoveFirst();
109         stopFlag=0;
110         anEdgesCount--;
111       }
112
113       aVInfo.AppendPassed (myEdge);
114       myResList.Prepend (myEdge);
115       anEdgesCount++;
116       myVertex=(myEdge.Orientation()==TopAbs_FORWARD)? 
117         TopExp::LastVertex(myEdge) : TopExp::FirstVertex(myEdge);
118       
119       if (myVertex.IsSame(myVertex0) && anEdgesCount==aNbEdges) {
120         aBreakFlag=1;
121         return;
122       }
123       
124       BuildPath (myVertex0, myEdge, myVertex, aNbEdges, M, anEdgesCount, aBreakFlag, myResList);
125       ////
126       if (aBreakFlag==1) {
127         return;
128       }
129
130       if (aBreakFlag==2) {// Come back
131         if (j==aNbCases) {
132           aVInfo.RemovePassed();
133           myResList.RemoveFirst();
134           anEdgesCount--;
135           ////
136           return;
137         }
138         else {
139           stopFlag=1;
140           aBreakFlag=0; // Next j if possible
141         }
142       } // end of if (aBreakFlag==2) 
143     } // end of else .i.e. aFoundOut#0
144   } // end of for (j=1; j<=aNbCases; j++)
145 }
146
147 //=======================================================================
148 // function :TopOpeBRepBuild_Tools2d::MakeMapOfShapeVertexInfo
149 // purpose: 
150 //=======================================================================
151   void TopOpeBRepBuild_Tools2d::MakeMapOfShapeVertexInfo(const TopoDS_Wire& aWire,
152                                                       TopOpeBRepBuild_IndexedDataMapOfShapeVertexInfo& M)
153 {
154   TopOpeBRepBuild_VertexInfo empty;
155   TopExp_Explorer exa(aWire,TopAbs_EDGE);
156   for (; exa.More(); exa.Next()) {
157     const TopoDS_Edge& anEdge = TopoDS::Edge(exa.Current());
158     TopExp_Explorer exs(anEdge, TopAbs_VERTEX);
159     for (; exs.More(); exs.Next()) {
160       const TopoDS_Vertex& aVertex= TopoDS::Vertex(exs.Current());
161       Standard_Integer index = M.FindIndex(aVertex);
162       if (!index) 
163         index = M.Add(aVertex, empty);
164       
165       TopOpeBRepBuild_VertexInfo& aVInfo= M(index);
166       aVInfo.SetVertex (aVertex);
167       TopAbs_Orientation anOr=aVertex.Orientation();
168       if      (anOr==TopAbs_FORWARD )   aVInfo.AddOut  (anEdge);
169       else if (anOr==TopAbs_REVERSED)   aVInfo.AddIn   (anEdge);
170     }
171   }
172
173   Standard_Integer i, aNb;
174   aNb=M.Extent();
175   for (i=1; i<=aNb; i++) {
176     TopOpeBRepBuild_VertexInfo& aVInfo=M(i);
177     const TopTools_IndexedMapOfOrientedShape& EdgesIn   =aVInfo.EdgesIn();
178     const TopTools_IndexedMapOfOrientedShape& EdgesOut  =aVInfo.EdgesOut();
179     Standard_Integer aNbEdgesIn, aNbEdgesOut;
180     aNbEdgesIn   =EdgesIn.Extent();
181     aNbEdgesOut  =EdgesOut.Extent();
182     if (aNbEdgesIn!=1 && aNbEdgesOut!=1) {
183       aVInfo.SetSmart (Standard_True);
184     }
185   }
186 }
187
188 //=======================================================================
189 // function :TopOpeBRepBuild_Tools2d::DumpMapOfShapeVertexInfo
190 // purpose: 
191 //=======================================================================
192   void TopOpeBRepBuild_Tools2d::DumpMapOfShapeVertexInfo(const TopOpeBRepBuild_IndexedDataMapOfShapeVertexInfo& M)
193 {
194   Standard_Integer i, aNb;
195   aNb=M.Extent();
196   for (i=1; i<=aNb; i++) {
197     const TopOpeBRepBuild_VertexInfo& aVInfo=M(i);
198     
199     printf(" Vert.#%d, ", i);
200     const TopTools_ListOfShape& aList=aVInfo.ListPassed();
201     
202     if (aList.Extent()) {
203       TopTools_ListIteratorOfListOfShape anIt(aList);
204       for (; anIt.More(); anIt.Next()) {
205         printf("pass,");
206       }
207     }
208     
209     else {
210       printf("none");
211     }
212     printf("\n");
213   }
214 }
215