IsotropicRemesherPlugin.cc 10.5 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
43
44
45
46
47
48
49
*                                                                            *
\*===========================================================================*/

/*===========================================================================*\
*                                                                            *
*   $Revision$                                                       *
*   $LastChangedBy$                                                *
*   $Date$                     *
*                                                                            *
\*===========================================================================*/

Jan Möbius's avatar
 
Jan Möbius committed
50
51
52
#include "IsotropicRemesherPlugin.hh"
#include "IsotropicRemesherT.hh"

Jan Möbius's avatar
Jan Möbius committed
53
54
55
56
57
IsotropicRemesherPlugin::IsotropicRemesherPlugin() :
tool_(0),
toolIcon_(0),
edgeLength_(0),
thread_(0)
58
59
60
{
}

Jan Möbius's avatar
 
Jan Möbius committed
61
/// init the Toolbox
Jan Möbius's avatar
Jan Möbius committed
62
void IsotropicRemesherPlugin::initializePlugin() {
Matthias Möller's avatar
Matthias Möller committed
63
64
  if ( OpenFlipper::Options::gui() ) {
    tool_ = new IsotropicRemesherToolBox();
Jan Möbius's avatar
 
Jan Möbius committed
65

Matthias Möller's avatar
Matthias Möller committed
66
67
    QSize size(300, 300);
    tool_->resize(size);
Jan Möbius's avatar
 
Jan Möbius committed
68

Matthias Möller's avatar
Matthias Möller committed
69
    connect(tool_->remeshButton, SIGNAL(clicked()), this, SLOT(slotRemeshButtonClicked()) );
Jan Möbius's avatar
 
Jan Möbius committed
70

Matthias Möller's avatar
Matthias Möller committed
71
72
73
    connect(tool_->minEdgeLength, SIGNAL(clicked()), this, SLOT(slotSetMinLength()) );
    connect(tool_->maxEdgeLength, SIGNAL(clicked()), this, SLOT(slotSetMaxLength()) );
    connect(tool_->meanEdgeLength, SIGNAL(clicked()), this, SLOT(slotSetMeanLength()) );
Jan Möbius's avatar
 
Jan Möbius committed
74

Matthias Möller's avatar
Matthias Möller committed
75
76
77
    toolIcon_ = new QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"remesher.png");
    emit addToolbox( tr("Isotropic Remesher") , tool_, toolIcon_ );
  }
Jan Möbius's avatar
 
Jan Möbius committed
78
79
}

80
81
82
void IsotropicRemesherPlugin::slotRemeshButtonClicked() {
  edgeLength_ = tool_->targetEdgeLength->value();
  
83
84
85
86
87
88
89
  if ( thread_ == 0){
    thread_ = new OpenFlipperThread(name() + "IsotropicRemesh");                         // Create your thread containing a unique id \n
    connect(thread_,SIGNAL( finished(QString)), this,SIGNAL(finishJob(QString)));                           // connect your threads finish info to the global one ( you can do the same for a local one ) \n
    connect(thread_,SIGNAL( function() ), this,SLOT(slotRemesh()),Qt::DirectConnection);           // You can directly give a slot of your app that gets called \n
    connect(this,SIGNAL( finishJob(QString)), this, SLOT(threadFinished(QString)), Qt::QueuedConnection);
  }

90
91
  emit startJob( name() + "IsotropicRemesh", "Isotropic remeshing" , 0 , 100 , true);  // As long as meshes cannot be locked, this thread has to be blocking. Otherwise, during operation the mesh could be deleted. We don't want that!

92
93
  thread_->start();                                                                                       // start thread
  thread_->startProcessing();                                                                             // start processing
94
95
}

Jan Möbius's avatar
 
Jan Möbius committed
96
97
98
99
100
101
102
103
104
105
106

void IsotropicRemesherPlugin::slotRemesh(){

  //read one target objects
  for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::TARGET_OBJECTS,DataType(DATA_TRIANGLE_MESH | DATA_POLY_MESH)) ;
                                        o_it != PluginFunctions::objectsEnd(); ++o_it)  {

    //check dataType
    if ( o_it->dataType(DATA_TRIANGLE_MESH) ) {
      TriMesh* mesh = PluginFunctions::triMesh(o_it);

107
108
109
110
      ProgressEmitter prgEmt(name() + "IsotropicRemesh");
      connect (&prgEmt, SIGNAL(changeDescription(QString,QString)), this, SIGNAL(setJobDescription(QString,QString)) );
      connect (&prgEmt, SIGNAL(signalJobState(QString,int)), this, SIGNAL(setJobState(QString,int)) );
      IsotropicRemesher< TriMesh > remesher(&prgEmt);
Jan Möbius's avatar
 
Jan Möbius committed
111

112
      remesher.remesh(*mesh, edgeLength_);
Jan Möbius's avatar
 
Jan Möbius committed
113
114
115
116
117
118
119
120
121

      mesh->update_normals();

    }else{
      emit log("Remeshing currently only implemented for triangle Meshes");
    }
  }
}

