SubdividerPlugin.cc 7.27 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
/*===========================================================================*\
*                                                                            *
*                              OpenFlipper                                   *
*      Copyright (C) 2001-2010 by Computer Graphics Group, RWTH Aachen       *
*                           www.openflipper.org                              *
*                                                                            *
*--------------------------------------------------------------------------- *
*  This file is part of OpenFlipper.                                         *
*                                                                            *
*  OpenFlipper 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, either version 3 of            *
*  the License, or (at your option) any later version with the               *
*  following exceptions:                                                     *
*                                                                            *
*  If other files instantiate templates or use macros                        *
*  or inline functions from this file, or you compile this file and          *
*  link it with other files to produce an executable, this file does         *
*  not by itself cause the resulting executable to be covered by the         *
*  GNU Lesser General Public License. This exception does not however        *
*  invalidate any other reasons why the executable file might be             *
*  covered by the GNU Lesser General Public License.                         *
*                                                                            *
*  OpenFlipper 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 LesserGeneral Public           *
*  License along with OpenFlipper. If not,                                   *
*  see <http://www.gnu.org/licenses/>.                                       *
*                                                                            *
\*===========================================================================*/

/*===========================================================================*\
*                                                                            *
*   $Revision$                                                       *
*   $LastChangedBy$                                                *
*   $Date$                     *
*                                                                            *
\*===========================================================================*/

Jan Möbius's avatar
 
Jan Möbius committed
43 44 45 46 47 48 49 50 51 52 53 54 55

#include <QtGui>

#include "SubdividerPlugin.hh"

#include <iostream>
#include <algorithm>

#include <OpenFlipper/BasePlugin/PluginFunctions.hh>
#include <OpenFlipper/common/GlobalOptions.hh>
#include <OpenFlipper/INIFile/INIFile.hh>
#include <ACG/Scenegraph/PointNode.hh>

56 57 58 59 60
#include <OpenMesh/Tools/Subdivider/Uniform/LoopT.hh>
#include <OpenMesh/Tools/Subdivider/Uniform/Sqrt3T.hh>
#include <OpenMesh/Tools/Subdivider/Uniform/Sqrt3InterpolatingSubdividerLabsikGreinerT.hh>
#include <OpenMesh/Tools/Subdivider/Uniform/ModifiedButterFlyT.hh>

Jan Möbius's avatar
 
Jan Möbius committed
61 62 63 64 65 66 67
void SubdividerPlugin::initializePlugin() 
{
  if ( OpenFlipper::Options::gui() ) {
    tool_ = new subdividerToolbarWidget();
    QSize size(300, 300);
    tool_->resize(size);
    
68
    connect(tool_->subdivide_uniform_toolButton,    SIGNAL( clicked() ),  this, SLOT( slotSubdivideUniform() ) );
Jan Möbius's avatar
 
Jan Möbius committed
69
    
Dirk Wilden's avatar
Dirk Wilden committed
70 71
    toolIcon_ = new QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"subdivider.png");
    emit addToolbox( tr("Subdivider") , tool_, toolIcon_ );
Jan Möbius's avatar
 
Jan Möbius committed
72 73 74 75 76
  }
}

void SubdividerPlugin::pluginsInitialized() 
{
77 78
  emit setSlotDescription("subdivide(int,QString,int)", "Smooth a triangular mesh",
                          QString("object_id,algorithm,iterations").split(","),
Jan Möbius's avatar
Jan Möbius committed
79
                          QString("id of an object, algorithm to use (loop | sqrt3 | interpolating_sqrt3 | modifiedButterfly ), number of iterations").split(","));
Jan Möbius's avatar
 
Jan Möbius committed
80 81 82 83
}

//-----------------------------------------------------------------------------

84
void SubdividerPlugin::slotSubdivideUniform()
Jan Möbius's avatar
 
