Back to index

salome-geom  6.5.0
NMTDS_ShapesDataStructure.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 
00023 // File:        NMTDS_ShapesDataStructure.cxx
00024 // Author:      Peter KURNEV
00025 
00026 #include <NMTDS_ShapesDataStructure.hxx>
00027 #include <TopoDS_Iterator.hxx>
00028 #include <TopoDS_Shape.hxx>
00029 #include <BooleanOperations_IndexedDataMapOfShapeAncestorsSuccessors.hxx>
00030 #include <NMTDS_ListOfIndexedDataMapOfShapeAncestorsSuccessors.hxx>
00031 #include <NMTDS_ListIteratorOfListOfIndexedDataMapOfShapeAncestorsSuccessors.hxx>
00032 #include <BooleanOperations_ShapeAndInterferences.hxx>
00033 #include <NMTDS_IndexRange.hxx>
00034 //
00035 #include <BooleanOperations_IndexedDataMapOfShapeAncestorsSuccessors.hxx>
00036 #include <BooleanOperations_AncestorsSeqAndSuccessorsSeq.hxx>
00037 #include <TColStd_MapOfInteger.hxx>
00038 #include <NMTDS_Tools.hxx>
00039 
00040 
00041 static
00042   void ComputeBoxExS(const Standard_Integer aIx,
00043                      const NMTDS_ShapesDataStructure* pDS,
00044                      Bnd_Box& aBoxEx);
00045 static
00046   void GetAllSuccessorsS(const Standard_Integer nS,
00047                          const NMTDS_ShapesDataStructure* myDS,
00048                          TColStd_IndexedMapOfInteger& aMA);
00049 
00050 //===========================================================================
00051 //function : NMTDS_ShapesDataStructure::NMTDS_ShapesDataStructure
00052 //purpose  : 
00053 //===========================================================================
00054 NMTDS_ShapesDataStructure::NMTDS_ShapesDataStructure()
00055 :
00056   BooleanOperations_ShapesDataStructure()
00057 {}
00058 //===========================================================================
00059 //function : SetCompositeShape
00060 //purpose  : 
00061 //===========================================================================
00062 void NMTDS_ShapesDataStructure::SetCompositeShape(const TopoDS_Shape& aS)
00063 {
00064   myCompositeShape=aS;
00065 }
00066 //===========================================================================
00067 //function : CompositeShape
00068 //purpose  : 
00069 //===========================================================================
00070 const TopoDS_Shape& NMTDS_ShapesDataStructure::CompositeShape()const
00071 {
00072   return myCompositeShape;
00073 }
00074 //===========================================================================
00075 //function : Ranges
00076 //purpose  : 
00077 //===========================================================================
00078 const NMTDS_CArray1OfIndexRange& NMTDS_ShapesDataStructure::Ranges()const
00079 {
00080   return myRanges;
00081 }
00082 //===========================================================================
00083 //function : FillMap
00084 //purpose  : 
00085 //===========================================================================
00086 void NMTDS_ShapesDataStructure::FillMap
00087   (const TopoDS_Shape& aS,
00088    BooleanOperations_IndexedDataMapOfShapeAncestorsSuccessors& aMSA,
00089    BooleanOperations_IndexedDataMapOfShapeAncestorsSuccessors& aMS) const
00090 {
00091   Standard_Integer iX, i, j, aIndex, aNbSc, aNbS;
00092   BooleanOperations_AncestorsSeqAndSuccessorsSeq aAS;
00093   //
00094   aMSA.Add(aS, aAS);
00095   aMS.Add(aS, aAS);
00096   FillSubshapes(aS, aMSA, aMS);
00097   //
00098   aNbS=aMS.Extent();
00099   for(i=1; i<=aNbS; ++i) {
00100     TColStd_MapOfInteger aMFence;
00101     //
00102     const TopoDS_Shape& aSX=aMS.FindKey(i);
00103     iX=aMSA.FindIndex(aSX);
00104     const BooleanOperations_AncestorsSeqAndSuccessorsSeq& aAS1=aMSA(iX);
00105     //
00106     aNbSc=aAS1.NumberOfSuccessors();
00107     for(j=1; j<=aNbSc; ++j) {
00108       aIndex=aAS1.GetSuccessor(j);
00109       if(aMFence.Add(aIndex)) {
00110         BooleanOperations_AncestorsSeqAndSuccessorsSeq& aAS2=aMSA.ChangeFromIndex(aIndex);
00111         aAS2.SetNewAncestor(iX);
00112       }
00113     }
00114   }
00115 }
00116 //===========================================================================
00117 //function : FillSubshapes
00118 //purpose  : 
00119 //===========================================================================
00120 void NMTDS_ShapesDataStructure::FillSubshapes
00121   (const TopoDS_Shape& aS,
00122    BooleanOperations_IndexedDataMapOfShapeAncestorsSuccessors& aMSA,
00123    BooleanOperations_IndexedDataMapOfShapeAncestorsSuccessors& aMS) const
00124 {
00125   Standard_Boolean bIsNewSubShape;
00126   Standard_Integer aIndexSubShape, aIndex;
00127   BooleanOperations_AncestorsSeqAndSuccessorsSeq aASx;
00128   //
00129   aIndex=aMSA.FindIndex(aS);
00130   BooleanOperations_AncestorsSeqAndSuccessorsSeq& aAS=aMSA.ChangeFromIndex(aIndex);
00131   //
00132   TopoDS_Iterator anIt(aS, Standard_True);
00133   for(; anIt.More(); anIt.Next()) {
00134     const TopoDS_Shape& aSubShape = anIt.Value();
00135     bIsNewSubShape = Standard_False;
00136     if(!aMSA.Contains(aSubShape)) {
00137       bIsNewSubShape=!bIsNewSubShape;
00138       aIndexSubShape=aMSA.Add(aSubShape, aASx);
00139       aMS.Add(aSubShape, aASx);
00140     }
00141     else {
00142       aIndexSubShape=aMSA.FindIndex(aSubShape);
00143     }
00144     aAS.SetNewSuccessor(aIndexSubShape);
00145     aAS.SetNewOrientation(aSubShape.Orientation());
00146     //
00147     if(bIsNewSubShape && (aSubShape.ShapeType() != TopAbs_VERTEX)) {
00148       FillSubshapes(aSubShape, aMSA, aMS);
00149     }
00150   }
00151 }
00152 //===========================================================================
00153 //function : Init
00154 //purpose  : 
00155 //===========================================================================
00156 void NMTDS_ShapesDataStructure::Init()
00157 {
00158   Standard_Integer i, j, aNbSx, aNbS, aShift, aNbRanges;
00159   Standard_Integer iFirst, iLast;
00160   NMTDS_ListOfIndexedDataMapOfShapeAncestorsSuccessors aLx;
00161   NMTDS_ListIteratorOfListOfIndexedDataMapOfShapeAncestorsSuccessors aLit;
00162   TopoDS_Iterator anIt;
00163   BooleanOperations_IndexedDataMapOfShapeAncestorsSuccessors aMSA;
00164   //
00165   anIt.Initialize(myCompositeShape);
00166   for (; anIt.More(); anIt.Next()) {
00167     const TopoDS_Shape& aSx=anIt.Value(); 
00168     BooleanOperations_IndexedDataMapOfShapeAncestorsSuccessors aMS;
00169     //
00170     if (!aMSA.Contains(aSx)) {
00171       FillMap(aSx, aMSA, aMS);
00172       aLx.Append(aMS);
00173     }
00174   }
00175   aNbS=aMSA.Extent(); 
00176   //
00177   // Fill myRanges
00178   i=aLx.Extent();
00179   myRanges.Resize(i);
00180   aLit.Initialize(aLx);
00181   for (i=1; aLit.More(); aLit.Next(), ++i) {
00182     const BooleanOperations_IndexedDataMapOfShapeAncestorsSuccessors& aMSx=aLit.Value();
00183     aNbSx=aMSx.Extent();
00184     if (i==1) {
00185       iFirst=1;
00186       iLast=aNbSx;
00187       myRanges(i).SetFirst(iFirst);
00188       myRanges(i).SetLast(iLast);
00189       continue;
00190     }
00191     iFirst=myRanges(i-1).Last()+1;
00192     iLast=iFirst+aNbSx-1;
00193     myRanges(i).SetFirst(iFirst);
00194     myRanges(i).SetLast(iLast);
00195   }
00196   //
00197   myNumberOfShapesOfTheObject=aNbS;
00198   myNumberOfShapesOfTheTool=0;
00199   myLength=2*aNbS;
00200   //
00201   // Allocate the whole Table
00202   myListOfShapeAndInterferences = (BooleanOperations_PShapeAndInterferences)
00203     Standard::Allocate(myLength*sizeof(BooleanOperations_ShapeAndInterferences));
00204   //
00205   // Fill the table
00206   
00207   aShift=0;
00208   for (j=1; j<=aNbS; ++j) {
00209     const TopoDS_Shape& aSx=aMSA.FindKey(j);
00210     const BooleanOperations_AncestorsSeqAndSuccessorsSeq& aASx=aMSA.FindFromIndex(j);
00211     InsertShapeAndAncestorsSuccessors(aSx, aASx, aShift);
00212   }
00213   // myShapeIndexMap
00214   myShapeIndexMap.Clear();
00215   //
00216   //modified by NIZNHY-PKV Mon Dec 12 09:01:53 2011f
00217   aNbRanges=myRanges.Extent();
00218   for (i=1; i<=aNbRanges; ++i){
00219     const NMTDS_IndexRange& aR=myRanges(i);
00220     iFirst=aR.First();
00221     iLast =aR.Last();
00222     for (j=iFirst; j<=iLast; ++j) { 
00223       const TopoDS_Shape& aS=Shape(j);
00224       myShapeIndexMap.Bind(aS, j);
00225     }
00226   }
00227   //modified by NIZNHY-PKV Mon Dec 12 09:02:00 2011t
00228   //
00229   // myRefEdges
00230   iLast=myNumberOfShapesOfTheObject+myNumberOfShapesOfTheTool;
00231   myRefEdges.Resize(iLast);
00232 
00233   for (i=1; i<=iLast; ++i) {
00234     const TopoDS_Shape& aS=Shape(i);
00235     myRefEdges(i)=0;
00236     if (aS.ShapeType()==TopAbs_EDGE) {
00237       myNbEdges++;
00238       myRefEdges(i)=myNbEdges;
00239     }
00240   }
00241 }
00242 //===========================================================================
00243 //function : ShapeRangeIndex
00244 //purpose  : 
00245 //===========================================================================
00246 Standard_Integer NMTDS_ShapesDataStructure::ShapeRangeIndex
00247   (const Standard_Integer aId)const
00248 {
00249   Standard_Boolean bFound;
00250   Standard_Integer i, aNbR, aIdx, aNbS, aZero=0;
00251   //
00252   aNbS=myNumberOfShapesOfTheObject+myNumberOfShapesOfTheTool;
00253   
00254   aIdx=aId;
00255   if (aIdx > aNbS || aIdx < 1){
00256     return aZero;
00257   }
00258   //  
00259   if (aIdx > myNumberOfShapesOfTheObject) {
00260     aIdx-=myNumberOfShapesOfTheObject;
00261   }
00262   //
00263   aNbR=myRanges.Extent();
00264   for (i=1; i<=aNbR; ++i) {
00265     const NMTDS_IndexRange& aRange=myRanges(i);
00266     bFound=aRange.IsInRange(aIdx);
00267     if (bFound) {
00268      return i;
00269     }
00270   }
00271   return aZero;
00272 }
00273 //===========================================================================
00274 //function : Rank
00275 //purpose  : 
00276 //===========================================================================
00277 Standard_Integer NMTDS_ShapesDataStructure::Rank
00278   (const Standard_Integer aId)const
00279 {
00280   Standard_Boolean bFound;
00281   Standard_Integer i, aNbR, aNbS, aZero=0;
00282   //
00283   aNbS=myNumberOfShapesOfTheObject;
00284   
00285   if (aId > aNbS || aId < 1){
00286     return aZero;
00287   }
00288   //  
00289   aNbR=myRanges.Extent();
00290   for (i=1; i<=aNbR; ++i) {
00291     const NMTDS_IndexRange& aRange=myRanges(i);
00292     bFound=aRange.IsInRange(aId);
00293     if (bFound) {
00294      return i;
00295     }
00296   }
00297   return aZero;
00298 }
00299 //===========================================================================
00300 //function : ShapeIndex
00301 //purpose  : 
00302 //===========================================================================
00303 Standard_Integer NMTDS_ShapesDataStructure::ShapeIndex
00304   (const TopoDS_Shape& aS,
00305    const Standard_Integer aRank)const
00306 {
00307   Standard_Address pIndex;
00308   Standard_Integer aIndex;
00309   //
00310   aIndex=0;
00311   //
00312   //modified by NIZNHY-PKV Mon Dec 12 09:02:48 2011f
00313   pIndex=myShapeIndexMap.Find1(aS);
00314   if (pIndex) {
00315     aIndex=*((Standard_Integer*)pIndex);
00316   }
00317   //modified by NIZNHY-PKV Mon Dec 12 09:02:54 2011t
00318   return aIndex;
00319 }
00320 
00321 //=======================================================================
00322 //function : ComputeBoxEx
00323 //purpose  : 
00324 //=======================================================================
00325 void NMTDS_ShapesDataStructure::ComputeBoxEx 
00326   (const Standard_Integer aIx,
00327    Bnd_Box& aBoxEx)const
00328 {
00329   ComputeBoxExS(aIx, this, aBoxEx);
00330 }
00331 //=======================================================================
00332 //function : GetAllSuccessors
00333 //purpose  : 
00334 //=======================================================================
00335 void NMTDS_ShapesDataStructure::GetAllSuccessors
00336   (const Standard_Integer nS,
00337    TColStd_IndexedMapOfInteger& aMA)const
00338 {
00339   GetAllSuccessorsS(nS, this, aMA);
00340 }
00341 //=======================================================================
00342 //function : GetAllSuccessorsS
00343 //purpose  : 
00344 //=======================================================================
00345 void GetAllSuccessorsS(const Standard_Integer nS,
00346                        const NMTDS_ShapesDataStructure* myDS,
00347                        TColStd_IndexedMapOfInteger& aMA)
00348 {
00349   TopAbs_ShapeEnum aT;
00350   Standard_Integer i, nSx, aNbSuccessors, *pSuccessors;
00351   Standard_Address xSuccessors;
00352   //
00353   const TopoDS_Shape& aS=myDS->Shape(nS);
00354   aT=aS.ShapeType();
00355   if(NMTDS_Tools::HasBRep(aT)) {
00356     aMA.Add(nS);
00357     //
00358     if (aT==TopAbs_VERTEX) {
00359       return;
00360     }
00361   }
00362   //
00363   myDS->GetSuccessors(nS, xSuccessors, aNbSuccessors);
00364   pSuccessors=(Standard_Integer*)xSuccessors;
00365   for (i=0; i<aNbSuccessors; ++i) {
00366     nSx=pSuccessors[i];
00367     GetAllSuccessorsS(nSx, myDS, aMA);
00368   }
00369 }
00370 //=======================================================================
00371 // function: ComputeBoxExS
00372 // purpose: 
00373 //=======================================================================
00374 void ComputeBoxExS(const Standard_Integer aIx,
00375                  const NMTDS_ShapesDataStructure* pDS,
00376                  Bnd_Box& aBoxEx)
00377 {
00378   Standard_Integer i, aNbS, iS;
00379   //
00380   const Bnd_Box& aBox=pDS->GetBoundingBox(aIx);
00381   aBoxEx.Add(aBox);
00382   //
00383   aNbS=pDS->NumberOfSuccessors(aIx);
00384   for (i=1; i<=aNbS; ++i) {
00385     Bnd_Box aBoxS;
00386     iS=pDS->GetSuccessor(aIx, i);
00387     ComputeBoxExS(iS, pDS, aBoxS);
00388     aBoxEx.Add(aBoxS);
00389   }
00390 }