PolyLinePlugin.cc 60.8 KB
Newer Older
Jan Möbius's avatar
   
Jan Möbius committed
1
2
3
4
5
6
7
8
9
//=============================================================================
//
//  CLASS PolyLinePlugin - IMPLEMENTATION
//
//=============================================================================


//== INCLUDES =================================================================

Matthias Möller's avatar
Matthias Möller committed
10
11
12
13
14
15
16

#if QT_VERSION >= 0x050000 
  #include <QtWidgets>
#else
  #include <QtGui>
#endif

Jan Möbius's avatar
   
Jan Möbius committed
17
18
19
20
21
22
23
24
25
26
27
28

#include "PolyLinePlugin.hh"

#include <iostream>
#include <ACG/GL/GLState.hh>
#include <QStringList>
#include <ACG/Scenegraph/ManipulatorNode.hh>

#include <OpenFlipper/BasePlugin/PluginFunctions.hh>

#include <OpenFlipper/common/GlobalOptions.hh>

29
30
31
32
#include <ACG/Geometry/bsp/BSPImplT.hh>

namespace {

Jan Möbius's avatar
Jan Möbius committed
33
34
class GlutPrimitiveNode : public ACG::SceneGraph::GlutPrimitiveNode {

35
	public:
Jan Möbius's avatar
Jan Möbius committed
36

37
38
		GlutPrimitiveNode(PolyLineObject* L, std::string name, int _index = -1)
			: ACG::SceneGraph::GlutPrimitiveNode(ACG::SceneGraph::GlutPrimitiveNode::SPHERE, L->manipulatorNode(), name)
39
		{
40
			index = _index;
41
42
			line = L;
		}
Jan Möbius's avatar
Jan Möbius committed
43

44
	public:
Jan Möbius's avatar
Jan Möbius committed
45

46
		PolyLineObject* line;
47
		int index;
Jan Möbius's avatar
Jan Möbius committed
48

49
50
};

51
class GlutLineNode : public ACG::SceneGraph::LineNode {
Jan Möbius's avatar
Jan Möbius committed
52

53
	public:
Jan Möbius's avatar
Jan Möbius committed
54

55
56
57
58
59
		GlutLineNode(PolyLineObject* L, std::string name)
			: ACG::SceneGraph::LineNode(LineSegmentsMode, L->manipulatorNode(), name)
		{
			line = L;
		}
Jan Möbius's avatar
Jan Möbius committed
60

61
62
	public:
		PolyLineObject* line;
Jan Möbius's avatar
Jan Möbius committed
63

64
};
65
66
67

}

Jan Möbius's avatar
   
Jan Möbius committed
68
69
70
71
72
73
74
75
76
77
78
79

//== IMPLEMENTATION ==========================================================

/// default constructor
PolyLinePlugin::PolyLinePlugin() :
        tool_(0),
        polyLineAction_(0),
        toolBarActions_(0),
        toolbar_(0),
        pickToolbar_(0),
        pickToolBarActions_(0),
        insertAction_(0),
80
        insertCircleAction_(0),
81
        insertSplineAction_(0),
Jan Möbius's avatar
   
Jan Möbius committed
82
83
84
85
86
87
88
89
90
91
        deleteAction_(0),
        moveAction_(0),
        smartMoveAction_(0),
        mergeAction_(0),
        splitAction_(0),
        cutAction_(0),
        cur_insert_id_(-1),
        cur_polyline_obj_(0),
        cur_move_id_(-1),
        move_point_ref_(0),
92
        create_point_ref_(0),
93
        createCircle_CurrSelIndex_(-1),
94
        createCircle_LastSelIndex_(-1),
95
        moveCircle_SelNode_(0),
96
97
98
99
100
        createSpline_CurrSelIndex_(-1),
        createSpline_LastSelIndex_(-1),
        moveBezSpline_SelNode_(0),
        moveBezSpline_SelIndex_(-1),
        moveBezSpline_SelSubIndex_(0),
Jan Möbius's avatar
   
Jan Möbius committed
101
102
103
        cur_merge_id_(-1),
        smart_move_timer_(0),
        cur_smart_move_obj_(0),
104
        planeSelect_(0)
Jan Möbius's avatar
   
Jan Möbius committed
105
106
107
108
109
110
111
112
{
}


void
PolyLinePlugin::
initializePlugin()
{
Jan Möbius's avatar
Jan Möbius committed
113
  // Initialize the Toolbox
Jan Möbius's avatar
   
Jan Möbius committed
114
115
116
  tool_ = new PolyLineToolbarWidget();
  QSize size(100, 100);
  tool_->resize(size);
Jan Möbius's avatar
Jan Möbius committed
117
  tool_->setObjectName("PolyLineToolbar");
Jan Möbius's avatar
   
Jan Möbius committed
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136

  // connect signals->slots
  connect(tool_->pb_subdivide,SIGNAL(clicked() ),this,SLOT(slot_subdivide()));
  connect(tool_->subdivide_relative,SIGNAL(toggled(bool) ),this,SLOT(slot_subdivide_percent(bool)));
  connect(tool_->pb_decimate,SIGNAL(clicked() ),this,SLOT(slot_decimate()));
  connect(tool_->decimate_relative,SIGNAL(toggled(bool) ),this,SLOT(slot_decimate_percent(bool)));
#ifdef EXTENDED_POLY_LINE
  connect(tool_->pb_resample_on_edges,SIGNAL(clicked() ),this,SLOT(slot_resample_on_edges()));
#else
  tool_->pb_resample_on_edges->setDisabled(true);
#endif
  connect(tool_->pb_smooth,SIGNAL(clicked() ),this,SLOT(slot_smooth()));
  connect(tool_->pb_smooth,SIGNAL(clicked() ),this,SIGNAL(updateView()));
  connect(tool_->pb_project,SIGNAL(clicked() ),this,SLOT(slot_project()));
  connect(tool_->pb_project,SIGNAL(clicked() ),this,SIGNAL(updateView()));
  connect(tool_->pb_smooth_project,SIGNAL(clicked() ),this,SLOT(slot_smooth_project()));
  connect(tool_->pb_smooth_project,SIGNAL(clicked() ),this,SIGNAL(updateView()));

  connect(tool_->rb_insert, SIGNAL( clicked() ), this, SLOT( slotEditModeChanged() ));
137
  connect(tool_->rb_InsertCircle, SIGNAL( clicked() ), this, SLOT( slotEditModeChanged() ));
138
  connect(tool_->rb_InsertSpline, SIGNAL( clicked() ), this, SLOT( slotEditModeChanged() ));
Jan Möbius's avatar
   
Jan Möbius committed
139
140
141
142
143
144
  connect(tool_->rb_delete, SIGNAL( clicked() ), this, SLOT( slotEditModeChanged() ));
  connect(tool_->rb_move,   SIGNAL( clicked() ), this, SLOT( slotEditModeChanged() ));
  connect(tool_->rb_smart_move, SIGNAL( clicked() ), this, SLOT( slotEditModeChanged() ));
  connect(tool_->rb_merge,  SIGNAL( clicked() ), this, SLOT( slotEditModeChanged() ));
  connect(tool_->rb_split,  SIGNAL( clicked() ), this, SLOT( slotEditModeChanged() ));

145
  connect(tool_->sb_CirclePointNum, SIGNAL(valueChanged(int)), this, SLOT(slot_setCirclePointNum(int)));
146
  connect(tool_->sb_SplineSegNum, SIGNAL(valueChanged(int)), this, SLOT(slot_setSplinePointNum(int)));
Jan Möbius's avatar
   
Jan Möbius committed
147
148
  //add icons
  tool_->rb_insert->setIcon( QIcon(OpenFlipper::Options::iconDirStr() + OpenFlipper::Options::dirSeparator() + "polyline_insert.png") );
149
  tool_->rb_InsertCircle->setIcon( QIcon(OpenFlipper::Options::iconDirStr() + OpenFlipper::Options::dirSeparator() + "polyline_circle.png") );
150
  tool_->rb_InsertSpline->setIcon( QIcon(OpenFlipper::Options::iconDirStr() + OpenFlipper::Options::dirSeparator() + "polyline_bezier.png") );
Jan Möbius's avatar
   
Jan Möbius committed
151
152
153
154
155
156
157
158
  tool_->rb_delete->setIcon( QIcon(OpenFlipper::Options::iconDirStr() + OpenFlipper::Options::dirSeparator() + "polyline_delete.png") );
  tool_->rb_move->setIcon( QIcon(OpenFlipper::Options::iconDirStr() + OpenFlipper::Options::dirSeparator() + "polyline_move.png") );
  tool_->rb_smart_move->setIcon( QIcon(OpenFlipper::Options::iconDirStr() + OpenFlipper::Options::dirSeparator() + "polyline_move.png") );
  tool_->rb_merge->setIcon( QIcon(OpenFlipper::Options::iconDirStr() + OpenFlipper::Options::dirSeparator() + "polyline_merge.png") );
  tool_->rb_split->setIcon( QIcon(OpenFlipper::Options::iconDirStr() + OpenFlipper::Options::dirSeparator() + "polyline_split.png") );
//   tool_->rb_smooth_c0->setIcon( QIcon(OpenFlipper::Options::iconDirStr() + OpenFlipper::Options::dirSeparator() + "polyline_insert.svg") );
//   tool_->rb_smooth_c1->setIcon( QIcon(OpenFlipper::Options::iconDirStr() + OpenFlipper::Options::dirSeparator() + "polyline_c1.svg") );
//   tool_->rb_smooth_c2->setIcon( QIcon(OpenFlipper::Options::iconDirStr() + OpenFlipper::Options::dirSeparator() + "polyline_c2.svg") );
159
160
161
162
163


  // Add the toolbox with the given icon
  QIcon* toolIcon = new QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"cut_polyline.png");
  emit addToolbox( tr("PolyLine") , tool_, toolIcon );
