Skip to content

Commit bf61753

Browse files
committed
show proxy + L2 + perf
1 parent 4fbe219 commit bf61753

8 files changed

+135
-27
lines changed

anchors.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ bool vector_contains(vector<int> v, int key) {
44
return count(v.begin(), v.end(), key);
55
}
66

7+
bool vector_contains(vector<double> v, double key) {
8+
return count(v.begin(), v.end(), key);
9+
}
710

811

912
int find_first_edge(HalfedgeDS he, int v, int r, MatrixXi R) {

anchors.h

+2
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,7 @@ using namespace Eigen;
1111
using namespace std;
1212

1313
bool vector_contains(vector<int> v, int key);
14+
bool vector_contains(vector<double> v, double key);
15+
1416
vector<vector<int>> anchor_points(HalfedgeDS he, MatrixXi R, MatrixXd V, MatrixXd Proxies, double treshold);
1517
vector<int> find_vertex_proxies(HalfedgeDS he, int v, MatrixXi R);

distance.cpp

+40-2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,24 @@ double distance_L_2(Vector3i T, Vector3d X, Vector3d N, MatrixXd V){
5151

5252
return (1./6.)*area*(d1*d1 + d2*d2 + d3*d3 + d1*d2 + d1*d3 + d2*d3);
5353

54+
};
55+
double distance_L_2_s(Vector3i T, Vector3d X, Vector3d N, MatrixXd V){
56+
57+
Vector3d v1 = V.row(T(0));
58+
Vector3d v2 = V.row(T(1));
59+
Vector3d v3 = V.row(T(2));
60+
61+
double l1 = (v2-v1).norm();
62+
double l2 = (v3-v2).norm();
63+
double l3 = (v1-v3).norm();
64+
double p = (l1+l2+l3)/2.;
65+
double area = p*(p-l1)*(p-l2)*(p-l3);
66+
double d1 = orthogonal_distance(X,N,v1);
67+
double d2 = orthogonal_distance(X,N,v2);
68+
double d3 = orthogonal_distance(X,N,v3);
69+
double x = (1./6.)*(d1*d1 + d2*d2 + d3*d3 + d1*d2 + d1*d3 + d2*d3);
70+
return area*x*x;
71+
5472
};
5573

5674
double distance_L_2_1(Vector3i T, Vector3d N, MatrixXd V){
@@ -65,15 +83,35 @@ double distance_L_2_1(Vector3i T, Vector3d N, MatrixXd V){
6583

6684
};
6785

86+
double distance_L_2_1_s(Vector3i T, Vector3d N, MatrixXd V){
87+
Vector3d v1 = V.row(T(0));
88+
Vector3d v2 = V.row(T(1));
89+
Vector3d v3 = V.row(T(2));
90+
double l1 = (v2-v1).norm();
91+
double l2 = (v3-v2).norm();
92+
double l3 = (v1-v3).norm();
93+
double p = (l1+l2+l3)/2.;
94+
double area = p*(p-l1)*(p-l2)*(p-l3);
95+
double x = ((v2-v1).cross(v3-v1).normalized()-N).norm();
96+
return area*x*x*x*x;
97+
};
98+
6899
double distance(Vector3i T, Vector3d X, Vector3d N, MatrixXd V, int norme){
69100
if (norme == 0){
70101
return distance_L_2(T,X,N,V);
71102
}
72-
else if (norme == 1){
103+
else {
73104
return distance_L_2_1(T,N,V);
74105
}
106+
};
107+
108+
109+
double distance_squared(Vector3i T, Vector3d X, Vector3d N, MatrixXd V, int norme){
110+
if (norme == 0){
111+
return distance_L_2_s(T,X,N,V);
112+
}
75113
else {
76-
cout<<"wrong norme parameter"<<endl;
114+
return distance_L_2_1_s(T,N,V);
77115
}
78116
};
79117

distance.h

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Vector3d triangle_center(Vector3i T, MatrixXd V);
1313
double distance_L_2(Vector3i T, Vector3d X, Vector3d N, MatrixXd V);
1414
double distance_L_2_1(Vector3i T, Vector3d N, MatrixXd V);
1515
double distance(Vector3i T, Vector3d X, Vector3d N, MatrixXd V, int norme);
16+
double distance_squared(Vector3i T, Vector3d X, Vector3d N, MatrixXd V, int norme);
1617

1718
double global_distortion_error(MatrixXi R, MatrixXd Proxies, MatrixXd V, MatrixXi F, int norme);
1819

main.cpp

+44-13
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,27 @@ void draw_anchors(igl::opengl::glfw::Viewer &viewer) {
6767
}
6868
}
6969

70+
}
71+
void triangle_proxy(Vector3d x, Vector3d n, MatrixXd& newV, int k) {
72+
Vector3d m1(-n(1), n(0),0);
73+
m1.normalize();
74+
Vector3d m2 = n.cross(m1);
75+
newV.row(3*k) = x;
76+
newV.row(3*k+1) = x+m1/100.0;
77+
newV.row(3*k+2) = x+m2/100.0;
78+
}
79+
void draw_prox(igl::opengl::glfw::Viewer &viewer) {
80+
MatrixXd newV(3*p,3);
81+
MatrixXi newF(p,3);
82+
83+
vector<vector<int>> anchors = anchor_points(*he, R, V, Proxies,treshold);
84+
for(int i = 0; i < p; i++) {
85+
triangle_proxy(Proxies.row(i),Proxies.row(i+p), newV, i);
86+
newF.row(i) << 3*i, 3*i+1, 3*i+2;
87+
}
88+
viewer.data().clear();
89+
viewer.data().set_mesh(newV, newF);
90+
7091
}
7192

7293
bool key_down(igl::opengl::glfw::Viewer &viewer, unsigned char key, int modifier) {
@@ -80,10 +101,9 @@ bool key_down(igl::opengl::glfw::Viewer &viewer, unsigned char key, int modifier
80101
}
81102
if (key=='3') {
82103
// debug_regions_vides(R,p);
83-
proxy_color(R, Proxies, V, F, Ad, norme);
104+
double error = proxy_color(R, Proxies, V, F, Ad, norme);
84105
Proxies = new_proxies(R, F, V, p, norme);
85106
iterations += 1;
86-
double error = global_distortion_error(R,Proxies,V,F,norme);
87107
cout<<"Global Error : "<<error<<endl;
88108
global_error_points.push_back(make_pair(iterations,error));
89109

@@ -123,21 +143,31 @@ bool key_down(igl::opengl::glfw::Viewer &viewer, unsigned char key, int modifier
123143
igl::jet(nR,true,C);
124144
viewer.data().set_colors(C);
125145
}
146+
if (key=='7') {
147+
draw_prox(viewer);
148+
}
126149
if (key == 'S' || (unsigned int)key == 83){
150+
vector<double> errors;
127151
while (fabs(error - precedent_error)>0.0001){
152+
precedent_error = error;
153+
error = proxy_color(R, Proxies, V, F, Ad, norme);
154+
Proxies = new_proxies(R, F, V, p, norme);
155+
iterations += 1;
156+
cout << error << endl;
128157

129-
proxy_color(R, Proxies, V, F, Ad, norme);
130-
Proxies = new_proxies(R, F, V, p, norme);
131-
iterations += 1;
132-
precedent_error = error;
133-
error = global_distortion_error(R,Proxies,V,F,norme);
134-
cout << error << endl;
135-
global_error_points.push_back(make_pair(iterations,error));
136-
137-
igl::jet(R,true,C);
138-
viewer.data(0).set_colors(C);
158+
if (vector_contains(errors,error)){
159+
cout<<"cycle !"<<endl;
160+
break;
161+
}
162+
163+
// error = global_distortion_error(R,Proxies,V,F,norme);
164+
global_error_points.push_back(make_pair(iterations,error));
165+
errors.push_back(error);
139166

167+
igl::jet(R,true,C);
168+
viewer.data(0).set_colors(C);
140169
}
170+
cout << " Done" <<endl;
141171

142172
}
143173
return false;
@@ -190,12 +220,13 @@ int main(int argc, char *argv[])
190220

191221
// coloring distance
192222
// MatrixXd Cf;
193-
// distance_color(Cf,F,V);
223+
// distance_color(Cf,F,V,0);
194224
// MatrixXd C;
195225
// igl::jet(Cf,true,C);
196226

197227
// coloring proxies
198228
if (argc>=5) {
229+
norme = 0;
199230
initial_partition(p, R, V, F, Ad, norme);
200231
cout << "uniform init" <<endl;
201232
}

partitioning.cpp

+9-5
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ int find_triangles_region(vector<int> Triangles, MatrixXi &R, MatrixXd V, Matrix
109109
for (int k=0;k<3;k++) {
110110
int tri = Ad(Triangles[i],k);
111111
double d = distance(F.row(tri), Proxies_center[i], Proxies_normal[i], V, norme);
112-
q.push(make_pair(1.0-d, tri+m*(i)));
112+
q.push(make_pair(-d, tri+m*(i)));
113113
if (d>furthest_distance(i) && !vector_contains(Triangles,tri)) {
114114
furthest_distance(i)=d;
115115
furthest_triangle(i)=tri;
@@ -135,7 +135,7 @@ int find_triangles_region(vector<int> Triangles, MatrixXi &R, MatrixXd V, Matrix
135135
for (int k=0;k<3;k++) {
136136
int tri = Ad(face,k);
137137
double d = distance(F.row(tri), Proxies_center[prox], Proxies_normal[prox], V, norme);
138-
q.push(make_pair(1.0-d, tri+m*prox));
138+
q.push(make_pair(-d, tri+m*prox));
139139
if (d>furthest_distance(prox) && !vector_contains(Triangles,tri)) {
140140
furthest_distance(prox)=d;
141141
furthest_triangle(prox)=tri;
@@ -194,8 +194,9 @@ VectorXi find_best_triangles(MatrixXi R, MatrixXd Proxies, MatrixXd V, MatrixXi
194194
}
195195

196196

197-
void proxy_color(MatrixXi &R, MatrixXd Proxies, MatrixXd V, MatrixXi F, MatrixXi Ad, int norme) {
197+
double proxy_color(MatrixXi &R, MatrixXd Proxies, MatrixXd V, MatrixXi F, MatrixXi Ad, int norme) {
198198
int m=F.rows();
199+
double error=0;
199200
priority_queue<pair<double, int>> q; // distance, proxy
200201

201202
// initialize proxies
@@ -210,10 +211,11 @@ void proxy_color(MatrixXi &R, MatrixXd Proxies, MatrixXd V, MatrixXi F, MatrixXi
210211
Proxies_center[i] = Proxies.row(i);
211212
Proxies_normal[i] = Proxies.row(i+p);
212213
R(triangles(i)) = i;
214+
error += distance(F.row(triangles(i)), Proxies_center[i], Proxies_normal[i], V, norme);
213215
for (int k=0;k<3;k++) {
214216
int tri = Ad(triangles(i),k);
215217
double d = distance(F.row(tri), Proxies_center[i], Proxies_normal[i], V, norme);
216-
q.push(make_pair(1.0-d, tri+m*(i)));
218+
q.push(make_pair(-d, tri+m*(i)));
217219
}
218220
}
219221

@@ -224,6 +226,7 @@ void proxy_color(MatrixXi &R, MatrixXd Proxies, MatrixXd V, MatrixXi F, MatrixXi
224226
while (q.size()!=0) {
225227
item = q.top();
226228
q.pop();
229+
error += - item.first;
227230
prox = item.second/m;
228231
face = item.second%m;
229232

@@ -233,9 +236,10 @@ void proxy_color(MatrixXi &R, MatrixXd Proxies, MatrixXd V, MatrixXi F, MatrixXi
233236
for (int k=0;k<3;k++) {
234237
int tri = Ad(face,k);
235238
double d = distance(F.row(tri), Proxies_center[prox], Proxies_normal[prox], V, norme);
236-
q.push(make_pair(1.0-d, tri+m*prox));
239+
q.push(make_pair(-d, tri+m*prox));
237240
}
238241
}
239242
}
243+
return error;
240244
}
241245

partitioning.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ void distance_color(MatrixXd &Cf, MatrixXi F, MatrixXd V, int norme);
1414
void initial_partition(int p, MatrixXi &R, MatrixXd V, MatrixXi F, MatrixXi Ad, int norme);
1515
void initial_partition2(int p, MatrixXi &R, MatrixXd V, MatrixXi F, MatrixXi Ad, int norme);
1616
VectorXi find_best_triangles(MatrixXi R, MatrixXd Proxies, MatrixXd V, MatrixXi F, int norme);
17-
void proxy_color(MatrixXi &R, MatrixXd Proxies, MatrixXd V, MatrixXi F, MatrixXi Ad, int norme);
17+
double proxy_color(MatrixXi &R, MatrixXd Proxies, MatrixXd V, MatrixXi F, MatrixXi Ad, int norme);
1818

1919

2020

proxies.cpp

+35-6
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,8 @@ Vector3d new_Ni_L_2 (MatrixXi R, int i, MatrixXi F, MatrixXd V){
8484
s = triangle_area(v1,v2,v3);
8585

8686
Ci += (2./72.)*s*MT*A*MT.transpose() + s*gT*gT.transpose();
87-
88-
}
89-
w += s;
87+
w += s;
88+
}
9089
}
9190

9291
Ci = Ci - w*Xi*Xi.transpose();
@@ -115,6 +114,37 @@ Vector3d new_Xi_L_2_1 (MatrixXi R, int i, MatrixXi F, MatrixXd V){
115114

116115
};
117116

117+
118+
MatrixXd compute_N (MatrixXi R, MatrixXi F, MatrixXd V, int p) {
119+
MatrixXd N;
120+
N.setZero(p,3);
121+
122+
Vector3i T;
123+
Vector3d v1;
124+
Vector3d v2;
125+
Vector3d v3;
126+
double s;
127+
Vector3d nT;
128+
129+
for (int f=0 ; f<R.rows() ; f++){
130+
//we only add the triangles that belong to the region i
131+
int i = R(f,0);
132+
133+
T = F.row(f);
134+
v1 = V.row(T(0));
135+
v2 = V.row(T(1));
136+
v3 = V.row(T(2));
137+
138+
s = triangle_area(v1,v2,v3);
139+
nT = triangle_normal(v1,v2,v3);
140+
141+
N.row(i) += s*nT;
142+
}
143+
144+
return N;
145+
}
146+
147+
118148
Vector3d new_Ni_L_2_1 (MatrixXi R, int i, MatrixXi F, MatrixXd V){
119149

120150
Vector3d Ni(0.,0.,0.);
@@ -170,13 +200,12 @@ MatrixXd new_proxies_L_2_1(MatrixXi R, MatrixXi F, MatrixXd V, int k){
170200
MatrixXd P(2*k,3);
171201

172202
Vector3d Xi;
173-
Vector3d Ni;
203+
MatrixXd N = compute_N(R,F,V,k);
174204

175205
for (int i=0 ; i<k ; i++){
176206
Xi = new_Xi_L_2_1(R,i,F,V);
177-
Ni = new_Ni_L_2_1(R,i,F,V);
178207
P.row(i) = Xi;
179-
P.row(k+i) = Ni.normalized();
208+
P.row(k+i) = N.row(i).normalized();
180209
}
181210

182211
return P;

0 commit comments

Comments
 (0)