fw4spl
tests/fwTest/src/fwTest/generator/Mesh.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2017.
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 "fwTest/generator/Mesh.hpp"
8 
9 #include <fwDataTools/Mesh.hpp>
10 
11 #include <fwTools/NumericRoundCast.hxx>
12 
13 #include <cstdlib>
14 #include <ctime>
15 
16 namespace fwTest
17 {
18 namespace generator
19 {
20 
21 struct RandFloat {
22  //------------------------------------------------------------------------------
23 
24  float operator()()
25  {
26  return ((rand()%101-50.f))/500.f;
27  }
28 };
29 
30 //------------------------------------------------------------------------------
31 
33 {
34  std::srand(::fwTools::numericRoundCast<unsigned int>(std::time(NULL)));
35 }
36 
37 //------------------------------------------------------------------------------
38 
39 void Mesh::generateMesh( ::fwData::Mesh::sptr mesh )
40 {
48  mesh->adjustAllocatedMemory();
49 }
50 
51 //------------------------------------------------------------------------------
52 
53 void Mesh::generateTriangleQuadMesh(::fwData::Mesh::sptr mesh)
54 {
55  size_t nbPointsByEdge = 10;
56  float edgeDim = 100.;
57  Mesh::PointsMapType points;
58 
59  mesh->clear();
60  Mesh::addTriangleMesh(mesh, points, nbPointsByEdge, edgeDim);
61  Mesh::addQuadMesh(mesh, points, nbPointsByEdge, edgeDim);
62 }
63 
64 //------------------------------------------------------------------------------
65 
66 void Mesh::generateTriangleMesh(::fwData::Mesh::sptr mesh)
67 {
68  size_t nbPointsByEdge = 10;
69  float edgeDim = 100.;
70  Mesh::PointsMapType points;
71 
72  mesh->clear();
73  Mesh::addTriangleMesh(mesh, points, nbPointsByEdge, edgeDim);
74 }
75 
76 //------------------------------------------------------------------------------
77 
78 void Mesh::generateQuadMesh(::fwData::Mesh::sptr mesh)
79 {
80  size_t nbPointsByEdge = 10;
81  float edgeDim = 100.;
82  Mesh::PointsMapType points;
83 
84  mesh->clear();
85  Mesh::addQuadMesh(mesh, points, nbPointsByEdge, edgeDim);
86 }
87 //------------------------------------------------------------------------------
88 
89 void Mesh::addQuadMesh(::fwData::Mesh::sptr mesh, PointsMapType& points, size_t nbPointsByEdge, float edgeDim)
90 {
91  ::fwDataTools::helper::Mesh::sptr meshHelper;
92  meshHelper = ::fwDataTools::helper::Mesh::New(mesh);
93 
94  ::fwData::Mesh::PointValueType pt1[3], pt2[3], pt3[3], pt4[3];
95  ::fwData::Mesh::Id idx1, idx2, idx3, idx4;
96  float step = edgeDim / nbPointsByEdge;
97 
98  //Face Y = edgeDim
99  for(size_t x = 0; x < nbPointsByEdge; x++)
100  {
101  for(size_t z = 0; z < nbPointsByEdge; z++)
102  {
103  pt1[0] = x*step;
104  pt1[1] = edgeDim;
105  pt1[2] = z*step;
106 
107  pt2[0] = (x+1)*step;
108  pt2[1] = edgeDim;
109  pt2[2] = (z)*step;
110 
111  pt3[0] = x*step;
112  pt3[1] = edgeDim;
113  pt3[2] = (z+1)*step;
114 
115  pt4[0] = (x+1)*step;
116  pt4[1] = edgeDim;
117  pt4[2] = (z+1)*step;
118 
119  idx1 = Mesh::addPoint(pt1, meshHelper, points);
120  idx2 = Mesh::addPoint(pt2, meshHelper, points);
121  idx3 = Mesh::addPoint(pt3, meshHelper, points);
122  idx4 = Mesh::addPoint(pt4, meshHelper, points);
123 
124  meshHelper->insertNextCell(idx1, idx3, idx4, idx2);
125  }
126  }
127 
128  //Face X = edgeDim
129  for(size_t y = 0; y < nbPointsByEdge; y++)
130  {
131  for(size_t z = 0; z < nbPointsByEdge; z++)
132  {
133  pt1[0] = edgeDim;
134  pt1[1] = y*step;
135  pt1[2] = z*step;
136 
137  pt2[0] = edgeDim;
138  pt2[1] = y*step;
139  pt2[2] = (z+1)*step;
140 
141  pt3[0] = edgeDim;
142  pt3[1] = (y+1)*step;
143  pt3[2] = z*step;
144 
145  pt4[0] = edgeDim;
146  pt4[1] = (y+1)*step;
147  pt4[2] = (z+1)*step;
148 
149  idx1 = Mesh::addPoint(pt1, meshHelper, points);
150  idx2 = Mesh::addPoint(pt2, meshHelper, points);
151  idx3 = Mesh::addPoint(pt3, meshHelper, points);
152  idx4 = Mesh::addPoint(pt4, meshHelper, points);
153 
154  meshHelper->insertNextCell(idx1, idx3, idx4, idx2);
155  }
156  }
157 }
158 
159 //------------------------------------------------------------------------------
160 
161 void Mesh::addTriangleMesh(::fwData::Mesh::sptr mesh, PointsMapType& points, size_t nbPointsByEdge, float edgeDim)
162 {
163  ::fwDataTools::helper::Mesh::sptr meshHelper;
164  meshHelper = ::fwDataTools::helper::Mesh::New(mesh);
165 
166  ::fwData::Mesh::PointValueType pt1[3], pt2[3], pt3[3], pt4[3];
167  ::fwData::Mesh::Id idx1, idx2, idx3, idx4;
168  float step = edgeDim / nbPointsByEdge;
169 
170  //Face Z = 0
171  for(size_t x = 0; x < nbPointsByEdge; x++)
172  {
173  for(size_t y = 0; y < nbPointsByEdge; y++)
174  {
175  pt1[0] = x*step;
176  pt1[1] = y*step;
177  pt1[2] = 0;
178 
179  pt2[0] = (x+1)*step;
180  pt2[1] = y*step;
181  pt2[2] = 0;
182 
183  pt3[0] = x*step;
184  pt3[1] = (y+1)*step;
185  pt3[2] = 0;
186 
187  pt4[0] = (x+1)*step;
188  pt4[1] = (y+1)*step;
189  pt4[2] = 0;
190 
191  idx1 = Mesh::addPoint(pt1, meshHelper, points);
192  idx2 = Mesh::addPoint(pt2, meshHelper, points);
193  idx3 = Mesh::addPoint(pt3, meshHelper, points);
194  idx4 = Mesh::addPoint(pt4, meshHelper, points);
195 
196  meshHelper->insertNextCell(idx1, idx4, idx2);
197  meshHelper->insertNextCell(idx1, idx3, idx4);
198  }
199  }
200 
201  //Face X = 0
202  for(size_t y = 0; y < nbPointsByEdge; y++)
203  {
204  for(size_t z = 0; z < nbPointsByEdge; z++)
205  {
206  pt1[0] = 0;
207  pt1[1] = y*step;
208  pt1[2] = z*step;
209 
210  pt2[0] = 0;
211  pt2[1] = y*step;
212  pt2[2] = (z+1)*step;
213 
214  pt3[0] = 0;
215  pt3[1] = (y+1)*step;
216  pt3[2] = z*step;
217 
218  pt4[0] = 0;
219  pt4[1] = (y+1)*step;
220  pt4[2] = (z+1)*step;
221 
222  idx1 = Mesh::addPoint(pt1, meshHelper, points);
223  idx2 = Mesh::addPoint(pt2, meshHelper, points);
224  idx3 = Mesh::addPoint(pt3, meshHelper, points);
225  idx4 = Mesh::addPoint(pt4, meshHelper, points);
226 
227  meshHelper->insertNextCell(idx2, idx4, idx3);
228  meshHelper->insertNextCell(idx1, idx2, idx3);
229  }
230  }
231 }
232 
233 //------------------------------------------------------------------------------
234 
235 ::fwData::Mesh::Id Mesh::addPoint(::fwData::Mesh::PointValueType* pt,
236  ::fwDataTools::helper::Mesh::sptr meshHelper,
237  PointsMapType& points)
238 {
239  ::fwDataTools::Point myPoint(pt[0], pt[1], pt[2]);
240 
241  PointsMapType::iterator it = points.find(myPoint);
242  if(it != points.end())
243  {
244  return it->second;
245  }
246  ::fwData::Mesh::Id idx = meshHelper->insertNextPoint(pt[0], pt[1], pt[2]);
247  points[myPoint] = idx;
248  return idx;
249 }
250 
251 //------------------------------------------------------------------------------
252 
253 } // namespace generator
254 } // namespace fwTest
static FWTEST_API void addQuadMesh(::fwData::Mesh::sptr mesh, PointsMapType &points, size_t nbPointsByEdge=10, float edgeDim=100.)
Add quad cells in mesh, this method generates synthetic data (two face of a cube).
static FWDATATOOLS_API void generateCellNormals(::fwData::Mesh::sptr mesh)
Generate cell normals for the mesh.
static FWDATATOOLS_API void colorizeMeshCells(::fwData::Mesh::sptr mesh)
Colorize mesh (cell color).
static FWTEST_API void initRand()
Initialize &#39;rand&#39; seed.
static FWDATATOOLS_API void generatePointNormals(::fwData::Mesh::sptr mesh)
Generate point normals for the mesh.
Definition: Data.hpp:15
static FWTEST_API void addTriangleMesh(::fwData::Mesh::sptr mesh, PointsMapType &points, size_t nbPointsByEdge=10, float edgeDim=100.)
Add triangle cells in mesh, this method generates synthetic data (two face of a cube).
static FWTEST_API void generateQuadMesh(::fwData::Mesh::sptr mesh)
Generate a quad mesh.
static FWDATATOOLS_API void shakePoint(::fwData::Mesh::sptr mesh)
Shake points of the mesh.
static FWTEST_API void generateTriangleQuadMesh(::fwData::Mesh::sptr mesh)
Generate a mesh with quad and triangle cells.
static FWDATATOOLS_API void colorizeMeshPoints(::fwData::Mesh::sptr mesh)
Colorize mesh (vertex point color).
static FWTEST_API void generateMesh(::fwData::Mesh::sptr mesh)
Generate a mesh.
static FWTEST_API void generateTriangleMesh(::fwData::Mesh::sptr mesh)
Generate a triangle mesh.