xc
Vector2d.h
1 // -*-c++-*-
2 //----------------------------------------------------------------------------
3 // xc utils library; general purpose classes and functions.
4 //
5 // Copyright (C) Luis C. Pérez Tato
6 //
7 // XC utils is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // This software is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program.
19 // If not, see <http://www.gnu.org/licenses/>.
20 //----------------------------------------------------------------------------
21 //Vector2d.h
22 
23 #ifndef VECTOR2D_H
24 #define VECTOR2D_H
25 
26 #include "utility/utils/misc_utils/mchne_eps.h"
27 #include "utility/matrices/op_tensor.h"
28 #include "../cgal_types.h"
29 #include "../ProtoGeom.h"
30 #include <boost/iterator/iterator_facade.hpp>
31 
32 class Pos2d;
33 class Dir2d;
34 class Plotter;
35 class FT_matrix;
36 
37 
39 //
41 class Vector2d: public ProtoGeom
42  {
43  CGVector_2 cgvct;
44  public:
45  Vector2d(void): ProtoGeom(), cgvct(CGAL::NULL_VECTOR) {}
46  explicit Vector2d(const CGVector_2 &v)
47  : ProtoGeom(), cgvct(v) {}
48  explicit Vector2d(const CGDirection_2 &dir)
49  : ProtoGeom(), cgvct(dir.vector()) {}
50  Vector2d(const GEOM_FT &x,const GEOM_FT &y);
51  explicit Vector2d(const boost::python::list &);
52 /* Vector2d(const double &x,const double &y); */
53  explicit Vector2d(const FT_matrix &m);
54  Vector2d(const Pos2d &p1,const Pos2d &p2);
55 
56  virtual bool operator==(const Vector2d &) const;
57  bool operator!=(const Vector2d &) const;
58  const CGVector_2 &ToCGAL(void) const
59  { return cgvct; }
60  inline int dimension(void) const
61  { return cgvct.dimension(); }
62  void Neg(void)
63  { cgvct= -cgvct; }
64  inline Vector2d operator-(void) const
65  {
66  Vector2d retval(*this);
67  retval.Neg();
68  return retval;
69  }
70  bool Nulo(void) const;
71  void SetX(const GEOM_FT &vx);
72  void SetY(const GEOM_FT &vy);
73  void Set(unsigned short int i,const GEOM_FT &v);
74 
75  inline GEOM_FT at(const size_t &i) const
76  { return cgvct.cartesian(i); }
77  inline GEOM_FT operator()(const size_t &i) const //Base 1.
78  { return at(i-1); }
79  inline GEOM_FT operator[](const size_t &j) const //Base 0.
80  { return at(j); }
81  inline GEOM_FT x() const
82  { return Vector2d::operator()(1); }
83  inline GEOM_FT y() const
84  { return Vector2d::operator()(2); }
85  FT_matrix getMatrix(void) const;
86  boost::python::list getPyList(void) const;
87  bool notAVector(void) const;
88  Vector2d &operator+=(const Vector2d &);
89  Vector2d &operator-=(const Vector2d &);
90  Vector2d operator+(const Vector2d &) const;
91  Vector2d operator-(const Vector2d &) const;
92  Vector2d operator*(const GEOM_FT &) const;
93  Vector2d &operator*=(const GEOM_FT &);
94  Vector2d operator/(const GEOM_FT &) const;
95  Vector2d &operator/=(const GEOM_FT &);
96 
97  inline virtual GEOM_FT GetModulus2(void) const
98  { return (x()*x()+y()*y()); }
99  virtual GEOM_FT GetModulus(void) const;
100  bool EsUnitario(const double &tol= mchne_eps_dbl) const;
101 
103  inline Vector2d getNormalized(void) const
104  { return (*this)/GetModulus(); }
105  void Normalize(void);
106  int getIndexMaxValue(void) const;
107  int getIndexMinValue(void) const;
108  int getIndexMaxAbsValue(void) const;
109  int getIndexMinAbsValue(void) const;
110  Dir2d getDirection(void) const;
111  GEOM_FT getSignedAngle(const Vector2d &v) const;
112  GEOM_FT getAngle(const Vector2d &v) const;
113  GEOM_FT XAxisAngle(void) const;
114  GEOM_FT YAxisAngle(void) const;
115 
116  //Producto escalar.
117  inline virtual GEOM_FT GetDot(const Vector2d &v) const
118  { return (x()*v.x()+y()*v.y()); }
119  virtual GEOM_FT GetDot(const FT_matrix &m) const;
120  inline friend GEOM_FT dot(const Vector2d &v1, const Vector2d &v2)
121  { return v1.GetDot(v2); }
122  friend GEOM_FT dot(const Vector2d &v1, const FT_matrix &m);
123  friend GEOM_FT dot(const FT_matrix &m, const Vector2d &v1);
124 
125  friend FT_matrix operator*(const FT_matrix &m,const Vector2d &v);
126 
127  //void Rotate(const GEOM_FT &ang_rad);
128  Vector2d Rotated(const double &ang_rad) const;
129  Vector2d Normal(void) const;
130  Vector2d Perpendicular(const Orientacion &o) const;
131  Vector2d Perpendicular(const Vector2d &v) const;
132 
133  boost::python::dict getPyDict(void) const;
134  void setPyDict(const boost::python::dict &);
135 
136  void Print(std::ostream &stream) const;
137  void Plot(Plotter &psos) const;
138  friend std::ostream &operator<<(std::ostream &stream,const Vector2d &n);
139  inline virtual ~Vector2d(void) {}
140 
141  struct iterator : boost::iterator_facade<iterator, int, boost::single_pass_traversal_tag, GEOM_FT>
142  {
143  private:
144  const Vector2d *v_ptr;
145  int current_;
146  public:
147  iterator(const Vector2d *v, int i)
148  : v_ptr(v), current_(i) {}
149 
150  bool equal(iterator const& other) const
151  { return ((v_ptr==other.v_ptr) && (current_ == other.current_)); }
152  GEOM_FT dereference() const
153  { return v_ptr->at(current_); }
154  void increment()
155  { ++current_; }
156  };
157 
158  iterator begin() { return iterator(this, 0); }
159  iterator end() { return iterator(this, 2); }
160  };
161 
162 inline GEOM_FT Abs2(const Vector2d &v)
163  { return v.GetModulus2(); }
164 inline GEOM_FT Abs(const Vector2d &v)
165  { return v.GetModulus(); }
166 double signedAngle(const Vector2d &v1,const Vector2d &v2);
167 double angle(const Vector2d &v1,const Vector2d &v2);
168 
170 inline Vector2d operator*(const GEOM_FT &d,const Vector2d &v)
171  { return v*d; }
173 inline Vector2d normalize(const Vector2d &v)
174  { return v.getNormalized(); }
175 
176 //Producto tensorial.
177 FT_matrix prod_tensor(const Vector2d &u,const Vector2d &v);
178 FT_matrix operator&(const Vector2d &u,const Vector2d &v);
179 
180 bool colineales(const Vector2d &v1,const Vector2d &v2);
181 bool parallel(const Vector2d &v1,const Vector2d &v2);
182 
183 const Vector2d VectorNulo2d;
184 
185 #endif
Dirección en dos dimensiones.
Definition: Dir2d.h:36
Vector2d Normal(void) const
Return a vector normal to this one.
Definition: Vector2d.cc:286
virtual GEOM_FT GetModulus(void) const
Return el módulo del vector.
Definition: Vector2d.cc:248
Vector2d operator/(const GEOM_FT &) const
Return the product of the vector and the inverse of the given scalar.
Definition: Vector2d.cc:138
Vector2d operator*(const GEOM_FT &) const
Return the product of the vector and the given scalar.
Definition: Vector2d.cc:128
Posición en dos dimensiones.
Definition: Pos2d.h:41
Definition: Vector2d.h:141
int getIndexMaxValue(void) const
Returns the index of the maximum of the values of the components.
Definition: Vector2d.cc:170
Vector2d Rotated(const double &ang_rad) const
Return the vector that results from rotating this one by the given angle.
Definition: Vector2d.cc:277
GEOM_FT getSignedAngle(const Vector2d &v) const
Return the angle with the vector argument.
Definition: Vector2d.cc:216
int getIndexMinValue(void) const
Returns the index of the maximum of the values of the components.
Definition: Vector2d.cc:181
Vector2d getNormalized(void) const
Return el versor (vector de módulo unidad) correspondiente a éste vector.
Definition: Vector2d.h:103
void Normalize(void)
Normalize vector.
Definition: Vector2d.cc:165
Vector2d Perpendicular(const Orientacion &o) const
Return a vector perpendicular to this one with the given orientation.
Definition: Vector2d.cc:291
Vector en dos dimensiones.
Definition: Vector2d.h:41
bool notAVector(void) const
Return true if one of the coordinate components is not a number.
Definition: Vector2d.cc:235
FT_matrix getMatrix(void) const
Return the point coordinates in a matrix.
Definition: Vector2d.cc:148
ProtoGeom(void)
False when solution is not possible: intersection of parallel planes.
Definition: ProtoGeom.cc:33
void setPyDict(const boost::python::dict &)
Set the values of the object members from a Python dictionary.
Definition: Vector2d.cc:339
Base class for geometry objects.
Definition: ProtoGeom.h:33
boost::python::dict getPyDict(void) const
Return a Python dictionary with the object members values.
Definition: Vector2d.cc:330
GEOM_FT getAngle(const Vector2d &v) const
Return the angle with the argument vector.
Definition: Vector2d.cc:220
Vector2d operator+(const Vector2d &) const
Return la suma de los vectores.
Definition: Vector2d.cc:120
int getIndexMaxAbsValue(void) const
Returns the index of the maximum of the values of the components.
Definition: Vector2d.cc:192
virtual bool operator==(const Vector2d &) const
Comparison operator.
Definition: Vector2d.cc:65
boost::python::list getPyList(void) const
Return the point coordinates in a Python list.
Definition: Vector2d.cc:156
int getIndexMinAbsValue(void) const
Returns the index of the maximum of the values of the components.
Definition: Vector2d.cc:203
Matrix which components are GEOM_FT numbers.
Definition: FT_matrix.h:40