DecimaterPlugin.cc 26.2 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
/*===========================================================================*\
*                                                                            *
*                              OpenFlipper                                   *
*      Copyright (C) 2001-2011 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/>.                                       *
*                                                                            *
\*===========================================================================*/

/*===========================================================================*\
*                                                                            *
Jan Möbius's avatar
Jan Möbius committed
37 38 39
*   $Revision: 11950 $                                                       *
*   $LastChangedBy: moebius $                                                *
*   $Date: 2011-07-07 10:01:14 +0200 (Do, 07 Jul 2011) $                     *
Jan Möbius's avatar
 
Jan Möbius committed
40 41 42 43
*                                                                            *
\*===========================================================================*/


Jan Möbius's avatar
Jan Möbius committed
44 45 46 47 48
//=============================================================================
//
//  CLASS DecimaterPlugin - IMPLEMENTATION
//
//=============================================================================
Jan Möbius's avatar
 
Jan Möbius committed
49 50


Jan Möbius's avatar
Jan Möbius committed
51
//== INCLUDES =================================================================
Jan Möbius's avatar
 
Jan Möbius committed
52

Jan Möbius's avatar
Jan Möbius committed
53
#include <QtGui>
Jan Möbius's avatar
 
Jan Möbius committed
54

Jan Möbius's avatar
Jan Möbius committed
55
#include "DecimaterPlugin.hh"
Jan Möbius's avatar
 
Jan Möbius committed
56

Jan Möbius's avatar
Jan Möbius committed
57 58 59 60
#include <iostream>
#include <ACG/GL/GLState.hh>
#include <QStringList>
#include <ACG/Scenegraph/ManipulatorNode.hh>
Jan Möbius's avatar
 
Jan Möbius committed
61

Jan Möbius's avatar
Jan Möbius committed
62
#include <OpenFlipper/BasePlugin/PluginFunctions.hh>
Jan Möbius's avatar
 
Jan Möbius committed
63

Jan Möbius's avatar
Jan Möbius committed
64
#define DECIMATER "DecimaterData"
Jan Möbius's avatar
 
Jan Möbius committed
65

Jan Möbius's avatar
Jan Möbius committed
66
//== IMPLEMENTATION ==========================================================
Jan Möbius's avatar
 
Jan Möbius committed
67

68 69 70 71 72 73 74
DecimaterPlugin::DecimaterPlugin() :
        tool_(0),
        toolIcon_(0)
{

}

Jan Möbius's avatar
Jan Möbius committed
75 76 77 78 79
void DecimaterPlugin::initializePlugin()
{
  tool_ = new DecimaterToolbarWidget();
  QSize size(100, 100);
  tool_->resize(size);
Jan Möbius's avatar
 
Jan Möbius committed
80

Jan Möbius's avatar
Jan Möbius committed
81 82
  // connect signals->slots
  connect(tool_->pbDecimate,SIGNAL(clicked() ),this,SLOT(slot_decimate()));
Jan Möbius's avatar
 
Jan Möbius committed
83

Jan Möbius's avatar
Jan Möbius committed
84 85
  connect(tool_->roundness,SIGNAL(valueChanged(double) ),this,SLOT(updateRoundness(double)) );
  connect(tool_->roundnessSlider,SIGNAL(valueChanged(int) ),this,SLOT(updateRoundness(int)) );
86 87
  connect(tool_->aspectRatio,SIGNAL(valueChanged(double) ),this,SLOT(updateAspectRatio(double)) );
  connect(tool_->aspectRatioSlider,SIGNAL(valueChanged(int) ),this,SLOT(updateAspectRatio(int)) );
Jan Möbius's avatar
Jan Möbius committed
88
  connect(tool_->distance,SIGNAL(valueChanged(double) ),this,SLOT(updateDistance()) );
89
  connect(tool_->edgeLength,SIGNAL(valueChanged(double) ),this,SLOT(updateEdgeLength()) );
Jan Möbius's avatar
Jan Möbius committed
90 91 92 93
  connect(tool_->normalDeviation,SIGNAL(valueChanged(int) ),this,SLOT(updateNormalDev()) );
  connect(tool_->normalDeviationSlider,SIGNAL(valueChanged(int) ),this,SLOT(updateNormalDev()) );
  connect(tool_->verticesCount,SIGNAL(valueChanged(int) ),this,SLOT(updateVertices()) );
  connect(tool_->verticesCountSlider,SIGNAL(valueChanged(int) ),this,SLOT(updateVertices()) );
94 95
  connect(tool_->trianglesCount,SIGNAL(valueChanged(int) ),this,SLOT(updateTriangles()) );
  connect(tool_->trianglesCountSlider,SIGNAL(valueChanged(int) ),this,SLOT(updateTriangles()) );
Jan Möbius's avatar
 
Jan Möbius committed
96

Jan Möbius's avatar
Jan Möbius committed
97 98
  // Force update if the Toolbox gets visible
  connect(tool_, SIGNAL(showing()), this, SLOT( slotUpdateNumVertices() ) );
99
  connect(tool_, SIGNAL(showing()), this, SLOT( slotUpdateNumTriangles() ) );
Jan Möbius's avatar
 
Jan Möbius committed
100

Jan Möbius's avatar
Jan Möbius committed
101 102
  toolIcon_ = new QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"decimater.png");
  emit addToolbox( tr("Decimater") , tool_, toolIcon_ );
