DecimaterPlugin.cc 39.3 KB
Newer Older
Jan Möbius's avatar
 
Jan Möbius committed
1 2 3
/*===========================================================================*\
*                                                                            *
*                              OpenFlipper                                   *
Martin Schultz's avatar
Martin Schultz committed
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
 *           Copyright (c) 2001-2015, RWTH-Aachen University                 *
 *           Department of Computer Graphics and Multimedia                  *
 *                          All rights reserved.                             *
 *                            www.openflipper.org                            *
 *                                                                           *
 *---------------------------------------------------------------------------*
 * This file is part of OpenFlipper.                                         *
 *---------------------------------------------------------------------------*
 *                                                                           *
 * Redistribution and use in source and binary forms, with or without        *
 * modification, are permitted provided that the following conditions        *
 * are met:                                                                  *
 *                                                                           *
 * 1. Redistributions of source code must retain the above copyright notice, *
 *    this list of conditions and the following disclaimer.                  *
 *                                                                           *
 * 2. Redistributions in binary form must reproduce the above copyright      *
 *    notice, this list of conditions and the following disclaimer in the    *
 *    documentation and/or other materials provided with the distribution.   *
 *                                                                           *
 * 3. Neither the name of the copyright holder nor the names of its          *
 *    contributors may be used to endorse or promote products derived from   *
 *    this software without specific prior written permission.               *
 *                                                                           *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       *
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A           *
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  *
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,       *
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR        *
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    *
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING      *
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS        *
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.              *
Jan Möbius's avatar
 
Jan Möbius committed
39 40 41 42
*                                                                            *
\*===========================================================================*/


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


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

52
#include "DecimaterPlugin.hh"
Matthias Möller's avatar
Matthias Möller committed
53

54
#include <OpenFlipper/BasePlugin/WhatsThisGenerator.hh>
Jan Möbius's avatar
 
Jan Möbius committed
55

Jan Möbius's avatar
Jan Möbius committed
56
#define DECIMATER "DecimaterData"
Jan Möbius's avatar
 
Jan Möbius committed
57

Jan Möbius's avatar
Jan Möbius committed
58
//== IMPLEMENTATION ==========================================================
Jan Möbius's avatar
 
Jan Möbius committed
59

60 61
DecimaterPlugin::DecimaterPlugin() :
        tool_(0),
62 63 64
        decimater_objects_(),
        toolIcon_(0),
        runningJobs_(0)
65 66 67 68
{

}

