Remove references to dlib and OpenCV. Added TODOs to use with OSF.
[facial-landmarks-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>
830d0ba4
AIL
30
31static const double PI = 3.14159265358979;
32
33template<class T>
34static double avg(T container, double defaultValue = 0)
35{
36 if (container.size() == 0)
37 {
38 return defaultValue;
39 }
40
41 double sum = 0;
42 for (auto it = container.begin(); it != container.end(); ++it)
43 {
44 sum += *it;
45 }
46 return sum / container.size();
47}
48
af96b559 49
830d0ba4 50template<class... Args>
af96b559 51static Point centroid(Args&... args)
830d0ba4
AIL
52{
53 std::size_t numArgs = sizeof...(args);
af96b559 54 if (numArgs == 0) return Point(0, 0);
830d0ba4
AIL
55
56 double sumX = 0, sumY = 0;
57 for (auto point : {args...})
58 {
af96b559
AIL
59 sumX += point.x;
60 sumY += point.y;
830d0ba4
AIL
61 }
62
af96b559 63 return Point(sumX / numArgs, sumY / numArgs);
830d0ba4
AIL
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
af96b559 91double dist(Point& p1, Point& p2)
830d0ba4 92{
af96b559
AIL
93 double xDist = p1.x - p2.x;
94 double yDist = p1.y - p2.y;
830d0ba4
AIL
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