@@ -135,7 +135,6 @@ CORE_NAMESPACE_END
135
135
CORE_NAMESPACE_BEGIN
136
136
CORE_GEOM_NAMESPACE_BEGIN
137
137
138
- // todo foot print 是什么意思??
139
138
float
140
139
pixel_footprint (std::size_t x, std::size_t y, float depth,
141
140
math::Matrix3f const & invproj)
@@ -168,11 +167,13 @@ dm_make_triangle (TriangleMesh* mesh, core::Image<unsigned int>& vidx,
168
167
core::TriangleMesh::VertexList& verts (mesh->get_vertices ());
169
168
core::TriangleMesh::FaceList& faces (mesh->get_faces ());
170
169
171
- for (int j = 0 ; j < 3 ; ++j) {
170
+ for (int j = 0 ; j < 3 ; ++j)
171
+ {
172
172
int iidx = i + (tverts[j] % 2 ) + width * (tverts[j] / 2 );
173
173
int x = iidx % width;
174
174
int y = iidx / width;
175
- if (vidx.at (iidx) == MATH_MAX_UINT) {
175
+ if (vidx.at (iidx) == MATH_MAX_UINT)
176
+ {
176
177
/* Add vertex for depth pixel. */
177
178
vidx.at (iidx) = verts.size ();
178
179
float depth = dm->at (iidx, 0 );
@@ -210,79 +211,59 @@ TriangleMesh::Ptr
210
211
depthmap_triangulate (FloatImage::ConstPtr dm, math::Matrix3f const & invproj,
211
212
float dd_factor, core::Image<unsigned int >* vids)
212
213
{
213
- // 深度图不为空
214
214
if (dm == nullptr )
215
215
throw std::invalid_argument (" Null depthmap given" );
216
216
217
- // 图像的宽和高
218
217
int const width = dm->width ();
219
218
int const height = dm->height ();
220
219
221
- // 创建三角网格接结构体
222
220
/* Prepare triangle mesh. */
223
221
TriangleMesh::Ptr mesh (TriangleMesh::create ());
224
222
225
223
/* Generate image that maps image pixels to vertex IDs. */
226
- // 创建映射图,将图像像素映射到三维点的索引
227
224
core::Image<unsigned int > vidx (width, height, 1 );
228
225
vidx.fill (MATH_MAX_UINT);
229
226
230
- // 在深度图中遍历2x2 blocks,并且创建三角面片
231
227
/* Iterate over 2x2-blocks in the depthmap and create triangles. */
232
228
int i = 0 ;
233
- for (int y = 0 ; y < height - 1 ; ++y, ++i) {
234
- for (int x = 0 ; x < width - 1 ; ++x, ++i) {
235
-
229
+ for (int y = 0 ; y < height - 1 ; ++y, ++i)
230
+ {
231
+ for (int x = 0 ; x < width - 1 ; ++x, ++i)
232
+ {
236
233
/* Cache the four depth values. */
237
- /*
238
- * 0, 1
239
- * 2, 3
240
- */
241
- float depths[4 ] = { dm->at (i, 0 ), dm->at (i + 1 , 0 ),
242
- dm->at (i + width, 0 ), dm->at (i + width + 1 , 0 )
243
- };
234
+ float depths[4 ] = { dm->at (i, 0 ), dm->at (i + 1 , 0 ),
235
+ dm->at (i + width, 0 ), dm->at (i + width + 1 , 0 ) };
244
236
245
237
/* Create a mask representation of the available depth values. */
246
- /* 创建mask记录深度有效的像素个数
247
- * mask=0000, 0001, 0010, 0011, 0100, 0101, 0110, 0111, 1000, 1001,
248
- * 1010, 1011, 1100, 1101, 1110, 1111
249
- */
250
238
int mask = 0 ;
251
239
int pixels = 0 ;
252
- for (int j = 0 ; j < 4 ; ++j){
253
- if (depths[j] > 0 .0f ) {
240
+ for (int j = 0 ; j < 4 ; ++j)
241
+ if (depths[j] > 0 .0f )
242
+ {
254
243
mask |= 1 << j;
255
244
pixels += 1 ;
256
245
}
257
- }
258
246
259
- // 至少保证3个深度值是可靠的
260
247
/* At least three valid depth values are required. */
261
248
if (pixels < 3 )
262
249
continue ;
263
250
264
-
265
251
/* Possible triangles, vertex indices relative to 2x2 block. */
266
- /* 可能出现的三角面片对,4个点有2个面片
267
- */
268
252
int tris[4 ][3 ] = {
269
253
{ 0 , 2 , 1 }, { 0 , 3 , 1 }, { 0 , 2 , 3 }, { 1 , 2 , 3 }
270
254
};
271
255
272
256
/* Decide which triangles to issue. */
273
- /* 决定用哪对面片
274
- */
275
257
int tri[2 ] = { 0 , 0 };
276
258
277
- switch (mask) {
278
-
279
- case 7 : tri[0 ] = 1 ; break ; // 0111- 0,1,2
280
- case 11 : tri[0 ] = 2 ; break ; // 1011- 0,1,3
281
- case 13 : tri[0 ] = 3 ; break ; // 1101- 0,2,3
282
- case 14 : tri[0 ] = 4 ; break ; // 1110- 1,2,3
283
- case 15 : // 1111- 0,1,2,3
259
+ switch (mask)
260
+ {
261
+ case 7 : tri[0 ] = 1 ; break ;
262
+ case 11 : tri[0 ] = 2 ; break ;
263
+ case 13 : tri[0 ] = 3 ; break ;
264
+ case 14 : tri[0 ] = 4 ; break ;
265
+ case 15 :
284
266
{
285
- // 空圆特性
286
267
/* Choose the triangulation with smaller diagonal. */
287
268
float ddiff1 = std::abs (depths[0 ] - depths[3 ]);
288
269
float ddiff2 = std::abs (depths[1 ] - depths[2 ]);
@@ -296,18 +277,21 @@ depthmap_triangulate (FloatImage::ConstPtr dm, math::Matrix3f const& invproj,
296
277
}
297
278
298
279
/* Omit depth discontinuity detection if dd_factor is zero. */
299
- if (dd_factor > 0 .0f ) {
280
+ if (dd_factor > 0 .0f )
281
+ {
300
282
/* Cache pixel footprints. */
301
283
float widths[4 ];
302
- for (int j = 0 ; j < 4 ; ++j) {
284
+ for (int j = 0 ; j < 4 ; ++j)
285
+ {
303
286
if (depths[j] == 0 .0f )
304
287
continue ;
305
- widths[j] = pixel_footprint (x + (j % 2 ), y + (j / 2 ), depths[j], invproj);// w, h, focal_len);
288
+ widths[j] = pixel_footprint (x + (j % 2 ), y + (j / 2 ),
289
+ depths[j], invproj);// w, h, focal_len);
306
290
}
307
291
308
- // 检查深度不一致性,相邻像素的深度差值不要超过像素宽度(三维空间中)的dd_factor倍
309
292
/* Check for depth discontinuities. */
310
- for (int j = 0 ; j < 2 && tri[j] != 0 ; ++j) {
293
+ for (int j = 0 ; j < 2 && tri[j] != 0 ; ++j)
294
+ {
311
295
int * tv = tris[tri[j] - 1 ];
312
296
#define DM_DD_ARGS widths, depths, dd_factor
313
297
if (dm_is_depthdisc (DM_DD_ARGS, tv[0 ], tv[1 ])) tri[j] = 0 ;
@@ -317,7 +301,8 @@ depthmap_triangulate (FloatImage::ConstPtr dm, math::Matrix3f const& invproj,
317
301
}
318
302
319
303
/* Build triangles. */
320
- for (int j = 0 ; j < 2 ; ++j) {
304
+ for (int j = 0 ; j < 2 ; ++j)
305
+ {
321
306
if (tri[j] == 0 ) continue ;
322
307
#define DM_MAKE_TRI_ARGS mesh.get(), vidx, dm.get(), invproj, i
323
308
dm_make_triangle (DM_MAKE_TRI_ARGS, tris[tri[j] - 1 ]);
@@ -337,48 +322,42 @@ TriangleMesh::Ptr
337
322
depthmap_triangulate (FloatImage::ConstPtr dm, ByteImage::ConstPtr ci,
338
323
math::Matrix3f const & invproj, float dd_factor)
339
324
{
340
- // 深度图像不为空
341
325
if (dm == nullptr )
342
326
throw std::invalid_argument (" Null depthmap given" );
343
327
344
- // 图像的宽和高
345
328
int const width = dm->width ();
346
329
int const height = dm->height ();
347
330
348
- // 彩色图像不为空,且和深度图像宽和高一致
349
331
if (ci != nullptr && (ci->width () != width || ci->height () != height))
350
332
throw std::invalid_argument (" Color image dimension mismatch" );
351
333
352
334
/* Triangulate depth map. */
353
- // 对深度图进行三角化
354
335
core::Image<unsigned int > vids;
355
336
core::TriangleMesh::Ptr mesh;
356
337
mesh = core::geom::depthmap_triangulate (dm, invproj, dd_factor, &vids);
357
338
358
- // 获取颜色
359
339
if (ci == nullptr )
360
340
return mesh;
361
341
362
- /* 计算顶点的颜色*/
363
342
/* Use vertex index mapping to color the mesh. */
364
343
core::TriangleMesh::ColorList& colors (mesh->get_vertex_colors ());
365
344
core::TriangleMesh::VertexList const & verts (mesh->get_vertices ());
366
345
colors.resize (verts.size ());
367
346
368
- // 像素个数
369
347
int num_pixel = vids.get_pixel_amount ();
370
348
for (int i = 0 ; i < num_pixel; ++i)
371
349
{
372
- // 像素没有对应的顶点
373
350
if (vids[i] == MATH_MAX_UINT)
374
351
continue ;
375
352
376
353
math::Vec4f color (ci->at (i, 0 ), 0 .0f , 0 .0f , 255 .0f );
377
- if (ci->channels () >= 3 ) {
354
+ if (ci->channels () >= 3 )
355
+ {
378
356
color[1 ] = ci->at (i, 1 );
379
357
color[2 ] = ci->at (i, 2 );
380
358
}
381
- else {
359
+ else
360
+ {
382
361
color[1 ] = color[2 ] = color[0 ];
383
362
}
384
363
colors[vids[i]] = color / 255 .0f ;
@@ -393,29 +372,22 @@ TriangleMesh::Ptr
393
372
depthmap_triangulate (FloatImage::ConstPtr dm, ByteImage::ConstPtr ci,
394
373
CameraInfo const & cam, float dd_factor)
395
374
{
396
- // 确保深度图不为空
397
375
if (dm == nullptr )
398
376
throw std::invalid_argument (" Null depthmap given" );
399
-
400
- // 确保相机参数已经恢复
401
377
if (cam.flen == 0 .0f )
402
378
throw std::invalid_argument (" Invalid camera given" );
403
379
404
380
/* Triangulate depth map. */
405
- // 计算投影矩阵的逆矩阵
406
381
math::Matrix3f invproj;
407
382
cam.fill_inverse_calibration (*invproj, dm->width (), dm->height ());
408
-
409
- // 对深度图进行三角化,注意此时的mesh顶点坐标位于相机坐标系中
410
383
core::TriangleMesh::Ptr mesh;
411
384
mesh = core::geom::depthmap_triangulate (dm, ci, invproj, dd_factor);
412
385
413
386
/* Transform mesh to world coordinates. */
414
- // 将网格从相机坐标系转化到世界坐标系中
415
387
math::Matrix4f ctw;
416
388
cam.fill_cam_to_world (*ctw);
417
389
core::geom::mesh_transform (mesh, ctw);
418
- // mesh->recalc_normals(false, true); // Remove this?
390
+ mesh->recalc_normals (false , true ); // Remove this?
419
391
420
392
return mesh;
421
393
}
@@ -534,10 +506,12 @@ depthmap_mesh_confidences (TriangleMesh::Ptr mesh, int iterations)
534
506
535
507
/* Find boundary vertices and remember them. */
536
508
std::vector<std::size_t > vidx;
537
- MeshInfo mesh_info (mesh);
509
+ VertexInfoList vinfo (mesh);
538
510
539
- for (std::size_t i = 0 ; i < mesh_info.size (); ++i) {
540
- if (mesh_info[i].vclass == MeshInfo::VERTEX_CLASS_BORDER)
511
+ for (std::size_t i = 0 ; i < vinfo.size (); ++i)
512
+ {
513
+ MeshVertexInfo const & info (vinfo[i]);
514
+ if (info.vclass == VERTEX_CLASS_BORDER)
541
515
vidx.push_back (i);
542
516
}
543
517
@@ -557,7 +531,7 @@ depthmap_mesh_confidences (TriangleMesh::Ptr mesh, int iterations)
557
531
std::swap (vidx, cvidx);
558
532
for (std::size_t i = 0 ; i < cvidx.size (); ++i)
559
533
{
560
- MeshInfo::VertexInfo info = mesh_info [cvidx[i]];
534
+ MeshVertexInfo info = vinfo [cvidx[i]];
561
535
for (std::size_t j = 0 ; j < info.verts .size (); ++j)
562
536
if (confs[info.verts [j]] == 1 .0f )
563
537
vidx.push_back (info.verts [j]);
@@ -586,11 +560,11 @@ depthmap_mesh_peeling (TriangleMesh::Ptr mesh, int iterations)
586
560
/* Iteratively invalidate triangles at the boundary. */
587
561
for (int iter = 0 ; iter < iterations; ++iter)
588
562
{
589
- MeshInfo mesh_info ( mesh);
590
- for (std::size_t i = 0 ; i < mesh_info. size (); ++i)
563
+ VertexInfoList:: Ptr vinfo ( VertexInfoList::create ( mesh) );
564
+ for (std::size_t i = 0 ; i < vinfo-> size (); ++i)
591
565
{
592
- MeshInfo::VertexInfo const & info (mesh_info[i] );
593
- if (info.vclass == MeshInfo:: VERTEX_CLASS_BORDER)
566
+ MeshVertexInfo const & info (vinfo-> at (i) );
567
+ if (info.vclass == VERTEX_CLASS_BORDER)
594
568
for (std::size_t j = 0 ; j < info.faces .size (); ++j)
595
569
for (int k = 0 ; k < 3 ; ++k)
596
570
{
0 commit comments