Developer Documentation
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 #include "QtPlaneSelect.hh"
43 
44 #include <ACG/Scenegraph/GlutPrimitiveNode.hh>
46 #define PLUGINFUNCTIONS_C
48 
49 #include <ACG/QtWidgets/QtColorTranslator.hh>
50 
51 /*******************************************************************************
52  Initialization and de initialization
53 *******************************************************************************/
54 QtPlaneSelect::QtPlaneSelect( ACG::GLState& glState )
55  : glState( glState ),
56  nodeIdx_(0),
57  targetIdx_(0),
58  isDragging_( false ),
59  planeNode_(0)
60 {
61 
62 }
63 
64 QtPlaneSelect::~QtPlaneSelect( )
65 {
66 }
67 
68 
69 /*******************************************************************************
70  Implementation of public slots
71 *******************************************************************************/
72 
73 void QtPlaneSelect::slotKeyReleaseEvent(QKeyEvent* event)
74 {
75  if (event->key() == Qt::Key_Escape){
76  if( planeNode_ )
77  {
78  planeNode_->delete_subtree( );
79  planeNode_ = 0;
80  }
81 
82  emit updateViewProxy( );
83 
84  // Trigger the event
85  isDragging_ = false;
86  }
87 }
88 
89 void QtPlaneSelect::slotMouseEvent(QMouseEvent* event)
90 {
91 // unsigned int width = glState.viewport_width();
92  unsigned int height = glState.viewport_height();
93 
94  //cancel on rightclick
95  if (event->button() == Qt::RightButton){
96  if( planeNode_ )
97  {
98  planeNode_->delete_subtree( );
99  planeNode_ = 0;
100  }
101 
102  emit updateViewProxy( );
103 
104  // Trigger the event
105  isDragging_ = false;
106 
107  return;
108  }
109 
110 
111  switch( event->type() )
112  {
113  case QEvent::MouseButtonPress:
114  {
115 
116  // Only react on the left button and ignore the others
117  if ( event->button() != Qt::LeftButton )
118  return;
119 
120 
121  size_t node_idx, target_idx;
122 
124  event->pos(),
125  node_idx,
126  target_idx,
127  &sourcePoint3D))
128  {
129 
130  // Shift toward viewer
131  //sourcePoint3D = sourcePoint3D + PluginFunctions::viewingDirection();
132 
133  isDragging_ = true;
134 
135  if ( planeNode_ == 0 ) {
136  planeNode_ = new PlaneNode(plane_,PluginFunctions::getRootNode(),"PolyLine generation Plane" );
137  }
138 
140  sourcePoint2D = ACG::Vec3d(event->pos().x(), height - event->pos().y() - 1.0, 0.0);
141 
142  setPlaneAndSize(sourcePoint3D, sourcePoint2D);
143 
144  planeNode_->show();
145  emit nodeVisChangedProxy(planeNode_->id());
146 
147  nodeIdx_ = node_idx;
148  targetIdx_ = target_idx;
149 
150 
151  emit updateViewProxy( );
152  }
153  }break;
154  case QEvent::MouseMove:
155  {
156  if( isDragging_ )
157  {
158  setPlaneAndSize(sourcePoint3D,ACG::Vec3d(event->pos().x(), height-event->pos().y()-1.0, 0.0));
159 
160  emit updateViewProxy( );
161  }
162  } break;
163 
164  case QEvent::MouseButtonRelease:
165  {
166  if( isDragging_ )
167  {
168  if( planeNode_ )
169  {
170  planeNode_->delete_subtree( );
171  planeNode_ = NULL;
172  }
173 
174  emit updateViewProxy( );
175 
176  emit( signalTriggerCut( ) );
177  // Trigger the event
178  isDragging_ = false;
179  }
180  } break;
181 
182  default:
183  break;
184  }
185 
186 
187 }
188 
189 
190 void QtPlaneSelect::setPlaneAndSize(const ACG::Vec3d& _sourcePoint3D,const ACG::Vec3d& _target2D)
191 {
192 
193  ACG::Vec3d source2D = glState.project( _sourcePoint3D );
194  source2D[2] = 0.;
195 
196  const ACG::Vec3d diff = source2D - _target2D;
197 
198  ACG::Vec3d ortho{0., 0., 0.};
199  if (clamp_)
200  {
201  constexpr auto clamp_angle = M_PI / 12.;
202  const auto newTarget = _target2D - source2D;
203  const auto angle = std::atan2(newTarget[1], newTarget[0]);
204  const auto remainder = std::fmod(angle, clamp_angle);
205 
207  double corrected_angle = .0;
208  if (remainder < clamp_angle / 2.)
209  corrected_angle = angle - remainder;
210  else
211  corrected_angle = angle + (clamp_angle - remainder);
212 
213  const auto radius = diff.length();
214  const auto newTarget2D = ACG::Vec3d{std::round(radius * std::cos(corrected_angle)), std::round(radius * std::sin(corrected_angle)), 0.} + source2D;
215  const auto newDiff = source2D - newTarget2D;
216  ortho = ACG::Vec3d{-newDiff[1], newDiff[0], 0.};
217  }
218  else
219  //diff.normalize( ); <- this is bad
220  ortho = ACG::Vec3d{-diff[1], diff[0], 0.};
221 
223  const auto left = glState.unproject(source2D + ortho * 10. + ACG::Vec3d{0., 0., 0.});
224  const auto right = glState.unproject(source2D - ortho * 10. + ACG::Vec3d{0., 0., 0.});
225 
226  const auto leftvec = (left - sourcePoint3D).normalized();
227  const auto rightvec = (right - sourcePoint3D).normalized();
228 
230  normal_ = cross(rightvec, leftvec);
231  normal_.normalize();
232 
233  // std::cout << "computed normal: " << normal_ << std::endl;
234 
236  const auto sourcePoint3Df(sourcePoint3D);
237  const auto normald(normal_);
238  plane_.setPlane(sourcePoint3Df,normald);
240  planeNode_->update();
241 }
242 
243 
244 
245 
bool scenegraphPick(ACG::SceneGraph::PickTarget _pickTarget, const QPoint &_mousePos, size_t &_nodeIdx, size_t &_targetIdx, ACG::Vec3d *_hitPointPtr=0)
Execute picking operation on scenegraph.
picks faces (should be implemented for all nodes)
Definition: PickTarget.hh:78
ACG::SceneGraph::BaseNode * getRootNode()
Get the root node for data objects.
double sceneRadius()
Returns the current scene radius from the active examiner widget.
void setPlaneAndSize(const ACG::Vec3d &_sourcePoint3D, const ACG::Vec3d &_target2D)
void slotMouseEvent(QMouseEvent *_event)
auto length() const -> decltype(std::declval< VectorT< S, DIM >>().norm())
compute squared euclidean norm
Definition: Vector11T.hh:442
VectorT< double, 3 > Vec3d
Definition: VectorT.hh:121