vtkMNIObjectWriter.cxx 25.1 KB
Newer Older
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
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkMNIObjectWriter.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 "vtkMNIObjectWriter.h"

#include "vtkObjectFactory.h"

#include "vtkPolyData.h"
#include "vtkPointData.h"
#include "vtkCellData.h"
#include "vtkPoints.h"
#include "vtkCellArray.h"
#include "vtkFloatArray.h"
59
#include "vtkInformation.h"
60 61 62
#include "vtkIntArray.h"
#include "vtkUnsignedCharArray.h"
#include "vtkProperty.h"
63
#include "vtkMapper.h"
64 65 66 67 68
#include "vtkLookupTable.h"
#include "vtkPolygon.h"
#include "vtkMath.h"
#include "vtkErrorCode.h"

69 70
#include <cctype>
#include <cmath>
71 72 73 74 75 76 77 78 79 80 81

#if !defined(_WIN32) || defined(__CYGWIN__)
# include <unistd.h> /* unlink */
#else
# include <io.h> /* unlink */
#endif

//--------------------------------------------------------------------------
vtkStandardNewMacro(vtkMNIObjectWriter);

vtkCxxSetObjectMacro(vtkMNIObjectWriter, Property, vtkProperty);
82
vtkCxxSetObjectMacro(vtkMNIObjectWriter, Mapper, vtkMapper);
83 84 85 86 87 88
vtkCxxSetObjectMacro(vtkMNIObjectWriter, LookupTable, vtkLookupTable);

//-------------------------------------------------------------------------
vtkMNIObjectWriter::vtkMNIObjectWriter()
{
  this->Property = 0;
89
  this->Mapper = 0;
90 91
  this->LookupTable = 0;

92 93
  this->FileName = 0;

94 95 96 97 98 99 100 101 102 103 104 105 106
  // Whether file is binary or ASCII
  this->FileType = VTK_ASCII;

  this->OutputStream = 0;
}

//-------------------------------------------------------------------------
vtkMNIObjectWriter::~vtkMNIObjectWriter()
{
  if (this->Property)
    {
    this->Property->Delete();
    }
107 108 109 110
  if (this->Mapper)
    {
    this->Mapper->Delete();
    }
111 112 113 114
  if (this->LookupTable)
    {
    this->LookupTable->Delete();
    }
115 116

  delete[] this->FileName;
117 118 119 120 121 122 123 124
}

//-------------------------------------------------------------------------
void vtkMNIObjectWriter::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os,indent);

  os << indent << "Property: " << this->Property << "\n";
125
  os << indent << "Mapper: " << this->Mapper << "\n";
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
  os << indent << "LookupTable: " << this->LookupTable << "\n";
}

//-------------------------------------------------------------------------
int vtkMNIObjectWriter::WriteObjectType(int objType)
{
  if (this->FileType == VTK_ASCII)
    {
    this->OutputStream->put(char(objType));
    }
  else
    {
    this->OutputStream->put(char(tolower(objType)));
    }

  return 1;
}