Jan Möbius's avatar
   
Jan Möbius committed
164
165
166
167
168
169
170
171
172
173
174
}


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


void
PolyLinePlugin::
slotMouseEvent( QMouseEvent* _event )
{
  // control modifier is reserved for selcting target
Jan Möbius's avatar
Jan Möbius committed
175
  if (_event->modifiers() & (Qt::ControlModifier))
Jan Möbius's avatar
   
Jan Möbius committed
176
177
    return;

Jan Möbius's avatar
Jan Möbius committed
178
179
  if (PluginFunctions::pickMode() == ("PolyLine") && PluginFunctions::actionMode() == Viewer::PickingMode
      && _event->button() != Qt::RightButton) {
Jan Möbius's avatar
   
Jan Möbius committed
180
    // handle mouse events depending on current mode
Jan Möbius's avatar
Jan Möbius committed
181
    switch (mode()) {
Jan Möbius's avatar
   
Jan Möbius committed
182
      case PL_INSERT:
Jan Möbius's avatar
Jan Möbius committed
183
184
        me_insert(_event);
        break;
Jan Möbius's avatar
   
Jan Möbius committed
185

186
187
188
189
      case PL_INSERTCIRCLE:
        me_insertCircle(_event);
        break;

190
191
192
193
      case PL_INSERTSPLINE:
        me_insertSpline(_event);
        break;

Jan Möbius's avatar
   
Jan Möbius committed
194
      case PL_DELETE:
Jan Möbius's avatar
Jan Möbius committed
195
196
        me_delete(_event);
        break;
Jan Möbius's avatar
   
Jan Möbius committed
197
198

      case PL_MOVE:
Jan Möbius's avatar
Jan Möbius committed
199
200
        me_move(_event);
        break;
Jan Möbius's avatar
   
Jan Möbius committed
201
202

      case PL_SPLIT:
Jan Möbius's avatar
Jan Möbius committed
203
204
        me_split(_event);
        break;
Jan Möbius's avatar
   
Jan Möbius committed
205
206

      case PL_MERGE:
Jan Möbius's avatar
Jan Möbius committed
207
208
        me_merge(_event);
        break;
Jan Möbius's avatar
   
Jan Möbius committed
209
210

      case PL_SMART_MOVE:
Jan Möbius's avatar
Jan Möbius committed
211
212
        me_smart_move(_event);
        break;
Jan Möbius's avatar
   
Jan Möbius committed
213
214

      default:
Jan Möbius's avatar
Jan Möbius committed
215
        break;
Jan Möbius's avatar
   
Jan Möbius committed
216
    }
Jan Möbius's avatar
Jan Möbius committed
217
218
  } else if (PluginFunctions::pickMode() == CREATE_CUT_POLYLINE) {
    planeSelect_->slotMouseEvent(_event);
Jan Möbius's avatar
   
Jan Möbius committed
219
220
221
  }
}

222
void PolyLinePlugin::slotKeyEvent(QKeyEvent* event) {
223
    if (!cur_polyline_obj_ || cur_insert_id_ == -1) return;
224
225
226
    switch (event->key()) {
        case Qt::Key_Return:
            if (PluginFunctions::pickMode() == ("PolyLine") && PluginFunctions::actionMode() == Viewer::PickingMode && mode() == PL_INSERT) {
Jan Möbius's avatar
   
Jan Möbius committed
227

228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
                cur_polyline_obj_->line()->delete_point(cur_polyline_obj_->line()->n_vertices() - 1);

                if (event->modifiers() & (Qt::ShiftModifier))
                  cur_polyline_obj_->line()->set_closed(true);

                emit updatedObject(cur_insert_id_, UPDATE_GEOMETRY | UPDATE_TOPOLOGY);

                cur_insert_id_ = -1;
                cur_polyline_obj_ = 0;
                create_point_ref_ = 0;

                clearStatusMessage();
            }
            break;
        default:
            break;
    }
}
Jan Möbius's avatar
   
Jan Möbius committed
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
//-----------------------------------------------------------------------------

void
PolyLinePlugin::
slotPickModeChanged( const std::string& _mode)
{
  polyLineAction_->setChecked(_mode == "PolyLine");
  cutAction_->setChecked( _mode == CREATE_CUT_POLYLINE );
}


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


void
PolyLinePlugin::
pluginsInitialized()
{
Jan Möbius's avatar
Jan Möbius committed
264
265
  // Add the required Picking modes (Hidden, controlled only by the buttons)
  emit addHiddenPickMode("PolyLine");
266
  emit setPickModeMouseTracking("PolyLine", true);
Jan Möbius's avatar
Jan Möbius committed
267
  emit addHiddenPickMode( CREATE_CUT_POLYLINE );
268
269
270

  emit registerKey(Qt::Key_Return, Qt::NoModifier, tr("Terminate creation of poly line."), true);
  emit registerKey(Qt::Key_Return, Qt::ShiftModifier, tr("Terminate creation of poly line and create loop."), true);
Jan Möbius's avatar
   
Jan Möbius committed
271
  
Jan Möbius's avatar
Jan Möbius committed
272
  // TOOLBAR
Jan Möbius's avatar
   
Jan Möbius committed
273
  toolbar_ = new QToolBar(tr("PolyLine Editing"));
Jan Möbius's avatar
Jan Möbius committed
274
275
  toolbar_->setObjectName("PolyLineEditingToolbar");

Jan Möbius's avatar
   
Jan Möbius committed
276
277
278
  
  toolBarActions_ = new QActionGroup(toolbar_);
  
Jan Möbius's avatar
Jan Möbius committed
279
  // icon which enables the Modeling toolbar
Jan Möbius's avatar
   
Jan Möbius committed
280
281
282
283
284
285
  polyLineAction_ = new QAction(tr("Edit PolyLines"), toolBarActions_);
  polyLineAction_->setStatusTip(tr("Edit and create PolyLines."));
  polyLineAction_->setIcon(QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"polyline_insert.png") );
  polyLineAction_->setCheckable(true);
  toolbar_->addAction(polyLineAction_);
  
Jan Möbius's avatar
Jan Möbius committed
286
  // icon for polyline cutting of objects
Jan Möbius's avatar
   
Jan Möbius committed
287
288
289
290
291
292
293
294
295
296
297
298
299
  cutAction_ = new QAction(tr("&Create polyline at intersection with plane"), this);
  cutAction_->setCheckable( true );
  cutAction_->setStatusTip(tr("Create a polyline by specifying a plane with which the object is then intersected. The polyline will be created at the intersection."));
  cutAction_->setIcon(QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"cut_polyline.png") );
  connect(cutAction_, SIGNAL(triggered()), this, SLOT(slotScissorButton()) );
  toolbar_->addAction(cutAction_);
  
  connect(toolBarActions_, SIGNAL(triggered(QAction*)), this, SLOT(slotSetPolyLineMode(QAction*)) );
  
  emit addToolbar(toolbar_);
  
  // Pick Toolbar
  pickToolbar_ = new QToolBar(tr("PolyLine Editing"));
