-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathvoronoi.c
137 lines (111 loc) · 3.36 KB
/
voronoi.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/* voronoi.c - */
// Example code for "Efficient Generatino of Poisson-Disk Sampling
// Patterns," Thouis R. Jones, JGT vol. 11, No. 2, pp. 27-36
//
// Copyright 2004-2006, Thouis R. Jones
// This code is distributed under the terms of the LGPL.
#include <math.h>
#include <stdlib.h>
#include "gts.h"
#include "voronoi.h"
#include "clip.h"
#include "triangle_area.h"
#define printf
#define g_assert(x)
GSList *create_voronoi_region(GtsVertex *v)
{
GSList *seg;
GSList *out_tris = NULL;
seg = v->segments;
while (seg) {
GtsTriangle *tri1 = GTS_TRIANGLE(GTS_EDGE(seg->data)->triangles->data);
GtsTriangle *tri2 = GTS_TRIANGLE(GTS_EDGE(seg->data)->triangles->next->data);
GtsPoint *p1, *p2;
g_assert(tri1);
g_assert(tri2);
p1 = gts_triangle_circumcircle_center(tri1, gts_point_class());
p2 = gts_triangle_circumcircle_center(tri2, gts_point_class());
out_tris = clip(out_tris, GTS_POINT(v), p1, p2);
gts_object_destroy(GTS_OBJECT(p1));
gts_object_destroy(GTS_OBJECT(p2));
seg = seg->next;
}
return (out_tris);
}
static int same_point(GtsVertex *v1, GtsVertex *v2)
{
return ((GTS_POINT(v1)->x == GTS_POINT(v2)->x) &&
(GTS_POINT(v1)->y == GTS_POINT(v2)->y));
}
int one_vertex_is(GtsTriangle *t, GtsVertex *v)
{
GtsVertex *v1, *v2, *v3;
gts_triangle_vertices(t, &v1, &v2, &v3);
return (same_point(v, v1) ||
same_point(v, v2) ||
same_point(v, v3));
}
double compute_voronoi_area(GtsVertex *v, double exclude_radius)
{
double area = 0.0;
GSList *voronoi_region = create_voronoi_region(v);
GSList *l;
l = voronoi_region;
while (l) {
g_assert(one_vertex_is(GTS_TRIANGLE(l->data), v));
area += triangle_area_exclude(GTS_TRIANGLE(l->data), v, exclude_radius);
gts_object_destroy(GTS_OBJECT(l->data));
l = l->next;
}
g_slist_free(voronoi_region);
return (area);
}
GtsVertex *random_point_in_triangle(GtsTriangle *t)
{
double a1 = drand48(), a2 = drand48();
GtsVertex *v1, *v2, *v3;
while ((a1 + a2) > 1.0) {
a1 = drand48(); a2 = drand48();
}
gts_triangle_vertices(t, &v1, &v2, &v3);
return (gts_vertex_new(gts_vertex_class(),
a1 * GTS_POINT(v1)->x + a2 * GTS_POINT(v2)->x + (1 - a1 - a2) * GTS_POINT(v3)->x,
a1 * GTS_POINT(v1)->y + a2 * GTS_POINT(v2)->y + (1 - a1 - a2) * GTS_POINT(v3)->y,
0.0));
}
GtsVertex *new_vertex_in_voronoi(GtsVertex *v, double exclude_radius)
{
double area = 0.0;
GtsVertex *vout = NULL;
GSList *voronoi_region = create_voronoi_region(v);
GSList *l;
GtsTriangle *last_positive = NULL;
l = voronoi_region;
while (l) {
double tarea = triangle_area_exclude(GTS_TRIANGLE(l->data), v, exclude_radius);
area += tarea;
if (tarea > 0.0) {
last_positive = GTS_TRIANGLE(l->data);
}
l = l->next;
}
g_assert(last_positive != NULL);
area *= drand48();
l = voronoi_region;
while (l && (area > 0.0)) {
area -= triangle_area_exclude(GTS_TRIANGLE(l->data), v, exclude_radius);
if (area <= 0.0) {
vout = random_point_in_triangle_exclude(GTS_TRIANGLE(l->data), v, exclude_radius);
}
l = l->next;
}
if (! vout) {
vout = random_point_in_triangle_exclude(last_positive, v, exclude_radius);
}
while (l) {
gts_object_destroy(GTS_OBJECT(l->data));
l = l->next;
}
g_slist_free(voronoi_region);
return (vout);
}