Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
io.hh
1 // Copyright (C) 2008 EPITA Research and Development Laboratory (LRDE)
2 //
3 // This file is part of Olena.
4 //
5 // Olena is free software: you can redistribute it and/or modify it under
6 // the terms of the GNU General Public License as published by the Free
7 // Software Foundation, version 2 of the License.
8 //
9 // Olena is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Olena. If not, see <http://www.gnu.org/licenses/>.
16 //
17 // As a special exception, you may use this file as part of a free
18 // software project without restriction. Specifically, if other files
19 // instantiate templates or use macros or inline functions from this
20 // file, or you compile this file and link it with other files to produce
21 // an executable, this file does not by itself cause the resulting
22 // executable to be covered by the GNU General Public License. This
23 // exception does not however invalidate any other reasons why the
24 // executable file might be covered by the GNU General Public License.
25 
26 #ifndef APPS_MESH_SEGM_SKEL_IO_HH
27 # define APPS_MESH_SEGM_SKEL_IO_HH
28 
31 
32 #include <cstdio>
33 
34 #include <algorithm>
35 
36 #include <TriMesh.h>
37 
38 #include <mln/value/rgb8.hh>
39 
40 
43 // Convert colors float -> uchar
44 inline unsigned char color2uchar(float p)
45 {
46  return min(max(int(255.0f * p + 0.5f), 0), 255);
47 }
48 
49 // Write a bunch of vertices to an ASCII file
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,
54  bool float_color,
55  const char *before_conf,
56  const char *after_line)
57 {
58  for (unsigned i = 0; i < mesh->vertices.size(); i++) {
59  fprintf(f, "%s%.7g %.7g %.7g", before_vert,
60  mesh->vertices[i][0],
61  mesh->vertices[i][1],
62  mesh->vertices[i][2]);
63  if (!mesh->normals.empty() && before_norm)
64  fprintf(f, "%s%.7g %.7g %.7g", before_norm,
65  mesh->normals[i][0],
66  mesh->normals[i][1],
67  mesh->normals[i][2]);
68  if (!mesh->colors.empty() && before_color && float_color)
69  fprintf(f, "%s%.7g %.7g %.7g", before_color,
70  mesh->colors[i][0],
71  mesh->colors[i][1],
72  mesh->colors[i][2]);
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);
81  }
82 }
84 
85 
88 
89 /*----------------------.
90 | OFF with color data. |
91 `----------------------*/
92 
94 inline void write_faces_asc_colored(TriMesh *mesh,
95  const std::vector<mln::value::rgb8>& colors,
96  FILE *f,
97  const char *before_face,
98  const char *after_line)
99 {
100  mesh->need_faces();
101  for (unsigned i = 0; i < mesh->faces.size(); i++)
102  {
103  fprintf(f, "%s%d %d %d %d %d %d%s\n",
104  before_face,
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()),
109  after_line);
110  }
111 }
112 
114 inline void write_off_colored(TriMesh *mesh,
115  const std::vector<mln::value::rgb8>& colors,
116  FILE *f)
117 {
118  fprintf(f, "OFF\n");
119  mesh->need_faces();
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 ", "");
124 }
125 
126 /*----------------------.
127 | OFF with float data. |
128 `----------------------*/
129 
131 inline void write_faces_asc_float(TriMesh *mesh,
132  const std::vector<float>& values,
133  FILE *f,
134  const char *before_face,
135  const char *after_line)
136 {
137  mesh->need_faces();
138  for (unsigned i = 0; i < mesh->faces.size(); i++)
139  {
140  // Vertices Color
141  // -------- ------------
142  // V0 V1 V2 R G B A
143  fprintf(f, "%s%d %d %d %f %f %f 1.0%s\n",
144  before_face,
145  mesh->faces[i][0], mesh->faces[i][1], mesh->faces[i][2],
146  values[i], values[i], values[i],
147  after_line);
148  }
149 }
150 
152 inline void write_off_float(TriMesh *mesh, const std::vector<float>& values,
153  FILE *f)
154 {
155  fprintf(f, "OFF\n");
156  mesh->need_faces();
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 ", "");
161 }
163 
164 
165 /*---------------------------------------.
166 | OFF without data (``binary values''). |
167 `---------------------------------------*/
168 
173 inline void write_faces_asc_binary(TriMesh *mesh,
174  const std::vector<bool>& face_value,
175  FILE *f,
176  const char *before_face,
177  const char *after_line)
178 {
179  mesh->need_faces();
180  for (unsigned i = 0; i < mesh->faces.size(); i++)
181  if (face_value[i])
182  {
183  fprintf(f, "%s%d %d %d%s\n",
184  before_face,
185  mesh->faces[i][0], mesh->faces[i][1], mesh->faces[i][2],
186  after_line);
187  }
188 }
189 
191 inline void write_off_binary(TriMesh *mesh,
192  const std::vector<bool>& face_value,
193  FILE *f)
194 {
195  fprintf(f, "OFF\n");
196  mesh->need_faces();
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 ", "");
202 }
204 
205 #endif // ! APPS_MESH_SEGM_SKEL_IO_HH