Jan Möbius's avatar
Jan Möbius committed
69 70
void DecimaterPlugin::initializePlugin()
{
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 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

  if ( OpenFlipper::Options::gui()) {
    tool_ = new DecimaterToolbarWidget();
    QSize size(100, 100);
    tool_->resize(size);

    // connect signals->slots
    connect(tool_->pbDecimate,SIGNAL(clicked() ),this,SLOT(slot_decimate()));
    connect(tool_->pbInitialize,SIGNAL(clicked() ), this, SLOT(slot_initialize()));

    connect(tool_->roundness,SIGNAL(valueChanged(double) ),this,SLOT(slotUpdateRoundness(double)) );
    connect(tool_->roundnessSlider,SIGNAL(valueChanged(int) ),this,SLOT(slotUpdateRoundness(int)) );
    connect(tool_->aspectRatio,SIGNAL(valueChanged(double) ),this,SLOT(slotUpdateAspectRatio(double)) );
    connect(tool_->aspectRatioSlider,SIGNAL(valueChanged(int) ),this,SLOT(slotUpdateAspectRatio(int)) );
    connect(tool_->distance,SIGNAL(valueChanged(double) ),this,SLOT(slotUpdateDistance()) );
    connect(tool_->edgeLength,SIGNAL(valueChanged(double) ),this,SLOT(slotUpdateEdgeLength()) );
    connect(tool_->normalDeviation,SIGNAL(valueChanged(int) ),this,SLOT(slotUpdateNormalDev()) );
    connect(tool_->normalDeviationSlider,SIGNAL(valueChanged(int) ),this,SLOT(slotUpdateNormalDev()) );
    connect(tool_->verticesCount,SIGNAL(valueChanged(int) ),this,SLOT(slotUpdateVertices()) );
    connect(tool_->verticesCountSlider,SIGNAL(valueChanged(int) ),this,SLOT(slotUpdateVertices()) );
    connect(tool_->trianglesCount,SIGNAL(valueChanged(int) ),this,SLOT(slotUpdateTriangles()) );
    connect(tool_->trianglesCountSlider,SIGNAL(valueChanged(int) ),this,SLOT(slotUpdateTriangles()) );

    // Force update if the Toolbox gets visible
    connect(tool_, SIGNAL(showing()), this, SLOT( slotUpdateNumVertices() ) );
    connect(tool_, SIGNAL(showing()), this, SLOT( slotUpdateNumTriangles() ) );
    connect(tool_->mixedFactorCounter, SIGNAL(valueChanged(double)), this, SLOT(slotMixedCounterValueChanged(double)) );
    connect(tool_->mixedFactorSlider, SIGNAL(valueChanged(int)), this, SLOT(slotMixedSliderValueChanged(int)) );
    connect(tool_->cbDistance, SIGNAL(toggled(bool)), this, SLOT(slotDisableDecimation()));
    connect(tool_->cbNormalDev, SIGNAL(toggled(bool)), this, SLOT(slotDisableDecimation()));
    connect(tool_->cbEdgeLength, SIGNAL(toggled(bool)), this, SLOT(slotDisableDecimation()));
    connect(tool_->cbIndependentSets, SIGNAL(toggled(bool)), this, SLOT(slotDisableDecimation()));
    connect(tool_->cbRoundness, SIGNAL(toggled(bool)), this, SLOT(slotDisableDecimation()));
    connect(tool_->cbAspectRatio, SIGNAL(toggled(bool)), this, SLOT(slotDisableDecimation()));
    connect(tool_->rbByDistance, SIGNAL(toggled(bool)), this, SLOT(slotDisableDecimation()));
    connect(tool_->rbByEdgeLength, SIGNAL(toggled(bool)), this, SLOT(slotDisableDecimation()));
    connect(tool_->rbByNormalDeviation, SIGNAL(toggled(bool)), this, SLOT(slotDisableDecimation()));
    connect(tool_->rbConstraintsOnly, SIGNAL(toggled(bool)), this, SLOT(slotDisableDecimation()));
    connect(tool_->rbTriangles, SIGNAL(toggled(bool)), this, SLOT(slotDisableDecimation()));
    connect(tool_->rbUseDecimater, SIGNAL(toggled(bool)), this, SLOT(slotDisableDecimation()));
    connect(tool_->rbUseMC, SIGNAL(toggled(bool)), this, SLOT(slotDisableDecimation()));
    connect(tool_->rbUseMixed, SIGNAL(toggled(bool)), this, SLOT(slotDisableDecimation()));
    connect(tool_->rbVertices, SIGNAL(toggled(bool)), this, SLOT(slotDisableDecimation()));

    toolIcon_ = new QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"decimater.png");
    emit addToolbox( tr("Decimater") , tool_, toolIcon_ );

    WhatsThisGenerator generator("Decimater");

    tool_->pbDecimate->setWhatsThis(tool_->pbDecimate->whatsThis()+generator.generateLink("quick_tutorial"));
    tool_->pbInitialize->setWhatsThis(tool_->pbInitialize->whatsThis()+generator.generateLink("quick_tutorial"));
    tool_->rbUseDecimater->setWhatsThis(tool_->rbUseDecimater->whatsThis()+generator.generateLink("incremental"));
    tool_->rbUseMC->setWhatsThis(tool_->rbUseMC->whatsThis()+generator.generateLink("multiple_choice"));
    tool_->rbUseMixed->setWhatsThis(tool_->rbUseMixed->whatsThis()+generator.generateLink("mixed"));
    tool_->cbDistance->setWhatsThis(tool_->cbDistance->whatsThis()+generator.generateLink());
    tool_->cbNormalDev->setWhatsThis(tool_->cbNormalDev->whatsThis()+generator.generateLink());
    tool_->cbEdgeLength->setWhatsThis(tool_->cbEdgeLength->whatsThis()+generator.generateLink());
    tool_->cbIndependentSets->setWhatsThis(tool_->cbIndependentSets->whatsThis()+generator.generateLink());
    tool_->cbRoundness->setWhatsThis(tool_->cbRoundness->whatsThis()+generator.generateLink());
    tool_->cbAspectRatio->setWhatsThis(tool_->cbAspectRatio->whatsThis()+generator.generateLink());
    tool_->rbByDistance->setWhatsThis(tool_->rbByDistance->whatsThis()+generator.generateLink());
    tool_->rbByEdgeLength->setWhatsThis(tool_->rbByEdgeLength->whatsThis()+generator.generateLink());
    tool_->rbByNormalDeviation->setWhatsThis(tool_->rbByNormalDeviation->whatsThis()+generator.generateLink());
    tool_->rbConstraintsOnly->setWhatsThis(tool_->rbConstraintsOnly->whatsThis()+generator.generateLink());
    tool_->rbTriangles->setWhatsThis(tool_->rbTriangles->whatsThis()+generator.generateLink());
    tool_->rbVertices->setWhatsThis(tool_->rbVertices->whatsThis()+generator.generateLink());


    tool_->randomSamplesCounter->setWhatsThis(tool_->randomSamplesCounter->whatsThis()+generator.generateLink("multiple_choice"));
    tool_->mixedFactorCounter->setWhatsThis(tool_->mixedFactorCounter->whatsThis()+generator.generateLink("mixed"));
    tool_->roundness->setWhatsThis(tool_->roundness->whatsThis()+generator.generateLink());
    tool_->aspectRatio->setWhatsThis(tool_->aspectRatio->whatsThis()+generator.generateLink());
    tool_->distance->setWhatsThis(tool_->distance->whatsThis()+generator.generateLink());
    tool_->edgeLength->setWhatsThis(tool_->edgeLength->whatsThis()+generator.generateLink());
    tool_->normalDeviation->setWhatsThis(tool_->normalDeviation->whatsThis()+generator.generateLink());
  }
Matthias Möller's avatar
doc  
Matthias Möller committed
147

Jan Möbius's avatar
 
Jan Möbius committed
148 149
}

Jan Möbius's avatar
Jan Möbius committed
150
/** \brief Initialization of the plugin when it is loaded by the core
Isaak Lim's avatar
Isaak Lim committed
151
 *
Jan Möbius's avatar
 
Jan Möbius committed
152
 */
Jan Möbius's avatar
Jan Möbius committed
153
void DecimaterPlugin::pluginsInitialized() {
Jan Möbius's avatar
 
Jan Möbius committed
154

Jan Möbius's avatar
Jan Möbius committed
155 156
  emit setSlotDescription("decimate(int,QVariantMap)",tr("Decimate a given object"),
                          QString(tr("objectId,constraints")).split(","),
157 158 159 160 161 162 163 164 165 166 167 168
                          QString(tr("ID of an object; Object that can has one or more constraint properties ("
                              "decimater_type [0 (Incremental), 1 (MC), 2 (Mixed)], "
                              "random_samples [For MC/Mixed], "
                              "incremental_percentage [For Mixed], "
                              "decimation_order [0 (by distance), 1 (by normal deviation), 2 (by edge length)], "
                              "distance, "
                              "edge_length, "
                              "normal_deviation, "
                              "roundness, "
                              "aspect_ratio,independent_sets, "
                              "vertices, "
                              "triangles)")).split(";"));
169

170 171 172
  if ( OpenFlipper::Options::gui()) {
    tool_->decTypeOps->setVisible(false);
  }
Jan Möbius's avatar
 
Jan Möbius committed
173 174 175 176 177
}


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

Jan Möbius's avatar
Jan Möbius committed
178
/** \brief sync between values of roundness slider and spinbox in the toolbox
Jan Möbius's avatar
 
Jan Möbius committed
179
 *
Jan Möbius's avatar
Jan Möbius committed
180
 * @param _value new roundness value
Jan Möbius's avatar
 
Jan Möbius committed
181
 */
