MOAB: Mesh Oriented datABase  (version 5.5.0)
test_boundbox.cpp
Go to the documentation of this file.
1 #include "TestUtil.hpp"
2 #include "moab/Core.hpp"
3 #include "moab/BoundBox.hpp"
4 #include "moab/ScdInterface.hpp"
5 
6 using namespace moab;
7 
8 #include <iostream>
9 
10 //! test add_entities, get_entities, num_entities
11 void test_bound_box();
12 
13 int main()
14 {
15  int err = 0;
16 
17  err += RUN_TEST( test_bound_box );
18 
19  if( !err )
20  printf( "ALL TESTS PASSED\n" );
21  else
22  printf( "%d TESTS FAILED\n", err );
23 
24  return err;
25 }
26 
28 {
29  BoundBox box;
30  CartVect vec( 0.0 );
31  double tol = 0.0;
32  std::vector< double > vals( 6, 0.0 );
33  BoundBox other_box( &vals[0] );
34 
35  // test for contains point failure
36  bool result = box.contains_point( vec.array(), tol );
37  CHECK( !result );
38  result = box.intersects_box( other_box, tol );
39  CHECK( !result );
40 
41  box = other_box;
42  tol = 1.0e-10;
43 
44  // test for success
45  result = box.contains_point( &vals[0], tol );
46  CHECK( result );
47  result = box.intersects_box( other_box, tol );
48  CHECK( result );
49 
50  // check update functions
51  CartVect three( 3.0 );
52  box.update_max( three.array() );
53  result = box.contains_point( three.array(), tol );
54  CHECK( result );
55  result = box.contains_point( ( three * 1.1 ).array(), tol );
56  CHECK( !result );
57  result = box.intersects_box( BoundBox( three, three ), tol );
58  CHECK( result );
59  result = box.intersects_box( BoundBox( 1.1 * three, 3.0 * three ), tol );
60  CHECK( !result );
61 
62  CartVect negthree( -3.0 );
63  box.update_min( negthree.array() );
64  result = box.contains_point( negthree.array(), tol );
65  CHECK( result );
66  result = box.contains_point( ( negthree * 1.1 ).array(), tol );
67  CHECK( !result );
68  result = box.intersects_box( BoundBox( negthree, negthree ), tol );
69  CHECK( result );
70  result = box.intersects_box( BoundBox( 3.0 * negthree, 1.1 * negthree ), tol );
71  CHECK( !result );
72 
73  for( int i = 0; i < 3; i++ )
74  {
75  vals[i] = -4.0;
76  vals[3 + i] = 4.0;
77  }
78  box.update( &vals[0] );
79  result = box.contains_point( &vals[0], tol );
80  CHECK( result );
81  result = box.contains_point( ( CartVect( &vals[0] ) * 1.1 ).array(), tol );
82  CHECK( !result );
83  result = box.intersects_box( BoundBox( &vals[0] ), tol );
84  CHECK( result );
85  result = box.intersects_box( BoundBox( 1.2 * CartVect( &vals[0] ), 1.1 * CartVect( &vals[0] ) ), tol );
86  CHECK( !result );
87 
88  // check length functions
89  tol = 1.0e-6;
90 
91  // box should be 8 on a side, or 3*(2^3)^2 or 192 for diag length squared
92  double diagsq = box.diagonal_squared();
93  CHECK_REAL_EQUAL( diagsq, 192.0, tol );
94  double diag = box.diagonal_length();
95  CHECK_REAL_EQUAL( diag, sqrt( 3.0 ) * 8.0, tol );
96 
97  // check distance function
98 
99  // face-centered
100  vals[0] = vals[1] = 0.0;
101  vals[2] = 6.0;
102  double dist = box.distance_squared( CartVect( &vals[0] ).array() );
103  CHECK_REAL_EQUAL( dist, 4.0, tol );
104  dist = box.distance( CartVect( &vals[0] ).array() );
105  CHECK_REAL_EQUAL( dist, 2.0, tol );
106 
107  // edge-centered
108  vals[0] = 0.0;
109  vals[1] = vals[2] = 6.0;
110  dist = box.distance_squared( CartVect( &vals[0] ).array() );
111  CHECK_REAL_EQUAL( dist, 8.0, tol );
112  dist = box.distance( CartVect( &vals[0] ).array() );
113  CHECK_REAL_EQUAL( dist, sqrt( 8.0 ), tol );
114 
115  // vertex-centered
116  vals[0] = vals[1] = vals[2] = 6.0;
117  dist = box.distance_squared( CartVect( &vals[0] ).array() );
118  CHECK_REAL_EQUAL( dist, 12.0, tol );
119  dist = box.distance( CartVect( &vals[0] ).array() );
120  CHECK_REAL_EQUAL( dist, sqrt( 12.0 ), tol );
121 
122  // check entity-based functions
123  Core mb;
124  ScdInterface* scdi;
125  ErrorCode rval = mb.query_interface( scdi );CHECK_ERR( rval );
126  ScdBox* scd_box;
127  // create a 10x10x10 box
128  rval = scdi->construct_box( HomCoord( 0, 0, 0 ), HomCoord( 10, 10, 10 ), NULL, 0, scd_box );CHECK_ERR( rval );
129 
130  EntityHandle vert = scd_box->start_vertex(), elem = scd_box->start_element();
131  rval = box.update( mb, vert );CHECK_ERR( rval );
132  rval = mb.get_coords( &vert, 1, &vals[0] );CHECK_ERR( rval );
133  tol = 1.0e-10;
134  result = box.contains_point( &vals[0], tol );
135  CHECK( result );
136  rval = box.update( mb, elem );CHECK_ERR( rval );
137  rval = mb.get_coords( &elem, 1, &vals[0] );CHECK_ERR( rval );
138  result = box.contains_point( &vals[0], tol );
139  CHECK( result );
140 
141  Range all_elems( elem, elem + scd_box->num_elements() - 1 );
142  rval = box.update( mb, all_elems );CHECK_ERR( rval );
144  box.compute_center( center );
145  tol = 1.0e-6;
146  CHECK_REAL_EQUAL( ( center - CartVect( 5.0, 5.0, 5.0 ) ).length(), 0.0, tol );
147 }