Jan Möbius's avatar
Jan Möbius committed
300
  pickToolbar_->setObjectName("PolyLine_Editing_Toolbar");
Jan Möbius's avatar
   
Jan Möbius committed
301
302
303
304
305
306
307
308
309
310
311
312
313
  pickToolbar_->setAttribute(Qt::WA_AlwaysShowToolTips, true);
  pickToolBarActions_ = new QActionGroup(pickToolbar_);
  pickToolBarActions_->setExclusive (true);
  
  insertAction_ = new QAction(tr("Create PolyLine"), pickToolBarActions_);
  insertAction_->setStatusTip(tr("Create a new PolyLine."));
  insertAction_->setToolTip(tr("Create a new PolyLine.\n"
                               "Use <Doubleclick> to finish the line.\n"
                               "Hold <Shift> to close line on finish."));
  insertAction_->setIcon(QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"polyline_insert.png") );
  insertAction_->setCheckable(true);
  pickToolbar_->addAction(insertAction_);
  
314
315
316
317
318
319
320
321
322
323
  insertCircleAction_ = new QAction(tr("Create PolyCircle"), pickToolBarActions_);
  insertCircleAction_->setStatusTip(tr("Create a new PolyCircle."));
  insertCircleAction_->setToolTip(tr("Create a new PolyCircle.\n"
		  	  	  	  	  	  	  	 "<Click> to select the center.\n"
		  	  	  	  	  	  	  	 "Drag to specify the radius."));

  insertCircleAction_->setIcon(QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"polyline_circle.png") );
  insertCircleAction_->setCheckable(true);
  pickToolbar_->addAction(insertCircleAction_);

324
325
326
327
328
  insertSplineAction_ = new QAction(tr("Create PolySpline"), pickToolBarActions_);
  insertSplineAction_->setStatusTip(tr("Create a new PolySpline."));
  insertSplineAction_->setToolTip(tr("Create a new PolySpline.\n"
		  	  	  	  	  	  	  	 "<Click> to select the points."));

329
  insertSplineAction_->setIcon(QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"polyline_bezier.png") );
330
331
332
  insertSplineAction_->setCheckable(true);
  pickToolbar_->addAction(insertSplineAction_);

Jan Möbius's avatar
   
Jan Möbius committed
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
  deleteAction_ = new QAction(tr("Delete PolyLine"), pickToolBarActions_);
  deleteAction_->setStatusTip(tr("Delete a complete PolyLine."));
  deleteAction_->setToolTip(tr( "Delete a complete PolyLine.\n"
                                "<Click> on the lines you want to delete."));
  deleteAction_->setIcon(QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"polyline_delete.png") );
  deleteAction_->setCheckable(true);
  pickToolbar_->addAction(deleteAction_);  

  smartMoveAction_ = new QAction(tr("Move PolyLine"), pickToolBarActions_);
  smartMoveAction_->setStatusTip(tr("Move the PolyLine."));
  smartMoveAction_->setToolTip(tr("Move the polyline."));
  smartMoveAction_->setIcon(QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"polyline_move.png") );
  smartMoveAction_->setCheckable(true);
  pickToolbar_->addAction(smartMoveAction_);
  
  mergeAction_ = new QAction(tr("Merge PolyLines"), pickToolBarActions_);
  mergeAction_->setStatusTip(tr("Merge two PolyLines."));
  mergeAction_->setToolTip(tr("Merge two PolyLines.\n"
                              "Drag one endpoint of a PolyLine to the EndPoint of another one.\n"));
  mergeAction_->setIcon(QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"polyline_merge.png") );
  mergeAction_->setCheckable(true);
  pickToolbar_->addAction(mergeAction_);  
  
  splitAction_ = new QAction(tr("Split a PolyLine"), pickToolBarActions_);
  splitAction_->setStatusTip(tr("Split a PolyLine at a given point."));
  splitAction_->setToolTip(tr("Split a PolyLine at a given point.\n"
                              "<Click> on the vertex where you want to split the PolyLine and drag it away."));
  splitAction_->setIcon(QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"polyline_split.png") );
  splitAction_->setCheckable(true);
  pickToolbar_->addAction(splitAction_);  
  
  pickToolbar_->addSeparator ();
  
  moveAction_ = new QAction(tr("Move PolyLine Vertex"), pickToolBarActions_);
  moveAction_->setStatusTip(tr("Move a Vertex of a PolyLine"));
  moveAction_->setToolTip(tr("Move a single Vertex of a PolyLine."));
  moveAction_->setIcon(QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"polyline_move.png") );
  moveAction_->setCheckable(true);
  pickToolbar_->addAction(moveAction_);
  
  emit setPickModeToolbar ("PolyLine", pickToolbar_);
  
  connect(pickToolBarActions_, SIGNAL(triggered(QAction*)), this, SLOT(slotPickToolbarAction(QAction*)) );

  // construct timer
  smart_move_timer_ = new QTimer(this);
  connect(smart_move_timer_, SIGNAL(timeout()), this, SLOT(slot_smart_move_timer()));
  
Jan Möbius's avatar
Jan Möbius committed
381
  // Instantiate plane select object
Jan Möbius's avatar
   
Jan Möbius committed
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
  planeSelect_ = new QtPlaneSelect( PluginFunctions::viewerProperties().glState() );
  connect( planeSelect_, SIGNAL( signalTriggerCut( ) ), this, SLOT( slotTriggerCutPlaneSelect() ) );
  connect( planeSelect_, SIGNAL( updateViewProxy( ) ), this, SIGNAL( updateView() ) );
}

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

/** \brief Scissor Button was hit
*
*/
void PolyLinePlugin::slotScissorButton( )
{
    PluginFunctions::actionMode( Viewer::PickingMode );
    PluginFunctions::pickMode( CREATE_CUT_POLYLINE );
}

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

/** \brief Generate PolyLine after the cutPlane has been drawn
*
*/
void PolyLinePlugin::slotTriggerCutPlaneSelect( )
{
    using ACG::SceneGraph::LineNode;
Jan Möbius's avatar
Jan Möbius committed
406
407
408
409
410
411
412
413
414

  // Iterate over all selected objects
  BaseObjectData* object;
  if (PluginFunctions::getPickedObject(planeSelect_->getNode(), object)) {
    emit log("Cutting object " + object->name());

    if (!(object->dataType(DATA_TRIANGLE_MESH) || object->dataType(DATA_POLY_MESH))) {
      emit log("Only Meshes are supported at the moment ");
      return;
Jan Möbius's avatar
   
Jan Möbius committed
415
    }
Jan Möbius's avatar
Jan Möbius committed
416
417
418
419

    ACG::Vec3d point = planeSelect_->getSourcePoint();
    ACG::Vec3d normal = planeSelect_->getNormal();

420
    int objectId = generatePolyLineFromCut(object->id(), point, normal);
Jan Möbius's avatar
Jan Möbius committed
421
422
423
424
425
426
427
428

    QString command = "generatePolyLineFromCut(" + QString::number(object->id()) + ",Vector("
        + QString::number(point[0]) + "," + QString::number(point[1]) + "," + QString::number(point[2]) + "),Vector("
        + QString::number(normal[0]) + "," + QString::number(normal[1]) + "," + QString::number(normal[2]) + "));";
    emit scriptInfo(command);

    //remove all other targets
    for (PluginFunctions::ObjectIterator o_it(PluginFunctions::TARGET_OBJECTS,
429
         DataType(DATA_TRIANGLE_MESH | DATA_POLY_MESH)); o_it != PluginFunctions::objectsEnd(); ++o_it) {
Jan Möbius's avatar
Jan Möbius committed
430
431
432
      if (o_it->id() != object->id()) {
        o_it->target(false);
      }
433
434
435
436
437
438
    }

    // If we successfully created the polyline, we can inform the core about it.
    if ( objectId != -1)
      emit updatedObject(objectId,UPDATE_ALL);

Jan Möbius's avatar
Jan Möbius committed
439
440
  }

Jan Möbius's avatar
   
Jan Möbius committed
441
442
443
444
445
446
447
448
449
450
}

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