//-------------------------------------------------------------------------
// Write floating-point values into a vtkFloatArray.
int vtkMNIObjectWriter::WriteValues(vtkDataArray *array)
{
  int dataType = array->GetDataType();
  void *data = array->GetVoidPointer(0);

  vtkIdType numTuples = array->GetNumberOfTuples();
  vtkIdType numComponents = array->GetNumberOfComponents();
  vtkIdType n = numTuples * numComponents;

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
  if (this->FileType == VTK_ASCII && dataType == VTK_UNSIGNED_CHAR)
    {
    vtkIdType m = numComponents;
    for (vtkIdType i = 0; i < n; i += m)
      {
      unsigned char *cdata = static_cast<unsigned char *>(data) + i;
      ostream &os = *this->OutputStream;

      double r = cdata[0]/255.0;
      double g = r;
      double b = r;
      double a = 1.0;

      if (m > 2)
        {
        g = cdata[1]/255.0;
        b = cdata[2]/255.0;
        }
      if (m == 2 || m == 4)
        {
        a = cdata[m-1]/255.0;
        }

      os << " " << r << " " << g << " " << b << " " << a;

      if (this->WriteNewline() == 0)
        {
        return 0;
        }
      }
    }
  else if (this->FileType == VTK_ASCII)
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
    {
    vtkIdType valuesPerLine = 8;
    if (numComponents > 1 && numComponents < 8)
      {
      valuesPerLine = numComponents;
      }

    vtkIdType i = 0;
    while (i < n)
      {
      for (vtkIdType j = 0; j < valuesPerLine && i < n; i++, j++)
        {
        ostream &os = *this->OutputStream;

        switch(dataType)
          {
          case VTK_FLOAT:
            {
            float *fdata = static_cast<float *>(data);
            double val = fdata[i];
            os << " " << val;
            }
            break;
          case VTK_DOUBLE:
            {
            double *ddata = static_cast<double *>(data);
            double val = ddata[i];
            os << " " << val;
            }
            break;
          case VTK_INT:
            {
            int *idata = static_cast<int *>(data);
            int val = idata[i];
            os << " " << val;
            }
            break;
          }
        }
      if (this->WriteNewline() == 0)
        {
        return 0;
        }
      }
    }
  else
    {
    // machine-order endianness and data size
    if (dataType == VTK_UNSIGNED_CHAR)
      {
      // colors need to be swapped to ABGR order for binary
      char *odata = static_cast<char *>(data);
239 240
      vtkIdType m = numComponents;
      for (vtkIdType i = 0; i < n && this->OutputStream->good(); i += m)
241 242
        {
        char cdata[4];
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
        if (m > 2)
          {
          cdata[3] = odata[0];
          cdata[2] = odata[1];
          cdata[1] = odata[2];
          }
        else
          {
          cdata[3] = odata[0];
          cdata[2] = odata[0];
          cdata[1] = odata[0];
          }
        if (m == 2 || m == 4)
          {
          cdata[0] = odata[m-1];
          }
        else
          {
          cdata[0] = static_cast<char>(255);
          }
263 264 265

        this->OutputStream->write(cdata, 4);

266
        odata += m;
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
        }
      }
    else if (dataType == VTK_DOUBLE)
      {
      // doubles must be converted to float
      double *ddata = (double *)(data);
      for (vtkIdType i = 0; i < n && this->OutputStream->good(); i++)
        {
        float fval = ddata[i];
        this->OutputStream->write((char *)(&fval), sizeof(float));
        }
      }
    else
      {
      this->OutputStream->write((char *)(data),
                                n*array->GetDataTypeSize());
      }

    if (this->OutputStream->fail())
      {
      this->SetErrorCode(vtkErrorCode::OutOfDiskSpaceError);
      return 0;
      }
    }

  return 1;
}

//-------------------------------------------------------------------------
int vtkMNIObjectWriter::WriteIdValue(vtkIdType value)
{
  // The .obj files use 32-bit integers exclusively
  int ival = static_cast<int>(value);

  if (this->FileType == VTK_ASCII)
    {
    *this->OutputStream << " " << ival;
    }
  else
    {
    // machine-order endianness and data size
    this->OutputStream->write((char *)(&ival), sizeof(int));
    }

  return 1;
}

//-------------------------------------------------------------------------
int vtkMNIObjectWriter::WriteNewline()
{
  if (this->FileType == VTK_ASCII)
    {
    *this->OutputStream << "\n";

    this->OutputStream->flush();
    if (this->OutputStream->fail())
      {
      this->SetErrorCode(vtkErrorCode::OutOfDiskSpaceError);
      return 0;
      }
    }

  return 1;
}

//-------------------------------------------------------------------------
int vtkMNIObjectWriter::WriteProperty(vtkProperty *property)
{
  float properties[5];

  properties[0] = 0.0;
  properties[1] = 1.0;
  properties[2] = 0.0;
  properties[3] = 1.0;
  properties[4] = 1.0;

  if (property)
    {
    properties[0] = property->GetAmbient();
    properties[1] = property->GetDiffuse();
    properties[2] = property->GetSpecular();
    properties[3] = property->GetSpecularPower();
    properties[4] = property->GetOpacity();
    }

  if (this->FileType == VTK_ASCII)
    {
    for (int i = 0; i < 5; i++)
      {
      *this->OutputStream << " " << properties[i];
      }
    }
  else
    {
    // machine-order endianness and data size
    this->OutputStream->write((char *)(properties), 5*sizeof(float));
    }

  return 1;
}

//-------------------------------------------------------------------------
int vtkMNIObjectWriter::WriteLineThickness(vtkProperty *property)
{
  float width = 1;

  if (property)
    {
    width = property->GetLineWidth();
    }

  if (this->FileType == VTK_ASCII)
    {
    *this->OutputStream << " " << width;
    }
  else
    {
    // machine-order endianness and data size
    this->OutputStream->write((char *)(&width), sizeof(float));
    }

  return 1;
}

