Moka controlers
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros
parameter-object-position.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-types.hh"
27 #include "transformation-matrix.hh"
28 #include "geometry.hh"
29 using namespace std;
30 using namespace GMap3d;
31 //******************************************************************************
32 /* Paramètres géométriques :
33  *
34  * C : Center
35  * F : FirstVertex
36  * N : Normal
37  * R : Rotations
38  * S : Scale
39  * P : Proportions
40  * D : Dimensions
41  *
42  * Dépendances : ("A -> B" signifie "quand on modifie A il mettre à jour B")
43  *
44  * C -> F
45  * F -> NSPD
46  * N -> FR
47  * R -> NF
48  * S -> FD
49  * P -> DF
50  * D -> F(P) (P ssi D est différent du vecteur nul)
51  */
52 //******************************************************************************
53 CParameterObjectPosition::CParameterObjectPosition(bool ANeedRotation,
54  int ANbRef) :
55  CParameter (ANbRef),
56  FNeedRotation(ANeedRotation)
57 {
58  reinit();
59 }
60 //******************************************************************************
63  CParameter (AParam),
64  FNeedRotation(AParam.FNeedRotation)
65 {
66  setScale (AParam.getScale());
68  setCenter (AParam.getCenter());
69  setRotations (AParam.getRotations());
70 }
71 //******************************************************************************
73 {}
74 //******************************************************************************
76 { return new CParameterObjectPosition(*this); }
77 //******************************************************************************
78 void CParameterObjectPosition::load(istream& /*AStream*/)
79 {}
80 //------------------------------------------------------------------------------
81 void CParameterObjectPosition::save(ostream& /*AStream*/)
82 {}
83 //------------------------------------------------------------------------------
85 {
86  setScale (1);
87  setProportions(1, 1, 1);
88  setCenter (0, 0, 0);
89  setRotations (0, 0, 0);
90 }
91 //******************************************************************************
93 { return FScale; }
94 //******************************************************************************
96 {
97  if (FScale != AScale)
98  {
100 
101  FScale = AScale;
102 
103  // Dimensions:
104  FDimensions = 2 * FScale * FProportions;
105 
106  // First vertex:
107  CTransformationMatrix matrix(CTransformationMatrix::IdentityMatrix);
108  matrix.rotate(FRotations);
109 
110  CVertex x(OX);
111  matrix.applyOn(x);
112  FFirstVertex = FCenter + (getDimensionX() / 2) * x;
113  }
114 }
115 //******************************************************************************
117 { return FProportions.getX(); }
119 { return FProportions.getY(); }
121 { return FProportions.getZ(); }
122 
124 { return FProportions; }
125 //******************************************************************************
127 { setProportions(AX, FProportions.getY(), FProportions.getZ()); }
129 { setProportions(FProportions.getX(), AY, FProportions.getZ()); }
131 { setProportions(FProportions.getX(), FProportions.getY(), AZ); }
132 
133 void CParameterObjectPosition::setProportions(float AX, float AY, float AZ)
134 { setProportions(CVertex(AX, AY, AZ)); }
135 
136 void CParameterObjectPosition::setProportions(const CVertex & AValues)
137 {
138  if (FProportions != AValues)
139  {
141 
142  FProportions = AValues;
143 
144  // Dimensions:
145  FDimensions = 2 * getScale() * getProportions();
146 
147  // First vertex:
148  CTransformationMatrix matrix(CTransformationMatrix::IdentityMatrix);
149  matrix.rotate(FRotations);
150  CVertex x(OX);
151  matrix.applyOn(x);
152  FFirstVertex = FCenter + (getDimensionX() / 2) * x;
153  }
154 }
155 //******************************************************************************
157 { return FDimensions.getX(); }
159 { return FDimensions.getY(); }
161 { return FDimensions.getZ(); }
162 
164 { return FDimensions; }
165 //******************************************************************************
167 { setDimensions(AX, FDimensions.getY(), FDimensions.getZ()); }
169 { setDimensions(FDimensions.getX(), AY, FDimensions.getZ()); }
171 { setDimensions(FDimensions.getX(), FDimensions.getY(), AZ); }
172 
173 void CParameterObjectPosition::setDimensions(float AX, float AY, float AZ)
174 { setDimensions(CVertex(AX,AY,AZ)); }
175 
176 void CParameterObjectPosition::setDimensions(const CVertex & AValues)
177 {
178  if (FDimensions != AValues)
179  {
181 
182  FDimensions = AValues;
183 
184  // Proportions:
185  if (! FDimensions.isNull())
186  FProportions = isZero(FScale) ? ORIGIN : FDimensions / 2 / getScale();
187 
188  // First vertex:
189  CTransformationMatrix matrix(CTransformationMatrix::IdentityMatrix);
190  matrix.rotate(getRotations());
191  CVertex x(OX);
192  matrix.applyOn(x);
193  FFirstVertex = FCenter + (getDimensionX() / 2) * x;
194  }
195 }
196 //******************************************************************************
198 { return FCenter.getX(); }
200 { return FCenter.getY(); }
202 { return FCenter.getZ(); }
203 
205 { return FCenter; }
206 //******************************************************************************
208 { setCenter(AX, FCenter.getY(), FCenter.getZ()); }
210 { setCenter(FCenter.getX(), AY, FCenter.getZ()); }
212 { setCenter(FCenter.getX(), FCenter.getY(), AZ); }
213 
214 void CParameterObjectPosition::setCenter(float AX, float AY, float AZ)
215 { setCenter(CVertex(AX,AY,AZ)); }
216 
217 void CParameterObjectPosition::setCenter(const CVertex & AVertex)
218 {
219  if (FCenter != AVertex)
220  {
222 
223  FCenter = AVertex;
224 
225  // First vertex:
226  CTransformationMatrix matrix(CTransformationMatrix::IdentityMatrix);
227  matrix.rotate(getRotations());
228  CVertex x(OX);
229  matrix.applyOn(x);
230  FFirstVertex = FCenter + (getDimensionX() / 2) * x;
231  }
232 }
233 //******************************************************************************
235 { return FRotations.getX(); }
237 { return FRotations.getY(); }
239 { return FRotations.getZ(); }
240 
242 {
243  return FRotations;
244 }
245 //******************************************************************************
247 { setRotations(AAlpha, FRotations.getY(), FRotations.getZ()); }
249 { setRotations(FRotations.getX(), ABeta, FRotations.getZ()); }
251 { setRotations(FRotations.getX(), FRotations.getY(), AGamma); }
252 
254  float ABeta,
255  float AGamma)
256 { setRotations(CVertex(AAlpha, ABeta, AGamma)); }
257 
258 void CParameterObjectPosition::setRotations(const CVertex & AAngles)
259 {
260  if (FRotations != AAngles)
261  {
263 
264  FRotations = AAngles;
265 
266  CTransformationMatrix matrix(CTransformationMatrix::IdentityMatrix);
267  matrix.rotate(AAngles);
268 
269  // First vertex:
270  CVertex x(OX);
271  matrix.applyOn(x);
272  FFirstVertex = FCenter + (getDimensionX() / 2) * x;
273 
274  // Normal vector:
275  CVertex z(OZ);
276  matrix.applyOn(z);
277  FNormalVector = z;
278  }
279 }
280 //******************************************************************************
282 { return FFirstVertex.getX(); }
284 { return FFirstVertex.getY(); }
286 { return FFirstVertex.getZ(); }
287 
289 { return FFirstVertex; }
290 //******************************************************************************
291 void CParameterObjectPosition::setFirstVertexX(float AX, bool AKeepNormalVector)
292 {
293  setFirstVertex(AX, FFirstVertex.getY(), FFirstVertex.getZ(),
294  AKeepNormalVector);
295 }
296 void CParameterObjectPosition::setFirstVertexY(float AY, bool AKeepNormalVector)
297 {
298  setFirstVertex(FFirstVertex.getX(), AY, FFirstVertex.getZ(),
299  AKeepNormalVector);
300 }
301 void CParameterObjectPosition::setFirstVertexZ(float AZ, bool AKeepNormalVector)
302 {
303  setFirstVertex(FFirstVertex.getX(), FFirstVertex.getY(), AZ,
304  AKeepNormalVector);
305 }
306 
307 void CParameterObjectPosition::setFirstVertex(float AX, float AY, float AZ,
308  bool AKeepNormalVector)
309 { setFirstVertex(CVertex(AX,AY,AZ), AKeepNormalVector); }
310 
311 void CParameterObjectPosition::setFirstVertex(const CVertex & AVertex,
312  bool AKeepNormalVector)
313 {
314  if (FFirstVertex != AVertex)
315  {
317 
318  FFirstVertex = AVertex;
319 
320  CVertex V(FFirstVertex - FCenter);
321 
322  // Proportions:
323  if (isZero(FProportions.getX()))
324  FProportions.setX(1);
325 
326  // Scale:
327  FScale = V.norm() / FProportions.getX();
328 
329  // Dimensions:
330  FDimensions = 2 * FScale * FProportions;
331 
332  // Normal vector: (ancien test : ! isZero(V.dot(FNormalVector)))
333  if (! V.isNull() && ! AKeepNormalVector)
334  FNormalVector = V * (isZero(V.getY()) ? OY : OX);
335 
336  // Rotations:
337  FRotations = CGeometry::getAngles(V, FNormalVector);
338  }
339 }
340 //******************************************************************************
342 { return FNormalVector.getX(); }
344 { return FNormalVector.getY(); }
346 { return FNormalVector.getZ(); }
347 
349 { return FNormalVector; }
350 //******************************************************************************
352 { setNormalVector(AX, FNormalVector.getY(), FNormalVector.getZ()); }
354 { setNormalVector(FNormalVector.getX(), AY, FNormalVector.getZ()); }
356 { setNormalVector(FNormalVector.getX(), FNormalVector.getY(), AZ); }
357 
358 void CParameterObjectPosition::setNormalVector(float AX, float AY, float AZ)
359 { setNormalVector(CVertex(AX, AY, AZ)); }
360 
361 void CParameterObjectPosition::setNormalVector(const CVertex & AVector)
362 {
363  if (FNormalVector != AVector)
364  {
366 
367  FNormalVector = AVector;
368 
369  // Rotations:
370  FRotations = CGeometry::getAngles(FFirstVertex - FCenter, FNormalVector);
371 
372  // First vertex:
373  CTransformationMatrix matrix(CTransformationMatrix::IdentityMatrix);
374  matrix.rotate(FRotations);
375  CVertex x(OX);
376  matrix.applyOn(x);
377  FFirstVertex = FCenter + (getDimensionX() / 2) * x;
378  }
379 }
380 //******************************************************************************
382 {
383  CTransformationMatrix matrix(CTransformationMatrix::IdentityMatrix);
384 
385  if (FNeedRotation)
386  matrix.rotate(OY, 90);
387 
388  matrix.scale(FDimensions);
389  matrix.rotate(FRotations);
390  matrix.translate(FCenter);
391 
392  return matrix;
393 }
394 //******************************************************************************
396 { return PARAMETER_OBJECT_POSITION; }
397 //******************************************************************************