void
PolyLinePlugin::
slot_subdivide()
{
  // get subdivision value
  double max_length = tool_->dsb_subdivide->value();
Jan Möbius's avatar
Jan Möbius committed
451

Jan Möbius's avatar
   
Jan Möbius committed
452
  // safety catch
Jan Möbius's avatar
Jan Möbius committed
453
  if (max_length == 0.0)
Jan Möbius's avatar
   
Jan Möbius committed
454
455
456
    max_length = 1e-4;

  // iterate over all target polylines
Jan Möbius's avatar
Jan Möbius committed
457
  PluginFunctions::ObjectIterator o_it(PluginFunctions::TARGET_OBJECTS, DATA_POLY_LINE);
Jan Möbius's avatar
   
Jan Möbius committed
458

Jan Möbius's avatar
Jan Möbius committed
459
  for (; o_it != PluginFunctions::objectsEnd(); ++o_it) {
Jan Möbius's avatar
   
Jan Möbius committed
460
    // subdivide polyline
Jan Möbius's avatar
Jan Möbius committed
461
    PluginFunctions::polyLineObject(*o_it)->line()->subdivide(max_length);
Jan Möbius's avatar
   
Jan Möbius committed
462
463
464
465
466
467
468
469
470
471
472
473
  }

  // update
  emit updateView();
}


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


void PolyLinePlugin::slot_subdivide_percent(bool _checked) {

Jan Möbius's avatar
Jan Möbius committed
474
475
476
  // Get subdivision value
  int n_active_pl = 0;
  double total_length = 0;
Jan Möbius's avatar
   
Jan Möbius committed
477

Jan Möbius's avatar
Jan Möbius committed
478
479
  // Iterate over all target polylines
  PluginFunctions::ObjectIterator o_it(PluginFunctions::TARGET_OBJECTS, DATA_POLY_LINE);
Jan Möbius's avatar
   
Jan Möbius committed
480

Jan Möbius's avatar
Jan Möbius committed
481
482
483
484
485
  for (; o_it != PluginFunctions::objectsEnd(); ++o_it) {
    // Add polyline length
    total_length += PluginFunctions::polyLineObject(*o_it)->line()->length();
    ++n_active_pl;
  }
Jan Möbius's avatar
   
Jan Möbius committed
486

Jan Möbius's avatar
Jan Möbius committed
487
  double v = total_length / double(n_active_pl);
Jan Möbius's avatar
   
Jan Möbius committed
488

Jan Möbius's avatar
Jan Möbius committed
489
  if (_checked) {
Jan Möbius's avatar
   
Jan Möbius committed
490

Jan Möbius's avatar
Jan Möbius committed
491
492
493
494
    // Compute current absolute length to relative portion
    if (n_active_pl > 0) {
      double val_new = tool_->dsb_subdivide->value() / v;
      tool_->dsb_subdivide->setValue(val_new);
Jan Möbius's avatar
   
Jan Möbius committed
495
    } else {
Jan Möbius's avatar
Jan Möbius committed
496
497
498
      emit log(LOGWARN, "Could not find any active polyline!");
    }
  } else {
Jan Möbius's avatar
   
Jan Möbius committed
499

Jan Möbius's avatar
Jan Möbius committed
500
501
502
503
504
505
    // Compute relative portion to absolute value
    if (n_active_pl > 0) {
      double val_new = tool_->dsb_subdivide->value() * v;
      tool_->dsb_subdivide->setValue(val_new);
    } else {
      emit log(LOGWARN, "Could not find any active polyline!");
Jan Möbius's avatar
   
Jan Möbius committed
506
    }
Jan Möbius's avatar
Jan Möbius committed
507
  }
Jan Möbius's avatar
   
Jan Möbius committed
508
509
510
511
512
513
514
515
516
517
518
519
520
521
}


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


void
PolyLinePlugin::
slot_decimate()
{
  // get subdivision value
  double min_length = tool_->dsb_decimate->value();

  // iterate over all target polylines
Jan Möbius's avatar
Jan Möbius committed
522
  PluginFunctions::ObjectIterator o_it(PluginFunctions::TARGET_OBJECTS, DATA_POLY_LINE);
Jan Möbius's avatar
   
Jan Möbius committed
523

Jan Möbius's avatar
Jan Möbius committed
524
  for (; o_it != PluginFunctions::objectsEnd(); ++o_it) {
Jan Möbius's avatar
   
Jan Möbius committed
525
    // decimate polyline
Jan Möbius's avatar
Jan Möbius committed
526
    PluginFunctions::polyLineObject(*o_it)->line()->collapse(min_length);
Jan Möbius's avatar
   
Jan Möbius committed
527
528
529
530
531
532
533
534
535
  }

  // update
  emit updateView();
}

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


Jan Möbius's avatar
Jan Möbius committed
536
537
void PolyLinePlugin::slot_decimate_percent(bool _checked)
{
Jan Möbius's avatar
   
Jan Möbius committed
538

Jan Möbius's avatar
Jan Möbius committed
539
540
541
  // Get decimation value
  int n_active_pl = 0;
  double total_length = 0;
Jan Möbius's avatar
   
Jan Möbius committed
542

Jan Möbius's avatar
Jan Möbius committed
543
544
  // Iterate over all target polylines
  PluginFunctions::ObjectIterator o_it(PluginFunctions::TARGET_OBJECTS, DATA_POLY_LINE);
Jan Möbius's avatar
   
Jan Möbius committed
545

Jan Möbius's avatar
Jan Möbius committed
546
547
548
549
550
  for (; o_it != PluginFunctions::objectsEnd(); ++o_it) {
    // Add polyline length
    total_length += PluginFunctions::polyLineObject(*o_it)->line()->length();
    ++n_active_pl;
  }
Jan Möbius's avatar
   
Jan Möbius committed
551

Jan Möbius's avatar
Jan Möbius committed
552
  double v = total_length / double(n_active_pl);
Jan Möbius's avatar
   
Jan Möbius committed
553

Jan Möbius's avatar
Jan Möbius committed
554
  if (_checked) {
Jan Möbius's avatar
   
Jan Möbius committed
555

Jan Möbius's avatar
Jan Möbius committed
556
557
558
559
    // Compute current absolute length to relative portion
    if (n_active_pl > 0) {
      double val_new = tool_->dsb_subdivide->value() / v;
      tool_->dsb_decimate->setValue(val_new);
Jan Möbius's avatar
   
Jan Möbius committed
560
    } else {
Jan Möbius's avatar
Jan Möbius committed
561
562
563
      emit log(LOGWARN, "Could not find any active polyline!");
    }
  } else {
Jan Möbius's avatar
   
Jan Möbius committed
564

Jan Möbius's avatar
Jan Möbius committed
565
566
567
568
569
570
    // Compute relative portion to absolute value
    if (n_active_pl > 0) {
      double val_new = tool_->dsb_subdivide->value() * v;
      tool_->dsb_decimate->setValue(val_new);
    } else {
      emit log(LOGWARN, "Could not find any active polyline!");
Jan Möbius's avatar
   
Jan Möbius committed
571
    }
Jan Möbius's avatar
Jan Möbius committed
572
  }
Jan Möbius's avatar
   
Jan Möbius committed
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
603
604
605
606
607
608
609
610
611
612
613
}


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

#ifdef EXTENDED_POLY_LINE

void
PolyLinePlugin::
slot_resample_on_edges()
{
  // count target meshes
  PluginFunctions::ObjectIterator o_it(PluginFunctions::TARGET_OBJECTS, DATA_TRIANGLE_MESH );
  unsigned int n_targetMeshes(0);
  for(; o_it != PluginFunctions::objectsEnd(); ++o_it)
    ++n_targetMeshes;

  if( n_targetMeshes != 1)
  {
    std::cerr << "Warning: resample_on_edges needs exactly 1 target mesh! Otherwise no operation is performed! \n";
    return;
  }

  // take first target mesh
  o_it = PluginFunctions::ObjectIterator(PluginFunctions::TARGET_OBJECTS, DATA_TRIANGLE_MESH );

  if(o_it != PluginFunctions::objectsEnd())
  {
    // get pointer to TriMeshObject
    TriMeshObject* tmesh_obj = PluginFunctions::triMeshObject(*o_it);

    // get pointer to mesh
    TriMesh* mesh = tmesh_obj->mesh();

    // get pointer to triangle bsp
    OpenMeshTriangleBSPT< TriMesh >* tbsp = tmesh_obj->requestTriangleBsp();
    //    OpenMeshTriangleBSPT< TriMesh >* tbsp = 0;

    // iterate over all target polylines
    PluginFunctions::ObjectIterator o_it2(PluginFunctions::TARGET_OBJECTS, DATA_POLY_LINE );

Jan Möbius's avatar
Jan Möbius committed
614
    for (; o_it2 != PluginFunctions::objectsEnd(); ++o_it2)
Jan Möbius's avatar
   
Jan Möbius committed
615
    {
616
      std::cerr << "resample " << o_it2->name().toStdString() << std::endl;
Jan Möbius's avatar
   
Jan Möbius committed
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
      // decimate polyline
      PluginFunctions::polyLineObject(*o_it2)->line()->resample_on_mesh_edges( *mesh, tbsp );
    }

    // update
    emit updateView();
  }
}

