MovePlugin.cc 48.4 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
//=============================================================================
//
//                               OpenFlipper
//        Copyright (C) 2008 by Computer Graphics Group, RWTH Aachen
//                           www.openflipper.org
//
//-----------------------------------------------------------------------------
//
//                                License
//
//  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.
15
//
Jan Möbius's avatar
Jan Möbius committed
16
17
18
19
//  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.
20
//
Jan Möbius's avatar
Jan Möbius committed
21
22
23
24
25
26
27
28
29
30
//  You should have received a copy of the GNU Lesser General Public License
//  along with OpenFlipper.  If not, see <http://www.gnu.org/licenses/>.
//
//-----------------------------------------------------------------------------
//
//   $Revision$
//   $Author$
//   $Date$
//
//=============================================================================
Jan Möbius's avatar
 
Jan Möbius committed
31
32
33
34
35
36
37
38
39

#include <QtGui>

#include "MovePlugin.hh"

#include <iostream>
#include <ACG/GL/GLState.hh>
#include <QStringList>
#include <ACG/QtScenegraph/QtTranslationManipulatorNode.hh>
Dirk Wilden's avatar
Dirk Wilden committed
40
#include <ACG/Scenegraph/PlaneNode.hh>
Jan Möbius's avatar
 
Jan Möbius committed
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58

#include <OpenFlipper/BasePlugin/PluginFunctions.hh>
#include <OpenFlipper/common/GlobalOptions.hh>

#include <MeshTools/MeshFunctions.hh>
#include <MeshTools/MeshInfoT.hh>

#ifdef USE_OPENMP
#include <omp.h>
#endif

#ifdef ENABLE_POLYLINE_SUPPORT
#include <ObjectTypes/PolyLine/PolyLine.hh>
#endif



/** \brief Default Constructor
59
 *
Jan Möbius's avatar
 
Jan Möbius committed
60
 */
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
61
MovePlugin::MovePlugin() :
62
63
64
 manMode_ (QtTranslationManipulatorNode::TranslationRotation),
 contextAction_(0),
 toAllTargets_(0)
Jan Möbius's avatar
 
Jan Möbius committed
65
66
67
68
69
70
71
72
73
{
    manip_size_          = 1.0;
    manip_size_modifier_ = 1.0;

    selectionType_ = VERTEX;
    selectionConnected_ = false;

    axisA_ = 0;
    axisB_ = 1;
74
75

    hide_ = true;
76
    allTargets_ = false;
Jan Möbius's avatar
 
Jan Möbius committed
77
78
79
80
81
82
83
84
}


/*******************************************************************************
        BaseInterface implementation
 *******************************************************************************/

/** \brief Initialization of the plugin when it is loaded by the core
85
 *
Jan Möbius's avatar
 
Jan Möbius committed
86
87
88
89
90
91
92
93
94
95
 */
void MovePlugin::pluginsInitialized() {

  //PICKMODES
  emit addPickMode("Separator");
  emit addHiddenPickMode("Move");
  emit setPickModeMouseTracking ("Move", true);
  emit addHiddenPickMode("MoveSelection");
  emit setPickModeMouseTracking ("MoveSelection", true);

Jan Möbius's avatar
Dennis:    
Jan Möbius committed
96
97
98
99
100
101
  //KEYS
  emit registerKey (Qt::Key_Shift, Qt::ShiftModifier, "Manipulator rotation", true);
  emit registerKey (Qt::Key_Shift, Qt::NoModifier, "Manipulator rotation", true);
  emit registerKey (Qt::Key_Control, Qt::ControlModifier, "Resize", true);
  emit registerKey (Qt::Key_Control, Qt::NoModifier, "Resize", true);

Jan Möbius's avatar
 
Jan Möbius committed
102
103
  //SCRIPTING SLOT DESCRIPTIONS
  setDescriptions();
104

105
  // CONTEXT MENU
106
107
108
109
110
  toAllTargets_ = new QAction("Apply to all targets", this);
  toAllTargets_->setCheckable(true);
  toAllTargets_->setToolTip("Apply transformation to all target objects");
  toAllTargets_->setStatusTip( toAllTargets_->toolTip() );

111
112
113
  contextAction_ = new QAction("Set properties", this);
  contextAction_->setToolTip("Set properties");
  contextAction_->setStatusTip( contextAction_->toolTip() );
114

115
  emit addContextMenuItem(toAllTargets_ , CONTEXTNODEMENU );
116
  emit addContextMenuItem(contextAction_ , CONTEXTNODEMENU );
117

118
119
  connect(toAllTargets_,SIGNAL(toggled(bool) ),this,SLOT(setAllTargets(bool)));

120
  connect( contextAction_ , SIGNAL( triggered() ),
121
	   this, SLOT(showProps()) );
Jan Möbius's avatar
 
Jan Möbius committed
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143

  //TOOLBAR
  toolbar_ = new QToolBar("Transform and Move");

  toolBarActions_ = new QActionGroup(toolbar_);

  moveAction_ = new QAction(tr("&Move objects"), toolBarActions_);
  std::cerr << "Todo : Show all manipulators placed" << std::endl;
  moveAction_->setStatusTip(tr("Move object in 3D."));
  moveAction_->setIcon(QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"move-objects.png") );
  moveAction_->setCheckable(true);
  toolbar_->addAction(moveAction_);

  moveSelectionAction_ = new QAction(tr("Move selections on objects"), toolBarActions_);
  moveSelectionAction_->setStatusTip(tr("Move selections in 3D."));
  moveSelectionAction_->setIcon(QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"move-selections.png") );
  moveSelectionAction_->setCheckable(true);
  toolbar_->addAction(moveSelectionAction_);

  connect(toolBarActions_, SIGNAL(triggered(QAction*)), this, SLOT(slotSetMoveMode(QAction*)) );

  emit addToolbar(toolbar_);
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194

  pickToolbar_ = new QToolBar("Transform and Move");
  pickToolBarActions_ = new QActionGroup(pickToolbar_);
  pickToolBarActions_->setExclusive (false);

  placeAction_ = new QAction(tr("Place manipulator"), pickToolBarActions_);
  placeAction_->setStatusTip(tr("Place manipulator on object. <Doubleclick>"));
  placeAction_->setIcon(QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"move-place.png") );
  placeAction_->setCheckable(true);
  pickToolbar_->addAction(placeAction_);

  pickToolbar_->addSeparator ();

  ratateTranslateAction_ = new QAction(tr("Rotate/Translate object"), pickToolBarActions_);
  ratateTranslateAction_->setStatusTip(tr("Rotate/Translate object."));
  ratateTranslateAction_->setIcon(QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"move-translaterotate.png") );
  ratateTranslateAction_->setCheckable(true);
  ratateTranslateAction_->setChecked(true);
  pickToolbar_->addAction(ratateTranslateAction_);

  resizeAction_ = new QAction(tr("Resize object"), pickToolBarActions_);
  resizeAction_->setStatusTip(tr("Resize object. <Control>"));
  resizeAction_->setIcon(QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"move-resize.png") );
  resizeAction_->setCheckable(true);
  pickToolbar_->addAction(resizeAction_);

  pickToolbar_->addSeparator ();

  ratateManipAction_ = new QAction(tr("Rotate manipulator"), pickToolBarActions_);
  ratateManipAction_->setStatusTip(tr("Rotate manipulator. <Shift>"));
  ratateManipAction_->setIcon(QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"move-maniprotate.png") );
  ratateManipAction_->setCheckable(true);
  pickToolbar_->addAction(ratateManipAction_);

  smallerManipAction_ = new QAction(tr("Decrease size of manipulator"), pickToolBarActions_);
  smallerManipAction_->setStatusTip(tr("Make manipulator smaller. <Mouse wheel up>"));
  smallerManipAction_->setIcon(QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"move-manipsmall.png") );
  smallerManipAction_->setCheckable(false);
  pickToolbar_->addAction(smallerManipAction_);

  biggerManipAction_ = new QAction(tr("&Increase size of manipulator"), pickToolBarActions_);
  biggerManipAction_->setStatusTip(tr("Make manipulator bigger. <Mouse wheel down>"));
  biggerManipAction_->setIcon(QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"move-manipbig.png") );
  biggerManipAction_->setCheckable(false);
  pickToolbar_->addAction(biggerManipAction_);

  connect(pickToolBarActions_, SIGNAL(triggered(QAction*)), this, SLOT(slotPickToolbarAction(QAction*)) );

  emit setPickModeToolbar ("Move", pickToolbar_);
  emit setPickModeToolbar ("MoveSelection", pickToolbar_);

