BinaryReaderSTLOneTriangleUnitTest.cpp File Reference
#include <fstream>
#include <iostream>
#include <BinaryReader.h>
#include <Logger.h>
#include <Math/Vector.h>
#include <CMakeDefinitions.h>

Functions

int main ()
 Test of the binary reader. The files used is STL file with containing one triange. More...
 

Function Documentation

◆ main()

int main ( )

Test of the binary reader. The files used is STL file with containing one triange.

This code users BinaryReader to open a binary file called SimpleTrianlge.stl and read in currently the single tringle from this file. The triangle was created using nclab.org and contains a single triangle with normal (0,0,1) and vertex (0,1,0), (1,0,0) and (1,1,0). This is currently a UnitTest as does not require selftest data; however, it does require he file SimpleTrianlge.stl to exist

43 {
44 
45  std::ifstream STLFile;
46 
47  std::string directory(getMercuryDPMSourceDir());
48 
49  BinaryReader STLReader(directory+"/Drivers/ImportTools/ExampleSTLFiles/SimpleTrianlge.stl");
50 
51  //First read the 80 character header
52  std::string header;
53  header=STLReader.readString(80);
54  //logger(INFO, "Header : %" ,header);
55 
56  //The next four characters contain at unsigned int which is the number of triangeles
57  unsigned int numTriangles = STLReader.readUnsignedInt(4);
58  //logger(INFO, "Number of traingles: %", numTriangles);
59 
60  if (numTriangles!=1)
61  logger(FATAL,"Failed to read the correct number of triangles");
62 
63  double xTmp,yTmp,zTmp;
64 
65  for (unsigned int i=0; i<(numTriangles);i++)
66  {
67 
68 
69  xTmp = STLReader.readFloat(4);
70  yTmp = STLReader.readFloat(4);
71  zTmp = STLReader.readFloat(4);
72 
73  Vec3D Normal(xTmp,yTmp,zTmp);
74 
75  if (!(Normal.isEqualTo(Vec3D(0,0,1),1e-10)))
76  logger(FATAL,"The normal has been misread");
77 
78 
79  xTmp = STLReader.readFloat(4);
80  yTmp = STLReader.readFloat(4);
81  zTmp = STLReader.readFloat(4);
82 
83  Vec3D Point1(xTmp,yTmp,zTmp);
84 
85  if (!(Point1.isEqualTo(Vec3D(0,1,0),1e-10)))
86  logger(FATAL,"The first vertex has been misread");
87 
88  xTmp = STLReader.readFloat(4);
89  yTmp = STLReader.readFloat(4);
90  zTmp = STLReader.readFloat(4);
91 
92  Vec3D Point2(xTmp,yTmp,zTmp);
93 
94  if (!(Point2.isEqualTo(Vec3D(1,0,0),1e-10)))
95  logger(FATAL,"The second vertex has been misread");
96 
97 
98  xTmp = STLReader.readFloat(4);
99  yTmp = STLReader.readFloat(4);
100  zTmp = STLReader.readFloat(4);
101 
102  Vec3D Point3(xTmp,yTmp,zTmp);
103 
104  if (!(Point3.isEqualTo(Vec3D(1,1,0),1e-10)))
105  logger(FATAL,"The third vertex has been misread");
106 
107  //Now ignore (read) the two dummy characters
109 
110  }
111 
112 
113 }
const std::string getMercuryDPMSourceDir()
This file is used for generating definitions that give access to CMakeVariables from within a cpp fil...
Definition: CMakeDefinitions.cc:32
LL< Log::FATAL > FATAL
Definition of the different loglevels by its wrapper class LL. These are used as tags in template met...
Definition: Logger.cc:52
Logger< MERCURYDPM_LOGLEVEL > logger("MercuryKernel")
Definition of different loggers with certain modules. A user can define its own custom logger here.
This gives functionality to read information from binary formats like STL etc. This class is complete...
Definition: BinaryReader.h:37
double readFloat(unsigned int size)
read the next so many bytes as a double (not in this case they were saves as a float orgainlly)
Definition: BinaryReader.cc:108
void ignoreChar(unsigned int size)
read and ignore the next number of characters
Definition: BinaryReader.cc:133
unsigned int readUnsignedInt(unsigned int size)
read the next so many bytes as a unsined int
Definition: BinaryReader.cc:121
std::string readString(unsigned int numChar)
reads the next so many Characters (bytes) as a std::string
Definition: BinaryReader.cc:80
Definition: BinaryReaderSTL1by1by1bySquareUnitTest.cpp:69
Definition: Vector.h:51
const std::complex< Mdouble > i
Definition: ExtendedMath.h:51

References FATAL, getMercuryDPMSourceDir(), constants::i, BinaryReader::ignoreChar(), Vec3D::isEqualTo(), logger, BinaryReader::readFloat(), BinaryReader::readString(), and BinaryReader::readUnsignedInt().