26 #ifndef APPS_MESH_SEGM_SKEL_IO_HH
27 # define APPS_MESH_SEGM_SKEL_IO_HH
38 #include <mln/value/rgb8.hh>
44 inline unsigned char color2uchar(
float p)
46 return min(
max(
int(255.0f * p + 0.5f), 0), 255);
50 inline void write_verts_asc(TriMesh *mesh, FILE *f,
51 const char *before_vert,
52 const char *before_norm,
53 const char *before_color,
55 const char *before_conf,
56 const char *after_line)
58 for (
unsigned i = 0; i < mesh->vertices.size(); i++) {
59 fprintf(f,
"%s%.7g %.7g %.7g", before_vert,
62 mesh->vertices[i][2]);
63 if (!mesh->normals.empty() && before_norm)
64 fprintf(f,
"%s%.7g %.7g %.7g", before_norm,
68 if (!mesh->colors.empty() && before_color && float_color)
69 fprintf(f,
"%s%.7g %.7g %.7g", before_color,
73 if (!mesh->colors.empty() && before_color && !float_color)
74 fprintf(f,
"%s%d %d %d", before_color,
75 color2uchar(mesh->colors[i][0]),
76 color2uchar(mesh->colors[i][1]),
77 color2uchar(mesh->colors[i][2]));
78 if (!mesh->confidences.empty() && before_conf)
79 fprintf(f,
"%s%.7g", before_conf, mesh->confidences[i]);
80 fprintf(f,
"%s\n", after_line);
94 inline void write_faces_asc_colored(TriMesh *mesh,
95 const std::vector<mln::value::rgb8>& colors,
97 const char *before_face,
98 const char *after_line)
101 for (
unsigned i = 0; i < mesh->faces.size(); i++)
103 fprintf(f,
"%s%d %d %d %d %d %d%s\n",
105 mesh->faces[i][0], mesh->faces[i][1], mesh->faces[i][2],
106 int(colors[i].red()),
107 int(colors[i].
green()),
108 int(colors[i].
blue()),
114 inline void write_off_colored(TriMesh *mesh,
115 const std::vector<mln::value::rgb8>& colors,
120 fprintf(f,
"%lu %lu 0\n", (
unsigned long) mesh->vertices.size(),
121 (
unsigned long) mesh->faces.size());
122 write_verts_asc(mesh, f,
"", 0, 0,
false, 0,
"");
123 write_faces_asc_colored(mesh, colors, f,
"3 ",
"");
131 inline void write_faces_asc_float(TriMesh *mesh,
132 const std::vector<float>& values,
134 const char *before_face,
135 const char *after_line)
138 for (
unsigned i = 0; i < mesh->faces.size(); i++)
143 fprintf(f,
"%s%d %d %d %f %f %f 1.0%s\n",
145 mesh->faces[i][0], mesh->faces[i][1], mesh->faces[i][2],
146 values[i], values[i], values[i],
152 inline void write_off_float(TriMesh *mesh,
const std::vector<float>& values,
157 fprintf(f,
"%lu %lu 0\n", (
unsigned long) mesh->vertices.size(),
158 (
unsigned long) mesh->faces.size());
159 write_verts_asc(mesh, f,
"", 0, 0,
false, 0,
"");
160 write_faces_asc_float(mesh, values, f,
"3 ",
"");
173 inline void write_faces_asc_binary(TriMesh *mesh,
174 const std::vector<bool>& face_value,
176 const char *before_face,
177 const char *after_line)
180 for (
unsigned i = 0; i < mesh->faces.size(); i++)
183 fprintf(f,
"%s%d %d %d%s\n",
185 mesh->faces[i][0], mesh->faces[i][1], mesh->faces[i][2],
191 inline void write_off_binary(TriMesh *mesh,
192 const std::vector<bool>& face_value,
197 unsigned long nfaces =
198 std::count(face_value.begin(), face_value.end(),
true);
199 fprintf(f,
"%lu %lu 0\n", (
unsigned long) mesh->vertices.size(), nfaces);
200 write_verts_asc(mesh, f,
"", 0, 0,
false, 0,
"");
201 write_faces_asc_binary(mesh, face_value, f,
"3 ",
"");
205 #endif // ! APPS_MESH_SEGM_SKEL_IO_HH