ISeeML  1.0
Vector.hpp
1 //
2 // Definition of iSeeML::geom::Vector, class of ISeeML' 2D geometric
3 // vectors.
4 //
5 
6 // For Borland C++
7 #ifdef __BORLANDC__
8 #pragma warn -8022 // to remove some stupid warnings
9 #endif
10 
11 // To only include this definition once
12 #ifndef ISEEML_GEOM_VECTOR_HH
13 #define ISEEML_GEOM_VECTOR_HH
14 
15 
16 // This class needs the definition of iSeeML::geom::BasicObject.
17 #include <iSeeML/geom/BasicObject.hpp>
18 
19 
35  // === Object's Fields ==========================================
36 
37  double XCoord;
38  double YCoord;
39 
40 public:
45  static const string ClassName;
46 
47 
48  // === Constructors and Destructors =============================
49 
53  Vector() : XCoord(0), YCoord(0) {}
54 
66  Vector(const double& theta) :
67  XCoord( cos(theta) ), YCoord( sin(theta) ) {}
68 
77  Vector(const double& x, const double& y) :
78  XCoord(x), YCoord(y) {}
79 
80  // The default copy constructor is OK.
81 
82 
83  // === Description Methods ======================================
84 
93  { return *( (iSeeML::Object*) (new Vector(*this)) ); }
94 
95 
103  void writeTo(ostream& O) const { algWriteTo(O); }
104 
105 
111  const double& xCoord() const { return XCoord; }
112 
118  const double& yCoord() const { return YCoord; }
119 
120 
137  double orientation() const
138  { return
139  // if XCoord is positive, direct computation using atan
140  ( isPositive(XCoord) ? atan(YCoord / XCoord) :
141  // if XCoord is negative, atan value is corrected
142  // wrt YCoord precise sign
143  isNegative(XCoord) ? ( YCoord < 0 ?
144  atan(YCoord / XCoord) - M_PI :
145  atan(YCoord / XCoord) + M_PI ) :
146  // if XCoord is zero, result can be 0 or +/- pi / 2
147  sign(YCoord) * M_PI_2 ); }
148 
159  double length() const { return sqrt(sqrLength()); }
160 
169  double sqrLength() const { return( (*this) * (*this) ); }
170 
171 protected:
180  int algDimension() const { return 2; }
181 
196  double algCoord(const int i) const {
197  switch (i) { case 1 : return xCoord();
198  case 2 : return yCoord(); default:
199 #ifdef ISEEML_CHECK_GEOM_VECT_PRECOND
200  cerr << ClassName << "::coord: parameter " << i << endl
201  << " outside allowed values (1 or 2), returning zero..."
202  << endl;
203 #endif
204  return 0; } }
205 
206 public:
207  // === Modification Methods =====================================
208 
220  Vector& moveTo(const double& x, const double& y)
221  { XCoord = x; YCoord = y; return *this; }
222 
234  Vector& translate(const Vector& v) { return( add(v) ); }
235 
236 
245  Vector& rotate(const double& theta) {
246  const double x = XCoord, y = YCoord,
247  ct = cos(theta), st = sin(theta);
248  XCoord = x * ct - y * st; YCoord = x * st + y * ct;
249  return *this; }
250 
251 
261  Vector& add(const Vector& v)
262  { XCoord += v.xCoord(); YCoord += v.yCoord(); return *this; }
263 
264 
274  Vector& multiply(const double& f)
275  { XCoord *= f; YCoord *= f; return *this; }
276 
292  Vector& divide(const double& f) {
293 #ifdef ISEEML_CHECK_GEOM_VECT_PRECOND
294  if ( isZero(f) )
295  cerr << ClassName << "::divide: zero factor!!!" << endl;
296 #endif
297  return multiply(1 / f); }
298 
299 
305  Vector& symmetryOx() { YCoord *= -1; return *this; }
306 
312  Vector& symmetryOy() { XCoord *= -1; return *this; }
313 
314 
315  // === Operators ================================================
316 
326  bool operator==(const Vector& other) const
327  { return( isZero( xCoord() - other.xCoord() ) &&
328  isZero( yCoord() - other.yCoord() ) ); }
329 
330 
340  Vector operator+(const Vector& v) const
341  { Vector res(*this); return res.add(v); }
342 
352  Vector operator-(const Vector& v) const
353  { return( *this + (-v) ); }
354 
355 
364  { Vector res(*this); return( res.multiply(-1) ); }
365 
366 
376  Vector operator*(const double& f) const
377  { Vector res(*this); return res.multiply(f); }
378 
388  friend Vector operator*(const double& f, const Vector& v)
389  { return(v * f); }
390 
402  Vector operator/(const double& f) const
403  { Vector res(*this); return res.divide(f); }
404 
416  friend Vector operator/(const double& f, const Vector& v)
417  { return(v / f); }
418 
419 
427  double operator*(const Vector& v) const
428  { return( xCoord() * v.xCoord() + yCoord() * v.yCoord() ); }
429 
437  double operator^(const Vector& v) const
438  { return( xCoord() * v.yCoord() - yCoord() * v.xCoord() ); }
439 
440 
441  // === Various Methods ==========================================
442 
457  static double FresnelCos(const double& s)
458  { return( FresnelInt(s).xCoord() ); }
459 
474  static double FresnelSin(const double& s)
475  { return( FresnelInt(s).yCoord() ); }
476 
503  static Vector FresnelInt(const double& s);
504 
505 }; // end of class iSeeML::geom::Vector
506 
507 #endif // end of definition
508 
509 
Vector operator*(const double &f) const
Multiplication operator between a vector and a real.
Definition: Vector.hpp:376
This class defines 2D geometric vectors, which can be defined by their Polar or Cartesian coordinates...
Definition: Vector.hpp:34
This class is the base class of ISeeML: every class of ISeeML inherits this one.
Definition: Object.hpp:47
Vector(const double &theta)
A simple constructor, creating the unit vector of given orientation.
Definition: Vector.hpp:66
double sqrLength() const
Description method, giving the square of the vector&#39;s length.
Definition: Vector.hpp:169
double operator^(const Vector &v) const
Vectorial product operator between two vectors.
Definition: Vector.hpp:437
static int sign(const double &x)
Method giving the sign of a double.
Definition: Object.hpp:301
Vector & rotate(const double &theta)
Modification method, rotating the vector of a given angle.
Definition: Vector.hpp:245
bool operator==(const Vector &other) const
Equality operator between vectors (differences between both Cartesian coordinates should be zero)...
Definition: Vector.hpp:326
This class defines basic 2D geometric objects, from which compound geometric objects are built...
Definition: BasicObject.hpp:21
Vector()
The default constructor, creating the zero vector with Cartesian coordinates (0,0).
Definition: Vector.hpp:53
Vector & moveTo(const double &x, const double &y)
Modification method, moving the point to a given position.
Definition: Vector.hpp:220
Vector & translate(const Vector &v)
Modification method, translating the vector along an other.
Definition: Vector.hpp:234
static Vector FresnelInt(const double &s)
Various method, computing the Fresnel integrals in a vector.
iSeeML::Object & clone() const
Description method, giving a copy of the current vector.
Definition: Vector.hpp:92
virtual void algWriteTo(ostream &O) const
Description method for algebraic vectors, writing this vector in a given output stream: coordinates f...
Definition: Object.hpp:161
void writeTo(ostream &O) const
Description method, writing a description of the vector into a stream: Cartesian coordinate in each d...
Definition: Vector.hpp:103
int algDimension() const
Description method, giving the dimension of the containing space (2) when this vector is considered a...
Definition: Vector.hpp:180
Vector & multiply(const double &f)
Modification method, multiplying a vector by a real factor.
Definition: Vector.hpp:274
friend Vector operator*(const double &f, const Vector &v)
Multiplication operator between a real and a vector.
Definition: Vector.hpp:388
static bool isPositive(const double &x)
Method telling whether a double is strictly positive.
Definition: Object.hpp:264
Vector operator/(const double &f) const
Division operator between a vector and a real.
Definition: Vector.hpp:402
static const string ClassName
The class name is public, as this class can be instanced.
Definition: Vector.hpp:45
static bool isNegative(const double &x)
Method telling whether a double is strictly negative.
Definition: Object.hpp:278
Vector & add(const Vector &v)
Modification method, adding a vector to the current one.
Definition: Vector.hpp:261
const double & xCoord() const
Description method, giving the vector&#39;s first Cartesian coordinate.
Definition: Vector.hpp:111
friend Vector operator/(const double &f, const Vector &v)
Division operator between a real and a vector.
Definition: Vector.hpp:416
double length() const
Description method, giving the vector&#39;s length.
Definition: Vector.hpp:159
Vector operator+(const Vector &v) const
Sum operator between two vectors, giving the translation of the first one by the other.
Definition: Vector.hpp:340
static double FresnelCos(const double &s)
Various method, computing the Fresnel Cosine integral.
Definition: Vector.hpp:457
Vector(const double &x, const double &y)
The main constructor, creating a vector from its Cartesian coordinates.
Definition: Vector.hpp:77
double orientation() const
Description method, giving the vector&#39;s orientation.
Definition: Vector.hpp:137
Vector & divide(const double &f)
Division method, dividing a vector by a real factor.
Definition: Vector.hpp:292
Vector operator-(const Vector &v) const
Difference operator between two vectors, giving the sum of the first vector and of the second one&#39;s o...
Definition: Vector.hpp:352
double algCoord(const int i) const
Description method, giving Cartesian coordinate of given index for the vector.
Definition: Vector.hpp:196
static double FresnelSin(const double &s)
Various method, computing the Fresnel Sine integral.
Definition: Vector.hpp:474
const double & yCoord() const
Description method, giving the vector&#39;s second Cartesian coordinate.
Definition: Vector.hpp:118
static bool isZero(const double &x)
Method comparing a double to zero.
Definition: Object.hpp:288
double operator*(const Vector &v) const
Scalar product operator between two vectors.
Definition: Vector.hpp:427
Vector & symmetryOx()
Modification method, transforming a vector into its symmetric with respect to the X axis...
Definition: Vector.hpp:305
Vector & symmetryOy()
Modification method, transforming a vector into its symmetric with respect to the Y axis...
Definition: Vector.hpp:312
Vector operator-() const
Opposite operator for a vector, such that the sum of the vector and of its opposite is the zero vecto...
Definition: Vector.hpp:363