Math2d.cpp
Go to the documentation of this file.
1 // ****************************************************************************
2 // This file is part of the Integrating Vision Toolkit (IVT).
3 //
4 // The IVT is maintained by the Karlsruhe Institute of Technology (KIT)
5 // (www.kit.edu) in cooperation with the company Keyetech (www.keyetech.de).
6 //
7 // Copyright (C) 2014 Karlsruhe Institute of Technology (KIT).
8 // All rights reserved.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are met:
12 //
13 // 1. Redistributions of source code must retain the above copyright
14 // notice, this list of conditions and the following disclaimer.
15 //
16 // 2. Redistributions in binary form must reproduce the above copyright
17 // notice, this list of conditions and the following disclaimer in the
18 // documentation and/or other materials provided with the distribution.
19 //
20 // 3. Neither the name of the KIT nor the names of its contributors may be
21 // used to endorse or promote products derived from this software
22 // without specific prior written permission.
23 //
24 // THIS SOFTWARE IS PROVIDED BY THE KIT AND CONTRIBUTORS “AS IS” AND ANY
25 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 // DISCLAIMED. IN NO EVENT SHALL THE KIT OR CONTRIBUTORS BE LIABLE FOR ANY
28 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 // ****************************************************************************
35 // ****************************************************************************
36 // Filename: Math2d.cpp
37 // Author: Pedram Azad
38 // Date: 2005
39 // ****************************************************************************
40 
41 
42 // ****************************************************************************
43 // Includes
44 // ****************************************************************************
45 
46 #include <new> // for explicitly using correct new/delete operators on VC DSPs
47 
48 #include "Math2d.h"
49 #include "Math3d.h"
50 #include "Structs/Structs.h"
51 
52 // system
53 #include <math.h>
54 
55 
56 
57 // ****************************************************************************
58 // Variables
59 // ****************************************************************************
60 
61 const Vec2d Math2d::zero_vec = { 0, 0 };
62 
63 
64 // ****************************************************************************
65 // Functions
66 // ****************************************************************************
67 
68 void Math2d::SetVec(Vec2d &vec, float x, float y)
69 {
70  vec.x = x;
71  vec.y = y;
72 }
73 
74 void Math2d::SetVec(Vec2d &vec, const Vec2d &sourceVector)
75 {
76  vec.x = sourceVector.x;
77  vec.y = sourceVector.y;
78 }
79 
80 
81 void Math2d::AddToVec(Vec2d &vec, const Vec2d &vectorToAdd)
82 {
83  vec.x += vectorToAdd.x;
84  vec.y += vectorToAdd.y;
85 }
86 
87 void Math2d::SubtractFromVec(Vec2d &vec, const Vec2d &vectorToSubtract)
88 {
89  vec.x -= vectorToSubtract.x;
90  vec.y -= vectorToSubtract.y;
91 }
92 
93 void Math2d::AddVecVec(const Vec2d &vector1, const Vec2d &vector2, Vec2d &result)
94 {
95  result.x = vector1.x + vector2.x;
96  result.y = vector1.y + vector2.y;
97 }
98 
99 void Math2d::SetRotationMat(Mat2d &matrix, float angle)
100 {
101  const float sa = sinf(angle);
102  const float ca = cosf(angle);
103 
104  matrix.r1 = matrix.r4 = ca;
105  matrix.r2 = -sa;
106  matrix.r3 = sa;
107 }
108 
109 void Math2d::MulMatScalar(const Mat2d &matrix, float scalar, Mat2d &result)
110 {
111  result.r1 = matrix.r1 * scalar;
112  result.r2 = matrix.r2 * scalar;
113  result.r3 = matrix.r3 * scalar;
114  result.r4 = matrix.r4 * scalar;
115 }
116 
117 void Math2d::MulMatMat(const Mat2d &matrix1, const Mat2d &matrix2, Mat2d &result)
118 {
119  const float r1 = matrix1.r1 * matrix2.r1 + matrix1.r2 * matrix2.r3;
120  const float r2 = matrix1.r1 * matrix2.r2 + matrix1.r2 * matrix2.r4;
121  const float r3 = matrix1.r3 * matrix2.r1 + matrix1.r4 * matrix2.r3;
122  result.r4 = matrix1.r3 * matrix2.r2 + matrix1.r4 * matrix2.r4;
123  result.r1 = r1;
124  result.r2 = r2;
125  result.r3 = r3;
126 }
127 
128 void Math2d::MulMatVec(const Mat2d &matrix, const Vec2d &vec, Vec2d &result)
129 {
130  const float temp = matrix.r1 * vec.x + matrix.r2 * vec.y;
131  result.y = matrix.r3 * vec.x + matrix.r4 * vec.y;
132  result.x = temp;
133 }
134 
135 void Math2d::MulMatVec(const Mat2d &matrix, const Vec2d &vector1, const Vec2d &vector2, Vec2d &result)
136 {
137  const float temp = matrix.r1 * vector1.x + matrix.r2 * vector1.y + vector2.x;
138  result.y = matrix.r3 * vector1.x + matrix.r4 * vector1.y + vector2.y;
139  result.x = temp;
140 }
141 
142 void Math2d::MulVecScalar(const Vec2d &vec, float scalar, Vec2d &result)
143 {
144  result.x = scalar * vec.x;
145  result.y = scalar * vec.y;
146 }
147 
148 void Math2d::SubtractVecVec(const Vec2d &vector1, const Vec2d &vector2, Vec2d &result)
149 {
150  result.x = vector1.x - vector2.x;
151  result.y = vector1.y - vector2.y;
152 }
153 
154 
155 float Math2d::ScalarProduct(const Vec2d &vector1, const Vec2d &vector2)
156 {
157  return vector1.x * vector2.x + vector1.y * vector2.y;
158 }
159 
161 {
162  const float length = sqrtf(vec.x * vec.x + vec.y * vec.y);
163 
164  if (length != 0.0f)
165  {
166  vec.x /= length;
167  vec.y /= length;
168  }
169 }
170 
171 float Math2d::Length(const Vec2d &vec)
172 {
173  return sqrtf(vec.x * vec.x + vec.y * vec.y);
174 }
175 
176 float Math2d::SquaredLength(const Vec2d &vec)
177 {
178  return vec.x * vec.x + vec.y * vec.y;
179 }
180 
181 float Math2d::Distance(const Vec2d &vector1, const Vec2d &vector2)
182 {
183  const float x1 = vector1.x - vector2.x;
184  const float x2 = vector1.y - vector2.y;
185 
186  return sqrtf(x1 * x1 + x2 * x2);
187 }
188 
189 float Math2d::SquaredDistance(const Vec2d &vector1, const Vec2d &vector2)
190 {
191  const float x1 = vector1.x - vector2.x;
192  const float x2 = vector1.y - vector2.y;
193 
194  return x1 * x1 + x2 * x2;
195 }
196 
197 float Math2d::Angle(const Vec2d &vector1, const Vec2d &vector2)
198 {
199  const float sp = vector1.x * vector2.x + vector1.y * vector2.y;
200  const float l1 = sqrtf(vector1.x * vector1.x + vector1.y * vector1.y);
201  const float l2 = sqrtf(vector2.x * vector2.x + vector2.y * vector2.y);
202 
203  return acosf(sp / (l1 * l2));
204 }
205 
206 void Math2d::RotateVec(const Vec2d &vec, float angle, Vec2d &result)
207 {
208  const float ca = cosf(angle);
209  const float sa = sinf(angle);
210 
211  const float temp = ca * vec.x - sa * vec.y;
212  result.y = sa * vec.x + ca * vec.y;
213  result.x = temp;
214 }
215 
216 void Math2d::RotateVec(const Vec2d &point, const Vec2d &center, float angle, Vec2d &result)
217 {
218  const float cx = center.x;
219  const float cy = center.y;
220 
221  SubtractVecVec(point, center, result);
222  RotateVec(result, angle, result);
223 
224  result.x += cx;
225  result.y += cy;
226 }
227 
228 void Math2d::Transpose(const Mat2d &matrix, Mat2d &result)
229 {
230  result.r1 = matrix.r1;
231  result.r4 = matrix.r4;
232 
233  const float temp = matrix.r2;
234  result.r2 = matrix.r3;
235  result.r3 = temp;
236 }
237 
238 void Math2d::Invert(const Mat2d &matrix, Mat2d &result)
239 {
240  const float a = matrix.r1;
241  const float b = matrix.r2;
242  const float c = matrix.r3;
243  const float d = matrix.r4;
244 
245  float det_inverse = 1 / (a * d - b * c);
246 
247  result.r1 = d * det_inverse;
248  result.r2 = -b * det_inverse;
249  result.r3 = -c * det_inverse;
250  result.r4 = a * det_inverse;
251 }
252 
253 void Math2d::ApplyHomography(const Mat3d &A, const Vec2d &p, Vec2d &result)
254 {
255  // optimized version of:
256  // (x', y', z') = A * (p.x, p.y, 1)^T
257  // x = x' / z'
258  // y = y' / z'
259 
260  const float y_ = A.r4 * p.x + A.r5 * p.y + A.r6;
261  const float z_ = A.r7 * p.x + A.r8 * p.y + A.r9;
262 
263  result.x = (A.r1 * p.x + A.r2 * p.y + A.r3) / z_;
264  result.y = y_ / z_;
265 }
266 
267 void Math2d::Average(const Vec2d &vector1, const Vec2d &vector2, Vec2d &result)
268 {
269  result.x = 0.5f * (vector1.x + vector2.x);
270  result.y = 0.5f * (vector1.y + vector2.y);
271 }
272 
273 void Math2d::Mean(const CVec2dArray &vectorList, Vec2d &result)
274 {
275  Mean(vectorList.GetElements(), vectorList.GetSize(), result);
276 }
277 
278 void Math2d::Mean(const Vec2d *pVectors, int nVectors, Vec2d &result)
279 {
280  if (nVectors == 0)
281  {
282  result = zero_vec;
283  return;
284  }
285 
286  float sum_x = 0.0f, sum_y = 0.0f;
287 
288  for (int i = 0; i < nVectors; i++)
289  {
290  sum_x += pVectors[i].x;
291  sum_y += pVectors[i].y;
292  }
293 
294  result.x = sum_x / float(nVectors);
295  result.y = sum_y / float(nVectors);
296 }
297 
298 void Math2d::ComputeRectangleCornerPoints(const Rectangle2d &rectangle, Vec2d resultCornerPoints[4])
299 {
300  const float cx = rectangle.center.x;
301  const float cy = rectangle.center.y;
302  const float width2 = 0.5f * rectangle.width;
303  const float height2 = 0.5f * rectangle.height;
304  const float angle = rectangle.angle;
305 
306  // set corner points at rotation angle zero
307  SetVec(resultCornerPoints[0], cx - width2, cy - height2);
308  SetVec(resultCornerPoints[1], cx + width2, cy - height2);
309  SetVec(resultCornerPoints[2], cx + width2, cy + height2);
310  SetVec(resultCornerPoints[3], cx - width2, cy + height2);
311 
312  if (angle != 0.0f)
313  {
314  for (int i = 0; i < 4; i++)
315  RotateVec(resultCornerPoints[i], rectangle.center, angle, resultCornerPoints[i]);
316  }
317 }
float r4
Definition: Math3d.h:95
void ApplyHomography(const Mat3d &A, const Vec2d &p, Vec2d &result)
Definition: Math2d.cpp:253
float height
The height of the rectangle.
Definition: Structs.h:104
float width
The width of the rectangle.
Definition: Structs.h:99
float Angle(const Vec2d &vector1, const Vec2d &vector2)
Definition: Math2d.cpp:197
void Transpose(const Mat2d &matrix, Mat2d &result)
Definition: Math2d.cpp:228
float r7
Definition: Math3d.h:95
GLuint GLenum matrix
Definition: glext.h:5683
float y
Definition: Math2d.h:84
void Average(const Vec2d &vector1, const Vec2d &vector2, Vec2d &result)
Definition: Math2d.cpp:267
float r1
Definition: Math3d.h:95
void RotateVec(const Vec2d &vec, float angle, Vec2d &result)
Definition: Math2d.cpp:206
float SquaredDistance(const Vec2d &vector1, const Vec2d &vector2)
Definition: Math2d.cpp:189
float r2
Definition: Math3d.h:95
float r3
Definition: Math3d.h:95
Data structure for the representation of a 2D rectangle.
Definition: Structs.h:89
void ComputeRectangleCornerPoints(const Rectangle2d &rectangle, Vec2d resultCornerPoints[4])
Definition: Math2d.cpp:298
float ScalarProduct(const Vec2d &vector1, const Vec2d &vector2)
Definition: Math2d.cpp:155
void MulVecScalar(const Vec2d &vec, float scalar, Vec2d &result)
Definition: Math2d.cpp:142
GLenum GLint x
Definition: glext.h:3125
float r3
Definition: Math2d.h:103
float Distance(const Vec2d &vector1, const Vec2d &vector2)
Definition: Math2d.cpp:181
float x
Definition: Math2d.h:84
const GLubyte * c
Definition: glext.h:5181
Vec2d center
The center of the rectangle.
Definition: Structs.h:94
float r4
Definition: Math2d.h:103
Data structure for the representation of a 2x2 matrix.
Definition: Math2d.h:101
void SetRotationMat(Mat2d &matrix, float angle)
Definition: Math2d.cpp:99
float r6
Definition: Math3d.h:95
void NormalizeVec(Vec2d &vec)
Definition: Math2d.cpp:160
float angle
The angle of the rectangle.
Definition: Structs.h:109
float Length(const Vec2d &vec)
Definition: Math2d.cpp:171
void SubtractVecVec(const Vec2d &vector1, const Vec2d &vector2, Vec2d &result)
Definition: Math2d.cpp:148
float r8
Definition: Math3d.h:95
float r9
Definition: Math3d.h:95
GLubyte GLubyte GLubyte a
Definition: glext.h:5166
const Vec2d zero_vec
Definition: Math2d.cpp:61
void Invert(const Mat2d &matrix, Mat2d &result)
Definition: Math2d.cpp:238
void AddVecVec(const Vec2d &vector1, const Vec2d &vector2, Vec2d &result)
Definition: Math2d.cpp:93
void MulMatMat(const Mat2d &matrix1, const Mat2d &matrix2, Mat2d &result)
Definition: Math2d.cpp:117
GLubyte GLubyte b
Definition: glext.h:5166
void AddToVec(Vec2d &vec, const Vec2d &vectorToAdd)
Definition: Math2d.cpp:81
float r5
Definition: Math3d.h:95
GLuint GLsizei GLsizei * length
Definition: glext.h:3509
GLenum GLint GLint y
Definition: glext.h:3125
Data structure for the representation of a 2D vector.
Definition: Math2d.h:82
void MulMatScalar(const Mat2d &matrix, float scalar, Mat2d &result)
Definition: Math2d.cpp:109
void SetVec(Vec2d &vec, float x, float y)
Definition: Math2d.cpp:68
void SubtractVecVec(const CFloatVector *pVector1, const CFloatVector *pVector2, CFloatVector *pResultVector)
void SubtractFromVec(Vec2d &vec, const Vec2d &vectorToSubtract)
Definition: Math2d.cpp:87
void Mean(const CVec2dArray &vectorList, Vec2d &result)
Definition: Math2d.cpp:273
GLfloat GLfloat p
Definition: glext.h:5178
float r1
Definition: Math2d.h:103
const T * GetElements() const
Data structure for the representation of a 3x3 matrix.
Definition: Math3d.h:93
double Mean(CByteImage *pImage1, CByteImage *pImage2)
Deprecated.
float r2
Definition: Math2d.h:103
float SquaredLength(const Vec2d &vec)
Definition: Math2d.cpp:176
void MulMatVec(const Mat2d &matrix, const Vec2d &vec, Vec2d &result)
Definition: Math2d.cpp:128


asr_ivt
Author(s): Allgeyer Tobias, Hutmacher Robin, Kleinert Daniel, Meißner Pascal, Scholz Jonas, Stöckle Patrick
autogenerated on Mon Dec 2 2019 03:47:28