Jan Möbius's avatar
 
Jan Möbius committed
103 104
}

Jan Möbius's avatar
Jan Möbius committed
105 106
/** \brief Initialization of the plugin when it is loaded by the core
 * 
Jan Möbius's avatar
 
Jan Möbius committed
107
 */
Jan Möbius's avatar
Jan Möbius committed
108
void DecimaterPlugin::pluginsInitialized() {
Jan Möbius's avatar
 
Jan Möbius committed
109

Jan Möbius's avatar
Jan Möbius committed
110 111
  emit setSlotDescription("decimate(int,QVariantMap)",tr("Decimate a given object"),
                          QString(tr("objectId,constraints")).split(","),
112
                          QString(tr("ID of an object; Object that can has one or more constraint properties (decimation_order,distance,edge_length,normal_deviation,roundness,aspect_ratio,independent_sets,vertices,triangles)")).split(";"));
Jan Möbius's avatar
 
Jan Möbius committed
113 114 115 116 117
}


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

Jan Möbius's avatar
Jan Möbius committed
118
/** \brief sync between values of roundness slider and spinbox in the toolbox
Jan Möbius's avatar
 
Jan Möbius committed
119
 *
Jan Möbius's avatar
Jan Möbius committed
120
 * @param _value new roundness value
Jan Möbius's avatar
 
Jan Möbius committed
121
 */
Jan Möbius's avatar
Jan Möbius committed
122 123 124 125
void DecimaterPlugin::updateRoundness(int _value)
{
  tool_->roundness->setValue( (double) _value / 100.0 );
  tool_->cbRoundness->setChecked (true);
Jan Möbius's avatar
 
Jan Möbius committed
126 127 128 129 130
}


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

Jan Möbius's avatar
Jan Möbius committed
131
/** \brief sync between values of roundness slider and spinbox in the toolbox
Jan Möbius's avatar
 
Jan Möbius committed
132
 *
Jan Möbius's avatar
Jan Möbius committed
133
 * @param _value new roundness value
Jan Möbius's avatar
 
Jan Möbius committed
134
 */
