-
Notifications
You must be signed in to change notification settings - Fork 14
/
Math.h
149 lines (130 loc) · 3.5 KB
/
Math.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
This file is part of memview, a real-time memory trace visualization
application.
Copyright (C) 2013 Andrew Clinton
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307, USA.
The GNU General Public License is contained in the file COPYING.
*/
#ifndef Math_H
#define Math_H
#include <stdio.h>
#include <limits.h>
#include <limits>
#include <string>
#define HAS_LAMBDA (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5))
typedef unsigned char uint8;
typedef unsigned short uint16;
typedef unsigned uint32;
typedef unsigned long long uint64;
typedef signed long long int64;
template <typename T>
inline T SYSmax(T a, T b)
{
return a > b ? a : b;
}
template <typename T>
inline T SYSmin(T a, T b)
{
return a < b ? a : b;
}
template <typename T>
inline T SYSclamp(T v, T a, T b)
{
return v < a ? a : (v > b ? b : v);
}
inline int SYSclamp32(int64 val)
{
return (int)SYSclamp(val,
(int64)std::numeric_limits<int>::min(),
(int64)std::numeric_limits<int>::max());
}
inline int SYSclamp32(uint64 val)
{
return (int)SYSmin(val, (uint64)std::numeric_limits<int>::max());
}
template <typename T>
inline T SYSlerp(T v1, T v2, T bias)
{
return v1 + bias*(v2-v1);
}
template <typename T>
inline void SYSswap(T &v1, T &v2)
{
T tmp(v1);
v1 = v2;
v2 = tmp;
}
template <typename T>
inline T SYSabs(T a)
{
return a >= 0 ? a : -a;
}
template <typename T>
struct Box {
void initBounds()
{
T minv = std::numeric_limits<T>::min();
T maxv = std::numeric_limits<T>::max();
l[0] = maxv; h[0] = minv;
l[1] = maxv; h[1] = minv;
}
void initBounds(T xl, T yl, T xh, T yh)
{
l[0] = xl; h[0] = xh;
l[1] = yl; h[1] = yh;
}
void enlargeBounds(T xl, T yl, T xh, T yh)
{
l[0] = SYSmin(l[0], xl); h[0] = SYSmax(h[0], xh);
l[1] = SYSmin(l[1], yl); h[1] = SYSmax(h[1], yh);
}
bool intersect(const Box &other)
{
for (T i = 0; i < 2; i++)
{
l[i] = SYSmax(l[i], other.l[i]);
h[i] = SYSmin(h[i], other.h[i]);
}
return isValid();
}
bool isValid() const { return h[0] > l[0] && h[1] > l[1]; }
void dump() const
{
fprintf(stderr, "box: %lld %lld - %lld %lld\n",
(int64)l[0], (int64)l[1], (int64)h[0], (int64)h[1]);
}
T xmin() const { return l[0]; }
T xmax() const { return h[0]; }
T ymin() const { return l[1]; }
T ymax() const { return h[1]; }
T width() const { return h[0] - l[0]; }
T height() const { return h[1] - l[1]; }
T l[2];
T h[2];
};
#if HAS_LAMBDA
inline std::string SYStoString(int val)
{
return std::to_string(val);
}
#else
#include <sstream>
inline std::string SYStoString(int val)
{
std::stringstream os;
os << val;
return os.str();
}
#endif
#endif