Path: blob/master/thirdparty/msdfgen/core/arithmetics.hpp
9902 views
1#pragma once23#include <cmath>4#include "base.h"56namespace msdfgen {78/// Returns the smaller of the arguments.9template <typename T>10inline T min(T a, T b) {11return b < a ? b : a;12}1314/// Returns the larger of the arguments.15template <typename T>16inline T max(T a, T b) {17return a < b ? b : a;18}1920/// Returns the middle out of three values21template <typename T>22inline T median(T a, T b, T c) {23return max(min(a, b), min(max(a, b), c));24}2526/// Returns the weighted average of a and b.27template <typename T, typename S>28inline T mix(T a, T b, S weight) {29return T((S(1)-weight)*a+weight*b);30}3132/// Clamps the number to the interval from 0 to 1.33template <typename T>34inline T clamp(T n) {35return n >= T(0) && n <= T(1) ? n : T(n > T(0));36}3738/// Clamps the number to the interval from 0 to b.39template <typename T>40inline T clamp(T n, T b) {41return n >= T(0) && n <= b ? n : T(n > T(0))*b;42}4344/// Clamps the number to the interval from a to b.45template <typename T>46inline T clamp(T n, T a, T b) {47return n >= a && n <= b ? n : n < a ? a : b;48}4950/// Returns 1 for positive values, -1 for negative values, and 0 for zero.51template <typename T>52inline int sign(T n) {53return (T(0) < n)-(n < T(0));54}5556/// Returns 1 for non-negative values and -1 for negative values.57template <typename T>58inline int nonZeroSign(T n) {59return 2*(n > T(0))-1;60}6162}636465