fw4spl
AlgoMeshDeformation.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 "Tuto14MeshGenerator/AlgoMeshDeformation.hpp"
8 
9 #include <fwDataTools/helper/Array.hpp>
10 #include <fwDataTools/Mesh.hpp>
11 
12 #include <fwTools/NumericRoundCast.hxx>
13 
14 namespace Tuto14MeshGenerator
15 {
16 
17 //-----------------------------------------------------------------------------
18 
20  m_nbStep(0),
21  m_amplitude(40),
22  m_step(0),
23  m_direction(1),
24  m_nbPoints(0),
25  m_nbCells(0),
26  m_yCenter(0)
27 {
28 }
29 
30 //-----------------------------------------------------------------------------
31 
33 {
34 }
35 
36 //-----------------------------------------------------------------------------
37 
39  ::fwData::Mesh::sptr _mesh,
40  const unsigned int _nbStep,
41  const unsigned int _amplitude)
42 {
43  m_mesh = _mesh;
44  m_nbStep = _nbStep;
45  m_amplitude = _amplitude;
46  m_direction = 1;
47 
48  m_nbPoints = _mesh->getNumberOfPoints();
49  m_nbCells = _mesh->getNumberOfCells();
50 }
51 
52 //-----------------------------------------------------------------------------
53 
54 void AlgoMeshDeformation::computeDeformation( ::fwData::Mesh::sptr _mesh,
55  const unsigned int _nbStep,
56  const unsigned int _amplitude )
57 {
58  if ( m_mesh.expired() ||
59  m_nbPoints != _mesh->getNumberOfPoints() ||
60  m_nbCells != _mesh->getNumberOfCells() ||
61  !_mesh->getPointColorsArray())
62  {
63  this->setParam( _mesh, _nbStep, _amplitude );
64  this->initSimu();
65  }
66  else
67  {
68  this->computeSimu();
69  }
70 }
71 
72 //-----------------------------------------------------------------------------
73 
75 {
77  m_originPoints = ::fwData::Object::copy( m_mesh.lock()->getPointsArray() );
78  m_step = 0;
79 
80  if ( !m_mesh.lock()->getPointColorsArray() )
81  {
83  }
84 
85  m_meshHelper = ::fwDataTools::helper::Mesh::New(m_mesh.lock());
86 
87  float max = std::numeric_limits<float>::min();
88  float min = std::numeric_limits<float>::max();
89 
90  ::fwData::Mesh::PointsMultiArrayType points = m_meshHelper->getPoints();
91  float coord;
92  for(unsigned int i = 0; i < m_nbPoints; ++i)
93  {
94  coord = points[i][1];
95  if ( coord < min )
96  {
97  min = coord;
98  }
99  if ( coord > max )
100  {
101  max = coord;
102  }
103  }
104 
105  m_yCenter = (max - min) / 2 + min;
106 }
107 
108 //-----------------------------------------------------------------------------
109 
111 {
112  SLM_TRACE_FUNC();
113  m_step += m_direction;
114  if ( m_step == m_nbStep )
115  {
116  m_direction = -1;
117  }
118  else if ( m_step == 0 )
119  {
120  m_direction = 1;
121  }
122 
123  const float scale = m_step / (float) m_nbStep;
124 
125  ::fwDataTools::helper::Array originPointsHelper(m_originPoints);
126 
127  ::fwData::Mesh::PointsMultiArrayType points = m_meshHelper->getPoints();
128  ::fwData::Mesh::PointColorsMultiArrayType colors = m_meshHelper->getPointColors();
129 
130  ::fwData::Mesh::PointsMultiArrayType opoints =
131  ::fwData::Mesh::PointsMultiArrayType(
132  static_cast< ::fwData::Mesh::PointsMultiArrayType::element* >(originPointsHelper.getBuffer()),
133  ::boost::extents[m_nbPoints][3] );
134 
135  for(unsigned int i = 0; i < m_nbPoints; ++i)
136  {
137  points[i][0] = opoints[i][0];
138  OSLM_TRACE("opoints[i][1] - m_yCenter = " << opoints[i][1] - m_yCenter);
139  if( opoints[i][1] - m_yCenter > 0 )
140  {
141  points[i][1] = opoints[i][1] + (opoints[i][1] - m_yCenter) * scale;
142  colors[i][0] = ::fwTools::numericRoundCast< ::fwData::Mesh::ColorValueType >(255 * scale);
143  }
144  else
145  {
146  colors[i][0] = 0;
147  }
148  points[i][2] = opoints[i][2];
149  }
150 
152 }
153 
154 //-----------------------------------------------------------------------------
155 
156 } // namespace Tuto14MeshGenerator
157 
#define SLM_TRACE_FUNC()
Trace contextual function signature.
Definition: spyLog.hpp:329
virtual TUTO14MESHGENERATOR_API ~AlgoMeshDeformation() noexcept
Destructor.
virtual FWDATATOOLS_API void * getBuffer()
Getter for the array buffer.
#define OSLM_TRACE(message)
Definition: spyLog.hpp:230
static FWDATATOOLS_API void generatePointNormals(::fwData::Mesh::sptr mesh)
Generate point normals for the mesh.
TUTO14MESHGENERATOR_API void setParam(::fwData::Mesh::sptr _mesh, const unsigned int _step, const unsigned int _amplitude)
Initialize algorithm&#39;s parameters.
TUTO14MESHGENERATOR_API void initSimu()
Initialize the simulated acquisition.
TUTO14MESHGENERATOR_API void computeDeformation(::fwData::Mesh::sptr _mesh, const unsigned int _nbStep, const unsigned int _amplitude)
Compute deformation or init algo if necessary.
static FWDATA_API::fwData::Object::sptr copy(const ::fwData::Object::csptr &source)
return a copy of the source. if source is a null pointer, return a null pointer.
TUTO14MESHGENERATOR_API AlgoMeshDeformation() noexcept
Constructor.
static FWDATATOOLS_API void colorizeMeshPoints(::fwData::Mesh::sptr mesh)
Colorize mesh (vertex point color).
Helper to manage array buffer. Lock the buffer before to modify it.
TUTO14MESHGENERATOR_API void computeSimu()
Compute the simulated acquisition for the given step between inspiration and expiration.