182
void DecimaterPlugin::slotUpdateRoundness(int _value)
Jan Möbius's avatar
Jan Möbius committed
183 184 185
{
  tool_->roundness->setValue( (double) _value / 100.0 );
  tool_->cbRoundness->setChecked (true);
Jan Möbius's avatar
 
Jan Möbius committed
186 187
}

188 189 190 191 192 193 194 195 196 197
void DecimaterPlugin::slotMixedCounterValueChanged(double _value)
{
  tool_->mixedFactorLabel->setText(QString::number(100-_value)+QString("%"));
  tool_->mixedFactorSlider->setValue(100-_value);
}
void DecimaterPlugin::slotMixedSliderValueChanged(int _value)
{
  tool_->mixedFactorLabel->setText(QString::number(_value)+QString("%"));
  tool_->mixedFactorCounter->setValue(100.0-_value);
}
Jan Möbius's avatar
 
Jan Möbius committed
198 199 200

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

Jan Möbius's avatar
Jan Möbius committed
201
/** \brief sync between values of roundness slider and spinbox in the toolbox
Jan Möbius's avatar
 
Jan Möbius committed
202
 *
Jan Möbius's avatar
Jan Möbius committed
203
 * @param _value new roundness value
Jan Möbius's avatar
 
Jan Möbius committed
204
 */
205
void DecimaterPlugin::slotUpdateRoundness(double _value)
Jan Möbius's avatar
Jan Möbius committed
206 207 208
{
  tool_->roundnessSlider->setValue( (int) (_value * 100) );
  tool_->cbRoundness->setChecked (true);
Jan Möbius's avatar
 
Jan Möbius committed
209 210 211 212
}

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

213 214 215 216 217

/** \brief sync between values of aspect ratio slider and spinbox in the toolbox
 *
 * @param _value new aspect ratio value
 */
218
void DecimaterPlugin::slotUpdateAspectRatio(int _value)
219 220 221 222 223 224 225 226 227 228 229 230
{
  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
 */
231
void DecimaterPlugin::slotUpdateAspectRatio(double _value)
232 233 234 235 236
{
  tool_->aspectRatioSlider->setValue( (int) (_value * 100) );
  tool_->cbAspectRatio->setChecked (true);
}
//-----------------------------------------------------------------------------
237 238


239
/** \brief Init called by toolbox
Jan Möbius's avatar
 
Jan Möbius committed
240 241
 *
 */
242
void DecimaterPlugin::slot_initialize()
Jan Möbius's avatar
 
Jan Möbius committed
243
{
244

245 246 247
  if ( ! OpenFlipper::Options::gui())
    return;

248 249
  decimater_objects_.clear();

Jan Möbius's avatar
Jan Möbius committed
250
  for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::TARGET_OBJECTS,DATA_TRIANGLE_MESH) ;
251
      o_it != PluginFunctions::objectsEnd(); ++o_it)  {
Jan Möbius's avatar
 
Jan Möbius committed
252

253 254 255 256 257 258 259
	  initialize_object(*o_it);

  }
  tool_->pbDecimate->setEnabled(true);
}

