Exemplo n.º 1
0
int main(int argc, char *argv[]) {
	int c;

	triangulate = debug = 0;
	plot = 1;
	while ((c = getopt(argc, argv, "dpt")) != EOF) {
		switch (c) {
		case 'd':
			debug = 1;
			break;
		case 't':
			triangulate = 1;
			plot = 0;
			break;
		case 'p':
			plot = 1;
			break;
		}
	}

	freeinit(&sfl, sizeof(Site));
	readsites();
	siteidx = 0;
	geominit();
	if (plot) {
		plotinit();
	}
	voronoi(nextone);
	return (0);
}
cv::Mat ImageProcessor::distanceTransformWithVoronoi(cv::Mat & origImage, cv::Mat &voronoiImage) {
	cv::Mat dists;
	cv::Mat targetImage;
	cv::Mat voronoi(origImage.size(), CV_32SC1, cv::Scalar::all(BLACK));
	cv::bitwise_not(origImage, targetImage);

	//TODO: uncomment this
	//cv::distanceTransform(targetImage, dists, voronoi, CV_DIST_L2, CV_DIST_MASK_5, cv::DIST_LABEL_CCOMP);

	return voronoi;
}
Exemplo n.º 3
0
float do_clump(ParticleKey *state, const float par_co[3], float time, const float orco_offset[3], float clumpfac, float clumppow, float pa_clump,
               bool use_clump_noise, float clump_noise_size, CurveMapping *clumpcurve)
{
	float clump;

	if (use_clump_noise && clump_noise_size != 0.0f) {
		float center[3], noisevec[3];
		float da[4], pa[12];

		mul_v3_v3fl(noisevec, orco_offset, 1.0f / clump_noise_size);
		voronoi(noisevec[0], noisevec[1], noisevec[2], da, pa, 1.0f, 0);
		mul_v3_fl(&pa[0], clump_noise_size);
		add_v3_v3v3(center, par_co, &pa[0]);

		do_clump_level(state->co, state->co, center, time, clumpfac, clumppow, pa_clump, clumpcurve);
	}

	clump = do_clump_level(state->co, state->co, par_co, time, clumpfac, clumppow, pa_clump, clumpcurve);

	return clump;
}
Exemplo n.º 4
0
int main(int argc, char **argv)