Jan Möbius's avatar
 
Jan Möbius committed
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
}


/*******************************************************************************
        ToolBoxInterface implementation
 *******************************************************************************/

/** \brief Create the move toolbox-widget and return a reference to it
 *
 * @param _widget A reference to the move toolbox that we will return
 * @return return wether the widget was successfully generated
 */
bool MovePlugin::initializeToolbox(QWidget*& _widget)
{
   toolboxActive_ = false;
   tool_ = new moveToolbarWidget();
Jan Möbius's avatar
Jan Möbius committed
211
   tool_ -> setMaximumWidth(300);
Jan Möbius's avatar
 
Jan Möbius committed
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
   _widget = tool_;

   connect(tool_->moveToOrigin,SIGNAL(clicked() ),this,SLOT(slotMoveToOrigin()));

   tool_->moveToOrigin->setIcon( QIcon(OpenFlipper::Options::iconDirStr() + OpenFlipper::Options::dirSeparator() + "moveToCOG.png") );
   tool_->moveToOrigin->setIconSize(QSize(48,48));

   connect(tool_->unifyBoundingBoxDiagonal,SIGNAL(clicked() ),this,SLOT(slotUnifyBoundingBoxDiagonal()));
   tool_->unifyBoundingBoxDiagonal->setIcon( QIcon(OpenFlipper::Options::iconDirStr() + OpenFlipper::Options::dirSeparator() + "unifyBB.png") );
   tool_->unifyBoundingBoxDiagonal->setIconSize(QSize(48,48));

   lastActiveManipulator_ = -1;

   return true;
}


/*******************************************************************************
        MouseInterface implementation
 *******************************************************************************/

/** \brief MouseWheel event occured
234
 *
Jan Möbius's avatar
 
Jan Möbius committed
235
236
237
238
239
240
241
 * @param _event the event that occured
 */
void MovePlugin::slotMouseWheelEvent(QWheelEvent * _event, const std::string & /*_mode*/)
{
   manip_size_modifier_ = manip_size_modifier_ - (float)_event->delta() / 120.0 * 0.1;
   for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::ALL_OBJECTS) ; o_it != PluginFunctions::objectsEnd(); ++o_it)
         o_it->manipulatorNode()->set_size(manip_size_ * manip_size_modifier_);
Dirk Wilden's avatar
Dirk Wilden committed
242
243
244
245

   for (uint i=0; i < additionalManipulators_.size(); i++)
     (additionalManipulators_[i])->set_size(manip_size_ * manip_size_modifier_);

Jan Möbius's avatar
Dennis:    
Jan Möbius committed
246
   emit visibilityChanged (-1);
Jan Möbius's avatar
 
Jan Möbius committed
247
248
249
250
251
252
}


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

/** \brief MousePress event occured
253
 *
Jan Möbius's avatar
 
Jan Möbius committed
254
255
256
257
 * @param _event the event that occured
 */
void MovePlugin::slotMouseEvent( QMouseEvent* _event )
{
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
258
259
  if (((PluginFunctions::pickMode() == ("Move")) || (PluginFunctions::pickMode() == ("MoveSelection"))) &&
      PluginFunctions::actionMode() == Viewer::PickingMode) {
Jan Möbius's avatar
 
Jan Möbius committed
260

261
    if (_event->type() == QEvent::MouseButtonDblClick ||
Jan Möbius's avatar
Jan Möbius committed
262
        (_event->type() == QEvent::MouseButtonPress &&
263
264
         _event->button () == Qt::LeftButton &&
         placeAction_->isChecked ())) {
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
265
266

      placeManip(_event);
Dirk Wilden's avatar
Dirk Wilden committed
267
      placeAction_->setChecked(false);
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
268
269
270
      updateManipulatorDialog();
      return;
    }
Jan Möbius's avatar
 
Jan Möbius committed
271

Jan Möbius's avatar
Dennis:    
Jan Möbius committed
272
273
274
    // interaction
    ACG::SceneGraph::MouseEventAction action(_event);
    PluginFunctions::traverse(action);
Jan Möbius's avatar
 
Jan Möbius committed
275

Jan Möbius's avatar
Dennis:    
Jan Möbius committed
276
277
    if (_event->buttons() == Qt::LeftButton)
      emit visibilityChanged (-1);
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
278

Jan Möbius's avatar
Dennis:    
Jan Möbius committed
279
280
281
282
283
284
285
286
287
288
  }
}

/*******************************************************************************
        KeyInterface implementation
 *******************************************************************************/

void MovePlugin::slotKeyEvent (QKeyEvent* _event)
{
  if (_event->key() == Qt::Key_Control)
289
    setManipMode (QtTranslationManipulatorNode::Resize);
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
290
  else if (_event->key () == Qt::Key_Shift)
291
    setManipMode (QtTranslationManipulatorNode::LocalRotation);
Jan Möbius's avatar
 
Jan Möbius committed
292
293
}

Jan Möbius's avatar
Dennis:    
Jan Möbius committed
294
295
296
297
298
299
//------------------------------------------------------------------------------

void MovePlugin::slotKeyReleaseEvent (QKeyEvent* _event)
{
  if ((_event->key() == Qt::Key_Control && manMode_ == QtTranslationManipulatorNode::Resize) ||
      (_event->key() == Qt::Key_Shift && manMode_ == QtTranslationManipulatorNode::LocalRotation))
300
    setManipMode (QtTranslationManipulatorNode::TranslationRotation);
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
301
}
Jan Möbius's avatar
 
Jan Möbius committed
302
303
304
305
306
307

/*******************************************************************************
        PickingInterface implementation
 *******************************************************************************/

/** \brief slot is called when the pickMode changed
308
 *
Jan Möbius's avatar
 
Jan Möbius committed
309
310
311
312
 * @param _mode new pickMode
 */
void MovePlugin::slotPickModeChanged( const std::string& _mode)
{
313
314
315
316
	moveAction_->setChecked(_mode == "Move");
	moveSelectionAction_->setChecked(_mode == "MoveSelection");

	hide_ = !(_mode == "Move" || _mode == "MoveSelection");
Jan Möbius's avatar
 
Jan Möbius committed
317

318
	showManipulators();
Jan Möbius's avatar
 
Jan Möbius committed
319
320
321
322
323
324
325
326
}


/*******************************************************************************
        MovePlugin implementation
 *******************************************************************************/

/** \brief Move object with given transformation matrix
327
 *
Jan Möbius's avatar
 
Jan Möbius committed
328
329
330
331
332
 * @param mat transformation matrix
 * @param _id id of the object
 */
