Skip to content

Commit

Permalink
final commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mariabrbz committed Jun 15, 2020
1 parent 7a8aa57 commit bf64c4a
Show file tree
Hide file tree
Showing 8 changed files with 674 additions and 3 deletions.
Binary file not shown.
Binary file added Geometry processing/Images/100points_weighted.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,16 @@ class Polygon {
};

// saves a static svg file. The polygon vertices are supposed to be in the range [0..1], and a canvas of size 1000x1000 is created
void save_svg(const vector<Polygon> &polygons, string filename, string fillcol = "none") {
void save_svg(const vector<Polygon> &polygons, const Polygon &vertices, string filename, string fillcol = "none") {
FILE* f = fopen(filename.c_str(), "w+");
fprintf(f, "<svg xmlns = \"http://www.w3.org/2000/svg\" width = \"1000\" height = \"1000\">\n");

for (int i = 0; i < vertices.vertices.size(); i++){
fprintf(f, "<g>\n");
fprintf(f, "<circle cx = \"%3.3f\" cy = \"%3.3f\" r=\"2\"/>", (vertices.vertices[i][0] * 1000), (1000 - vertices.vertices[i][1] * 1000));
fprintf(f, "</g>\n");
}

for (int i=0; i<polygons.size(); i++) {
fprintf(f, "<g>\n");
fprintf(f, "<polygon points = \"");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ using namespace std;

static default_random_engine engine(10);
static uniform_real_distribution<double> uniform(0, 1);
static uniform_real_distribution<double> uniform_modified(0, log(100)/100);

vector<Vector> random_pointcloud(int n) {
vector<Vector> point_cloud(n);
Expand All @@ -25,11 +26,30 @@ Vector intersect(Vector A, Vector B, Vector u, Vector v) {
return P;
}

Vector intersect_weighted(Vector A, Vector B, Vector u, Vector v, double wu, double wv) {
Vector M = (u + v) / 2;
M += ((wu - wv) / (2 * sq_norm(u - v))) * (v - u);
double t = dot(M - A, u - v) / dot(B - A, u - v);
Vector P(0., 0., 0.);

if (t >= 0 && t <= 1) {
P = A + t * (B - A);
}

return P;
}

bool inside(Vector P, Vector u, Vector v) {
Vector M = (u + v) / 2;
return (dot(P - M, v - u) < 0);
}

bool inside_weighted(Vector P, Vector u, Vector v, double wu, double wv) {
Vector M = (u + v) / 2;
M += ((wu - wv) / (2 * sq_norm(u - v))) * (v - u);
return (dot(P - M, v - u) < 0);
}

Polygon sutherland_hodgman(Polygon subject, Polygon clip) {
Polygon* out;
for (int i = 0; i < clip.vertices.size() - 1; i++) {
Expand Down Expand Up @@ -97,12 +117,54 @@ vector<Polygon> voronoi(Polygon subject, Polygon clip) {
return res;
}

vector<Polygon> voronoi_weighted(Polygon subject, Polygon clip, vector<double> weights) {
vector<Polygon> res;

#pragma omp parallel for
for (int i = 0; i < subject.vertices.size(); i++) {
Polygon current = clip;

#pragma omp parallel for
for (int j = 0; j < subject.vertices.size(); j++) {
if (i != j) {
Polygon out;

#pragma omp parallel for
for (int k = 0; k < current.vertices.size(); k++) {
Vector current_vertex = current.vertices[k];
Vector previous_vertex = current.vertices[(k > 0) ? (k - 1) : current.vertices.size() - 1];
Vector intersection = intersect_weighted(previous_vertex, current_vertex, subject.vertices[i], subject.vertices[j], weights[i], weights[j]);

if (inside_weighted(current_vertex, subject.vertices[i], subject.vertices[j], weights[i], weights[j])) {
if(!inside_weighted(previous_vertex, subject.vertices[i], subject.vertices[j], weights[i], weights[j])) {
out.vertices.push_back(intersection);
}
out.vertices.push_back(current_vertex);
}

else if (inside_weighted(previous_vertex, subject.vertices[i], subject.vertices[j], weights[i], weights[j])) {
out.vertices.push_back(intersection);
}
}
current = out;
}
}
res.push_back(current);
}
return res;
}

int main() {
vector<Vector> point_cloud = random_pointcloud(10000);
int n = 100;
vector<Vector> point_cloud = random_pointcloud(n);
vector<double> weights(n);
for (int i = 0; i < n; i++) {
weights[i] = uniform_modified(engine);
}
vector<Vector> rectangle{Vector(0, 0, 0), Vector(0, 1, 0), Vector(1, 1, 0), Vector(1, 0, 0)};
Polygon subject(point_cloud);
Polygon clip(rectangle);

save_svg(voronoi(subject, clip), "10kpoints.svg") ;
save_svg(voronoi_weighted(subject, clip, weights), subject, "100points_weighted.svg") ;
return 0;
}
File renamed without changes
Loading

0 comments on commit bf64c4a

Please sign in to comment.