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

mesh-pinv-curv.cc

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 
00029 
00030 #include <cstdlib>
00031 #include <cmath>
00032 
00033 #include <algorithm>
00034 #include <vector>
00035 #include <iostream>
00036 #include <utility>
00037 
00038 #include <TriMesh.h>
00039 
00040 #include "io.hh"
00041 
00042 
00043 // Doesn't C++ have a better way to express Pi?
00044 static const float pi = 4 * atanf(1);
00045 
00046 
00047 int main(int argc, char* argv[])
00048 {
00049   if (argc != 3)
00050     {
00051       std::cerr << "usage: " << argv[0] << " input.off output.off"
00052                 << std::endl;
00053       std::exit(1);
00054     }
00055 
00056   std::string input_filename = argv[1];
00057   std::string output_filename = argv[2];
00058 
00059 
00060   // TriMesh is a pain: it systematically allocates on the heap.
00061   // Introduce another name to manipulate the mesh as a (non-pointer)
00062   // object.
00063   TriMesh* mesh_ptr = TriMesh::read(input_filename.c_str());
00064   if (!mesh_ptr)
00065     std::exit(2);
00066   TriMesh& mesh = *mesh_ptr;
00067 
00068   // Computes faces (triangles).
00069   mesh.need_faces();
00070   // Computation of the curvature on each vertex of the mesh.
00071   mesh.need_curvatures();
00072   std::vector<float> vertex_h_inv(mesh.vertices.size(), 0.f);
00073   for (unsigned v = 0; v < mesh.vertices.size(); ++v)
00074     {
00075       float h = (mesh.curv1[v] + mesh.curv2[v]) / 2;
00076       // Pseudo-inverse curvature.
00077       float h_inv = 1 / pi * (atan(-h) + pi / 2);
00078       vertex_h_inv[v] = h_inv;
00079     }
00080   // For each face of the mesh, computean an average curvature value
00081   // from the mean curvature at its vertices.
00082   std::vector<float> face_h_inv(mesh.faces.size(), 42.f);
00083   for (unsigned f = 0; f < mesh.faces.size(); ++f)
00084     {
00085       float h_inv = (vertex_h_inv[mesh.faces[f][0]] +
00086                      vertex_h_inv[mesh.faces[f][1]] +
00087                      vertex_h_inv[mesh.faces[f][2]]) / 3;
00088       mln_invariant(0.f <= h_inv);
00089       mln_invariant(h_inv <= 1.f);
00090       face_h_inv[f] = h_inv;
00091     }
00092 
00093   // Taken and adapted from TriMesh_io.cc
00094   FILE* f_out = fopen(output_filename.c_str(), "wb");
00095   if (!f_out)
00096     {
00097       std::cerr << "Error opening " << output_filename.c_str()
00098                 << " for writing." << std::endl;
00099       std::exit(2);
00100     }
00101   write_off_float(mesh_ptr, face_h_inv, f_out);
00102   fclose(f_out);
00103 
00104   delete mesh_ptr;
00105 }

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