Source

nrnr / test.c

Full commit
#include <stdio.h>
#include "nrnr.h"


#include <sys/time.h>
#include <time.h>



float GetTime(void);
void CheckSorting(struct NrNrHood* hood);
void CheckNextIndices(struct NrNrHood* hood);



#ifndef MIN
#define MIN(a,b) ((a)<(b) ? (a) : (b))
#endif



int main(int _argc, char** _argv) {

    float start;

    // Create random points

    start = GetTime();
    srand(12345);
    size_t numPoints = 5; //0000000;
    float* points = (float*)malloc(sizeof(float) * 3 * numPoints);
	for(size_t i=0; i<numPoints; i++) {
		float* p = points + i * 3;
		p[0] = ((float)rand() / RAND_MAX) * 200.0 - 100.0;
		p[1] = ((float)rand() / RAND_MAX) * 200.0 - 100.0;
		p[2] = ((float)rand() / RAND_MAX) * 200.0 - 100.0;

    }
	printf("Create random points (%zu): %.3f sec\n", numPoints, GetTime() - start);

    // Create neighborhood

    start = GetTime();
    struct NrNrHood* hood = NrNrCreateNeighborhood(numPoints, 3, points);
	printf("Create neighborhood: %.3f sec\n", GetTime() - start);


	printf("  x:"); for (size_t i=0; i<MIN(numPoints,10); ++i) {printf(" %zu=%.3f", hood->indices[i], points[hood->indices[i] * 3]);} printf("\n");
	printf("  y:"); for (size_t i=0; i<MIN(numPoints,10); ++i) {printf(" %zu=%.3f", hood->indices[numPoints + i], points[hood->indices[numPoints + i] * 3 + 1]);} printf("\n");
	printf("  z:"); for (size_t i=0; i<MIN(numPoints,10); ++i) {printf(" %zu=%.3f", hood->indices[numPoints * 2 + i], points[hood->indices[numPoints * 2 + i] * 3 + 2]);} printf("\n");
    CheckSorting(hood);

	printf(" nx:"); for (size_t i=0; i<MIN(numPoints,10); ++i) {printf(" %zu", hood->nextIndices[i]);} printf("\n");
	printf(" ny:"); for (size_t i=0; i<MIN(numPoints,10); ++i) {printf(" %zu", hood->nextIndices[numPoints + i]);} printf("\n");
	printf(" nz:"); for (size_t i=0; i<MIN(numPoints,10); ++i) {printf(" %zu", hood->nextIndices[numPoints * 2 + i]);} printf("\n");
    CheckNextIndices(hood);

    // Search block

    start = GetTime();
    float minPos[3] = {0.0f, 20.0f, -20.0f};
    float maxPos[3] = {minPos[0] + 40.0, minPos[1] + 40.0, minPos[2] + 40.0};
    size_t* results;
    size_t count = NrNrSearchBlock(hood, &results, minPos, maxPos);
    printf("Search block %zu: %.3f sec\n", count, GetTime() - start);

    // Cleanup

    start = GetTime();
    free(points);
    results = NrNrFreeResults(results);
    hood = NrNrFreeNeighborhood(hood);
    printf("Cleanup: %.3f sec\n", GetTime() - start);

	return 0;
}





float GetTime(void)
{
	static struct timeval timeval, first_timeval;
	gettimeofday(&timeval, 0);
	if(first_timeval.tv_sec == 0) {
		first_timeval = timeval;
		return 0;
	}
	unsigned int s = (timeval.tv_sec - first_timeval.tv_sec) * 1000 + (timeval.tv_usec - first_timeval.tv_usec) / 1000.0;
    return s / 1000.0f;
}




void CheckSorting(struct NrNrHood* hood) {
    // Check value ordering
    for (size_t d = 0; d < hood->dimensions; ++d) {
        size_t* indices = hood->indices + hood->numPoints * d;
        float val = hood->points[indices[0] * 3 + d];
        for (size_t pt = 1; pt < hood->numPoints; ++pt) {
            float nextVal = hood->points[indices[pt] * 3 + d];
            if (nextVal < val) {
                printf("Error sorting, values not in ascending order: axis=%zu, index=%zu\n", d, pt);
                exit(1);
            }
            val = nextVal;
        }
    }

    // Check repeated indices
    for (size_t d = 0; d < hood->dimensions; ++d) {
        size_t* indices = hood->indices + hood->numPoints * d;
        for (size_t i = 0; i < hood->numPoints; ++i) {
            size_t pt = indices[i];
            if (pt >= hood->numPoints) {
                printf("Error sorting, point number out of range: axis=%zu, index=%zu\n", d, i);
                exit(1);
            }
            for (size_t j = i + 1; j < hood->numPoints; ++j) {
                if (pt == indices[j]) {
                    printf("Error sorting, point number used more than once: axis=%zu, index=%zu\n", d, i);
                    exit(1);
                }
            }
        }
    }
}


void CheckNextIndices(struct NrNrHood* hood) {
    // Check repeated indices
    for (size_t d = 0; d < hood->dimensions; ++d) {
        size_t* next = hood->nextIndices + hood->numPoints * d;
        for (size_t i = 0; i < hood->numPoints; ++i) {
            size_t pt = next[i];
            if (pt >= hood->numPoints) {
                printf("Error with nexts, point number out of range: axis=%zu, index=%zu\n", d, i);
                exit(1);
            }
            for (size_t j = i + 1; j < hood->numPoints; ++j) {
                if (pt == next[j]) {
                    printf("Error with nexts, point number used more than once: axis=%zu, index=%zu\n", d, i);
                    exit(1);
                }
            }
        }
    }

    // Check roundtrip through dimensions ends with the same
    for (size_t d = 1; d < hood->dimensions; ++d) {
        size_t* next = hood->nextIndices + hood->numPoints * d;
        size_t* indices = hood->indices + hood->numPoints * d;
        size_t* from;
        if (d) {
            from = hood->indices + hood->numPoints * (d - 1);
        } else {
            from = hood->indices + hood->numPoints * (hood->dimensions - 1);
        }
        for (size_t i = 0; i < hood->numPoints; ++i) {
            size_t pt = from[i];
            if (pt != indices[next[i]]) {
                printf("Error with nexts, incorrect index: axis=%zu, index=%zu\n", d, i);
                exit(1);
            }
        }
    }

}