//-------------------------------------------------------------------------
int vtkMNIObjectWriter::WritePoints(vtkPolyData *data)
{
  return this->WriteValues(data->GetPoints()->GetData());
}

//-------------------------------------------------------------------------
int vtkMNIObjectWriter::WriteNormals(vtkPolyData *data)
{
  vtkDataArray *normals = data->GetPointData()->GetNormals();
  vtkFloatArray *newNormals = 0;

  // Calculate normals if necessary
  if (!normals)
    {
    // Normals will be calculated according to BIC conventions,
    // which weighs the polygon normals by the interior angle.

    vtkPoints *points = data->GetPoints();
    vtkIdType numPoints = points->GetNumberOfPoints();
    vtkCellArray *polyArray = data->GetPolys();
    vtkCellArray *stripArray = data->GetStrips();
    vtkIdType numPolys = data->GetNumberOfPolys();
    vtkIdType numCells = numPolys + data->GetNumberOfStrips();

    newNormals = vtkFloatArray::New();
    newNormals->SetNumberOfComponents(3);
    newNormals->SetNumberOfTuples(numPoints);

420
    for (vtkIdType ii = 0; ii < numPoints; ii++)
421
      {
422
      float *normal = newNormals->GetPointer(3*ii);
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
      normal[0] = 0.0;
      normal[1] = 0.0;
      normal[2] = 0.0;
      }

    vtkIdType polyIndex = 0;
    vtkIdType stripIndex = 0;
    for (vtkIdType i = 0; i < numCells; i++)
      {
      vtkIdType *pointIds;
      vtkIdType numIds;
      vtkIdType numFaces = 1;

      if (i < numPolys)
        {
        polyArray->GetCell(polyIndex, numIds, pointIds);
        polyIndex += 1 + numIds;
        }
      else
        {
        stripArray->GetCell(stripIndex, numIds, pointIds);
        stripIndex += 1 + numIds;
        numFaces = numIds - 2;
        numIds = 3;
        }

      for (vtkIdType j = 0; j < numFaces; j++)
        {
        double faceNormal[3];
        vtkPolygon::ComputeNormal(points, numIds, pointIds, faceNormal);

        // For strips, reverse normal of every other triangle
        if ((j & 1) == 1)
          {
          faceNormal[0] = -faceNormal[0];
          faceNormal[1] = -faceNormal[1];
          faceNormal[2] = -faceNormal[2];
          }

        // Extra stuff is for calculating the angle
        double vec1[3];
        double vec2[3];
        double p1[3];
        double p2[3];

        points->GetPoint(pointIds[numIds-1], p1);
        points->GetPoint(pointIds[0], p2);
        vec2[0] = p2[0] - p1[0];
        vec2[1] = p2[1] - p1[1];
        vec2[2] = p2[2] - p1[2];
        vtkMath::Normalize(vec2);

        // Go through all points in the face
        for (vtkIdType k = 0; k < numIds; k++)
          {
          vec1[0] = -vec2[0];
          vec1[1] = -vec2[1];
          vec1[2] = -vec2[2];

          p1[0] = p2[0];
          p1[1] = p2[1];
          p1[2] = p2[2];

          points->GetPoint(pointIds[(k+1)%numIds], p2);

          vec2[0] = p2[0] - p1[0];
          vec2[1] = p2[1] - p1[1];
          vec2[2] = p2[2] - p1[2];
          vtkMath::Normalize(vec2);

          // Use dotprod to calculate angle between vectors
          double angle = 0.0;
          double dotprod = vtkMath::Dot(vec1, vec2);
          if (dotprod >= 1.0)
            {
            angle = 0.0;
            }
          else if (dotprod <= -1.0)
            {
502
            angle = vtkMath::Pi();
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
            }
          else
            {
            angle = acos(dotprod);
            }

          // Accumulate contributions of each polygon to the point normals
          float *normal = newNormals->GetPointer(3*pointIds[k]);
          normal[0] += angle*faceNormal[0];
          normal[1] += angle*faceNormal[1];
          normal[2] += angle*faceNormal[2];
          }

        // Go to next triangle in strip
        pointIds++;
        }
      }

    // Normalize the normals
522
    for (vtkIdType jj = 0; jj < numPoints; jj++)
523
      {
524
      float *normal = newNormals->GetPointer(3*jj);
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
      vtkMath::Normalize(normal);
      }

    normals = newNormals;
    }

  int status = this->WriteValues(normals);

  if (newNormals)
    {
    newNormals->Delete();
    }

  return status;
}

