Beispiel #1
0
QRgb NwwMapImage::pixel( double const lonRad, double const latRad )
{
    double const x = lonRadToPixelX( lonRad );
    double const y = latRadToPixelY( latRad );

    switch ( m_interpolationMethod ) {
    case NearestNeighborInterpolation:
        return nearestNeighbor( x, y );
    case BilinearInterpolation:
        return bilinearInterpolation( x, y );
    default:
        return nearestNeighbor( x, y );
    }
}
Beispiel #2
0
void scrambleNotes(Array<NoteUnit>& notes) {
   Array<NoteUnit> notes2;
   notes2 = notes;
   int i;
   for (i=0; i<notes2.getSize(); i++) {
      notes2[i].random = rand();
   }

   if (voiceQ) {
      qsort(notes.getBase(), notes.getSize(), sizeof(NoteUnit), 
         compareNoteSortTrack);
      qsort(notes2.getBase(), notes2.getSize(), sizeof(NoteUnit), 
         compareNoteUnitTrack);
   } else {
      qsort(notes2.getBase(), notes2.getSize(), sizeof(NoteUnit), 
         compareNoteUnit);
   }

   if (neighborQ) {
      for (i=0; i<notes.getSize(); i++) {
         notes[i].newpitch = nearestNeighbor(notes[i].pitch, notes2[i].pitch);
      }
   } else {
      for (i=0; i<notes.getSize(); i++) {
         notes[i].newpitch = notes2[i].pitch;
      }
   }
}
Beispiel #3
0
void NN::clusterize(AbstractMetric *pMetric) {
    int nObjectCount = _pContainer->ids().size();

    list<Cluster*> lsClusters;
    for (int i = 0; i < nObjectCount; i++) {
        Cluster *pC = new Cluster(_pContainer);
        pC->addObject(_pContainer->getByIndex(i));
        lsClusters.push_back(pC);
    }
    srand(time(NULL));
    //Pick a random cluster index
    int nRandomIndex = rand() % nObjectCount;
    Cluster *pC = NULL, *pC2 = NULL;
    //Find the cluster for that index
    {
        list<Cluster*>::iterator iC;
        int i = 0;
        for (iC = lsClusters.begin();
                iC != lsClusters.end() && i < nObjectCount;
                iC++, i++) {
            if (i == nRandomIndex) {
                pC = *iC;
                break;
            }
        }
    }
    if (pC == NULL) {
        fprintf(stderr, "Failed to generate random cluster for NN!\n");
        return;
    }
    Cluster *pC0 = *(lsClusters.begin());
    while (lsClusters.size() > 1) {
        pC = nearestNeighbor(pC0, lsClusters, pMetric);
        pC2 = nearestNeighbor(pC, lsClusters, pMetric);
        if (pC0 == pC2) {
            Cluster *pC_new = new Cluster(_pContainer);
            pC_new->addCluster(pC);
            pC_new->addCluster(pC2);
            lsClusters.push_back(pC_new);
            lsClusters.remove(pC);
            lsClusters.remove(pC2);
            pC0 = *(lsClusters.begin());
        } else {
            pC0 = pC2;
        }
    }
};
Beispiel #4
0
void findPairs(double thresholdSIFT,
               std::vector<cv::KeyPoint>& keypoints1, cv::Mat& descriptors1,
               std::vector<cv::KeyPoint>& keypoints2, cv::Mat& descriptors2,
               std::vector<cv::Point2f>& srcPoints, std::vector<cv::Point2f>& dstPoints)
{
  for (int i = 0; i < descriptors1.rows; i++)
  {
    clear_line();
    fprintf(stderr,"Checking SIFT pairs , threshold %0.2f : \n",thresholdSIFT);
    fprintf(stderr,"%u / %u checks  - %u matches \n",i, descriptors1.rows , srcPoints.size());
    cv::KeyPoint pt1 = keypoints1[i];
    cv::Mat desc1 = descriptors1.row(i);
    int nn = nearestNeighbor(thresholdSIFT,desc1, keypoints2, descriptors2);
    if (nn >= 0) {
      cv::KeyPoint pt2 = keypoints2[nn];
      srcPoints.push_back(pt1.pt);
      dstPoints.push_back(pt2.pt);
    }
  }
}
Beispiel #5
0
int main(int argc,char* argv[])
{
	int k;
	clock_t start, end;
	float seconds = 0;
	int r;
	double minDist = BIG_NUM;
	double totalDist;
	int i,seed,storeSeed;
	char* x;
	struct Arr2D* Array = create2DArr(4,40000);
	
	//1 = city//2 = x // 3 = y// 4 = order
	FILE* fp;
	
	
	getCityXY(fp,argv[1],Array);
	
	for(i = 0; i < Array->size[CITY]; i++)
	{
		Array->array[ORDER][i] = -1;
	}
	
	
	
	if (Array->size[CITY] > 1000)
	{
			printf("MAX SOLVE TIME = %d Seconds\n",SOLVE_TIME);
	
		do{
			start = clock();
		
	
			seed = (rand())%(Array->size[CITY]);
		

			totalDist = nearestNeighbor(Array,seed);
			if(totalDist < minDist)
			{
				storeSeed = seed;
				minDist = totalDist;
			}
		
	
			end = clock();
	
			seconds += (float)(end - start) / CLOCKS_PER_SEC;
		
		
		}while(seconds <= SOLVE_TIME);
	
		
	}
	else
	{
		storeSeed = RepNearestNeighbor(Array);
		
	}
	
	minDist = nearestNeighbor(Array,storeSeed);
	
	
	
	x = strcat(argv[1],".tour");
	outPutFile(fp,x,Array,minDist);
	
	
	delete2DArr(Array);
	
	printf("Success!\n");
	
	return 0;
}
Beispiel #6
0
int main(int argc, char** argv)
{
	if (argc != 4)
	{
		cerr << "Usage: " << argv[0] << " dim n area." << endl;
		return -1;
	}

	int dim = atol(argv[1]);
	int n = atol(argv[2]);
	double area = atof(argv[3]);
	if(dim <= 0)
	{
		cerr << "Dimension should be larger than 0." << endl;
		return -1;
	}
	if(n <= 0)
	{
		cerr << "The number of query points should be larger than 0." << endl;
		return -1;
	}
	if(area <= 0 || area > 1)
	{
		cerr << "the area of query points should be in (0, 1]." << endl;
		return -1;
	}

	/*read static data set*/
	vector <Point> P;
	ifstream in("../data.ini");
	if(!in)
	{
		cerr << "Cannot open file data.ini.\n";
		return -1;
	}
 	P = readPoints(in, dim);
	uint32_t N = P.size();

	try {
		IStorageManager* memfile = StorageManager::createNewMemoryStorageManager();
		StorageManager::IBuffer* file = StorageManager::createNewRandomEvictionsBuffer(*memfile, 10, false);
		id_type indexIdentifier;
		ISpatialIndex* tree = RTree::createNewRTree(*file, 0.7, CAPACITY, CAPACITY, dim, SpatialIndex::RTree::RV_RSTAR, indexIdentifier);
		id_type id = 0;
		for(uint32_t i = 0; i < N; ++i)
		{
			std::ostringstream os;
			os << P[i];
			std::string data = os.str();
			tree->insertData(data.size() + 1, reinterpret_cast<const byte*>(data.c_str()), P[i], id);
			id++;
		}
		/*std::cerr << "Operations: " << N << std::endl;
		std::cerr << *tree;
		std::cerr << "Buffer hits: " << file->getHits() << std::endl;
		std::cerr << "Index ID: " << indexIdentifier << std::endl; 
		bool ret = tree->isIndexValid();
		if (ret == false) std::cerr << "ERROR: Structure is invalid!" << std::endl;
		else std::cerr << "The stucture seems O.K." << std::endl; */


	for(uint32_t loop = 1; loop <= LOOPNUM; ++loop)
	{
		cout << "/**************** BEGIN " << loop << " ***************/" << endl;

		/*generate query set*/
		vector <Point> Q;
		//Q = genPoints(dim, n, area, loop);
		stringstream ss;
		ss << "../query/n" << n << "M" << area << "/loop" << loop;
		cout << ss.str().c_str() << endl;
		ifstream qin(ss.str().c_str());
		if(!qin)
		{
			cerr << "Cannot open query file";
			return -1;
		}
		Q = readPoints(qin, dim);
		
		/*************** BEGIN MQM method ******************/
		MQM(tree, Q, n, FUN); // MQM method for finding ANN of Q
		/*************** END MQM method *******************/


		/*************** BEGIN ADM method ******************/
		CATCH cost1;
		cost1.catch_time();

		vector <uint32_t> nnIDs = nearestNeighborSet(tree, Q, n); // find the NN for every qi in Q as qi'
		vector <Point> newQ;
		for(uint32_t i = 0; i < n; ++i)
		{
			newQ.push_back(P[nnIDs[i]]);
		}
		cost1.catch_time();
		cout << "proposal method: cpu cost for finding NNs of Q as Q' is " << cost1.get_cost(2) << " millisecond(s)" << endl;

		/***** read dist-matrix index for Q' ********/
		uint32_t maxK = P.size() / RATIO; // the length of dist-matrix index
		uint32_t * dmindex[n];
		for(uint32_t i = 0; i < n; ++i)
	 	{ // read the dist-matrix index of qi'
			dmindex[i] = readDMIndex(nnIDs[i], maxK);
			if (dmindex[i] == NULL)
			{
				cerr << "error for loading Dist-Matrix Index." << endl;
				return -1;
			}
		}

		double minadist  = 0;
		/* ADM method for finding approxiamte ANN  */
		Point adm_ANN = ADM(newQ, n, P, N, dmindex, maxK, FUN, minadist, ERROR_RATE);
		cout << "ADM: best_dist is " << getAdist(adm_ANN, Q, n, FUN) << endl << endl; 
		/*************** END ADM method *******************/

		/*************** BEGIN approxiamte vp-ANN method ******************/
		/* approximate vp-ANN method for finding ANN of Q'*/
		CATCH cost2;
		cost2.catch_time();

		Point centroid = findCentroid(Q, dim, n);
		minadist = getAdist(centroid, Q, n, FUN);
		uint32_t vpID = nearestNeighbor(tree, centroid);
		uint32_t best_id_Q = epsilonANN(Q, n, P, N, vpID, dmindex, maxK, FUN); 

		cost2.catch_time();
		cout << "approxiamte vp-ANN method: cpu cost is " << cost2.get_cost(2) << " millisecond(s)" << endl;
		cout << "approximate vp-ANN method: best_dist is " << getAdist(P[best_id_Q], Q, n, FUN) << endl;
		cout << "approxiamte vp-ANN method: best_NN is ";
		displayCoordinates(P[best_id_Q]); 
		cout << endl;
		/*************** END approxiamte vp-ANN method *******************/


		/*************** BEGIN BF MBM method ******************/
		/* MBM method for finding ANN of Q */
		CATCH mbmcost;
		mbmcost.catch_time();

		Region M = getMBR(Q, dim, n);
		MyQueryStrategy qs = MyQueryStrategy(M, Q, FUN);
		tree->queryStrategy(qs);

		mbmcost.catch_time();
		cout << "MBM: cpu cost is " << mbmcost.get_cost(2) << " millisecond(s)" << endl;
		cout << "MBM: best_dist is " << qs.best_dist << endl;
		cout << "MBM: best_NN is ";
		displayCoordinates(qs.best_NN);
		cout << "MBM: leafIO = " << qs.mbm_leafIO << "; indexIO = " << qs.mbm_indexIO << endl << endl;
		/*************** END BF MBM method *******************/


		/*************** BEGIN brute method ******************/
		/* brute method for finding ANN of Q*/
		CATCH brute_cost;
		brute_cost.catch_time();

		uint32_t ANNid = brute_ANN(Q, n, P, N, FUN);

		brute_cost.catch_time();
		cout << "brute method: cpu cost is " << brute_cost.get_cost(2) << " millisecond(s)" << endl;
		double adist = getAdist(P[ANNid], Q, n, FUN);
		cout << "brute method: best_dist is " << adist << endl;
		cout << "brute method: best_NN is ";
		displayCoordinates(P[ANNid]);
		/*************** END brute method *******************/
		
		cout << "/**************** END " << loop << " ****************/" << endl << endl;

	} // end loop
		
		delete tree;
		delete file;
		delete memfile;
	}
	catch(Tools::Exception& e)
	{
		cerr << "*********ERROR**********" << endl;
		std::string s = e.what();
		cerr << s << endl;
		return -1;
	}
	catch(...)
	{
		cerr << "**********ERROR********" << endl;
		return -1;
	}

	return 1;
}