Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Vector2.cpp
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5 Multimediale Systeme der Informationsverarbeitung
6 Institut fuer Informatik
7 Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 #include <Base/Math/Vector2.hh>
26 #include <Base/Math/Vector.hh>
27 #include <Base/Math/Matrix2x2.hh>
28 #include <Base/Math/Utils.hh>
29 #include <Base/Common/W32Compat.hh>
30 #include <iostream>
31 #include <fstream>
32 
33 //additional math constants are not used by default in WIN32 - so set usage
34 
35 using namespace BIAS;
36 using namespace std;
37 
38 namespace BIAS {
39 
40  template<class T>
42  {
43  // check if v has correct size:
44  BIASASSERT(v.size() == VECTOR2SIZE);
45  // set the 2 elements:
46  data_[0] = v[0];
47  data_[1] = v[1];
48  }
49 
50 
51  // JW 09/2003 - default: not implemented template < class T >
52  template<class T>
55  {
56  BIASERR("CoordPolarToEuclidean not implemented for this template type T - but e.g. for double");
57 #ifdef BIAS_DEBUG
58  BIASABORT; // HACK
59 #endif
60  return Vector2<T>(0,0);
61  }
62 
63 
64  // JW
65  template <>
68  {
69  // for clarity symbolic names:
70  const double r = (*this)[0];
71  const double phi = (*this)[1];
72 
73  // polar to euclidean
74  const double xx( r * cos(phi) );
75  const double yy( r * sin(phi) );
76 
77  return Vector2<double>( xx, yy );
78  }
79 
80 
81  // JW 09/2003 - default: not implemented
82  template < class T >
85  {
86  BIASERR("CoordEuclideanToPolar not implemented for this template type T - but e.g. for double");
87 #ifdef BIAS_DEBUG
88  BIASABORT; // HACK
89 #endif
90  return Vector2<T>(0,0);
91  }
92 
93 
94  // JW
95  template <>
98  {
99  // for clarity symbolic names:
100  const double x = (*this)[0];
101  const double y = (*this)[1];
102 
103  // polar to euclidean
104  const double r( NormL2() );
105 
106  if (r==0) return Vector2<double>( 0,0 ); // special case nullvector
107 
108  if (y==0) {
109  // on x-axis.
110  if (x>=0) {
111  // special case point on positive x-axis
112  return Vector2<double>( r, 0 );
113  } else {
114  // special case point on negative x-axis
115  return Vector2<double>(r, M_PI);
116 
117  };
118 
119  } else {
120 
121  // not on x-axis
122  const double phi( atan2(y,x) );
123  return Vector2<double>( r, phi );
124 
125  }
126  }
127 
128 
129 
130  // jw
131  template<class T>
132  bool Vector2<T>::Load(const std::string & filename)
133  {
134  std::ifstream fs( filename.c_str() );
135  if (!fs) {
136  return false; // error, could not open file.
137  } else {
138  // read in
139  fs>>(*this);
140  fs.close();
141  };
142  return true;
143  }
144 
145  // jw
146  template<class T>
147  bool Vector2<T>::Save(const std::string & filename) const
148  {
149  std::ofstream fs( filename.c_str() );
150  if (!fs) {
151  return false; // error, could not open file.
152  } else {
153  // write out to disk
154  fs<<(*this);
155  fs.close();
156  };
157  return true;
158  }
159 
160 } // namespace BIAS
161 
162 
163 ///
164 /// explicit instantiation
165 ///
166 #define INST(type) \
167  template class BIASMathBase_EXPORT BIAS::Vector2<type>;
168 
169 INST(unsigned char)
170 INST(float)
171 INST(char)
172 INST(short)
173 INST(unsigned short)
174 INST(long int)
175 INST(int)
176 INST(unsigned int)
177 INST(double)
bool Load(const std::string &filename)
method to load directly from a given filename.
Definition: Vector2.cpp:132
class for column vectors with arbitrary size
BIAS::Vector2< T > CoordEuclideanToPolar() const
coordinate transform compute the polar coordinates (radius, phi) for (this) given eucldian point (x...
Definition: Vector2.cpp:84
BIAS::Vector2< T > CoordPolarToEuclidean() const
coordinate transform compute the euclidean coord p=(x,y) for (this) given polarcoord (radius...
Definition: Vector2.cpp:54
class Vector2 contains a Vector of dim.
Definition: Vector2.hh:79
bool Save(const std::string &filename) const
method to save directly to a given filename.
Definition: Vector2.cpp:147
INST(unsigned char)
Vector2()
default constructor
Definition: Vector2.hh:94
Subscript size() const
Definition: vec.h:262