14 #include <vtkCellData.h>
15 #include <vtkDataArray.h>
16 #include <vtkImageData.h>
17 #include <vtkPointData.h>
18 #include <vtkSmartPointer.h>
34 std::string
const& array_name)
45 const double origin[3],
46 const double scalingFactor,
49 std::string
const& array_name)
56 ERR(
"Invalid Mesh Element Type.");
60 int* dims = img->GetDimensions();
65 ERR(
"Triangle or Quad elements cannot be used to construct meshes from "
70 vtkSmartPointer<vtkDataArray> pixelData =
71 vtkSmartPointer<vtkDataArray>(img->GetPointData()->GetScalars());
72 int nTuple = pixelData->GetNumberOfComponents();
73 if (nTuple < 1 || nTuple > 4)
75 ERR(
"VtkMeshConverter::convertImgToMesh(): Unsupported pixel "
81 std::array<double, 3>{{origin[0] - 0.5 * scalingFactor,
82 origin[1] - 0.5 * scalingFactor, origin[2]}});
84 static_cast<std::size_t
>(dims[1]),
85 static_cast<std::size_t
>(dims[2]),
90 std::vector<double> pix(header.n_cols * header.n_rows * header.n_depth, 0);
91 for (std::size_t k = 0; k < header.n_depth; k++)
93 std::size_t
const layer_idx = (k * header.n_rows * header.n_cols);
94 for (std::size_t i = 0; i < header.n_rows; i++)
96 std::size_t
const idx = i * header.n_cols + layer_idx;
97 for (std::size_t j = 0; j < header.n_cols; j++)
99 double* colour = pixelData->GetTuple(idx + j);
100 bool const visible = (nTuple == 2 || nTuple == 4)
101 ? (colour[nTuple - 1] != 0)
105 pix[idx + j] = header.no_data;
109 pix[idx + j] = (nTuple < 3) ? colour[0] :
110 (0.3 * colour[0] + 0.6 * colour[1] +
117 return convert(pix.data(), header, elem_type, intensity_type, array_name);
121 double const*
const img,
125 std::string
const& array_name)
132 ERR(
"Invalid Mesh Element Type.");
140 ERR(
"Triangle or Quad elements cannot be used to construct meshes from "
149 ERR(
"Elevation mapping can only be performed for 2D meshes.");
153 std::unique_ptr<MeshLib::Mesh> mesh;
193 std::unique_ptr<MeshLib::Mesh> new_mesh;
194 std::vector<std::size_t> elements_to_remove;
197 std::vector<MeshLib::Node*>
const& nodes(mesh->getNodes());
198 std::vector<MeshLib::Element*>
const& elems(mesh->getElements());
199 std::size_t
const n_nodes(elems[0]->getNumberOfNodes());
202 std::size_t
const m = (double_idx) ? 2 : 1;
203 for (std::size_t k = 0; k < header.
n_depth; k++)
205 std::size_t
const layer_idx = (k * header.
n_rows * header.
n_cols);
206 for (std::size_t i = 0; i < header.
n_cols; i++)
208 std::size_t
const idx(i * header.
n_rows + layer_idx);
209 for (std::size_t j = 0; j < header.
n_rows; j++)
211 double const val(img[idx + j]);
214 elements_to_remove.push_back(m * (idx + j));
217 elements_to_remove.push_back(m * (idx + j) + 1);
221 for (std::size_t n = 0; n < n_nodes; ++n)
239 if (array_name ==
"MaterialIDs")
243 fillPropertyVector<int>(*prop_vec, img, header, elem_type);
245 static_cast<int>(header.
no_data));
251 fillPropertyVector<double>(*prop_vec, img, header, elem_type);
256 if (!elements_to_remove.empty())
259 *mesh, elements_to_remove, mesh->getName()));
263 new_mesh = std::move(mesh);
268 new_mesh->getProperties().removePropertyVector(array_name);
void ERR(char const *fmt, Args const &... args)
Definition of the Mesh class.
Definition of the Node class.
Class Raster is used for managing raster data.
const_iterator begin() const
RasterHeader const & getHeader() const
Returns the complete header information.
std::size_t searchByPropertyValue(std::string const &property_name, PROPERTY_TYPE const property_value)
const std::vector< std::size_t > & getSearchedElementIDs() const
return marked elements
Property manager on mesh items. Class Properties manages scalar, vector or matrix properties....
PropertyVector< T > * createNewPropertyVector(std::string const &name, MeshItemType mesh_item_type, std::size_t n_components=1)
static std::unique_ptr< MeshLib::Mesh > convert(GeoLib::Raster const &raster, MeshElemType elem_type, UseIntensityAs intensity_type, std::string const &array_name="Colour")
Mesh * generateRegularPrismMesh(const double x_length, const double y_length, const double z_length, const std::size_t x_subdivision, const std::size_t y_subdivision, const std::size_t z_subdivision, MathLib::Point3d const &origin=MathLib::ORIGIN, std::string const &mesh_name="mesh")
Mesh * generateRegularTriMesh(const BaseLib::ISubdivision &div_x, const BaseLib::ISubdivision &div_y, MathLib::Point3d const &origin=MathLib::ORIGIN, std::string const &mesh_name="mesh")
Mesh * generateRegularQuadMesh(const BaseLib::ISubdivision &div_x, const BaseLib::ISubdivision &div_y, MathLib::Point3d const &origin=MathLib::ORIGIN, std::string const &mesh_name="mesh")
Mesh * generateRegularHexMesh(const BaseLib::ISubdivision &div_x, const BaseLib::ISubdivision &div_y, const BaseLib::ISubdivision &div_z, MathLib::Point3d const &origin=MathLib::ORIGIN, std::string const &mesh_name="mesh")
MeshLib::Mesh * removeElements(const MeshLib::Mesh &mesh, const std::vector< std::size_t > &removed_element_ids, const std::string &new_mesh_name)
std::size_t getNodeIndex(Element const &element, unsigned const idx)
UseIntensityAs
Selection of possible interpretations for intensities.
MeshElemType
Types of mesh elements supported by OpenGeoSys. Values are from VTKCellType enum.