PageRenderTime 126ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/Utilities/MetaIO/metaTubeGraph.cxx

https://github.com/luisibanez/ITK
C++ | 589 lines | 469 code | 100 blank | 20 comment | 71 complexity | 75a4c0db6733f7263ab12e9ef8b06f09 MD5 | raw file
  1. /*============================================================================
  2. MetaIO
  3. Copyright 2000-2010 Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "metaTubeGraph.h"
  11. #ifdef _MSC_VER
  12. #pragma warning(disable:4702)
  13. #endif
  14. #include <stdio.h>
  15. #include <ctype.h>
  16. #include <string>
  17. #if (METAIO_USE_NAMESPACE)
  18. namespace METAIO_NAMESPACE {
  19. #endif
  20. /** MetaTubeGraph Constructors */
  21. MetaTubeGraph::
  22. MetaTubeGraph()
  23. :MetaObject()
  24. {
  25. if(META_DEBUG)
  26. {
  27. METAIO_STREAM::cout << "MetaTubeGraph()" << METAIO_STREAM::endl;
  28. }
  29. Clear();
  30. }
  31. MetaTubeGraph::
  32. MetaTubeGraph(const char *_headerName)
  33. :MetaObject()
  34. {
  35. if(META_DEBUG)
  36. {
  37. METAIO_STREAM::cout << "MetaTubeGraph()" << METAIO_STREAM::endl;
  38. }
  39. Clear();
  40. Read(_headerName);
  41. }
  42. MetaTubeGraph::
  43. MetaTubeGraph(const MetaTubeGraph *_tube)
  44. :MetaObject()
  45. {
  46. if(META_DEBUG)
  47. {
  48. METAIO_STREAM::cout << "MetaTubeGraph()" << METAIO_STREAM::endl;
  49. }
  50. Clear();
  51. CopyInfo(_tube);
  52. }
  53. MetaTubeGraph::
  54. MetaTubeGraph(unsigned int dim)
  55. :MetaObject(dim)
  56. {
  57. if(META_DEBUG)
  58. {
  59. METAIO_STREAM::cout << "MetaTubeGraph()" << METAIO_STREAM::endl;
  60. }
  61. Clear();
  62. }
  63. /** Destructor */
  64. MetaTubeGraph::
  65. ~MetaTubeGraph()
  66. {
  67. // Delete the list of pointers to tubes.
  68. PointListType::iterator it = m_PointList.begin();
  69. while(it != m_PointList.end())
  70. {
  71. TubeGraphPnt* pnt = *it;
  72. it++;
  73. delete pnt;
  74. }
  75. m_PointList.clear();
  76. M_Destroy();
  77. }
  78. //
  79. void MetaTubeGraph::
  80. PrintInfo() const
  81. {
  82. MetaObject::PrintInfo();
  83. METAIO_STREAM::cout << "Root = " << m_Root << METAIO_STREAM::endl;
  84. METAIO_STREAM::cout << "PointDim = " << m_PointDim << METAIO_STREAM::endl;
  85. METAIO_STREAM::cout << "NPoints = " << m_NPoints << METAIO_STREAM::endl;
  86. char str[255];
  87. MET_TypeToString(m_ElementType, str);
  88. METAIO_STREAM::cout << "ElementType = " << str << METAIO_STREAM::endl;
  89. }
  90. void MetaTubeGraph::
  91. CopyInfo(const MetaObject * _object)
  92. {
  93. MetaObject::CopyInfo(_object);
  94. }
  95. void MetaTubeGraph::
  96. PointDim(const char* pointDim)
  97. {
  98. strcpy(m_PointDim,pointDim);
  99. }
  100. const char* MetaTubeGraph::
  101. PointDim(void) const
  102. {
  103. return m_PointDim;
  104. }
  105. void MetaTubeGraph::
  106. NPoints(int npnt)
  107. {
  108. m_NPoints = npnt;
  109. }
  110. int MetaTubeGraph::
  111. NPoints(void) const
  112. {
  113. return m_NPoints;
  114. }
  115. void MetaTubeGraph::
  116. Root(int root)
  117. {
  118. m_Root = root;
  119. }
  120. int MetaTubeGraph::
  121. Root(void) const
  122. {
  123. return m_Root;
  124. }
  125. /** Clear tube information */
  126. void MetaTubeGraph::
  127. Clear(void)
  128. {
  129. if(META_DEBUG)
  130. {
  131. METAIO_STREAM::cout << "MetaTubeGraph: Clear" << METAIO_STREAM::endl;
  132. }
  133. MetaObject::Clear();
  134. // Delete the list of pointers to tubes.
  135. PointListType::iterator it = m_PointList.begin();
  136. while(it != m_PointList.end())
  137. {
  138. TubeGraphPnt* pnt = *it;
  139. it++;
  140. delete pnt;
  141. }
  142. m_PointList.clear();
  143. m_Root = 0;
  144. m_NPoints = 0;
  145. strcpy(m_PointDim, "Node r p txx txy txz tyx tyy tyz tzx tzy tzz");
  146. m_ElementType = MET_FLOAT;
  147. }
  148. /** Destroy tube information */
  149. void MetaTubeGraph::
  150. M_Destroy(void)
  151. {
  152. MetaObject::M_Destroy();
  153. }
  154. /** Set Read fields */
  155. void MetaTubeGraph::
  156. M_SetupReadFields(void)
  157. {
  158. if(META_DEBUG)
  159. {
  160. METAIO_STREAM::cout << "MetaTubeGraph: M_SetupReadFields"
  161. << METAIO_STREAM::endl;
  162. }
  163. MetaObject::M_SetupReadFields();
  164. MET_FieldRecordType * mF;
  165. mF = new MET_FieldRecordType;
  166. MET_InitReadField(mF, "Root", MET_INT, false);
  167. m_Fields.push_back(mF);
  168. mF = new MET_FieldRecordType;
  169. MET_InitReadField(mF, "PointDim", MET_STRING, true);
  170. m_Fields.push_back(mF);
  171. mF = new MET_FieldRecordType;
  172. MET_InitReadField(mF, "NPoints", MET_INT, true);
  173. m_Fields.push_back(mF);
  174. mF = new MET_FieldRecordType;
  175. MET_InitReadField(mF, "Points", MET_NONE, true);
  176. mF->terminateRead = true;
  177. m_Fields.push_back(mF);
  178. }
  179. void MetaTubeGraph::
  180. M_SetupWriteFields(void)
  181. {
  182. strcpy(m_ObjectTypeName,"TubeGraph");
  183. MetaObject::M_SetupWriteFields();
  184. MET_FieldRecordType * mF;
  185. FieldsContainerType::iterator it;
  186. mF = MET_GetFieldRecord("TransformMatrix",&m_Fields);
  187. it = m_Fields.begin();
  188. while(it != m_Fields.end())
  189. {
  190. if(*it == mF)
  191. {
  192. m_Fields.erase(it);
  193. break;
  194. }
  195. it++;
  196. }
  197. mF = MET_GetFieldRecord("Offset",&m_Fields);
  198. it = m_Fields.begin();
  199. while(it != m_Fields.end())
  200. {
  201. if(*it == mF)
  202. {
  203. m_Fields.erase(it);
  204. break;
  205. }
  206. it++;
  207. }
  208. mF = MET_GetFieldRecord("ElementSpacing",&m_Fields);
  209. it = m_Fields.begin();
  210. while(it != m_Fields.end())
  211. {
  212. if(*it == mF)
  213. {
  214. m_Fields.erase(it);
  215. break;
  216. }
  217. it++;
  218. }
  219. mF = MET_GetFieldRecord("CenterOfRotation",&m_Fields);
  220. it = m_Fields.begin();
  221. while(it != m_Fields.end())
  222. {
  223. if(*it == mF)
  224. {
  225. m_Fields.erase(it);
  226. break;
  227. }
  228. it++;
  229. }
  230. if(m_Root>0)
  231. {
  232. mF = new MET_FieldRecordType;
  233. MET_InitWriteField(mF, "Root", MET_INT,m_Root);
  234. m_Fields.push_back(mF);
  235. }
  236. if(strlen(m_PointDim)>0)
  237. {
  238. mF = new MET_FieldRecordType;
  239. MET_InitWriteField(mF, "PointDim", MET_STRING,
  240. strlen(m_PointDim),m_PointDim);
  241. m_Fields.push_back(mF);
  242. }
  243. m_NPoints = (int)m_PointList.size();
  244. mF = new MET_FieldRecordType;
  245. MET_InitWriteField(mF, "NPoints", MET_INT,m_NPoints);
  246. m_Fields.push_back(mF);
  247. mF = new MET_FieldRecordType;
  248. MET_InitWriteField(mF, "Points", MET_NONE);
  249. m_Fields.push_back(mF);
  250. }
  251. bool MetaTubeGraph::
  252. M_Read(void)
  253. {
  254. if(META_DEBUG)
  255. {
  256. METAIO_STREAM::cout << "MetaTubeGraph: M_Read: Loading Header"
  257. << METAIO_STREAM::endl;
  258. }
  259. if(!MetaObject::M_Read())
  260. {
  261. METAIO_STREAM::cout << "MetaTubeGraph: M_Read: Error parsing file"
  262. << METAIO_STREAM::endl;
  263. return false;
  264. }
  265. if(META_DEBUG)
  266. {
  267. METAIO_STREAM::cout << "MetaTubeGraph: M_Read: Parsing Header"
  268. << METAIO_STREAM::endl;
  269. }
  270. MET_FieldRecordType * mF;
  271. mF = MET_GetFieldRecord("Root", &m_Fields);
  272. if(mF->defined)
  273. {
  274. m_Root= (int)mF->value[0];
  275. }
  276. mF = MET_GetFieldRecord("NPoints", &m_Fields);
  277. if(mF->defined)
  278. {
  279. m_NPoints= (int)mF->value[0];
  280. }
  281. mF = MET_GetFieldRecord("PointDim", &m_Fields);
  282. if(mF->defined)
  283. {
  284. strcpy(m_PointDim,(char *)(mF->value));
  285. }
  286. int i;
  287. int posR = -1;
  288. int posP = -1;
  289. int posTx = -1;
  290. int posGraphNode = -1;
  291. int pntDim;
  292. char** pntVal = NULL;
  293. MET_StringToWordArray(m_PointDim, &pntDim, &pntVal);
  294. if(META_DEBUG)
  295. {
  296. METAIO_STREAM::cout << "MetaTubeGraph: Parsing point dim"
  297. << METAIO_STREAM::endl;
  298. }
  299. unsigned int j;
  300. for(j = 0; j < (unsigned int)pntDim; j++)
  301. {
  302. if(!strcmp(pntVal[j], "node") || !strcmp(pntVal[j], "Node"))
  303. {
  304. posGraphNode = j;
  305. }
  306. if(!strcmp(pntVal[j], "s") || !strcmp(pntVal[j], "S") ||
  307. !strcmp(pntVal[j], "r") || !strcmp(pntVal[j], "R") ||
  308. !strcmp(pntVal[j], "rad") || !strcmp(pntVal[j], "Rad") ||
  309. !strcmp(pntVal[j], "radius") || !strcmp(pntVal[j], "Radius"))
  310. {
  311. posR = j;
  312. }
  313. if(!strcmp(pntVal[j], "p") || !strcmp(pntVal[j], "P"))
  314. {
  315. posP = j;
  316. }
  317. if(!strcmp(pntVal[j], "txx"))
  318. {
  319. posTx = j;
  320. }
  321. }
  322. for(i=0;i<pntDim;i++)
  323. {
  324. delete [] pntVal[i];
  325. }
  326. delete [] pntVal;
  327. float v[30];
  328. if(m_Event)
  329. {
  330. m_Event->StartReading(m_NPoints);
  331. }
  332. if(m_BinaryData)
  333. {
  334. int elementSize;
  335. MET_SizeOfType(m_ElementType, &elementSize);
  336. int readSize = m_NPoints*pntDim*elementSize;
  337. char* _data = new char[readSize];
  338. m_ReadStream->read((char *)_data, readSize);
  339. int gc = m_ReadStream->gcount();
  340. if(gc != readSize)
  341. {
  342. METAIO_STREAM::cout << "MetaLine: m_Read: data not read completely"
  343. << METAIO_STREAM::endl;
  344. METAIO_STREAM::cout << " ideal = " << readSize << " : actual = " << gc
  345. << METAIO_STREAM::endl;
  346. return false;
  347. }
  348. double td;
  349. for(j=0; j<(unsigned int)m_NPoints; j++)
  350. {
  351. TubeGraphPnt* pnt = new TubeGraphPnt(m_NDims);
  352. MET_ValueToDouble(m_ElementType,_data,posGraphNode,&td);
  353. pnt->m_GraphNode=(int)td;
  354. if(posR != -1)
  355. {
  356. MET_ValueToDouble(m_ElementType, _data, posR, &td);
  357. pnt->m_R = (float)td;
  358. }
  359. if(posP != -1)
  360. {
  361. MET_ValueToDouble(m_ElementType, _data, posP, &td);
  362. pnt->m_P = (float)td;
  363. }
  364. if(posTx != -1)
  365. {
  366. for(int r=0; r<m_NDims*m_NDims; r++)
  367. {
  368. MET_ValueToDouble(m_ElementType, _data, posTx+r, &td);
  369. pnt->m_T[r] = (float)td;
  370. }
  371. }
  372. m_PointList.push_back(pnt);
  373. }
  374. delete [] _data;
  375. }
  376. else
  377. {
  378. for(j=0; j<(unsigned int)m_NPoints; j++)
  379. {
  380. if(m_Event)
  381. {
  382. m_Event->SetCurrentIteration(j+1);
  383. }
  384. TubeGraphPnt* pnt = new TubeGraphPnt(m_NDims);
  385. for(int k=0; k<pntDim; k++)
  386. {
  387. *m_ReadStream >> v[k];
  388. m_ReadStream->get();
  389. }
  390. pnt->m_GraphNode = (int)v[posGraphNode];
  391. if(posR != -1)
  392. {
  393. pnt->m_R = v[posR];
  394. }
  395. if(posP != -1)
  396. {
  397. pnt->m_P = v[posP];
  398. }
  399. if(posTx >= 0 && posTx<pntDim)
  400. {
  401. for(int r=0; r<m_NDims*m_NDims; r++)
  402. {
  403. pnt->m_T[r] = v[posTx+r];
  404. }
  405. }
  406. m_PointList.push_back(pnt);
  407. }
  408. char c = ' ';
  409. while( (c!='\n') && (!m_ReadStream->eof()))
  410. {
  411. c = m_ReadStream->get();// to avoid unrecognize charactere
  412. }
  413. }
  414. if(m_Event)
  415. {
  416. m_Event->StopReading();
  417. }
  418. return true;
  419. }
  420. MET_ValueEnumType MetaTubeGraph::
  421. ElementType(void) const
  422. {
  423. return m_ElementType;
  424. }
  425. void MetaTubeGraph::
  426. ElementType(MET_ValueEnumType _elementType)
  427. {
  428. m_ElementType = _elementType;
  429. }
  430. bool MetaTubeGraph::
  431. M_Write(void)
  432. {
  433. if(!MetaObject::M_Write())
  434. {
  435. METAIO_STREAM::cout << "MetaTubeGraph: M_Read: Error parsing file"
  436. << METAIO_STREAM::endl;
  437. return false;
  438. }
  439. /** Then copy all tubes points */
  440. if(m_BinaryData)
  441. {
  442. PointListType::const_iterator it = m_PointList.begin();
  443. PointListType::const_iterator itEnd = m_PointList.end();
  444. int elementSize;
  445. MET_SizeOfType(m_ElementType, &elementSize);
  446. char* data = new char[(m_NDims*m_NDims+3)*m_NPoints*elementSize];
  447. int i=0;
  448. int d;
  449. while(it != itEnd)
  450. {
  451. MET_DoubleToValue((double)(*it)->m_GraphNode,m_ElementType,data,i++);
  452. MET_DoubleToValue((double)(*it)->m_R,m_ElementType,data,i++);
  453. MET_DoubleToValue((double)(*it)->m_P,m_ElementType,data,i++);
  454. for(d = 0; d < m_NDims*m_NDims; d++)
  455. {
  456. MET_DoubleToValue((double)(*it)->m_T[d],m_ElementType,data,i++);
  457. }
  458. it++;
  459. }
  460. m_WriteStream->write((char *)data,
  461. (m_NDims*m_NDims+3)*m_NPoints*elementSize);
  462. m_WriteStream->write("\n",1);
  463. delete [] data;
  464. }
  465. else
  466. {
  467. PointListType::const_iterator it = m_PointList.begin();
  468. PointListType::const_iterator itEnd = m_PointList.end();
  469. int d;
  470. while(it != itEnd)
  471. {
  472. *m_WriteStream << (*it)->m_GraphNode << " ";
  473. *m_WriteStream << (*it)->m_R << " ";
  474. *m_WriteStream << (*it)->m_P << " ";
  475. for(d = 0; d < m_NDims*m_NDims; d++)
  476. {
  477. *m_WriteStream << (*it)->m_T[d] << " ";
  478. }
  479. *m_WriteStream << METAIO_STREAM::endl;
  480. it++;
  481. }
  482. }
  483. return true;
  484. }
  485. #if (METAIO_USE_NAMESPACE)
  486. };
  487. #endif