SmootherPlugin.cc 12.3 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

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
71
SmootherPlugin::
Jan Möbius's avatar
Jan Möbius committed
72
initializePlugin()
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()));
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_ );
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
 *
 */
111 112 113 114
void
SmootherPlugin::
slot_smooth()
{
Dirk Wilden's avatar
Dirk Wilden committed
115 116
  bool found = false;
  
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 (";
  
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;

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

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

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

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

159
    OpenMesh::Smoother::SmootherT< TriMesh >::Continuity continuity = OpenMesh::Smoother::SmootherT< TriMesh >::C0;
160
    if( tool_->rB_c0->isChecked() ) {
161
      continuity = OpenMesh::Smoother::SmootherT< TriMesh >::C0;
162 163
      jobDescription += "C0";
    } else if( tool_->rB_c1->isChecked() ) {
164
      continuity = OpenMesh::Smoother::SmootherT< TriMesh >::C1;
165 166
      jobDescription += "C1";
    }
167 168 169
    
    // Set perObjectData
    data->continuity(continuity);
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 {
183
        emit log(LOGWARN , "Unable to read distance error from LineEdit");
184
      }
185 186
      
      jobDescription += ",max_error: " + QString::number(absoluteError);
187
    }
188
    
189 190 191 192 193
    // Set perObjectData
    data->features(tool_->respectFeatures->isChecked());
    data->iterations(tool_->sB_iter->value());
    
    // Initialize smoother
194 195 196 197 198 199 200

    if(tool_->cbReinitialize->isChecked() || !data->initialized())
    {
         smoother.initialize(component,continuity );
         data->initialized(true);
    }

201
    smoother.skip_features(data->features());
202

203
    smoother.smooth( data->iterations() );
204 205
    
    jobDescription +=  ") " + QString::number(tool_->sB_iter->value()) + " iterations";
206 207

    if (mesh != 0)
208 209
//      mesh->garbage_collection();
      mesh->update_normals();
210

211
    emit updatedObject( o_it->id(), UPDATE_GEOMETRY );
Dirk Wilden's avatar
Dirk Wilden committed
212
    emit createBackup(o_it->id(), "Smoothing", UPDATE_GEOMETRY );
213 214
  }

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

218 219
}

Dirk Wilden's avatar
Dirk Wilden committed
220 221 222 223 224 225 226 227 228 229 230 231 232

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

/** \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
233 234 235 236 237 238 239 240 241
  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);

242 243
    QString jobDescription = "Smoothed (";
    
Jan Möbius's avatar
Jan Möbius committed
244 245 246 247 248 249 250
    if ( object == 0 ) {
      emit log(LOGWARN , "Unable to get object ( Only Triangle Meshes supported)");
      return;
    }

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

251 252 253
    // Get triangle mesh
    TriMesh* mesh = PluginFunctions::triMesh(object);
    
Jan Möbius's avatar
Jan Möbius committed
254
    if (data == 0){
255
      data = new SmootherObject();
Jan Möbius's avatar
Jan Möbius committed
256 257
      object->setObjectData(SMOOTHER, data);
    }
258 259
    
    SmootherType smoother(*mesh);
Jan Möbius's avatar
Jan Möbius committed
260 261 262 263 264

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

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

    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;
285
    if( c1 ) {
Jan Möbius's avatar
Jan Möbius committed
286
      continuity = OpenMesh::Smoother::SmootherT< TriMesh >::C1;
287 288
      jobDescription += "C1";
    } else if( c0 ) {
Jan Möbius's avatar
Jan Möbius committed
289
      continuity = OpenMesh::Smoother::SmootherT< TriMesh >::C0;
290 291
      jobDescription += "C0";
    }
292 293 294
    
    // Set perObjectData
    data->continuity(continuity);
Jan Möbius's avatar
Jan Möbius committed
295

296
    if ( _maxDistance > 0.0) {
297 298 299
      // Set perObjectData
      data->distance(_maxDistance);
      smoother.set_absolute_local_error( _maxDistance );
300
      jobDescription += ",max_error: " + QString::number(_maxDistance);
301 302 303 304 305
    } else {
      // Set perObjectData
      data->distance( FLT_MAX );
      smoother.set_absolute_local_error( FLT_MAX );
    }
Dirk Wilden's avatar
Dirk Wilden committed
306

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

309
    smoother.smooth( _iterations );
Jan Möbius's avatar
Jan Möbius committed
310

311 312
    jobDescription +=  ") " + QString::number(_iterations) + " iterations";
    
Jan Möbius's avatar
Jan Möbius committed
313 314 315 316 317
    if (mesh != 0) {
      mesh->garbage_collection();
      mesh->update_normals();
    }

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

320
    // Create backup
Dirk Wilden's avatar
Dirk Wilden committed
321
    emit createBackup(object->id(),"Smoothing", UPDATE_GEOMETRY );
322 323 324
    
    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
325 326 327 328 329 330 331 332

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

}

333 334 335 336
//-----------------------------------------------------------------------------

Q_EXPORT_PLUGIN2( SmootherPlugin , SmootherPlugin );