void MovePlugin::moveObject(ACG::Matrix4x4d mat, int _id) {
  BaseObjectData* object;
Dirk Wilden's avatar
Dirk Wilden committed
333
  if ( ! PluginFunctions::getObject(_id,object) )
Jan Möbius's avatar
 
Jan Möbius committed
334
335
    return;

336
337
338
339
340
341
342
343
344
345
346
347
  if  ( object->dataType()  == DATA_TRIANGLE_MESH ) {
    transformMesh(mat , *PluginFunctions::triMesh(object) );
  } else  if  ( object->dataType()  == DATA_POLY_MESH ) {
    transformMesh(mat , *PluginFunctions::polyMesh(object) );
  #ifdef ENABLE_POLYLINE_SUPPORT
  } else  if  ( object->dataType()  == DATA_POLY_LINE ) {
    transformPolyLine(mat , *PluginFunctions::polyLine(object) );
  #endif
  } else {

    emit log(LOGERR,"moveObject called for unsupported Object Type");
    return;
Jan Möbius's avatar
 
Jan Möbius committed
348
349
350
351
352
353
354
355
356
357
  }

  emit updatedObject(_id);
  emit createBackup(_id,"Move");
}


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

/** \brief Move selection on an object with given transformation matrix
358
 *
Jan Möbius's avatar
 
Jan Möbius committed
359
360
361
362
 * Which Selection (Vertex, Edge, Face) is used is determined by the
 * current SelectionMode in SelectionPlugin.
 * If the Plugin is unable to communicate with SelectionPlugin, Vertex Selection is used.
 *
363
364
 * @param mat
 * @param _id
Jan Möbius's avatar
 
Jan Möbius committed
365
366
367
368
369
370
371
372
373
374
375
376
377
378
 */
void MovePlugin::moveSelection(ACG::Matrix4x4d mat, int _id) {

  if (selectionType_ == VERTEX)
    transformVertexSelection( _id , mat );
  else if (selectionType_ == FACE)
    transformFaceSelection( _id , mat );
  else if (selectionType_ == EDGE)
    transformEdgeSelection( _id , mat );

  emit updatedObject(_id);
//   emit createBackup(_id,"MoveSelection");
}

379
380
//------------------------------------------------------------------------------

381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/** \brief Set the manipulator manipulation mode
 *
 * @param _mode Mode
 */

void MovePlugin::setManipMode (QtTranslationManipulatorNode::ManipulatorMode _mode)
{
  if (_mode != manMode_)
  {
    manMode_ = _mode;
    if ((PluginFunctions::pickMode() == "Move" ) || (PluginFunctions::pickMode() == "MoveSelection" )) {
        for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::ALL_OBJECTS) ;
              o_it != PluginFunctions::objectsEnd(); ++o_it)
           if ( o_it->manipPlaced() )
                o_it->manipulatorNode()->setMode (_mode);
    }
    switch (manMode_)
    {
      case QtTranslationManipulatorNode::Resize:
        resizeAction_->setChecked (true);
        ratateManipAction_->setChecked (false);
        ratateTranslateAction_->setChecked (false);
403
        PluginFunctions::setViewObjectMarker (PluginFunctions::defaultViewObjectMarker ());
404
405
406
407
408
        break;
      case QtTranslationManipulatorNode::LocalRotation:
        resizeAction_->setChecked (false);
        ratateManipAction_->setChecked (true);
        ratateTranslateAction_->setChecked (false);
409
        PluginFunctions::setViewObjectMarker (&objectMarker_);
410
411
412
413
414
        break;
      case QtTranslationManipulatorNode::TranslationRotation:
        resizeAction_->setChecked (false);
        ratateManipAction_->setChecked (false);
        ratateTranslateAction_->setChecked (true);
415
        PluginFunctions::setViewObjectMarker (PluginFunctions::defaultViewObjectMarker ());
416
417
418
419
420
421
422
        break;
    }
  }
}

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

423
/** \brief Hide context menu entry when right clicking on node other than manipulator node
424
 *
425
426
427
 * @param _nodeId Identifier of node that has been clicked
 */
void MovePlugin::slotUpdateContextMenuNode( int _nodeId ) {
428

429
    ACG::SceneGraph::BaseNode* node = ACG::SceneGraph::find_node( PluginFunctions::getSceneGraphRootNode(), _nodeId );
430

431
    if (node == 0) return;
432

433
434
435
436
437
438
439
    if(node->className() != "QtTranslationManipulatorNode") {
	contextAction_->setVisible(false);
    }
    else {
	contextAction_->setVisible(true);
    }
}
Jan Möbius's avatar
 
Jan Möbius committed
440
441
442
443

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

/** \brief move the object when its manipulator moves
444
 *
Jan Möbius's avatar
 
Jan Möbius committed
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
 * @param _node the manipulator node
 * @param _event the mouse event
 */
void MovePlugin::manipulatorMoved( QtTranslationManipulatorNode* _node , QMouseEvent* _event) {

  // React on event only in move mode
  if ( PluginFunctions::pickMode() != "Move" && PluginFunctions::pickMode() != "MoveSelection" )
    return;

  OpenFlipper::Options::redrawDisabled( true );

  // Apply changes only on Release for moveMode and after every movement in MoveSelection Mode
  if ( (_event->type() == QEvent::MouseButtonRelease) || (PluginFunctions::pickMode() == "MoveSelection") ) {

    int objectId = _node->getIdentifier();

    ACG::Matrix4x4d mat;
    mat.identity();
    mat = _node->matrix();

    // Reset Node
    _node->loadIdentity();
    _node->set_center(mat.transform_point(_node->center()));

    // Always move the object which corresponds to the manipulator
    if (PluginFunctions::pickMode() != "MoveSelection")
      moveObject( mat, objectId );
    else
      moveSelection( mat, objectId );

475
476
477
478
479
480
481
482
483
484
485
486
487
    if(allTargets_) {
		for (PluginFunctions::ObjectIterator o_it(
				PluginFunctions::TARGET_OBJECTS); o_it
				!= PluginFunctions::objectsEnd(); ++o_it) {
			if ((o_it->id() != objectId)
					&& !o_it->manipulatorNode()->visible()) { // If it has its own manipulator active, dont move it
				if (PluginFunctions::pickMode() != "MoveSelection")
					moveObject(mat, o_it->id());
				else
					moveSelection(mat, o_it->id());
			}
		}
	}
Jan Möbius's avatar
 
Jan Möbius committed
488
489
490
491
492
493
494
495
496
497
498
499
500

    lastActiveManipulator_ = objectId;
    updateManipulatorDialog();
  }

  OpenFlipper::Options::redrawDisabled( false );

}


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

/** \brief update object when its manipulator changes position
501
 *
Jan Möbius's avatar
 
Jan Möbius committed
502
503
504
505
506
507
508
 * @param _node the manipulator node
 */
void MovePlugin::ManipulatorPositionChanged(QtTranslationManipulatorNode* _node ) {

  // Position has been changed of the manipulator by a direct function
  int objectId = _node->getIdentifier();

Dirk Wilden's avatar
Dirk Wilden committed
509
510
511
512
  if ( objectId > 0 ){

    BaseObjectData* object;
    PluginFunctions::getObject(objectId,object);
Jan Möbius's avatar
 
Jan Möbius committed
513

Dirk Wilden's avatar
Dirk Wilden committed
514
515
516
    // Assume that it has a good position now
    object->manipPlaced( true );
  }
Jan Möbius's avatar
 
Jan Möbius committed
517
518
519
520
521
522
523
524
525
526
527
528
529
530

  // Show manipulator only if in Move mode
  if ( PluginFunctions::pickMode() == "Move" )
    _node->show();

  lastActiveManipulator_ = objectId;
  updateManipulatorDialog();

}


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

/** \brief Place and show the Manipulator
531
 *
Jan Möbius's avatar
 
Jan Möbius committed
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
 * @param _event  mouseEvent that occured
 */