#endif

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


void
PolyLinePlugin::
slot_smooth()
{
  // iterate over all target polylines
  PluginFunctions::ObjectIterator o_it(PluginFunctions::TARGET_OBJECTS, DATA_POLY_LINE );

  for (  ; o_it != PluginFunctions::objectsEnd(); ++o_it)
  {
    for( int i=0; i<tool_->sb_smooth_iter->value(); ++i)
      if( tool_->rb_smooth_c0->isChecked())
	// smooth polyline C0
	PluginFunctions::polyLineObject(*o_it)->line()->smooth_uniform_laplace();
      else
	if( tool_->rb_smooth_c1->isChecked())
	  // smooth polyline C1
	  PluginFunctions::polyLineObject(*o_it)->line()->smooth_uniform_laplace2();
	else
	  if( tool_->rb_smooth_c2->isChecked())
	    // smooth polyline C2
	    PluginFunctions::polyLineObject(*o_it)->line()->smooth_uniform_laplace3();

    emit updatedObject( o_it->id(), UPDATE_GEOMETRY);
  }

}


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


void
PolyLinePlugin::
slot_smooth( PolyLineObject*& _pol)
{
  // if polyline object is valid
Jan Möbius's avatar
Jan Möbius committed
667
668
669
  if (_pol) {
    for (int i = 0; i < tool_->sb_smooth_iter->value(); ++i)
      if (tool_->rb_smooth_c0->isChecked())
Jan Möbius's avatar
   
Jan Möbius committed
670
671
        // smooth polyline C0
        _pol->line()->smooth_uniform_laplace();
Jan Möbius's avatar
Jan Möbius committed
672
673
674
675
676
677
678
679
      else if (tool_->rb_smooth_c1->isChecked())
        // smooth polyline C1
        _pol->line()->smooth_uniform_laplace2();
      else if (tool_->rb_smooth_c2->isChecked())
        // smooth polyline C2
        _pol->line()->smooth_uniform_laplace3();

    emit updatedObject(_pol->id(), UPDATE_GEOMETRY);
Jan Möbius's avatar
   
Jan Möbius committed
680
681
682
683
684
685
686
687
688
689
690
  }
}

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


void
PolyLinePlugin::
slot_project()
{
  // create meshe and bsp vectors
Jan Möbius's avatar
Jan Möbius committed
691
692
  std::vector<TriMesh*> meshes;
  std::vector<OpenMeshTriangleBSPT<TriMesh>*> bsps;
Jan Möbius's avatar
   
Jan Möbius committed
693

Jan Möbius's avatar
Jan Möbius committed
694
  PluginFunctions::ObjectIterator o_it(PluginFunctions::TARGET_OBJECTS, DATA_TRIANGLE_MESH);
Jan Möbius's avatar
   
Jan Möbius committed
695

Jan Möbius's avatar
Jan Möbius committed
696
  for (; o_it != PluginFunctions::objectsEnd(); ++o_it) {
Jan Möbius's avatar
   
Jan Möbius committed
697
698
699
700
    // get pointer to TriMeshObject
    TriMeshObject* tmesh_obj = PluginFunctions::triMeshObject(*o_it);

    // save meshes and bsps
Jan Möbius's avatar
Jan Möbius committed
701
    meshes.push_back(tmesh_obj->mesh());
Jan Möbius's avatar
   
Jan Möbius committed
702
    //    bsps.push_back(0);
Jan Möbius's avatar
Jan Möbius committed
703
    bsps.push_back(tmesh_obj->requestTriangleBsp());
Jan Möbius's avatar
   
Jan Möbius committed
704
705
706
  }

  // iterate over all target polylines
Jan Möbius's avatar
Jan Möbius committed
707
  PluginFunctions::ObjectIterator o_it2(PluginFunctions::TARGET_OBJECTS, DATA_POLY_LINE);
Jan Möbius's avatar
   
Jan Möbius committed
708

Jan Möbius's avatar
Jan Möbius committed
709
  for (; o_it2 != PluginFunctions::objectsEnd(); ++o_it2) {
Jan Möbius's avatar
   
Jan Möbius committed
710
    // project polyline
Jan Möbius's avatar
Jan Möbius committed
711
712
    PluginFunctions::polyLineObject(*o_it2)->line()->project_to_mesh(meshes, &bsps);
    emit updatedObject(o_it2->id(), UPDATE_GEOMETRY);
Jan Möbius's avatar
   
Jan Möbius committed
713
714
715
716
717
718
719
720
721
722
723
724
  }
}


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


void 
PolyLinePlugin::
slot_project( PolyLineObject*& _pol)
{
  // check if polyline ok
Jan Möbius's avatar
Jan Möbius committed
725
726
  if (_pol) {

Jan Möbius's avatar
   
Jan Möbius committed
727
    // create meshes and bsp vectors
Jan Möbius's avatar
Jan Möbius committed
728
729
    std::vector<TriMesh*> meshes;
    std::vector<OpenMeshTriangleBSPT<TriMesh>*> bsps;
Jan Möbius's avatar
   
Jan Möbius committed
730

Jan Möbius's avatar
Jan Möbius committed
731
732
733
    PluginFunctions::ObjectIterator o_it(PluginFunctions::TARGET_OBJECTS, DATA_TRIANGLE_MESH);

    for (; o_it != PluginFunctions::objectsEnd(); ++o_it) {
Jan Möbius's avatar
   
Jan Möbius committed
734
735
736
737
738

      // get pointer to TriMeshObject
      TriMeshObject* tmesh_obj = PluginFunctions::triMeshObject(*o_it);

      // save meshes and bsps
Jan Möbius's avatar
Jan Möbius committed
739
      meshes.push_back(tmesh_obj->mesh());
Jan Möbius's avatar
   
Jan Möbius committed
740
      //    bsps.push_back(0);
Jan Möbius's avatar
Jan Möbius committed
741
      bsps.push_back(tmesh_obj->requestTriangleBsp());
Jan Möbius's avatar
   
Jan Möbius committed
742
743
744
    }

    // only project given PolyLine
Jan Möbius's avatar
Jan Möbius committed
745
    _pol->line()->project_to_mesh(meshes, &bsps);
Jan Möbius's avatar
   
Jan Möbius committed
746
747
748
749
750
751
752
753
754
755
756
757
758
759
  }

}


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


void
PolyLinePlugin::
slot_smooth_project()
{
  int smooth_project_iter = tool_->sb_smooth_project_iter->value();

Jan Möbius's avatar
Jan Möbius committed
760
761
  for (int i = 0, j = 1; i < smooth_project_iter; ++i, ++j) {

Jan Möbius's avatar
   
Jan Möbius committed
762
763
764
765
766
    // n smoothing steps
    slot_smooth();

    // projection step
    slot_project();
Jan Möbius's avatar
Jan Möbius committed
767

Jan Möbius's avatar
   
Jan Möbius committed
768
769
770
771
772
773
774
775
776
  }
}


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


void
PolyLinePlugin::
Jan Möbius's avatar
Jan Möbius committed
777
slot_smooth_project(PolyLineObject*& _pol)
Jan Möbius's avatar
   
Jan Möbius committed
778
779
780
{
  int smooth_project_iter = tool_->sb_smooth_project_iter->value();

Jan Möbius's avatar
Jan Möbius committed
781
782
  for (int i = 0, j = 1; i < smooth_project_iter; ++i, ++j) {

Jan Möbius's avatar
   
Jan Möbius committed
783
    // n smoothing steps
Jan Möbius's avatar
Jan Möbius committed
784
    slot_smooth(_pol);
Jan Möbius's avatar
   
Jan Möbius committed
785
786

    // projection step
Jan Möbius's avatar
Jan Möbius committed
787
788
    slot_project(_pol);

Jan Möbius's avatar
   
Jan Möbius committed
789
790
791
792
793
794
795
796
797
798
799
800
801
  }
}


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


