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 
33 #include "TYPolyLineEditor.h"
34 
35 #define TR(id) OLocalizator::getString("TYPolyLineEditor", (id))
36 
38 {
39  _shiftOn = false;
40  _active = false;
41  _dispDist = false;
42 
43  // Taille des points differente selon le type de modeler
44  if (QString(pModeler->metaObject()->className()).compare("TYSiteModelerFrame") == 0)
45  {
46  _pointSize = 1.0f;
47  }
48  else if (QString(pModeler->metaObject()->className()).compare("TYBatimentModelerFrame") == 0)
49  {
50  _pointSize = 0.2f;
51  }
52  else if (QString(pModeler->metaObject()->className()).compare("TYMachineModelerFrame") == 0)
53  {
54  _pointSize = 0.1f;
55  }
56  else
57  {
58  _pointSize = 0.5f;
59  }
60 
61  _pOGLLineElement = NULL;
62 }
63 
65 {
66  cancel();
67 
68  if (_pOGLLineElement)
69  {
71  delete _pOGLLineElement;
72  _pOGLLineElement = NULL;
73  }
74 
75  if (_OGLSphereElementList.count() > 0)
76  {
77  for (QList<OGLSphereElement*>::iterator ite = _OGLSphereElementList.begin();
78  ite != _OGLSphereElementList.end(); ite++)
79  {
81  delete *ite;
82  }
83  _OGLSphereElementList.clear();
84  }
85 
86  if (_OGLLineElementList.count() > 0)
87  {
88  for (QList<OGLLineElement*>::iterator ite = _OGLLineElementList.begin();
89  ite != _OGLLineElementList.end(); ite++)
90  {
92  delete *ite;
93  }
94  _OGLLineElementList.clear();
95  }
96 }
97 
99 {
100  _savedPoints.clear();
101  if (_OGLSphereElementList.count() > 0)
102  {
103  for (QList<OGLSphereElement*>::iterator ite = _OGLSphereElementList.begin();
104  ite != _OGLSphereElementList.end(); ite++)
105  {
107  delete *ite;
108  }
109  _OGLSphereElementList.clear();
110  }
111 
112  if (_OGLLineElementList.count() > 0)
113  {
114  for (QList<OGLLineElement*>::iterator ite = _OGLLineElementList.begin();
115  ite != _OGLLineElementList.end(); ite++)
116  {
118  delete *ite;
119  }
120  _OGLLineElementList.clear();
121  }
122 
123  updateMovingLine(0, 0);
124  showText(false);
125 
126  _pInteractor->update();
127 }
128 
130 {
131  init();
132 }
133 
135 {
136  _savedPoints.clear();
137 }
138 
140 {
141  switch (key)
142  {
143  case Qt::Key_Return:
144  case Qt::Key_Enter:
145  if (_active)
146  {
147  // Commented until we could understand what it does
148  // int nbpoints = static_cast<int>(_savedPoints.size());
149  //
150  // //if (nbpoints >= 3)
151  //{
152  // OVector3D normale;
153  // OPoint3D* pts = new OPoint3D[nbpoints];
154 
155  // for (unsigned int i = 0; i < nbpoints; i++)
156  // {
157  // pts[i] = _savedPoints[i];
158  // }
159 
160  // OGeometrie::computeNormal(pts, nbpoints, normale);
161 
162  // OVector3D viewnormale;
163  // OGLCamera* camera = _pInteractor->getActiveCamera();
164  // if (!camera->fromTo.sameDirection(NxVec3(normale._x, normale._y, normale._z)))
165  // {
166  // _savedPoints.clear();
167 
168  // for (long i = (nbpoints - 1); i >= 0; i--)
169  // {
170  // _savedPoints.push_back(pts[i]);
171  // }
172  // }
173 
174  // delete [] pts;
175 
176  //}
177 
178  // Set all z values to modeler default Z coord
179  for (size_t i = 0; i < _savedPoints.size(); i++)
180  {
182  }
183 
184  emit(endedSavingPoints());
185 
186  init();
187 
189  }
190  break;
191  case Qt::Key_Shift:
192  _shiftOn = true;
193  break;
194  case Qt::Key_Escape:
195  if (_active)
196  {
197  cancel();
198  }
199  break;
200  case Qt::Key_Delete:
201  if (_active)
202  {
203  removeLastPoint();
204  }
205  break;
206  }
207 }
208 
210 {
211  if (key == Qt::Key_Shift)
212  {
213  _shiftOn = false;
214  }
215 }
216 
218 {
219  if (view == TYModelerFrame::TopView)
220  {
221  _active = true;
222  }
223  else
224  {
225  _active = false;
226 
227  showText(false);
228  _pInteractor->update();
229  }
230 
231  // On relaie le signal au CameraEditor
233 }
234 
235 void TYPolyLineEditor::slotMouseMoved(int x, int y, Qt::MouseButtons button, Qt::KeyboardModifiers state)
236 {
237  updateMovingLine(x, y);
238 }
239 
240 void TYPolyLineEditor::slotMouseReleased(int x, int y, Qt::MouseButton button, Qt::KeyboardModifiers state)
241 {
242  if (_active && !_shiftOn)
243  {
244  if (button == Qt::LeftButton)
245  {
246  addPoint(x, y);
247  }
248  else if ((button == Qt::RightButton) && (_savedPoints.size() > 0))
249  {
250  removeLastPoint();
251  }
252  }
253 }
254 
255 void TYPolyLineEditor::addPoint(int x, int y)
256 {
257  QVector3D pt = _pInteractor->getViewport().displayToWorld(
258  QVector3D(x, _pInteractor->getViewport().height() - y, 0.1));
259  TYPoint pt2(pt.x(), -pt.z(), pt.y());
260  validatePoint(pt2);
261 
262  // Changement de repere Disp->World
263  if (_savedPoints.size() >= 1)
264  {
267  OColor oLineColor;
268  oLineColor.r = 1.0;
269  oLineColor.g = 0.0;
270  oLineColor.b = 0.0;
271  _pOGLLineElement->setColor(oLineColor);
272  _pOGLLineElement->setIs3D(true);
273  const OPoint3D lastPosition = _OGLSphereElementList.last()->getCenter();
274  _pOGLLineElement->setPoint1(QVector3D(lastPosition._x, lastPosition._y, lastPosition._z));
275  _pOGLLineElement->setPoint2(QVector3D(pt2._x, pt2._z, -pt2._y));
278  }
279 
280  // Ajout du nouveau point a la collection
281  _savedPoints.push_back(pt2);
282 
283  OGLSphereElement* _pOGLSphereElement = new OGLSphereElement();
284  _pOGLSphereElement->setCenter(OPoint3D(pt2._x, pt2._z, -pt2._y));
285  _pOGLSphereElement->setRadius(_pointSize);
286  _pOGLSphereElement->setIs3D(true);
287  _pOGLSphereElement->setColor(OColor(1.0, 0.0, 0.0));
288  _pOGLSphereElement->setVisibility(true);
289  _pInteractor->getRenderer()->addOGLElement(_pOGLSphereElement);
290  _OGLSphereElementList.append(_pOGLSphereElement);
291 
292  _pInteractor->update();
293 }
294 
296 {
297  bool res = true;
298 
299  // Suppression du point precedent
300  _savedPoints.pop_back();
301 
302  if (!_savedPoints.size())
303  {
304  res = false;
305  }
306 
307  if (_OGLSphereElementList.count() == 0)
308  {
309  return false;
310  }
311  else
312  {
314  delete _OGLSphereElementList.last();
315  _OGLSphereElementList.removeLast();
316 
317  if (_OGLLineElementList.count() == 0)
318  {
319  return false;
320  }
321  else
322  {
324  delete _OGLLineElementList.last();
325  _OGLLineElementList.removeLast();
326  if (!updateMovingLine(0, 0))
327  {
328  _pInteractor->update();
329  }
330  }
331  }
332 
333  return res;
334 }
335 
337 {
338  if (!_active)
339  {
340  return false;
341  }
342  if (_OGLSphereElementList.count() == 0)
343  {
344  if (_pOGLLineElement)
345  {
347  }
348 
349  showText(false);
350 
351  return false;
352  }
353 
354  QPoint pos = _pInteractor->toViewport(_pInteractor->mapFromGlobal(QCursor::pos()));
355 
356  // Changement de repere Disp->World
357  // On recupere le pt precedent
358  TYPoint pt1 = _savedPoints.back();
359 
360  QVector3D pt;
361  if ((x == 0) && (y == 0))
362  {
364  QVector3D(pos.x(), _pInteractor->getViewport().height() - pos.y(), 0.1));
365  }
366  else
367  {
369  QVector3D(x, _pInteractor->getViewport().height() - y, 0.1));
370  }
371  TYPoint pt2(pt.x(), -pt.z(), pt.y());
372  validatePoint(pt2);
373 
374  // Init des objets graphiques
375  if (!_pOGLLineElement)
376  {
379  _pOGLLineElement->setIs3D(true);
380  OColor oLineColor;
381  oLineColor.r = 1.0;
382  oLineColor.g = 0.0;
383  oLineColor.b = 0.0;
384  _pOGLLineElement->setColor(oLineColor);
385 
387  }
388 
389  // On trace une ligne avec les 2 pts
391  _pOGLLineElement->setPoint1(QVector3D(pt1._x, pt1._z, -pt1._y));
392  _pOGLLineElement->setPoint2(QVector3D(pt2._x, pt2._z, -pt2._y));
393 
394  if (_dispDist)
395  {
396  // Affichage de la distance entre les 2 points
397  double dist = OVector3D(pt1, pt2).norme();
398  QString msg = QString(TR("id_output_dist")).arg(dist, 0, 'f', 2);
399 
400  double textPos[4];
402  (_pOGLLineElement->getPoint2()[0] - _pOGLLineElement->getPoint1()[0]) / 2.0),
403  (_pOGLLineElement->getPoint2()[1] +
404  (_pOGLLineElement->getPoint1()[1] - _pOGLLineElement->getPoint2()[1]) / 2.0),
405  (_pOGLLineElement->getPoint2()[2] +
406  (_pOGLLineElement->getPoint1()[2] - _pOGLLineElement->getPoint2()[2]) / 2.0),
407  textPos);
408  updateText(msg, (int)textPos[0], (int)textPos[1]);
409  }
410 
411  // Redraw
412  _pInteractor->update();
413 
414  return true;
415 }
416 
418 {
419  // Si la grille magnetique est activee
421  {
422  snapToGrid(pt._x, pt._y, pt._z, _gridMagnStep);
423  }
424 }
All base classes related to 3D manipulation.
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:31
float b
Definition: color.h:33
float r
Definition: color.h:33
float g
Definition: color.h:33
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
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...
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.
TYCameraEditor * _pCameraEditor
Le caméra editor associé Ã&#160; cet éditor.
void showText(bool show=true)
Affiche ou pas le texte informatif sur la vue 3D.
TYRenderWindowInteractor * _pInteractor
La vue graphique associee a cet editor.
virtual void slotViewTypeChanged(int view)
static void setIsSavedOk(const bool &toSave)
Definition: TYElement.h:914
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...
Super-class for editors performing object construction.
void addOGLElement(OGLElement *pOGLElement)
void removeOGLElement(OGLElement *pOGLElement)
void endedSavingPoints()
virtual void cancel()
QList< OGLLineElement * > _OGLLineElementList
Lignes dans la vue 3D.
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 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()