Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion src/cotmatrix.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,37 @@
#include "cotmatrix.h"
#include <iostream>

typedef Eigen::Triplet<double> T;

void cotmatrix(
const Eigen::MatrixXd & l,
const Eigen::MatrixXi & F,
Eigen::SparseMatrix<double> & L)
{
// Add your code here
// setFromTriplets will sum up all entries:
std::vector<T> triplet_list;
for (int n = 0; n < F.rows(); n ++) {
for (int m = 0; m < 3; m ++) {
int i = F(n, m);
int j = F(n, (m+1)%3);
int k = F(n, (m+2)%3);

double ei = l(n, m);
double ej = l(n, (m+1)%3);
double ek = l(n, (m+2)%3);

double cos_ij = (pow(ei, 2) + pow(ej, 2) - pow(ek, 2)) / (2.0*ei*ej);
double sin_ij = sqrt(1-pow(cos_ij, 2));
double cot_ij = 0.5 * (cos_ij / sin_ij);

triplet_list.push_back(T(i, j, cot_ij));
triplet_list.push_back(T(j, i, cot_ij));
triplet_list.push_back(T(i, i, -cot_ij));
triplet_list.push_back(T(j, j, -cot_ij));
}
}
int num_V = F.maxCoeff() + 1;
L.resize(num_V, num_V);
L.setFromTriplets(triplet_list.begin(), triplet_list.end());
}

18 changes: 17 additions & 1 deletion src/massmatrix.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
#include "massmatrix.h"
#include <igl/doublearea.h>
#include <iostream>

void massmatrix(
const Eigen::MatrixXd & l,
const Eigen::MatrixXi & F,
Eigen::DiagonalMatrix<double,Eigen::Dynamic> & M)
{
// Add your code here
// Calculate Area
Eigen::MatrixXd dblA;
igl::doublearea(l, dblA);
dblA = dblA / 2.0;

// Constuct M:
int num_V = F.maxCoeff() + 1;
M.setZero(num_V);
for(int i = 0; i < F.rows(); i ++){
double area = dblA(i);
for(int j = 0; j < 3; j ++) {
int cur = F(i, j);
M.diagonal()(cur) += area / 3.0;
}
}
}

27 changes: 25 additions & 2 deletions src/smooth.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,35 @@
#include "smooth.h"
#include "cotmatrix.h"
#include "massmatrix.h"
#include <igl/edge_lengths.h>
#include <iostream>

typedef Eigen::Triplet<double> T;
void smooth(
const Eigen::MatrixXd & V,
const Eigen::MatrixXi & F,
const Eigen::MatrixXd & G,
double lambda,
Eigen::MatrixXd & U)
{
// Replace with your code
U = G;
// Find Laplacian
Eigen::MatrixXd l;
igl::edge_lengths(V, F, l);
Eigen::SparseMatrix<double> L;
cotmatrix(l, F, L);

// Find M
Eigen::DiagonalMatrix<double, Eigen::Dynamic> M;
massmatrix(l, F, M);
std::vector<T> triplet_list;
for(int i = 0; i < M.rows(); i ++) {
triplet_list.push_back(T(i, i, M.diagonal()(i)));
}
Eigen::SparseMatrix<double> sparse_M(M.rows(), M.rows());
sparse_M.setFromTriplets(triplet_list.begin(), triplet_list.end());

// Solve system:
Eigen::SimplicialLDLT<Eigen::SparseMatrix<double>> solver;
solver.compute(-lambda * L + sparse_M);
U = solver.solve(M * G);
}