SmootherPlugin.cc 12.1 KB
Newer Older
1
/*===========================================================================*\
Jan Möbius's avatar
Jan Möbius committed
2 3
*                                                                            *
*                              OpenFlipper                                   *
Jan Möbius's avatar
Jan Möbius committed
4
*      Copyright (C) 2001-2011 by Computer Graphics Group, RWTH Aachen       *
Jan Möbius's avatar
Jan Möbius committed
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
*                           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/>.                                       *
*                                                                            *
33 34 35
\*===========================================================================*/

/*===========================================================================*\
Jan Möbius's avatar
Jan Möbius committed
36 37 38 39 40
*                                                                            *
*   $Revision$                                                       *
*   $LastChangedBy$                                                *
*   $Date$                     *
*                                                                            *
41
\*===========================================================================*/
Jan Möbius's avatar
Jan Möbius committed
42

Jan Möbius's avatar
 
Jan Möbius committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
//=============================================================================
//
//  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 ==========================================================


Jan Möbius's avatar
Jan Möbius committed
70
void
Jan Möbius's avatar
 
Jan Möbius committed
71
SmootherPlugin::
Jan Möbius's avatar
Jan Möbius committed
72
initializePlugin()
Jan Möbius's avatar
 
Jan Möbius committed
73 74 75 76 77 78
{
  tool_ = new SmootherToolbarWidget();
  QSize size(100, 100);
  tool_->resize(size);

  // connect signals->slots
Jan Möbius's avatar
Jan Möbius committed
79
  connect(tool_->pB_smooth,SIGNAL(clicked() ),this,SLOT(slot_smooth()));
Jan Möbius's avatar
 
Jan Möbius committed
80

Dirk Wilden's avatar
Dirk Wilden committed
81 82
  toolIcon_ = new QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"smoother2.png");
  emit addToolbox( tr("Smoother") , tool_, toolIcon_ );
Jan Möbius's avatar
 
Jan Möbius committed
83 84 85 86
}

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

Dirk Wilden's avatar
Dirk Wilden committed
87
/** \brief Set the scripting slot descriptions
88
 *
Dirk Wilden's avatar
Dirk Wilden committed
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
 */
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(","));

}


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

106 107
/** \brief Smooth all target objects
 *
Dirk Wilden's avatar
Dirk Wilden committed
108 109 110
 * Parameters for the smoothing are retrieved from the toolbox
 *
 */
Jan Möbius's avatar
 
