PageRenderTime 72ms CodeModel.GetById 42ms RepoModel.GetById 1ms app.codeStats 0ms

/DataStructures/Grids/IO/SvolLoader_DTTop1.0.h

https://github.com/ITKv4-LevelSets/dtgrid_library
C Header | 417 lines | 329 code | 70 blank | 18 comment | 56 complexity | 4a2f5e1f14f4f5434918ee8c332f257d MD5 | raw file
  1. /*************************************************************************************************
  2. *
  3. * Copyright (c) 2009, Michael Bang Nielsen (nielsenmb@gmail.com), Aarhus University, Denmark.
  4. * All rights reserved.
  5. *
  6. *************************************************************************************************/
  7. #ifndef _grids_svolloaderdttop1_h_
  8. #define _grids_svolloaderdttop1_h_
  9. #include <string>
  10. #include <stdlib.h>
  11. #include "DefaultException.h"
  12. #include "SvolLoaderInterface.h"
  13. #include "SvolSaver_DTTop1.0.h"
  14. namespace Grids
  15. {
  16. template<typename IndexType, typename RealType, typename DataType, typename UIntType>
  17. class SvolLoaderDTTop1 : public SvolLoaderInterface<IndexType, RealType, DataType, UIntType>
  18. {
  19. public:
  20. const static std::string idString;
  21. const static std::string header;
  22. public:
  23. void init(const typename SvolLoaderInterface<IndexType, RealType, DataType, UIntType>::InitParameters& initParameters);
  24. void load(typename SvolLoaderInterface<IndexType, RealType, DataType, UIntType>::LoadOutput *output);
  25. protected:
  26. inline bool isBigEndian();
  27. inline bool isLittleEndian();
  28. template<typename MyReal>
  29. inline void byteSwap ( MyReal& real );
  30. template<typename MyType1, typename MyType2>
  31. void loadArray(std::istream& is, MyType1 *a, unsigned int numEntries, bool swapEndian);
  32. template<typename MyIntType>
  33. void loadIntegerArray(std::istream& is, MyIntType *a, unsigned int numEntries, unsigned int intSize, bool swapEndian);
  34. template<typename MyRealType>
  35. void loadRealArray(std::istream& is, MyRealType *a, unsigned int numEntries, unsigned int realSize, bool swapEndian);
  36. void buildAAData(UIntType *aa, IndexType *index, UIntType numIndex);
  37. protected:
  38. UIntType numIndexEndMarkers;
  39. UIntType numAAEndMarkers;
  40. UIntType numValueEndMarkers;
  41. std::string fileName;
  42. };
  43. //////////////////////////////////////////////////////////////////////////
  44. // IMPLEMENTATION
  45. //////////////////////////////////////////////////////////////////////////
  46. template<typename IndexType, typename RealType, typename DataType, typename UIntType>
  47. const std::string SvolLoaderDTTop1<IndexType, RealType, DataType, UIntType>::idString = "DTG_v1.0 ";
  48. template<typename IndexType, typename RealType, typename DataType, typename UIntType>
  49. const std::string SvolLoaderDTTop1<IndexType, RealType, DataType, UIntType>::header = "DTG v1.0 ";
  50. template<typename IndexType, typename RealType, typename DataType, typename UIntType>
  51. bool SvolLoaderDTTop1<IndexType, RealType, DataType, UIntType>::isBigEndian()
  52. {
  53. short int word = 0x001;
  54. char *byte = reinterpret_cast<char *> ( &word );
  55. return byte[0] == 0;
  56. }
  57. template<typename IndexType, typename RealType, typename DataType, typename UIntType>
  58. bool SvolLoaderDTTop1<IndexType, RealType, DataType, UIntType>::isLittleEndian()
  59. {
  60. short int word = 0x001;
  61. char *byte = reinterpret_cast<char *> ( &word );
  62. return byte[0] != 0;
  63. }
  64. template<typename IndexType, typename RealType, typename DataType, typename UIntType>
  65. template<typename MyReal>
  66. void SvolLoaderDTTop1<IndexType, RealType, DataType, UIntType>::byteSwap ( MyReal& real )
  67. {
  68. unsigned char *ptr = reinterpret_cast<unsigned char *> ( &real );
  69. register int i = 0;
  70. register int j = sizeof ( MyReal ) - 1;
  71. unsigned char tmp;
  72. while ( i < j )
  73. {
  74. tmp = ptr[i];
  75. ptr[i] = ptr[j];
  76. ptr[j] = tmp;
  77. i++; j--;
  78. }
  79. }
  80. template<typename IndexType, typename RealType, typename DataType, typename UIntType>
  81. template<typename MyType1, typename MyType2>
  82. void SvolLoaderDTTop1<IndexType, RealType, DataType, UIntType>::loadArray(std::istream& is, MyType1 *a, unsigned int numEntries, bool swapEndian)
  83. {
  84. unsigned int i;
  85. MyType2 tmp;
  86. if (swapEndian)
  87. {
  88. for (i=0; i<numEntries; i++)
  89. {
  90. is.read((char *)&tmp, sizeof(MyType2));
  91. byteSwap(tmp);
  92. a[i] = (MyType1)tmp;
  93. }
  94. }
  95. else
  96. {
  97. for (i=0; i<numEntries; i++)
  98. {
  99. is.read((char *)&tmp, sizeof(MyType2));
  100. a[i] = (MyType1)tmp;
  101. }
  102. }
  103. }
  104. template<typename IndexType, typename RealType, typename DataType, typename UIntType>
  105. template<typename MyIntType>
  106. void SvolLoaderDTTop1<IndexType, RealType, DataType, UIntType>::loadIntegerArray(std::istream& is, MyIntType *a, unsigned int numEntries, unsigned int intSize, bool swapEndian)
  107. {
  108. if (intSize == sizeof(MyIntType) && !swapEndian)
  109. {
  110. is.read((char *)a, sizeof(MyIntType) * numEntries);
  111. }
  112. else
  113. {
  114. switch (intSize)
  115. {
  116. case sizeof(char):
  117. loadArray<MyIntType, char>(is, a, numEntries, swapEndian);
  118. break;
  119. case sizeof(short):
  120. loadArray<MyIntType, short>(is, a, numEntries, swapEndian);
  121. break;
  122. case sizeof(int):
  123. loadArray<MyIntType, int>(is, a, numEntries, swapEndian);
  124. break;
  125. case sizeof(long long):
  126. loadArray<MyIntType, long long>(is, a, numEntries, swapEndian);
  127. break;
  128. }
  129. }
  130. }
  131. template<typename IndexType, typename RealType, typename DataType, typename UIntType>
  132. template<typename MyRealType>
  133. void SvolLoaderDTTop1<IndexType, RealType, DataType, UIntType>::loadRealArray(std::istream& is, MyRealType *a, unsigned int numEntries, unsigned int realSize, bool swapEndian)
  134. {
  135. if (realSize == sizeof(MyRealType) && !swapEndian)
  136. {
  137. is.read((char *)a, sizeof(MyRealType) * numEntries);
  138. }
  139. else
  140. {
  141. switch (realSize)
  142. {
  143. case sizeof(float):
  144. loadArray<MyRealType, float>(is, a, numEntries, swapEndian);
  145. break;
  146. case sizeof(double):
  147. loadArray<MyRealType, double>(is, a, numEntries, swapEndian);
  148. break;
  149. }
  150. }
  151. }
  152. template<typename IndexType, typename RealType, typename DataType, typename UIntType>
  153. void SvolLoaderDTTop1<IndexType, RealType, DataType, UIntType>::buildAAData(UIntType *aa, IndexType *index, UIntType numIndex)
  154. {
  155. UIntType count = 0;
  156. for (UIntType i=0, j=0; i<numIndex; i+=2, j++)
  157. {
  158. aa[j] = count;
  159. count += static_cast<UIntType>(index[i+1]-index[i]+1);
  160. }
  161. }
  162. template<typename IndexType, typename RealType, typename DataType, typename UIntType>
  163. void SvolLoaderDTTop1<IndexType, RealType, DataType, UIntType>::init(const typename SvolLoaderInterface<IndexType, RealType, DataType, UIntType>::InitParameters& initParameters)
  164. {
  165. numIndexEndMarkers = initParameters.numIndexEndMarkers;
  166. numAAEndMarkers = initParameters.numAAEndMarkers;
  167. numValueEndMarkers = initParameters.numValueEndMarkers;
  168. fileName = initParameters.fileName;
  169. }
  170. template<typename IndexType, typename RealType, typename DataType, typename UIntType>
  171. void SvolLoaderDTTop1<IndexType, RealType, DataType, UIntType>::load(typename SvolLoaderInterface<IndexType, RealType, DataType, UIntType>::LoadOutput *output)
  172. {
  173. std::ifstream is(fileName.c_str(), ios_base::binary);
  174. if (!is)
  175. {
  176. Core::throwDefaultException("Unable to open file "+fileName+" for loading!", __FILE__, __LINE__);
  177. }
  178. // load topology
  179. {
  180. std::string line;
  181. char header[10];
  182. unsigned int indexSize, realSize, intSize;
  183. bool swapEndian;
  184. is.read(header, 10);
  185. // From this point we assume that the file is in the correct format
  186. getline(is, line); // ignore: IndexTypeTextDescription
  187. is >> line; // IndexTypeSizeInBytes
  188. is >> indexSize;
  189. getline(is, line); // eat the newline
  190. getline(is, line); // ignore: IntTypeTextDescription
  191. is >> line; // IntTypeSizeInBytes
  192. is >> intSize;
  193. getline(is, line); // eat the newline
  194. getline(is, line); // ignore: RealTypeTextDescription
  195. is >> line; // RealTypeSizeInBytes
  196. is >> realSize;
  197. getline(is, line); // eat the newline
  198. is >> line; // Endian
  199. is >> line;
  200. if ( (line == "LittleEndian" && isBigEndian()) || (line == "BigEndian" && isLittleEndian()) )
  201. {
  202. swapEndian = true;
  203. }
  204. else
  205. {
  206. swapEndian = false;
  207. }
  208. getline(is, line); // eat newline
  209. if (indexSize != sizeof(IndexType))
  210. {
  211. cerr << "Warning: Loading file, but be aware that Index size is " << sizeof(IndexType) << ", Index size in file is " << indexSize << std::endl;
  212. }
  213. if (intSize != sizeof(unsigned int))
  214. {
  215. cerr << "Warning: Loading file, but be aware that Int size is " << sizeof(unsigned int) << ", Int size in file is " << intSize << std::endl;
  216. }
  217. if (realSize != sizeof(RealType))
  218. {
  219. cerr << "Warning: Loading file, but be aware that Real size is " << sizeof(RealType) << ", Real size in file is " << realSize << std::endl;
  220. }
  221. loadIntegerArray(is, (IndexType *)output->bbox, 6, indexSize, swapEndian);
  222. loadRealArray(is, (RealType *)output->translation, 3, realSize, swapEndian);
  223. loadRealArray(is, (RealType *)output->rotation, 3, realSize, swapEndian);
  224. // 1D
  225. loadIntegerArray(is, &output->numXIndex, 1, intSize, swapEndian);
  226. output->xIndex = new IndexType[output->numXIndex+numIndexEndMarkers];
  227. loadIntegerArray(is, output->xIndex, output->numXIndex, indexSize, swapEndian);
  228. output->aa1D = new unsigned int[(output->numXIndex>>1)+numAAEndMarkers];
  229. if (SvolSaverDTTop1<IndexType, RealType, DataType, UIntType>::saveAAData)
  230. {
  231. loadIntegerArray(is, output->aa1D, output->numXIndex>>1, intSize, swapEndian);
  232. }
  233. else
  234. {
  235. buildAAData(output->aa1D, output->xIndex, output->numXIndex);
  236. }
  237. loadIntegerArray(is, &output->numVa1D, 1, intSize, swapEndian);
  238. output->va1D = new unsigned int[output->numVa1D+numValueEndMarkers];
  239. loadIntegerArray(is, output->va1D, output->numVa1D, intSize, swapEndian);
  240. // 2D
  241. loadIntegerArray(is, &output->numYIndex, 1, intSize, swapEndian);
  242. output->yIndex = new IndexType[output->numYIndex+numIndexEndMarkers];
  243. loadIntegerArray(is, output->yIndex, output->numYIndex, indexSize, swapEndian);
  244. output->aa2D = new unsigned int[(output->numYIndex>>1)+numAAEndMarkers];
  245. if (SvolSaverDTTop1<IndexType, RealType, DataType, UIntType>::saveAAData)
  246. {
  247. loadIntegerArray(is, output->aa2D, output->numYIndex>>1, intSize, swapEndian);
  248. }
  249. else
  250. {
  251. buildAAData(output->aa2D, output->yIndex, output->numYIndex);
  252. }
  253. loadIntegerArray(is, &output->numVa2D, 1, intSize, swapEndian);
  254. output->va2D = new unsigned int[output->numVa2D+numValueEndMarkers];
  255. loadIntegerArray(is, output->va2D, output->numVa2D, intSize, swapEndian);
  256. // 3D
  257. loadIntegerArray(is, &output->numZIndex, 1, intSize, swapEndian);
  258. output->zIndex = new IndexType[output->numZIndex+numIndexEndMarkers];
  259. loadIntegerArray(is, output->zIndex, output->numZIndex, indexSize, swapEndian);
  260. output->aa3D = new unsigned int[(output->numZIndex>>1)+numAAEndMarkers];
  261. if (SvolSaverDTTop1<IndexType, RealType, DataType, UIntType>::saveAAData)
  262. {
  263. loadIntegerArray(is, output->aa3D, output->numZIndex>>1, intSize, swapEndian);
  264. }
  265. else
  266. {
  267. buildAAData(output->aa3D, output->zIndex, output->numZIndex);
  268. }
  269. loadIntegerArray(is, &output->numVa3D, 1, intSize, swapEndian);
  270. output->va3D = new DataType[output->numVa3D+numValueEndMarkers];
  271. }
  272. // load values as a separate scalar field
  273. {
  274. std::string line;
  275. unsigned int dataSize, tmpInt, j;
  276. bool swapEndian;
  277. unsigned int numberOfScalarFields;
  278. // Load the scalar fields header
  279. getline(is, line);
  280. if (line != "Auxiliary Fields v1.0")
  281. {
  282. Core::throwDefaultException("Unknown auxiliary fields format: "+line, __FILE__, __LINE__);
  283. }
  284. // from this point on we assume the file format is correct
  285. is >> line; // NumberOfScalarFields
  286. is >> numberOfScalarFields;
  287. getline(is, line); // eat the newline
  288. getline(is, line); // ignore "Header DTGridTopology Auxiliary Field v1.0"
  289. getline(is, line); // ignore DataTextDescription
  290. if (line != "DataTextDescription SignedDistanceField")
  291. {
  292. Core::throwDefaultException("The first scalar field should be 'DataTextDescription SignedDistanceField' but is: "+line, __FILE__, __LINE__);
  293. }
  294. is >> line; // Endian
  295. is >> line;
  296. if ( (line == "LittleEndian" && isBigEndian()) || (line == "BigEndian" && isLittleEndian()) )
  297. {
  298. swapEndian = true;
  299. }
  300. else
  301. {
  302. swapEndian = false;
  303. }
  304. getline(is, line); // eat newline
  305. getline(is, line); // ignore DataTypeTextDescription
  306. is >> line; // DataTypeSizeInBytes
  307. is >> dataSize;
  308. getline(is, line); // eat newline
  309. getline(is, line); // ignore NumberOfDataItems, should be the same as the number of values specified by the topology
  310. output->dx = output->gamma = output->beta = output->insideConstant = output->outsideConstant = 0; // default values
  311. is >> line; // NumberOfConstants
  312. is >> tmpInt;
  313. for (j=0; j<tmpInt; j++)
  314. {
  315. is >> line; // Constant
  316. is >> line; // Name
  317. if (line == "dx")
  318. {
  319. is >> output->dx;
  320. }
  321. else if (line == "gamma")
  322. {
  323. is >> output->gamma;
  324. }
  325. else if (line == "beta")
  326. {
  327. is >> output->beta;
  328. }
  329. else if (line == "insideConstant")
  330. {
  331. is >> output->insideConstant;
  332. }
  333. else if (line == "outsideConstant")
  334. {
  335. is >> output->outsideConstant;
  336. }
  337. getline(is, line); // eat newline
  338. }
  339. // load numerical data
  340. this->loadRealArray(is, output->va3D, output->numVa3D, dataSize, swapEndian);
  341. }
  342. output->lastXIndex = output->numXIndex-1;
  343. output->lastYIndex = output->numYIndex-1;
  344. output->lastZIndex = output->numZIndex-1;
  345. }
  346. }
  347. #endif