iMSTK
Interactive Medical Simulation Toolkit
imstkAbstractCellMesh.cpp
1 /*
2 ** This file is part of the Interactive Medical Simulation Toolkit (iMSTK)
3 ** iMSTK is distributed under the Apache License, Version 2.0.
4 ** See accompanying NOTICE for details.
5 */
6 
7 #include "imstkAbstractCellMesh.h"
8 
9 namespace imstk
10 {
11 void
13 {
15 
16  m_vertexToCells.clear();
18  for (auto i : m_cellAttributes)
19  {
20  i.second->clear();
21  }
22 
23  m_activeCellNormals = "";
24  m_activeCellTangents = "";
25  m_activeCellScalars = "";
26 }
27 
28 void
30 {
32 
33  LOG(INFO) << "Number of cells: " << getNumCells();
34  LOG(INFO) << "Active Cell Normals: " << m_activeCellNormals;
35  LOG(INFO) << "Active Cell Tangents: " << m_activeCellTangents;
36  LOG(INFO) << "Active Cell Scalars: " << m_activeCellScalars;
37 }
38 
39 const std::vector<int>
41 {
42  if (vertexId < 0 || vertexId >= getNumVertices())
43  {
44  return {};
45  }
46 
47  if (m_vertexToCells.size() == 0)
48  {
50  }
51  std::vector<int> cells(m_vertexToCells[vertexId].size());
52  std::copy(m_vertexToCells[vertexId].begin(), m_vertexToCells[vertexId].end(), cells.begin());
53  return cells;
54 }
55 
56 void
57 AbstractCellMesh::setCellActiveAttribute(std::string& activeAttributeName, std::string attributeName,
58  const int expectedNumComponents, const ScalarTypeId expectedScalarType)
59 {
60  std::shared_ptr<AbstractDataArray> attribute = m_cellAttributes[attributeName];
61  if (attribute->getNumberOfComponents() != expectedNumComponents)
62  {
63  LOG(WARNING) << "Failed to set cell attribute on Mesh " + getName() + " with "
64  << attribute->getNumberOfComponents() << " components. Expected " <<
65  expectedNumComponents << " components.";
66  return;
67  }
68  else if (attribute->getScalarType() != expectedScalarType)
69  {
70  LOG(INFO) << "Tried to set cell attribute on Mesh " + getName() + " with scalar type "
71  << static_cast<int>(attribute->getScalarType()) << ". Casting to "
72  << static_cast<int>(expectedScalarType) << " scalar type";
73  m_cellAttributes[attributeName] = attribute->cast(expectedScalarType);
74  }
75  activeAttributeName = attributeName;
76 }
77 
78 void
79 AbstractCellMesh::setCellScalars(const std::string& arrayName, std::shared_ptr<AbstractDataArray> scalars)
80 {
81  m_activeCellScalars = arrayName;
82  m_cellAttributes[arrayName] = scalars;
83 }
84 
85 void
86 AbstractCellMesh::setCellScalars(const std::string& arrayName)
87 {
88  if (hasCellAttribute(arrayName))
89  {
90  m_activeCellScalars = arrayName;
91  }
92 }
93 
94 std::shared_ptr<AbstractDataArray>
95 AbstractCellMesh::getCellScalars() const
96 {
97  if (hasCellAttribute(m_activeCellScalars))
98  {
99  return m_cellAttributes.at(m_activeCellScalars);
100  }
101  else
102  {
103  return nullptr;
104  }
105 }
106 
107 void
108 AbstractCellMesh::setCellNormals(const std::string& arrayName, std::shared_ptr<VecDataArray<double, 3>> normals)
109 {
110  m_activeCellNormals = arrayName;
111  m_cellAttributes[arrayName] = normals;
112 }
113 
114 void
115 AbstractCellMesh::setCellNormals(const std::string& arrayName)
116 {
117  if (hasCellAttribute(arrayName))
118  {
119  setCellActiveAttribute(m_activeCellNormals, arrayName, 3, IMSTK_DOUBLE);
120  }
121 }
122 
123 std::shared_ptr<VecDataArray<double, 3>>
124 AbstractCellMesh::getCellNormals() const
125 {
126  if (hasCellAttribute(m_activeCellNormals))
127  {
128  return std::dynamic_pointer_cast<VecDataArray<double, 3>>(m_cellAttributes.at(m_activeCellNormals));
129  }
130  else
131  {
132  return nullptr;
133  }
134 }
135 
136 void
137 AbstractCellMesh::setCellTangents(const std::string& arrayName, std::shared_ptr<VecDataArray<double, 3>> tangents)
138 {
139  m_activeCellTangents = arrayName;
140  m_cellAttributes[arrayName] = tangents;
141 }
142 
143 void
144 AbstractCellMesh::setCellTangents(const std::string& arrayName)
145 {
146  if (hasCellAttribute(arrayName))
147  {
148  setCellActiveAttribute(m_activeCellTangents, arrayName, 3, IMSTK_DOUBLE);
149  }
150 }
151 
152 std::shared_ptr<VecDataArray<double, 3>>
153 AbstractCellMesh::getCellTangents() const
154 {
155  if (hasCellAttribute(m_activeCellTangents))
156  {
157  return std::dynamic_pointer_cast<VecDataArray<double, 3>>(m_cellAttributes.at(m_activeCellTangents));
158  }
159  else
160  {
161  return nullptr;
162  }
163 }
164 } // namespace imstk
void setCellNormals(const std::string &arrayName, std::shared_ptr< VecDataArray< double, 3 >> normals)
Get/Set the active normals.
virtual void print() const override
Print the mesh info.
void print() const override
Print the surface mesh.
std::vector< std::unordered_set< int > > m_vertexToNeighborVertex
Map of vertices to neighbor vertices.
void clear() override
Clears all the mesh data.
Compound Geometry.
const std::vector< int > getCellsForVertex(const int vertexId)
Returns cells that contain the vertex, will calculate vertex cells if necessary.
virtual void computeVertexToCellMap()
Computes neighboring cells for all vertices.
std::vector< std::unordered_set< int > > m_vertexToCells
Map of vertices to neighbor cells.
void setCellTangents(const std::string &arrayName, std::shared_ptr< VecDataArray< double, 3 >> tangents)
Get/Set the active tangents.
const std::string & getName() const
Get the name of the geometry.
virtual void clear()
Clears all the mesh data.
bool hasCellAttribute(const std::string &arrayName) const
Check if a specific data array exists.
int getNumVertices() const
Returns the number of total vertices in the mesh.
void setCellScalars(const std::string &arrayName, std::shared_ptr< AbstractDataArray > scalars)
Get/Set the active scalars.