Overte C++ Documentation
MeshMassProperties.h
1 //
2 // MeshMassProperties.h
3 // libraries/physics/src
4 //
5 // Created by Andrew Meadows 2015.05.25
6 // Copyright 2015 High Fidelity, Inc.
7 //
8 // Distributed under the Apache License, Version 2.0.
9 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
10 //
11 
12 #ifndef hifi_MeshMassProperties_h
13 #define hifi_MeshMassProperties_h
14 
15 #include <vector>
16 #include <stdint.h>
17 
18 #include <btBulletDynamicsCommon.h>
19 
20 typedef std::vector<btVector3> VectorOfPoints;
21 typedef std::vector<uint32_t> VectorOfIndices;
22 
23 #define EXPOSE_HELPER_FUNCTIONS_FOR_UNIT_TEST
24 #ifdef EXPOSE_HELPER_FUNCTIONS_FOR_UNIT_TEST
25 void computeBoxInertia(btScalar mass, const btVector3& diagonal, btMatrix3x3& I);
26 
27 // mass = input mass of tetrahedron
28 // points = input array of four points with center of mass at origin
29 // I = output inertia of tetrahedron about its center of mass
30 void computeTetrahedronInertia(btScalar mass, btVector3* points, btMatrix3x3& I);
31 void computeTetrahedronInertiaByBruteForce(btVector3* points, btMatrix3x3& I);
32 
33 btScalar computeTetrahedronVolume(btVector3* points);
34 
35 void applyParallelAxisTheorem(btMatrix3x3& inertia, const btVector3& shift, btScalar mass);
36 #endif // EXPOSE_HELPER_FUNCTIONS_FOR_UNIT_TEST
37 
38 // Given a closed mesh with right-hand triangles a MeshMassProperties instance will compute
39 // its mass properties:
40 //
41 // volume
42 // center-of-mass
43 // normalized interia tensor about center of mass
44 //
45 class MeshMassProperties {
46 public:
47 
48  // the mass properties calculation is done in the constructor, so if the mesh is complex
49  // then the construction could be computationally expensiuve.
50  MeshMassProperties(const VectorOfPoints& points, const VectorOfIndices& triangleIndices);
51 
52  // compute the mass properties of a new mesh
53  void computeMassProperties(const VectorOfPoints& points, const VectorOfIndices& triangleIndices);
54 
55  // harveste the mass properties from these public data members
56  btScalar _volume = 1.0f;
57  btVector3 _centerOfMass = btVector3(0.0f, 0.0f, 0.0f);
58  btMatrix3x3 _inertia = btMatrix3x3(1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f);
59 };
60 
61 #endif // _hifi_MeshMassProperties_h