GLC_lib  2.5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
glc_axis.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 2.1 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_axis.h"
25 #include "../glc_factory.h"
26 #include "../maths/glc_line3d.h"
27 #include "../maths/glc_geomtools.h"
28 #include "../geometry/glc_cylinder.h"
29 #include "../geometry/glc_cone.h"
30 #include "glc_pullmanipulator.h"
31 
32 GLC_Axis::GLC_Axis(const GLC_Point3d& center, GLC_3DWidgetManagerHandle* pWidgetManagerHandle)
33 : GLC_3DWidget(pWidgetManagerHandle)
34 , m_Center(center)
35 , m_ScaleFactor(1.0)
36 , m_CurrentManipulator(NoneManipulator)
37 , m_pCurrentManipulator(NULL)
38 , m_AxisLength(1.0)
39 , m_AxisRadiusRatio(0.03)
40 {
41 
42 }
43 
45 : GLC_3DWidget(axis)
46 , m_Center(axis.m_Center)
47 , m_ScaleFactor(axis.m_ScaleFactor)
48 , m_CurrentManipulator(axis.m_CurrentManipulator)
49 , m_pCurrentManipulator(NULL)
50 , m_AxisLength(axis.m_AxisLength)
51 , m_AxisRadiusRatio(axis.m_AxisRadiusRatio)
52 {
53  if (NULL != axis.m_pCurrentManipulator)
54  {
56  }
57 }
58 
60 {
61  delete m_pCurrentManipulator;
62 }
63 
65 {
67 
68  m_Center= axis.m_Center;
69  delete m_pCurrentManipulator;
70  if (NULL != axis.m_pCurrentManipulator)
71  {
73  }
74 
75  return *this;
76 }
77 
79 {
80  const double viewTangent= GLC_3DWidget::widgetManagerHandle()->viewportTangent();
81  const GLC_Point3d eye(GLC_3DWidget::widgetManagerHandle()->cameraHandle()->eye());
82  const double distanceToNormal= (m_Center - eye).length();
83  const double viewWidth= distanceToNormal * viewTangent;
84 
85  m_ScaleFactor= viewWidth * 0.1;
87 }
88 
89 void GLC_Axis::setAxisLength(double length)
90 {
91  m_AxisLength= length;
92  if (!GLC_3DWidget::isEmpty())
93  {
96  }
97 }
98 
99 void GLC_Axis::setCenter(const GLC_Point3d& newCenter)
100 {
101  m_Center= newCenter;
103 }
104 
106 {
107  //qDebug() << "GLC_Axis::select";
108  Q_ASSERT(NULL == m_pCurrentManipulator);
110 
111  const int selectedInstanceIndex= GLC_3DWidget::indexOfIntsanceId(id);
114  if (selectedInstanceIndex < 2)
115  {
119  }
120  else if (selectedInstanceIndex < 4)
121  {
125  }
126  else
127  {
128  Q_ASSERT((selectedInstanceIndex < 6) && (selectedInstanceIndex >= 4));
132  }
133 
135 
136  updateWidgetRep();
137 
138  return glc::BlockedEvent;
139 }
140 
141 glc::WidgetEventFlag GLC_Axis::mousePressed(const GLC_Point3d& pos, Qt::MouseButton button, GLC_uint id)
142 {
143  //qDebug() << "GLC_Axis::mousePressed";
145  if (button == Qt::LeftButton)
146  {
147  returnFlag= select(pos, id);
148  }
149 
150  return returnFlag;
151 }
152 
154 {
155  //qDebug() << "GLC_Axis::mouseReleased";
157  if (button == Qt::LeftButton)
158  {
159 
160  // get selected instance index
161 
163  {
165  }
167  {
169  }
171  {
173  }
174 
176  delete m_pCurrentManipulator;
177  m_pCurrentManipulator= NULL;
178 
179  returnFlag= glc::BlockedEvent;
180  }
181  return returnFlag;
182 }
183 
185 {
186  //qDebug() << "GLC_Axis::unselect";
187  delete m_pCurrentManipulator;
188  m_pCurrentManipulator= NULL;
189 
191 
192  return glc::AcceptEvent;
193 }
194 
195 glc::WidgetEventFlag GLC_Axis::mouseMove(const GLC_Point3d& pos, Qt::MouseButtons button, GLC_uint)
196 {
198  if (button & Qt::LeftButton)
199  {
200  if (NULL != m_pCurrentManipulator)
201  {
203  m_Center= moveMatrix * m_Center;
204  // Update the instance
205  for (int i= 0; i < 6; ++i)
206  {
208  }
209 
210  // Plane throw intersection and plane normal and camera up vector
212 
213  emit asChanged();
214  returnFlag= glc::AcceptEvent;
215  }
216  }
217 
218  return returnFlag;
219 }
221 {
222  Q_ASSERT(GLC_3DWidget::isEmpty());
223  const double axisRadius= m_AxisLength * m_AxisRadiusRatio;
224  const double arrowLength= m_AxisLength * 0.3;
225  const double arrowFactor= 2.5;
226 
227  { // Create X axis
228  // The X axis material
229  GLC_Material* pMaterial= new GLC_Material(Qt::red);
230 
232  axisInstance.representation().geomAt(0)->replaceMasterMaterial(pMaterial);
233  GLC_3DViewInstance arrowInstance= GLC_Factory::instance()->createCone(arrowFactor * axisRadius, arrowLength);
234  arrowInstance.representation().geomAt(0)->replaceMasterMaterial(pMaterial);
235  arrowInstance.translate(0.0, 0.0, m_AxisLength);
236  // Rotate the axis
237  GLC_Matrix4x4 rotation(glc::Y_AXIS, glc::PI / 2);
238  axisInstance.multMatrix(rotation);
239  arrowInstance.multMatrix(rotation);
240 
241  GLC_3DWidget::add3DViewInstance(axisInstance);
242  GLC_3DWidget::add3DViewInstance(arrowInstance);
243  }
244  { // Create Y axis
245  // The Y axis material
246  GLC_Material* pMaterial= new GLC_Material(Qt::green);
247 
249  axisInstance.representation().geomAt(0)->replaceMasterMaterial(pMaterial);
250  GLC_3DViewInstance arrowInstance= GLC_Factory::instance()->createCone(arrowFactor * axisRadius, arrowLength);
251  arrowInstance.representation().geomAt(0)->replaceMasterMaterial(pMaterial);
252  arrowInstance.translate(0.0, 0.0, m_AxisLength);
253  // Rotate the axis
254  GLC_Matrix4x4 rotation(glc::X_AXIS, - glc::PI / 2);
255  axisInstance.multMatrix(rotation);
256  arrowInstance.multMatrix(rotation);
257 
258  GLC_3DWidget::add3DViewInstance(axisInstance);
259  GLC_3DWidget::add3DViewInstance(arrowInstance);
260  }
261  { // Create Z axis
262  // The Z axis material
263  GLC_Material* pMaterial= new GLC_Material(Qt::blue);
264 
266  axisInstance.representation().geomAt(0)->replaceMasterMaterial(pMaterial);
267  GLC_3DViewInstance arrowInstance= GLC_Factory::instance()->createCone(arrowFactor * axisRadius, arrowLength);
268  arrowInstance.representation().geomAt(0)->replaceMasterMaterial(pMaterial);
269  arrowInstance.translate(0.0, 0.0, m_AxisLength);
270 
271  GLC_3DWidget::add3DViewInstance(axisInstance);
272  GLC_3DWidget::add3DViewInstance(arrowInstance);
273  }
274 }
275 
277 {
278 
279 }
280 
282 {
283 
284  const GLC_Matrix4x4 translationMatrix(pos);
285  GLC_Matrix4x4 scaleMatrix;
287  for (int i= 0; i < 6; ++i)
288  {
289  GLC_Matrix4x4 rotationMatrix;
290  GLC_Matrix4x4 intTranslation;
291  if (i < 2)
292  {
293  rotationMatrix.setMatRot(glc::Y_AXIS, glc::PI / 2);
294  if (i == 1)
295  {
296  intTranslation.setMatTranslate(0.0, 0.0, m_AxisLength);
297  }
298  }
299  else if (i < 4)
300  {
301  rotationMatrix.setMatRot(glc::X_AXIS, - glc::PI / 2);
302  if (i == 3)
303  {
304  intTranslation.setMatTranslate(0.0, 0.0, m_AxisLength);
305  }
306 
307  }
308  else if (i == 5)
309  {
310  intTranslation.setMatTranslate(0.0, 0.0, m_AxisLength);
311  }
312 
313  GLC_3DWidget::instanceHandle(i)->setMatrix(translationMatrix * scaleMatrix * rotationMatrix * intTranslation);
314  }
315 }

©2005-2013 Laurent Ribon