Mesh Oriented datABase  (version 5.5.1)
An array-based unstructured mesh library
obb_analysis.cpp File Reference
#include <iostream>
#include <fstream>
#include <sstream>
#include "moab/Core.hpp"
#include "moab/Interface.hpp"
#include "moab/OrientedBoxTreeTool.hpp"
#include "moab/CartVect.hpp"
#include "moab/OrientedBox.hpp"
#include "moab/GeomTopoTool.hpp"
#include "cgm2moab.hpp"
+ Include dependency graph for obb_analysis.cpp:

Go to the source code of this file.

Classes

class  TriCounter
 
class  TriStats
 

Functions

ErrorCode obbvis_create (GeomTopoTool &gtt, std::vector< int > &volumes, int grid, std::string &filename)
 
static double std_dev (double sqr, double sum, double count)
 
ErrorCode obbstat_write (GeomTopoTool &gtt, std::vector< int > &volumes, std::vector< std::string > &, std::ostream &out)
 

Function Documentation

◆ obbstat_write()

ErrorCode obbstat_write ( GeomTopoTool gtt,
std::vector< int > &  volumes,
std::vector< std::string > &  ,
std::ostream &  out 
)

Definition at line 374 of file obb_analysis.cpp.

378 {
379 
380  ErrorCode ret = MB_SUCCESS;
381  OrientedBoxTreeTool& obbtool = *gtt.obb_tree();
382 
383  // can assume that volume numbers are valid.
384  for( std::vector< int >::iterator i = volumes.begin(); i != volumes.end(); ++i )
385  {
386  EntityHandle vol_root;
387  EntityHandle vol = gtt.entity_by_id( 3, *i );
388  CHECKERR( gtt, ret );
389 
390  if( vol == 0 )
391  {
392  std::cerr << "ERROR: volume " << *i << " has no entity." << std::endl;
393  continue;
394  }
395 
396  ret = gtt.get_root( vol, vol_root );
397  CHECKERR( gtt, ret );
398 
399  out << "\nVolume " << *i << " " << std::flush;
400 
401  if( gtt.is_implicit_complement( vol ) ) out << "(implicit complement) ";
402  out << std::endl;
403 
404  // get all surfaces in volume
405  Range surfs;
406  ret = gtt.get_moab_instance()->get_child_meshsets( vol, surfs );
407  CHECKERR( gtt, ret );
408 
409  out << " with " << surfs.size() << " surfaces" << std::endl;
410 
411  TriStats ts( gtt.get_moab_instance(), &obbtool, vol_root );
412  ret = obbtool.preorder_traverse( vol_root, ts );
413  CHECKERR( gtt, ret );
414  ts.write_results( out );
415 
416  if( verbose )
417  {
418  out << "Surface list: " << std::flush;
419  for( Range::iterator j = surfs.begin(); j != surfs.end(); ++j )
420  {
421  out << gtt.global_id( *j );
422  if( j + 1 != surfs.end() ) out << ",";
423  }
424  out << std::endl;
425  ret = obbtool.stats( vol_root, out );
426  CHECKERR( gtt, ret );
427  }
428 
429  out << "\n ------------ " << std::endl;
430  }
431 
432  return ret;
433 }

References moab::Range::begin(), CHECKERR, moab::Range::end(), moab::GeomTopoTool::entity_by_id(), ErrorCode, moab::Interface::get_child_meshsets(), moab::GeomTopoTool::get_moab_instance(), moab::GeomTopoTool::get_root(), moab::GeomTopoTool::global_id(), moab::GeomTopoTool::is_implicit_complement(), MB_SUCCESS, moab::GeomTopoTool::obb_tree(), moab::OrientedBoxTreeTool::preorder_traverse(), moab::Range::size(), moab::OrientedBoxTreeTool::stats(), verbose, and TriStats::write_results().

◆ obbvis_create()

ErrorCode obbvis_create ( GeomTopoTool gtt,
std::vector< int > &  volumes,
int  grid,
std::string &  filename 
)

Definition at line 50 of file obb_analysis.cpp.

