void MeshMassPropertiesTests::testBoxAsMesh() { // verify that a mesh box produces the same mass properties as the analytic box. // build a box: // / // y // / // 6-------------------------7 // /| /| // / | / | // / 2----------------------/--3 // / / / / // | 4-------------------------5 / --x-- // z | / | / // | |/ |/ // 0 ------------------------1 btScalar x(5.0f); btScalar y(3.0f); btScalar z(2.0f); VectorOfPoints points; points.reserve(8); points.push_back(btVector3(0.0f, 0.0f, 0.0f)); points.push_back(btVector3(x, 0.0f, 0.0f)); points.push_back(btVector3(0.0f, y, 0.0f)); points.push_back(btVector3(x, y, 0.0f)); points.push_back(btVector3(0.0f, 0.0f, z)); points.push_back(btVector3(x, 0.0f, z)); points.push_back(btVector3(0.0f, y, z)); points.push_back(btVector3(x, y, z)); VectorOfIndices triangles; pushTriangle(triangles, 0, 1, 4); pushTriangle(triangles, 1, 5, 4); pushTriangle(triangles, 1, 3, 5); pushTriangle(triangles, 3, 7, 5); pushTriangle(triangles, 2, 0, 6); pushTriangle(triangles, 0, 4, 6); pushTriangle(triangles, 3, 2, 7); pushTriangle(triangles, 2, 6, 7); pushTriangle(triangles, 4, 5, 6); pushTriangle(triangles, 5, 7, 6); pushTriangle(triangles, 0, 2, 1); pushTriangle(triangles, 2, 3, 1); // compute expected mass properties analytically btVector3 expectedCenterOfMass = 0.5f * btVector3(x, y, z); btScalar expectedVolume = x * y * z; btMatrix3x3 expectedInertia; computeBoxInertia(expectedVolume, btVector3(x, y, z), expectedInertia); // compute the mass properties using the mesh MeshMassProperties mesh(points, triangles); // verify QCOMPARE_WITH_ABS_ERROR(mesh._volume, expectedVolume, acceptableRelativeError * expectedVolume); QCOMPARE_WITH_ABS_ERROR(mesh._centerOfMass, expectedCenterOfMass, acceptableAbsoluteError); // test this twice: _RELATIVE_ERROR doesn't test zero cases (to avoid divide-by-zero); _ABS_ERROR does. QCOMPARE_WITH_ABS_ERROR(mesh._inertia, expectedInertia, acceptableAbsoluteError); QCOMPARE_WITH_RELATIVE_ERROR(mesh._inertia, expectedInertia, acceptableRelativeError); // These two macros impl this: // for (int i = 0; i < 3; ++i) { // for (int j = 0; j < 3; ++j) { // if (expectedInertia [i][j] == btScalar(0.0f)) { // error = mesh._inertia[i][j] - expectedInertia[i][j]; // COMPARE_WITH_ABS_ERROR // if (fabsf(error) > acceptableAbsoluteError) { // std::cout << __FILE__ << ":" << __LINE__ << " ERROR : inertia[" << i << "][" << j << "] off by " // << error << " absolute"<< std::endl; // } // } else { // error = (mesh._inertia[i][j] - expectedInertia[i][j]) / expectedInertia[i][j]; // COMPARE_WITH_RELATIVE_ERROR // if (fabsf(error) > acceptableRelativeError) { // std::cout << __FILE__ << ":" << __LINE__ << " ERROR : inertia[" << i << "][" << j << "] off by " // << error << std::endl; // } // } // } // } }