<commit_before>/*=========================================================================

  Program:   Visualization Toolkit
  Module:    TestIncrementalOctreePointLocator.cxx
  Copyright (c) <NAME>, <NAME>, <NAME>
  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_


// 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;
  char *      fileName = NULL;
  FILE *      diskFile = NULL;
  FILE *      pntsFile = NULL;
  double      tmpDist2 = 0.0;
  double      fTempRad = 0.0;
  double *    pDataPts = 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;
  
  // xxx
  #if 1
  int         nUniques = 4;
  int         nDuplics = 300;
  int         numDupls = 0;
  int         arrayIdx = 0;
  double      pntCoord[3];
  double *    duplPnts = NULL;
  #endif
  
  
  // 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  );
    }
  
  
  // xxx  
  #if 1 
  arrayIdx    = numbPnts * 3;
  numDupls    = numbPnts + nUniques * nDuplics;
  pntCoord[0] = pntCoord[1] = pntCoord[2] = 0.0;
  // allocate memory and inherit the points
  #if 1
  duplPnts = new double[ numDupls * 3 ];  
  for ( i = 0; i < numbPnts * 3; i ++ ) duplPnts[i] = pDataPts[i]; 
  
  //duplPnts = ( double * )
  //           realloc(  duplPnts, sizeof( double ) * 3 * numDupls  );                                 
  //memcpy(  duplPnts,  pDataPts,  sizeof( double ) * 3 * numbPnts  );
  #endif
  #endif
  
  
  // xxx
  #if 0
  // add a huge number of exactly duplicate points
  for ( j = 0; j < nUniques; j ++ )
    {<commit_msg>BUG: Win32-VS60 does not allow for memory allocation for exactly duplicate points.<commit_after>/*=========================================================================

  Program:   Visualization Toolkit
  Module:    TestIncrementalOctreePointLocator.cxx
  Copyright (c) <NAME>, <NAME>, <NAME>
  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_


// 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;
  char *      fileName = NULL;
  FILE *      diskFile = NULL;
  FILE *      pntsFile = NULL;
  double      tmpDist2 = 0.0;
  double      fTempRad = 0.0;
  double *    pDataPts = 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;
  
  // xxx
  #if 1
  int         nUniques = 4;
  int         nDuplics = 300;
  int         numDupls = 0;
  int         arrayIdx = 0;
  double      pntCoord[3];
  double *    duplPnts = NULL;
  #endif
  
  
  // 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  );
    }
  
  
  // xxx  
  #if 1 
  arrayIdx    = numbPnts * 3;
  numDupls    = numbPnts + nUniques * nDuplics;
  pntCoord[0] = pntCoord[1] = pntCoord[2] = 0.0;
  // allocate memory and inherit the points
  #if 1
  duplPnts = new double[ numDupls * 3 ];  
  //for ( i = 0; i < numbPnts * 3; i ++ ) duplPnts[i] = pDataPts[i]; 
  
  //duplPnts = ( double * )
  //           realloc(  duplPnts, sizeof( double ) * 3 * numDupls  );                                 
  //memcpy(  duplPnts,  pDataPts,  sizeof( double ) * 3 * numbPnts  );
  #endif
  #endif
  
  
  // xxx
  #if 0
  // add a huge number of exactly duplicate points
  for ( j = 0; j < nUniques; j ++ )
    {
{'fraction_non_alphanumeric': 0.16808417360806663, 'fraction_numerical': 0.025602805786935554, 'mean_word_length': 2.123220153340635, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6538326', 'n_tokens_mistral': 4096, 'n_tokens_neox': 3815, 'n_words': 1023}