void
PolyLinePlugin::
slot_smart_move_timer()
{
  int smooth_project_iter = tool_->sb_smooth_project_iter->value();

Jan Möbius's avatar
Jan Möbius committed
802
803
804
805
  if (smooth_project_iter)
    slot_smooth_project(cur_smart_move_obj_);
  else // only smooth
    slot_smooth(cur_smart_move_obj_);
Jan Möbius's avatar
   
Jan Möbius committed
806
807
808
809
810
811
812

  emit updateView();
}




813
814
//-----------------------------------------------------------------------------

Jan Möbius's avatar
   
Jan Möbius committed
815
816
817
818
819
820
821
PolyLinePlugin::EditMode
PolyLinePlugin::
mode()
{
  if(tool_ )
  {
    if( tool_->rb_insert->isChecked()         ) return PL_INSERT;
822
    if( tool_->rb_InsertCircle->isChecked()	  ) return PL_INSERTCIRCLE;
823
    if( tool_->rb_InsertSpline->isChecked()	  ) return PL_INSERTSPLINE;
Jan Möbius's avatar
   
Jan Möbius committed
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
    else if( tool_->rb_delete->isChecked()    ) return PL_DELETE;
    else if( tool_->rb_move->isChecked()      ) return PL_MOVE;
    else if( tool_->rb_split->isChecked()     ) return PL_SPLIT;
    else if( tool_->rb_merge->isChecked()     ) return PL_MERGE;
    else if( tool_->rb_smart_move->isChecked()) return PL_SMART_MOVE;
  }

  return PL_NONE;
}

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

void
PolyLinePlugin::
me_insert( QMouseEvent* _event )
{
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
    if (_event->type() == QEvent::MouseMove) {
        if (create_point_ref_) {
            // Pick position
            unsigned int node_idx, target_idx;
            ACG::Vec3d hit_point;
            if (PluginFunctions::scenegraphPick(ACG::SceneGraph::PICK_FACE, _event->pos(), node_idx, target_idx, &hit_point)) {
                *create_point_ref_ = (PolyLine::Point) hit_point;

                // update
                emit updatedObject(cur_insert_id_, UPDATE_GEOMETRY);
            }
        }
        return;
    }

Jan Möbius's avatar
   
Jan Möbius committed
855
  // Pick position
Jan Möbius's avatar
Jan Möbius committed
856
857
858
  unsigned int node_idx, target_idx;
  ACG::Vec3d hit_point;
  if (PluginFunctions::scenegraphPick(ACG::SceneGraph::PICK_FACE, _event->pos(), node_idx, target_idx, &hit_point)) {
Jan Möbius's avatar
   
Jan Möbius committed
859

Jan Möbius's avatar
Jan Möbius committed
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
    switch (_event->type()) {
      // insert new point
      case QEvent::MouseButtonPress: {
        // new Polyline?
        if (cur_insert_id_ == -1 || !PluginFunctions::objectRoot()->childExists(cur_insert_id_)) {
          // add new polyline
          emit addEmptyObject(DATA_POLY_LINE, cur_insert_id_);

          // get current polylineobject
          BaseObjectData *obj = 0;
          PluginFunctions::getObject(cur_insert_id_, obj);
          // default: set as target
          obj->target(true);
          // get polyline object
          cur_polyline_obj_ = PluginFunctions::polyLineObject(obj);

          cur_polyline_obj_->materialNode()->set_random_color();
877
878
879
880

          cur_polyline_obj_->line()->add_point((PolyLine::Point) hit_point);

          emit showStatusMessage(tr("Double click/Enter to terminate poly line. Use with shift to create loop."));
Jan Möbius's avatar
Jan Möbius committed
881
        }
Jan Möbius's avatar
   
Jan Möbius committed
882

Jan Möbius's avatar
Jan Möbius committed
883
884
        // add new point
        cur_polyline_obj_->line()->add_point((PolyLine::Point) hit_point);
885
        create_point_ref_ = &cur_polyline_obj_->line()->points().back();
Jan Möbius's avatar
   
Jan Möbius committed
886

Jan Möbius's avatar
Jan Möbius committed
887
888
        // update
        emit updatedObject(cur_insert_id_, UPDATE_GEOMETRY | UPDATE_TOPOLOGY);
Jan Möbius's avatar
   
Jan Möbius committed
889

Jan Möbius's avatar
Jan Möbius committed
890
891
        break;
      }
Jan Möbius's avatar
   
Jan Möbius committed
892

Jan Möbius's avatar
Jan Möbius committed
893
894
      // finish polyline
      case QEvent::MouseButtonDblClick: {
Jan Möbius's avatar
   
Jan Möbius committed
895

Jan Möbius's avatar
Jan Möbius committed
896
897
898
899
        // close polyline
        if (_event->modifiers() & (Qt::ShiftModifier)) {
          cur_polyline_obj_->line()->set_closed(true);
        }
Jan Möbius's avatar
   
Jan Möbius committed
900

901
902
903
904
905
906
907
908
909
910
911
912
        if (cur_polyline_obj_->line()->n_vertices() >= 2) {
            const PolyLine::Point &p1 = cur_polyline_obj_->line()->point(cur_polyline_obj_->line()->n_vertices() - 1),
                    &p2 = cur_polyline_obj_->line()->point(cur_polyline_obj_->line()->n_vertices() - 2);

            /*
             * Remove duplicate created as a move sentinel.
             */
            if ((p2 - p1).sqrnorm() < 1e-6) {
                cur_polyline_obj_->line()->delete_point(cur_polyline_obj_->line()->n_vertices() - 1);
            }
        }

Jan Möbius's avatar
Jan Möbius committed
913
914
        // update
        emit updatedObject(cur_insert_id_, UPDATE_GEOMETRY | UPDATE_TOPOLOGY);
Jan Möbius's avatar
   
Jan Möbius committed
915

Jan Möbius's avatar
Jan Möbius committed
916
917
918
        // reset current variables
        cur_insert_id_ = -1;
        cur_polyline_obj_ = 0;
919
920
921
        create_point_ref_ = 0;

        clearStatusMessage();
Jan Möbius's avatar
   
Jan Möbius committed
922

Jan Möbius's avatar
Jan Möbius committed
923
        break;
Jan Möbius's avatar
   
Jan Möbius committed
924
925
926
      }

      default:
Jan Möbius's avatar
Jan Möbius committed
927
        break;
Jan Möbius's avatar
   
Jan Möbius committed
928
929
930
931
932
933
    }
  }
}

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

