vtkFacetWriter.cxx 8.46 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
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkFacetWriter.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.

=========================================================================*/
#include "vtkFacetWriter.h"

#include "vtkUnstructuredGrid.h"
#include "vtkObjectFactory.h"
#include "vtkErrorCode.h"
#include "vtkCellType.h"
#include "vtkCellArray.h"
#include "vtkGarbageCollector.h"
#include "vtkAppendFilter.h"
#include "vtkPointData.h"
#include "vtkCellData.h"
#include "vtkInformationVector.h"
#include "vtkInformation.h"

#include "vtkUnsignedIntArray.h"
#include "vtkDoubleArray.h"
#include "vtkSmartPointer.h"

#include <sys/stat.h>
#include <vtkstd/string>
#include <vtkstd/vector>

37
vtkCxxRevisionMacro(vtkFacetWriter, "1.5");
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
vtkStandardNewMacro(vtkFacetWriter);

//----------------------------------------------------------------------------
vtkFacetWriter::vtkFacetWriter()
{
  this->FileName  = NULL;
  this->OutputStream = 0;
}

//----------------------------------------------------------------------------
vtkFacetWriter::~vtkFacetWriter()
{
  this->SetFileName(0);
}

//----------------------------------------------------------------------------
void vtkFacetWriter::Write()
{
  this->WriteToStream(0);
}

//----------------------------------------------------------------------------
int vtkFacetWriter::RequestData(
  vtkInformation* vtkNotUsed( request ),
  vtkInformationVector** inputVector,
  vtkInformationVector* vtkNotUsed( outputVector) )
{
  this->SetErrorCode(vtkErrorCode::NoError);

  int cleanStream = 0;
  if ( !this->OutputStream )
    {
    if ( !this->FileName )
      {
      vtkErrorMacro("File name not specified");
      return 0;
      }

    this->OutputStream = new ofstream(this->FileName);
    if ( !this->OutputStream )
      {
      vtkErrorMacro("Error opening file: " << this->FileName << " for writing");
      return 0;
      }
    cleanStream = 1;
    }

  if ( !this->OutputStream )
    {
    vtkErrorMacro("No output stream");
    return 0;
    }

  int cc;
  int len = inputVector[0]->GetNumberOfInformationObjects();
  *this->OutputStream << "FACET FILE FROM VTK" << endl
    << len << endl;

  for ( cc =0; cc < len; cc ++ )
    {
    vtkInformation *inInfo = inputVector[0]->GetInformationObject(cc);
    vtkPolyData *input = 
      vtkPolyData::SafeDownCast(inInfo->Get(vtkDataObject::DATA_OBJECT()));
    if ( !this->WriteDataToStream(this->OutputStream, input) )
      {
      if ( cleanStream )
        {
        delete this->OutputStream;
        this->OutputStream = 0;
        }
      return 0;
      }
    }
  if ( cleanStream )
    {
    delete this->OutputStream;
    this->OutputStream = 0;
    }
  return 1;
}

//----------------------------------------------------------------------------
void vtkFacetWriter::WriteToStream(ostream* ost)
{
  this->OutputStream = ost;
  // we always write, even if nothing has changed, so send a modified
  this->Modified();
  this->UpdateInformation();
  this->GetInput()->SetUpdateExtent(this->GetInput()->GetWholeExtent());
  this->Update();
  this->OutputStream = 0;
}