//-------------------------------------------------------------------------
542 543
int vtkMNIObjectWriter::WriteColors(
  vtkProperty *property, vtkMapper *mapper, vtkPolyData *data)
544 545 546 547 548 549 550 551 552 553 554
{
  vtkUnsignedCharArray *newScalars = 0;
  vtkDataArray *scalars = data->GetPointData()->GetScalars();
  vtkIdType colorType = 2;

  if (scalars == 0)
    {
    scalars = data->GetCellData()->GetScalars();
    colorType = 1;
    }

555 556 557
  if (this->Mapper)
    {
    int cellFlag = 0;
558
    scalars = 0;
559

560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
    // Get color scalars according to Mapper rules
    if (mapper->GetScalarVisibility())
      {
      scalars = vtkAbstractMapper::GetScalars(
        data, mapper->GetScalarMode(), mapper->GetArrayAccessMode(),
        mapper->GetArrayId(), mapper->GetArrayName(), cellFlag);
      }

    // Cell or point scalars?
    colorType = 2;
    if (cellFlag)
      {
      colorType = 1;
      }

    // Cannot use cell scalars for triangle strips
    if (cellFlag == 1 && data->GetStrips() &&
        data->GetStrips()->GetNumberOfCells() != 0)
      {
      scalars = 0;
      }
581

582
    if (scalars)
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
      {
      int arrayComponent = mapper->GetArrayComponent();
      if (scalars->GetNumberOfComponents() <= arrayComponent)
        {
        arrayComponent = 0;
        }

      vtkScalarsToColors *lookupTable = scalars->GetLookupTable();
      if (lookupTable == 0)
        {
        lookupTable = mapper->GetLookupTable();
        lookupTable->Build();
        }

      if (!mapper->GetUseLookupTableScalarRange())
        {
        lookupTable->SetRange(mapper->GetScalarRange());
        }

      newScalars = lookupTable->MapScalars(
        scalars, mapper->GetColorMode(), arrayComponent);
      scalars = newScalars;
      }
    }
  else if (scalars != 0)
608 609 610
    {
    if (this->LookupTable)
      {
611 612
      newScalars = this->LookupTable->MapScalars(
        scalars, VTK_COLOR_MODE_MAP_SCALARS, -1);
613 614
      scalars = newScalars;
      }
615
    else if (scalars->GetDataType() != VTK_UNSIGNED_CHAR)
616 617 618 619 620 621 622 623 624 625 626 627 628
      {
      scalars = 0;
      }
    }

  if (scalars == 0)
    {
    colorType = 0;

    newScalars = vtkUnsignedCharArray::New();
    newScalars->SetNumberOfComponents(4);
    newScalars->SetNumberOfTuples(1);

629 630 631
    unsigned char rgba[4];

    if (property)
632 633
      {
      double color[3];
634 635
      property->GetColor(color);
      double opacity = property->GetOpacity();
636 637 638 639 640 641 642 643 644 645 646 647 648 649

      rgba[0] = static_cast<unsigned char>(color[0]*255);
      rgba[1] = static_cast<unsigned char>(color[1]*255);
      rgba[2] = static_cast<unsigned char>(color[2]*255);
      rgba[3] = static_cast<unsigned char>(opacity*255);
      }
    else
      {
      rgba[0] = 255;
      rgba[1] = 255;
      rgba[2] = 255;
      rgba[3] = 255;
      }

650
    newScalars->SetTypedTuple(0, rgba);
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 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839
    scalars = newScalars;
    }

  int status = this->WriteIdValue(colorType);

  if (status != 0)
    {
    status = this->WriteValues(scalars);
    }

  if (newScalars)
    {
    newScalars->Delete();
    }

  return status;
}

