65 template<
class T>
void min_equal(T& A, T B) {
if (A > B) A = B; }
66 template<
class T>
void max_equal(T& A, T B) {
if (A < B) A = B; }
68 inline double nint(
double x) {
return floor(x + .5); }
71 return (i + 1) % imax;
75 return (i +
index_t(
int(imax) - 1)) % imax;
78 template <
class T> T clamp(T in, T vmin, T vmax) {
79 if (in<vmin)
return vmin;
80 if (in>vmax)
return vmax;
93 inline void show(
mat3 r) {
98 std::cerr <<
" " << r(i, j);
104 return vec3(M(0, j), M(1, j), M(2, j));
109 M(0, 0)*v[0] + M(0, 1)*v[1] + M(0, 2)*v[2],
110 M(1, 0)*v[0] + M(1, 1)*v[1] + M(1, 2)*v[2],
111 M(2, 0)*v[0] + M(2, 1)*v[1] + M(2, 2)*v[2]
117 int(M(0, 0)*v[0] + M(0, 1)*v[1] + M(0, 2)*v[2]),
118 int(M(1, 0)*v[0] + M(1, 1)*v[1] + M(1, 2)*v[2]),
119 int(M(2, 0)*v[0] + M(2, 1)*v[1] + M(2, 2)*v[2])
123 inline mat3 mat3_from_coeffs(
double a00,
double a01,
double a02,
double a10,
double a11,
double a12,
double a20,
double a21,
double a22) {
125 res(0, 0) = a00; res(0, 1) = a01; res(0, 2) = a02;
126 res(1, 0) = a10; res(1, 1) = a11; res(1, 2) = a12;
127 res(2, 0) = a20; res(2, 1) = a21; res(2, 2) = a22;
131 inline mat3 mat3_from_coeffs(
double* c) {
133 FOR(i, 9) res.data()[i] = c[i];
137 inline
double trace(const
mat3& m) {
return m(0, 0) + m(1, 1) + m(2, 2); }
139 inline double Frobenius_norm(
const mat3& m) {
return trace(m.transpose()*m);}
142 return vec2(M(0, 0)*v[0] + M(0, 1)*v[1], M(1, 0)*v[0] + M(1, 1)*v[1]) ;
146 inline vec3i snap_to_integer(
const vec3& f) {
150 inline bool is_integer(
double d) {
151 return d == floor(d);
156 template <
class T>
inline
157 T& aupp(
int id, vector<T>& data){
158 while (
id <0)
id += int(data.size());
159 while (
id >=
int(data.size()))
id -= int(data.size());
164 template <
class T>
inline
165 T& aupp(
index_t id, vector<T>& data){
166 while (
id >= data.size())
id -= data.size();
174 BBox1() { min = 1e20; max = -1e20; }
175 double length() {
return max - min; }
176 bool intersect(
const BBox1& b)
const {
return contains(b.min) || contains(b.max) || b.contains(min) || b.contains(max); }
177 bool contains(
const double& v)
const {
return v > min && v < max; }
178 bool is_null()
const;
179 void add(
const BBox1& b);
180 void add(
const double& P) { min_equal(min, P); max_equal(max, P);}
181 void dilate(
double eps) { min -= eps; max += eps; }
182 double bary()
const {
return (max + min) / 2.; }