Developer documentation | Axl-2.5.1

axlSphere.cpp
Go to the documentation of this file.
1 /* axlSphere.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 "axlSphere.h"
21 
22 #include <dtkCoreSupport/dtkGlobal.h>
23 
24 // /////////////////////////////////////////////////////////////////
25 // axlSpherePrivate
26 // /////////////////////////////////////////////////////////////////
27 
28 class axlSpherePrivate
29 {
30 public:
31  double coordinates[3];
32  double radius;
33 
34 };
35 
36 // /////////////////////////////////////////////////////////////////
37 // axlSphere implementation
38 // /////////////////////////////////////////////////////////////////
39 
41 
46 axlSphere::axlSphere(QObject *parent) : axlAbstractData(), d(new axlSpherePrivate)
47 {
48  this->setObjectName(this->identifier());
49  d->coordinates[0] = 0.0;
50  d->coordinates[1] = 0.0;
51  d->coordinates[2] = 0.0;
52  d->radius = 1.0;
53 }
54 
56 
61 axlSphere::axlSphere(double x, double y, double z, double radius, QObject *parent) : axlAbstractData(), d(new axlSpherePrivate)
62 {
63  this->setObjectName(this->identifier());
64  d->coordinates[0] = x;
65  d->coordinates[1] = y;
66  d->coordinates[2] = z;
67  d->radius = radius;
68 
69 }
70 
72 
75 axlSphere::axlSphere(const axlSphere& other) : axlAbstractData(), d(new axlSpherePrivate)
76 {
77  this->setObjectName(this->identifier());
78  this->setParent(other.parent());
79  d->coordinates[0] = other.d->coordinates[0];
80  d->coordinates[1] = other.d->coordinates[1];
81  d->coordinates[2] = other.d->coordinates[2];
82  d->radius = other.d->radius;
83 
84 }
85 
87 
91 {
92  delete d;
93 
94  d = NULL;
95 }
96 
98 
102 {
103  d->coordinates[0] = other.d->coordinates[0];
104  d->coordinates[1] = other.d->coordinates[1];
105  d->coordinates[2] = other.d->coordinates[2];
106  d->radius = other.d->radius;
107 
108  return (*this);
109 }
110 
112 
116 {
117  d->coordinates[0] += other.d->coordinates[0];
118  d->coordinates[1] += other.d->coordinates[1];
119  d->coordinates[2] += other.d->coordinates[2];
120  d->radius += other.d->radius;
121 
122  return (*this);
123 }
124 
126 
130 {
131  d->coordinates[0] -= other.d->coordinates[0];
132  d->coordinates[1] -= other.d->coordinates[1];
133  d->coordinates[2] -= other.d->coordinates[2];
134  d->radius -= other.d->radius;
135 
136  return (*this);
137 }
138 
140 
144 {
145  d->coordinates[0] *= scalar;
146  d->coordinates[1] *= scalar;
147  d->coordinates[2] *= scalar;
148  d->radius *= scalar;
149 
150  return (*this);
151 }
152 
154 
158 {
159  if(!(scalar != 0.)) {
160  qWarning() << DTK_PRETTY_FUNCTION << "Preventing division by 0. Operand not affected.";
161  return (*this);
162  }
163 
164  d->coordinates[0] /= scalar;
165  d->coordinates[1] /= scalar;
166  d->coordinates[2] /= scalar;
167  d->radius /= scalar;
168 
169  return (*this);
170 }
171 
173 
177 {
178  axlSphere result(d->coordinates[0] + other.d->coordinates[0],
179  d->coordinates[1] + other.d->coordinates[1],
180  d->coordinates[2] + other.d->coordinates[2],
181  d->radius + other.d->radius);
182  return result;
183 }
184 
186 
190 {
191  axlSphere result(d->coordinates[0] - other.d->coordinates[0],
192  d->coordinates[1] - other.d->coordinates[1],
193  d->coordinates[2] - other.d->coordinates[2],
194  d->radius - other.d->radius);
195  return result;
196 }
197 
199 
202 axlSphere axlSphere::operator*(double scalar) const
203 {
204  axlSphere result(d->coordinates[0] * scalar,
205  d->coordinates[1] * scalar,
206  d->coordinates[2] * scalar,
207  d->radius * scalar);
208  return result;
209 }
210 
212 
215 axlSphere axlSphere::operator/(double scalar) const
216 {
217  if(!(scalar != 0.)) {
218  qWarning() << DTK_PRETTY_FUNCTION << "Preventing division by 0. Returning default sphere";
219  return axlSphere();
220  }
221 
222  axlSphere result(d->coordinates[0] / scalar,
223  d->coordinates[1] / scalar,
224  d->coordinates[2] / scalar,
225  d->radius / scalar);
226 
227  return result;
228 }
229 
231 
234 double axlSphere::x(void) const
235 {
236  return d->coordinates[0];
237 }
238 
240 
243 double axlSphere::y(void) const
244 {
245  return d->coordinates[1];
246 }
247 
249 
252 double axlSphere::z(void) const
253 {
254  return d->coordinates[2];
255 }
256 
257 double axlSphere::radius(void) const
258 {
259  return d->radius;
260 }
261 
262 void axlSphere::setRadius(double radius)
263 {
264  d->radius = radius;
265 // this->touchGeometry();
266 }
267 
268 void axlSphere::touchRadius(double radius)
269 {
270  d->radius = radius;
271  this->touchGeometry();
272 }
273 
275 {
276  d->coordinates[0] = center.x();
277  d->coordinates[1] = center.y();
278  d->coordinates[2] = center.z();
279 
280  this->touchGeometry();
281 }
282 
284 {
285  axlPoint center(d->coordinates);
286 // return (new axlPoint(d->coordinates));
287  return center;
288 }
290 
293 void axlSphere::setValues(double x, double y, double z, double radius)
294 {
295  d->coordinates[0] = x;
296  d->coordinates[1] = y;
297  d->coordinates[2] = z;
298  d->radius = radius;
299  this->touchGeometry();
300 }
301 
302 void axlSphere::setCenter(double x, double y, double z)
303 {
304  d->coordinates[0] = x;
305  d->coordinates[1] = y;
306  d->coordinates[2] = z;
307  this->touchGeometry();
308 }
309 
311 {
312  double norm = sqrt(d->coordinates[0] * d->coordinates[0] + d->coordinates[1] * d->coordinates[1] + d->coordinates[2] * d->coordinates[2]);
313  d->coordinates[0] /= norm;
314  d->coordinates[1] /= norm;
315  d->coordinates[2] /= norm;
316  d->radius /= norm;
317 }
318 
319 
320 
322 
325 double *axlSphere::coordinates(void) const
326 {
327  return d->coordinates ;
328 }
329 
331 
334 double axlSphere::distance(const axlSphere& lhs, const axlSphere& rhs)
335 {
336  double dx = rhs.d->coordinates[0] - lhs.d->coordinates[0];
337  double dy = rhs.d->coordinates[1] - lhs.d->coordinates[1];
338  double dz = rhs.d->coordinates[2] - lhs.d->coordinates[2];
339 
340  return qSqrt(dx*dx + dy*dy + dz*dz);
341 }
342 
344 
348 {
349  double dx = rhs->d->coordinates[0] - lhs->d->coordinates[0];
350  double dy = rhs->d->coordinates[1] - lhs->d->coordinates[1];
351  double dz = rhs->d->coordinates[2] - lhs->d->coordinates[2];
352 
353  return qSqrt(dx*dx + dy*dy + dz*dz);
354 }
355 
356 
357 // /////////////////////////////////////////////////////////////////
358 // Debug operators
359 // /////////////////////////////////////////////////////////////////
360 
361 QDebug operator<<(QDebug dbg, axlSphere sphere)
362 {
363  dbg.nospace() << QString("axlSphere: center: (%1, %2, %3) radius : (%4)").arg(sphere.x()).arg(sphere.y()).arg(sphere.z()).arg(sphere.radius());
364 
365  return dbg.space();
366 }
367 
368 QDebug operator<<(QDebug dbg, axlSphere& sphere)
369 {
370  dbg.nospace() << QString("axlSphere: center: (%1, %2, %3) radius : (%4)").arg(sphere.x()).arg(sphere.y()).arg(sphere.z()).arg(sphere.radius());
371 
372  return dbg.space();
373 }
374 
375 QDebug operator<<(QDebug dbg, axlSphere *sphere)
376 {
377  dbg.nospace() << QString("axlSphere: center: (%1, %2, %3) radius : (%4)").arg(sphere->x()).arg(sphere->y()).arg(sphere->z()).arg(sphere->radius());
378 
379  return dbg.space();
380 }
381 
382 QString axlSphere::description(void) const
383 {
384  QString result = "axlSphere";
385  result.append("\nX : "+QString::number(d->coordinates[0]) + "\nY : "+QString::number(d->coordinates[1]) + "\nZ : "+QString::number(d->coordinates[2]) + "\nRadius : "+QString::number(d->radius));
386  return result;
387 }
388 
389 QString axlSphere::identifier(void) const
390 {
391  return "axlSphere";
392 }
393 
394 //void axlSphere::emitDataChanged(void)
395 //{
396 // emit dataChanged();
397 //}
398 
399 //SLOT
400 void axlSphere::onXChanged(double x)
401 {
402  d->coordinates[0] = x;
403 }
404 
405 void axlSphere::onYChanged(double y)
406 {
407  d->coordinates[1] = y;
408 }
409 
410 void axlSphere::onZChanged(double z)
411 {
412  d->coordinates[2] = z;
413 }
414 
415 void axlSphere::onRadiusChanged(double radius)
416 {
417  d->radius = radius;
418 }
419 
420 
421 QVariantList axlSphere::convertDataToQVariant(void) const{
422  QVariantList list;
423  QVariant id = QVariant::fromValue(identifier());
424  QVariant xcenter = QVariant::fromValue(d->coordinates[0]);
425  QVariant ycenter = QVariant::fromValue(d->coordinates[1]);
426  QVariant zcenter = QVariant::fromValue(d->coordinates[2]);
427  QVariant radius = QVariant::fromValue(d->radius);
428  list.append(id);
429  list.append(xcenter);
430  list.append(ycenter);
431  list.append(zcenter);
432  list.append(radius);
433  QVariant name = QVariant::fromValue(objectName());
434  list.append(name);
435  return list;
436 
437 }
438 
439 int axlSphere::convertQVariantToData(const QVariantList &data){
440  setCenter(data.at(1).toDouble(), data.at(2).toDouble(), data.at(3).toDouble());
441  setRadius(data.at(4).toDouble());
442  setObjectName(data.last().toString());
443  return 1;
444 }
445 
446 //to be registered to the data factory.
447 dtkAbstractData *createaxlSphere(void)
448 {
449  return new axlSphere;
450 }
451 
452 
453 // /////////////////////////////////////////////////////////////////
454 // axlSphere documentation
455 // /////////////////////////////////////////////////////////////////
456 
void touchGeometry(void)
Class axlPoint defines 3D points.
Definition: axlPoint.h:34
virtual QString identifier(void) const
Definition: axlSphere.cpp:389
~axlSphere(void)
Destroys the axel sphere.
Definition: axlSphere.cpp:90
double radius
Definition: axlSphere.h:36
axlSphere & operator/=(double scalar)
Divides the coordinates and radius of this sphere by a scalar value and returns a reference to this s...
Definition: axlSphere.cpp:157
axlPoint centerPoint(void)
Definition: axlSphere.cpp:283
void onYChanged(double y)
Definition: axlSphere.cpp:405
void setValues(double x, double y, double z, double radius)
Change coordinates of this sphere.
Definition: axlSphere.cpp:293
QVariantList convertDataToQVariant(void) const
Convert an axlAbstractData into a QVariantList that specifies all properties of the axlAbstractData...
Definition: axlSphere.cpp:421
int convertQVariantToData(const QVariantList &data)
Modify properties and geometry variables of the axlAbstractData. Return 1 if the modification was suc...
Definition: axlSphere.cpp:439
double x(void) const
Returns x-coordinate of this sphere.
Definition: axlSphere.cpp:234
double z(void) const
Returns z-coordinate of this sphere.
Definition: axlSphere.cpp:252
void setCenter(double x, double y, double z)
Definition: axlSphere.cpp:302
void touchCenter(axlPoint center)
Definition: axlSphere.cpp:274
void normalize()
Definition: axlSphere.cpp:310
axlSphere operator*(double scalar) const
Returns a sphere that results from the multiplication of the scalar with this sphere.
Definition: axlSphere.cpp:202
double radius(void) const
Definition: axlSphere.cpp:257
void onXChanged(double x)
Definition: axlSphere.cpp:400
virtual QString description(void) const
Definition: axlSphere.cpp:382
double * coordinates(void) const
Returns coordinates of this sphere.
Definition: axlSphere.cpp:325
void touchRadius(double radius)
Definition: axlSphere.cpp:268
axlSphere & operator*=(double scalar)
Multiplies the coordinates and radius of this sphere by a scalar value and returns a reference to thi...
Definition: axlSphere.cpp:143
axlSphere & operator+=(const axlSphere &other)
Adds the coordinates and radius of the other sphere to this sphere and returns a reference to this sp...
Definition: axlSphere.cpp:115
void onRadiusChanged(double radius)
Definition: axlSphere.cpp:415
void setRadius(double radius)
Definition: axlSphere.cpp:262
Class axlSphere defines 3D spheres.
Definition: axlSphere.h:33
QDebug operator<<(QDebug dbg, axlSphere sphere)
Definition: axlSphere.cpp:361
void onZChanged(double z)
Definition: axlSphere.cpp:410
axlSphere(QObject *parent=0)
Constructs a axel sphere of zero coordinates and 1.0 radius with parent parent of QObject type...
Definition: axlSphere.cpp:46
double y
Definition: axlPoint.h:37
axlSphere & operator-=(const axlSphere &other)
Substracts the coordinates and radius of the other sphere to this sphere and returns a reference to t...
Definition: axlSphere.cpp:129
static double distance(const axlSphere &lhs, const axlSphere &rhs)
Returns the distance between lhs sphere and rhs sphere.
Definition: axlSphere.cpp:334
double z
Definition: axlPoint.h:38
dtkAbstractData * createaxlSphere(void)
Definition: axlSphere.cpp:447
axlSphere operator/(double scalar) const
Returns a sphere that results from the division of the scalar with this sphere.
Definition: axlSphere.cpp:215
double y(void) const
Returns y-coordinate of this sphere.
Definition: axlSphere.cpp:243
double x
Definition: axlPoint.h:37
axlSphere operator-(const axlSphere &other) const
Returns a sphere that results from the substraction of this sphere and the other sphere.
Definition: axlSphere.cpp:189
axlSphere operator+(const axlSphere &other) const
Returns a sphere that results from the addition of this sphere and the other sphere.
Definition: axlSphere.cpp:176
Class axlAbstractData defines an API for all type of axel data.
axlSphere & operator=(const axlSphere &other)
Assigns other to this sphere and returns a reference to this sphere.
Definition: axlSphere.cpp:101
axlPoint center
Definition: axlSphere.h:36