1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830
|
///////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2012-2018 DreamWorks Animation LLC
//
// All rights reserved. This software is distributed under the
// Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
//
// Redistributions of source code must retain the above copyright
// and license notice and the following restrictions and disclaimer.
//
// * Neither the name of DreamWorks Animation nor the names of
// its contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// IN NO EVENT SHALL THE COPYRIGHT HOLDERS' AND CONTRIBUTORS' AGGREGATE
// LIABILITY FOR ALL CLAIMS REGARDLESS OF THEIR BASIS EXCEED US$250.00.
//
///////////////////////////////////////////////////////////////////////////
#include <openvdb/Exceptions.h>
#include <openvdb/math/Maps.h>
#include <openvdb/util/MapsUtil.h>
#include <cppunit/extensions/HelperMacros.h>
class TestMaps: public CppUnit::TestCase
{
public:
CPPUNIT_TEST_SUITE(TestMaps);
CPPUNIT_TEST(testTranslation);
CPPUNIT_TEST(testRotation);
CPPUNIT_TEST(testScaleDefault);
CPPUNIT_TEST(testScaleTranslate);
CPPUNIT_TEST(testUniformScaleTranslate);
CPPUNIT_TEST(testDecomposition);
CPPUNIT_TEST(testFrustum);
CPPUNIT_TEST(testCalcBoundingBox);
CPPUNIT_TEST(testApproxInverse);
CPPUNIT_TEST(testUniformScale);
CPPUNIT_TEST(testJacobians);
CPPUNIT_TEST_SUITE_END();
void testTranslation();
void testRotation();
void testScaleDefault();
void testScaleTranslate();
void testUniformScaleTranslate();
void testDecomposition();
void testFrustum();
void testCalcBoundingBox();
void testApproxInverse();
void testUniformScale();
void testJacobians();
//void testIsType();
};
CPPUNIT_TEST_SUITE_REGISTRATION(TestMaps);
void
TestMaps::testApproxInverse()
{
using namespace openvdb::math;
Mat4d singular = Mat4d::identity();
singular[1][1] = 0.f;
{
Mat4d singularInv = approxInverse(singular);
CPPUNIT_ASSERT( singular == singularInv );
}
{
Mat4d rot = Mat4d::identity();
rot.setToRotation(X_AXIS, M_PI/4.);
Mat4d rotInv = rot.inverse();
Mat4d mat = rotInv * singular * rot;
Mat4d singularInv = approxInverse(mat);
// this matrix is equal to its own singular inverse
CPPUNIT_ASSERT( mat.eq(singularInv) );
}
{
Mat4d m = Mat4d::identity();
m[0][1] = 1;
// should give true inverse, since this matrix has det=1
Mat4d minv = approxInverse(m);
Mat4d prod = m * minv;
CPPUNIT_ASSERT( prod.eq( Mat4d::identity() ) );
}
{
Mat4d m = Mat4d::identity();
m[0][1] = 1;
m[1][1] = 0;
// should give true inverse, since this matrix has det=1
Mat4d minv = approxInverse(m);
Mat4d expected = Mat4d::zero();
expected[3][3] = 1;
CPPUNIT_ASSERT( minv.eq(expected ) );
}
}
void
TestMaps::testUniformScale()
{
using namespace openvdb::math;
AffineMap map;
CPPUNIT_ASSERT(map.hasUniformScale());
// Apply uniform scale: should still have square voxels
map.accumPreScale(Vec3d(2, 2, 2));
CPPUNIT_ASSERT(map.hasUniformScale());
// Apply a rotation, should still have squaure voxels.
map.accumPostRotation(X_AXIS, 2.5);
CPPUNIT_ASSERT(map.hasUniformScale());
// non uniform scaling will stretch the voxels
map.accumPostScale(Vec3d(1, 3, 1) );
CPPUNIT_ASSERT(!map.hasUniformScale());
}
void
TestMaps::testTranslation()
{
using namespace openvdb::math;
double TOL = 1e-7;
TranslationMap::Ptr translation(new TranslationMap(Vec3d(1,1,1)));
CPPUNIT_ASSERT(is_linear<TranslationMap>::value);
TranslationMap another_translation(Vec3d(1,1,1));
CPPUNIT_ASSERT(another_translation == *translation);
TranslationMap::Ptr translate_by_two(new TranslationMap(Vec3d(2,2,2)));
CPPUNIT_ASSERT(*translate_by_two != *translation);
CPPUNIT_ASSERT_DOUBLES_EQUAL(translate_by_two->determinant(), 1, TOL);
CPPUNIT_ASSERT(translate_by_two->hasUniformScale());
/// apply the map forward
Vec3d unit(1,0,0);
Vec3d result = translate_by_two->applyMap(unit);
CPPUNIT_ASSERT_DOUBLES_EQUAL(result(0), 3, TOL);
CPPUNIT_ASSERT_DOUBLES_EQUAL(result(1), 2, TOL);
CPPUNIT_ASSERT_DOUBLES_EQUAL(result(2), 2, TOL);
/// invert the map
result = translate_by_two->applyInverseMap(result);
CPPUNIT_ASSERT_DOUBLES_EQUAL(result(0), 1, TOL);
CPPUNIT_ASSERT_DOUBLES_EQUAL(result(1), 0, TOL);
CPPUNIT_ASSERT_DOUBLES_EQUAL(result(2), 0, TOL);
/// Inverse Jacobian Transpose
result = translate_by_two->applyIJT(result);
CPPUNIT_ASSERT_DOUBLES_EQUAL(result(0), 1, TOL);
CPPUNIT_ASSERT_DOUBLES_EQUAL(result(1), 0, TOL);
CPPUNIT_ASSERT_DOUBLES_EQUAL(result(2), 0, TOL);
/// Jacobian Transpose
result = translate_by_two->applyJT(translate_by_two->applyIJT(unit));
CPPUNIT_ASSERT_DOUBLES_EQUAL(result(0), unit(0), TOL);
CPPUNIT_ASSERT_DOUBLES_EQUAL(result(1), unit(1), TOL);
CPPUNIT_ASSERT_DOUBLES_EQUAL(result(2), unit(2), TOL);
MapBase::Ptr inverse = translation->inverseMap();
CPPUNIT_ASSERT(inverse->type() == TranslationMap::mapType());
// apply the map forward and the inverse map back
result = inverse->applyMap(translation->applyMap(unit));
CPPUNIT_ASSERT_DOUBLES_EQUAL(result(0), 1, TOL);
CPPUNIT_ASSERT_DOUBLES_EQUAL(result(1), 0, TOL);
CPPUNIT_ASSERT_DOUBLES_EQUAL(result(2), 0, TOL);
}
void
TestMaps::testScaleDefault()
{
using namespace openvdb::math;
double TOL = 1e-7;
// testing default constructor
// should be the identity
ScaleMap::Ptr scale(new ScaleMap());
Vec3d unit(1, 1, 1);
Vec3d result = scale->applyMap(unit);
CPPUNIT_ASSERT_DOUBLES_EQUAL(unit(0), result(0), TOL);
CPPUNIT_ASSERT_DOUBLES_EQUAL(unit(1), result(1), TOL);
CPPUNIT_ASSERT_DOUBLES_EQUAL(unit(2), result(2), TOL);
result = scale->applyInverseMap(unit);
CPPUNIT_ASSERT_DOUBLES_EQUAL(unit(0), result(0), TOL);
CPPUNIT_ASSERT_DOUBLES_EQUAL(unit(1), result(1), TOL);
CPPUNIT_ASSERT_DOUBLES_EQUAL(unit(2), result(2), TOL);
MapBase::Ptr inverse = scale->inverseMap();
CPPUNIT_ASSERT(inverse->type() == ScaleMap::mapType());
// apply the map forward and the inverse map back
result = inverse->applyMap(scale->applyMap(unit));
CPPUNIT_ASSERT_DOUBLES_EQUAL(result(0), unit(0), TOL);
CPPUNIT_ASSERT_DOUBLES_EQUAL(result(1), unit(1), TOL);
CPPUNIT_ASSERT_DOUBLES_EQUAL(result(2), unit(2), TOL);
}
void
TestMaps::testRotation()
{
using namespace openvdb::math;
double TOL = 1e-7;
double pi = 4.*atan(1.);
UnitaryMap::Ptr rotation(new UnitaryMap(Vec3d(1,0,0), pi/2));
CPPUNIT_ASSERT(is_linear<UnitaryMap>::value);
UnitaryMap another_rotation(Vec3d(1,0,0), pi/2.);
CPPUNIT_ASSERT(another_rotation == *rotation);
UnitaryMap::Ptr rotation_two(new UnitaryMap(Vec3d(1,0,0), pi/4.));
CPPUNIT_ASSERT(*rotation_two != *rotation);
CPPUNIT_ASSERT_DOUBLES_EQUAL(rotation->determinant(), 1, TOL);
CPPUNIT_ASSERT(rotation_two->hasUniformScale());
/// apply the map forward
Vec3d unit(0,1,0);
Vec3d result = rotation->applyMap(unit);
CPPUNIT_ASSERT_DOUBLES_EQUAL(0, result(0), TOL);
CPPUNIT_ASSERT_DOUBLES_EQUAL(0, result(1), TOL);
CPPUNIT_ASSERT_DOUBLES_EQUAL(1, result(2), TOL);
/// invert the map
result = rotation->applyInverseMap(result);
CPPUNIT_ASSERT_DOUBLES_EQUAL(0, result(0), TOL);
CPPUNIT_ASSERT_DOUBLES_EQUAL(1, result(1), TOL);
CPPUNIT_ASSERT_DOUBLES_EQUAL(0, result(2), TOL);
/// Inverse Jacobian Transpose
result = rotation_two->applyIJT(result); // rotate backwards
CPPUNIT_ASSERT_DOUBLES_EQUAL(0, result(0), TOL);
CPPUNIT_ASSERT_DOUBLES_EQUAL(sqrt(2.)/2, result(1), TOL);
CPPUNIT_ASSERT_DOUBLES_EQUAL(sqrt(2.)/2, result(2), TOL);
/// Jacobian Transpose
result = rotation_two->applyJT(rotation_two->applyIJT(unit));
CPPUNIT_ASSERT_DOUBLES_EQUAL(result(0), unit(0), TOL);
CPPUNIT_ASSERT_DOUBLES_EQUAL(result(1), unit(1), TOL);
CPPUNIT_ASSERT_DOUBLES_EQUAL(result(2), unit(2), TOL);
// Test inverse map
MapBase::Ptr inverse = rotation->inverseMap();
CPPUNIT_ASSERT(inverse->type() == UnitaryMap::mapType());
// apply the map forward and the inverse map back
result = inverse->applyMap(rotation->applyMap(unit));
CPPUNIT_ASSERT_DOUBLES_EQUAL(result(0), unit(0), TOL);
CPPUNIT_ASSERT_DOUBLES_EQUAL(result(1), unit(1), TOL);
CPPUNIT_ASSERT_DOUBLES_EQUAL(result(2), unit(2), TOL);
}
void
TestMaps::testScaleTranslate()
{
using namespace openvdb::math;
double TOL = 1e-7;
CPPUNIT_ASSERT(is_linear<ScaleTranslateMap>::value);
TranslationMap::Ptr translation(new TranslationMap(Vec3d(1,1,1)));
ScaleMap::Ptr scale(new ScaleMap(Vec3d(1,2,3)));
ScaleTranslateMap::Ptr scaleAndTranslate(
new ScaleTranslateMap(*scale, *translation));
TranslationMap translate_by_two(Vec3d(2,2,2));
ScaleTranslateMap another_scaleAndTranslate(*scale, translate_by_two);
CPPUNIT_ASSERT(another_scaleAndTranslate != *scaleAndTranslate);
CPPUNIT_ASSERT(!scaleAndTranslate->hasUniformScale());
//CPPUNIT_ASSERT_DOUBLES_EQUAL(scaleAndTranslate->determinant(), 6, TOL);
/// apply the map forward
Vec3d unit(1,0,0);
Vec3d result = scaleAndTranslate->applyMap(unit);
CPPUNIT_ASSERT_DOUBLES_EQUAL(2, result(0), TOL);
CPPUNIT_ASSERT_DOUBLES_EQUAL(1, result(1), TOL);
CPPUNIT_ASSERT_DOUBLES_EQUAL(1, result(2), TOL);
/// invert the map
result = scaleAndTranslate->applyInverseMap(result);
CPPUNIT_ASSERT_DOUBLES_EQUAL(1, result(0), TOL);
CPPUNIT_ASSERT_DOUBLES_EQUAL(0, result(1), TOL);
CPPUNIT_ASSERT_DOUBLES_EQUAL(0, result(2), TOL);
/// Inverse Jacobian Transpose
result = Vec3d(0,2,0);
result = scaleAndTranslate->applyIJT(result );
CPPUNIT_ASSERT_DOUBLES_EQUAL(0, result(0), TOL);
CPPUNIT_ASSERT_DOUBLES_EQUAL(1, result(1), TOL);
CPPUNIT_ASSERT_DOUBLES_EQUAL(0, result(2), TOL);
/// Jacobian Transpose
result = scaleAndTranslate->applyJT(scaleAndTranslate->applyIJT(unit));
CPPUNIT_ASSERT_DOUBLES_EQUAL(result(0), unit(0), TOL);
CPPUNIT_ASSERT_DOUBLES_EQUAL(result(1), unit(1), TOL);
CPPUNIT_ASSERT_DOUBLES_EQUAL(result(2), unit(2), TOL);
// Test inverse map
MapBase::Ptr inverse = scaleAndTranslate->inverseMap();
CPPUNIT_ASSERT(inverse->type() == ScaleTranslateMap::mapType());
// apply the map forward and the inverse map back
result = inverse->applyMap(scaleAndTranslate->applyMap(unit));
CPPUNIT_ASSERT_DOUBLES_EQUAL(result(0), unit(0), TOL);
CPPUNIT_ASSERT_DOUBLES_EQUAL(result(1), unit(1), TOL);
CPPUNIT_ASSERT_DOUBLES_EQUAL(result(2), unit(2), TOL);
}
void
TestMaps::testUniformScaleTranslate()
{
using namespace openvdb::math;
double TOL = 1e-7;
CPPUNIT_ASSERT(is_linear<UniformScaleMap>::value);
CPPUNIT_ASSERT(is_linear<UniformScaleTranslateMap>::value);
TranslationMap::Ptr translation(new TranslationMap(Vec3d(1,1,1)));
UniformScaleMap::Ptr scale(new UniformScaleMap(2));
UniformScaleTranslateMap::Ptr scaleAndTranslate(
new UniformScaleTranslateMap(*scale, *translation));
TranslationMap translate_by_two(Vec3d(2,2,2));
UniformScaleTranslateMap another_scaleAndTranslate(*scale, translate_by_two);
CPPUNIT_ASSERT(another_scaleAndTranslate != *scaleAndTranslate);
CPPUNIT_ASSERT(scaleAndTranslate->hasUniformScale());
//CPPUNIT_ASSERT_DOUBLES_EQUAL(scaleAndTranslate->determinant(), 6, TOL);
/// apply the map forward
Vec3d unit(1,0,0);
Vec3d result = scaleAndTranslate->applyMap(unit);
CPPUNIT_ASSERT_DOUBLES_EQUAL(3, result(0), TOL);
CPPUNIT_ASSERT_DOUBLES_EQUAL(1, result(1), TOL);
CPPUNIT_ASSERT_DOUBLES_EQUAL(1, result(2), TOL);
/// invert the map
result = scaleAndTranslate->applyInverseMap(result);
CPPUNIT_ASSERT_DOUBLES_EQUAL(1, result(0), TOL);
CPPUNIT_ASSERT_DOUBLES_EQUAL(0, result(1), TOL);
CPPUNIT_ASSERT_DOUBLES_EQUAL(0, result(2), TOL);
/// Inverse Jacobian Transpose
result = Vec3d(0,2,0);
result = scaleAndTranslate->applyIJT(result );
CPPUNIT_ASSERT_DOUBLES_EQUAL(0, result(0), TOL);
CPPUNIT_ASSERT_DOUBLES_EQUAL(1, result(1), TOL);
CPPUNIT_ASSERT_DOUBLES_EQUAL(0, result(2), TOL);
/// Jacobian Transpose
result = scaleAndTranslate->applyJT(scaleAndTranslate->applyIJT(unit));
CPPUNIT_ASSERT_DOUBLES_EQUAL(result(0), unit(0), TOL);
CPPUNIT_ASSERT_DOUBLES_EQUAL(result(1), unit(1), TOL);
CPPUNIT_ASSERT_DOUBLES_EQUAL(result(2), unit(2), TOL);
// Test inverse map
MapBase::Ptr inverse = scaleAndTranslate->inverseMap();
CPPUNIT_ASSERT(inverse->type() == UniformScaleTranslateMap::mapType());
// apply the map forward and the inverse map back
result = inverse->applyMap(scaleAndTranslate->applyMap(unit));
CPPUNIT_ASSERT_DOUBLES_EQUAL(result(0), unit(0), TOL);
CPPUNIT_ASSERT_DOUBLES_EQUAL(result(1), unit(1), TOL);
CPPUNIT_ASSERT_DOUBLES_EQUAL(result(2), unit(2), TOL);
}
void
TestMaps::testDecomposition()
{
using namespace openvdb::math;
//double TOL = 1e-7;
CPPUNIT_ASSERT(is_linear<UnitaryMap>::value);
CPPUNIT_ASSERT(is_linear<SymmetricMap>::value);
CPPUNIT_ASSERT(is_linear<PolarDecomposedMap>::value);
CPPUNIT_ASSERT(is_linear<FullyDecomposedMap>::value);
Mat4d matrix(Mat4d::identity());
Vec3d input_translation(0,0,1);
matrix.setTranslation(input_translation);
matrix(0,0) = 1.8930039;
matrix(1,0) = -0.120080537;
matrix(2,0) = -0.497615212;
matrix(0,1) = -0.120080537;
matrix(1,1) = 2.643265436;
matrix(2,1) = 0.6176957495;
matrix(0,2) = -0.497615212;
matrix(1,2) = 0.6176957495;
matrix(2,2) = 1.4637305884;
FullyDecomposedMap::Ptr decomp = createFullyDecomposedMap(matrix);
/// the singular values
const Vec3<double>& singular_values =
decomp->firstMap().firstMap().secondMap().getScale();
/// expected values
Vec3d expected_values(2, 3, 1);
CPPUNIT_ASSERT( isApproxEqual(singular_values, expected_values) );
const Vec3<double>& the_translation = decomp->secondMap().secondMap().getTranslation();
CPPUNIT_ASSERT( isApproxEqual(the_translation, input_translation));
}
void
TestMaps::testFrustum()
{
using namespace openvdb::math;
openvdb::BBoxd bbox(Vec3d(0), Vec3d(100));
NonlinearFrustumMap frustum(bbox, 1./6., 5);
/// frustum will have depth, far plane - near plane = 5
/// the frustum has width 1 in the front and 6 in the back
Vec3d trans(2,2,2);
NonlinearFrustumMap::Ptr map =
openvdb::StaticPtrCast<NonlinearFrustumMap, MapBase>(
frustum.preScale(Vec3d(10,10,10))->postTranslate(trans));
CPPUNIT_ASSERT(!map->hasUniformScale());
Vec3d result;
result = map->voxelSize();
CPPUNIT_ASSERT( isApproxEqual(result.x(), 0.1));
CPPUNIT_ASSERT( isApproxEqual(result.y(), 0.1));
CPPUNIT_ASSERT( isApproxEqual(result.z(), 0.5, 0.0001));
//--------- Front face
Vec3d corner(0,0,0);
result = map->applyMap(corner);
CPPUNIT_ASSERT(isApproxEqual(result, Vec3d(-5, -5, 0) + trans));
corner = Vec3d(100,0,0);
result = map->applyMap(corner);
CPPUNIT_ASSERT( isApproxEqual(result, Vec3d(5, -5, 0) + trans));
corner = Vec3d(0,100,0);
result = map->applyMap(corner);
CPPUNIT_ASSERT( isApproxEqual(result, Vec3d(-5, 5, 0) + trans));
corner = Vec3d(100,100,0);
result = map->applyMap(corner);
CPPUNIT_ASSERT( isApproxEqual(result, Vec3d(5, 5, 0) + trans));
//--------- Back face
corner = Vec3d(0,0,100);
result = map->applyMap(corner);
CPPUNIT_ASSERT( isApproxEqual(result, Vec3d(-30, -30, 50) + trans)); // 10*(5/2 + 1/2) = 30
corner = Vec3d(100,0,100);
result = map->applyMap(corner);
CPPUNIT_ASSERT( isApproxEqual(result, Vec3d(30, -30, 50) + trans));
corner = Vec3d(0,100,100);
result = map->applyMap(corner);
CPPUNIT_ASSERT( isApproxEqual(result, Vec3d(-30, 30, 50) + trans));
corner = Vec3d(100,100,100);
result = map->applyMap(corner);
CPPUNIT_ASSERT( isApproxEqual(result, Vec3d(30, 30, 50) + trans));
// invert a single corner
result = map->applyInverseMap(Vec3d(30,30,50) + trans);
CPPUNIT_ASSERT( isApproxEqual(result, Vec3d(100, 100, 100)));
CPPUNIT_ASSERT(map->hasSimpleAffine());
/// create a frustum from from camera type information
// the location of the camera
Vec3d position(100,10,1);
// the direction the camera is pointing
Vec3d direction(0,1,1);
direction.normalize();
// the up-direction for the camera
Vec3d up(10,3,-3);
// distance from camera to near-plane measured in the direction 'direction'
double z_near = 100.;
// depth of frustum to far-plane to near-plane
double depth = 500.;
//aspect ratio of frustum: width/height
double aspect = 2;
// voxel count in frustum. the y_count = x_count / aspect
Coord::ValueType x_count = 500;
Coord::ValueType z_count = 5000;
NonlinearFrustumMap frustumMap_from_camera(
position, direction, up, aspect, z_near, depth, x_count, z_count);
Vec3d center;
// find the center of the near plane and make sure it is in the correct place
center = Vec3d(0,0,0);
center += frustumMap_from_camera.applyMap(Vec3d(0,0,0));
center += frustumMap_from_camera.applyMap(Vec3d(500,0,0));
center += frustumMap_from_camera.applyMap(Vec3d(0,250,0));
center += frustumMap_from_camera.applyMap(Vec3d(500,250,0));
center = center /4.;
CPPUNIT_ASSERT( isApproxEqual(center, position + z_near * direction));
// find the center of the far plane and make sure it is in the correct place
center = Vec3d(0,0,0);
center += frustumMap_from_camera.applyMap(Vec3d( 0, 0,5000));
center += frustumMap_from_camera.applyMap(Vec3d(500, 0,5000));
center += frustumMap_from_camera.applyMap(Vec3d( 0,250,5000));
center += frustumMap_from_camera.applyMap(Vec3d(500,250,5000));
center = center /4.;
CPPUNIT_ASSERT( isApproxEqual(center, position + (z_near+depth) * direction));
// check that the frustum has the correct heigh on the near plane
Vec3d corner1 = frustumMap_from_camera.applyMap(Vec3d(0,0,0));
Vec3d corner2 = frustumMap_from_camera.applyMap(Vec3d(0,250,0));
Vec3d side = corner2-corner1;
CPPUNIT_ASSERT( isApproxEqual( side.length(), 2 * up.length()));
// check that the frustum is correctly oriented w.r.t up
side.normalize();
CPPUNIT_ASSERT( isApproxEqual( side * (up.length()), up));
// check that the linear map inside the frustum is a simple affine map (i.e. has no shear)
CPPUNIT_ASSERT(frustumMap_from_camera.hasSimpleAffine());
}
void
TestMaps::testCalcBoundingBox()
{
using namespace openvdb::math;
openvdb::BBoxd world_bbox(Vec3d(0,0,0), Vec3d(1,1,1));
openvdb::BBoxd voxel_bbox;
openvdb::BBoxd expected;
{
AffineMap affine;
affine.accumPreScale(Vec3d(2,2,2));
openvdb::util::calculateBounds<AffineMap>(affine, world_bbox, voxel_bbox);
expected = openvdb::BBoxd(Vec3d(0,0,0), Vec3d(0.5, 0.5, 0.5));
CPPUNIT_ASSERT(isApproxEqual(voxel_bbox.min(), expected.min()));
CPPUNIT_ASSERT(isApproxEqual(voxel_bbox.max(), expected.max()));
affine.accumPostTranslation(Vec3d(1,1,1));
openvdb::util::calculateBounds<AffineMap>(affine, world_bbox, voxel_bbox);
expected = openvdb::BBoxd(Vec3d(-0.5,-0.5,-0.5), Vec3d(0, 0, 0));
CPPUNIT_ASSERT(isApproxEqual(voxel_bbox.min(), expected.min()));
CPPUNIT_ASSERT(isApproxEqual(voxel_bbox.max(), expected.max()));
}
{
AffineMap affine;
affine.accumPreScale(Vec3d(2,2,2));
affine.accumPostTranslation(Vec3d(1,1,1));
// test a sphere:
Vec3d center(0,0,0);
double radius = 10;
openvdb::util::calculateBounds<AffineMap>(affine, center, radius, voxel_bbox);
expected = openvdb::BBoxd(Vec3d(-5.5,-5.5,-5.5), Vec3d(4.5, 4.5, 4.5));
CPPUNIT_ASSERT(isApproxEqual(voxel_bbox.min(), expected.min()));
CPPUNIT_ASSERT(isApproxEqual(voxel_bbox.max(), expected.max()));
}
{
AffineMap affine;
affine.accumPreScale(Vec3d(2,2,2));
double pi = 4.*atan(1.);
affine.accumPreRotation(X_AXIS, pi/4.);
Vec3d center(0,0,0);
double radius = 10;
openvdb::util::calculateBounds<AffineMap>(affine, center, radius, voxel_bbox);
expected = openvdb::BBoxd(Vec3d(-5,-5,-5), Vec3d(5, 5, 5));
CPPUNIT_ASSERT(isApproxEqual(voxel_bbox.min(), expected.min()));
CPPUNIT_ASSERT(isApproxEqual(voxel_bbox.max(), expected.max()));
}
{
AffineMap affine;
affine.accumPreScale(Vec3d(2,1,1));
double pi = 4.*atan(1.);
affine.accumPreRotation(X_AXIS, pi/4.);
Vec3d center(0,0,0);
double radius = 10;
openvdb::util::calculateBounds<AffineMap>(affine, center, radius, voxel_bbox);
expected = openvdb::BBoxd(Vec3d(-5,-10,-10), Vec3d(5, 10, 10));
CPPUNIT_ASSERT(isApproxEqual(voxel_bbox.min(), expected.min()));
CPPUNIT_ASSERT(isApproxEqual(voxel_bbox.max(), expected.max()));
}
{
AffineMap affine;
affine.accumPreScale(Vec3d(2,1,1));
double pi = 4.*atan(1.);
affine.accumPreRotation(X_AXIS, pi/4.);
affine.accumPostTranslation(Vec3d(1,1,1));
Vec3d center(1,1,1);
double radius = 10;
openvdb::util::calculateBounds<AffineMap>(affine, center, radius, voxel_bbox);
expected = openvdb::BBoxd(Vec3d(-5,-10,-10), Vec3d(5, 10, 10));
CPPUNIT_ASSERT(isApproxEqual(voxel_bbox.min(), expected.min()));
CPPUNIT_ASSERT(isApproxEqual(voxel_bbox.max(), expected.max()));
}
{
openvdb::BBoxd bbox(Vec3d(0), Vec3d(100));
NonlinearFrustumMap frustum(bbox, 2, 5);
NonlinearFrustumMap::Ptr map =
openvdb::StaticPtrCast<NonlinearFrustumMap, MapBase>(
frustum.preScale(Vec3d(2,2,2)));
Vec3d center(20,20,10);
double radius(1);
openvdb::util::calculateBounds<NonlinearFrustumMap>(*map, center, radius, voxel_bbox);
}
}
void
TestMaps::testJacobians()
{
using namespace openvdb::math;
const double TOL = 1e-7;
{
AffineMap affine;
const int n = 10;
const double dtheta = M_PI / n;
const Vec3d test(1,2,3);
const Vec3d origin(0,0,0);
for (int i = 0; i < n; ++i) {
double theta = i * dtheta;
affine.accumPostRotation(X_AXIS, theta);
Vec3d result = affine.applyJacobian(test);
Vec3d expected = affine.applyMap(test) - affine.applyMap(origin);
CPPUNIT_ASSERT_DOUBLES_EQUAL(result(0), expected(0), TOL);
CPPUNIT_ASSERT_DOUBLES_EQUAL(result(1), expected(1), TOL);
CPPUNIT_ASSERT_DOUBLES_EQUAL(result(2), expected(2), TOL);
Vec3d tmp = affine.applyInverseJacobian(result);
CPPUNIT_ASSERT_DOUBLES_EQUAL(tmp(0), test(0), TOL);
CPPUNIT_ASSERT_DOUBLES_EQUAL(tmp(1), test(1), TOL);
CPPUNIT_ASSERT_DOUBLES_EQUAL(tmp(2), test(2), TOL);
}
}
{
UniformScaleMap scale(3);
const Vec3d test(1,2,3);
const Vec3d origin(0,0,0);
Vec3d result = scale.applyJacobian(test);
Vec3d expected = scale.applyMap(test) - scale.applyMap(origin);
CPPUNIT_ASSERT_DOUBLES_EQUAL(result(0), expected(0), TOL);
CPPUNIT_ASSERT_DOUBLES_EQUAL(result(1), expected(1), TOL);
CPPUNIT_ASSERT_DOUBLES_EQUAL(result(2), expected(2), TOL);
Vec3d tmp = scale.applyInverseJacobian(result);
CPPUNIT_ASSERT_DOUBLES_EQUAL(tmp(0), test(0), TOL);
CPPUNIT_ASSERT_DOUBLES_EQUAL(tmp(1), test(1), TOL);
CPPUNIT_ASSERT_DOUBLES_EQUAL(tmp(2), test(2), TOL);
}
{
ScaleMap scale(Vec3d(1,2,3));
const Vec3d test(1,2,3);
const Vec3d origin(0,0,0);
Vec3d result = scale.applyJacobian(test);
Vec3d expected = scale.applyMap(test) - scale.applyMap(origin);
CPPUNIT_ASSERT_DOUBLES_EQUAL(result(0), expected(0), TOL);
CPPUNIT_ASSERT_DOUBLES_EQUAL(result(1), expected(1), TOL);
CPPUNIT_ASSERT_DOUBLES_EQUAL(result(2), expected(2), TOL);
Vec3d tmp = scale.applyInverseJacobian(result);
CPPUNIT_ASSERT_DOUBLES_EQUAL(tmp(0), test(0), TOL);
CPPUNIT_ASSERT_DOUBLES_EQUAL(tmp(1), test(1), TOL);
CPPUNIT_ASSERT_DOUBLES_EQUAL(tmp(2), test(2), TOL);
}
{
TranslationMap map(Vec3d(1,2,3));
const Vec3d test(1,2,3);
const Vec3d origin(0,0,0);
Vec3d result = map.applyJacobian(test);
Vec3d expected = map.applyMap(test) - map.applyMap(origin);
CPPUNIT_ASSERT_DOUBLES_EQUAL(result(0), expected(0), TOL);
CPPUNIT_ASSERT_DOUBLES_EQUAL(result(1), expected(1), TOL);
CPPUNIT_ASSERT_DOUBLES_EQUAL(result(2), expected(2), TOL);
Vec3d tmp = map.applyInverseJacobian(result);
CPPUNIT_ASSERT_DOUBLES_EQUAL(tmp(0), test(0), TOL);
CPPUNIT_ASSERT_DOUBLES_EQUAL(tmp(1), test(1), TOL);
CPPUNIT_ASSERT_DOUBLES_EQUAL(tmp(2), test(2), TOL);
}
{
ScaleTranslateMap map(Vec3d(1,2,3), Vec3d(3,5,4));
const Vec3d test(1,2,3);
const Vec3d origin(0,0,0);
Vec3d result = map.applyJacobian(test);
Vec3d expected = map.applyMap(test) - map.applyMap(origin);
CPPUNIT_ASSERT_DOUBLES_EQUAL(result(0), expected(0), TOL);
CPPUNIT_ASSERT_DOUBLES_EQUAL(result(1), expected(1), TOL);
CPPUNIT_ASSERT_DOUBLES_EQUAL(result(2), expected(2), TOL);
Vec3d tmp = map.applyInverseJacobian(result);
CPPUNIT_ASSERT_DOUBLES_EQUAL(tmp(0), test(0), TOL);
CPPUNIT_ASSERT_DOUBLES_EQUAL(tmp(1), test(1), TOL);
CPPUNIT_ASSERT_DOUBLES_EQUAL(tmp(2), test(2), TOL);
}
{
openvdb::BBoxd bbox(Vec3d(0), Vec3d(100));
NonlinearFrustumMap frustum(bbox, 1./6., 5);
/// frustum will have depth, far plane - near plane = 5
/// the frustum has width 1 in the front and 6 in the back
Vec3d trans(2,2,2);
NonlinearFrustumMap::Ptr map =
openvdb::StaticPtrCast<NonlinearFrustumMap, MapBase>(
frustum.preScale(Vec3d(10,10,10))->postTranslate(trans));
const Vec3d test(1,2,3);
const Vec3d origin(0, 0, 0);
// these two drop down to just the linear part
Vec3d lresult = map->applyJacobian(test);
Vec3d ltmp = map->applyInverseJacobian(lresult);
CPPUNIT_ASSERT_DOUBLES_EQUAL(ltmp(0), test(0), TOL);
CPPUNIT_ASSERT_DOUBLES_EQUAL(ltmp(1), test(1), TOL);
CPPUNIT_ASSERT_DOUBLES_EQUAL(ltmp(2), test(2), TOL);
Vec3d isloc(4,5,6);
// these two drop down to just the linear part
Vec3d result = map->applyJacobian(test, isloc);
Vec3d tmp = map->applyInverseJacobian(result, isloc);
CPPUNIT_ASSERT_DOUBLES_EQUAL(tmp(0), test(0), TOL);
CPPUNIT_ASSERT_DOUBLES_EQUAL(tmp(1), test(1), TOL);
CPPUNIT_ASSERT_DOUBLES_EQUAL(tmp(2), test(2), TOL);
}
}
// Copyright (c) 2012-2018 DreamWorks Animation LLC
// All rights reserved. This software is distributed under the
// Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
|