vtkOBJWriter.cxx 7.88 KB
Newer Older
Michael Migliore's avatar
Michael Migliore committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*=========================================================================

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

#include "vtkDataSet.h"
#include "vtkErrorCode.h"
#include "vtkImageData.h"
#include "vtkInformation.h"
21
#include "vtkNumberToString.h"
Michael Migliore's avatar
Michael Migliore committed
22 23 24 25 26
#include "vtkPNGWriter.h"
#include "vtkPointData.h"
#include "vtkPolyData.h"
#include "vtkSmartPointer.h"
#include "vtkTriangleStrip.h"
27

28
#include "vtksys/FStream.hxx"
Michael Migliore's avatar
Michael Migliore committed
29 30 31 32
#include "vtksys/SystemTools.hxx"

namespace
{
33
//------------------------------------------------------------------------------
34
void WriteFaces(std::ostream& f, vtkCellArray* faces, bool withNormals, bool withTCoords)
Michael Migliore's avatar
Michael Migliore committed
35 36
{
  vtkIdType npts;
37
  const vtkIdType* indx;
Michael Migliore's avatar
Michael Migliore committed
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
  for (faces->InitTraversal(); faces->GetNextCell(npts, indx);)
  {
    f << "f";
    for (vtkIdType i = 0; i < npts; i++)
    {
      f << " " << indx[i] + 1;
      if (withTCoords)
      {
        f << "/" << indx[i] + 1;
        if (withNormals)
        {
          f << "/" << indx[i] + 1;
        }
      }
      else if (withNormals)
      {
        f << "//" << indx[i] + 1;
      }
    }
    f << "\n";
  }
}

61
//------------------------------------------------------------------------------
62
void WriteLines(std::ostream& f, vtkCellArray* lines)
Michael Migliore's avatar
Michael Migliore committed
63 64
{
  vtkIdType npts;
65
  const vtkIdType* indx;
Michael Migliore's avatar
Michael Migliore committed
66 67 68 69 70 71 72 73 74 75 76
  for (lines->InitTraversal(); lines->GetNextCell(npts, indx);)
  {
    f << "l";
    for (vtkIdType i = 0; i < npts; i++)
    {
      f << " " << indx[i] + 1;
    }
    f << "\n";
  }
}

77
//------------------------------------------------------------------------------
78
void WritePoints(std::ostream& f, vtkPoints* pts, vtkDataArray* normals, vtkDataArray* tcoords)
Michael Migliore's avatar
Michael Migliore committed
79
{
80
  vtkNumberToString convert;
Michael Migliore's avatar
Michael Migliore committed
81
  vtkIdType nbPts = pts->GetNumberOfPoints();
82

Michael Migliore's avatar
Michael Migliore committed
83 84 85 86 87
  // Positions
  for (vtkIdType i = 0; i < nbPts; i++)
  {
    double p[3];
    pts->GetPoint(i, p);
88
    f << "v " << convert(p[0]) << " " << convert(p[1]) << " " << convert(p[2]) << "\n";
Michael Migliore's avatar
Michael Migliore committed
89 90 91 92 93 94 95 96 97
  }

  // Normals
  if (normals)
  {
    for (vtkIdType i = 0; i < nbPts; i++)
    {
      double p[3];
      normals->GetTuple(i, p);
98
      f << "vn " << convert(p[0]) << " " << convert(p[1]) << " " << convert(p[2]) << "\n";
Michael Migliore's avatar
Michael Migliore committed
99 100 101 102 103 104 105 106 107 108
    }
  }

  // Textures
  if (tcoords)
  {
    for (vtkIdType i = 0; i < nbPts; i++)
    {
      double p[2];
      tcoords->GetTuple(i, p);
109
      f << "vt " << convert(p[0]) << " " << convert(p[1]) << "\n";
Michael Migliore's avatar
Michael Migliore committed
110 111 112 113
    }
  }
}

114
//------------------------------------------------------------------------------
115
bool WriteTexture(std::ostream& f, const std::string& baseName, vtkImageData* texture)
Michael Migliore's avatar
Michael Migliore committed
116 117
{
  std::string mtlName = baseName + ".mtl";
118
  vtksys::ofstream fmtl(mtlName.c_str(), vtksys::ofstream::out);
Michael Migliore's avatar
Michael Migliore committed
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
  if (fmtl.fail())
  {
    return false;
  }

  // write png file
  std::string pngName = baseName + ".png";
  vtkNew<vtkPNGWriter> pngWriter;
  pngWriter->SetInputData(texture);
  pngWriter->SetFileName(pngName.c_str());
  pngWriter->Write();

  // remove directories
  mtlName = vtksys::SystemTools::GetFilenameName(mtlName);
  pngName = vtksys::SystemTools::GetFilenameName(pngName);

  // set material
  fmtl << "newmtl vtktexture\n";
  fmtl << "map_Kd " << pngName << "\n";

  // declare material in obj file
  f << "mtllib " + mtlName + "\n";
  f << "usemtl vtktexture\n";

  return true;
}
}

147
//------------------------------------------------------------------------------
Michael Migliore's avatar
Michael Migliore committed
148 149
vtkStandardNewMacro(vtkOBJWriter);

