FaceGen 3 SDKs Reference
Loading...
Searching...
No Matches
algs.hpp
1//
2// Copyright (c) Singular Inversions Inc. 1999.
3//
4// Authors: Andrew Beatty
5// Created: Nov 5, 1999.
6//
7
8#ifndef FR2_ALGS_HPP
9#define FR2_ALGS_HPP
10
11#include "FgSerial.hpp"
12
13namespace Fg {
14
15inline int
16futFloor(float v)
17{return int(std::floor(v)); }
18
19inline int
20futFloor(double v)
21{return int(std::floor(v)); }
22
23// Useful within template defintions:
24inline void futConvert(double a,uchar &b) {b = static_cast<uchar>(a + 0.5); }
25inline void futConvert(double a,ushort &b) {b = static_cast<ushort>(a + 0.5); }
26inline void futConvert(double a,uint &b) {b = static_cast<uint>(a + 0.5); }
27
28inline void futConvert(double a,char &b) {b = static_cast<char>(futFloor(a + 0.5)); }
29inline void futConvert(double a,short &b) {b = static_cast<short>(futFloor(a + 0.5)); }
30inline void futConvert(double a,int &b) {b = static_cast<int>(futFloor(a + 0.5)); }
31
32inline void futConvert(float a,uchar &b) {b = static_cast<uchar>(a + 0.5f); }
33inline void futConvert(float a,ushort &b) {b = static_cast<ushort>(a + 0.5f); }
34inline void futConvert(float a,uint &b) {b = static_cast<uint>(a + 0.5f); }
35
36inline void futConvert(float a,char &b) {b = static_cast<char>(futFloor(a + 0.5f)); }
37inline void futConvert(float a,short &b) {b = static_cast<short>(futFloor(a + 0.5f)); }
38inline void futConvert(float a,int &b) {b = static_cast<int>(futFloor(a + 0.5f)); }
39
40inline void futConvert(ushort a,char &b) {b = static_cast<char>(a); }
41inline void futConvert(ushort a,uchar &b) {b = static_cast<uchar>(a); }
42
43inline void futConvert(uchar a,uchar &b) {b = a; }
44inline void futConvert(uchar a,ushort &b) {b = a; }
45inline void futConvert(uchar a,float &b) {b = a; }
46
47inline void futConvert(int a,float &b) {b = static_cast<float>(a); }
48
49inline schar fgClampToSchar(int val)
50{
51 if (val < -128) return schar(-128);
52 if (val > 127) return schar(127);
53 return schar(val);
54};
55
56uint fr2Log2Floor(uint); // Floor (log_2 (arg) ) but implemented
57 // efficiently for ints.
58uint fr2Log2Ceil(uint); // Ceil (log_2 (arg) ) but implemented
59 // efficiently for ints.
60
61inline uint fr2Pow2Floor(uint a) // Nearest power of 2 <= arg
62{
63 return (1 << (fr2Log2Floor(a)));
64}
65
66inline uint fr2Pow2Ceil(uint a) // Nearest power of 2 >= arg
67{
68 return (1 << (fr2Log2Ceil(a)));
69}
70
71inline bool futIsPow2(uint a) // Is the number a power of 2 ?
72{
73 return (fr2Log2Floor(a) == fr2Log2Ceil(a));
74}
75
76template<class T>
77inline T fr2RadsToDegs(T const angle)
78{
79 return(angle * (T)(180.0 / 3.141592653589793237462643383279));
80}
81
82template<class T>
83inline T fr2DegsToRads(T const angle)
84{
85 return(angle * (T)(3.141592653589793237462643383279 / 180.0));
86}
87
88 // Clamp a value equal or between the given bounds.
89 //
90template<class T>
91inline T const& futClamp(T const& v,T const& l,T const& h)
92{
93 if (v < l) return l;
94 else if (v > h) return h;
95 else
96 return v;
97}
98
99// Return a contiguous subset of vector.
100template<class T>
101std::vector<T> futSubVec(
102
103 const std::vector<T> &v,
104 std::size_t b,
105 std::size_t e)
106{
107 FGASSERT(e < v.size());
108 FGASSERT(b <= e);
109 std::vector<T> r(e-b+1);
110
111 for (size_t ii=b; ii<=e; ii++)
112 r[ii-b] = v[ii];
113
114 return r;
115}
116
117}
118
119#endif