934
935
936
937
938
939
940
941
942
void PolyLinePlugin::
me_insertCircle(QMouseEvent* _event)
{
	TriMeshObject* mesh;
	TriMesh::FaceHandle fh;
	TriMesh::VertexHandle vh;
	ACG::Vec3d hit_point;
	if(!pick_triangle_mesh(_event->pos(), mesh, fh, vh, hit_point))
		return;//can't generate a circle in empty space
943
944

	if(_event->type() == QEvent::MouseMove && createCircle_CurrSelIndex_ != -1) {
Jan Möbius's avatar
Jan Möbius committed
945
946
		PolyLineObject* lineObject = 0;
		if(!PluginFunctions::getObject(createCircle_CurrSelIndex_, lineObject))
947
948
			return;

949
		const ACG::Vec3d n = createCircle_Normal_, x0 = createCircle_Point_;
Jan Möbius's avatar
Jan Möbius committed
950
		const double t = ((n | x0) - (n | hit_point)) / n.sqrnorm();
951
952
953
954
		const ACG::Vec3d onPlane = hit_point + t * n,  d = onPlane - x0;
		const double r = d.norm();
		const ACG::Vec3d mainAxis = (onPlane - x0).normalize(), sideAxis = (mainAxis % n).normalize();

Jan Möbius's avatar
Jan Möbius committed
955
956
957
958
		PolyLineCircleData* lineData = new PolyLineCircleData(x0, n, mainAxis, sideAxis, r, r, mesh->id());
		lineObject->setObjectData(CIRCLE_DATA, lineData);
		updatePolyEllipse(lineObject, tool_->sb_CirclePointNum->value());
		updateHandles(lineObject);
959
960
961
962
963
964
	}
	else if(_event->type() == QEvent::MouseButtonPress) {
		emit addEmptyObject(DATA_POLY_LINE, cur_insert_id_);
		BaseObjectData *obj = 0;
		PluginFunctions::getObject(cur_insert_id_, obj);
		obj->target(true);
965
966
		PolyLineObject* newLine = PluginFunctions::polyLineObject(obj);
		newLine->materialNode()->set_random_color();
967
		newLine->lineNode()->drawMode(ACG::SceneGraph::DrawModes::WIREFRAME);
968
969
970
971

		createCircle_Point_ = hit_point;
		if(!mesh->mesh()->has_face_normals())
			mesh->mesh()->request_face_normals();
972
		createCircle_Normal_ = mesh->mesh()->normal(fh);
973
		emit updatedObject(cur_insert_id_, UPDATE_GEOMETRY | UPDATE_TOPOLOGY);
Jan Möbius's avatar
Jan Möbius committed
974
		createCircle_LastSelIndex_ = createCircle_CurrSelIndex_ = newLine->id();
975
		createSpline_LastSelIndex_ = -1;
976
977
978
979
980
981

		ACG::Vec3d bbMin( FLT_MAX, FLT_MAX, FLT_MAX);
		ACG::Vec3d bbMax(-FLT_MAX,-FLT_MAX,-FLT_MAX);
		mesh->boundingBox(bbMin, bbMax);
		const ACG::Vec3d sizeBB((bbMax-bbMin));

982
		GlutPrimitiveNode* handle0 = new GlutPrimitiveNode(newLine, "N_Handle0");
983
984
985
986
987
988
989
990
		handle0->get_primitive(0).color = ACG::Vec4f(1,0,0,1);
		handle0->set_size(0.005*sizeBB.norm());
		handle0->show();
		handle0->enablePicking(false);
		handle0->set_position(hit_point);
		newLine->addAdditionalNode(handle0, name(), "handle0");
		handle0->drawMode(ACG::SceneGraph::DrawModes::SOLID_FLAT_SHADED);

991
		GlutPrimitiveNode* handle1 = new GlutPrimitiveNode(newLine, "N_Handle1");
992
993
994
995
996
997
998
999
		handle1->get_primitive(0).color = ACG::Vec4f(0,1,0,1);
		handle1->set_size(0.005*sizeBB.norm());
		handle1->show();
		handle1->enablePicking(true);
		handle1->set_position(hit_point);
		newLine->addAdditionalNode(handle1, name(), "handle1");
		handle1->drawMode(ACG::SceneGraph::DrawModes::SOLID_FLAT_SHADED);

1000
		GlutPrimitiveNode* cenNode = new GlutPrimitiveNode(newLine, "N_Center");
1001
1002
1003
1004
1005
1006
1007
1008
1009
		cenNode->get_primitive(0).color = ACG::Vec4f(0,0,1,1);
		cenNode->set_size(0.005*sizeBB.norm());
		cenNode->show();
		cenNode->enablePicking(true);
		cenNode->set_position(hit_point);
		newLine->addAdditionalNode(cenNode, name(), "circle");
		cenNode->drawMode(ACG::SceneGraph::DrawModes::SOLID_FLAT_SHADED);

		emit updatedObject(newLine->id(), UPDATE_ALL);
1010
1011
	}
	else if(_event->type() == QEvent::MouseButtonRelease) {
Jan Möbius's avatar
Jan Möbius committed
1012
1013
		PolyLineObject* lineObject;
		if(!PluginFunctions::getObject(createCircle_CurrSelIndex_, lineObject))
1014
1015
			return;
		ACG::SceneGraph::GlutPrimitiveNode* N;
Jan Möbius's avatar
Jan Möbius committed
1016
		if(lineObject->getAdditionalNode(N, name(), "handle0"))
1017
1018
			N->enablePicking(true);
		createCircle_CurrSelIndex_ = -1;
1019
1020
1021
1022
	}
}

//-----------------------------------------------------------------------------
Jan Möbius's avatar
   
Jan Möbius committed
1023

1024
1025
1026
void PolyLinePlugin::
me_insertSpline(QMouseEvent* _event)
{
Jan Möbius's avatar
Jan Möbius committed
1027

1028
1029
1030
1031
	TriMeshObject* mesh;
	TriMesh::FaceHandle fh;
	TriMesh::VertexHandle vh;
	ACG::Vec3d hit_point;
Jan Möbius's avatar
Jan Möbius committed
1032

1033
1034
	if(!pick_triangle_mesh(_event->pos(), mesh, fh, vh, hit_point))
		return;//can't generate a circle in empty space
Jan Möbius's avatar
Jan Möbius committed
1035

1036
1037
1038
1039
	ACG::Vec3d bbMin( FLT_MAX, FLT_MAX, FLT_MAX);
	ACG::Vec3d bbMax(-FLT_MAX,-FLT_MAX,-FLT_MAX);
	mesh->boundingBox(bbMin, bbMax);
	const ACG::Vec3d sizeBB((bbMax-bbMin));
Jan Möbius's avatar
Jan Möbius committed
1040

1041
1042
	if(!mesh->mesh()->has_face_normals())
		mesh->mesh()->request_face_normals();
Jan Möbius's avatar
Jan Möbius committed
1043

1044
	ACG::Vec3d nor = mesh->mesh()->normal(fh);
Jan Möbius's avatar
Jan Möbius committed
1045

1046
	if(_event->type() == QEvent::MouseButtonPress) {
Jan Möbius's avatar
Jan Möbius committed
1047

Jan Möbius's avatar
Jan Möbius committed
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
	  if(createSpline_CurrSelIndex_ == -1) {

	    emit addEmptyObject(DATA_POLY_LINE, createSpline_CurrSelIndex_);
	    createSpline_LastSelIndex_ = createSpline_CurrSelIndex_;
	    createCircle_LastSelIndex_ = -1;
	    BaseObjectData *obj = 0;
	    PluginFunctions::getObject(createSpline_CurrSelIndex_, obj);
	    obj->target(true);
	    PolyLineObject* newLine = PluginFunctions::polyLineObject(obj);
	    newLine->materialNode()->set_random_color();
	    newLine->lineNode()->drawMode(ACG::SceneGraph::DrawModes::WIREFRAME);
	    emit updatedObject(createSpline_CurrSelIndex_, UPDATE_GEOMETRY | UPDATE_TOPOLOGY);

	    PolyLineBezierSplineData* lineData = new PolyLineBezierSplineData(mesh->id());
	    newLine->setObjectData(BEZSPLINE_DATA, lineData);

	    GlutLineNode* lineN = new GlutLineNode(newLine, "N_Line");
	    lineN->show();
	    lineN->enablePicking(false);
	    newLine->addAdditionalNode(lineN, name(), "line");
	    lineN->drawMode(ACG::SceneGraph::DrawModes::WIREFRAME);
	  }

	  ACG::Vec3d insert_Point = hit_point + nor * 0.003 * sizeBB.norm();
	  PolyLineObject* lineObject = 0;

	  if(!PluginFunctions::getObject(createSpline_CurrSelIndex_, lineObject))
	    return;

	  PolyLineBezierSplineData* splineData = dynamic_cast<PolyLineBezierSplineData*>(lineObject->objectData(BEZSPLINE_DATA));
	  GlutPrimitiveNode* handle0 = new GlutPrimitiveNode(lineObject, "N_Control", splineData->points_.size());
	  handle0->get_primitive(0).color = ACG::Vec4f(0,1,0,1);
	  handle0->set_size(0.005*sizeBB.norm());
	  handle0->show();
	  handle0->set_position(insert_Point);
	  handle0->drawMode(ACG::SceneGraph::DrawModes::SOLID_FLAT_SHADED);
1084
		handle0->enablePicking(false);
Jan Möbius's avatar
Jan Möbius committed
1085
		lineObject->addAdditionalNode(handle0, name(), "control", splineData->points_.size());
1086
1087
1088

		emit updatedObject(createSpline_CurrSelIndex_, UPDATE_ALL);
		splineData->addInterpolatePoint(insert_Point, nor);
Jan Möbius's avatar
Jan Möbius committed
1089

1090
1091
	}
	if(_event->type() == QEvent::MouseButtonDblClick) {
Jan Möbius's avatar
Jan Möbius committed
1092

1093
		PolyLineObject* lineObject = 0;
Jan Möbius's avatar
Jan Möbius committed
1094

1095
1096
		if(!PluginFunctions::getObject(createSpline_CurrSelIndex_, lineObject))
			return;
Jan Möbius's avatar
Jan Möbius committed
1097

1098
1099
		GlutPrimitiveNode* control = 0;
		PolyLineBezierSplineData* splineData = dynamic_cast<PolyLineBezierSplineData*>(lineObject->objectData(BEZSPLINE_DATA));
Jan Möbius's avatar
Jan Möbius committed
1100
1101

		for(unsigned int i = 0; i < splineData->points_.size(); i++) {
1102
1103
1104
			lineObject->getAdditionalNode(control, name(), "control", i);
			control->enablePicking(true);
		}
Jan Möbius's avatar
Jan Möbius committed
1105

1106
1107
		if(splineData->finishSpline()) {

Jan Möbius's avatar
Jan Möbius committed
1108
			for(unsigned int i = 0; i < splineData->handles_.size(); i++) {
1109
				const PolyLineBezierSplineData::InterpolatePoint& control = splineData->getInterpolatePoint(i);
Jan Möbius's avatar
Jan Möbius committed
1110
				const ACG::Vec3d hndlPos = splineData->handles_[i], ctrlPos = control.position;
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136

				GlutPrimitiveNode* handle0 = new GlutPrimitiveNode(lineObject, "N_Handle", i);
				handle0->get_primitive(0).color = ACG::Vec4f(0,0,1,1);
				handle0->set_size(0.004*sizeBB.norm());
				handle0->show();
				handle0->enablePicking(true);
				handle0->set_position(hndlPos);
				lineObject->addAdditionalNode(handle0, name(), "handle", i);
				handle0->drawMode(ACG::SceneGraph::DrawModes::SOLID_FLAT_SHADED);

				GlutLineNode* lineN;
				if(lineObject->getAdditionalNode(lineN,  name(), "line")) {
					lineN->add_line(ctrlPos, hndlPos);
					lineN->add_color(ACG::Vec4f(1,0,0,1));
				}

				emit updatedObject(createSpline_CurrSelIndex_, UPDATE_ALL);
			}
			updatePolyBezierSpline(lineObject, tool_->sb_SplineSegNum->value());
			createSpline_CurrSelIndex_ = -1;
		}
	}
}

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

