OSDN Git Service

bug fix and optimize
[gintenlib/gintenlib.git] / gintenlib / plane / .svn / text-base / vector.hpp.svn-base
1 #ifndef GINTENLIB_PLANE_INCLUDED_VECTOR_HPP_
2 #define GINTENLIB_PLANE_INCLUDED_VECTOR_HPP_
3
4 /*
5
6       <gintenlib/plane/vector.hpp>
7
8   vector ¡§ Æ󼡸µ¥Ù¥¯¥È¥ë
9
10   Àë¸À¡§
11   template< typename Real >
12   struct basic_vector
13     : private boost::equality_comparable< basic_vector<Real> >,
14      private boost::additive< basic_vector<Real> >,
15      private boost::multiplicative< basic_vector<Real>, Real >
16   {
17     typedef Real real_type;
18     real_type x, y;
19     
20     basic_vector();
21     basic_vector( real_type x_, real_type y_ );
22       
23     template<typename T>
24     basic_vector( const basic_vector<T>& src );
25     
26     template<typename T>
27     basic_vector( const std::pair<T, T>& src );
28     
29     // ±é»»»Ò¿½ÅÄêµÁ
30     // ¤½¤ì¤¾¤ì¸«¤¿¤Þ¤ó¤Þ¤Ç¤¹¡£operator+ ¤È¤«¤Ï boost:: operators ¤Ë¤è¤Ã¤Æ³Æ¼ï¼«Æ°ÄêµÁ¤µ¤ì¤Þ¤¹¡£
31     basic_vector& operator+=( const basic_vector& rhs );
32     basic_vector& operator-=( const basic_vector& rhs );
33     basic_vector operator-() const;
34     basic_vector& operator*=( real_type rhs );
35     basic_vector& operator/=( real_type rhs );
36     bool operator==( const basic_vector& rhs ) const;
37     
38     // 2-Norm
39     real_type norm() const;
40     friend real_type norm( const basic_vector& target );
41     
42     // ÀäÂÐÃÍ
43     real_type absolute() const;
44     friend real_type absolute( const basic_vector& target );
45     
46     // Àµµ¬²½
47     // Àµµ¬²½¤·¤¿·ë²Ì¤òÊÖ¤¹¤Î¤Ç¤Ï¤Ê¤¯¡¢¤³¤Î¥Ù¥¯¥È¥ë¼«ÂΤòÀµµ¬²½¤·¤Þ¤¹¡£
48     basic_vector& normalize();
49     // friend ÈǤϥ³¥Ô¡¼¤·¤Æ¤«¤éÀµµ¬²½¤·¤Þ¤¹¡£
50     friend basic_vector normalized( const basic_vector& x );
51     
52     // ÊгÑ
53     real_type argument() const;
54     friend real_type argument( const basic_vector& x );
55     
56     // ÆâÀÑ
57     real_type dot( const basic_vector& rhs ) const;
58     friend real_type dot( const basic_vector& lhs, const basic_vector& rhs );
59     friend real_type operator*( const basic_vector& lhs, const basic_vector& rhs );
60     
61     // ¥í¡¼¥Æ¡¼¥È¡£²óž¤µ¤»¤¿·ë²Ì¤òÊÖ¤¹¤Î¤Ç¤Ï¤Ê¤¯¡¢¤³¤Î¥Ù¥¯¥È¥ë¼«ÂΤò²ó¤·¤Þ¤¹¡£
62     // ³ÑÅÙ»ØÄê¥Ð¡¼¥¸¥ç¥ó¡£¥Æ¥ó¥×¥ì¡¼¥È¤Ê¤Î¤Ï¡¢gintenlib::plane::angle ¤âÅϤ»¤ë¤è¤¦¤Ë¡¢¤Ç¤¹¡£
63     template< typename Angle >
64     basic_vector& rotate( const Angle& angle );
65     // ¥µ¥¤¥ó¤È¥³¥µ¥¤¥ó¤ò»ØÄꤷ¤¿¥Ð¡¼¥¸¥ç¥ó¡£¹â®¤Ç¤¹¡£
66     basic_vector& rotate( real_type s, real_type c );
67     
68     // ¾åÆó¤Ä¤Î friend ÈÇ¡£¥³¥Ô¡¼¤·¤Æ¤«¤é²ó¤·¤Þ¤¹¡£
69     template< typename Angle >
70     friend basic_vector rotate( const basic_vector& target, const Angle& angle );
71     friend basic_vector rotate( const basic_vector& target, real_type s, real_type c );
72   };
73   typedef basic_vector<double> vector;
74
75   µ¡Ç½¡§
76     Æ󼡸µ¤ÎÁêÂÐ¥Ù¥¯¥È¥ë¤òɽ¤¹¥¯¥é¥¹¡£
77     ÀäÂÐÃÍ·×»»¡¢Êгѷ׻»¡¢Àµµ¬²½¡¢²óž¤Ê¤É¡¢¤È¤ê¤¢¤¨¤º°ìÄ̤êɬÍפ½¤¦¤Ê¤â¤Î¤Ï·¤¨¤Æ¤¤¤Þ¤¹¡£
78
79 */
80
81 #include "fwd.hpp"
82
83 #include <cmath>
84 #include <utility>
85 #include <boost/operators.hpp>
86
87 namespace gintenlib
88 {
89  namespace plane
90  {
91   template< typename Real >
92   struct basic_vector
93     : private boost::equality_comparable< basic_vector<Real> >,
94      private boost::additive< basic_vector<Real> >,
95      private boost::multiplicative< basic_vector<Real>, Real >
96   {
97     typedef Real real_type;
98     real_type x, y;
99     
100     basic_vector()
101       : x(), y() {}
102     
103     basic_vector( real_type x_, real_type y_ )
104       : x(x_), y(y_) {}
105       
106     template<typename T>
107     basic_vector( const basic_vector<T>& src )
108       : x(src.x), y(src.y) {}
109     
110     template<typename T>
111     basic_vector( const std::pair<T, T>& src )
112       : x(src.first), y(src.second) {}
113     
114     // ±é»»»Ò¿½ÅÄêµÁ
115     basic_vector& operator+=( const basic_vector& rhs )
116     {
117       x += rhs.x;
118       y += rhs.y;
119       
120       return *this;
121     }
122     basic_vector& operator-=( const basic_vector& rhs )
123     {
124       x -= rhs.x;
125       y -= rhs.y;
126       
127       return *this;
128     }
129     basic_vector operator-() const
130     {
131       return basic_vector( -x, -y );
132     }
133     basic_vector& operator*=( real_type rhs )
134     {
135       x *= rhs;
136       y *= rhs;
137       
138       return *this;
139     }
140     basic_vector& operator/=( real_type rhs )
141     {
142       x /= rhs;
143       y /= rhs;
144       
145       return *this;
146     }
147     bool operator==( const basic_vector& rhs ) const
148     {
149       return ( x == rhs.x ) && ( y == rhs.y );
150     }
151     
152     // 2-Norm
153     real_type norm() const
154     {
155       return x * x + y * y;
156     }
157     friend real_type norm( const basic_vector& target )
158     {
159       return target.norm();
160     }
161     
162     // ÀäÂÐÃÍ
163     real_type absolute() const
164     {
165       return std::sqrt( x * x + y * y );
166     }
167     friend real_type absolute( const basic_vector& target )
168     {
169       return target.absolute();
170     }
171     
172     // Àµµ¬²½
173     basic_vector& normalize()
174     {
175       real_type r = absolute();
176       
177       if( r == 0 )
178       {
179         x = 1;
180         y = 0;
181       }
182       else
183       {
184         x /= r;
185         y /= r;
186       }
187       
188       return *this;
189     }
190     friend basic_vector normalized( const basic_vector& x )
191     {
192       basic_vector temp = x;
193       temp.normalize();
194       return temp;
195     }
196     
197     // ÊгÑ
198     real_type argument() const
199     {
200       if( x == 0 && y == 0 )
201       {
202         return 0;
203       }
204       return std::atan2( y, x );
205     }
206     friend real_type argument( const basic_vector& x )
207     {
208       return x.argument();
209     }
210     
211     // ÆâÀÑ
212     real_type dot( const basic_vector& rhs ) const
213     {
214       return x * rhs.x + y * rhs.y;
215     }
216     friend real_type dot( const basic_vector& lhs, const basic_vector& rhs )
217     {
218       return lhs.dot(rhs);
219     }
220     friend real_type operator*( const basic_vector& lhs, const basic_vector& rhs )
221     {
222       return lhs.dot(rhs);
223     }
224     
225     // ¥í¡¼¥Æ¡¼¥È
226     template< typename Angle >
227     basic_vector& rotate( const Angle& angle )
228     {
229       using std::sin; using std::cos;
230       return rotate( sin(angle), cos(angle) );
231     }
232     basic_vector& rotate( real_type s, real_type c )
233     {
234       real_type sx = s*x, sy = s*y, cx = c*x, cy = c*y;
235       
236       x = cx - sy;
237       y = sx + cy;
238       
239       return *this;
240     }
241     template< typename Angle >
242     friend basic_vector rotate( const basic_vector& target, const Angle& angle )
243     {
244       basic_vector temp = target;
245       temp.rotate( angle );
246       return temp;
247     }
248     friend basic_vector rotate( const basic_vector& target, real_type s, real_type c )
249     {
250       basic_vector temp = target;
251       temp.rotate( s, c );
252       return temp;
253     }
254   };
255   typedef basic_vector<double> vector;
256   
257  }  // namespace plane
258 }   // namespace gintenlib
259
260 #endif  // #ifndef GINTENLIB_PLANE_INCLUDED_VECTOR_HPP_