FlatbuffersHelper.hpp
Go to the documentation of this file.
1 // This code is based on Jet framework.
2 // Copyright (c) 2018 Doyub Kim
3 // CubbyFlow is voxel-based fluid simulation engine for computer games.
4 // Copyright (c) 2020 CubbyFlow Team
5 // Core Part: Chris Ohk, Junwoo Hwang, Jihong Sin, Seungwoo Yoo
6 // AI Part: Dongheon Cho, Minseo Kim
7 // We are making my contributions/submissions to this project solely in our
8 // personal capacity and are not conveying any rights to any intellectual
9 // property of any third parties.
10 
11 #ifndef CUBBYFLOW_FLATBUFFERS_HELPER_HPP
12 #define CUBBYFLOW_FLATBUFFERS_HELPER_HPP
13 
14 #include <Core/Matrix/Matrix.hpp>
15 
16 #include <Flatbuffers/generated/BasicTypes_generated.h>
17 
18 namespace CubbyFlow
19 {
21 {
22  return fbs::Vector2UZ(vec.x, vec.y);
23 }
24 
26 {
27  return fbs::Vector3UZ(vec.x, vec.y, vec.z);
28 }
29 
31 {
32  return fbs::Vector2D(vec.x, vec.y);
33 }
34 
36 {
37  return fbs::Vector3D(vec.x, vec.y, vec.z);
38 }
39 
41 {
42  return Vector2UZ{ { vec.x(), vec.y() } };
43 }
44 
46 {
47  return Vector3UZ{ { vec.x(), vec.y(), vec.z() } };
48 }
49 
51 {
52  return Vector2D{ vec.x(), vec.y() };
53 }
54 
56 {
57  return Vector3D{ vec.x(), vec.y(), vec.z() };
58 }
59 
60 template <typename GridType, typename FbsFactoryFunc, typename FbsGridType>
61 void SerializeGrid(flatbuffers::FlatBufferBuilder* builder,
62  const std::vector<GridType>& gridList, FbsFactoryFunc func,
63  std::vector<flatbuffers::Offset<FbsGridType>>* fbsGridList)
64 {
65  for (const auto& grid : gridList)
66  {
67  auto type = builder->CreateString(grid->TypeName());
68 
69  std::vector<uint8_t> gridSerialized;
70  grid->Serialize(&gridSerialized);
71 
72  auto fbsGrid = func(*builder, type,
73  builder->CreateVector(gridSerialized.data(),
74  gridSerialized.size()));
75  fbsGridList->push_back(fbsGrid);
76  }
77 }
78 
79 template <typename FbsGridList, typename GridType, typename FactoryFunc>
80 void DeserializeGrid(FbsGridList* fbsGridList, FactoryFunc factoryFunc,
81  std::vector<GridType>* gridList)
82 {
83  for (const auto grid : *fbsGridList)
84  {
85  auto type = grid->type()->c_str();
86 
87  std::vector<uint8_t> gridSerialized(grid->data()->begin(),
88  grid->data()->end());
89 
90  auto newGrid = factoryFunc(type);
91  newGrid->Deserialize(gridSerialized);
92 
93  gridList->push_back(newGrid);
94  }
95 }
96 } // namespace CubbyFlow
97 
98 #endif
void DeserializeGrid(FbsGridList *fbsGridList, FactoryFunc factoryFunc, std::vector< GridType > *gridList)
Definition: FlatbuffersHelper.hpp:80
Vector2< size_t > Vector2UZ
Definition: Matrix.hpp:776
Vector2UZ FlatbuffersToCubbyFlow(const fbs::Vector2UZ &vec)
Definition: FlatbuffersHelper.hpp:40
void SerializeGrid(flatbuffers::FlatBufferBuilder *builder, const std::vector< GridType > &gridList, FbsFactoryFunc func, std::vector< flatbuffers::Offset< FbsGridType >> *fbsGridList)
Definition: FlatbuffersHelper.hpp:61
Definition: Matrix.hpp:27
Definition: pybind11Utils.hpp:20
Vector2< double > Vector2D
Definition: Matrix.hpp:774
Vector3< size_t > Vector3UZ
Definition: Matrix.hpp:789
fbs::Vector2UZ CubbyFlowToFlatbuffers(const Vector2UZ &vec)
Definition: FlatbuffersHelper.hpp:20
Vector3< double > Vector3D
Definition: Matrix.hpp:787