Commit 045b2e76 authored by Nghia Truong's avatar Nghia Truong
Browse files

STYLE: Forward declaration for Graph and break header dependency

parent b11575fb
......@@ -22,9 +22,8 @@
#ifndef imstkPbdCollisionConstraint_h
#define imstkPbdCollisionConstraint_h
#include "imstkPbdModel.h"
#include <vector>
#include <memory>
namespace imstk
{
......
......@@ -20,6 +20,7 @@
=========================================================================*/
#include "imstkPointSet.h"
#include "imstkGraph.h"
namespace imstk
{
......@@ -314,11 +315,11 @@ PointSet::getMaxNumVertices()
return m_maxNumVertices;
}
Graph
std::shared_ptr<Graph>
PointSet::getMeshGraph()
{
LOG(WARNING) << "The graph of a point set has no edges";
return Graph(this->getNumVertices());
return std::make_shared<Graph>(this->getNumVertices());
}
} // imstk
......@@ -23,10 +23,11 @@
#define imstkPointSet_h
#include "imstkGeometry.h"
#include "imstkGraph.h"
namespace imstk
{
class Graph;
///
/// \class PointSet
///
......@@ -44,7 +45,7 @@ public:
///
/// \brief Destructor
///
~PointSet() = default;
virtual ~PointSet() override = default;
///
/// \brief Initializes the data structure given vertex positions
......@@ -175,7 +176,7 @@ public:
///
/// \brief Returns the mesh graph
///
virtual Graph getMeshGraph();
virtual std::shared_ptr<Graph> getMeshGraph();
protected:
......
......@@ -20,6 +20,7 @@
=========================================================================*/
#include "imstkSurfaceMesh.h"
#include "imstkGraph.h"
namespace imstk
{
......@@ -613,17 +614,17 @@ SurfaceMesh::getMaxNumTriangles()
return m_maxNumTriangles;
}
Graph
std::shared_ptr<Graph>
SurfaceMesh::getMeshGraph()
{
Graph gMesh(this->getNumVertices());
auto gMesh = std::make_shared<Graph>(this->getNumVertices());
for (auto tri : this->getTrianglesVertices())
{
gMesh.addEdge(tri[0], tri[1]);
gMesh.addEdge(tri[0], tri[2]);
gMesh.addEdge(tri[1], tri[2]);
gMesh->addEdge(tri[0], tri[1]);
gMesh->addEdge(tri[0], tri[2]);
gMesh->addEdge(tri[1], tri[2]);
}
return std::move(gMesh);
return gMesh;
}
} // imstk
......@@ -220,7 +220,7 @@ public:
///
/// \brief Returns the mesh graph
///
Graph getMeshGraph() override;
std::shared_ptr<Graph> getMeshGraph() override;
protected:
......
......@@ -20,6 +20,7 @@
=========================================================================*/
#include "imstkTetrahedralMesh.h"
#include "imstkGraph.h"
namespace imstk
{
......@@ -303,19 +304,19 @@ TetrahedralMesh::getNumTetrahedra() const
return m_tetrahedraVertices.size();
}
Graph
std::shared_ptr<Graph>
TetrahedralMesh::getMeshGraph()
{
Graph gMesh(this->getNumVertices());
auto gMesh = std::make_shared<Graph>(this->getNumVertices());
for (auto tet : this->getTetrahedraVertices())
{
gMesh.addEdge(tet[0], tet[1]);
gMesh.addEdge(tet[0], tet[2]);
gMesh.addEdge(tet[0], tet[3]);
gMesh.addEdge(tet[1], tet[2]);
gMesh.addEdge(tet[1], tet[3]);
gMesh.addEdge(tet[2], tet[3]);
gMesh->addEdge(tet[0], tet[1]);
gMesh->addEdge(tet[0], tet[2]);
gMesh->addEdge(tet[0], tet[3]);
gMesh->addEdge(tet[1], tet[2]);
gMesh->addEdge(tet[1], tet[3]);
gMesh->addEdge(tet[2], tet[3]);
}
return std::move(gMesh);
return gMesh;
}
} // imstk
......@@ -51,7 +51,7 @@ public:
///
/// \brief Destructor
///
~TetrahedralMesh() = default;
virtual ~TetrahedralMesh() override = default;
///
/// \brief Initializes the rest of the data structures given vertex positions and
......@@ -130,7 +130,7 @@ public:
///
/// \brief Returns the mesh graph
///
Graph getMeshGraph() override;
std::shared_ptr<Graph> getMeshGraph() override;
protected:
......
/*=========================================================================
Library: iMSTK
Copyright (c) Kitware, Inc. & Center for Modeling, Simulation,
& 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.txt
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 "imstkPbdObject.h"
#include "imstkPbdSolver.h"
#include "imstkPbdCollisionConstraint.h"
#include "imstkParallelUtils.h"
namespace imstk
{
void PbdSolver::solve()
{
m_pbdObject->integratePosition();
m_pbdObject->solveConstraints();
resolveCollisionConstraints();
m_pbdObject->updateVelocity();
}
void PbdSolver::resolveCollisionConstraints()
{
if (m_PBDConstraints != nullptr)
{
unsigned int maxIter = 2;
if (!m_PBDConstraints->empty())
{
unsigned int i = 0;
while (++i < maxIter)
{
for (size_t k = 0; k < m_PBDConstraints->size(); ++k)
{
(*m_PBDConstraints)[k]->solvePositionConstraint();
}
}
}
}
}
} // end namespace imstk
......@@ -23,11 +23,13 @@
#define imstkPbdSolver_h
#include "imstkSolverBase.h"
#include "imstkPbdObject.h"
#include "imstkGraph.h"
#include <vector>
#include <memory>
namespace imstk
{
class PbdObject;
class PbdCollisionConstraint;
///
/// \class PbdSolver
......@@ -37,12 +39,13 @@ class PbdCollisionConstraint;
class PbdSolver : public SolverBase
{
typedef std::vector<std::shared_ptr<PbdCollisionConstraint>> PBDConstraintVector;
public:
///
/// \brief Constructors/Destructor
///
PbdSolver() = default;
~PbdSolver() = default;
virtual ~PbdSolver() override = default;
PbdSolver(const PbdSolver &other) = delete;
PbdSolver &operator=(const PbdSolver &other) = delete;
......@@ -50,149 +53,38 @@ public:
///
/// \brief Set MaxIterations. The maximum number of nonlinear iterations.
///
void setMaxIterations(const size_t newMaxIterations)
{
this->m_maxIterations = newMaxIterations;
}
void setMaxIterations(const size_t newMaxIterations) { this->m_maxIterations = newMaxIterations; }
///
/// \brief Get MaxIterations. Returns current maximum nonlinear iterations.
///
size_t getMaxIterations() const
{
return this->m_maxIterations;
}
size_t getMaxIterations() const { return this->m_maxIterations; }
///
/// \brief
///
void setPbdObject(const std::shared_ptr<PbdObject>& pbdObj)
{
m_pbdObject = pbdObj;
}
void setPbdObject(const std::shared_ptr<PbdObject>& pbdObj) { m_pbdObject = pbdObj; }
///
/// \brief Solve the non linear system of equations G(x)=0 using Newton's method.
///
void solve()
{
m_pbdObject->integratePosition();
m_pbdObject->solveConstraints();
this->resolveCollisionConstraints();
m_pbdObject->updateVelocity();
}
///
/// \brief Partition constraints for parallelization
///
vector<vector<size_t>> partitionCostraints(const bool print = false)
{
auto pbdModel = std::dynamic_pointer_cast<PbdModel>(m_pbdObject->getDynamicalModel());
const auto constraints = pbdModel->getConstraints();
// Find the number of nodes involved
size_t maxVertId = 0;
for (const auto& constr : constraints)
{
auto vIds = constr->m_vertexIds;
maxVertId = std::max(maxVertId, *max_element(begin(vIds), end(vIds)));
}
// Form the node-constraint adjacency list
std::vector<std::vector<size_t>> adj(maxVertId + 1);
size_t constrId = 0;
for (const auto& constr : constraints)
{
for (const auto& vIds : constr->m_vertexIds)
{
adj[vIds].push_back(constrId);
}
constrId++;
}
// Add edges to the constraint graph. Every constraint that depends on a particular node
// forms an edge with other constraints that depend on the same node
imstk::Graph constraintGraph(constraints.size());
for (const auto& node : adj)
{
for (int i = 0; i < node.size(); i++)
{
for (int j = i + 1; j < node.size(); j++)
{
constraintGraph.addEdge(node[i], node[j]);
}
}
}
adj.clear();
// do graph coloring for the constraint graph
auto coloredGraph = constraintGraph.doGreedyColoring();
const auto numPartitions = *std::max_element(std::begin(coloredGraph), std::end(coloredGraph)) + 1;
vector<vector<size_t>> partitions(numPartitions);
size_t constrainId = 0;
for (const auto& partition : coloredGraph)
{
partitions[partition].push_back(constrainId);
constrainId++;
}
coloredGraph.clear();
// print
if (print)
{
size_t partitionId = 0;
for (const auto& partition : partitions)
{
std::cout << "Partition # " << partitionId << " (# nodes: " << partition.size() << ")" << std::endl;
for (const auto& node : partition)
{
std::cout << " " << node;
}
std::cout << std::endl;
partitionId++;
}
}
return partitions;
}
void solve() override;
///
/// \brief Add the global collision contraints to this solver
///
void addCollisionConstraints(PBDConstraintVector* collisionConstraints)
{
m_PBDConstraints = collisionConstraints;
}
void addCollisionConstraints(PBDConstraintVector* constraints) { m_PBDConstraints = constraints; }
///
/// \brief Solve the global collision contraints charged to this solver
///
void resolveCollisionConstraints()
{
if (m_PBDConstraints != nullptr)
{
unsigned int maxIter = 2;
if (!m_PBDConstraints->empty())
{
unsigned int i = 0;
while (++i < maxIter)
{
for (size_t k = 0; k < m_PBDConstraints->size(); ++k)
{
m_PBDConstraints->at(k)->solvePositionConstraint();
}
}
}
}
}
void resolveCollisionConstraints();
private:
size_t m_maxIterations = 20; ///< Maximum number of NL Gauss-Seidel iterations
PBDConstraintVector* m_PBDConstraints = nullptr; /// collision contraints charged to this solver
size_t m_maxIterations = 20; ///< Maximum number of NL Gauss-Seidel iterations
PBDConstraintVector* m_PBDConstraints = nullptr; /// collision contraints charged to this solver
std::shared_ptr<PbdObject> m_pbdObject;
};
} // imstk
#endif // imstkPbdSolver_h
\ No newline at end of file
#endif // imstkPbdSolver_h
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment