From 8236837dd035aeaab7e4819bace0c41611801986 Mon Sep 17 00:00:00 2001 From: Andrew Meadows Date: Wed, 14 Jan 2015 15:00:31 -0800 Subject: [PATCH] add ShapeInfo::computeVolume() --- libraries/shared/src/ShapeInfo.cpp | 35 ++++++++++++++++++++++++++++++ libraries/shared/src/ShapeInfo.h | 1 + 2 files changed, 36 insertions(+) diff --git a/libraries/shared/src/ShapeInfo.cpp b/libraries/shared/src/ShapeInfo.cpp index d402a048a0..a1e72cdca0 100644 --- a/libraries/shared/src/ShapeInfo.cpp +++ b/libraries/shared/src/ShapeInfo.cpp @@ -24,18 +24,21 @@ void ShapeInfo::clear() { void ShapeInfo::setBox(const glm::vec3& halfExtents) { _type = BOX_SHAPE; _data.clear(); + // _data[0] = < halfX, halfY, halfZ > _data.push_back(halfExtents); } void ShapeInfo::setSphere(float radius) { _type = SPHERE_SHAPE; _data.clear(); + // _data[0] = < radius, radius, radius > _data.push_back(glm::vec3(radius)); } void ShapeInfo::setCylinder(float radius, float halfHeight) { _type = CYLINDER_SHAPE; _data.clear(); + // _data[0] = < radius, halfHeight, radius > // NOTE: default cylinder has (UpAxis = 1) axis along yAxis and radius stored in X _data.push_back(glm::vec3(radius, halfHeight, radius)); } @@ -43,6 +46,7 @@ void ShapeInfo::setCylinder(float radius, float halfHeight) { void ShapeInfo::setCapsule(float radius, float halfHeight) { _type = CAPSULE_SHAPE; _data.clear(); + // _data[0] = < radius, halfHeight, radius > _data.push_back(glm::vec3(radius, halfHeight, radius)); } @@ -58,3 +62,34 @@ glm::vec3 ShapeInfo::getBoundingBoxDiagonal() const { } return glm::vec3(0.0f); } + +float ShapeInfo::computeVolume() const { + const float DEFAULT_VOLUME = 1.0f; + float volume = DEFAULT_VOLUME; + switch(_type) { + case BOX_SHAPE: { + // factor of 8.0 because the components of _data[0] are all halfExtents + volume = 8.0f * _data[0].x * _data[0].y * _data[0].z; + break; + } + case SPHERE_SHAPE: { + float radius = _data[0].x; + volume = 4.0f * PI * radius * radius * radius / 3.0f; + break; + } + case CYLINDER_SHAPE: { + float radius = _data[0].x; + volume = PI * radius * radius * 2.0f * _data[0].y; + break; + } + case CAPSULE_SHAPE: { + float radius = _data[0].x; + volume = PI * radius * radius * (2.0f * _data[0].y + 4.0f * radius / 3.0f); + break; + } + default: + break; + } + assert(volume > 0.0f); + return volume; +} diff --git a/libraries/shared/src/ShapeInfo.h b/libraries/shared/src/ShapeInfo.h index 7ba290122c..9b4c587c3f 100644 --- a/libraries/shared/src/ShapeInfo.h +++ b/libraries/shared/src/ShapeInfo.h @@ -32,6 +32,7 @@ public: const QVector& getData() const { return _data; } glm::vec3 getBoundingBoxDiagonal() const; + float computeVolume() const; protected: int _type;