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 #ifndef APPS_MESH_SEGM_SKEL_IO_HH
00027 # define APPS_MESH_SEGM_SKEL_IO_HH
00028 
00031 
00032 #include <cstdio>
00033 
00034 #include <algorithm>
00035 
00036 #include <TriMesh.h>
00037 
00038 #include <mln/value/rgb8.hh>
00039 
00040 
00043 
00044 inline unsigned char color2uchar(float p)
00045 {
00046         return min(max(int(255.0f * p + 0.5f), 0), 255);
00047 }
00048 
00049 
00050 inline void write_verts_asc(TriMesh *mesh, FILE *f,
00051                             const char *before_vert,
00052                             const char *before_norm,
00053                             const char *before_color,
00054                             bool float_color,
00055                             const char *before_conf,
00056                             const char *after_line)
00057 {
00058     for (unsigned i = 0; i < mesh->vertices.size(); i++) {
00059                 fprintf(f, "%s%.7g %.7g %.7g", before_vert,
00060                                 mesh->vertices[i][0],
00061                                 mesh->vertices[i][1],
00062                                 mesh->vertices[i][2]);
00063                 if (!mesh->normals.empty() && before_norm)
00064                         fprintf(f, "%s%.7g %.7g %.7g", before_norm,
00065                                 mesh->normals[i][0],
00066                                 mesh->normals[i][1],
00067                                 mesh->normals[i][2]);
00068                 if (!mesh->colors.empty() && before_color && float_color)
00069                         fprintf(f, "%s%.7g %.7g %.7g", before_color,
00070                                 mesh->colors[i][0],
00071                                 mesh->colors[i][1],
00072                                 mesh->colors[i][2]);
00073                 if (!mesh->colors.empty() && before_color && !float_color)
00074                         fprintf(f, "%s%d %d %d", before_color,
00075                                 color2uchar(mesh->colors[i][0]),
00076                                 color2uchar(mesh->colors[i][1]),
00077                                 color2uchar(mesh->colors[i][2]));
00078                 if (!mesh->confidences.empty() && before_conf)
00079                         fprintf(f, "%s%.7g", before_conf, mesh->confidences[i]);
00080                 fprintf(f, "%s\n", after_line);
00081         }
00082 }
00084 
00085 
00088 
00089 
00090 
00091 
00092 
00094 inline void write_faces_asc_colored(TriMesh *mesh,
00095                                     const std::vector<mln::value::rgb8>& colors,
00096                                     FILE *f,
00097                                     const char *before_face,
00098                                     const char *after_line)
00099 {
00100   mesh->need_faces();
00101   for (unsigned i = 0; i < mesh->faces.size(); i++)
00102     {
00103       fprintf(f, "%s%d %d %d %d %d %d%s\n",
00104               before_face,
00105               mesh->faces[i][0], mesh->faces[i][1], mesh->faces[i][2],
00106               int(colors[i].red()),
00107               int(colors[i].green()),
00108               int(colors[i].blue()),
00109               after_line);
00110     }
00111 }
00112 
00114 inline void write_off_colored(TriMesh *mesh,
00115                               const std::vector<mln::value::rgb8>& colors,
00116                               FILE *f)
00117 {
00118   fprintf(f, "OFF\n");
00119   mesh->need_faces();
00120   fprintf(f, "%lu %lu 0\n", (unsigned long) mesh->vertices.size(),
00121           (unsigned long) mesh->faces.size());
00122   write_verts_asc(mesh, f, "", 0, 0, false, 0, "");
00123   write_faces_asc_colored(mesh, colors, f, "3 ", "");
00124 }
00125 
00126 
00127 
00128 
00129 
00131 inline void write_faces_asc_float(TriMesh *mesh,
00132                                   const std::vector<float>& values,
00133                                   FILE *f,
00134                                   const char *before_face,
00135                                   const char *after_line)
00136 {
00137   mesh->need_faces();
00138   for (unsigned i = 0; i < mesh->faces.size(); i++)
00139     {
00140       
00141       
00142       
00143       fprintf(f, "%s%d %d %d %f %f %f 1.0%s\n",
00144               before_face,
00145               mesh->faces[i][0], mesh->faces[i][1], mesh->faces[i][2],
00146               values[i], values[i], values[i],
00147               after_line);
00148     }
00149 }
00150 
00152 inline void write_off_float(TriMesh *mesh, const std::vector<float>& values,
00153                             FILE *f)
00154 {
00155   fprintf(f, "OFF\n");
00156   mesh->need_faces();
00157   fprintf(f, "%lu %lu 0\n", (unsigned long) mesh->vertices.size(),
00158           (unsigned long) mesh->faces.size());
00159   write_verts_asc(mesh, f, "", 0, 0, false, 0, "");
00160   write_faces_asc_float(mesh, values, f, "3 ", "");
00161 }
00163 
00164 
00165 
00166 
00167 
00168 
00173 inline void write_faces_asc_binary(TriMesh *mesh,
00174                                    const std::vector<bool>& face_value,
00175                                    FILE *f,
00176                                    const char *before_face,
00177                                    const char *after_line)
00178 {
00179   mesh->need_faces();
00180   for (unsigned i = 0; i < mesh->faces.size(); i++)
00181     if (face_value[i])
00182     {
00183       fprintf(f, "%s%d %d %d%s\n",
00184               before_face,
00185               mesh->faces[i][0], mesh->faces[i][1], mesh->faces[i][2],
00186               after_line);
00187     }
00188 }
00189 
00191 inline void write_off_binary(TriMesh *mesh,
00192                              const std::vector<bool>& face_value,
00193                              FILE *f)
00194 {
00195   fprintf(f, "OFF\n");
00196   mesh->need_faces();
00197   unsigned long nfaces =
00198     std::count(face_value.begin(), face_value.end(), true);
00199   fprintf(f, "%lu %lu 0\n", (unsigned long) mesh->vertices.size(), nfaces);
00200   write_verts_asc(mesh, f, "", 0, 0, false, 0, "");
00201   write_faces_asc_binary(mesh, face_value, f, "3 ", "");
00202 }
00204 
00205 #endif // ! APPS_MESH_SEGM_SKEL_IO_HH