Developer documentation | Axl-2.5.1

axlPoint.cpp
Go to the documentation of this file.
1 /* axlPoint.cpp ---
2  *
3  * Author: Meriadeg Perrinel
4  * Copyright (C) 2008 - Meriadeg Perrinel, Inria.
5  * Created: Tue Nov 9 16:58:59 2010 (+0100)
6  * Version: $Id$
7  * Last-Updated: Tue Nov 9 17:09:38 2010 (+0100)
8  * By: Meriadeg Perrinel
9  * Update #: 19
10  */
11 
12 /* Commentary:
13  *
14  */
15 
16 /* Change log:
17  *
18  */
19 
20 #include "axlPoint.h"
21 
22 #include <dtkCoreSupport/dtkGlobal.h>
23 
24 // /////////////////////////////////////////////////////////////////
25 // axlPointPrivate
26 // /////////////////////////////////////////////////////////////////
27 
28 class axlPointPrivate
29 {
30 public:
31  double coordinates[3];
32 
33 };
34 
35 // /////////////////////////////////////////////////////////////////
36 // axlPoint implementation
37 // /////////////////////////////////////////////////////////////////
38 
40 
45 axlPoint::axlPoint(QObject *parent) : axlAbstractData(), d(new axlPointPrivate)
46 {
47  this->setObjectName(this->identifier());
48  d->coordinates[0] = 0.0;
49  d->coordinates[1] = 0.0;
50  d->coordinates[2] = 0.0;
51 }
52 
54 
59 axlPoint::axlPoint(double x, double y, double z, QObject *parent) : axlAbstractData(), d(new axlPointPrivate)
60 {
61  this->setObjectName(this->identifier());
62  d->coordinates[0] = x;
63  d->coordinates[1] = y;
64  d->coordinates[2] = z;
65 }
66 
68 
71 axlPoint::axlPoint(const axlPoint& other) : axlAbstractData(), d(new axlPointPrivate)
72 {
73  this->setObjectName(this->identifier());
74  this->setParent(other.parent());
75  d->coordinates[0] = other.d->coordinates[0];
76  d->coordinates[1] = other.d->coordinates[1];
77  d->coordinates[2] = other.d->coordinates[2];
78 }
79 
81 
84 axlPoint::axlPoint(const axlPoint *other) : axlAbstractData(), d(new axlPointPrivate)
85 {
86  this->setObjectName(this->identifier());
87  this->setParent(other->parent());
88  d->coordinates[0] = other->d->coordinates[0];
89  d->coordinates[1] = other->d->coordinates[1];
90  d->coordinates[2] = other->d->coordinates[2];
91 }
92 
93 
94 axlPoint::axlPoint(double *p, QObject *parent): d(new axlPointPrivate)
95 {
96  // There is no verification of p;
97  this->setObjectName(this->identifier());
98  d->coordinates[0] = p[0];
99  d->coordinates[1] = p[1];
100  d->coordinates[2] = p[2];
101 }
102 
104 
108 {
109  delete d;
110 
111  d = NULL;
112 }
113 
115 
119 {
120  d->coordinates[0] = other.d->coordinates[0];
121  d->coordinates[1] = other.d->coordinates[1];
122  d->coordinates[2] = other.d->coordinates[2];
123 
124  return (*this);
125 }
126 
128 
132 {
133  d->coordinates[0] += other.d->coordinates[0];
134  d->coordinates[1] += other.d->coordinates[1];
135  d->coordinates[2] += other.d->coordinates[2];
136 
137  return (*this);
138 }
139 
141 
145 {
146  d->coordinates[0] -= other.d->coordinates[0];
147  d->coordinates[1] -= other.d->coordinates[1];
148  d->coordinates[2] -= other.d->coordinates[2];
149 
150  return (*this);
151 }
152 
154 
158 {
159  d->coordinates[0] *= scalar;
160  d->coordinates[1] *= scalar;
161  d->coordinates[2] *= scalar;
162 
163  return (*this);
164 }
165 
167 
171 {
172  if(!(scalar != 0.)) {
173  qWarning() << DTK_PRETTY_FUNCTION << "Preventing division by 0. Operand not affected.";
174  return (*this);
175  }
176 
177  d->coordinates[0] /= scalar;
178  d->coordinates[1] /= scalar;
179  d->coordinates[2] /= scalar;
180 
181  return (*this);
182 }
183 
184 
185 
187 
191 {
192  axlPoint result(d->coordinates[0] + other.d->coordinates[0],
193  d->coordinates[1] + other.d->coordinates[1],
194  d->coordinates[2] + other.d->coordinates[2] );
195  return result;
196 }
197 
199 
203 {
204  axlPoint result(d->coordinates[0] - other.d->coordinates[0],
205  d->coordinates[1] - other.d->coordinates[1],
206  d->coordinates[2] - other.d->coordinates[2]);
207  return result;
208 }
209 
211 
214 axlPoint axlPoint::operator*(double scalar) const
215 {
216  axlPoint result(d->coordinates[0] * scalar,
217  d->coordinates[1] * scalar,
218  d->coordinates[2] * scalar);
219  return result;
220 }
221 
223 
226 double axlPoint::operator*(const axlPoint& other) const
227 {
228  return (d->coordinates[0] * other[0]
229  + d->coordinates[1] * other[1]
230  + d->coordinates[2] * other[2]);
231 
232 }
236 bool axlPoint::operator<(const axlPoint& other) const{
237  return (d->coordinates[0] < other.x()
238  || (!(other.x() == d->coordinates[0]) && d->coordinates[1] < other.y())
239  || (!(other.x() == d->coordinates[0]) && !(other.y() == d->coordinates[1]) && d->coordinates[2] < other.z()));
240 }
241 
242 
243 
245 
248 axlPoint axlPoint::operator/(double scalar) const
249 {
250  if(!(scalar != 0.)) {
251  qWarning() << DTK_PRETTY_FUNCTION << "Preventing division by 0. Returning default point";
252  return axlPoint();
253  }
254 
255  axlPoint result(d->coordinates[0] / scalar,
256  d->coordinates[1] / scalar,
257  d->coordinates[2] / scalar);
258 
259  return result;
260 }
261 
263 void axlPoint::transformAsPoint(double matrix[12]) {
264  this->setCoordinates(
265  d->coordinates[0] * matrix[0] + d->coordinates[1] * matrix[1] + d->coordinates[2] * matrix[2] + matrix[3],
266  d->coordinates[0] * matrix[4] + d->coordinates[1] * matrix[5] + d->coordinates[2] * matrix[6] + matrix[7],
267  d->coordinates[0] * matrix[8] + d->coordinates[1] * matrix[9] + d->coordinates[2] * matrix[10] + matrix[11]
268  );
269 }
270 
272 void axlPoint::transformAsVector(double matrix[12]) {
273  this->setCoordinates(
274  d->coordinates[0] * matrix[0] + d->coordinates[1] * matrix[1] + d->coordinates[2] * matrix[2],
275  d->coordinates[0] * matrix[4] + d->coordinates[1] * matrix[5] + d->coordinates[2] * matrix[6],
276  d->coordinates[0] * matrix[8] + d->coordinates[1] * matrix[9] + d->coordinates[2] * matrix[10]
277  );
278 }
279 
280 
281 bool axlPoint::operator== (const axlPoint& point2) const {
282  return (this->x() == point2.x()
283  && this->y() == point2.y()
284  && this->z() == point2.z());
285 }
286 
287 bool axlPoint::operator !=(const axlPoint& point2) const {
288  return !(*this == point2);
289 }
290 
291 
293 
296 double axlPoint::x(void) const
297 {
298  return d->coordinates[0];
299 }
300 
302 
305 double axlPoint::y(void) const
306 {
307  return d->coordinates[1];
308 }
309 
311 
314 double axlPoint::z(void) const
315 {
316  return d->coordinates[2];
317 }
318 
320 
323 double axlPoint::operator [] (int i) const
324 {
325  //assert( i>=0 && i<3);
326  return d->coordinates[i];
327 }
328 
330 
333 double& axlPoint::x(void)
334 {
335  return d->coordinates[0];
336 }
337 
339 
342 double& axlPoint::y(void)
343 {
344  return d->coordinates[1];
345 }
346 
348 
351 double& axlPoint::z(void)
352 {
353  return d->coordinates[2];
354 }
355 
357 
361 {
362  //assert( i>=0 && i<3);
363  return d->coordinates[i];
364 }
365 
367 
370 void axlPoint::setCoordinates(double x, double y, double z)
371 {
372  d->coordinates[0] = x;
373  d->coordinates[1] = y;
374  d->coordinates[2] = z;
375 // this->touchGeometry();
376 }
377 
378 
380 
383 //void axlPoint::updateCoordinates(double x, double y, double z)
384 //{
385 // this->setCoordinates(x,y,z);
386 // this->touchGeometry();
387 //}
388 
389 void axlPoint::touchX(double x)
390 {
391  d->coordinates[0] = x;
392 
393  this->touchGeometry();
394 }
395 
396 void axlPoint::touchY(double y)
397 {
398  d->coordinates[1] = y;
399 
400  this->touchGeometry();
401 }
402 
403 void axlPoint::touchZ(double z)
404 {
405  d->coordinates[2] = z;
406 
407  this->touchGeometry();
408 }
409 
411 {
412  double n = this->norm();
413  if(n > 0.0005)
414  {
415  d->coordinates[0] /= n;
416  d->coordinates[1] /= n;
417  d->coordinates[2] /= n;
418  }
419 }
420 
422  double n = this->norm();
423  if(n > 0.0005)
424  {
425  d->coordinates[0] /= n;
426  d->coordinates[1] /= n;
427  d->coordinates[2] /= n;
428  }
429  return *this;
430 }
431 
432 
434 
438  return (d->coordinates[0]==point->x() && d->coordinates[1]==point->y() && d->coordinates[2]==point->z());
439 }
440 
442 
445 double *axlPoint::coordinates(void) const
446 {
447  return d->coordinates ;
448 }
449 
450 double axlPoint::norm() const
451 {
452  return sqrt(d->coordinates[0] * d->coordinates[0] + d->coordinates[1] * d->coordinates[1] + d->coordinates[2] * d->coordinates[2]);
453 }
454 
456 
459 double axlPoint::distance(const axlPoint& lhs, const axlPoint& rhs)
460 {
461  double dx = rhs.d->coordinates[0] - lhs.d->coordinates[0];
462  double dy = rhs.d->coordinates[1] - lhs.d->coordinates[1];
463  double dz = rhs.d->coordinates[2] - lhs.d->coordinates[2];
464 
465  return qSqrt(dx*dx + dy*dy + dz*dz);
466 }
467 
469 
473 {
474  double dx = rhs->d->coordinates[0] - lhs->d->coordinates[0];
475  double dy = rhs->d->coordinates[1] - lhs->d->coordinates[1];
476  double dz = rhs->d->coordinates[2] - lhs->d->coordinates[2];
477 
478  return qSqrt(dx*dx + dy*dy + dz*dz);
479 }
480 
482 
486 {
487  return axlPoint(
488  lhs.d->coordinates[1]*rhs.d->coordinates[2] - lhs.d->coordinates[2]*rhs.d->coordinates[1],
489  lhs.d->coordinates[2]*rhs.d->coordinates[0] - lhs.d->coordinates[0]*rhs.d->coordinates[2],
490  lhs.d->coordinates[0]*rhs.d->coordinates[1] - lhs.d->coordinates[1]*rhs.d->coordinates[0]
491  );
492 }
493 
495 
499 {
500  return new axlPoint(
501  lhs->d->coordinates[1]*rhs->d->coordinates[2] - lhs->d->coordinates[2]*rhs->d->coordinates[1],
502  lhs->d->coordinates[2]*rhs->d->coordinates[0] - lhs->d->coordinates[0]*rhs->d->coordinates[2],
503  lhs->d->coordinates[0]*rhs->d->coordinates[1] - lhs->d->coordinates[1]*rhs->d->coordinates[0]
504  );
505 }
506 
507 
508 double axlPoint::dotProduct(const axlPoint& lhs, const axlPoint& rhs) {
509  return lhs.x()*rhs.x() + lhs.y()*rhs.y() + lhs.z()*rhs.z();
510 }
511 
513  return lhs->x()*rhs->x() + lhs->y()*rhs->y() + lhs->z()*rhs->z();
514 }
515 
516 
517 // /////////////////////////////////////////////////////////////////
518 // Debug operators
519 // /////////////////////////////////////////////////////////////////
520 
521 QDebug operator<<(QDebug dbg, axlPoint point)
522 {
523  dbg.nospace() << QString("axlPoint: (%1, %2, %3)").arg(point.x()).arg(point.y()).arg(point.z());
524 
525  return dbg.space();
526 }
527 
528 QDebug operator<<(QDebug dbg, axlPoint& point)
529 {
530  dbg.nospace() << QString("axlPoint: (%1, %2, %3)").arg(point.x()).arg(point.y()).arg(point.z());
531 
532  return dbg.space();
533 }
534 
535 QDebug operator<<(QDebug dbg, axlPoint *point)
536 {
537  dbg.nospace() << QString("axlPoint: (%1, %2, %3)").arg(point->x()).arg(point->y()).arg(point->z());
538 
539  return dbg.space();
540 }
541 
542 QString axlPoint::description(void) const
543 {
544  QString result = "axlPoint";
545  result.append("\nX : "+QString::number(d->coordinates[0]) + "\nY : "+QString::number(d->coordinates[1]) + "\nZ : "+QString::number(d->coordinates[2]));
546  return result;
547 }
548 
549 QString axlPoint::identifier(void) const
550 {
551  return "axlPoint";
552 }
553 
555 //void axlPoint::onXChanged(double x)
556 //{
557 // d->coordinates[0] = x;
558 //}
559 
560 //void axlPoint::onYChanged(double y)
561 //{
562 // d->coordinates[1] = y;
563 //}
564 
565 //void axlPoint::onZChanged(double z)
566 //{
567 // d->coordinates[2] = z;
568 //}
569 
570 
571 QVariantList axlPoint::convertDataToQVariant(void) const{
572  QVariantList list;
573  QVariant id = QVariant::fromValue(identifier());
574  QVariant x = QVariant::fromValue(d->coordinates[0]);
575  QVariant y = QVariant::fromValue(d->coordinates[1]);
576  QVariant z = QVariant::fromValue(d->coordinates[2]);
577  list.append(id);
578  list.append(x);
579  list.append(y);
580  list.append(z);
581  QVariant name = QVariant::fromValue(objectName());
582  list.append(name);
583 
584  return list;
585 
586 }
587 
588 int axlPoint::convertQVariantToData(const QVariantList &data){
589  QVariant x = data.at(1);
590  QVariant y = data.at(2);
591  QVariant z = data.at(3);
592  QString name = data.last().toString();
593  setObjectName(name);
594  setCoordinates(qvariant_cast<double>(x),qvariant_cast<double>(y),qvariant_cast<double>(z) );
595  return 1;
596 }
597 
598 //to be registered to the data factory.
599 dtkAbstractData *createaxlPoint(void)
600 {
601  return new axlPoint;
602 }
603 
604 // /////////////////////////////////////////////////////////////////
605 // axlPoint documentation
606 // /////////////////////////////////////////////////////////////////
607 
QDebug operator<<(QDebug dbg, axlPoint point)
Definition: axlPoint.cpp:521
void touchGeometry(void)
Class axlPoint defines 3D points.
Definition: axlPoint.h:34
double operator*(const axlPoint &other) const
Compute the inner product of the two vectors.
Definition: axlPoint.cpp:226
double * coordinates(void) const
Returns coordinates of this point.
Definition: axlPoint.cpp:445
void transformAsPoint(double matrix[12])
Multiplication by 3x3 left block (rotation part) and addition with 3x1 right block (translation part)...
Definition: axlPoint.cpp:263
QVariantList convertDataToQVariant(void) const
Convert an axlAbstractData into a QVariantList that specifies all properties of the axlAbstractData...
Definition: axlPoint.cpp:571
bool operator!=(const axlPoint &point2) const
Definition: axlPoint.cpp:287
axlPoint(QObject *parent=0)
Constructs an axel point of zero coordinates with parent parent of QObject type.
Definition: axlPoint.cpp:45
void touchY(double y)
Definition: axlPoint.cpp:396
~axlPoint(void)
Destroys the axel point.
Definition: axlPoint.cpp:107
axlPoint operator+(const axlPoint &other) const
Returns a point that results from the addition of this point and the other point. ...
Definition: axlPoint.cpp:190
void touchX(double x)
Change coordinates of this point.
Definition: axlPoint.cpp:389
dtkAbstractData * createaxlPoint(void)
Definition: axlPoint.cpp:599
virtual QString description(void) const
Definition: axlPoint.cpp:542
virtual QString identifier(void) const
Definition: axlPoint.cpp:549
axlPoint & operator-=(const axlPoint &other)
Substracts the coordinates of the other point to this point and returns a reference to this point...
Definition: axlPoint.cpp:144
void touchZ(double z)
Definition: axlPoint.cpp:403
void normalize(void)
Definition: axlPoint.cpp:410
double norm(void) const
Definition: axlPoint.cpp:450
double z(void) const
Returns z-coordinate of this point.
Definition: axlPoint.cpp:314
axlPoint & operator/=(double scalar)
Divides the coordinates of this point by a scalar value and returns a reference to this point...
Definition: axlPoint.cpp:170
axlPoint & normalized(void)
Definition: axlPoint.cpp:421
static double dotProduct(const axlPoint &lhs, const axlPoint &rhs)
Definition: axlPoint.cpp:508
axlPoint operator/(double scalar) const
Returns a point that results from the division of the scalar with this point.
Definition: axlPoint.cpp:248
double y
Definition: axlPoint.h:37
axlPoint operator-(const axlPoint &other) const
Returns a point that results from the substraction of this point and the other point.
Definition: axlPoint.cpp:202
axlPoint & operator+=(const axlPoint &other)
Adds the coordinates of the other point to this point and returns a reference to this point...
Definition: axlPoint.cpp:131
double x(void) const
Returns x-coordinate of this point.
Definition: axlPoint.cpp:296
double z
Definition: axlPoint.h:38
bool operator==(const axlPoint &point2) const
Definition: axlPoint.cpp:281
static axlPoint crossProduct(const axlPoint &lhs, const axlPoint &rhs)
Returns the cross product between lhs (coords) and rhs (coords).
Definition: axlPoint.cpp:485
bool isEqualTo(axlPoint *point)
Check if the point we apply the method on is equal to point.
Definition: axlPoint.cpp:437
static double distance(const axlPoint &lhs, const axlPoint &rhs)
Returns the distance between lhs point and rhs point.
Definition: axlPoint.cpp:459
axlPoint & operator=(const axlPoint &other)
Assigns other to this point and returns a reference to this point.
Definition: axlPoint.cpp:118
int convertQVariantToData(const QVariantList &data)
Modify properties and geometry variables of the axlAbstractData. Return 1 if the modification was suc...
Definition: axlPoint.cpp:588
double x
Definition: axlPoint.h:37
void setCoordinates(double x, double y, double z)
Change coordinates of this point.
Definition: axlPoint.cpp:370
double operator[](int i) const
Returns i-th coordinate of this point.
Definition: axlPoint.cpp:323
Class axlAbstractData defines an API for all type of axel data.
double y(void) const
Returns y-coordinate of this point.
Definition: axlPoint.cpp:305
axlPoint & operator*=(double scalar)
Multiplies the coordinates of this point by a scalar value and returns a reference to this point...
Definition: axlPoint.cpp:157
void transformAsVector(double matrix[12])
Multiplication by 3x3 left block (rotation part) and addition with 3x1 right block (translation part)...
Definition: axlPoint.cpp:272
bool operator<(const axlPoint &other) const
order on axlPoints. Compare the coordinates (x,y,z) by lexicographic order.
Definition: axlPoint.cpp:236