Fawkes API  Fawkes Development Version
common.h
1 
2 /***************************************************************************
3  * common.h - common math helper functions
4  *
5  * Created: Wed Oct 16 21:03:26 2013
6  * Copyright 2013 Bahram Maleki-Fard
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #ifndef __UTILS_MATH_COMMON_H_
25 #define __UTILS_MATH_COMMON_H_
26 
27 #include <limits>
28 #include <cmath>
29 
30 namespace fawkes {
31 
32 /** Fast square multiplication.
33  * @param x
34  * @return x^2
35  */
36 inline double
37 sqr(double x)
38 {
39  return (x*x);
40 }
41 
42 /** Fast square multiplication.
43  * @param x
44  * @return x^2
45  */
46 inline float
47 sqr(float x)
48 {
49  return (x*x);
50 }
51 
52 /** Fast square multiplication.
53  * @param x
54  * @return x^2
55  */
56 inline int
57 sqr(int x)
58 {
59  return (x*x);
60 }
61 
62 /** Fast square multiplication.
63  * @param x
64  * @return x^2
65  */
66 inline unsigned long
67 sqr(unsigned long x)
68 {
69  return (x*x);
70 }
71 
72 /** Get distance of two points.
73  * This is particularly useful if not using a more powerful
74  * representation like Eigen.
75  * @param x1 x coordinate of first point
76  * @param y1 y coordinate of first point
77  * @param x2 x coordinate of second point
78  * @param y2 y coordinate of second point
79  * @return distance
80  */
81 inline float
82 point_dist(float x1, float y1, float x2, float y2)
83 {
84  return sqrtf(powf(x1 - x2, 2) + powf(y1 - y2, 2));
85 }
86 
87 /** Check if two points are different with regard to a given threshold.
88  * This is particularly useful if not using a more powerful
89  * representation like Eigen.
90  * @param x1 x coordinate of first point
91  * @param y1 y coordinate of first point
92  * @param x2 x coordinate of second point
93  * @param y2 y coordinate of second point
94  * @param threshold the threshold to compare the distance between the
95  * points to.
96  * @return true if the distance of the two points is greater than or equal
97  * to the given threshold, false otherwise.
98  */
99 inline bool
100 points_different(float x1, float y1, float x2, float y2, float threshold = 1e-4)
101 {
102  return sqrtf(powf(x1 - x2, 2) + powf(y1 - y2, 2)) >= threshold;
103 }
104 
105 } // end namespace fawkes
106 
107 #endif
float point_dist(float x1, float y1, float x2, float y2)
Get distance of two points.
Definition: common.h:82
bool points_different(float x1, float y1, float x2, float y2, float threshold=1e-4)
Check if two points are different with regard to a given threshold.
Definition: common.h:100
Fawkes library namespace.
double sqr(double x)
Fast square multiplication.
Definition: common.h:37