Accuracy improved to 0.01

This commit is contained in:
Virendra Singh 2015-03-06 22:07:35 +05:30
parent 42867bf98d
commit 84e05e361e
3 changed files with 184 additions and 181 deletions

View file

@ -32,8 +32,8 @@ MeshInfo::~MeshInfo(){
inline Vertex MeshInfo::getCentroid(const Vertex p1, const Vertex p2, const Vertex p3, const Vertex p4) const{
Vertex com;
com.x = (p1.x + p2.x + p3.x + p4.x) / 4.0f;
com.y = (p2.y + p2.y + p3.y + p4.y) / 4.0f;
com.z = (p2.z + p2.z + p3.z + p4.z) / 4.0f;
com.y = (p1.y + p2.y + p3.y + p4.y) / 4.0f;
com.z = (p1.z + p2.z + p3.z + p4.z) / 4.0f;
return com;
}
@ -49,18 +49,18 @@ Vertex MeshInfo::getMeshCentroid() const{
vector<float> MeshInfo::computeMassProperties(){
vector<float> volumeAndInertia = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
Vertex p0(0.0, 0.0, 0.0);
Vertex origin(0.0, 0.0, 0.0);
float meshVolume = 0.0f;
glm::mat3 globalMomentOfInertia(0.0);
glm::mat3 globalProductOfInertia(0.0);
//First we need need the center of mass of the mesh in order to translate the tetrahedron inertia to center of mass of the mesh.
for (int i = 0; i < _triangles->size(); i += 3){
for (unsigned int i = 0; i < _triangles->size(); i += 3){
Vertex p1 = _vertices->at(_triangles->at(i));
Vertex p2 = _vertices->at(_triangles->at(i + 1));
Vertex p3 = _vertices->at(_triangles->at(i + 2));
float volume = getVolume(p1, p2, p3, p0);
Vertex com = getCentroid(p0, p1, p2, p3);
float volume = getVolume(p1, p2, p3, origin);
Vertex com = getCentroid(origin, p1, p2, p3);
//Translate accumulated center of mass from each tetrahedron to mesh's center of mass using parallel axis theorem
meshVolume += volume;
_centerOfMass += com * volume;
@ -71,19 +71,19 @@ vector<float> MeshInfo::computeMassProperties(){
_centerOfMass = (_centerOfMass / (float)meshVolume);
//Translate the moment of inertia from each tetrahedron to mesh's center of mass using parallel axis theorem
for (int i = 0; i < _triangles->size(); i += 3){
for (unsigned int i = 0; i < _triangles->size(); i += 3){
Vertex p1 = _vertices->at(_triangles->at(i));
Vertex p2 = _vertices->at(_triangles->at(i + 1));
Vertex p3 = _vertices->at(_triangles->at(i + 2));
float volume = getVolume(p1, p2, p3, p0);
Vertex com = getCentroid(p0, p1, p2, p3);
float volume = getVolume(p1, p2, p3, origin);
Vertex com = getCentroid(origin, p1, p2, p3);
glm::mat3 identity;
Vertex diff = _centerOfMass - com;
float diffDot = glm::dot(diff, diff);
glm::mat3 outerDiff = glm::outerProduct(diff, diff);
//centroid is used for calculating inertia tensor relative to center of mass.
// translate the tetrahedron to its center of mass using P = P - centroid
p0 = p0 - com;
Vertex p0 = origin - com;
p1 = p1 - com;
p2 = p2 - com;
p3 = p3 - com;
@ -92,7 +92,7 @@ vector<float> MeshInfo::computeMassProperties(){
//http://docsdrive.com/pdfs/sciencepublications/jmssp/2005/8-11.pdf
//Explicit exact formulas for the 3-D tetrahedron inertia tensor in terms of its vertex coordinates - F.Tonon
float inertia_a = (volume * 6.0 / 60.0) * (
float inertia_a = (volume * 6.0f / 60.0f) * (
p0.y*p0.y + p0.y*p1.y + p0.y*p2.y + p0.y*p3.y +
p1.y*p1.y + p1.y*p2.y + p1.y*p3.y +
p2.y*p2.y + p2.y*p3.y +
@ -102,7 +102,7 @@ vector<float> MeshInfo::computeMassProperties(){
p2.z*p2.z + p2.z*p3.z +
p3.z*p3.z);
float inertia_b = (volume * 6.0 / 60.0) * (
float inertia_b = (volume * 6.0f / 60.0f) * (
p0.x*p0.x + p0.x*p1.x + p0.x*p2.x + p0.x*p3.x +
p1.x*p1.x + p1.x*p2.x + p1.x*p3.x +
p2.x*p2.x + p2.x*p3.x +
@ -112,7 +112,7 @@ vector<float> MeshInfo::computeMassProperties(){
p2.z*p2.z + p2.z*p3.z +
p3.z*p3.z);
float inertia_c = (volume * 6.0 / 60.0) * (
float inertia_c = (volume * 6.0f / 60.0f) * (
p0.x*p0.x + p0.x*p1.x + p0.x*p2.x + p0.x*p3.x +
p1.x*p1.x + p1.x*p2.x + p1.x*p3.x +
p2.x*p2.x + p2.x*p3.x +
@ -122,23 +122,24 @@ vector<float> MeshInfo::computeMassProperties(){
p2.y*p2.y + p2.y*p3.y +
p3.y*p3.y);
float inertia_aa = (volume * 6.0 / 120.0) * (2.0 * (p0.y*p0.z + p1.y*p1.z + p2.y*p2.z + p3.y*p3.z) +
float inertia_aa = (volume * 6.0f / 120.0f) * (2.0f * (p0.y*p0.z + p1.y*p1.z + p2.y*p2.z + p3.y*p3.z) +
p0.y*p1.z + p0.y*p2.z + p0.y*p3.z +
p1.y*p0.z + p1.y*p2.z + p1.y*p3.z +
p2.y*p0.z + p2.y*p1.z + p2.y*p3.z +
p3.y*p0.z + p3.y*p1.z + p3.y*p2.z);
float inertia_bb = (volume * 6.0 / 120.0) * (2.0 * (p0.x*p0.z + p1.x*p1.z + p2.x*p2.z + p3.x*p3.z) +
float inertia_bb = (volume * 6.0f / 120.0f) * (2.0f * (p0.x*p0.z + p1.x*p1.z + p2.x*p2.z + p3.x*p3.z) +
p0.x*p1.z + p0.x*p2.z + p0.x*p3.z +
p1.x*p0.z + p1.x*p2.z + p1.x*p3.z +
p2.x*p0.z + p2.x*p1.z + p2.x*p3.z +
p3.x*p0.z + p3.x*p1.z + p3.x*p2.z);
float inertia_cc = (volume * 6.0 / 120.0) * (2.0 * (p0.x*p0.y + p1.x*p1.y + p2.x*p2.y + p3.x*p3.y) +
float inertia_cc = (volume * 6.0f / 120.0f) * (2.0f * (p0.x*p0.y + p1.x*p1.y + p2.x*p2.y + p3.x*p3.y) +
p0.x*p1.y + p0.x*p2.y + p0.x*p3.y +
p1.x*p0.y + p1.x*p2.y + p1.x*p3.y +
p2.x*p0.y + p2.x*p1.y + p2.x*p3.y +
p3.x*p0.y + p3.x*p1.y + p3.x*p2.y);
//3x3 of local inertia tensors of each tetrahedron. Inertia tensors are the diagonal elements
glm::mat3 localMomentInertia = { Vertex(inertia_a, 0.0f, 0.0f), Vertex(0.0f, inertia_b, 0.0f),
Vertex(0.0f, 0.0f, inertia_c) };
@ -149,12 +150,12 @@ vector<float> MeshInfo::computeMassProperties(){
globalMomentOfInertia += localMomentInertia + volume * ((diffDot*identity) - outerDiff);
globalProductOfInertia += localProductInertia + volume * ((diffDot * identity) - outerDiff);
}
volumeAndInertia.push_back(meshVolume);
volumeAndInertia.push_back(globalMomentOfInertia[0][0]);
volumeAndInertia.push_back(globalMomentOfInertia[1][1]);
volumeAndInertia.push_back(globalMomentOfInertia[2][2]);
volumeAndInertia.push_back(globalProductOfInertia[0][0]);
volumeAndInertia.push_back(globalProductOfInertia[1][1]);
volumeAndInertia.push_back(globalProductOfInertia[2][2]);
volumeAndInertia[0] = meshVolume;
volumeAndInertia[1] = globalMomentOfInertia[0][0];
volumeAndInertia[2] = globalMomentOfInertia[1][1];
volumeAndInertia[3] = globalMomentOfInertia[2][2];
volumeAndInertia[4] = globalProductOfInertia[0][0];
volumeAndInertia[5] = globalProductOfInertia[1][1];
volumeAndInertia[6] = globalProductOfInertia[2][2];
return volumeAndInertia;
}

View file

@ -14,7 +14,7 @@
#include <MeshInfo.h>
#include "MeshInfoTests.h"
const double epsilon = 0.02;
const float epsilon = 0.01f;
void MeshInfoTests::testWithTetrahedron(){
glm::vec3 p0(8.33220, -11.86875, 0.93355);
glm::vec3 p1(0.75523, 5.00000, 16.37072);
@ -29,13 +29,15 @@ void MeshInfoTests::testWithTetrahedron(){
vector<glm::vec3> vertices = { p11, p22, p33 };
vector<int> triangles = { 0, 1, 2 };
float volume = 1873.233236;
float inertia_a = 43520.33257;
float inertia_b = 194711.28938;
float inertia_c = 191168.76173;
float inertia_aa = 4417.66150;
float inertia_bb = -46343.16662;
float inertia_cc = 11996.20119;
float volume = 1873.233236f;
float inertia_a = 43520.33257f;
//actual should be 194711.28938f. But for some reason it becomes 194711.296875 during
//runtime due to how floating points are stored.
float inertia_b = 194711.289f;
float inertia_c = 191168.76173f;
float inertia_aa = 4417.66150f;
float inertia_bb = -46343.16662f;
float inertia_cc = 11996.20119f;
meshinfo::MeshInfo meshinfo(&vertices,&triangles);
glm::vec3 tetCenterOfMass = meshinfo.getCentroid(p0, p1, p2, p3);
@ -58,11 +60,11 @@ void MeshInfoTests::testWithTetrahedron(){
//test if moment of inertia with respect to x axis is correct
if (abs(inertia_a - (voumeAndInertia.at(1))) > epsilon){
std::cout << __FILE__ << ":" << __LINE__ << " ERROR : Moment of inertia with respect to x axis is incorrect : Expected = " <<
inertia_a << " " << ", actual = " << (voumeAndInertia.at(1)) << std::endl;
inertia_a << " " << ", actual = " << voumeAndInertia.at(1) << std::endl;
}
//test if moment of inertia with respect to y axis is correct
if (abs(inertia_b - (voumeAndInertia.at(2))) > epsilon){
if (abs(inertia_b - voumeAndInertia.at(2)) > epsilon){
std::cout << __FILE__ << ":" << __LINE__ << " ERROR : Moment of inertia with respect to y axis is incorrect : Expected = " <<
inertia_b << " " << ", actual = " << (voumeAndInertia.at(2)) << std::endl;
}
@ -172,8 +174,8 @@ void MeshInfoTests::testWithUnitCube()
//test with origin as reference point
meshinfo::MeshInfo massProp1(&vertices, &triangles);
vector<float> volumeAndInertia1 = massProp1.computeMassProperties();
if (abs(centerOfMass.x - massProp1.getMeshCentroid().x) > epsilon || abs(centerOfMass.y - massProp1.getMeshCentroid().y) > epsilon ||
abs(centerOfMass.z - massProp1.getMeshCentroid().z) > epsilon){
if (abs(centerOfMass.x - massProp1.getMeshCentroid().x) > epsilon || abs(centerOfMass.y - massProp1.getMeshCentroid().y) >
epsilon || abs(centerOfMass.z - massProp1.getMeshCentroid().z) > epsilon){
std::cout << __FILE__ << ":" << __LINE__ << " ERROR : Center of mass is incorrect : Expected = " << centerOfMass.x <<
" " << centerOfMass.y << " " << centerOfMass.z << ", actual = " << massProp1.getMeshCentroid().x << " " <<
massProp1.getMeshCentroid().y << " " << massProp1.getMeshCentroid().z << std::endl;