7 #include "fwDataIO/reader/MeshReader.hpp" 9 #include "fwDataIO/reader/registry/macros.hpp" 11 #include <fwData/location/SingleFile.hpp> 12 #include <fwData/Mesh.hpp> 14 #include <fwDataTools/helper/Array.hpp> 15 #include <fwDataTools/helper/Mesh.hpp> 17 #include <boost/spirit/include/phoenix_bind.hpp> 18 #include <boost/spirit/include/phoenix_core.hpp> 19 #include <boost/spirit/include/phoenix_operator.hpp> 20 #include <boost/spirit/include/phoenix_statement.hpp> 21 #include <boost/spirit/include/phoenix_stl.hpp> 22 #include <boost/spirit/include/qi.hpp> 38 ::fwData::Mesh::CellDataOffsetType current;
47 ::fwData::Mesh::CellDataOffsetType res = current;
55 template <
typename Iterator>
56 bool parseTrian2(Iterator first, Iterator last, ::fwData::Mesh::sptr mesh)
58 using boost::spirit::qi::ulong_long;
59 using boost::spirit::qi::int_;
60 using boost::spirit::qi::float_;
61 using boost::spirit::qi::phrase_parse;
62 using boost::spirit::qi::_1;
63 using boost::spirit::qi::_2;
64 using boost::spirit::qi::_3;
65 using boost::spirit::qi::_4;
66 using boost::spirit::qi::_5;
67 using boost::spirit::qi::_6;
68 using boost::spirit::qi::repeat;
69 using boost::spirit::ascii::space;
70 using boost::phoenix::push_back;
71 using boost::phoenix::ref;
72 namespace phx = boost::phoenix;
74 unsigned long long int nbPoints;
75 unsigned long long int nbCells;
77 mesh->allocateCellNormals();
79 ::fwData::Array::sptr pointArray = mesh->getPointsArray();
80 ::fwData::Array::sptr cellDataArray = mesh->getCellDataArray();
81 ::fwData::Array::sptr cellTypesArray = mesh->getCellTypesArray();
82 ::fwData::Array::sptr cellDataOffsetsArray = mesh->getCellDataOffsetsArray();
83 ::fwData::Array::sptr cellNormalsArray = mesh->getCellNormalsArray();
94 ::fwData::Mesh::PointValueType* pointArrayBuffer = 0;
95 ::fwData::Mesh::CellValueType* cellDataArrayBuffer = 0;
96 ::fwData::Mesh::NormalValueType* cellNormalsArrayBuffer = 0;
102 std::ref(pointArraySize),
true);
104 bool r = phrase_parse(first, last,
112 phx::push_back(phx::ref(pointArraySize), phx::ref(nbPoints)),
114 ref(pointArrayBuffer) =
115 phx::bind(&::fwDataTools::helper::Array::begin < ::fwData::Mesh::PointValueType >,
119 >> repeat(ref(nbPoints))
121 (float_ >> float_ >> float_)
123 *ref(pointArrayBuffer)++ = _1,
124 *ref(pointArrayBuffer)++ = _2,
125 *ref(pointArrayBuffer)++ = _3
135 ref(cellDataArrayBuffer) =
136 phx::bind(&::fwDataTools::helper::Array::begin < ::fwData::Mesh::CellValueType >,
138 ref(cellNormalsArrayBuffer) =
139 phx::bind(&::fwDataTools::helper::Array::begin < ::fwData::Mesh::NormalValueType >
144 >> repeat(ref(nbCells))
146 (int_ >> int_ >> int_ >> float_ >> float_ >> float_)
148 *ref(cellDataArrayBuffer)++ = _1,
149 *ref(cellDataArrayBuffer)++ = _2,
150 *ref(cellDataArrayBuffer)++ = _3,
151 *ref(cellNormalsArrayBuffer)++ = _4,
152 *ref(cellNormalsArrayBuffer)++ = _5,
153 *ref(cellNormalsArrayBuffer)++ = _6
163 cellTypesHelper.
begin< ::fwData::Mesh::CellTypes >(),
164 cellTypesHelper.end< ::fwData::Mesh::CellTypes >(),
165 static_cast< ::fwData::Mesh::CellTypes >(::fwData::Mesh::TRIANGLE)
171 cellDataOffsetsHelper.
begin< ::fwData::Mesh::CellDataOffsetType >(),
172 cellDataOffsetsHelper.end< ::fwData::Mesh::CellDataOffsetType >(),
173 cellDataOffsetGenerator
177 const float normalBadValue = -1.f;
178 float normal = normalBadValue;
179 int& n = *
reinterpret_cast<int*
>(&normal);
182 cellNormalsHelper.
begin<
int >(),
183 cellNormalsHelper.end<
int >(),
184 ref(n) &= boost::phoenix::arg_names::arg1
189 mesh->clearCellNormals();
190 SLM_WARN(
"normals equals to (-1,-1,-1) : normals removed.");
218 assert( std::dynamic_pointer_cast< ::fwData::location::SingleFile >(
m_location) );
221 SLM_TRACE(
"Trian file: " + path.string());
222 SLM_ASSERT(
"Empty path for Trian file", !path.empty() );
228 file.open(path.string().c_str(), std::ios::binary );
232 SLM_ERROR(
"Trian file loading error for " + path.string());
233 throw std::ios_base::failure(
"Unable to open " + path.string());
236 file.seekg(0, std::ios::end);
237 length = file.tellg();
238 file.seekg(0, std::ios::beg);
242 char* buffer = &buf[0];
244 file.read(buffer, length);
251 if (!parseTrian2(buffer, buffer+length, mesh))
253 OSLM_ERROR(
"Bad file format : " << path.string());
254 throw std::ios_base::failure(
"Unable to open " + path.string() +
" : Bad file format.");
261 std::string MeshReader::extension()
virtual FWDATA_API size_t resize(const ::fwTools::Type &type, const SizeType &size, size_t nbOfComponents, bool reallocate=false)
Resizes and allocate (if needed) the array.
std::vector< size_t > SizeType
Array size type.
This class defines a single file location.
FWDATA_API bool adjustAllocatedMemory()
Adjust mesh memory usage.
This namespace fwDataIO contains reader and writer for several framework's data.
Key class used to restrict access to Object construction. See http://www.drdobbs.com/184402053.
Mesh reader. Read file format .trian.
#define SLM_WARN(message)
#define SLM_ERROR(message)
Provides a way to manage a view on a multidimentionnal array.
#define OSLM_ERROR(message)
FWDATAIO_API MeshReader(::fwDataIO::reader::IObjectReader::Key key)
Constructor. Do nothing.
#define SLM_ASSERT(message, cond)
work like 'assert' from 'cassert', with in addition a message logged by spylog (with FATAL loglevel) ...
virtual std::shared_ptr< DataType > getConcreteObject()
m_object getter.
virtual FWDATAIO_API void read() override
Read the file with standard iostream API.
FWDATA_API void setNumberOfPoints(Id nb)
Set number of points.
#define SLM_TRACE(message)
FWDATA_API void setCellDataSize(Id nb)
Set cell data size.
Base class for all object readers.
Contains the representation of the data objects used in the framework.
FWDATA_API void setNumberOfCells(Id nb)
Set number of cells.
virtual FWDATAIO_API ~MeshReader()
Destructor. Do nothing.
::fwData::location::ILocation::sptr m_location
Object location ( file path, directory path, url, etc )