PolyLinePlugin.hh 17.7 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
/*===========================================================================*\
 *                                                                           *
 *                              OpenFlipper                                  *
 *           Copyright (c) 2001-2015, RWTH-Aachen University                 *
 *           Department of Computer Graphics and Multimedia                  *
 *                          All rights reserved.                             *
 *                            www.openflipper.org                            *
 *                                                                           *
 *---------------------------------------------------------------------------*
 * This file is part of OpenFlipper.                                         *
 *---------------------------------------------------------------------------*
 *                                                                           *
 * Redistribution and use in source and binary forms, with or without        *
 * modification, are permitted provided that the following conditions        *
 * are met:                                                                  *
 *                                                                           *
 * 1. Redistributions of source code must retain the above copyright notice, *
 *    this list of conditions and the following disclaimer.                  *
 *                                                                           *
 * 2. Redistributions in binary form must reproduce the above copyright      *
 *    notice, this list of conditions and the following disclaimer in the    *
 *    documentation and/or other materials provided with the distribution.   *
 *                                                                           *
 * 3. Neither the name of the copyright holder nor the names of its          *
 *    contributors may be used to endorse or promote products derived from   *
 *    this software without specific prior written permission.               *
 *                                                                           *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       *
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A           *
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  *
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,       *
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR        *
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    *
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING      *
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS        *
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.              *
 *                                                                           *
\*===========================================================================*/

42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
//=============================================================================
//
// CLASS PolyLinePlugin
//
// Author:  David Bommes <bommes@cs.rwth-aachen.de>
//
// Version: $Revision: 1$
// Date:    $Author$
//   $Date: 03-10-2007$
//
//
//=============================================================================

#ifndef POLYLINEPLUGIN_HH
#define POLYLINEPLUGIN_HH

#define CGAL_NO_AUTOLINK_MPFR
#define CGAL_NO_AUTOLINK_GMP

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

#include <QObject>
#include <QMenuBar>
#include <QTimer>

#include <OpenFlipper/BasePlugin/BaseInterface.hh>
#include <OpenFlipper/BasePlugin/MouseInterface.hh>
#include <OpenFlipper/BasePlugin/PickingInterface.hh>
#include <OpenFlipper/BasePlugin/ToolboxInterface.hh>
#include <OpenFlipper/BasePlugin/BackupInterface.hh>
#include <OpenFlipper/BasePlugin/LoggingInterface.hh>
#include <OpenFlipper/BasePlugin/ToolbarInterface.hh>
#include <OpenFlipper/BasePlugin/LoadSaveInterface.hh>
75 76
#include <OpenFlipper/BasePlugin/KeyInterface.hh>
#include <OpenFlipper/BasePlugin/StatusbarInterface.hh>
77
#include <OpenFlipper/BasePlugin/ScriptInterface.hh>
78
#include <OpenFlipper/BasePlugin/ContextMenuInterface.hh>
79 80 81 82 83 84 85
#include <OpenFlipper/common/Types.hh>
#include <ObjectTypes/PolyLine/PolyLine.hh>
#include <ObjectTypes/PolyMesh/PolyMesh.hh>
#include <ObjectTypes/TriangleMesh/TriangleMesh.hh>

#include "PolyLineToolbarWidget.hh"

Jan Möbius's avatar
Jan Möbius committed
86
#include <ObjectTypes/Plane/QtPlaneSelect.hh>
87

88 89 90
#include <ACG/Scenegraph/GlutPrimitiveNode.hh>

#include <ObjectTypes/PolyLine/PolyLineCircleData.hh>
91
#include <ObjectTypes/PolyLine/PolyLineBezierSplineData.hh>
92

93
#define CREATE_CUT_POLYLINE "Create Polyline"
94
#define CREATE_CUT_POLYLINES "Create Polylines"
95 96 97 98 99 100

//== CLASS DEFINITION =========================================================


/** Plugin for PolyLine Support
 */
