Salome HOME
Merge from V5_1_main 14/05/2010
[modules/geom.git] / src / GEOMAlgo / GEOMAlgo_VertexSolid.cxx
1 //  Copyright (C) 2007-2010  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 // File:        GEOMAlgo_VertexSolid.cxx
24 // Created:     Wed Jan 12 16:36:40 2005
25 // Author:      Peter KURNEV
26 //              <pkv@irinox>
27 //
28 #include <GEOMAlgo_VertexSolid.ixx>
29
30 #include <gp_Pnt.hxx>
31
32 #include <TopAbs_ShapeEnum.hxx>
33 #include <TopAbs_State.hxx>
34
35 #include <TopTools_ListIteratorOfListOfShape.hxx>
36 #include <TopTools_ListOfShape.hxx>
37 #include <TopTools_IndexedMapOfShape.hxx>
38
39 #include <TopoDS.hxx>
40 #include <TopoDS_Solid.hxx>
41 #include <TopoDS_Vertex.hxx>
42
43 #include <TopExp.hxx>
44
45 #include <BRep_Tool.hxx>
46 #include <BRepClass3d_SolidClassifier.hxx>
47
48 #include <BOPTColStd_Dump.hxx>
49
50 #include <IntTools_Context.hxx>
51
52 #include <BooleanOperations_StateOfShape.hxx>
53 #include <BooleanOperations_ShapesDataStructure.hxx>
54
55 #include <BOPTools_InterferencePool.hxx>
56 #include <BOPTools_CArray1OfVVInterference.hxx>
57 #include <BOPTools_VVInterference.hxx>
58 #include <BOPTools_PaveFiller.hxx>
59 #include <BOPTools_DSFiller.hxx>
60
61 //=======================================================================
62 //function : GEOMAlgo_VertexSolid
63 //purpose  : 
64 //=======================================================================
65 GEOMAlgo_VertexSolid::GEOMAlgo_VertexSolid()
66 :
67   GEOMAlgo_ShapeSolid()
68 {
69 }
70 //=======================================================================
71 //function : ~
72 //purpose  : 
73 //=======================================================================
74 GEOMAlgo_VertexSolid::~GEOMAlgo_VertexSolid()
75 {
76 }
77 //=======================================================================
78 // function: Perform
79 // purpose: 
80 //=======================================================================
81 void GEOMAlgo_VertexSolid::Perform()
82 {
83   myErrorStatus=0;
84   //
85   try {
86     if (myDSFiller==NULL) {
87       myErrorStatus=10;
88       return;
89     }
90     if(!myDSFiller->IsDone()) {
91       myErrorStatus=11;
92       return;
93     }
94     //
95     Standard_Boolean bIsNewFiller;
96     Standard_Integer aNbF;
97     TopTools_IndexedMapOfShape aM;
98     //
99     const BooleanOperations_ShapesDataStructure& aDS=myDSFiller->DS();
100     const TopoDS_Shape& aObj=aDS.Object();
101     //
102     TopExp::MapShapes(aObj, TopAbs_FACE, aM);
103     aNbF=aM.Extent();
104     myRank=(aNbF) ? 2 : 1;
105     //
106     bIsNewFiller=myDSFiller->IsNewFiller();
107     
108     if (bIsNewFiller) {
109       Prepare();
110       myDSFiller->SetNewFiller(!bIsNewFiller);
111     }
112     BuildResult();
113   }
114   //
115   catch (Standard_Failure) {
116     myErrorStatus = 12;
117   }
118
119 //=======================================================================
120 // function: Prepare
121 // purpose: 
122 //=======================================================================
123 void GEOMAlgo_VertexSolid::Prepare()
124 {
125   Standard_Integer i, iBeg, iEnd, aNbVV, j, n1, n2, iFound;
126   Standard_Real aTol;
127   TopAbs_State aSt;
128   TopAbs_ShapeEnum aType;
129   BooleanOperations_StateOfShape aState;
130   gp_Pnt aP3D;
131   //
132   const BooleanOperations_ShapesDataStructure& aDS=myDSFiller->DS();
133   BooleanOperations_ShapesDataStructure* pDS=(BooleanOperations_ShapesDataStructure*)&aDS;
134   const BOPTools_InterferencePool& aIP=myDSFiller->InterfPool();
135   BOPTools_InterferencePool* pIP=(BOPTools_InterferencePool*) &aIP;
136   BOPTools_CArray1OfVVInterference& aVVs=pIP->VVInterferences();
137   const BOPTools_PaveFiller& aPF=myDSFiller->PaveFiller();
138   BOPTools_PaveFiller* pPF=(BOPTools_PaveFiller*)&aPF; 
139   IntTools_Context& aCtx=pPF->ChangeContext();
140   //
141   const TopoDS_Shape& aObj=aDS.Object();
142   const TopoDS_Shape& aTool=aDS.Tool();
143   //
144   const TopoDS_Solid& aSolid=(myRank==1) ? TopoDS::Solid(aTool) : TopoDS::Solid(aObj);
145   const TopoDS_Shape& aSV   =(myRank==1)? aObj : aTool;
146   //
147   BRepClass3d_SolidClassifier& aSC=aCtx.SolidClassifier(aSolid);
148   //
149   iBeg=1;
150   iEnd=aDS.NumberOfShapesOfTheObject();
151   if (myRank==2) {
152     iBeg=iEnd+1;
153     iEnd=aDS.NumberOfSourceShapes();
154   }
155   //
156   for (i=iBeg; i<=iEnd; ++i) {
157     aType=aDS.GetShapeType(i);
158     if (aType!=TopAbs_VERTEX) {
159       continue;
160     }
161     //
162     const TopoDS_Vertex& aV=TopoDS::Vertex(aDS.Shape(i));
163     //
164     aState=aDS.GetState(i);
165     if (aState==BooleanOperations_ON ||
166         aState==BooleanOperations_IN ||
167         aState==BooleanOperations_OUT) {
168       continue;
169     }
170     //
171     iFound=0;
172     aNbVV=aVVs.Extent();
173     for (j=1; j<=aNbVV; ++j) {
174       BOPTools_VVInterference& aVV=aVVs(j);
175       aVV.Indices(n1, n2);
176       if (n1==i || n2==i) {
177         pDS->SetState (n1, BooleanOperations_ON);
178         pDS->SetState (n2, BooleanOperations_ON);
179         iFound=1;
180         break;
181       } 
182     }
183     if (iFound) {
184       continue;
185     }
186     // 
187     aP3D=BRep_Tool::Pnt(aV);
188     aTol=1.E-7;
189     aSC.Perform(aP3D, aTol);
190     aSt=aSC.State();
191     if (aSt==TopAbs_IN) {
192       pDS->SetState (i, BooleanOperations_IN);
193     }
194     else if (aSt==TopAbs_OUT) {
195       pDS->SetState (i, BooleanOperations_OUT);
196     }
197   }
198 }
199 //=======================================================================
200 // function: BuildResult
201 // purpose: 
202 //=======================================================================
203 void GEOMAlgo_VertexSolid::BuildResult()
204 {
205   const BooleanOperations_ShapesDataStructure& aDS=myDSFiller->DS();
206   //
207   Standard_Integer i, iBeg, iEnd;
208   TopAbs_ShapeEnum aType;
209   BooleanOperations_StateOfShape aState;
210   //
211   myLSIN.Clear();
212   myLSOUT.Clear();
213   myLSON.Clear();
214   //
215   iBeg=1;
216   iEnd=aDS.NumberOfShapesOfTheObject();
217   if (myRank==2) {
218     iBeg=iEnd+1;
219     iEnd=aDS.NumberOfSourceShapes();
220   }
221   //
222   for (i=iBeg; i<=iEnd; ++i) {
223     aType=aDS.GetShapeType(i);
224     if (aType!=TopAbs_VERTEX) {
225       continue;
226     }
227     const TopoDS_Shape& aV=aDS.Shape(i);
228     aState=aDS.GetState(i);
229     //
230     if (aState==BooleanOperations_IN) {
231       myLSIN.Append(aV);
232     }
233     else if (aState==BooleanOperations_OUT) {
234       myLSOUT.Append(aV);
235     }
236     else if (aState==BooleanOperations_ON) {
237       myLSON.Append(aV);
238     }
239   }
240 }