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
27 changes: 25 additions & 2 deletions src/cotmatrix.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,33 @@
#include "cotmatrix.h"
#include <vector>
#include <math.h>

void cotmatrix(
const Eigen::MatrixXd & l,
const Eigen::MatrixXi & F,
Eigen::SparseMatrix<double> & L)
{
// Add your code here
typedef Eigen::Triplet<double> T;
std::vector<T> tripletList;

for (int f = 0; f < F.rows(); f++) {
for (int v = 0; v < 3; v++) {
int i = F(f, v);
int j = F(f, (v+1) % 3);

double eij = l(f, v);
double ej = l(f, (v+1) % 3);
double ei = l(f, (v+2) % 3);

double s = (ei + ej + eij) / 2;
double A = sqrt(s * (s-ei) * (s-ej) * (s-eij)); // heron's formula
double cot_aij = (ei*ei + ej*ej - eij*eij) / (4 * A); // law of cosines + sine area formula
tripletList.push_back(T(i, j, cot_aij / 2));
tripletList.push_back(T(j, i, cot_aij / 2));
tripletList.push_back(T(i, i, -cot_aij / 2));
tripletList.push_back(T(j, j, -cot_aij / 2));
}
}

L.setFromTriplets(tripletList.begin(), tripletList.end());
}

12 changes: 10 additions & 2 deletions src/massmatrix.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
#include "massmatrix.h"
#include <math.h>

void massmatrix(
const Eigen::MatrixXd & l,
const Eigen::MatrixXi & F,
Eigen::DiagonalMatrix<double,Eigen::Dynamic> & M)
{
// Add your code here
M.resize(F.maxCoeff() + 1);
for (int f = 0; f < F.rows(); f++) {
double s = l.row(f).sum() / 2;
double f_A = sqrt(s * (s-l(f,0)) * (s-l(f,1)) * (s-l(f,2)));
for (int v = 0; v < 3; v++) {
int i = F(f, v);
M.diagonal()[i] += f_A / 3;
}
}
}

28 changes: 26 additions & 2 deletions src/smooth.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
#include "smooth.h"
#include "cotmatrix.h"
#include "massmatrix.h"
#include "igl/edge_lengths.h"
#include <vector>
#include <Eigen/SparseCholesky>

void smooth(
const Eigen::MatrixXd & V,
Expand All @@ -7,6 +12,25 @@ void smooth(
double lambda,
Eigen::MatrixXd & U)
{
// Replace with your code
U = G;
Eigen::MatrixXd l(F.rows(), 3);
igl::edge_lengths(V, F, l);

int n = F.maxCoeff() + 1;
Eigen::SparseMatrix<double> L(n,n);
Eigen::DiagonalMatrix<double,Eigen::Dynamic> M;
cotmatrix(l, F, L);
massmatrix(l, F, M);

// construct the system matrix A = M - lambda * L
Eigen::SparseMatrix<double> A(n,n);
typedef Eigen::Triplet<double> T;
std::vector<T> tripletList;
for (int i = 0; i < M.rows(); i++) {
tripletList.push_back(T(i, i, M.diagonal()[i]));
}
A.setFromTriplets(tripletList.begin(), tripletList.end());
A -= lambda * L;

Eigen::SimplicialCholesky<Eigen::SparseMatrix<double>> chol(A);
U = chol.solve(M*G);
}