Jan Möbius committed
85 86 87 88 89 90
{
  std::vector< int > ids;
  if ( PluginFunctions::getTargetIdentifiers( ids ) )
  {
    for (unsigned int i = 0; i < ids.size(); ++i)
    {
91
     
Jan Möbius's avatar
 
Jan Möbius committed
92 93
      if(tool_->loop_radioButton->isChecked())
      {
94
        subdivide(ids[i],"loop",tool_->subdivision_steps_spinBox->value());
Jan Möbius's avatar
 
Jan Möbius committed
95
      }
96
      else if ( tool_->sqrt3_radioButton->isChecked() )
Jan Möbius's avatar
 
Jan Möbius committed
97
      {
98
        subdivide(ids[i],"sqrt3",tool_->subdivision_steps_spinBox->value());
Jan Möbius's avatar
 
Jan Möbius committed
99
      }
100 101
      else if ( tool_->LabsikGreiner_radioButton->isChecked()  )
      {
Jan Möbius's avatar
Jan Möbius committed
102
        subdivide(ids[i],"interpolating_sqrt3",tool_->subdivision_steps_spinBox->value());
103 104 105
      }
      else if ( tool_->modifiedButterfly_radioButton->isChecked()  )
      {
106
        subdivide(ids[i],"modifiedButterfly",tool_->subdivision_steps_spinBox->value());
107
      }
108 109 110 111
    }
  }
  emit updateView();
}
112

113
//-----------------------------------------------------------------------------
Jan Möbius's avatar
 
Jan Möbius committed
114

115 116 117 118 119 120 121 122 123 124 125
void SubdividerPlugin::subdivide(int _objectId, QString _algorithm , int _steps) {

  BaseObjectData* object;
  if(!test_trimesh_object(_objectId , object))
    return;

  TriMesh* mesh = PluginFunctions::triMesh(object);

  if(_algorithm.contains("loop",Qt::CaseInsensitive))
  {
    OpenMesh::Subdivider::Uniform::LoopT<TriMesh,double> subdivider;
Jan Möbius's avatar
 
Jan Möbius committed
126
    
127 128 129
    subdivider.attach(*mesh);
    subdivider(_steps);
    subdivider.detach();
Jan Möbius's avatar
 
Jan Möbius committed
130
  }
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
  else if ( _algorithm.contains("sqrt3",Qt::CaseInsensitive) )
  {
    OpenMesh::Subdivider::Uniform::Sqrt3T<TriMesh,double> subdivider;
    
    subdivider.attach(*mesh);
    subdivider(_steps);
    subdivider.detach();
  }
  else if ( _algorithm.contains("interpolating_sqrt(3)",Qt::CaseInsensitive)  )
  {
    OpenMesh::Subdivider::Uniform::InterpolatingSqrt3LGT<TriMesh,double> subdivider;
    
    subdivider.attach(*mesh);
    subdivider(_steps);
    subdivider.detach();
  }
  else if ( _algorithm.contains("modifiedButterfly",Qt::CaseInsensitive)  )
  {
    OpenMesh::Subdivider::Uniform::ModifiedButterflyT<TriMesh,double> subdivider;
    
    subdivider.attach(*mesh);
    subdivider(_steps);
    subdivider.detach();
  }


  mesh->update_face_normals();
  mesh->update_vertex_normals();
  TriMeshObject* tmo = PluginFunctions::triMeshObject(object);
  tmo->update();

  // Create backup
  emit createBackup(object->id(), "Subdivider");
  
  // Geometry and topology changed!
  emit updatedObject(object->id(), UPDATE_ALL);

Jan Möbius's avatar
 
Jan Möbius committed
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
}


//-----------------------------------------------------------------------------


bool SubdividerPlugin::test_trimesh_object(int _identifier, BaseObjectData*& _object)
{
  if ( _identifier == -1)
    return false;

  if (! PluginFunctions::getObject(_identifier,_object) ) 
    return false;  

  if (!_object->dataType(DATA_TRIANGLE_MESH) )
    return false;
  return true;
}

187
Q_EXPORT_PLUGIN2( subdividerplugin , SubdividerPlugin );