Commit b0bb26f2 authored by Jan Möbius's avatar Jan Möbius

git-svn-id: http://www.openflipper.org/svnrepo/OpenFlipper/branches/Free@12835 383ad7c9-94d9-4d36-a494-682f7c89f535
parent eaf14737
include (plugin)
openflipper_plugin ()
//=============================================================================
//
// CLASS ModFeatureT
//
//=============================================================================
#ifndef MB_MODFEATURET_HH
#define MB_MODFEATURET_HH
//== INCLUDES =================================================================
#include <mb_base/mb_base.hh>
#include <OpenMesh/Tools/Decimater/ModBaseT.hh>
#include <OpenMesh/Core/Utils/Property.hh>
//== NAMESPACES ===============================================================
namespace OpenMesh {
namespace Decimater {
//== CLASS DEFINITION =========================================================
template <class DecimaterT>
class ModFeatureT : public ModBaseT< DecimaterT >
{
public:
DECIMATING_MODULE( ModFeatureT, DecimaterT, Feature );
public:
/// Constructor
ModFeatureT(DecimaterT& _dec) : Base(_dec, true)
{
Base::mesh().add_property(feature_IDs_);
}
/// Destructor
~ModFeatureT()
{
Base::mesh().remove_property(feature_IDs_);
}
int& feature_ID(typename Mesh::VertexHandle _vh)
{
return Base::mesh().property(feature_IDs_, _vh);
}
float collapse_priority(const CollapseInfo& _ci)
{
int f0 = feature_ID(_ci.v0);
if (f0)
{
int f1 = feature_ID(_ci.v1);
return (f0 == f1 ? 0.0 : -1.0);
}
return true;
}
private:
OpenMesh::VPropHandleT<int> feature_IDs_;
};
//=============================================================================
}
}
//=============================================================================
#endif // MB_MODFEATURET_HH defined
//=============================================================================
//=============================================================================
//
// CLASS ModRoundnessT - IMPLEMENTATION
//
//=============================================================================
#define MB_MODROUNDNESST_C
//== INCLUDES =================================================================
#include "ModRoundnessT.hh"
#include <ACG/Geometry/Algorithms.hh>
//== NAMESPACES ===============================================================
namespace OpenMesh {
namespace Decimater {
//== IMPLEMENTATION ==========================================================
template <class DecimaterT>
void
ModRoundnessT<DecimaterT>::
initialize()
{
typename Mesh::FaceIter f_it, f_end(mesh_.faces_end());
typename Mesh::FVIter fv_it;
for (f_it=mesh_.faces_begin(); f_it!=f_end; ++f_it)
{
typename Mesh::Point& p0 = mesh_.point(fv_it=mesh_.fv_iter(f_it));
typename Mesh::Point& p1 = mesh_.point(++fv_it);
typename Mesh::Point& p2 = mesh_.point(++fv_it);
mesh_.property(roundness_, f_it) =
1.0/ACG::Geometry::aspectRatio(p0, p1, p2);
}
}
//-----------------------------------------------------------------------------
template <class DecimaterT>
void
ModRoundnessT<DecimaterT>::
preprocess_collapse(const CollapseInfo& _ci)
{
typename Mesh::FaceHandle fh;
typename Mesh::FVIter fv_it;
for (typename Mesh::VFIter vf_it=mesh_.vf_iter(_ci.v0); vf_it; ++vf_it)
{
fh = vf_it.handle();
if (fh != _ci.fl && fh != _ci.fr)
{
typename Mesh::Point& p0 = mesh_.point(fv_it=mesh_.fv_iter(fh));
typename Mesh::Point& p1 = mesh_.point(++fv_it);
typename Mesh::Point& p2 = mesh_.point(++fv_it);
mesh_.property(roundness_, fh) =
1.0/ACG::Geometry::aspectRatio(p0, p1, p2);
}
}
}
//-----------------------------------------------------------------------------
template <class DecimaterT>
float
ModRoundnessT<DecimaterT>::
collapse_priority(const CollapseInfo& _ci)
{
typename Mesh::VertexHandle v2, v3;
typename Mesh::FaceHandle fh;
const typename Mesh::Point *p1(&_ci.p1), *p2, *p3;
typename Mesh::Scalar r0, r1, r0_min(1.0), r1_min(1.0);
typename Mesh::CVVIter vv_it(mesh_, _ci.v0);
v3 = vv_it.handle();
p3 = &mesh_.point(v3);
while (vv_it)
{
v2 = v3;
p2 = p3;
v3 = (++vv_it).handle();
p3 = &mesh_.point(v3);
fh = mesh_.face_handle(vv_it.current_halfedge_handle());
// if not boundary
if (fh.is_valid())
{
// roundness before
if ((r0 = mesh_.property(roundness_,fh)) < r0_min)
r0_min = r0;
// roundness after
if (!(v2 == _ci.v1 || v3 == _ci.v1))
if ((r1 = 1.0/ACG::Geometry::aspectRatio(*p1, *p2, *p3)) < r1_min)
r1_min = r1;
}
}
if (Base::is_binary())
{
return ((r1_min > r0_min) || (r1_min > min_roundness_)) ? 0.0 : -1.0;
}
else
{
if (r1_min > r0_min)
return 1.0 - r1_min;
else
return (r1_min > min_roundness_) ? 2.0 - r1_min : -1.0;
}
}
//=============================================================================
}
}
//=============================================================================
//=============================================================================
//
// CLASS ModRoundnessT
//
//=============================================================================
#ifndef MB_MODROUNDNESST_HH
#define MB_MODROUNDNESST_HH
//== INCLUDES =================================================================
#include <OpenMesh/Tools/Decimater/ModBaseT.hh>
#include <OpenMesh/Core/Utils/Property.hh>
//== NAMESPACES ===============================================================
namespace OpenMesh {
namespace Decimater {
//== CLASS DEFINITION =========================================================
template <class DecimaterT>
class ModRoundnessT : public ModBaseT<DecimaterT>
{
public:
DECIMATING_MODULE( ModRoundnessT, DecimaterT, Roundness );
// constructor
ModRoundnessT(DecimaterT& _dec,
float _min_roundness = 5.0,
bool _is_binary = true)
: Base(_dec, _is_binary),
mesh_(Base::mesh()),
min_roundness_(1.0/_min_roundness)
{
mesh_.add_property( roundness_ );
}
// destructor
~ModRoundnessT()
{
mesh_.remove_property( roundness_ );
}
/// get roundness
float roundness() const { return 1.0/min_roundness_; }
/// set roundness
void set_roundness(float _f) { min_roundness_ = 1.0/_f; }
// precompute face roundness
void initialize();
// blabla
float collapse_priority(const CollapseInfo& _ci);
// update roundness of one-ring
void preprocess_collapse(const CollapseInfo& _ci);
private:
Mesh& mesh_;
float min_roundness_;
FPropHandleT<float> roundness_;
};
//=============================================================================
}
}
//=============================================================================
#if defined(INCLUDE_TEMPLATES) && !defined(MB_MODROUNDNESST_C)
#define MB_MODROUNDNESST_TEMPLATES
#include "ModRoundnessT.cc"
#endif
//=============================================================================
#endif // MB_MODROUNDNESST_HH defined
//=============================================================================
//=============================================================================
//
// OpenMesh
// Copyright (C) 2003 by Computer Graphics Group, RWTH Aachen
// www.openmesh.org
//
//-----------------------------------------------------------------------------
//
// License
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Library General Public License as published
// by the Free Software Foundation, version 2.
//
// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Library General Public License for more details.
//
// You should have received a copy of the GNU Library General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
//-----------------------------------------------------------------------------
//
// $Revision: 16 $
// $Date: 2006-09-28 11:10:52 +0200 (Thu, 28 Sep 2006) $
//
//=============================================================================
/** \file ModRoundnessT.hh
*/
//=============================================================================
//
// CLASS ModRoundnessT
//
//=============================================================================
#ifndef OPENMESH_TOOLS_MODROUNDNESST_HH
#define OPENMESH_TOOLS_MODROUNDNESST_HH
//== INCLUDES =================================================================
#include "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 =========================================================
/** Compute error value based on roundness criteria.
*/
template <class DecimaterType>
class ModRoundnessT : public ModBaseT<DecimaterType>
{
public:
DECIMATING_MODULE( ModRoundnessT, DecimaterType, Roundness );
public:
// typedefs
typedef typename Mesh::Point Point;
typedef typename vector_traits<Point>::value_type value_type;
public:
/// Constructor
ModRoundnessT( DecimaterType &_dec ) :
Base(_dec, false),
min_r_(-1.0)
{ }
/// Destructor
~ModRoundnessT() { }
public: // inherited
/** 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
* normalized.
*
* \return [0:1] or ILLEGAL_COLLAPSE in non-binary mode
* \return LEGAL_COLLAPSE or ILLEGAL_COLLAPSE in binary mode
* \see set_min_roundness()
*/
float collapse_priority(const CollapseInfo& _ci)
{
using namespace OpenMesh;
typename Mesh::ConstVertexOHalfedgeIter voh_it(Self::mesh(), _ci.v0);
double r;
double priority = 0.0; //==LEGAL_COLLAPSE
typename Mesh::FaceHandle fhC, fhB;
Vec3f B,C;
if ( min_r_ < 0.0 ) // continues mode
{
C = vector_cast<Vec3f>(Self::mesh().point( Self::mesh().to_vertex_handle(voh_it)));
fhC = Self::mesh().face_handle( voh_it.handle() );
for (++voh_it; voh_it; ++voh_it)
{
B = C;
fhB = fhC;
C = vector_cast<Vec3f>(Self::mesh().point(Self::mesh().to_vertex_handle(voh_it)));
fhC = Self::mesh().face_handle( voh_it.handle() );
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) );
}
}
else // binary mode
{
C = vector_cast<Vec3f>(Self::mesh().point( Self::mesh().to_vertex_handle(voh_it)));
fhC = Self::mesh().face_handle( voh_it.handle() );
for (++voh_it; voh_it && (priority==Self::LEGAL_COLLAPSE); ++voh_it)
{
B = C;
fhB = fhC;
C = vector_cast<Vec3f>(Self::mesh().point(Self::mesh().to_vertex_handle(voh_it)));
fhC = Self::mesh().face_handle( voh_it.handle() );
if ( fhB == _ci.fl || fhB == _ci.fr )
continue;
priority = ( (r=roundness( vector_cast<Vec3f>(_ci.p1), B, C )) < min_r_)
? Self::ILLEGAL_COLLAPSE
: Self::LEGAL_COLLAPSE;
}
}
return priority;
}
public: // specific methods
void set_min_angle( float _angle, bool _binary=true )
{
assert( _angle > 0 && _angle < 60 );
_angle = M_PI * _angle /180.0;
Vec3f A,B,C;
A = Vec3f( 0, 0, 0);
B = Vec3f( 2*cos(_angle), 0, 0);
C = Vec3f( cos(_angle), sin(_angle), 0);
double r1 = roundness(A,B,C);
_angle = 0.5 * ( M_PI - _angle );
A = Vec3f( 0, 0, 0);
B = Vec3f( 2*cos(_angle), 0, 0);
C = Vec3f( cos(_angle), sin(_angle), 0);
double r2 = roundness(A,B,C);
set_min_roundness( std::min(r1,r2), true );
}
/** Set a minimum roundness value.
* \param _min_roundness in range (0,1)
* \param _binary Set true, if the binary mode should be enabled,
* else false. In latter case the collapse_priority()
* returns a float value if the constrain does not apply
* 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;
Self::set_binary(_binary);
}
/// Unset minimum value constraint and enable non-binary mode.
void unset_min_roundness()
{
min_r_ = -1.0;
Self::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
//
// radius of circumference
// R := -----------------------
// length of shortest edge
//
// ||a|| * ||b|| * ||c||
// ---------------------
// 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
// R� = ----------------------------------------------------------
// 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 )
//
// At angle 60� R has it's minimum for all edge lengths = sqrt(1/3)
//
// Define normalized roundness
//
// 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);
static const value_type sqrt43 = sqrt(4.0/3.0); // 60�,a=b=c, **)
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 );
double denom = aa * bb * cc;
double nR = sqrt43 * sqrt(nom/denom);
return nR;
}
private:
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
//=============================================================================
#endif // OPENMESH_TOOLS_PROGMESHT_HH defined
//=============================================================================
# angle in degrees [0,60]
#
# compute roundness of first case
A = [ 0.0335717 0.0576863 -0.0503314 ]';
B = [ 0.0325544 0.057614 -0.0504989 ]';
C = [ 0.0323531 0.057051 -0.0504476 ]';
#
vecAC=C-A;
vecAB=B-A;
aa = norm(B-C)^2;
bb = norm(vecAC)^2;
cc = norm(vecAB)^2;
AA = norm(cross(vecAC,vecAB))^2
nom = AA * min( aa, min(bb,cc) );
denom = aa * bb * cc;
nR1 = sqrt(4.0/3.0) * sqrt(nom/denom)
# angle in degrees [0,60]
# [replace :angle: with a value between 0 and 60]
alpha_d = :angle:;
# compute roundness of first case
alpha = pi * alpha_d/180;
A = [ 0 0 0 ]';
B = [ 2*cos(alpha) 0 0 ]';
C = [ cos(alpha) sin(alpha) 0 ]';
#
vecAC=C-A;
vecAB=B-A;
aa = norm(B-C)^2;
bb = norm(vecAC)^2;
cc = norm(vecAB)^2;
AA = norm(cross(vecAC,vecAB))^2;
nom = AA * min( aa, min(bb,cc) );
denom = aa * bb * cc;
nR1 = sqrt(4.0/3.0) * sqrt(nom/denom)
# compute roundness of 2nd case
alpha = pi * ((180-alpha_d)/2)/180;
A = [ 0 0 0 ]';
B = [ 2*cos(alpha) 0 0 ]';
C = [ cos(alpha) sin(alpha) 0 ]';
#
vecAC=C-A;
vecAB=B-A;
aa = norm(B-C)^2;
bb = norm(vecAC)^2;
cc = norm(vecAB)^2;
AA = norm(cross(vecAC,vecAB))^2;
nom = AA * min( aa, min(bb,cc) );
denom = aa * bb * cc;
nR2 = sqrt(4.0/3.0) * sqrt(nom/denom)
#!/bin/sh
i=0
while ((i <= 60)); do
cat roundness.m | perl -pe "s/:angle:/$i/" > tmp.m
echo $i `octave -q tmp.m 2> /dev/null | grep -v "nR2" | perl -pe 's/^nR1 = (.*)$/\1/'`
i=$((++i))
done
This diff is collapsed.
/*===========================================================================*\
* *
* OpenFlipper *
* Copyright (C) 2001-2011 by Computer Graphics Group, RWTH Aachen *
* www.openflipper.org *
* *
*--------------------------------------------------------------------------- *