Jan Möbius's avatar
Jan Möbius committed
135 136 137 138
void DecimaterPlugin::updateRoundness(double _value)
{
  tool_->roundnessSlider->setValue( (int) (_value * 100) );
  tool_->cbRoundness->setChecked (true);
Jan Möbius's avatar
 
Jan Möbius committed
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

/** \brief sync between values of aspect ratio slider and spinbox in the toolbox
 *
 * @param _value new aspect ratio value
 */
void DecimaterPlugin::updateAspectRatio(int _value)
{
  tool_->aspectRatio->setValue( (double) _value / 100.0 );
  tool_->cbAspectRatio->setChecked (true);
}


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

/** \brief sync between values of aspect ratio slider and spinbox in the toolbox
 *
 * @param _value new aspect ratio value
 */
void DecimaterPlugin::updateAspectRatio(double _value)
{
  tool_->aspectRatioSlider->setValue( (int) (_value * 100) );
  tool_->cbAspectRatio->setChecked (true);
}

//-----------------------------------------------------------------------------
Jan Möbius's avatar
Jan Möbius committed
168
/** \brief Decimation called by toolbox
Jan Möbius's avatar
 
Jan Möbius committed
169 170
 *
 */
Jan Möbius's avatar
Jan Möbius committed
171
void DecimaterPlugin::slot_decimate()
Jan Möbius's avatar
 
Jan Möbius committed
172
{
Jan Möbius's avatar
Jan Möbius committed
173 174 175
  
  for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::TARGET_OBJECTS,DATA_TRIANGLE_MESH) ;
                                        o_it != PluginFunctions::objectsEnd(); ++o_it)  {
Jan Möbius's avatar
 
Jan Möbius committed
176

Jan Möbius's avatar
Jan Möbius committed
177 178
    //initialize
    TriMeshObject* object = PluginFunctions::triMeshObject(*o_it);
Jan Möbius's avatar
 
Jan Möbius committed
179

Jan Möbius's avatar
Jan Möbius committed
180 181
    if ( object == 0 )
      emit log(LOGWARN , tr("Unable to get object"));
Jan Möbius's avatar
 
Jan Möbius committed
182

Jan Möbius's avatar
Jan Möbius committed
183
    DecimaterInfo* decimater = dynamic_cast< DecimaterInfo* > ( o_it->objectData(DECIMATER) );
Jan Möbius's avatar
 
Jan Möbius committed
184

Jan Möbius's avatar
Jan Möbius committed
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
    TriMesh* mesh = PluginFunctions::triMesh(*o_it);
  
    if (decimater == 0){
      decimater = new DecimaterInfo();
      o_it->setObjectData(DECIMATER, decimater);
    }
    
    // constraint handles for decimation
    ModAspectRatioH     hModAspectRatio;
    ModEdgeLengthH      hModEdgeLength;
    ModHausdorffH       hModHausdorff;
    ModIndependentH     hModIndependent;
    ModNormalDeviationH hModNormalDeviation;
    ModNormalFlippingH  hModNormalFlipping;
    ModQuadricH         hModQuadric;
    ModRoundnessH       hModRoundness;
    
    // Create decimater
    DecimaterType decimater_object( *mesh );
    
    // Remove old constraints
    if(decimater->distance()) {
        decimater->removeDistanceConstraint();
        decimater_object.remove(hModHausdorff);
    }
    if(decimater->normalDeviation()) {
        decimater->removeNormalDeviationConstraint();
212 213 214 215
        decimater_object.remove(hModNormalDeviation);
    }
    if(decimater->normalFlipping()) {
        decimater->removeNormalFlippingConstraint();
Jan Möbius's avatar
Jan Möbius committed
216 217 218 219 220 221
        decimater_object.remove(hModNormalFlipping);
    }
    if(decimater->roundness()) {
        decimater->removeRoundnessConstraint();
        decimater_object.remove(hModRoundness);
    }
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
    if(decimater->aspectRatio()) {
      decimater->removeAspectRatioConstraint();
      decimater_object.remove(hModAspectRatio);
    }
    if(decimater->edgeLength()) {
      decimater->removeEdgeLengthConstraint();
      decimater_object.remove(hModEdgeLength);
    }
    if(decimater->independentSets()) {
      decimater->removeIndependentSetsConstraint();
      decimater_object.remove(hModIndependent);
    }

    // set priority module: quadric, normal deviation or edge length
    if (tool_->rbByDistance->isChecked()) {
      decimater->setDecimationOrder(DecimaterInfo::DISTANCE);
      decimater_object.add( hModQuadric );
      decimater_object.module( hModQuadric ).unset_max_err();
    } else if (tool_->rbByNormalDeviation->isChecked()) {
      decimater->setDecimationOrder(DecimaterInfo::NORMALDEV);
      decimater_object.add(hModNormalDeviation);
      decimater_object.module(hModNormalDeviation).set_binary(false);
    } else if (tool_->rbByEdgeLength->isChecked()) {
      decimater->setDecimationOrder(DecimaterInfo::EDGELENGTH);
      decimater_object.add(hModEdgeLength);
      decimater_object.module(hModEdgeLength).set_binary(false);
    }
Jan Möbius's avatar
 
Jan Möbius committed
249

250
    // and set new constraints
Jan Möbius's avatar
Jan Möbius committed
251
    if ( tool_->cbDistance->isChecked() ) {
252
      if (  decimater_object.add( hModHausdorff ) || tool_->rbConstraintsOnly->isChecked() ) {
Jan Möbius's avatar
Jan Möbius committed
253 254 255 256
          decimater->setDistanceConstraint( tool_->distance->value() );
          decimater_object.module( hModHausdorff ).set_tolerance( decimater->distanceValue() );
      }
    }
Jan Möbius's avatar
 
Jan Möbius committed
257

Jan Möbius's avatar
Jan Möbius committed
258
    if ( tool_->cbNormalDev->isChecked() ) {
259
      if (  decimater_object.add( hModNormalDeviation ) || tool_->rbConstraintsOnly->isChecked() ) {
Jan Möbius's avatar
Jan Möbius committed
260
          decimater->setNormalDeviationConstraint( tool_->normalDeviation->value() );
261 262 263 264 265 266
          decimater_object.module( hModNormalDeviation ).set_normal_deviation( decimater->normalDeviationValue() );
      }
    } else {
      if ( decimater_object.add( hModNormalFlipping ) || tool_->rbConstraintsOnly->isChecked() ) {
          decimater->setNormalFlippingConstraint();
          // decimater_object.module( hModNormalFlipping ).set_max_normal_deviation( decimater->normalDeviationValue() ); ?
Jan Möbius's avatar
Jan Möbius committed
267 268
      }
    }
Jan Möbius's avatar
 
Jan Möbius committed
269

Jan Möbius's avatar
Jan Möbius committed
270
    if ( tool_->cbRoundness->isChecked() ) {      
271
      if (  decimater_object.add( hModRoundness ) || tool_->rbConstraintsOnly->isChecked() ) {
Jan Möbius's avatar
Jan Möbius committed
272 273 274 275
          decimater->setRoundnessConstraint( tool_->roundness->value() );
          decimater_object.module( hModRoundness ).set_min_roundness( decimater->roundnessValue(), true );
      }
    }
Jan Möbius's avatar
 
Jan Möbius committed
276

277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
    if ( tool_->cbAspectRatio->isChecked() ) {
      if ( decimater_object.add( hModAspectRatio ) || tool_->rbConstraintsOnly->isChecked() ) {
          decimater->setAspectRatioConstraint( tool_->aspectRatio->value() );
          decimater_object.module( hModAspectRatio ).set_aspect_ratio( decimater->aspectRatioValue() );
      }
    }

    if ( tool_->cbEdgeLength->isChecked() ) {
      if ( decimater_object.add( hModEdgeLength ) || tool_->rbConstraintsOnly->isChecked() ) {
          decimater->setEdgeLengthConstraint( tool_->edgeLength->value() );
          decimater_object.module( hModEdgeLength ).set_edge_length( decimater->edgeLengthValue() );
      }
    }

    if ( tool_->cbIndependentSets->isChecked() ) {
      if ( decimater_object.add( hModIndependent ) || tool_->rbConstraintsOnly->isChecked() ) {
          decimater->setIndependentSetsConstraint();
      }
    }

Jan Möbius's avatar
Jan Möbius committed
297 298 299 300 301
    // Initialize the decimater
    if( ! decimater_object.initialize() ){
      emit log(LOGWARN, tr("Decimater could not be initialized"));
      continue;
    }
Jan Möbius's avatar
 
Jan Möbius committed
302

Jan Möbius's avatar
Jan Möbius committed
303
    //decimate
304
    if ( tool_->rbVertices->isChecked() )
Jan Möbius's avatar
Jan Möbius committed
305
        decimater_object.decimate_to(tool_->verticesCount->value());
306 307 308 309
    else if (tool_->rbTriangles->isChecked() )
        decimater_object.decimate_to_faces(0, tool_->trianglesCount->value());
    else // constraints only
        decimater_object.decimate_to_faces(0, 1);
Jan Möbius's avatar
Jan Möbius committed
310 311 312 313 314 315 316 317 318

    object->mesh()->garbage_collection();
    object->mesh()->update_normals();
    object->update();
    
    // Create backup
    emit createBackup(o_it->id(), "Decimation");
    emit updatedObject( o_it->id() , UPDATE_TOPOLOGY );
  }
