fw4spl
core/fwDataIO/src/fwDataIO/writer/MeshWriter.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2018.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #include "fwDataIO/writer/MeshWriter.hpp"
8 
9 #include "fwDataIO/writer/registry/macros.hpp"
10 
11 #include <fwDataTools/helper/Array.hpp>
12 #include <fwDataTools/helper/MeshGetter.hpp>
13 #include <fwDataTools/Mesh.hpp>
14 
15 #include <fstream>
16 #include <iostream>
17 
18 fwDataIOWriterRegisterMacro( ::fwDataIO::writer::MeshWriter );
19 
20 namespace fwDataIO
21 {
22 
23 namespace writer
24 {
25 
26 //------------------------------------------------------------------------------
27 
29  ::fwData::location::enableSingleFile< ::fwDataIO::writer::IObjectWriter >(this)
30 {
31 }
32 
33 //------------------------------------------------------------------------------
34 
36 {
37 }
38 
39 //------------------------------------------------------------------------------
40 
42 {
43  OSLM_INFO( "[MeshReader::read] Trian file: " << getFile());
44  assert( getFile().empty() == false );
45 
46  ::fwData::Mesh::csptr mesh = this->getConcreteObject();
47  FW_RAISE_IF("Can't convert this Mesh to Trian file",
48  !::fwDataTools::Mesh::hasUniqueCellType(mesh, ::fwData::Mesh::TRIANGLE));
49 
50  std::fstream file;
51  file.open(getFile().string().c_str(), std::fstream::out);
52  file.precision(std::numeric_limits< float >::digits10);
53 
54  if (!file.is_open())
55  {
56  OSLM_ERROR( "Trian file writing error for " << getFile().string());
57  std::string str = "Unable to write ";
58  str += getFile().string();
59  throw std::ios_base::failure(str);
60  }
61 
62  ::fwDataTools::helper::MeshGetter meshHelper(mesh);
63 
64  size_t i, nbPts, nbCells;
65  nbPts = mesh->getNumberOfPoints();
66  ::fwData::Mesh::ConstPointsMultiArrayType points = meshHelper.getPoints();
67  file<<nbPts<<std::endl;
68  for( i = 0; i < nbPts; ++i )
69  {
70  file << points[i][0] << " " << points[i][1] << " " << points[i][2] << std::endl;
71  }
72 
73  nbCells = mesh->getNumberOfCells();
74  ::fwData::Array::sptr cells = mesh->getCellDataArray();
75 
76  ::fwDataTools::helper::Array cellsArrayHelper(cells);
77 
78  FW_RAISE_IF("Not able to write " << cells->getType().string() << " cell type in trian file.",
79  cells->getType() != ::fwTools::Type::create< std::uint64_t >());
80 
81  std::uint64_t* cellBuf = cellsArrayHelper.begin< std::uint64_t >();
82  std::uint64_t* cellBufEnd = cellBuf + 3*nbCells;
83 
84  SLM_ASSERT("Wrong CellDataMultiArray size", cells->getNumberOfElements() >= nbCells*3);
85  file << nbCells << std::endl;
86 
87  ::fwData::Array::sptr normals = mesh->getCellNormalsArray();
88 
89  if(normals
90  && !normals->empty()
91  && normals->getType() == ::fwTools::Type::create<float>()
92  && normals->getNumberOfComponents() == 3
93  && normals->getNumberOfDimensions() == 1
94  && nbCells == normals->getSize().at(0)
95  )
96  {
97  ::fwDataTools::helper::Array normalsArrayHelper(normals);
98  float* normalBuf = normalsArrayHelper.begin< float >();
99 
100  while (cellBuf != cellBufEnd)
101  {
102  file << (*cellBuf++) << " ";
103  file << (*cellBuf++) << " ";
104  file << (*cellBuf++) << " ";
105  file << (*normalBuf++) << " ";
106  file << (*normalBuf++) << " ";
107  file << (*normalBuf++) << std::endl;
108  }
109  }
110  else
111  {
112  while (cellBuf != cellBufEnd)
113  {
114  file << (*cellBuf++) << " ";
115  file << (*cellBuf++) << " ";
116  file << (*cellBuf++) << " -1 -1 -1" << std::endl;
117  }
118  }
119  file.close();
120 }
121 
122 //------------------------------------------------------------------------------
123 
125 {
126  return ".trian";
127 }
128 
129 //------------------------------------------------------------------------------
130 
131 } // namespace writer
132 
133 } // namespace fwDataIO
This namespace fwDataIO contains reader and writer for several framework&#39;s data.
virtual std::shared_ptr< const DataType > getConcreteObject() const
m_object getter.
virtual FWDATAIO_API std::string extension() override
Defines extension supported by this reader ".trian".
#define OSLM_INFO(message)
Definition: spyLog.hpp:252
Key class used to restrict access to Object construction. See http://www.drdobbs.com/184402053.
FWDATAIO_API MeshWriter(::fwDataIO::writer::IObjectWriter::Key key)
Constructor. Do nothing.
#define OSLM_ERROR(message)
Definition: spyLog.hpp:274
virtual FWDATATOOLS_API char * begin()
Returns the begining/end of the buffer interpreted as a char buffer.
Helper to manage Mesh. Lock the mesh buffer before to modify it.
Definition: MeshGetter.hpp:28
#define SLM_ASSERT(message, cond)
work like &#39;assert&#39; from &#39;cassert&#39;, with in addition a message logged by spylog (with FATAL loglevel) ...
Definition: spyLog.hpp:308
Base class for all object writers.
FWDATATOOLS_API::fwData::Mesh::ConstPointsMultiArrayType getPoints() const
Returns the internal corresponding array as a boost::multi_array_ref.
Definition: MeshGetter.cpp:77
virtual FWDATAIO_API ~MeshWriter()
Destructor. Do nothing.
Contains the representation of the data objects used in the framework.
Helper to manage array buffer. Lock the buffer before to modify it.
static FWDATATOOLS_API bool hasUniqueCellType(::fwData::Mesh::csptr mesh,::fwData::Mesh::CellTypes cell)