OpenStructure
vecmat4_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_VECMAT4_OP_HH
20 #define GEOM_VECMAT4_OP_HH
21 
22 #include "constants.hh"
23 
25 #include <ost/geom/vec4.hh>
26 #include <ost/geom/vec3.hh>
27 #include <ost/geom/mat4.hh>
28 #include <ost/geom/mat3.hh>
29 
30 namespace geom {
31 
32 
34 inline Real Length2(const Vec4& v)
35 {
36  return v[0]*v[0]+v[1]*v[1]+v[2]*v[2]+v[3]*v[3];
37 }
38 
40 inline Real Length(const Vec4& v)
41 {
42  return std::sqrt(Length2(v));
43 }
44 
46 inline bool Equal(const Vec4& v1, const Vec4& v2, Real epsilon=EPSILON)
47 {
48  return std::fabs(v1[0]-v2[0])<epsilon &&
49  std::fabs(v1[1]-v2[1])<epsilon &&
50  std::fabs(v1[2]-v2[2])<epsilon &&
51  std::fabs(v1[3]-v2[3])<epsilon;
52 }
53 
55 inline bool Equal(const Mat4& m1, const Mat4& m2, Real epsilon=EPSILON)
56 {
57  return std::fabs(m1(0,0)-m2(0,0))<epsilon &&
58  std::fabs(m1(0,1)-m2(0,1))<epsilon &&
59  std::fabs(m1(0,2)-m2(0,2))<epsilon &&
60  std::fabs(m1(0,3)-m2(0,3))<epsilon &&
61  std::fabs(m1(1,0)-m2(1,0))<epsilon &&
62  std::fabs(m1(1,1)-m2(1,1))<epsilon &&
63  std::fabs(m1(1,2)-m2(1,2))<epsilon &&
64  std::fabs(m1(1,3)-m2(1,3))<epsilon &&
65  std::fabs(m1(2,0)-m2(2,0))<epsilon &&
66  std::fabs(m1(2,1)-m2(2,1))<epsilon &&
67  std::fabs(m1(2,2)-m2(2,2))<epsilon &&
68  std::fabs(m1(2,3)-m2(2,3))<epsilon &&
69  std::fabs(m1(3,0)-m2(3,0))<epsilon &&
70  std::fabs(m1(3,1)-m2(3,1))<epsilon &&
71  std::fabs(m1(3,2)-m2(3,2))<epsilon &&
72  std::fabs(m1(3,3)-m2(3,3))<epsilon;
73 }
74 
76 inline Real Dot(const Vec4& v1, const Vec4& v2)
77 {
78  return v1[0]*v2[0]+v1[1]*v2[1]+v1[2]*v2[2]+v1[3]*v2[3];
79 }
80 
81 inline Vec4 Normalize(const Vec4& v)
82 {
83  Real l=Length(v);
84  if(l==0.0) {
85  return v;
86  }
87  return v/l;
88 }
89 
91 inline Vec4 CompMultiply(const Vec4& v1, const Vec4& v2)
92 {
93  Vec4 nrvo(v1[0]*v2[0],v1[1]*v2[1],v1[2]*v2[2],v1[3]*v2[3]);
94  return nrvo;
95 }
96 
98 inline Vec4 CompDivide(const Vec4& v1, const Vec4& v2)
99 {
100  Vec4 nrvo(v1[0]/v2[0],v1[1]/v2[1],v1[2]/v2[2],v1[3]/v2[3]);
101  return nrvo;
102 }
103 
105 inline Vec4 operator*(const Vec4& v,const Mat4& m)
106 {
107  Vec4 nrvo(v[0]*m(0,0)+v[1]*m(1,0)+v[2]*m(2,0)+v[3]*m(3,0),
108  v[0]*m(0,1)+v[1]*m(1,1)+v[2]*m(2,1)+v[3]*m(3,1),
109  v[0]*m(0,2)+v[1]*m(1,2)+v[2]*m(2,2)+v[3]*m(3,2),
110  v[0]*m(0,3)+v[1]*m(1,3)+v[2]*m(2,3)+v[3]*m(3,3));
111  return nrvo;
112 }
113 
115 inline Vec4 operator*(const Mat4& m, const Vec4& v)
116 {
117  Vec4 nrvo(v[0]*m(0,0)+v[1]*m(0,1)+v[2]*m(0,2)+v[3]*m(0,3),
118  v[0]*m(1,0)+v[1]*m(1,1)+v[2]*m(1,2)+v[3]*m(1,3),
119  v[0]*m(2,0)+v[1]*m(2,1)+v[2]*m(2,2)+v[3]*m(2,3),
120  v[0]*m(3,0)+v[1]*m(3,1)+v[2]*m(3,2)+v[3]*m(3,3));
121  return nrvo;
122 }
123 
124 // algebraic complement
125 Real DLLEXPORT_OST_GEOM Comp(const Mat4& m, unsigned int i, unsigned int j);
126 
127 // minor
128 Real DLLEXPORT_OST_GEOM Minor(const Mat4& m, unsigned int i, unsigned int j);
129 
130 Real DLLEXPORT_OST_GEOM Det(const Mat4& m);
131 Mat4 DLLEXPORT_OST_GEOM Transpose(const Mat4& m);
132 Mat4 DLLEXPORT_OST_GEOM Invert(const Mat4& m);
133 Mat4 DLLEXPORT_OST_GEOM operator*(const Mat4& m1, const Mat4& m2);
134 Real DLLEXPORT_OST_GEOM Angle(const Vec4& v1, const Vec4& v2);
135 
137 inline Vec4 Min(const Vec4& v1, const Vec4& v2)
138 {
139  Vec4 nrvo(std::min(v1[0],v2[0]),
140  std::min(v1[1],v2[1]),
141  std::min(v1[2],v2[2]),
142  std::min(v1[3],v2[3]));
143  return nrvo;
144 }
145 
147 inline Vec4 Max(const Vec4& v1, const Vec4& v2)
148 {
149  Vec4 nrvo(std::max(v1[0],v2[0]),
150  std::max(v1[1],v2[1]),
151  std::max(v1[2],v2[2]),
152  std::max(v1[3],v2[3]));
153  return nrvo;
154 }
155 
156 } // ns
157 
158 #endif