SmootherPlugin.cc 8.34 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
//=============================================================================
//
//                               OpenFlipper
//        Copyright (C) 2008 by Computer Graphics Group, RWTH Aachen
//                           www.openflipper.org
//
//-----------------------------------------------------------------------------
//
//                                License
//
//  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.
// 
//  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 Lesser General Public License
//  along with OpenFlipper.  If not, see <http://www.gnu.org/licenses/>.
//
//-----------------------------------------------------------------------------
//
//   $Revision$
//   $Author$
//   $Date$
//
//=============================================================================

Jan Möbius's avatar
 
Jan Möbius committed
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
//=============================================================================
//
//  CLASS SmootherPlugin - IMPLEMENTATION
//
//=============================================================================


//== INCLUDES =================================================================

#include <QtGui>

#include "SmootherPlugin.hh"

#include <iostream>
#include <ACG/GL/GLState.hh>
#include <QStringList>
#include <ACG/Scenegraph/ManipulatorNode.hh>

#include <OpenFlipper/BasePlugin/PluginFunctions.hh>

#include "SmootherObject.hh"

#define SMOOTHER "SmootherData"

//== IMPLEMENTATION ==========================================================


Dirk Wilden's avatar
Dirk Wilden committed
59 60 61 62 63
/** \brief Initialize the toolbox
 * 
 * @param _widget reference to the toolbox
 * @return was the toolbox successfully created?
 */
Jan Möbius's avatar
 
Jan Möbius committed
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
bool
SmootherPlugin::
initializeToolbox(QWidget*& _widget)
{
  tool_ = new SmootherToolbarWidget();
  _widget = tool_;
  QSize size(100, 100);
  tool_->resize(size);

  // connect signals->slots
	connect(tool_->pB_smooth,SIGNAL(clicked() ),this,SLOT(slot_smooth()));

  return true;
}

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

Dirk Wilden's avatar
Dirk Wilden committed
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
/** \brief Set the scripting slot descriptions
 * 
 */
void
SmootherPlugin::pluginsInitialized(){

  emit setSlotDescription("smooth(int,int,QString,QString,double)", "Smooth an object",
                          QString("object_id,iterations,direction,continuity,maxDistance").split(","),
                          QString("id of an object, number of smoothing iterations, Smoothing direction. (tangential;normal;tangential+normal), Continuity. (C1 or C2), max distance the smoothed mesh is allowed to differ from the original").split(","));

  emit setSlotDescription("smooth(int,int,QString,QString)", "Smooth an object",
                          QString("object_id,iterations,direction,continuity").split(","),
                          QString("id of an object, number of smoothing iterations, Smoothing direction. (tangential;normal;tangential+normal), Continuity. (C1 or C2)").split(","));

}


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

/** \brief Smooth all target objects 
 * 
 * Parameters for the smoothing are retrieved from the toolbox
 *
 */
Jan Möbius's avatar
 
Jan Möbius committed
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 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 168 169
void
SmootherPlugin::
slot_smooth()
{
  for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::TARGET_OBJECTS,DATA_TRIANGLE_MESH) ;
        o_it != PluginFunctions::objectsEnd(); ++o_it)  {

    TriMeshObject* object = PluginFunctions::triMeshObject(*o_it);

    if ( object == 0 ) {
      emit log(LOGWARN , "Unable to get object ( Only Triangle Meshes supported)");
      continue;
    }

    SmootherObject* data = dynamic_cast< SmootherObject* > ( o_it->objectData(SMOOTHER) );

    if (data == 0){
      TriMesh* mesh = PluginFunctions::triMesh(*o_it);
      data = new SmootherObject(mesh);
      o_it->setObjectData(SMOOTHER, data);
    }


    OpenMesh::Smoother::SmootherT< TriMesh >::Component component = OpenMesh::Smoother::SmootherT< TriMesh >::Tangential_and_Normal;
    if( tool_->rbTangential_and_Normal->isChecked() )
      component = OpenMesh::Smoother::SmootherT< TriMesh >::Tangential_and_Normal;
    else if( tool_->rbNormal->isChecked() )
      component = OpenMesh::Smoother::SmootherT< TriMesh >::Normal;
    else if( tool_->rbTangential->isChecked() )
      component = OpenMesh::Smoother::SmootherT< TriMesh >::Tangential;

    OpenMesh::Smoother::SmootherT< TriMesh >::Continuity continuity = OpenMesh::Smoother::SmootherT< TriMesh >::C0;
    if( tool_->rB_c0->isChecked() )
      continuity = OpenMesh::Smoother::SmootherT< TriMesh >::C0;
    else if( tool_->rB_c1->isChecked() )
      continuity = OpenMesh::Smoother::SmootherT< TriMesh >::C1;

    // Read maximum distance Error from lineEdit
    if ( tool_->cbDistance->isChecked() ) {
      QString value;
      value = tool_->distance->text();
      bool ok = false;

      double absoluteError = value.toDouble(&ok);

      if ( ok )
        data->smoother->set_absolute_local_error( absoluteError );
      else
        emit log(LOGWARN , "Unable to read distance error from LineEdit");
    }

    data->smoother->initialize(component,continuity);

    data->smoother->smooth( tool_->sB_iter->value() );

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

    if (mesh != 0)
      mesh->garbage_collection();

    emit updatedObject( o_it->id() );
  }

}

