int main(int argc, char** argv)
{
    printf("%d arguments\n", argc);
    for (int idx = 0; idx < argc; idx++)
    {
        printf("Argument %d: %s\n", idx, argv[idx]);
    }
    const size_t num_points = (argc >= 2) ? (size_t)(atoi(argv[1])) : 1000;
    std::cout << "Generating " << num_points << " random points..." << std::endl;
    const auto seed = std::chrono::high_resolution_clock::now().time_since_epoch().count();
    std::mt19937_64 prng(seed);
    std::uniform_real_distribution<double> dist(0.0, 10.0);
    EigenHelpers::VectorVector3d random_points(num_points);
    std::vector<size_t> indices(num_points);
    for (size_t idx = 0; idx < num_points; idx++)
    {
        const double x = dist(prng);
        const double y = dist(prng);
        random_points[idx] = Eigen::Vector3d(x, y, 0.0);
        indices[idx] = idx;
    }
    std::cout << "Clustering " << num_points << " points..." << std::endl;
    std::function<double(const Eigen::Vector3d&, const Eigen::Vector3d&)> distance_fn = [] (const Eigen::Vector3d& v1, const Eigen::Vector3d& v2) { return EigenHelpers::Distance(v1, v2); };
    const Eigen::MatrixXd distance_matrix = arc_helpers::BuildDistanceMatrixParallel(random_points, distance_fn);
    const std::vector<std::vector<size_t>> clusters = simple_hierarchical_clustering::SimpleHierarchicalClustering::Cluster(indices, distance_matrix, 1.0, simple_hierarchical_clustering::COMPLETE_LINK).first;
    for (size_t cluster_idx = 0; cluster_idx < clusters.size(); cluster_idx++)
    {
        const std::vector<size_t>& current_cluster = clusters[cluster_idx];
        const double cluster_num = 1.0 + (double)cluster_idx;
        for (size_t element_idx = 0; element_idx < current_cluster.size(); element_idx++)
        {
            const size_t index = current_cluster[element_idx];
            random_points[index].z() = cluster_num;
        }
    }
    std::cout << "Saving to CSV..." << std::endl;
    const std::string log_file_name = (argc >=3 ) ? std::string(argv[2]) : "/tmp/test_hierarchical_clustering.csv";
    std::ofstream log_file(log_file_name, std::ios_base::out);
    if (!log_file.is_open())
    {
        std::cerr << "\x1b[31;1m Unable to create folder/file to log to: " << log_file_name << "\x1b[0m \n";
        throw std::invalid_argument("Log filename must be write-openable");
    }
    for (size_t idx = 0; idx < num_points; idx++)
    {
        const Eigen::Vector3d& point = random_points[idx];
        log_file << point.x() << "," << point.y() << "," << point.z() << std::endl;
    }
    log_file.close();
    std::cout << "Done saving, you can import into matlab and draw with \"scatter3(x, y, z, 50, z, '.')\"" << std::endl;
    return 0;
}
int main(int argc, char* argv[])
{
  std::cerr.precision(17);

  std::ifstream points_file(argv[2]);

  std::vector<Point> points;
  std::copy(std::istream_iterator<Point>(points_file),
            std::istream_iterator<Point>(),
            std::back_inserter(points));


  int gridsize = 10;
  if(argc>3){
    gridsize = boost::lexical_cast<int>(argv[3]);
  }
  std::cerr << "gridsize = " << gridsize << std::endl;
  
  int nb_points=points.size();
  
  std::vector<bool> ray_res(nb_points);
  std::vector<bool> grid_res(nb_points);
  
  //using ray
  {
    Polyhedron polyhedron;
    std::ifstream polyhedron_file(argv[1]);
    polyhedron_file >> polyhedron;
    std::cerr << "|V| = " << polyhedron.size_of_vertices() << std::endl;
    
    CGAL::Timer timer;
    timer.start();
    CGAL::Point_inside_polyhedron_3<Polyhedron,K> inside_with_ray(polyhedron,0);
    timer.stop();
    std::cerr <<"Using ray"<< std::endl;
    std::cerr << "  Preprocessing took " << timer.time() << " sec." << std::endl;
    timer.reset();  
    int n_inside = 0;
    
    timer.start();
    for(int k=0;k<nb_points;++k){
      ray_res[k]=inside_with_ray(points[k]);
      if(ray_res[k]){
        ++n_inside;
      }
    }
    timer.stop();
    std::cerr << "  " << n_inside << " points inside " << std::endl;
    std::cerr << "  " << points.size() - n_inside << " points outside "  << std::endl;
    std::cerr << " Queries took " << timer.time() << " sec." << std::endl; 

  }
  
  //using grid
  {
    Polyhedron polyhedron;
    std::ifstream polyhedron_file(argv[1]);
    polyhedron_file >> polyhedron;
    std::cerr << "|V| = " << polyhedron.size_of_vertices() << std::endl;
    
    CGAL::Timer timer;
    timer.start();
    CGAL::Point_inside_polyhedron_3<Polyhedron,K> inside_with_grid(polyhedron, gridsize);
    timer.stop();
    std::cerr <<"Using grid"<< std::endl;
    std::cerr << "  Preprocessing took " << timer.time() << " sec." << std::endl;
    timer.reset();

    if(argc>5){
      random_points(argv[4],inside_with_grid.bbox() ,boost::lexical_cast<int>(argv[5]) );
    }
    
    int n_inside = 0;
    timer.start();
    for(int k=0;k<nb_points;++k){
      grid_res[k]=inside_with_grid(points[k]);
      if(grid_res[k]){
        ++n_inside;
      }
    }
    timer.stop();
    std::cerr << "  " << n_inside << " points inside " << std::endl;
    std::cerr << "  " << points.size() - n_inside << " points outside "  << std::endl;
    std::cerr << "  Queries took " << timer.time() << " sec." << std::endl; 
  }

  for(int k=0;k<nb_points;++k){
    if(ray_res[k]!=grid_res[k]){
      std::cerr << "WARNING: Result is different for point " << k << std::endl;
    }
  }
  
  //using original code
  {
    Polyhedron polyhedron;
    std::ifstream polyhedron_file(argv[1]);
    polyhedron_file >> polyhedron;
    std::cerr << "|V| = " << polyhedron.size_of_vertices() << std::endl;

    
    std::cerr <<"Using ray (original code)"<< std::endl;
    CGAL::Point_inside_polyhedron_3<Polyhedron,K> inside_with_ray(polyhedron,0);
    
    CGAL::Timer timer;  
    int n_inside = 0;
    timer.start();
    for(int k=0;k<nb_points;++k)
      if(inside_with_ray(points[k],true)) ++n_inside;
    timer.stop();
    std::cerr << "  " << n_inside << " points inside " << std::endl;
    std::cerr << "  " << points.size() - n_inside << " points outside "  << std::endl;
    std::cerr << "  Queries took " << timer.time() << " sec." << std::endl; 
  }
  
  return 0;
}
예제 #3
0
void test_type(int seed, int count, p_q_settings const& settings)
{
    boost::timer t;

    typedef boost::minstd_rand base_generator_type;

    //boost::uniform_real<> random_factor(0.5, 1.2);
    //boost::uniform_real<> random_location(-10.0, 10.0);
    //boost::uniform_int<> random_points(5, 20);

    // This set (next 4 lines) are now solved for the most part
    // 2009-12-03, 3 or 4 errors in 1000000 calls
    // 2009-12-07,     no errors in 1000000 calls
    //boost::uniform_real<> random_factor(1.0 - 1e-3, 1.0 + 1e-3);
    //boost::uniform_real<> random_location(-1e-3, 1e-3);
    //boost::uniform_real<> random_rotation(-1e-3, 1e-3);
    //boost::uniform_int<> random_points(3, 3);

    // 2009-12-08, still errors, see notes
    // 2009-12-09, (probably) solved by order on side
    // 2010-01-16: solved (no errors in 1000000 calls)
    //boost::uniform_real<> random_factor(1.0 - 1e-3, 1.0 + 1e-3);
    //boost::uniform_real<> random_location(-1e-3, -1e-3);
    //boost::uniform_real<> random_rotation(-1e-3, 1e-3);
    //boost::uniform_int<> random_points(3, 4);

    // This set (next 4 lines) are now solved ("distance-zero"/"merge iiii" problem)
    // 2009-12-03: 5,50 -> 2:1 000 000 wrong (2009-12-03)
    // 2010-01-16: solved (no errors in 10000000 calls)
    boost::uniform_real<> random_factor(0.3, 1.2);
    boost::uniform_real<> random_location(-20.0, +20.0); // -25.0, +25.0
    boost::uniform_real<> random_rotation(0, 0.5);
    boost::uniform_int<> random_points(5, 15);

    base_generator_type generator(seed);

    boost::variate_generator<base_generator_type&, boost::uniform_real<> >
        factor_generator(generator, random_factor);

    boost::variate_generator<base_generator_type&, boost::uniform_real<> >
        location_generator(generator, random_location);

    boost::variate_generator<base_generator_type&, boost::uniform_real<> >
        rotation_generator(generator, random_rotation);

    boost::variate_generator<base_generator_type&, boost::uniform_int<> >
        int_generator(generator, random_points);

    for(int i = 0; i < count; i++)
    {
        test_star_ellipse<T, Clockwise, Closed>(seed, i + 1,
            star_params(int_generator() * 2 + 1,
                    factor_generator(), factor_generator(),
                    location_generator(), location_generator(), rotation_generator()),
            star_params(int_generator() * 2 + 1,
                    factor_generator(), factor_generator(),
                    location_generator(), location_generator(), rotation_generator()),
            settings);
    }
    std::cout
        << "type: " << string_from_type<T>::name()
        << " time: " << t.elapsed()  << std::endl;
}
예제 #4
0
int main(void)
{ //int N;
	double t1,t2; double total_time=0; int count=0; double total1=0;double total2=0;double total3=0; int i;
	int size_user=0;  double determinant; int choice=0; int change_size_value=1;

	Result r1;
	Result r2;
	Result r3;
	printf("\nDimension: %d\n",DIM);
	while(change_size_value==1 )
	{

	    //change_size(change_size_value);

		p_array= (Point *) malloc(sizeof(Point)* size);
		arr1= (Point *) malloc(sizeof(Point)* size);
		arr2= (Point *) malloc(sizeof(Point)* size);
		AA = (double *) malloc(sizeof(Point) * size*DIM) ;


		choice=2;
		printf("-----------------------------------------------\n");
		switch(choice)
		{
		case 1:
			strcpy ( return_path_, "ornek.txt" );
			fill_point_array() ;
			break;
		case 2:
			strcpy ( return_path_, "ornek.txt" );
			random_points();
			break;
		case 3:

            // strcpy ( return_path_, "C:\\Users\\PBell\\Documents\\Visual Studio 2008\\Projects\\closestPair_k_dim\\closestPair_k_dim\\noktalar_db\\short.txt" );
			 strcpy ( return_path_, "C:\\Users\\PBell\\Documents\\Visual Studio 2008\\Projects\\closestPair_k_dim\\closestPair_k_dim\\long.txt");

//C:\Users\PBell\Documents\Visual Studio 2008\Projects\closestPair_k_dim\closestPair_k_dim\noktalar_db
			fill_AA();
			break;


		}



		/*
		for(i=0;i<DIM;i++)
		{
			if(i%2==0)
		{
		    recursivin_smartforcu_gectigi_nokta(recursivin_smartforcu_gectigi_nokta_,i,size/2,1);
		}
		else
		{
		    recursivin_smartforcu_gectigi_nokta(recursivin_smartforcu_gectigi_nokta_,i,size/2,3);
		}

		}
		*/



		for(count=0; count<1; count++)
		{

			t2=0; t1=0;

			//-----------------------------------------------

			//if(change_size_value<3)
			{
			t1=(double) clock();
			//printf ("It took me %d clicks (%f seconds).\n",t1,(double)t1/CLOCKS_PER_SEC);
			r1=bruteForceClosestPair(p_array,size,0);
			t2 =(double) clock();
			//printf ("It took me %d clicks (%f seconds).\n",t2,(double)t2/CLOCKS_PER_SEC);
			total1=total1+ t2-t1;
			}
			//else
			{
			//if(flag__==0) printf("Bruteforce runtime c*k uzun\n\n");
			flag__=1;
			}




			//printf ("bruteForceClosestPair: %.f clicks (%f seconds).\n\n",t2-t1,((double)t2-(double)t1)/CLOCKS_PER_SEC);

			t2=0; t1=0;

			//-----------------------------------------------
			t1=(double) clock();

//for(i=0;i<DIM;i++)
//origine[i]=0;


//generate_basis();

            /*
			for(i=0;i<DIM;i++)
			inv[i]= (double *) malloc(sizeof(double)* DIM);

			 determinant = detrm( basis, DIM );
            // printf( "THE DETERMINANT IS=%f", determinant );

             if ( determinant == 0 )
             {//  printf( "\nMATRIX IS NOT INVERSIBLE\n" );
			 }
              else
             cofact( basis, DIM );
			  */

//fill_projected_point_array() ;


			//printf("p_array: \n\n");
			//print_array(p_array);





			for(i=0;i<DIM;i++)
			{
				A[i]= (Point *) malloc(sizeof(Point)* size);
				memcpy ( A[i], p_array,sizeof(Point)* size );

			}

			//for(i=0;i<DIM;i++)
			{
			//Histogram( i);
			//Histogram_projected(i);
			}
			flag2__=1;

			for(i=0;i<DIM;i++)
			{
				merge_sort(A[i],0,size-1, 1,i);
				//printf("A[%d]: \n\n",i);
				//print_array(A[i]);
			}

			//print_array(p_array);



			r2=smartForce(A,1);
			t2 =(double) clock();
			total2=total2+ t2-t1;

			for(i=0;i<DIM;i++)
			{
				free(A[i]);

			}

			//printf ("It took me %d clicks (%f seconds).\n",t2,(double)t2/CLOCKS_PER_SEC);

			//printf ("smartForce: %f clicks (%f seconds).\n\n",t2-t1,((double)t2-(double)t1)/CLOCKS_PER_SEC);


			t2=0; t1=0;

			//-----------------------------------------------


	t1= (double)clock();
			for(i=0;i<DIM;i++)
			{
				A[i]= (Point *) malloc(sizeof(Point)* size);
				memcpy ( A[i], p_array,sizeof(Point)* size );

				merge_sort(A[i],0,size-1, 1,i);
				//printf("A[%d]: \n\n",i);
				//print_array(A[i]);
			}



			r3=recursiveClosestPair(A[0] , size);
			t2 =(double) clock();
			total3=total3+ t2-t1;

			for(i=0;i<DIM;i++)
			{
				free(A[i]);

			}


			//printf ("recursiveClosestPair: %f clicks (%f seconds).\n",t2-t1,((double)t2-(double)t1)/CLOCKS_PER_SEC);

			//printf("\n\n\n------------------------------------------\n\n\n");
			t2=0; t1=0;


			//print_array(p_array	);



		}
		    flag2__=0;

			//if(flag__==0)
			{
			printf("bruteForceClosestPair'da en yakin iki double nokta: ");
			print_result(r1);
			printf ("bruteForceClosestPair: %f clicks (%f seconds).\n\n",total1/count,total1/(CLOCKS_PER_SEC*count));

			}


			printf("quickcp'de en yakin iki double nokta: ");
			print_result(r2);
			printf ("quickcp: %f clicks (%f seconds).\n\n",total2/count,total2/(CLOCKS_PER_SEC*count));

			printf("recursiveClosestPair'da en yakin iki double nokta: ");
			print_result(r3);
			printf ("recursiveClosestPair: %f clicks (%f seconds).\n\n",total3/count,total3/(CLOCKS_PER_SEC*count));




		free(p_array);
		free(arr1);
		free(arr2);

		total1=0;
		total2=0;
		total3=0;


		change_size_value++;
		}

	scanf("%d",&a);

	return 0;
}
예제 #5
0
int main(int argc, char *argv[])
{
	srand(time(NULL));
	if (argc != 6)
	{
		std::cout << usage << std::endl;
		return -1;
	}

	int count = atoi(argv[1]);
	double min = atof(argv[2]);
	double max = atof(argv[3]);
	std::ofstream file(argv[4], std::ofstream::out);
	bool integer = false;
	if (strcmp(argv[5], "double") == 0)
		integer = false;
	else if (strcmp(argv[5], "int") == 0)
		integer = true;

	point *ps = new point[count];
	random_points(ps, count, min, max, integer);
	space_partition(ps, count);

	double real_min = 999999;
	double real_max = -999999;
	for (int i = 0; i < count; i++)
	{
		real_max = ps[i].x > real_max ? ps[i].x : real_max;
		real_max = ps[i].y > real_max ? ps[i].y : real_max;
		real_max = ps[i].x < real_min ? ps[i].x : real_min;
		real_max = ps[i].y < real_min ? ps[i].y : real_min;
	}

	point start;
	point end;
	do
	{
		random_points(&start, 1, real_min, real_max, integer);
	} while (!in_polygon(start, ps, count));
	do
	{
		random_points(&end, 1, real_min, real_max, integer);
	} while (!in_polygon(end, ps, count));

	if (integer)
	{
		file << (int)round(start.x) << " " << (int)round(start.y) << " "
			<< (int)round(end.x) << " " << (int)round(end.y) << std::endl;
		file << count << std::endl;
		for (int i = 0; i < count; i++)
			file << (int)round(ps[i].x) << " " << (int)round(ps[i].y) << std::endl;
	}
	else
	{
		file << start.x << " " << start.y << " " << end.x << " " << end.y << std::endl;
		file << count << std::endl;
		for (int i = 0; i < count; i++)
			file << ps[i].x << " " << ps[i].y << std::endl;
	}
	
	delete[] ps;
	return 0;
}