Compute the volume of the convex mesh We use the divergence theorem to compute the volume of the convex mesh using a sum over its faces.
| 246 | // Compute the volume of the convex mesh |
| 247 | /// We use the divergence theorem to compute the volume of the convex mesh using a sum over its faces. |
| 248 | void ConvexMesh::computeVolume() { |
| 249 | |
| 250 | decimal sum = 0.0; |
| 251 | |
| 252 | // For each face of the mesh |
| 253 | for (uint32 f=0; f < getNbFaces(); f++) { |
| 254 | |
| 255 | const HalfEdgeStructure::Face& face = mHalfEdgeStructure.getFace(f); |
| 256 | const decimal faceArea = computeFaceNormal(f).length() * decimal(0.5); |
| 257 | const Vector3 faceNormal = mFacesNormals[f]; |
| 258 | const Vector3& faceVertex = getVertex(face.faceVertices[0]); |
| 259 | |
| 260 | sum += faceVertex.dot(faceNormal) * faceArea; |
| 261 | } |
| 262 | |
| 263 | mVolume = std::abs(sum) / decimal(3.0); |
| 264 | } |