void MovePlugin::placeManip(QMouseEvent * _event)
{
   unsigned int  node_idx, target_idx;
   OpenMesh::Vec3d         hitPoint;

   if ( PluginFunctions::scenegraphPick(ACG::SceneGraph::PICK_ANYTHING,_event->pos(), node_idx, target_idx,&hitPoint) ) {

      BaseObjectData* object;

      if ( PluginFunctions::getPickedObject(node_idx, object) ) {
         object->manipPlaced( true );

         if ( !object->picked(node_idx) ) {
            emit log(LOGWARN,"Picking failed");
         }

         object->manipulatorNode()->loadIdentity();
         object->manipulatorNode()->set_center(hitPoint);
         object->manipulatorNode()->set_draw_cylinder(true);
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
553
         object->manipulatorNode()->set_autosize(QtTranslationManipulatorNode::Once);
Jan Möbius's avatar
 
Jan Möbius committed
554
         object->manipulatorNode()->set_size(manip_size_ * manip_size_modifier_);
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
555
         object->manipulatorNode()->setMode (manMode_);
Jan Möbius's avatar
 
Jan Möbius committed
556
557
558
559
560
561
562
563
564
565
566
567
568
569
         object->manipulatorNode()->show();

         object->manipulatorNode()->apply_transformation( PluginFunctions::pickMode() == "Move" );

         connect(object->manipulatorNode() , SIGNAL(manipulatorMoved(QtTranslationManipulatorNode*,QMouseEvent*)),
                 this                      , SLOT(  manipulatorMoved(QtTranslationManipulatorNode*,QMouseEvent*)));

         connect(object->manipulatorNode() , SIGNAL(positionChanged(QtTranslationManipulatorNode*)),
                 this                      , SLOT(  ManipulatorPositionChanged(QtTranslationManipulatorNode*)));

         lastActiveManipulator_ = object->id();

         emit updateView();
      } else {
Dirk Wilden's avatar
Dirk Wilden committed
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602

         ACG::SceneGraph::BaseNode* node = ACG::SceneGraph::find_node( PluginFunctions::getSceneGraphRootNode(), node_idx );

         ACG::SceneGraph::PlaneNode* planeNode = dynamic_cast< ACG::SceneGraph::PlaneNode* > (node);

         if (planeNode){

            ACG::SceneGraph::QtTranslationManipulatorNode* manipNode = dynamic_cast< ACG::SceneGraph::QtTranslationManipulatorNode* > (node->parent());

            if (manipNode != 0){

              manipNode->loadIdentity();
              manipNode->set_center(hitPoint);
              manipNode->set_draw_cylinder(true);
              manipNode->set_autosize(QtTranslationManipulatorNode::Once);
              manipNode->set_size(manip_size_ * manip_size_modifier_);
              manipNode->setMode (manMode_);
              manipNode->show();

              manipNode->apply_transformation( PluginFunctions::pickMode() == "Move" );

              connect(manipNode , SIGNAL(manipulatorMoved(QtTranslationManipulatorNode*,QMouseEvent*)),
                      this      , SLOT(  manipulatorMoved(QtTranslationManipulatorNode*,QMouseEvent*)));

              connect(manipNode , SIGNAL(positionChanged(QtTranslationManipulatorNode*)),
                      this      , SLOT(  ManipulatorPositionChanged(QtTranslationManipulatorNode*)));

              additionalManipulators_.push_back( manipNode );
            }

         } else {
           emit log(LOGERR,"Picking ok, but Object was not found in the Object list");
         }
Jan Möbius's avatar
 
Jan Möbius committed
603
604
605
606
607
608
609
610
611
612
613
614
      }

   } else {
       emit log(LOGWARN,"Picking failed");
   }

}


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

/** \brief Checks if the manipulators should be visible or not
615
 *
Jan Möbius's avatar
 
Jan Möbius committed
616
617
618
 */
void MovePlugin::showManipulators( )
{
Dirk Wilden's avatar
Dirk Wilden committed
619

620
621
622
623
624
625
626
627
628
629
	if (!hide_ && (toolboxActive_ || (PluginFunctions::pickMode() == "Move")
			|| (PluginFunctions::pickMode() == "MoveSelection"))) {
		for (PluginFunctions::ObjectIterator o_it(PluginFunctions::ALL_OBJECTS); o_it
				!= PluginFunctions::objectsEnd(); ++o_it)

			if (o_it->manipPlaced()) {
				o_it->manipulatorNode()->show();
				o_it->manipulatorNode()->apply_transformation(
						PluginFunctions::pickMode() == "Move");
			}
Dirk Wilden's avatar
Dirk Wilden committed
630
631
632
633

      for (uint i=0; i < additionalManipulators_.size(); i++)
        (additionalManipulators_[i])->show();

634
635
636
637
638
	} else {
		for (PluginFunctions::ObjectIterator o_it(PluginFunctions::ALL_OBJECTS); o_it
				!= PluginFunctions::objectsEnd(); ++o_it)

			o_it->manipulatorNode()->hide();
Dirk Wilden's avatar
Dirk Wilden committed
639
640
641
642
643
644
645
646
647
648
649
650
651
652

    for (uint i=0; i < additionalManipulators_.size(); i++){
      //apply the transformation for planeNode
      ACG::SceneGraph::BaseNode* node = (additionalManipulators_[i])->find("Plane");

      if (node){
        ACG::SceneGraph::PlaneNode* plane = dynamic_cast< ACG::SceneGraph::PlaneNode* > (node);

        if (plane){

          plane->setPosition( plane->position(), plane->normal() );

          (additionalManipulators_[i])->set_center( (additionalManipulators_[i])->matrix().transform_point( (additionalManipulators_[i])->center() ) );
          (additionalManipulators_[i])->loadIdentity();
653

Dirk Wilden's avatar
Dirk Wilden committed
654
655
656
657
658
        }
      }

      (additionalManipulators_[i])->hide();
    }
659
	}
Jan Möbius's avatar
 
Jan Möbius committed
660

661
	emit updateView();
Jan Möbius's avatar
 
Jan Möbius committed
662
663
664

}

665
//------------------------------------------------------------------------------
Jan Möbius's avatar
 
Jan Möbius committed
666

667
/** \brief Get Dialog Widget that contains the button
668
 *
669
670
671
672
673
674
675
 * @param _but Reference to QPushButton object that has been pressed
 */
movePropsWidget* MovePlugin::getDialogFromButton(QPushButton* _but) {

    if(_but == 0) return 0;
    return dynamic_cast<movePropsWidget*>((((_but->parentWidget())->parentWidget())->parentWidget()));
}
Jan Möbius's avatar
 
Jan Möbius committed
676
677
678
//------------------------------------------------------------------------------

/** \brief Position of manipulator in tab changed
679
 *
Jan Möbius's avatar
 
Jan Möbius committed
680
681
 */
void MovePlugin::slotSetPosition() {
682

683
684
685
    QPushButton* but = dynamic_cast<QPushButton*>(QObject::sender());
    movePropsWidget* pW = getDialogFromButton(but);
    if(pW == 0) return;
686

687
    TriMesh::Point newpos;
688

689
690
691
692
693
694
695
    bool ok = false;
    newpos[0] =  (pW->nposx->text()).toDouble(&ok);
    if ( !ok ) { emit log(LOGERR,"Wrong Format for X Coordinate"); return; }
    newpos[1] =  (pW->nposy->text()).toDouble(&ok);
    if ( !ok ) { emit log(LOGERR,"Wrong Format for Y Coordinate"); return; }
    newpos[2] =  (pW->nposz->text()).toDouble(&ok);
    if ( !ok ) { emit log(LOGERR,"Wrong Format for Z Coordinate"); return; }
696

697
698
699
700
701
702
703
    BaseObjectData* object;
    if ( PluginFunctions::getObject(lastActiveManipulator_ , object) ) {
	if (  object->manipulatorNode()->visible() )
	    object->manipulatorNode()->set_center( newpos );
	updateManipulatorDialog();
	emit updateView();
    }
Jan Möbius's avatar
 
Jan Möbius committed
704
705
706
707
708
709
}


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

