https://github.com/HTDerekLiu/surface_multigrid_code
Revision db5c7fa56b977d3f1ac0ac11ed875f75229f2d27 authored by eriszhang on 08 August 2021, 03:27:39 UTC, committed by eriszhang on 08 August 2021, 03:27:39 UTC
1 parent d366192
Raw File
Tip revision: db5c7fa56b977d3f1ac0ac11ed875f75229f2d27 authored by eriszhang on 08 August 2021, 03:27:39 UTC
add
Tip revision: db5c7fa
mg_precompute.cpp
#include "mg_precompute.h"

// Build up a multi-level hierarchy for the surface multigrid solver
//
// Inputs:
//   Vf    #Vf-by-3 input vertex positions 
//   Ff    #Ff-by-3 input triangle indices
//   ratio  decimation rate between levels
//   nVCoarset  desired number of output faces for the coarest level
//   dec_type decimation type (0:qslim, 1:midpoint, 2:vertex removal)
//
// Outputs:
//   mg    a vector that contains precomputed information for each level

void mg_precompute(
	const Eigen::MatrixXd & Vf,
	const Eigen::MatrixXi & Ff,
	const float & ratio,
	const int & nVCoarsest,
	const int & dec_type,
  	std::vector<mg_data> & mg)
{
	using namespace std;
	using namespace Eigen;

	// compute number of multigrid levels
	int nLvs;
	{
		nLvs = 1;
		float nV = Vf.rows();
		while (true)
		{
			nV *= ratio;
			if (nV > nVCoarsest)
				nLvs += 1;
			else
				break;				
		}
		// TODO: handle the case where we only have 1 level		
	}

	mg_data data_lv0;
	if (mg.size() > 0) {
		data_lv0 = mg[0];
	}
	else {
		data_lv0.V = Vf;
		data_lv0.F = Ff;
	}

	mg.clear();
	mg.reserve(nLvs);

	// initialize the first level
	mg.push_back(data_lv0);

	for (int lv = 1; lv < nLvs; lv++)
	{
		int tarF = round((float)(mg[lv-1].F.rows()) * ratio);
		cout << "lv: " << lv << ", tarF: " << tarF << endl;

		MatrixXd V = mg[lv-1].V;
		MatrixXi F = mg[lv-1].F;

		SparseMatrix<double> P;
		MatrixXd Vc;
		MatrixXi Fc;
		get_prolong(V,F,tarF, dec_type, Vc,Fc,P);
		cout << "lv: " << lv << ", Vc: " << Vc.rows() << endl;

		mg_data data;
		data.V = Vc;
		data.F = Fc;
		data.P = P;
		data.PT = P.transpose();
		data.P_full = P;
		mg.push_back(data);
	}


	// print multigrid info
	cout << "============\n";
	cout << "Multigrid Info\n";
	cout << "============\n";
	cout << "numLv: " << mg.size() << endl;
	cout << "|V_coarsest|: " << mg[mg.size()-1].V.rows() << endl;
}

void mg_precompute(
	const Eigen::MatrixXd & Vf,
	const Eigen::MatrixXi & Ff,
  	std::vector<mg_data> & mg)
{
	int decimation_type = 1; // default: mid point decimation
	mg_precompute(Vf,Ff,decimation_type, mg);
}

void mg_precompute(
	const Eigen::MatrixXd & Vf,
	const Eigen::MatrixXi & Ff,
	const int & dec_type,
    std::vector<mg_data> & mg)
{
	float coarsening_ratio = 0.25;
	int min_coarsest_nV = 500;
	mg_precompute(Vf,Ff,coarsening_ratio,min_coarsest_nV,dec_type, mg);
}
back to top