diff --git a/examples/demo 1/CMakeLists.txt b/examples/demo 1/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..1f8d2289b0c67483df4ec86e5f703c8b5e61716d --- /dev/null +++ b/examples/demo 1/CMakeLists.txt @@ -0,0 +1,9 @@ + +add_executable(vegaFemExample main.cpp demo1.cpp demo1.h) +target_link_libraries(demo1 + smCore + smRendering + smSimulators + smMesh + smShader + ) diff --git a/examples/demo 1/demo1.cpp b/examples/demo 1/demo1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..84d58139e620286053b2bcbf45f38409bce813e1 --- /dev/null +++ b/examples/demo 1/demo1.cpp @@ -0,0 +1,83 @@ +// This file is part of the SimMedTK project. +// Copyright (c) Center for Modeling, Simulation, and Imaging in Medicine, +// Rensselaer Polytechnic Institute +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//--------------------------------------------------------------------------- +// +// Authors: +// +// Contact: +//--------------------------------------------------------------------------- + +#include "vegaFemExample.h" + +/// \brief Create a scene with one fem object using vega library +demo1Scene::demo1Scene() +{ + //Create an instance of the SoFMIS framework/SDK + sdk = smSDK::createSDK(); + + //Create a new scene to work in + scene = sdk->createScene(); + + //Create a viewer to see the scene + sdk->addViewer(&viewer); + + //------------------------------------------------------- + // Create scene object 1: fem scene object + //------------------------------------------------------- + + /// create a FEM simulator + femSim = new smVegaFemSimulator(sdk->getErrorLog()); + sdk->registerObjectSim(femSim); + + /// create a Vega based FEM object and attach it to the fem simulator + femobj = new smVegaFemSceneObject(sdk->getErrorLog(), + "../../../resources/vega/asianDragon/asianDragon.config"); + femobj->attachObjectSimulator(femSim); + + sdk->registerSceneObject(femobj); + + //------------------------------------------------------- + // Create scene object 2: static plane + //------------------------------------------------------- + + // Create dummy simulator + defaultSimulator = std::make_shared<smDummySimulator>(sdk->getErrorLog()); + sdk->registerObjectSim(defaultSimulator); + + + + /// add the FEM object to the scene + scene->addSceneObject(femobj); + + /// create a simulator module and register FEM simulator + simulator = sdk->createSimulator(); + simulator->registerObjectSimulator(femSim); + + /// create a viewer + viewer.viewerRenderDetail = viewer.viewerRenderDetail | SIMMEDTK_VIEWERRENDER_FADEBACKGROUND; + viewer.setEventDispatcher(sdk->getEventDispatcher()); + + /// run the SDK + sdk->run(); +} + +void buildDemo1Scene() +{ + vegaFemExample *ve = new vegaFemExample(); + delete ve; +} + diff --git a/examples/demo 1/demo1.h b/examples/demo 1/demo1.h new file mode 100644 index 0000000000000000000000000000000000000000..5043fbdaae3820a7644ce66f5d93c3ed6c27dbd6 --- /dev/null +++ b/examples/demo 1/demo1.h @@ -0,0 +1,50 @@ +// This file is part of the SimMedTK project. +// Copyright (c) Center for Modeling, Simulation, and Imaging in Medicine, +// Rensselaer Polytechnic Institute +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//--------------------------------------------------------------------------- +// +// Authors: +// +// Contact: +//--------------------------------------------------------------------------- + +#include "smCore/smConfig.h" +#include "smCore/smErrorLog.h" +#include "smCore/smCoreClass.h" +#include "smCore/smSDK.h" +#include "smRendering/smViewer.h" +#include "smSimulators/smVegaFemSimulator.h" +#include "smSimulators/smVegaFemSceneObject.h" + +class demo1Scene : public smSimulationMain, public smCoreClass +{ + +public: + demo1Scene(); + + virtual ~demo1Scene(){} + void simulateMain(smSimulationMainParam /*p_param*/) { }; +private: + std::unique_ptr<smSDK> sdk; + smVegaFemSceneObject *femobj; + smVegaFemSimulator *femSim; + smMatrix33d mat; + smSimulator *simulator; + smViewer viewer; + smScene *scene1; +}; + +void buildDemo1Scene(); diff --git a/examples/demo 1/main.cpp b/examples/demo 1/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4d7549568c3df29785ff2b1b43cff751ef10731c --- /dev/null +++ b/examples/demo 1/main.cpp @@ -0,0 +1,30 @@ +// This file is part of the SimMedTK project. +// Copyright (c) Center for Modeling, Simulation, and Imaging in Medicine, +// Rensselaer Polytechnic Institute +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//--------------------------------------------------------------------------- +// +// Authors: +// +// Contact: +//--------------------------------------------------------------------------- + +#include "vegaFemExample.h" + +int main() +{ + demo1(); + return 0; +} diff --git a/include/smCollision/smMeshCollisionModel.h b/include/smCollision/smMeshCollisionModel.h index 794e910fa985a8cd7b2507c9b65dbaea0e27ff91..50b7d74fc2f22a4ce560dba68e5b71e65bd95342 100644 --- a/include/smCollision/smMeshCollisionModel.h +++ b/include/smCollision/smMeshCollisionModel.h @@ -90,6 +90,19 @@ public: /// std::array<smVec3d,3> getTrianglePositions(size_t i) const; + /// + /// @brief Returns array of vertices + /// + const smStdVector3d &getVertices() const + { + return mesh->getVertices(); + } + + void draw() override + { + this->mesh->draw(); + } + private: std::shared_ptr<smMesh> mesh; // Underlying mesh std::shared_ptr<AABBTreeType> aabbTree; // Bounding volume hierarchy diff --git a/include/smCollision/smPlaneCollisionModel.h b/include/smCollision/smPlaneCollisionModel.h new file mode 100644 index 0000000000000000000000000000000000000000..69c2a44d6f798a3491edf07f3a61468ed654c5ac --- /dev/null +++ b/include/smCollision/smPlaneCollisionModel.h @@ -0,0 +1,55 @@ +/* + * Copyright 2015 Ricardo Ortiz <email> + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#ifndef SMPLANECOLLISIONMODEL_H +#define SMPLANECOLLISIONMODEL_H + +#include <memory> + +#include "smCore/smGeometry.h" +#include "smCore/smModelRepresentation.h" + +class smPlane; + +class smPlaneCollisionModel : public smModelRepresentation +{ +public: + using RigidTransform = Eigen::Transform<double, 2, Eigen::Isometry>; + +public: + smPlaneCollisionModel(const smVec3d &p, const smVec3d &n); + + ~smPlaneCollisionModel(); + + void draw() override; + + const smVec3d &getNormal() const; + + void setNormal(const smVec3d &normal); + + const smVec3d &getPosition() const; + + const RigidTransform &getTransform() const; + + void setTransform(const RigidTransform &t); + +private: + std::shared_ptr<smPlane> plane; + RigidTransform transform; +}; + +#endif // SMPLANECOLLISIONMODEL_H_H diff --git a/include/smCollision/smPlaneToMeshCollision.h b/include/smCollision/smPlaneToMeshCollision.h index b3338d5f0f92a451953382a4f66fffe3e34b6894..27b68b0a56b9413c4626b7bc63ac9cc9ba0be2d6 100644 --- a/include/smCollision/smPlaneToMeshCollision.h +++ b/include/smCollision/smPlaneToMeshCollision.h @@ -27,7 +27,6 @@ // SimMedTK includes #include "smCollision/smCollisionDetection.h" -#include "smCore/smGeometry.h" class smCollisionPair; @@ -43,7 +42,6 @@ public: smPlaneToMeshCollision(const smPlaneToMeshCollision &) = delete; private: - smPlane plane; virtual void doComputeCollision(std::shared_ptr<smCollisionPair> pairs) override; }; diff --git a/include/smContactHandling/smContactConfig.h b/include/smContactHandling/smContactConfig.h index 7d593ced77814f0f05ed57fa3aea5c2205c34221..5da5216191bb13e07be42e02ca7690e52851fd5a 100644 --- a/include/smContactHandling/smContactConfig.h +++ b/include/smContactHandling/smContactConfig.h @@ -24,15 +24,4 @@ #ifndef SMCONTACTHANDLINGCONFIG_H #define SMCONTACTHANDLINGCONFIG_H -enum class smContactHandlingType -{ - SIMMEDTK_CONTACT_PENALTY_UNILATERAL, - SIMMEDTK_CONTACT_PENALTY_BILATERAL, - SIMMEDTK_CONTACT_PBD_UNILATERAL, - SIMMEDTK_CONTACT_PBD_BILATERAL, - SIMMEDTK_CONTACT_LPC_UNILATERAL, - SIMMEDTK_CONTACT_LCP_UNILATERAL, - SIMMEDTK_CONTACT_LCP_BILATERAL -}; - #endif diff --git a/include/smContactHandling/smContactHandling.h b/include/smContactHandling/smContactHandling.h index 6e06a7ea506792ce30723d9ff567c783354792cf..9004fad823492ee8d001543e2daa4e8a92f2f87b 100644 --- a/include/smContactHandling/smContactHandling.h +++ b/include/smContactHandling/smContactHandling.h @@ -61,6 +61,16 @@ public: void setSceneObjects(const std::shared_ptr< smSceneObject >& first, const std::shared_ptr< smSceneObject >& second); + /// + /// @brief Set the scene objects that are colliding + /// + void setCollisionPairs(const std::shared_ptr< smCollisionPair >& colPair); + + /// + /// @brief Get how the contact between scene objects is processed + /// + std::shared_ptr<smCollisionPair> getCollisionPairs() const; + /// /// @brief Get how the contact between scene objects is processed /// @@ -76,16 +86,18 @@ public: /// std::shared_ptr<smSceneObject> getSecondSceneObject() const; + /// + /// @brief Implementation of how the contacts between colliding objects is resolved + /// + virtual void resolveContacts() = 0; protected: smContactHandlingType contactHandlingType; - std::pair<std::shared_ptr<smSceneObject>, std::shared_ptr<smSceneObject>> collidingSceneObjects; + std::pair<std::shared_ptr<smSceneObject>, std::shared_ptr<smSceneObject>> collidingSceneObjects; + + std::shared_ptr<smCollisionPair> collisionPairs; - /// - /// @brief Implementation of how the contacts between colliding objects is resolved - /// - virtual void processContacts(std::shared_ptr<smCollisionPair> pairs)=0; }; diff --git a/include/smContactHandling/smPenaltyContactHandling.h b/include/smContactHandling/smPenaltyContactHandling.h index 8442cbc9f47a43d5b0c559f18a07f76bd6521c5d..a6292368ded4f8ec67ae6ca81349cb97b852e450 100644 --- a/include/smContactHandling/smPenaltyContactHandling.h +++ b/include/smContactHandling/smPenaltyContactHandling.h @@ -49,12 +49,12 @@ private: /// /// @brief Get the forces on one the first scene object using penalty method /// - void computeUnilateralContactForces(std::shared_ptr<smCollisionPair> pairs); + void computeUnilateralContactForces(); /// /// @brief Get the forces on both the scene objects using penalty method /// - void computeBilateralContactForces(std::shared_ptr<smCollisionPair> pairs); + void computeBilateralContactForces(); }; diff --git a/include/smCore/smCoreClass.h b/include/smCore/smCoreClass.h index b086d9e526feddbd9a34bfd9a581715be746df64..8344f55a3b4df9360d8ba96d1183b636cb16919a 100644 --- a/include/smCore/smCoreClass.h +++ b/include/smCore/smCoreClass.h @@ -117,6 +117,11 @@ public: /// virtual void draw(const smDrawParam &p_params); + /// + /// \brief draw function is called for visualization the object + /// + virtual void draw(); + /// /// \brief initialization of simulation /// diff --git a/include/smCore/smGeometry.h b/include/smCore/smGeometry.h index baae07492efb0362d9a4d1842ce2d916d456deda..54df944616640ee1a1c5452c0399fc6137d7168d 100644 --- a/include/smCore/smGeometry.h +++ b/include/smCore/smGeometry.h @@ -27,16 +27,37 @@ // SimMedTK includes #include "smCore/smConfig.h" #include "smUtilities/smVector.h" +#include "smUtilities/smMatrix.h" //forward declaration struct smSphere; +class smAnalyticalGeometry +{ +public: + smAnalyticalGeometry(){} + ~smAnalyticalGeometry(){} + + virtual void translate(const smVec3d t)=0; + virtual void rotate(const smMatrix33d &rot)=0; + + virtual void draw()=0; +}; + /// \brief Simple Plane definition with unit normal and spatial location -class smPlane -{ +class smPlane : public smAnalyticalGeometry +{ public: smPlane(){} ~smPlane(){} + + /// \brief sphere constructor with center and radius + smPlane(const smVec3d &p, const smVec3d &n) + { + this->point = p; + this->unitNormal = n; + } + double distance(const smVec3d &p_vector) { auto m = (p_vector-point).dot(unitNormal); @@ -47,32 +68,149 @@ public: { return p_vector-((point-p_vector)*unitNormal.transpose())*unitNormal; }; - + const smVec3d &getUnitNormal() const { return this->unitNormal; } - + void setUnitNormal(const smVec3d &normal) { this->unitNormal = normal; } - + const smVec3d &getPoint() const { return this->point; } - + void setPoint(const smVec3d &p) { this->point = p; } - + + void translate(const smVec3d t) + { + point += t; + } + + void rotate(const smMatrix33d &rot) + { + unitNormal = rot * unitNormal; + } + + void draw() + { + // add plane rendering here + } + private: smVec3d unitNormal; smVec3d point; }; + +/// \brief sphere with center and radius +class smSphere : public smAnalyticalGeometry +{ +public: + /// \brief constructor + smSphere(); + + /// \brief sphere constructor with center and radius + smSphere(const smVec3d &c, const double &r) + { + this->center = c; + this->radius = r; + } + + ~smSphere(); + + void setRadius(const double r) + { + this->radius = r; + } + + void setCenter(const smVec3d& c) + { + this->center = c; + } + + void incrementRadius(const double r) + { + this->radius += r; + } + + void translate(const smVec3d t) + { + center += t; + } + + void rotate(const smMatrix33d &rot) + { + //Its a sphere! nothing to be done. + } + + void draw() + { + // add sphere rendering here + } + + double getRadius() const + { + return this->radius; + } + + const smVec3d &getCenter() const + { + return this->center; + } + +private: + /// \brief center of sphere + smVec3d center; + + /// \brief radius of sshere + double radius; +}; + +/// \brief cube +struct smCube +{ + /// \brief cube center + smVec3d center; + + /// \brief cube length + double sideLength; + + /// \brief constructor + smCube(); + + /// \brief subdivides the cube in mulitple cube with given number of cubes identified for each axis with p_divisionPerAxis + void subDivide(smInt p_divisionPerAxis, smCube *p_cube); + + /// \brief expands the cube. increases the edge length with expansion*edge length + void expand(double p_expansion); + + /// \brief returns the left most corner + smVec3d leftMinCorner() const ; + + /// \brief returns right most corner + smVec3d rightMaxCorner() const; + + /// \brief returns the smallest sphere encapsulates the cube + smSphere getCircumscribedSphere(); + + /// \brief returns the sphere with half edge of the cube as a radius + smSphere getInscribedSphere(); + + /// \brief get tangent sphere + smSphere getTangent2EdgeSphere(); +}; + + + + /// \brief Axis Aligned bounding box declarions class smAABB { @@ -180,56 +318,4 @@ public: } }; -/// \brief sphere structure -struct smSphere -{ - -public: - /// \brief center of sphere - smVec3d center; - - /// \brief radius of sshere - double radius; - - /// \brief constructor - smSphere(); - - /// \brief sphere constructor with center and radius - smSphere(smVec3d p_center, double p_radius); -}; - -/// \brief cube -struct smCube -{ - /// \brief cube center - smVec3d center; - - /// \brief cube length - double sideLength; - - /// \brief constructor - smCube(); - - /// \brief subdivides the cube in mulitple cube with given number of cubes identified for each axis with p_divisionPerAxis - void subDivide(smInt p_divisionPerAxis, smCube *p_cube); - - /// \brief expands the cube. increases the edge length with expansion*edge length - void expand(double p_expansion); - - /// \brief returns the left most corner - smVec3d leftMinCorner() const ; - - /// \brief returns right most corner - smVec3d rightMaxCorner() const; - - /// \brief returns the smallest sphere encapsulates the cube - smSphere getCircumscribedSphere(); - - /// \brief returns the sphere with half edge of the cube as a radius - smSphere getInscribedSphere(); - - /// \brief get tangent sphere - smSphere getTangent2EdgeSphere(); -}; - #endif diff --git a/include/smCore/smModelRepresentation.h b/include/smCore/smModelRepresentation.h index a8424c1ab8103c7e1ea9cc80add5febf238050c4..30725a38ee728b33dc621a77e8398e1c0a798a54 100644 --- a/include/smCore/smModelRepresentation.h +++ b/include/smCore/smModelRepresentation.h @@ -29,6 +29,10 @@ // STL includes #include <memory> +// SimMedTK includes +#include "smCore/smCoreClass.h" +#include "smMesh/smMesh.h" + class smMesh; class smModelRepresentation @@ -38,6 +42,9 @@ public: ~smModelRepresentation(){} virtual std::shared_ptr<smMesh> getMesh() = 0; + virtual std::shared_ptr<smCoreClass> getObject() = 0; + + virtual void draw() = 0; }; #endif // SMMODELREPRESENTATION_H diff --git a/include/smCore/smSimulator.h b/include/smCore/smSimulator.h index 3ec277e9981600727ebf2bb3a1033ad62cd47637..b49d932450b6b6c1436703db9a37a34df6763c08 100644 --- a/include/smCore/smSimulator.h +++ b/include/smCore/smSimulator.h @@ -36,6 +36,7 @@ #include "smUtilities/smMakeUnique.h" #include "smUtilities/smMakeUnique.h" #include "smCollision/smCollisionDetection.h" +#include "smContactHandling/smContactHandling.h" struct smSimulationMainParam { @@ -96,6 +97,7 @@ private: std::vector<std::shared_ptr<smObjectSimulator>> simulators; std::vector<std::shared_ptr<smCollisionDetection>> collisionDetectors; std::vector<std::shared_ptr<smCollisionPair>> collisionPairs; + std::vector<std::shared_ptr<smContactHandling>> contactHandlers; std::unique_ptr<ThreadPool> threadPool; // std::unique_ptr<ThreadPool> asyncPool; // asynchronous thread pool diff --git a/include/smCore/smStaticSceneObject.h b/include/smCore/smStaticSceneObject.h index b565e2099a258fe834131744121ca405464943e9..27f1e2d955b2e72c2a04449cbfd976764429d0dd 100644 --- a/include/smCore/smStaticSceneObject.h +++ b/include/smCore/smStaticSceneObject.h @@ -26,6 +26,7 @@ // SimMedTK includes #include "smCore/smConfig.h" +#include "smCore/smModelRepresentation.h" #include "smCore/smSceneObject.h" #include "smMesh/smSurfaceMesh.h" #include "smCore/smCoreClass.h" @@ -62,15 +63,19 @@ public: virtual void handleEvent(std::shared_ptr<smtk::Event::smEvent>) override {} - void setMesh(std::shared_ptr<smMesh> surfaceMesh) + void setModel(std::shared_ptr<smModelRepresentation> model) { - mesh = std::static_pointer_cast<smSurfaceMesh>(surfaceMesh); - mesh->meshType = SMMESH_RIGID; + this->staticModel = model; + } + + std::shared_ptr<smModelRepresentation> getModel() + { + return staticModel; } public: /// \brief static scene object contains a mesh - std::shared_ptr<smSurfaceMesh> mesh; + std::shared_ptr<smModelRepresentation> staticModel; }; #endif diff --git a/include/smMesh/smMesh.h b/include/smMesh/smMesh.h index 5d6f31247735cb80c67473977da6cd640e04ed20..9b4ef1d912f3ab1c51a004d8058ade855f9c2056 100644 --- a/include/smMesh/smMesh.h +++ b/include/smMesh/smMesh.h @@ -94,6 +94,11 @@ public: /// \brief update the original texture vertices with the current void updateOriginalVertsWithCurrent(); + const smStdVector3d &getVertices() const + { + return this->vertices; + } + public: smCollisionGroup collisionGroup; ///< !! smGLInt renderingID; ///< !! @@ -197,6 +202,12 @@ public: /// \brief render the surface mesh virtual void draw(const smDrawParam &p_params) override; + /// \brief render the surface mesh + virtual void draw() override + { + + } + public: smInt nbrTriangles; ///< number of triangles smTriangle *triangles; ///< list of triangles diff --git a/src/smCollision/smPlaneCollisionModel.cpp b/src/smCollision/smPlaneCollisionModel.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0f2f20454433c72924f626ecf4e5a2112467bd32 --- /dev/null +++ b/src/smCollision/smPlaneCollisionModel.cpp @@ -0,0 +1,48 @@ +/* + * Copyright 2015 Ricardo Ortiz <email> + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include "smCollision/smPlaneCollisionModel.h" + +smPlaneCollisionModel::smPlaneCollisionModel(const smVec3d& p, const smVec3d& n) : plane(std::make_shared<smPlane>(p, n)) +{ + +} +smPlaneCollisionModel::~smPlaneCollisionModel() {} +void smPlaneCollisionModel::draw() +{ + this->plane->draw(); +} +const smVec3d& smPlaneCollisionModel::getNormal() const +{ + return this->transform.linear() * this->plane->getUnitNormal(); +} +void smPlaneCollisionModel::setNormal(const smVec3d& normal) +{ + this->plane->setUnitNormal(normal); +} +const smVec3d& smPlaneCollisionModel::getPosition() const +{ + return this->transform * this->plane->getPoint(); +} +const smPlaneCollisionModel::RigidTransform& smPlaneCollisionModel::getTransform() const +{ + return this->transform; +} +void smPlaneCollisionModel::setTransform(const smPlaneCollisionModel::RigidTransform& t) +{ + this->transform = t; +} diff --git a/src/smCollision/smPlaneToMeshCollision.cpp b/src/smCollision/smPlaneToMeshCollision.cpp index 61fe006689a65d6aa5b22c5c6a2321913cc61607..0c83363a8b6cd2ef641effdf56769a439c988998 100644 --- a/src/smCollision/smPlaneToMeshCollision.cpp +++ b/src/smCollision/smPlaneToMeshCollision.cpp @@ -21,85 +21,35 @@ // Contact: //--------------------------------------------------------------------------- -// SimMedTK includes #include "smCollision/smPlaneToMeshCollision.h" + +// SimMedTK includes #include "smCollision/smCollisionMoller.h" #include "smCollision/smCollisionPair.h" #include "smCollision/smMeshCollisionModel.h" +#include "smCollision/smPlaneCollisionModel.h" // STL includes -#include <vector> +#include <limits> -void smPlaneToMeshCollision::doComputeCollision(std::shared_ptr<smCollisionPair> pairs) +void smPlaneToMeshCollision::doComputeCollision(std::shared_ptr<smCollisionPair> pair) { - auto mesh = std::static_pointer_cast<smMeshCollisionModel>(pairs->getFirst()); - auto plane = std::static_pointer_cast<smpPlaneCollisionModel>(pairs->getFirst()); + auto mesh = std::static_pointer_cast<smMeshCollisionModel>(pair->getFirst()); + auto plane = std::static_pointer_cast<smPlaneCollisionModel>(pair->getFirst()); if(!mesh || !plane) return; - - int nodeID; - double depth; - smVec3d normal; - smVec3d contactPoint; - -/* - for (int i=0; i<collisionModel->getMesh()-> ; i++) - { - - }*/ - -} - -std::vector<std::pair<std::shared_ptr<CellType>,std::shared_ptr<CellType>>> - getIntersectingNodes(std::shared_ptr<smSurfaceTree<CellType>> otherTree) - { - std::vector<std::pair<std::shared_ptr<CellType>,std::shared_ptr<CellType>>> intersectingNodes; - getIntersectingNodes(root, otherTree->getRoot(),intersectingNodes); - return intersectingNodes; - } - - void getIntersectingNodes(const std::shared_ptr<CellType> left, - const std::shared_ptr<smpPlaneCollisionModel> right, - std::vector<std::pair<std::shared_ptr<CellType>>> &result ) + smVec3d planeNormal = plane->getNormal(); + float planeOffset = planeNormal.dot(plane->getPosition()); + for (const auto& vertex : mesh->getVertices()) { - if(!smCollisionMoller::checkOverlapAABBAABB(left->getAabb(),right->getAabb())) + double d = planeNormal.dot(vertex) - planeOffset; + if (d < std::numeric_limits<float>::epsilon()) { - return; + // Create contact + pair->addContact(-d, vertex, planeNormal); } - - if(left->getIsLeaf() && right->getIsLeaf()) - { - result.emplace_back(left,right); - } - else if(left->getIsLeaf()) - { - for(const auto &child : right->getChildNodes()) - { - if(!child) continue; - getIntersectingNodes(left,child,result); - } - } - else if(right->getIsLeaf()) - { - for(const auto &child : left->getChildNodes()) - { - if(!child) continue; - getIntersectingNodes(child,right,result); - } - } - else - { - for(const auto &rightChild : right->getChildNodes()) - { - if(!rightChild) continue; - for(const auto &leftChild : left->getChildNodes()) - { - if(!leftChild) continue; - getIntersectingNodes(leftChild,rightChild,result); - } - } - } - } + +} diff --git a/src/smCollision/smSpatialHashCollision.cpp b/src/smCollision/smSpatialHashCollision.cpp index a258ccfdd41356ac9873c86f8888dbeafbef3061..120c82a470bf6c34c164b907c8bb6ae92f56591f 100644 --- a/src/smCollision/smSpatialHashCollision.cpp +++ b/src/smCollision/smSpatialHashCollision.cpp @@ -396,7 +396,7 @@ void smSpatialHashCollision::addOctreeCell(std::shared_ptr<smSpatialHashCollisio cellModel.primID = i; cellModel.center = iter[i].getCube().center; - cellModel.radius = iter[i].getCube().getCircumscribedSphere().radius; + cellModel.radius = iter[i].getCube().getCircumscribedSphere().getRadius(); for (smInt ix = xStartIndex; ix <= xEndIndex; ix++) for (smInt iy = yStartIndex; iy <= yEndIndex; iy++) diff --git a/src/smContactHandling/smContactHandling.cpp b/src/smContactHandling/smContactHandling.cpp index 02d17d8f410c54e418cabcced65a83276236bbad..b58a11876f25c90d1213f4664f59d7dad14f798b 100644 --- a/src/smContactHandling/smContactHandling.cpp +++ b/src/smContactHandling/smContactHandling.cpp @@ -21,26 +21,37 @@ // Contact: //--------------------------------------------------------------------------- -#include "smConstacHangling/smContactHandling.h" +#include "smContactHandling/smContactHandling.h" void smContactHandling::setSceneObjects(const std::shared_ptr< smSceneObject >& first, const std::shared_ptr< smSceneObject >& second) { - collidingSceneObjects.first = first; - collidingSceneObjects.second = second; + this->collidingSceneObjects.first = first; + this->collidingSceneObjects.second = second; } +void setCollisionPairs(const std::shared_ptr< smCollisionPair >& colPair) +{ + this->collisionPairs = colPair; +} + +std::shared_ptr<smCollisionPair> getCollisionPairs() const +{ + return this->collisionPairs; +} + + smContactHandlingType smContactHandling::getContactHandlingType() { - return contactHandlingType; + return this->contactHandlingType; } std::shared_ptr<smSceneObject> smContactHandling::getFirstSceneObject() { - return collidingSceneObjects.first; + return this->collidingSceneObjects.first; } std::shared_ptr<smSceneObject> smContactHandling::getSecondSceneObject() { - return collidingSceneObjects.second; + return this->collidingSceneObjects.second; } diff --git a/src/smContactHandling/smPenaltyContactHandling.cpp b/src/smContactHandling/smPenaltyContactHandling.cpp index 01bbaa82b5a4d2e7b1dd0fc105ff54ad77e45746..a03fb3218cc93920216c322ec84ac8f2d81f8971 100644 --- a/src/smContactHandling/smPenaltyContactHandling.cpp +++ b/src/smContactHandling/smPenaltyContactHandling.cpp @@ -45,30 +45,30 @@ smPenaltyCollisionHandling(smContactHandlingType contactHandlingType, this->collidingSceneObjects.second = sceneObjSecond; } -smContactHandlingType smPenaltyCollisionHandling::getContactHandlingType() +smContactHandlingType smPenaltyCollisionHandling::getContactHandlingType() { return contactHandlingType; } -void smPenaltyCollisionHandling::processContacts(std::shared_ptr<smCollisionPair> pairs) +void smPenaltyCollisionHandling::processContacts() { if (typeBilateral) { - computeUnilateralContactForces(std::shared_ptr<smCollisionPair> pairs); + computeUnilateralContactForces(); } else { - computeBilateralContactForces(std::shared_ptr<smCollisionPair> pairs); + computeBilateralContactForces(); } } -void smPenaltyCollisionHandling::computeUnilateralContactForces(std::shared_ptr<smCollisionPair> pairs) +void smPenaltyCollisionHandling::computeUnilateralContactForces() { int penetratedNode, nodeDofID; const double stiffness=1.0e5, damping=2000.0; smVec3d velocityProjection; - std::vector<std::shared_ptr<smContact>> contactInfo = pairs->getContacts(); + std::vector<std::shared_ptr<smContact>> contactInfo = this->getCollisionPairs()->getContacts(); if( this->getFirstSceneObject()->getType() == SIMMEDTK_SMVEGAFEMSCENEOBJECT && this->getSecondSceneObject()->getType() == SIMMEDTK_SMSTATICSCENEOBJECT) @@ -77,7 +77,7 @@ void smPenaltyCollisionHandling::computeUnilateralContactForces(std::shared_ptr< femSceneObject->setContactForcesToZero(); - for (int i = 0; i < pairs->size(); i++) + for (int i = 0; i < this->getCollisionPairs()->size(); i++) { nodeDofID = 3*contactInfo->nodeIndex; @@ -95,7 +95,7 @@ void smPenaltyCollisionHandling::computeUnilateralContactForces(std::shared_ptr< } -void smPenaltyCollisionHandling::computeBilateralContactForces(std::shared_ptr<smCollisionPair> pairs) +void smPenaltyCollisionHandling::computeBilateralContactForces() { diff --git a/src/smCore/smGeometry.cpp b/src/smCore/smGeometry.cpp index b727c278e535044dcf3aaa2d28e9fdc5b9d9ff2b..0715f2943f36cee093c8664cc8ff235dcefd40e3 100644 --- a/src/smCore/smGeometry.cpp +++ b/src/smCore/smGeometry.cpp @@ -147,12 +147,6 @@ smSphere::smSphere() radius = 1.0; } -smSphere::smSphere( smVec3d p_center, double p_radius ) -{ - radius = p_radius; - center = p_center; -} - smCube::smCube() { center << 0, 0, 0; diff --git a/src/smCore/smSimulator.cpp b/src/smCore/smSimulator.cpp index 223f1eeb7242a99508e3fb7ed554223ccc9fe36c..68af748bac565021e8e2fff622546d02f7d901ed 100644 --- a/src/smCore/smSimulator.cpp +++ b/src/smCore/smSimulator.cpp @@ -155,6 +155,14 @@ void smSimulator::run() } } + results.clear(); //clear the results buffer for new + std::shared_ptr<smContactHandling> contactHandling; + for (size_t i = 0; i < this->contactHandlers.size(); i++) + { + contactHandlers[i]->resolveContacts(); + } + + for (auto&& result : results) { //Wait until there is a valid return value from each thread result.get(); //waits for result value diff --git a/src/smCore/smStaticSceneObject.cpp b/src/smCore/smStaticSceneObject.cpp index e20920d22e47c5ad81e01385c53ef76999f45375..0d2a3b9457ae9c206ceae6123318872d5f12fb26 100644 --- a/src/smCore/smStaticSceneObject.cpp +++ b/src/smCore/smStaticSceneObject.cpp @@ -26,7 +26,6 @@ smStaticSceneObject::smStaticSceneObject ( std::shared_ptr<smErrorLog> p_log ) { type = SIMMEDTK_SMSTATICSCENEOBJECT; - mesh = std::make_shared<smSurfaceMesh>( SMMESH_RIGID, p_log ); } smStaticSceneObject::~smStaticSceneObject() {} void smStaticSceneObject::init() {} @@ -35,8 +34,7 @@ void smStaticSceneObject::serialize ( void* /*p_memoryBlock*/ ) {} void smStaticSceneObject::draw(const smDrawParam &p_params) { -// p_params.caller = this; - mesh->draw(p_params); + staticModel->draw(); } std::shared_ptr<smSceneObject> smStaticSceneObject::clone() diff --git a/src/smSimulators/smDummySimulator.cpp b/src/smSimulators/smDummySimulator.cpp index 908688972811f1626b98e4c4c61877966c3db327..989af0b45076cbf9196eabd3917eea59d7b725c1 100644 --- a/src/smSimulators/smDummySimulator.cpp +++ b/src/smSimulators/smDummySimulator.cpp @@ -26,6 +26,7 @@ #include "smCore/smStaticSceneObject.h" #include "smEvent/smEvent.h" #include "smEvent/smKeyboardEvent.h" +#include "smCollision/smMeshCollisionModel.h" smDummySimulator::smDummySimulator( std::shared_ptr<smErrorLog> p_errorLog ) : smObjectSimulator( p_errorLog ) { @@ -46,9 +47,16 @@ void smDummySimulator::initCustom() case SIMMEDTK_SMSTATICSCENEOBJECT: { auto staticObject = std::static_pointer_cast<smStaticSceneObject>(object); - object->getLocalVertices().reserve( staticObject->mesh->nbrVertices ); + auto model = std::static_pointer_cast<smMeshCollisionModel>(staticObject->getModel()); + if(nullptr == model) + { + break; + } + std::shared_ptr<smMesh> mesh = model->getMesh(); + + object->getLocalVertices().reserve( mesh->nbrVertices ); // WARNING: Copy!!? - object->getLocalVertices() = staticObject->mesh->vertices; + object->getLocalVertices() = mesh->vertices; object->getFlags().isSimulatorInit = true; break; } @@ -70,9 +78,14 @@ void smDummySimulator::run() if ( sceneObj->getType() == SIMMEDTK_SMSTATICSCENEOBJECT ) { auto staticSceneObject = std::static_pointer_cast<smStaticSceneObject>(sceneObj); - auto mesh = staticSceneObject->mesh; + auto model = std::static_pointer_cast<smMeshCollisionModel>(staticSceneObject->getModel()); + if(nullptr == model) + { + break; + } + std::shared_ptr<smMesh> mesh = model->getMesh(); - for ( smInt vertIndex = 0; vertIndex < staticSceneObject->mesh->nbrVertices; vertIndex++ ) + for ( smInt vertIndex = 0; vertIndex < mesh->nbrVertices; vertIndex++ ) { staticSceneObject->getLocalVertices()[vertIndex][1] = staticSceneObject->getLocalVertices()[vertIndex][1] + 0.000001; } @@ -95,7 +108,12 @@ void smDummySimulator::syncBuffers() if ( sceneObj->getType() == SIMMEDTK_SMSTATICSCENEOBJECT ) { auto staticSceneObject = std::static_pointer_cast<smStaticSceneObject>(sceneObj); - auto mesh = staticSceneObject->mesh; + auto model = std::static_pointer_cast<smMeshCollisionModel>(staticSceneObject->getModel()); + if(nullptr == model) + { + break; + } + std::shared_ptr<smMesh> mesh = model->getMesh(); // WARNING: Copy??! mesh->vertices = staticSceneObject->getLocalVertices(); }