OpenStructure
vecmat2_op.hh
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
2 // This file is part of the OpenStructure project <www.openstructure.org>
3 //
4 // Copyright (C) 2008-2011 by the OpenStructure authors
5 //
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License as published by the Free
8 // Software Foundation; either version 3.0 of the License, or (at your option)
9 // any later version.
10 // This library is distributed in the hope that it will be useful, but WITHOUT
11 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13 // details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with this library; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 //------------------------------------------------------------------------------
19 #ifndef GEOM_VECMAT2_OP_HH
20 #define GEOM_VECMAT2_OP_HH
21 
22 #include "constants.hh"
23 
25 
26 #include <ost/geom/vec2.hh>
27 #include <ost/geom/mat2.hh>
28 
29 namespace geom {
30 
31 class Vec2;
32 class Mat2;
33 
35 inline Real Length2(const Vec2& v)
36 {
37  return v[0]*v[0]+v[1]*v[1];
38 }
39 
41 inline Real Length(const Vec2& v)
42 {
43  return std::sqrt(Length2(v));
44 }
45 
47 inline bool Equal(const Vec2& v1, const Vec2& v2, Real ephilon=EPSILON)
48 {
49  return std::fabs(v1[0]-v2[0])<ephilon &&
50  std::fabs(v1[1]-v2[1])<ephilon;
51 }
52 
54 inline bool Equal(const Mat2& m1, const Mat2& m2, Real ephilon=EPSILON)
55 {
56  return std::fabs(m1(0,0)-m2(0,0))<ephilon &&
57  std::fabs(m1(0,1)-m2(0,1))<ephilon &&
58  std::fabs(m1(1,0)-m2(1,0))<ephilon &&
59  std::fabs(m1(1,1)-m2(1,1))<ephilon;
60 }
61 
63 inline Real Dot(const Vec2& v1, const Vec2& v2)
64 {
65  return v1[0]*v2[0]+v1[1]*v2[1];
66 }
67 
69 inline Vec2 Normalize(const Vec2& v)
70 {
71  Real l=Length(v);
72  if(l==0.0) {
73  return v;
74  }
75  return v/l;
76 }
77 
79 inline Vec2 CompMultiply(const Vec2& v1, const Vec2& v2)
80 {
81  Vec2 nrvo(v1[0]*v2[0],v1[1]*v2[1]);
82  return nrvo;
83 }
84 
86 inline Vec2 CompDivide(const Vec2& v1, const Vec2& v2)
87 {
88  Vec2 nrvo(v1[0]/v2[0],v1[1]/v2[1]);
89  return nrvo;
90 }
91 
93 
99 inline Vec2 operator*(const Vec2& v,const Mat2& m)
100 {
101  Vec2 nrvo(v[0]*m(0,0)+v[1]*m(1,0),
102  v[0]*m(0,1)+v[1]*m(1,1));
103  return nrvo;
104 }
105 
107 
113 inline Vec2 operator*(const Mat2& m, const Vec2& v)
114 {
115  Vec2 nrvo(v[0]*m(0,0)+v[1]*m(0,1),
116  v[0]*m(1,0)+v[1]*m(1,1));
117  return nrvo;
118 }
119 
121 Real DLLEXPORT_OST_GEOM Det(const Mat2& m);
123 Mat2 DLLEXPORT_OST_GEOM Transpose(const Mat2& m);
124 
126 Mat2 DLLEXPORT_OST_GEOM Invert(const Mat2& m);
127 
129 Real DLLEXPORT_OST_GEOM Angle(const Vec2& v1, const Vec2& v2);
131 Real DLLEXPORT_OST_GEOM SignedAngle(const Vec2& v1, const Vec2& v2);
132 
134 inline Mat2 operator*(const Mat2& m1, const Mat2& m2)
135 {
136  Mat2 nrvo(m1(0,0)*m2(0,0)+m1(0,1)*m2(1,0),
137  m1(0,0)*m2(0,1)+m1(0,1)*m2(1,1),
138  m1(1,0)*m2(0,0)+m1(1,1)*m2(1,0),
139  m1(1,0)*m2(0,1)+m1(1,1)*m2(1,1));
140  return nrvo;
141 }
142 
143 inline Vec2 Min(const Vec2& v1, const Vec2& v2)
144 {
145  Vec2 nrvo(std::min(v1[0],v2[0]),
146  std::min(v1[1],v2[1]));
147  return nrvo;
148 }
149 
150 inline Vec2 Max(const Vec2& v1, const Vec2& v2)
151 {
152  Vec2 nrvo(std::max(v1[0],v2[0]),
153  std::max(v1[1],v2[1]));
154  return nrvo;
155 }
156 
158 DLLEXPORT Vec2 Rotate(const Vec2& v,Real ang);
159 
160 } // namespace geom
161 
162 #endif