//-------------------------------------------------------------------------
int vtkMNIObjectWriter::WriteCells(vtkPolyData *data, int cellType)
{
  vtkCellArray *cellArray = 0;
  if (cellType == VTK_POLYGON)
    {
    cellArray = data->GetPolys();
    }
  else if (cellType == VTK_POLY_LINE)
    {
    cellArray = data->GetLines();
    }
  else
    {
    return 0;
    }

  vtkIntArray *endIndices = vtkIntArray::New();
  vtkIntArray *cellIndices = vtkIntArray::New();

  if (cellArray)
    {
    vtkIdType numCells = cellArray->GetNumberOfCells();
    vtkIdType numCellIndices = (cellArray->GetNumberOfConnectivityEntries()
                                - numCells);

    endIndices->Allocate(numCells);
    cellIndices->Allocate(numCellIndices);

    vtkIdType cellIndex = 0;
    vtkIdType endIndex = 0;
    for (vtkIdType i = 0; i < numCells; i++)
      {
      vtkIdType *pointIds;
      vtkIdType numIds;
      cellArray->GetCell(cellIndex, numIds, pointIds);
      cellIndex += 1 + numIds;

      endIndex += numIds;
      endIndices->InsertNextValue(endIndex);
      for (vtkIdType j = 0; j < numIds; j++)
        {
        cellIndices->InsertNextValue(pointIds[j]);
        }
      }
    }

  // Convert triangle strips to triangles
  if (cellType == VTK_POLYGON && data->GetNumberOfStrips() != 0)
    {
    cellArray = data->GetStrips();
    vtkIdType numCells = cellArray->GetNumberOfCells();

    vtkIdType cellIndex = 0;
    vtkIdType endIndex = 0;
    if (endIndices->GetMaxId() >= 0)
      {
      endIndex = endIndices->GetValue(endIndices->GetMaxId());
      }
    for (vtkIdType i = 0; i < numCells; i++)
      {
      vtkIdType *pointIds;
      vtkIdType numIds;
      cellArray->GetCell(cellIndex, numIds, pointIds);
      cellIndex += 1 + numIds;

      int inc1 = 2;
      int inc2 = 1;
      for (vtkIdType j = 2; j < numIds; j++)
        {
        endIndex += 3;
        endIndices->InsertNextValue(endIndex);

        cellIndices->InsertNextValue(pointIds[j-inc1]);
        cellIndices->InsertNextValue(pointIds[j-inc2]);
        cellIndices->InsertNextValue(pointIds[j]);

        // reverse the order each time around
        int tmp = inc1;
        inc1 = inc2;
        inc2 = tmp;
        }
      }
    }

  // Write the cell end indices
  int status = this->WriteValues(endIndices);

  // Write the newline
  if (status != 0)
    {
    status = this->WriteNewline();
    }

  // Write the cell point indices
  if (status != 0)
    {
    status = this->WriteValues(cellIndices);
    }

  endIndices->Delete();
  cellIndices->Delete();

  return status;
}

//-------------------------------------------------------------------------
int vtkMNIObjectWriter::WritePolygonObject(vtkPolyData *output)
{
  // Write the surface property
  if (this->WriteProperty(this->Property) == 0)
    {
    return 0;
    }

  // Write the number of points
  if (this->WriteIdValue(output->GetNumberOfPoints()) == 0)
    {
    return 0;
    }

  // Write a newline
  if (this->WriteNewline() == 0)
    {
    return 0;
    }

  // Write the points
  if (this->WritePoints(output) == 0)
    {
    return 0;
    }

  // Write a newline
  if (this->WriteNewline() == 0)
    {
    return 0;
    }

  // Write the normals
  if (this->WriteNormals(output) == 0)
    {
    return 0;
    }

  // Write a newline
  if (this->WriteNewline() == 0)
    {
    return 0;
    }

  // Write the number of items
  vtkIdType numPolys = output->GetNumberOfPolys();
  vtkIdType numStrips = output->GetNumberOfStrips();
  if (numStrips > 0)
    {
    numPolys += (output->GetStrips()->GetNumberOfConnectivityEntries()
                 - 3*numStrips);
    }
  if (this->WriteIdValue(numPolys) == 0)
    {
    return 0;
    }

  // Write a newline
  if (this->WriteNewline() == 0)
    {
    return 0;
    }

  // Write the colors
840
  if (this->WriteColors(this->Property, this->Mapper, output) == 0)
841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911
    {
    return 0;
    }

  // Write a newline
  if (this->WriteNewline() == 0)
    {
    return 0;
    }

  // Write the cells
  if (this->WriteCells(output, VTK_POLYGON) == 0)
    {
    return 0;
    }

  // Write a newline
  if (this->WriteNewline() == 0)
    {
    return 0;
    }

  return 1;
}