void DecimaterPlugin::initialize_object(BaseObjectData *obj) {
Jan Möbius's avatar
Jan Möbius committed
260
    //initialize
261
    TriMeshObject* object = PluginFunctions::triMeshObject(obj);
Jan Möbius's avatar
 
Jan Möbius committed
262

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

266
    DecimaterInfo* decimater = dynamic_cast< DecimaterInfo* > ( obj->objectData(DECIMATER) );
Jan Möbius's avatar
 
Jan Möbius committed
267

268
    TriMesh* mesh = PluginFunctions::triMesh(obj);
Isaak Lim's avatar
Isaak Lim committed
269

Jan Möbius's avatar
Jan Möbius committed
270 271
    if (decimater == 0){
      decimater = new DecimaterInfo();
272
      obj->setObjectData(DECIMATER, decimater);
Jan Möbius's avatar
Jan Möbius committed
273
    }
Isaak Lim's avatar
Isaak Lim committed
274

Jan Möbius's avatar
Jan Möbius committed
275 276 277 278 279 280 281 282 283
    // constraint handles for decimation
    ModAspectRatioH     hModAspectRatio;
    ModEdgeLengthH      hModEdgeLength;
    ModHausdorffH       hModHausdorff;
    ModIndependentH     hModIndependent;
    ModNormalDeviationH hModNormalDeviation;
    ModNormalFlippingH  hModNormalFlipping;
    ModQuadricH         hModQuadric;
    ModRoundnessH       hModRoundness;
Isaak Lim's avatar
Isaak Lim committed
284

Jan Möbius's avatar
Jan Möbius committed
285
    // Create decimater
286 287 288 289 290 291 292 293
    ptr::shared_ptr<BaseDecimaterType> decimater_object;
    if (tool_->rbUseDecimater->isChecked())
      decimater_object = ptr::shared_ptr<DecimaterType>(new DecimaterType(*mesh));
    else if(tool_->rbUseMC->isChecked())
      decimater_object = ptr::shared_ptr<McDecimaterType>(new McDecimaterType(*mesh));
    else if(tool_->rbUseMixed->isChecked())
      decimater_object = ptr::shared_ptr<MixedDecimaterType>(new MixedDecimaterType(*mesh));

Isaak Lim's avatar
Isaak Lim committed
294

Jan Möbius's avatar
Jan Möbius committed
295 296
    // Remove old constraints
    if(decimater->distance()) {
297 298
      decimater->removeDistanceConstraint();
      decimater_object->remove(hModHausdorff);
Jan Möbius's avatar
Jan Möbius committed
299 300
    }
    if(decimater->normalDeviation()) {
301 302
      decimater->removeNormalDeviationConstraint();
      decimater_object->remove(hModNormalDeviation);
303 304
    }
    if(decimater->normalFlipping()) {
305 306
      decimater->removeNormalFlippingConstraint();
      decimater_object->remove(hModNormalFlipping);
Jan Möbius's avatar
Jan Möbius committed
307 308
    }
    if(decimater->roundness()) {
309 310
      decimater->removeRoundnessConstraint();
      decimater_object->remove(hModRoundness);
Jan Möbius's avatar
Jan Möbius committed
311
    }
312 313
    if(decimater->aspectRatio()) {
      decimater->removeAspectRatioConstraint();
314
      decimater_object->remove(hModAspectRatio);
315 316 317
    }
    if(decimater->edgeLength()) {
      decimater->removeEdgeLengthConstraint();
318
      decimater_object->remove(hModEdgeLength);
319 320 321
    }
    if(decimater->independentSets()) {
      decimater->removeIndependentSetsConstraint();
322
      decimater_object->remove(hModIndependent);
323 324 325 326 327
    }

    // set priority module: quadric, normal deviation or edge length
    if (tool_->rbByDistance->isChecked()) {
      decimater->setDecimationOrder(DecimaterInfo::DISTANCE);
328 329
      decimater_object->add( hModQuadric );
      decimater_object->module( hModQuadric ).unset_max_err();
330 331
    } else if (tool_->rbByNormalDeviation->isChecked()) {
      decimater->setDecimationOrder(DecimaterInfo::NORMALDEV);
332 333
      decimater_object->add(hModNormalDeviation);
      decimater_object->module(hModNormalDeviation).set_binary(false);
334 335
    } else if (tool_->rbByEdgeLength->isChecked()) {
      decimater->setDecimationOrder(DecimaterInfo::EDGELENGTH);
336 337
      decimater_object->add(hModEdgeLength);
      decimater_object->module(hModEdgeLength).set_binary(false);
338
    }
Jan Möbius's avatar
 
Jan Möbius committed
339

340
    // and set new constraints
Matthias Möller's avatar
Matthias Möller committed
341
    ptr::shared_ptr<DecimaterInit> decInit (new DecimaterInit);
Jan Möbius's avatar
Jan Möbius committed
342
    if ( tool_->cbDistance->isChecked() ) {
Matthias Möller's avatar
Matthias Möller committed
343
      if (  decimater_object->add( decInit->hModHausdorff ) || tool_->rbConstraintsOnly->isChecked() ) {
344
        decimater->setDistanceConstraint( tool_->distance->value() );
Matthias Möller's avatar
Matthias Möller committed
345
        decimater_object->module( decInit->hModHausdorff ).set_tolerance( decimater->distanceValue() );
Jan Möbius's avatar
Jan Möbius committed
346 347
      }
    }
Jan Möbius's avatar
 
Jan Möbius committed
348

Jan Möbius's avatar
Jan Möbius committed
349
    if ( tool_->cbNormalDev->isChecked() ) {
Matthias Möller's avatar
Matthias Möller committed
350
      if (  decimater_object->add( decInit->hModNormalDeviation ) || tool_->rbConstraintsOnly->isChecked() ) {
351
        decimater->setNormalDeviationConstraint( tool_->normalDeviation->value() );
Matthias Möller's avatar
Matthias Möller committed
352
        decimater_object->module( decInit->hModNormalDeviation ).set_normal_deviation( decimater->normalDeviationValue() );
353 354
      }
    } else {
Matthias Möller's avatar
Matthias Möller committed
355
      if ( decimater_object->add( decInit->hModNormalFlipping ) || tool_->rbConstraintsOnly->isChecked() ) {
356
        decimater->setNormalFlippingConstraint();
Jan Möbius's avatar
Jan Möbius committed
357 358
      }
    }
Jan Möbius's avatar
 
Jan Möbius committed
359

Isaak Lim's avatar
Isaak Lim committed
360
    if ( tool_->cbRoundness->isChecked() ) {
Matthias Möller's avatar
Matthias Möller committed
361
      if (  decimater_object->add( decInit->hModRoundness ) || tool_->rbConstraintsOnly->isChecked() ) {
362
        decimater->setRoundnessConstraint( tool_->roundness->value() );
Matthias Möller's avatar
Matthias Möller committed
363
        decimater_object->module( decInit->hModRoundness ).set_min_roundness( decimater->roundnessValue(), true );
Jan Möbius's avatar
Jan Möbius committed
364 365
      }
    }
Jan Möbius's avatar
 
Jan Möbius committed
366

367
    if ( tool_->cbAspectRatio->isChecked() ) {
Matthias Möller's avatar
Matthias Möller committed
368
      if ( decimater_object->add( decInit->hModAspectRatio ) || tool_->rbConstraintsOnly->isChecked() ) {
369
        decimater->setAspectRatioConstraint( tool_->aspectRatio->value() );
Matthias Möller's avatar
Matthias Möller committed
370
        decimater_object->module( decInit->hModAspectRatio ).set_aspect_ratio( decimater->aspectRatioValue() );
371 372 373 374
      }
    }

    if ( tool_->cbEdgeLength->isChecked() ) {
Matthias Möller's avatar
Matthias Möller committed
375
      if ( decimater_object->add( decInit->hModEdgeLength ) || tool_->rbConstraintsOnly->isChecked() ) {
376
        decimater->setEdgeLengthConstraint( tool_->edgeLength->value() );
Matthias Möller's avatar
Matthias Möller committed
377
        decimater_object->module( decInit->hModEdgeLength ).set_edge_length( decimater->edgeLengthValue() );
378 379 380 381
      }
    }

    if ( tool_->cbIndependentSets->isChecked() ) {
Matthias Möller's avatar
Matthias Möller committed
382
      if ( decimater_object->add( decInit->hModIndependent ) || tool_->rbConstraintsOnly->isChecked() ) {
383
        decimater->setIndependentSetsConstraint();
384 385 386
      }
    }

Jan Möbius's avatar
 
Jan Möbius committed
387

388 389 390
    // Initialize the decimater
    if( ! decimater_object->initialize() ){
      emit log(LOGWARN, tr("Decimater could not be initialized"));
391
      return;
392
    }
Matthias Möller's avatar
Matthias Möller committed
393 394

    decInit->decimater = decimater_object;
395
    decInit->objId = obj->id();
Matthias Möller's avatar
Matthias Möller committed
396 397

    decimater_objects_.push_back(decInit);
398
}
399 400 401 402 403 404 405 406 407 408 409 410 411 412

