00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef APPS_MESH_SEGM_SKEL_IO_HH
00029 # define APPS_MESH_SEGM_SKEL_IO_HH
00030
00033
00034 #include <cstdio>
00035
00036 #include <algorithm>
00037
00038 #include <TriMesh.h>
00039
00040 #include <mln/value/rgb8.hh>
00041
00042
00045
00046 inline unsigned char color2uchar(float p)
00047 {
00048 return min(max(int(255.0f * p + 0.5f), 0), 255);
00049 }
00050
00051
00052 inline void write_verts_asc(TriMesh *mesh, FILE *f,
00053 const char *before_vert,
00054 const char *before_norm,
00055 const char *before_color,
00056 bool float_color,
00057 const char *before_conf,
00058 const char *after_line)
00059 {
00060 for (unsigned i = 0; i < mesh->vertices.size(); i++) {
00061 fprintf(f, "%s%.7g %.7g %.7g", before_vert,
00062 mesh->vertices[i][0],
00063 mesh->vertices[i][1],
00064 mesh->vertices[i][2]);
00065 if (!mesh->normals.empty() && before_norm)
00066 fprintf(f, "%s%.7g %.7g %.7g", before_norm,
00067 mesh->normals[i][0],
00068 mesh->normals[i][1],
00069 mesh->normals[i][2]);
00070 if (!mesh->colors.empty() && before_color && float_color)
00071 fprintf(f, "%s%.7g %.7g %.7g", before_color,
00072 mesh->colors[i][0],
00073 mesh->colors[i][1],
00074 mesh->colors[i][2]);
00075 if (!mesh->colors.empty() && before_color && !float_color)
00076 fprintf(f, "%s%d %d %d", before_color,
00077 color2uchar(mesh->colors[i][0]),
00078 color2uchar(mesh->colors[i][1]),
00079 color2uchar(mesh->colors[i][2]));
00080 if (!mesh->confidences.empty() && before_conf)
00081 fprintf(f, "%s%.7g", before_conf, mesh->confidences[i]);
00082 fprintf(f, "%s\n", after_line);
00083 }
00084 }
00086
00087
00090
00091
00092
00093
00094
00096 inline void write_faces_asc_colored(TriMesh *mesh,
00097 const std::vector<mln::value::rgb8>& colors,
00098 FILE *f,
00099 const char *before_face,
00100 const char *after_line)
00101 {
00102 mesh->need_faces();
00103 for (unsigned i = 0; i < mesh->faces.size(); i++)
00104 {
00105 fprintf(f, "%s%d %d %d %d %d %d%s\n",
00106 before_face,
00107 mesh->faces[i][0], mesh->faces[i][1], mesh->faces[i][2],
00108 int(colors[i].red()),
00109 int(colors[i].green()),
00110 int(colors[i].blue()),
00111 after_line);
00112 }
00113 }
00114
00116 inline void write_off_colored(TriMesh *mesh,
00117 const std::vector<mln::value::rgb8>& colors,
00118 FILE *f)
00119 {
00120 fprintf(f, "OFF\n");
00121 mesh->need_faces();
00122 fprintf(f, "%lu %lu 0\n", (unsigned long) mesh->vertices.size(),
00123 (unsigned long) mesh->faces.size());
00124 write_verts_asc(mesh, f, "", 0, 0, false, 0, "");
00125 write_faces_asc_colored(mesh, colors, f, "3 ", "");
00126 }
00127
00128
00129
00130
00131
00133 inline void write_faces_asc_float(TriMesh *mesh,
00134 const std::vector<float>& values,
00135 FILE *f,
00136 const char *before_face,
00137 const char *after_line)
00138 {
00139 mesh->need_faces();
00140 for (unsigned i = 0; i < mesh->faces.size(); i++)
00141 {
00142
00143
00144
00145 fprintf(f, "%s%d %d %d %f %f %f 1.0%s\n",
00146 before_face,
00147 mesh->faces[i][0], mesh->faces[i][1], mesh->faces[i][2],
00148 values[i], values[i], values[i],
00149 after_line);
00150 }
00151 }
00152
00154 inline void write_off_float(TriMesh *mesh, const std::vector<float>& values,
00155 FILE *f)
00156 {
00157 fprintf(f, "OFF\n");
00158 mesh->need_faces();
00159 fprintf(f, "%lu %lu 0\n", (unsigned long) mesh->vertices.size(),
00160 (unsigned long) mesh->faces.size());
00161 write_verts_asc(mesh, f, "", 0, 0, false, 0, "");
00162 write_faces_asc_float(mesh, values, f, "3 ", "");
00163 }
00165
00166
00167
00168
00169
00170
00175 inline void write_faces_asc_binary(TriMesh *mesh,
00176 const std::vector<bool>& face_value,
00177 FILE *f,
00178 const char *before_face,
00179 const char *after_line)
00180 {
00181 mesh->need_faces();
00182 for (unsigned i = 0; i < mesh->faces.size(); i++)
00183 if (face_value[i])
00184 {
00185 fprintf(f, "%s%d %d %d%s\n",
00186 before_face,
00187 mesh->faces[i][0], mesh->faces[i][1], mesh->faces[i][2],
00188 after_line);
00189 }
00190 }
00191
00193 inline void write_off_binary(TriMesh *mesh,
00194 const std::vector<bool>& face_value,
00195 FILE *f)
00196 {
00197 fprintf(f, "OFF\n");
00198 mesh->need_faces();
00199 unsigned long nfaces =
00200 std::count(face_value.begin(), face_value.end(), true);
00201 fprintf(f, "%lu %lu 0\n", (unsigned long) mesh->vertices.size(), nfaces);
00202 write_verts_asc(mesh, f, "", 0, 0, false, 0, "");
00203 write_faces_asc_binary(mesh, face_value, f, "3 ", "");
00204 }
00206
00207 #endif // ! APPS_MESH_SEGM_SKEL_IO_HH