Dirk Wilden's avatar
Dirk Wilden committed
170 171 172 173 174 175 176 177 178 179 180 181 182

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

/** \brief  Smooth object
*
* @param _objectId Object to smooth
* @param _iterations Number of smoothing iterations
* @param _direction tangential/normal/tangential+normal
* @param _continuity C0/C1
* @param _maxDistance the maximum distance that the smoothed mesh is allowed to differ from the original mesh
*/
void SmootherPlugin::smooth(int _objectId , int _iterations , QString _direction , QString _continuity, double _maxDistance) {

Jan Möbius's avatar
Jan Möbius committed
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
  BaseObjectData* baseObjectData;
  if ( ! PluginFunctions::getObject(_objectId,baseObjectData) ) {
    emit log(LOGERR,"Unable to get Object");
    return;
  }

  if ( baseObjectData->dataType() == DATA_TRIANGLE_MESH ) {
    TriMeshObject* object = PluginFunctions::triMeshObject(baseObjectData);

    if ( object == 0 ) {
      emit log(LOGWARN , "Unable to get object ( Only Triangle Meshes supported)");
      return;
    }

    SmootherObject* data = dynamic_cast< SmootherObject* > ( object->objectData(SMOOTHER) );

    if (data == 0){
      TriMesh* mesh = PluginFunctions::triMesh(object);
      data = new SmootherObject(mesh);
      object->setObjectData(SMOOTHER, data);
    }

    OpenMesh::Smoother::SmootherT< TriMesh >::Component component = OpenMesh::Smoother::SmootherT< TriMesh >::Tangential_and_Normal;
    bool tangential = _direction.contains("tangential");
    bool normal     = _direction.contains("normal");

    if ( tangential && normal )
      component = OpenMesh::Smoother::SmootherT< TriMesh >::Tangential_and_Normal;
    else if ( tangential )
      component = OpenMesh::Smoother::SmootherT< TriMesh >::Tangential;
    else
      component = OpenMesh::Smoother::SmootherT< TriMesh >::Normal;

    OpenMesh::Smoother::SmootherT< TriMesh >::Continuity continuity = OpenMesh::Smoother::SmootherT< TriMesh >::C0;
    bool c0 = _continuity.contains("C0");
    bool c1 = _continuity.contains("C1");

    if ( c0 && c1 )
      std::cerr << "Continuity C0 + C1 ? Using C1" << std::endl;
    if( c1 )
      continuity = OpenMesh::Smoother::SmootherT< TriMesh >::C1;
    else if( c0 )
      continuity = OpenMesh::Smoother::SmootherT< TriMesh >::C0;

Dirk Wilden's avatar
Dirk Wilden committed
227 228 229 230 231
    if ( _maxDistance > 0.0)
      data->smoother->set_absolute_local_error( _maxDistance );
    else
      data->smoother->set_absolute_local_error( FLT_MAX );

Jan Möbius's avatar
Jan Möbius committed
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
    data->smoother->initialize(component,continuity);

    data->smoother->smooth( _iterations );
    TriMesh* mesh = PluginFunctions::triMesh(object);

    if (mesh != 0) {
      mesh->garbage_collection();
      mesh->update_normals();
    }

    emit updatedObject( object->id() );


  } else {
    emit log(LOGERR,"Unsupported object type for smoother");
    return;
  }

}

Jan Möbius's avatar
 
Jan Möbius committed
252 253 254 255
//-----------------------------------------------------------------------------

Q_EXPORT_PLUGIN2( SmootherPlugin , SmootherPlugin );