{
  /* Read command line arguments */
  read_command_line(argc,argv);

  /* Read Parameters from parameter file */
  read_parameters();

  /* Initialize cell data structures */
  init_cells();

  /* Read atoms */
  read_stress(infilename);

  /* Calculate volume of Voronoi cells */
  voronoi();

  /* Output results */
  write_stress(restart);

  exit(0);

}
//TODO:
//TODO:  Use libnoise http://libnoise.sourceforge.net/tutorials/index.html !!!
//TODO:
void generate()
{

	//set crop height and width
	if (crop_height < 1)
		crop_height = tmap_size;
	if (crop_width < 1)
		crop_width = tmap_size;

	//if a crop value is set
	//set tmap_size to fit the cropped values
	int max_size = std::max(crop_height, crop_width);
	int max_size_tmp = max_size - 1;

	if ((max_size_tmp & (max_size_tmp - 1)) == 0)
	{
		//leave set size as highest crop value
		tmap_size = max_size;
	}
	else
	{
		//find smallest value such that (value is power of 2) + 1 and value > max_size
		int t = ceil(log2(max_size)) + 1;
		tmap_size = (1 << t) + 1;
	}

	double finish = 0;
	//display info
	if (verbose)
	{
		std::cout << "Using " << config_file << std::endl;
		std::cout << "Staring square diamond" << std::endl;
		std::cout << "Size: " << crop_width << " x " << crop_height
				<< " original size " << tmap_size << std::endl;
		std::cout << "Starting seed value " << seed << std::endl;
		std::cout << "Starting random offset " << random_offset << std::endl;
		std::cout << "Random offset decrease ratio " << offset_dr << std::endl;

	}

	//init map array
	tmap = new int*[tmap_size];
	for (int i = 0; i < tmap_size; ++i)
	{
		tmap[i] = new int[tmap_size];
		for (int j = 0; j < tmap_size; j++)
			tmap[i][j] = 0;
	}

//	initialize random seed:
//	use for generating a random map every time
//  srand ( time(NULL) );
	//harcoded for now as produces a nice map for testing
	srand(12);

	//fill the array with values
	square_diamond();

	//interpolate voronoi diagram
	//TODO: add noise to voronoi
	if (verbose)
	{
		std::cout << "Voronoi points " << voronoi_size << std::endl;
		/*
		 for (int i = 0; i < voronoi_size; ++i) {
		 std::cout << "\t" << voronoi_points[i][0] << "," << voronoi_points[i][1] << std::endl;
		 }
		 */
	}
	voronoi();

	erosion();

	if (!neg)
		clear_neg();

//		finish = clock() - start;
	if (verbose)
		std::cout << "Finished square diamond " << (finish / 1000000)
				<< std::endl;
	double sqadia = (finish / 1000000);

	if (normalise)
	{
		if (verbose)
			std::cout << "Normalising with value range " << normalise_min << "-"
					<< normalise_max << std::endl;
		normalise_map();
	}

	if (output_format == STANDRARD_HEIGHTS)
	{
		print_map(fopen(output_file.c_str(), "w"));
	}
	else if (output_format == STANDARD_XML)
	{
		print_map_xml(fopen(output_file.c_str(), "w"));
	}

	if (scale > 0 && crop_height > 256 && crop_width > 256)
	{

//			start = clock();
		if (verbose)
			std::cout << "Generating rivers" << std::endl;
		rivers();
//			finish = clock() - start;
		if (verbose)
			std::cout << "Done " << (finish / 1000000) << std::endl;
		double rivers_time = (finish / 1000000);
		print_rivers(0);

//			start = clock();
		if (verbose)
			std::cout << "Generating vegetation" << std::endl;
		vegetation(verbose);
//			finish = clock() - start;
		if (verbose)
			std::cout << "Done " << (finish / 1000000) << std::endl;
		double veg_time = (finish / 1000000);
		print_vegetation(0);

		if (verbose)
			std::cout << "Generating settlements" << std::endl;
		settlements();
//			finish = clock() - start;
		if (verbose)
			std::cout << "Done " << (finish / 1000000) << std::endl;
		double settlement_time = (finish / 1000000);
		print_settlements(0);

		std::cout << crop_height << "\t"
				<< (sqadia + rivers_time + veg_time + settlement_time) << "\t"
				<< sqadia << "\t " << rivers_time << "\t" << veg_time << "\t"
				<< settlement_time << std::endl;
	}

	std::cout << "Drawing contours" << std::endl;
	contour_map(32, 32, verbose);
	print_contour(0);
	print_kf(0);

}
Exemplo n.º 6
0
bool VoronoiDiagramGenerator::generateVoronoi(float *xValues, float *yValues, int numPoints, float minX, float maxX, float minY, float maxY, float minDist)
{
	cleanup();
	cleanupEdges();
	int i;

	minDistanceBetweenSites = minDist;

	nsites=numPoints;
	plot = 0;
	triangulate = 0;	
	debug = 1;
	sorted = 0; 
	freeinit(&sfl, sizeof (Site));
		
	sites = (struct Site *) myalloc(nsites*sizeof( *sites));

	if(sites == 0)
		return false;

	xmin = xValues[0];
	ymin = yValues[0];
	xmax = xValues[0];
	ymax = yValues[0];

	for(i = 0; i< nsites; i++)
	{
		sites[i].coord.x = xValues[i];
		sites[i].coord.y = yValues[i];
		sites[i].sitenbr = i;
		sites[i].refcnt = 0;

		if(xValues[i] < xmin)
			xmin = xValues[i];
		else if(xValues[i] > xmax)
			xmax = xValues[i];

		if(yValues[i] < ymin)
			ymin = yValues[i];
		else if(yValues[i] > ymax)
			ymax = yValues[i];

		//printf("\n%f %f\n",xValues[i],yValues[i]);
	}
	
	qsort(sites, nsites, sizeof (*sites), scomp);
	
	siteidx = 0;
	geominit();
	float temp = 0;
	if(minX > maxX)
	{
		temp = minX;
		minX = maxX;
		maxX = temp;
	}
	if(minY > maxY)
	{
		temp = minY;
		minY = maxY;
		maxY = temp;
	}
	borderMinX = minX;
	borderMinY = minY;
	borderMaxX = maxX;
	borderMaxY = maxY;
	
	siteidx = 0;
	voronoi(triangulate);

	return true;
}
Exemplo n.º 7
0
int main(int argc, char **argv)
{
    int i;
    int **cats, *ncats, nfields, *fields;
    struct Flag *line_flag;

    /* struct Flag *all_flag; */
    struct Option *in_opt, *out_opt;
    struct Flag *table_flag;
    struct GModule *module;
    struct line_pnts *Points;
    struct line_cats *Cats;
    int node, nnodes;
    COOR *coor;
    int ncoor, acoor;
    int line, nlines, type, ctype, area, nareas;
    int err_boundaries, err_centr_out, err_centr_dupl, err_nocentr;

    G_gisinit(argv[0]);

    module = G_define_module();
    G_add_keyword(_("vector"));
    G_add_keyword(_("geometry"));
    G_add_keyword(_("triangulation"));
    module->description = _("Creates a Voronoi diagram from an input vector "
			    "map containing points or centroids.");

    in_opt = G_define_standard_option(G_OPT_V_INPUT);
    out_opt = G_define_standard_option(G_OPT_V_OUTPUT);

    /*
       all_flag = G_define_flag ();
       all_flag->key = 'a';
       all_flag->description = _("Use all points (do not limit to current region)");
     */

    line_flag = G_define_flag();
    line_flag->key = 'l';
    line_flag->description =
	_("Output tessellation as a graph (lines), not areas");

    table_flag = G_define_flag();
    table_flag->key = 't';
    table_flag->description = _("Do not create attribute table");

    if (G_parser(argc, argv))
	exit(EXIT_FAILURE);


    if (line_flag->answer)
	Type = GV_LINE;
    else
	Type = GV_BOUNDARY;

    All = 0;

    Points = Vect_new_line_struct();
    Cats = Vect_new_cats_struct();

    /* open files */
    Vect_set_open_level(2);
    Vect_open_old(&In, in_opt->answer, "");

    if (Vect_open_new(&Out, out_opt->answer, 0) < 0)
	G_fatal_error(_("Unable to create vector map <%s>"), out_opt->answer);

    Vect_hist_copy(&In, &Out);
    Vect_hist_command(&Out);

    /* initialize working region */
    G_get_window(&Window);
    Vect_region_box(&Window, &Box);
    Box.T = 0.5;
    Box.B = -0.5;

    freeinit(&sfl, sizeof(struct Site));

    G_message(_("Reading sites..."));
    readsites();

    siteidx = 0;
    geominit();

    triangulate = 0;
    plot = 0;
    debug = 0;

    G_message(_("Voronoi triangulation..."));
    voronoi(triangulate, nextone);

    /* Close free ends by current region */
    Vect_build_partial(&Out, GV_BUILD_BASE);

    ncoor = 0;
    acoor = 100;
    coor = (COOR *) G_malloc(sizeof(COOR) * acoor);

    nnodes = Vect_get_num_nodes(&Out);
    for (node = 1; node <= nnodes; node++) {
	double x, y;

	if (Vect_get_node_n_lines(&Out, node) < 2) {	/* add coordinates */
	    Vect_get_node_coor(&Out, node, &x, &y, NULL);

	    if (ncoor == acoor - 5) {	/* always space for 5 region corners */
		acoor += 100;
		coor = (COOR *) G_realloc(coor, sizeof(COOR) * acoor);
	    }

	    coor[ncoor].x = x;
	    coor[ncoor].y = y;
	    ncoor++;
	}
    }

    /* Add region corners */
    coor[ncoor].x = Box.W;
    coor[ncoor].y = Box.S;
    ncoor++;
    coor[ncoor].x = Box.E;
    coor[ncoor].y = Box.S;
    ncoor++;
    coor[ncoor].x = Box.E;
    coor[ncoor].y = Box.N;
    ncoor++;
    coor[ncoor].x = Box.W;
    coor[ncoor].y = Box.N;
    ncoor++;

    /* Sort */
    qsort(coor, ncoor, sizeof(COOR), (void *)cmp);

    /* add last (first corner) */
    coor[ncoor].x = Box.W;
    coor[ncoor].y = Box.S;
    ncoor++;

    for (i = 1; i < ncoor; i++) {
	if (coor[i].x == coor[i - 1].x && coor[i].y == coor[i - 1].y)
	    continue;		/* duplicate */

	Vect_reset_line(Points);
	Vect_append_point(Points, coor[i].x, coor[i].y, 0.0);
	Vect_append_point(Points, coor[i - 1].x, coor[i - 1].y, 0.0);
	Vect_write_line(&Out, Type, Points, Cats);
    }

    G_free(coor);

    /* Copy input points as centroids */
    nfields = Vect_cidx_get_num_fields(&In);
    cats = (int **)G_malloc(nfields * sizeof(int *));
    ncats = (int *)G_malloc(nfields * sizeof(int));
    fields = (int *)G_malloc(nfields * sizeof(int));
    for (i = 0; i < nfields; i++) {
	ncats[i] = 0;
	cats[i] =
	    (int *)G_malloc(Vect_cidx_get_num_cats_by_index(&In, i) *
			    sizeof(int));
	fields[i] = Vect_cidx_get_field_number(&In, i);
    }

    if (line_flag->answer)
	ctype = GV_POINT;
    else
	ctype = GV_CENTROID;

    nlines = Vect_get_num_lines(&In);

    G_message(_("Writing sites to output..."));

    for (line = 1; line <= nlines; line++) {

	G_percent(line, nlines, 2);

	type = Vect_read_line(&In, Points, Cats, line);
	if (!(type & GV_POINTS))
	    continue;

	if (!Vect_point_in_box(Points->x[0], Points->y[0], 0.0, &Box))
	    continue;

	Vect_write_line(&Out, ctype, Points, Cats);


	for (i = 0; i < Cats->n_cats; i++) {
	    int f, j;

	    f = -1;
	    for (j = 0; j < nfields; j++) {	/* find field */
		if (fields[j] == Cats->field[i]) {
		    f = j;
		    break;
		}
	    }
	    if (f > -1) {
		cats[f][ncats[f]] = Cats->cat[i];
		ncats[f]++;
	    }
	}
    }

    /* Copy tables */
    if (!(table_flag->answer)) {
	int ttype, ntabs = 0;
	struct field_info *IFi, *OFi;

	/* Number of output tabs */
	for (i = 0; i < Vect_get_num_dblinks(&In); i++) {
	    int f, j;

	    IFi = Vect_get_dblink(&In, i);

	    f = -1;
	    for (j = 0; j < nfields; j++) {	/* find field */
		if (fields[j] == IFi->number) {
		    f = j;
		    break;
		}
	    }
	    if (f > -1) {
		if (ncats[f] > 0)
		    ntabs++;
	    }
	}

	if (ntabs > 1)
	    ttype = GV_MTABLE;
	else
	    ttype = GV_1TABLE;

	for (i = 0; i < nfields; i++) {
	    int ret;

	    if (fields[i] == 0)
		continue;

	    G_message(_("Layer %d"), fields[i]);

	    /* Make a list of categories */
	    IFi = Vect_get_field(&In, fields[i]);
	    if (!IFi) {		/* no table */
		G_message(_("No table"));
		continue;
	    }

	    OFi =
		Vect_default_field_info(&Out, IFi->number, IFi->name, ttype);

	    ret =
		db_copy_table_by_ints(IFi->driver, IFi->database, IFi->table,
				      OFi->driver,
				      Vect_subst_var(OFi->database, &Out),
				      OFi->table, IFi->key, cats[i],
				      ncats[i]);

	    if (ret == DB_FAILED) {
		G_warning(_("Cannot copy table"));
	    }
	    else {
		Vect_map_add_dblink(&Out, OFi->number, OFi->name, OFi->table,
				    IFi->key, OFi->database, OFi->driver);
	    }
	}
    }


    Vect_close(&In);

    /* cleaning part 1: count errors */
    Vect_build_partial(&Out, GV_BUILD_CENTROIDS);
    err_boundaries = err_centr_out = err_centr_dupl = err_nocentr = 0;
    nlines = Vect_get_num_lines(&Out);
    for (line = 1; line <= nlines; line++) {

	if (!Vect_line_alive(&Out, line))
	    continue;

	type = Vect_get_line_type(&Out, line);
	if (type == GV_BOUNDARY) {
	    int left, right;

	    Vect_get_line_areas(&Out, line, &left, &right);

	    if (left == 0 || right == 0) {
		G_debug(3, "line = %d left = %d right = %d", line, 
			left, right);
		err_boundaries++;
	    }
	}
	if (type == GV_CENTROID) {
	    area = Vect_get_centroid_area(&Out, line);
	    if (area == 0)
		err_centr_out++;
	    else if (area < 0)
		err_centr_dupl++;
	}
    }

    err_nocentr = 0;
    nareas = Vect_get_num_areas(&Out);
    for (area = 1; area <= nareas; area++) {
	if (!Vect_area_alive(&Out, area))
	    continue;
	line = Vect_get_area_centroid(&Out, area);
	if (line == 0)
	    err_nocentr++;
    }

    /* cleaning part 2: snap */
    if (err_nocentr || err_centr_dupl || err_centr_out) {
	int nmod;

	G_important_message(_("Output needs topological cleaning"));
	Vect_snap_lines(&Out, GV_BOUNDARY, 1e-7, NULL);
	do {
	    Vect_break_lines(&Out, GV_BOUNDARY, NULL);
	    Vect_remove_duplicates(&Out, GV_BOUNDARY, NULL);
	    nmod =
		Vect_clean_small_angles_at_nodes(&Out, GV_BOUNDARY, NULL);
	} while (nmod > 0);

	err_boundaries = 0;
	nlines = Vect_get_num_lines(&Out);
	for (line = 1; line <= nlines; line++) {

	    if (!Vect_line_alive(&Out, line))
		continue;

	    type = Vect_get_line_type(&Out, line);
	    if (type == GV_BOUNDARY) {
		int left, right;

		Vect_get_line_areas(&Out, line, &left, &right);

		if (left == 0 || right == 0) {
		    G_debug(3, "line = %d left = %d right = %d", line, 
			    left, right);
		    err_boundaries++;
		}
	    }
	}
    }
    /* cleaning part 3: remove remaining incorrect boundaries */
    if (err_boundaries) {
	G_important_message(_("Removing incorrect boundaries from output"));
	nlines = Vect_get_num_lines(&Out);
	for (line = 1; line <= nlines; line++) {

	    if (!Vect_line_alive(&Out, line))
		continue;

	    type = Vect_get_line_type(&Out, line);
	    if (type == GV_BOUNDARY) {
		int left, right;

		Vect_get_line_areas(&Out, line, &left, &right);

		/* &&, not ||, no typo */
		if (left == 0 && right == 0) {
		    G_debug(3, "line = %d left = %d right = %d", line, 
			    left, right);
		    Vect_delete_line(&Out, line);
		}
	    }
	}
    }

    /* build clean topology */
    Vect_build_partial(&Out, GV_BUILD_NONE);
    Vect_build(&Out);
    Vect_close(&Out);

    G_done_msg(" ");
    exit(EXIT_SUCCESS);
}
Exemplo n.º 8
0
        void operator()(const PRIMITIVE& _ring, STRATEGY, output_type& _medial_axis)
        {
          auto&& _voronoi = voronoi(_ring);
          _medial_axis = detail::medial_axis_remove_segments_from_voronoi(_ring,_voronoi);
//          _medial_axis = voronoi(_ring);
        }