-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVoronoi.cpp
More file actions
127 lines (120 loc) · 4.77 KB
/
Voronoi.cpp
File metadata and controls
127 lines (120 loc) · 4.77 KB
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
//
// Created by bpiranda on 10/12/2019.
//
#include "Voronoi.h"
#include <map>
#include <iostream>
#include <algorithm>
#include <glutWindow.h>
const array<float,4> tabColors[6]={{1.0f,0.0,0.0,0.5f},{1.0f,0.27f,0.0,0.5f},{1.0f,1.0f,0.0,0.5f},{0.0f,1.0f,0.0,0.5f},
{0.0f,1.0f,1.0,0.5f},{0.0f,0.0f,1.0,0.5f}};
Voronoi::Voronoi(const Mesh &mesh) {
MyPolygon *poly;
auto m_vert = mesh.vertices.begin();
int currentColor=0;
vector<const Triangle*> tabTri;
while (m_vert!=mesh.vertices.end()) { // for all vertices of the mesh
auto mt_it = mesh.triangles.begin();
tabTri.clear(); // tabTri: list of triangles containing m_vert
while (mt_it!=mesh.triangles.end()) {
if ((*mt_it).hasVertex(&(*m_vert))) {
tabTri.push_back(&(*mt_it));
cout << "Add triangle:" << (*mt_it).whoami(mesh.vertices) << endl;
}
mt_it++;
}
// find left border
auto first = tabTri.begin();
auto tt_it = tabTri.begin();
bool found=false;
while (tt_it!=tabTri.end() && !found) {
auto comp_it = tabTri.begin();
while (comp_it!=tabTri.end() && (*tt_it)->getNextVertex(&(*m_vert))!=(*comp_it)->getPrevVertex(&(*m_vert))) {
comp_it++;
}
if (comp_it==tabTri.end()) {
first=tt_it;
found=true;
}
tt_it++;
}
if (found) {
cout << "border found :" << (*first)->whoami(mesh.vertices) << endl;
} else {
cout << "no border" << endl;
}
// create polygon
poly = new MyPolygon(20);
polygons.push_back(poly);
poly->setColor(tabColors[currentColor]);
currentColor = (currentColor+1)%6;
tt_it=first;
if (found && GlutWindow::isInRect((*tt_it)->circumCenter.x,(*tt_it)->circumCenter.y,0,0,GlutWindow::getWindowWidth(),GlutWindow::getWindowHeight())) { // add a point for the left border
Vector2D V = (*first)->nextEdgeNormal(&(*m_vert));
cout << V << endl;
float k;
if (V.x > 0) { // (circumCenter+k V).x=width
k = (GlutWindow::getWindowWidth() - (*first)->circumCenter.x) / V.x;
} else {
k = (-(*first)->circumCenter.x) / V.x;
}
if (V.y > 0) { // (circumCenter+k V).x=width
k = std::min(k, (GlutWindow::getWindowHeight() - (*first)->circumCenter.y) / V.y);
} else {
k = std::min(k, (-(*first)->circumCenter.y) / V.y);
}
poly->addPoint(Vector2D((*first)->circumCenter + k * V));
Vector2D pt = (*first)->circumCenter + k * V;
cout << "newPoint=" << pt << endl;
}
auto comp_it = first;
do {
poly->addPoint(Vector2D((*tt_it)->circumCenter));
cout << "newCCPoint=" << (*tt_it)->circumCenter << endl;
// search triangle on right of tt_it
comp_it = tabTri.begin();
while (comp_it!=tabTri.end() && (*tt_it)->getPrevVertex(&(*m_vert))!=(*comp_it)->getNextVertex(&(*m_vert))) {
comp_it++;
}
if (comp_it!=tabTri.end()) tt_it = comp_it;
} while (tt_it!=first && comp_it!=tabTri.end());
if (found && GlutWindow::isInRect((*tt_it)->circumCenter.x,(*tt_it)->circumCenter.y,0,0,GlutWindow::getWindowWidth(),GlutWindow::getWindowHeight())) { // add a point for the right border
Vector2D V = (*tt_it)->previousEdgeNormal(&(*m_vert));
float k;
if (V.x > 0) { // (circumCenter+k V).x=width
k = (GlutWindow::getWindowWidth() - (*tt_it)->circumCenter.x) / V.x;
} else {
k = (-(*tt_it)->circumCenter.x) / V.x;
}
if (V.y > 0) { // (circumCenter+k V).y=height
k = std::min(k, (GlutWindow::getWindowHeight() - (*tt_it)->circumCenter.y) / V.y);
} else {
k = std::min(k, (-(*tt_it)->circumCenter.y) / V.y);
}
poly->addPoint(Vector2D((*tt_it)->circumCenter + k * V));
Vector2D pt = (*tt_it)->circumCenter + k * V;
cout << "newPoint=" << pt << endl;
}
poly->clip(0,0,GlutWindow::getWindowWidth(),GlutWindow::getWindowHeight());
cout << "Final -------------------" << endl;
poly->print();
m_vert++;
}
}
Voronoi::~Voronoi() {
// free polygons
}
void Voronoi::draw() {
auto p = polygons.begin();
while (p!=polygons.end()) {
(*p)->draw();
p++;
}
}
MyPolygon* Voronoi::findPolygon(const Vector2D &pos) {
auto p = polygons.begin();
while (p!=polygons.end() && !(*p)->isInsideTriangles(pos.x,pos.y)) {
p++;
}
return (p!=polygons.end())?*p: nullptr;
}