Salome HOME
a4420571affd0cb2b99ed8860a69ea2e03437cdc
[tools/medcoupling.git] / src / INTERP_KERNELTest / BBTreeTest.cxx
1 // Copyright (C) 2007-2015  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "BBTreeTest.hxx"
21 #include <iostream>
22 #include <vector>
23 #include "DirectedBoundingBox.hxx"
24
25 namespace INTERP_TEST
26 {
27
28
29   void BBTreeTest::setUp() 
30   {
31   }
32
33  
34   void BBTreeTest::tearDown() 
35   {
36   }
37
38   /**
39    * Test that creates a tree in 2D and check that 
40    * the results are correct in three
41    * cases :
42    * a non matching search
43    * a standard case
44    * a bbox overlapping the bboxes of the tree
45    */
46   void BBTreeTest::test_BBTree() {
47     //bbox tree creation
48     const int N=10;
49     double* bbox=new double[4*N*N];
50     for (int i=0; i<N; i++)
51       for (int j=0; j<N; j++)
52         {
53           bbox[4*(i*N+j)]=i;
54           bbox[4*(i*N+j)+1]=i+1;
55           bbox[4*(i*N+j)+2]=j;
56           bbox[4*(i*N+j)+3]=j+1;
57         }
58     BBTree<2> tree(bbox,0,0,N*N);
59     std::vector <int> elems; 
60   
61     //box outside the tree
62     double bbox1[4]={-2.0, -1.0, 0.0, 1.0};
63     tree.getIntersectingElems(bbox1,elems);
64     CPPUNIT_ASSERT_EQUAL(0,(int)elems.size());
65     elems.clear();
66   
67     //box intersecting 4 tree elems
68     double bbox2[4]={2.5, 3.5, 0.5, 1.5};
69     tree.getIntersectingElems(bbox2,elems);
70     CPPUNIT_ASSERT_EQUAL(4,(int)elems.size());
71     elems.clear();
72   
73     //box exactly superimposed to two tree elems
74     double bbox3[4]={5.0,6.0,7.0,9.0};
75     tree.getIntersectingElems(bbox3,elems);
76     CPPUNIT_ASSERT_EQUAL(2,(int)elems.size());
77     elems.clear();
78
79     double xx[2]={1.0,1.0};
80     tree.getElementsAroundPoint(xx,elems);
81     CPPUNIT_ASSERT_EQUAL(4,(int)elems.size());
82
83     delete[] bbox;
84   }
85
86   void BBTreeTest::test_DirectedBB_3D()
87   {
88     // a rectangle 1x2 extruded along vector (10,0,10)
89     const int nbP = 8, dim = 3;
90     double coords[nbP*dim] =
91       {
92         0,0,0,    2,0,0,   2,1,0,   0,1,0, 
93         10,0,10, 12,0,10, 12,1,10, 10,1,10
94       };
95     INTERP_KERNEL::DirectedBoundingBox bb( coords, nbP, dim);
96     bb.enlarge( 1e-12 );
97
98     // corners of extrusion are IN
99     for ( int i = 0; i < nbP*dim; i+=dim )
100       CPPUNIT_ASSERT( !bb.isOut( coords + i ));
101
102     // points near corners of extrusion are OUT
103     double p[nbP*dim] = 
104       {
105         0,0,3,  6,0,3,   5,1,2,   0,1,2, 
106         8,0,9, 11,0,8, 11,0.5,8, 8,0.5,9
107       };
108     for ( int i = 0; i < nbP*dim; i+=dim )
109       CPPUNIT_ASSERT( bb.isOut( p + i ));
110
111     // the extrusions  shifted by 3 in XOY plane are OUT
112     double shifted_X[nbP*dim] =
113       {
114         3,0,0,    5,0,0,   5,1,0,   3,1,0, 
115         13,0,10, 15,0,10, 15,1,10, 13,1,10
116       };
117     double shifted_x[nbP*dim] =
118       {
119         -3,0,0, -1,0,0, -1,1,0, -3,1,0, 
120         7,0,10, 9,0,10, 9,1,10, 7,1,10
121       };
122     double shifted_Y[nbP*dim] =
123       {
124         0,3,0,    2,3,0,   2,4,0,   0,4,0, 
125         10,3,10, 12,3,10, 12,4,10, 10,4,10
126       };
127     double shifted_y[nbP*dim] =
128       {
129         0,-3,0,    2,-3,0,   2,-2,0,   0,-2,0, 
130         10,-3,10, 12,-3,10, 12,-2,10, 10,-2,10
131       };
132     INTERP_KERNEL::DirectedBoundingBox shiftedBB_x( shifted_x, nbP, dim);
133     INTERP_KERNEL::DirectedBoundingBox shiftedBB_X( shifted_X, nbP, dim);
134     INTERP_KERNEL::DirectedBoundingBox shiftedBB_y( shifted_y, nbP, dim);
135     INTERP_KERNEL::DirectedBoundingBox shiftedBB_Y( shifted_Y, nbP, dim);
136
137     CPPUNIT_ASSERT( bb.isDisjointWith( shiftedBB_x ));
138     CPPUNIT_ASSERT( bb.isDisjointWith( shiftedBB_X ));
139     CPPUNIT_ASSERT( bb.isDisjointWith( shiftedBB_y ));
140     CPPUNIT_ASSERT( bb.isDisjointWith( shiftedBB_Y ));
141
142     // intersecting box is IN
143     double inters_coords[nbP*dim] =
144       {
145         0,0,0,    2,0,0,   2,1,0,   0,1,0, 
146         0,0,2,    2,0,2,   2,1,2,   0,1,2
147       };
148     INTERP_KERNEL::DirectedBoundingBox ibb( inters_coords, nbP, dim);
149     CPPUNIT_ASSERT( !bb.isDisjointWith( ibb ));
150
151     // overlapping non-directed BB
152     double overlappingBB[2*dim] =
153       {
154         5,6, 0, 1, -5,4
155       };
156     CPPUNIT_ASSERT( !bb.isDisjointWith( overlappingBB ));
157
158     // non-overlapping non-directed BB
159     double nonoverlappingBB_1[2*dim] =
160       {
161         5,6, 0,1, -5,2
162       };
163     CPPUNIT_ASSERT( bb.isDisjointWith( nonoverlappingBB_1 ));
164     double nonoverlappingBB_2[2*dim] =
165       {
166         5,6, 0,1, 7,20
167       };
168     CPPUNIT_ASSERT( bb.isDisjointWith( nonoverlappingBB_2 ));
169   }
170
171   void BBTreeTest::test_DirectedBB_2D()
172   {
173     // a segment of length 2 extruded along vector (10,10)
174     const int nbP = 4, dim = 2;
175     double coords[nbP*dim] =
176       {
177         0,0,    2,0,
178         10,10, 12,10,
179       };
180     INTERP_KERNEL::DirectedBoundingBox bb( coords, nbP, dim);
181     bb.enlarge( 1e-12 );
182
183     // corners of extrusion are IN
184     for ( int i = 0; i < nbP*dim; i+=dim )
185       CPPUNIT_ASSERT( !bb.isOut( coords + i ));
186
187     // points near corners of extrusion are OUT
188     double p[nbP*dim] = 
189       {
190         1,2,  4,1,
191         11,8, 8,9,
192       };
193     for ( int i = 0; i < nbP*dim; i+=dim )
194       CPPUNIT_ASSERT( bb.isOut( p + i ));
195
196     // the extrusions shifted by 3 along OX are OUT
197     double shifted_X[nbP*dim] =
198       {
199         3,0,    5,0,
200         13,10, 15,10,
201       };
202     double shifted_x[nbP*dim] =
203       {
204         -3,0, -1,0,
205         7,10, 9,10,
206       };
207     INTERP_KERNEL::DirectedBoundingBox shiftedBB_x( shifted_x, nbP, dim);
208     INTERP_KERNEL::DirectedBoundingBox shiftedBB_X( shifted_X, nbP, dim);
209
210     CPPUNIT_ASSERT( bb.isDisjointWith( shiftedBB_x ));
211     CPPUNIT_ASSERT( bb.isDisjointWith( shiftedBB_X ));
212
213     // intersecting box is IN
214     double inters_coords[nbP*dim] =
215       {
216         0,0,    2,0, 
217         0,2,    2,2
218       };
219     INTERP_KERNEL::DirectedBoundingBox ibb( inters_coords, nbP, dim);
220     CPPUNIT_ASSERT( !bb.isDisjointWith( ibb ));
221
222     // overlapping non-directed BB
223     double overlappingBB[2*dim] =
224       {
225         5,6, -5,4
226       };
227     CPPUNIT_ASSERT( !bb.isDisjointWith( overlappingBB ));
228
229     // non-overlapping non-directed BB
230     double nonoverlappingBB_1[2*dim] =
231       {
232         5,6, -5,2
233       };
234     CPPUNIT_ASSERT( bb.isDisjointWith( nonoverlappingBB_1 ));
235     double nonoverlappingBB_2[2*dim] =
236       {
237         5,6, 7,20
238       };
239     CPPUNIT_ASSERT( bb.isDisjointWith( nonoverlappingBB_2 ));
240   }
241
242   void BBTreeTest::test_DirectedBB_1D()
243   {
244     const int nbP = 2, dim = 1;
245     double coords[nbP*dim] =
246       {
247         0, 10
248       };
249     INTERP_KERNEL::DirectedBoundingBox bb( coords, nbP, dim);
250     bb.enlarge( 1e-12 );
251
252     // coords are IN
253     for ( int i = 0; i < nbP*dim; i+=dim )
254       CPPUNIT_ASSERT( !bb.isOut( coords + i ));
255
256     // points near ends are OUT
257     double p[nbP*dim] = 
258       {
259         -0.0001, 10.1
260       };
261     for ( int i = 0; i < nbP*dim; i+=dim )
262       CPPUNIT_ASSERT( bb.isOut( p + i ));
263
264     // shifted boxes are OUT
265     double shifted_X[nbP*dim] =
266       {
267         10.1, 11
268       };
269     double shifted_x[nbP*dim] =
270       {
271         -3.0, -0.001
272       };
273     INTERP_KERNEL::DirectedBoundingBox shiftedBB_x( shifted_x, nbP, dim);
274     INTERP_KERNEL::DirectedBoundingBox shiftedBB_X( shifted_X, nbP, dim);
275
276     CPPUNIT_ASSERT( bb.isDisjointWith( shiftedBB_x ));
277     CPPUNIT_ASSERT( bb.isDisjointWith( shiftedBB_X ));
278
279     // intersecting box is IN
280     double inters_coords[nbP*dim] =
281       {
282         -2,2
283       };
284     INTERP_KERNEL::DirectedBoundingBox ibb( inters_coords, nbP, dim);
285     CPPUNIT_ASSERT( !bb.isDisjointWith( ibb ));
286
287     // overlapping non-directed BB
288     double overlappingBB[2*dim] =
289       {
290         -5,4
291       };
292     CPPUNIT_ASSERT( !bb.isDisjointWith( overlappingBB ));
293
294     // non-overlapping non-directed BB
295     double nonoverlappingBB_1[2*dim] =
296       {
297         -5,-2
298       };
299     CPPUNIT_ASSERT( bb.isDisjointWith( nonoverlappingBB_1 ));
300     double nonoverlappingBB_2[2*dim] =
301       {
302         11,16
303       };
304     CPPUNIT_ASSERT( bb.isDisjointWith( nonoverlappingBB_2 ));
305   }
306
307 }