Helios++
Helios software for LiDAR simulations
SerializationTest.h
1 #pragma once
2 
3 #include <string>
4 #include "BaseTest.h"
5 #include <DetailedVoxel.h>
6 #include <Scene.h>
7 #include <SerialIO.h>
8 #include <vector>
9 #include <cstdio>
10 
11 namespace HeliosTests{
12 
19 class SerializationTest : public BaseTest{
20 public:
21  // *** CONSTRUCTOR *** //
22  // ********************* //
26  SerializationTest() : BaseTest("Serialization test"){}
27 
28  // *** R U N *** //
29  // *************** //
33  bool run() override;
34 
35  // *** UTILS *** //
36  // *************** //
44  bool validate(DetailedVoxel &dv1, DetailedVoxel &dv2);
51  bool validate(Voxel &v1, Voxel &v2);
59  bool validate(AABB &box1, AABB &box2);
60 
67  bool validate(Triangle &t1, Triangle &t2);
68 };
69 
71  std::string path = "SerializationTest.tmp";
72 
73  // Detailed voxel serialization test
74  DetailedVoxel dv1(
75  1.0,
76  2.0,
77  1.0,
78  1.0,
79  std::vector<int>({0,1,2}),
80  std::vector<double>({1.0, 1.5, 2.0, 2.5, 3.0})
81  );
82  dv1.part = std::make_shared<ScenePart>();
83  dv1.part->mPrimitives.push_back(&dv1);
84  dv1.part->onRayIntersectionMode = "TRANSMITTIVE";
85  dv1.material = std::make_shared<Material>();
86  dv1.material->ka[0] = 1.0;
87  dv1.material->ka[0] = 2.0;
88  dv1.material->ka[0] = 3.0;
89  dv1.material->ka[0] = 4.0;
91  sio->write<DetailedVoxel>(path, &dv1);
94  if(!validate(dv1,*dv2)) return false;
95 
96  // Scene serialization test
97  size_t nRepeats = 32;
98  Scene scene1;
99  Vertex t1v1; t1v1.pos = glm::dvec3(0.0, 0.0, 0.0);
100  Vertex t1v2; t1v2.pos = glm::dvec3(0.0, 0.0, 4.0);
101  Vertex t1v3; t1v3.pos = glm::dvec3(2.0, 0.0, 0.0);
102  Triangle t1(t1v1, t1v2, t1v3);
103  Voxel v1(1.0, 1.0, 1.0, 1.0);
104  AABB box1(glm::dvec3(0.0, 0.0, 0.0), glm::dvec3(5.0, 5.0, 5.0));
105  scene1.primitives.push_back(dv1.clone());
106  scene1.primitives.push_back(t1.clone());
107  scene1.primitives.push_back(v1.clone());
108  scene1.primitives.push_back(box1.clone());
109  for(size_t i = 0 ; i < nRepeats ; i++)
110  scene1.primitives.push_back(dv1.clone());
111  shared_ptr<ScenePart> part = make_shared<ScenePart>();
112  for(Primitive *prim : scene1.primitives){
113  prim->part = part;
114  part->mPrimitives.push_back(prim);
115  }
116  scene1.finalizeLoading(true);
117  shared_ptr<KDGroveFactory> kdgf = scene1.getKDGroveFactory();
118  scene1.setKDGroveFactory(nullptr);
119  scene1.writeObject(path);
120  scene1.setKDGroveFactory(kdgf);
121  Scene *scene2 = Scene::readObject(path);
122  scene2->setKDGroveFactory(kdgf);
123  scene2->finalizeLoading(true);
124  if(!validate(dv1, *(DetailedVoxel *) scene2->primitives[0])) return false;
125  if(!validate(t1, *(Triangle *) scene2->primitives[1])) return false;
126  if(!validate(v1, *(Voxel *) scene2->primitives[2])) return false;
127  if(!validate(box1, *(AABB *) scene2->primitives[3])) return false;
128  for(size_t i = 0 ; i < nRepeats ; i++){
129  if(!validate(dv1, *(DetailedVoxel *) scene2->primitives[i+4]))
130  return false;
131  }
132 
133 
134  // Remove temporary file
135  std::remove(path.c_str());
136 
137  // Remove stuff allocated at serialization
138  delete dv2;
139  delete scene2;
140 
141  // Successfully reached end of test
142  return true;
143 }
144 
145 // *** UTILS *** //
146 // *************** //
148  DetailedVoxel &dv1, DetailedVoxel &dv2
149 ){
150  glm::dvec3 dv1c = dv1.getCentroid();
151  glm::dvec3 dv2c = dv2.getCentroid();
152  if(dv1c.x!=dv2c.x || dv1c.y!=dv2c.y || dv1c.z!=dv2c.z) return false;
153  if(dv1.halfSize!=dv2.halfSize) return false;
154  for(size_t i = 0 ; i < dv1.getNumberOfIntValues() ; i++){
155  if(dv1.getIntValue(i)!=dv2.getIntValue(i)) return false;
156  }
157  for(size_t i = 0 ; i < dv1.getNumberOfDoubleValues() ; i++){
158  if(dv1[i]!=dv2[i]) return false;
159  }
160  if(dv1.material == nullptr && dv2.material != nullptr) return false;
161  if(dv1.material != nullptr && dv2.material == nullptr) return false;
162  if(dv1.material != nullptr && dv2.material != nullptr) {
163  for (size_t i = 0; i < 4; i++) {
164  if (dv1.material->ka[i] != dv2.material->ka[i]) return false;
165  }
166  }
167  return true;
168 }
170  Voxel &v1, Voxel &v2
171 ){
172  glm::dvec3 v1c = v1.getCentroid();
173  glm::dvec3 v2c = v2.getCentroid();
174  if(v1c.x!=v2c.x || v1c.y!=v2c.y || v1c.z!=v2c.z) return false;
175  if(v1.halfSize!=v2.halfSize) return false;
176  return true;
177 }
179  AABB &box1, AABB &box2
180 ){
181  double minX1 = box1.getMin().x; double maxX1 = box1.getMax().x;
182  double minY1 = box1.getMin().y; double maxY1 = box1.getMax().y;
183  double minZ1 = box1.getMin().z; double maxZ1 = box1.getMax().z;
184  double minX2 = box2.getMin().x; double maxX2 = box2.getMax().x;
185  double minY2 = box2.getMin().y; double maxY2 = box2.getMax().y;
186  double minZ2 = box2.getMin().z; double maxZ2 = box2.getMax().z;
187 
188  if (minX1 != minX2) return false;
189  if (minY1 != minY2) return false;
190  if (minZ1 != minZ2) return false;
191 
192  if (maxX1 != maxX2) return false;
193  if (maxY1 != maxY2) return false;
194  if (maxZ1 != maxZ2) return false;
195 
196  return true;
197 }
198 
200  double x1, x2, y1, y2, z1, z2;
201  for(size_t i = 0 ; i < t1.getNumVertices() ; i++){
202  x1 = t1.verts[i].getX();
203  x2 = t2.verts[i].getX();
204  if(x1 != x2) return false;
205  y1 = t1.verts[i].getY();
206  y2 = t2.verts[i].getY();
207  if(y1 != y2) return false;
208  z1 = t1.verts[i].getZ();
209  z2 = t2.verts[i].getZ();
210  if(z1 != z2) return false;
211  }
212  return true;
213 }
214 
215 
216 
217 
218 }
Class representing an Axis Aligned Bounding Box (AABB)
Definition: AABB.h:10
glm::dvec3 const & getMax() const
Get the max value for each coordinate of the axis aligned bounding box.
Definition: AABB.h:109
glm::dvec3 const & getMin() const
Get the min value for each coordinate of the axis aligned bounding box.
Definition: AABB.h:103
Primitive * clone() override
Generate a clone of the primitive.
Definition: AABB.cpp:17
Class which extends Voxel to support AMAPVox format with extra features.
Definition: DetailedVoxel.h:15
size_t getNumberOfDoubleValues() const
Obtain the number of double values defining the detailed voxel.
Definition: DetailedVoxel.h:191
int getIntValue(size_t index) const
Obtain integer value at given index.
Definition: DetailedVoxel.h:206
Primitive * clone() override
Definition: DetailedVoxel.cpp:6
size_t getNumberOfIntValues() const
Obtain the number of integer values defining the detailed voxel.
Definition: DetailedVoxel.h:185
BaseTest class.
Definition: BaseTest.h:20
Test serialization.
Definition: SerializationTest.h:19
SerializationTest()
Serialization test constructor.
Definition: SerializationTest.h:26
bool validate(DetailedVoxel &dv1, DetailedVoxel &dv2)
Check two detailed voxels are equal.
Definition: SerializationTest.h:147
bool run() override
Definition: SerializationTest.h:70
Abstract class defining the common behavior for all primitives.
Definition: Primitive.h:24
std::shared_ptr< ScenePart > part
Shared pointer to the scene part the primitive belongs to.
Definition: Primitive.h:57
std::shared_ptr< Material > material
Shared pointer to the material defining certain properties such as reflectance, specularity,...
Definition: Primitive.h:63
Class representing a scene asset.
Definition: Scene.h:28
virtual void setKDGroveFactory(std::shared_ptr< KDGroveFactory > const kdgf)
Set the KDGrove factory to be used by the scene.
Definition: Scene.h:399
static Scene * readObject(std::string path)
Read serialized scene from given file.
Definition: Scene.cpp:392
std::vector< Primitive * > primitives
Vector of primitives composing the scene.
Definition: Scene.h:132
virtual void writeObject(std::string path)
Serialize the scene and write it to given output file.
Definition: Scene.cpp:385
virtual std::shared_ptr< KDGroveFactory > getKDGroveFactory() const
Obtain the KDGrove factory used by the scene.
Definition: Scene.h:392
bool finalizeLoading(bool const safe=false)
Handle scene loading finish process.
Definition: Scene.cpp:69
Utils for Input/Output operations of serializable objects.
Definition: SerialIO.h:13
void write(std::string const &path, SerialClass const *object, bool fastCompression=true)
static SerialIO * getInstance()
Definition: SerialIO.cpp:6
SerialClass * read(std::string const &path, bool const fastCompression=true)
Class representing triangle primitive.
Definition: Triangle.h:13
Vertex verts[3]
The 3 vertices defining the triangle.
Definition: Triangle.h:66
Primitive * clone() override
Definition: Triangle.cpp:25
size_t getNumVertices() override
Definition: Triangle.h:108
Class representing a vertex.
Definition: Vertex.h:14
glm::dvec3 pos
Vertex 3D position.
Definition: Vertex.h:39
double getX() const
Get the X coordinate of vertex position.
Definition: Vertex.h:98
double getZ() const
Get the Z coordinate of vertex position.
Definition: Vertex.h:116
double getY() const
Get the Y coordinate of vertex position.
Definition: Vertex.h:107
Class representing a voxel primitive.
Definition: Voxel.h:11
Primitive * clone() override
Definition: Voxel.cpp:17
glm::dvec3 getCentroid() override
Definition: Voxel.cpp:41
double halfSize
Half of the voxel sizxe.
Definition: Voxel.h:64