/** \brief Toggle the first axis for changing direction in tab
710
 *
Jan Möbius's avatar
 
Jan Möbius committed
711
712
 */
void MovePlugin::slotToggleAxisA() {
713

714
715
716
    QPushButton* but = dynamic_cast<QPushButton*>(QObject::sender());
    movePropsWidget* pW = getDialogFromButton(but);
    if(pW == 0) return;
Jan Möbius's avatar
 
Jan Möbius committed
717
718

    axisA_ = (axisA_ + 1) % 3;
719

720
721
    if (axisA_ == axisB_)
	axisA_ = (axisA_ + 1) % 3;
722

723
724
725
726
727
728
    switch(axisA_){
	case 0: pW->axisAButton->setText("X Direction"); break;
	case 1: pW->axisAButton->setText("Y Direction"); break;
	case 2: pW->axisAButton->setText("Z Direction"); break;
	default: break;
    }
Jan Möbius's avatar
 
Jan Möbius committed
729
730
731
732
733
734
}


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

/** \brief Toggle the second axis for changing direction in tab
735
 *
Jan Möbius's avatar
 
Jan Möbius committed
736
737
 */
void MovePlugin::slotToggleAxisB() {
738

739
740
741
    QPushButton* but = dynamic_cast<QPushButton*>(QObject::sender());
    movePropsWidget* pW = getDialogFromButton(but);
    if(pW == 0) return;
Jan Möbius's avatar
 
Jan Möbius committed
742
743

    axisB_ = (axisB_ + 1) % 3;
744

745
746
    if (axisA_ == axisB_)
	axisB_ = (axisB_ + 1) % 3;
747

748
749
750
751
752
753
    switch(axisB_){
	case 0: pW->axisBButton->setText("X Direction"); break;
	case 1: pW->axisBButton->setText("Y Direction"); break;
	case 2: pW->axisBButton->setText("Z Direction"); break;
	default: break;
    }
Jan Möbius's avatar
 
Jan Möbius committed
754
755
756
757
758
759
}


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

/** \brief Set Direction of manipulator in tab changed
760
 *
Jan Möbius's avatar
 
Jan Möbius committed
761
762
 */
void MovePlugin::slotSetDirection() {
763

764
765
766
    QPushButton* but = dynamic_cast<QPushButton*>(QObject::sender());
    movePropsWidget* pW = getDialogFromButton(but);
    if(pW == 0) return;
Jan Möbius's avatar
 
Jan Möbius committed
767

768
769
770
    ACG::Vec3d newdirA,newdirB;
    ACG::Vec3d dirX,dirY;
    ACG::Vec3d dirZ(0.0,0.0,0.0);
771

772
773
774
775
776
777
778
    bool ok = false;
    newdirA[0] =  (pW->ndirAx->text()).toDouble(&ok);
    if ( !ok ) { emit log(LOGERR,"Wrong Format for X Coordinate");  return; }
    newdirA[1] =  (pW->ndirAy->text()).toDouble(&ok);
    if ( !ok ) { emit log(LOGERR,"Wrong Format for Y Coordinate"); return; }
    newdirA[2] =  (pW->ndirAz->text()).toDouble(&ok);
    if ( !ok ) { emit log(LOGERR,"Wrong Format for Z Coordinate"); return; }
779

780
781
782
783
784
785
    newdirB[0] =  (pW->ndirBx->text()).toDouble(&ok);
    if ( !ok ) { emit log(LOGERR,"Wrong Format for X Coordinate");  return; }
    newdirB[1] =  (pW->ndirBy->text()).toDouble(&ok);
    if ( !ok ) { emit log(LOGERR,"Wrong Format for Y Coordinate"); return; }
    newdirB[2] =  (pW->ndirBz->text()).toDouble(&ok);
    if ( !ok ) { emit log(LOGERR,"Wrong Format for Z Coordinate"); return; }
786

787
788
    bool xAxis = false;
    bool yAxis = false;
789

790
791
792
793
794
    switch(axisA_){
	case  0: dirX = newdirA; xAxis = true; break;
	case  1: dirY = newdirA; yAxis = true; break;
	default: dirZ = newdirA; break;
    }
795

796
797
798
799
800
    switch(axisB_){
	case  0: dirX = newdirB; xAxis = true; break;
	case  1: dirY = newdirB; yAxis = true; break;
	default: dirZ = newdirB; break;
    }
801

802
803
    if (!xAxis)
	dirX = dirY % dirZ;
804

805
806
    if (!yAxis)
	dirY = dirX % dirZ;
807
808


809
810
811
812
    if ( (dirX | dirY) != 0.0){
	emit log(LOGERR,"The axes of the new direction have to be orthogonal");
	return;
    }
813

Jan Möbius's avatar
Jan Möbius committed
814
815
816
817
818
819
820
821
822
//    // Apply to All Target Objects
//     if ( pW->targetObjects->isChecked() ) {
// 	for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::TARGET_OBJECTS) ;
//              o_it PluginFunctions::objectsEnd(); ++o_it){
//
// 	    o_it->manipulatorNode()->set_direction( dirX, dirY );
// 	}
//     }

823
824
825
    BaseObjectData* object = pW->getBaseObjectData();
    if ( object != 0 ) {
        if (  object->manipulatorNode()->visible() ){
Jan Möbius's avatar
 
Jan Möbius committed
826
827

            object->manipulatorNode()->set_direction( dirX, dirY );
828
829
	}
    } else return;
830

831
832
    updateManipulatorDialog();
    emit updateView();
Jan Möbius's avatar
 
Jan Möbius committed
833
834
835
836
837
838
}


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

/** \brief perform a translation for Manipulator in tab
839
 *
Jan Möbius's avatar
 
Jan Möbius committed
840
841
 */
void MovePlugin::slotTranslation() {
842

843
844
845
    QPushButton* but = dynamic_cast<QPushButton*>(QObject::sender());
    movePropsWidget* pW = getDialogFromButton(but);
    if(pW == 0) return;
Jan Möbius's avatar
 
Jan Möbius committed
846

847
    ACG::Vec3d translation;
848

849
850
851
852
853
854
855
    bool ok = false;
    translation[0] =  (pW->translationX->text()).toDouble(&ok);
    if ( !ok ) { emit log(LOGERR,"Wrong Format for X Coordinate");  return; }
    translation[1] =  (pW->translationY->text()).toDouble(&ok);
    if ( !ok ) { emit log(LOGERR,"Wrong Format for Y Coordinate"); return; }
    translation[2] =  (pW->translationZ->text()).toDouble(&ok);
    if ( !ok ) { emit log(LOGERR,"Wrong Format for Z Coordinate"); return; }
856

Jan Möbius's avatar
Jan Möbius committed
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
//	// Apply to All Target Objects
//	if ( pW->targetObjects->isChecked() ) {
//
//		int manipcount = 0; // Check how many of the target meshes have an visible manipulator
//		int targets = 0; // Count the number of target meshes
//		for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::TARGET_OBJECTS); o_it != PluginFunctions::objectsEnd(); ++o_it) {
//			++targets;
//			if ( ! o_it->manipulatorNode()->hidden() ) {
//				++ manipcount;
//			}
//		}
//
//		if (manipcount == 0 ) // No manipulator -> no translation
//		return;
//
//		for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::TARGET_OBJECTS,DataType( DATA_TRIANGLE_MESH | DATA_POLY_MESH ));
//				o_it != PluginFunctions::objectsEnd(); ++o_it) {
//			if ( manipcount > 1 ) { // Use manipulator direction for each target mesh
//				if ( o_it->manipulatorNode()->hidden() )
//				continue;
//			}
//
//			translate( o_it->id() , translation );
//
//			o_it->manipulatorNode()->set_center( o_it->manipulatorNode()->center() + translation );
//			emit createBackup(o_it->id(),"Translation");
//			emit updatedObject(o_it->id());
//		}
//
//	}

888
    BaseObjectData* object = pW->getBaseObjectData();
889
	if (object != 0) {
890
		if (object->manipulatorNode()->visible()) {
Jan Möbius's avatar
 
Jan Möbius committed
891

892
			translate(object->id(), translation);
893

894
895
896
897
898
899
900
			object->manipulatorNode()->set_center(
					object->manipulatorNode()->center() + translation);
			emit createBackup(object->id(), "Translation");
			emit updatedObject(object->id());
		}
	} else {
		return;
901
	}
Jan Möbius's avatar
 
Jan Möbius committed
902

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

904
905
906
    updateManipulatorDialog();
    emit scriptInfo(QString("slotTranslation()"));
    emit updateView();
Jan Möbius's avatar
 
Jan Möbius committed
907
908
909
910
911
912
}


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