Jan Möbius committed
111 112 113 114
void
SmootherPlugin::
slot_smooth()
{
Dirk Wilden's avatar
Dirk Wilden committed
115 116
  bool found = false;
  
Jan Möbius's avatar
 
Jan Möbius committed
117 118 119
  for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::TARGET_OBJECTS,DATA_TRIANGLE_MESH) ;
        o_it != PluginFunctions::objectsEnd(); ++o_it)  {

120 121
    QString jobDescription = "Smoothed (";
  
Jan Möbius's avatar
 
Jan Möbius committed
122 123 124 125 126 127 128
    TriMeshObject* object = PluginFunctions::triMeshObject(*o_it);

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

Dirk Wilden's avatar
Dirk Wilden committed
129 130
    found = true;

Jan Möbius's avatar
 
Jan Möbius committed
131 132
    SmootherObject* data = dynamic_cast< SmootherObject* > ( o_it->objectData(SMOOTHER) );

133 134 135
    // Get triangle mesh
    TriMesh* mesh = PluginFunctions::triMesh(*o_it);
    
Jan Möbius's avatar
 
Jan Möbius committed
136
    if (data == 0){
137
      data = new SmootherObject();
Jan Möbius's avatar
 
Jan Möbius committed
138 139
      o_it->setObjectData(SMOOTHER, data);
    }
140 141 142
    
    // Create smoother
    SmootherType smoother(*mesh);
Jan Möbius's avatar
 
Jan Möbius committed
143 144

    OpenMesh::Smoother::SmootherT< TriMesh >::Component component = OpenMesh::Smoother::SmootherT< TriMesh >::Tangential_and_Normal;
145
    if( tool_->rbTangential_and_Normal->isChecked() ) {
Jan Möbius's avatar
 
Jan Möbius committed
146
      component = OpenMesh::Smoother::SmootherT< TriMesh >::Tangential_and_Normal;
147 148
      jobDescription += "tangential and normal,";
    } else if( tool_->rbNormal->isChecked() ) {
Jan Möbius's avatar
 
Jan Möbius committed
149
      component = OpenMesh::Smoother::SmootherT< TriMesh >::Normal;
150 151
      jobDescription += "normal,";
    } else if( tool_->rbTangential->isChecked() ) {
Jan Möbius's avatar
 
Jan Möbius committed
152
      component = OpenMesh::Smoother::SmootherT< TriMesh >::Tangential;
153 154
      jobDescription += "tangential,";
    }
Jan Möbius's avatar
 
Jan Möbius committed
155

156 157 158
    // Set perObjectData
    data->component(component);

Jan Möbius's avatar
 
Jan Möbius committed
159
    OpenMesh::Smoother::SmootherT< TriMesh >::Continuity continuity = OpenMesh::Smoother::SmootherT< TriMesh >::C0;
160
    if( tool_->rB_c0->isChecked() ) {
Jan Möbius's avatar
 
Jan Möbius committed
161
      continuity = OpenMesh::Smoother::SmootherT< TriMesh >::C0;
162 163
      jobDescription += "C0";
    } else if( tool_->rB_c1->isChecked() ) {
Jan Möbius's avatar
 
Jan Möbius committed
164
      continuity = OpenMesh::Smoother::SmootherT< TriMesh >::C1;
165 166
      jobDescription += "C1";
    }
167 168 169
    
    // Set perObjectData
    data->continuity(continuity);
Jan Möbius's avatar
 
Jan Möbius committed
170 171 172 173 174 175 176 177 178

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

      double absoluteError = value.toDouble(&ok);

179 180 181 182
      if ( ok ) {
        data->distance(absoluteError);
        smoother.set_absolute_local_error( absoluteError );
      } else {
Jan Möbius's avatar
 
Jan Möbius committed
183
        emit log(LOGWARN , "Unable to read distance error from LineEdit");
184
      }
185 186
      
      jobDescription += ",max_error: " + QString::number(absoluteError);
Jan Möbius's avatar
 
Jan Möbius committed
187
    }
188
    
189 190 191 192 193 194 195
    // Set perObjectData
    data->features(tool_->respectFeatures->isChecked());
    data->iterations(tool_->sB_iter->value());
    
    // Initialize smoother
    smoother.initialize(component,continuity );
    smoother.skip_features(data->features());
Jan Möbius's avatar
 
Jan Möbius committed
196

197
    smoother.smooth( data->iterations() );
198 199
    
    jobDescription +=  ") " + QString::number(tool_->sB_iter->value()) + " iterations";
Jan Möbius's avatar
 
Jan Möbius committed
200 201 202 203

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

204
    emit updatedObject( o_it->id(), UPDATE_GEOMETRY );
205 206
    
    emit createBackup(o_it->id(),jobDescription );
Jan Möbius's avatar
 
Jan Möbius committed
207 208
  }

Dirk Wilden's avatar
Dirk Wilden committed
209 210 211
  if ( !found )
    emit log(LOGERR , tr("Unable to smooth. No triangle mesh selected as target!") );

Jan Möbius's avatar
 
Jan Möbius committed
212 213
}

Dirk Wilden's avatar
Dirk Wilden committed
214 215 216 217 218 219 220 221 222 223 224 225 226

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

