Salome HOME
Mantis issue 0020603: MakePartition fails on 1st try but succeeds on 2nd try. A fix...
[modules/geom.git] / src / NMTTools / NMTTools_PaveFiller_2.cxx
1 //  Copyright (C) 2007-2008  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 //  File:        NMTTools_PaveFiller_2.cxx
23 //  Created:     Mon Dec  8 12:02:56 2003
24 //  Author:      Peter KURNEV
25 //               <pkv@irinox>
26
27 #include <NMTTools_PaveFiller.ixx>
28
29 #include <Precision.hxx>
30
31 #include <gp_Pnt.hxx>
32
33 #include <Geom_Curve.hxx>
34
35 #include <TopAbs_Orientation.hxx>
36
37 #include <TopoDS.hxx>
38 #include <TopoDS_Edge.hxx>
39 #include <TopoDS_Vertex.hxx>
40 #include <TopoDS_Iterator.hxx>
41
42 #include <BRep_Builder.hxx>
43 #include <BRep_Tool.hxx>
44
45 #include <BOPTools_Pave.hxx>
46 #include <BOPTools_PaveSet.hxx>
47 #include <BOPTools_CArray1OfVEInterference.hxx>
48 #include <BOPTools_VEInterference.hxx>
49
50 #include <BooleanOperations_AncestorsSeqAndSuccessorsSeq.hxx>
51
52 #include <NMTDS_Iterator.hxx>
53 #include <NMTDS_ShapesDataStructure.hxx>
54 #include <NMTDS_InterfPool.hxx>
55
56 #include <Standard_Version.hxx>
57
58 // Modified  Thu Sep 14 14:35:18 2006
59 // Contribution of Samtech www.samcef.com BEGIN
60 #include <BOPTools_IndexedMapOfCoupleOfInteger.hxx>
61 #include <BOPTools_CoupleOfInteger.hxx>
62 #include <BooleanOperations_OnceExplorer.hxx>
63
64 static
65   Standard_Boolean Contains(const TopoDS_Edge& aE,
66                             const TopoDS_Vertex& aV);
67 // Contribution of Samtech www.samcef.com END
68
69 // In OCCT6.3.0sp9 is changed a signature of IntTools_Context::ComputeVE() method
70 #ifdef OCC_VERSION_SERVICEPACK
71 #if (OCC_VERSION_MAJOR << 24 | OCC_VERSION_MINOR << 16 | OCC_VERSION_MAINTENANCE << 8 | OCC_VERSION_SERVICEPACK) > 0x06030008
72 #define OCCT_6_3_0_sp_9
73 #endif
74 #endif
75
76 //=======================================================================
77 // function: PerformVE
78 // purpose:
79 //=======================================================================
80 void NMTTools_PaveFiller::PerformVE()
81 {
82   myIsDone=Standard_False;
83   //
84   Standard_Boolean bJustAdd;
85   Standard_Integer n1, n2, anIndexIn, aFlag, aWhat;
86   Standard_Integer aWith, aNbVEs, aBlockLength, iSDV, nV1;
87   Standard_Real aT;
88 #ifdef OCCT_6_3_0_sp_9
89   Standard_Boolean bToUpdateVertex;
90   Standard_Real aDist;
91 #endif
92   TopoDS_Vertex aV1;
93   TopoDS_Edge aE2;
94   BOPTools_IndexedMapOfCoupleOfInteger aSnareMap;
95   BOPTools_CoupleOfInteger aCouple;
96   //
97   BOPTools_CArray1OfVEInterference& aVEs=myIP->VEInterferences();
98   //
99   myDSIt->Initialize (TopAbs_VERTEX, TopAbs_EDGE);
100   //
101   // BlockLength correction
102   aNbVEs=myDSIt->BlockLength();
103   aBlockLength=aVEs.BlockLength();
104   if (aNbVEs > aBlockLength) {
105     aVEs.SetBlockLength(aNbVEs);
106   }
107   //
108   for (; myDSIt->More(); myDSIt->Next()) {
109     myDSIt->Current(n1, n2, bJustAdd);
110     if (!IsSuccessorsComputed(n1, n2)) {
111       anIndexIn=0;
112       aWhat=n1; // Vertex
113       aWith=n2; // Edge
114       if (myDS->GetShapeType(n1)==TopAbs_EDGE) {
115         aWhat=n2;
116         aWith=n1;
117       }
118       //
119       if(bJustAdd) {
120         continue;
121       }
122       // Edge
123       aE2=TopoDS::Edge(myDS->Shape(aWith));
124       if (BRep_Tool::Degenerated(aE2)){
125         continue;
126       }
127       // Vertex
128       nV1=aWhat;
129       aV1=TopoDS::Vertex(myDS->Shape(aWhat));
130       //
131       iSDV=FindSDVertex(aWhat);
132       if (iSDV) {
133         nV1=iSDV;
134         aV1=TopoDS::Vertex(myDS->Shape(nV1));
135         // Modified to find same domain vertex Thu Sep 14 14:35:18 2006
136         // Contribution of Samtech www.samcef.com BEGIN
137         Standard_Integer nVE, iSDVE, iRet;
138         //
139         BooleanOperations_OnceExplorer aExp(*myDS);
140         iRet=0;
141         aExp.Init(aWith, TopAbs_VERTEX);
142         for (; aExp.More(); aExp.Next()) {
143           nVE=aExp.Current();
144           iSDVE=FindSDVertex(nVE);
145           if (iSDVE==iSDV) {
146             iRet=1;
147             break;
148           }
149         }
150         if (iRet) {
151           continue;
152         }
153       }
154       else {
155         if (Contains(aE2, aV1)) {
156           continue;
157         }
158         // Contribution of Samtech www.samcef.com END
159       }
160       //
161       //modified by NIZNHY-PKV Mon Dec 28 08:58:05 2009f
162 #ifdef OCCT_6_3_0_sp_9
163       aFlag=myContext.ComputeVE (aV1, aE2, aT, bToUpdateVertex, aDist);
164 #else
165       aFlag=myContext.ComputeVE (aV1, aE2, aT);
166 #endif
167       //modified by NIZNHY-PKV Mon Dec 28 08:58:13 2009t
168       //
169       if (!aFlag) {
170         // Add Interference to the Pool
171         BOPTools_VEInterference anInterf (aWhat, aWith, aT);
172         anIndexIn=aVEs.Append(anInterf);
173         //
174         // Add Pave to the Edge's myPavePool
175         aCouple.SetCouple(nV1, aWith);
176         if (!aSnareMap.Contains(aCouple)){
177           aSnareMap.Add(aCouple);
178           //
179           BOPTools_Pave aPave(nV1, aT, BooleanOperations_VertexEdge);
180           aPave.SetInterference(anIndexIn);
181           BOPTools_PaveSet& aPaveSet= myPavePool(myDS->RefEdge(aWith));
182           aPaveSet.Append(aPave);
183         }
184         //
185         // State for the Vertex in DS;
186         myDS->SetState (aWhat, BooleanOperations_ON);
187         // Insert Vertex in Interference Object
188         BOPTools_VEInterference& aVE=aVEs(anIndexIn);
189         aVE.SetNewShape(aWhat);
190         //
191         myIP->Add(aWhat, aWith, Standard_True, NMTDS_TI_VE);
192         //
193         //modified by NIZNHY-PKV Mon Dec 28 09:00:54 2009f
194 #ifdef OCCT_6_3_0_sp_9
195         if (bToUpdateVertex) {
196           BRep_Builder aBB;
197           //
198           aBB.UpdateVertex(aV1, aDist);
199         }
200 #endif
201         //modified by NIZNHY-PKV Mon Dec 28 09:00:57 2009t
202         //
203       } //if (!aFlag) {
204     }
205   }
206   myIsDone=Standard_True;
207 }
208
209 //=======================================================================
210 // function: PrepareEdges
211 // purpose:
212 //=======================================================================
213 void NMTTools_PaveFiller::PrepareEdges()
214 {
215   Standard_Integer  i, nV, ii, aNBSuc, ip, aNbShapesObject;
216   Standard_Real aT;
217   TopAbs_Orientation anOr;
218   TopoDS_Edge   aE;
219   TopoDS_Vertex aV;
220   //
221   aNbShapesObject=myDS->NumberOfShapesOfTheObject();
222   for (i=1; i<=myNbSources; ++i) {
223     if (myDS->GetShapeType(i)==TopAbs_EDGE) {
224       aE=TopoDS::Edge(myDS->Shape(i));
225       //
226       if (BRep_Tool::Degenerated(aE)){
227         continue;
228       }
229       //
230       BOPTools_PaveSet& aPaveSet=myPavePool(myDS->RefEdge(i));
231       //
232       // A <-
233       aNBSuc=myDS->NumberOfSuccessors(i);
234       for (ii=1; ii <=aNBSuc; ii++) {
235         nV=myDS->GetSuccessor(i, ii);
236         anOr=myDS->GetOrientation(i, ii);
237         aV=TopoDS::Vertex(myDS->Shape(nV));
238         aV.Orientation(anOr);
239         aT=BRep_Tool::Parameter(aV, aE);
240         //
241         ip=FindSDVertex(nV);
242         if (ip) {
243           aV=TopoDS::Vertex(myDS->Shape(ip));
244           aV.Orientation(anOr);// XX ? if the edge is closed it'll be amazing result
245           nV=ip;
246         }
247         //
248         BOPTools_Pave aPave(nV, aT);
249         aPaveSet.Append (aPave);
250       }
251     }
252   }
253 }
254
255 // Modified  Thu Sep 14 14:35:18 2006
256 // Contribution of Samtech www.samcef.com BEGIN
257 //=======================================================================
258 //function : Contains
259 //purpose  :
260 //=======================================================================
261 Standard_Boolean Contains(const TopoDS_Edge& aE,
262                           const TopoDS_Vertex& aV)
263 {
264   Standard_Boolean bRet;
265   TopoDS_Iterator aIt;
266   //
267   bRet=Standard_False;
268   aIt.Initialize(aE);
269   for (; aIt.More(); aIt.Next()) {
270     const TopoDS_Shape& aVE=aIt.Value();
271     if (aVE.IsSame(aV)) {
272       bRet=!bRet;
273       break;
274     }
275   }
276   return bRet;
277 }
278 // Contribution of Samtech www.samcef.com END