//-------------------------------------------------------------------------
int vtkMNIObjectWriter::WriteLineObject(vtkPolyData *output)
{
  // Write the surface property
  if (this->WriteLineThickness(this->Property) == 0)
    {
    return 0;
    }

  // Write the number of points
  if (this->WriteIdValue(output->GetNumberOfPoints()) == 0)
    {
    return 0;
    }

  // Write a newline
  if (this->WriteNewline() == 0)
    {
    return 0;
    }

  // Write the points
  if (this->WritePoints(output) == 0)
    {
    return 0;
    }

  // Write a newline
  if (this->WriteNewline() == 0)
    {
    return 0;
    }

  // Write the number of items
  if (this->WriteIdValue(output->GetNumberOfLines()) == 0)
    {
    return 0;
    }

  // Write a newline
  if (this->WriteNewline() == 0)
    {
    return 0;
    }

  // Write the colors
912
  if (this->WriteColors(this->Property, this->Mapper, output) == 0)
913 914 915 916 917 918 919 920 921 922 923
    {
    return 0;
    }

  // Write a newline
  if (this->WriteNewline() == 0)
    {
    return 0;
    }

  // Write the cells
924
  if (this->WriteCells(output, VTK_POLY_LINE) == 0)
925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970
    {
    return 0;
    }

  // Write a newline
  if (this->WriteNewline() == 0)
    {
    return 0;
    }

  return 1;
}

//-------------------------------------------------------------------------
void vtkMNIObjectWriter::WriteData()
{
  vtkPolyData *input = this->GetInput();
  int objType = 0;

  vtkIdType npolys = input->GetNumberOfPolys();
  vtkIdType nstrips = input->GetNumberOfStrips();
  vtkIdType nlines = input->GetNumberOfLines();
  vtkIdType nverts = input->GetNumberOfVerts();

  if (nverts != 0)
    {
    vtkErrorMacro("Unable to write vertexes.");
    return;
    }

  if ((npolys != 0 || nstrips != 0) && nlines != 0)
    {
    vtkErrorMacro("Unable to write a data set with multiple cell types.");
    return;
    }

  if (npolys != 0 || nstrips != 0)
    {
    objType = 'P';
    }
  else if (nlines != 0)
    {
    objType = 'L';
    }

  // Open the file
971
  this->OutputStream = this->OpenFile();
972

973 974 975 976 977
  if (!this->OutputStream)
    {
    return;
    }

978 979 980 981 982 983 984 985 986 987 988 989 990 991 992
  // Write the type character
  this->WriteObjectType(objType);

  // Write the object
  switch(objType)
    {
    case 'P':
      this->WritePolygonObject(input);
      break;
    case 'L':
      this->WriteLineObject(input);
      break;
    }

  // Close the file
993
  this->CloseFile(this->OutputStream);
994 995 996 997 998 999 1000 1001 1002

  // 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);
    }
}
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066


//-------------------------------------------------------------------------
vtkPolyData* vtkMNIObjectWriter::GetInput()
{
  return vtkPolyData::SafeDownCast(this->Superclass::GetInput());
}

//-------------------------------------------------------------------------
vtkPolyData* vtkMNIObjectWriter::GetInput(int port)
{
  return vtkPolyData::SafeDownCast(this->Superclass::GetInput(port));
}

//-------------------------------------------------------------------------
int vtkMNIObjectWriter::FillInputPortInformation(int, vtkInformation *info)
{
  info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkPolyData");
  return 1;
}

//-------------------------------------------------------------------------
ostream *vtkMNIObjectWriter::OpenFile()
{
  ostream *fptr;

  if (!this->FileName )
    {
    vtkErrorMacro(<< "No FileName specified! Can't write!");
    this->SetErrorCode(vtkErrorCode::NoFileNameError);
    return NULL;
    }

  vtkDebugMacro(<<"Opening file for writing...");

  if ( this->FileType == VTK_ASCII )
    {
    fptr = new ofstream(this->FileName, ios::out);
    }
  else
    {
#ifdef _WIN32
    fptr = new ofstream(this->FileName, ios::out | ios::binary);
#else
    fptr = new ofstream(this->FileName, ios::out);
#endif
    }

  if (fptr->fail())
    {
    vtkErrorMacro(<< "Unable to open file: "<< this->FileName);
    this->SetErrorCode(vtkErrorCode::CannotOpenFileError);
    delete fptr;
    return NULL;
    }

  return fptr;
}

//-------------------------------------------------------------------------
void vtkMNIObjectWriter::CloseFile(ostream *fp)
{
  vtkDebugMacro(<<"Closing file\n");

1067
  delete fp;
1068
}