⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test.cpp

📁 c++课程中要求的实现KNN的问题。解决了数据的产生问题
💻 CPP
字号:
#include <stdio.h>
#include <memory.h>
#include <crtdbg.h>
#include <iostream>
#include <stdlib.h>

#include "RTree.h"
using namespace std;
#define DIMENSION 3




// Use CRT Debug facility to dump memory leaks on app exit
#ifdef WIN32
  // The following macros set and clear, respectively, given bits
  // of the C runtime library debug flag, as specified by a bitmask.
  #ifdef   _DEBUG
    #define  SET_CRT_DEBUG_FIELD(a) \
              _CrtSetDbgFlag((a) | _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG))
    #define  CLEAR_CRT_DEBUG_FIELD(a) \
              _CrtSetDbgFlag(~(a) & _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG))
  #else
    #define  SET_CRT_DEBUG_FIELD(a)   ((void) 0)
    #define  CLEAR_CRT_DEBUG_FIELD(a) ((void) 0)
  #endif
#endif //WIN32


/// Simplify handling of 3 dimensional coordinate
struct Vec3
{
  /// Default constructor
  Vec3() {}

  /// Construct from three elements
  Vec3(float a_x, float a_y, float a_z)
  {
    v[0] = a_x;
    v[1] = a_y;
    v[2] = a_z;
  }

  /// Add two vectors and return result
  Vec3 operator+ (const Vec3& a_other) const
  {
    return Vec3(v[0] + a_other.v[0], 
                v[1] + a_other.v[1],
                v[2] + a_other.v[2]);
  }  

  Vec3 operator=(const Vec3& a_other)
  {
	  return Vec3(a_other.v[0],a_other.v[1],a_other.v[2]);
  }

  float v[3];  ///< 3 float components for axes or dimensions
};

/*static bool BoxesIntersect(const Vec3& a_boxMinA, const Vec3& a_boxMaxA, 
                           const Vec3& a_boxMinB, const Vec3& a_boxMaxB)
{
   for(int axis=0; axis<3; ++axis)
   {
    if(a_boxMinA.v[axis] > a_boxMaxB.v[axis] ||
       a_boxMaxA.v[axis] < a_boxMinB.v[axis])
    {
      return false;
    }
  }
  return true;
}*/

/// A user type to test with, instead of a simple type such as an 'int'
struct ObjectPoint
{
  ObjectPoint()
  {
    ++s_outstandingAllocs;
  }
  ~ObjectPoint()
  {
    --s_outstandingAllocs;
  }

  int m_creationCounter;                          ///< Just a number for identifying within test program
  float m_min[DIMENSION], m_max[DIMENSION];                              ///< Minimal bounding rect, values must be known and constant in order to remove from RTree,点的表识符而已

  static int s_outstandingAllocs;                 ///< Count how many outstanding objects remain,类的成员函数
};

/// Init static
int ObjectPoint::s_outstandingAllocs = 0;


/// A callback function to obtain query results in this implementation
bool _cdecl QueryResultCallback(ObjectPoint* a_data, void* a_context)
{
  printf("search found %d\n", a_data->m_creationCounter);
  return true;
}


int main(int argc,char *argv)
{
	int num_points=0;
    int demension=0;
	FILE *in_data;
	ObjectPoint* point;
	Vec3 vet;
	int i=0;
	int count=0;
	int accout_point=0;
	int num_count=0;
    
	//typedef the RTree useage just for conveniance with iteration
	typedef RTree<ObjectPoint*,float,3> PointTree;

	PointTree tree;
	in_data=fopen("data.txt","r");
	if(in_data==NULL)
	{
		printf("Can't open the file!\n");
		return 0;
	}

	fscanf(in_data,"%d %d\n",&num_points,&demension);

	while(!feof(in_data))//创建点,构建R_tree,
	{ 
	/*	printf("The count is %d",count);*/
		count++;
		fscanf(in_data,"%f %f %f\n",&vet.v[0],&vet.v[1],&vet.v[2]);
		point=new ObjectPoint;
		point->m_creationCounter=count;
        
		for(i=0;i<DIMENSION;i++)
		{
         point->m_min[i]=vet.v[i];
	   	 point->m_max[i]=vet.v[i];
		}
    	tree.Insert(point->m_min,point->m_max,point);
	//	printf("Inserting %d\n",point->s_outstandingAllocs);
	}

    tree.CountRec(tree.m_root,accout_point);
   	printf("tree count=%d\n",accout_point);
   //  printf("The levle of the root is %d\n",tree.m_root->m_level);//根的结构
   
	printf("%s","Input the value of the point:\n");
	printf("%s","NOTE THAT THE DIMENSION IS 3\n");
	scanf("%f %f %f",&vet.v[0],&vet.v[1],&vet.v[2]);
	printf("%s","Input the num of points you want to search\n");
	scanf("%d",&num_count);

	if(num_count>0)
	{
	  tree.find(vet.v,tree.m_root,num_count);
	}
  // Remove all contents (This would have happened automatically during destructor)
    tree.RemoveAll();
 
	return 0;

}



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -