First checkin for OpenMesh 2.0

git-svn-id: http://www.openmesh.org/svnrepo/OpenMesh/trunk@2 fdac6126-5c0c-442c-9429-916003d36597
This commit is contained in:
Jan Möbius
2009-02-06 13:37:46 +00:00
parent c3321ebdd9
commit 97f515985d
417 changed files with 76182 additions and 0 deletions

View File

@@ -0,0 +1,63 @@
//=============================================================================
//
// 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 Lesser General Public License as published
// by the Free Software Foundation, version 2.1.
//
// 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
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
//-----------------------------------------------------------------------------
//
// $Revision: 1800 $
// $Date: 2008-05-19 11:51:23 +0200 (Mo, 19. Mai 2008) $
//
//=============================================================================
//== INCLUDES =================================================================
#ifdef _MSC_VER
# pragma warning(disable: 4267 4311)
#endif
#include <OpenMesh/Core/IO/MeshIO.hh>
#include <OpenMesh/Apps/Subdivider/MeshViewerWidget.hh>
using namespace OpenMesh;
//== IMPLEMENTATION ==========================================================
bool
MeshViewerWidget::open_mesh(const char* _filename, IO::Options _opt)
{
if ( Base::open_mesh( _filename, _opt ) )
{
orig_mesh_ = mesh_;
return true;
}
return false;
}
//=============================================================================

View File

@@ -0,0 +1,84 @@
//=============================================================================
//
// 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 Lesser General Public License as published
// by the Free Software Foundation, version 2.1.
//
// 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
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
//-----------------------------------------------------------------------------
//
// $Revision: 1800 $
// $Date: 2008-05-19 11:51:23 +0200 (Mo, 19. Mai 2008) $
//
//=============================================================================
#ifndef OPENMESHAPPS_MESHVIEWERWIDGET_HH
#define OPENMESHAPPS_MESHVIEWERWIDGET_HH
//== INCLUDES =================================================================
// -------------------- OpenMesh
#include <OpenMesh/Apps/QtViewer/MeshViewerWidgetT.hh>
#include <OpenMesh/Core/Mesh/TriMesh_ArrayKernelT.hh>
#include <OpenMesh/Tools/Subdivider/Uniform/Composite/CompositeTraits.hh>
//
typedef OpenMesh::Subdivider::Uniform::CompositeTraits Traits;
typedef OpenMesh::TriMesh_ArrayKernelT<Traits> Mesh;
//== CLASS DEFINITION =========================================================
class MeshViewerWidget : public MeshViewerWidgetT<Mesh>
{
public:
typedef MeshViewerWidgetT<Mesh> Base;
/// default constructor
MeshViewerWidget(QWidget* _parent=0)
: Base(_parent)
{}
/// destructor
~MeshViewerWidget() {}
/// open mesh
bool open_mesh(const char* _filename, OpenMesh::IO::Options);
Mesh& orig_mesh() { return orig_mesh_; }
const Mesh& orig_mesh() const { return orig_mesh_; }
protected:
Mesh orig_mesh_;
};
//=============================================================================
#endif // OPENMESHAPPS_MESHVIEWERWIDGET_HH defined
//=============================================================================

View File

@@ -0,0 +1,373 @@
//=============================================================================
//
// CLASS SubdivideWidget - IMPLEMENTATION
//
//=============================================================================
#ifndef SUBDIVIDEWIDGET_CC
#define SUBDIVIDEWIDGET_CC
//== INCLUDES =================================================================
// Qt
#include <QApplication>
#include <QFileDialog>
#include <QButtonGroup>
#include <QRadioButton>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QPushButton>
#include <QLabel>
#include <QString>
#include <QMessageBox>
// OpenMesh
#include <OpenMesh/Core/IO/MeshIO.hh>
#include <OpenMesh/Core/Mesh/PolyConnectivity.hh>
#include <OpenMesh/Tools/Subdivider/Uniform/CompositeLoopT.hh>
#include <OpenMesh/Tools/Subdivider/Uniform/CompositeSqrt3T.hh>
#include <OpenMesh/Tools/Subdivider/Uniform/LoopT.hh>
#include <OpenMesh/Tools/Subdivider/Uniform/Sqrt3T.hh>
// My stuff
#include <OpenMesh/Apps/Subdivider/SubdivideWidget.hh>
//
using namespace OpenMesh::Subdivider;
//== IMPLEMENTATION ==========================================================
SubdivideWidget::
SubdivideWidget(QWidget* _parent, const char* _name)
: QWidget(_parent),
timer_(NULL), animate_step_(0), max_animate_steps_(4), msecs_(0)
{
setWindowTitle( QString(_name) );
QVBoxLayout* vbox = new QVBoxLayout();
cur_topo_type = SOP_Undefined;
// sel_topo_type will be set when adding the radio button.;
// examiner widget
viewer_widget_ = new MeshViewerWidget();
vbox->addWidget(viewer_widget_);
QHBoxLayout* hbox = new QHBoxLayout();
// hbox->setFixedSize(400, 30);
vbox->addLayout(hbox);
// insert subdivision pushbutton
QPushButton* subdiv_button = new QPushButton( "Subdivide");
subdiv_button->setMinimumWidth(50);
QObject::connect( subdiv_button, SIGNAL( clicked() ),
this, SLOT( subdiv_slot() ) );
hbox->addWidget(subdiv_button);
// insert load pushbutton
QPushButton* load_button = new QPushButton( "Load Mesh");
load_button->setMinimumWidth(50);
QObject::connect( load_button, SIGNAL( clicked() ),
this, SLOT( load_slot() ) );
hbox->addWidget(load_button);
// insert save pushbutton
QPushButton* save_button = new QPushButton( "Save Mesh");
save_button->setMinimumWidth(50);
QObject::connect( save_button, SIGNAL( clicked() ),
this, SLOT( save_slot() ) );
hbox->addWidget(save_button);
// insert reset pushbutton
QPushButton* reset_button = new QPushButton( "Reset");
reset_button->setMinimumWidth(50);
QObject::connect( reset_button, SIGNAL( clicked() ),
this, SLOT( reset_slot() ) );
hbox->addWidget(reset_button);
// Create an exclusive button group: Topology Operators
// QButtonGroup *bgrp1 = new QButtonGroup( 1, QGroupBox::Vertical,"Subdivision Operators:");
QButtonGroup* buttonGroup = new QButtonGroup();
buttonGroup->setExclusive( TRUE );
// insert 2 radiobuttons
QRadioButton* radio1 = new QRadioButton( "Comp. Loop" );
QRadioButton* radio2 = new QRadioButton( "Comp. SQRT(3)" );
QRadioButton* radio3 = new QRadioButton( "Loop" );
radio3->setChecked( TRUE );
sel_topo_type = SOP_UniformLoop;
QRadioButton* radio4 = new QRadioButton( "Sqrt(3)" );
buttonGroup->addButton(radio1, SOP_UniformCompositeLoop);
buttonGroup->addButton(radio2, SOP_UniformCompositeSqrt3);
buttonGroup->addButton(radio3, SOP_UniformLoop);
buttonGroup->addButton(radio4, SOP_UniformSqrt3);
vbox->addWidget(radio1);
vbox->addWidget(radio2);
vbox->addWidget(radio3);
vbox->addWidget(radio4);
QObject::connect( buttonGroup, SIGNAL( buttonPressed(int) ),
this, SLOT( slot_select_sop(int) ) );
status_bar = new QStatusBar();
status_bar->setFixedHeight(20);
status_bar->showMessage("0 Faces, 0 Edges, 0 Vertices");
vbox->addWidget(status_bar);
setLayout(vbox);
// animation
timer_ = new QTimer(this);
connect( timer_, SIGNAL( timeout() ), this, SLOT( animate_slot() ) );
// --------------------
subdivider_[SOP_UniformCompositeLoop] = new Uniform::CompositeLoopT<Mesh>;
subdivider_[SOP_UniformCompositeSqrt3] = new Uniform::CompositeSqrt3T<Mesh>;
subdivider_[SOP_UniformLoop] = new Uniform::LoopT<Mesh>;
subdivider_[SOP_UniformSqrt3] = new Uniform::Sqrt3T<Mesh>;
}
//-----------------------------------------------------------------------------
void SubdivideWidget::slot_select_sop(int i)
{
switch(i)
{
case SOP_UniformCompositeLoop:
case SOP_UniformCompositeSqrt3:
case SOP_UniformLoop:
case SOP_UniformSqrt3: sel_topo_type = (SOPType)i; break;
default: sel_topo_type = SOP_Undefined;
}
}
//-----------------------------------------------------------------------------
void SubdivideWidget::keyPressEvent( QKeyEvent *k )
{
bool timerStopped = false;
if ( timer_->isActive())
{
timer_->stop();
timerStopped = true;
}
switch ( k->key() )
{
case Qt::Key_R: // reset
reset_slot();
break;
case Qt::Key_S: // save
save_slot();
break;
case Qt::Key_L: // load
load_slot();
break;
case Qt::Key_A:
if ( timerStopped )
break;
if (timer_->isActive())
{
timer_->stop();
}
else
{
reset_slot();
timer_->setSingleShot( true );
timer_->start(0);
}
break;
case ' ': // subdivide
subdiv_slot();
}
}
//-----------------------------------------------------------------------------
void SubdivideWidget::update()
{
unsigned int n_faces = viewer_widget_->mesh().n_faces();
unsigned int n_edges = viewer_widget_->mesh().n_edges();
unsigned int n_vertices = viewer_widget_->mesh().n_vertices();
QString message(""), temp;
message.append(temp.setNum(n_faces));
message.append(" Faces, ");
message.append(temp.setNum(n_edges));
message.append(" Edges, ");
message.append(temp.setNum(n_vertices));
message.append(" Vertices. ");
if (msecs_)
{
message.append(temp.setNum(msecs_/1000.0));
message.append("s");
}
status_bar->showMessage(message);
}
//-----------------------------------------------------------------------------
void SubdivideWidget::reset_slot()
{
if (cur_topo_type != SOP_Undefined)
subdivider_[cur_topo_type]->detach();
viewer_widget_->mesh() = viewer_widget_->orig_mesh();
viewer_widget_->mesh().update_face_normals();
viewer_widget_->mesh().update_vertex_normals();
viewer_widget_->updateGL();
update();
cur_topo_type = SOP_Undefined;
}
//-----------------------------------------------------------------------------
void SubdivideWidget::subdiv_slot()
{
assert( sel_topo_type != SOP_Undefined );
//QTime t;
using namespace OpenMesh::Subdivider::Uniform;
status_bar->showMessage( "processing subdivision step...");
if (cur_topo_type != sel_topo_type)
{
if (cur_topo_type!=SOP_Undefined)
subdivider_[cur_topo_type]->detach();
subdivider_[cur_topo_type=sel_topo_type]->attach(viewer_widget_->mesh());
}
std::clog << "subdiving...\n";
(*subdivider_[sel_topo_type])(1);
std::clog << "subdiving...done\n";
// Update viewer
viewer_widget_->mesh().update_normals();
viewer_widget_->updateGL();
// Update status bar information
update();
}
//-----------------------------------------------------------------------------
bool
SubdivideWidget::open_mesh(const char* _filename)
{
OpenMesh::IO::Options opt;
if (viewer_widget_->open_mesh(_filename, opt))
{
update();
return true;
}
return false;
}
//-----------------------------------------------------------------------------
void
SubdivideWidget::save_slot()
{
using OpenMesh::IO::IOManager;
QString write_filter(IOManager().qt_write_filters().c_str());
QString filename = QFileDialog::getSaveFileName(this, "", "", write_filter);
if (!filename.isEmpty()){
if (OpenMesh::IO::write_mesh(viewer_widget_->mesh(), filename.toStdString(),
OpenMesh::IO::Options::Binary) )
std::cerr << "ok\n";
else
std::cerr << "FAILED\n";
}
}
//-----------------------------------------------------------------------------
void
SubdivideWidget::load_slot()
{
using OpenMesh::IO::IOManager;
QString read_filter(IOManager().qt_read_filters().c_str());
QString filename =
QFileDialog::getOpenFileName(this, "", "", read_filter);
if (!filename.isNull())
{
if (cur_topo_type != SOP_Undefined)
subdivider_[cur_topo_type]->detach();
OpenMesh::IO::Options opt;
std::string file( filename.toStdString() );
if ( !viewer_widget_->open_mesh(file.c_str() , opt) )
{
QString msg = "Cannot read mesh from file ";
QMessageBox::critical( this,"", msg + filename, QMessageBox::Ok );
}
update();
cur_topo_type = SOP_Undefined;
}
}
//-----------------------------------------------------------------------------
void
SubdivideWidget::animate_slot()
{
if (++animate_step_ < max_animate_steps_)
{
subdiv_slot();
}
else
{
reset_slot();
animate_step_ = 0;
}
timer_->setSingleShot(true);
timer_->start( 500 );
}
//=============================================================================
#endif //SUBDIVIDEWIDGET_CC deifined
//=============================================================================

View File

@@ -0,0 +1,94 @@
//=============================================================================
//
// CLASS SubdivideWidget
//
//=============================================================================
#ifndef SUBDIVIDEWIDGET_HH
#define SUBDIVIDEWIDGET_HH
//== INCLUDES =================================================================
#include <QTimer>
#include <QMainWindow>
#include <QStatusBar>
#include <OpenMesh/Apps/Subdivider/MeshViewerWidget.hh>
#include <OpenMesh/Tools/Subdivider/Uniform/SubdividerT.hh>
//== CLASS DEFINITION =========================================================
class SubdivideWidget : public QWidget
{
Q_OBJECT
public:
typedef OpenMesh::Subdivider::Uniform::SubdividerT< Mesh > Subdivider;
enum SOPType {
SOP_UniformCompositeLoop,
SOP_UniformCompositeSqrt3,
SOP_UniformLoop,
SOP_UniformSqrt3,
SOP_Undefined
};
typedef std::map< SOPType, Subdivider* > SubdividerPool;
public:
/// constructor
SubdivideWidget(QWidget* _parent=0, const char* _name=0);
/// destructor
~SubdivideWidget() {};
/// open mesh from _filename
bool open_mesh(const char* _filename);
void keyPressEvent( QKeyEvent *k );
/// Updates Status Bar Information
QStatusBar *status_bar;
void update();
private slots:
void animate_slot(void);
void slot_select_sop(int i);
void subdiv_slot();
void reset_slot();
void save_slot();
void load_slot();
private:
// widgets
MeshViewerWidget* viewer_widget_;
QTimer *timer_;
int animate_step_;
int max_animate_steps_;
int msecs_;
// Selected and current subdivision operator
SOPType sel_topo_type; // selected operator
SOPType cur_topo_type; // active operator
//
SubdividerPool subdivider_;
};
//=============================================================================
#endif // SUBDIVIDEWIDGET_HH defined
//=============================================================================

View File

@@ -0,0 +1,26 @@
################################################################################
#
################################################################################
contains( OPENFLIPPER , OpenFlipper ){
include( $$TOPDIR/qmake/all.include )
} else {
include( $$TOPDIR/OpenMesh/qmake/all.include )
}
INCLUDEPATH += ../../..
Application()
glew()
glut()
openmesh()
DIRECTORIES = . ../QtViewer
# Input
HEADERS += $$getFilesFromDir($$DIRECTORIES,*.hh)
SOURCES += $$getFilesFromDir($$DIRECTORIES,*.cc)
SOURCES -= ../QtViewer/meshviewer.cc
FORMS += $$getFilesFromDir($$DIRECTORIES,*.ui)
################################################################################

View File

@@ -0,0 +1,467 @@
// ============================================================================
// -------------------------------------------------------------- includes ----
// -------------------- OpenMesh
#include <OpenMesh/Core/IO/MeshIO.hh>
#include <OpenMesh/Core/Mesh/TriMesh_ArrayKernelT.hh>
#include <OpenMesh/Tools/Utils/Timer.hh>
#include <OpenMesh/Tools/Utils/getopt.h>
// -------------------- OpenMesh Adaptive Composite Subdivider
#include <OpenMesh/Tools/Subdivider/Adaptive/Composite/CompositeT.hh>
#include <OpenMesh/Tools/Subdivider/Adaptive/Composite/RulesT.hh>
// -------------------- STL
#include <iostream>
#include <fstream>
#include <sstream>
#if defined(OM_CC_MIPS)
# include <math.h>
#else
# include <cmath>
using std::pow;
#endif
using OpenMesh::Subdivider::Adaptive::CompositeTraits;
// define mesh, rule interface, and subdivider types
typedef OpenMesh::TriMesh_ArrayKernelT<CompositeTraits> MyMesh;
typedef OpenMesh::Subdivider::Adaptive::RuleInterfaceT<MyMesh> Rule;
typedef OpenMesh::Subdivider::Adaptive::CompositeT<MyMesh> Subdivider;
// ----------------------------------------------------------------------------
using namespace OpenMesh::Subdivider;
// factory function to add a RULE to a subdivider
#define ADD_FN( RULE ) \
bool add_ ## RULE( Subdivider& _sub ) \
{ return _sub.add< Adaptive:: RULE < MyMesh > >(); }
ADD_FN( Tvv3 );
ADD_FN( Tvv4 );
ADD_FN( VF );
ADD_FN( FF );
ADD_FN( FFc );
ADD_FN( FV );
ADD_FN( FVc );
ADD_FN( VV );
ADD_FN( VVc );
ADD_FN( VE );
ADD_FN( VdE );
ADD_FN( VdEc );
ADD_FN( EV );
ADD_FN( EVc );
ADD_FN( EF );
ADD_FN( FE );
ADD_FN( EdE );
ADD_FN( EdEc );
#undef ADD_FN
typedef bool (*add_rule_ft)( Subdivider& );
// map rule name to factory function
struct RuleMap : std::map< std::string, add_rule_ft >
{
RuleMap()
{
#define ADD( RULE ) \
(*this)[ #RULE ] = add_##RULE;
ADD( Tvv3 );
ADD( Tvv4 );
ADD( VF );
ADD( FF );
ADD( FFc );
ADD( FV );
ADD( FVc );
ADD( VV );
ADD( VVc );
ADD( VE );
ADD( VdE );
ADD( VdEc );
ADD( EV );
ADD( EVc );
ADD( EF );
ADD( FE );
ADD( EdE );
ADD( EdEc );
#undef ADD
}
} available_rules;
// ----------------------------------------------------------------------------
std::string basename( const std::string& _fname );
void usage_and_exit(const std::string& _fname, int xcode);
// ----------------------------------------------------------------------------
int main(int argc, char **argv)
{
size_t n_iter = 0; // n iteration
size_t max_nv = size_t(-1); // max. number of vertices in the end
std::string ifname; // input mesh
std::string ofname; // output mesh
std::string rule_sequence = "Tvv3 VF FF FVc"; // sqrt3 default
bool uniform = false;
int c;
// ---------------------------------------- evaluate command line
while ( (c=getopt(argc, argv, "hlm:n:r:sU"))!=-1 )
{
switch(c)
{
case 's': rule_sequence = "Tvv3 VF FF FVc"; break; // sqrt3
case 'l': rule_sequence = "Tvv4 VdE EVc VdE EVc"; break; // loop
case 'n': { std::stringstream s; s << optarg; s >> n_iter; } break;
case 'm': { std::stringstream s; s << optarg; s >> max_nv; } break;
case 'r': rule_sequence = optarg; break;
case 'U': uniform = true; break;
case 'h': usage_and_exit(argv[0],0);
case '?':
default: usage_and_exit(argv[0],1);
}
}
if ( optind == argc )
usage_and_exit(argv[0],2);
if ( optind < argc )
ifname = argv[optind++];
if ( optind < argc )
ofname = argv[optind++];
// if ( optind < argc ) // too many arguments
// ---------------------------------------- mesh and subdivider
MyMesh mesh;
Subdivider subdivider(mesh);
// -------------------- read mesh from file
std::cout << "Input mesh : " << ifname << std::endl;
if (!OpenMesh::IO::read_mesh(mesh, ifname))
{
std::cerr << " Error reading file!\n";
return 1;
}
// store orignal size of mesh
size_t n_vertices = mesh.n_vertices();
size_t n_edges = mesh.n_edges();
size_t n_faces = mesh.n_faces();
if ( n_iter > 0 )
std::cout << "Desired #iterations: " << n_iter << std::endl;
if ( max_nv < size_t(-1) )
{
std::cout << "Desired max. #V : " << max_nv << std::endl;
if (!n_iter )
n_iter = size_t(-1);
}
// -------------------- Setup rule sequence
{
std::stringstream s;
std::string token;
RuleMap::iterator it = available_rules.end();
for (s << rule_sequence; s >> token; )
{
if ( (it=available_rules.find( token )) != available_rules.end() )
{
it->second( subdivider );
}
else if ( token[0]=='(' && (subdivider.n_rules() > 0) )
{
std::string::size_type beg(1);
if (token.length()==1)
{
s >> token;
beg = 0;
}
std::string::size_type
end = token.find_last_of(')');
std::string::size_type
size = end==std::string::npos ? token.size()-beg : end-beg;
std::stringstream v;
MyMesh::Scalar coeff;
std::cout << " " << token << std::endl;
std::cout << " " << beg << " " << end << " " << size << std::endl;
v << token.substr(beg, size);
v >> coeff;
std::cout << " coeffecient " << coeff << std::endl;
subdivider.rule( subdivider.n_rules()-1 ).set_coeff(coeff);
if (end == std::string::npos)
{
s >> token;
if (token[0]!=')')
{
std::cerr << "Syntax error: Missing ')'\n";
return 1;
}
}
}
else
{
std::cerr << "Syntax error: " << token << "?\n";
return 1;
}
}
}
std::cout << "Rule sequence : "
<< subdivider.rules_as_string() << std::endl;
// -------------------- Initialize subdivider
std::cout << "Initialize subdivider\n";
if (!subdivider.initialize())
{
std::cerr << " Error!\n";
return 1;
}
//
MyMesh::FaceFaceIter ff_it;
double quality(0.0), face_quality, temp_quality;
int valence;
// ---------------------------------------- subdivide
std::cout << "\nSubdividing...\n";
OpenMesh::Utils::Timer timer, timer2;
size_t i;
if ( uniform )
{ // unifom
MyMesh::VertexHandle vh;
MyMesh::VertexIter v_it;
MyMesh::FaceHandle fh;
MyMesh::FaceIter f_it;
// raise all vertices to target state
timer.start();
size_t n = n_iter;
size_t n_rules = subdivider.n_rules();
i = 0;
// calculate target states for faces and vertices
int target1 = (n - 1) * n_rules + subdivider.subdiv_rule().number() + 1;
int target2 = n * n_rules;
for (f_it = mesh.faces_begin(); f_it != mesh.faces_end(); ++f_it) {
if (mesh.data(f_it).state() < target1) {
++i;
fh = f_it.handle();
timer2.start();
subdivider.refine(fh);
timer2.stop();
}
}
for (v_it = mesh.vertices_begin(); v_it != mesh.vertices_end(); ++v_it) {
if (mesh.data(v_it).state() < target2) {
vh = v_it.handle();
timer2.cont();
subdivider.refine(vh);
timer2.stop();
}
}
timer.stop();
}
else
{ // adaptive
MyMesh::FaceIter f_it;
MyMesh::FaceHandle fh;
std::vector<double> __acos;
size_t buckets(3000);
double range(2.0);
double range2bucket(buckets/range);
for (i = 0; i < buckets; ++i)
__acos.push_back( acos(-1.0 + i * range / buckets) );
timer.start(); // total time needed
// n iterations or until desired number of vertices reached approx.
for (i = 0; i < n_iter && mesh.n_vertices() < max_nv; ++i)
{
mesh.update_face_normals();
// calculate quality
quality = 0.0;
fh = mesh.faces_begin().handle();
// check every face
for (f_it = mesh.faces_begin(); f_it != mesh.faces_end(); ++f_it) {
face_quality = 0.0;
valence = 0;
for (ff_it = mesh.ff_iter(f_it.handle()); ff_it; ++ff_it) {
temp_quality = OpenMesh::dot( mesh.normal(f_it),
mesh.normal(ff_it) );
if (temp_quality >= 1.0)
temp_quality = .99;
else if (temp_quality <= -1.0)
temp_quality = -.99;
temp_quality = (1.0+temp_quality) * range2bucket;
face_quality += __acos[int(temp_quality+.5)];
++valence;
}
face_quality /= valence;
// calaculate face area
MyMesh::Point p1, p2, p3;
MyMesh::Scalar area;
#define heh halfedge_handle
#define nheh next_halfedge_handle
#define tvh to_vertex_handle
#define fvh from_vertex_handle
p1 = mesh.point(mesh.tvh(mesh.heh(f_it.handle())));
p2 = mesh.point(mesh.fvh(mesh.heh(f_it.handle())));
p3 = mesh.point(mesh.tvh(mesh.nheh(mesh.heh(f_it.handle()))));
#undef heh
#undef nheh
#undef tvh
#undef fvh
area = ((p2 - p1) % (p3 - p1)).norm();
// weight face_quality
face_quality *= pow(double(area), double(.1));
//face_quality *= area;
if (face_quality >= quality && !mesh.is_boundary(f_it.handle()))
{
quality = face_quality;
fh = f_it.handle();
}
}
// Subdivide Face
timer2.cont();
subdivider.refine(fh);
timer2.stop();
}
// calculate time
timer.stop();
} // uniform/adaptive?
// calculate maximum refinement level
Adaptive::state_t max_level(0);
for (MyMesh::VertexIter v_it = mesh.vertices_begin();
v_it != mesh.vertices_end(); ++v_it)
{
if (mesh.data(v_it).state() > max_level)
max_level = mesh.data(v_it).state();
}
// output results
std::cout << "\nDid " << i << (uniform ? " uniform " : "" )
<< " subdivision steps in "
<< timer.as_string()
<< ", " << i/timer.seconds() << " steps/s\n";
std::cout << " only refinement: " << timer2.as_string()
<< ", " << i/timer2.seconds() << " steps/s\n\n";
std::cout << "Before: ";
std::cout << n_vertices << " Vertices, ";
std::cout << n_edges << " Edges, ";
std::cout << n_faces << " Faces. \n";
std::cout << "Now : ";
std::cout << mesh.n_vertices() << " Vertices, ";
std::cout << mesh.n_edges() << " Edges, ";
std::cout << mesh.n_faces() << " Faces. \n\n";
std::cout << "Maximum quality : " << quality << std::endl;
std::cout << "Maximum Subdivision Level: " << max_level/subdivider.n_rules()
<< std::endl << std::endl;
// ---------------------------------------- write mesh to file
{
if ( ofname.empty() )
{
std::stringstream s;
s << "result." << subdivider.rules_as_string("_")
<< "-" << i << "x.off";
s >> ofname;
}
std::cout << "Output file: '" << ofname << "'.\n";
if (!OpenMesh::IO::write_mesh(mesh, ofname, OpenMesh::IO::Options::Binary))
{
std::cerr << " Error writing file!\n";
return 1;
}
}
return 0;
}
// ----------------------------------------------------------------------------
// helper
void usage_and_exit(const std::string& _fname, int xcode)
{
using namespace std;
cout << endl
<< "Usage: " << basename(_fname)
<< " [Options] input-mesh [output-mesh]\n\n";
cout << "\tAdaptively refine an input-mesh. The refined mesh is stored in\n"
<< "\ta file named \"result.XXX.off\" (binary .off), if not specified\n"
<< "\texplicitely (optional 2nd parameter of command line).\n\n";
cout << "Options:\n\n";
cout << "-m <int>\n\tAdaptively refine up to approx. <int> vertices.\n\n"
<< "-n <int>\n\tAdaptively refine <int> times.\n\n"
<< "-r <rule sequence>\n\tDefine a custom rule sequence.\n\n"
<< "-l\n\tUse rule sequence for adaptive Loop.\n\n"
<< "-s\n\tUse rule sequence for adaptive sqrt(3).\n\n"
<< "-U\n\tRefine mesh uniformly (simulates uniform subdivision).\n\n";
exit(xcode);
}
std::string basename(const std::string& _f)
{
std::string::size_type dot = _f.rfind("/");
if (dot == std::string::npos)
return _f;
return _f.substr(dot+1, _f.length()-(dot+1));
}
// ----------------------------------------------------------------------------
// end of file
// ============================================================================

View File

@@ -0,0 +1,72 @@
//=============================================================================
//
// 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 Lesser General Public License as published
// by the Free Software Foundation, version 2.1.
//
// 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
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
//=============================================================================
#ifdef _MSC_VER
# pragma warning(disable: 4267 4311)
#endif
#include <iostream>
#include <fstream>
#include <QApplication>
#include <QMessageBox>
#include "SubdivideWidget.hh"
#include <GL/glut.h>
int main(int argc, char **argv)
{
// OpenGL check
QApplication::setColorSpec( QApplication::CustomColor );
QApplication app(argc,argv);
glutInit(&argc,argv);
if ( !QGLFormat::hasOpenGL() ) {
QString msg = "System has no OpenGL support!";
QMessageBox::critical( NULL, "OpenGL", msg + argv[1], QMessageBox::Ok );
return -1;
}
// create widget
SubdivideWidget* w = new SubdivideWidget(0, "Subdivider");
w->resize(400, 400);
w->show();
// load scene
if (argc > 1)
{
if ( ! w->open_mesh(argv[1]) )
{
QString msg = "Cannot read mesh from file ";
QMessageBox::critical( NULL, argv[1], msg + argv[1], QMessageBox::Ok );
return -1;
}
}
return app.exec();
}

View File

@@ -0,0 +1,288 @@
//=============================================================================
//
// 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 Lesser General Public License as published
// by the Free Software Foundation, version 2.1.
//
// 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
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
//-----------------------------------------------------------------------------
//
// $Revision: 1800 $
// $Date: 2008-05-19 11:51:23 +0200 (Mo, 19. Mai 2008) $
//
//=============================================================================
#include <iostream>
#include <sstream>
// ---------------------------------------- OpenMesh Stuff
#include <OpenMesh/Core/IO/MeshIO.hh>
#include <OpenMesh/Core/Mesh/TriMesh_ArrayKernelT.hh>
#include <OpenMesh/Tools/Utils/Timer.hh>
#include <OpenMesh/Tools/Utils/getopt.h>
// ---------------------------------------- Subdivider
#include <OpenMesh/Tools/Subdivider/Uniform/Sqrt3T.hh>
#include <OpenMesh/Tools/Subdivider/Uniform/LoopT.hh>
#include <OpenMesh/Tools/Subdivider/Uniform/CompositeSqrt3T.hh>
#include <OpenMesh/Tools/Subdivider/Uniform/CompositeLoopT.hh>
// ----------------------------------------------------------------------------
using namespace OpenMesh::Subdivider;
typedef OpenMesh::Subdivider::Uniform::CompositeTraits CTraits;
typedef OpenMesh::TriMesh_ArrayKernelT<CTraits> CMesh;
typedef OpenMesh::TriMesh_ArrayKernelT<> Mesh;
typedef Uniform::Sqrt3T< Mesh > Sqrt3;
typedef Uniform::LoopT< Mesh > Loop;
typedef Uniform::CompositeSqrt3T< CMesh > CompositeSqrt3;
typedef Uniform::CompositeLoopT< CMesh > CompositeLoop;
using OpenMesh::Utils::Timer;
// ----------------------------------------------------------------------------
std::map< std::string, double > timings;
// ----------------------------------------------------------------------------
template < typename Subdivider >
bool subdivide( typename Subdivider::mesh_t& _m, size_t _n,
Timer::Format _fmt )
{
bool rc;
Timer t;
Subdivider subdivide;
std::cout << "Subdivide " << _n
<< " times with '" << subdivide.name() << "'\n";
subdivide.attach(_m);
t.start();
rc=subdivide( _n );
t.stop();
subdivide.detach();
if (rc)
{
std::cout << " Done [" << t.as_string(_fmt) << "]\n";
timings[subdivide.name()] = t.seconds();
}
else
std::cout << " Failed!\n";
return rc;
}
// ----------------------------------------------------------------------------
void usage_and_exit(int _xcode);
// ----------------------------------------------------------------------------
template < typename Subdivider >
int mainT( size_t _n,
const std::string& _ifname,
const std::string& _ofname,
const Timer::Format _fmt )
{
// -------------------- read mesh
std::cout << "Read mesh from file " << _ifname << std::endl;
typename Subdivider::mesh_t mesh;
if ( OpenMesh::IO::read_mesh( mesh, _ifname ) )
std::cout << " Ok\n";
else
{
std::cout << " Failed!\n";
return 1;
}
std::cout << " #V " << mesh.n_vertices()
<< ", #F " << mesh.n_faces()
<< ", #E " << mesh.n_edges() << std::endl;
// -------------------- subdividing
try
{
if (!subdivide< Subdivider >( mesh, _n, _fmt ))
return 1;
}
catch(std::bad_alloc& x)
{
std::cerr << "Out of memory: " << x.what() << std::endl;
return 1;
}
catch(std::exception& x)
{
std::cerr << x.what() << std::endl;
return 1;
}
catch(...)
{
std::cerr << "Unknown exception!\n";
return 1;
}
// -------------------- write mesh
std::cout << " #V " << mesh.n_vertices()
<< ", #F " << mesh.n_faces()
<< ", #E " << mesh.n_edges() << std::endl;
if ( !_ofname.empty() )
{
std::cout << "Write resulting mesh to file " << _ofname << "..";
if (OpenMesh::IO::write_mesh(mesh, _ofname, OpenMesh::IO::Options::Binary))
{
std::cout << "ok\n";
}
else
{
std::cerr << "Failed! Could not write file!\n";
return 1;
}
}
return 0;
}
// ----------------------------------------------------------------------------
int main(int argc, char **argv)
{
int c;
bool compare_all = false;
size_t n;
std::string ifname;
std::string ofname;
enum {
TypeSqrt3,
TypeLoop,
TypeCompSqrt3,
TypeCompLoop
} st = TypeSqrt3;
Timer::Format fmt = Timer::Automatic;
while ( (c=getopt(argc, argv, "csSlLhf:"))!=-1 )
{
switch(c)
{
case 'c': compare_all=true; break;
case 's': st = TypeSqrt3; break;
case 'S': st = TypeCompSqrt3; break;
case 'l': st = TypeLoop; break;
case 'L': st = TypeCompLoop; break;
case 'f':
{
switch(*optarg)
{
case 'm': fmt = Timer::MSeconds; break;
case 'c': fmt = Timer::HSeconds; break;
case 's': fmt = Timer::Seconds; break;
case 'a':
default: fmt = Timer::Automatic; break;
}
break;
}
case 'h': usage_and_exit(0);
case '?':
default: usage_and_exit(1);
}
}
if (argc-optind < 2)
usage_and_exit(1);
// # iterations
{
std::stringstream str; str << argv[optind]; str >> n;
}
// input file
ifname = argv[++optind];
// output file, if provided
if ( ++optind < argc )
ofname = argv[optind];
// --------------------
if ( compare_all )
{
int rc;
rc = mainT<Sqrt3> ( n, ifname, "", fmt );
rc += mainT<Loop> ( n, ifname, "", fmt );
rc += mainT<CompositeSqrt3>( n, ifname, "", fmt );
rc += mainT<CompositeLoop> ( n, ifname, "", fmt );
if (rc)
return rc;
std::cout << std::endl;
std::map< std::string, double >::iterator it;
std::cout << "Timings:\n";
for(it = timings.begin();it!=timings.end();++it)
std::cout << it->first << ": " << Timer::as_string(it->second)
<< std::endl;
std::cout << std::endl;
std::cout << "Ratio composite/native algorithm:\n";
std::cout << "sqrt(3): "
<< timings["Uniform Composite Sqrt3"]/timings["Uniform Sqrt3"]
<< std::endl
<< "loop : "
<< timings["Uniform Composite Loop"]/timings["Uniform Loop"]
<< std::endl;
return 0;
}
else switch(st)
{
case TypeSqrt3:
return mainT<Sqrt3>( n, ifname, ofname, fmt );
case TypeLoop:
return mainT<Loop>( n, ifname, ofname, fmt );
case TypeCompSqrt3:
return mainT<CompositeSqrt3>( n, ifname, ofname, fmt );
case TypeCompLoop:
return mainT<CompositeLoop> ( n, ifname, ofname, fmt );
}
return 1;
}
// ----------------------------------------------------------------------------
void usage_and_exit(int _xcode)
{
std::cout << "Usage: subdivide [Subdivider Type] #Iterations Input [Output].\n";
std::cout << std::endl;
std::cout << "Subdivider Type\n"
<< std::endl
<< " -l\tLoop\n"
<< " -L\tComposite Loop\n"
<< " -s\tSqrt3\n"
<< " -S\tComposite Sqrt3\n"
<< std::endl;
exit(_xcode);
}