void DecimaterPlugin::slot_initialize_object(int obj_id, bool clear) {
	if (clear)
		decimater_objects_.clear();

	BaseObjectData *obj = 0;
	PluginFunctions::getObject(obj_id, obj);
	if (!obj) return;

	initialize_object(obj);

	tool_->pbDecimate->setEnabled(true);
}

413 414 415 416 417 418
//-----------------------------------------------------------------------------
/** \brief Decimation called by toolbox
 *
 */
void DecimaterPlugin::slot_decimate()
{
Matthias Möller's avatar
Matthias Möller committed
419

420 421 422
  if ( ! OpenFlipper::Options::gui())
    return;

423
  //decimate
424
  runningJobs_ = decimater_objects_.size();
Matthias Möller's avatar
Matthias Möller committed
425
  for (std::vector< ptr::shared_ptr<DecimaterInit> >::iterator decIter = decimater_objects_.begin();
426 427
      decIter != decimater_objects_.end(); ++decIter)
  {
Matthias Möller's avatar
Matthias Möller committed
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
    ptr::shared_ptr<DecimaterInit> decInit = *decIter;
    ptr::shared_ptr<BaseDecimaterType> decimater = decInit->decimater;

    // set values for constraints
    if ( tool_->cbDistance->isChecked() ) {
        decimater->module( decInit->hModHausdorff ).set_tolerance( tool_->distance->value() );
    }

    if ( tool_->cbNormalDev->isChecked() ) {
      decimater->module( decInit->hModNormalDeviation ).set_normal_deviation( tool_->normalDeviation->value() );
    }

    if ( tool_->cbRoundness->isChecked() ) {
      decimater->module( decInit->hModRoundness ).set_min_roundness( tool_->roundness->value(), true );
    }

    if ( tool_->cbAspectRatio->isChecked() ) {
      decimater->module( decInit->hModAspectRatio ).set_aspect_ratio( tool_->aspectRatio->value() );
    }

    if ( tool_->cbEdgeLength->isChecked() ) {
      decimater->module( decInit->hModEdgeLength ).set_edge_length( tool_->edgeLength->value() );
    }

452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
    // fill data for the decimate thread
    DecimateThread::Params params;
    params.dec = (tool_->rbUseDecimater->isChecked()) ? dynamic_cast<DecimaterType*>(decimater.get()) : NULL;
    params.mcDec = (tool_->rbUseMC->isChecked())   ? dynamic_cast<McDecimaterType*>(decimater.get()) : NULL;
    params.mixedDec = (tool_->rbUseMixed->isChecked())   ? dynamic_cast<MixedDecimaterType*>(decimater.get()) : NULL;

    params.facesCount = (tool_->rbTriangles->isChecked()) ? tool_->trianglesCount->value() : -1;
    params.verticesCount = (tool_->rbVertices->isChecked() ) ? tool_->verticesCount->value() : -1;
    params.samples = tool_->randomSamplesCounter->value();
    params.mc_factor = 1.0 - (tool_->mixedFactorCounter->value()*0.01);

    // create and start decimate thread
    QString jobId = QString("Decimate_Object_%1").arg(decInit->objId);
    DecimateThread* th = new DecimateThread(params, jobId, decInit->objId);
    connect(th, SIGNAL(finished(QString)), this,SIGNAL(finishJob(QString)));
    connect(th, SIGNAL(finished(QString)), this, SLOT(slot_decimate_finished(QString)));
    connect(th, SIGNAL(state(QString, int)), this, SIGNAL(setJobState(QString, int)));
    connect(this, SIGNAL(jobCanceled(QString)), th, SLOT(slotCancel(QString)));

    tool_->pbDecimate->setEnabled(false);
    tool_->pbInitialize->setEnabled(false);
Jan Möbius's avatar
Jan Möbius committed
473

474
    emit startJob(jobId , QString("Decimate Object with Id %1").arg(decInit->objId) , 0, 100, false);
475

476 477
    th->start();
    th->startProcessing();
Isaak Lim's avatar
Isaak Lim committed
478

Jan Möbius's avatar
Jan Möbius committed
479
  }
Jan Möbius's avatar
 
Jan Möbius committed
480

481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513

}

void DecimaterPlugin::canceledJob (QString _job )
{
  emit jobCanceled(_job);
}

void DecimaterPlugin::slot_decimate_finished(QString _jobId)
{
  //This function is executed by the main thread! but the sender is the finished thread
  DecimateThread* thread = dynamic_cast<DecimateThread*>(sender());

  if (!thread)
    return;
  if (!thread->baseDecimater())
    return;

  //update mesh
  thread->baseDecimater()->mesh().garbage_collection();
  thread->baseDecimater()->mesh().update_normals();

  emit updatedObject( thread->objectId() , UPDATE_TOPOLOGY );
  emit createBackup( thread->objectId(), "Decimation");

  //cleanup when all threads are done
  --runningJobs_;//running in main thread, so no race condition
  if (runningJobs_ == 0)
  {
    tool_->pbDecimate->setEnabled(true);
    tool_->pbInitialize->setEnabled(true);
    emit updateView();
  }
Jan Möbius's avatar
 
Jan Möbius committed
514 515
}

Jan Möbius's avatar
Jan Möbius committed
516

Jan Möbius's avatar
Jan Möbius committed
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
//-----------------------------------------------------------------------------

void DecimaterPlugin::decimate(int _objID, QString _constraints) {
  QStringList constraintList = _constraints.split(";");

  QVariantMap map;

  for ( auto i = 0 ; i <  constraintList.size() ; ++i) {
    const auto& currentConstraint =  constraintList[i];

    int pos = currentConstraint.indexOf("=" );

    if ( pos == -1 ) {
      emit log(LOGERR,"Wrong parameter format for decimater: " + currentConstraint);
      return;
    }

    QString constraintName = currentConstraint.left(pos);
    QString constraintValue = currentConstraint.right(currentConstraint.size() - pos -1 );

    bool ok = false;

    double value = constraintValue.toDouble(&ok);

    if ( !ok)  {
      emit log(LOGERR,"Wrong parameter format for decimater. Conversion failed!");
      emit log(LOGERR,"Got Name : " + constraintName );
      emit log(LOGERR,"Got Value: " + constraintValue);
      return;
    }
//    decimater.decimate(5,"random_samples=2;decimater_type=2;distance=0.0001")

    map[constraintName] = value;
  }

  decimate(_objID,map);


}

