Microflow 3D  v1.0
GridVDB.h
Go to the documentation of this file.
1 // ==============================================================================================
2 // Microflow 3D, http://www.microflow.pwr.edu.pl/
3 // Created by Roman Szafran on 04.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 # pragma once
11 
12 // OpenVDB
13 #include <openvdb/openvdb.h>
14 
15 #include <memory>
16 
19 #include "MFDatabase/ConfigData.h"
20 
21 namespace MF {
22  namespace Database {
23 
29 
30  template<class T_VDBGridType> // T_VDBGridType = <openvdb::FloatGrid>, <openvdb::Int32Grid>, <openvdb::DobleGrid> etc.
31  class GridVDB {
32  public:
33  GridVDB() = default;
34  ~GridVDB() = default;
35 
36  static std::shared_ptr<MF::Database::GridVDB<T_VDBGridType>> New() {
37  auto newGrid_Ptr = std::make_shared<MF::Database::GridVDB<T_VDBGridType>>();
38  newGrid_Ptr->m_VDBGrid_Ptr = T_VDBGridType::create();
39  return newGrid_Ptr;
40  }
41 
42  std::shared_ptr<T_VDBGridType> getVDBGridPtr() {return m_VDBGrid_Ptr;};
43 
44  inline void setGridName(const std::string &GridName) { m_VDBGrid_Ptr->setName(GridName);};
45 
46  inline void insertMetaDataFloat(const std::string &MetaName, float value) { m_VDBGrid_Ptr->insertMeta(MetaName, openvdb::FloatMetadata(value));};
47 
48  inline void insertMetaDataInt(const std::string &MetaName, int32_t value) { m_VDBGrid_Ptr->insertMeta(MetaName, openvdb::Int32Metadata(value));};
49 
50  inline void insertMetaDataDouble(const std::string &MetaName, double value) {m_VDBGrid_Ptr->insertMeta(MetaName, openvdb::DoubleMetadata(value));};
51 
52  inline void insertMetaDataString(const std::string &MetaName, const std::string &string) {m_VDBGrid_Ptr->insertMeta(MetaName, openvdb::StringMetadata(string)); };
53 
54  template <typename T_ValueType> // T_ValueType = <int>, <double>, <string> etc.
55  const T_ValueType &getMetaData(const std::string &MetaName) { return m_VDBGrid_Ptr->template metaValue<T_ValueType>(MetaName); };
56 
57  inline void setGridToFogVolume(bool voxelizeActiveTiles = false) {openvdb::tools::sdfToFogVolume(m_VDBGrid_Ptr);
58  if (voxelizeActiveTiles) m_VDBGrid_Ptr->treePtr()->voxelizeActiveTiles();};
59 
60  inline void setGridBackgroundValue(float value) { openvdb::tools::changeBackground(m_VDBGrid_Ptr->tree(), value); };
61 
62  inline T_VDBGridType getGridNewCopy() { return m_VDBGrid_Ptr->deepCopy(); };
63 
64  inline void saveToVDBFile(const std::string &FileName) {auto w = MF::RW::VDBWriter<T_VDBGridType>::New(m_VDBGrid_Ptr, FileName);};
65 
67  template<typename T1_VTKArrayType> // T1_VTKArrayType = <vtkFloatArray>, <vtkDoubleArray>, <vtkIntArray> etc.
68  void saveToVTIFile(const std::string &FileName, const std::string &DataName, int numComponents, float voxelSize) {
69  auto w = MF::RW::VTIWriter<T_VDBGridType>::New(m_VDBGrid_Ptr, FileName, voxelSize);
70  w->template addDataFromGrid<T1_VTKArrayType>(DataName,numComponents);
71  w->addNodeCoordinateFromGrid();
72  w->writeFile();
73  };
74 
76  void saveAllDataToVTIFile(const std::shared_ptr<MF::Database::ThreadArray> & ThreadArray_Ptr, const std::shared_ptr<MF::Database::ConfigData>& ConfigData_Ptr,
77  const std::string &FileName) {
78  double PhysVoxelSize = ConfigData_Ptr->getCaseFloatParam("GeometryParams.PhysicalVoxelSize");
79  auto w = MF::RW::VTIWriter<T_VDBGridType>::New(m_VDBGrid_Ptr, FileName, PhysVoxelSize);
80  if (ConfigData_Ptr->getCaseBooleanParam("VTKFileParams.NodeID_Save"))
81  w->addNodeIDFromGrid();
82  if (ConfigData_Ptr->getCaseBooleanParam("VTKFileParams.NodeUidThread_Save"))
83  w->addUidThreadFromThread(ThreadArray_Ptr);
84  if (ConfigData_Ptr->getCaseBooleanParam("VTKFileParams.NodeType_Save"))
85  w->addNodeTypeFromThread(ThreadArray_Ptr);
86  if (ConfigData_Ptr->getCaseBooleanParam("VTKFileParams.NodeCoordinate_Save"))
87  w->addNodeCoordinateFromThread(ThreadArray_Ptr);
88  if (ConfigData_Ptr->getCaseBooleanParam("VTKFileParams.VelocityLB_Save"))
89  w->addVelocityLBFromThread(ThreadArray_Ptr);
90  if (ConfigData_Ptr->getCaseBooleanParam("VTKFileParams.RhoLB_Save"))
91  w->addRhoLBFromThread(ThreadArray_Ptr);
92  if (ConfigData_Ptr->getCaseBooleanParam("VTKFileParams.RhoPhys_Save"))
93  w->addRhoPhysFromThread(ThreadArray_Ptr);
94  if (ConfigData_Ptr->getCaseBooleanParam("VTKFileParams.PressurePhys_Save"))
95  w->addPressPhysFromThread(ThreadArray_Ptr);
96  if (ConfigData_Ptr->getCaseBooleanParam("VTKFileParams.VelocityPhys_Save"))
97  w->addVelocityPhysFromThread(ThreadArray_Ptr);
98  if (ConfigData_Ptr->getCaseBooleanParam("VTKFileParams.FQ19_Save"))
99  w->addFQ19FromThread(ThreadArray_Ptr);
100  if (ConfigData_Ptr->getCaseBooleanParam("VTKFileParams.PropagationDirections_Save"))
101  w->addPropagationDirFromThread(ThreadArray_Ptr);
102  if (ConfigData_Ptr->getCaseBooleanParam("VTKFileParams.InitialVelocityLB_Save"))
103  w->addInitialVelocityLBFromThread(ThreadArray_Ptr);
104  if (ConfigData_Ptr->getCaseBooleanParam("VTKFileParams.InitialRhoLB_Save"))
105  w->addInitialRhoLBFromThread(ThreadArray_Ptr);
106  if (ConfigData_Ptr->getCaseBooleanParam("VTKFileParams.ThreadsNamed_Save"))
107  w->addNodeThreadsByNameThread(ThreadArray_Ptr);
108  w->writeFile();
109  };
110 
111  private:
112  std::shared_ptr<T_VDBGridType> m_VDBGrid_Ptr;
113 
114  };
115 
116  } /* namespace Database */
117 } /* namespace MF */
void insertMetaDataFloat(const std::string &MetaName, float value)
Adds a grid name to a VDBGrid.
Definition: GridVDB.h:46
void insertMetaDataInt(const std::string &MetaName, int32_t value)
Adds a float metadata to a VDBGrid.
Definition: GridVDB.h:48
void setGridName(const std::string &GridName)
Returns a VDBGrid_Ptr.
Definition: GridVDB.h:44
static std::shared_ptr< MF::Database::GridVDB< T_VDBGridType > > New()
Definition: GridVDB.h:36
void saveAllDataToVTIFile(const std::shared_ptr< MF::Database::ThreadArray > &ThreadArray_Ptr, const std::shared_ptr< MF::Database::ConfigData > &ConfigData_Ptr, const std::string &FileName)
Saves all data specified in the case_params.cfg file to .vti file. Parameters are calculated from dat...
Definition: GridVDB.h:76
void setGridToFogVolume(bool voxelizeActiveTiles=false)
Returns the metadata of a type T_ValueType = <int>, <double>, <string> from VDBGrid.
Definition: GridVDB.h:57
void setGridBackgroundValue(float value)
Definition: GridVDB.h:60
void insertMetaDataDouble(const std::string &MetaName, double value)
Adds a int metadata to a VDBGrid.
Definition: GridVDB.h:50
void insertMetaDataString(const std::string &MetaName, const std::string &string)
Adds a double metadata to a VDBGrid.
Definition: GridVDB.h:52
T_VDBGridType getGridNewCopy()
Sets the background value of a VDBGrid.
Definition: GridVDB.h:62
The GridVDB class provides some additional functionalities to standard VDB grid object. It is used for storage of sparse geometry points.
Definition: GridVDB.h:31
const T_ValueType & getMetaData(const std::string &MetaName)
Adds a string metadata to a VDBGrid.
Definition: GridVDB.h:55
static std::shared_ptr< MF::RW::VDBWriter< T_VDBGridType > > New(const std::shared_ptr< T_VDBGridType > &Grid_Ptr, const std::string &FileName)
Definition: VDBWriter.h:40
static std::shared_ptr< MF::RW::VTIWriter< T_VDBGridType > > New(std::shared_ptr< T_VDBGridType > VDBGrid_Ptr, const std::string &FileName, float voxelSize)
Definition: VTIWriter.h:62
void saveToVTIFile(const std::string &FileName, const std::string &DataName, int numComponents, float voxelSize)
Saves data stored in VDB grid to a file.
Definition: GridVDB.h:68
std::shared_ptr< T_VDBGridType > getVDBGridPtr()
Definition: GridVDB.h:42
void saveToVDBFile(const std::string &FileName)
Copies of a VDBGrid.
Definition: GridVDB.h:64