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

📄 ann2fig.cpp

📁 c++实现的KNN库:建立高维度的K-d tree,实现K邻域搜索
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//----------------------------------------------------------------------// File:			ann2fig.cpp// Programmer:		David Mount// Last modified:	05/03/05// Description:		convert ann dump file to fig file//----------------------------------------------------------------------// Copyright (c) 1997-2005 University of Maryland and Sunil Arya and// David Mount.  All Rights Reserved.// // This software and related documentation is part of the Approximate// Nearest Neighbor Library (ANN).  This software is provided under// the provisions of the Lesser GNU Public License (LGPL).  See the// file ../ReadMe.txt for further information.// // The University of Maryland (U.M.) and the authors make no// representations about the suitability or fitness of this software for// any purpose.  It is provided "as is" without express or implied// warranty.//----------------------------------------------------------------------// History://	Revision 0.1  03/04/98//		Initial release//	Revision 1.0  04/01/05//		Changed dump file suffix from .ann to .dmp.//	Revision 1.1  05/03/05//		Fixed usage output string.//----------------------------------------------------------------------//	This program inputs an ann dump file of a search structure//	perhaps along with point coordinates, and outputs a fig (Ver 3.1)//	file (see fig2dev (1)) displaying the tree.  The fig file may//	then be displayed using xfig, or converted to any of a number of//	other formats using fig2dev.////	If the dimension is 2 then the entire tree is display.  If the//	dimension is larger than 2 then the user has the option of//	selecting which two dimensions will be displayed, and the slice//	value for each of the remaining dimensions.  All leaf cells//	intersecting the slice are shown along with the points in these//	cells. See the procedure getArgs() below for the command-line//	arguments.//----------------------------------------------------------------------#include <cstdio>						// C standard I/O#include <fstream>						// file I/O#include <string>						// string manipulation#include <ANN/ANNx.h>					// all ANN includesusing namespace std;					// make std:: accessible//----------------------------------------------------------------------// Globals and their defaults//----------------------------------------------------------------------const int		STRING_LEN		= 500;	// string lengthsconst int		MAX_DIM			= 1000; // maximum dimensionconst double	DEF_SLICE_VAL	= 0;	// default slice valueconst char		FIG_HEAD[]		= {"#FIG 3.1"}; // fig file headerconst char		DUMP_SUFFIX[]	= {".dmp"};	// suffix for dump fileconst char		FIG_SUFFIX[]	= {".fig"};	// suffix for fig filechar			file_name[STRING_LEN];	// (root) file name (say xxx)char			infile_name[STRING_LEN];// input file name (xxx.dmp)char			outfile_name[STRING_LEN];// output file name (xxx.fig)char			caption[STRING_LEN];	// caption line (= command line)ofstream		ofile;					// output file streamifstream		ifile;					// input file streamint				dim_x = 0;				// horizontal dimensionint				dim_y = 1;				// vertical dimensiondouble			slice_val[MAX_DIM];		// array of slice valuesdouble			u_per_in = 1200;		// fig units per inch (version 3.1)double			in_size = 5;			// size of figure (in inches)double			in_low_x = 1;			// fig upper left corner (in inches)double			in_low_y = 1;			// fig upper left corner (in inches)double			u_size = 6000;			// size of figure (in units)double			u_low_x = 1200;			// fig upper left corner (in units)double			u_low_y = 1200;			// fig upper left corner (in units)int				pt_size = 10;			// point size (in fig units)int				dim;					// dimensionint				n_pts;					// number of pointsANNpointArray	pts = NULL;				// point arraydouble			scale;					// scale factor for transformationdouble			offset_x;				// offsets for transformationdouble			offset_y;										// transformations#define TRANS_X(p)		(offset_x + scale*(p[dim_x]))#define TRANS_Y(p)		(offset_y - scale*(p[dim_y]))//----------------------------------------------------------------------//	Error handler//----------------------------------------------------------------------void Error(char *msg, ANNerr level){	if (level == ANNabort) {		cerr << "ann2fig: ERROR------->" << msg << "<-------------ERROR\n";		exit(1);	}	else {		cerr << "ann2fig: WARNING----->" << msg << "<-------------WARNING\n";	}}//----------------------------------------------------------------------// set_slice_val - set all slice values to given value//----------------------------------------------------------------------void set_slice_val(double val){	for (int i = 0; i < MAX_DIM; i++) {		slice_val[i] = val;	}}//----------------------------------------------------------------------// getArgs - get input arguments////		Syntax://		ann2fig [-upi scale] [-x low_x] [-y low_y] //				[-sz size] [-dx dim_x] [-dy dim_y] [-sl dim value]*//				[-ps pointsize]//				file//		//		where://			-upi scale			fig units per inch (default = 1200)//			-x low_x			x and y offset of upper left corner (inches)//			-y low_y			...(default = 1)//			-sz size			maximum side length of figure (in inches)//								...(default = 5)//			-dx dim_x			horizontal dimension (default = 0)//			-dy dim_y			vertical dimension (default = 1)//			-sv value			default slice value (default = 0)//			-sl dim value		each such pair defines the value along the//								...given dimension at which to slice.  This//								...may be supplied for all dimensions except//								...dim_x and dim_y.//			-ps pointsize		size of points in fig units (def = 10)//			file				file (input=file.dmp, output=file.fig)////----------------------------------------------------------------------void getArgs(int argc, char **argv){	int i;	int sl_dim;									// temp slice dimension	double sl_val;								// temp slice value	set_slice_val(DEF_SLICE_VAL);				// set initial slice-values	if (argc <= 1) {		cerr << "Syntax:\n\        ann2fig [-upi scale] [-x low_x] [-y low_y]\n\                [-sz size] [-dx dim_x] [-dy dim_y] [-sl dim value]*\n\                file\n\        \n\        where:\n\            -upi scale          fig units per inch (default = 1200)\n\            -x low_x            x and y offset of upper left corner (inches)\n\            -y low_y            ...(default = 1)\n\            -sz size            maximum side length of figure (in inches)\n\                                ...(default = 5)\n\            -dx dim_x           horizontal dimension (default = 0)\n\            -dy dim_y           vertical dimension (default = 1)\n\            -sv value           default slice value (default = 0)\n\            -sl dim value       each such pair defines the value along the\n\                                ...given dimension at which to slice.  This\n\                                ...may be supplied for each dimension except\n\                                ...dim_x and dim_y.\n\            -ps pointsize       size of points in fig units (def = 10)\n\            file                file (input=file.dmp, output=file.fig)\n";		exit(0);	}	ANNbool fileSeen = ANNfalse;				// file argument seen?	for (i = 1; i < argc; i++) {		if (!strcmp(argv[i], "-upi")) {			// process -upi option			sscanf(argv[++i], "%lf", &u_per_in);		}		else if (!strcmp(argv[i], "-x")) {		// process -x option			sscanf(argv[++i], "%lf", &in_low_x);		}		else if (!strcmp(argv[i], "-y")) {		// process -y option			sscanf(argv[++i], "%lf", &in_low_y);		}		else if (!strcmp(argv[i], "-sz")) {		// process -sz option			sscanf(argv[++i], "%lf", &in_size);		}		else if (!strcmp(argv[i], "-dx")) {		// process -dx option			sscanf(argv[++i], "%d", &dim_x);		}		else if (!strcmp(argv[i], "-dy")) {		// process -dy option			sscanf(argv[++i], "%d", &dim_y);		}		else if (!strcmp(argv[i], "-sv")) {		// process -sv option			sscanf(argv[++i], "%lf", &sl_val);			set_slice_val(sl_val);				// set slice values		}		else if (!strcmp(argv[i], "-sl")) {		// process -sl option			sscanf(argv[++i], "%d", &sl_dim);			if (sl_dim < 0 || sl_dim >= MAX_DIM) {				Error("Slice dimension out of bounds", ANNabort);			}			sscanf(argv[++i], "%lf", &slice_val[sl_dim]);		}		if (!strcmp(argv[i], "-ps")) {			// process -ps option			sscanf(argv[++i], "%i", &pt_size);		}		else {									// must be file name			fileSeen = ANNtrue;			sscanf(argv[i], "%s", file_name);			strcpy(infile_name, file_name);		// copy to input file name	    	strcat(infile_name, DUMP_SUFFIX);	    	strcpy(outfile_name, file_name);	// copy to output file name	    	strcat(outfile_name, FIG_SUFFIX);		}	}	if (!fileSeen) {							// no file seen		Error("File argument is required", ANNabort);	}	ifile.open(infile_name, ios::in);			// open for reading	if (!ifile) {		Error("Cannot open input file", ANNabort);	}	ofile.open(outfile_name, ios::out);			// open for writing	if (!ofile) {		Error("Cannot open output file", ANNabort);	}	u_low_x = u_per_in * in_low_x;				// convert inches to fig units	u_low_y = u_per_in * in_low_y;	u_size  = u_per_in * in_size;	strcpy(caption, argv[0]);					// copy command line to caption	for (i = 1; i < argc; i++) {		strcat(caption, " ");		strcat(caption, argv[i]);	}}//----------------------------------------------------------------------// Graphics utilities for fig output////		writeHeader				write header for fig file//		writePoint				write a point//		writeBox				write a box//		writeLine				write a line//----------------------------------------------------------------------void writeHeader(){	ofile << FIG_HEAD << "\n"					// fig file header		 << "Portrait\n"		 << "Center\n"		 << "Inches\n"		 << (int) u_per_in << " 2\n";}void writePoint(ANNpoint p)						// write a single point{												// filled black point object	ofile << "1 3 0 1 -1 7 0 0 0 0.000 1 0.0000 ";	int cent_x = (int) TRANS_X(p);				// transform center coords	int cent_y = (int) TRANS_Y(p);	ofile << cent_x << " " << cent_y << " "		// write center, radius, bounds		 << pt_size << " " << pt_size << " "		 << cent_x << " " << cent_y << " "		 << cent_x + pt_size << " " << cent_y + pt_size << "\n";}void writeBox(const ANNorthRect &r)				// write box{												// unfilled box object	ofile << "2 2 0 1 -1 7 0 0 -1 0.000 0 0 -1 0 0 5\n";	int p0_x = (int) TRANS_X(r.lo);				// transform endpoints	int p0_y = (int) TRANS_Y(r.lo);	int p1_x = (int) TRANS_X(r.hi);	int p1_y = (int) TRANS_Y(r.hi);	ofile << "\t"		 << p0_x << " " << p0_y << " "			// write vertices		 << p1_x << " " << p0_y << " "		 << p1_x << " " << p1_y << " "		 << p0_x << " " << p1_y << " "		 << p0_x << " " << p0_y << "\n";}void writeLine(ANNpoint p0, ANNpoint p1)		// write line{

⌨️ 快捷键说明

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