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 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkMNITagPointReader.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
/*=========================================================================
Copyright (c) 2006 Atamai, Inc.
Use, modification and redistribution of the software, in source or
binary forms, are permitted provided that the following terms and
conditions are met:
1) Redistribution of the source code, in verbatim or modified
form, must retain the above copyright notice, this license,
the following disclaimer, and any notices that refer to this
license and/or the following disclaimer.
2) Redistribution in binary form must include the above copyright
notice, a copy of this license and the following disclaimer
in the documentation or with other materials provided with the
distribution.
3) Modified copies of the source code must be clearly marked as such,
and must not be misrepresented as verbatim copies of the source code.
THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS"
WITHOUT EXPRESSED OR IMPLIED WARRANTY INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE. IN NO EVENT SHALL ANY COPYRIGHT HOLDER OR OTHER PARTY WHO MAY
MODIFY AND/OR REDISTRIBUTE THE SOFTWARE UNDER THE TERMS OF THIS LICENSE
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, LOSS OF DATA OR DATA BECOMING INACCURATE
OR LOSS OF PROFIT OR BUSINESS INTERRUPTION) ARISING IN ANY WAY OUT OF
THE USE OR INABILITY TO USE THE SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGES.
=========================================================================*/
#include "vtkMNITagPointReader.h"
#include "vtkObjectFactory.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkStreamingDemandDrivenPipeline.h"
#include "vtkPolyData.h"
#include "vtkPointData.h"
#include "vtkPoints.h"
#include "vtkCellArray.h"
#include "vtkDoubleArray.h"
#include "vtkIntArray.h"
#include "vtkStringArray.h"
#include <cctype>
#include <sys/types.h>
#include <sys/stat.h>
#include <string>
#include <vector>
#include <vtksys/SystemTools.hxx>
//--------------------------------------------------------------------------
vtkStandardNewMacro(vtkMNITagPointReader);
//-------------------------------------------------------------------------
vtkMNITagPointReader::vtkMNITagPointReader()
{
this->FileName = 0;
this->NumberOfVolumes = 1;
this->LineNumber = 0;
this->Comments = 0;
this->SetNumberOfInputPorts(0);
this->SetNumberOfOutputPorts(2);
}
//-------------------------------------------------------------------------
vtkMNITagPointReader::~vtkMNITagPointReader()
{
delete [] this->FileName;
delete [] this->Comments;
}
//-------------------------------------------------------------------------
void vtkMNITagPointReader::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
os << indent << "FileName: "
<< (this->FileName ? this->FileName : "none") << "\n";
os << indent << "NumberOfVolumes: "
<< this->NumberOfVolumes << "\n";
os << indent << "Comments: "
<< (this->Comments ? this->Comments : "none") << "\n";
}
//-------------------------------------------------------------------------
int vtkMNITagPointReader::CanReadFile(const char* fname)
{
// First make sure the file exists. This prevents an empty file
// from being created on older compilers.
struct stat fs;
if(stat(fname, &fs) != 0)
{
return 0;
}
// Try to read the first line of the file.
int status = 0;
ifstream infile(fname);
if (infile.good())
{
status = 1;
char linetext[256];
infile.getline(linetext, 256);
if (strncmp(linetext, "MNI Tag Point File", 18) != 0)
{
status = 0;
}
infile.close();
}
return status;
}
//-------------------------------------------------------------------------
// Internal function to read in a line up to 256 characters and then
// skip to the next line in the file.
int vtkMNITagPointReader::ReadLine(
istream &infile, std::string &linetext, std::string::iterator &pos)
{
this->LineNumber++;
std::getline(infile, linetext);
pos = linetext.begin();
if (infile.fail())
{
if (infile.eof())
{
return 0;
}
}
return 1;
}
//-------------------------------------------------------------------------
// Skip all blank lines or comment lines and return the first useful line
int vtkMNITagPointReader::ReadLineAfterComments(
istream &infile, std::string &linetext, std::string::iterator &pos)
{
// Skip over any comment lines or blank lines.
// Comment lines start with '%'
std::string comments;
do
{
this->ReadLine(infile, linetext, pos);
while (pos != linetext.end() && isspace(*pos))
{
++pos;
}
if (linetext.length() != 0 && linetext[0] == '%')
{
if (comments.length() > 0)
{
comments.push_back('\n');
}
if (linetext.length())
{
comments.append(linetext);
}
}
else if (linetext.length() != 0 && pos != linetext.end())
{
delete [] this->Comments;
this->Comments = new char[comments.length() + 1];
strncpy(this->Comments, comments.c_str(), comments.length());
this->Comments[comments.length()] = '\0';
return 1;
}
}
while (infile.good());
return 0;
}
//-------------------------------------------------------------------------
// Skip all whitespace, reading additional lines if necessary if nl != 0
int vtkMNITagPointReader::SkipWhitespace(
istream &infile, std::string &linetext, std::string::iterator &pos,
int nl)
{
while (infile.good())
{
// Skip leading whitespace
while (pos != linetext.end() && isspace(*pos))
{
++pos;
}
if (pos != linetext.end())
{
return 1;
}
if (nl == 0)
{
break;
}
this->ReadLine(infile, linetext, pos);
}
return 0;
}
//-------------------------------------------------------------------------
// Read the left hand side of a statement, including the equals sign
// and any whitespace following the equals.
int vtkMNITagPointReader::ParseLeftHandSide(
istream &infile, std::string &linetext, std::string::iterator &pos,
std::string &identifier)
{
identifier.clear();
// Read alphanumeric plus underscore
if (pos != linetext.end() && !isdigit(*pos))
{
while (pos != linetext.end() && (isalnum(*pos) || *pos == '_'))
{
identifier.push_back(*pos);
++pos;
}
}
// Check for equals
this->SkipWhitespace(infile, linetext, pos, 1);
if (pos == linetext.end() || *pos != '=')
{
return 0;
}
// Eat the equals
++pos;
// Skip ahead to the value part of the statement
this->SkipWhitespace(infile, linetext, pos, 1);
return 1;
}
//-------------------------------------------------------------------------
// Read a string value. The terminating semicolon will be read, but
// won't be included in the output string. Neither will any
// whitespace occurring before the semicolon. The string may not be
// split across multiple lines.
int vtkMNITagPointReader::ParseStringValue(
istream &infile, std::string &linetext, std::string::iterator &pos,
std::string &data)
{
this->SkipWhitespace(infile, linetext, pos, 0);
if (pos != linetext.end() && *pos == '\"')
{
// eat the opening quote
++pos;
// read the string
while (pos != linetext.end() && *pos != '\"')
{
char c = *pos;
++pos;
if (c == '\\')
{
if (pos != linetext.end())
{
c = '\0';
static char ctrltable[] = {
'\a', 'a', '\b', 'b', '\f', 'f', '\n', 'n', '\r', 'r',
'\t', 't', '\v', 'v', '\\', '\\', '\"', '\"', '\0', '\0'
};
if (*pos >= 0 && *pos <= 9)
{
for (int j = 0; j < 3 && pos != linetext.end() &&
*pos >= 0 && *pos <= 9; ++j, ++pos)
{
c = ((c << 3) | (*pos - '0'));
}
}
else if (*pos == 'x')
{
++pos;
for (int j = 0; j < 2 && pos != linetext.end() &&
isalnum(*pos); ++j, ++pos)
{
char x = tolower(*pos);
if (x >= '0' && x <= '9')
{
c = ((c << 4) | (x - '0'));
}
else if (x >= 'a' && x <= 'f')
{
c = ((c << 4) | (x - 'a' + 10));
}
}
}
else
{
for (int ci = 0; ctrltable[ci] != '\0'; ci += 2)
{
if (*pos == ctrltable[ci+1])
{
c = ctrltable[ci];
break;
}
}
if (c == '\0')
{
c = *pos;
}
++pos;
}
}
}
data.push_back(c);
}
}
if (pos == linetext.end())
{
vtkErrorMacro("Syntax error " << this->FileName
<< ":" << this->LineNumber);
return 0;
}
// eat the trailing quote
++pos;
return 1;
}
//-------------------------------------------------------------------------
// Read an int value
int vtkMNITagPointReader::ParseIntValues(
istream &infile, std::string &linetext, std::string::iterator &pos,
int *values, int n)
{
this->SkipWhitespace(infile, linetext, pos, 0);
int i = 0;
while (pos != linetext.end() && *pos != ';' && i < n)
{
const char *cp = linetext.c_str() + (pos - linetext.begin());
char *ep = 0;
long val = strtol(cp, &ep, 10);
if (ep == cp)
{
vtkErrorMacro("Syntax error " << this->FileName
<< ":" << this->LineNumber);
return 0;
}
pos += (ep - cp);
values[i++] = static_cast<int>(val);
this->SkipWhitespace(infile, linetext, pos, 0);
}
if (i != n)
{
vtkErrorMacro("Not enough values: " << this->FileName
<< ":" << this->LineNumber);
return 0;
}
return 1;
}
//-------------------------------------------------------------------------
// Read floating-point values into a point triplet.
int vtkMNITagPointReader::ParseFloatValues(
istream &infile, std::string &linetext, std::string::iterator &pos,
double *values, int n)
{
this->SkipWhitespace(infile, linetext, pos, 0);
int i = 0;
while (pos != linetext.end() && *pos != ';' && i < n)
{
const char *cp = linetext.c_str() + (pos - linetext.begin());
char *ep = 0;
double val = strtod(cp, &ep);
if (ep == cp)
{
vtkErrorMacro("Syntax error " << this->FileName
<< ":" << this->LineNumber);
return 0;
}
pos += (ep - cp);
values[i++] = val;
this->SkipWhitespace(infile, linetext, pos, 0);
}
if (i != n)
{
vtkErrorMacro("Not enough values: " << this->FileName
<< ":" << this->LineNumber);
return 0;
}
return 1;
}
//-------------------------------------------------------------------------
int vtkMNITagPointReader::ReadFile(
vtkPolyData *output1, vtkPolyData *output2)
{
// Check that the file name has been set.
if (!this->FileName)
{
vtkErrorMacro("ReadFile: No file name has been set");
return 0;
}
// Make sure that the file exists.
struct stat fs;
if(stat(this->FileName, &fs) != 0)
{
vtkErrorMacro("ReadFile: Can't open file " << this->FileName);
return 0;
}
// Make sure that the file is readable.
ifstream infile(this->FileName);
std::string linetext;
std::string::iterator pos = linetext.begin();
if (infile.fail())
{
vtkErrorMacro("ReadFile: Can't read the file " << this->FileName);
return 0;
}
// Read the first line
this->LineNumber = 0;
this->ReadLine(infile, linetext, pos);
if (strncmp(linetext.c_str(), "MNI Tag Point File", 18) != 0)
{
vtkErrorMacro("ReadFile: File is not a MNI tag file: " << this->FileName);
infile.close();
return 0;
}
// Read the number of volumes
this->ReadLine(infile, linetext, pos);
this->SkipWhitespace(infile, linetext, pos, 1);
int numVolumes = 1;
std::string identifier;
if (!this->ParseLeftHandSide(infile, linetext, pos, identifier) ||
strcmp(identifier.c_str(), "Volumes") != 0 ||
!this->ParseIntValues(infile, linetext, pos, &numVolumes, 1) ||
(numVolumes != 1 && numVolumes != 2) ||
!this->SkipWhitespace(infile, linetext, pos, 0) ||
*pos != ';')
{
vtkErrorMacro("ReadFile: Line must be Volumes = 1; or Volumes = 2; "
<< this->FileName << ":" << this->LineNumber);
infile.close();
return 0;
}
this->NumberOfVolumes = numVolumes;
// Read the comments
this->ReadLineAfterComments(infile, linetext, pos);
// Rad the tag points
if (!this->ParseLeftHandSide(infile, linetext, pos, identifier) ||
strcmp(identifier.c_str(), "Points") != 0)
{
vtkErrorMacro("ReadFile: Cannot find Points in file; " << this->FileName);
infile.close();
linetext.clear();
return 0;
}
vtkPoints *points[2];
points[0] = vtkPoints::New();
points[1] = vtkPoints::New();
vtkCellArray *verts = vtkCellArray::New();
vtkStringArray *labels = vtkStringArray::New();
vtkDoubleArray *weights = vtkDoubleArray::New();
vtkIntArray *structureIds = vtkIntArray::New();
vtkIntArray *patientIds = vtkIntArray::New();
int errorOccurred = 0;
this->SkipWhitespace(infile, linetext, pos, 1);
for (vtkIdType count = 0; infile.good() && *pos != ';'; count++)
{
for (int i = 0; i < numVolumes; i++)
{
double point[3];
if (!this->ParseFloatValues(infile, linetext, pos, point, 3))
{
errorOccurred = 1;
break;
}
points[i]->InsertNextPoint(point);
verts->InsertNextCell(1);
verts->InsertCellPoint(count);
}
if (errorOccurred)
{
break;
}
this->SkipWhitespace(infile, linetext, pos, 0);
if (pos != linetext.end() && *pos != '\"' && *pos != ';')
{
double weight;
int structureId;
int patientId;
if (!this->ParseFloatValues(infile, linetext, pos, &weight, 1) ||
!this->ParseIntValues(infile, linetext, pos, &structureId, 1) ||
!this->ParseIntValues(infile, linetext, pos, &patientId, 1))
{
errorOccurred = 1;
break;
}
vtkIdType lastCount = weights->GetNumberOfTuples();
weights->InsertValue(count, weight);
structureIds->InsertValue(count, structureId);
patientIds->InsertValue(count, patientId);
for (vtkIdType j = lastCount; j < count; j++)
{
weights->SetValue(j, 0.0);
structureIds->SetValue(j, -1);
patientIds->SetValue(j, -1);
}
}
this->SkipWhitespace(infile, linetext, pos, 0);
if (pos != linetext.end() && *pos == '\"')
{
vtkStdString stringval;
if (!this->ParseStringValue(infile, linetext, pos, stringval))
{
errorOccurred = 1;
break;
}
labels->InsertValue(count, stringval);
}
this->SkipWhitespace(infile, linetext, pos, 1);
}
// Close the file
infile.close();
if (!errorOccurred)
{
output1->SetPoints(points[0]);
output2->SetPoints(points[1]);
vtkPolyData *output[2];
output[0] = output1;
output[1] = output2;
weights->SetName("Weights");
structureIds->SetName("StructureIds");
patientIds->SetName("PatientIds");
labels->SetName("LabelText");
for (int k = 0; k < this->NumberOfVolumes; k++)
{
output[k]->SetVerts(verts);
if (weights->GetNumberOfTuples())
{
output[k]->GetPointData()->AddArray(weights);
}
if (structureIds->GetNumberOfTuples())
{
output[k]->GetPointData()->AddArray(structureIds);
}
if (patientIds->GetNumberOfTuples())
{
output[k]->GetPointData()->AddArray(patientIds);
}
if (labels->GetNumberOfValues())
{
output[k]->GetPointData()->AddArray(labels);
}
}
}
points[0]->Delete();
points[1]->Delete();
verts->Delete();
weights->Delete();
structureIds->Delete();
patientIds->Delete();
labels->Delete();
return 1;
}
//-------------------------------------------------------------------------
int vtkMNITagPointReader::GetNumberOfVolumes()
{
this->Update();
return this->NumberOfVolumes;
}
//-------------------------------------------------------------------------
vtkPoints *vtkMNITagPointReader::GetPoints(int port)
{
this->Update();
if (port < 0 || port >= this->NumberOfVolumes)
{
return 0;
}
vtkPolyData *output = static_cast<vtkPolyData *>(
this->GetOutputDataObject(port));
if (output)
{
return output->GetPoints();
}
return 0;
}
//-------------------------------------------------------------------------
vtkStringArray *vtkMNITagPointReader::GetLabelText()
{
this->Update();
vtkPolyData *output = static_cast<vtkPolyData *>(
this->GetOutputDataObject(0));
if (output)
{
return vtkArrayDownCast<vtkStringArray>(
output->GetPointData()->GetAbstractArray("LabelText"));
}
return 0;
}
//-------------------------------------------------------------------------
vtkDoubleArray *vtkMNITagPointReader::GetWeights()
{
this->Update();
vtkPolyData *output = static_cast<vtkPolyData *>(
this->GetOutputDataObject(0));
if (output)
{
return vtkArrayDownCast<vtkDoubleArray>(
output->GetPointData()->GetArray("Weights"));
}
return 0;
}
//-------------------------------------------------------------------------
vtkIntArray *vtkMNITagPointReader::GetStructureIds()
{
this->Update();
vtkPolyData *output = static_cast<vtkPolyData *>(
this->GetOutputDataObject(0));
if (output)
{
return vtkArrayDownCast<vtkIntArray>(
output->GetPointData()->GetArray("StructureIds"));
}
return 0;
}
//-------------------------------------------------------------------------
vtkIntArray *vtkMNITagPointReader::GetPatientIds()
{
this->Update();
vtkPolyData *output = static_cast<vtkPolyData *>(
this->GetOutputDataObject(0));
if (output)
{
return vtkArrayDownCast<vtkIntArray>(
output->GetPointData()->GetArray("PatientIds"));
}
return 0;
}
//-------------------------------------------------------------------------
const char *vtkMNITagPointReader::GetComments()
{
this->Update();
return this->Comments;
}
//-------------------------------------------------------------------------
int vtkMNITagPointReader::RequestData(
vtkInformation *vtkNotUsed(request),
vtkInformationVector **vtkNotUsed(inputVector),
vtkInformationVector *outputVector)
{
// get the info object
vtkInformation *outInfo1 = outputVector->GetInformationObject(0);
vtkInformation *outInfo2 = outputVector->GetInformationObject(1);
// get the ouptut
vtkPolyData *output1 = vtkPolyData::SafeDownCast(
outInfo1->Get(vtkDataObject::DATA_OBJECT()));
vtkPolyData *output2 = vtkPolyData::SafeDownCast(
outInfo2->Get(vtkDataObject::DATA_OBJECT()));
// all of the data in the first piece.
if (outInfo1->Get(vtkStreamingDemandDrivenPipeline::UPDATE_PIECE_NUMBER())
> 0 ||
outInfo2->Get(vtkStreamingDemandDrivenPipeline::UPDATE_PIECE_NUMBER())
> 0)
{
return 0;
}
// read the file
return this->ReadFile(output1, output2);
}
|