Welcome to mirror list, hosted at ThFree Co, Russian Federation.

admmpd_tetmesh.cpp « src « softbody « extern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 43157c416ae5f9909156a86b13548d9490fc91f8 (plain)
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
// Copyright Matt Overby 2020.
// Distributed under the MIT License.

#include "admmpd_tetmesh.h"
#include "admmpd_math.h"
#include <unordered_map>
#include <set>
#include "BLI_assert.h"

namespace admmpd {
using namespace Eigen;

void TetMesh::compute_masses(
    TetMeshData *mesh,
    const Eigen::MatrixXd *x,
    Eigen::VectorXd *masses,
	double density_kgm3)
{
	BLI_assert(mesh != NULL);
	BLI_assert(x != NULL);
	BLI_assert(masses != NULL);
	BLI_assert(density_kgm3 > 0);

	// Source: https://github.com/mattoverby/mclscene/blob/master/include/MCL/TetMesh.hpp
	// Computes volume-weighted masses for each vertex
	// density_kgm3 is the unit-volume density
	int nx = x->rows();
	masses->resize(nx);
	masses->setZero();
	int n_tets = mesh->tets.rows();
	for (int t=0; t<n_tets; ++t)
	{
		RowVector4i tet = mesh->tets.row(t);
		RowVector3d tet_v0 = x->row(tet[0]);
		Matrix3d edges;
		edges.col(0) = x->row(tet[1]) - tet_v0;
		edges.col(1) = x->row(tet[2]) - tet_v0;
		edges.col(2) = x->row(tet[3]) - tet_v0;
		double vol = std::abs((edges).determinant()/6.f);
		double tet_mass = density_kgm3 * vol;
		masses->operator[](tet[0]) += tet_mass / 4.f;
		masses->operator[](tet[1]) += tet_mass / 4.f;
		masses->operator[](tet[2]) += tet_mass / 4.f;
		masses->operator[](tet[3]) += tet_mass / 4.f;
	}

	// Verify masses
	for (int i=0; i<nx; ++i)
	{
		if (masses->operator[](i) <= 0.0)
		{
			printf("**TetMesh::compute_masses Error: unreferenced vertex\n");
			masses->operator[](i)=1;
		}
	}
} // end compute masses

} // namespace admmpd