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