Skip to content

Commit

Permalink
Fixing the setting of fixed nodes via their indexes #149.
Browse files Browse the repository at this point in the history
If a softbody is defined via .OBJ file, we can retrieve the original indexes from the mesh file to set the fixed nodes.
  • Loading branch information
adnanmunawar committed Dec 15, 2021
1 parent 7125515 commit 529ab34
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 16 deletions.
2 changes: 1 addition & 1 deletion adf_loader/version_1_0/adf_loader_1_0.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1570,7 +1570,7 @@ bool ADFLoader_1_0::loadSoftBodyAttribs(YAML::Node *a_node, afSoftBodyAttributes
}
if (cfg_fixed_nodesNode.IsDefined()){
for (uint i = 0 ; i < cfg_fixed_nodesNode.size() ; i++){
attribs->m_fixedNodes.push_back(i);
attribs->m_fixedNodes.push_back(cfg_fixed_nodesNode[i].as<int>());
}
}
if(cfg_clustersNode.IsDefined()){
Expand Down
32 changes: 31 additions & 1 deletion ambf_framework/afFramework.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3871,10 +3871,40 @@ bool afSoftBody::createFromAttribs(afSoftBodyAttributes *a_attribs)
softBody->generateBendingConstraints(attribs.m_bendingConstraint);
}


// If a vertexIdx Map is defined, we can retrieve the actual indices defined in the mesh file.
bool useOriginalIndexes = getVisualObject()->m_vtxIdxMap.size() > 0 ? true : false;

for (uint i = 0 ; i < attribs.m_fixedNodes.size() ; i++){
uint nodeIdx = attribs.m_fixedNodes[i];
if ( nodeIdx < softBody->m_nodes.size()){
softBody->setMass(nodeIdx, 0);
if (useOriginalIndexes){
// Find the node's original vertex index
map<int, vector<int> >::iterator nIt = getVisualObject()->m_vtxIdxMap.find(nodeIdx);
if ( nIt != getVisualObject()->m_vtxIdxMap.end()){
if (nIt->second.size() > 0){
int remappedIdx = nIt->second[0];
int j = 0;
bool found = false;
while (j < m_afVertexTree.size() && !found){
for (int k = 0 ; k < m_afVertexTree[j].vertexIdx.size() ; k++){
if (remappedIdx == m_afVertexTree[j].vertexIdx[k]){
// cerr << "Node Idx: " << nodeIdx
// << " | Original Vtx Idx: " << nIt->first
// << " | Remapped Vtx Idx: " << remappedIdx << endl;
softBody->setMass(nodeIdx, 0);
found = true;
break;
}
}
j++;
}
}
}
}
else{
softBody->setMass(nodeIdx, 0);
}
}
}

Expand Down
31 changes: 18 additions & 13 deletions external/chai3d/src/files/CFileModelOBJ.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -327,13 +327,6 @@ bool cLoadFileOBJ(cMultiMesh* a_object, const std::string& a_filename)
{
indexTriangle = curMesh->newTriangle(indexV1,indexV2,indexV3);
curMesh->m_triangles->computeNormal(indexTriangle, true);

// This map keeps track of the vertex indices in the newly created mesh
// as compared to original indices in obj file. These indices are later
// used to add polylines without vertex duplication
fileObj.m_vtxIdxMap[originalV1].push_back(curMesh->m_vertices->getNumElements() - 3);
fileObj.m_vtxIdxMap[originalV2].push_back(curMesh->m_vertices->getNumElements() - 2);
fileObj.m_vtxIdxMap[originalV3].push_back(curMesh->m_vertices->getNumElements() - 1);
}
else
{
Expand Down Expand Up @@ -363,6 +356,13 @@ bool cLoadFileOBJ(cMultiMesh* a_object, const std::string& a_filename)
curMesh->m_vertices->setTexCoord(curMesh->m_triangles->getVertexIndex1(indexTriangle), face.m_pTexCoords[triangleVert-1]);
curMesh->m_vertices->setTexCoord(curMesh->m_triangles->getVertexIndex2(indexTriangle), face.m_pTexCoords[triangleVert]);
}

// This map keeps track of the vertex indices in the newly created mesh
// as compared to original indices in obj file. These indices are later
// used to add polylines without vertex duplication
fileObj.m_vtxIdxMap[originalV1].push_back(indexV1);
fileObj.m_vtxIdxMap[originalV2].push_back(indexV2);
fileObj.m_vtxIdxMap[originalV3].push_back(indexV3);
}
}
else
Expand Down Expand Up @@ -393,11 +393,7 @@ bool cLoadFileOBJ(cMultiMesh* a_object, const std::string& a_filename)
cVector3d vertex = fileObj.m_pVertices[j];
int vertexIdx = mesh->newVertex(vertex, cVector3d(1,0,0), cVector3d(0,0,0), color);
j++;


// This map keeps track of the vertex indices in the newly created mesh
// as compared to original indices in obj file. These indices are later
// used to add polylines without vertex duplication
// These indices are later used to add polylines without vertex duplication
fileObj.m_vtxIdxMap[j].push_back(vertexIdx);
}

Expand All @@ -413,10 +409,19 @@ bool cLoadFileOBJ(cMultiMesh* a_object, const std::string& a_filename)
mesh->setUseVertexColors(false);
}
}

}
}

// Remove duplicates from vtxIdxMap
std::map<int, std::vector<int> >::iterator vIt;

for (vIt = fileObj.m_vtxIdxMap.begin() ; vIt != fileObj.m_vtxIdxMap.end() ; ++vIt){
sort( vIt->second.begin(), vIt->second.end() );
vIt->second.erase( unique( vIt->second.begin(), vIt->second.end() ), vIt->second.end() );
}

a_object->m_vtxIdxMap = fileObj.m_vtxIdxMap;

// Add line data if present
// get main mesh
cMesh* curMesh = a_object->getMesh(0);
Expand Down
2 changes: 1 addition & 1 deletion external/chai3d/src/files/CFileModelOBJ.h
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ class cOBJModel
//! List of lines.
std::vector< std::vector<int> > m_pLines;

//! Mapping original vertex indices to regenerated indices in CHAI-3D
//! Map of original vertex indices (defined in the mesh file) to regenerated indices in CHAI-3D
std::map<int, std::vector<int> > m_vtxIdxMap;

//! List of colors.
Expand Down
4 changes: 4 additions & 0 deletions external/chai3d/src/world/CMultiMesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
//------------------------------------------------------------------------------
#include "world/CMesh.h"
//------------------------------------------------------------------------------
#include<map>

//------------------------------------------------------------------------------
namespace chai3d {
Expand Down Expand Up @@ -462,6 +463,9 @@ class cMultiMesh : public cGenericObject
//! Array of meshes.
std::vector<cMesh*> *m_meshes;

//! Map of original vertex indices (defined in the mesh file) to regenerated indices in CHAI-3D
std::map<int, std::vector<int> > m_vtxIdxMap;

};

//------------------------------------------------------------------------------
Expand Down

0 comments on commit 529ab34

Please sign in to comment.