Microflow 3D  v1.0
GeometryBuildFromSTL.cpp
Go to the documentation of this file.
1 // ==============================================================================================
2 // Microflow 3D, http://www.microflow.pwr.edu.pl/
3 // Created by Roman Szafran on 01.05.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 "GeometryBuildFromSTL.h"
11 
12 void MF::GB::GeometryBuildFromSTL::SubtractsSTLGridsFromGeometryGrid(const bool verbose) {
13 
14  float voxelSize = m_ConfigData_Ptr->getCaseFloatParam("GeometryParams.VoxelSize");
15  unsigned int nr = 1;
16  std::string GridFileNameWithoutExtension = m_ConfigData_Ptr->GeometryFile.substr(0, m_ConfigData_Ptr->GeometryFile.length() - 4);
17  std::string NewGridFileName = (GridFileNameWithoutExtension + std::to_string(nr) + ".stl");
18  std::ifstream isFile(NewGridFileName);
19  while (isFile.good()) {
20  auto NewSTLMesher_Ptr = MF::GB::StlMesher::New(NewGridFileName, voxelSize, verbose);
21  if (m_ConfigData_Ptr->getCaseFloatParam ("GeometryParams.AddInnerWall") != 0)
22  MF::GB::GeometryBuildFromSTL::AddInnerWallToGrid(NewSTLMesher_Ptr->getVDBGrid_Ptr(),
23  m_ConfigData_Ptr->getCaseFloatParam(
24  "GeometryParams.AddInnerWall"));
25  MF::GB::differenceOfTwoGrids(m_STLGeometryGrid_Ptr, NewSTLMesher_Ptr->getVDBGrid_Ptr());
26  std::cout << "Grid " << NewGridFileName << " has been subtracted from the geometry grid " << std::endl;
27  NewSTLMesher_Ptr.reset();
28  isFile.close();
29  nr++;
30  NewGridFileName = (GridFileNameWithoutExtension + std::to_string(nr) + ".stl");
31  isFile.open(NewGridFileName);
32  }
33 }
34 
36  m_AutoSettingNode_Ptr = MF::GB::AutoSettingNode::New(m_ConfigData_Ptr, m_VDBInt64GeometryGrid_Ptr, m_LatticeParameters_Ptr, m_VDBPropagationGrid_Ptr);
37  m_AutoSettingNode_Ptr->BoundaryFind();
38 }
39 
40 void MF::GB::GeometryBuildFromSTL::AddInnerWallToGrid(const openvdb::FloatGrid::Ptr & VDBGrid_Ptr, const double ThinWallValue) {
41  for (openvdb::FloatGrid::ValueOnIter iter = VDBGrid_Ptr->beginValueOn(); iter.test(); ++iter) {
42  if ((iter.getValue() < ThinWallValue) and (iter.getValue() > -ThinWallValue))
43  iter.setValue(1.0);
44  }
45 }
46 
49  m_AutoSettingNode_Ptr->DefaultTypeSet();
50  m_AutoSettingNode_Ptr->NodeTypeClass();
51 }
52 
55  m_AutoSettingNode_Ptr->BoundaryFind();
56 }
57 
60  MF::GB::GeometryBuildFromSTL::NewPropagationTest();
61  m_AutoSettingNode_Ptr->BoundaryFind();
62 }
63 
65  std::string GridFileName = (m_ConfigData_Ptr->GeometryFile.substr(0, m_ConfigData_Ptr->GeometryFile.length() - 4));
66  double PhysVoxelSize = m_ConfigData_Ptr->getCaseFloatParam("GeometryParams.PhysicalVoxelSize");
67  m_MFGrid_GeometryGrid_Ptr->saveToVTIFile<vtkIntArray>(GridFileName, "Node type", 1, PhysVoxelSize);
68 }
69 
71  std::string GridFileName = (m_ConfigData_Ptr->GeometryFile.substr(0, m_ConfigData_Ptr->GeometryFile.length() - 4));
72  std::string GeometryName = m_ConfigData_Ptr->getCaseStringParam("GeometryParams.GeometryName");
73  m_MFGrid_GeometryGrid_Ptr->setGridName(GeometryName);
74  double PhysVoxelSize = m_ConfigData_Ptr->getCaseFloatParam("GeometryParams.PhysicalVoxelSize");
75  m_MFGrid_GeometryGrid_Ptr->insertMetaDataFloat("Physical size of voxel", PhysVoxelSize);
76  m_MFGrid_GeometryGrid_Ptr->saveToVDBFile(GridFileName);
77 }
78 
80  m_PropagationTest_Ptr.reset();
81  m_PropagationGrid_Ptr.reset();
82  m_VDBPropagationGrid_Ptr = nullptr;
83  m_STLMesher_Ptr.reset();
84  m_STLGeometryGrid_Ptr = nullptr;
85  m_AutoSettingNode_Ptr.reset();
86 }
87 
88 void MF::GB::GeometryBuildFromSTL::GeometryBuildFromSTL::ReadGrid(const bool verbose) {
89  // Geometry reading and voxelization. Creation of float type of VDB levelSet grid
90  m_STLMesher_Ptr = MF::GB::StlMesher::New(m_ConfigData_Ptr->GeometryFile, m_voxelSize, verbose);
91  m_STLGeometryGrid_Ptr = m_STLMesher_Ptr->getVDBGrid_Ptr();
92 
93  // Here, one can add some operations on geometry grids (levelSet type) e.g. union etc.
94  //-------------------------------------------------------------------------------------------------------------------------------------------------
95  // Geometries with consecutive numbers 1,2,3... e.g geometry1.stl, geometry 2.stl are automatically substracted from the main grid (by default geometry.stl)
96  MF::GB::GeometryBuildFromSTL::SubtractsSTLGridsFromGeometryGrid(verbose);
97  //-------------------------------------------------------------------------------------------------------------------------------------------------
98 
99  // Change the VDB tree type to FogVolume from default LevelSet type.
100  MF::GB::setGridToFogVolume<openvdb::FloatGrid::Ptr>(m_STLGeometryGrid_Ptr, true); // voxelize all Active Tiles true/false ---> (default = true). If true all acive tiles will be voxelized and the tree will increase in size
101 
102  // Set grid all active points to values 1.
103  for (openvdb::FloatGrid::ValueOnIter iter = m_STLGeometryGrid_Ptr->beginValueOn(); iter.test(); ++iter)
104  iter.setValue(1.0);
105 
106  // Create a propagation grid that nodes contain bitmaps of active propagation directions for all active nodes.
107  m_PropagationGrid_Ptr = MF::Database::GridVDB<openvdb::Int32Grid>::New();
108  m_VDBPropagationGrid_Ptr = m_PropagationGrid_Ptr->getVDBGridPtr();
109  auto accessor = m_VDBPropagationGrid_Ptr->getAccessor();
110  for (openvdb::FloatGrid::ValueOnIter iter = m_STLGeometryGrid_Ptr->beginValueOn(); iter.test(); ++iter)
111  accessor.setValue(iter.getCoord(),1); // Fill all active voxels of propagation grid (int32 type) with 1.
112  m_PropagationTest_Ptr = MF::GU::PropagationD3Q19::New(m_VDBPropagationGrid_Ptr, m_LatticeParameters_Ptr);
113  m_PropagationTest_Ptr->PropagationTest();
114 
115  // Change STLGeometryGrid to Int64 type
118  auto accessor2 = m_VDBInt64GeometryGrid_Ptr->getAccessor();
119  for (openvdb::FloatGrid::ValueOnIter iter = m_STLGeometryGrid_Ptr->beginValueOn(); iter.test(); ++iter)
120  accessor2.setValue(iter.getCoord(),1); // Fill all active voxels of geometry grid (int64 type) with 1.
121 }
122 
123 void MF::GB::GeometryBuildFromSTL::NewPropagationTest() {
124  auto accessor = m_VDBPropagationGrid_Ptr->getAccessor();
125  for (openvdb::Int64Grid::ValueOnIter iter = m_VDBInt64GeometryGrid_Ptr->beginValueOn(); iter.test(); ++iter) {
126  MF::GB::NodeID nodeTMP;
127  nodeTMP.node_id.NodeID = iter.getValue();
128  if (nodeTMP.node_id.Node.nodeType == 0) {
129  accessor.setValueOff(iter.getCoord(), 0); // Fill solid nodes with 0 value and set it off
130  }
131  }
132  m_PropagationTest_Ptr->PropagationTest();
133 }
void Clean()
Delete some unnecessary objects.
struct MF::GB::NodeID::@0::@1 Node
static void VDBBoundaryAdd(const openvdb::Int64Grid::Ptr &VDBGrid, const std::shared_ptr< MF::Database::ConfigData > &ConfigData_Ptr)
static std::shared_ptr< MF::Database::GridVDB< T_VDBGridType > > New()
Definition: GridVDB.h:36
static std::shared_ptr< MF::GU::PropagationD3Q19 > New(const openvdb::Int32Grid::Ptr &VDBPropagationGrid_Ptr, const std::shared_ptr< MF::GU::LatticeParametersD3Q19 > &LatticeParameters_Ptr)
A class that allows to determine the proper NodeID from combined nodeType, uidThreadNr, ThreadCount, ComponentNr and PhaseNr.
Definition: NodeID.h:18
static void VDBFluidAdd(const openvdb::Int64Grid::Ptr &VDBGrid, const std::shared_ptr< MF::Database::ConfigData > &ConfigData_Ptr)
void differenceOfTwoGrids(const openvdb::FloatGrid::Ptr &gridA, const openvdb::FloatGrid::Ptr &gridB)
Difference of two VDBGrids.
void AutomaticBoundaryFind()
Boundary nodes automatic finding.
void FluidAddFromThreadFile()
Adds fluid from thread.cfg file.
void SolidAddFromThreadFile()
Adds solid from thread.cfg file.
openvdb::Int32Grid::Ptr m_VDBPropagationGrid_Ptr
Shared pointer to the VDB propagation grid of int value.
static std::shared_ptr< MF::GB::StlMesher > New(const std::string &fileName, const double voxelSize, const bool verbose=false)
Definition: StlMesher.h:32
static void VDBSolidAdd(const openvdb::Int64Grid::Ptr &VDBGrid, const std::shared_ptr< MF::Database::ConfigData > &ConfigData_Ptr)
static std::shared_ptr< MF::GB::AutoSettingNode > New(const std::shared_ptr< MF::Database::ConfigData > &ConfigData_Ptr, const openvdb::Int64Grid::Ptr &GeometryGrid_Ptr, const std::shared_ptr< MF::GU::LatticeParametersD3Q19 > &LatticeParameters_Ptr, const openvdb::Int32Grid::Ptr &PropagationGrid_Ptr)
void WriteGeometryGridToVtiFile()
Writes geometry Int32 geometry grid to .vti file.
std::shared_ptr< MF::Database::GridVDB< openvdb::Int64Grid > > m_MFGrid_GeometryGrid_Ptr
Shared pointer to the MFGrid geometry grid of Int64 value.
uint64_t NodeID
Definition: NodeID.h:29
union MF::GB::NodeID::@0 node_id
openvdb::FloatGrid::Ptr m_STLGeometryGrid_Ptr
Shared pointer to the VDB geometry grid of float value - a volumetric mesh created from the ...
void WriteGeometryGridToVDBFile()
Writes geometry Int32 geometry grid to .vti file.
openvdb::Int64Grid::Ptr m_VDBInt64GeometryGrid_Ptr
Shared pointer to the VDB geometry grid of Int64 value.
void BoundaryAddFromThreadFile()
Adds boundary from thread.cfg file.