Adding options to enable auto blink, auto breath and random motion
[mouse-tracker-for-cubism.git] / src / math_utils.h
CommitLineData
830d0ba4
AIL
1// -*- mode: c++ -*-
2
3#ifndef __FACE_DETECTOR_MATH_UTILS_H__
4#define __FACE_DETECTOR_MATH_UTILS_H__
5
6/****
7Copyright (c) 2020 Adrian I. Lam
8
9Permission is hereby granted, free of charge, to any person obtaining a copy
10of this software and associated documentation files (the "Software"), to deal
11in the Software without restriction, including without limitation the rights
12to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13copies of the Software, and to permit persons to whom the Software is
14furnished to do so, subject to the following conditions:
15
16The above copyright notice and this permission notice shall be included in all
17copies or substantial portions of the Software.
18
19THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25SOFTWARE.
26****/
27
28#include <cmath>
29#include <initializer_list>
30#include <dlib/image_processing.h>
31
32static const double PI = 3.14159265358979;
33
34template<class T>
35static 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
50template<class... Args>
51static 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
66static inline double sq(double x)
67{
68 return x * x;
69}
70
71static 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
81static inline double radToDeg(double rad)
82{
83 return rad * 180 / PI;
84}
85
86static inline double degToRad(double deg)
87{
88 return deg * PI / 180;
89}
90
91double 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) */
100double 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