GLC_lib  2.5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
glc_cuttingplane.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 
3  This file is part of the GLC-lib library.
4  Copyright (C) 2005-2008 Laurent Ribon (laumaya@users.sourceforge.net)
5  http://glc-lib.sourceforge.net
6 
7  GLC-lib is free software; you can redistribute it and/or modify
8  it under the terms of the GNU Lesser General Public License as published by
9  the Free Software Foundation; either version 3 of the License, or
10  (at your option) any later version.
11 
12  GLC-lib is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public License
18  along with GLC-lib; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 
21  *****************************************************************************/
23 
24 #include "glc_cuttingplane.h"
25 #include "../glc_factory.h"
26 #include "../maths/glc_line3d.h"
27 #include "../maths/glc_geomtools.h"
28 #include "../geometry/glc_arrow.h"
29 #include "../geometry/glc_disc.h"
30 #include "glc_pullmanipulator.h"
32 
33 GLC_CuttingPlane::GLC_CuttingPlane(const GLC_Point3d& center, const GLC_Vector3d& normal, double l1, double l2, GLC_3DWidgetManagerHandle* pWidgetManagerHandle)
34 : GLC_3DWidget(pWidgetManagerHandle)
35 , m_Center(center)
36 , m_Normal(normal)
37 , m_CompMatrix()
38 , m_L1(l1)
39 , m_L2(l2)
40 , m_Color(Qt::darkGreen)
41 , m_Opacity(0.3)
42 , m_ManipulatorOffsetFactor(1.0)
43 , m_ScaleFactor(1.0)
44 , m_SelectionIndex(-1)
45 , m_CurrentManipulator(TranslationManipulator)
46 , m_pCurrentManipulator(NULL)
47 , m_CurrentNavigatorPosition()
48 {
49  if (NULL != pWidgetManagerHandle)
50  {
52  }
53 
55 }
56 
58 : GLC_3DWidget(cuttingPlane)
59 , m_Center(cuttingPlane.m_Center)
60 , m_Normal(cuttingPlane.m_Normal)
61 , m_CompMatrix(cuttingPlane.m_CompMatrix)
62 , m_L1(cuttingPlane.m_L1)
63 , m_L2(cuttingPlane.m_L2)
64 , m_Color(cuttingPlane.m_Color)
65 , m_Opacity(cuttingPlane.m_Opacity)
66 , m_ManipulatorOffsetFactor(cuttingPlane.m_ManipulatorOffsetFactor)
67 , m_ScaleFactor(cuttingPlane.m_ScaleFactor)
68 , m_SelectionIndex(cuttingPlane.m_SelectionIndex)
69 , m_CurrentManipulator(cuttingPlane.m_CurrentManipulator)
70 , m_pCurrentManipulator(NULL)
71 , m_CurrentNavigatorPosition(cuttingPlane.m_CurrentNavigatorPosition)
72 {
73  if (NULL != cuttingPlane.m_pCurrentManipulator)
74  {
76  }
77 }
78 
80 {
81  delete m_pCurrentManipulator;
82 }
83 
85 {
86  GLC_3DWidget::operator=(cuttingPlane);
87 
88  m_Center= cuttingPlane.m_Center;
89  m_Normal= cuttingPlane.m_Normal;
90  m_CompMatrix= cuttingPlane.m_CompMatrix;
91  m_L1= cuttingPlane.m_L1;
92  m_L2= cuttingPlane.m_L2;
93  m_Color= cuttingPlane.m_Color;
94  m_Opacity= cuttingPlane.m_Opacity;
96  delete m_pCurrentManipulator;
97  if (NULL != cuttingPlane.m_pCurrentManipulator)
98  {
100  }
101 
102  return *this;
103 }
104 
105 void GLC_CuttingPlane::updateLength(double l1, double l2)
106 {
107  m_L1= l1;
108  m_L2= l2;
109 
111  {
114  }
115 }
116 
118 {
119  const double viewTangent= GLC_3DWidget::widgetManagerHandle()->viewportTangent();
120  const GLC_Point3d eye(GLC_3DWidget::widgetManagerHandle()->cameraHandle()->eye());
121  const double distanceToNormal= (m_CurrentNavigatorPosition - eye).length();
122  const double viewWidth= distanceToNormal * viewTangent;
123 
124  m_ScaleFactor= viewWidth * 0.1;
126 
128 }
129 
131 {
132  Q_ASSERT(NULL == m_pCurrentManipulator);
134 
138 
141 
143 
144  updateWidgetRep();
145 
146  return glc::BlockedEvent;
147 }
148 
150 {
152  if (button == Qt::LeftButton)
153  {
154  const int selectedInstanceIndex= GLC_3DWidget::indexOfIntsanceId(id);
155  if (selectedInstanceIndex > 0)
156  {
157  m_SelectionIndex= selectedInstanceIndex;
159  {
160  delete m_pCurrentManipulator;
161  m_pCurrentManipulator= rotationNavigator(selectedInstanceIndex);
162  }
164  }
165  else
166  {
167  if (NULL != m_pCurrentManipulator)
168  {
170  {
171  delete m_pCurrentManipulator;
172  m_pCurrentManipulator= NULL;
173  }
174  else
175  {
177  }
178 
179  }
181  updateWidgetRep();
182  }
183 
184  returnFlag= glc::BlockedEvent;
185  }
186 
187  return returnFlag;
188 }
189 
191 {
193  if ((button == Qt::LeftButton) && (m_SelectionIndex != -1))
194  {
195 
196  // get selected instance index
197 
199  {
204 
205  returnFlag= glc::BlockedEvent;
207  delete m_pCurrentManipulator;
208  m_pCurrentManipulator= NULL;
209 
211  }
213  {
218 
219  returnFlag= glc::BlockedEvent;
221 
222  delete m_pCurrentManipulator;
223 
227 
229  }
230  m_SelectionIndex= -1;
231  }
232  return returnFlag;
233 }
234 
236 {
237  resetViewState();
238  return glc::AcceptEvent;
239 }
240 
242 {
244  if (button & Qt::LeftButton)
245  {
246  if (NULL != m_pCurrentManipulator)
247  {
248  if (m_SelectionIndex != -1)
249  {
251  m_SelectionIndex= -1;
252  }
254 
255  // Update plane normal
257  {
258  m_Normal= moveMatrix.rotationMatrix() * m_Normal;
259  }
260  m_CompMatrix= moveMatrix * m_CompMatrix;
261  m_Center= moveMatrix * m_Center;
263 
264  // Update the instance
265  for (int i= 0; i < 5; ++i)
266  {
268  }
269 
270  // Plane throw intersection and plane normal and camera up vector
272 
273  emit asChanged();
274  returnFlag= glc::AcceptEvent;
275  }
276  }
277 
278  return returnFlag;
279 }
280 
282 {
283  Q_ASSERT(GLC_3DWidget::isEmpty());
284  // The cutting plane material
285  GLC_Material* pMaterial= new GLC_Material(m_Color);
286  pMaterial->setOpacity(m_Opacity);
287 
288  // Cutting plane 3Dview instance
289  GLC_3DViewInstance cuttingPlaneInstance= GLC_Factory::instance()->createCuttingPlane(m_Center, m_Normal, m_L1, m_L2, pMaterial);
290  GLC_3DWidget::add3DViewInstance(cuttingPlaneInstance);
291 
292  // Normal arrow geometry
293  GLC_Arrow* pArrow= new GLC_Arrow(GLC_Point3d(), -glc::Z_AXIS, GLC_3DWidget::widgetManagerHandle()->cameraHandle()->forward().normalize());
294  pArrow->setLineWidth(4.5);
295  pArrow->setHeadLength(0.15);
296  QColor arrowColor(Qt::red);
297  arrowColor.setAlphaF(0.4);
298  pArrow->setWireColor(arrowColor);
299 
300  //Base arrow disc
301  pMaterial= new GLC_Material(Qt::red);
302  pMaterial->setOpacity(m_Opacity);
303  GLC_Disc* pDisc= new GLC_Disc(0.3);
304  pDisc->replaceMasterMaterial(pMaterial);
305 
306  // Normal arrow + base instance
307  GLC_3DRep normalLine(pArrow);
308  normalLine.addGeom(pDisc);
311 
312  // Rotation manipulator
313  const double initRadius= 1;
314  // Arrond X axis
315  pDisc= new GLC_Disc(initRadius);
316  pMaterial= new GLC_Material(Qt::red);
317  pMaterial->setOpacity(m_Opacity);
318  pDisc->replaceMasterMaterial(pMaterial);
319  pDisc->setAngle(glc::PI);
322  // Arround Y axis
323  pDisc= new GLC_Disc(initRadius);
324  pMaterial= new GLC_Material(Qt::green);
325  pMaterial->setOpacity(m_Opacity);
326  pDisc->replaceMasterMaterial(pMaterial);
327  pDisc->setAngle(glc::PI);
330  // Arround Z axis
331  pDisc= new GLC_Disc(initRadius);
332  pMaterial= new GLC_Material(Qt::blue);
333  pMaterial->setOpacity(m_Opacity);
334  pDisc->replaceMasterMaterial(pMaterial);
335  //pDisc->setAngle(glc::PI / 2.0);
338 }
339 
341 {
342  Q_ASSERT(m_SelectionIndex == -1);
343  for (int i= 0; i < 4; ++i)
344  {
346  }
347  m_pCurrentManipulator= NULL;
348 
350 }
351 
353 {
354  // Create the widget rotation matrix
355  const GLC_Matrix4x4 rotationMatrix(m_CompMatrix.rotationMatrix());
356 
357  const GLC_Matrix4x4 translationMatrix(pos);
358  const GLC_Matrix4x4 offsetMatrix(m_Normal * m_ManipulatorOffsetFactor);
359  GLC_Matrix4x4 scaleMatrix;
361  GLC_3DWidget::instanceHandle(1)->setMatrix(offsetMatrix * translationMatrix * rotationMatrix *scaleMatrix);
362 
363  // Rotation manipulator
364  QVector<GLC_Matrix4x4> rotations(3);
365  rotations[0].setMatRot(glc::Y_AXIS, glc::PI / 2.0); // X
366  rotations[0]= GLC_Matrix4x4(glc::X_AXIS, -glc::PI / 2.0) * rotations[0];
367  rotations[1].setMatRot(glc::X_AXIS, -glc::PI / 2.0); // Y
368  // Z
369  for (int i= 0; i < 3; ++i)
370  {
371  GLC_3DWidget::instanceHandle(2 + i)->setMatrix(offsetMatrix * translationMatrix * rotationMatrix * rotations.at(i) * scaleMatrix);
372  }
373 
374  GLC_Arrow* pArrow= dynamic_cast<GLC_Arrow*>(GLC_3DWidget::instanceHandle(1)->geomAt(0));
375  Q_ASSERT(NULL != pArrow);
376 
377  pArrow->setViewDir(rotationMatrix * GLC_3DWidget::widgetManagerHandle()->cameraHandle()->forward().normalize());
378 }
379 
381 {
382  index= index - 2;
383  Q_ASSERT((index > -1) && (index < 3));
384 
385  const GLC_Matrix4x4 rotationMatrix(m_CompMatrix.rotationMatrix());
386  GLC_Vector3d axis;
387  if (index == 0)
388  {
389  axis= rotationMatrix * glc::X_AXIS;
390  }
391  else if (index == 1)
392  {
393  axis= rotationMatrix * glc::Y_AXIS;
394  }
395  else
396  {
397  axis= rotationMatrix * glc::Z_AXIS;
398  }
400 
401  return pManipulator;
402 }
403 

©2005-2013 Laurent Ribon