//----------------------------------------------------------------------------
int vtkFacetWriter::WriteDataToStream(ostream* ost, vtkPolyData* data)
{
  *ost << "Element" << data << endl
    << "0" << endl
    << data->GetNumberOfPoints() << " 0 0" << endl;
  vtkIdType point;
  for ( point = 0; point < data->GetNumberOfPoints(); point ++ )
    {
    double xyz[3];
    data->GetPoint(point, xyz);
    *ost << xyz[0] << " " << xyz[1] << " " << xyz[2] << endl;
    }
  *ost << "1" << endl
    << "Element" << data << endl;
  int written = 0;
  vtkCellArray* ca;
  vtkIdType numCells;
  vtkIdType totalCells = 0;
  int material = 0;
  int part = 0;
  vtkIdType cc;

  if ( data->GetVerts()->GetNumberOfCells() )
    {
156 157 158 159 160 161 162 163 164
    // This test is needed if another cell type is written above this
    // block.  We must remove it here because it produces an
    // unreachable code warning.
    //
    //if ( written )
    //  {
    //  vtkErrorMacro("Multiple different cells in the poly data");
    //  return 0;
    //  }
165 166
    ca = data->GetVerts();
    numCells = ca->GetNumberOfCells();
167 168
    vtkIdType numPts = 0;
    vtkIdType *pts = NULL;
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
    ca->InitTraversal();
    while ( ca->GetNextCell( numPts, pts ) )
      {
      // Each vertex is one cell
      for ( cc = 0; cc < numPts; cc ++ )
        {
        totalCells ++;
        }
      }
    *ost << totalCells << " 1" << endl;
    ca->InitTraversal();
    while ( ca->GetNextCell( numPts, pts ) )
      {
      for ( cc = 0; cc < numPts; cc ++ )
        {
        // Indices of point starts with 1
Andy Cedilnik's avatar
Andy Cedilnik committed
185 186
        vtkIdType pointIndex = pts[cc] + 1;
        *ost << pointIndex << " " << material << " " << part << endl;
187 188 189 190 191 192 193 194 195 196 197 198 199 200
        }
      }
    written = 1;
    }

  if ( data->GetLines()->GetNumberOfCells() )
    {
    if ( written )
      {
      vtkErrorMacro("Multiple different cells in the poly data");
      return 0;
      }
    ca = data->GetLines();
    numCells = ca->GetNumberOfCells();
201 202
    vtkIdType numPts = 0;
    vtkIdType *pts = NULL;
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
    ca->InitTraversal();
    while ( ca->GetNextCell( numPts, pts ) )
      {
      // One line per cell
      for ( cc = 1; cc < numPts; cc ++ )
        {
        totalCells ++;
        }
      }
    *ost << totalCells << " 2" << endl;
    ca->InitTraversal();
    while ( ca->GetNextCell( numPts, pts ) )
      {
      for ( cc = 1; cc < numPts; cc ++ )
        {
        vtkIdType point1 = pts[cc-1] + 1;
        vtkIdType point2 = pts[cc] + 1;
        *ost << point1 << " " << point2 << " " << material << " " << part << endl;
        }
      }
    written = 1;
    }

  if ( data->GetPolys()->GetNumberOfCells() )
    {
    if ( written )
      {
      vtkErrorMacro("Multiple different cells in the poly data");
      return 0;
      }
    ca = data->GetPolys();
    numCells = ca->GetNumberOfCells();
235 236
    vtkIdType numPts = 0;
    vtkIdType *pts = NULL;
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
    ca->InitTraversal();
    ca->GetNextCell(numPts, pts);
    totalCells ++;
    vtkIdType numPoints = numPts;
    while ( ca->GetNextCell( numPts, pts ) )
      {
      if ( numPts != numPoints )
        {
        vtkErrorMacro("Found polygons with different order");
        return 0;
        }
      totalCells ++;
      }
    *ost << totalCells << " " << numPoints << endl;
    ca->InitTraversal();
    int cnt = 0;
    while ( ca->GetNextCell( numPts, pts ) )
      {
      for ( cc = 0; cc < numPts; cc ++ )
        {
257 258
        vtkIdType pointindex = pts[cc] + 1;
        *ost << pointindex << " ";
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
        }
      *ost << material << " " << part << endl;
      cnt ++;
      }
    cout << "Written: " << cnt  << " / " << numCells << " / " << totalCells << endl;
    written = 1;
    }

  if ( data->GetStrips()->GetNumberOfCells() )
    {
    if ( written )
      {
      vtkErrorMacro("Multiple different cells in the poly data");
      return 0;
      }
    ca = data->GetStrips();
    numCells = ca->GetNumberOfCells();
276 277
    vtkIdType numPts = 0;
    vtkIdType *pts = NULL;
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
    ca->InitTraversal();
    while ( ca->GetNextCell( numPts, pts ) )
      {
      // One triangle per cell
      for ( cc = 2; cc < numPts; cc ++ )
        {
        totalCells ++;
        }
      }
    *ost << totalCells << " 3" << endl;
    ca->InitTraversal();
    while ( ca->GetNextCell( numPts, pts ) )
      {
      for ( cc = 2; cc < numPts; cc ++ )
        {
        vtkIdType point1 = pts[cc-2] + 1;
        vtkIdType point2 = pts[cc-1] + 1;
        vtkIdType point3 = pts[cc] + 1;
        *ost << point1 << " " << point2 << " " << point3 << " " << material << " " << part << endl;
        }
      }
    written = 1;
    }

 return 1;
}

//----------------------------------------------------------------------------
int vtkFacetWriter::FillInputPortInformation(int port, vtkInformation *info)
{
  if (!this->Superclass::FillInputPortInformation(port, info))
    {
    return 0;
    }
  info->Set(vtkAlgorithm::INPUT_IS_REPEATABLE(), 1);
  return 1;
}
//----------------------------------------------------------------------------
void vtkFacetWriter::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os,indent);

  os << indent << "File Name: " 
    << (this->FileName ? this->FileName : "(none)") << "\n";
}