/** \brief Project the current manipulator onto the tangent plane of the object
913
 *
Jan Möbius's avatar
 
Jan Möbius committed
914
915
 */
void MovePlugin::slotProjectToTangentPlane() {
916

917
918
919
920
    QPushButton* but = dynamic_cast<QPushButton*>(QObject::sender());
    movePropsWidget* pW = getDialogFromButton(but);
    if(pW == 0) return;

921
922
923
    if ( allTargets_ ) {
    	emit log(LOGWARN,"TODO Project for multiple targets");
    	return;
924
    } else {
925
926
    	emit log(LOGWARN,"TODO Project for one target");
    	return;
927
    }
Jan Möbius's avatar
 
Jan Möbius committed
928
929
930
931
932
933
934

}


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

/** \brief Move the current manipulator to the cog of the object
935
 *
Jan Möbius's avatar
 
Jan Möbius committed
936
937
 */
void MovePlugin::slotMoveManipToCOG() {
938

939
940
941
    QPushButton* but = dynamic_cast<QPushButton*>(QObject::sender());
    movePropsWidget* pW = getDialogFromButton(but);
    if(pW == 0) return;
942

Jan Möbius's avatar
Jan Möbius committed
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958

//     if ( pW->targetObjects->isChecked() ) {
// 	for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::TARGET_OBJECTS) ; o_it != PluginFunctions::objectsEnd(); ++o_it)  {
// 		if (  o_it->manipulatorNode()->hidden() )
// 		continue;
//
// 		if ( o_it->dataType( DATA_TRIANGLE_MESH ) )
// 		o_it->manipulatorNode()->set_center( MeshInfo::cog(*PluginFunctions::triMesh(*o_it) ) );
// 		else if ( o_it->dataType( DATA_POLY_MESH ) )
// 		o_it->manipulatorNode()->set_center( MeshInfo::cog(*PluginFunctions::polyMesh(*o_it)) );
//
// 		updateManipulatorDialog();
// 		o_it->manipulatorNode()->loadIdentity();
// 	}
//     } else {

959
960
961
    BaseObjectData* object = pW->getBaseObjectData();
    if ( object != 0 ) {
	if (  object->manipulatorNode()->visible() ){
962

963
964
965
966
	    if ( object->dataType( DATA_TRIANGLE_MESH ) )
	    object->manipulatorNode()->set_center( MeshInfo::cog(*PluginFunctions::triMesh(object)) );
	    else if ( object->dataType( DATA_POLY_MESH ) )
	    object->manipulatorNode()->set_center( MeshInfo::cog(*PluginFunctions::polyMesh(object)) );
Jan Möbius's avatar
 
Jan Möbius committed
967

968
969
970
971
	    updateManipulatorDialog();
	    object->manipulatorNode()->loadIdentity();
	}
    }
Jan Möbius's avatar
 
Jan Möbius committed
972

973
    emit updateView();
Jan Möbius's avatar
 
Jan Möbius committed
974
975
976
977
978
979
}


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

/** \brief Rotate Manipulator (with values from Tab)
980
 *
Jan Möbius's avatar
 
Jan Möbius committed
981
982
 */
void MovePlugin::slotRotate() {
983

984
985
986
    QPushButton* but = dynamic_cast<QPushButton*>(QObject::sender());
    movePropsWidget* pW = getDialogFromButton(but);
    if(pW == 0) return;
987

988
989
    TriMesh::Point axis;
    double angle;
990

991
992
993
994
995
996
997
    bool ok = false;
    axis[0] =  (pW->rotx->text()).toDouble(&ok);
    if ( !ok ) { emit log(LOGERR,"Wrong Format for X Coordinate");  return; }
    axis[1] =  (pW->roty->text()).toDouble(&ok);
    if ( !ok ) { emit log(LOGERR,"Wrong Format for Y Coordinate"); return; }
    axis[2] =  (pW->rotz->text()).toDouble(&ok);
    if ( !ok ) { emit log(LOGERR,"Wrong Format for Z Coordinate"); return; }
998

999
1000
    angle =  (pW->rotAngle->text()).toDouble(&ok);
    if ( !ok ) {  emit log(LOGERR,"Wrong Format for Angle"); return; }
1001

Jan Möbius's avatar
Jan Möbius committed
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
//     if ( pW->targetObjects->isChecked() ) {
// 	for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::TARGET_OBJECTS) ; o_it != PluginFunctions::objectsEnd(); ++o_it)  {
// 		if (  o_it->manipulatorNode()->hidden() )
// 		continue;
// 		o_it->manipulatorNode()->rotate(  angle,axis);
//
// 		if (o_it->dataType( DATA_TRIANGLE_MESH ) )
// 		transformMesh(  o_it->manipulatorNode()->matrix() , (*PluginFunctions::triMesh(*o_it)) );
//
// 		if (o_it->dataType( DATA_POLY_MESH ) )
// 		transformMesh(  o_it->manipulatorNode()->matrix() , (*PluginFunctions::polyMesh(*o_it)) );
//
// 		o_it->manipulatorNode()->loadIdentity();
// 		updateManipulatorDialog();
//
// 		emit createBackup(o_it->id(),"Rotation");
// 		emit updatedObject(o_it->id());
// 	}
//     } else {


1023
    BaseObjectData* object = pW->getBaseObjectData();
1024
1025
	if (object != 0) {
		if (object->manipulatorNode()->visible() && (object->target()
1026
				|| !allTargets_)) {
1027

1028
			object->manipulatorNode()->rotate(angle, axis);
1029

1030
1031
1032
			if (object->dataType(DATA_TRIANGLE_MESH))
				transformMesh(getLastManipulatorMatrix(true),
						(*PluginFunctions::triMesh(object)));
1033

1034
1035
1036
			if (object->dataType(DATA_POLY_MESH))
				transformMesh(getLastManipulatorMatrix(true),
						(*PluginFunctions::polyMesh(object)));
1037

1038
			updateManipulatorDialog();
1039

1040
1041
1042
			emit createBackup(object->id(), "Rotation");
			emit updatedObject(object->id());
		}
1043
	}
1044

1045
1046
	emit scriptInfo(QString("slotRotate()"));
	emit updateView();
Jan Möbius's avatar
 
Jan Möbius committed
1047
1048
1049
1050
1051
1052
}


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

/** \brief Scale Manipulator (with values from Tab)
1053
 *
Jan Möbius's avatar
 
Jan Möbius committed
1054
1055
 */