Jan Möbius's avatar
 
Jan Möbius committed
319

Jan Möbius's avatar
Jan Möbius committed
320
  emit updateView();
Jan Möbius's avatar
 
Jan Möbius committed
321 322
}

Jan Möbius's avatar
Jan Möbius committed
323 324 325

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

Jan Möbius's avatar
Jan Möbius committed
326
/** \brief Decimation called by Scripting
Jan Möbius's avatar
 
Jan Möbius committed
327
 *
Jan Möbius's avatar
Jan Möbius committed
328 329 330
 * @param _objID id of an object
 * @param _constraints A string containing a comma separated list of constraints (distance,normal_deviation,roundness,triangles)
 * @param _values a string containing a comma separated list of constraint values suited to the _constraints parameter
Jan Möbius's avatar
 
Jan Möbius committed
331
 */
Jan Möbius's avatar
Jan Möbius committed
332
void DecimaterPlugin::decimate(int _objID, QVariantMap _constraints) {
Jan Möbius's avatar
 
Jan Möbius committed
333 334


Jan Möbius's avatar
Jan Möbius committed
335 336 337 338 339 340 341 342
  BaseObjectData* baseObjectData;
  if ( ! PluginFunctions::getObject(_objID,baseObjectData) ) {
    emit log(LOGERR,tr("Unable to get Object"));
    return;
  }

  if ( baseObjectData->dataType() == DATA_TRIANGLE_MESH ) {
    TriMeshObject* object = PluginFunctions::triMeshObject(baseObjectData);
Jan Möbius's avatar
 
Jan Möbius committed
343

Jan Möbius's avatar
Jan Möbius committed
344 345 346 347
    if ( object == 0 ) {
      emit log(LOGWARN , tr("Unable to get object ( Only Triangle Meshes supported)"));
      return;
    }
Jan Möbius's avatar
 
Jan Möbius committed
348

Jan Möbius's avatar
Jan Möbius committed
349
    DecimaterInfo* decimater = dynamic_cast< DecimaterInfo* > ( object->objectData(DECIMATER) );
Jan Möbius's avatar
 
Jan Möbius committed
350

Jan Möbius's avatar
Jan Möbius committed
351 352 353 354 355 356 357
    TriMesh* mesh = PluginFunctions::triMesh(baseObjectData);
    
    if (decimater == 0){
      decimater = new DecimaterInfo();
      object->setObjectData(DECIMATER, decimater);
    }
    
Jan Möbius's avatar
 
Jan Möbius committed
358
    // constraint handles for decimation
Jan Möbius's avatar
Jan Möbius committed
359 360 361 362
    ModAspectRatioH     hModAspectRatio;
    ModEdgeLengthH      hModEdgeLength;
    ModHausdorffH       hModHausdorff;
    ModIndependentH     hModIndependent;
Jan Möbius's avatar
 
Jan Möbius committed
363
    ModNormalDeviationH hModNormalDeviation;
Jan Möbius's avatar
Jan Möbius committed
364 365 366
    ModNormalFlippingH  hModNormalFlipping;
    ModQuadricH         hModQuadric;
    ModRoundnessH       hModRoundness;
Jan Möbius's avatar
Jan Möbius committed
367 368 369 370 371 372
    
    // Create decimater
    DecimaterType decimater_object( *mesh );

    // Remove old constraints
    if(decimater->distance()) {
373 374
      decimater->removeDistanceConstraint();
      decimater_object.remove(hModHausdorff);
Jan Möbius's avatar
Jan Möbius committed
375 376
    }
    if(decimater->normalDeviation()) {
377 378 379 380 381 382
      decimater->removeNormalDeviationConstraint();
      decimater_object.remove(hModNormalDeviation);
    }
    if(decimater->normalFlipping()) {
      decimater->removeNormalFlippingConstraint();
      decimater_object.remove(hModNormalFlipping);
Jan Möbius's avatar
Jan Möbius committed
383 384
    }
    if(decimater->roundness()) {
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
      decimater->removeRoundnessConstraint();
      decimater_object.remove(hModRoundness);
    }
    if(decimater->aspectRatio()) {
      decimater->removeAspectRatioConstraint();
      decimater_object.remove(hModAspectRatio);
    }
    if(decimater->edgeLength()) {
      decimater->removeEdgeLengthConstraint();
      decimater_object.remove(hModEdgeLength);
    }
    if(decimater->independentSets()) {
      decimater->removeIndependentSetsConstraint();
      decimater_object.remove(hModIndependent);
    }

    // set priority module: quadric, normal deviation or edge length
    if ( _constraints.contains("decimation_order") ){
      bool ok;

      int value = _constraints["decimation_order"].toInt(&ok);

      if (ok) {
        switch (value) {
        case 0:
          decimater->setDecimationOrder(DecimaterInfo::DISTANCE);
          decimater_object.add( hModQuadric );
          decimater_object.module( hModQuadric ).unset_max_err();
          break;
        case 1:
          decimater->setDecimationOrder(DecimaterInfo::NORMALDEV);
          decimater_object.add(hModNormalDeviation);
          decimater_object.module(hModNormalDeviation).set_binary(false);
          break;
        case 2:
          decimater->setDecimationOrder(DecimaterInfo::EDGELENGTH);
          decimater_object.add(hModEdgeLength);
          decimater_object.module(hModEdgeLength).set_binary(false);
          break;
        default:
          emit log(LOGERR,tr("Invalid Decimation Order"));
          return;
        }
      }
    } else {
      emit log(LOGERR,tr("No Decimation Order set"));
      return;
    }

    // stock options (triangle and vertices count) constraint
    bool verticesCount = false;
    bool trianglesCount = false;
    int vertices = 0;
    int triangles = 0;

    if ( _constraints.contains("vertices") ){

      bool ok;

      int value = _constraints["vertices"].toInt(&ok);

      if (ok){
        verticesCount = true;
        vertices = value;
      }
    } else if ( _constraints.contains("triangles") ){

      bool ok;

      int value = _constraints["triangles"].toInt(&ok);

      if (ok){
        trianglesCount = true;
        triangles = value;
      }
Jan Möbius's avatar
Jan Möbius committed
460
    }
Jan Möbius's avatar
Jan Möbius committed
461

Jan Möbius's avatar
Jan Möbius committed
462 463
    //distance constraint
    if ( _constraints.contains("distance") ){
Jan Möbius's avatar
 
Jan Möbius committed
464

Jan Möbius's avatar
Jan Möbius committed
465 466 467 468 469
      bool ok;

      double value = _constraints["distance"].toDouble(&ok);

      if (ok) {
470
        if (  decimater_object.add( hModHausdorff ) || (!verticesCount && !trianglesCount)  ) {
Jan Möbius's avatar
Jan Möbius committed
471 472 473 474
            decimater->setDistanceConstraint( value );
            decimater_object.module( hModHausdorff ).set_tolerance( decimater->distanceValue() );
        }
      }
Jan Möbius's avatar
 
Jan Möbius committed
475 476
    }

Jan Möbius's avatar
Jan Möbius committed
477 478
    //normal deviation constraint
    if ( _constraints.contains("normal_deviation") ){
Jan Möbius's avatar
 
Jan Möbius committed
479

Jan Möbius's avatar
Jan Möbius committed
480
      bool ok;
Jan Möbius's avatar
 
Jan Möbius committed
481

Jan Möbius's avatar
Jan Möbius committed
482
      int value = _constraints["normal_deviation"].toInt(&ok);
Jan Möbius's avatar
 
Jan Möbius committed
483

Jan Möbius's avatar
Jan Möbius committed
484
      if (ok) {
485
        if (  decimater_object.add( hModNormalDeviation ) || (!verticesCount && !trianglesCount)  ) {
Jan Möbius's avatar
Jan Möbius committed
486
            decimater->setNormalDeviationConstraint( value );
487
            decimater_object.module( hModNormalDeviation ).set_normal_deviation( decimater->normalDeviationValue() );
Jan Möbius's avatar
Jan Möbius committed
488 489
        }
      }
490 491 492 493 494
    } else { // flipping constraint
      if (  decimater_object.add( hModNormalFlipping ) || (!verticesCount && !trianglesCount)  ) {
        decimater->setNormalFlippingConstraint();
        // decimater_object.module( hModNormalFlipping ).set_max_normal_deviation( decimater->normalDeviationValue() ); ?
      }
Jan Möbius's avatar
 
Jan Möbius committed
495 496
    }

Jan Möbius's avatar
Jan Möbius committed
497 498 499 500 501 502 503 504
    //roundness constraint
    if ( _constraints.contains("roundness") ){

      bool ok;

      double value = _constraints["roundness"].toDouble(&ok);

      if (ok) {
505
        if (  decimater_object.add( hModRoundness ) || (!verticesCount && !trianglesCount)  ) {
Jan Möbius's avatar
Jan Möbius committed
506 507 508
            decimater->setRoundnessConstraint( value );
            decimater_object.module( hModRoundness ).set_min_roundness( decimater->roundnessValue(), true );
        }
Jan Möbius's avatar
 
Jan Möbius committed
509 510 511
      }
    }

512 513
    //aspect ratio constraint
    if ( _constraints.contains("aspect_ratio") ){
Jan Möbius's avatar
 
Jan Möbius committed
514

515 516 517 518 519 520 521 522 523 524 525 526 527 528
      bool ok;

      double value = _constraints["aspect_ratio"].toDouble(&ok);

      if (ok) {
        if (  decimater_object.add( hModAspectRatio ) || (!verticesCount && !trianglesCount)  ) {
            decimater->setAspectRatioConstraint( value );
            decimater_object.module( hModAspectRatio ).set_aspect_ratio( decimater->aspectRatioValue() );
        }
      }
    }

    //edge length constraint
    if ( _constraints.contains("edge_length") ){
Jan Möbius's avatar
 
Jan Möbius committed
529

Jan Möbius's avatar
Jan Möbius committed
530
      bool ok;
Jan Möbius's avatar
 
Jan Möbius committed
531

532
      double value = _constraints["edge_length"].toDouble(&ok);
Jan Möbius's avatar
 
Jan Möbius committed
533

534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
      if (ok) {
        if (  decimater_object.add( hModEdgeLength ) || (!verticesCount && !trianglesCount)  ) {
            decimater->setEdgeLengthConstraint( value );
            decimater_object.module( hModEdgeLength ).set_edge_length( decimater->edgeLengthValue() );
        }
      }
    }

    //independent sets constraint
    if ( _constraints.contains("independent_sets") ){

      bool value = _constraints["independent_sets"].toBool();

      if (value) {
        if (  decimater_object.add( hModIndependent ) || (!verticesCount && !trianglesCount)  ) {
            decimater->setIndependentSetsConstraint();
        }
Jan Möbius's avatar
Jan Möbius committed
551 552 553 554 555 556 557 558 559 560
      }
    }

    //init the decimater
    if( ! decimater_object.initialize() ){
      emit log(LOGWARN, tr("Decimater could not be initialized"));
      return;
    }

    //decimate
561 562 563 564
    if ( verticesCount )
      decimater_object.decimate_to( vertices ); // do decimation (vertices)
    else if (trianglesCount )
      decimater_object.decimate_to_faces(0, triangles); // do decimation (triangles)
Jan Möbius's avatar
Jan Möbius committed
565
    else
566
      decimater_object.decimate_to_faces(0, 1); // do decimation
Jan Möbius's avatar
Jan Möbius committed
567 568 569 570 571 572 573 574 575

    object->mesh()->garbage_collection();
    object->mesh()->update_normals();
    object->update();

    // Create backup
    emit createBackup(_objID, "Decimation");
    
    // Create QVariantMap parameter string
576 577
    QString param = "("  + (_constraints.contains("decimation_order") ? tr("decimation_order =  %1").arg(_constraints["decimation_order"].toString()) : "") +
                    ", " + (_constraints.contains("distance") ? tr("distance = %1").arg(_constraints["distance"].toString()) : "") +
Jan Möbius's avatar
Jan Möbius committed
578
                    ", " + (_constraints.contains("normal_deviation") ? tr("normal_deviation = %1").arg(_constraints["normal_deviation"].toString()) : "") +
579
                    ", " + (_constraints.contains("edge_length") ? tr("edge_length = %1").arg(_constraints["edge_length"].toString()) : "") +
Jan Möbius's avatar
Jan Möbius committed
580
                    ", " + (_constraints.contains("roundness") ? tr("roundness = %1").arg(_constraints["roundness"].toString()) : "") +
581 582 583
                    ", " + (_constraints.contains("aspect_ratio") ? tr("aspect_ratio = %1").arg(_constraints["aspect_ratio"].toString()) : "") +
                    ", " + (_constraints.contains("independent_sets") ? tr("independent_sets = %1").arg(_constraints["independent_sets"].toString()) : "") +
                    ", " + (_constraints.contains("triangles") ? tr("triangles = %1").arg(_constraints["triangles"].toString()) : "") +
Jan Möbius's avatar
Jan Möbius committed
584 585 586 587 588 589 590 591 592
                    ", " + (_constraints.contains("vertices") ? tr("vertices = %1").arg(_constraints["vertices"].toString()) : "") + ")";
    
    emit scriptInfo( "decimate(" + QString::number(_objID) + ", " + param + ")" );
    
    emit updatedObject( baseObjectData->id() , UPDATE_TOPOLOGY);

  } else {
    emit log(LOGERR,tr("Unsupported object type for decimater"));
    return;
Jan Möbius's avatar
 
Jan Möbius committed
593 594 595 596 597 598 599
  }

  emit updateView();
}

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

