Developer Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
QtPlaneSelect.cc
1 /*===========================================================================*\
2 * *
3 * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39 * *
40 \*===========================================================================*/
41 
42 /*===========================================================================*\
43 * *
44 * $Revision$ *
45 * $LastChangedBy$ *
46 * $Date$ *
47 * *
48 \*===========================================================================*/
49 
50 #include "QtPlaneSelect.hh"
51 
53 #include <ACG/Scenegraph/GlutPrimitiveNode.hh>
54 #define PLUGINFUNCTIONS_C
56 
57 #include <ACG/QtWidgets/QtColorTranslator.hh>
58 
59 
60 /*******************************************************************************
61  Initialization and de initialization
62 *******************************************************************************/
63 QtPlaneSelect::QtPlaneSelect( ACG::GLState& glState )
64  : glState( glState ),
65  nodeIdx_(0),
66  targetIdx_(0),
67  isDragging( false ),
68  planeNode_(0)
69 {
70 
71 }
72 
73 QtPlaneSelect::~QtPlaneSelect( )
74 {
75 }
76 
77 
78 /*******************************************************************************
79  Implementation of public slots
80 *******************************************************************************/
81 
82 void QtPlaneSelect::slotKeyReleaseEvent(QKeyEvent* event)
83 {
84  if (event->key() == Qt::Key_Escape){
85  if( planeNode_ )
86  {
87  planeNode_->delete_subtree( );
88  planeNode_ = 0;
89  }
90 
91  emit updateViewProxy( );
92 
93  // Trigger the event
94  isDragging = false;
95  }
96 }
97 
98 void QtPlaneSelect::slotMouseEvent(QMouseEvent* event)
99 {
100 // unsigned int width = glState.viewport_width();
101  unsigned int height = glState.viewport_height();
102 
103  //cancel on rightclick
104  if (event->button() == Qt::RightButton){
105  if( planeNode_ )
106  {
107  planeNode_->delete_subtree( );
108  planeNode_ = 0;
109  }
110 
111  emit updateViewProxy( );
112 
113  // Trigger the event
114  isDragging = false;
115 
116  return;
117  }
118 
119 
120  switch( event->type() )
121  {
122  case QEvent::MouseButtonPress:
123  {
124 
125  // Only react on the left button and ignore the others
126  if ( event->button() != Qt::LeftButton )
127  return;
128 
129 
130  unsigned int node_idx, target_idx;
131 
133  event->pos(),
134  node_idx,
135  target_idx,
136  &sourcePoint3D))
137  {
138 
139  // Shift toward viewer
140  //sourcePoint3D = sourcePoint3D + PluginFunctions::viewingDirection();
141 
142  isDragging = true;
143 
144  if ( planeNode_ == 0 ) {
145  planeNode_ = new PlaneNode(plane_,PluginFunctions::getRootNode(),"PolyLine generation Plane" );
146  }
147 
148  setPlaneAndSize(sourcePoint3D,ACG::Vec3d(event->pos().x(), height-event->pos().y()-1.0, 0.0));
149 
150  planeNode_->show();
151  emit nodeVisChangedProxy(planeNode_->id());
152 
153  nodeIdx_ = node_idx;
154  targetIdx_ = target_idx;
155 
156 
157  emit updateViewProxy( );
158  }
159  }break;
160  case QEvent::MouseMove:
161  {
162  if( isDragging )
163  {
164  setPlaneAndSize(sourcePoint3D,ACG::Vec3d(event->pos().x(), height-event->pos().y()-1.0, 0.0));
165 
166  emit updateViewProxy( );
167  }
168  } break;
169 
170  case QEvent::MouseButtonRelease:
171  {
172  if( isDragging )
173  {
174  if( planeNode_ )
175  {
176  planeNode_->delete_subtree( );
177  planeNode_ = NULL;
178  }
179 
180  emit updateViewProxy( );
181 
182  emit( signalTriggerCut( ) );
183  // Trigger the event
184  isDragging = false;
185  }
186  } break;
187 
188  default:
189  break;
190  }
191 
192 
193 }
194 
195 
196 void QtPlaneSelect::setPlaneAndSize(const ACG::Vec3d& _sourcePoint3D,const ACG::Vec3d& _target2D)
197 {
198 
199  ACG::Vec3d source2D = glState.project( _sourcePoint3D );
200 
201  source2D[2] = 0;
202 
203 
204  ACG::Vec3d diff = source2D - _target2D;
205  //diff.normalize( ); <- this is bad
206  ACG::Vec3d ortho(-diff[1], diff[0], 0 );
207 
208  ACG::Vec3d left = glState.unproject( source2D+ortho*10 + ACG::Vec3d(0,0,0) );
209  ACG::Vec3d right= glState.unproject( source2D-ortho*10 + ACG::Vec3d(0,0,0) );
210 
211  ACG::Vec3d leftvec = left-sourcePoint3D;
212  leftvec.normalize( );
213  ACG::Vec3d rightvec = right-sourcePoint3D;
214  rightvec.normalize( );
215 
216  normal = cross( rightvec, leftvec );
217  normal.normalize( );
218 
219  ACG::Vec3d sourcePoint3Df(sourcePoint3D);
220  ACG::Vec3d normald(normal);
221  plane_.setPlane(sourcePoint3Df,normald);
223  planeNode_->update();
224 }
225 
226 
227 
228 
unsigned int id() const
Definition: BaseNode.hh:399
picks faces (should be implemented for all nodes)
Definition: BaseNode.hh:104
Vec3d unproject(const Vec3d &_winPoint) const
unproject point in window coordinates _winPoint to world coordinates
Definition: GLState.cc:649
osg::Vec3f cross(const osg::Vec3f &_v1, const osg::Vec3f &_v2)
Adapter for osg vector member computing a scalar product.
void show()
Show node: set status to Active.
Definition: BaseNode.hh:383
void setSize(double _xDirection, double _yDirection)
Set plane size.
Definition: PlaneType.cc:108
void update()
updates the plane before the next render call
Definition: PlaneNode.cc:323
void delete_subtree()
Delete the whole subtree of this node.
ACG::SceneGraph::BaseNode * getRootNode()
Get the root node for data objects.
double sceneRadius()
Returns the current scene radius from the active examiner widget.
void setPlane(const ACG::Vec3d &_position, const ACG::Vec3d &_xDirection, const ACG::Vec3d &)
Set plane.
Definition: PlaneType.cc:56
bool scenegraphPick(ACG::SceneGraph::PickTarget _pickTarget, const QPoint &_mousePos, unsigned int &_nodeIdx, unsigned int &_targetIdx, ACG::Vec3d *_hitPointPtr=0)
Execute picking operation on scenegraph.
Vec3d project(const Vec3d &_point) const
project point in world coordinates to window coordinates
Definition: GLState.cc:638
int viewport_height() const
get viewport height
Definition: GLState.hh:827