2015-04-28 11:54:17 +00:00
|
|
|
/* ========================================================================= *
|
2009-06-04 08:46:29 +00:00
|
|
|
* *
|
|
|
|
|
* OpenMesh *
|
2015-04-28 11:33:32 +00:00
|
|
|
* Copyright (c) 2001-2015, RWTH-Aachen University *
|
2015-04-28 13:07:46 +00:00
|
|
|
* Department of Computer Graphics and Multimedia *
|
2015-04-28 11:33:32 +00:00
|
|
|
* All rights reserved. *
|
|
|
|
|
* www.openmesh.org *
|
|
|
|
|
* *
|
|
|
|
|
*---------------------------------------------------------------------------*
|
|
|
|
|
* This file is part of OpenMesh. *
|
|
|
|
|
*---------------------------------------------------------------------------*
|
|
|
|
|
* *
|
|
|
|
|
* Redistribution and use in source and binary forms, with or without *
|
|
|
|
|
* modification, are permitted provided that the following conditions *
|
|
|
|
|
* are met: *
|
|
|
|
|
* *
|
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, *
|
|
|
|
|
* this list of conditions and the following disclaimer. *
|
|
|
|
|
* *
|
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright *
|
|
|
|
|
* notice, this list of conditions and the following disclaimer in the *
|
|
|
|
|
* documentation and/or other materials provided with the distribution. *
|
|
|
|
|
* *
|
|
|
|
|
* 3. Neither the name of the copyright holder nor the names of its *
|
|
|
|
|
* contributors may be used to endorse or promote products derived from *
|
|
|
|
|
* this software without specific prior written permission. *
|
|
|
|
|
* *
|
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
|
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
|
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
|
|
|
|
|
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
|
|
|
|
|
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
|
|
|
|
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
|
|
|
|
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
|
|
|
|
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
|
|
|
|
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
|
|
|
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
|
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
|
2015-04-28 11:54:17 +00:00
|
|
|
* *
|
|
|
|
|
* ========================================================================= */
|
2009-06-04 08:46:29 +00:00
|
|
|
|
2019-01-15 11:21:12 +01:00
|
|
|
|
2009-02-06 13:37:46 +00:00
|
|
|
|
|
|
|
|
#if !defined(OM_USE_OSG)
|
|
|
|
|
# define OM_USE_OSG 0
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <fstream>
|
|
|
|
|
#include <sstream>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <memory>
|
|
|
|
|
#include <map>
|
|
|
|
|
//--------------------
|
|
|
|
|
#include <OpenMesh/Core/IO/MeshIO.hh>
|
|
|
|
|
//--------------------
|
|
|
|
|
#if OM_USE_OSG
|
|
|
|
|
# include <OpenMesh/Tools/Kernel_OSG/TriMesh_OSGArrayKernelT.hh>
|
|
|
|
|
#else
|
|
|
|
|
# include <OpenMesh/Core/Mesh/TriMesh_ArrayKernelT.hh>
|
|
|
|
|
#endif
|
|
|
|
|
#include <OpenMesh/Core/Utils/vector_cast.hh>
|
|
|
|
|
//--------------------
|
|
|
|
|
#include <OpenMesh/Tools/Utils/getopt.h>
|
|
|
|
|
#include <OpenMesh/Tools/Utils/Timer.hh>
|
|
|
|
|
#include <OpenMesh/Tools/Decimater/DecimaterT.hh>
|
2011-11-16 09:45:08 +00:00
|
|
|
#include <OpenMesh/Tools/Decimater/ModAspectRatioT.hh>
|
|
|
|
|
#include <OpenMesh/Tools/Decimater/ModEdgeLengthT.hh>
|
|
|
|
|
#include <OpenMesh/Tools/Decimater/ModHausdorffT.hh>
|
|
|
|
|
#include <OpenMesh/Tools/Decimater/ModNormalDeviationT.hh>
|
2009-02-06 13:37:46 +00:00
|
|
|
#include <OpenMesh/Tools/Decimater/ModNormalFlippingT.hh>
|
|
|
|
|
#include <OpenMesh/Tools/Decimater/ModQuadricT.hh>
|
|
|
|
|
#include <OpenMesh/Tools/Decimater/ModProgMeshT.hh>
|
|
|
|
|
#include <OpenMesh/Tools/Decimater/ModIndependentSetsT.hh>
|
|
|
|
|
#include <OpenMesh/Tools/Decimater/ModRoundnessT.hh>
|
|
|
|
|
|
|
|
|
|
//----------------------------------------------------------------- traits ----
|
|
|
|
|
|
|
|
|
|
#if OM_USE_OSG
|
|
|
|
|
typedef OpenMesh::Kernel_OSG::Traits MyTraits;
|
|
|
|
|
#else
|
|
|
|
|
typedef OpenMesh::DefaultTraits MyTraits;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
//------------------------------------------------------------------- mesh ----
|
|
|
|
|
|
|
|
|
|
#if OM_USE_OSG
|
|
|
|
|
typedef OpenMesh::Kernel_OSG::TriMesh_OSGArrayKernelT<MyTraits> ArrayTriMesh;
|
|
|
|
|
#else
|
|
|
|
|
typedef OpenMesh::TriMesh_ArrayKernelT<MyTraits> ArrayTriMesh;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//-------------------------------------------------------------- decimator ----
|
|
|
|
|
|
|
|
|
|
typedef OpenMesh::Decimater::DecimaterT<ArrayTriMesh> Decimater;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------- globals ----
|
|
|
|
|
|
|
|
|
|
int gverbose = 0;
|
|
|
|
|
int gdebug = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------------------- forwards ----
|
|
|
|
|
|
|
|
|
|
void usage_and_exit(int xcode);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------- decimater arguments ----
|
|
|
|
|
|
|
|
|
|
#include "CmdOption.hh"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct DecOptions
|
|
|
|
|
{
|
|
|
|
|
DecOptions()
|
2011-11-16 09:45:08 +00:00
|
|
|
: n_collapses(0)
|
2009-02-06 13:37:46 +00:00
|
|
|
{ }
|
|
|
|
|
|
|
|
|
|
CmdOption<bool> decorate_name;
|
|
|
|
|
CmdOption<float> n_collapses;
|
2011-11-16 09:45:08 +00:00
|
|
|
|
|
|
|
|
CmdOption<float> AR; // Aspect ratio
|
|
|
|
|
CmdOption<float> EL; // Edge length
|
|
|
|
|
CmdOption<float> HD; // Hausdorff distance
|
2009-02-06 13:37:46 +00:00
|
|
|
CmdOption<bool> IS; // Independent Sets
|
2011-11-16 09:45:08 +00:00
|
|
|
CmdOption<float> ND; // Normal deviation
|
|
|
|
|
CmdOption<float> NF; // Normal flipping
|
2009-02-06 13:37:46 +00:00
|
|
|
CmdOption<std::string> PM; // Progressive Mesh
|
2011-11-16 09:45:08 +00:00
|
|
|
CmdOption<float> Q; // Quadrics
|
2009-02-06 13:37:46 +00:00
|
|
|
CmdOption<float> R; // Roundness
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
|
bool init( CmdOption<T>& _o, const std::string& _val )
|
|
|
|
|
{
|
|
|
|
|
if ( _val.empty() )
|
|
|
|
|
_o.enable();
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
std::istringstream istr( _val );
|
|
|
|
|
|
|
|
|
|
T v;
|
|
|
|
|
|
|
|
|
|
if ( (istr >> v).fail() )
|
2011-11-16 09:45:08 +00:00
|
|
|
return false;
|
|
|
|
|
|
2009-02-06 13:37:46 +00:00
|
|
|
_o = v;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool parse_argument( const std::string& arg )
|
|
|
|
|
{
|
|
|
|
|
std::string::size_type pos = arg.find(':');
|
|
|
|
|
|
|
|
|
|
std::string name;
|
|
|
|
|
std::string value;
|
|
|
|
|
|
|
|
|
|
if (pos == std::string::npos)
|
|
|
|
|
name = arg;
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
name = arg.substr(0, pos);
|
|
|
|
|
value = arg.substr(pos+1, arg.size());
|
|
|
|
|
}
|
|
|
|
|
strip(name);
|
|
|
|
|
strip(value);
|
2011-11-16 09:45:08 +00:00
|
|
|
|
|
|
|
|
if (name == "AR") return init(AR, value);
|
|
|
|
|
if (name == "EL") return init(EL, value);
|
|
|
|
|
if (name == "HD") return init(HD, value);
|
|
|
|
|
if (name == "IS") return init(IS, value);
|
|
|
|
|
if (name == "ND") return init(ND, value);
|
2009-02-06 13:37:46 +00:00
|
|
|
if (name == "NF") return init(NF, value);
|
|
|
|
|
if (name == "PM") return init(PM, value);
|
2011-11-16 09:45:08 +00:00
|
|
|
if (name == "Q") return init(Q, value);
|
2009-02-06 13:37:46 +00:00
|
|
|
if (name == "R") return init(R, value);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string& strip(std::string & line)
|
|
|
|
|
{
|
|
|
|
|
std::string::size_type pos = 0;
|
2011-11-16 09:45:08 +00:00
|
|
|
|
2009-02-06 13:37:46 +00:00
|
|
|
pos = line.find_last_not_of(" \t");
|
2011-11-16 09:45:08 +00:00
|
|
|
|
2009-02-06 13:37:46 +00:00
|
|
|
if ( pos!=0 && pos!=std::string::npos )
|
|
|
|
|
{
|
|
|
|
|
++pos;
|
|
|
|
|
line.erase( pos, line.length()-pos );
|
|
|
|
|
}
|
2011-11-16 09:45:08 +00:00
|
|
|
|
2009-02-06 13:37:46 +00:00
|
|
|
pos = line.find_first_not_of(" \t");
|
|
|
|
|
if ( pos!=0 && pos!=std::string::npos )
|
|
|
|
|
{
|
|
|
|
|
line.erase(0,pos);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return line;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//----------------------------------------------------- decimater wrapper ----
|
|
|
|
|
//
|
2012-01-13 09:46:55 +00:00
|
|
|
template <typename Mesh, typename DecimaterType>
|
2009-02-06 13:37:46 +00:00
|
|
|
bool
|
|
|
|
|
decimate(const std::string &_ifname,
|
|
|
|
|
const std::string &_ofname,
|
2011-11-16 09:45:08 +00:00
|
|
|
DecOptions &_opt)
|
2009-02-06 13:37:46 +00:00
|
|
|
{
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
|
|
Mesh mesh;
|
2019-05-29 08:32:28 +02:00
|
|
|
OpenMesh::IO::Options readopt;
|
2009-02-06 13:37:46 +00:00
|
|
|
OpenMesh::Utils::Timer timer;
|
|
|
|
|
|
|
|
|
|
// ---------------------------------------- read source mesh
|
|
|
|
|
{
|
|
|
|
|
if (gverbose)
|
|
|
|
|
clog << "source mesh: ";
|
|
|
|
|
bool rc;
|
2011-11-16 09:45:08 +00:00
|
|
|
|
2009-02-06 13:37:46 +00:00
|
|
|
if (gverbose)
|
|
|
|
|
clog << _ifname << endl;
|
2019-05-29 08:32:28 +02:00
|
|
|
if ( !(rc = OpenMesh::IO::read_mesh(mesh, _ifname, readopt)) )
|
2009-02-06 13:37:46 +00:00
|
|
|
{
|
|
|
|
|
cerr << " ERROR: read failed!" << endl;
|
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-11-16 09:45:08 +00:00
|
|
|
|
2009-02-06 13:37:46 +00:00
|
|
|
// ---------------------------------------- do some decimation
|
|
|
|
|
{
|
|
|
|
|
// ---- 0 - For module NormalFlipping one needs face normals
|
|
|
|
|
|
2019-05-29 08:32:28 +02:00
|
|
|
if ( !readopt.check( OpenMesh::IO::Options::FaceNormal ) )
|
2009-02-06 13:37:46 +00:00
|
|
|
{
|
|
|
|
|
if ( !mesh.has_face_normals() )
|
2011-11-16 09:45:08 +00:00
|
|
|
mesh.request_face_normals();
|
2009-02-06 13:37:46 +00:00
|
|
|
|
|
|
|
|
if (gverbose)
|
2011-11-16 09:45:08 +00:00
|
|
|
clog << " updating face normals" << endl;
|
2009-02-06 13:37:46 +00:00
|
|
|
mesh.update_face_normals();
|
|
|
|
|
}
|
2011-11-16 09:45:08 +00:00
|
|
|
|
2009-02-06 13:37:46 +00:00
|
|
|
// ---- 1 - create decimater instance
|
2012-01-13 09:46:55 +00:00
|
|
|
DecimaterType decimater( mesh );
|
2011-11-16 09:45:08 +00:00
|
|
|
|
|
|
|
|
// ---- 2 - register modules
|
2009-02-06 13:37:46 +00:00
|
|
|
if (gverbose)
|
2011-11-16 09:45:08 +00:00
|
|
|
clog << " register modules" << endl;
|
2009-02-06 13:37:46 +00:00
|
|
|
|
2011-11-16 09:45:08 +00:00
|
|
|
|
|
|
|
|
|
2012-08-08 14:04:23 +00:00
|
|
|
typename OpenMesh::Decimater::ModAspectRatioT<Mesh>::Handle modAR;
|
2011-11-16 09:45:08 +00:00
|
|
|
|
|
|
|
|
if (_opt.AR.is_enabled())
|
2009-02-06 13:37:46 +00:00
|
|
|
{
|
2011-11-16 09:45:08 +00:00
|
|
|
decimater.add(modAR);
|
|
|
|
|
if (_opt.AR.has_value())
|
|
|
|
|
decimater.module( modAR ).set_aspect_ratio( _opt.AR ) ;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-08 14:04:23 +00:00
|
|
|
typename OpenMesh::Decimater::ModEdgeLengthT<Mesh>::Handle modEL;
|
2011-11-16 09:45:08 +00:00
|
|
|
|
|
|
|
|
if (_opt.EL.is_enabled())
|
|
|
|
|
{
|
|
|
|
|
decimater.add(modEL);
|
|
|
|
|
if (_opt.EL.has_value())
|
|
|
|
|
decimater.module( modEL ).set_edge_length( _opt.EL ) ;
|
2012-08-15 09:52:01 +00:00
|
|
|
decimater.module(modEL).set_binary(false);
|
2011-11-16 09:45:08 +00:00
|
|
|
}
|
|
|
|
|
|
2012-08-08 14:04:23 +00:00
|
|
|
typename OpenMesh::Decimater::ModHausdorffT <Mesh>::Handle modHD;
|
2011-11-16 09:45:08 +00:00
|
|
|
|
|
|
|
|
if (_opt.HD.is_enabled())
|
|
|
|
|
{
|
|
|
|
|
decimater.add(modHD);
|
|
|
|
|
if (_opt.HD.has_value())
|
|
|
|
|
decimater.module( modHD ).set_tolerance( _opt.HD ) ;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-08 14:04:23 +00:00
|
|
|
typename OpenMesh::Decimater::ModIndependentSetsT<Mesh>::Handle modIS;
|
2011-11-16 09:45:08 +00:00
|
|
|
|
|
|
|
|
if ( _opt.IS.is_enabled() )
|
|
|
|
|
decimater.add(modIS);
|
|
|
|
|
|
2012-08-08 14:04:23 +00:00
|
|
|
typename OpenMesh::Decimater::ModNormalDeviationT<Mesh>::Handle modND;
|
2011-11-16 09:45:08 +00:00
|
|
|
|
|
|
|
|
if (_opt.ND.is_enabled())
|
|
|
|
|
{
|
|
|
|
|
decimater.add(modND);
|
|
|
|
|
if (_opt.ND.has_value())
|
|
|
|
|
decimater.module( modND ).set_normal_deviation( _opt.ND );
|
2013-06-03 09:00:42 +00:00
|
|
|
decimater.module( modND ).set_binary(false);
|
2009-02-06 13:37:46 +00:00
|
|
|
}
|
|
|
|
|
|
2012-08-08 14:04:23 +00:00
|
|
|
typename OpenMesh::Decimater::ModNormalFlippingT<Mesh>::Handle modNF;
|
2011-11-16 09:45:08 +00:00
|
|
|
|
2009-02-06 13:37:46 +00:00
|
|
|
if (_opt.NF.is_enabled())
|
|
|
|
|
{
|
|
|
|
|
decimater.add(modNF);
|
|
|
|
|
if (_opt.NF.has_value())
|
2011-11-16 09:45:08 +00:00
|
|
|
decimater.module( modNF ).set_max_normal_deviation( _opt.NF );
|
2009-02-06 13:37:46 +00:00
|
|
|
}
|
|
|
|
|
|
2011-11-16 09:45:08 +00:00
|
|
|
|
2012-08-08 14:04:23 +00:00
|
|
|
typename OpenMesh::Decimater::ModProgMeshT<Mesh>::Handle modPM;
|
2009-02-06 13:37:46 +00:00
|
|
|
|
|
|
|
|
if ( _opt.PM.is_enabled() )
|
|
|
|
|
decimater.add(modPM);
|
|
|
|
|
|
2012-08-08 14:04:23 +00:00
|
|
|
typename OpenMesh::Decimater::ModQuadricT<Mesh>::Handle modQ;
|
2009-02-06 13:37:46 +00:00
|
|
|
|
2011-11-16 09:45:08 +00:00
|
|
|
if (_opt.Q.is_enabled())
|
|
|
|
|
{
|
|
|
|
|
decimater.add(modQ);
|
|
|
|
|
if (_opt.Q.has_value())
|
|
|
|
|
decimater.module( modQ ).set_max_err( _opt.Q );
|
2012-08-15 09:52:01 +00:00
|
|
|
decimater.module(modQ).set_binary(false);
|
2011-11-16 09:45:08 +00:00
|
|
|
}
|
2009-02-06 13:37:46 +00:00
|
|
|
|
2012-08-08 14:04:23 +00:00
|
|
|
typename OpenMesh::Decimater::ModRoundnessT<Mesh>::Handle modR;
|
2009-02-06 13:37:46 +00:00
|
|
|
|
|
|
|
|
if ( _opt.R.is_enabled() )
|
|
|
|
|
{
|
|
|
|
|
decimater.add( modR );
|
|
|
|
|
if ( _opt.R.has_value() )
|
2011-11-16 09:45:08 +00:00
|
|
|
decimater.module( modR ).set_min_angle( _opt.R,
|
|
|
|
|
!modQ.is_valid() ||
|
|
|
|
|
!decimater.module(modQ).is_binary());
|
2009-02-06 13:37:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ---- 3 - initialize decimater
|
|
|
|
|
|
|
|
|
|
if (gverbose)
|
|
|
|
|
clog << "initializing mesh" << endl;
|
2011-11-16 09:45:08 +00:00
|
|
|
|
2009-02-06 13:37:46 +00:00
|
|
|
{
|
|
|
|
|
bool rc;
|
|
|
|
|
timer.start();
|
|
|
|
|
rc = decimater.initialize();
|
|
|
|
|
timer.stop();
|
|
|
|
|
if (!rc)
|
|
|
|
|
{
|
2011-11-16 09:45:08 +00:00
|
|
|
std::cerr << " initializing failed!" << std::endl;
|
2012-08-15 09:52:01 +00:00
|
|
|
std::cerr << " maybe no priority module or more than one were defined!" << std::endl;
|
2011-11-16 09:45:08 +00:00
|
|
|
return false;
|
2009-02-06 13:37:46 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (gverbose)
|
|
|
|
|
std::clog << " Elapsed time: " << timer.as_string() << std::endl;
|
|
|
|
|
|
|
|
|
|
if (gverbose)
|
|
|
|
|
decimater.info( clog );
|
|
|
|
|
|
|
|
|
|
// ---- 4 - do it
|
|
|
|
|
|
|
|
|
|
if (gverbose)
|
|
|
|
|
{
|
|
|
|
|
std::clog << "decimating" << std::endl;
|
|
|
|
|
std::clog << " # vertices: " << mesh.n_vertices() << std::endl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float nv_before = float(mesh.n_vertices());
|
|
|
|
|
|
|
|
|
|
timer.start();
|
2013-08-14 16:33:26 +00:00
|
|
|
size_t rc = 0;
|
2009-02-06 13:37:46 +00:00
|
|
|
if (_opt.n_collapses < 0.0)
|
|
|
|
|
rc = decimater.decimate_to( size_t(-_opt.n_collapses) );
|
|
|
|
|
else if (_opt.n_collapses >= 1.0 || _opt.n_collapses == 0.0)
|
|
|
|
|
rc = decimater.decimate( size_t(_opt.n_collapses) );
|
|
|
|
|
else if (_opt.n_collapses > 0.0f)
|
|
|
|
|
rc = decimater.decimate_to(size_t(mesh.n_vertices()*_opt.n_collapses));
|
|
|
|
|
timer.stop();
|
2011-11-16 09:45:08 +00:00
|
|
|
|
2009-02-06 13:37:46 +00:00
|
|
|
// ---- 5 - write progmesh file for progviewer (before garbage collection!)
|
2011-11-16 09:45:08 +00:00
|
|
|
|
2009-02-06 13:37:46 +00:00
|
|
|
if ( _opt.PM.has_value() )
|
|
|
|
|
decimater.module(modPM).write( _opt.PM );
|
2011-11-16 09:45:08 +00:00
|
|
|
|
2009-02-06 13:37:46 +00:00
|
|
|
// ---- 6 - throw away all tagged edges
|
|
|
|
|
|
|
|
|
|
mesh.garbage_collection();
|
|
|
|
|
|
|
|
|
|
if (gverbose)
|
|
|
|
|
{
|
|
|
|
|
std::clog << " # executed collapses: " << rc << std::endl;
|
|
|
|
|
std::clog << " # vertices: " << mesh.n_vertices() << ", "
|
2011-11-16 09:45:08 +00:00
|
|
|
<< ( 100.0*mesh.n_vertices()/nv_before ) << "%\n";
|
2009-02-06 13:37:46 +00:00
|
|
|
std::clog << " Elapsed time: " << timer.as_string() << std::endl;
|
|
|
|
|
std::clog << " collapses/s : " << rc/timer.seconds() << std::endl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2011-11-16 09:45:08 +00:00
|
|
|
|
2009-02-06 13:37:46 +00:00
|
|
|
// write resulting mesh
|
|
|
|
|
if ( ! _ofname.empty() )
|
|
|
|
|
{
|
|
|
|
|
std::string ofname(_ofname);
|
|
|
|
|
|
|
|
|
|
std::string::size_type pos = ofname.rfind('.');
|
|
|
|
|
if (pos == std::string::npos)
|
|
|
|
|
{
|
|
|
|
|
ofname += ".off";
|
|
|
|
|
pos = ofname.rfind('.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( _opt.decorate_name.is_enabled() )
|
|
|
|
|
{
|
|
|
|
|
std::stringstream s; s << mesh.n_vertices();
|
|
|
|
|
std::string n; s >> n;
|
|
|
|
|
ofname.insert( pos, "-");
|
|
|
|
|
ofname.insert(++pos, n );
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-29 08:32:28 +02:00
|
|
|
OpenMesh::IO::Options writeopt;
|
2009-02-06 13:37:46 +00:00
|
|
|
|
|
|
|
|
//opt += OpenMesh::IO::Options::Binary;
|
|
|
|
|
|
2019-05-29 08:32:28 +02:00
|
|
|
if ( !OpenMesh::IO::write_mesh(mesh, ofname, writeopt ) )
|
2009-02-06 13:37:46 +00:00
|
|
|
{
|
|
|
|
|
std::cerr << " Cannot write decimated mesh to file '"
|
2011-11-16 09:45:08 +00:00
|
|
|
<< ofname << "'\n";
|
2009-02-06 13:37:46 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
std::clog << " Exported decimated mesh to file '" << ofname << "'\n";
|
|
|
|
|
}
|
2011-11-16 09:45:08 +00:00
|
|
|
|
2009-02-06 13:37:46 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//------------------------------------------------------------------ main -----
|
|
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
|
{
|
|
|
|
|
std::string ifname, ofname;
|
|
|
|
|
|
|
|
|
|
DecOptions opt;
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
#if OM_USE_OSG
|
|
|
|
|
osg::osgInit( argc, argv );
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
//---------------------------------------- parse command line
|
|
|
|
|
{
|
|
|
|
|
int c;
|
2011-11-16 09:45:08 +00:00
|
|
|
|
2009-02-06 13:37:46 +00:00
|
|
|
while ( (c=getopt( argc, argv, "dDhi:M:n:o:v")) != -1 )
|
|
|
|
|
{
|
|
|
|
|
switch (c)
|
|
|
|
|
{
|
|
|
|
|
case 'D': opt.decorate_name = true; break;
|
2011-11-16 09:45:08 +00:00
|
|
|
case 'd': gdebug = true; break;
|
2019-01-15 11:07:39 +01:00
|
|
|
case 'h': usage_and_exit(0); break;
|
2011-11-16 09:45:08 +00:00
|
|
|
case 'i': ifname = optarg; break;
|
|
|
|
|
case 'M': opt.parse_argument( optarg ); break;
|
|
|
|
|
case 'n': opt.n_collapses = float(atof(optarg)); break;
|
|
|
|
|
case 'o': ofname = optarg; break;
|
|
|
|
|
case 'v': gverbose = true; break;
|
|
|
|
|
case '?':
|
|
|
|
|
default:
|
|
|
|
|
std::cerr << "FATAL: cannot process command line option!"
|
|
|
|
|
<< std::endl;
|
|
|
|
|
exit(-1);
|
2009-02-06 13:37:46 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//----------------------------------------
|
|
|
|
|
|
|
|
|
|
if ( (-1.0f < opt.n_collapses) && (opt.n_collapses < 0.0f) )
|
|
|
|
|
{
|
|
|
|
|
std::cerr << "Error: Option -n: invalid value argument!" << std::endl;
|
|
|
|
|
usage_and_exit(2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//----------------------------------------
|
|
|
|
|
|
|
|
|
|
if (gverbose)
|
|
|
|
|
{
|
|
|
|
|
std::clog << " Input file: " << ifname << std::endl;
|
|
|
|
|
std::clog << " Output file: " << ofname << std::endl;
|
|
|
|
|
std::clog << " #collapses: " << opt.n_collapses << std::endl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//----------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (gverbose)
|
|
|
|
|
{
|
|
|
|
|
std::clog << "Begin decimation" << std::endl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool rc = decimate<ArrayTriMesh, Decimater>( ifname, ofname, opt );
|
|
|
|
|
|
|
|
|
|
if (gverbose)
|
|
|
|
|
{
|
|
|
|
|
if (!rc)
|
|
|
|
|
std::clog << "Decimation failed!" << std::endl;
|
|
|
|
|
else
|
|
|
|
|
std::clog << "Decimation done." << std::endl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//----------------------------------------
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
void usage_and_exit(int xcode)
|
|
|
|
|
{
|
|
|
|
|
std::string errmsg;
|
2011-11-16 09:45:08 +00:00
|
|
|
|
2009-02-06 13:37:46 +00:00
|
|
|
switch(xcode)
|
|
|
|
|
{
|
|
|
|
|
case 1: errmsg = "Option not supported!"; break;
|
|
|
|
|
case 2: errmsg = "Invalid output file format!"; break;
|
|
|
|
|
}
|
2011-11-16 09:45:08 +00:00
|
|
|
|
2009-02-06 13:37:46 +00:00
|
|
|
std::cerr << std::endl;
|
2011-11-16 09:45:08 +00:00
|
|
|
if (xcode) {
|
2009-02-06 13:37:46 +00:00
|
|
|
std::cerr << "Error " << xcode << ": " << errmsg << std::endl << std::endl;
|
|
|
|
|
}
|
|
|
|
|
std::cerr << "Usage: decimator [Options] -i input-file -o output-file\n"
|
2011-11-16 09:45:08 +00:00
|
|
|
<< " Decimating a mesh using quadrics and normal flipping.\n" << std::endl;
|
|
|
|
|
std::cerr << "Options\n" << std::endl;
|
2009-02-06 13:37:46 +00:00
|
|
|
std::cerr << " -M \"{Module-Name}[:Value]}\"\n"
|
2013-05-30 15:11:38 +00:00
|
|
|
<< " Use named module with eventually given parameterization\n"
|
|
|
|
|
<< " Several modules can also be used in order to introduce further constraints\n"
|
|
|
|
|
<< " Note that -M has to be given before each new module \n"
|
|
|
|
|
<< " An example with ModQuadric as a priority module\n"
|
|
|
|
|
<< " and ModRoundness as a binary module could look like this:\n"
|
|
|
|
|
<< " commandlineDecimater -M Q -M R:40.0 -n 0.1 -i inputfile.obj -o outputfile.obj\n" << std::endl;
|
2009-02-06 13:37:46 +00:00
|
|
|
std::cerr << " -n <N>\n"
|
2011-11-16 09:45:08 +00:00
|
|
|
<< " N >= 1: do N halfedge collapses.\n"
|
|
|
|
|
<< " N <=-1: decimate down to |N| vertices.\n"
|
|
|
|
|
<< " 0 < N < 1: decimate down to N%.\n" << std::endl;
|
2009-02-06 13:37:46 +00:00
|
|
|
std::cerr << std::endl;
|
|
|
|
|
std::cerr << "Modules:\n\n";
|
2011-11-16 09:45:08 +00:00
|
|
|
std::cerr << " AR[:ratio] - ModAspectRatio\n";
|
2012-08-15 09:52:01 +00:00
|
|
|
std::cerr << " EL[:legth] - ModEdgeLength*\n";
|
2011-11-16 09:45:08 +00:00
|
|
|
std::cerr << " HD[:distance] - ModHausdorff\n";
|
2009-02-06 13:37:46 +00:00
|
|
|
std::cerr << " IS - ModIndependentSets\n";
|
2013-06-03 09:00:42 +00:00
|
|
|
std::cerr << " ND[:angle] - ModNormalDeviation*\n";
|
2009-02-06 13:37:46 +00:00
|
|
|
std::cerr << " NF[:angle] - ModNormalFlipping\n";
|
|
|
|
|
std::cerr << " PM[:file name] - ModProgMesh\n";
|
2012-08-15 09:52:01 +00:00
|
|
|
std::cerr << " Q[:error] - ModQuadric*\n";
|
2009-02-06 13:37:46 +00:00
|
|
|
std::cerr << " R[:angle] - ModRoundness\n";
|
|
|
|
|
std::cerr << " 0 < angle < 60\n";
|
2012-08-15 09:52:01 +00:00
|
|
|
std::cerr << " *: priority module. Decimater needs one of them (not more).\n";
|
2011-11-16 09:45:08 +00:00
|
|
|
|
2009-02-06 13:37:46 +00:00
|
|
|
exit( xcode );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// end of file
|
|
|
|
|
//=============================================================================
|