Code_TYMPAN  4.4.0
Industrial site acoustic simulation
TYPolyLineEditor.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) <2012-2024> <EDF-DTG> <FRANCE>
3  * This file is part of Code_TYMPAN (R).
4  * Code_TYMPAN (R) is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  * Code_TYMPAN (R) is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  * See the GNU General Public License for more details.
12  * You should have received a copy of the GNU General Public License along
13  * with Code_TYMPAN (R). If not, see <https://www.gnu.org/licenses/>.
14  */
15 
21 #include <qcursor.h>
22 #include <QList>
23 #include <qvector3d.h>
24 
34 #include "TYPolyLineEditor.h"
35 
36 #define TR(id) OLocalizator::getString("TYPolyLineEditor", (id))
37 
39 {
40  _pCamEditor = new TYCameraEditor(pModeler);
41 
47 
48  _shiftOn = false;
49  _active = false;
50  _dispDist = false;
51 
52  // Taille des points differente selon le type de modeler
53  if (QString(pModeler->metaObject()->className()).compare("TYSiteModelerFrame") == 0)
54  {
55  _pointSize = 1.0f;
56  }
57  else if (QString(pModeler->metaObject()->className()).compare("TYBatimentModelerFrame") == 0)
58  {
59  _pointSize = 0.2f;
60  }
61  else if (QString(pModeler->metaObject()->className()).compare("TYMachineModelerFrame") == 0)
62  {
63  _pointSize = 0.1f;
64  }
65  else
66  {
67  _pointSize = 0.5f;
68  }
69 
70  _pOGLLineElement = NULL;
71 }
72 
74 {
75  cancel();
76 
77  delete _pCamEditor;
78 
79  if (_pOGLLineElement)
80  {
82  delete _pOGLLineElement;
83  _pOGLLineElement = NULL;
84  }
85 
86  if (_OGLSphereElementList.count() > 0)
87  {
88  for (QList<OGLSphereElement*>::iterator ite = _OGLSphereElementList.begin();
89  ite != _OGLSphereElementList.end(); ite++)
90  {
92  delete *ite;
93  }
94  _OGLSphereElementList.clear();
95  }
96 
97  if (_OGLLineElementList.count() > 0)
98  {
99  for (QList<OGLLineElement*>::iterator ite = _OGLLineElementList.begin();
100  ite != _OGLLineElementList.end(); ite++)
101  {
103  delete *ite;
104  }
105  _OGLLineElementList.clear();
106  }
107 }
108 
110 {
112 }
113 
115 {
118 }
119 
121 {
122  _savedPoints.clear();
123  if (_OGLSphereElementList.count() > 0)
124  {
125  for (QList<OGLSphereElement*>::iterator ite = _OGLSphereElementList.begin();
126  ite != _OGLSphereElementList.end(); ite++)
127  {
129  delete *ite;
130  }
131  _OGLSphereElementList.clear();
132  }
133 
134  if (_OGLLineElementList.count() > 0)
135  {
136  for (QList<OGLLineElement*>::iterator ite = _OGLLineElementList.begin();
137  ite != _OGLLineElementList.end(); ite++)
138  {
140  delete *ite;
141  }
142  _OGLLineElementList.clear();
143  }
144 
145  updateMovingLine(0, 0);
146  showText(false);
147 
148  _pInteractor->update();
149 }
150 
152 {
153  init();
154 }
155 
157 {
158  _savedPoints.clear();
159 }
160 
162 {
163  switch (key)
164  {
165  case Qt::Key_Return:
166  case Qt::Key_Enter:
167  if (_active)
168  {
169  // Commented until we could understand what it does
170  // int nbpoints = static_cast<int>(_savedPoints.size());
171  //
172  // //if (nbpoints >= 3)
173  //{
174  // OVector3D normale;
175  // OPoint3D* pts = new OPoint3D[nbpoints];
176 
177  // for (unsigned int i = 0; i < nbpoints; i++)
178  // {
179  // pts[i] = _savedPoints[i];
180  // }
181 
182  // OGeometrie::computeNormal(pts, nbpoints, normale);
183 
184  // OVector3D viewnormale;
185  // OGLCamera* camera = _pInteractor->getActiveCamera();
186  // if (!camera->fromTo.sameDirection(NxVec3(normale._x, normale._y, normale._z)))
187  // {
188  // _savedPoints.clear();
189 
190  // for (long i = (nbpoints - 1); i >= 0; i--)
191  // {
192  // _savedPoints.push_back(pts[i]);
193  // }
194  // }
195 
196  // delete [] pts;
197 
198  //}
199 
200  // Set all z values to modeler default Z coord
201  for (size_t i = 0; i < _savedPoints.size(); i++)
202  {
204  }
205 
206  emit(endedSavingPoints());
207 
208  init();
209 
211  }
212  break;
213  case Qt::Key_Shift:
215  {
216  _pCamEditor->connect();
217  }
218  _shiftOn = true;
219  break;
220  case Qt::Key_Escape:
221  if (_active)
222  {
223  cancel();
224  }
225  break;
226  case Qt::Key_Delete:
227  if (_active)
228  {
229  removeLastPoint();
230  }
231  break;
232  }
233 }
234 
236 {
237  if (key == Qt::Key_Shift)
238  {
240  {
242  }
243  _shiftOn = false;
244  }
245 }
246 
248 {
249  if (view == TYModelerFrame::TopView)
250  {
252  _active = true;
253  }
254  else
255  {
256  _pCamEditor->connect();
257  _active = false;
258 
259  showText(false);
260  _pInteractor->update();
261  }
262 
263  // On relaie le signal au CameraEditor
265 }
266 
267 void TYPolyLineEditor::slotMouseMoved(int x, int y, Qt::MouseButtons button, Qt::KeyboardModifiers state)
268 {
269  updateMovingLine(x, y);
270 }
271 
272 void TYPolyLineEditor::slotMouseReleased(int x, int y, Qt::MouseButton button, Qt::KeyboardModifiers state)
273 {
274  if (_active && !_shiftOn)
275  {
276  if (button == Qt::LeftButton)
277  {
278  addPoint(x, y);
279  }
280  else if ((button == Qt::RightButton) && (_savedPoints.size() > 0))
281  {
282  removeLastPoint();
283  }
284  }
285 }
286 
287 void TYPolyLineEditor::addPoint(int x, int y)
288 {
289  QVector3D pt = _pInteractor->getViewport().displayToWorld(
290  QVector3D(x, _pInteractor->getViewport().height() - y, 0.1));
291  TYPoint pt2(pt.x(), -pt.z(), pt.y());
292  validatePoint(pt2);
293 
294  // Changement de repere Disp->World
295  if (_savedPoints.size() >= 1)
296  {
299  OColor oLineColor;
300  oLineColor.r = 1.0;
301  oLineColor.g = 0.0;
302  oLineColor.b = 0.0;
303  _pOGLLineElement->setColor(oLineColor);
304  _pOGLLineElement->setIs3D(true);
305  const OPoint3D lastPosition = _OGLSphereElementList.last()->getCenter();
306  _pOGLLineElement->setPoint1(QVector3D(lastPosition._x, lastPosition._y, lastPosition._z));
307  _pOGLLineElement->setPoint2(QVector3D(pt2._x, pt2._z, -pt2._y));
310  }
311 
312  // Ajout du nouveau point a la collection
313  _savedPoints.push_back(pt2);
314 
315  OGLSphereElement* _pOGLSphereElement = new OGLSphereElement();
316  _pOGLSphereElement->setCenter(OPoint3D(pt2._x, pt2._z, -pt2._y));
317  _pOGLSphereElement->setRadius(_pointSize);
318  _pOGLSphereElement->setIs3D(true);
319  _pOGLSphereElement->setColor(OColor(1.0, 0.0, 0.0));
320  _pOGLSphereElement->setVisibility(true);
321  _pInteractor->getRenderer()->addOGLElement(_pOGLSphereElement);
322  _OGLSphereElementList.append(_pOGLSphereElement);
323 
324  _pInteractor->update();
325 }
326 
328 {
329  bool res = true;
330 
331  // Suppression du point precedent
332  _savedPoints.pop_back();
333 
334  if (!_savedPoints.size())
335  {
336  res = false;
337  }
338 
339  if (_OGLSphereElementList.count() == 0)
340  {
341  return false;
342  }
343  else
344  {
346  delete _OGLSphereElementList.last();
347  _OGLSphereElementList.removeLast();
348 
349  if (_OGLLineElementList.count() == 0)
350  {
351  return false;
352  }
353  else
354  {
356  delete _OGLLineElementList.last();
357  _OGLLineElementList.removeLast();
358  if (!updateMovingLine(0, 0))
359  {
360  _pInteractor->update();
361  }
362  }
363  }
364 
365  return res;
366 }
367 
369 {
370  if (!_active)
371  {
372  return false;
373  }
374  if (_OGLSphereElementList.count() == 0)
375  {
376  if (_pOGLLineElement)
377  {
379  }
380 
381  showText(false);
382 
383  return false;
384  }
385 
386  QPoint pos = _pInteractor->toViewport(_pInteractor->mapFromGlobal(QCursor::pos()));
387 
388  // Changement de repere Disp->World
389  // On recupere le pt precedent
390  TYPoint pt1 = _savedPoints.back();
391 
392  QVector3D pt;
393  if ((x == 0) && (y == 0))
394  {
396  QVector3D(pos.x(), _pInteractor->getViewport().height() - pos.y(), 0.1));
397  }
398  else
399  {
401  QVector3D(x, _pInteractor->getViewport().height() - y, 0.1));
402  }
403  TYPoint pt2(pt.x(), -pt.z(), pt.y());
404  validatePoint(pt2);
405 
406  // Init des objets graphiques
407  if (!_pOGLLineElement)
408  {
411  _pOGLLineElement->setIs3D(true);
412  OColor oLineColor;
413  oLineColor.r = 1.0;
414  oLineColor.g = 0.0;
415  oLineColor.b = 0.0;
416  _pOGLLineElement->setColor(oLineColor);
417 
419  }
420 
421  // On trace une ligne avec les 2 pts
423  _pOGLLineElement->setPoint1(QVector3D(pt1._x, pt1._z, -pt1._y));
424  _pOGLLineElement->setPoint2(QVector3D(pt2._x, pt2._z, -pt2._y));
425 
426  if (_dispDist)
427  {
428  // Affichage de la distance entre les 2 points
429  double dist = OVector3D(pt1, pt2).norme();
430  QString msg = QString(TR("id_output_dist")).arg(dist, 0, 'f', 2);
431 
432  double textPos[4];
434  (_pOGLLineElement->getPoint2()[0] - _pOGLLineElement->getPoint1()[0]) / 2.0),
435  (_pOGLLineElement->getPoint2()[1] +
436  (_pOGLLineElement->getPoint1()[1] - _pOGLLineElement->getPoint2()[1]) / 2.0),
437  (_pOGLLineElement->getPoint2()[2] +
438  (_pOGLLineElement->getPoint1()[2] - _pOGLLineElement->getPoint2()[2]) / 2.0),
439  textPos);
440  updateText(msg, (int)textPos[0], (int)textPos[1]);
441  }
442 
443  // Redraw
444  _pInteractor->update();
445 
446  return true;
447 }
448 
450 {
451  // Si la grille magnetique est activee
453  {
454  snapToGrid(pt._x, pt._y, pt._z, _gridMagnStep);
455  }
456 }
All base classes related to 3D manipulation.
gestion de l'edition de la camera (fichier header)
Classe generique pour une fenetre de modeleur (fichier header)
#define TR(id)
gestion de l'edition d'une polyligne (fichier header)
Le role de cette classe est limite a emettre des signaux pouvant etre utilise pour interagir sur le r...
Definition: color.h:36
float b
Definition: color.h:38
float r
Definition: color.h:38
float g
Definition: color.h:38
double _y
y coordinate of OCoord3D
Definition: 3d.h:283
double _z
z coordinate of OCoord3D
Definition: 3d.h:284
double _x
x coordinate of OCoord3D
Definition: 3d.h:282
void setVisibility(bool bVisible)
Definition: OGLElement.h:51
void setIs3D(bool bIs3D)
Definition: OGLElement.h:59
const QVector3D & getPoint1()
void setPoint2(const QVector3D &point2)
const QVector3D & getPoint2()
void setColor(const OColor &oColor)
void setPoint1(const QVector3D &point1)
void setColor(const OColor &oColor)
void setRadius(float radius)
void setCenter(const OPoint3D &center)
The 3D point class.
Definition: 3d.h:487
The 3D vector class.
Definition: 3d.h:298
double norme() const
Computes the length of this vector.
Definition: 3d.cpp:215
Classe abstraite pour la gestion de l'interaction entre la vue graphique (2D ou 3D) et le clavier et ...
double _gridMagnStep
Pas de la grille magnétique de positionnement.
void worldToDisplay(double x, double y, double z, double *displayPoint)
Methode utilitaire pour convertir un point en coordonnees globale en coordonnees ecran.
static void snapToGrid(float &x, float &y, float &z, float &gridMagnStep)
Methode utilitaire qui adapte les coordonnees d'un point pour que celui-ci soit aligne avec la grille...
virtual void disconnect()
Deconnecte cet editor a l'interactor associe.
void updateText(QString msg="", int posX=0, int posY=0, bool show=true)
Mets a jour le texte informatif sur la vue 3D.
TYModelerFrame * _pModeler
Le modeler associe a cet editor.
virtual void connect()
Connecte cet editor a l'interactor associe.
void showText(bool show=true)
Affiche ou pas le texte informatif sur la vue 3D.
TYRenderWindowInteractor * _pInteractor
La vue graphique associee a cet editor.
Gestion de l'edition en mode camera.
void setLeftButtonFunction2D(void(TYCameraEditor::*function)())
void setMiddleButtonFunction2D(void(TYCameraEditor::*function)())
void setRightButtonFunction2D(void(TYCameraEditor::*function)())
void setShiftRightButtonFunction2D(void(TYCameraEditor::*function)())
void setShiftLeftButtonFunction2D(void(TYCameraEditor::*function)())
virtual void slotViewTypeChanged(int view)
static void setIsSavedOk(const bool &toSave)
Definition: TYElement.h:905
Generic class for a modeler window.
bool getSnapGridActive()
virtual float getDefaultZCoord()
Returns default Z coordinate for editors This value will depend on the type of modeler and of the fix...
void addOGLElement(OGLElement *pOGLElement)
void removeOGLElement(OGLElement *pOGLElement)
virtual void connect()
void endedSavingPoints()
virtual void cancel()
QList< OGLLineElement * > _OGLLineElementList
Lignes dans la vue 3D.
TYCameraEditor * _pCamEditor
Un CameraEditor associe a cet editor.
void addPoint(int x, int y)
QList< OGLSphereElement * > _OGLSphereElementList
Table de points openGL.
bool updateMovingLine(int x, int y)
bool _dispDist
Indique si l'information de distance doit etre affichee ou non.
TYTabPoint _savedPoints
Tableau pour la sauvegarde des points du mode Save points.
void validatePoint(TYPoint &pt)
TYPolyLineEditor(TYModelerFrame *pModeler)
virtual void init()
virtual void disconnect()
virtual void slotMouseMoved(int x, int y, Qt::MouseButtons button, Qt::KeyboardModifiers state)
virtual void slotKeyPressed(int key)
float _pointSize
Taille pour la representation des points.
virtual void slotViewTypeChanged(int view)
bool _shiftOn
Indique si le bouton Shift est enfonce.
bool _active
Indique si cet editor est actif.
virtual void close()
virtual void slotKeyReleased(int key)
OGLLineElement * _pOGLLineElement
La ligne courante.
virtual void slotMouseReleased(int x, int y, Qt::MouseButton button, Qt::KeyboardModifiers state)
QVector3D displayToWorld(const QVector3D &display) const
const TYRenderViewport & getViewport() const
QPoint toViewport(QPoint point)
TYOpenGLRenderer * getRenderer()