Changeset 971

Show
Ignore:
Timestamp:
04/02/07 17:27:28 (3 years ago)
Author:
jakraft
Message:

o Add test for overlap between a box and a plane
o Add test for overlap between a box and a triangle
o Add unit tests for above methods

GeomUtilTests?.cpp | 328 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
MBGeomUtil.cpp | 139 ++++++++++++++++++++++
MBGeomUtil.hpp | 69 +++++++++++
3 files changed, 536 insertions(+)

Location:
MOAB/trunk
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • MOAB/trunk/GeomUtilTests.cpp

    r875 r971  
    4848 
    4949 
     50void test_box_plane_norm( MBCartVect norm,  
     51                          MBCartVect min, 
     52                          MBCartVect max ) 
     53{ 
     54  MBCartVect c_lower = min; 
     55  MBCartVect c_upper = max; 
     56  for (int i = 0; i < 3; ++i) 
     57    if (norm[i] < 0.0) 
     58      std::swap(c_lower[i],c_upper[i]); 
     59   
     60  MBCartVect p_below = c_lower - norm; 
     61  MBCartVect p_lower = c_lower + norm; 
     62  MBCartVect p_upper = c_upper - norm; 
     63  MBCartVect p_above = c_upper + norm; 
     64   
     65  double below = -(p_below % norm); 
     66  double lower = -(p_lower % norm); 
     67  double upper = -(p_upper % norm); 
     68  double above = -(p_above % norm); 
     69   
     70  ASSERT( !box_plane_overlap( norm, below, min, max ) ); 
     71  ASSERT(  box_plane_overlap( norm, lower, min, max ) ); 
     72  ASSERT(  box_plane_overlap( norm, upper, min, max ) ); 
     73  ASSERT( !box_plane_overlap( norm, above, min, max ) ); 
     74} 
     75 
     76void test_box_plane_axis( int axis, double ns,  
     77                          const MBCartVect& min,  
     78                          const MBCartVect& max ) 
     79{ 
     80  MBCartVect norm(0.0); 
     81  norm[axis] = ns; 
     82  test_box_plane_norm( norm, min, max ); 
     83} 
     84 
     85void test_box_plane_edge( int axis1, int axis2, bool flip_axis2, 
     86                          MBCartVect min, MBCartVect max ) 
     87{ 
     88  MBCartVect norm(0.0); 
     89  norm[axis1] = max[axis1] - min[axis1]; 
     90  if (flip_axis2) 
     91    norm[axis2] = min[axis2] - max[axis2]; 
     92  else 
     93    norm[axis2] = max[axis2] - min[axis2]; 
     94  norm.normalize(); 
     95   
     96  test_box_plane_norm( norm, min, max ); 
     97} 
     98 
     99void test_box_plane_corner( int xdir, int ydir, int zdir,  
     100                            MBCartVect min, MBCartVect max ) 
     101{ 
     102  MBCartVect norm(max - min); 
     103  norm[0] *= xdir; 
     104  norm[1] *= ydir; 
     105  norm[2] *= zdir; 
     106  test_box_plane_norm( norm, min, max ); 
     107} 
     108 
     109void test_box_plane_overlap() 
     110{ 
     111  const MBCartVect min( -1, -2, -3 ); 
     112  const MBCartVect max(  6,  4,  2 ); 
     113   
     114    // test with planes orthogonal to Z axis 
     115  test_box_plane_axis( 2, 2.0, min, max ); 
     116    // test with planes orthogonal to X axis 
     117  test_box_plane_axis( 1,-2.0, min, max ); 
     118    // test with planes orthogonal to Y axis 
     119  test_box_plane_axis( 1, 1.0, min, max ); 
     120 
     121    // test with plane orthogonal to face diagonals 
     122  test_box_plane_edge( 0, 1, true,  min, max ); 
     123  test_box_plane_edge( 0, 1, false, min, max ); 
     124  test_box_plane_edge( 0, 2, true,  min, max ); 
     125  test_box_plane_edge( 0, 2, false, min, max ); 
     126  test_box_plane_edge( 2, 1, true,  min, max ); 
     127  test_box_plane_edge( 2, 1, false, min, max ); 
     128   
     129    // test with plane orthogonal to box diagonals 
     130  test_box_plane_corner( 1, 1, 1, min, max ); 
     131  test_box_plane_corner( 1, 1,-1, min, max ); 
     132  test_box_plane_corner( 1,-1,-1, min, max ); 
     133  test_box_plane_corner( 1,-1, 1, min, max ); 
     134}      
     135 
     136void test_box_tri_overlap() 
     137{ 
     138  MBCartVect coords[3]; 
     139  MBCartVect center, dims; 
     140   
     141    // test box projection within triangle, z-plane 
     142  coords[0] = MBCartVect( 0, 0, 0 ); 
     143  coords[1] = MBCartVect( 0, 4, 0 ); 
     144  coords[2] = MBCartVect(-4, 0, 0 ); 
     145  center = MBCartVect( -2, 1, 0 ); 
     146  dims = MBCartVect( 1, 0.5, 3 ); 
     147  ASSERT(  box_tri_overlap( coords, center, dims ) ); 
     148    // move box below plane of triangle 
     149  center[2] = -4; 
     150  ASSERT( !box_tri_overlap( coords, center, dims ) ); 
     151    // move box above plane of triangle 
     152  center[2] =  4; 
     153  ASSERT( !box_tri_overlap( coords, center, dims ) ); 
     154   
     155    // test box projection within triangle, x-plane 
     156  coords[0] = MBCartVect( 3, 3, 0 ); 
     157  coords[1] = MBCartVect( 3, 3, 1 ); 
     158  coords[2] = MBCartVect( 3, 0, 0 ); 
     159  center = MBCartVect( 3, 2.5, .25 ); 
     160  dims = MBCartVect( 0.001, 0.4, .2 ); 
     161  ASSERT(  box_tri_overlap( coords, center, dims ) ); 
     162    // move box below plane of triangle 
     163  center[0] = 2; 
     164  ASSERT( !box_tri_overlap( coords, center, dims ) ); 
     165    // move box above plane of triangle 
     166  center[0] = 4; 
     167  ASSERT( !box_tri_overlap( coords, center, dims ) ); 
     168   
     169    // test tri slices corner at +x,+y,+z 
     170  coords[0] = MBCartVect(3,1,1); 
     171  coords[1] = MBCartVect(1,3,1); 
     172  coords[2] = MBCartVect(1,1,3); 
     173  ASSERT(  box_tri_overlap( coords, MBCartVect(1,1,1), MBCartVect(1,1,1) ) ); 
     174    // test with tri above the corner 
     175  ASSERT( !box_tri_overlap( coords, MBCartVect(0,0,0), MBCartVect(1,1,1) ) ); 
     176    // test tri slices corner at -x,-y,-z 
     177  coords[0] = MBCartVect(-1,1,1); 
     178  coords[1] = MBCartVect(1,-1,1); 
     179  coords[2] = MBCartVect(1,1,-1); 
     180  ASSERT(  box_tri_overlap( coords, MBCartVect(1,1,1), MBCartVect(1,1,1) ) ); 
     181    // test with tri below the corner 
     182  ASSERT( !box_tri_overlap( coords, MBCartVect(2,2,2),MBCartVect(1,1,1) ) ); 
     183   
     184    // test tri slices corner at -x,+y,+z 
     185  coords[0] = MBCartVect( 0.5, 0.0, 2.5); 
     186  coords[1] = MBCartVect( 0.5, 2.5, 0.0); 
     187  coords[2] = MBCartVect(-0.5, 0.0, 0.0); 
     188  ASSERT(  box_tri_overlap( coords, MBCartVect(1,1,1), MBCartVect(1,1,1) ) ); 
     189    // test with tri above the corner 
     190  ASSERT( !box_tri_overlap( coords, MBCartVect(2,1,1), MBCartVect(1,1,1) ) ); 
     191   
     192    // test tri slices corner at +x,-y,-z 
     193  coords[0] = MBCartVect( 0.5, 0.0,-1.5); 
     194  coords[1] = MBCartVect( 0.5,-1.5, 0.0); 
     195  coords[2] = MBCartVect( 1.5, 0.0, 0.0); 
     196  ASSERT(  box_tri_overlap( coords, MBCartVect(1,1,1), MBCartVect(1,1,1) ) ); 
     197    // test with tri above the corner 
     198  ASSERT( !box_tri_overlap( coords, MBCartVect(0,1,1), MBCartVect(1,1,1) ) ); 
     199 
     200    // test tri slices corner at +x,-y,+z 
     201  coords[0] = MBCartVect( 1.0, 1.0, 2.5 ); 
     202  coords[1] = MBCartVect( 2.5, 1.0, 1.0 ); 
     203  coords[2] = MBCartVect( 1.0,-0.5, 1.0 ); 
     204  ASSERT(  box_tri_overlap( coords, MBCartVect(1,1,1), MBCartVect(1,1,1) ) ); 
     205    // test with tri above the corner 
     206  ASSERT( !box_tri_overlap( coords, MBCartVect(-1,1,1), MBCartVect(1,1,1) ) ); 
     207 
     208    // test tri slices corner at -x,+y,-z   
     209  coords[0] = MBCartVect( 1.0,  1.0,-0.5 ); 
     210  coords[1] = MBCartVect(-0.5,  1.0, 1.0 ); 
     211  coords[2] = MBCartVect( 1.0,  2.5, 1.0 ); 
     212  ASSERT(  box_tri_overlap( coords, MBCartVect(1,1,1), MBCartVect(1,1,1) ) ); 
     213    // test with tri above the corner 
     214  ASSERT( !box_tri_overlap( coords, MBCartVect(3,1,1), MBCartVect(1,1,1) ) ); 
     215 
     216    // test tri slices corner at +x,+y,-z 
     217  coords[0] = MBCartVect(-0.1, 1.0, 1.0); 
     218  coords[1] = MBCartVect( 1.0,-0.1, 1.0); 
     219  coords[2] = MBCartVect( 1.0, 1.0,-0.1); 
     220  ASSERT(  box_tri_overlap( coords, MBCartVect(1,1,1), MBCartVect(1,1,1) ) ); 
     221    // test with tri outside box 
     222  ASSERT( !box_tri_overlap( coords, MBCartVect(1,1,3), MBCartVect(1,1,1) ) ); 
     223   
     224    // test tri slices corner at -x,-y,+z 
     225  coords[0] = MBCartVect( 2.1, 1.0, 1.0); 
     226  coords[1] = MBCartVect( 1.0, 2.1, 1.0); 
     227  coords[2] = MBCartVect( 1.0, 1.0, 2.1); 
     228  ASSERT(  box_tri_overlap( coords, MBCartVect(1,1,1), MBCartVect(1,1,1) ) ); 
     229    // test with tri outside box 
     230  ASSERT( !box_tri_overlap( coords, MBCartVect(1,1,-1), MBCartVect(1,1,1) ) ); 
     231   
     232    // box edge parallel to x at +y,+z passes through triangle 
     233  coords[0] = MBCartVect( 1.0, 1.0, 3.0); 
     234  coords[1] = MBCartVect( 1.0, 3.0, 3.0); 
     235  coords[2] = MBCartVect( 1.0, 3.0, 1.0); 
     236  ASSERT(  box_tri_overlap( coords, MBCartVect(1,1,1), MBCartVect(1,1,1) ) ); 
     237    // test with tri outside box 
     238  ASSERT( !box_tri_overlap( coords, MBCartVect(1,1,0.3), MBCartVect(1,1,1) ) ); 
     239   
     240    // box edge parallel to x at +y,-z passes through triangle 
     241  coords[0] = MBCartVect( 1.0, 3.0, 1.0); 
     242  coords[1] = MBCartVect( 1.0, 3.0,-1.0); 
     243  coords[2] = MBCartVect( 1.0, 1.0,-1.0); 
     244  ASSERT(  box_tri_overlap( coords, MBCartVect(1,1,1), MBCartVect(1,1,1) ) ); 
     245    // test with tri outside box 
     246  ASSERT( !box_tri_overlap( coords, MBCartVect(1,1,1.7), MBCartVect(1,1,1) ) ); 
     247   
     248    // box edge parallel to x at -y,-z passes through triangle 
     249  coords[0] = MBCartVect( 1.0,-1.0, 1.0); 
     250  coords[1] = MBCartVect( 1.0,-1.0,-1.0); 
     251  coords[2] = MBCartVect( 1.0, 1.0,-1.0); 
     252  ASSERT(  box_tri_overlap( coords, MBCartVect(1,1,1), MBCartVect(1,1,1) ) ); 
     253    // test with tri outside box 
     254  ASSERT( !box_tri_overlap( coords, MBCartVect(1,1,1.7), MBCartVect(1,1,1) ) ); 
     255   
     256    // box edge parallel to x at -y,+z passes through triangle 
     257  coords[0] = MBCartVect( 1.0,-1.0, 1.0); 
     258  coords[1] = MBCartVect( 1.0,-1.0, 3.0); 
     259  coords[2] = MBCartVect( 1.0, 1.0, 3.0); 
     260  ASSERT(  box_tri_overlap( coords, MBCartVect(1,1,1), MBCartVect(1,1,1) ) ); 
     261    // test with tri outside box 
     262  ASSERT( !box_tri_overlap( coords, MBCartVect(1,1,0.3), MBCartVect(1,1,1) ) ); 
     263   
     264    // box edge parallel to y at +x,+z passes through triangle 
     265  coords[0] = MBCartVect( 1.0, 1.0, 3.0); 
     266  coords[1] = MBCartVect( 3.0, 1.0, 3.0); 
     267  coords[2] = MBCartVect( 3.0, 1.0, 1.0); 
     268  ASSERT(  box_tri_overlap( coords, MBCartVect(1,1,1), MBCartVect(1,1,1) ) ); 
     269    // test with tri outside box 
     270  ASSERT( !box_tri_overlap( coords, MBCartVect(1,1,0.3), MBCartVect(1,1,1) ) ); 
     271   
     272    // box edge parallel to y at -x,+z passes through triangle 
     273  coords[0] = MBCartVect( 1.0, 1.0, 3.0); 
     274  coords[1] = MBCartVect(-1.0, 1.0, 3.0); 
     275  coords[2] = MBCartVect(-1.0, 1.0, 1.0); 
     276  ASSERT(  box_tri_overlap( coords, MBCartVect(1,1,1), MBCartVect(1,1,1) ) ); 
     277    // test with tri outside box 
     278  ASSERT( !box_tri_overlap( coords, MBCartVect(1,1,0.3), MBCartVect(1,1,1) ) ); 
     279   
     280    // box edge parallel to y at +x,-z passes through triangle 
     281  coords[0] = MBCartVect( 1.0, 1.0,-1.0); 
     282  coords[1] = MBCartVect( 3.0, 1.0,-1.0); 
     283  coords[2] = MBCartVect( 3.0, 1.0, 1.0); 
     284  ASSERT(  box_tri_overlap( coords, MBCartVect(1,1,1), MBCartVect(1,1,1) ) ); 
     285    // test with tri outside box 
     286  ASSERT( !box_tri_overlap( coords, MBCartVect(1,1,1.7), MBCartVect(1,1,1) ) ); 
     287   
     288    // box edge parallel to y at -x,-z passes through triangle 
     289  coords[0] = MBCartVect( 1.0, 1.0,-1.0); 
     290  coords[1] = MBCartVect(-1.0, 1.0,-1.0); 
     291  coords[2] = MBCartVect(-1.0, 1.0, 1.0); 
     292  ASSERT(  box_tri_overlap( coords, MBCartVect(1,1,1), MBCartVect(1,1,1) ) ); 
     293    // test with tri outside box 
     294  ASSERT( !box_tri_overlap( coords, MBCartVect(1,1,1.7), MBCartVect(1,1,1) ) ); 
     295   
     296    // box edge parallel to z at +x,+y passes through triangle 
     297  coords[0] = MBCartVect( 1.0, 3.0, 1.0); 
     298  coords[1] = MBCartVect( 3.0, 3.0, 1.0); 
     299  coords[2] = MBCartVect( 3.0, 1.0, 1.0); 
     300  ASSERT(  box_tri_overlap( coords, MBCartVect(1,1,1), MBCartVect(1,1,1) ) ); 
     301    // test with tri outside box 
     302  ASSERT( !box_tri_overlap( coords, MBCartVect(0.3,1,1), MBCartVect(1,1,1) ) ); 
     303   
     304    // box edge parallel to z at +x,-y passes through triangle 
     305  coords[0] = MBCartVect( 1.0,-1.0, 1.0); 
     306  coords[1] = MBCartVect( 3.0,-1.0, 1.0); 
     307  coords[2] = MBCartVect( 3.0, 1.0, 1.0); 
     308  ASSERT(  box_tri_overlap( coords, MBCartVect(1,1,1), MBCartVect(1,1,1) ) ); 
     309    // test with tri outside box 
     310  ASSERT( !box_tri_overlap( coords, MBCartVect(0.3,1,1), MBCartVect(1,1,1) ) ); 
     311   
     312    // box edge parallel to z at -x,+y passes through triangle 
     313  coords[0] = MBCartVect( 1.0, 3.0, 1.0); 
     314  coords[1] = MBCartVect(-1.0, 3.0, 1.0); 
     315  coords[2] = MBCartVect(-1.0, 1.0, 1.0); 
     316  ASSERT(  box_tri_overlap( coords, MBCartVect(1,1,1), MBCartVect(1,1,1) ) ); 
     317    // test with tri outside box 
     318  ASSERT( !box_tri_overlap( coords, MBCartVect(1.7,1,1), MBCartVect(1,1,1) ) ); 
     319   
     320    // box edge parallel to z at -x,-y passes through triangle 
     321  coords[0] = MBCartVect( 1.0,-1.0, 1.0); 
     322  coords[1] = MBCartVect(-1.0,-1.0, 1.0); 
     323  coords[2] = MBCartVect(-1.0, 1.0, 1.0); 
     324  ASSERT(  box_tri_overlap( coords, MBCartVect(1,1,1), MBCartVect(1,1,1) ) ); 
     325    // test with tri outside box 
     326  ASSERT( !box_tri_overlap( coords, MBCartVect(1.7,1,1), MBCartVect(1,1,1) ) ); 
     327   
     328    // triangle penetrates +x face 
     329  coords[0] = MBCartVect( 2.0, 2.0, 2.0 ); 
     330  coords[1] = MBCartVect( 5.0, 3.0, 2.0 ); 
     331  coords[2] = MBCartVect( 5.0, 1.0, 2.0 ); 
     332  ASSERT(  box_tri_overlap( coords, MBCartVect(2,2,2), MBCartVect(2,2,2) ) ); 
     333    // test with tri outside box 
     334  ASSERT( !box_tri_overlap( coords, MBCartVect(-1,2,2), MBCartVect(2,2,2) ) ); 
     335   
     336    // triangle penetrates -x face 
     337  coords[0] = MBCartVect( 2.0, 2.0, 2.0 ); 
     338  coords[1] = MBCartVect(-1.0, 3.0, 2.0 ); 
     339  coords[2] = MBCartVect(-1.0, 1.0, 2.0 ); 
     340  ASSERT(  box_tri_overlap( coords, MBCartVect(2,2,2), MBCartVect(2,2,2) ) ); 
     341    // test with tri outside box 
     342  ASSERT( !box_tri_overlap( coords, MBCartVect(5,2,2), MBCartVect(2,2,2) ) ); 
     343   
     344    // triangle penetrates +y face 
     345  coords[0] = MBCartVect( 2.0, 2.0, 2.0 ); 
     346  coords[1] = MBCartVect( 3.0, 5.0, 2.0 ); 
     347  coords[2] = MBCartVect( 1.0, 5.0, 2.0 ); 
     348  ASSERT(  box_tri_overlap( coords, MBCartVect(2,2,2), MBCartVect(2,2,2) ) ); 
     349    // test with tri outside box 
     350  ASSERT( !box_tri_overlap( coords, MBCartVect(2,-1,2), MBCartVect(2,2,2) ) ); 
     351   
     352    // triangle penetrates -y face 
     353  coords[0] = MBCartVect( 2.0, 2.0, 2.0 ); 
     354  coords[1] = MBCartVect( 3.0,-1.0, 2.0 ); 
     355  coords[2] = MBCartVect( 1.0,-1.0, 2.0 ); 
     356  ASSERT(  box_tri_overlap( coords, MBCartVect(2,2,2), MBCartVect(2,2,2) ) ); 
     357    // test with tri outside box 
     358  ASSERT( !box_tri_overlap( coords, MBCartVect(2,5,2), MBCartVect(2,2,2) ) ); 
     359   
     360    // triangle penetrates +z face 
     361  coords[0] = MBCartVect( 2.0, 2.0, 2.0 ); 
     362  coords[1] = MBCartVect( 2.0, 3.0, 5.0 ); 
     363  coords[2] = MBCartVect( 2.0, 1.0, 5.0 ); 
     364  ASSERT(  box_tri_overlap( coords, MBCartVect(2,2,2), MBCartVect(2,2,2) ) ); 
     365    // test with tri outside box 
     366  ASSERT( !box_tri_overlap( coords, MBCartVect(2,2,-1), MBCartVect(2,2,2) ) ); 
     367   
     368    // triangle penetrates -z face 
     369  coords[0] = MBCartVect( 2.0, 2.0, 2.0 ); 
     370  coords[1] = MBCartVect( 2.0, 3.0,-1.0 ); 
     371  coords[2] = MBCartVect( 2.0, 1.0,-1.0 ); 
     372  ASSERT(  box_tri_overlap( coords, MBCartVect(2,2,2), MBCartVect(2,2,2) ) ); 
     373    // test with tri outside box 
     374  ASSERT( !box_tri_overlap( coords, MBCartVect(2,2,5), MBCartVect(2,2,2) ) ); 
     375} 
    50376 
    51377void test_ray_tri_intersect() 
     
    272598int main() 
    273599{ 
     600  test_box_plane_overlap(); 
     601  test_box_tri_overlap(); 
    274602  test_ray_tri_intersect(); 
    275603  test_closest_location_on_tri(); 
  • MOAB/trunk/MBGeomUtil.cpp

    r939 r971  
    2121#include "MBCartVect.hpp" 
    2222#include <cmath> 
     23#include <algorithm> 
    2324 
    2425namespace MBGeomUtil { 
     
    7677  return true; 
    7778} 
     79 
     80bool box_plane_overlap( const MBCartVect& normal, 
     81                        double d, 
     82                        MBCartVect min, 
     83                        MBCartVect max ) 
     84{ 
     85  if (normal[0] < 0.0) 
     86    std::swap( min[0], max[0] ); 
     87  if (normal[1] < 0.0) 
     88    std::swap( min[1], max[1] ); 
     89  if (normal[2] < 0.0) 
     90    std::swap( min[2], max[2] ); 
     91   
     92  return (normal % min <= -d) && (normal % max >= -d); 
     93} 
     94 
     95 
     96#define CHECK_RANGE( A, B, R ) \ 
     97  if ((A) < (B)) { \ 
     98    if ((A) > (R) || (B) < -(R)) \ 
     99      return false; \ 
     100  } \ 
     101  else if ((B) > (R) || (A) < -(R)) \ 
     102    return false 
     103 
     104/* Adapted from: http://jgt.akpeters.com/papers/AkenineMoller01/tribox.html 
     105 * Use separating axis theorem to test for overlap between triangle 
     106 * and axis-aligned box. 
     107 * 
     108 * Test for overlap in these directions: 
     109 * 1) {x,y,z}-directions  
     110 * 2) normal of triangle 
     111 * 3) crossprod of triangle edge with {x,y,z}-direction 
     112 */ 
     113bool box_tri_overlap( const MBCartVect vertices[3], 
     114                      const MBCartVect& box_center, 
     115                      const MBCartVect& box_dims ) 
     116{ 
     117    // translate everthing such that box is centered at origin 
     118  const MBCartVect v0( vertices[0] - box_center ); 
     119  const MBCartVect v1( vertices[1] - box_center ); 
     120  const MBCartVect v2( vertices[2] - box_center ); 
     121 
     122  // do case 1) tests 
     123  if (v0[0] > box_dims[0] && v1[0] > box_dims[0] && v2[0] > box_dims[0]) 
     124    return false; 
     125  if (v0[1] > box_dims[1] && v1[1] > box_dims[1] && v2[1] > box_dims[1]) 
     126    return false; 
     127  if (v0[2] > box_dims[2] && v1[2] > box_dims[2] && v2[2] > box_dims[2]) 
     128    return false; 
     129  if (v0[0] < -box_dims[0] && v1[0] < -box_dims[0] && v2[0] < -box_dims[0]) 
     130    return false; 
     131  if (v0[1] < -box_dims[1] && v1[1] < -box_dims[1] && v2[1] < -box_dims[1]) 
     132    return false; 
     133  if (v0[2] < -box_dims[2] && v1[2] < -box_dims[2] && v2[2] < -box_dims[2]) 
     134    return false; 
     135   
     136    // compute triangle edge vectors 
     137  const MBCartVect e0( vertices[1] - vertices[0] ); 
     138  const MBCartVect e1( vertices[2] - vertices[1] ); 
     139  const MBCartVect e2( vertices[0] - vertices[2] ); 
     140   
     141    // do case 3) tests  
     142  double fex, fey, fez, p0, p1, p2, rad; 
     143  fex = fabs(e0[0]); 
     144  fey = fabs(e0[1]); 
     145  fez = fabs(e0[2]); 
     146   
     147  p0 = e0[2]*v0[1] - e0[1]*v0[2]; 
     148  p2 = e0[2]*v2[1] - e0[1]*v2[2]; 
     149  rad = fez * box_dims[1] + fey * box_dims[2]; 
     150  CHECK_RANGE( p0, p2, rad ); 
     151   
     152  p0 = -e0[2]*v0[0] + e0[0]*v0[2]; 
     153  p2 = -e0[2]*v2[0] + e0[0]+v2[2]; 
     154  rad = fez * box_dims[0] + fex * box_dims[2]; 
     155  CHECK_RANGE( p0, p2, rad ); 
     156     
     157  p1 = e0[1]*v1[0] - e0[0]*v1[1]; 
     158  p2 = e0[1]*v2[0] - e0[0]*v2[1]; 
     159  rad = fey * box_dims[0] + fex * box_dims[1]; 
     160  CHECK_RANGE( p1, p2, rad ); 
     161   
     162  fex = fabs(e1[0]); 
     163  fey = fabs(e1[1]); 
     164  fez = fabs(e1[2]); 
     165   
     166  p0 = e1[2]*v0[1] - e1[1]*v0[2]; 
     167  p2 = e1[2]*v2[1] - e1[1]*v2[2]; 
     168  rad = fez * box_dims[1] + fey * box_dims[2]; 
     169  CHECK_RANGE( p0, p2, rad ); 
     170   
     171  p0 = -e1[2]*v0[0] + e1[0]*v0[2]; 
     172  p2 = -e1[2]*v2[0] + e1[0]*v2[2]; 
     173  rad = fez * box_dims[0] + fex * box_dims[2]; 
     174  CHECK_RANGE( p0, p2, rad ); 
     175   
     176  p0 = e1[1]*v0[0] - e1[0]*v0[1]; 
     177  p1 = e1[1]*v1[0] - e1[0]*v1[1]; 
     178  rad = fey * box_dims[0] + fex * box_dims[1]; 
     179  CHECK_RANGE( p0, p1, rad ); 
     180   
     181  fex = fabs(e2[0]); 
     182  fey = fabs(e2[1]); 
     183  fez = fabs(e2[2]); 
     184   
     185  p0 = e2[2]*v0[1] - e2[1]*v0[2]; 
     186  p1 = e2[2]*v1[1] - e2[1]*v1[2]; 
     187  rad = fez * box_dims[1] + fey * box_dims[2]; 
     188  CHECK_RANGE( p0, p1, rad ); 
     189   
     190  p0 = -e2[2]*v0[0] + e2[0]*v0[2]; 
     191  p1 = -e2[2]*v1[0] + e2[0]*v1[2]; 
     192  rad = fez * box_dims[0] + fex * box_dims[2]; 
     193  CHECK_RANGE( p0, p1, rad ); 
     194   
     195  p1 = e2[1]*v1[0] - e2[0]*v1[1]; 
     196  p2 = e2[1]*v2[0] - e2[0]*v2[1]; 
     197  rad = fey * box_dims[0] + fex * box_dims[1]; 
     198  CHECK_RANGE( p1, p2, rad ); 
     199   
     200  // do case 2) test 
     201  MBCartVect n = e0 * e1; 
     202  return box_plane_overlap( n, -(n % v0), -box_dims, box_dims ); 
     203} 
     204   
     205 
     206bool box_tri_overlap( const MBCartVect  triangle_corners[3], 
     207                      const MBCartVect& box_min_corner, 
     208                      const MBCartVect& box_max_corner, 
     209                      double            tolerance ) 
     210{ 
     211  const MBCartVect box_center = 0.5 * (box_max_corner + box_min_corner); 
     212  const MBCartVect box_hf_dim = 0.5 * (box_max_corner - box_min_corner); 
     213  return box_tri_overlap( triangle_corners, 
     214                          box_center, 
     215                          box_hf_dim + MBCartVect(tolerance) ); 
     216}  
    78217 
    79218//from: http://www.geometrictools.com/Documentation/DistancePoint3Triangle3.pdf#search=%22closest%20point%20on%20triangle%22 
  • MOAB/trunk/MBGeomUtil.hpp

    r902 r971  
    3333                        double& t_out, 
    3434                        const double* ray_length = 0 ); 
     35 
     36/**\brief Test if plane intersects axis-aligned box 
     37 * 
     38 * Test for intersection between an unbounded plane and 
     39 * an axis-aligned box. 
     40 *\param plane_normal Vector in plane normal direction (need *not* 
     41 *                    be a unit vector).  The N in  
     42 *                    the plane equation: N . X + D = 0 
     43 *\param plane_coeff  The scalar 'D' term in the plane equation: 
     44 *                    N . X + D = 0 
     45 *\param box_min_corner The smallest coordinates of the box along each 
     46 *                    axis.  The corner of the box for which all three 
     47 *                    coordinate values are smaller than those of any 
     48 *                    other corner.  The X, Y, Z values for the planes 
     49 *                    normal to those axes and bounding the box on the 
     50 *                    -X, -Y, and -Z sides respectively. 
     51 *\param box_max_corner The largest coordinates of the box along each 
     52 *                    axis.  The corner of the box for which all three 
     53 *                    coordinate values are larger than those of any 
     54 *                    other corner.  The X, Y, Z values for the planes 
     55 *                    normal to those axes and bounding the box on the 
     56 *                    +X, +Y, and +Z sides respectively. 
     57 *\return true if overlap, false otherwise. 
     58 */ 
     59bool box_plane_overlap( const MBCartVect& plane_normal,  
     60                        double            plane_coeff, 
     61                        MBCartVect        box_min_corner,  
     62                        MBCartVect        box_max_corner ); 
     63 
     64/**\brief Test if triangle intersects axis-aligned box 
     65 * 
     66 * Test if a triangle intersects an axis-aligned box. 
     67 *\param triangle_corners  The corners of the triangle. 
     68 *\param box_min_corner The smallest coordinates of the box along each 
     69 *                    axis.  The corner of the box for which all three 
     70 *                    coordinate values are smaller than those of any 
     71 *                    other corner.  The X, Y, Z values for the planes 
     72 *                    normal to those axes and bounding the box on the 
     73 *                    -X, -Y, and -Z sides respectively. 
     74 *\param box_max_corner The largest coordinates of the box along each 
     75 *                    axis.  The corner of the box for which all three 
     76 *                    coordinate values are larger than those of any 
     77 *                    other corner.  The X, Y, Z values for the planes 
     78 *                    normal to those axes and bounding the box on the 
     79 *                    +X, +Y, and +Z sides respectively. 
     80 *\param tolerance    The tolerance used in the intersection test.  The box 
     81 *                    size is increased by this amount before the intersection 
     82 *                    test. 
     83 *\return true if overlap, false otherwise. 
     84 */ 
     85bool box_tri_overlap( const MBCartVect  triangle_corners[3], 
     86                      const MBCartVect& box_min_corner, 
     87                      const MBCartVect& box_max_corner, 
     88                      double            tolerance ); 
     89 
     90/**\brief Test if triangle intersects axis-aligned box 
     91 * 
     92 * Test if a triangle intersects an axis-aligned box. 
     93 *\param triangle_corners  The corners of the triangle. 
     94 *\param box_center   The center of the box. 
     95 *\param box_hanf_dims The distance along each axis, respectively, from the 
     96 *                    box_center to the boundary of the box. 
     97 *\return true if overlap, false otherwise. 
     98 */ 
     99bool box_tri_overlap( const MBCartVect  triangle_corners[3], 
     100                      const MBCartVect& box_center, 
     101                      const MBCartVect& box_half_dims ); 
     102 
     103                        
    35104 
    36105/**\brief find closest location on triangle