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
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkMNITagPointWriter.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 "vtkMNITagPointWriter.h"
#include "vtkObjectFactory.h"
#include "vtkInformationVector.h"
#include "vtkInformation.h"
#include "vtkPointSet.h"
#include "vtkPointData.h"
#include "vtkPoints.h"
#include "vtkStringArray.h"
#include "vtkDoubleArray.h"
#include "vtkIntArray.h"
#include "vtkMath.h"
#include "vtkErrorCode.h"
#include "vtkCommand.h"
#include <cctype>
#include <cmath>
#if !defined(_WIN32) || defined(__CYGWIN__)
# include <unistd.h> /* unlink */
#else
# include <io.h> /* unlink */
#endif
//--------------------------------------------------------------------------
vtkStandardNewMacro(vtkMNITagPointWriter);
vtkCxxSetObjectMacro(vtkMNITagPointWriter, LabelText, vtkStringArray);
vtkCxxSetObjectMacro(vtkMNITagPointWriter, Weights, vtkDoubleArray);
vtkCxxSetObjectMacro(vtkMNITagPointWriter, StructureIds, vtkIntArray);
vtkCxxSetObjectMacro(vtkMNITagPointWriter, PatientIds, vtkIntArray);
//-------------------------------------------------------------------------
vtkMNITagPointWriter::vtkMNITagPointWriter()
{
this->Points[0] = 0;
this->Points[1] = 0;
this->LabelText = 0;
this->Weights = 0;
this->StructureIds = 0;
this->PatientIds = 0;
this->Comments = 0;
this->SetNumberOfInputPorts(2);
this->SetNumberOfOutputPorts(0);
this->FileName = 0;
}
//-------------------------------------------------------------------------
vtkMNITagPointWriter::~vtkMNITagPointWriter()
{
vtkObject *objects[6];
objects[0] = this->Points[0];
objects[1] = this->Points[1];
objects[2] = this->LabelText;
objects[3] = this->Weights;
objects[4] = this->StructureIds;
objects[5] = this->PatientIds;
for (int i = 0; i < 6; i++)
{
if (objects[i])
{
objects[i]->Delete();
}
}
delete [] this->Comments;
delete[] this->FileName;
}
//-------------------------------------------------------------------------
void vtkMNITagPointWriter::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
os << indent << "Points: " << this->Points[0] << " "
<< this->Points[1] << "\n";
os << indent << "LabelText: " << this->LabelText << "\n";
os << indent << "Weights: " << this->Weights << "\n";
os << indent << "StructureIds: " << this->StructureIds << "\n";
os << indent << "PatientIds: " << this->PatientIds << "\n";
os << indent << "Comments: "
<< (this->Comments ? this->Comments : "none") << "\n";
}
//----------------------------------------------------------------------------
int vtkMNITagPointWriter::FillInputPortInformation(
int, vtkInformation* info)
{
info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkPointSet");
info->Set(vtkAlgorithm::INPUT_IS_OPTIONAL(), 1);
return 1;
}
//-------------------------------------------------------------------------
vtkMTimeType vtkMNITagPointWriter::GetMTime()
{
vtkMTimeType mtime = this->Superclass::GetMTime();
vtkObject *objects[6];
objects[0] = this->Points[0];
objects[1] = this->Points[1];
objects[2] = this->LabelText;
objects[3] = this->Weights;
objects[4] = this->StructureIds;
objects[5] = this->PatientIds;
for (int i = 0; i < 6; i++)
{
if (objects[i])
{
vtkMTimeType m = objects[i]->GetMTime();
if (m > mtime)
{
mtime = m;
}
}
}
return mtime;
}
//-------------------------------------------------------------------------
void vtkMNITagPointWriter::SetPoints(int port, vtkPoints *points)
{
if (port < 0 || port > 1)
{
return;
}
if (this->Points[port] == points)
{
return;
}
if (this->Points[port])
{
this->Points[port]->Delete();
}
this->Points[port] = points;
if (this->Points[port])
{
this->Points[port]->Register(this);
}
this->Modified();
}
//-------------------------------------------------------------------------
vtkPoints *vtkMNITagPointWriter::GetPoints(int port)
{
if (port < 0 || port > 1)
{
return 0;
}
return this->Points[port];
}
//-------------------------------------------------------------------------
void vtkMNITagPointWriter::WriteData(vtkPointSet *inputs[2])
{
static const char *arrayNames[3] = {
"Weights",
"StructureIds",
"PatientIds"
};
vtkPoints *points[2];
vtkStringArray *labels = 0;
vtkDataArray *darray[3];
darray[0] = 0;
darray[1] = 0;
darray[2] = 0;
vtkDataArray *ivarArrays[3];
ivarArrays[0] = this->Weights;
ivarArrays[1] = this->StructureIds;
ivarArrays[2] = this->PatientIds;
for (int ii = 1; ii >= 0; --ii)
{
points[ii] = 0;
if (inputs[ii])
{
points[ii] = inputs[ii]->GetPoints();
vtkStringArray *stringArray = vtkArrayDownCast<vtkStringArray>(
inputs[ii]->GetPointData()->GetAbstractArray("LabelText"));
if (stringArray)
{
labels = stringArray;
}
for (int j = 0; j < 3; j++)
{
vtkDataArray *dataArray =
inputs[ii]->GetPointData()->GetArray(arrayNames[j]);
if (dataArray)
{
darray[j] = dataArray;
}
}
}
if (this->Points[ii])
{
points[ii] = this->Points[ii];
}
}
if (this->LabelText)
{
labels = this->LabelText;
}
for (int j = 0; j < 3; j++)
{
if (ivarArrays[j])
{
darray[j] = ivarArrays[j];
}
}
if (points[0] == 0)
{
vtkErrorMacro("No input points have been provided");
return;
}
// numVolumes is 1 if there is only one set of points
int numVolumes = 1;
vtkIdType n = points[0]->GetNumberOfPoints();
if (points[1])
{
numVolumes = 2;
if (points[1]->GetNumberOfPoints() != n)
{
vtkErrorMacro("Input point counts do not match: " << n << " versus "
<< points[1]->GetNumberOfPoints());
return;
}
}
// labels is null if there are no labels
if (labels && labels->GetNumberOfValues() != n)
{
vtkErrorMacro("LabelText count does not match point count: "
<< labels->GetNumberOfValues() << " versus " << n);
return;
}
// dataArrays is null if there are no data arrays
vtkDataArray **dataArrays = 0;
for (int jj = 0; jj < 3; jj++)
{
if (darray[jj])
{
dataArrays = darray;
if (darray[jj]->GetNumberOfTuples() != n)
{
vtkErrorMacro("" << arrayNames[jj]
<< " count does not match point count: "
<< darray[jj]->GetNumberOfTuples() << " versus " << n);
return;
}
}
}
// If we got this far, the data seems to be okay
ostream *outfilep = this->OpenFile();
if (!outfilep)
{
return;
}
ostream &outfile = *outfilep;
// Write the header
outfile << "MNI Tag Point File\n";
outfile << "Volumes = " << numVolumes << ";\n";
// Write user comments
if (this->Comments)
{
char *cp = this->Comments;
while (*cp)
{
if (*cp != '%')
{
outfile << "% ";
}
while (*cp && *cp != '\n')
{
if (isprint(*cp) || *cp == '\t')
{
outfile << *cp;
}
cp++;
}
outfile << "\n";
if (*cp == '\n')
{
cp++;
}
}
}
else
{
for (int k = 0; k < numVolumes; k++)
{
outfile << "% Volume " << (k + 1) << " produced by VTK\n";
}
}
// Add a blank line
outfile << "\n";
// Write the points
outfile << "Points =\n";
char text[256];
for (int i = 0; i < n; i++)
{
for (int kk = 0; kk < 2; kk++)
{
if (points[kk])
{
double point[3];
points[kk]->GetPoint(i, point);
sprintf(text, " %.15g %.15g %.15g", point[0], point[1], point[2]);
outfile << text;
}
}
if (dataArrays)
{
double w = 0.0;
int s = -1;
int p = -1;
if (dataArrays[0])
{
w = dataArrays[0]->GetComponent(i, 0);
}
if (dataArrays[1])
{
s = static_cast<int>(dataArrays[1]->GetComponent(i, 0));
}
if (dataArrays[2])
{
p = static_cast<int>(dataArrays[2]->GetComponent(i, 0));
}
sprintf(text, " %.15g %d %d", w, s, p);
outfile << text;
}
if (labels)
{
vtkStdString l = labels->GetValue(i);
outfile << " \"";
for (std::string::iterator si = l.begin(); si != l.end(); ++si)
{
if (isprint(*si) && *si != '\"' && *si != '\\')
{
outfile.put(*si);
}
else
{
outfile.put('\\');
char c = '\0';
static char ctrltable[] = {
'\a', 'a', '\b', 'b', '\f', 'f', '\n', 'n', '\r', 'r',
'\t', 't', '\v', 'v', '\\', '\\', '\"', '\"', '\0', '\0'
};
for (int ci = 0; ctrltable[ci] != '\0'; ci += 2)
{
if (*si == ctrltable[ci])
{
c = ctrltable[ci + 1];
break;
}
}
if (c != '\0')
{
outfile.put(c);
}
else
{
sprintf(text, "x%2.2x", (static_cast<int>(*si) & 0x00ff));
outfile << text;
}
}
}
outfile << "\"";
}
if (i < n-1)
{
outfile << "\n";
}
}
outfile << ";\n";
outfile.flush();
// Close the file
this->CloseFile(outfilep);
// Delete the file if an error occurred
if (this->ErrorCode == vtkErrorCode::OutOfDiskSpaceError)
{
vtkErrorMacro("Ran out of disk space; deleting file: "
<< this->FileName);
unlink(this->FileName);
}
}
//-------------------------------------------------------------------------
int vtkMNITagPointWriter::Write()
{
// Allow writer to work when no inputs are provided
this->Modified();
this->Update();
return 1;
}
//-------------------------------------------------------------------------
int vtkMNITagPointWriter::RequestData(
vtkInformation *, vtkInformationVector **inputVector,
vtkInformationVector *)
{
this->SetErrorCode(vtkErrorCode::NoError);
vtkInformation *inInfo[2];
inInfo[0] = inputVector[0]->GetInformationObject(0);
inInfo[1] = inputVector[1]->GetInformationObject(0);
vtkPointSet *input[2];
input[0] = 0;
input[1] = 0;
vtkMTimeType lastUpdateTime = 0;
for (int idx = 0; idx < 2; ++idx)
{
if (inInfo[idx])
{
input[idx] = vtkPointSet::SafeDownCast(
inInfo[idx]->Get(vtkDataObject::DATA_OBJECT()));
if (input[idx])
{
vtkMTimeType updateTime = input[idx]->GetUpdateTime();
if (updateTime > lastUpdateTime)
{
lastUpdateTime = updateTime;
}
}
}
}
if (lastUpdateTime < this->WriteTime && this->GetMTime() < this->WriteTime)
{
// we are up to date
return 1;
}
this->InvokeEvent(vtkCommand::StartEvent, NULL);
this->WriteData(input);
this->InvokeEvent(vtkCommand::EndEvent, NULL);
this->WriteTime.Modified();
return 1;
}
//-------------------------------------------------------------------------
ostream *vtkMNITagPointWriter::OpenFile()
{
ostream *fptr;
if (!this->FileName )
{
vtkErrorMacro(<< "No FileName specified! Can't write!");
this->SetErrorCode(vtkErrorCode::NoFileNameError);
return NULL;
}
vtkDebugMacro(<<"Opening file for writing...");
fptr = new ofstream(this->FileName, ios::out);
if (fptr->fail())
{
vtkErrorMacro(<< "Unable to open file: "<< this->FileName);
this->SetErrorCode(vtkErrorCode::CannotOpenFileError);
delete fptr;
return NULL;
}
return fptr;
}
//-------------------------------------------------------------------------
void vtkMNITagPointWriter::CloseFile(ostream *fp)
{
vtkDebugMacro(<<"Closing file\n");
delete fp;
}
|