void MovePlugin::slotScale() {
1056

1057
1058
1059
    QPushButton* but = dynamic_cast<QPushButton*>(QObject::sender());
    movePropsWidget* pW = getDialogFromButton(but);
    if(pW == 0) return;
1060
1061


1062
    TriMesh::Point scale;
1063

1064
1065
1066
1067
1068
1069
1070
    bool ok = false;
    scale[0] =  (pW->scalex->text()).toDouble(&ok);
    if ( !ok ) { emit log(LOGERR,"Wrong Format for factor 1");  return; }
    scale[1] =  (pW->scaley->text()).toDouble(&ok);
    if ( !ok ) { emit log(LOGERR,"Wrong Format for factor 2"); return; }
    scale[2] =  (pW->scalez->text()).toDouble(&ok);
    if ( !ok ) { emit log(LOGERR,"Wrong Format for factor 3"); return; }
1071

Jan Möbius's avatar
Jan Möbius committed
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
//    if ( pW->targetObjects->isChecked() ) {
// 	for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::TARGET_OBJECTS) ; o_it != PluginFunctions::objectsEnd(); ++o_it)  {
// 		if (  o_it->manipulatorNode()->hidden() )
// 		continue;
// 		o_it->manipulatorNode()->scale(  scale);
//
// 		if (o_it->dataType( DATA_TRIANGLE_MESH ) )
// 		transformMesh(  o_it->manipulatorNode()->matrix() , (*PluginFunctions::triMesh(*o_it)) );
//
// 		if (o_it->dataType( DATA_POLY_MESH ) )
// 		transformMesh(  o_it->manipulatorNode()->matrix() , (*PluginFunctions::polyMesh(*o_it)) );
//
// 		o_it->manipulatorNode()->loadIdentity();
// 		updateManipulatorDialog();
//
// 		emit createBackup(o_it->id(),"Scaling");
// 		emit updatedObject(o_it->id());
// 	}
//     } else {


1093
    BaseObjectData* object = pW->getBaseObjectData();
1094
1095
	if (object != 0) {
		if (object->manipulatorNode()->visible() && (object->target()
1096
				|| !allTargets_)) {
Jan Möbius's avatar
 
Jan Möbius committed
1097

1098
			object->manipulatorNode()->scale(scale);
1099

1100
1101
1102
			if (object->dataType(DATA_TRIANGLE_MESH))
				transformMesh(getLastManipulatorMatrix(true),
						(*PluginFunctions::triMesh(object)));
1103

1104
1105
1106
			if (object->dataType(DATA_POLY_MESH))
				transformMesh(getLastManipulatorMatrix(true),
						(*PluginFunctions::polyMesh(object)));
1107

1108
			updateManipulatorDialog();
1109

1110
1111
1112
			emit createBackup(object->id(), "Scaling");
			emit updatedObject(object->id());
		}
1113
	}
Jan Möbius's avatar
 
Jan Möbius committed
1114

1115
1116
    emit scriptInfo(QString("slotScale()"));
    emit updateView();
Jan Möbius's avatar
 
Jan Möbius committed
1117
1118
1119
1120
1121
1122
1123
}


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


/** \brief Move currently active or first Mesh with its COG to the origin
1124
 *
Jan Möbius's avatar
 
Jan Möbius committed
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
 */
void MovePlugin::slotMoveToOrigin() {

   for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::TARGET_OBJECTS) ; o_it != PluginFunctions::objectsEnd(); ++o_it)  {
      if ( o_it->dataType( DATA_TRIANGLE_MESH )) {
         TriMesh& mesh = *PluginFunctions::triMesh(*o_it);
         const TriMesh::Point cog = MeshInfo::cog(mesh);

         for ( TriMesh::VertexIter v_it = mesh.vertices_begin(); v_it != mesh.vertices_end() ; ++v_it)
            mesh.set_point(v_it , ( mesh.point(v_it) ) - cog );

         o_it->manipulatorNode()->set_center( o_it->manipulatorNode()->center() - cog );
      }

      if ( o_it->dataType( DATA_POLY_MESH )) {
         PolyMesh& mesh = *PluginFunctions::polyMesh(*o_it);
         const PolyMesh::Point cog = MeshInfo::cog(mesh);

         for ( PolyMesh::VertexIter v_it = mesh.vertices_begin(); v_it != mesh.vertices_end() ; ++v_it)
            mesh.set_point(v_it , ( mesh.point(v_it) ) - cog );

         o_it->manipulatorNode()->set_center( o_it->manipulatorNode()->center() - cog );

      }

      emit updatedObject( o_it->id() );

      updateManipulatorDialog();
      o_it->manipulatorNode()->loadIdentity();

      emit createBackup(o_it->id(),"Move to origin");
   }

   emit updateView();
}


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

/** \brief Scale Boundingbox Diagonal to unit size
1165
 *
Jan Möbius's avatar
 
Jan Möbius committed
1166
1167
1168
 */
void MovePlugin::slotUnifyBoundingBoxDiagonal()
{
1169
   if ( tool_->targetObjects->isChecked() ) {
Jan Möbius's avatar
 
Jan Möbius committed
1170
1171
1172
1173
       for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::TARGET_OBJECTS) ; o_it != PluginFunctions::objectsEnd(); ++o_it)  {
            if ( o_it->dataType( DATA_TRIANGLE_MESH ) )
              unifyBBDiag(*PluginFunctions::triMesh(*o_it));
            else if ( o_it->dataType( DATA_POLY_MESH ) )
1174
              unifyBBDiag(*PluginFunctions::polyMesh(*o_it));
Jan Möbius's avatar
 
Jan Möbius committed
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199

            emit updatedObject( o_it->id() );

       }
   } else {
      BaseObjectData* object;
      if ( PluginFunctions::getObject(lastActiveManipulator_ , object) ) {
         if (  object->manipulatorNode()->visible() ) {

            if ( object->dataType( DATA_TRIANGLE_MESH ) )
              unifyBBDiag(*PluginFunctions::triMesh(object));
            else if ( object->dataType( DATA_POLY_MESH ) )
             unifyBBDiag(*PluginFunctions::polyMesh(object));
         }

         emit updatedObject( object->id() );
      }
   }
   emit updateView();
}


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

/** \brief Update the Dialog with the last clicked manipulator
1200
 *
Jan Möbius's avatar
 
Jan Möbius committed
1201
1202
 */
void MovePlugin::updateManipulatorDialog() {
1203

1204
1205
1206
1207
    BaseObjectData* object;
    if ( !PluginFunctions::getObject(lastActiveManipulator_ , object) ) {
	return;
    }
1208

1209
    if ( object->manipulatorNode()->visible() ) {
1210

1211
1212
1213
	// Get properties widget that corresponds to
        // to the last manipulated object
        movePropsWidget* pW = getDialogWidget(object);
1214

1215
1216
        // If there's no properties dialog yet...
        if(pW == 0) return;
1217

1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
	const TriMesh::Point pos = object->manipulatorNode()->center();

	QString num;

	num = QString::number(pos[0]); pW->posx->setText(num);
	num = QString::number(pos[1]); pW->posy->setText(num);
	num = QString::number(pos[2]); pW->posz->setText(num);

	TriMesh::Point direction = object->manipulatorNode()->directionX();
	num = QString::number(direction[0]); pW->dirxx->setText(num);
	num = QString::number(direction[1]); pW->dirxy->setText(num);
	num = QString::number(direction[2]); pW->dirxz->setText(num);

	direction = object->manipulatorNode()->directionY();
	num = QString::number(direction[0]); pW->diryx->setText(num);
	num = QString::number(direction[1]); pW->diryy->setText(num);
	num = QString::number(direction[2]); pW->diryz->setText(num);

	direction = object->manipulatorNode()->directionZ();
	num = QString::number(direction[0]); pW->dirzx->setText(num);
	num = QString::number(direction[1]); pW->dirzy->setText(num);
	num = QString::number(direction[2]); pW->dirzz->setText(num);
1240

1241
    }
Jan Möbius's avatar
 
Jan Möbius committed
1242
1243
}

