Microflow 3D  v1.0
StlMesher.cpp
Go to the documentation of this file.
1 // ==============================================================================================
2 // Microflow 3D, http://www.microflow.pwr.edu.pl/
3 // Created by Roman Szafran on 15.03.19.
4 // Copyright (c) 2019 Wroclaw University of Science and Technology.
5 // Distributed under the Apache License, Version 2.0. You may obtain a copy of the License at
6 // http://www.apache.org/licenses/LICENSE-2.0 or see accompanying file license.txt.
7 // Redistributions of source code must retain the above copyright and license notice.
8 // ==============================================================================================
9 
10 #include "StlMesher.h"
11 
12  openvdb::FloatGrid::Ptr MF::GB::StlMesher::readSTLGrid(const std::string &fileName, const double voxelSize, const bool verbose) {
13 
14  // Read stl grid with tyti stl reader.
15  auto STLGrid = std::make_unique<MF::RW::STLReaderTyti>(fileName, verbose);
16 
17  // Linear transformation object world-to-grid and grid-to-world coordinates.
18  static openvdb::math::Transform::Ptr transformer = {
19  openvdb::math::Transform::createLinearTransform(voxelSize)};
20 
21  // Mesh adapter structure required by openvdb meshToVolume.
22  struct MeshAdapter {
23  const std::vector<tyti::stl::vec3> in_vertices;
24  const double in_voxelSize;
25 
26  MeshAdapter(tyti::stl::basic_solid<float> &grid, const double voxelSize) : in_vertices(grid.vertices), in_voxelSize(voxelSize) {}
27  size_t pointCount() const { return in_vertices.size(); }
28  size_t vertexCount(size_t n) const { return 3; }
29  size_t vertexCount() const { return 3; }
30  size_t polygonCount() const { return pointCount() / vertexCount(); }
31 
32  // Return position pos in local grid index space for polygon n and vertex v
33  void getIndexSpacePoint(size_t n, size_t v, openvdb::Vec3d &pos) const {
34  const size_t offset = 3 * n + v;
35  const openvdb::Vec3d &vertex = {in_vertices[offset].data[0], in_vertices[offset].data[1],
36  in_vertices[offset].data[2]};
37  pos = transformer->worldToIndexCellCentered(vertex).asVec3d();
38  //pos = transformer->worldToIndexNodeCentered(vertex).asVec3d();
39  }
40  };
41 
42  const MeshAdapter mesh_adapter(STLGrid->getSTLGrid_ref().first, voxelSize);
43  // Conversion of surface grid to volumetric grid (openvdb level-set grid: inside (-), outside (+)).
44  m_VDBGridPtr = openvdb::tools::meshToVolume<openvdb::FloatGrid, MeshAdapter>(mesh_adapter, *transformer, 3.0, 3.0);
45  return m_VDBGridPtr;
46  }
47 
openvdb::FloatGrid::Ptr readSTLGrid(const std::string &fileName, double voxelSize, bool verbose=false)
Reads .stl file with tyti stl reader.
Definition: StlMesher.cpp:12