| | 50 | void 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 | |
| | 76 | void 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 | |
| | 85 | void 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 | |
| | 99 | void 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 | |
| | 109 | void 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 | |
| | 136 | void 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 | } |