MOAB: Mesh Oriented datABase  (version 5.5.0)
bsp_tree_poly_test.cpp
Go to the documentation of this file.
1 #include "moab/BSPTreePoly.hpp"
2 #include "TestUtil.hpp"
3 #include "moab/CartVect.hpp"
4 
5 using namespace moab;
6 
9 void test_volume();
10 
11 int main()
12 {
13  int error_count = 0;
17  return error_count;
18 }
19 
20 const int hex_faces[6][4] = { { 0, 1, 5, 4 }, { 1, 2, 6, 5 }, { 2, 3, 7, 6 },
21  { 3, 0, 4, 7 }, { 3, 2, 1, 0 }, { 4, 5, 6, 7 } };
22 
23 static void get_corners( CartVect corners[8] )
24 {
25  corners[0] = CartVect( 1, 1, 0 );
26  corners[1] = CartVect( 6, 1, 0 );
27  corners[2] = CartVect( 6, 3, 0 );
28  corners[3] = CartVect( 1, 3, 0 );
29  corners[4] = CartVect( 1, 1, 2 );
30  corners[5] = CartVect( 4, 1, 2 );
31  corners[6] = CartVect( 4, 3, 2 );
32  corners[7] = CartVect( 1, 3, 2 );
33 }
34 
36  const CartVect* coords,
37  int num_corners,
38  const int* face_indices = 0 )
39 {
40  std::vector< const BSPTreePoly::Face* >::iterator i;
41  std::vector< const BSPTreePoly::Face* > faces;
42  std::vector< CartVect > corners;
43  poly.get_faces( faces );
44  for( i = faces.begin(); i != faces.end(); ++i )
45  {
46  corners.clear();
47  poly.get_vertices( *i, corners );
48  if( corners.size() != (unsigned)num_corners ) continue;
49 
50  int j;
51  for( j = 0; j < num_corners; ++j )
52  {
53  int corner = face_indices ? face_indices[j] : j;
54  if( ( coords[corner] - corners.front() ).length_squared() < 1e-12 ) break;
55  }
56  if( j == num_corners ) continue;
57 
58  int k;
59  for( k = 1; k < num_corners; ++k )
60  {
61  int corner = face_indices ? face_indices[( j + k ) % num_corners] : ( j + k ) % num_corners;
62  if( ( coords[corner] - corners[k] ).length_squared() > 1e-12 ) break;
63  }
64 
65  if( k == num_corners ) return *i;
66  }
67  return 0;
68 }
69 
71 {
73 
74  CartVect corners[8];
75  get_corners( corners );
76  BSPTreePoly poly( corners );
77  CHECK( poly.is_valid() );
78 
79  std::vector< const BSPTreePoly::Face* > faces;
80  poly.get_faces( faces );
81  CHECK_EQUAL( (size_t)6, faces.size() );
82 
83  CHECK( 0 != find_face( poly, corners, 4, hex_faces[0] ) );
84  CHECK( 0 != find_face( poly, corners, 4, hex_faces[1] ) );
85  CHECK( 0 != find_face( poly, corners, 4, hex_faces[2] ) );
86  CHECK( 0 != find_face( poly, corners, 4, hex_faces[3] ) );
87  CHECK( 0 != find_face( poly, corners, 4, hex_faces[4] ) );
88  CHECK( 0 != find_face( poly, corners, 4, hex_faces[5] ) );
89 }
90 
92 {
93  // create a hexahedron
95  CartVect corners[8];
96  get_corners( corners );
97  BSPTreePoly poly( corners );
98  CHECK( poly.is_valid() );
99 
100  // check that a plane entirely above the
101  // polyhedron (coincident with one face)
102  // doesn't modify the polyhedron
103  bool r = poly.cut_polyhedron( CartVect( 0, 0, 1 ), -2 );
104  CHECK( !r );
105  CHECK( poly.is_valid() );
106 
107  // cut in half with Z=1 plane
108  r = poly.cut_polyhedron( CartVect( 0, 0, 1 ), -1 );
109  CHECK( r );
110  CHECK( poly.is_valid() );
111  for( int i = 0; i < 8; ++i )
112  {
113  if( fabs( corners[i][2] - 2 ) < 1e-6 ) corners[i][2] = 1;
114  if( fabs( corners[i][0] - 4 ) < 1e-6 ) corners[i][0] = 5;
115  }
116 
117  std::vector< const BSPTreePoly::Face* > faces;
118  poly.get_faces( faces );
119  CHECK_EQUAL( (size_t)6, faces.size() );
120 
121  CHECK( 0 != find_face( poly, corners, 4, hex_faces[0] ) );
122  CHECK( 0 != find_face( poly, corners, 4, hex_faces[1] ) );
123  CHECK( 0 != find_face( poly, corners, 4, hex_faces[2] ) );
124  CHECK( 0 != find_face( poly, corners, 4, hex_faces[3] ) );
125  CHECK( 0 != find_face( poly, corners, 4, hex_faces[4] ) );
126  CHECK( 0 != find_face( poly, corners, 4, hex_faces[5] ) );
127 
128  // create a hexahedron
130  get_corners( corners );
131  poly.set( corners );
132  CHECK( poly.is_valid() );
133 
134  // cut off two corners using X=5 plane
135  r = poly.cut_polyhedron( CartVect( 1, 0, 0 ), -5 );
136  CHECK( r );
137  CHECK( poly.is_valid() );
138 
139  faces.clear();
140  poly.get_faces( faces );
141  CHECK_EQUAL( (size_t)7, faces.size() );
142 
143  CartVect new_vtx1( 5, 1, 1 );
144  CartVect new_vtx2( 5, 1, 0 );
145  CartVect new_vtx3( 5, 3, 0 );
146  CartVect new_vtx4( 5, 3, 1 );
147 
148  CartVect face1[5] = { corners[0], new_vtx2, new_vtx1, corners[5], corners[4] };
149  CartVect face2[4] = { new_vtx1, new_vtx4, corners[6], corners[5] };
150  CartVect face3[5] = { new_vtx4, new_vtx3, corners[3], corners[7], corners[6] };
151  CartVect face5[4] = { corners[3], new_vtx3, new_vtx2, corners[0] };
152  CartVect face7[4] = { new_vtx1, new_vtx2, new_vtx3, new_vtx4 };
153 
154  CHECK( 0 != find_face( poly, face1, 5 ) );
155  CHECK( 0 != find_face( poly, face2, 4 ) );
156  CHECK( 0 != find_face( poly, face3, 5 ) );
157  CHECK( 0 != find_face( poly, corners, 4, hex_faces[3] ) );
158  CHECK( 0 != find_face( poly, face5, 4 ) );
159  CHECK( 0 != find_face( poly, corners, 4, hex_faces[5] ) );
160  CHECK( 0 != find_face( poly, face7, 4 ) );
161 }
162 
164 {
165  CartVect corners[8];
166  get_corners( corners );
167  BSPTreePoly poly( corners );
168  CHECK( poly.is_valid() );
169 
170  CHECK_REAL_EQUAL( 16.0, poly.volume(), 1e-6 );
171 }