Jan Möbius's avatar
Jan Möbius committed
557 558
//-----------------------------------------------------------------------------

Jan Möbius's avatar
Jan Möbius committed
559
void DecimaterPlugin::decimate(int _objID, QVariantMap _constraints) {
Jan Möbius's avatar
 
Jan Möbius committed
560

Jan Möbius's avatar
Jan Möbius committed
561 562 563 564 565 566 567 568
  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
569

Jan Möbius's avatar
Jan Möbius committed
570 571 572 573
    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
574

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

Jan Möbius's avatar
Jan Möbius committed
577
    TriMesh* mesh = PluginFunctions::triMesh(baseObjectData);
Isaak Lim's avatar
Isaak Lim committed
578

Jan Möbius's avatar
Jan Möbius committed
579 580 581 582
    if (decimater == 0){
      decimater = new DecimaterInfo();
      object->setObjectData(DECIMATER, decimater);
    }
Isaak Lim's avatar
Isaak Lim committed
583

Jan Möbius's avatar
 
Jan Möbius committed
584
    // constraint handles for decimation
Jan Möbius's avatar
Jan Möbius committed
585 586 587 588
    ModAspectRatioH     hModAspectRatio;
    ModEdgeLengthH      hModEdgeLength;
    ModHausdorffH       hModHausdorff;
    ModIndependentH     hModIndependent;
Jan Möbius's avatar
 
Jan Möbius committed
589
    ModNormalDeviationH hModNormalDeviation;
Jan Möbius's avatar
Jan Möbius committed
590 591 592
    ModNormalFlippingH  hModNormalFlipping;
    ModQuadricH         hModQuadric;
    ModRoundnessH       hModRoundness;
Isaak Lim's avatar
Isaak Lim committed
593

Jan Möbius's avatar
Jan Möbius committed
594
    // Create decimater
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
    ptr::shared_ptr<BaseDecimaterType> decimater_object;
    if (_constraints.contains("decimater_type"))
    {
      bool ok;
      int value = _constraints["decimater_type"].toInt(&ok);
      if (ok)
      {
        if (value == 0)
          decimater_object = ptr::shared_ptr<DecimaterType>(new DecimaterType(*mesh));
        else if (value == 1)
          decimater_object = ptr::shared_ptr<McDecimaterType>(new McDecimaterType(*mesh));
        else if (value == 2)
          decimater_object = ptr::shared_ptr<MixedDecimaterType>(new MixedDecimaterType(*mesh));
      }

    }

    if (!decimater_object)
      decimater_object = ptr::shared_ptr<DecimaterType>(new DecimaterType(*mesh));
Jan Möbius's avatar
Jan Möbius committed
614 615 616

    // Remove old constraints
    if(decimater->distance()) {
617
      decimater->removeDistanceConstraint();
618
      decimater_object->remove(hModHausdorff);
Jan Möbius's avatar
Jan Möbius committed
619 620
    }
    if(decimater->normalDeviation()) {
621
      decimater->removeNormalDeviationConstraint();
622
      decimater_object->remove(hModNormalDeviation);
623 624 625
    }
    if(decimater->normalFlipping()) {
      decimater->removeNormalFlippingConstraint();
626
      decimater_object->remove(hModNormalFlipping);
Jan Möbius's avatar
Jan Möbius committed
627 628
    }
    if(decimater->roundness()) {
629
      decimater->removeRoundnessConstraint();
630
      decimater_object->remove(hModRoundness);
631 632 633
    }
    if(decimater->aspectRatio()) {
      decimater->removeAspectRatioConstraint();
634
      decimater_object->remove(hModAspectRatio);
635 636 637
    }
    if(decimater->edgeLength()) {
      decimater->removeEdgeLengthConstraint();
638
      decimater_object->remove(hModEdgeLength);
639 640 641
    }
    if(decimater->independentSets()) {
      decimater->removeIndependentSetsConstraint();
642
      decimater_object->remove(hModIndependent);
643 644 645 646 647 648 649 650 651 652 653 654
    }

    // 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);
655 656
          decimater_object->add( hModQuadric );
          decimater_object->module( hModQuadric ).unset_max_err();
657 658 659
          break;
        case 1:
          decimater->setDecimationOrder(DecimaterInfo::NORMALDEV);
660 661
          decimater_object->add(hModNormalDeviation);
          decimater_object->module(hModNormalDeviation).set_binary(false);
662 663 664
          break;
        case 2:
          decimater->setDecimationOrder(DecimaterInfo::EDGELENGTH);
665 666
          decimater_object->add(hModEdgeLength);
          decimater_object->module(hModEdgeLength).set_binary(false);
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703
          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
704
    }
Jan Möbius's avatar
Jan Möbius committed
705

Jan Möbius's avatar
Jan Möbius committed
706 707
    //distance constraint
    if ( _constraints.contains("distance") ){
Jan Möbius's avatar
 
Jan Möbius committed
708

Jan Möbius's avatar
Jan Möbius committed
709 710 711 712 713
      bool ok;

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

      if (ok) {
714
        if (  decimater_object->add( hModHausdorff ) || (!verticesCount && !trianglesCount)  ) {
Jan Möbius's avatar
Jan Möbius committed
715
            decimater->setDistanceConstraint( value );
716
            decimater_object->module( hModHausdorff ).set_tolerance( decimater->distanceValue() );
Jan Möbius's avatar
Jan Möbius committed
717 718
        }
      }
Jan Möbius's avatar
 
Jan Möbius committed
719 720
    }