101 102 103 104 105 106 107 108 109 110
class PolyLinePlugin: public QObject,
                      BaseInterface,
                      MouseInterface,
                      PickingInterface,
                      ToolboxInterface,
                      LoggingInterface,
                      LoadSaveInterface,
                      ToolbarInterface,
                      StatusbarInterface,
                      KeyInterface,
111 112
                      ScriptInterface,
                      ContextMenuInterface {
113 114
    Q_OBJECT
    Q_INTERFACES(BaseInterface)
115 116 117 118 119 120
    Q_INTERFACES(MouseInterface)
    Q_INTERFACES(PickingInterface)
    Q_INTERFACES(ToolboxInterface)
    Q_INTERFACES(LoggingInterface)
    Q_INTERFACES(LoadSaveInterface)
    Q_INTERFACES(ToolbarInterface)
121 122
    Q_INTERFACES(StatusbarInterface)
    Q_INTERFACES(KeyInterface)
123
    Q_INTERFACES(ScriptInterface)
124
    Q_INTERFACES(ContextMenuInterface)
125

Matthias Möller's avatar
Matthias Möller committed
126 127 128 129
#if QT_VERSION >= 0x050000
  Q_PLUGIN_METADATA(IID "org.OpenFlipper.Plugins.Plugin-PolyLine")
#endif

130
signals:
131

132 133
  // BaseInterface
  void updateView();
134
  void updatedObject(int _identifier, const UpdateType& _type);
135
  void nodeVisibilityChanged(int _id);
136 137 138 139 140

  // PickingInterface
  void addPickMode( const std::string& _mode );
  void addHiddenPickMode( const std::string& _mode );
  void setPickModeToolbar (const std::string& _mode, QToolBar * _toolbar);
141
  void setPickModeMouseTracking(const std::string& _mode , bool _mouseTracking);
142 143 144 145 146 147 148 149

  // LoggingInterface
  void log(Logtype _type, QString _message);
  void log(QString _message);

  // ToolbarInterface
  void addToolbar(QToolBar* _toolbar);
  void getToolBar( QString _name, QToolBar*& _toolbar);
150

151 152
  // ScriptInterface
  void scriptInfo( QString _functionName );
153

154
  // ToolboxInterface
155
  void addToolbox( QString  _name  , QWidget* _widget, QIcon* _icon);
156

157
  void deleteObject( int _id );
158
  void addEmptyObject( DataType _type, int& _id);
159

160 161 162
  void registerKey(int _key, Qt::KeyboardModifiers _modifiers, QString _description, bool _multiUse = false);
  void showStatusMessage(QString _message, int _timeout = 0);
  void clearStatusMessage();
163

164 165 166
  //ContextMenuInterface
  void addContextMenuItem(QAction* _action ,DataType _objectType , ContextMenuType _type );

167 168 169 170 171 172 173
private slots :
  // BaseInterface
  void initializePlugin();

  void slotEditModeChanged();

  void slotMouseEvent( QMouseEvent* _event );
174
  void slotKeyEvent(QKeyEvent* _event);
175 176 177 178 179

  void slotPickModeChanged( const std::string& _mode);

  void pluginsInitialized();

180 181 182
  bool pick_triangle_mesh( QPoint mPos,
  						TriMeshObject*& _mesh_object_, TriMesh::FaceHandle& _fh, TriMesh::VertexHandle& _vh, ACG::Vec3d& _hitPoint);

183
  void slotUpdateContextMenu(int objectId);
184 185 186 187 188 189 190 191 192
public slots :

  void slotEnablePickMode(QString _name);

public :

  /// Edit Mode of PolyLinePlugin
  enum EditMode { PL_NONE  =0,
                  PL_INSERT=1,
193
                  PL_INSERTCIRCLE,
194
                  PL_INSERTSPLINE,
195 196 197 198
                  PL_DELETE,
                  PL_MOVE,
                  PL_SPLIT,
                  PL_MERGE,
199 200
                  PL_SMART_MOVE,
                  PL_COPY_PASTE };
201 202 203 204 205 206 207 208 209 210 211 212 213

  /// default constructor
  PolyLinePlugin();

  /// default destructor
  ~PolyLinePlugin() {};

  /// Name of the Plugin
  QString name(){ return (QString("PolyLine")); };

  /// Description of the Plugin
  QString description() { return (QString("Handling of PolyLines (Insertion/Deletion/Modeling/...")); };

214 215 216 217 218 219
public slots:

#ifdef EXTENDED_POLY_LINE
    void slot_resample_on_edges();
#endif

220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
private slots:

  void slot_subdivide();
  void slot_subdivide_percent(bool _checked);
  void slot_decimate();
  void slot_decimate_percent(bool _checked);

  void slot_smooth();
  void slot_smooth( PolyLineObject*& _pol);
  void slot_project();
  void slot_project( PolyLineObject*& _pol);
  void slot_smooth_project();
  void slot_smooth_project( PolyLineObject*& _pol);

  void slot_smart_move_timer();

236
  void slot_setCirclePointNum(int i);
237
  void slot_setSplinePointNum(int i);
238

239
  virtual void slotObjectUpdated( int _identifier, const UpdateType &_type );
240

241 242 243
  void slot_duplicate();
  void slot_instanciate();
  void finishSpline();
244 245 246 247 248
private :

  EditMode mode();

  // mouse events
249 250
  void me_insert      ( QMouseEvent* _event );
  void me_insertCircle( QMouseEvent* _event );
251
  void me_insertSpline( QMouseEvent* _event );
252 253 254 255 256
  void me_delete      ( QMouseEvent* _event );
  void me_move        ( QMouseEvent* _event );
  void me_split       ( QMouseEvent* _event );
  void me_merge       ( QMouseEvent* _event );
  void me_smart_move  ( QMouseEvent* _event );
257 258 259 260 261

  //===========================================================================
  /** @name ToolBox
  * @{ */
  //===========================================================================
262

263
  private :
264

265 266
    /// Widget for Toolbox
    PolyLineToolbarWidget* tool_;
267 268 269

  /** @} */

270 271 272 273
  //===========================================================================
  /** @name ToolBar
  * @{ */
  //===========================================================================
274

275 276 277
  private :
    QAction*      polyLineAction_;
    QActionGroup* toolBarActions_;
278

279
    QToolBar*     toolbar_;
280

281
  private slots:
282

283 284
    /// Called by Toolbar to enable pick mode
    void slotSetPolyLineMode(QAction* _action);
285 286 287

  /** @} */

288 289 290 291
  //===========================================================================
  /** @name PickToolBar
  * @{ */
  //===========================================================================
292

293 294 295
  private :
    QToolBar*     pickToolbar_;
    QActionGroup* pickToolBarActions_;
296

297
    QAction*      insertAction_;
298
    QAction*	  insertCircleAction_;
299
    QAction*	  insertSplineAction_;
300 301 302 303 304 305
    QAction*      deleteAction_;
    QAction*      moveAction_;
    QAction*      smartMoveAction_;
    QAction*      mergeAction_;
    QAction*      splitAction_;
    QAction*      cutAction_;
306
    QAction*      cutMultipleAction_;
307

308
  private slots:
309

310 311
    /// Called by pick Toolbar
    void slotPickToolbarAction(QAction* _action);
312

313
  /** @} */
314

315 316 317 318
  //===========================================================================
  /** @name Template Functions
  * @{ */
  //===========================================================================
319

320
  private:
321

322
  /// get the points from the closest connected intersection between mesh and plane
323 324 325
  template< class MeshT > std::vector< ACG::Vec3d >
  getIntersectionPoints ( MeshT* _mesh, uint _fh, ACG::Vec3d _planeNormal ,
                          ACG::Vec3d _planePoint, bool& _closed );
326

327 328 329 330
  /// get all points from the intersection between mesh and plane
  template< class MeshT > std::vector< std::vector< ACG::Vec3d > >
  getMultipleIntersectionPoints( MeshT* _mesh, ACG::Vec3d _planeNormal , ACG::Vec3d _planePoint );

331 332 333 334 335 336
  /// get an edge of the mesh that is cut by the plane
  template< class MeshT >
  typename MeshT::EdgeHandle
  getCuttedEdge(MeshT& _mesh, ACG::Vec3d& _planeNormal, ACG::Vec3d& _planePoint);

  /** @} */
337

338
public slots:
339

340
  QString version() { return QString("1.02"); };
341

342
  //============================================
343

344 345
  /// Generates a polyLine of a plane intersection
  int generatePolyLineFromCut( int _objectId, Vector _planePoint, Vector _planeNormal, int _polyLineId = -1 );
346

347
  /// Generates a polyLine of a plane intersection
348
  IdList generatePolyLinesFromCut( int _objectId, Vector _planePoint, Vector _planeNormal );
349

350
private slots:
351

352 353
  /// Scissor Button was hit
  void slotScissorButton();
354

355 356 357
  /// Scissor Button for multiple polylines was hit
  void slotScissorLinesButton();

358 359
  /// Generate PolyLine after the cutPlane has been drawn
  void slotTriggerCutPlaneSelect();
360

361
private:
362 363
    int               cur_insert_id_;
    PolyLineObject   *cur_polyline_obj_;
364

365 366
    int               cur_move_id_;
    PolyLine::Point*  move_point_ref_;
367
    PolyLine::Point*  create_point_ref_;
368
    PolyLine::Point   move_point_orig_;
369

Jan Möbius's avatar
Jan Möbius committed
370 371 372 373 374
    /// The object which is being modified(created, dragged)
    int				    createCircle_CurrSelIndex_;
    /// Use this one to mark the last index to update the number of points
    int					createCircle_LastSelIndex_;
    /// The handle which is being dragged.
375
    ACG::SceneGraph::GlutPrimitiveNode* moveCircle_SelNode_;
376 377 378 379 380
    ///The last valid hit on the mesh
    ACG::Vec3d          moveCircle_LastHitPos_;
    ACG::Vec3d          moveCircle_LastHitNor_;
    bool                moveCircle_IsLocked;
    bool                moveCircle_IsFloating;
381

382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
    QAction*            copyPaste_Action_;
    int                 copyPaste_ObjectId_;
    int                 copyPaste_ActionType_;
    int                 copyPaste_NewObjectId_;
    std::vector<ACG::Vec3d> copyPaste_RelativePoints_;
    QPoint              copyPaste_LastMouse;

    /*
     * \brief Handles the mouse event during a copy paste event
     *
     * Determines wether duplicate or instanciate is happening and performs the
     * action, ending the mode.
     */
    void me_copyPasteMouse(QMouseEvent* _event);

Jan Möbius's avatar
Jan Möbius committed
397
    /**
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
	 * \brief Calculates a point on the mesh.
	 *
	 * Finds the nearest point on the mesh given a point on the normal plane
	 *
	 * @param _triMeshObject The mesh to use
	 * @param _center The center of the ellipse
	 * @param _pOnPlane The projection of the hit point on the plane
	 * @param _n The normal of the ellipse
	 * @param _pOut The point on the mesh if one could be found.
	 * @return True if a point could be found.
	 */
	bool createCircle_getPointOnMesh(TriMeshObject* _triMeshObject,
									 ACG::Vec3d     _center,
									 ACG::Vec3d     _pOnPlane,
									 ACG::Vec3d     _n,
									 ACG::Vec3d*    _pOut);

Jan Möbius's avatar
Jan Möbius committed
415
	/** \brief Calculates common info.
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
	 *
	 * Calculates common info like point on the mesh, point on the normal plane and the distance from the circle center
	 *
	 * @param _circleData The circle to use.
	 * @param _hit_Point The point which to "project".
	 * @param _pOut (optional)Point on the mesh.
	 * @param _r The distance between circle center and point on the plane.
	 * @param _onPlane Point on the normal plane regarding the hit_point.
	 */
	bool createCircle_getHitInfo(PolyLineCircleData* _circleData,
								 ACG::Vec3d          _hit_Point,
								 ACG::Vec3d*         _pOut = 0,
								 double*             _r = 0,
								 ACG::Vec3d*         _onPlane = 0);

	/** \brief Returns point on mesh or point on the normal plane.
	 *
	 * Helper function which returns the point on the mesh or if none could be found the point on the plane
	 *
	 * @param _circleData The circle to use.
	 * @param _hit_point The point to "project".
	 *
	 * @return Point on the mesh if one was found or on the normal plane.
	 */
	ACG::Vec3d createCircle_getHit(PolyLineCircleData* _circleData, ACG::Vec3d _hit_point);

442 443 444 445 446 447 448 449 450 451 452 453
public slots:
	/* \brief Creates the UI elements for the poly line(asserting it has circle data)
	 *
	 */
	void createCircle_createUI(int _polyLineObjectID);

	/* \brief Creates the UI elements for the poly line(asserting it has spline data)
	 *
	 */
	void createSpline_createUI(int _polyLineObjectID);

private:
Jan Möbius's avatar
Jan Möbius committed
454
	/** \brief Generates points for the ellipse.
455 456 457 458 459 460 461
	 *
	 * @param _lineObject The object to generate points for.
	 * @param _pointCount The number of points to generate.
	 *
	 */
	void updatePolyEllipse(PolyLineObject* _lineObject, unsigned int _pointCount);

Jan Möbius's avatar
Jan Möbius committed
462
	/** \brief Updates the center, forward and side handle of the Poly ellipse
463 464 465 466 467
	 *
	 * @param _lineObject The object containing the handles.
	 */
	void updateHandles(PolyLineObject* _lineObject);

Jan Möbius's avatar
Jan Möbius committed
468
	/** \brief Generates points for the spline, updates handles.
469
	 *
Jan Möbius's avatar
Jan Möbius committed
470 471
	 * @param _lineObject       The object to generate points for.
	 * @param _pointsPerSegment The number of points per segment.
472 473 474 475
	 *
	 */
	void updatePolyBezierSpline(PolyLineObject* _lineObject, unsigned int _pointsPerSegment);

Jan Möbius's avatar
Jan Möbius committed
476
	/** \brief Updates all the handles on the PolyBezier
477 478 479 480 481
	 * @param _lineObject The object to update.
	 * @param _line the line node containing all the handles
	 */
	void updatePolyBezierHandles(PolyLineObject* _lineObject, ACG::SceneGraph::LineNode* _line);

Jan Möbius's avatar
Jan Möbius committed
482
	/** \brief Returns the nearest point on the mesh or if none could be found the input.
483 484 485 486 487 488 489 490 491 492 493 494
	 *
	 * @param _SplineData The spline which is being used.
	 * @param _point The input point.
	 * @param _nor (Optional)Normal at nearest point.
	 * @return The nearest point on the mesh from _point
	 */
	ACG::Vec3d getPointOnMesh(PolyLineBezierSplineData* _SplineData, ACG::Vec3d _point, ACG::Vec3d* _nor = 0);

	/// The index of the currently created spline.
	int createSpline_CurrSelIndex_;
	/// Use this one to mark the last index to update the number of points
	int createSpline_LastSelIndex_;
495

Jan Möbius's avatar
Jan Möbius committed
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
	/// The handle which is being dragged.
	ACG::SceneGraph::GlutPrimitiveNode* moveBezSpline_SelNode_;
	/// The object which is being moved
	int moveBezSpline_SelIndex_;
	/// The index of the control or handle being moved
	int moveBezSpline_SelSubIndex_;

	int               cur_merge_id_;

	QTimer*           smart_move_timer_;

	PolyLineObject*   cur_smart_move_obj_;

	/// Plane selection tool
	QtPlaneSelect*    planeSelect_;
511 512 513 514

};

#if defined(INCLUDE_TEMPLATES) && !defined(POLYLINEPLUGIN_CC)
515
#define POLYLINEPLUGIN_TEMPLATES
516 517 518 519
#include "PolyLinePluginT.cc"
#endif

#endif //MOVEPLUGIN_HH