Dirk Wilden's avatar
Dirk Wilden committed
122
void IsotropicRemesherPlugin::threadFinished(QString /*_jobId*/) {
123
124
125
  
  std::cerr << "threadFinished() called" << std::endl;
  
126
127
128
  for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::TARGET_OBJECTS,DataType(DATA_TRIANGLE_MESH | DATA_POLY_MESH)) ;
                                        o_it != PluginFunctions::objectsEnd(); ++o_it)  {

Jan Möbius's avatar
Jan Möbius committed
129
      emit updatedObject( o_it->id(), UPDATE_ALL );
130
131

      emit createBackup(o_it->id(),"Isotropic Remeshing");
Jan Möbius's avatar
Jan Möbius committed
132
  
133
134
135
136
137
      emit updateView();
    
  }
}

Jan Möbius's avatar
 
Jan Möbius committed
138
139
140
141
142
143
void IsotropicRemesherPlugin::slotSetMinLength()
{
  double current = 0.0;
  bool first = true;

  bool ok;
Jan Möbius's avatar
Jan Möbius committed
144
  emit functionExists( "infomeshobject" , "minEdgeLength(int)", ok  ) ;
Jan Möbius's avatar
 
Jan Möbius committed
145
146
147
148
149
150
151
152
  if (!ok)
    return;

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

    if (first)
    {
Jan Möbius's avatar
Jan Möbius committed
153
      current = RPC::callFunctionValue<double>("infomeshobject" , "minEdgeLength",o_it->id() );
Jan Möbius's avatar
 
Jan Möbius committed
154
155
156
      first = false;
    }
    else
Jan Möbius's avatar
Jan Möbius committed
157
      current = qMin (current, RPC::callFunctionValue<double>("infomeshobject" , "minEdgeLength",o_it->id() ));
Jan Möbius's avatar
 
Jan Möbius committed
158
159
160
161
162
163
164
165
166
167
168
169
170
    
  }

  if (!first)
    tool_->targetEdgeLength->setValue (current);
}

void IsotropicRemesherPlugin::slotSetMaxLength()
{
  double current = 0.0;
  bool first = true;

  bool ok;
Jan Möbius's avatar
Jan Möbius committed
171
  emit functionExists( "infomeshobject" , "maxEdgeLength(int)", ok  ) ;
Jan Möbius's avatar
 
Jan Möbius committed
172
173
174
175
176
177
178
179
  if (!ok)
    return;

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

    if (first)
    {
Jan Möbius's avatar
Jan Möbius committed
180
      current = RPC::callFunctionValue<double>("infomeshobject" , "maxEdgeLength",o_it->id() );
Jan Möbius's avatar
 
Jan Möbius committed
181
182
183
      first = false;
    }
    else
Jan Möbius's avatar
Jan Möbius committed
184
      current = qMax (current, RPC::callFunctionValue<double>("infomeshobject" , "maxEdgeLength",o_it->id() ));
Jan Möbius's avatar
 
Jan Möbius committed
185
186
187
188
189
190
191
192
193
194
195
196
197
    
  }

  if (!first)
    tool_->targetEdgeLength->setValue (current);
}

void IsotropicRemesherPlugin::slotSetMeanLength()
{
  double current = 0.0;
  int  div = 0;

  bool ok;
Jan Möbius's avatar
Jan Möbius committed
198
  emit functionExists( "infomeshobject" , "edgeCount(int)", ok  ) ;
Jan Möbius's avatar
 
Jan Möbius committed
199
200
201
202
203
204
205
  if (!ok)
    return;

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


Jan Möbius's avatar
Jan Möbius committed
206
207
208
    current += RPC::callFunctionValue<int>   ("infomeshobject" , "edgeCount",o_it->id()) *
               RPC::callFunctionValue<double>("infomeshobject" , "meanEdgeLength",o_it->id() );
    div     += RPC::callFunctionValue<int>   ("infomeshobject" , "edgeCount",o_it->id()      );
Jan Möbius's avatar
 
Jan Möbius committed
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
  }

  if (div > 0)
    tool_->targetEdgeLength->setValue (current / div);
}


/// Initialize the plugin
void IsotropicRemesherPlugin::pluginsInitialized(){

  emit setSlotDescription("isotropicRemesh(int,double)", "Isotropic Remeshing",
                          QString("object_id,targetEdgeLength").split(","),
                          QString("id of an object, target edge length").split(","));
}


void IsotropicRemesherPlugin::isotropicRemesh(int _objectID, double _targetEdgeLength ){
  BaseObjectData* object  = 0;


  if ( PluginFunctions::getObject(_objectID, object) ){

    //check dataType
    if ( object->dataType(DATA_TRIANGLE_MESH)) {

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

      IsotropicRemesher< TriMesh > remesher;

      remesher.remesh(*mesh, _targetEdgeLength);

      mesh->update_normals();

Jan Möbius's avatar
Jan Möbius committed
242
      emit updatedObject( object->id(), UPDATE_ALL );
Jan Möbius's avatar
Jan Möbius committed
243
      
244
      emit scriptInfo("isotropicRemesh(" + QString::number(_objectID) + ", " + QString::number(_targetEdgeLength) + ")");
Jan Möbius's avatar
 
Jan Möbius committed
245
246
247
248
249
250
251
252
253
254
255
256
257
258

      emit updateView();

      return;

    }else{
      emit log("Remeshing currently only implemented for triangle Meshes");
      return;
    }
  }else{
    emit log("Unable to get object");
  }
}

Matthias Möller's avatar
Matthias Möller committed
259
260
261
262
263
#if QT_VERSION < 0x050000
  Q_EXPORT_PLUGIN2( isotropicRemesherPlugin , IsotropicRemesherPlugin );
#endif


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