150
//------------------------------------------------------------------------------
Michael Migliore's avatar
Michael Migliore committed
151 152 153 154 155 156
vtkOBJWriter::vtkOBJWriter()
{
  this->FileName = nullptr;
  this->SetNumberOfInputPorts(2);
}

157
//------------------------------------------------------------------------------
Michael Migliore's avatar
Michael Migliore committed
158 159 160 161 162
vtkOBJWriter::~vtkOBJWriter()
{
  this->SetFileName(nullptr);
}

163
//------------------------------------------------------------------------------
Michael Migliore's avatar
Michael Migliore committed
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
void vtkOBJWriter::WriteData()
{
  vtkPolyData* input = this->GetInputGeometry();
  vtkImageData* texture = this->GetInputTexture();

  if (input == nullptr)
  {
    vtkErrorMacro("No geometry to write!");
    this->SetErrorCode(vtkErrorCode::UnknownError);
    return;
  }

  vtkPoints* pts = input->GetPoints();
  vtkCellArray* polys = input->GetPolys();
  vtkCellArray* strips = input->GetStrips();
  vtkCellArray* lines = input->GetLines();
  vtkDataArray* normals = input->GetPointData()->GetNormals();
  vtkDataArray* tcoords = input->GetPointData()->GetTCoords();

  if (pts == nullptr)
  {
    vtkErrorMacro("No data to write!");
    this->SetErrorCode(vtkErrorCode::UnknownError);
    return;
  }

  if (this->FileName == nullptr)
  {
    vtkErrorMacro("Please specify FileName to write");
    this->SetErrorCode(vtkErrorCode::NoFileNameError);
    return;
  }

  vtkIdType npts = 0;

199
  vtksys::ofstream f(this->FileName, vtksys::ofstream::out);
Michael Migliore's avatar
Michael Migliore committed
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
  if (f.fail())
  {
    vtkErrorMacro("Unable to open file: " << this->FileName);
    this->SetErrorCode(vtkErrorCode::CannotOpenFileError);
    return;
  }

  // Write header
  f << "# Generated by Visualization Toolkit\n";

  // Write material if a texture is specified
  if (texture)
  {
    std::vector<std::string> comp;
    vtksys::SystemTools::SplitPath(vtksys::SystemTools::GetFilenamePath(this->FileName), comp);
    comp.push_back(vtksys::SystemTools::GetFilenameWithoutLastExtension(this->FileName));
    if (!::WriteTexture(f, vtksys::SystemTools::JoinPath(comp), texture))
    {
      vtkErrorMacro("Unable to create material file");
    }
  }

  // Write points
  ::WritePoints(f, pts, normals, tcoords);

  // Decompose any triangle strips into triangles
  vtkNew<vtkCellArray> polyStrips;
  if (strips->GetNumberOfCells() > 0)
  {
229
    const vtkIdType* ptIds = nullptr;
Michael Migliore's avatar
Michael Migliore committed
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
    for (strips->InitTraversal(); strips->GetNextCell(npts, ptIds);)
    {
      vtkTriangleStrip::DecomposeStrip(npts, ptIds, polyStrips);
    }
  }

  // Write triangle strips
  ::WriteFaces(f, polyStrips, normals != nullptr, tcoords != nullptr);

  // Write polygons.
  if (polys)
  {
    ::WriteFaces(f, polys, normals != nullptr, tcoords != nullptr);
  }

  // Write lines.
  if (lines)
  {
    ::WriteLines(f, lines);
  }

  f.close();
}

254
//------------------------------------------------------------------------------
Michael Migliore's avatar
Michael Migliore committed
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
void vtkOBJWriter::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os, indent);

  os << indent << "FileName: " << (this->GetFileName() ? this->GetFileName() : "(none)") << endl;
  os << indent << "Input: " << this->GetInputGeometry() << endl;

  vtkImageData* texture = this->GetInputTexture();
  if (texture)
  {
    os << indent << "Texture:" << endl;
    texture->PrintSelf(os, indent.GetNextIndent());
  }
}

270
//------------------------------------------------------------------------------
Michael Migliore's avatar
Michael Migliore committed
271 272 273 274 275
vtkPolyData* vtkOBJWriter::GetInputGeometry()
{
  return vtkPolyData::SafeDownCast(this->GetInput(0));
}

276
//------------------------------------------------------------------------------
Michael Migliore's avatar
Michael Migliore committed
277 278 279 280 281
vtkImageData* vtkOBJWriter::GetInputTexture()
{
  return vtkImageData::SafeDownCast(this->GetInput(1));
}

282
//------------------------------------------------------------------------------
Michael Migliore's avatar
Michael Migliore committed
283 284 285 286 287
vtkDataSet* vtkOBJWriter::GetInput(int port)
{
  return vtkDataSet::SafeDownCast(this->Superclass::GetInput(port));
}

288
//------------------------------------------------------------------------------
Michael Migliore's avatar
Michael Migliore committed
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
int vtkOBJWriter::FillInputPortInformation(int port, vtkInformation* info)
{
  if (port == 0)
  {
    info->Set(vtkDataObject::DATA_TYPE_NAME(), "vtkPolyData");
    return 1;
  }
  if (port == 1)
  {
    info->Set(vtkDataObject::DATA_TYPE_NAME(), "vtkImageData");
    info->Set(vtkAlgorithm::INPUT_IS_OPTIONAL(), 1);
    return 1;
  }
  return 0;
}