//Checks if copy Works bool pointBasicCopyTest() { double data[2] = { 1.0, 1.0 }; int dim = 2; int index = 1; SPPoint p = spPointCreate(data, dim, index); SPPoint q = spPointCopy(p); ASSERT_TRUE(spPointGetIndex(p) == spPointGetIndex(q)); ASSERT_TRUE(spPointGetDimension(p) == spPointGetDimension(q)); int i; for (i = 0; i < spPointGetDimension(p); i++) { ASSERT_TRUE(spPointGetAxisCoor(p, i) == spPointGetAxisCoor(q, i)); } spPointDestroy(p); spPointDestroy(q); return true; }
bool pointGettersTest() { double data1[3] = { 1.0, 1.0, 2.0 }; int dim1 = 3; int index1 = 1; SPPoint p = spPointCreate((double *)data1, dim1, index1); ASSERT_TRUE(spPointGetAxisCoor(p, 0) == 1.0); ASSERT_TRUE(spPointGetAxisCoor(p, 1) == 1.0); ASSERT_FALSE(spPointGetAxisCoor(p, 2) == 1.0); ASSERT_TRUE(spPointGetAxisCoor(p, 2) == 2.0); ASSERT_TRUE(spPointGetIndex(p) == 1); ASSERT_TRUE(spPointGetDimension(p) == 3); spPointDestroy(p); return true; }
SP_CONFIG_MSG writeImageFeaturesToFile(SPPoint* imFeatures, int numOfFeats, SPConfig config, int imageIndex) { FILE* featsFile; SP_CONFIG_MSG msg = getFeatsFile(config, imageIndex, &featsFile, "w+"); int i, j; if (msg != SP_CONFIG_SUCCESS) { return msg; } fprintf(featsFile, "%d\n", numOfFeats); for (i = 0; i < numOfFeats; i++) { SPPoint point = imFeatures[i]; fprintf(featsFile, "%d,%d\n", spPointGetIndex(point), spPointGetDimension(point)); for (j = 0; j < spPointGetDimension(point); j++) { fprintf(featsFile, "%f\n", spPointGetAxisCoor(point, j)); } } fflush(featsFile); fclose(featsFile); return SP_CONFIG_SUCCESS; }
/** * prints the tree for debuging */ void printTree(KDTreeNode kdTree) { int i; if (kdTree == NULL ) { return; } printf(" ("); printTree(kdTree->left); if (kdTree->data == NULL ) printf("dim: %d, val: %f", kdTree->dim, kdTree->val); else { printf("index: %d,point: ", spPointGetIndex(kdTree->data)); for (i = 0; i < spPointGetDimension(kdTree->data); i++) { printf("%lf,", spPointGetAxisCoor(kdTree->data, i)); } } printTree(kdTree->right); printf(" )"); }
SPKDArray Init(SPPoint* arr, int size){ SPKDArray KDArray; int d; // d = the dimension of the points (assuming dimension is the same for all points) double** index_val_arr; //double array containing n rows. each row contains the index and the value of a specific coordinate in the point of that index int i,j; int malloc_result; // holds the result of the call to mallocForNewKdArray //check validation of the parameters if (size<1){ spLoggerPrintError(INVALID_ARG_ERROR, __FILE__, __func__, __LINE__); spLoggerPrintDebug(PARAMETER_SIZE_INVALID, __FILE__, __func__, __LINE__); return NULL; } if (arr==NULL){ spLoggerPrintError(INVALID_ARG_ERROR, __FILE__, __func__, __LINE__); spLoggerPrintDebug(PARAMETER_ARR_INVALID, __FILE__, __func__, __LINE__); return NULL; } //initialize d d = spPointGetDimension(arr[0]); // allocate memory for KDArray and its fields malloc_result = mallocForNewKdArray(&KDArray, size, d); if (malloc_result==-1){ // the memory freeing would already be done inside mallocForInitKdArray return NULL; } //copy each point from arr to KDArray->array_of_points for (i=0; i<size; i++){ KDArray->array_of_points[i] = spPointCopy(arr[i]); //spPointCopy returns NULL if an allocation error occurred if (KDArray->array_of_points[i] == NULL){ spLoggerPrintError(GENERAL_ERROR_MSG, __FILE__, __func__, __LINE__); spLoggerPrintDebug(SPPOINTCOPY_RETURNED_NULL, __FILE__, __func__, __LINE__); destroyKDArray(KDArray); return NULL; } } //allocate memory for index_val_arr: n rows, 2 columns if ( (index_val_arr = (double**)malloc(size*sizeof(double*))) == NULL){ spLoggerPrintError(ALLOC_ERROR_MSG, __FILE__, __func__, __LINE__); destroyKDArray(KDArray); return NULL; } for (i=0; i<size; i++){ if( (index_val_arr[i]=(double*)malloc(2*sizeof(double))) == NULL){ spLoggerPrintError(ALLOC_ERROR_MSG, __FILE__, __func__, __LINE__); for (j=0; j<i; j++){ free(index_val_arr[j]); } free(index_val_arr); destroyKDArray(KDArray); return NULL; } } // for each coordinate for (i=0; i<d; i++){ // i=coordinate // fill index and value of coordinate to val_index_arr for (j=0; j<size; j++){ //j=index of point index_val_arr[j][0] = (double)j; index_val_arr[j][1] = spPointGetAxisCoor(arr[j],i); } /* sort the rows in index_val_arr by the value in the second column, * meaning by the value of the coordinate of each point. * the first column will be the indexes of the points sorted by the values of the coordinate */ qsort(index_val_arr, size, sizeof(index_val_arr[0]), copmareByValue); // fill the sorted indexes in to KDArray->Array for (j=0; j<size; j++){ //j=index of point KDArray->matrix_of_sorted_indexes[i][j] = (int)index_val_arr[j][0]; } } // initialize n and d KDArray->n = size; KDArray->d = d; for (j=0; j<size; j++){ free(index_val_arr[j]); } free(index_val_arr); return KDArray; }
/**** * The purpose of the test is to compare the features before and after the saving. */ bool main_saveLoad() { LoadedFeatures loadedFeatures = NULL; LoadedFeatures features = NULL; int numOfImages = 0; int numOfFeatures = 0; int numOfCoords = 0; SPPoint * orgArr = NULL; SPPoint pointOrg = NULL; SPPoint * loadedArr = NULL; SPPoint pointLoad = NULL; SP_CONFIG_MSG msg; char fileName[30] = "./tests/config/saveLoad.cfg"; SPConfig conf = spConfigCreate(fileName, &msg); ASSERT_TRUE(NULL != conf); /**** * Allocates, creates(by imageProc) and saves the features ****/ features = allocateFeaturesList(conf); ASSERT_TRUE(NULL != features); ASSERT_TRUE(true == debugSaveFeatures(conf, features)); /**** * Load the features from the files *****/ loadedFeatures = allocateFeaturesList(conf); ASSERT_TRUE(NULL != loadedFeatures); ASSERT_TRUE(true == loadFeatures(conf, loadedFeatures)); /**************************************** * Compare original and loaded features * ****************************************/ ASSERT_TRUE(getFeaturesImagesNumber(features) == getFeaturesImagesNumber(loadedFeatures)); numOfImages = getFeaturesImagesNumber(features); for (int index_image=0; index_image<numOfImages ; index_image++) { numOfFeatures = getFeaturesNumber(features, index_image); ASSERT_TRUE(numOfFeatures == getFeaturesNumber(loadedFeatures, index_image)); for (int index_feature=0; index_feature<numOfFeatures ; index_feature++) { orgArr = getFeatureArray(features, index_image); loadedArr = getFeatureArray(loadedFeatures, index_image); pointOrg = orgArr[index_feature]; pointLoad = loadedArr[index_feature]; numOfCoords = spPointGetDimension(pointOrg); ASSERT_TRUE(numOfCoords == spPointGetDimension(pointLoad)); ASSERT_TRUE(spPointGetIndex(pointOrg) == spPointGetIndex(pointLoad)); for (int index_coor=0; index_coor<numOfCoords ; index_coor++) { ASSERT_TRUE(spPointGetAxisCoor(pointOrg, index_coor) == spPointGetAxisCoor(pointLoad, index_coor)); } } } loadedFeaturesDestroy(loadedFeatures); loadedFeaturesDestroy(features); return true; }