Moka controlers
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros
controler-gmap-object-transformation.cc
Go to the documentation of this file.
1 /*
2  * lib-controler-gmap : Le contrôleur de 3-G-cartes, surcouche de lib-controler.
3  * Copyright (C) 2004, Moka Team, Université de Poitiers, Laboratoire SIC
4  * http://www.sic.sp2mi.univ-poitiers.fr/
5  * Copyright (C) 2009, Guillaume Damiand, CNRS, LIRIS,
6  * guillaume.damiand@liris.cnrs.fr, http://liris.cnrs.fr/
7  *
8  * This file is part of lib-controler-gmap
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this program. If not, see <http://www.gnu.org/licenses/>.
22  */
23 
24 //******************************************************************************
25 #include "controler-gmap.hh"
30 #include "parameter-selection.hh"
31 #include "view-precompile.hh"
32 #include "vertex.hh"
33 #include "vector.hh"
34 #include "g-map-vertex.hh"
35 #include "geometry.hh"
36 using namespace GMap3d;
37 //******************************************************************************
38 void CControlerGMap::setObjectTranslationVector(const CVertex & AVector)
39 {
40  if ( AVector!= FParameterTranslation->getVector() )
41  {
42  FParameterTranslation->setVector(AVector);
44  {
45  FMap->translate(FDeformationMark, AVector,
46  FDirectInfoVertex, FDirectInfoPonderation);
48  }
49  }
50 }
51 //******************************************************************************
53 { return FParameterTranslation->getVector(); }
54 //******************************************************************************
56 {
57  if (FParameterTranslation->getPonderation()!=AActive)
58  {
59  FParameterTranslation->setPonderation(AActive);
61  {
62  if (FParameterTranslation->getPonderation())
63  {
64  addPonderationCoefs();
65  updatePonderationCoefs();
66  }
67  else
68  delPonderationCoefs();
69 
70  FMap->updateDirectInfoWithVertex(FDirectInfoVertex);
71  }
72  }
73 }
74 //******************************************************************************
76 { return FParameterTranslation->getPonderation(); }
77 //******************************************************************************
80 //******************************************************************************
81 void CControlerGMap::setObjectRotationAxeVertex(const CVertex & AAxeVertex)
82 {
84  {
85  FParameterRotation->setAxeVertex(AAxeVertex);
86  FParameterRotation->setAngle(0.0);
87  }
88 }
89 //******************************************************************************
91 { return FParameterRotation->getAxeVertex(); }
92 //******************************************************************************
93 void CControlerGMap::setObjectRotationAxeVector(const CVertex & AAxeVector)
94 {
96  {
97  FParameterRotation->setAxeVector(AAxeVector);
98  FParameterRotation->setAngle(0.0);
99  }
100 }
101 //******************************************************************************
103 { return FParameterRotation->getAxeVector(); }
104 //******************************************************************************
106 {
107  if (AAngle!=FParameterRotation->getAngle())
108  {
109  FParameterRotation->setAngle(AAngle);
111  {
112  FMap->rotate(FDeformationMark, FParameterRotation->getAxeVertex(),
113  FParameterRotation->getAxeVector(),
114  AAngle, FDirectInfoVertex, FDirectInfoPonderation);
116  }
117  }
118 }
119 //******************************************************************************
121 { return FParameterRotation->getAngle(); }
122 //******************************************************************************
124 {
125  if (FParameterRotation->getPonderation()!=AActive)
126  {
127  FParameterRotation->setPonderation(AActive);
129  {
130  if (FParameterRotation->getPonderation())
131  {
132  addPonderationCoefs();
133  updatePonderationCoefs();
134  }
135  else
136  delPonderationCoefs();
137 
138  FMap->updateDirectInfoWithVertex(FDirectInfoVertex);
139  }
140  }
141 }
142 //******************************************************************************
144 { return FParameterRotation->getPonderation(); }
145 //******************************************************************************
148 //******************************************************************************
150 {
152  {
153  FParameterScale->setScaleType(AScaleType);
154  }
155 }
156 //******************************************************************************
158 { return FParameterScale->getScaleType(); }
159 //******************************************************************************
161 {
162  if ( AValue!=FParameterScale->getIsoScale() )
163  {
164  FParameterScale->setIsoScale(AValue);
165  FParameterScale->setIsoCoef(1.0);
166 
167  FMap->updateDirectInfoWithVertex(FDirectInfoVertex);
168  }
169 }
170 //******************************************************************************
172 { return FParameterScale->getIsoScale(); }
173 //******************************************************************************
176 //******************************************************************************
177 void CControlerGMap::setObjectScaleCenter(const CVertex & ACenter)
178 {
180  {
181  FParameterScale->setCenter(ACenter);
182  }
183 }
184 //******************************************************************************
186 { return FParameterScale->getCenter(); }
187 //******************************************************************************
188 void CControlerGMap::setObjectScaleVector(const CVertex & AVector)
189 {
191  {
192  FParameterScale->setVector(AVector);
193  }
194 }
195 //******************************************************************************
197 { return FParameterScale->getVector(); }
198 //******************************************************************************
200 {
201  if ( AValue!=FParameterScale->getIsoCoef() )
202  {
203  FParameterScale->setIsoCoef(AValue);
205  {
206  FMap->scale(FDeformationMark, FParameterScale->getCenter(),
207  CVertex(AValue,AValue,AValue),
208  FDirectInfoVertex, FDirectInfoPonderation);
210  }
211  }
212 }
213 //******************************************************************************
215 { return FParameterScale->getIsoCoef(); }
216 //******************************************************************************
217 void CControlerGMap::setObjectScaleCoef(const CVertex & AValue)
218 {
219  if ( AValue!=FParameterScale->getCoef() )
220  {
221  FParameterScale->setCoef(AValue);
223  {
224  FMap->scale(FDeformationMark, FParameterScale->getCenter(),
225  AValue, FDirectInfoVertex, FDirectInfoPonderation);
227  }
228  }
229 }
230 //******************************************************************************
232 { return FParameterScale->getCoef(); }
233 //******************************************************************************
234 void CControlerGMap::setObjectScaleCoef(int ADim, float AValue)
235 {
236  if ( AValue!=FParameterScale->getCoef(ADim) )
237  {
238  FParameterScale->setCoef(ADim,AValue);
240  {
241  FMap->scale(FDeformationMark, FParameterScale->getCenter(),
242  getObjectScaleCoef(), FDirectInfoVertex,
243  FDirectInfoPonderation);
245  }
246  }
247 }
248 //******************************************************************************
250 { return FParameterScale->getCoef(ADim); }
251 //******************************************************************************
253 {
254  if (FParameterScale->getPonderation()!=AActive)
255  {
256  FParameterScale->setPonderation(AActive);
258  {
259  if (FParameterScale->getPonderation())
260  {
261  addPonderationCoefs();
262  updatePonderationCoefs();
263  }
264  else
265  delPonderationCoefs();
266 
267  FMap->scale(FDeformationMark, FParameterScale->getCenter(),
268  FParameterScale->getCoef(), FDirectInfoVertex,
269  FDirectInfoPonderation);
271  }
272  }
273 }
274 //******************************************************************************
276 { return FParameterScale->getPonderation(); }
277 //******************************************************************************
280 //******************************************************************************
281 void CControlerGMap::modeObjectTransformationBegin(bool APonderation)
282 {
283  FDeformationDone = false;
284  basicPreSave();
285  FDeformationMark = getSelectionMark();
286 
287  FDirectInfoVertex = FMap->getNewDirectInfo();
288  FMap->duplicateVertexToDirectInfo(FDeformationMark, FDirectInfoVertex);
289 
290  assert(getParameterGMapVertex()->getMarkPartialChanged() == -1);
292 
293  FMap->markIncidentCells(ORBIT_VOLUME, FDeformationMark,
294  getParameterGMapVertex()->getMarkPartialChanged());
295  FMap->markIncidentCells(ORBIT_VERTEX,
296  getParameterGMapVertex()->getMarkPartialChanged());
297  FMap->markIncidentCells(ORBIT_VOLUME,
298  getParameterGMapVertex()->getMarkPartialChanged());
299 
300  if (APonderation)
301  {
302  addPonderationCoefs();
303  updatePonderationCoefs();
304  }
305 
306  for (unsigned int i=0; i<FViews.size(); ++i)
308 }
309 //------------------------------------------------------------------------------
310 void CControlerGMap::modeTranslationBegin()
311 {
312  modeObjectTransformationBegin(FParameterTranslation->getPonderation());
313  setMessage("Translation mode");
314  FParameterTranslation->setVector(ORIGIN);
315 }
316 //------------------------------------------------------------------------------
317 void CControlerGMap::modeRotationBegin()
318 {
319  modeObjectTransformationBegin(FParameterRotation->getPonderation());
320  setMessage("Rotation mode");
321  CVertex bary = FMap->directInfoBarycenter(FDirectInfoVertex);
322  FParameterRotation->setAxeVertex(bary);
323 }
324 //------------------------------------------------------------------------------
325 void CControlerGMap::modeScaleBegin()
326 {
327  modeObjectTransformationBegin(FParameterScale->getPonderation());
328  setMessage("Scaling mode");
329  FParameterScale->setIsoCoef(1.0);
330  FParameterScale->setCenter(FMap->directInfoBarycenter(FDirectInfoVertex));
331 }
332 //******************************************************************************
333 void CControlerGMap::modeObjectTransformationEnd(bool APonderation)
334 {
335  FMap->deleteDuplicatedVertex(FDirectInfoVertex);
336  FMap->freeDirectInfo(FDirectInfoVertex);
337 
338  FMap->unmarkAll(getParameterGMapVertex()->getMarkPartialChanged());
339  FMap->freeMark(getParameterGMapVertex()->getMarkPartialChanged());
341 
342  if (APonderation)
343  delPonderationCoefs();
344 
345  for (unsigned int i=0; i<FViews.size(); ++i)
347 
348  if (FDeformationDone)
349  basicPostSaveOk();
350  else
352 }
353 //------------------------------------------------------------------------------
354 void CControlerGMap::modeTranslationEnd()
355 {
356  modeObjectTransformationEnd(FParameterTranslation->getPonderation());
357 }
358 //------------------------------------------------------------------------------
359 void CControlerGMap::modeRotationEnd()
360 {
361  modeObjectTransformationEnd(FParameterRotation->getPonderation());
362 }
363 //------------------------------------------------------------------------------
364 void CControlerGMap::modeScaleEnd()
365 {
366  modeObjectTransformationEnd(FParameterScale->getPonderation());
367 }
368 //******************************************************************************
369 void CControlerGMap::modeTranslationOperationStart()
370 {
372  assert(view != NULL);
373 
375 
376  FMap->updateDirectInfoWithVertex(FDirectInfoVertex);
377 }
378 //******************************************************************************
379 void CControlerGMap::modeRotationOperationStart()
380 {
382  assert(view != NULL);
383 
385 
386  FParameterRotation->setAxeVector(view->getEyeDirection());
387 
388  FMap->updateDirectInfoWithVertex(FDirectInfoVertex);
389 }
390 //******************************************************************************
391 void CControlerGMap::modeScaleOperationStart()
392 {
394  assert(view != NULL);
395 
397 
398  FMap->updateDirectInfoWithVertex(FDirectInfoVertex);
399 }
400 //******************************************************************************
401 void CControlerGMap::modeTranslationOperationMove()
402 {
403  float last3D[3];
404  FViews[FCurrentViewId]->unproject(FLastX , FLastY , last3D);
405 
406  CVertex vect= CVertex(last3D[0], last3D[1], last3D[2]) -
407  CVertex(FFirst3D[0], FFirst3D[1], FFirst3D[2]);
408 
409  FDeformationDone |= !vect.isNull();
411 }
412 //******************************************************************************
413 void CControlerGMap::modeRotationOperationMove()
414 {
415  float last3D[3];
416  FViews[FCurrentViewId]->unproject(FLastX , FLastY , last3D);
417 
418  CVertex R =
419  CGeometry::orthoProjectVertexOnPlane(FParameterRotation->getAxeVertex(),
420  FViews[FCurrentViewId]->getLookAt(),
421  FViews[FCurrentViewId]->getEyeDirection());
422 
423  float newAngle = CGeometry::getAngle(CVertex(FFirst3D[0], FFirst3D[1], FFirst3D[2])-R,
424  CVertex(last3D[0], last3D[1], last3D[2])-R,
425  FViews[FCurrentViewId]->getEyeDirection());
426 
427  FDeformationDone |= !isZero(newAngle);
428  setObjectRotationAngle(newAngle);
429 }
430 //******************************************************************************
431 void CControlerGMap::modeScaleOperationMove()
432 {
434 
435  if (FParameterScale->getIsoScale())
436  {
437  float coef = (FLastX-FFirstX+FLastY-FFirstY)/100.0 + 1.0;
438 
439  FDeformationDone |= !areEqual(coef, 1.0);
440 
441  setObjectScaleIsoCoef(coef);
442  }
443  else
444  {
445  float last3D[3];
446  view->unproject(FLastX , FLastY , last3D);
447 
448  CVertex R =
449  CGeometry::orthoProjectVertexOnPlane(FParameterScale->getCenter(),
450  view->getLookAt() +
451  view->getEyeDirection(),
452  view->getEyeDirection());
453 
454  CVertex vect = CVertex(FFirst3D[0], FFirst3D[1], FFirst3D[2])-R;
455 
456  for (int i=0;i<3;++i)
457  if (!isZero(vect.getCoord(i)))
458  vect.setCoord(i,1/vect.getCoord(i));
459 
460  vect = (CVertex(last3D[0], last3D[1], last3D[2])-R).multiply(vect);
461 
462  FDeformationDone |= ( vect != CVertex(1,1,1) );
463 
464  setObjectScaleCoef(vect);
465  }
466 }
467 //******************************************************************************
468 void CControlerGMap::modeTransformationOperationStop()
469 {}
470 //******************************************************************************
471 void CControlerGMap::addPonderationCoefs()
472 {
473  FDirectInfoPonderation = FMap->getNewDirectInfo();
474  FMap->allocCoordToDirectInfo(getSelectionMark(),
475  FDirectInfoPonderation,
476  ORBIT_VERTEX);
477 }
478 //******************************************************************************
479 void CControlerGMap::delPonderationCoefs()
480 {
481  FMap->freeMemoryFromDirectInfo(getSelectionMark(),
482  FDirectInfoPonderation,
483  ORBIT_VERTEX);
484  FMap->freeDirectInfo(FDirectInfoPonderation);
485  FDirectInfoPonderation = -1;
486 }
487 //******************************************************************************
488 void CControlerGMap::updatePonderationCoefs()
489 {
490  CVertex center = FParameterPonderation->getPonderationCenter();
491  CVertex vect = FParameterPonderation->getPonderationVector();
492 
493  if (vect.isNull())
494  vect = OZ;
495 
496  switch (FParameterPonderation->getPonderationType())
497  {
498  case PONDERATION_POINT:
499  FMap->computeDistanceToVertex(getSelectionMark(),
500  FDirectInfoPonderation, center);
501  break;
502 
503  case PONDERATION_AXE:
504  FMap->computeDistanceToLine(getSelectionMark(),
505  FDirectInfoPonderation, center, vect);
506  break;
507 
508  case PONDERATION_PLANE:
509  FMap->computeDistanceToPlane(getSelectionMark(),
510  FDirectInfoPonderation, center, vect);
511  break;
512 
513  default:
514  return;
515  }
516 
517  if (FParameterPonderation->getPonderationStretchIn())
518  FMap->normalizeParameter(getSelectionMark(),
519  FDirectInfoPonderation,
520  FParameterPonderation->
521  getPonderationStretchInMin(),
522  FParameterPonderation->
523  getPonderationStretchInMax());
524 
525  FMap->applyFunctionOnParameter(getSelectionMark(),
526  FDirectInfoPonderation,
527  FParameterPonderation->
528  getPonderationFunctionType());
529 
530  if (FParameterPonderation->getPonderationStretchOut())
531  FMap->normalizeParameter(getSelectionMark(),
532  FDirectInfoPonderation,
533  FParameterPonderation->
534  getPonderationStretchOutMin(),
535  FParameterPonderation->
536  getPonderationStretchOutMax());
537 }
538 //******************************************************************************
540 {
541  if ( getLastSelectedDart()==NULL ) return ORIGIN;
542  return *FMap->findVertex(getLastSelectedDart());
543 }
544 //------------------------------------------------------------------------------
545 void CControlerGMap::setVertexPosition(const CVertex& AVertex)
546 {
547  if ( getLastSelectedDart()!=NULL && getVertexPosition()!=AVertex)
548  {
549  undoRedoPreSave();
550  FMap->translate(getSelectionMark(), AVertex-getVertexPosition());
552 
553  setModelChanged();
554  }
555 }
556 //******************************************************************************