source: GTP/trunk/Lib/Vis/Preprocessing/src/Vector2.h @ 1975

Revision 1975, 5.5 KB checked in by bittner, 18 years ago (diff)

gauss sampling

Line 
1 // ===================================================================
2// $Id: vector2.h,v 1.5 2005/11/02 12:58:48 xkrivanj Exp $
3//
4// vector2.h
5//     Header file for CVector2D class - implements 2-dimensional vector
6//
7// Class: CVector2D
8//
9// Licence: the use and distribution of this file is severely limited, please
10// see the file 'doc/Licence.txt'. Any non-authorized use can be prosecuted under
11// International Law. For further questions, please, e-mail to VHavran@seznam.cz
12// or mail to Vlastimil Havran, Pohodli 27, 57001 Litomysl, the Czech Republic.
13// REPLACEMENT_STRING
14//
15// Initial coding by Jiri Bittner
16
17#ifndef __VECTOR2_H__
18#define __VECTOR2_H__
19
20// #include "basmacr.h"
21//#include "basmath.h"
22#include "common.h"
23#include <iostream>
24using namespace std;
25
26namespace GtpVisibilityPreprocessor {
27
28// ---------------------------------------------------------------------------
29// This class describes two-dimensional vector.
30// ---------------------------------------------------------------------------
31class Vector2
32{
33public:
34  float xx, yy; // coordinates of the vector
35
36  // constructors
37  Vector2(float xs, float ys):xx(xs), yy(ys) {}
38  // default constructor
39  Vector2() {}
40 
41  friend ostream& operator<< (ostream &s, const Vector2 &A);
42  friend istream& operator>> (istream &s, Vector2 &A);
43
44  const   float&   x() const { return xx; }
45  const   float&   y() const { return yy; }
46  float&   x() {return xx;}
47  float&   y() {return yy;}
48 
49  // Functions to get at the vector components
50  float& operator[] (int inx) {
51    if (inx == 0) return xx;
52    else return yy;
53  }
54
55  const float& operator[] (int inx) const {
56    if (inx == 0) return xx;
57    else return yy;
58  }
59
60  // returns x-coordinate for which==0, x-coordinate for which==1
61  void     ExtractVerts(float *p, int which) const;
62
63  float&   SetX(const float x) {return xx = x;}
64  float&   SetY(const float y) {return yy = y;}
65
66  Vector2& operator=(Vector2 const &v) {
67    xx = v.x(); yy = v.y(); return *this;
68  }
69
70  void   SetValue(float xs, float ys ) { xx = xs; yy = ys; }
71
72  float  Size() const { return (float) sqrt(xx*xx + yy*yy);}
73
74  // returns the squared magnitude of a vector
75  friend inline float SqrMagnitude(const Vector2 &v);
76
77  // returns the squared distance between two vectors
78  friend inline float SqrDistance(const Vector2 &v1, const Vector2 &v2);
79 
80  // normalize the vector to the size=1.0
81  float    Normalize();
82 
83  // Assignment operators
84  Vector2& operator+= (const Vector2 &a);
85  Vector2& operator-= (const Vector2 &a);
86  Vector2& operator*= (const Vector2 &a);
87  Vector2& operator*= (float a);
88  Vector2& operator/= (float a);
89
90  Vector2 operator-(const Vector2 &) const;
91  Vector2 operator+(const Vector2 &) const;
92  Vector2 operator*(const float t) const;
93
94  //  Vector2 operator*(const Matrix3C &mat) const;
95  Vector2 operator-() const { return Vector2(-x(),-y()); }
96
97  int  operator==(const Vector2 &u) const {return Equal(u,Limits::Small);}
98  int  operator!=(const Vector2 &u) const {return !operator == (u);}
99
100  int  Equal(const Vector2 &u,float trash) const;
101
102  // dot product of the two vectors
103  friend inline double DotProd(const Vector2 &u,
104                               const Vector2 &v) {
105    return ( u.x() * v.x()  +  u.y() * v.y() );
106  }
107
108  // the angle between two vectors $\in <0, PI>$
109  float  Angle(const Vector2 &v) const;
110  // cosine of the angle between the two vectors $\in <-1,1>$
111  float  Cosine(const Vector2 &v) const;
112  // supposes this vector is normalized
113  float  CosineN(const Vector2 &v) const;
114
115  // checks if the this vector is not opposite to a given vector
116  int    IsOpposite(const Vector2 &v) const {
117    return (Abs(DotProd(*this,v) + Size() * v.Size()) < Limits::Small);
118  }
119
120  // computes the distance between this and a given vector
121  float  Distance(const Vector2 &v) const {
122    return  (float) sqrt( sqr(xx-v.x()) + sqr(yy - v.y()));
123  }
124
125  // computes the squared distance between this and a given vector
126  float  SqrDistance(const Vector2 &v) const {
127    return  sqr(x()-v.x())+sqr(y()-v.y());
128  }
129
130  // if a given vector has a smaller(larger) coordinate, then this is updated
131  Vector2&  UpdateMin(const Vector2 &v);
132  Vector2&  UpdateMax(const Vector2 &v);
133
134  // checks if both coordinates are smaller than a given one
135  int operator<=(Vector2 &v) {
136    return x() < (v.x() + Limits::Small)
137      && y() < (v.y()+Limits::Small);
138  }
139
140  // checks if both coordinates are larger than a given one
141  int operator>=(Vector2 &v) {
142    return x() > (v.x()-Limits::Small) &&
143      y() > (v.y()-Limits::Small);
144  }
145
146  // returns which coordinates has larger size
147  int DominantAxis();
148};
149
150const Vector2 ZeroVector2(0,0);
151
152inline Vector2&
153Vector2::operator+= (const Vector2 &a)
154{
155  xx += a.xx;  yy += a.yy;
156  return *this;
157}
158
159inline Vector2&
160Vector2::operator-= (const Vector2 &a)
161{
162  xx -= a.xx;  yy -= a.yy;
163  return *this;
164}
165
166inline Vector2&
167Vector2::operator*= (float a)
168{
169  xx *= a;  yy *= a;
170  return *this;
171}
172
173inline Vector2&
174Vector2::operator/= (float a)
175{
176  xx /= a;  yy /= a;
177  return *this;
178}
179
180inline Vector2&
181Vector2::operator*= (const Vector2 &a)
182{
183  xx *= a.xx;  yy *= a.yy;
184  return *this;
185}
186
187inline float
188SqrMagnitude(const Vector2 &v)
189{
190  return v.x() * v.x() + v.y() * v.y();
191}
192
193inline float
194SqrDistance(const Vector2 &v1, const Vector2 &v2)
195{
196  return sqr(v1.xx - v2.xx) + sqr(v1.yy - v2.yy);
197}
198
199}
200
201#endif // __VECTOR2_H__
202
Note: See TracBrowser for help on using the repository browser.