Jan Möbius's avatar
   
Jan Möbius committed
1137
1138
1139
1140
1141
void
PolyLinePlugin::
me_delete( QMouseEvent* _event )
{
  // MousePress ?
Jan Möbius's avatar
Jan Möbius committed
1142
1143
1144
1145
  if (_event->type() == QEvent::MouseButtonPress) {

    unsigned int node_idx, target_idx;
    ACG::Vec3d hit_point;
Jan Möbius's avatar
   
Jan Möbius committed
1146
    // pick
Jan Möbius's avatar
Jan Möbius committed
1147
1148
    if (PluginFunctions::scenegraphPick(ACG::SceneGraph::PICK_ANYTHING, _event->pos(), node_idx, target_idx, &hit_point)) {

Jan Möbius's avatar
   
Jan Möbius committed
1149
      BaseObjectData* obj = 0;
Jan Möbius's avatar
Jan Möbius committed
1150
1151

      if (PluginFunctions::getPickedObject(node_idx, obj))
Jan Möbius's avatar
   
Jan Möbius committed
1152
        // is picked object polyline?
Jan Möbius's avatar
Jan Möbius committed
1153
        if (PluginFunctions::polyLineObject(obj)) {
Jan Möbius's avatar
   
Jan Möbius committed
1154
1155
1156
          emit deleteObject(obj->id());
        }
    }
Jan Möbius's avatar
Jan Möbius committed
1157

Jan Möbius's avatar
   
Jan Möbius committed
1158
  }
Jan Möbius's avatar
Jan Möbius committed
1159

Jan Möbius's avatar
   
Jan Möbius committed
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
}


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


void
PolyLinePlugin::
me_move( QMouseEvent* _event )
{
1170
  // MousePress ? -> get reference point
Jan Möbius's avatar
Jan Möbius committed
1171
1172
1173
1174
  if (_event->type() == QEvent::MouseButtonPress) {

    unsigned int node_idx, target_idx;
    ACG::Vec3d hit_point;
Jan Möbius's avatar
   
Jan Möbius committed
1175
    // pick
1176
    if (PluginFunctions::scenegraphPick(ACG::SceneGraph::PICK_ANYTHING, _event->pos(), node_idx, target_idx, &hit_point)) {
Jan Möbius's avatar
Jan Möbius committed
1177

1178
      BaseNode* node = find_node( PluginFunctions::getRootNode(), node_idx );
1179
      GlutPrimitiveNode* glutNode = dynamic_cast<GlutPrimitiveNode*>(node);
Jan Möbius's avatar
Jan Möbius committed
1180

1181
      if(glutNode) {
Jan Möbius's avatar
Jan Möbius committed
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
        PolyLineCircleData* circleData = dynamic_cast<PolyLineCircleData*>(glutNode->line->objectData(CIRCLE_DATA));
        PolyLineBezierSplineData* splineData = dynamic_cast<PolyLineBezierSplineData*>(glutNode->line->objectData(BEZSPLINE_DATA));
        if(circleData) {
          moveCircle_SelNode_        = glutNode;
          createCircle_LastSelIndex_ = createCircle_CurrSelIndex_ = glutNode->line->id();
        }
        if(splineData) {
          moveBezSpline_SelNode_ = glutNode;
          createSpline_LastSelIndex_ = moveBezSpline_SelIndex_ = glutNode->line->id();
          moveBezSpline_SelSubIndex_ = glutNode->index;
        }
1193
1194
      }

Jan Möbius's avatar
   
Jan Möbius committed
1195
      BaseObjectData* obj = 0;
Jan Möbius's avatar
Jan Möbius committed
1196
1197
1198
      if (PluginFunctions::getPickedObject(node_idx, obj)) {
        // is picked object polyline?
        PolyLineObject* cur_pol = PluginFunctions::polyLineObject(obj);
1199
        if (cur_pol && !cur_pol->objectData(CIRCLE_DATA) && !cur_pol->objectData(BEZSPLINE_DATA)) {//no vertex dragging on circles!
1200
1201
1202
1203
1204

          // Check if we got a line segment or a vertex
          if ( target_idx >= cur_pol->line()->n_vertices() )
            return;

Jan Möbius's avatar
Jan Möbius committed
1205
1206
          // save references
          cur_move_id_ = cur_pol->id();
1207

Jan Möbius's avatar
Jan Möbius committed
1208
1209
          move_point_ref_ = &(cur_pol->line()->point(target_idx));
        }
Jan Möbius's avatar
   
Jan Möbius committed
1210
1211
1212
1213
1214
      }
    }
  }

  // Move ? -> move reference point
1215
  if (_event->type() == QEvent::MouseMove){
Jan Möbius's avatar
Jan Möbius committed
1216

1217
1218
    unsigned int node_idx, target_idx;
    ACG::Vec3d hit_point;
Jan Möbius's avatar
Jan Möbius committed
1219

1220
    if(moveCircle_SelNode_) {
Jan Möbius's avatar
Jan Möbius committed
1221

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

Jan Möbius's avatar
Jan Möbius committed
1224
1225
        PolyLineObject* lineObject;
        if(!PluginFunctions::getObject(createCircle_CurrSelIndex_, lineObject))
1226
1227
          return;

Jan Möbius's avatar
Jan Möbius committed
1228
        PolyLineCircleData* lineData = dynamic_cast<PolyLineCircleData*>(lineObject->objectData(CIRCLE_DATA));
1229
1230
        double cr;
        ACG::Vec3d onPlane;
Jan Möbius's avatar
Jan Möbius committed
1231
        const ACG::Vec3d x0 = lineData->circleCenter_, n = lineData->circleNormal_;
1232

Jan Möbius's avatar
Jan Möbius committed
1233
        createCircle_getHitInfo(lineData, hit_point, &hit_point, &cr, &onPlane);
1234
1235

        if(!moveCircle_SelNode_->name().compare("N_Center")) {
Jan Möbius's avatar
Jan Möbius committed
1236
          lineData->circleCenter_ = hit_point;
1237
1238
1239
1240
1241
        }
        else {
          ACG::Vec3d axisa = (onPlane - x0).normalize();
          if(!moveCircle_SelNode_->name().compare("N_Handle0")) {
            ACG::Vec3d axisb = (axisa % n).normalize();
Jan Möbius's avatar
Jan Möbius committed
1242
            lineData->circleMainRadius_ = cr;
Jan Möbius's avatar
Jan Möbius committed
1243
1244
1245
1246

            if(_event->modifiers() & Qt::ShiftModifier)
              lineData->circleSideRadius_ = cr;

Jan Möbius's avatar
Jan Möbius committed
1247
1248
            lineData->circleMainAxis_ = axisa;
            lineData->circleSideAxis_ = axisb;
1249
1250
          } else {
            ACG::Vec3d axisb = (n % axisa).normalize();
Jan Möbius's avatar
Jan Möbius committed
1251