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
47 changes: 46 additions & 1 deletion src/cotmatrix.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,55 @@
#include "cotmatrix.h"
#include <iostream>
#include <vector>
#include <cmath>

typedef Eigen::Triplet<double> T;

// Given edge lengths of a triangle ABC
// Return the cotan of angle A
double my_cot(double a, double b, double c) {
// Heron's Formula
double s = (a + b + c) / 2.0;
double area = sqrt(s * (s - a) * (s - b) * (s - c));

// Law of Sine and Cosine
double sinA = 2.0 * area / (b * c);
double cosA = (a*a - b*b - c*c) / (-2.0 * b * c);

return cosA / sinA;
}

void cotmatrix(
const Eigen::MatrixXd & l,
const Eigen::MatrixXi & F,
Eigen::SparseMatrix<double> & L)
{
// Add your code here
int nV = F.maxCoeff() + 1;
std::vector<T> tripletList;

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

double a = l(fi, (e + 2) % 3);
double b = l(fi, (e + 0) % 3);
double c = l(fi, (e + 1) % 3);

double cotA = my_cot(a, b, c);

double Lij = 0.5 * cotA;

tripletList.push_back(T(i, j, Lij)); // Duplicates will be summed
tripletList.push_back(T(j, i, Lij));

// Diagonal
tripletList.push_back(T(i, i, -Lij));
tripletList.push_back(T(j, j, -Lij));
}
}

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

14 changes: 13 additions & 1 deletion src/massmatrix.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
#include "massmatrix.h"
#include "igl/doublearea.h"

void massmatrix(
const Eigen::MatrixXd & l,
const Eigen::MatrixXi & F,
Eigen::DiagonalMatrix<double,Eigen::Dynamic> & M)
{
// Add your code here
int nV = F.maxCoeff() + 1;
M.resize(nV);

Eigen::VectorXd dblA;
igl::doublearea(l, dblA);

for(int fi = 0; fi < F.rows(); fi++) {
for(int vd = 0; vd < 3; vd++) {
int vert = F(fi, vd);
M.diagonal()[vert] += dblA[fi] / 6.0;
}
}
}

22 changes: 20 additions & 2 deletions src/smooth.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#include "smooth.h"
#include "cotmatrix.h"
#include "massmatrix.h"
#include "igl/edge_lengths.h"

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

Eigen::SparseMatrix<double> L;
cotmatrix(l, F, L);

Eigen::DiagonalMatrix<double,Eigen::Dynamic> M;
massmatrix(l, F, M);

// M * G = (M - lambda * L) * U
Eigen::SparseMatrix<double> A = -lambda * L;
for(int i = 0; i < M.rows(); i++) {
A.coeffRef(i, i) += M.diagonal()[i];
}

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