Exemple #1
0
int main(int argc, char* argv[])
{
	KDTree<Value, 2> kdtree = KDTree<Value, 2>::CreateKDTree(INSERT_NUM);

	std::vector<KDTree<Value, 2>::DataType> vData;
	for(uint32_t i=0; i<INSERT_NUM; ++i)
	{
		KDTree<Value, 2>::DataType item;
		item.Vector[0] = random() % 99;
		item.Vector[1] = random() % 99;
		item.Value.Uin = i;

		vData.push_back(item);
		//printf("new vector (%u, %u)\n", item.Vector[0], item.Vector[1]);
	}

	timeval t1, t2;
	gettimeofday(&t1, NULL);
	kdtree.Build(vData);
	gettimeofday(&t2, NULL);
	printf("const: %.03fs\n", TIME_COST(t1, t2));

	printf("Tree Dump:\n");
	kdtree.DumpTree();

	printf("\n");

	KDTree<Value, 2>::VectorType key;
	key[0] = (random() * time(NULL)) % 99;
	key[1] = (random() * time(NULL)) % 99;

	kdtree.Draw2DMap("map.jpg", key[0], key[1]);

#define SEARCH_COUNT	5

	uint32_t range = 0;

	KDTree<Value, 2>::DataType buffer[SEARCH_COUNT];
	int foundCount = kdtree.Nearest(key, buffer, SEARCH_COUNT);
	printf("nearest (%u, %u):\n", key[0], key[1]);
	for(int i=0; i<foundCount; ++i)
	{
		uint32_t distance = EuclideanDistance<uint32_t, KDVector<uint32_t, 2>, 2>::Distance(key, buffer[i].Vector);
		if(i == foundCount / 2)
			range = distance;
		printf("  %d:(%u, %u) distance:%u\n", i, buffer[i].Vector[0], buffer[i].Vector[1], distance);
	}

	printf("\n");

	foundCount = kdtree.Range(key, range, buffer, SEARCH_COUNT);
	printf("nearest (%u, %u) with range %u:\n", key[0], key[1], range);
	for(int i=0; i<foundCount; ++i)
	{
		uint32_t distance = EuclideanDistance<uint32_t, KDVector<uint32_t, 2>, 2>::Distance(key, buffer[i].Vector);
		printf("  %d:(%u, %u) distance:%u\n", i, buffer[i].Vector[0], buffer[i].Vector[1], distance);
	}

	printf("\n");

	kdtree.Delete();
	return 0;
}