14
14
15
15
#include " math/defines.h"
16
16
#include " math/matrix.h"
17
- #include " mve /mesh_info.h"
18
- #include " mve /depthmap.h"
19
- #include " mve /mesh_tools.h"
17
+ #include " core /mesh_info.h"
18
+ #include " core /depthmap.h"
19
+ #include " core /mesh_tools.h"
20
20
21
- MVE_NAMESPACE_BEGIN
22
- MVE_IMAGE_NAMESPACE_BEGIN
21
+ CORE_NAMESPACE_BEGIN
22
+ CORE_IMAGE_NAMESPACE_BEGIN
23
23
24
24
void
25
25
depthmap_cleanup_grow (FloatImage::ConstPtr dm, FloatImage::Ptr ret,
@@ -127,14 +127,15 @@ depthmap_confidence_clean (FloatImage::Ptr dm, FloatImage::ConstPtr cm)
127
127
dm->at (i, 0 ) = 0 .0f ;
128
128
}
129
129
130
- MVE_IMAGE_NAMESPACE_END
131
- MVE_NAMESPACE_END
130
+ CORE_IMAGE_NAMESPACE_END
131
+ CORE_NAMESPACE_END
132
132
133
133
/* ---------------------------------------------------------------- */
134
134
135
- MVE_NAMESPACE_BEGIN
136
- MVE_GEOM_NAMESPACE_BEGIN
135
+ CORE_NAMESPACE_BEGIN
136
+ CORE_GEOM_NAMESPACE_BEGIN
137
137
138
+ // todo foot print 是什么意思??
138
139
float
139
140
pixel_footprint (std::size_t x, std::size_t y, float depth,
140
141
math::Matrix3f const & invproj)
@@ -158,22 +159,20 @@ pixel_3dpos (std::size_t x, std::size_t y, float depth,
158
159
/* ---------------------------------------------------------------- */
159
160
160
161
void
161
- dm_make_triangle (TriangleMesh* mesh, mve ::Image<unsigned int >& vidx,
162
+ dm_make_triangle (TriangleMesh* mesh, core ::Image<unsigned int >& vidx,
162
163
FloatImage const * dm, math::Matrix3f const & invproj,
163
164
std::size_t i, int * tverts)
164
165
{
165
166
int const width = vidx.width ();
166
167
// int const height = vidx.height();
167
- mve ::TriangleMesh::VertexList& verts (mesh->get_vertices ());
168
- mve ::TriangleMesh::FaceList& faces (mesh->get_faces ());
168
+ core ::TriangleMesh::VertexList& verts (mesh->get_vertices ());
169
+ core ::TriangleMesh::FaceList& faces (mesh->get_faces ());
169
170
170
- for (int j = 0 ; j < 3 ; ++j)
171
- {
171
+ for (int j = 0 ; j < 3 ; ++j) {
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)
176
- {
175
+ if (vidx.at (iidx) == MATH_MAX_UINT) {
177
176
/* Add vertex for depth pixel. */
178
177
vidx.at (iidx) = verts.size ();
179
178
float depth = dm->at (iidx, 0 );
@@ -209,61 +208,81 @@ dm_is_depthdisc (float* widths, float* depths, float dd_factor, int i1, int i2)
209
208
210
209
TriangleMesh::Ptr
211
210
depthmap_triangulate (FloatImage::ConstPtr dm, math::Matrix3f const & invproj,
212
- float dd_factor, mve ::Image<unsigned int >* vids)
211
+ float dd_factor, core ::Image<unsigned int >* vids)
213
212
{
213
+ // 深度图不为空
214
214
if (dm == nullptr )
215
215
throw std::invalid_argument (" Null depthmap given" );
216
216
217
+ // 图像的宽和高
217
218
int const width = dm->width ();
218
219
int const height = dm->height ();
219
220
221
+ // 创建三角网格接结构体
220
222
/* Prepare triangle mesh. */
221
223
TriangleMesh::Ptr mesh (TriangleMesh::create ());
222
224
223
225
/* Generate image that maps image pixels to vertex IDs. */
224
- mve::Image<unsigned int > vidx (width, height, 1 );
226
+ // 创建映射图,将图像像素映射到三维点的索引
227
+ core::Image<unsigned int > vidx (width, height, 1 );
225
228
vidx.fill (MATH_MAX_UINT);
226
229
230
+ // 在深度图中遍历2x2 blocks,并且创建三角面片
227
231
/* Iterate over 2x2-blocks in the depthmap and create triangles. */
228
232
int i = 0 ;
229
- for (int y = 0 ; y < height - 1 ; ++y, ++i)
230
- {
231
- for (int x = 0 ; x < width - 1 ; ++x, ++i)
232
- {
233
+ for (int y = 0 ; y < height - 1 ; ++y, ++i) {
234
+ for (int x = 0 ; x < width - 1 ; ++x, ++i) {
235
+
233
236
/* Cache the four depth values. */
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 ) };
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
+ };
236
244
237
245
/* 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
+ */
238
250
int mask = 0 ;
239
251
int pixels = 0 ;
240
- for (int j = 0 ; j < 4 ; ++j)
241
- if (depths[j] > 0 .0f )
242
- {
252
+ for (int j = 0 ; j < 4 ; ++j){
253
+ if (depths[j] > 0 .0f ) {
243
254
mask |= 1 << j;
244
255
pixels += 1 ;
245
256
}
257
+ }
246
258
259
+ // 至少保证3个深度值是可靠的
247
260
/* At least three valid depth values are required. */
248
261
if (pixels < 3 )
249
262
continue ;
250
263
264
+
251
265
/* Possible triangles, vertex indices relative to 2x2 block. */
266
+ /* 可能出现的三角面片对,4个点有2个面片
267
+ */
252
268
int tris[4 ][3 ] = {
253
269
{ 0 , 2 , 1 }, { 0 , 3 , 1 }, { 0 , 2 , 3 }, { 1 , 2 , 3 }
254
270
};
255
271
256
272
/* Decide which triangles to issue. */
273
+ /* 决定用哪对面片
274
+ */
257
275
int tri[2 ] = { 0 , 0 };
258
276
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 :
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
266
284
{
285
+ // 空圆特性
267
286
/* Choose the triangulation with smaller diagonal. */
268
287
float ddiff1 = std::abs (depths[0 ] - depths[3 ]);
269
288
float ddiff2 = std::abs (depths[1 ] - depths[2 ]);
@@ -277,21 +296,18 @@ depthmap_triangulate (FloatImage::ConstPtr dm, math::Matrix3f const& invproj,
277
296
}
278
297
279
298
/* Omit depth discontinuity detection if dd_factor is zero. */
280
- if (dd_factor > 0 .0f )
281
- {
299
+ if (dd_factor > 0 .0f ) {
282
300
/* Cache pixel footprints. */
283
301
float widths[4 ];
284
- for (int j = 0 ; j < 4 ; ++j)
285
- {
302
+ for (int j = 0 ; j < 4 ; ++j) {
286
303
if (depths[j] == 0 .0f )
287
304
continue ;
288
- widths[j] = pixel_footprint (x + (j % 2 ), y + (j / 2 ),
289
- depths[j], invproj);// w, h, focal_len);
305
+ widths[j] = pixel_footprint (x + (j % 2 ), y + (j / 2 ), depths[j], invproj);// w, h, focal_len);
290
306
}
291
307
308
+ // 检查深度不一致性,相邻像素的深度差值不要超过像素宽度(三维空间中)的dd_factor倍
292
309
/* Check for depth discontinuities. */
293
- for (int j = 0 ; j < 2 && tri[j] != 0 ; ++j)
294
- {
310
+ for (int j = 0 ; j < 2 && tri[j] != 0 ; ++j) {
295
311
int * tv = tris[tri[j] - 1 ];
296
312
#define DM_DD_ARGS widths, depths, dd_factor
297
313
if (dm_is_depthdisc (DM_DD_ARGS, tv[0 ], tv[1 ])) tri[j] = 0 ;
@@ -301,8 +317,7 @@ depthmap_triangulate (FloatImage::ConstPtr dm, math::Matrix3f const& invproj,
301
317
}
302
318
303
319
/* Build triangles. */
304
- for (int j = 0 ; j < 2 ; ++j)
305
- {
320
+ for (int j = 0 ; j < 2 ; ++j) {
306
321
if (tri[j] == 0 ) continue ;
307
322
#define DM_MAKE_TRI_ARGS mesh.get(), vidx, dm.get(), invproj, i
308
323
dm_make_triangle (DM_MAKE_TRI_ARGS, tris[tri[j] - 1 ]);
@@ -322,42 +337,48 @@ TriangleMesh::Ptr
322
337
depthmap_triangulate (FloatImage::ConstPtr dm, ByteImage::ConstPtr ci,
323
338
math::Matrix3f const & invproj, float dd_factor)
324
339
{
340
+ // 深度图像不为空
325
341
if (dm == nullptr )
326
342
throw std::invalid_argument (" Null depthmap given" );
327
343
344
+ // 图像的宽和高
328
345
int const width = dm->width ();
329
346
int const height = dm->height ();
330
347
348
+ // 彩色图像不为空,且和深度图像宽和高一致
331
349
if (ci != nullptr && (ci->width () != width || ci->height () != height))
332
350
throw std::invalid_argument (" Color image dimension mismatch" );
333
351
334
352
/* Triangulate depth map. */
335
- mve::Image<unsigned int > vids;
336
- mve::TriangleMesh::Ptr mesh;
337
- mesh = mve::geom::depthmap_triangulate (dm, invproj, dd_factor, &vids);
353
+ // 对深度图进行三角化
354
+ core::Image<unsigned int > vids;
355
+ core::TriangleMesh::Ptr mesh;
356
+ mesh = core::geom::depthmap_triangulate (dm, invproj, dd_factor, &vids);
338
357
358
+ // 获取颜色
339
359
if (ci == nullptr )
340
360
return mesh;
341
361
362
+ /* 计算顶点的颜色*/
342
363
/* Use vertex index mapping to color the mesh. */
343
- mve ::TriangleMesh::ColorList& colors (mesh->get_vertex_colors ());
344
- mve ::TriangleMesh::VertexList const & verts (mesh->get_vertices ());
364
+ core ::TriangleMesh::ColorList& colors (mesh->get_vertex_colors ());
365
+ core ::TriangleMesh::VertexList const & verts (mesh->get_vertices ());
345
366
colors.resize (verts.size ());
346
367
368
+ // 像素个数
347
369
int num_pixel = vids.get_pixel_amount ();
348
370
for (int i = 0 ; i < num_pixel; ++i)
349
371
{
372
+ // 像素没有对应的顶点
350
373
if (vids[i] == MATH_MAX_UINT)
351
374
continue ;
352
375
353
376
math::Vec4f color (ci->at (i, 0 ), 0 .0f , 0 .0f , 255 .0f );
354
- if (ci->channels () >= 3 )
355
- {
377
+ if (ci->channels () >= 3 ) {
356
378
color[1 ] = ci->at (i, 1 );
357
379
color[2 ] = ci->at (i, 2 );
358
380
}
359
- else
360
- {
381
+ else {
361
382
color[1 ] = color[2 ] = color[0 ];
362
383
}
363
384
colors[vids[i]] = color / 255 .0f ;
@@ -372,22 +393,29 @@ TriangleMesh::Ptr
372
393
depthmap_triangulate (FloatImage::ConstPtr dm, ByteImage::ConstPtr ci,
373
394
CameraInfo const & cam, float dd_factor)
374
395
{
396
+ // 确保深度图不为空
375
397
if (dm == nullptr )
376
398
throw std::invalid_argument (" Null depthmap given" );
399
+
400
+ // 确保相机参数已经恢复
377
401
if (cam.flen == 0 .0f )
378
402
throw std::invalid_argument (" Invalid camera given" );
379
403
380
404
/* Triangulate depth map. */
405
+ // 计算投影矩阵的逆矩阵
381
406
math::Matrix3f invproj;
382
407
cam.fill_inverse_calibration (*invproj, dm->width (), dm->height ());
383
- mve::TriangleMesh::Ptr mesh;
384
- mesh = mve::geom::depthmap_triangulate (dm, ci, invproj, dd_factor);
408
+
409
+ // 对深度图进行三角化,注意此时的mesh顶点坐标位于相机坐标系中
410
+ core::TriangleMesh::Ptr mesh;
411
+ mesh = core::geom::depthmap_triangulate (dm, ci, invproj, dd_factor);
385
412
386
413
/* Transform mesh to world coordinates. */
414
+ // 将网格从相机坐标系转化到世界坐标系中
387
415
math::Matrix4f ctw;
388
416
cam.fill_cam_to_world (*ctw);
389
- mve ::geom::mesh_transform (mesh, ctw);
390
- mesh->recalc_normals (false , true ); // Remove this?
417
+ core ::geom::mesh_transform (mesh, ctw);
418
+ // mesh->recalc_normals(false, true); // Remove this?
391
419
392
420
return mesh;
393
421
}
@@ -508,8 +536,7 @@ depthmap_mesh_confidences (TriangleMesh::Ptr mesh, int iterations)
508
536
std::vector<std::size_t > vidx;
509
537
MeshInfo mesh_info (mesh);
510
538
511
- for (std::size_t i = 0 ; i < mesh_info.size (); ++i)
512
- {
539
+ for (std::size_t i = 0 ; i < mesh_info.size (); ++i) {
513
540
if (mesh_info[i].vclass == MeshInfo::VERTEX_CLASS_BORDER)
514
541
vidx.push_back (i);
515
542
}
@@ -578,5 +605,5 @@ depthmap_mesh_peeling (TriangleMesh::Ptr mesh, int iterations)
578
605
math::algo::vector_clean (delete_list, &faces);
579
606
}
580
607
581
- MVE_GEOM_NAMESPACE_END
582
- MVE_NAMESPACE_END
608
+ CORE_GEOM_NAMESPACE_END
609
+ CORE_NAMESPACE_END
0 commit comments