• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List

io.hh

00001 // Copyright (C) 2008 EPITA Research and Development Laboratory (LRDE)
00002 //
00003 // This file is part of Olena.
00004 //
00005 // Olena is free software: you can redistribute it and/or modify it under
00006 // the terms of the GNU General Public License as published by the Free
00007 // Software Foundation, version 2 of the License.
00008 //
00009 // Olena is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 // General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with Olena.  If not, see <http://www.gnu.org/licenses/>.
00016 //
00017 // As a special exception, you may use this file as part of a free
00018 // software project without restriction.  Specifically, if other files
00019 // instantiate templates or use macros or inline functions from this
00020 // file, or you compile this file and link it with other files to produce
00021 // an executable, this file does not by itself cause the resulting
00022 // executable to be covered by the GNU General Public License.  This
00023 // exception does not however invalidate any other reasons why the
00024 // executable file might be covered by the GNU General Public License.
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 // Convert colors float -> uchar
00044 inline unsigned char color2uchar(float p)
00045 {
00046         return min(max(int(255.0f * p + 0.5f), 0), 255);
00047 }
00048 
00049 // Write a bunch of vertices to an ASCII file
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 | OFF with color data.  |
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 | OFF with float data.  |
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       //            Vertices    Color
00141       //            -------- ------------
00142       //            V0 V1 V2  R  G  B  A
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 | OFF without data (``binary values'').  |
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

Generated on Tue Oct 4 2011 15:24:00 for Milena (Olena) by  doxygen 1.7.1