Initial commit - it should now be more or less working
[mouse-tracker-for-cubism.git] / src / math_utils.h
1 // -*- mode: c++ -*-
2
3 #ifndef __FACE_DETECTOR_MATH_UTILS_H__
4 #define __FACE_DETECTOR_MATH_UTILS_H__
5
6 /****
7 Copyright (c) 2020 Adrian I. Lam
8
9 Permission is hereby granted, free of charge, to any person obtaining a copy
10 of this software and associated documentation files (the "Software"), to deal
11 in the Software without restriction, including without limitation the rights
12 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 copies of the Software, and to permit persons to whom the Software is
14 furnished to do so, subject to the following conditions:
15
16 The above copyright notice and this permission notice shall be included in all
17 copies or substantial portions of the Software.
18
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 SOFTWARE.
26 ****/
27
28 #include <cmath>
29 #include <initializer_list>
30 #include <dlib/image_processing.h>
31
32 static const double PI = 3.14159265358979;
33
34 template<class T>
35 static double avg(T container, double defaultValue = 0)
36 {
37 if (container.size() == 0)
38 {
39 return defaultValue;
40 }
41
42 double sum = 0;
43 for (auto it = container.begin(); it != container.end(); ++it)
44 {
45 sum += *it;
46 }
47 return sum / container.size();
48 }
49
50 template<class... Args>
51 static dlib::point centroid(Args&... args)
52 {
53 std::size_t numArgs = sizeof...(args);
54 if (numArgs == 0) return dlib::point(0, 0);
55
56 double sumX = 0, sumY = 0;
57 for (auto point : {args...})
58 {
59 sumX += point.x();
60 sumY += point.y();
61 }
62
63 return dlib::point(sumX / numArgs, sumY / numArgs);
64 }
65
66 static inline double sq(double x)
67 {
68 return x * x;
69 }
70
71 static double solveCosineRuleAngle(double opposite,
72 double adjacent1,
73 double adjacent2)
74 {
75 // c^2 = a^2 + b^2 - 2 a b cos(C)
76 double cosC = (sq(opposite) - sq(adjacent1) - sq(adjacent2)) /
77 (-2 * adjacent1 * adjacent2);
78 return std::acos(cosC);
79 }
80
81 static inline double radToDeg(double rad)
82 {
83 return rad * 180 / PI;
84 }
85
86 static inline double degToRad(double deg)
87 {
88 return deg * PI / 180;
89 }
90
91 double dist(dlib::point& p1, dlib::point& p2)
92 {
93 double xDist = p1.x() - p2.x();
94 double yDist = p1.y() - p2.y();
95
96 return std::hypot(xDist, yDist);
97 }
98
99 /*! Scale linearly from 0 to 1 (both end-points inclusive) */
100 double linearScale01(double num, double min, double max,
101 bool clipMin = true, bool clipMax = true)
102 {
103 if (num < min && clipMin) return 0.0;
104 if (num > max && clipMax) return 1.0;
105 return (num - min) / (max - min);
106 }
107
108 #endif