7 #include "imstkPointSet.h" 8 #include "imstkParallelUtils.h" 9 #include "imstkLogger.h" 10 #include "imstkVecDataArray.h" 14 PointSet::PointSet() :
15 m_initialVertexPositions(
std::make_shared<VecDataArray<double, 3>>()),
16 m_vertexPositions(
std::make_shared<VecDataArray<double, 3>>())
33 if (m_initialVertexPositions !=
nullptr)
35 m_initialVertexPositions->clear();
37 if (m_vertexPositions !=
nullptr)
39 m_vertexPositions->clear();
41 for (
auto i : m_vertexAttributes)
52 LOG(INFO) <<
"Vertex positions:";
53 for (
auto& verts : *m_vertexPositions)
55 LOG(INFO) <<
"\t" << verts.x() <<
", " << verts.y() <<
", " << verts.z();
57 for (
auto i : m_vertexAttributes)
67 bool updateBounds = !m_transformApplied || m_boundsDirty;
71 ParallelUtils::findAABB(*m_vertexPositions, m_lowerCorner, m_upperCorner);
72 m_boundsDirty =
false;
74 lowerCorner = m_lowerCorner;
75 upperCorner = m_upperCorner;
76 if (paddingPercent > 0.0)
78 const Vec3d range = (m_upperCorner - m_lowerCorner) * (paddingPercent / 100.0);
87 m_initialVertexPositions = vertices;
93 #if defined(DEBUG) || defined(_DEBUG) || !defined(NDEBUG) 94 LOG_IF(FATAL, (static_cast<int>(vertNum) >= m_initialVertexPositions->size())) <<
"Invalid index";
96 return (*m_initialVertexPositions)[vertNum];
102 m_vertexPositions = vertices;
103 m_boundsDirty =
true;
109 std::shared_ptr<VecDataArray<double, 3>>
112 if (type == DataType::PostTransform)
115 return m_vertexPositions;
117 return m_initialVertexPositions;
123 #if defined(DEBUG) || defined(_DEBUG) || !defined(NDEBUG) 124 LOG_IF(FATAL, (static_cast<int>(vertNum) >= m_vertexPositions->size())) <<
"Invalid index";
126 (*m_vertexPositions)[vertNum] = pos;
127 m_transformApplied =
false;
128 m_boundsDirty =
true;
135 #if defined(DEBUG) || defined(_DEBUG) || !defined(NDEBUG) 136 LOG_IF(FATAL, (static_cast<int>(vertNum) >=
getVertexPositions()->size())) <<
"Invalid index";
144 #if defined(DEBUG) || defined(_DEBUG) || !defined(NDEBUG) 145 LOG_IF(FATAL, (static_cast<int>(vertNum) >=
getVertexPositions()->size())) <<
"Invalid index";
153 return m_vertexPositions->size();
163 std::shared_ptr<VecDataArray<double, 3>> normalsPtr = getVertexNormals();
164 std::shared_ptr<VecDataArray<float, 3>> tangentsPtr = getVertexTangents();
166 ParallelUtils::parallelFor(initVertices.size(),
169 initVertices[i] = (m * Vec4d(initVertices[i][0], initVertices[i][1], initVertices[i][2], 1.0)).head<3>();
170 vertices[i] = initVertices[i];
174 if (normalsPtr != NULL)
177 const Vec3d& x = m.block<3, 1>(0, 0);
178 const Vec3d& y = m.block<3, 1>(0, 1);
179 const Vec3d& z = m.block<3, 1>(0, 2);
182 r.block<3, 1>(0, 0) = x.normalized();
183 r.block<3, 1>(0, 1) = y.normalized();
184 r.block<3, 1>(0, 2) = z.normalized();
187 ParallelUtils::parallelFor(normals.size(),
190 normals[i] = (r * Vec3d(normals[i][0], normals[i][1], normals[i][2]));
196 if (tangentsPtr != NULL)
199 const Vec3d& x = m.block<3, 1>(0, 0);
200 const Vec3d& y = m.block<3, 1>(0, 1);
201 const Vec3d& z = m.block<3, 1>(0, 2);
204 rd.block<3, 1>(0, 0) = x.normalized();
205 rd.block<3, 1>(0, 1) = y.normalized();
206 rd.block<3, 1>(0, 2) = z.normalized();
208 Mat3f r = rd.cast<
float>();
211 ParallelUtils::parallelFor(tangents.size(),
214 tangents[i] = (r * Vec3f(tangents[i][0], tangents[i][1], tangents[i][2]));
220 m_boundsDirty =
true;
221 m_transformApplied =
false;
228 if (m_transformApplied ||
m_transform.isApprox(Mat4d::Identity()))
236 if (initVertices.size() != vertices.size())
238 vertices.
resize(initVertices.size());
241 ParallelUtils::parallelFor(vertices.size(),
244 vertices[i] = (
m_transform * Vec4d(initVertices[i][0], initVertices[i][1], initVertices[i][2], 1.0)).head<3>();
246 m_transformApplied =
true;
247 m_boundsDirty =
true;
253 return (m_vertexAttributes.find(arrayName) != m_vertexAttributes.end());
259 m_vertexAttributes[arrayName] = arr;
262 std::shared_ptr<AbstractDataArray>
265 auto it = m_vertexAttributes.find(arrayName);
266 if (it == m_vertexAttributes.end())
276 m_activeVertexScalars = arrayName;
277 m_vertexAttributes[arrayName] = scalars;
285 m_activeVertexScalars = arrayName;
289 std::shared_ptr<AbstractDataArray>
290 PointSet::getVertexScalars()
const 294 return m_vertexAttributes.at(m_activeVertexScalars);
305 m_activeVertexNormals = arrayName;
306 m_vertexAttributes[arrayName] = normals;
318 std::shared_ptr<VecDataArray<double, 3>>
319 PointSet::getVertexNormals()
const 334 m_activeVertexTangents = arrayName;
335 m_vertexAttributes[arrayName] = tangents;
347 std::shared_ptr<VecDataArray<float, 3>>
348 PointSet::getVertexTangents()
const 363 m_activeVertexTCoords = arrayName;
364 m_vertexAttributes[arrayName] = tcoords;
376 std::shared_ptr<VecDataArray<float, 2>>
377 PointSet::getVertexTCoords()
const 391 const int expectedNumComponents,
const ScalarTypeId expectedScalarType)
393 std::shared_ptr<AbstractDataArray> attribute = m_vertexAttributes[attributeName];
394 if (attribute->getNumberOfComponents() != expectedNumComponents)
396 LOG(WARNING) <<
"Failed to set vertex attribute on PointSet " +
getName() +
" with " 397 << attribute->getNumberOfComponents() <<
" components. Expected " <<
398 expectedNumComponents <<
" components.";
401 else if (attribute->getScalarType() != expectedScalarType)
403 LOG(INFO) <<
"Tried to set vertex attribute on PointSet " +
getName() +
" with scalar type " 404 <<
static_cast<int>(attribute->getScalarType()) <<
". Casting to " 405 << static_cast<int>(expectedScalarType) <<
" scalar type";
406 m_vertexAttributes[attributeName] = attribute->cast(expectedScalarType);
408 activeAttributeName = attributeName;
412 PointSet::cloneImplementation()
const 417 geom->m_initialVertexPositions = std::make_shared<VecDataArray<double, 3>>(*m_initialVertexPositions);
418 geom->m_vertexPositions = std::make_shared<VecDataArray<double, 3>>(*m_vertexPositions);
419 for (
auto i : m_vertexAttributes)
421 geom->m_vertexAttributes[i.first] = i.second->clone();
Mat4d m_transform
Transformation matrix.
void initialize(std::shared_ptr< VecDataArray< double, 3 >> positions)
Initializes the data structure given vertex positions.
void setVertexNormals(const std::string &arrayName, std::shared_ptr< VecDataArray< double, 3 >> normals)
Get/Set the active normals.
Base class for all geometries represented by discrete points and elements The pointsets follow a pipe...
void setVertexTangents(const std::string &arrayName, std::shared_ptr< VecDataArray< float, 3 >> tangents)
Get/Set the active tangents.
virtual void print() const
Print.
std::shared_ptr< AbstractDataArray > getVertexAttribute(const std::string &arrayName) const
Get a specific data array. If the array name cannot be found, nullptr is returned.
void applyTransform(const Mat4d &m) override
Applies transformation m directly the initial and post transform data.
virtual void print() const override
Print the mesh info.
bool hasVertexAttribute(const std::string &arrayName) const
Check if a specific data array exists.
void setVertexTCoords(const std::string &arrayName, std::shared_ptr< VecDataArray< float, 2 >> tcoords)
Get/Set the active tcoords.
void resize(const int size) override
Resize data array to hold exactly size number of values.
virtual void computeBoundingBox(Vec3d &lowerCorner, Vec3d &upperCorner, const double paddingPercent=0.0) override
Compute the bounding box for the entire mesh.
void setVertexAttribute(const std::string &arrayName, std::shared_ptr< AbstractDataArray > arr)
Set a data array holding some per vertex data.
void updatePostTransformData() const override
Applies the geometries member transform to produce currPositions.
void setVertexScalars(const std::string &arrayName, std::shared_ptr< AbstractDataArray > scalars)
Get/Set the active scalars.
void setActiveVertexAttribute(std::string &activeAttributeName, const std::string attributeName, const int expectedNumComponents, const ScalarTypeId expectedScalarType)
Sets the active vertex attribute name as long as the # components is satisfied. Throws message and ca...
const Vec3d & getVertexPosition(const size_t vertNum, DataType type=DataType::PostTransform) const
Returns the position of a vertex given its index.
const std::string & getName() const
Get the name of the geometry.
void setVertexPosition(const size_t vertNum, const Vec3d &pos)
Set the current position of a vertex given its index to certain position (this is not a thread-safe m...
void setInitialVertexPositions(std::shared_ptr< VecDataArray< double, 3 >> vertices)
Sets initial positions from an array.
std::shared_ptr< VecDataArray< double, 3 > > getVertexPositions(DataType type=DataType::PostTransform) const
Returns the vector of current positions of the mesh vertices.
void postModified()
emits signal to all observers, informing them on the current address in memory and size of array ...
virtual void clear()
Clears all the mesh data.
void setVertexPositions(std::shared_ptr< VecDataArray< double, 3 >> positions)
Sets current vertex positions of the mesh.
Vec3d & getInitialVertexPosition(const size_t vertNum)
Returns the initial position of a vertex given its index.
int getNumVertices() const
Returns the number of total vertices in the mesh.
DataType
Enumeration for the data to retrieve PreTransform for data where transform matrix is not applied Po...