Jan Möbius's avatar
Jan Möbius committed
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
void DecimaterPlugin::slotUpdateNumVertices()
{
  // Only update if tool is visible
  if ( !tool_->isVisible() ) {
    return;
  }

  int max = 0;
  int div = 0;

  bool ok;
  emit functionExists( "infomeshobject" , "vertexCount(int)", ok  ) ;
  if (!ok)
  {
    tool_->currentNumVertices->setText ("<not available>");
    return;
  }

  for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::TARGET_OBJECTS,DataType(DATA_TRIANGLE_MESH)) ;
                                        o_it != PluginFunctions::objectsEnd(); ++o_it)  {


    max = std::max( RPC::callFunctionValue<int>   ("infomeshobject" , "vertexCount",o_it->id()) , max );
    div++;
  }

  if (div <= 0)
    tool_->currentNumVertices->setText ("<not available>");
  else {
    tool_->currentNumVertices->setText (QString::number(max));
    tool_->verticesCount->setMaximum(max);
    tool_->verticesCountSlider->setMaximum(max);
    
    if ( tool_->verticesCount->value() < 2 )
      tool_->verticesCount->setValue( max / 2 );
  }
}

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

640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
/** \brief gets and sets the current maximum number of triangles
 *
 */
void DecimaterPlugin::slotUpdateNumTriangles() {
  // only update if the tool is visible
  if (!tool_->isVisible())
    return;

  uint max = 0;
  int meshN = 0;

  for (PluginFunctions::ObjectIterator o_it(PluginFunctions::TARGET_OBJECTS, DataType(DATA_TRIANGLE_MESH));
                                       o_it != PluginFunctions::objectsEnd(); ++o_it) {
    TriMesh* mesh = PluginFunctions::triMesh(o_it->id());
    max = std::max(mesh->n_faces(), max);
    meshN++;
  }

  tool_->trianglesCount->setMinimum(1);
  tool_->trianglesCount->setMaximum(max);
  tool_->trianglesCountSlider->setMinimum(1);
  tool_->trianglesCountSlider->setMaximum(max);

  if (tool_->trianglesCount->value() < 2)
    tool_->trianglesCount->setValue(max/2);
}

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

