File size: 9,423 Bytes
5b89cb9 | 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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 | #include <igl/avg_edge_length.h>
#include <igl/barycenter.h>
#include <igl/comb_cross_field.h>
#include <igl/comb_frame_field.h>
#include <igl/compute_frame_field_bisectors.h>
#include <igl/cross_field_mismatch.h>
#include <igl/cut_mesh_from_singularities.h>
#include <igl/find_cross_field_singularities.h>
#include <igl/local_basis.h>
#include <igl/readOFF.h>
#include <igl/rotate_vectors.h>
#include <igl/copyleft/comiso/miq.h>
#include <igl/copyleft/comiso/nrosy.h>
#include <igl/opengl/glfw/Viewer.h>
#include <igl/PI.h>
#include <sstream>
#include <igl/serialize.h>
// Input mesh
Eigen::MatrixXd V;
Eigen::MatrixXi F;
// Face barycenters
Eigen::MatrixXd B;
// Scale for visualizing the fields
double global_scale;
bool extend_arrows = false;
// Cross field
Eigen::MatrixXd X1,X2;
// Bisector field
Eigen::MatrixXd BIS1, BIS2;
// Combed bisector
Eigen::MatrixXd BIS1_combed, BIS2_combed;
// Per-corner, integer mismatches
Eigen::Matrix<int, Eigen::Dynamic, 3> MMatch;
// Field singularities
Eigen::Matrix<int, Eigen::Dynamic, 1> isSingularity, singularityIndex;
// Per corner seams
Eigen::Matrix<int, Eigen::Dynamic, 3> Seams;
// Combed field
Eigen::MatrixXd X1_combed, X2_combed;
// Global parametrization (with seams)
Eigen::MatrixXd UV_seams;
Eigen::MatrixXi FUV_seams;
// Global parametrization
Eigen::MatrixXd UV;
Eigen::MatrixXi FUV;
// Create a texture that hides the integer translation in the parametrization
void line_texture(Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic> &texture_R,
Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic> &texture_G,
Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic> &texture_B)
{
unsigned size = 128;
unsigned size2 = size/2;
unsigned lineWidth = 3;
texture_R.setConstant(size, size, 255);
for (unsigned i=0; i<size; ++i)
for (unsigned j=size2-lineWidth; j<=size2+lineWidth; ++j)
texture_R(i,j) = 0;
for (unsigned i=size2-lineWidth; i<=size2+lineWidth; ++i)
for (unsigned j=0; j<size; ++j)
texture_R(i,j) = 0;
texture_G = texture_R;
texture_B = texture_R;
}
bool key_down(igl::opengl::glfw::Viewer& viewer, unsigned char key, int modifier)
{
if (key == 'E')
{
extend_arrows = !extend_arrows;
}
if (key <'1' || key >'8')
return false;
viewer.data().clear();
viewer.data().show_lines = false;
viewer.data().show_texture = false;
if (key == '1')
{
// Cross field
viewer.data().set_mesh(V, F);
viewer.data().add_edges(extend_arrows ? B - global_scale*X1 : B, B + global_scale*X1 ,Eigen::RowVector3d(1,0,0));
viewer.data().add_edges(extend_arrows ? B - global_scale*X2 : B, B + global_scale*X2 ,Eigen::RowVector3d(0,0,1));
}
if (key == '2')
{
// Bisector field
viewer.data().set_mesh(V, F);
viewer.data().add_edges(extend_arrows ? B - global_scale*BIS1 : B, B + global_scale*BIS1 ,Eigen::RowVector3d(1,0,0));
viewer.data().add_edges(extend_arrows ? B - global_scale*BIS2 : B, B + global_scale*BIS2 ,Eigen::RowVector3d(0,0,1));
}
if (key == '3')
{
// Bisector field combed
viewer.data().set_mesh(V, F);
viewer.data().add_edges(extend_arrows ? B - global_scale*BIS1_combed : B, B + global_scale*BIS1_combed ,Eigen::RowVector3d(1,0,0));
viewer.data().add_edges(extend_arrows ? B - global_scale*BIS2_combed : B, B + global_scale*BIS2_combed ,Eigen::RowVector3d(0,0,1));
}
if (key == '4')
{
// Singularities and cuts
viewer.data().set_mesh(V, F);
// Plot cuts
int l_count = Seams.sum();
Eigen::MatrixXd P1(l_count,3);
Eigen::MatrixXd P2(l_count,3);
for (unsigned i=0; i<Seams.rows(); ++i)
{
for (unsigned j=0; j<Seams.cols(); ++j)
{
if (Seams(i,j) != 0)
{
P1.row(l_count-1) = V.row(F(i,j));
P2.row(l_count-1) = V.row(F(i,(j+1)%3));
l_count--;
}
}
}
viewer.data().add_edges(P1, P2, Eigen::RowVector3d(1, 0, 0));
// Plot the singularities as colored dots (red for negative, blue for positive)
for (unsigned i=0; i<singularityIndex.size();++i)
{
if (singularityIndex(i) < 2 && singularityIndex(i) > 0)
viewer.data().add_points(V.row(i),Eigen::RowVector3d(1,0,0));
else if (singularityIndex(i) > 2)
viewer.data().add_points(V.row(i),Eigen::RowVector3d(0,1,0));
}
}
if (key == '5')
{
// Singularities and cuts, original field
// Singularities and cuts
viewer.data().set_mesh(V, F);
viewer.data().add_edges(extend_arrows ? B - global_scale*X1_combed : B, B + global_scale*X1_combed ,Eigen::RowVector3d(1,0,0));
viewer.data().add_edges(extend_arrows ? B - global_scale*X2_combed : B, B + global_scale*X2_combed ,Eigen::RowVector3d(0,0,1));
// Plot cuts
int l_count = Seams.sum();
Eigen::MatrixXd P1(l_count,3);
Eigen::MatrixXd P2(l_count,3);
for (unsigned i=0; i<Seams.rows(); ++i)
{
for (unsigned j=0; j<Seams.cols(); ++j)
{
if (Seams(i,j) != 0)
{
P1.row(l_count-1) = V.row(F(i,j));
P2.row(l_count-1) = V.row(F(i,(j+1)%3));
l_count--;
}
}
}
viewer.data().add_edges(P1, P2, Eigen::RowVector3d(1, 0, 0));
// Plot the singularities as colored dots (red for negative, blue for positive)
for (unsigned i=0; i<singularityIndex.size();++i)
{
if (singularityIndex(i) < 2 && singularityIndex(i) > 0)
viewer.data().add_points(V.row(i),Eigen::RowVector3d(1,0,0));
else if (singularityIndex(i) > 2)
viewer.data().add_points(V.row(i),Eigen::RowVector3d(0,1,0));
}
}
if (key == '6')
{
// Global parametrization UV
viewer.data().set_mesh(UV, FUV);
viewer.data().set_uv(UV);
viewer.data().show_lines = true;
}
if (key == '7')
{
// Global parametrization in 3D
viewer.data().set_mesh(V, F);
viewer.data().set_uv(UV,FUV);
viewer.data().show_texture = true;
}
if (key == '8')
{
// Global parametrization in 3D with seams
viewer.data().set_mesh(V, F);
viewer.data().set_uv(UV_seams,FUV_seams);
viewer.data().show_texture = true;
}
viewer.data().set_colors(Eigen::RowVector3d(1,1,1));
// Replace the standard texture with an integer shift invariant texture
Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic> texture_R, texture_G, texture_B;
line_texture(texture_R, texture_G, texture_B);
viewer.data().set_texture(texture_R, texture_B, texture_G);
viewer.core().align_camera_center(viewer.data().V,viewer.data().F);
return false;
}
int main(int argc, char *argv[])
{
using namespace Eigen;
// Load a mesh in OFF format
igl::readOFF(TUTORIAL_SHARED_PATH "/3holes.off", V, F);
double gradient_size = 50;
double iter = 0;
double stiffness = 5.0;
bool direct_round = 0;
// Compute face barycenters
igl::barycenter(V, F, B);
// Compute scale for visualizing fields
global_scale = .5*igl::avg_edge_length(V, F);
// Contrain one face
VectorXi b(1);
b << 0;
MatrixXd bc(1, 3);
bc << 1, 0, 0;
// Create a smooth 4-RoSy field
VectorXd S;
igl::copyleft::comiso::nrosy(V, F, b, bc, VectorXi(), VectorXd(), MatrixXd(), 4, 0.5, X1, S);
// Find the orthogonal vector
MatrixXd B1, B2, B3;
igl::local_basis(V, F, B1, B2, B3);
X2 = igl::rotate_vectors(X1, VectorXd::Constant(1, igl::PI / 2), B1, B2);
// Always work on the bisectors, it is more general
igl::compute_frame_field_bisectors(V, F, X1, X2, BIS1, BIS2);
// Comb the field, implicitly defining the seams
igl::comb_cross_field(V, F, BIS1, BIS2, BIS1_combed, BIS2_combed);
// Find the integer mismatches
igl::cross_field_mismatch(V, F, BIS1_combed, BIS2_combed, true, MMatch);
// Find the singularities
igl::find_cross_field_singularities(V, F, MMatch, isSingularity, singularityIndex);
// Cut the mesh, duplicating all vertices on the seams
igl::cut_mesh_from_singularities(V, F, MMatch, Seams);
// Comb the frame-field accordingly
igl::comb_frame_field(V, F, X1, X2, BIS1_combed, BIS2_combed, X1_combed, X2_combed);
// Global parametrization
igl::copyleft::comiso::miq(V,
F,
X1_combed,
X2_combed,
MMatch,
isSingularity,
Seams,
UV,
FUV,
gradient_size,
stiffness,
direct_round,
iter,
5,
true);
// Global parametrization (with seams, only for demonstration)
igl::copyleft::comiso::miq(V,
F,
X1_combed,
X2_combed,
MMatch,
isSingularity,
Seams,
UV_seams,
FUV_seams,
gradient_size,
stiffness,
direct_round,
iter,
5,
false);
// Plot the mesh
igl::opengl::glfw::Viewer viewer;
// Plot the original mesh with a texture parametrization
key_down(viewer,'7',0);
// Launch the viewer
viewer.callback_key_down = &key_down;
viewer.launch();
}
|