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 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032
|
/*=========================================================================
Program: Visualization Toolkit
Module: TestIncrementalOctreePointLocator.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkMath.h"
#include "vtkPoints.h"
#include "vtkIdList.h"
#include "vtkTestUtilities.h"
#include "vtkUnstructuredGrid.h"
#include "vtkUnstructuredGridReader.h"
#include "vtkIncrementalOctreePointLocator.h"
#define _BRUTE_FORCE_VERIFICATION_
// The following epsilon value is needed to address numerical inaccuracy /
// precision issues on some platforms: Fast-Release-g++, Win32-cygwin344,
// Win32-minGW, Win32-FreeVC++, and Win32-VS71. In particular, two of the
// references of this value below, as marked, handle Win32-cygwin344 and
// Win32-minGW. The numerical inaccuracy problem has nothing to do with
// the incremental octree point locator or the associated incremental octree
// node itself. Instead it is just due to the multiple sub-tests themselves
// (combined in this single file) in which the brute-force mode employs many
// if-statements / comparisons that involve double values.
//
// For example, vtkMath::Distance2BetweenPoints( point1, point2 ) may not be
// directly used in if-statements (even for some platforms other than the above
// mentioned 5), even though the incremental octree point locator always uses
// double variables for computation and returning values. Another example is
// that the min SQUARED distance D between point A (a given point) and point B
// (the closest point to A) may not be directly used to test the number of points
// within the SQUARED radius D relative to point A herein, though it is supposed
// to be ok (and the number is expected to be 1). The fact is that an epsilon
// needs to be added to D for such a test. Otherwise the numerical inaccuracy
// issue would just cause 0 to be returned --- no any point exists within the
// SQUARED radius D relative to A. Please note that this problem is not caused by
// sqrt() at all because the incremental octree point locator offers an accurate
// function variant FindPointsWithinSquaredRadius() to avoid the obvious numerical
// inaccuracy related to sqrt().
//
// Given the numerical inaccuracy issues on some platforms, the rapid verification
// mode might not be used. Fortunately, this test is fast enough.
#define INC_OCT_PNT_LOC_TESTS_ZERO 0.00000000000001
// NOTE: ALL THE FOLLOWING FLAGS SHOULD BE OFF
//#ifdef _BRUTE_FORCE_VERIFICATION_
//#define _BRUTE_FORCE_VERIFICATION_WRITE_RESULT_
//#define _BRUTE_FORCE_VERIFICATION_WRITE_DISTANCE2_ // do NOT turn this ON
//#endif
// ---------------------------------------------------------------------------
// Meta information of the test data
//
// number of grid points = 2288
// number of unique points = 2200 (in terms of zero tolerance)
// number of duplicate occurrences = 88
// bounding box: [ -2.839926, 2.862497 ]
// [ -2.856848, 2.856848 ]
// [ 0.000000, 1.125546 ]
//
// min squared distance = 1.036624e-005 (for zero-tolerance unique points)
// max squared distance = 3.391319e+001
//----------------------------------------------------------------------------
// Swap an array of (or a single) int, double, or vtkIdType values when
// loading little-endian / Win-based disk files on big-endian platforms.
// Note the two data files for this test are in little-endian binary format.
void SwapForBigEndian( unsigned char * theArray, int tupleSiz, int numTuple )
{
int i, j;
unsigned char * tmpChar0 = theArray;
unsigned char * tmpChar1 = ( unsigned char * ) malloc( tupleSiz );
for ( j = 0; j < numTuple; j ++, tmpChar0 += tupleSiz )
{
for ( i = 0; i < tupleSiz; i ++ )
{
tmpChar1[i] = tmpChar0[ tupleSiz - 1 - i ];
}
for ( i = 0; i < tupleSiz; i ++ )
{
tmpChar0[i] = tmpChar1[i];
}
}
tmpChar0 = NULL;
free( tmpChar1 ); tmpChar1 = NULL;
}
//----------------------------------------------------------------------------
int TestIncrementalOctreePointLocator( int argc, char * argv[] )
{
// specifications
int nClzNpts = 4;
int octreRes[3] = { 64, 128, 256 };
double tolerans[2] = { 0.0, 0.01 };
// variables
int r, t, m, i, j, k;
int retValue = 0;
int numbPnts = 0;
int inserted = 0;
int numInsrt = 0;
int nLocPnts = 0;
int nUniques = 0;
int nDuplics = 0;
int arrayIdx = 0;
int numExPts = 0;
char * fileName = NULL;
FILE * diskFile = NULL;
FILE * pntsFile = NULL;
double pntCoord[3] = { 0.0, 0.0, 0.0 };
double tmpDist2 = 0.0;
double fTempRad = 0.0;
double * pDataPts = NULL;
double * xtentPts = NULL;
double * pLocPnts = NULL;
double * minDist2 = NULL;
double * maxDist2 = NULL;
double * clzNdst2 = NULL;
vtkPoints * dataPnts = NULL;
vtkPoints * insrtPts = NULL;
vtkIdType pointIdx;
vtkIdType * truthIds = NULL;
vtkIdType * resltIds = NULL;
vtkIdList * ptIdList = NULL;
vtkIdList * idxLists[3] = { NULL, NULL, NULL };
vtkUnstructuredGrid * unstruct = NULL;
vtkUnstructuredGridReader * ugReader = NULL;
vtkIncrementalOctreePointLocator * octLocat = NULL;
// load an unstructured grid dataset
fileName = vtkTestUtilities::ExpandDataFileName
( argc, argv, "Data/post.vtk" );
ugReader = vtkUnstructuredGridReader::New();
ugReader->SetFileName( fileName );
delete [] fileName; fileName = NULL;
ugReader->Update();
unstruct = ugReader->GetOutput();
dataPnts = unstruct->GetPoints();
numbPnts = dataPnts->GetNumberOfPoints();
// open a file for reading or writing the ground truth data
fileName = vtkTestUtilities::ExpandDataFileName
( argc, argv, "Data/IncOctPntLocResult.dat" );
#ifdef _BRUTE_FORCE_VERIFICATION_WRITE_RESULT_
diskFile = fopen( fileName, "wb" );
#else
#ifndef _BRUTE_FORCE_VERIFICATION_
diskFile = fopen( fileName, "rb" );
truthIds = ( vtkIdType * )
realloc( truthIds, sizeof( vtkIdType ) * numbPnts );
#endif
#endif
delete [] fileName; fileName = NULL;
// obtain the 3D point coordinates
pDataPts = ( double * )
realloc( pDataPts, sizeof( double ) * 3 * numbPnts );
for ( i = 0; i < numbPnts; i ++ )
{
dataPnts->GetPoint( i, pDataPts + ( i << 1 ) + i );
}
// allocate memory for exactly duplicate points and inherit some points
nUniques = 4;
nDuplics = 300;
arrayIdx = numbPnts * 3;
numExPts = numbPnts + nUniques * nDuplics;
xtentPts = ( double * )
realloc( xtentPts, sizeof( double ) * 3 * numExPts );
memcpy( xtentPts, pDataPts, sizeof( double ) * 3 * numbPnts );
// add an additional number of exactly duplicate points
for ( j = 0; j < nUniques; j ++ )
{
i = ( numbPnts / ( nUniques + 2 ) ) * ( j + 1 );
i = ( i << 1 ) + i;
pntCoord[0] = pDataPts[ i ];
pntCoord[1] = pDataPts[ i + 1 ];
pntCoord[2] = pDataPts[ i + 2 ];
for ( i = 0; i < nDuplics; i ++, arrayIdx += 3 )
{
xtentPts[ arrayIdx ] = pntCoord[0];
xtentPts[ arrayIdx + 1 ] = pntCoord[1];
xtentPts[ arrayIdx + 2 ] = pntCoord[2];
}
}
// memory allocation
ptIdList = vtkIdList::New();
ptIdList->Allocate( numbPnts, numbPnts >> 1 );
insrtPts = vtkPoints::New();
insrtPts->Allocate( numbPnts, numbPnts >> 1 );
octLocat = vtkIncrementalOctreePointLocator::New();
// =========================================================================
// ============================ Point Insertion ============================
// =========================================================================
for ( r = 0; ( r < 3 ) && ( retValue == 0 ); r ++ ) // three resolutions
{
// -----------------------------------------------------------------------
// --------------------- check-based point insertion ---------------------
// -----------------------------------------------------------------------
for ( t = 0; ( t < 2 ) && ( retValue == 0 ); t ++ ) // two tolerances
for ( m = 0; ( m < 3 ) && ( retValue == 0 ); m ++ ) // three functions
{
ptIdList->Reset(); // indices of the inserted points: based on dataPnts
insrtPts->Reset();
octLocat->FreeSearchStructure();
octLocat->SetMaxPointsPerLeaf( octreRes[r] );
octLocat->SetTolerance( tolerans[t] );
octLocat->InitPointInsertion
( insrtPts, dataPnts->GetBounds(), numbPnts );
// ---------------------------------------------------------------------
if ( m == 0 ) // vtkIncrementalOctreePointLocator::InsertUniquePoint()
{
for ( i = 0; i < numbPnts; i ++ )
{
inserted = octLocat->InsertUniquePoint
( pDataPts + ( i << 1 ) + i, pointIdx );
if ( inserted ) ptIdList->InsertNextId( i );
}
}
else
if ( m == 1 ) // vtkIncrementalOctreePointLocator::InsertNextPoint()
{
for ( i = 0; i < numbPnts; i ++ )
{
inserted = octLocat->IsInsertedPoint( pDataPts + ( i << 1 ) + i );
if ( inserted == -1 )
{
octLocat->InsertNextPoint( pDataPts + ( i << 1 ) + i );
ptIdList->InsertNextId( i );
}
}
}
else // vtkIncrementalOctreePointLocator::InsertPoint()
{
numInsrt = 0;
for ( i = 0; i < numbPnts; i ++ )
{
inserted = octLocat->IsInsertedPoint( pDataPts + ( i << 1 ) + i );
if ( inserted == -1 )
{
octLocat->InsertPoint( numInsrt ++, pDataPts + ( i << 1 ) + i );
ptIdList->InsertNextId( i );
}
}
}
// -------------------------------------------------------------------//
#ifdef _BRUTE_FORCE_VERIFICATION_
// ---------------------------------------------------------------------
// verify the point insertion result in brute force mode
double tempPnt0[3];
double tempPnt1[3];
double tolerns2[2] = { tolerans[0] * tolerans[0],
tolerans[1] * tolerans[1]
};
numInsrt = ptIdList->GetNumberOfIds();
// check if the squared distance between any two inserted points is
// less than the threshold
for ( j = 0; ( j < numInsrt - 1 ) && ( retValue == 0 ); j ++ )
{
insrtPts->GetPoint( j, tempPnt0 );
for ( i = j + 1; ( i < numInsrt ) && ( retValue == 0 ); i ++ )
{
insrtPts->GetPoint( i, tempPnt1 );
tmpDist2 = vtkMath::Distance2BetweenPoints( tempPnt0, tempPnt1 );
if ( tmpDist2 <= tolerns2[t]) retValue = 1;
}
}
// check if there is any point whose distance to ALL inserted points
// is greater than the threshold
for ( j = 0; ( j < numbPnts ) && ( retValue == 0 ); j ++ )
{
if ( ptIdList->IsId( j ) != -1 ) continue; // already inserted
int bGreater = 1;
for ( i = 0; i < numInsrt; i ++ )
{
insrtPts->GetPoint( i, tempPnt1 );
tmpDist2 = vtkMath::Distance2BetweenPoints
( pDataPts + ( j << 1 ) + j, tempPnt1 );
if ( tmpDist2 <= tolerns2[t] ) bGreater = 0; // No 'break' here !!!
}
retValue = bGreater;
}
// -------------------------------------------------------------------//
#else
// ---------------------------------------------------------------------
// rapid point index-based verification
fread( &numInsrt, sizeof( int ), 1, diskFile );
#ifdef VTK_WORDS_BIGENDIAN
SwapForBigEndian
( ( unsigned char * ) ( &numInsrt ), sizeof( int ), 1 );
#endif
if ( numInsrt == ptIdList->GetNumberOfIds() )
{
int samePtId = 1;
fread( truthIds, sizeof( vtkIdType ), numInsrt, diskFile );
#ifdef VTK_WORDS_BIGENDIAN
SwapForBigEndian
( ( unsigned char * ) truthIds, sizeof( vtkIdType ), numInsrt );
#endif
for ( i = 0; ( i < numInsrt ) && ( samePtId == 1 ); i ++ )
{
samePtId = ( truthIds[i] == ptIdList->GetId( i ) ) ? 1 : 0;
}
retValue = 1 - samePtId;
}
else retValue = 1;
// -------------------------------------------------------------------//
#endif
// ---------------------------------------------------------------------
// write the point indices as the ground truth
#ifdef _BRUTE_FORCE_VERIFICATION_WRITE_RESULT_
if ( retValue == 0 )
{
numInsrt = ptIdList->GetNumberOfIds();
fwrite( &numInsrt, sizeof( int ), 1, diskFile );
fwrite( ptIdList->GetPointer( 0 ),
sizeof( vtkIdType ), numInsrt, diskFile );
}
#endif
// -------------------------------------------------------------------//
} // end of two tolerances and three functions
// -----------------------------------------------------------------------
// ------------------ direct check-free point insertion ------------------
// -----------------------------------------------------------------------
insrtPts->Reset();
octLocat->FreeSearchStructure();
octLocat->SetMaxPointsPerLeaf( octreRes[r] );
octLocat->InitPointInsertion
( insrtPts, dataPnts->GetBounds(), numbPnts );
for ( i = 0; i < numbPnts; i ++ )
{
octLocat->InsertPointWithoutChecking
( pDataPts + ( i << 1 ) + i, pointIdx, 1 );
}
retValue = ( insrtPts->GetNumberOfPoints() == numbPnts ) ? 0 : 1;
} // end of three resolutions
// =========================================================================
// direct check-free insertion of a huge number of EXACTLY DUPLICATE points
// (number > the maximum number of points per leaf node)
// =========================================================================
if ( retValue == 0 )
{
// perform direct / check-free point insertion
for ( r = 0; ( r < 3 ) && ( retValue == 0 ); r ++ ) // three resolutions
{
insrtPts->Reset();
octLocat->FreeSearchStructure();
octLocat->SetMaxPointsPerLeaf( octreRes[r] );
octLocat->InitPointInsertion
( insrtPts, dataPnts->GetBounds(), numExPts );
for ( i = 0; i < numExPts; i ++ )
{
octLocat->InsertPointWithoutChecking
( xtentPts + ( i << 1 ) + i, pointIdx, 1 );
}
retValue = ( insrtPts->GetNumberOfPoints() == numExPts ) ? 0 : 1;
}
}
if ( xtentPts ) free( xtentPts ); xtentPts = NULL;
// =======================================================================//
// =======================================================================//
// reclaim this vtkPoints as it will be never used again
if ( insrtPts ) insrtPts->Delete(); insrtPts = NULL;
// =========================================================================
// ============================ Point Location ============================
// =========================================================================
// load points and radius data from a disk file for point location tasks
fileName = vtkTestUtilities::ExpandDataFileName
( argc, argv, "Data/IncOctPntLocData.dat" );
pntsFile = fopen( fileName, "rb" );
delete [] fileName; fileName = NULL;
fread( &nLocPnts, sizeof( int ), 1, pntsFile );
#ifdef VTK_WORDS_BIGENDIAN
SwapForBigEndian
( ( unsigned char * ) ( &nLocPnts ), sizeof( int ), 1 );
#endif
pLocPnts = ( double * ) realloc( pLocPnts, sizeof( double ) * nLocPnts * 3 );
minDist2 = ( double * ) realloc( minDist2, sizeof( double ) * nLocPnts );
maxDist2 = ( double * ) realloc( maxDist2, sizeof( double ) * nLocPnts );
fread( pLocPnts, sizeof( double ), nLocPnts * 3, pntsFile );
//fread( minDist2, sizeof( double ), nLocPnts, pntsFile );
//fread( maxDist2, sizeof( double ), nLocPnts, pntsFile );
#ifdef VTK_WORDS_BIGENDIAN
SwapForBigEndian
( ( unsigned char * ) pLocPnts, sizeof( double ), nLocPnts * 3 );
//SwapForBigEndian
// ( ( unsigned char * ) minDist2, sizeof( double ), nLocPnts );
//SwapForBigEndian
// ( ( unsigned char * ) maxDist2, sizeof( double ), nLocPnts );
#endif
fclose( pntsFile ); pntsFile = NULL;
// destroy the context of point insertion while attaching the dataset
octLocat->FreeSearchStructure();
octLocat->SetDataSet( unstruct );
// memory allocation
clzNdst2 = ( double * ) realloc( clzNdst2, sizeof( double ) * nClzNpts );
for ( i = 0; i < 3; i ++ )
{
idxLists[i] = vtkIdList::New();
idxLists[i]->Allocate( nClzNpts, nClzNpts );
}
// the main component
for ( r = 0; ( r < 3 ) && ( retValue == 0 ); r ++ ) // three resolutions
{
// establish a new octree with the specified resolution
octLocat->Modified();
octLocat->SetMaxPointsPerLeaf( octreRes[r] );
octLocat->BuildLocator();
// memory allocation
resltIds = ( vtkIdType * )
realloc( resltIds, sizeof( vtkIdType ) * nLocPnts );
#ifndef _BRUTE_FORCE_VERIFICATION_
truthIds = ( vtkIdType * )
realloc( truthIds, sizeof( vtkIdType ) * nLocPnts );
#endif
// -----------------------------------------------------------------------
// -------------------- location of the closest point --------------------
// -----------------------------------------------------------------------
// find the closest point
for ( i = 0; i < nLocPnts; i ++ )
{
resltIds[i] = octLocat->FindClosestPoint( pLocPnts + ( i << 1 ) + i,
minDist2 + i );
}
#ifdef _BRUTE_FORCE_VERIFICATION_
// -----------------------------------------------------------------------
// verify the result in brute force mode
for ( j = 0; ( j < nLocPnts ) && ( retValue == 0 ); j ++ )
for ( i = 0; ( i < numbPnts ) && ( retValue == 0 ); i ++ )
{
if ( i == resltIds[j] ) continue; // just the selected closest point
tmpDist2 = vtkMath::Distance2BetweenPoints
( pLocPnts + ( j << 1 ) + j, pDataPts + ( i << 1 ) + i );
if ( tmpDist2 + INC_OCT_PNT_LOC_TESTS_ZERO < minDist2[j] ) retValue = 1;
}
// ---------------------------------------------------------------------//
#else
// -----------------------------------------------------------------------
// rapid point index-based verification
fread( &nLocPnts, sizeof( int ), 1, diskFile );
#ifdef VTK_WORDS_BIGENDIAN
SwapForBigEndian
( ( unsigned char * ) ( &nLocPnts ), sizeof( int ), 1 );
#endif
fread( truthIds, sizeof( vtkIdType ), nLocPnts, diskFile );
#ifdef VTK_WORDS_BIGENDIAN
SwapForBigEndian
( ( unsigned char * ) truthIds, sizeof( vtkIdType ), nLocPnts );
#endif
for ( i = 0; ( i < nLocPnts ) && ( retValue == 0 ); i ++ )
{
retValue = ( resltIds[i] == truthIds[i] ) ? 0 : 1;
}
// ---------------------------------------------------------------------//
#endif
// -----------------------------------------------------------------------
// write the point indices as the ground truth
#ifdef _BRUTE_FORCE_VERIFICATION_WRITE_RESULT_
if ( retValue == 0 )
{
fwrite( &nLocPnts, sizeof( int ), 1, diskFile );
fwrite( resltIds, sizeof( vtkIdType ), nLocPnts, diskFile );
}
#endif
// ---------------------------------------------------------------------//
if ( retValue == 1 ) continue;
// -----------------------------------------------------------------------
// ------------------ location of the closest N points -------------------
// -----------------------------------------------------------------------
// memory allocation
ptIdList->SetNumberOfIds( nClzNpts * 10 ); // to claim part of the memory
resltIds = ( vtkIdType * )
realloc( resltIds, sizeof( vtkIdType ) * nClzNpts * nLocPnts );
#ifndef _BRUTE_FORCE_VERIFICATION_
truthIds = ( vtkIdType * )
realloc( truthIds, sizeof( vtkIdType ) * nClzNpts * nLocPnts );
fread( &numInsrt, sizeof( int ), 1, diskFile );
#ifdef VTK_WORDS_BIGENDIAN
SwapForBigEndian
( ( unsigned char * ) ( &numInsrt ), sizeof( int ), 1 );
#endif
fread( truthIds, sizeof( vtkIdType ), numInsrt, diskFile );
#ifdef VTK_WORDS_BIGENDIAN
SwapForBigEndian
( ( unsigned char * ) truthIds, sizeof( vtkIdType ), numInsrt );
#endif
#endif
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
// find the closest N points (with embedded brute-force verification)
for ( i = 0; i < nLocPnts; i ++ )
{
ptIdList->Reset();
octLocat->FindClosestNPoints
( nClzNpts, pLocPnts + ( i << 1 ) + i, ptIdList );
// ---------------------------------------------------------------------
// verify the result in brute force mode
#ifdef _BRUTE_FORCE_VERIFICATION_
// check the order of the closest points
for ( j = 0; j < nClzNpts; j ++ )
{
pointIdx = ptIdList->GetId( j );
clzNdst2[j] = vtkMath::Distance2BetweenPoints
( pLocPnts + ( i << 1 ) + i,
pDataPts + ( pointIdx << 1 ) + pointIdx );
}
for ( j = 0; ( j < nClzNpts - 1 ) && ( retValue == 0 ); j ++ )
{
retValue = ( clzNdst2[j + 1] >= clzNdst2[j] ) ? 0 : 1;
}
// write some data for locating the closest point
// within a radius and the points within a radius
//#ifdef _BRUTE_FORCE_VERIFICATION_WRITE_DISTANCE2_
minDist2[i] = clzNdst2[0];
maxDist2[i] = clzNdst2[ nClzNpts - 1 ];
//#endif
// check if there are any ignored but closer points
for ( j = 0; ( j < numbPnts ) && ( retValue == 0 ); j ++ )
{
tmpDist2 = vtkMath::Distance2BetweenPoints
( pLocPnts + ( i << 1 ) + i, pDataPts + ( j << 1 ) + j );
if ( tmpDist2 + INC_OCT_PNT_LOC_TESTS_ZERO // for cygwin and minGW
< clzNdst2[ nClzNpts - 1 ] // Not "<=" here as there
&& ptIdList->IsId( j ) == -1 // may be other points that were
) retValue = 1; // rejected simply due to the limit of N
}
if ( retValue == 1 ) break;
#endif
// -------------------------------------------------------------------//
// transfer the point indices for subsequent file writing purposes
memcpy( resltIds + i * nClzNpts, ptIdList->GetPointer( 0 ),
sizeof( vtkIdType ) * nClzNpts );
}
// ---------------------------------------------------------------------//
// ---------------------------------------------------------------------//
// -----------------------------------------------------------------------
// write the point indices as the ground truth
#ifdef _BRUTE_FORCE_VERIFICATION_WRITE_RESULT_
if ( retValue == 0 )
{
numInsrt = nClzNpts * nLocPnts;
fwrite( &numInsrt, sizeof( int ), 1, diskFile );
fwrite( resltIds, sizeof( vtkIdType ), numInsrt, diskFile );
}
#endif
// ---------------------------------------------------------------------//
// -----------------------------------------------------------------------
// rapid point index-based verification
#ifndef _BRUTE_FORCE_VERIFICATION_
numInsrt = nClzNpts * nLocPnts; // data has been read at the beginning
for ( i = 0; ( i < numInsrt ) && ( retValue == 0 ); i ++ )
{
retValue = ( resltIds[i] == truthIds[i] ) ? 0 : 1;
}
#endif
// ---------------------------------------------------------------------//
if ( retValue == 1 ) continue;
// -----------------------------------------------------------------------
// ------------ location of the closest point within a radius ------------
// -----------------------------------------------------------------------
// memory allocation
resltIds = ( vtkIdType * )
realloc( resltIds, sizeof( vtkIdType ) * nLocPnts * 3 );
// find the closest point within three radii
for ( i = 0; i < nLocPnts; i ++ )
{
j = ( i << 1 ) + i;
pointIdx = octLocat->FindClosestPoint( pLocPnts + j, minDist2 + i );
// there are some points falling on the in-octree points
fTempRad = ( minDist2[i] <= INC_OCT_PNT_LOC_TESTS_ZERO )
? 0.000001 : minDist2[i];
fTempRad = sqrt( fTempRad ); // causes inaccuracy if minDist2 is nonzero
resltIds[ j ] = octLocat->FindClosestPointWithinRadius
( fTempRad * 0.5, pLocPnts + j, tmpDist2 );
if ( minDist2[i] <= INC_OCT_PNT_LOC_TESTS_ZERO )
resltIds[ j + 1 ] = octLocat->FindClosestPointWithinRadius
( fTempRad * 1.0, pLocPnts + j, tmpDist2 );
else // for non-zero cases, use the original squared radius for accuracy
resltIds[ j + 1 ] = octLocat->FindClosestPointWithinSquaredRadius
( minDist2[i], pLocPnts + j, tmpDist2 );
resltIds[ j + 2 ] = octLocat->FindClosestPointWithinRadius
( fTempRad * 1.5, pLocPnts + j, tmpDist2 );
// -----------------------------------------------------------------------
// verify the result in brute force mode
#ifdef _BRUTE_FORCE_VERIFICATION_
if ( ( ( minDist2[i] > INC_OCT_PNT_LOC_TESTS_ZERO )
&& ( resltIds[j] != -1 )
)
|| ( ( minDist2[i] <= INC_OCT_PNT_LOC_TESTS_ZERO )
&& ( resltIds[j] != pointIdx )
)
|| ( resltIds[ j + 1 ] != pointIdx )
|| ( resltIds[ j + 2 ] != pointIdx )
) { retValue = 1; break; }
// ---------------------------------------------------------------------//
#endif
}
// -----------------------------------------------------------------------
// write the point indices as the ground truth
#ifdef _BRUTE_FORCE_VERIFICATION_WRITE_RESULT_
if ( retValue == 0 )
{
numInsrt = nLocPnts * 3; // as we test three radii
fwrite( &numInsrt, sizeof( int ), 1, diskFile );
fwrite( resltIds, sizeof( vtkIdType ), numInsrt, diskFile );
}
#endif
// ---------------------------------------------------------------------//
// -----------------------------------------------------------------------
// rapid point index-based verification
#ifndef _BRUTE_FORCE_VERIFICATION_
fread( &numInsrt, sizeof( int ), 1, diskFile );
#ifdef VTK_WORDS_BIGENDIAN
SwapForBigEndian
( ( unsigned char * ) ( &numInsrt ), sizeof( int ), 1 );
#endif
truthIds = ( vtkIdType * )
realloc( truthIds, sizeof( vtkIdType ) * numInsrt );
fread( truthIds, sizeof( vtkIdType ), numInsrt, diskFile );
#ifdef VTK_WORDS_BIGENDIAN
SwapForBigEndian
( ( unsigned char * ) truthIds, sizeof( vtkIdType ), numInsrt );
#endif
for ( i = 0; ( i < numInsrt ) && ( retValue == 0 ); i ++ )
{
retValue = ( resltIds[i] == truthIds[i] ) ? 0 : 1;
}
#endif
// ---------------------------------------------------------------------//
if ( retValue == 1 ) continue;
// -----------------------------------------------------------------------
// --------------- location of the points within a radius ----------------
// -----------------------------------------------------------------------
ptIdList->Reset();
for ( i = 0; i < nLocPnts; i ++ )
{
// set the coordinate index of the point under check
j = ( i << 1 ) + i;
minDist2[i] += INC_OCT_PNT_LOC_TESTS_ZERO; // for cygwin and minGW
// obtain the points within three radii (use squared radii for test
// as sqrt can incur inaccuracy that complicates the test)
idxLists[0]->Reset();
idxLists[1]->Reset();
idxLists[2]->Reset();
if ( minDist2[i] <= INC_OCT_PNT_LOC_TESTS_ZERO )
{
// each ( maxDist2[i] * 0.3 ) has been guranteed to be >
// INC_OCT_PNT_LOC_TESTS_ZERO
octLocat->FindPointsWithinSquaredRadius
( maxDist2[i] * 0.3, pLocPnts + j, idxLists[0] );
octLocat->FindPointsWithinSquaredRadius
( maxDist2[i] * 0.6, pLocPnts + j, idxLists[1] );
octLocat->FindPointsWithinSquaredRadius
( maxDist2[i], pLocPnts + j, idxLists[2] );
}
else
{
octLocat->FindPointsWithinSquaredRadius
( minDist2[i] * 0.5, pLocPnts + j, idxLists[0] );
octLocat->FindPointsWithinSquaredRadius
( minDist2[i], pLocPnts + j, idxLists[1] );
octLocat->FindPointsWithinSquaredRadius
( maxDist2[i], pLocPnts + j, idxLists[2] );
if ( idxLists[0]->GetNumberOfIds() == 0 )
idxLists[0]->InsertNextId( -1 ); // to handle an empty id list
}
// copy the point indices to a vtkIdList for comparison and file writing
for ( m = 0; m < 3; m ++ )
{
numInsrt = idxLists[m]->GetNumberOfIds();
for ( k = 0; k < numInsrt; k ++ )
{
ptIdList->InsertNextId( idxLists[m]->GetId( k ) );
}
}
// ---------------------------------------------------------------------
// verify the result in brute force mode
#ifdef _BRUTE_FORCE_VERIFICATION_
// check if the monotonical property holds among the 3 point-index lists
if ( minDist2[i] <= INC_OCT_PNT_LOC_TESTS_ZERO )
{
pointIdx = octLocat->FindClosestPoint( pLocPnts + j );
if ( idxLists[0]->IsId( pointIdx ) == -1 ||
idxLists[1]->IsId( pointIdx ) == -1 ||
idxLists[2]->IsId( pointIdx ) == -1 ||
idxLists[1]->GetNumberOfIds() < idxLists[0]->GetNumberOfIds() ||
idxLists[2]->GetNumberOfIds() < idxLists[0]->GetNumberOfIds() ||
idxLists[2]->GetNumberOfIds() < idxLists[1]->GetNumberOfIds()
) retValue = 1;
}
else
{
if ( idxLists[0]->GetNumberOfIds() != 1 ||
idxLists[0]->GetId( 0 ) != -1 ||
idxLists[1]->GetNumberOfIds() < 1 ||
idxLists[2]->GetNumberOfIds() < idxLists[1]->GetNumberOfIds()
) retValue = 1;
}
// check the points within each of the three radii
for ( m = 0; ( m < 3 ) && ( retValue == 0 ); m ++ )
{
// get the squared radius actually used
if ( minDist2[i] <= INC_OCT_PNT_LOC_TESTS_ZERO )
{
if ( m == 0 ) fTempRad = maxDist2[i] * 0.3;
else
if ( m == 1 ) fTempRad = maxDist2[i] * 0.6;
else fTempRad = maxDist2[i];
}
else
{
if ( m == 0 ) fTempRad = minDist2[i] * 0.5;
else
if ( m == 1 ) fTempRad = minDist2[i];
else fTempRad = maxDist2[i];
}
// check if there is any false insertion
numInsrt = idxLists[m]->GetNumberOfIds();
for ( k = 0; ( k < numInsrt ) && ( retValue == 0 ); k ++ )
{
if ( m == 0 && idxLists[0]->GetId( 0 ) == -1 ) break;
pointIdx = idxLists[m]->GetId( k );
pointIdx = ( pointIdx << 1 ) + pointIdx;
tmpDist2 = vtkMath::Distance2BetweenPoints
( pLocPnts + j, pDataPts + pointIdx );
if ( tmpDist2 > fTempRad + INC_OCT_PNT_LOC_TESTS_ZERO ) retValue = 1;
}
// check if there is any missed insertion
numInsrt = 0;
for ( k = 0; k < numbPnts; k ++ )
{
tmpDist2 = vtkMath::Distance2BetweenPoints
( pLocPnts + j, pDataPts + ( k << 1 ) + k );
if ( tmpDist2 + INC_OCT_PNT_LOC_TESTS_ZERO <= fTempRad ) numInsrt ++;
}
// get the actual size of the vtkIdList for comparison
int listSize = ( m == 0 && idxLists[0]->GetId( 0 ) == -1 )
? 0 // for an actually NULL vtkIdList
: idxLists[m]->GetNumberOfIds();
if ( numInsrt > listSize ) retValue = 1;
}
#endif
// -------------------------------------------------------------------//
}
// -----------------------------------------------------------------------
// write the point indices as the ground truth
#ifdef _BRUTE_FORCE_VERIFICATION_WRITE_RESULT_
if ( retValue == 0 )
{
numInsrt = ptIdList->GetNumberOfIds();
fwrite( &numInsrt, sizeof( int ), 1, diskFile );
fwrite( ptIdList->GetPointer( 0 ),
sizeof( vtkIdType ), numInsrt, diskFile );
}
#endif
// ---------------------------------------------------------------------//
// -----------------------------------------------------------------------
// rapid point index-based verification
#ifndef _BRUTE_FORCE_VERIFICATION_
fread( &numInsrt, sizeof( int ), 1, diskFile );
#ifdef VTK_WORDS_BIGENDIAN
SwapForBigEndian
( ( unsigned char * ) ( &numInsrt ), sizeof( int ), 1 );
#endif
truthIds = ( vtkIdType * )
realloc( truthIds, sizeof( vtkIdType ) * numInsrt );
fread( truthIds, sizeof( vtkIdType ), numInsrt, diskFile );
#ifdef VTK_WORDS_BIGENDIAN
SwapForBigEndian
( ( unsigned char * ) truthIds, sizeof( vtkIdType ), numInsrt );
#endif
vtkIdType * tmpPtIds = ptIdList->GetPointer( 0 );
for ( i = 0; ( i < numInsrt ) && ( retValue == 0 ); i ++ )
{
retValue = ( tmpPtIds[i] == truthIds[i] ) ? 0 : 1;
}
tmpPtIds = NULL;
#endif
// ---------------------------------------------------------------------//
}
// =========================================================================
// ================== DO NOT TURN ON THIS SEGMENT OF CODE ==================
// =========================================================================
#ifdef _BRUTE_FORCE_VERIFICATION_WRITE_DISTANCE2_
fileName = vtkTestUtilities::ExpandDataFileName
( argc, argv, "Data/IncOctPntLocData.dat" );
pntsFile = fopen( fileName, "wb" );
delete [] fileName; fileName = NULL;
fwrite(&nLocPnts, sizeof( int ), 1, pntsFile );
fwrite( pLocPnts, sizeof( double ), nLocPnts * 3, pntsFile );
fwrite( minDist2, sizeof( double ), nLocPnts, pntsFile );
fwrite( maxDist2, sizeof( double ), nLocPnts, pntsFile );
fclose( pntsFile ); pntsFile = NULL;
#endif
// =======================================================================//
// =======================================================================//
// =========================================================================
// ================== DO NOT TURN ON THIS SEGMENT OF CODE ==================
// =========================================================================
// NOTE: do *NOT* turn on this segment of code as the points resulting from
// the following random generator would change the points disk file!!!
// The points generated below are used to challege point location.
// In case this segment is executed, it needs to be turned off and
// re-run the test with _BRUTE_FORCE_VERIFICATION_ on to create new
// disk files.
#if 0
int duplPnts = 200;
int inerPnts = 500;
int outrPnts = 300;
int totalPts = duplPnts + inerPnts + outrPnts;
int coordIdx = 0;
int gridIndx = 0;
double * ptCoords = ( double * ) malloc( sizeof( double ) * 3 * totalPts );
// duplicating grid points
for ( i = 0; i < duplPnts; i ++, coordIdx ++ )
{
gridIndx = rand() % numbPnts;
ptCoords[ ( coordIdx << 1 ) + coordIdx + 0 ] =
pDataPts[ ( gridIndx << 1 ) + gridIndx + 0 ];
ptCoords[ ( coordIdx << 1 ) + coordIdx + 1 ] =
pDataPts[ ( gridIndx << 1 ) + gridIndx + 1 ];
ptCoords[ ( coordIdx << 1 ) + coordIdx + 2 ] =
pDataPts[ ( gridIndx << 1 ) + gridIndx + 2 ];
}
// non-duplicate within-data points
// some randomly selected grid points are jittered, in each axis,
// between [ -0.1, 0.1 ] with resolution 0.001
for ( i = 0; i < inerPnts; i ++, coordIdx ++ )
{
gridIndx = ( rand() % numbPnts ) + ( numbPnts >> 1 );
gridIndx = gridIndx % numbPnts;
ptCoords[ ( coordIdx << 1 ) + coordIdx + 0 ] =
pDataPts[ ( gridIndx << 1 ) + gridIndx + 0 ] +
0.001 * ( ( ( rand() % 2 ) << 1 ) - 1 ) * ( rand() % 101 );
ptCoords[ ( coordIdx << 1 ) + coordIdx + 1 ] =
pDataPts[ ( gridIndx << 1 ) + gridIndx + 1 ] +
0.001 * ( ( ( rand() % 2 ) << 1 ) - 1 ) * ( rand() % 101 );
ptCoords[ ( coordIdx << 1 ) + coordIdx + 2 ] =
pDataPts[ ( gridIndx << 1 ) + gridIndx + 2 ] +
0.001 * ( ( ( rand() % 2 ) << 1 ) - 1 ) * ( rand() % 101 );
}
// outer points: x --- [ -5.0, -3.0 ] or [ 3.0, 5.0 ]
// AND
// y --- [ -5.0, -3.0 ] or [ 3.0, 5.0 ]
// AND
// z --- [ -5.0, -3.0 ] or [ 3.0, 5.0 ]
for ( i = 0; i < outrPnts; i ++, coordIdx ++ )
{
ptCoords[ ( coordIdx << 1 ) + coordIdx + 0 ] =
3.0 + 0.01 * ( rand() % 201 );
ptCoords[ ( coordIdx << 1 ) + coordIdx + 0 ] *=
( ( rand() % 2 ) << 1 ) - 1;
ptCoords[ ( coordIdx << 1 ) + coordIdx + 1 ] =
3.0 + 0.01 * ( rand() % 201 );
ptCoords[ ( coordIdx << 1 ) + coordIdx + 1 ] *=
( ( rand() % 2 ) << 1 ) - 1;
ptCoords[ ( coordIdx << 1 ) + coordIdx + 2 ] =
3.0 + 0.01 * ( rand() % 201 );
ptCoords[ ( coordIdx << 1 ) + coordIdx + 2 ] *=
( ( rand() % 2 ) << 1 ) - 1;
}
// write the points to a disk file
fileName = vtkTestUtilities::ExpandDataFileName
( argc, argv, "Data/IncOctPntLocData.dat" );
pntsFile = fopen( fileName, "wb" );
delete [] fileName; fileName = NULL;
fwrite( &totalPts, sizeof( int ), 1, pntsFile );
fwrite( ptCoords, sizeof( double ), totalPts * 3, pntsFile );
fclose( pntsFile ); pntsFile = NULL;
free( ptCoords ); ptCoords = NULL;
#endif
// =======================================================================//
// =======================================================================//
// memory clearance
dataPnts = NULL; unstruct = NULL;
if ( ptIdList ) ptIdList->Delete(); ptIdList = NULL;
if ( octLocat ) octLocat->Delete(); octLocat = NULL;
if ( ugReader ) ugReader->Delete(); ugReader = NULL;
if ( truthIds ) free( truthIds ); truthIds = NULL;
if ( resltIds ) free( resltIds ); resltIds = NULL;
if ( pDataPts ) free( pDataPts ); pDataPts = NULL;
if ( pLocPnts ) free( pLocPnts ); pLocPnts = NULL;
if ( minDist2 ) free( minDist2 ); minDist2 = NULL;
if ( maxDist2 ) free( maxDist2 ); maxDist2 = NULL;
if ( clzNdst2 ) free( clzNdst2 ); clzNdst2 = NULL;
if ( diskFile ) fclose( diskFile ); diskFile = NULL;
for ( i = 0; i < 3; i ++ )
{
if ( idxLists[i] ) idxLists[i]->Delete(); idxLists[i] = NULL;
}
return retValue;
}
|