Back to index

salome-med  6.5.0
BBTreeTest.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D
00002 //
00003 // This library is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU Lesser General Public
00005 // License as published by the Free Software Foundation; either
00006 // version 2.1 of the License.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // Lesser General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU Lesser General Public
00014 // License along with this library; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00016 //
00017 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00018 //
00019 
00020 #include "BBTreeTest.hxx"
00021 #include <iostream>
00022 #include <vector>
00023 #include "DirectedBoundingBox.hxx"
00024 
00025 namespace INTERP_TEST
00026 {
00027 
00028 
00029   void BBTreeTest::setUp() 
00030   {
00031   }
00032 
00033  
00034   void BBTreeTest::tearDown() 
00035   {
00036   }
00037 
00046   void BBTreeTest::test_BBTree() {
00047     //bbox tree creation
00048     const int N=10;
00049     double* bbox=new double[4*N*N];
00050     for (int i=0; i<N; i++)
00051       for (int j=0; j<N; j++)
00052         {
00053           bbox[4*(i*N+j)]=i;
00054           bbox[4*(i*N+j)+1]=i+1;
00055           bbox[4*(i*N+j)+2]=j;
00056           bbox[4*(i*N+j)+3]=j+1;
00057         }
00058     BBTree<2> tree(bbox,0,0,N*N);
00059     std::vector <int> elems; 
00060   
00061     //box outside the tree
00062     double bbox1[4]={-2.0, -1.0, 0.0, 1.0};
00063     tree.getIntersectingElems(bbox1,elems);
00064     CPPUNIT_ASSERT_EQUAL(0,(int)elems.size());
00065     elems.clear();
00066   
00067     //box intersecting 4 tree elems
00068     double bbox2[4]={2.5, 3.5, 0.5, 1.5};
00069     tree.getIntersectingElems(bbox2,elems);
00070     CPPUNIT_ASSERT_EQUAL(4,(int)elems.size());
00071     elems.clear();
00072   
00073     //box exactly superimposed to two tree elems
00074     double bbox3[4]={5.0,6.0,7.0,9.0};
00075     tree.getIntersectingElems(bbox3,elems);
00076     CPPUNIT_ASSERT_EQUAL(2,(int)elems.size());
00077     elems.clear();
00078 
00079     double xx[2]={1.0,1.0};
00080     tree.getElementsAroundPoint(xx,elems);
00081     CPPUNIT_ASSERT_EQUAL(4,(int)elems.size());
00082 
00083     delete[] bbox;
00084   }
00085 
00086   void BBTreeTest::test_DirectedBB_3D()
00087   {
00088     // a rectangle 1x2 extruded along vector (10,0,10)
00089     const int nbP = 8, dim = 3;
00090     double coords[nbP*dim] =
00091       {
00092         0,0,0,    2,0,0,   2,1,0,   0,1,0, 
00093         10,0,10, 12,0,10, 12,1,10, 10,1,10
00094       };
00095     INTERP_KERNEL::DirectedBoundingBox bb( coords, nbP, dim);
00096     bb.enlarge( 1e-12 );
00097 
00098     // corners of extrusion are IN
00099     for ( int i = 0; i < nbP*dim; i+=dim )
00100       CPPUNIT_ASSERT( !bb.isOut( coords + i ));
00101 
00102     // points near corners of extrusion are OUT
00103     double p[nbP*dim] = 
00104       {
00105         0,0,3,  6,0,3,   5,1,2,   0,1,2, 
00106         8,0,9, 11,0,8, 11,0.5,8, 8,0.5,9
00107       };
00108     for ( int i = 0; i < nbP*dim; i+=dim )
00109       CPPUNIT_ASSERT( bb.isOut( p + i ));
00110 
00111     // the extrusions  shifted by 3 in XOY plane are OUT
00112     double shifted_X[nbP*dim] =
00113       {
00114         3,0,0,    5,0,0,   5,1,0,   3,1,0, 
00115         13,0,10, 15,0,10, 15,1,10, 13,1,10
00116       };
00117     double shifted_x[nbP*dim] =
00118       {
00119         -3,0,0, -1,0,0, -1,1,0, -3,1,0, 
00120         7,0,10, 9,0,10, 9,1,10, 7,1,10
00121       };
00122     double shifted_Y[nbP*dim] =
00123       {
00124         0,3,0,    2,3,0,   2,4,0,   0,4,0, 
00125         10,3,10, 12,3,10, 12,4,10, 10,4,10
00126       };
00127     double shifted_y[nbP*dim] =
00128       {
00129         0,-3,0,    2,-3,0,   2,-2,0,   0,-2,0, 
00130         10,-3,10, 12,-3,10, 12,-2,10, 10,-2,10
00131       };
00132     INTERP_KERNEL::DirectedBoundingBox shiftedBB_x( shifted_x, nbP, dim);
00133     INTERP_KERNEL::DirectedBoundingBox shiftedBB_X( shifted_X, nbP, dim);
00134     INTERP_KERNEL::DirectedBoundingBox shiftedBB_y( shifted_y, nbP, dim);
00135     INTERP_KERNEL::DirectedBoundingBox shiftedBB_Y( shifted_Y, nbP, dim);
00136 
00137     CPPUNIT_ASSERT( bb.isDisjointWith( shiftedBB_x ));
00138     CPPUNIT_ASSERT( bb.isDisjointWith( shiftedBB_X ));
00139     CPPUNIT_ASSERT( bb.isDisjointWith( shiftedBB_y ));
00140     CPPUNIT_ASSERT( bb.isDisjointWith( shiftedBB_Y ));
00141 
00142     // intersecting box is IN
00143     double inters_coords[nbP*dim] =
00144       {
00145         0,0,0,    2,0,0,   2,1,0,   0,1,0, 
00146         0,0,2,    2,0,2,   2,1,2,   0,1,2
00147       };
00148     INTERP_KERNEL::DirectedBoundingBox ibb( inters_coords, nbP, dim);
00149     CPPUNIT_ASSERT( !bb.isDisjointWith( ibb ));
00150 
00151     // overlapping non-directed BB
00152     double overlappingBB[2*dim] =
00153       {
00154         5,6, 0, 1, -5,4
00155       };
00156     CPPUNIT_ASSERT( !bb.isDisjointWith( overlappingBB ));
00157 
00158     // non-overlapping non-directed BB
00159     double nonoverlappingBB_1[2*dim] =
00160       {
00161         5,6, 0,1, -5,2
00162       };
00163     CPPUNIT_ASSERT( bb.isDisjointWith( nonoverlappingBB_1 ));
00164     double nonoverlappingBB_2[2*dim] =
00165       {
00166         5,6, 0,1, 7,20
00167       };
00168     CPPUNIT_ASSERT( bb.isDisjointWith( nonoverlappingBB_2 ));
00169   }
00170 
00171   void BBTreeTest::test_DirectedBB_2D()
00172   {
00173     // a segment of length 2 extruded along vector (10,10)
00174     const int nbP = 4, dim = 2;
00175     double coords[nbP*dim] =
00176       {
00177         0,0,    2,0,
00178         10,10, 12,10,
00179       };
00180     INTERP_KERNEL::DirectedBoundingBox bb( coords, nbP, dim);
00181     bb.enlarge( 1e-12 );
00182 
00183     // corners of extrusion are IN
00184     for ( int i = 0; i < nbP*dim; i+=dim )
00185       CPPUNIT_ASSERT( !bb.isOut( coords + i ));
00186 
00187     // points near corners of extrusion are OUT
00188     double p[nbP*dim] = 
00189       {
00190         1,2,  4,1,
00191         11,8, 8,9,
00192       };
00193     for ( int i = 0; i < nbP*dim; i+=dim )
00194       CPPUNIT_ASSERT( bb.isOut( p + i ));
00195 
00196     // the extrusions shifted by 3 along OX are OUT
00197     double shifted_X[nbP*dim] =
00198       {
00199         3,0,    5,0,
00200         13,10, 15,10,
00201       };
00202     double shifted_x[nbP*dim] =
00203       {
00204         -3,0, -1,0,
00205         7,10, 9,10,
00206       };
00207     INTERP_KERNEL::DirectedBoundingBox shiftedBB_x( shifted_x, nbP, dim);
00208     INTERP_KERNEL::DirectedBoundingBox shiftedBB_X( shifted_X, nbP, dim);
00209 
00210     CPPUNIT_ASSERT( bb.isDisjointWith( shiftedBB_x ));
00211     CPPUNIT_ASSERT( bb.isDisjointWith( shiftedBB_X ));
00212 
00213     // intersecting box is IN
00214     double inters_coords[nbP*dim] =
00215       {
00216         0,0,    2,0, 
00217         0,2,    2,2
00218       };
00219     INTERP_KERNEL::DirectedBoundingBox ibb( inters_coords, nbP, dim);
00220     CPPUNIT_ASSERT( !bb.isDisjointWith( ibb ));
00221 
00222     // overlapping non-directed BB
00223     double overlappingBB[2*dim] =
00224       {
00225         5,6, -5,4
00226       };
00227     CPPUNIT_ASSERT( !bb.isDisjointWith( overlappingBB ));
00228 
00229     // non-overlapping non-directed BB
00230     double nonoverlappingBB_1[2*dim] =
00231       {
00232         5,6, -5,2
00233       };
00234     CPPUNIT_ASSERT( bb.isDisjointWith( nonoverlappingBB_1 ));
00235     double nonoverlappingBB_2[2*dim] =
00236       {
00237         5,6, 7,20
00238       };
00239     CPPUNIT_ASSERT( bb.isDisjointWith( nonoverlappingBB_2 ));
00240   }
00241 
00242   void BBTreeTest::test_DirectedBB_1D()
00243   {
00244     const int nbP = 2, dim = 1;
00245     double coords[nbP*dim] =
00246       {
00247         0, 10
00248       };
00249     INTERP_KERNEL::DirectedBoundingBox bb( coords, nbP, dim);
00250     bb.enlarge( 1e-12 );
00251 
00252     // coords are IN
00253     for ( int i = 0; i < nbP*dim; i+=dim )
00254       CPPUNIT_ASSERT( !bb.isOut( coords + i ));
00255 
00256     // points near ends are OUT
00257     double p[nbP*dim] = 
00258       {
00259         -0.0001, 10.1
00260       };
00261     for ( int i = 0; i < nbP*dim; i+=dim )
00262       CPPUNIT_ASSERT( bb.isOut( p + i ));
00263 
00264     // shifted boxes are OUT
00265     double shifted_X[nbP*dim] =
00266       {
00267         10.1, 11
00268       };
00269     double shifted_x[nbP*dim] =
00270       {
00271         -3.0, -0.001
00272       };
00273     INTERP_KERNEL::DirectedBoundingBox shiftedBB_x( shifted_x, nbP, dim);
00274     INTERP_KERNEL::DirectedBoundingBox shiftedBB_X( shifted_X, nbP, dim);
00275 
00276     CPPUNIT_ASSERT( bb.isDisjointWith( shiftedBB_x ));
00277     CPPUNIT_ASSERT( bb.isDisjointWith( shiftedBB_X ));
00278 
00279     // intersecting box is IN
00280     double inters_coords[nbP*dim] =
00281       {
00282         -2,2
00283       };
00284     INTERP_KERNEL::DirectedBoundingBox ibb( inters_coords, nbP, dim);
00285     CPPUNIT_ASSERT( !bb.isDisjointWith( ibb ));
00286 
00287     // overlapping non-directed BB
00288     double overlappingBB[2*dim] =
00289       {
00290         -5,4
00291       };
00292     CPPUNIT_ASSERT( !bb.isDisjointWith( overlappingBB ));
00293 
00294     // non-overlapping non-directed BB
00295     double nonoverlappingBB_1[2*dim] =
00296       {
00297         -5,-2
00298       };
00299     CPPUNIT_ASSERT( bb.isDisjointWith( nonoverlappingBB_1 ));
00300     double nonoverlappingBB_2[2*dim] =
00301       {
00302         11,16
00303       };
00304     CPPUNIT_ASSERT( bb.isDisjointWith( nonoverlappingBB_2 ));
00305   }
00306 
00307 }