Back to index

salome-geom  6.5.0
NMTTools_PaveFiller_4.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 //  File:        NMTTools_PaveFiller_4.cxx
00023 //  Created:     Mon Dec  8 17:08:58 2003
00024 //  Author:      Peter KURNEV
00025 
00026 #include <NMTTools_PaveFiller.hxx>
00027 
00028 #include <stdio.h>
00029 #include <Precision.hxx>
00030 
00031 #include <gp_XYZ.hxx>
00032 #include <gp_Pnt.hxx>
00033 #include <Bnd_Box.hxx>
00034 
00035 #include <GeomAPI_ProjectPointOnCurve.hxx>
00036 
00037 #include <TColStd_MapOfInteger.hxx>
00038 #include <TColStd_IndexedMapOfInteger.hxx>
00039 #include <TColStd_ListIteratorOfListOfInteger.hxx>
00040 #include <TColStd_MapIteratorOfMapOfInteger.hxx>
00041 
00042 #include <TopoDS.hxx>
00043 #include <TopoDS_Edge.hxx>
00044 #include <TopoDS_Vertex.hxx>
00045 #include <TopoDS_Compound.hxx>
00046 
00047 #include <TopTools_IndexedMapOfShape.hxx>
00048 #include <TopTools_ListIteratorOfListOfShape.hxx>
00049 #include <TopTools_DataMapIteratorOfDataMapOfShapeListOfShape.hxx>
00050 #include <TopTools_DataMapOfShapeListOfShape.hxx>
00051 #include <TopTools_ListOfShape.hxx>
00052 #include <TopTools_DataMapOfShapeShape.hxx>
00053 
00054 #include <BRep_Tool.hxx>
00055 #include <BRep_Builder.hxx>
00056 #include <BRepBndLib.hxx>
00057 
00058 #include <BOPTColStd_Dump.hxx>
00059 #include <BOPTColStd_Failure.hxx>
00060 
00061 #include <IntTools_ShrunkRange.hxx>
00062 #include <IntTools_Range.hxx>
00063 #include <IntTools_CommonPrt.hxx>
00064 #include <IntTools_SequenceOfRanges.hxx>
00065 #include <IntTools_EdgeEdge.hxx>
00066 #include <IntTools_SequenceOfCommonPrts.hxx>
00067 #include <IntTools_Tools.hxx>
00068 #include <IntTools_Context.hxx>
00069 
00070 #include <BOPTools_Pave.hxx>
00071 #include <BOPTools_PaveSet.hxx>
00072 #include <BOPTools_PaveBlockIterator.hxx>
00073 #include <BOPTools_PaveBlock.hxx>
00074 #include <BOPTools_CArray1OfEEInterference.hxx>
00075 #include <BOPTools_EEInterference.hxx>
00076 #include <BOPTools_ListOfPaveBlock.hxx>
00077 #include <BOPTools_ListIteratorOfListOfPaveBlock.hxx>
00078 #include <BOPTools_CArray1OfVVInterference.hxx>
00079 #include <BOPTools_VVInterference.hxx>
00080 #include <BOPTools_CArray1OfEEInterference.hxx>
00081 #include <BOPTools_Tools.hxx>
00082 #include <BOPTools_IDMapOfPaveBlockIMapOfPaveBlock.hxx>
00083 #include <BOPTools_IMapOfPaveBlock.hxx>
00084 #include <BOPTools_ListIteratorOfListOfPave.hxx>
00085 #include <BOPTools_SequenceOfPaveBlock.hxx>
00086 
00087 #include <BooleanOperations_AncestorsSeqAndSuccessorsSeq.hxx>
00088 #include <BooleanOperations_IndexedDataMapOfShapeInteger.hxx>
00089 #include <BooleanOperations_KindOfInterference.hxx>
00090 
00091 #include <NMTDS_Iterator.hxx>
00092 #include <NMTDS_ShapesDataStructure.hxx>
00093 #include <NMTDS_IndexedDataMapOfIntegerShape.hxx>
00094 #include <NMTDS_IndexedDataMapOfShapeBox.hxx>
00095 #include <NMTDS_BoxBndTree.hxx>
00096 #include <NCollection_UBTreeFiller.hxx>
00097 #include <NMTDS_InterfPool.hxx>
00098 
00099 #include <NMTTools_IndexedDataMapOfIndexedMapOfInteger.hxx>
00100 #include <NMTTools_ListOfCommonBlock.hxx>
00101 #include <NMTTools_CommonBlock.hxx>
00102 #include <NMTTools_ListIteratorOfListOfCommonBlock.hxx>
00103 
00104 #include <TColStd_ListOfInteger.hxx>
00105 #include <TColStd_ListIteratorOfListOfInteger.hxx>
00106 #include <BRepBndLib.hxx>
00107 #include <BOPTools_CArray1OfVSInterference.hxx>
00108 #include <BOPTools_VSInterference.hxx>
00109 #include <TColStd_MapOfInteger.hxx>
00110 #include <TColStd_MapIteratorOfMapOfInteger.hxx>
00111 
00112 static
00113   void TreatNewVertices(const BooleanOperations_IndexedDataMapOfShapeInteger& aMapVI,
00114                         TopTools_DataMapOfShapeListOfShape& myImages,
00115                         TopTools_DataMapOfShapeShape& myOrigins);
00116 
00117 static
00118   void MakeNewVertex(const TopTools_ListOfShape& aLV,
00119                      TopoDS_Vertex& aNewVertex);
00120 
00121 static
00122   void VertexParameters(const IntTools_CommonPrt& aCPart,
00123                         Standard_Real& aT1,
00124                         Standard_Real& aT2);
00125 
00126 static
00127   Standard_Boolean IsOnPave(const Standard_Real& aT1,
00128                             const IntTools_Range& aRange,
00129                             const Standard_Real& aTolerance);
00130 
00131 // static
00132 //   void EECommonBlocks(const BOPTools_IDMapOfPaveBlockIMapOfPaveBlock& aMapCB);
00133 
00134 static
00135   void ProcessBlock(const BOPTools_PaveBlock& aPB,
00136                     const BOPTools_IDMapOfPaveBlockIMapOfPaveBlock& aMapCB,
00137                     BOPTools_IMapOfPaveBlock& aProcessedBlocks,
00138                     BOPTools_IMapOfPaveBlock& aChain);
00139 
00140 static
00141   void FindChains(const BOPTools_IDMapOfPaveBlockIMapOfPaveBlock& aMapCB,
00142                   NMTTools_ListOfCommonBlock& aLCB);
00143 
00144 //=======================================================================
00145 // function: PerformEE
00146 // purpose:
00147 //=======================================================================
00148 void NMTTools_PaveFiller::PerformEE()
00149 {
00150   myIsDone=Standard_False;
00151   //
00152   Standard_Boolean bJustAdd;
00153   Standard_Integer n1, n2, anIndexIn, nE1, nE2, aNbVEs, aBlockLength;
00154   Standard_Integer aTmp, aWhat, aWith, i, aNbCPrts, aDiscretize=30;
00155   Standard_Integer aNbLPB1, aNbLPB2;
00156   Standard_Real aTolE1, aTolE2, aDeflection=0.01;
00157   BOPTools_ListIteratorOfListOfPaveBlock anIt1, anIt2;
00158   TopoDS_Edge aEWhat, aEWith;
00159   TopoDS_Vertex aNewVertex;
00160   BooleanOperations_IndexedDataMapOfShapeInteger aMapVI;
00161   BOPTools_IDMapOfPaveBlockIMapOfPaveBlock aMapCB;
00162   //
00163   BOPTools_CArray1OfEEInterference& aEEs=myIP->EEInterferences();
00164   //
00165   myDSIt->Initialize(TopAbs_EDGE, TopAbs_EDGE);
00166   //
00167   // BlockLength correction
00168   aNbVEs=myDSIt->BlockLength();
00169   aBlockLength=aEEs.BlockLength();
00170   if (aNbVEs > aBlockLength) {
00171     aEEs.SetBlockLength(aNbVEs);
00172   }
00173   //
00174   for (; myDSIt->More(); myDSIt->Next()) {
00175     myDSIt->Current(n1, n2, bJustAdd);
00176     anIndexIn = 0;
00177     nE1=n1;
00178     nE2=n2;
00179     //
00180     if(bJustAdd) {
00181       continue;
00182     }
00183     //
00184     const TopoDS_Edge aE1=TopoDS::Edge(myDS->Shape(nE1));//mpv
00185     const TopoDS_Edge aE2=TopoDS::Edge(myDS->Shape(nE2));//mpv
00186     //
00187     if (BRep_Tool::Degenerated(aE1) || BRep_Tool::Degenerated(aE2)){
00188       continue;
00189     }
00190     //
00191     aTolE1=BRep_Tool::Tolerance(aE1);
00192     aTolE2=BRep_Tool::Tolerance(aE2);
00193     //
00194     BOPTools_ListOfPaveBlock& aLPB1=mySplitShapesPool(myDS->RefEdge(nE1));
00195     BOPTools_ListOfPaveBlock& aLPB2=mySplitShapesPool(myDS->RefEdge(nE2));
00196     //
00197     // Modified  Thu Sep 14 14:35:18 2006
00198     // Contribution of Samtech www.samcef.com BEGIN
00199     aNbLPB1=aLPB1.Extent();
00200     aNbLPB2=aLPB2.Extent();
00201     //
00202     //if (aE1.IsSame(aE2) && aNbLPB1==1 && aNbLPB2==1) {
00203     //  continue;
00204     //}
00205     // Contribution of Samtech www.samcef.com END
00206     //
00207     for (anIt1.Initialize(aLPB1); anIt1.More(); anIt1.Next()) {
00208       BOPTools_PaveBlock& aPB1=anIt1.Value();
00209       const IntTools_ShrunkRange& aShrunkRange1=aPB1.ShrunkRange();
00210       //
00211       const IntTools_Range& aSR1=aShrunkRange1.ShrunkRange();
00212       const Bnd_Box&        aBB1=aShrunkRange1.BndBox();
00213       //
00214       for (anIt2.Initialize(aLPB2); anIt2.More(); anIt2.Next()) {
00215         BOPTools_PaveBlock& aPB2=anIt2.Value();
00216         const IntTools_ShrunkRange& aShrunkRange2=aPB2.ShrunkRange();
00217         //
00218         const IntTools_Range& aSR2=aShrunkRange2.ShrunkRange();
00219         const Bnd_Box&        aBB2=aShrunkRange2.BndBox();
00220         //
00221         if (aBB1.IsOut (aBB2)) {
00222           continue;
00223         }
00224         //
00225         // EE
00226         IntTools_EdgeEdge aEE;
00227         aEE.SetEdge1 (aE1);
00228         aEE.SetEdge2 (aE2);
00229         aEE.SetTolerance1 (aTolE1);
00230         aEE.SetTolerance2 (aTolE2);
00231         aEE.SetDiscretize (aDiscretize);
00232         aEE.SetDeflection (aDeflection);
00233         //
00234         IntTools_Range anewSR1 = aSR1;
00235         IntTools_Range anewSR2 = aSR2;
00236         //
00237         BOPTools_Tools::CorrectRange (aE1, aE2, aSR1, anewSR1);
00238         BOPTools_Tools::CorrectRange (aE2, aE1, aSR2, anewSR2);
00239         //
00240         aEE.SetRange1(anewSR1);
00241         aEE.SetRange2(anewSR2);
00242         //
00243         aEE.Perform();
00244         //
00245         anIndexIn=0;
00246         //
00247         if (aEE.IsDone()) {
00248           // reverse order if it is necessary
00249           aEWhat=aE1;
00250           aEWith=aE2;
00251           aWhat=nE1;
00252           aWith=nE2;
00253           if (aEE.Order()) {
00254             aTmp=aWhat;
00255             aWhat=aWith;
00256             aWith=aTmp;
00257             aEWhat=aE2;
00258             aEWith=aE1;
00259           }
00260           //
00261           const IntTools_SequenceOfCommonPrts& aCPrts=aEE.CommonParts();
00262           aNbCPrts=aCPrts.Length();
00263           for (i=1; i<=aNbCPrts; i++) {
00264             const IntTools_CommonPrt& aCPart=aCPrts(i);
00265             const IntTools_SequenceOfRanges& aRanges2=aCPart.Ranges2();
00266             //
00267             anIndexIn=0;
00268             //
00269             TopAbs_ShapeEnum aType=aCPart.Type();
00270             switch (aType) {
00271               case TopAbs_VERTEX:  {
00272                 Standard_Real aT1, aT2, aTol=Precision::PConfusion();
00273                 Standard_Boolean bIsOnPave1, bIsOnPave2;
00274                 IntTools_Range aR1, aR2;
00275                 //
00276                 VertexParameters(aCPart, aT1, aT2);
00277                 //
00278                 //decide to keep the pave or not
00279                 aR1 = (aEE.Order()) ? anewSR2 : anewSR1;
00280                 aR2 = (aEE.Order()) ? anewSR1 : anewSR2;
00281                 //
00282                 aTol=0.8*aTol;
00283                 bIsOnPave1=IsOnPave(aT1, aR1, aTol);
00284                 bIsOnPave2=IsOnPave(aT2, aR2, aTol);
00285                 //
00286                 if(bIsOnPave1 || bIsOnPave2) {
00287                    continue;
00288                 }
00289                 //
00290                 BOPTools_Tools::MakeNewVertex(aEWhat, aT1, aEWith, aT2, aNewVertex);
00291                 //
00292                 {
00293                   Standard_Integer nV11, nV12, nV21, nV22, nVS[2], k, j, iFound;
00294                   Standard_Real aTolVx, aTolVnew, aD2, aDT2;
00295                   TColStd_MapOfInteger aMV;
00296                   gp_Pnt aPnew, aPx;
00297                   //
00298                   iFound=0;
00299                   j=-1;
00300                   nV11=aPB1.Pave1().Index();
00301                   nV12=aPB1.Pave2().Index();
00302                   nV21=aPB2.Pave1().Index();
00303                   nV22=aPB2.Pave2().Index();
00304                   aMV.Add(nV11);
00305                   aMV.Add(nV12);
00306                   //
00307                   if (aMV.Contains(nV21)) {
00308                     ++j;
00309                     nVS[j]=nV21;
00310                   }
00311                   if (aMV.Contains(nV22)) {
00312                     ++j;
00313                     nVS[j]=nV22;
00314                   }
00315                   //
00316                   aTolVnew=BRep_Tool::Tolerance(aNewVertex);
00317                   aPnew=BRep_Tool::Pnt(aNewVertex);
00318                   //
00319                   for (k=0; k<=j; ++k) {
00320                     const TopoDS_Vertex& aVx=TopoDS::Vertex(myDS->Shape(nVS[k]));
00321                     aTolVx=BRep_Tool::Tolerance(aVx);
00322                     aPx=BRep_Tool::Pnt(aVx);
00323                     aD2=aPnew.SquareDistance(aPx);
00324                     //
00325                     aDT2=100.*(aTolVnew+aTolVx)*(aTolVnew+aTolVx);
00326                     //
00327                     if (aD2<aDT2) {
00328                       iFound=1;
00329                       break;
00330                     }
00331                   }
00332                   //
00333                   if (iFound) {
00334                     continue;
00335                   }
00336                 }
00337                 //
00338                 // Add Interference to the Pool
00339                 BOPTools_EEInterference anInterf (aWhat, aWith, aCPart);
00340                 //
00341                 anIndexIn=aEEs.Append(anInterf);
00342                 // qqf
00343                 {
00344                   myIP->Add(aWhat, aWith, Standard_True, NMTDS_TI_EE);
00345                 }
00346                 // qqt
00347                 //
00348                 // Collect
00349                 aMapVI.Add(aNewVertex, anIndexIn);
00350               }
00351                 break;
00352 
00353               case TopAbs_EDGE: {
00354                 Standard_Integer aNbComPrt2;
00355                 Standard_Boolean aCoinsideFlag;
00356                 //
00357                 aNbComPrt2=aRanges2.Length();
00358                 aCoinsideFlag=IsBlocksCoinside(aPB1, aPB2);
00359                 //
00360                 if (aNbComPrt2>1 || !aCoinsideFlag) {
00361                   break;
00362                 }
00363                 //
00364                 // Fill aMapCB
00365                 if (aMapCB.Contains(aPB1)) {
00366                   BOPTools_IMapOfPaveBlock& aMapPB=aMapCB.ChangeFromKey(aPB1);
00367                   aMapPB.Add(aPB1);
00368                   aMapPB.Add(aPB2);
00369                 }
00370                 else {
00371                   BOPTools_IMapOfPaveBlock aMapPB;
00372                   aMapPB.Add(aPB1);
00373                   aMapPB.Add(aPB2);
00374                   aMapCB.Add(aPB1, aMapPB);
00375                 }
00376                 //
00377                 if (aMapCB.Contains(aPB2)) {
00378                   BOPTools_IMapOfPaveBlock& aMapPB=aMapCB.ChangeFromKey(aPB2);
00379                   aMapPB.Add(aPB1);
00380                   aMapPB.Add(aPB2);
00381                 }
00382                 else {
00383                   BOPTools_IMapOfPaveBlock aMapPB;
00384                   aMapPB.Add(aPB1);
00385                   aMapPB.Add(aPB2);
00386                   aMapCB.Add(aPB2, aMapPB);
00387                 }
00388                 // qqf
00389                 {
00390                   myIP->Add(aWhat, aWith, Standard_True, NMTDS_TI_EE);
00391                 }
00392                 // qqt
00393               }
00394                 break;
00395             default:
00396               break;
00397             } // switch (aType)
00398           } // for (i=1; i<=aNbCPrts; i++)
00399         }// if (aEE.IsDone())
00400       } // for (; anIt2.More(); anIt2.Next())
00401     } // for (; anIt1.More(); anIt1.Next())
00402   }// for (; myDSIt.More(); myDSIt.Next())
00403   //
00404   {
00405     NMTTools_ListOfCommonBlock aLCB;
00406     //
00407     FindChains(aMapCB, aLCB);
00408     EENewVertices (aMapVI);
00409     //TreatPaveBlocks(*this, aLCB);
00410     TreatPaveBlocks(aLCB);
00411     ReplaceCommonBlocks(aLCB);
00412   }
00413   //
00414   PerformVF1();
00415   //
00416   myIsDone=Standard_True;
00417 }
00418 
00419 //=======================================================================
00420 // function:TreatPaveBlocks
00421 // purpose:
00422 //=======================================================================
00423 void NMTTools_PaveFiller::TreatPaveBlocks (NMTTools_ListOfCommonBlock& theLCB)
00424 {
00425   Standard_Boolean bFound;
00426   Standard_Integer nE, nV, nVp, iFlag;
00427   Standard_Real aT;
00428   TColStd_MapOfInteger aMI;
00429   TColStd_MapIteratorOfMapOfInteger aItMI;
00430   NMTTools_ListIteratorOfListOfCommonBlock aItLCB;
00431   BOPTools_ListIteratorOfListOfPaveBlock aItLPB;
00432   BOPTools_ListIteratorOfListOfPave aItLP;
00433   //
00434   aItLCB.Initialize(theLCB);
00435   for (; aItLCB.More(); aItLCB.Next()) {
00436     const NMTTools_CommonBlock& aCB=aItLCB.Value();
00437     //
00438     aMI.Clear();
00439     const BOPTools_ListOfPaveBlock& aLPB=aCB.PaveBlocks();
00440     //
00441     // 1 -> aMI
00442     aItLPB.Initialize(aLPB);
00443     for (; aItLPB.More(); aItLPB.Next()) {
00444       const BOPTools_PaveBlock& aPB=aItLPB.Value();
00445       nE=aPB.OriginalEdge();
00446       BOPTools_PaveSet& aPaveSet=myPavePoolNew(myDS->RefEdge(nE));
00447       BOPTools_ListOfPave& aLP=aPaveSet.ChangeSet();
00448       //
00449       aItLP.Initialize(aLP);
00450       for (; aItLP.More(); aItLP.Next()) {
00451         const BOPTools_Pave& aPave=aItLP.Value();
00452         nV=aPave.Index();
00453         aMI.Add(nV);
00454       }
00455     }//for (; anItLPB.More(); anItLPB.Next()) {
00456     //
00457     // 2
00458     aItLPB.Initialize(aLPB);
00459     for (; aItLPB.More(); aItLPB.Next()) {
00460       const BOPTools_PaveBlock& aPB=aItLPB.Value();
00461       nE=aPB.OriginalEdge();
00462       BOPTools_PaveSet& aPaveSet=myPavePoolNew(myDS->RefEdge(nE));
00463       BOPTools_ListOfPave& aLP=aPaveSet.ChangeSet();
00464       //
00465       aItMI.Initialize(aMI);
00466       for (; aItMI.More(); aItMI.Next()) {
00467         nV=aItMI.Key();
00468         bFound=Standard_False;
00469         aItLP.Initialize(aLP);
00470         for (; aItLP.More(); aItLP.Next()) {
00471           const BOPTools_Pave& aPave=aItLP.Value();
00472           nVp=aPave.Index();
00473           if (nVp==nV) {
00474             bFound=!bFound;
00475             break;
00476           }
00477         }
00478         //
00479         if (!bFound) {
00480           // Append Pave of nV to rhe edge nE
00481           const TopoDS_Edge& aE=*(TopoDS_Edge*)(&myDS->Shape(nE));
00482           const TopoDS_Vertex& aV= *(TopoDS_Vertex*)(&myDS->Shape(nV));
00483           iFlag=myContext->ComputeVE (aV, aE, aT);
00484           if (!iFlag) {
00485             BOPTools_Pave aPave;
00486             //
00487             aPave.SetInterference(-1);
00488             aPave.SetType (BooleanOperations_EdgeEdge);
00489             aPave.SetIndex(nV);
00490             aPave.SetParam(aT);
00491             aPaveSet.Append(aPave);
00492           }
00493         }
00494       }//for (; aItMI.More(); aItMI.Next()) {
00495     }//for (; anItLPB.More(); anItLPB.Next()) {
00496   }
00497 }
00498 
00499 //=======================================================================
00500 // function:EECommonBlocks
00501 // purpose:
00502 //=======================================================================
00503 void NMTTools_PaveFiller::EECommonBlocks(const BOPTools_IDMapOfPaveBlockIMapOfPaveBlock& aMapCB)
00504 {
00505   NMTTools_ListOfCommonBlock aLCB;
00506   //
00507   FindChains(aMapCB, aLCB);
00508   ReplaceCommonBlocks(aLCB);
00509 }
00510 
00511 //=======================================================================
00512 // function:EENewVertices
00513 // purpose:
00514 //=======================================================================
00515 void NMTTools_PaveFiller::EENewVertices (const BooleanOperations_IndexedDataMapOfShapeInteger& aMapVI)
00516 {
00517   Standard_Integer aNb, aNbVSD, nVnew, nIEE, nE[2], j, iFlag;
00518   Standard_Real aT;
00519   TopoDS_Edge aE;
00520   TopTools_DataMapOfShapeListOfShape myImages;
00521   TopTools_DataMapOfShapeShape myOrigins;
00522   TopTools_DataMapIteratorOfDataMapOfShapeListOfShape aItIm;
00523   TopTools_ListIteratorOfListOfShape aIt;
00524   BooleanOperations_AncestorsSeqAndSuccessorsSeq anASSeq;
00525   TColStd_MapOfInteger aMFence;
00526   BOPTools_Pave aPave;
00527   //
00528   BOPTools_CArray1OfEEInterference& aEEs=myIP->EEInterferences();
00529   //
00530   aNb=aMapVI.Extent();
00531   if (!aNb) { // no new vertices, no new problems
00532     return;
00533   }
00534   //
00535   // 0.
00536   if (aNb==1) {
00537     TopoDS_Vertex aV1=TopoDS::Vertex(aMapVI.FindKey(1));
00538     EENewVertices(aV1, aMapVI);
00539     return;
00540   }
00541   //
00542   // 1.
00543   TreatNewVertices(aMapVI, myImages, myOrigins);
00544   //
00545   aItIm.Initialize(myImages);
00546   for (; aItIm.More(); aItIm.Next()) {
00547     const TopoDS_Vertex& aVnew=TopoDS::Vertex(aItIm.Key());
00548     const TopTools_ListOfShape& aLVSD=aItIm.Value();
00549     //
00550     aNbVSD=aLVSD.Extent();
00551     if (aNbVSD==1) {// simple case aVnew=aVold
00552       EENewVertices(aVnew, aMapVI);
00553       continue;
00554     }
00555     //
00556     // aNbVSD>1
00557     myDS->InsertShapeAndAncestorsSuccessors(aVnew, anASSeq);
00558     nVnew=myDS->NumberOfInsertedShapes();
00559     myDS->SetState(nVnew, BooleanOperations_ON);
00560     //
00561     aMFence.Clear();
00562     aIt.Initialize(aLVSD);
00563     for (; aIt.More(); aIt.Next()) {
00564       const TopoDS_Vertex& aVold=TopoDS::Vertex(aIt.Value());
00565       nIEE=aMapVI.FindFromKey(aVold);
00566       BOPTools_EEInterference& aEE=aEEs(nIEE);
00567       aEE.Indices(nE[0], nE[1]);
00568       aEE.SetNewShape(nVnew);
00569       //
00570       for (j=0; j<2; ++j) {
00571         if (aMFence.Add(nE[j])) {
00572           aE=TopoDS::Edge(myDS->Shape(nE[j]));
00573           iFlag=myContext->ComputeVE (aVnew, aE, aT);
00574           if (!iFlag) {
00575             aPave.SetInterference(-1);
00576             aPave.SetType (BooleanOperations_EdgeEdge);
00577             aPave.SetIndex(nVnew);
00578             aPave.SetParam(aT);
00579             //
00580             BOPTools_PaveSet& aPaveSet=myPavePoolNew(myDS->RefEdge(nE[j]));
00581             aPaveSet.Append(aPave);
00582           }
00583         }// if (aMFence.Add(nE[j])) {
00584       }// for (j=0; j<2; ++j) {
00585     }//for (; aIt.More(); aIt.Next()) {
00586   }// for (; aItIm.More(); aItIm.Next())
00587 }
00588 //
00589 // case: use_02
00590 // completely rewritten
00591 //=======================================================================
00592 //function : TreatNewVertices
00593 //purpose  :
00594 //=======================================================================
00595 void TreatNewVertices(const BooleanOperations_IndexedDataMapOfShapeInteger& aMapVI,
00596                       TopTools_DataMapOfShapeListOfShape& myImages,
00597                       TopTools_DataMapOfShapeShape& myOrigins)
00598 {
00599   Standard_Integer j, i, aNbV, aNbVSD;
00600   Standard_Real aTol;
00601   TColStd_ListIteratorOfListOfInteger aIt;
00602   TopoDS_Shape aSTmp, aVF;
00603   TopoDS_Vertex aVnew;
00604   TopTools_IndexedMapOfShape aMV, aMVProcessed;
00605   TopTools_ListIteratorOfListOfShape aItS;
00606   TopTools_DataMapIteratorOfDataMapOfShapeListOfShape aItIm;
00607   TopTools_DataMapOfShapeListOfShape aMVV;
00608   NMTDS_IndexedDataMapOfIntegerShape aMIS;
00609   NMTDS_IndexedDataMapOfShapeBox aMSB;
00610   //
00611   NMTDS_BoxBndTreeSelector aSelector;
00612   NMTDS_BoxBndTree aBBTree;
00613   NCollection_UBTreeFiller <Standard_Integer, Bnd_Box> aTreeFiller(aBBTree);
00614   //
00615   myImages.Clear();
00616   myOrigins.Clear();
00617   //
00618   aNbV=aMapVI.Extent();
00619   for (i=1; i<=aNbV; ++i) {
00620     const TopoDS_Shape& aV=aMapVI.FindKey(i);
00621     aMV.Add(aV);
00622   }
00623   //
00624   for (i=1; i<=aNbV; ++i) {
00625     const TopoDS_Shape& aV=aMV(i);
00626     Bnd_Box aBox;
00627     //
00628     aTol=BRep_Tool::Tolerance(TopoDS::Vertex(aV));
00629     aBox.SetGap(aTol);
00630     BRepBndLib::Add(aV, aBox);
00631     //
00632     aTreeFiller.Add(i, aBox);
00633     //
00634     aMIS.Add(i, aV);
00635     aMSB.Add(aV, aBox);
00636   }
00637   //
00638   aTreeFiller.Fill();
00639   //
00640   // Chains
00641   for (i=1; i<=aNbV; ++i) {
00642     const TopoDS_Shape& aV=aMV(i);
00643     //
00644     if (aMVProcessed.Contains(aV)) {
00645       continue;
00646     }
00647     //
00648     Standard_Integer aNbIP, aIP, aNbIP1, aIP1;
00649     TopTools_ListOfShape aLVSD;
00650     TColStd_MapOfInteger aMIP, aMIP1, aMIPC;
00651     TColStd_MapIteratorOfMapOfInteger aIt1;
00652     //
00653     aMIP.Add(i);
00654     while(1) {
00655       aNbIP=aMIP.Extent();
00656       aIt1.Initialize(aMIP);
00657       for(; aIt1.More(); aIt1.Next()) {
00658         aIP=aIt1.Key();
00659         if (aMIPC.Contains(aIP)) {
00660           continue;
00661         }
00662         //
00663         const TopoDS_Shape& aVP=aMIS.FindFromKey(aIP);
00664         const Bnd_Box& aBoxVP=aMSB.FindFromKey(aVP);
00665         //
00666         aSelector.Clear();
00667         aSelector.SetBox(aBoxVP);
00668         //
00669         aNbVSD=aBBTree.Select(aSelector);
00670         if (!aNbVSD) {
00671           continue;  // it must not be
00672         }
00673         //
00674         const TColStd_ListOfInteger& aLI=aSelector.Indices();
00675         aIt.Initialize(aLI);
00676         for (; aIt.More(); aIt.Next()) {
00677           aIP1=aIt.Value();
00678           if (aMIP.Contains(aIP1)) {
00679             continue;
00680           }
00681           aMIP1.Add(aIP1);
00682         } //for (; aIt.More(); aIt.Next()) {
00683       }//for(; aIt1.More(); aIt1.Next()) {
00684       //
00685       aNbIP1=aMIP1.Extent();
00686       if (!aNbIP1) {
00687         break; // from while(1)
00688       }
00689       //
00690       aIt1.Initialize(aMIP);
00691       for(; aIt1.More(); aIt1.Next()) {
00692         aIP=aIt1.Key();
00693         aMIPC.Add(aIP);
00694       }
00695       //
00696       aMIP.Clear();
00697       aIt1.Initialize(aMIP1);
00698       for(; aIt1.More(); aIt1.Next()) {
00699         aIP=aIt1.Key();
00700         aMIP.Add(aIP);
00701       }
00702       aMIP1.Clear();
00703     }// while(1)
00704     //...
00705     aNbIP=aMIPC.Extent();
00706     if (!aNbIP) {
00707       aMIPC.Add(i);
00708     }
00709     //
00710     aIt1.Initialize(aMIPC);
00711     for(j=0; aIt1.More(); aIt1.Next(), ++j) {
00712       aIP=aIt1.Key();
00713       const TopoDS_Shape& aVP=aMIS.FindFromKey(aIP);
00714       if (!j) {
00715         aVF=aVP;
00716       }
00717       aLVSD.Append(aVP);
00718       aMVProcessed.Add(aVP);
00719     }
00720     myImages.Bind(aVF, aLVSD);
00721   }// for (i=1; i<=aNbV; ++i) {
00722   //------------------------------
00723   //
00724   // Make new vertices
00725   aMV.Clear();
00726   aItIm.Initialize(myImages);
00727   for (; aItIm.More(); aItIm.Next()) {
00728     const TopoDS_Shape& aV=aItIm.Key();
00729     const TopTools_ListOfShape& aLVSD=aItIm.Value();
00730     aNbVSD=aLVSD.Extent();
00731     if (aNbVSD>1) {
00732       aMV.Add(aV);
00733       MakeNewVertex(aLVSD, aVnew);
00734       aMVV.Bind(aVnew, aLVSD);
00735     }
00736   }
00737   //
00738   // UnBind old vertices
00739   aNbV=aMV.Extent();
00740   for (i=1; i<=aNbV; ++i) {
00741     const TopoDS_Shape& aV=aMV(i);
00742     myImages.UnBind(aV);
00743   }
00744   //
00745   // Bind new vertices
00746   aItIm.Initialize(aMVV);
00747   for (; aItIm.More(); aItIm.Next()) {
00748     const TopoDS_Shape& aV=aItIm.Key();
00749     const TopTools_ListOfShape& aLVSD=aItIm.Value();
00750     myImages.Bind(aV, aLVSD);
00751   }
00752   //
00753   // Origins
00754   aItIm.Initialize(myImages);
00755   for (; aItIm.More(); aItIm.Next()) {
00756     const TopoDS_Shape& aV=aItIm.Key();
00757     const TopTools_ListOfShape& aLVSD=aItIm.Value();
00758     //
00759     aItS.Initialize(aLVSD);
00760     for (; aItS.More(); aItS.Next()) {
00761       const TopoDS_Shape& aVSD=aItS.Value();
00762       if (!myOrigins.IsBound(aVSD)) {
00763         myOrigins.Bind(aVSD, aV);
00764       }
00765     }
00766   }
00767 }
00768 
00769 //=======================================================================
00770 //function : MakeNewVertex
00771 //purpose  :
00772 //=======================================================================
00773 void MakeNewVertex(const TopTools_ListOfShape& aLV,
00774                    TopoDS_Vertex& aNewVertex)
00775 {
00776   Standard_Integer aNbV;
00777   Standard_Real aTolV, aD, aDmax;
00778   gp_XYZ aGC;
00779   gp_Pnt aP3D, aPGC;
00780   TopoDS_Vertex aVx;
00781   BRep_Builder aBB;
00782   TopTools_ListIteratorOfListOfShape aIt;
00783   //
00784   aNbV=aLV.Extent();
00785   if (!aNbV) {
00786     return;
00787   }
00788   //
00789   // center of gravity
00790   aGC.SetCoord(0.,0.,0.);
00791   aIt.Initialize(aLV);
00792   for (; aIt.More(); aIt.Next()) {
00793     aVx=TopoDS::Vertex(aIt.Value());
00794     aP3D=BRep_Tool::Pnt(aVx);
00795     aGC+=aP3D.XYZ();
00796   }
00797   aGC/=(Standard_Real)aNbV;
00798   aPGC.SetXYZ(aGC);
00799   //
00800   // tolerance value
00801   aDmax=-1.;
00802   aIt.Initialize(aLV);
00803   for (; aIt.More(); aIt.Next()) {
00804     aVx=TopoDS::Vertex(aIt.Value());
00805     aP3D=BRep_Tool::Pnt(aVx);
00806     aTolV=BRep_Tool::Tolerance(aVx);
00807     aD=aPGC.Distance(aP3D)+aTolV;
00808     if (aD>aDmax) {
00809       aDmax=aD;
00810     }
00811   }
00812   //
00813   aBB.MakeVertex (aNewVertex, aPGC, aDmax);
00814 }
00815 
00816 //=======================================================================
00817 // function:EENewVertices
00818 // purpose:
00819 //=======================================================================
00820 void NMTTools_PaveFiller::EENewVertices (const TopoDS_Vertex& aNewVertex,
00821                                          const BooleanOperations_IndexedDataMapOfShapeInteger& aMapVI)
00822 {
00823   Standard_Integer  i, aNewShape, nE1, nE2;
00824   Standard_Real  aT1, aT2;
00825   BooleanOperations_AncestorsSeqAndSuccessorsSeq anASSeq;
00826   BOPTools_Pave aPave;
00827   //
00828   BOPTools_CArray1OfEEInterference& aEEs=myIP->EEInterferences();
00829   //
00830   // one new vertex case is treated in usual way
00831   //
00832   // Insert New Vertex in DS;
00833   myDS->InsertShapeAndAncestorsSuccessors(aNewVertex, anASSeq);
00834   aNewShape=myDS->NumberOfInsertedShapes();
00835   myDS->SetState (aNewShape, BooleanOperations_ON);
00836   // Insert New Vertex in EE Interference
00837   i=aMapVI.FindFromKey(aNewVertex);
00838   BOPTools_EEInterference& aEEInterf= aEEs(i);
00839   aEEInterf.SetNewShape(aNewShape);
00840   // Extact interference info
00841   aEEInterf.Indices(nE1, nE2);
00842   const IntTools_CommonPrt& aCPart=aEEInterf.CommonPrt();
00843   VertexParameters(aCPart, aT1, aT2);
00844   //
00845   // Add Paves to the myPavePoolNew
00846   aPave.SetInterference(i);
00847   aPave.SetType (BooleanOperations_EdgeEdge);
00848   aPave.SetIndex(aNewShape);
00849   // Pave for edge nE1
00850   aPave.SetParam(aT1);
00851   BOPTools_PaveSet& aPaveSet1=myPavePoolNew(myDS->RefEdge(nE1));
00852   aPaveSet1.Append(aPave);
00853   // Pave for edge nE2
00854   aPave.SetParam(aT2);
00855   BOPTools_PaveSet& aPaveSet2=myPavePoolNew(myDS->RefEdge(nE2));
00856   aPaveSet2.Append(aPave);
00857 }
00858 
00859 //=======================================================================
00860 // function: RefinePavePool
00861 // purpose:
00862 //=======================================================================
00863 void NMTTools_PaveFiller::RefinePavePool()
00864 {
00865   Standard_Integer  i, aNbNew;
00866 
00867   for (i=1; i<=myNbSources; i++) {
00868 
00869     if ((myDS->GetShape(i)).ShapeType()==TopAbs_EDGE) {
00870       BOPTools_PaveSet& aPS= myPavePool(myDS->RefEdge(i));
00871       //
00872       BOPTools_PaveSet& aNewPS= myPavePoolNew(myDS->RefEdge(i));
00873       BOPTools_ListOfPave& aNewLP=aNewPS.ChangeSet();
00874       //
00875       aNbNew=aNewLP.Extent();
00876       if (aNbNew) {
00877         BOPTools_ListIteratorOfListOfPave anIt(aNewLP);
00878         for (; anIt.More(); anIt.Next()) {
00879           const BOPTools_Pave& aPave=anIt.Value();
00880           aPS.Append(aPave);
00881         }
00882         // Clear the ListOfPaveBlock
00883         BOPTools_ListOfPaveBlock& aLPB=mySplitShapesPool(myDS->RefEdge(i));
00884         aLPB.Clear();
00885         // Prepare the paveBlocks for that egde again
00886         PreparePaveBlocks(i);
00887       }
00888       aNewLP.Clear();
00889     }
00890   }
00891 }
00892 
00893 //=======================================================================
00894 // function: PreparePaveBlocks
00895 // purpose:
00896 //=======================================================================
00897 void NMTTools_PaveFiller::PreparePaveBlocks(const TopAbs_ShapeEnum aType1,
00898                                             const TopAbs_ShapeEnum aType2)
00899 {
00900   myIsDone=Standard_False;
00901   //
00902   Standard_Boolean bOk1, bOk2, bOk3, bFlag;
00903   Standard_Integer i, aNb, nE[2], n1, n2, aNbSplits;
00904   TColStd_MapOfInteger aMap;
00905   //
00906   bOk1= (aType1==TopAbs_VERTEX) &&  (aType2==TopAbs_EDGE) ;
00907   bOk2= (aType1==TopAbs_EDGE)   &&  (aType2==TopAbs_EDGE) ;
00908   bOk3= (aType1==TopAbs_EDGE)   &&  (aType2==TopAbs_FACE) ;
00909   if (!bOk1 && !bOk2 && !bOk3) {// error: Type mismatch
00910     return;
00911   }
00912   //
00913   aNb=bOk2 ? 2 : 1;
00914   //
00915   myDSIt->Initialize(aType1, aType2);
00916   for (; myDSIt->More(); myDSIt->Next()) {
00917     myDSIt->Current(n1, n2, bFlag);
00918     //
00919     nE[0]=n1;
00920     nE[1]=n2;
00921     if (myDS->GetShapeType(n1)!=TopAbs_EDGE) {
00922       nE[0]=n2;
00923       nE[1]=n1;
00924     }
00925     //
00926     for (i=0; i<aNb; ++i) {
00927       BOPTools_ListOfPaveBlock& aLPB=mySplitShapesPool(myDS->RefEdge(nE[i]));
00928       aNbSplits=aLPB.Extent();
00929       if (!aNbSplits) {
00930         if (aMap.Add(nE[i])) {
00931           PreparePaveBlocks(nE[i]);
00932           if (!myIsDone) {
00933             return;
00934           }
00935         }
00936       }
00937     }
00938   }// for (; myDSIt.More(); myDSIt.Next())
00939   myIsDone=Standard_True;
00940 }
00941 
00942 //=======================================================================
00943 // function: PreparePaveBlocks
00944 // purpose:
00945 //=======================================================================
00946 void NMTTools_PaveFiller::PreparePaveBlocks(const Standard_Integer nE)
00947 {
00948   myIsDone=Standard_False;
00949   //
00950   char buf[512];
00951   Standard_Integer nV1, nV2, iErr;
00952   TopoDS_Edge aE;
00953   TopoDS_Vertex aV1, aV2;
00954   //
00955   BOPTools_ListOfPaveBlock& aLPB=mySplitShapesPool(myDS->RefEdge(nE));
00956   // Edge
00957   aE=TopoDS::Edge(myDS->Shape(nE));
00958   if (BRep_Tool::Degenerated(aE)) {
00959     myIsDone=Standard_True;
00960     return;
00961   }
00962   //
00963   BOPTools_PaveSet& aPS=myPavePool(myDS->RefEdge(nE));
00964   //
00965   BOPTools_PaveBlockIterator aPBIt(nE, aPS);
00966   for (; aPBIt.More(); aPBIt.Next()) {
00967     BOPTools_PaveBlock& aPB=aPBIt.Value();
00968     const IntTools_Range& aRange=aPB.Range();
00969     //
00970     const BOPTools_Pave& aPave1=aPB.Pave1();
00971     nV1=aPave1.Index();
00972     aV1=TopoDS::Vertex(myDS->GetShape(nV1));
00973     //
00974     const BOPTools_Pave& aPave2=aPB.Pave2();
00975     nV2=aPave2.Index();
00976     aV2=TopoDS::Vertex(myDS->GetShape(nV2));
00977     //
00978     // ShrunkRange
00979     IntTools_ShrunkRange aSR (aE, aV1, aV2, aRange, myContext);
00980     iErr=aSR.ErrorStatus();
00981     if (!aSR.IsDone()) {
00982       sprintf (buf, "Can not obtain ShrunkRange for Edge %d\n", nE);
00983       BOPTColStd_Dump::PrintMessage(buf);
00984       sprintf (buf, "Can not obtain ShrunkRange for Edge %d", nE);
00985       throw
00986         BOPTColStd_Failure(buf) ;
00987     }
00988     //
00989     if (iErr==6) {
00990       sprintf(buf,
00991               "Warning: [PreparePaveBlocks()] Max.Dummy Shrunk Range for Edge %d\n", nE);
00992       BOPTColStd_Dump::PrintMessage(buf);
00993     }
00994     else {
00995       // Check left paves and correct ShrunkRange if it is necessary
00996       CorrectShrunkRanges (0, aPave1, aSR);
00997       CorrectShrunkRanges (1, aPave2, aSR);
00998     }
00999     //
01000     aPB.SetShrunkRange(aSR);
01001     aLPB.Append(aPB);
01002   } //for (; aPBIt.More(); aPBIt.Next())
01003   myIsDone=Standard_True;
01004 }
01005 
01006 //=======================================================================
01007 // function: CorrectShrunkRanges
01008 // purpose:
01009 //=======================================================================
01010 void NMTTools_PaveFiller::CorrectShrunkRanges(const Standard_Integer aSide,
01011                                               const BOPTools_Pave& aPave,
01012                                               IntTools_ShrunkRange& aShrunkRange)
01013 {
01014   BooleanOperations_KindOfInterference aType;
01015   Standard_Integer anIndexInterf ;
01016   //
01017   aType=aPave.Type();
01018   if (aType!=BooleanOperations_EdgeEdge) {
01019     return;
01020   }
01021   //
01022   anIndexInterf=aPave.Interference();
01023   if (anIndexInterf<0) {
01024     // it can be EE interf between E and (e1,e2,..en) -> vertex
01025     // so we can't decide which aEE.CommonPrt() we should take.
01026     return;
01027   }
01028 
01029   BOPTools_CArray1OfEEInterference& aEEs=myIP->EEInterferences();
01030   const BOPTools_EEInterference& aEE=aEEs(anIndexInterf);
01031   const IntTools_CommonPrt& aCP=aEE.CommonPrt();
01032   const TopoDS_Edge& aE1=aCP.Edge1();
01033   const TopoDS_Edge& aE2=aCP.Edge2();
01034 
01035   const IntTools_Range& aSR=aShrunkRange.ShrunkRange();
01036   const TopoDS_Edge& aE=aShrunkRange.Edge();
01037 
01038   IntTools_Range aNewRange;
01039   IntTools_Range aCPRange;
01040 
01041   if (aE1.IsSame(aE)) {
01042     const IntTools_Range& aR1=aCP.Range1();
01043     aCPRange=aR1;
01044   }
01045   if (aE2.IsSame(aE)) {
01046     const IntTools_SequenceOfRanges& aSeqR=aCP.Ranges2();
01047     const IntTools_Range& aR2=aSeqR(1);
01048      aCPRange=aR2;
01049   }
01050   //
01051   Standard_Real aCoeff=1.05, tV, tNV;
01052   tV=aPave.Param();
01053   if (aSide==0) { // Left
01054     if (aCPRange.Last() > aSR.First()) {
01055       tNV=aCPRange.Last();
01056       tNV=tV+aCoeff*(tNV-tV);
01057       aNewRange.SetFirst(tNV);
01058       aNewRange.SetLast (aSR.Last());
01059       if(aNewRange.First() < aNewRange.Last()) {
01060         aShrunkRange.SetShrunkRange(aNewRange);
01061       }
01062     }
01063   }
01064   else { // Right
01065     if (aCPRange.First() < aSR.Last()) {
01066       tNV=aCPRange.First();
01067       tNV=tV-aCoeff*(tV-tNV);
01068       aNewRange.SetFirst(aSR.First());
01069       aNewRange.SetLast (tNV);
01070 
01071       if(aNewRange.First() < aNewRange.Last()) {
01072         aShrunkRange.SetShrunkRange(aNewRange);
01073       }
01074     }
01075   }
01076 }
01077 
01078 //=======================================================================
01079 // function:  IsBlocksCoinside
01080 // purpose:
01081 //=======================================================================
01082 Standard_Boolean NMTTools_PaveFiller::IsBlocksCoinside(const BOPTools_PaveBlock& aPB1,
01083                                                        const BOPTools_PaveBlock& aPB2) const
01084 {
01085   Standard_Boolean bRetFlag=Standard_True;
01086   Standard_Real aTolV11, aTolV12, aTolV21, aTolV22;
01087   Standard_Real d1121, d1122, d1222, d1221, aTolSum, aCoeff=1.05;
01088   gp_Pnt aP11, aP12, aP21, aP22;
01089 
01090   const TopoDS_Vertex aV11=TopoDS::Vertex(myDS->Shape(aPB1.Pave1().Index()));//mpv
01091   const TopoDS_Vertex aV12=TopoDS::Vertex(myDS->Shape(aPB1.Pave2().Index()));//mpv
01092   const TopoDS_Vertex aV21=TopoDS::Vertex(myDS->Shape(aPB2.Pave1().Index()));//mpv
01093   const TopoDS_Vertex aV22=TopoDS::Vertex(myDS->Shape(aPB2.Pave2().Index()));//mpv
01094 
01095   aTolV11=BRep_Tool::Tolerance(aV11);
01096   aTolV12=BRep_Tool::Tolerance(aV12);
01097   aTolV21=BRep_Tool::Tolerance(aV21);
01098   aTolV22=BRep_Tool::Tolerance(aV22);
01099 
01100   aP11=BRep_Tool::Pnt(aV11);
01101   aP12=BRep_Tool::Pnt(aV12);
01102   aP21=BRep_Tool::Pnt(aV21);
01103   aP22=BRep_Tool::Pnt(aV22);
01104 
01105   d1121=aP11.Distance(aP21);
01106   aTolSum=aCoeff*(aTolV11+aTolV21);
01107   if (d1121<aTolSum) {
01108     d1222=aP12.Distance(aP22);
01109     aTolSum=aCoeff*(aTolV12+aTolV22);
01110     if (d1222<aTolSum) {
01111       return bRetFlag;
01112     }
01113   }
01114   //
01115   d1122=aP11.Distance(aP22);
01116   aTolSum=aCoeff*(aTolV11+aTolV22);
01117   if (d1122<aTolSum) {
01118     d1221=aP12.Distance(aP21);
01119     aTolSum=aCoeff*(aTolV12+aTolV21);
01120     if (d1221<aTolSum) {
01121       return bRetFlag;
01122     }
01123   }
01124   return !bRetFlag;
01125 }
01126 
01127 //=======================================================================
01128 // function: ReplaceCommonBlocks
01129 // purpose:
01130 //=======================================================================
01131 void NMTTools_PaveFiller::ReplaceCommonBlocks(const NMTTools_ListOfCommonBlock& aLCB)
01132 {
01133   RemoveCommonBlocks(aLCB);
01134   SplitCommonBlocks(aLCB);
01135 }
01136 
01137 //=======================================================================
01138 // function: SplitCommonBlocks
01139 // purpose:
01140 //=======================================================================
01141 void NMTTools_PaveFiller::SplitCommonBlocks(const NMTTools_ListOfCommonBlock& aLCB)
01142 {
01143   Standard_Integer nE;
01144   NMTTools_ListOfCommonBlock aLCBx;
01145   NMTTools_ListIteratorOfListOfCommonBlock anIt, anItCBx;
01146   BOPTools_ListIteratorOfListOfPaveBlock anItLPE;
01147   //
01148   anIt.Initialize(aLCB);
01149   for (; anIt.More(); anIt.Next()) {
01150     const NMTTools_CommonBlock& aCB=anIt.Value();
01151     //
01152     //XXX
01153     aLCBx.Clear();
01154     //XXX
01155     SplitCommonBlock(aCB, aLCBx);
01156     //
01157     anItCBx.Initialize(aLCBx);
01158     for (; anItCBx.More(); anItCBx.Next()) {
01159       const NMTTools_CommonBlock& aCBx=anItCBx.Value();
01160       const BOPTools_ListOfPaveBlock& aLPBx=aCBx.PaveBlocks();
01161       //
01162       anItLPE.Initialize(aLPBx);
01163       for (; anItLPE.More(); anItLPE.Next()) {
01164         const BOPTools_PaveBlock& aPBx=anItLPE.Value();
01165         nE=aPBx.OriginalEdge();
01166         NMTTools_ListOfCommonBlock& aLCBE=myCommonBlockPool(myDS->RefEdge(nE));
01167         aLCBE.Append(aCBx);
01168       }
01169     }
01170   }
01171   // Modified to provide the order of edges
01172   // in common block where the edge with max
01173   // tolerance value will be the first
01174   //  Thu Sep 14 14:35:18 2006
01175   // Contribution of Samtech www.samcef.com BEGIN
01176   Standard_Integer i, iMax, aNb, aNbCB, nSp;
01177   Standard_Real aTolSp, aTolMax;
01178   BOPTools_ListOfPaveBlock *pLPBE;
01179   //
01180   aNb=myDS->NumberOfShapesOfTheObject();
01181   for (nE=1; nE<=aNb; ++nE) {
01182     const TopoDS_Shape& aE=myDS->Shape(nE);
01183     if (aE.ShapeType()!=TopAbs_EDGE) {
01184       continue;
01185     }
01186     //
01187     NMTTools_ListOfCommonBlock& aLCBE=myCommonBlockPool(myDS->RefEdge(nE));
01188     aNbCB=aLCBE.Extent();
01189     if (!aNbCB) {
01190       continue;
01191     }
01192     //
01193     anIt.Initialize(aLCBE);
01194     for (; anIt.More(); anIt.Next()) {
01195       NMTTools_CommonBlock& aCBE=anIt.ChangeValue();
01196       const BOPTools_ListOfPaveBlock& aLPBE=aCBE.PaveBlocks();
01197       //
01198       aTolMax=-1.;
01199       anItLPE.Initialize(aLPBE);
01200       for (i=0; anItLPE.More(); anItLPE.Next(), ++i) {
01201         const BOPTools_PaveBlock& aPB=anItLPE.Value();
01202         nSp=aPB.OriginalEdge();
01203         const TopoDS_Edge& aSp=TopoDS::Edge(myDS->Shape(nSp));
01204         aTolSp=BRep_Tool::Tolerance(aSp);
01205         if (aTolSp>aTolMax) {
01206           iMax=i;
01207           aTolSp=aTolMax;
01208         }
01209       }
01210       //
01211       BOPTools_ListOfPaveBlock aLPBx;
01212       //
01213       anItLPE.Initialize(aLPBE);
01214       for (i=0; anItLPE.More(); anItLPE.Next(), ++i) {
01215         const BOPTools_PaveBlock& aPB=anItLPE.Value();
01216         if (i==iMax) {
01217           aLPBx.Prepend(aPB);
01218         }
01219         else {
01220           aLPBx.Append(aPB);
01221         }
01222       }
01223       //
01224       pLPBE=(BOPTools_ListOfPaveBlock *)&aLPBE;
01225       pLPBE->Clear();
01226       pLPBE->Append(aLPBx);
01227     }//for (; anIt.More(); anIt.Next()) {
01228   }//for (nE=1; nE<=aNb; ++nE) {
01229   // Contribution of Samtech www.samcef.com END
01230 }
01231 
01232 //=======================================================================
01233 // function: RemoveCommonBlocks
01234 // purpose:
01235 //=======================================================================
01236 void NMTTools_PaveFiller::RemoveCommonBlocks(const NMTTools_ListOfCommonBlock& aLCB)
01237 {
01238   Standard_Integer nE;
01239   NMTTools_ListOfCommonBlock aLCBx;
01240   NMTTools_ListIteratorOfListOfCommonBlock anItCB, anItCBE;
01241   BOPTools_ListIteratorOfListOfPaveBlock anItLPB;
01242   //
01243   anItCB.Initialize(aLCB);
01244   for (; anItCB.More(); anItCB.Next()) {
01245     const NMTTools_CommonBlock& aCB=anItCB.Value();
01246     const BOPTools_ListOfPaveBlock& aLPB=aCB.PaveBlocks();
01247     //
01248     // Remove aCB from each edge
01249     anItLPB.Initialize(aLPB);
01250     for (; anItLPB.More(); anItLPB.Next()) {
01251       const BOPTools_PaveBlock& aPB=anItLPB.Value();
01252       nE=aPB.OriginalEdge();
01253       //
01254       NMTTools_ListOfCommonBlock& aLCBE=myCommonBlockPool(myDS->RefEdge(nE));
01255       anItCBE.Initialize(aLCBE);
01256       for (; anItCBE.More(); anItCBE.Next()) {
01257         const NMTTools_CommonBlock& aCBE=anItCBE.Value();
01258         if (aCBE.IsEqual(aCB)) {
01259           aLCBE.Remove(anItCBE);
01260           break;
01261         }
01262       }
01263     }
01264   }
01265 }
01266 
01267 //=======================================================================
01268 // function: SplitCommonBlock
01269 // purpose:
01270 //=======================================================================
01271 void NMTTools_PaveFiller::SplitCommonBlock(const NMTTools_CommonBlock& aCB,
01272                                            NMTTools_ListOfCommonBlock& aLCBx)
01273 {
01274   Standard_Boolean bIsCoincided;
01275   Standard_Integer i, j,nE, aNbE, aNbSPBx, aNbPB, k;
01276   BOPTools_SequenceOfPaveBlock aSPBx;
01277   BOPTools_ListIteratorOfListOfPaveBlock anItLPB;
01278   BOPTools_ListIteratorOfListOfPave anIt;
01279   BOPTools_PaveBlockIterator anPBIt;
01280   //
01281   const BOPTools_ListOfPaveBlock& aLPB=aCB.PaveBlocks();
01282   aNbE=aLPB.Extent();
01283   //
01284   // 1. Checking: Whether we realy need to split the common block ?
01285   anItLPB.Initialize(aLPB);
01286   for (; anItLPB.More(); anItLPB.Next()) {
01287     const BOPTools_PaveBlock& aPB=anItLPB.Value();
01288     nE=aPB.OriginalEdge();
01289     BOPTools_PaveSet& aPSE=myPavePoolNew(myDS->RefEdge(nE));
01290     aPSE.SortSet();
01291     //
01292     BOPTools_PaveSet aPSx;
01293     //
01294     const BOPTools_ListOfPave& aLPE=aPSE.Set();
01295     anIt.Initialize(aLPE);
01296     for (; anIt.More(); anIt.Next()) {
01297       const BOPTools_Pave& aPx=anIt.Value();
01298       if (aPB.IsInBlock(aPx)) {
01299         aPSx.Append(aPx);
01300       }
01301     }
01302     aNbPB=aPSx.Set().Extent();
01303     break;
01304   }
01305   //
01306   if (!aNbPB) {
01307     // we need not split it
01308     aLCBx.Append(aCB);
01309     return;
01310   }
01311   //
01312   // 2. Get sequence of pave Blocks containing all new pave blocks
01313   // for each edges's source pave Block
01314   anItLPB.Initialize(aLPB);
01315   for (; anItLPB.More(); anItLPB.Next()) {
01316     const BOPTools_PaveBlock& aPB=anItLPB.Value();
01317     const BOPTools_Pave& aPave1=aPB.Pave1();
01318     const BOPTools_Pave& aPave2=aPB.Pave2();
01319     nE=aPB.OriginalEdge();
01320     //
01321     BOPTools_PaveSet aPSx;
01322     //
01323     // the set aPsx will contain bounadry paves aPave1, aPave2 and
01324     // all paves of the edge nE that are inside block aPB
01325     aPSx.Append(aPave1);
01326     aPSx.Append(aPave2);
01327     //
01328     BOPTools_PaveSet& aPSE=myPavePoolNew(myDS->RefEdge(nE));
01329     aPSE.SortSet();
01330     //
01331     const BOPTools_ListOfPave& aLPE=aPSE.Set();
01332     anIt.Initialize(aLPE);
01333     for (; anIt.More(); anIt.Next()) {
01334       const BOPTools_Pave& aPx=anIt.Value();
01335       if (aPB.IsInBlock(aPx)) {
01336         aPSx.Append(aPx);
01337       }
01338     }
01339     //
01340     // Form pave blocks from aPSx and collect them in aSPBx
01341     anPBIt.Initialize(nE, aPSx);
01342     for (; anPBIt.More(); anPBIt.Next()) {
01343       const BOPTools_PaveBlock& aPBx=anPBIt.Value();
01344       aSPBx.Append(aPBx);
01345     }
01346   }
01347   //
01348   // 3. Do new common blocks
01349   //
01350   const TColStd_ListOfInteger& aLF=aCB.Faces();
01351   aNbSPBx=aSPBx.Length();
01352   aNbPB=aNbSPBx/aNbE;
01353   //
01354   Standard_Integer k1, k2, n11, n12, n21, n22;
01355   //
01356   for (i=1; i<=aNbPB; ++i) {
01357     NMTTools_CommonBlock aCBx;
01358     //
01359     aCBx.AddFaces(aLF);
01360     //
01361     const BOPTools_PaveBlock& aPB1=aSPBx(i);
01362     n11=aPB1.Pave1().Index();
01363     n12=aPB1.Pave2().Index();
01364     //
01365     aCBx.AddPaveBlock(aPB1);
01366     //
01367     for (j=2; j<=aNbE; ++j) {
01368       k1=(j-1)*aNbPB+1;
01369       k2=k1+aNbPB-1;
01370       for(k=k1; k<=k2; ++k) {
01371         const BOPTools_PaveBlock& aPB2=aSPBx(k);
01372         n21=aPB2.Pave1().Index();
01373         n22=aPB2.Pave2().Index();
01374         if ((n21==n11 && n22==n12) || (n21==n12 && n22==n11)) {
01375          //modified by NIZNHY-PKV Thu Nov 11 08:13:24 2010f
01376          bIsCoincided=CheckCoincidence(aPB2, aPB1);
01377          if (bIsCoincided) {
01378            aCBx.AddPaveBlock(aPB2);
01379            break;
01380          }
01381          //aCBx.AddPaveBlock(aPB2);
01382          //break;
01383          //modified by NIZNHY-PKV Thu Nov 11 08:13:31 2010t
01384         }
01385       }
01386     }
01387     aLCBx.Append(aCBx);
01388   }
01389 }
01390 
01391 //=======================================================================
01392 // function: VertexParameters
01393 // purpose:
01394 //=======================================================================
01395 void VertexParameters(const IntTools_CommonPrt& aCPart,
01396                       Standard_Real& aT1,
01397                       Standard_Real& aT2)
01398 {
01399   const IntTools_Range& aR1=aCPart.Range1();
01400   aT1=0.5*(aR1.First()+aR1.Last());
01401   //
01402   if((aCPart.VertexParameter1() >= aR1.First()) &&
01403      (aCPart.VertexParameter1() <= aR1.Last())) {
01404     aT1 = aCPart.VertexParameter1();
01405   }
01406   //
01407   const IntTools_SequenceOfRanges& aRanges2=aCPart.Ranges2();
01408   const IntTools_Range& aR2=aRanges2(1);
01409   aT2=0.5*(aR2.First()+aR2.Last());
01410   //
01411   if((aCPart.VertexParameter2() >= aR2.First()) &&
01412      (aCPart.VertexParameter2() <= aR2.Last())) {
01413     aT2 = aCPart.VertexParameter2();
01414   }
01415 }
01416 
01417 //=======================================================================
01418 // function: KeepPave
01419 // purpose:
01420 //=======================================================================
01421 Standard_Boolean IsOnPave(const Standard_Real& aT1,
01422                           const IntTools_Range& aRange,
01423                           const Standard_Real& aTolerance)
01424 {
01425   Standard_Boolean firstisonpave1, firstisonpave2, bIsOnPave;
01426   //
01427   firstisonpave1  = (Abs(aRange.First() - aT1) < aTolerance);
01428   firstisonpave2  = (Abs(aRange.Last()  - aT1) < aTolerance);
01429   bIsOnPave=(firstisonpave1 || firstisonpave2);
01430   return bIsOnPave;
01431 }
01432 
01433 //=======================================================================
01434 // function:FindChains
01435 // purpose:
01436 //=======================================================================
01437 void FindChains(const BOPTools_IDMapOfPaveBlockIMapOfPaveBlock& aMapCB,
01438                 NMTTools_ListOfCommonBlock& aLCB)
01439 {
01440   Standard_Integer  i, j, aNbCB, aNbPB;
01441   BOPTools_IMapOfPaveBlock aProcessedBlocks, aChain;
01442   //
01443   aNbCB=aMapCB.Extent();
01444   for (i=1; i<=aNbCB; ++i) {
01445     const BOPTools_PaveBlock& aPB=aMapCB.FindKey(i);
01446     if (aProcessedBlocks.Contains(aPB)) {
01447       continue;
01448     }
01449     //
01450     aProcessedBlocks.Add(aPB);
01451     aChain.Add(aPB);
01452     //
01453     const BOPTools_IMapOfPaveBlock& aMapPB=aMapCB(i);
01454     aNbPB=aMapPB.Extent();
01455     for (j=1; j<=aNbPB; ++j) {
01456       const BOPTools_PaveBlock& aPBx=aMapPB(j);
01457       ProcessBlock(aPBx, aMapCB, aProcessedBlocks, aChain);
01458     }
01459     //
01460     NMTTools_CommonBlock aCB;
01461     //
01462     aNbPB=aChain.Extent();
01463     for (j=1; j<=aNbPB; ++j) {
01464       const BOPTools_PaveBlock& aPBx=aChain(j);
01465       aCB.AddPaveBlock(aPBx);
01466     }
01467     aLCB.Append(aCB);
01468     aChain.Clear();
01469   }
01470 }
01471 
01472 //=======================================================================
01473 // function:ProcessBlock
01474 // purpose:
01475 //=======================================================================
01476 void ProcessBlock(const BOPTools_PaveBlock& aPB,
01477                   const BOPTools_IDMapOfPaveBlockIMapOfPaveBlock& aMapCB,
01478                   BOPTools_IMapOfPaveBlock& aProcessedBlocks,
01479                   BOPTools_IMapOfPaveBlock& aChain)
01480 {
01481   Standard_Integer j, aNbPB;
01482   //
01483   if (aProcessedBlocks.Contains(aPB)) {
01484     return;
01485   }
01486   aProcessedBlocks.Add(aPB);
01487   aChain.Add(aPB);
01488   //
01489   const BOPTools_IMapOfPaveBlock& aMapPB=aMapCB.FindFromKey(aPB);
01490   aNbPB=aMapPB.Extent();
01491   for (j=1; j<=aNbPB; ++j) {
01492     const BOPTools_PaveBlock& aPBx=aMapPB(j);
01493     ProcessBlock(aPBx, aMapCB, aProcessedBlocks, aChain);
01494   }
01495 }
01496 // Modified  to provide VS interference between
01497 // vertex as result of EE and a Face of argument
01498 // Thu Sep 14 14:35:18 2006
01499 // Contribution of Samtech www.samcef.com BEGIN
01500 //=======================================================================
01501 // function: PerformVF1
01502 // purpose:
01503 //=======================================================================
01504   void NMTTools_PaveFiller::PerformVF1()
01505 {
01506   Standard_Integer i, aNbEE, n1, n2, nNewShape, aNbS, nF;
01507   Standard_Integer anIndexIn, aFlag;
01508   Standard_Real aU, aV;
01509   TColStd_ListOfInteger aLFI;
01510   TColStd_ListIteratorOfListOfInteger aItLFI;
01511   //
01512   BOPTools_CArray1OfVSInterference& aVSs=myIP->VSInterferences();
01513   BOPTools_CArray1OfEEInterference& aEEs=myIP->EEInterferences();
01514   //
01515   aNbS=myDS->NumberOfShapesOfTheObject();
01516   for (i=1; i<=aNbS; ++i) {
01517     const TopoDS_Shape& aF=myDS->Shape(i);
01518     if (aF.ShapeType()==TopAbs_FACE) {
01519       aLFI.Append(i);
01520     }
01521   }
01522   if (!aLFI.Extent()) {
01523     return;
01524   }
01525   //
01526   aNbEE=aEEs.Extent();
01527   for (i=1; i<=aNbEE; ++i) {
01528     BOPTools_EEInterference& aEE=aEEs(i);
01529     aEE.Indices(n1, n2);
01530     nNewShape=aEE.NewShape();
01531     if (!nNewShape) {
01532       continue;
01533     }
01534     //
01535     const TopoDS_Shape& aSnew=myDS->Shape(nNewShape);
01536     if (aSnew.ShapeType()!=TopAbs_VERTEX) {
01537       continue;
01538     }
01539     //
01540     const TopoDS_Vertex& aVnew=TopoDS::Vertex(aSnew);
01541     //
01542     Bnd_Box aBV;
01543     //
01544     BRepBndLib::Add(aVnew, aBV);
01545     //
01546     aItLFI.Initialize(aLFI);
01547     for (; aItLFI.More(); aItLFI.Next()) {
01548       nF=aItLFI.Value();
01549       //
01550       const TopoDS_Face& aF=TopoDS::Face(myDS->Shape(nF));
01551       const Bnd_Box& aBF=myDS->GetBoundingBox(nF);
01552       if (aBF.IsOut(aBV)) {
01553         continue;
01554       }
01555       //
01556       anIndexIn=0;
01557       aFlag=myContext->ComputeVS (aVnew, aF, aU, aV);
01558       if (!aFlag) {
01559         BOPTools_VSInterference anInterf (nNewShape, nF, aU, aV);
01560         //
01561         anIndexIn=aVSs.Append(anInterf);
01562         BOPTools_VSInterference& aVS=aVSs(anIndexIn);
01563         aVS.SetNewShape(nNewShape);//->
01564       }
01565     }
01566   }
01567 }
01568 // Contribution of Samtech www.samcef.com END
01569 //modified by NIZNHY-PKV Thu Nov 11 08:13:48 2010f
01570 //=======================================================================
01571 // function: CheckCoincidence
01572 // purpose:
01573 //=======================================================================
01574 Standard_Boolean NMTTools_PaveFiller::CheckCoincidence(const BOPTools_PaveBlock& aPB1,
01575                                                  const BOPTools_PaveBlock& aPB2)
01576 {
01577   Standard_Boolean bRet;
01578   Standard_Integer nE1, nE2, aNbPoints;
01579   Standard_Real aT11, aT12, aT21, aT22, aT1m, aD, aTol, aT2x;
01580   gp_Pnt aP1m;
01581   //
01582   bRet=Standard_False;
01583   //
01584   aT11=aPB1.Pave1().Param();
01585   aT12=aPB1.Pave2().Param();
01586   aT1m=IntTools_Tools::IntermediatePoint (aT11, aT12);
01587   nE1=aPB1.OriginalEdge();
01588   const TopoDS_Edge& aE1=(*(TopoDS_Edge*)(&myDS->Shape(nE1)));
01589   BOPTools_Tools::PointOnEdge(aE1, aT1m, aP1m);
01590   //
01591   aT21=aPB2.Pave1().Param();
01592   aT22=aPB2.Pave2().Param();
01593   nE2=aPB2.OriginalEdge();
01594   const TopoDS_Edge& aE2=(*(TopoDS_Edge*)(&myDS->Shape(nE2)));
01595   //
01596   GeomAPI_ProjectPointOnCurve& aPPC=myContext->ProjPC(aE2);
01597   aPPC.Perform(aP1m);
01598   aNbPoints=aPPC.NbPoints();
01599   if (aNbPoints) {
01600     aD=aPPC.LowerDistance();
01601     //
01602     aTol=BRep_Tool::Tolerance(aE1);
01603     aTol=aTol+BRep_Tool::Tolerance(aE2);
01604     if (aD<aTol) {
01605       aT2x=aPPC.LowerDistanceParameter();
01606       if (aT2x>aT21 && aT2x<aT22) {
01607        return !bRet;
01608       }
01609     }
01610   }
01611   return bRet;
01612 }
01613 //modified by NIZNHY-PKV Thu Nov 11 08:13:55 2010t