vgl_vector_2d.hxx
Go to the documentation of this file.
1 // This is core/vgl/vgl_vector_2d.hxx
2 #ifndef vgl_vector_2d_hxx_
3 #define vgl_vector_2d_hxx_
4 //:
5 // \file
6 
7 #include <cmath>
8 #include <iostream>
9 #include <string>
10 #include "vgl_vector_2d.h"
11 
12 #ifdef _MSC_VER
13 # include <vcl_msvc_warnings.h>
14 #endif
15 
16 template <class T>
18 {
19  return std::sqrt( 0.0+sqr_length() );
20 }
21 
22 template<class T>
23 double angle(vgl_vector_2d<T> const& a, vgl_vector_2d<T> const& b)
24 {
25  return std::acos(cos_angle(a,b));
26 }
27 
28 
29 template<class T>
31 {
32  return std::atan2(double(cross_product(a, b)), double(dot_product(a, b)));
33 }
34 
35 
36 template <class T>
37 bool orthogonal(vgl_vector_2d<T> const& a, vgl_vector_2d<T> const& b, double eps)
38 {
39  T dot = dot_product(a,b); // should be zero
40  if (eps <= 0 || dot == T(0)) return dot == T(0);
41  eps *= eps * a.sqr_length() * b.sqr_length();
42  dot *= dot;
43  return dot < eps;
44 }
45 
46 template <class T>
47 bool parallel(vgl_vector_2d<T> const& a, vgl_vector_2d<T> const& b, double eps)
48 {
49  T cross = cross_product(a,b); // should be zero
50  if (eps <= 0 || cross == T(0)) return cross == T(0);
51  eps *= eps * a.sqr_length() * b.sqr_length();
52  return cross*cross < eps;
53 }
54 
55 
56 template <class T>
58 {
59  return vgl_vector_2d<T>( T(std::cos(angle)*a.x()-std::sin(angle)*a.y()),
60  T(std::sin(angle)*a.x() + std::cos(angle)*a.y()) );
61 }
62 
63 
64 //: Write "<vgl_vector_2d x,y> " to stream
65 template <class T>
66 std::ostream& operator<<(std::ostream& s, vgl_vector_2d<T> const& p)
67 {
68  return s << "<vgl_vector_2d "<< p.x() << ',' << p.y() << "> ";
69 }
70 
71 //: Read from stream, possibly with formatting
72 // Either just reads two blank-separated numbers,
73 // or reads two comma-separated numbers,
74 // or reads two numbers in parenthesized form "(123, 321)"
75 // Also can read the form "<vgl_vector_2d x,y>"
76 template <class T>
77 std::istream& vgl_vector_2d<T>::read(std::istream& is)
78 {
79  if (! is.good()) return is; // (TODO: should throw an exception)
80  bool paren = false;
81  T tx, ty;
82  is >> std::ws; // jump over any leading whitespace
83  if (is.eof()) return is; // nothing to be set because of EOF (TODO: should throw an exception)
84  char c = is.peek();
85  if (c == '(') { is.ignore(); paren=true; }
86  if(paren){
87  is >> std::ws >> tx >> std::ws;
88  if (is.eof()) return is;
89  if (is.peek() == ',') is.ignore();
90  is >> std::ws >> ty >> std::ws;
91  if (is.eof()) return is;
92  if (is.peek() == ')') is.ignore();
93  else return is; // closing parenthesis is missing (TODO: throw an exception)
94  }else if(c == '<'){
95  std::string temp;
96  is >> temp >> std::ws; // read <vgl_vector_2d
97  is >> tx >> std::ws;
98  c = is.peek();
99  if(c != ','){
100  std::cout << "Invalid syntax: >> vgl_vector_2d" << std::endl;
101  set(0.0, 0.0);
102  return is;
103  }else is.ignore();
104  is >> ty>>std::ws;
105  if(is.peek() != '>'){
106  std::cout << "Invalid syntax: >> vgl_vector_2d" << std::endl;
107  set(0.0, 0.0);
108  return is;
109  }else is.ignore();
110  }else{
111  is >> tx >> std::ws;
112  c = is.peek();
113  if(c == ',') is.ignore();
114  is >> std::ws >> ty;
115  }
116  set(tx,ty);
117  return is;
118 }
119 
120 //: Read x y from stream
121 template <class T>
122 std::istream& operator>>(std::istream& is, vgl_vector_2d<T>& p)
123 {
124  return p.read(is);
125 }
126 
127 
128 #undef VGL_VECTOR_2D_INSTANTIATE
129 #define VGL_VECTOR_2D_INSTANTIATE(T) \
130 template class vgl_vector_2d<T >;\
131 /*template vgl_vector_2d<T > operator+ (vgl_vector_2d<T > const&, vgl_vector_2d<T > const&); */\
132 /*template vgl_vector_2d<T > operator- (vgl_vector_2d<T > const&, vgl_vector_2d<T > const&); */\
133 /*template vgl_vector_2d<T >& operator+= (vgl_vector_2d<T >&, vgl_vector_2d<T > const&); */\
134 /*template vgl_vector_2d<T >& operator-= (vgl_vector_2d<T >&, vgl_vector_2d<T > const&); */\
135 /*template vgl_vector_2d<T > operator+ (vgl_vector_2d<T > const&); */\
136 /*template vgl_vector_2d<T > operator- (vgl_vector_2d<T > const&); */\
137 /*template vgl_vector_2d<T > operator* (double, vgl_vector_2d<T > const&); */\
138 /*template vgl_vector_2d<T > operator* (vgl_vector_2d<T > const&, double); */\
139 /*template vgl_vector_2d<T > operator/ (vgl_vector_2d<T > const&, double); */\
140 /*template vgl_vector_2d<T >& operator*= (vgl_vector_2d<T >&, double); */\
141 /*template vgl_vector_2d<T >& operator/= (vgl_vector_2d<T >&, double); */\
142 /*template T dot_product (vgl_vector_2d<T > const&, vgl_vector_2d<T > const&); */\
143 /*template T inner_product(vgl_vector_2d<T > const&, vgl_vector_2d<T > const&); */\
144 /*template T cross_product(vgl_vector_2d<T > const&, vgl_vector_2d<T > const&); */\
145 /*template double cos_angle (vgl_vector_2d<T > const&, vgl_vector_2d<T > const&); */\
146 template double angle (vgl_vector_2d<T > const&, vgl_vector_2d<T > const&);\
147 template double signed_angle (vgl_vector_2d<T > const&, vgl_vector_2d<T > const&);\
148 template bool orthogonal (vgl_vector_2d<T > const&, vgl_vector_2d<T > const&, double);\
149 template bool parallel (vgl_vector_2d<T > const&, vgl_vector_2d<T > const&, double);\
150 /*template double operator/ (vgl_vector_2d<T > const&, vgl_vector_2d<T > const&); */\
151 /*template vgl_vector_2d<T >& normalize (vgl_vector_2d<T >&); */\
152 /*template vgl_vector_2d<T > normalized (vgl_vector_2d<T > const&); */\
153 template vgl_vector_2d<T > rotated (vgl_vector_2d<T > const&, double);\
154 template std::ostream& operator<< (std::ostream&, vgl_vector_2d<T >const&);\
155 template std::istream& operator>> (std::istream&, vgl_vector_2d<T >&)
156 
157 #endif // vgl_vector_2d_hxx_
std::istream & read(std::istream &is)
Read from stream, possibly with formatting.
T dot_product(v const &a, v const &b)
dot product or inner product of two vectors.
Direction vector in Euclidean 2D space, templated by type of element.
Definition: vgl_fwd.h:12
T sqr_length() const
Return the squared length of this vector.
Definition: vgl_vector_2d.h:65
T y() const
Definition: vgl_vector_2d.h:36
bool orthogonal(v const &a, v const &b, double eps=0.0)
are two vectors orthogonal, i.e., is their dot product zero?.
double length() const
Return the length of this vector.
std::ostream & operator<<(std::ostream &s, vgl_orient_box_3d< Type > const &p)
Write box to stream.
v rotated(v const &a, double angle)
Return a CCW rotated version of a (angle in radian).
#define dot(p, q)
T cross_product(v const &a, v const &b)
cross product of two vectors (area of enclosed parallellogram).
double signed_angle(v const &a, v const &b)
signed angle between two vectors (in radians, between -Pi and Pi).
direction vector in Euclidean 2D space
std::istream & operator>>(std::istream &is, vgl_orient_box_3d< Type > &p)
Read box from stream.
T sqr_length(v const &a)
Return the squared length of a vector.
Definition: vgl_vector_2d.h:98
double angle(v const &a, v const &b)
smallest angle between two vectors (in radians, between 0 and Pi).
T x() const
Definition: vgl_vector_2d.h:35
bool parallel(v const &a, v const &b, double eps=0.0)
are two vectors parallel, i.e., is one a scalar multiple of the other?.
double cos_angle(v const &a, v const &b)
cosine of the angle between two vectors.