/******************************************************************************** * ReactPhysics3D physics library, http://code.google.com/p/reactphysics3d/ * * Copyright (c) 2010-2013 Daniel Chappuis * ********************************************************************************* * * * This software is provided 'as-is', without any express or implied warranty. * * In no event will the authors be held liable for any damages arising from the * * use of this software. * * * * Permission is granted to anyone to use this software for any purpose, * * including commercial applications, and to alter it and redistribute it * * freely, subject to the following restrictions: * * * * 1. The origin of this software must not be misrepresented; you must not claim * * that you wrote the original software. If you use this software in a * * product, an acknowledgment in the product documentation would be * * appreciated but is not required. * * * * 2. Altered source versions must be plainly marked as such, and must not be * * misrepresented as being the original software. * * * * 3. This notice may not be removed or altered from any source distribution. * * * ********************************************************************************/ // Libraries #include #include "configuration.h" #include "ConvexMeshShape.h" using namespace reactphysics3d; // Constructor to initialize with a array of 3D vertices. /// This method creates an internal copy of the input vertices. /** * @param arrayVertices Array with the vertices of the convex mesh * @param nbVertices Number of vertices in the convex mesh * @param stride Stride between the beginning of two elements in the vertices array * @param margin Collision margin (in meters) around the collision shape */ ConvexMeshShape::ConvexMeshShape(const decimal* arrayVertices, uint nbVertices, int stride, decimal margin) : CollisionShape(CONVEX_MESH, margin), mNbVertices(nbVertices), mMinBounds(0, 0, 0), mMaxBounds(0, 0, 0), mIsEdgesInformationUsed(false) { assert(nbVertices > 0); assert(stride > 0); const unsigned char* vertexPointer = (const unsigned char*) arrayVertices; // Copy all the vertices into the internal array for (uint i=0; i 0); // For all neighbors of the current vertex std::set::const_iterator it; std::set::const_iterator itBegin = mEdgesAdjacencyList.at(maxVertex).begin(); std::set::const_iterator itEnd = mEdgesAdjacencyList.at(maxVertex).end(); for (it = itBegin; it != itEnd; ++it) { // Compute the dot product decimal dotProduct = direction.dot(mVertices[*it]); // If the current vertex is a better vertex (larger dot product) if (dotProduct > maxDotProduct) { maxVertex = *it; maxDotProduct = dotProduct; isOptimal = false; } } } while(!isOptimal); // Cache the support vertex *((int*)(*cachedCollisionData)) = maxVertex; // Return the support vertex return mVertices[maxVertex]; } else { // If the edges information is not used double maxDotProduct = DECIMAL_SMALLEST; uint indexMaxDotProduct = 0; // For each vertex of the mesh for (uint i=0; i maxDotProduct) { indexMaxDotProduct = i; maxDotProduct = dotProduct; } } assert(maxDotProduct >= decimal(0.0)); // Return the vertex with the largest dot product in the support direction return mVertices[indexMaxDotProduct]; } } // Recompute the bounds of the mesh void ConvexMeshShape::recalculateBounds() { mMinBounds.setToZero(); mMaxBounds.setToZero(); // For each vertex of the mesh for (uint i=0; i mMaxBounds.x) mMaxBounds.x = mVertices[i].x; if (mVertices[i].x < mMinBounds.x) mMinBounds.x = mVertices[i].x; if (mVertices[i].y > mMaxBounds.y) mMaxBounds.y = mVertices[i].y; if (mVertices[i].y < mMinBounds.y) mMinBounds.y = mVertices[i].y; if (mVertices[i].z > mMaxBounds.z) mMaxBounds.z = mVertices[i].z; if (mVertices[i].z < mMinBounds.z) mMinBounds.z = mVertices[i].z; } // Add the object margin to the bounds mMaxBounds += Vector3(mMargin, mMargin, mMargin); mMinBounds -= Vector3(mMargin, mMargin, mMargin); } // Test equality between two cone shapes bool ConvexMeshShape::isEqualTo(const CollisionShape& otherCollisionShape) const { const ConvexMeshShape& otherShape = dynamic_cast(otherCollisionShape); assert(mNbVertices == mVertices.size()); if (mNbVertices != otherShape.mNbVertices) return false; if (mIsEdgesInformationUsed != otherShape.mIsEdgesInformationUsed) return false; if (mEdgesAdjacencyList.size() != otherShape.mEdgesAdjacencyList.size()) return false; // Check that the vertices are the same for (uint i=0; imBody->mWorld.mCollisionDetection.mNarrowPhaseGJKAlgorithm.raycast( ray, proxyShape, raycastInfo); }