• 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 the Milena Library.  This library is free
00004 // software; you can redistribute it and/or modify it under the terms
00005 // of the GNU General Public License version 2 as published by the
00006 // Free Software Foundation.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU General Public License
00014 // along with this library; see the file COPYING.  If not, write to
00015 // the Free Software Foundation, 51 Franklin Street, Fifth Floor,
00016 // Boston, MA 02111-1307, USA.
00017 //
00018 // As a special exception, you may use this file as part of a free
00019 // software library without restriction.  Specifically, if other files
00020 // instantiate templates or use macros or inline functions from this
00021 // file, or you compile this file and link it with other files to
00022 // produce an executable, this file does not by itself cause the
00023 // resulting executable to be covered by the GNU General Public
00024 // License.  This exception does not however invalidate any other
00025 // reasons why the executable file might be covered by the GNU General
00026 // Public License.
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 // Convert colors float -> uchar
00046 inline unsigned char color2uchar(float p)
00047 {
00048         return min(max(int(255.0f * p + 0.5f), 0), 255);
00049 }
00050 
00051 // Write a bunch of vertices to an ASCII file
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 | OFF with color data.  |
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 | OFF with float data.  |
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       //            Vertices    Color
00143       //            -------- ------------
00144       //            V0 V1 V2  R  G  B  A
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 | OFF without data (``binary values'').  |
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

Generated on Fri Oct 19 2012 04:15:56 for Milena (Olena) by  doxygen 1.7.1