51 {
52  OrientedBoxTreeTool& obbtool = *gtt.obb_tree();
53 
54  CartVect min, max;
55  EntityHandle vol = gtt.entity_by_id( 3, volumes.front() );
56  double middle[3];
57  double axis1[3], axis2[3], axis3[3];
58  double minPt[3], maxPt[3];
59  ErrorCode rval = gtt.get_obb( vol, middle, axis1, axis2, axis3 );
60  // compute min and max verticies
61  for( int i = 0; i < 3; i++ )
62  {
63  double sum = fabs( axis1[i] ) + fabs( axis2[i] ) + fabs( axis3[i] );
64  minPt[i] = middle[i] - sum;
65  maxPt[i] = middle[i] + sum;
66  }
67  CHECKERR( gtt, rval );
68 
69  /* Compute an axis-aligned bounding box of all the requested volumes */
70  for( std::vector< int >::iterator i = volumes.begin() + 1; i != volumes.end(); ++i )
71  {
72  CartVect i_min, i_max;
73  vol = gtt.entity_by_id( 3, *i );
74  rval = gtt.get_obb( vol, middle, axis1, axis2, axis3 );
75  // compute min and max verticies
76  for( int j = 0; j < 3; j++ )
77  {
78  double sum = fabs( axis1[j] ) + fabs( axis2[j] ) + fabs( axis3[j] );
79  minPt[j] = middle[j] - sum;
80  maxPt[j] = middle[j] + sum;
81  }
82 
83  for( int j = 0; j < 3; ++j )
84  {
85  min[j] = std::min( min[j], i_min[j] );
86  max[j] = std::max( max[j], i_max[j] );
87  }
88  }
89 
90  // These vectors could be repurposed to describe an OBB without changing the loops below
91  CartVect center( middle );
92  CartVect v1( axis1 );
93  CartVect v2( axis2 );
94  CartVect v3( axis3 );
95 
96  /* Compute the vertices of the visualization grid. Calculation points are at the center
97  of each cell in this grid, so make grid+1 vertices in each direction. */
98  int numpoints = pow( (double)( grid + 1 ), 3 );
99  double* pgrid = new double[numpoints * 3];
100  int idx = 0;
101 
102  for( int i = 0; i < numpoints * 3; ++i )
103  pgrid[i] = 0.0;
104 
105  for( int i = 0; i <= grid; ++i )
106  {
107  CartVect x = -v1 + ( ( v1 * 2.0 ) * ( i / (double)grid ) );
108 
109  for( int j = 0; j <= grid; ++j )
110  {
111  CartVect y = -v2 + ( ( v2 * 2.0 ) * ( j / (double)grid ) );
112 
113  for( int k = 0; k <= grid; ++k )
114  {
115  CartVect z = -v3 + ( ( v3 * 2.0 ) * ( k / (double)grid ) );
116 
117  CartVect p = center + x + y + z;
118  for( int d = 0; d < 3; ++d )
119  {
120  pgrid[idx++] = p[d];
121  }
122  }
123  }
124  }
125 
126  /* Create a new MOAB to use for output, and build the vertex grid */
127  Core mb2;
128  Range r;
129  rval = mb2.create_vertices( pgrid, numpoints, r );
130  CHECKERR( mb2, rval );
131 
132  Tag lttag;
133  rval = mb2.tag_get_handle( "LEAFTRIS", sizeof( int ), MB_TYPE_INTEGER, lttag,
135  CHECKERR( mb2, rval );
136 
137  int row = grid + 1;
138  int side = row * row;
139  EntityHandle connect[8];
140  EntityHandle hex;
141 
142  // offset from grid corner to grid center
143  CartVect grid_hex_center_offset = ( v1 + v2 + v3 ) * 2 * ( 1.0 / grid );
144 
145  // collect all the surfaces from the requested volumes to iterate over --
146  // this prevents checking a shared surface more than once.
147  Range surfs;
148  for( std::vector< int >::iterator it = volumes.begin(); it != volumes.end(); ++it )
149  {
150 
151  vol = gtt.entity_by_id( 3, *it );
152  Range it_surfs;
153  rval = gtt.get_moab_instance()->get_child_meshsets( vol, it_surfs );
154  CHECKERR( gtt, rval );
155  surfs.merge( it_surfs );
156  }
157  std::cout << "visualizing " << surfs.size() << " surfaces." << std::endl;
158 
159  /* Build hexes for any point with 1 or more leaftris */
160  for( int i = 0; i < grid; ++i )
161  {
162  for( int j = 0; j < grid; ++j )
163  {
164  for( int k = 0; k < grid; ++k )
165  {
166 
167  idx = ( side * k ) + ( row * j ) + i;
168  assert( idx + 1 + row + side > numpoints - 1 );
169 
170  CartVect loc = CartVect( ( pgrid + ( idx * 3 ) ) ) + grid_hex_center_offset;
171  TriCounter tc( gtt.get_moab_instance(), &obbtool, loc );
172 
173  for( Range::iterator it = surfs.begin(); it != surfs.end(); ++it )
174  {
175 
176  EntityHandle surf_tree;
177  rval = gtt.get_root( *it, surf_tree );
178  CHECKERR( gtt, rval );
179 
180  rval = obbtool.preorder_traverse( surf_tree, tc );
181  CHECKERR( gtt, rval );
182  }
183 
184  if( tc.count == 0 ) continue;
185 
186  connect[0] = r[idx];
187  connect[1] = r[idx + 1];
188  connect[2] = r[idx + 1 + row];
189  connect[3] = r[idx + row];
190  connect[4] = r[idx + side];
191  connect[5] = r[idx + 1 + side];
192  connect[6] = r[idx + 1 + row + side];
193  connect[7] = r[idx + row + side];
194 
195  rval = mb2.create_element( MBHEX, connect, 8, hex );
196  CHECKERR( mb2, rval );
197 
198  rval = mb2.tag_set_data( lttag, &hex, 1, &( tc.count ) );
199  CHECKERR( mb2, rval );
200  }
201  }
202  }
203 
204  if( verbose )
205  {
206  std::cout << "Writing " << filename << std::endl;
207  }
208  rval = mb2.write_file( filename.c_str() );
209  CHECKERR( mb2, rval );
210 
211  return rval;
212 }

References moab::Range::begin(), center(), CHECKERR, TriCounter::count, moab::Core::create_element(), moab::Core::create_vertices(), moab::Range::end(), moab::GeomTopoTool::entity_by_id(), ErrorCode, moab::Interface::get_child_meshsets(), moab::GeomTopoTool::get_moab_instance(), moab::GeomTopoTool::get_obb(), moab::GeomTopoTool::get_root(), MB_TAG_BYTES, MB_TAG_CREAT, MB_TAG_DENSE, MB_TAG_EXCL, MB_TYPE_INTEGER, MBHEX, moab::Range::merge(), moab::GeomTopoTool::obb_tree(), moab::OrientedBoxTreeTool::preorder_traverse(), moab::Range::size(), moab::sum(), moab::Core::tag_get_handle(), moab::Core::tag_set_data(), verbose, and moab::Core::write_file().

◆ std_dev()

static double std_dev ( double  sqr,
double  sum,
double  count 
)
inlinestatic

Definition at line 215 of file obb_analysis.cpp.

216 {
217  sum /= count;
218  sqr /= count;
219  return sqrt( sqr - sum * sum );
220 }

References moab::sum().