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 *
|
2009-06-04 08:46:29 +00:00
|
|
|
|
* *
|
2012-09-19 16:15:39 +00:00
|
|
|
|
*---------------------------------------------------------------------------*
|
2015-04-28 11:33:32 +00:00
|
|
|
|
* This file is part of OpenMesh. *
|
|
|
|
|
|
*---------------------------------------------------------------------------*
|
2009-06-04 08:46:29 +00:00
|
|
|
|
* *
|
2015-04-28 11:33:32 +00:00
|
|
|
|
* 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
|
|
|
|
|
|
|
|
|
|
/** \file ModRoundnessT.hh
|
2011-11-16 09:45:08 +00:00
|
|
|
|
|
2009-02-06 13:37:46 +00:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
|
//
|
|
|
|
|
|
// CLASS ModRoundnessT
|
|
|
|
|
|
//
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
|
|
2011-11-16 09:45:08 +00:00
|
|
|
|
#ifndef OPENMESH_DECIMATER_MODROUNDNESST_HH
|
|
|
|
|
|
#define OPENMESH_DECIMATER_MODROUNDNESST_HH
|
2009-02-06 13:37:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//== INCLUDES =================================================================
|
|
|
|
|
|
|
|
|
|
|
|
#include <OpenMesh/Tools/Decimater/ModBaseT.hh>
|
|
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
|
|
|
|
#if defined(OM_CC_MSVC)
|
|
|
|
|
|
# define OM_ENABLE_WARNINGS 4244
|
|
|
|
|
|
# pragma warning(disable : OM_ENABLE_WARNINGS )
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
//== NAMESPACE ================================================================
|
|
|
|
|
|
|
|
|
|
|
|
namespace OpenMesh { // BEGIN_NS_OPENMESH
|
|
|
|
|
|
namespace Decimater { // BEGIN_NS_DECIMATER
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//== CLASS DEFINITION =========================================================
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-11-16 09:45:08 +00:00
|
|
|
|
/** \brief Use Roundness of triangles to control decimation
|
|
|
|
|
|
*
|
|
|
|
|
|
*
|
|
|
|
|
|
* In binary and mode, the collapse is legal if:
|
|
|
|
|
|
* - The roundness after the collapse is greater than the given value
|
|
|
|
|
|
*
|
|
|
|
|
|
* In continuous mode the roundness after the collapse is returned
|
|
|
|
|
|
*/
|
2012-08-08 15:15:17 +00:00
|
|
|
|
template <class MeshT>
|
|
|
|
|
|
class ModRoundnessT : public ModBaseT<MeshT>
|
2009-02-06 13:37:46 +00:00
|
|
|
|
{
|
2011-11-16 09:45:08 +00:00
|
|
|
|
public:
|
2012-08-08 15:15:17 +00:00
|
|
|
|
DECIMATING_MODULE( ModRoundnessT, MeshT, Roundness );
|
2009-02-06 13:37:46 +00:00
|
|
|
|
|
2011-11-16 09:45:08 +00:00
|
|
|
|
public:
|
2009-02-06 13:37:46 +00:00
|
|
|
|
|
|
|
|
|
|
// typedefs
|
2013-08-20 15:45:10 +00:00
|
|
|
|
typedef typename MeshT::Point Point;
|
2009-02-06 13:37:46 +00:00
|
|
|
|
typedef typename vector_traits<Point>::value_type value_type;
|
|
|
|
|
|
|
2011-11-16 09:45:08 +00:00
|
|
|
|
public:
|
|
|
|
|
|
|
2009-02-06 13:37:46 +00:00
|
|
|
|
/// Constructor
|
2012-08-08 15:15:17 +00:00
|
|
|
|
ModRoundnessT( MeshT &_dec ) :
|
2012-09-19 16:15:39 +00:00
|
|
|
|
Base(_dec, false),
|
2009-02-06 13:37:46 +00:00
|
|
|
|
min_r_(-1.0)
|
|
|
|
|
|
{ }
|
2011-11-16 09:45:08 +00:00
|
|
|
|
|
2009-02-06 13:37:46 +00:00
|
|
|
|
/// Destructor
|
|
|
|
|
|
~ModRoundnessT() { }
|
|
|
|
|
|
|
2011-11-16 09:45:08 +00:00
|
|
|
|
public: // inherited
|
|
|
|
|
|
|
2009-02-06 13:37:46 +00:00
|
|
|
|
/** Compute collapse priority due to roundness of triangle.
|
|
|
|
|
|
*
|
|
|
|
|
|
* The roundness is computed by dividing the radius of the
|
|
|
|
|
|
* circumference by the length of the shortest edge. The result is
|
2012-09-19 16:15:39 +00:00
|
|
|
|
* normalized.
|
2009-02-06 13:37:46 +00:00
|
|
|
|
*
|
|
|
|
|
|
* \return [0:1] or ILLEGAL_COLLAPSE in non-binary mode
|
|
|
|
|
|
* \return LEGAL_COLLAPSE or ILLEGAL_COLLAPSE in binary mode
|
|
|
|
|
|
* \see set_min_roundness()
|
|
|
|
|
|
*/
|
2012-09-19 16:15:39 +00:00
|
|
|
|
float collapse_priority(const CollapseInfo& _ci)
|
|
|
|
|
|
{
|
2011-11-16 09:45:08 +00:00
|
|
|
|
// using namespace OpenMesh;
|
2009-02-06 13:37:46 +00:00
|
|
|
|
|
|
|
|
|
|
typename Mesh::ConstVertexOHalfedgeIter voh_it(Base::mesh(), _ci.v0);
|
|
|
|
|
|
double r;
|
|
|
|
|
|
double priority = 0.0; //==LEGAL_COLLAPSE
|
|
|
|
|
|
typename Mesh::FaceHandle fhC, fhB;
|
|
|
|
|
|
Vec3f B,C;
|
2011-11-16 09:45:08 +00:00
|
|
|
|
|
2013-08-15 08:43:04 +00:00
|
|
|
|
if ( min_r_ < 0.0f ) // continues mode
|
2012-09-19 16:15:39 +00:00
|
|
|
|
{
|
2013-08-07 12:24:09 +00:00
|
|
|
|
C = vector_cast<Vec3f>(Base::mesh().point( Base::mesh().to_vertex_handle(*voh_it)));
|
2013-08-07 09:40:10 +00:00
|
|
|
|
fhC = Base::mesh().face_handle( *voh_it );
|
2009-02-06 13:37:46 +00:00
|
|
|
|
|
2013-08-07 11:18:44 +00:00
|
|
|
|
for (++voh_it; voh_it.is_valid(); ++voh_it)
|
2009-02-06 13:37:46 +00:00
|
|
|
|
{
|
2011-11-16 09:45:08 +00:00
|
|
|
|
B = C;
|
|
|
|
|
|
fhB = fhC;
|
2013-08-07 12:24:09 +00:00
|
|
|
|
C = vector_cast<Vec3f>(Base::mesh().point(Base::mesh().to_vertex_handle(*voh_it)));
|
2013-08-07 09:40:10 +00:00
|
|
|
|
fhC = Base::mesh().face_handle( *voh_it );
|
2011-11-16 09:45:08 +00:00
|
|
|
|
|
|
|
|
|
|
if ( fhB == _ci.fl || fhB == _ci.fr )
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
// simulate collapse using position of v1
|
|
|
|
|
|
r = roundness( vector_cast<Vec3f>(_ci.p1), B, C );
|
|
|
|
|
|
|
|
|
|
|
|
// return the maximum non-roundness
|
|
|
|
|
|
priority = std::max( priority, (1.0-r) );
|
2009-02-06 13:37:46 +00:00
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else // binary mode
|
|
|
|
|
|
{
|
2013-08-07 12:24:09 +00:00
|
|
|
|
C = vector_cast<Vec3f>(Base::mesh().point( Base::mesh().to_vertex_handle(*voh_it)));
|
2013-08-07 09:40:10 +00:00
|
|
|
|
fhC = Base::mesh().face_handle( *voh_it );
|
2009-02-06 13:37:46 +00:00
|
|
|
|
|
2013-08-07 11:18:44 +00:00
|
|
|
|
for (++voh_it; voh_it.is_valid() && (priority==Base::LEGAL_COLLAPSE); ++voh_it)
|
2009-02-06 13:37:46 +00:00
|
|
|
|
{
|
2011-11-16 09:45:08 +00:00
|
|
|
|
B = C;
|
|
|
|
|
|
fhB = fhC;
|
2013-08-07 12:24:09 +00:00
|
|
|
|
C = vector_cast<Vec3f>(Base::mesh().point(Base::mesh().to_vertex_handle(*voh_it)));
|
2013-08-07 09:40:10 +00:00
|
|
|
|
fhC = Base::mesh().face_handle( *voh_it );
|
2009-02-06 13:37:46 +00:00
|
|
|
|
|
2011-11-16 09:45:08 +00:00
|
|
|
|
if ( fhB == _ci.fl || fhB == _ci.fr )
|
|
|
|
|
|
continue;
|
2009-02-06 13:37:46 +00:00
|
|
|
|
|
2011-11-16 09:45:08 +00:00
|
|
|
|
priority = ( (r=roundness( vector_cast<Vec3f>(_ci.p1), B, C )) < min_r_)
|
|
|
|
|
|
? Base::ILLEGAL_COLLAPSE : Base::LEGAL_COLLAPSE;
|
2009-02-06 13:37:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return (float) priority;
|
|
|
|
|
|
}
|
2011-11-16 09:45:08 +00:00
|
|
|
|
|
2012-09-19 16:15:39 +00:00
|
|
|
|
/// set the percentage of minimum roundness
|
|
|
|
|
|
void set_error_tolerance_factor(double _factor) {
|
|
|
|
|
|
if (this->is_binary()) {
|
|
|
|
|
|
if (_factor >= 0.0 && _factor <= 1.0) {
|
|
|
|
|
|
// the smaller the factor, the smaller min_r_ gets
|
|
|
|
|
|
// thus creating a stricter constraint
|
|
|
|
|
|
// division by error_tolerance_factor_ is for normalization
|
2016-10-28 10:44:04 +02:00
|
|
|
|
value_type min_roundness = min_r_ * static_cast<value_type>(_factor / this->error_tolerance_factor_);
|
2012-09-19 16:15:39 +00:00
|
|
|
|
set_min_roundness(min_roundness);
|
|
|
|
|
|
this->error_tolerance_factor_ = _factor;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2011-11-16 09:45:08 +00:00
|
|
|
|
|
2009-02-06 13:37:46 +00:00
|
|
|
|
|
|
|
|
|
|
public: // specific methods
|
|
|
|
|
|
|
|
|
|
|
|
void set_min_angle( float _angle, bool /* _binary=true */ )
|
|
|
|
|
|
{
|
|
|
|
|
|
assert( _angle > 0 && _angle < 60 );
|
|
|
|
|
|
|
|
|
|
|
|
_angle = float(M_PI * _angle /180.0);
|
|
|
|
|
|
|
|
|
|
|
|
Vec3f A,B,C;
|
|
|
|
|
|
|
2013-08-15 08:43:04 +00:00
|
|
|
|
A = Vec3f( 0.0f, 0.0f, 0.0f);
|
|
|
|
|
|
B = Vec3f( 2.0f * cos(_angle), 0.0f, 0.0f);
|
|
|
|
|
|
C = Vec3f( cos(_angle), sin(_angle), 0.0f);
|
2009-02-06 13:37:46 +00:00
|
|
|
|
|
|
|
|
|
|
double r1 = roundness(A,B,C);
|
|
|
|
|
|
|
|
|
|
|
|
_angle = float(0.5 * ( M_PI - _angle ));
|
|
|
|
|
|
|
2013-08-15 08:43:04 +00:00
|
|
|
|
A = Vec3f( 0.0f, 0.0f, 0.0f);
|
|
|
|
|
|
B = Vec3f( 2.0f*cos(_angle), 0.0f, 0.0f);
|
|
|
|
|
|
C = Vec3f( cos(_angle), sin(_angle), 0.0f);
|
2009-02-06 13:37:46 +00:00
|
|
|
|
|
|
|
|
|
|
double r2 = roundness(A,B,C);
|
|
|
|
|
|
|
2012-09-19 16:15:39 +00:00
|
|
|
|
set_min_roundness( value_type(std::min(r1,r2)), true );
|
2009-02-06 13:37:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Set a minimum roundness value.
|
|
|
|
|
|
* \param _min_roundness in range (0,1)
|
2012-09-19 16:15:39 +00:00
|
|
|
|
* \param _binary Set true, if the binary mode should be enabled,
|
2009-02-06 13:37:46 +00:00
|
|
|
|
* else false. In latter case the collapse_priority()
|
2011-11-16 09:45:08 +00:00
|
|
|
|
* returns a float value if the constraint does not apply
|
2009-02-06 13:37:46 +00:00
|
|
|
|
* and ILLEGAL_COLLAPSE else.
|
|
|
|
|
|
*/
|
|
|
|
|
|
void set_min_roundness( value_type _min_roundness, bool _binary=true )
|
|
|
|
|
|
{
|
|
|
|
|
|
assert( 0.0 <= _min_roundness && _min_roundness <= 1.0 );
|
|
|
|
|
|
min_r_ = _min_roundness;
|
|
|
|
|
|
Base::set_binary(_binary);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Unset minimum value constraint and enable non-binary mode.
|
|
|
|
|
|
void unset_min_roundness()
|
|
|
|
|
|
{
|
|
|
|
|
|
min_r_ = -1.0;
|
|
|
|
|
|
Base::set_binary(false);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Compute a normalized roundness of a triangle ABC
|
|
|
|
|
|
//
|
|
|
|
|
|
// Having
|
|
|
|
|
|
// A,B,C corner points of triangle
|
|
|
|
|
|
// a,b,c the vectors BC,CA,AB
|
|
|
|
|
|
// Area area of triangle
|
|
|
|
|
|
//
|
|
|
|
|
|
// then define
|
|
|
|
|
|
//
|
2012-09-19 16:15:39 +00:00
|
|
|
|
// radius of circumference
|
2009-02-06 13:37:46 +00:00
|
|
|
|
// R := -----------------------
|
|
|
|
|
|
// length of shortest edge
|
|
|
|
|
|
//
|
2012-09-19 16:15:39 +00:00
|
|
|
|
// ||a|| * ||b|| * ||c||
|
2009-02-06 13:37:46 +00:00
|
|
|
|
// ---------------------
|
|
|
|
|
|
// 4 * Area ||a|| * ||b|| * ||c||
|
|
|
|
|
|
// = ----------------------- = -----------------------------------
|
|
|
|
|
|
// min( ||a||,||b||,||c||) 4 * Area * min( ||a||,||b||,||c|| )
|
|
|
|
|
|
//
|
|
|
|
|
|
// ||a|| * ||b|| * ||c||
|
|
|
|
|
|
// = -------------------------------------------------------
|
|
|
|
|
|
// 4 * 1/2 * ||cross(B-A,C-A)|| * min( ||a||,||b||,||c|| )
|
|
|
|
|
|
//
|
|
|
|
|
|
// a'a * b'b * c'c
|
2009-06-04 08:46:29 +00:00
|
|
|
|
// R<> = ----------------------------------------------------------
|
2009-02-06 13:37:46 +00:00
|
|
|
|
// 4 * cross(B-A,C-A)'cross(B-A,C-A) * min( a'a, b'b, c'c )
|
|
|
|
|
|
//
|
|
|
|
|
|
// a'a * b'b * c'c
|
|
|
|
|
|
// R = 1/2 * sqrt(---------------------------)
|
|
|
|
|
|
// AA * min( a'a, b'b, c'c )
|
|
|
|
|
|
//
|
2009-06-04 08:46:29 +00:00
|
|
|
|
// At angle 60<36> R has it's minimum for all edge lengths = sqrt(1/3)
|
2009-02-06 13:37:46 +00:00
|
|
|
|
//
|
2012-09-19 16:15:39 +00:00
|
|
|
|
// Define normalized roundness
|
2009-02-06 13:37:46 +00:00
|
|
|
|
//
|
|
|
|
|
|
// nR := sqrt(1/3) / R
|
|
|
|
|
|
//
|
|
|
|
|
|
// AA * min( a'a, b'b, c'c )
|
|
|
|
|
|
// = sqrt(4/3) * sqrt(---------------------------)
|
|
|
|
|
|
// a'a * b'b * c'c
|
|
|
|
|
|
//
|
|
|
|
|
|
double roundness( const Vec3f& A, const Vec3f& B, const Vec3f &C )
|
|
|
|
|
|
{
|
|
|
|
|
|
const value_type epsilon = value_type(1e-15);
|
|
|
|
|
|
|
2012-09-19 16:15:39 +00:00
|
|
|
|
static const value_type sqrt43 = value_type(sqrt(4.0/3.0)); // 60<36>,a=b=c, **)
|
2009-02-06 13:37:46 +00:00
|
|
|
|
|
|
|
|
|
|
Vec3f vecAC = C-A;
|
|
|
|
|
|
Vec3f vecAB = B-A;
|
|
|
|
|
|
|
|
|
|
|
|
// compute squared values to avoid sqrt-computations
|
|
|
|
|
|
value_type aa = (B-C).sqrnorm();
|
|
|
|
|
|
value_type bb = vecAC.sqrnorm();
|
|
|
|
|
|
value_type cc = vecAB.sqrnorm();
|
|
|
|
|
|
value_type AA = cross(vecAC,vecAB).sqrnorm(); // without factor 1/4 **)
|
|
|
|
|
|
|
|
|
|
|
|
if ( AA < epsilon )
|
|
|
|
|
|
return 0.0;
|
|
|
|
|
|
|
|
|
|
|
|
double nom = AA * std::min( std::min(aa,bb),cc );
|
2012-09-19 16:15:39 +00:00
|
|
|
|
double denom = aa * bb * cc;
|
2009-02-06 13:37:46 +00:00
|
|
|
|
double nR = sqrt43 * sqrt(nom/denom);
|
|
|
|
|
|
|
|
|
|
|
|
return nR;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2011-11-16 09:45:08 +00:00
|
|
|
|
private:
|
|
|
|
|
|
|
2009-02-06 13:37:46 +00:00
|
|
|
|
value_type min_r_;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
|
} // END_NS_DECIMATER
|
|
|
|
|
|
} // END_NS_OPENMESH
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
|
#if defined(OM_CC_MSVC) && defined(OM_ENABLE_WARNINGS)
|
|
|
|
|
|
# pragma warning(default : OM_ENABLE_WARNINGS)
|
|
|
|
|
|
# undef OM_ENABLE_WARNINGS
|
|
|
|
|
|
#endif
|
|
|
|
|
|
//=============================================================================
|
2011-11-16 09:45:08 +00:00
|
|
|
|
#endif // OPENMESH_DECIMATER_MODROUNDNESST_HH defined
|
2009-02-06 13:37:46 +00:00
|
|
|
|
//=============================================================================
|
|
|
|
|
|
|