iMSTK
Interactive Medical Simulation Toolkit
imstkImplicitGeometryToImageData.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 "imstkImplicitGeometryToImageData.h"
8 #include "imstkImageData.h"
9 #include "imstkLogger.h"
10 #include "imstkGeometryUtilities.h"
11 #include "imstkImplicitGeometry.h"
12 #include "imstkDataArray.h"
13 
14 namespace imstk
15 {
16 ImplicitGeometryToImageData::ImplicitGeometryToImageData()
17 {
19  setRequiredInputType<ImplicitGeometry>(0);
20 
22  setOutput(std::make_shared<ImageData>());
23 }
24 
25 std::shared_ptr<ImageData>
26 ImplicitGeometryToImageData::getOutputImage() const
27 {
28  return std::dynamic_pointer_cast<ImageData>(getOutput(0));
29 }
30 
31 void
32 ImplicitGeometryToImageData::setInputGeometry(std::shared_ptr<ImplicitGeometry> inputGeometry)
33 {
34  setInput(inputGeometry, 0);
35 }
36 
37 void
39 {
40  std::shared_ptr<ImplicitGeometry> inputGeometry = std::dynamic_pointer_cast<ImplicitGeometry>(getInput(0));
41  if (inputGeometry == nullptr)
42  {
43  LOG(WARNING) << "No inputGeometry to rasterize";
44  return;
45  }
46 
47  if (m_Dimensions[0] == 0 || m_Dimensions[1] == 0 || m_Dimensions[2] == 0)
48  {
49  LOG(WARNING) << "Dimensions must be non-zero";
50  return;
51  }
52 
53  const Vec3d size = Vec3d(m_Bounds[1] - m_Bounds[0], m_Bounds[3] - m_Bounds[2], m_Bounds[5] - m_Bounds[4]);
54  const Vec3d spacing = size.cwiseQuotient(m_Dimensions.cast<double>());
55  const Vec3d origin = Vec3d(m_Bounds[0], m_Bounds[2], m_Bounds[4]);
56  const Vec3d shift = origin + spacing * 0.5;
57 
58  std::shared_ptr<ImageData> outputImage = std::make_shared<ImageData>();
59  outputImage->allocate(IMSTK_DOUBLE, 1, m_Dimensions, spacing, origin);
60  DataArray<double>& scalars = *std::dynamic_pointer_cast<DataArray<double>>(outputImage->getScalars());
61  double* imgPtr = scalars.getPointer();
62 
63  int i = 0;
64  for (int z = 0; z < m_Dimensions[2]; z++)
65  {
66  for (int y = 0; y < m_Dimensions[1]; y++)
67  {
68  for (int x = 0; x < m_Dimensions[0]; x++, i++)
69  {
70  const Vec3d pos = Vec3d(x, y, z).cwiseProduct(spacing) + shift;
71  imgPtr[i] = inputGeometry->getFunctionValue(pos);
72  }
73  }
74  }
75 
76  setOutput(outputImage);
77 }
78 } // namespace imstk
std::shared_ptr< Geometry > getInput(size_t port=0) const
Returns input geometry given port, returns nullptr if doesn&#39;t exist.
Compound Geometry.
void setInputGeometry(std::shared_ptr< ImplicitGeometry > inputGeometry)
Required input, port 0.
void setNumOutputPorts(const size_t numPorts)
Get/Set the amount of output ports.
std::shared_ptr< Geometry > getOutput(size_t port=0) const
Returns output geometry given port, returns nullptr if doesn&#39;t exist.
void setInput(std::shared_ptr< Geometry > inputGeometry, size_t port=0)
Set the input at the port.
void setOutput(std::shared_ptr< Geometry > inputGeometry, const size_t port=0)
Set the output at the port.
void requestUpdate() override
Users can implement this for the logic to be run.
void setNumInputPorts(const size_t numPorts)
Get/Set the amount of input ports.
Class that can represent the geometry of multiple implicit geometries as boolean functions One may su...