1244
//------------------------------------------------------------------------------
Jan Möbius's avatar
 
Jan Möbius committed
1245

1246
/** \brief Get pointer to corresponding dialog widget
1247
 *
1248
1249
1250
 * @param _obj the object to which the dialog is attached
 */
movePropsWidget* MovePlugin::getDialogWidget(BaseObjectData* _obj) {
1251

1252
1253
1254
1255
1256
1257
1258
    for(QList<movePropsWidget*>::iterator it = propsWindows_.begin();
       it != propsWindows_.end(); it++) {
	   if ((*it)->getBaseObjectData() == _obj)
	       return *it;
       }
    return 0;
}
Jan Möbius's avatar
 
Jan Möbius committed
1259
1260
1261
//------------------------------------------------------------------------------

/** \brief Called by Toolbar to enable move mode
1262
 *
Jan Möbius's avatar
 
Jan Möbius committed
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
 * @param _action the action that was triggered
 */
void MovePlugin::slotSetMoveMode(QAction* _action) {

  if (_action == moveAction_){
    PluginFunctions::actionMode(Viewer::PickingMode);
    PluginFunctions::pickMode("Move");

    moveAction_->setChecked( true );
  }

  if (_action == moveSelectionAction_){

    connectSelectionActions();

    PluginFunctions::actionMode(Viewer::PickingMode);
    PluginFunctions::pickMode("MoveSelection");

    moveSelectionAction_->setChecked( true );
  }
}

1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
//------------------------------------------------------------------------------

/** \brief Called by pick mode Toolbar
 *
 * @param _action the action that was triggered
 */

void MovePlugin::slotPickToolbarAction(QAction* _action)
{

  if (_action == ratateTranslateAction_)
  {
    setManipMode (QtTranslationManipulatorNode::TranslationRotation);
  }

  if (_action == ratateManipAction_)
  {
    setManipMode (QtTranslationManipulatorNode::LocalRotation);
  }

  if (_action == resizeAction_)
  {
    setManipMode(QtTranslationManipulatorNode::Resize);
  }

  if (_action == biggerManipAction_)
  {
    manip_size_modifier_ = manip_size_modifier_ + 0.1;
    for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::ALL_OBJECTS) ; o_it != PluginFunctions::objectsEnd(); ++o_it)
         o_it->manipulatorNode()->set_size(manip_size_ * manip_size_modifier_);
    emit visibilityChanged (-1);
  }

  if (_action == smallerManipAction_)
  {
    manip_size_modifier_ = manip_size_modifier_ - 0.1;
    for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::ALL_OBJECTS) ; o_it != PluginFunctions::objectsEnd(); ++o_it)
         o_it->manipulatorNode()->set_size(manip_size_ * manip_size_modifier_);
    emit visibilityChanged (-1);
  }
}

Jan Möbius's avatar
 
Jan Möbius committed
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352

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

/** \brief Get the Matrix of the last active Manipulator ( Identity if not found or hidden Manipulator )
  *
  * @param _reset reset the transformation matrix of the manipulator to identity)
  * @return current transformation matrix of the manipulator
  */
ACG::Matrix4x4d MovePlugin::getLastManipulatorMatrix(bool _reset) {
   ACG::Matrix4x4d matrix;
   matrix.identity();

   BaseObjectData* object;
   if ( PluginFunctions::getObject(lastActiveManipulator_ , object) )
         if (  object->manipulatorNode()->visible() ) {
            matrix = object->manipulatorNode()->matrix();
            if (_reset)
               object->manipulatorNode()->loadIdentity();
         }
   return matrix;
}


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

/** \brief Transform a mesh with the given transformation matrix
1353
 *
Jan Möbius's avatar
 
Jan Möbius committed
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
 * @param _mat transformation matrix
 * @param _mesh the mesh
 */
template< typename MeshT >
void MovePlugin::transformMesh(ACG::Matrix4x4d _mat , MeshT& _mesh ) {
   typename MeshT::VertexIter v_it     = _mesh.vertices_begin();
   typename MeshT::VertexIter v_end = _mesh.vertices_end();
   for (; v_it!=v_end; ++v_it) {
            _mesh.set_point(v_it,(typename MeshT::Point)_mat.transform_point((OpenMesh::Vec3d)(_mesh.point(v_it))));
            _mesh.set_normal(v_it,(typename MeshT::Point)_mat.transform_vector((OpenMesh::Vec3d)(_mesh.normal(v_it))));
   }

   typename MeshT::FaceIter f_it     = _mesh.faces_begin();
   typename MeshT::FaceIter f_end = _mesh.faces_end();
   for (; f_it != f_end; ++f_it)
            _mesh.set_normal(f_it,(typename MeshT::Point)_mat.transform_vector((OpenMesh::Vec3d)(_mesh.normal(f_it))));
}


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

#ifdef ENABLE_POLYLINE_SUPPORT

/** \brief Transform a polyline with the given transformation matrix
1378
 *
Jan Möbius's avatar
 
Jan Möbius committed
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
 * @param _mat transformation matrix
 * @param _polyLine the polyline
 */
template< class PolyLineT >
void MovePlugin::transformPolyLine( ACG::Matrix4x4d _mat , PolyLineT& _polyLine  ) {
  #ifdef USE_OPENMP
  #pragma omp parallel for
  #endif
  for ( int i = 0 ; i < (int)_polyLine.n_vertices(); ++i )
    _polyLine.point(i) = _mat.transform_point( _polyLine.point(i) );
}

#endif


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

/** \brief scale mesh to have a boundingboxdiagonal of one
1397
 *
Jan Möbius's avatar
 
Jan Möbius committed
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
 * @param _mesh the mesh
 */
template< typename MeshT >
void MovePlugin::unifyBBDiag(MeshT& _mesh )
{
  typename MeshT::VertexIter v_it  = _mesh.vertices_begin();
  typename MeshT::VertexIter v_end = _mesh.vertices_end();

  // no vertices?
  if( v_it == v_end) return;

  typename MeshT::Point bb_min = _mesh.point(v_it);
  typename MeshT::Point bb_max = _mesh.point(v_it);

  for(; v_it!=v_end; ++v_it)
  {
    bb_min.minimize( _mesh.point(v_it));
    bb_max.maximize( _mesh.point(v_it));
  }

  typename MeshT::Point bb_center =  0.5 * (bb_min + bb_max) ;

  typename MeshT::Scalar scale = 1.0/(bb_max-bb_min).norm();

  for( v_it = _mesh.vertices_begin(); v_it!=v_end; ++v_it)
  {

     _mesh.point(v_it) = (_mesh.point(v_it) - bb_center) * scale + bb_center;
  }

1428
1429
  _mesh.update_normals();

Jan Möbius's avatar
 
Jan Möbius committed
1430
1431
1432
1433
1434
1435
}


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

/** \brief Connect to SelectionPlugin
1436
 *
Jan Möbius's avatar
 
Jan Möbius committed
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
 */
void MovePlugin::connectSelectionActions(){

  if (!selectionConnected_){

    selectionConnected_ = true;

    bool connected = false;

    QToolBar* selToolBar = 0;
    emit getToolBar( "Selection", selToolBar );
1448

Jan Möbius's avatar
 
Jan Möbius committed
1449
1450
1451
    if (selToolBar != 0){

      QActionGroup* actionGroup = 0;
1452

Jan Möbius's avatar
 
Jan Möbius committed
1453
1454
1455
1456
      for (int i=0; i < selToolBar->actions().count(); i++){
        if( selToolBar->actions().at(i)->text() == "Enable Vertex Selection"){

          actionGroup = selToolBar->actions().at(i)->actionGroup();
1457

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