Jan Möbius's avatar
Jan Möbius committed
669 670 671
void DecimaterPlugin::slotObjectSelectionChanged(int /*_identifier*/)
{
  slotUpdateNumVertices ();
672
  slotUpdateNumTriangles ();
Jan Möbius's avatar
Jan Möbius committed
673 674 675 676
}

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

677
void DecimaterPlugin::slotObjectUpdated(int /*_identifier*/ , const UpdateType& _type )
Jan Möbius's avatar
Jan Möbius committed
678
{
679
  if ( _type.contains(UPDATE_TOPOLOGY) ) {
Jan Möbius's avatar
Jan Möbius committed
680
    slotUpdateNumVertices ();
681 682
    slotUpdateNumTriangles ();
  }
Jan Möbius's avatar
Jan Möbius committed
683 684 685 686 687 688 689
}

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

// activate checkbox if value has changed
void DecimaterPlugin::updateVertices()
{
690 691 692 693 694 695 696 697 698
  tool_->rbVertices->setChecked (true);
}

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

// activate checkbox if value has changed
void DecimaterPlugin::updateTriangles()
{
  tool_->rbTriangles->setChecked (true);
Jan Möbius's avatar
Jan Möbius committed
699 700 701 702 703 704 705 706 707 708 709 710
}

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

// activate checkbox if value has changed
void DecimaterPlugin::updateNormalDev()
{
  tool_->cbNormalDev->setChecked (true);
}

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

711 712 713 714 715 716 717 718
// activate checkbox if value has changed
void DecimaterPlugin::updateEdgeLength()
{
  tool_->cbEdgeLength->setChecked (true);
}

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

Jan Möbius's avatar
Jan Möbius committed
719 720 721 722 723 724 725 726 727 728
// activate checkbox if value has changed
void DecimaterPlugin::updateDistance()
{
  tool_->cbDistance->setChecked (true);
}

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

Q_EXPORT_PLUGIN2(DecimaterPlugin , DecimaterPlugin );