/** \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
227 228 229 230 231 232 233 234 235
  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);

236 237
    QString jobDescription = "Smoothed (";
    
Jan Möbius's avatar
Jan Möbius committed
238 239 240 241 242 243 244
    if ( object == 0 ) {
      emit log(LOGWARN , "Unable to get object ( Only Triangle Meshes supported)");
      return;
    }

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

245 246 247
    // Get triangle mesh
    TriMesh* mesh = PluginFunctions::triMesh(object);
    
Jan Möbius's avatar
Jan Möbius committed
248
    if (data == 0){
249
      data = new SmootherObject();
Jan Möbius's avatar
Jan Möbius committed
250 251
      object->setObjectData(SMOOTHER, data);
    }
252 253
    
    SmootherType smoother(*mesh);
Jan Möbius's avatar
Jan Möbius committed
254 255 256 257 258

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

259
    if ( tangential && normal ) {
Jan Möbius's avatar
Jan Möbius committed
260
      component = OpenMesh::Smoother::SmootherT< TriMesh >::Tangential_and_Normal;
261 262
      jobDescription += "tangential and normal,";
    } else if ( tangential ) {
Jan Möbius's avatar
Jan Möbius committed
263
      component = OpenMesh::Smoother::SmootherT< TriMesh >::Tangential;
264 265
      jobDescription += "normal,";
    } else {
Jan Möbius's avatar
Jan Möbius committed
266
      component = OpenMesh::Smoother::SmootherT< TriMesh >::Normal;
267 268 269
      jobDescription += "tangential,";
    }
    
270 271
    // Set perObjectData
    data->component(component);
Jan Möbius's avatar
Jan Möbius committed
272 273 274 275 276 277 278

    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;
279
    if( c1 ) {
Jan Möbius's avatar
Jan Möbius committed
280
      continuity = OpenMesh::Smoother::SmootherT< TriMesh >::C1;
281 282
      jobDescription += "C1";
    } else if( c0 ) {
Jan Möbius's avatar
Jan Möbius committed
283
      continuity = OpenMesh::Smoother::SmootherT< TriMesh >::C0;
284 285
      jobDescription += "C0";
    }
286 287 288
    
    // Set perObjectData
    data->continuity(continuity);
Jan Möbius's avatar
Jan Möbius committed
289

290
    if ( _maxDistance > 0.0) {
291 292 293
      // Set perObjectData
      data->distance(_maxDistance);
      smoother.set_absolute_local_error( _maxDistance );
294
      jobDescription += ",max_error: " + QString::number(_maxDistance);
295 296 297 298 299
    } else {
      // Set perObjectData
      data->distance( FLT_MAX );
      smoother.set_absolute_local_error( FLT_MAX );
    }
Dirk Wilden's avatar
Dirk Wilden committed
300

301
    smoother.initialize(component,continuity);
Jan Möbius's avatar
Jan Möbius committed
302

303
    smoother.smooth( _iterations );
Jan Möbius's avatar
Jan Möbius committed
304

305 306
    jobDescription +=  ") " + QString::number(_iterations) + " iterations";
    
Jan Möbius's avatar
Jan Möbius committed
307 308 309 310 311
    if (mesh != 0) {
      mesh->garbage_collection();
      mesh->update_normals();
    }

312
    emit updatedObject( object->id(), UPDATE_GEOMETRY );
Jan Möbius's avatar
Jan Möbius committed
313

314
    // Create backup
315
    emit createBackup(object->id(),jobDescription );
316 317 318
    
    emit scriptInfo(tr("smooth(%1, %2, %3, %4, %5)").arg(QString::number(_objectId), QString::number(_iterations),
                                                    _direction, _continuity, QString::number(_maxDistance)));
Jan Möbius's avatar
Jan Möbius committed
319 320 321 322 323 324 325 326

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

}

Jan Möbius's avatar
 
Jan Möbius committed
327 328 329 330
//-----------------------------------------------------------------------------

Q_EXPORT_PLUGIN2( SmootherPlugin , SmootherPlugin );