Jan Möbius's avatar
Jan Möbius committed
721 722
    //normal deviation constraint
    if ( _constraints.contains("normal_deviation") ){
Jan Möbius's avatar
 
Jan Möbius committed
723

Jan Möbius's avatar
Jan Möbius committed
724
      bool ok;
Jan Möbius's avatar
 
Jan Möbius committed
725

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

Jan Möbius's avatar
Jan Möbius committed
728
      if (ok) {
729
        if (  decimater_object->add( hModNormalDeviation ) || (!verticesCount && !trianglesCount)  ) {
Jan Möbius's avatar
Jan Möbius committed
730
            decimater->setNormalDeviationConstraint( value );
731
            decimater_object->module( hModNormalDeviation ).set_normal_deviation( decimater->normalDeviationValue() );
Jan Möbius's avatar
Jan Möbius committed
732 733
        }
      }
734
    } else { // flipping constraint
735
      if (  decimater_object->add( hModNormalFlipping ) || (!verticesCount && !trianglesCount)  ) {
736
        decimater->setNormalFlippingConstraint();
737
        // decimater_object->module( hModNormalFlipping ).set_max_normal_deviation( decimater->normalDeviationValue() ); ?
738
      }
Jan Möbius's avatar
 
Jan Möbius committed
739 740
    }

Jan Möbius's avatar
Jan Möbius committed
741 742 743 744 745 746 747 748
    //roundness constraint
    if ( _constraints.contains("roundness") ){

      bool ok;

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

      if (ok) {
749
        if (  decimater_object->add( hModRoundness ) || (!verticesCount && !trianglesCount)  ) {
Jan Möbius's avatar
Jan Möbius committed
750
            decimater->setRoundnessConstraint( value );
751
            decimater_object->module( hModRoundness ).set_min_roundness( decimater->roundnessValue(), true );
Jan Möbius's avatar
Jan Möbius committed
752
        }
Jan Möbius's avatar
 
Jan Möbius committed
753 754 755
      }
    }

756 757
    //aspect ratio constraint
    if ( _constraints.contains("aspect_ratio") ){
Jan Möbius's avatar
 
Jan Möbius committed
758

759 760 761 762 763
      bool ok;

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

      if (ok) {
764
        if (  decimater_object->add( hModAspectRatio ) || (!verticesCount && !trianglesCount)  ) {
765
            decimater->setAspectRatioConstraint( value );
766
            decimater_object->module( hModAspectRatio ).set_aspect_ratio( decimater->aspectRatioValue() );
767 768 769 770 771 772
        }
      }
    }

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

Jan Möbius's avatar
Jan Möbius committed
774
      bool ok;
Jan Möbius's avatar
 
Jan Möbius committed
775

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

778
      if (ok) {
779
        if (  decimater_object->add( hModEdgeLength ) || (!verticesCount && !trianglesCount)  ) {
780
            decimater->setEdgeLengthConstraint( value );
781
            decimater_object->module( hModEdgeLength ).set_edge_length( decimater->edgeLengthValue() );
782 783 784 785 786 787 788 789 790 791
        }
      }
    }

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

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

      if (value) {
792
        if (  decimater_object->add( hModIndependent ) || (!verticesCount && !trianglesCount)  ) {
793 794
            decimater->setIndependentSetsConstraint();
        }
Jan Möbius's avatar
Jan Möbius committed
795 796 797 798
      }
    }

    //init the decimater
799
    if( ! decimater_object->initialize() ){
Jan Möbius's avatar
Jan Möbius committed
800 801 802 803
      emit log(LOGWARN, tr("Decimater could not be initialized"));
      return;
    }

804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822
    float mc_factor = 0.5;
    size_t randomSamples = 8;

    if (_constraints.contains("random_samples"))
    {
      bool ok;
      unsigned value =_constraints["random_samples"].toUInt(&ok);
      if (ok)
        randomSamples = value;
    }

    if (_constraints.contains("incremental_percentage"))
    {
      bool ok;
      unsigned value =_constraints["incremental_percentage"].toUInt(&ok);
      if (ok)
        mc_factor = 1.f - (value*0.01f);
    }

Jan Möbius's avatar
Jan Möbius committed
823
    //decimate
824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859
    DecimaterType* dec = dynamic_cast<DecimaterType*>(decimater_object.get());
    McDecimaterType* mcDec = dynamic_cast<McDecimaterType*>(decimater_object.get());
    MixedDecimaterType* mixedDec = dynamic_cast<MixedDecimaterType*>(decimater_object.get());

    if(dec && !mixedDec)
    {
      if ( verticesCount )
        dec->decimate_to(vertices);
      else if (trianglesCount )
        dec->decimate_to_faces(0, triangles);
      else // constraints only
        dec->decimate_to_faces(0, 1);
    }
    else if (mcDec && !mixedDec)
    {
      mcDec->set_samples(randomSamples);
      if ( verticesCount )
        mcDec->decimate_to(vertices);
      else if (trianglesCount)
        mcDec->decimate_to_faces(0, triangles);
      else // constraints only
        mcDec->decimate_to_faces(0, 1);
    }
    else if (mixedDec)
    {
      mixedDec->set_samples(randomSamples);
      if ( verticesCount )
        mixedDec->decimate_to(vertices,mc_factor);
      else if (trianglesCount)
        mixedDec->decimate_to_faces(0, triangles,mc_factor);
      else // constraints only
        mixedDec->decimate_to_faces(0, 1,mc_factor);
    }else
    {
      emit log(LOGERR,tr("Could not find Decimater Type"));
    }
Jan Möbius's avatar
Jan Möbius committed
860 861 862

    object->mesh()->garbage_collection();
    object->mesh()->update_normals();
863 864

    emit updatedObject( baseObjectData->id() , UPDATE_TOPOLOGY);
Jan Möbius's avatar
Jan Möbius committed
865 866 867

    // Create backup
    emit createBackup(_objID, "Decimation");
Isaak Lim's avatar
Isaak Lim committed
868

Jan Möbius's avatar
Jan Möbius committed
869
    // Create QVariantMap parameter string
870 871
    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
872
                    ", " + (_constraints.contains("normal_deviation") ? tr("normal_deviation = %1").arg(_constraints["normal_deviation"].toString()) : "") +
873
                    ", " + (_constraints.contains("edge_length") ? tr("edge_length = %1").arg(_constraints["edge_length"].toString()) : "") +
Jan Möbius's avatar
Jan Möbius committed
874
                    ", " + (_constraints.contains("roundness") ? tr("roundness = %1").arg(_constraints["roundness"].toString()) : "") +
875 876 877
                    ", " + (_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
878
                    ", " + (_constraints.contains("vertices") ? tr("vertices = %1").arg(_constraints["vertices"].toString()) : "") + ")";
Isaak Lim's avatar
Isaak Lim committed
879

Jan Möbius's avatar
Jan Möbius committed
880
    emit scriptInfo( "decimate(" + QString::number(_objID) + ", " + param + ")" );
Isaak Lim's avatar
Isaak Lim committed
881

Jan Möbius's avatar
Jan Möbius committed
882 883 884 885

  } else {
    emit log(LOGERR,tr("Unsupported object type for decimater"));
    return;
Jan Möbius's avatar
 
Jan Möbius committed
886 887 888 889 890 891 892
  }

  emit updateView();
}

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

Jan Möbius's avatar
Jan Möbius committed
893 894 895
void DecimaterPlugin::slotUpdateNumVertices()
{
  // Only update if tool is visible
896
  if ( !OpenFlipper::Options::gui() || !tool_->isVisible() ) {
Jan Möbius's avatar
Jan Möbius committed
897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921
    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 {
922 923 924
    tool_->verticesCount->blockSignals(true);
    tool_->verticesCountSlider->blockSignals(true);

Jan Möbius's avatar
Jan Möbius committed
925 926 927
    tool_->currentNumVertices->setText (QString::number(max));
    tool_->verticesCount->setMaximum(max);
    tool_->verticesCountSlider->setMaximum(max);
928

Jan Möbius's avatar
Jan Möbius committed
929
    if ( tool_->verticesCount->value() < 2 )
930
    {
Jan Möbius's avatar
Jan Möbius committed
931
      tool_->verticesCount->setValue( max / 2 );
932 933
      tool_->verticesCountSlider->setValue( max / 2);
    }
934 935 936

    tool_->verticesCount->blockSignals(false);
    tool_->verticesCountSlider->blockSignals(false);
Jan Möbius's avatar
Jan Möbius committed
937 938 939 940 941
  }
}

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

942 943 944 945 946
/** \brief gets and sets the current maximum number of triangles
 *
 */
void DecimaterPlugin::slotUpdateNumTriangles() {
  // only update if the tool is visible
947
  if (!OpenFlipper::Options::gui() || !tool_->isVisible())
948 949
    return;

950
  size_t max = 0;
951 952 953 954 955 956 957 958 959
  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++;
  }

960 961 962
  tool_->trianglesCount->blockSignals(true);
  tool_->trianglesCountSlider->blockSignals(true);

963 964 965 966 967 968
  tool_->trianglesCount->setMinimum(1);
  tool_->trianglesCount->setMaximum(max);
  tool_->trianglesCountSlider->setMinimum(1);
  tool_->trianglesCountSlider->setMaximum(max);

  if (tool_->trianglesCount->value() < 2)
969 970 971 972
  {
    tool_->trianglesCount->setValue (max / 2 );
    tool_->trianglesCountSlider->setValue( max / 2);
  }
973 974 975

  tool_->trianglesCount->blockSignals(false);
  tool_->trianglesCountSlider->blockSignals(false);
976 977 978 979
}

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

Jan Möbius's avatar
Jan Möbius committed
980 981 982
void DecimaterPlugin::slotObjectSelectionChanged(int /*_identifier*/)
{
  slotUpdateNumVertices ();
983
  slotUpdateNumTriangles ();
Jan Möbius's avatar
Jan Möbius committed
984
}
985 986 987 988
//-----------------------------------------------------------------------------

void DecimaterPlugin::objectDeleted(int _id)
{
Matthias Möller's avatar
Matthias Möller committed
989
  slotDisableDecimation();
990
}
Jan Möbius's avatar
Jan Möbius committed
991 992 993

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

994
void DecimaterPlugin::slotAboutToRestore(int _id)
Matthias Möller's avatar
Matthias Möller committed
995 996 997 998 999 1000 1001
{
  slotDisableDecimation();
}

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

void DecimaterPlugin::slotDisableDecimation()
1002
{
1003 1004 1005
  if ( ! OpenFlipper::Options::gui())
    return;

1006
  decimater_objects_.clear();
Matthias Möller's avatar
Matthias Möller committed
1007
  tool_->pbDecimate->setEnabled(false);
1008 1009 1010 1011
}

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

1012
void DecimaterPlugin::slotObjectUpdated(int /*_identifier*/ , const UpdateType& _type )
Jan Möbius's avatar
Jan Möbius committed
1013
{
1014
  if ( _type.contains(UPDATE_TOPOLOGY) ) {
Jan Möbius's avatar
Jan Möbius committed
1015
    slotUpdateNumVertices ();
1016 1017
    slotUpdateNumTriangles ();
  }
Jan Möbius's avatar
Jan Möbius committed
1018 1019 1020 1021 1022
}

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

// activate checkbox if value has changed
1023
void DecimaterPlugin::slotUpdateVertices()
Jan Möbius's avatar
Jan Möbius committed
1024
{
1025 1026 1027 1028 1029 1030
  tool_->rbVertices->setChecked (true);
}

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

// activate checkbox if value has changed
1031
void DecimaterPlugin::slotUpdateTriangles()
1032 1033
{
  tool_->rbTriangles->setChecked (true);
Jan Möbius's avatar
Jan Möbius committed
1034 1035 1036 1037 1038
}

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

// activate checkbox if value has changed
1039
void DecimaterPlugin::slotUpdateNormalDev()
Jan Möbius's avatar
Jan Möbius committed
1040 1041 1042 1043 1044 1045
{
  tool_->cbNormalDev->setChecked (true);
}

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

1046
// activate checkbox if value has changed
1047
void DecimaterPlugin::slotUpdateEdgeLength()
1048 1049 1050 1051 1052 1053
{
  tool_->cbEdgeLength->setChecked (true);
}

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

Jan Möbius's avatar
Jan Möbius committed
1054
// activate checkbox if value has changed
1055
void DecimaterPlugin::slotUpdateDistance()
Jan Möbius's avatar
Jan Möbius committed
1056 1057 1058 1059
{
  tool_->cbDistance->setChecked (true);
}