1
0
mirror of https://github.com/zenorogue/hyperrogue.git synced 2024-11-05 05:36:17 +00:00
hyperrogue/hyperpoint.cpp

1185 lines
32 KiB
C++
Raw Normal View History

// Hyperbolic Rogue -- basic computations in non-Euclidean geometry
// Copyright (C) 2011-2019 Zeno Rogue, see 'hyper.cpp' for details
/** \file hyperpoint.cpp
* \brief basic computations in non-Euclidean geometry
*
* This implements hyperpoint (a point in non-Euclidean space), transmatrix (a transformation matrix),
* and various basic routines related to them: rotations, translations, inverses and determinants, etc.
* For nonisotropic geometries, it rather refers to nonisotropic.cpp.
*/
2015-08-08 13:57:52 +00:00
#include "hyper.h"
namespace hr {
2019-08-09 19:18:13 +00:00
#if HDR
static const ld degree = M_PI / 180;
#endif
eGeometry geometry;
eVariation variation;
2019-02-21 17:47:32 +00:00
2016-08-26 09:58:03 +00:00
2019-08-09 23:56:00 +00:00
#if HDR
2019-08-22 11:45:51 +00:00
/** \brief A point in our continuous space
* Originally used for representing points in the hyperbolic plane.
* Currently used for all kinds of supported spaces, as well as
* for all vector spaces (up to 4 dimensions). We are using
* the normalized homogeneous coordinates, which allows us to work with most
* geometries in HyperRogue in a uniform way.
* In the hyperbolic plane, this is the Minkowski hyperboloid model:
* (x,y,z) such that x*x+y*y-z*z == -1 and z > 0.
*
* In spherical geometry, we have x*x+y*y+z*z == 1.
*
* In Euclidean geometry, we have z = 1.
*
* In isotropic 3D geometries an extra coordinate is added.
*
* In nonisotropic coordinates h[3] == 1.
*
* In product geometries the 'z' coordinate is modelled by multiplying all
* three coordinates with exp(z).
*
*/
2019-08-09 23:56:00 +00:00
struct hyperpoint : array<ld, MAXMDIM> {
hyperpoint() {}
2019-09-13 17:23:22 +00:00
#if ISGCC46
2019-08-24 10:58:44 +00:00
#if MAXMDIM == 4
2019-09-13 17:23:22 +00:00
// aaa
constexpr hyperpoint(ld x, ld y, ld z, ld w) : array<ld, MAXMDIM> { (array<ld, MAXMDIM>) {{x, y, z, w}}} {}
2019-08-24 10:58:44 +00:00
#else
2019-09-13 17:23:22 +00:00
constexpr hyperpoint(ld x, ld y, ld z, ld w) : array<ld, MAXMDIM> { (array<ld, MAXMDIM>) {{x, y, z}}} {}
#endif
#else
#if MAXMDIM == 4
constexpr hyperpoint(ld x, ld y, ld z, ld w) : array<ld, MAXMDIM> {{x,y,z,w}} {}
#else
constexpr hyperpoint(ld x, ld y, ld z, ld w) : array<ld, MAXMDIM> {{x,y,z}} {}
#endif
2019-08-24 10:58:44 +00:00
#endif
2019-08-09 23:56:00 +00:00
inline hyperpoint& operator *= (ld d) {
for(int i=0; i<MDIM; i++) self[i] *= d;
return self;
}
inline hyperpoint& operator /= (ld d) {
for(int i=0; i<MDIM; i++) self[i] /= d;
return self;
}
inline hyperpoint& operator += (const hyperpoint h2) {
for(int i=0; i<MDIM; i++) self[i] += h2[i];
return self;
}
inline hyperpoint& operator -= (const hyperpoint h2) {
for(int i=0; i<MDIM; i++) self[i] -= h2[i];
return self;
}
inline friend hyperpoint operator * (ld d, hyperpoint h) { return h *= d; }
inline friend hyperpoint operator * (hyperpoint h, ld d) { return h *= d; }
inline friend hyperpoint operator / (hyperpoint h, ld d) { return h /= d; }
inline friend hyperpoint operator + (hyperpoint h, hyperpoint h2) { return h += h2; }
inline friend hyperpoint operator - (hyperpoint h, hyperpoint h2) { return h -= h2; }
2019-08-24 16:14:38 +00:00
inline friend hyperpoint operator - (hyperpoint h) { return h * -1; }
2019-08-09 23:56:00 +00:00
// cross product
inline friend hyperpoint operator ^ (hyperpoint h1, hyperpoint h2) {
return hyperpoint(
h1[1] * h2[2] - h1[2] * h2[1],
h1[2] * h2[0] - h1[0] * h2[2],
h1[0] * h2[1] - h1[1] * h2[0],
0
);
}
// inner product
inline friend ld operator | (hyperpoint h1, hyperpoint h2) {
ld sum = 0;
for(int i=0; i<MDIM; i++) sum += h1[i] * h2[i];
return sum;
}
};
2019-08-22 11:45:51 +00:00
/** \brief A matrix acting on hr::hyperpoint
* Since we are using homogeneous coordinates for hr::hyperpoint,
* rotations and translations can be represented
* as matrix multiplications. Other applications of matrices in HyperRogue
* (in dimension up to 4) are also implemented using transmatrix.
*/
2019-08-09 23:56:00 +00:00
struct transmatrix {
ld tab[MAXMDIM][MAXMDIM];
hyperpoint& operator [] (int i) { return (hyperpoint&)tab[i][0]; }
const ld * operator [] (int i) const { return tab[i]; }
inline friend hyperpoint operator * (const transmatrix& T, const hyperpoint& H) {
hyperpoint z;
for(int i=0; i<MDIM; i++) {
z[i] = 0;
for(int j=0; j<MDIM; j++) z[i] += T[i][j] * H[j];
}
return z;
}
inline friend transmatrix operator * (const transmatrix& T, const transmatrix& U) {
transmatrix R;
for(int i=0; i<MDIM; i++) for(int j=0; j<MDIM; j++) {
R[i][j] = 0;
for(int k=0; k<MDIM; k++)
R[i][j] += T[i][k] * U[k][j];
}
return R;
}
};
2019-08-22 11:45:51 +00:00
/** returns a diagonal matrix */
2019-08-09 23:56:00 +00:00
constexpr transmatrix diag(ld a, ld b, ld c, ld d) {
#if MAXMDIM==3
return transmatrix{{{a,0,0}, {0,b,0}, {0,0,c}}};
#else
return transmatrix{{{a,0,0,0}, {0,b,0,0}, {0,0,c,0}, {0,0,0,d}}};
#endif
}
2019-08-24 10:58:44 +00:00
constexpr hyperpoint Hypc = hyperpoint(0, 0, 0, 0);
2019-08-09 23:56:00 +00:00
2019-08-22 11:45:51 +00:00
/** identity matrix */
2019-08-24 10:58:44 +00:00
constexpr transmatrix Id = diag(1,1,1,1);
2019-08-09 23:56:00 +00:00
2019-08-22 11:45:51 +00:00
/** zero matrix */
2019-08-24 10:58:44 +00:00
constexpr transmatrix Zero = diag(0,0,0,0);
2019-08-09 23:56:00 +00:00
2019-08-22 11:45:51 +00:00
/** mirror image */
2019-08-24 10:58:44 +00:00
constexpr transmatrix Mirror = diag(1,-1,1,1);
2019-08-22 11:45:51 +00:00
/** mirror image: flip in the Y coordinate */
2019-08-24 10:58:44 +00:00
constexpr transmatrix MirrorY = diag(1,-1,1,1);
2019-08-09 23:56:00 +00:00
2019-08-22 11:45:51 +00:00
/** mirror image: flip in the X coordinate */
2019-08-24 10:58:44 +00:00
constexpr transmatrix MirrorX = diag(-1,1,1,1);
2019-08-09 23:56:00 +00:00
2019-08-22 11:45:51 +00:00
/** mirror image: flip in the Z coordinate */
2019-08-24 10:58:44 +00:00
constexpr transmatrix MirrorZ = diag(1,1,-1,1);
2019-08-09 23:56:00 +00:00
2019-08-22 11:45:51 +00:00
/** rotate by PI in the XY plane */
2019-08-24 10:58:44 +00:00
constexpr transmatrix pispin = diag(-1,-1,1,1);
2019-08-09 23:56:00 +00:00
2019-08-22 11:45:51 +00:00
/** central symmetry matrix */
2019-08-24 10:58:44 +00:00
constexpr transmatrix centralsym = diag(-1,-1,-1,-1);
2019-08-09 23:56:00 +00:00
2019-08-18 11:05:47 +00:00
inline hyperpoint hpxyz(ld x, ld y, ld z) { return MDIM == 3 ? hyperpoint(x,y,z,0) : hyperpoint(x,y,0,z); }
inline hyperpoint hpxyz3(ld x, ld y, ld z, ld w) { return MDIM == 3 ? hyperpoint(x,y,w,0) : hyperpoint(x,y,z,w); }
2019-08-24 10:58:44 +00:00
constexpr hyperpoint point3(ld x, ld y, ld z) { return hyperpoint(x,y,z,0); }
constexpr hyperpoint point31(ld x, ld y, ld z) { return hyperpoint(x,y,z,1); }
constexpr hyperpoint point2(ld x, ld y) { return hyperpoint(x,y,0,0); }
2019-08-09 23:56:00 +00:00
2019-08-24 10:58:44 +00:00
constexpr hyperpoint C02 = hyperpoint(0,0,1,0);
constexpr hyperpoint C03 = hyperpoint(0,0,0,1);
2019-08-09 23:56:00 +00:00
2019-08-22 11:45:51 +00:00
/** C0 is the origin in our space */
#define C0 (MDIM == 3 ? C02 : C03)
2019-08-09 23:56:00 +00:00
#endif
2015-08-08 13:57:52 +00:00
// basic functions and types
//===========================
2017-03-23 10:53:57 +00:00
#ifndef M_PI
#define M_PI 3.14159265358979
2016-08-26 09:58:03 +00:00
#endif
2015-08-08 13:57:52 +00:00
2019-08-09 19:00:52 +00:00
EX ld squar(ld x) { return x*x; }
2015-08-08 13:57:52 +00:00
EX int sig(int z) { return ginf[geometry].g.sig[z]; }
2015-08-08 13:57:52 +00:00
2019-08-09 19:00:52 +00:00
EX int curvature() {
switch(cgclass) {
case gcEuclid: return 0;
case gcHyperbolic: return -1;
case gcSphere: return 1;
case gcProduct: return PIU(curvature());
default: return 0;
}
}
2019-08-09 19:00:52 +00:00
EX ld sin_auto(ld x) {
switch(cgclass) {
case gcEuclid: return x;
case gcHyperbolic: return sinh(x);
case gcSphere: return sin(x);
case gcProduct: return PIU(sin_auto(x));
default: return x;
}
}
2019-08-09 19:00:52 +00:00
EX ld asin_auto(ld x) {
switch(cgclass) {
case gcEuclid: return x;
case gcHyperbolic: return asinh(x);
case gcSphere: return asin(x);
case gcProduct: return PIU(asin_auto(x));
default: return x;
}
}
2019-08-09 19:00:52 +00:00
EX ld acos_auto(ld x) {
switch(cgclass) {
case gcHyperbolic: return acosh(x);
case gcSphere: return acos(x);
case gcProduct: return PIU(acos_auto(x));
default: return x;
}
}
2019-08-09 19:00:52 +00:00
EX ld volume_auto(ld r) {
2019-03-30 16:45:56 +00:00
switch(cgclass) {
case gcEuclid: return 4 * r * r * r / 3 * M_PI;
case gcHyperbolic: return M_PI * (sinh(2*r) - 2 * r);
case gcSphere: return M_PI * (2 * r - sin(2*r));
default: return 0;
}
}
2019-08-26 07:06:39 +00:00
EX ld asin_clamp(ld x) { return x>1 ? M_PI/2 : x<-1 ? -M_PI/2 : std::isnan(x) ? 0 : asin(x); }
2019-08-09 19:00:52 +00:00
EX ld asin_auto_clamp(ld x) {
switch(cgclass) {
case gcEuclid: return x;
case gcHyperbolic: return asinh(x);
2019-08-26 07:06:39 +00:00
case gcSphere: return asin_clamp(x);
default: return x;
}
}
2019-08-18 14:58:44 +00:00
EX ld acos_auto_clamp(ld x) {
switch(cgclass) {
case gcHyperbolic: return x < 1 ? 0 : acosh(x);
case gcSphere: return x > 1 ? 0 : x < -1 ? M_PI : acos(x);
case gcProduct: return PIU(acos_auto_clamp(x));
default: return x;
}
}
2019-08-09 19:00:52 +00:00
EX ld cos_auto(ld x) {
switch(cgclass) {
case gcEuclid: return 1;
case gcHyperbolic: return cosh(x);
case gcSphere: return cos(x);
case gcProduct: return PIU(cos_auto(x));
default: return 1;
}
}
2019-08-09 19:00:52 +00:00
EX ld tan_auto(ld x) {
switch(cgclass) {
case gcEuclid: return x;
case gcHyperbolic: return tanh(x);
case gcSphere: return tan(x);
case gcProduct: return PIU(tan_auto(x));
default: return 1;
}
}
2019-08-09 19:00:52 +00:00
EX ld atan_auto(ld x) {
switch(cgclass) {
case gcEuclid: return x;
case gcHyperbolic: return atanh(x);
case gcSphere: return atan(x);
case gcProduct: return PIU(atan_auto(x));
2019-07-23 13:08:07 +00:00
default: return x;
}
}
2019-08-09 19:00:52 +00:00
EX ld atan2_auto(ld y, ld x) {
switch(cgclass) {
case gcEuclid: return y/x;
case gcHyperbolic: return atanh(y/x);
case gcSphere: return atan2(y, x);
case gcProduct: return PIU(atan2_auto(y, x));
2019-07-23 13:08:07 +00:00
default: return y/x;
}
}
2019-08-22 11:45:51 +00:00
/** This function returns the length of the edge opposite the angle alpha in
* a triangle with angles alpha, beta, gamma. This is called the cosine rule,
* and of course works only in non-Euclidean geometry. */
2019-08-09 19:00:52 +00:00
EX ld edge_of_triangle_with_angles(ld alpha, ld beta, ld gamma) {
return acos_auto((cos(alpha) + cos(beta) * cos(gamma)) / (sin(beta) * sin(gamma)));
}
EX hyperpoint hpxy(ld x, ld y) {
return hpxyz(x,y, sl2 ? sqrt(1+x*x+y*y) : translatable ? 1 : sphere ? sqrt(1-x*x-y*y) : sqrt(1+x*x+y*y));
2019-02-22 19:58:40 +00:00
}
EX hyperpoint hpxy3(ld x, ld y, ld z) {
return hpxyz3(x,y,z, sl2 ? sqrt(1+x*x+y*y-z*z) :translatable ? 1 : sphere ? sqrt(1-x*x-y*y-z*z) : sqrt(1+x*x+y*y+z*z));
2015-08-08 13:57:52 +00:00
}
#if HDR
2015-08-08 13:57:52 +00:00
// a point (I hope this number needs no comments ;) )
2019-08-24 10:58:44 +00:00
constexpr hyperpoint Cx12 = hyperpoint(1,0,1.41421356237,0);
constexpr hyperpoint Cx13 = hyperpoint(1,0,0,1.41421356237);
2019-05-08 16:33:08 +00:00
#define Cx1 (GDIM==2?Cx12:Cx13)
#endif
2015-08-08 13:57:52 +00:00
2019-08-09 19:00:52 +00:00
EX bool zero_d(int d, hyperpoint h) {
for(int i=0; i<d; i++) if(h[i]) return false;
return true;
}
2019-08-22 11:45:51 +00:00
/** this function returns approximate square of distance between two points
* (in the spherical analogy, this would be the distance in the 3D space,
* through the interior, not on the surface)
* also used to verify whether a point h1 is on the hyperbolic plane by using Hypc for h2
*/
2015-08-08 13:57:52 +00:00
2019-08-09 19:00:52 +00:00
EX ld intval(const hyperpoint &h1, const hyperpoint &h2) {
2019-02-17 17:47:19 +00:00
ld res = 0;
for(int i=0; i<MDIM; i++) res += squar(h1[i] - h2[i]) * sig(i);
2017-05-31 16:33:50 +00:00
if(elliptic) {
2019-02-17 17:47:19 +00:00
ld res2 = 0;
for(int i=0; i<MDIM; i++) res2 += squar(h1[i] + h2[i]) * sig(i);
return min(res, res2);
2017-05-31 16:33:50 +00:00
}
2019-02-17 17:47:19 +00:00
return res;
2017-03-23 10:53:57 +00:00
}
EX ld quickdist(const hyperpoint &h1, const hyperpoint &h2) {
if(prod) return hdist(h1, h2);
return intval(h1, h2);
}
2019-08-22 11:45:51 +00:00
/** square Euclidean hypotenuse in the first d dimensions */
2019-08-09 19:00:52 +00:00
EX ld sqhypot_d(int d, const hyperpoint& h) {
2019-02-22 19:58:40 +00:00
ld sum = 0;
for(int i=0; i<d; i++) sum += h[i]*h[i];
return sum;
2017-12-27 05:31:47 +00:00
}
2019-08-22 11:45:51 +00:00
/** Euclidean hypotenuse in the first d dimensions */
2019-08-09 19:00:52 +00:00
EX ld hypot_d(int d, const hyperpoint& h) {
return sqrt(sqhypot_d(d, h));
}
2019-08-09 19:00:52 +00:00
EX ld zlevel(const hyperpoint &h) {
2019-08-24 09:55:45 +00:00
if(sl2) return sqrt(-intval(h, Hypc));
else if(translatable) return h[LDIM];
2017-03-23 10:53:57 +00:00
else if(sphere) return sqrt(intval(h, Hypc));
2019-08-18 14:58:44 +00:00
else if(prod) return log(sqrt(abs(intval(h, Hypc)))); /* abs works with both underlying spherical and hyperbolic */
else return (h[LDIM] < 0 ? -1 : 1) * sqrt(-intval(h, Hypc));
2015-08-08 13:57:52 +00:00
}
2019-08-09 19:00:52 +00:00
EX ld hypot_auto(ld x, ld y) {
switch(cgclass) {
case gcEuclid:
return hypot(x, y);
case gcHyperbolic:
return acosh(cosh(x) * cosh(y));
case gcSphere:
return acos(cos(x) * cos(y));
default:
return hypot(x, y);
}
}
2019-08-22 11:45:51 +00:00
/** normalize the homogeneous coordinates */
2019-08-09 19:00:52 +00:00
EX hyperpoint normalize(hyperpoint H) {
if(prod) return H;
2018-05-20 13:30:43 +00:00
ld Z = zlevel(H);
2019-02-17 17:47:19 +00:00
for(int c=0; c<MDIM; c++) H[c] /= Z;
2018-04-03 23:19:21 +00:00
return H;
}
2019-08-22 11:45:51 +00:00
/** get the center of the line segment from H1 to H2 */
2019-09-03 06:26:01 +00:00
EX hyperpoint mid(const hyperpoint& H1, const hyperpoint& H2) {
if(prod) {
auto d1 = product_decompose(H1);
auto d2 = product_decompose(H2);
return zshift(PIU( mid(d1.second, d2.second) ), (d1.first + d2.first) / 2);
}
2018-04-03 23:19:21 +00:00
return normalize(H1 + H2);
2015-08-08 13:57:52 +00:00
}
2019-08-22 11:45:51 +00:00
/** like mid, but take 3D into account */
2019-08-09 19:00:52 +00:00
EX hyperpoint midz(const hyperpoint& H1, const hyperpoint& H2) {
if(prod) return mid(H1, H2);
2018-04-03 23:19:21 +00:00
hyperpoint H3 = H1 + H2;
2016-08-26 09:58:03 +00:00
ld Z = 2;
2018-04-03 23:19:21 +00:00
if(!euclid) Z = zlevel(H3) * 2 / (zlevel(H1) + zlevel(H2));
2019-02-17 17:47:19 +00:00
for(int c=0; c<MDIM; c++) H3[c] /= Z;
2016-08-26 09:58:03 +00:00
2017-03-23 10:53:57 +00:00
return H3;
2016-08-26 09:58:03 +00:00
}
2015-08-08 13:57:52 +00:00
// matrices
//==========
2019-08-22 11:45:51 +00:00
/** rotate by alpha degrees in the coordinates a, b */
2019-08-09 19:00:52 +00:00
EX transmatrix cspin(int a, int b, ld alpha) {
2015-08-08 13:57:52 +00:00
transmatrix T = Id;
2019-02-17 17:47:19 +00:00
T[a][a] = +cos(alpha); T[a][b] = +sin(alpha);
T[b][a] = -sin(alpha); T[b][b] = +cos(alpha);
2015-08-08 13:57:52 +00:00
return T;
}
2019-08-22 11:45:51 +00:00
/** rotate by alpha degrees in the XY plane */
2019-08-09 19:00:52 +00:00
EX transmatrix spin(ld alpha) { return cspin(0, 1, alpha); }
2019-02-17 17:47:19 +00:00
2019-08-09 19:00:52 +00:00
EX transmatrix random_spin() {
if(WDIM == 2) return spin(randd() * 2 * M_PI);
2019-03-30 16:51:37 +00:00
else {
ld alpha2 = acos(randd() * 2 - 1);
ld alpha = randd() * 2 * M_PI;
ld alpha3 = randd() * 2 * M_PI;
return cspin(0, 2, alpha2) * cspin(0, 1, alpha) * cspin(1, 2, alpha3);
2019-03-30 16:51:37 +00:00
}
}
2019-08-09 19:00:52 +00:00
EX transmatrix eupush(ld x, ld y) {
2015-08-08 13:57:52 +00:00
transmatrix T = Id;
T[0][LDIM] = x;
T[1][LDIM] = y;
2019-02-22 19:58:40 +00:00
return T;
}
2019-08-09 19:00:52 +00:00
EX transmatrix eupush(hyperpoint h) {
2019-08-06 10:00:46 +00:00
if(nonisotropic) return nisot::translate(h);
2017-12-14 01:53:29 +00:00
transmatrix T = Id;
for(int i=0; i<GDIM; i++) T[i][LDIM] = h[i];
2017-12-14 01:53:29 +00:00
return T;
}
2019-08-09 19:00:52 +00:00
EX transmatrix eupush3(ld x, ld y, ld z) {
2019-08-24 09:55:45 +00:00
if(sl2) return slr::translate(slr::xyz_point(x, y, z));
2019-08-06 10:00:46 +00:00
return eupush(point3(x, y, z));
}
2019-08-09 19:00:52 +00:00
EX transmatrix euscalezoom(hyperpoint h) {
2017-12-14 01:53:29 +00:00
transmatrix T = Id;
T[0][0] = h[0];
T[0][1] = -h[1];
T[1][0] = h[1];
T[1][1] = h[0];
return T;
}
2019-08-09 19:00:52 +00:00
EX transmatrix euaffine(hyperpoint h) {
2017-12-14 01:53:29 +00:00
transmatrix T = Id;
2018-01-03 00:05:03 +00:00
T[0][1] = h[0];
T[1][1] = exp(h[1]);
2017-12-14 01:53:29 +00:00
return T;
}
2019-08-09 19:00:52 +00:00
EX transmatrix cpush(int cid, ld alpha) {
2015-08-08 13:57:52 +00:00
transmatrix T = Id;
2019-08-17 23:31:37 +00:00
if(prod && cid == 2)
2019-08-18 11:05:47 +00:00
return mscale(Id, alpha);
2019-08-06 19:04:40 +00:00
if(nonisotropic)
2019-08-06 10:00:46 +00:00
return eupush3(cid == 0 ? alpha : 0, cid == 1 ? alpha : 0, cid == 2 ? alpha : 0);
T[LDIM][LDIM] = T[cid][cid] = cos_auto(alpha);
T[cid][LDIM] = sin_auto(alpha);
T[LDIM][cid] = -curvature() * sin_auto(alpha);
2015-08-08 13:57:52 +00:00
return T;
}
2019-02-17 17:47:19 +00:00
// push alpha units to the right
2019-08-09 19:00:52 +00:00
EX transmatrix xpush(ld alpha) { return cpush(0, alpha); }
2017-03-23 10:53:57 +00:00
2019-08-09 19:00:52 +00:00
EX bool eqmatrix(transmatrix A, transmatrix B, ld eps IS(.01)) {
for(int i=0; i<MDIM; i++)
for(int j=0; j<MDIM; j++)
if(std::abs(A[i][j] - B[i][j]) > eps)
return false;
return true;
}
#if MAXMDIM >= 4
// in the 3D space, move the point h orthogonally to the (x,y) plane by z units
2019-08-09 19:00:52 +00:00
EX hyperpoint orthogonal_move(const hyperpoint& h, ld z) {
2019-08-18 11:05:47 +00:00
if(prod) return zshift(h, z);
2019-08-24 12:49:49 +00:00
if(sl2) return slr::translate(h) * cpush0(2, z);
if(!hyperbolic) return rgpushxto0(h) * cpush(2, z) * C0;
2019-08-06 10:00:46 +00:00
if(nil) return nisot::translate(h) * cpush0(2, z);
if(translatable) return hpxy3(h[0], h[1], h[2] + z);
ld u = 1;
if(h[2]) z += asin_auto(h[2]), u /= acos_auto(z);
u *= cos_auto(z);
return hpxy3(h[0] * u, h[1] * u, sinh(z));
}
#endif
2015-08-08 13:57:52 +00:00
// push alpha units vertically
2019-08-09 19:00:52 +00:00
EX transmatrix ypush(ld alpha) { return cpush(1, alpha); }
2019-02-17 17:47:19 +00:00
2019-08-09 19:00:52 +00:00
EX transmatrix zpush(ld z) { return cpush(2, z); }
2019-08-09 19:00:52 +00:00
EX transmatrix matrix3(ld a, ld b, ld c, ld d, ld e, ld f, ld g, ld h, ld i) {
#if MAXMDIM==3
return transmatrix {{{a,b,c},{d,e,f},{g,h,i}}};
#else
2019-08-15 13:05:43 +00:00
if(GDIM == 2)
return transmatrix {{{a,b,c,0},{d,e,f,0},{g,h,i,0},{0,0,0,1}}};
2019-05-08 18:15:50 +00:00
else
return transmatrix {{{a,b,0,c},{d,e,0,f},{0,0,1,0},{g,h,0,i}}};
#endif
2019-02-22 19:58:40 +00:00
}
2019-08-09 19:00:52 +00:00
EX transmatrix matrix4(ld a, ld b, ld c, ld d, ld e, ld f, ld g, ld h, ld i, ld j, ld k, ld l, ld m, ld n, ld o, ld p) {
#if MAXMDIM==3
return transmatrix {{{a,b,d},{e,f,h},{m,n,p}}};
#else
return transmatrix {{{a,b,c,d},{e,f,g,h},{i,j,k,l},{m,n,o,p}}};
#endif
2019-02-22 19:58:40 +00:00
}
#if MAXMDIM >= 4
2019-08-09 19:00:52 +00:00
EX void swapmatrix(transmatrix& T) {
for(int i=0; i<4; i++) swap(T[i][2], T[i][3]);
for(int i=0; i<4; i++) swap(T[2][i], T[3][i]);
2019-08-15 13:05:43 +00:00
if(GDIM == 3) {
for(int i=0; i<4; i++) T[i][2] = T[2][i] = 0;
T[2][2] = 1;
}
fixmatrix(T);
for(int i=0; i<4; i++) for(int j=0; j<4; j++) if(isnan(T[i][j])) T = Id;
}
2019-05-15 13:20:37 +00:00
2019-08-09 19:00:52 +00:00
EX void swapmatrix(hyperpoint& h) {
2019-05-15 13:20:37 +00:00
swap(h[2], h[3]);
}
#endif
2019-08-09 19:00:52 +00:00
EX transmatrix parabolic1(ld u) {
2019-02-17 17:47:19 +00:00
if(euclid)
return ypush(u);
2017-03-23 10:53:57 +00:00
else {
2019-02-22 19:58:40 +00:00
ld diag = u*u/2;
return matrix3(
-diag+1, u, diag,
-u, 1, u,
-diag, u, diag+1
);
}
}
2019-08-09 19:00:52 +00:00
EX transmatrix parabolic13(ld u, ld v) {
2019-02-22 19:58:40 +00:00
if(euclid)
return ypush(u);
else {
ld diag = (u*u+v*v)/2;
return matrix4(
-diag+1, u, v, diag,
-u, 1, 0, u,
-v, 0, 1, v,
-diag, u, v, diag+1
);
2017-03-23 10:53:57 +00:00
}
2015-08-08 13:57:52 +00:00
}
2019-08-09 19:00:52 +00:00
EX transmatrix spintoc(const hyperpoint& H, int t, int f) {
2019-02-17 17:47:19 +00:00
transmatrix T = Id;
ld R = hypot(H[f], H[t]);
if(R >= 1e-12) {
T[t][t] = +H[t]/R; T[t][f] = +H[f]/R;
T[f][t] = -H[f]/R; T[f][f] = +H[t]/R;
}
return T;
}
2019-08-22 11:45:51 +00:00
/** an Euclidean rotation in the axes (t,f) which rotates
* the point H to the positive 't' axis
*/
2019-08-09 19:00:52 +00:00
EX transmatrix rspintoc(const hyperpoint& H, int t, int f) {
2015-08-08 13:57:52 +00:00
transmatrix T = Id;
2019-02-17 17:47:19 +00:00
ld R = hypot(H[f], H[t]);
2015-08-08 13:57:52 +00:00
if(R >= 1e-12) {
2019-02-17 17:47:19 +00:00
T[t][t] = +H[t]/R; T[t][f] = -H[f]/R;
T[f][t] = +H[f]/R; T[f][f] = +H[t]/R;
2015-08-08 13:57:52 +00:00
}
return T;
}
2019-08-22 11:45:51 +00:00
/** an isometry which takes the point H to the positive X axis
* \see rspintox
*/
2019-08-09 19:00:52 +00:00
EX transmatrix spintox(const hyperpoint& H) {
if(GDIM == 2 || prod) return spintoc(H, 0, 1);
2019-02-17 17:47:19 +00:00
transmatrix T1 = spintoc(H, 0, 1);
return spintoc(T1*H, 0, 2) * T1;
}
2019-08-22 11:45:51 +00:00
/** inverse of hr::spintox
*/
EX transmatrix rspintox(const hyperpoint& H) {
if(GDIM == 2 || prod) return rspintoc(H, 0, 1);
transmatrix T1 = spintoc(H, 0, 1);
return rspintoc(H, 0, 1) * rspintoc(T1*H, 0, 2);
}
/** for H on the X axis, this matrix pushes H to C0
* \see gpushxto0
*/
EX transmatrix pushxto0(const hyperpoint& H) {
transmatrix T = Id;
T[0][0] = +H[LDIM]; T[0][LDIM] = -H[0];
T[LDIM][0] = curvature() * H[0]; T[LDIM][LDIM] = +H[LDIM];
return T;
}
/** set the i-th column of T to H */
2019-08-09 19:00:52 +00:00
EX void set_column(transmatrix& T, int i, const hyperpoint& H) {
2019-02-17 17:47:19 +00:00
for(int j=0; j<MDIM; j++)
2018-04-03 21:39:18 +00:00
T[j][i] = H[j];
}
2019-08-22 11:45:51 +00:00
/** build a matrix using the given vectors as columns */
2019-08-09 19:00:52 +00:00
EX transmatrix build_matrix(hyperpoint h1, hyperpoint h2, hyperpoint h3, hyperpoint h4) {
transmatrix T;
2019-02-17 17:47:19 +00:00
for(int i=0; i<MDIM; i++)
2018-04-03 23:19:21 +00:00
T[i][0] = h1[i],
T[i][1] = h2[i],
2019-02-22 19:58:40 +00:00
T[i][2] = h3[i];
2019-05-15 07:36:25 +00:00
if(MAXMDIM == 4) for(int i=0; i<MDIM; i++) T[i][3] = h4[i];
2018-04-03 23:19:21 +00:00
return T;
}
2019-08-22 11:45:51 +00:00
/** for H on the X axis, this matrix pushes C0 to H
* \see rgpushxto0
*/
2015-08-08 13:57:52 +00:00
2019-08-09 19:00:52 +00:00
EX transmatrix rpushxto0(const hyperpoint& H) {
2015-08-08 13:57:52 +00:00
transmatrix T = Id;
T[0][0] = +H[LDIM]; T[0][LDIM] = H[0];
T[LDIM][0] = -curvature() * H[0]; T[LDIM][LDIM] = +H[LDIM];
2015-08-08 13:57:52 +00:00
return T;
}
2019-08-09 19:00:52 +00:00
EX transmatrix ggpushxto0(const hyperpoint& H, ld co) {
2019-08-06 10:00:46 +00:00
if(translatable) {
2019-02-22 19:58:40 +00:00
return eupush(co * H);
}
if(prod) {
auto d = product_decompose(H);
2019-08-18 11:05:47 +00:00
return mscale(PIU(ggpushxto0(d.second, co)), d.first * co);
}
2019-02-17 17:47:19 +00:00
transmatrix res = Id;
2019-05-08 16:33:08 +00:00
if(sqhypot_d(GDIM, H) < 1e-12) return res;
ld fac = (H[LDIM]-1) / sqhypot_d(GDIM, H);
2019-05-08 16:33:08 +00:00
for(int i=0; i<GDIM; i++)
for(int j=0; j<GDIM; j++)
2019-02-17 17:47:19 +00:00
res[i][j] += H[i] * H[j] * fac;
2019-05-08 16:33:08 +00:00
for(int d=0; d<GDIM; d++)
res[d][LDIM] = co * H[d],
res[LDIM][d] = -curvature() * co * H[d];
res[LDIM][LDIM] = H[LDIM];
2019-02-17 17:47:19 +00:00
return res;
}
2019-08-22 11:45:51 +00:00
/** a translation matrix which takes H to 0 */
2019-08-09 19:00:52 +00:00
EX transmatrix gpushxto0(const hyperpoint& H) {
2019-02-17 17:47:19 +00:00
return ggpushxto0(H, -1);
2015-08-08 13:57:52 +00:00
}
2019-08-22 11:45:51 +00:00
/** a translation matrix which takes 0 to H */
2019-08-09 19:00:52 +00:00
EX transmatrix rgpushxto0(const hyperpoint& H) {
2019-02-17 17:47:19 +00:00
return ggpushxto0(H, 1);
2015-08-08 13:57:52 +00:00
}
2019-08-22 11:45:51 +00:00
/** \brief Fix the numerical inaccuracies in the isometry T
* The nature of hyperbolic geometry makes the computations numerically unstable.
* The numerical errors tend to accumulate, eventually destroying the projection.
* This function fixes this problem by replacing T with a 'correct' isometry.
*/
2015-08-08 13:57:52 +00:00
2019-08-09 19:00:52 +00:00
EX void fixmatrix(transmatrix& T) {
2019-08-06 10:00:46 +00:00
if(nonisotropic) ; // T may be inverse... do not do that
2019-08-18 20:28:10 +00:00
else if(prod) {
auto z = zlevel(tC0(T));
T = mscale(T, -z);
PIU(fixmatrix(T));
T = mscale(T, +z);
}
2019-07-23 13:08:07 +00:00
else if(euclid) {
2019-05-08 16:33:08 +00:00
for(int x=0; x<GDIM; x++) for(int y=0; y<=x; y++) {
2016-01-02 10:09:13 +00:00
ld dp = 0;
2019-05-08 16:33:08 +00:00
for(int z=0; z<GDIM; z++) dp += T[z][x] * T[z][y];
2016-01-02 10:09:13 +00:00
if(y == x) dp = 1 - sqrt(1/dp);
2019-05-08 16:33:08 +00:00
for(int z=0; z<GDIM; z++) T[z][x] -= dp * T[z][y];
2016-01-02 10:09:13 +00:00
}
for(int x=0; x<GDIM; x++) T[LDIM][x] = 0;
T[LDIM][LDIM] = 1;
2016-01-02 10:09:13 +00:00
}
2019-02-17 17:47:19 +00:00
else for(int x=0; x<MDIM; x++) for(int y=0; y<=x; y++) {
2015-08-08 13:57:52 +00:00
ld dp = 0;
2019-02-17 17:47:19 +00:00
for(int z=0; z<MDIM; z++) dp += T[z][x] * T[z][y] * sig(z);
2015-08-08 13:57:52 +00:00
if(y == x) dp = 1 - sqrt(sig(x)/dp);
2019-02-17 17:47:19 +00:00
for(int z=0; z<MDIM; z++) T[z][x] -= dp * T[z][y];
2015-08-08 13:57:52 +00:00
}
}
2019-08-22 11:45:51 +00:00
/** determinant */
2019-08-09 19:00:52 +00:00
EX ld det(const transmatrix& T) {
2019-05-08 16:33:08 +00:00
if(GDIM == 2) {
ld det = 0;
for(int i=0; i<3; i++)
det += T[0][i] * T[1][(i+1)%3] * T[2][(i+2)%3];
for(int i=0; i<3; i++)
det -= T[0][i] * T[1][(i+2)%3] * T[2][(i+1)%3];
return det;
}
else {
ld det = 1;
transmatrix M = T;
for(int a=0; a<MDIM; a++) {
2019-05-08 16:33:08 +00:00
for(int b=a; b<=GDIM; b++)
if(M[b][a]) {
if(b != a)
for(int c=a; c<MDIM; c++) tie(M[b][c], M[a][c]) = make_pair(-M[a][c], M[b][c]);
break;
}
if(!M[a][a]) return 0;
2019-05-08 16:33:08 +00:00
for(int b=a+1; b<=GDIM; b++) {
ld co = -M[b][a] / M[a][a];
for(int c=a; c<MDIM; c++) M[b][c] += M[a][c] * co;
2019-02-17 17:47:19 +00:00
}
det *= M[a][a];
2019-02-17 17:47:19 +00:00
}
return det;
2019-02-17 17:47:19 +00:00
}
2017-07-22 23:33:27 +00:00
}
2019-08-22 11:45:51 +00:00
/** warning about incorrect inverse */
void inverse_error(const transmatrix& T) {
println(hlog, "Warning: inverting a singular matrix: ", T);
}
2019-08-22 11:45:51 +00:00
/** inverse */
2019-08-09 19:00:52 +00:00
EX transmatrix inverse(const transmatrix& T) {
2019-08-22 11:46:01 +00:00
if(MDIM == 3) {
ld d = det(T);
transmatrix T2;
if(d == 0) {
inverse_error(T);
return Id;
}
for(int i=0; i<3; i++)
for(int j=0; j<3; j++)
T2[j][i] = (T[(i+1)%3][(j+1)%3] * T[(i+2)%3][(j+2)%3] - T[(i+1)%3][(j+2)%3] * T[(i+2)%3][(j+1)%3]) / d;
return T2;
}
else {
transmatrix T1 = T;
transmatrix T2 = Id;
2015-08-08 13:57:52 +00:00
for(int a=0; a<MDIM; a++) {
int best = a;
for(int b=a+1; b<MDIM; b++)
if(abs(T1[b][a]) > abs(T1[best][a]))
best = b;
int b = best;
if(b != a)
for(int c=0; c<MDIM; c++)
swap(T1[b][c], T1[a][c]), swap(T2[b][c], T2[a][c]);
if(!T1[a][a]) { inverse_error(T); return Id; }
2019-05-08 16:33:08 +00:00
for(int b=a+1; b<=GDIM; b++) {
ld co = -T1[b][a] / T1[a][a];
for(int c=0; c<MDIM; c++) T1[b][c] += T1[a][c] * co, T2[b][c] += T2[a][c] * co;
2019-02-17 17:47:19 +00:00
}
}
for(int a=MDIM-1; a>=0; a--) {
for(int b=0; b<a; b++) {
ld co = -T1[b][a] / T1[a][a];
for(int c=0; c<MDIM; c++) T1[b][c] += T1[a][c] * co, T2[b][c] += T2[a][c] * co;
}
ld co = 1 / T1[a][a];
for(int c=0; c<MDIM; c++) T1[a][c] *= co, T2[a][c] *= co;
2019-02-17 17:47:19 +00:00
}
return T2;
2019-02-17 17:47:19 +00:00
}
2015-08-08 13:57:52 +00:00
}
2016-08-26 09:58:03 +00:00
EX pair<ld, hyperpoint> product_decompose(hyperpoint h) {
ld z = zlevel(h);
2019-08-18 11:05:47 +00:00
return make_pair(z, mscale(h, -z));
}
2019-08-22 11:45:51 +00:00
/** distance from mh and 0 */
2019-08-09 19:00:52 +00:00
EX ld hdist0(const hyperpoint& mh) {
2018-02-27 18:21:43 +00:00
switch(cgclass) {
case gcHyperbolic:
if(mh[LDIM] < 1) return 0;
return acosh(mh[LDIM]);
2018-02-27 18:21:43 +00:00
case gcEuclid: {
2019-05-08 16:33:08 +00:00
return hypot_d(GDIM, mh);
2018-02-27 18:21:43 +00:00
}
case gcSphere: {
ld res = mh[LDIM] >= 1 ? 0 : mh[LDIM] <= -1 ? M_PI : acos(mh[LDIM]);
2018-02-27 18:21:43 +00:00
if(elliptic && res > M_PI/2) res = M_PI-res;
return res;
}
case gcProduct: {
auto d1 = product_decompose(mh);
return hypot(PIU(hdist0(d1.second)), d1.first);
}
2019-08-24 09:55:45 +00:00
case gcSL2: {
auto cosh_r = hypot(mh[2], mh[3]);
auto phi = atan2(mh[2], mh[3]);
return hypot(cosh_r < 1 ? 0 : acosh(cosh_r), phi);
}
2019-10-06 09:51:10 +00:00
case gcNil: {
ld bz = mh[0] * mh[1] / 2;
return hypot(mh[0], mh[1]) + abs(mh[2] - bz);
}
2018-02-27 18:21:43 +00:00
default:
2019-07-23 13:08:07 +00:00
return hypot_d(GDIM, mh);
2017-05-31 16:33:50 +00:00
}
2017-03-23 10:53:57 +00:00
}
2019-08-22 11:45:51 +00:00
/** length of a circle of radius r */
2019-08-09 19:00:52 +00:00
EX ld circlelength(ld r) {
2018-02-27 18:21:43 +00:00
switch(cgclass) {
case gcEuclid:
return 2 * M_PI * r;
case gcHyperbolic:
return 2 * M_PI * sinh(r);
case gcSphere:
return 2 * M_PI * sin(r);
default:
2019-07-23 13:08:07 +00:00
return 2 * M_PI * r;
2018-02-27 18:21:43 +00:00
}
}
2019-08-22 11:45:51 +00:00
/* distance between h1 and h2 */
2019-08-09 19:00:52 +00:00
EX ld hdist(const hyperpoint& h1, const hyperpoint& h2) {
2018-02-27 18:21:43 +00:00
ld iv = intval(h1, h2);
switch(cgclass) {
case gcEuclid:
if(iv < 0) return 0;
2018-02-27 18:21:43 +00:00
return sqrt(iv);
case gcHyperbolic:
if(iv < 0) return 0;
2018-02-27 18:21:43 +00:00
return 2 * asinh(sqrt(iv) / 2);
case gcSphere:
2018-05-01 17:35:09 +00:00
return 2 * asin_auto_clamp(sqrt(iv) / 2);
case gcProduct: {
auto d1 = product_decompose(h1);
auto d2 = product_decompose(h2);
return hypot(PIU(hdist(d1.second, d2.second)), d1.first - d2.first);
}
case gcSL2:
return hdist0(inverse(slr::translate(h1)) * h2);
2018-02-27 18:21:43 +00:00
default:
2019-07-23 13:08:07 +00:00
if(iv < 0) return 0;
return sqrt(iv);
2018-02-27 18:21:43 +00:00
}
2016-08-26 09:58:03 +00:00
}
2019-08-09 19:00:52 +00:00
EX hyperpoint mscale(const hyperpoint& t, double fac) {
if(GDIM == 3 && !prod) return cpush(2, fac) * t;
2019-08-18 11:05:47 +00:00
if(prod) fac = exp(fac);
2017-03-23 10:53:57 +00:00
hyperpoint res;
2019-02-17 17:47:19 +00:00
for(int i=0; i<MDIM; i++)
2017-03-23 10:53:57 +00:00
res[i] = t[i] * fac;
return res;
}
2019-08-09 19:00:52 +00:00
EX transmatrix mscale(const transmatrix& t, double fac) {
if(GDIM == 3 && !prod) {
// if(pmodel == mdFlatten) { transmatrix u = t; u[2][LDIM] -= fac; return u; }
2019-05-12 12:35:14 +00:00
return t * cpush(2, fac);
}
2019-08-18 11:05:47 +00:00
if(prod) fac = exp(fac);
2017-03-23 10:53:57 +00:00
transmatrix res;
2019-02-17 17:47:19 +00:00
for(int i=0; i<MDIM; i++) for(int j=0; j<MDIM; j++)
2017-03-23 10:53:57 +00:00
res[i][j] = t[i][j] * fac;
return res;
}
2019-08-09 19:00:52 +00:00
EX transmatrix xyscale(const transmatrix& t, double fac) {
2017-03-23 10:53:57 +00:00
transmatrix res;
2019-05-08 16:33:08 +00:00
for(int i=0; i<MDIM; i++) for(int j=0; j<GDIM; j++)
2017-03-23 10:53:57 +00:00
res[i][j] = t[i][j] * fac;
return res;
}
2019-08-09 19:00:52 +00:00
EX transmatrix xyzscale(const transmatrix& t, double fac, double facz) {
2017-03-23 10:53:57 +00:00
transmatrix res;
2019-05-08 16:33:08 +00:00
for(int i=0; i<MDIM; i++) for(int j=0; j<GDIM; j++)
2017-03-23 10:53:57 +00:00
res[i][j] = t[i][j] * fac;
2019-02-17 17:47:19 +00:00
for(int i=0; i<MDIM; i++)
res[i][LDIM] = t[i][LDIM] * facz;
2017-03-23 10:53:57 +00:00
return res;
}
2019-08-09 19:00:52 +00:00
EX transmatrix mzscale(const transmatrix& t, double fac) {
2019-05-08 16:33:08 +00:00
if(GDIM == 3) return t * cpush(2, fac);
2017-03-23 10:53:57 +00:00
// take only the spin
transmatrix tcentered = gpushxto0(tC0(t)) * t;
// tcentered = tcentered * spin(downspin_zivory);
fac -= 1;
transmatrix res = t * inverse(tcentered) * ypush(-fac) * tcentered;
fac *= .2;
fac += 1;
2019-02-17 17:47:19 +00:00
for(int i=0; i<MDIM; i++) for(int j=0; j<MDIM; j++)
2017-03-23 10:53:57 +00:00
res[i][j] = res[i][j] * fac;
return res;
}
2019-08-09 19:00:52 +00:00
EX hyperpoint mid3(hyperpoint h1, hyperpoint h2, hyperpoint h3) {
return mid(h1+h2+h3, h1+h2+h3);
}
2019-08-09 19:00:52 +00:00
EX hyperpoint mid_at(hyperpoint h1, hyperpoint h2, ld v) {
hyperpoint h = h1 * (1-v) + h2 * v;
return mid(h, h);
}
2019-08-09 19:00:52 +00:00
EX hyperpoint mid_at_actual(hyperpoint h, ld v) {
2018-08-19 14:28:36 +00:00
return rspintox(h) * xpush0(hdist0(h) * v);
2018-08-05 03:07:34 +00:00
}
2019-08-22 11:45:51 +00:00
/** in 3D, an orthogonal projection of C0 on the given triangle */
2019-08-09 19:00:52 +00:00
EX hyperpoint orthogonal_of_C0(hyperpoint h0, hyperpoint h1, hyperpoint h2) {
2019-04-07 01:08:43 +00:00
h0 /= h0[3];
h1 /= h1[3];
h2 /= h2[3];
hyperpoint w = h0;
hyperpoint d1 = h1 - h0;
hyperpoint d2 = h2 - h0;
ld denom = (d1|d1) * (d2|d2) - (d1|d2) * (d1|d2);
ld a1 = (d2|w) * (d1|d2) - (d1|w) * (d2|d2);
ld a2 = (d1|w) * (d1|d2) - (d2|w) * (d1|d1);
hyperpoint h = w * denom + d1 * a1 + d2 * a2;
return normalize(h);
}
2019-08-09 19:00:52 +00:00
EX hyperpoint zshift(hyperpoint x, ld z) {
2019-08-15 13:05:43 +00:00
if(GDIM == 3 && WDIM == 2) return rgpushxto0(x) * cpush0(2, z);
2019-08-24 12:49:49 +00:00
else if(sl2) return mscale(x, z);
2019-08-18 11:05:47 +00:00
else if(prod) return mscale(x, z);
else return mscale(x, z);
}
2019-08-09 19:00:52 +00:00
EX hyperpoint hpxd(ld d, ld x, ld y, ld z) {
hyperpoint H = hpxyz(d*x, d*y, z);
H = mid(H, H);
return H;
}
2019-08-09 19:00:52 +00:00
EX ld signum(ld x) { return x<0?-1:x>0?1:0; }
EX bool asign(ld y1, ld y2) { return signum(y1) != signum(y2); }
EX ld xcross(ld x1, ld y1, ld x2, ld y2) { return x1 + (x2 - x1) * y1 / (y1 - y2); }
EX transmatrix parallel_transport(const transmatrix Position, const transmatrix& ori, const hyperpoint direction, int precision IS(100)) {
2019-08-26 07:06:39 +00:00
if(nonisotropic) return nisot::parallel_transport(Position, direction);
else if(prod) {
hyperpoint h = product::direct_exp(ori * direction);
return Position * rgpushxto0(h);
}
else return Position * rgpushxto0(direct_exp(direction, precision));
2019-08-26 07:06:39 +00:00
}
EX void apply_parallel_transport(transmatrix& Position, const transmatrix orientation, const hyperpoint direction) {
Position = parallel_transport(Position, orientation, direction);
2019-08-20 16:02:03 +00:00
}
2019-08-26 07:06:39 +00:00
EX void rotate_object(transmatrix& Position, transmatrix& orientation, transmatrix R) {
if(prod) orientation = orientation * R;
else Position = Position * R;
2019-07-31 13:19:40 +00:00
}
2019-08-26 07:06:39 +00:00
EX transmatrix spin_towards(const transmatrix Position, transmatrix& ori, const hyperpoint goal, int dir, int back) {
transmatrix T;
ld alpha = 0;
if(nonisotropic && nisot::geodesic_movement)
2019-08-26 07:06:39 +00:00
T = nisot::spin_towards(Position, goal);
else {
hyperpoint U = inverse(Position) * goal;
if(prod) {
hyperpoint h = product::inverse_exp(U);
alpha = asin_clamp(h[2] / hypot_d(3, h));
U = product_decompose(U).second;
}
T = rspintox(U);
}
if(back < 0) T = T * spin(M_PI), alpha = -alpha;
if(prod) {
if(dir == 0) ori = cspin(2, 0, alpha);
if(dir == 2) ori = cspin(2, 0, alpha - M_PI/2), dir = 0;
}
2019-07-31 15:05:12 +00:00
if(dir) T = T * cspin(dir, 0, -M_PI/2);
T = Position * T;
return T;
2019-07-31 13:19:40 +00:00
}
2019-08-09 19:00:52 +00:00
EX ld ortho_error(transmatrix T) {
2019-08-03 10:59:08 +00:00
ld err = 0;
for(int x=0; x<3; x++) for(int y=0; y<3; y++) {
ld s = 0;
for(int z=0; z<3; z++) s += T[z][x] * T[z][y];
s -= (x==y);
err += s*s;
}
return err;
}
2019-08-09 22:29:03 +00:00
EX transmatrix transpose(transmatrix T) {
transmatrix result;
for(int i=0; i<MDIM; i++)
for(int j=0; j<MDIM; j++)
result[j][i] = T[i][j];
return result;
}
#if HDR
2019-08-24 09:55:45 +00:00
namespace slr {
hyperpoint xyz_point(ld x, ld y, ld z);
hyperpoint polar(ld r, ld theta, ld phi);
}
inline hyperpoint cpush0(int c, ld x) {
hyperpoint h = Hypc;
2019-08-24 09:55:45 +00:00
if(sl2) return slr::xyz_point(c==0?x:0, c==1?x:0, c==2?x:0);
2019-08-19 08:32:23 +00:00
if(c == 2 && prod) {
h[2] = exp(x);
return h;
}
h[LDIM] = cos_auto(x);
h[c] = sin_auto(x);
return h;
}
inline hyperpoint xspinpush0(ld alpha, ld x) {
2019-08-24 09:55:45 +00:00
if(sl2) return slr::polar(x, -alpha, 0);
hyperpoint h = Hypc;
h[LDIM] = cos_auto(x);
h[0] = sin_auto(x) * cos(alpha);
h[1] = sin_auto(x) * -sin(alpha);
return h;
}
inline hyperpoint xpush0(ld x) { return cpush0(0, x); }
inline hyperpoint ypush0(ld x) { return cpush0(1, x); }
inline hyperpoint zpush0(ld x) { return cpush0(2, x); }
2019-08-22 11:45:51 +00:00
/** T * C0, optimized */
inline hyperpoint tC0(const transmatrix &T) {
hyperpoint z;
for(int i=0; i<MDIM; i++) z[i] = T[i][LDIM];
return z;
}
#endif
2019-08-24 16:14:38 +00:00
/** tangent vector in the given direction */
EX hyperpoint ctangent(int c, ld x) { return point3(c==0?x:0, c==1?x:0, c==2?x:0); }
/** tangent vector in direction X */
EX hyperpoint xtangent(ld x) { return ctangent(0, x); }
/** tangent vector in direction Y */
EX hyperpoint ztangent(ld z) { return ctangent(2, z); }
/** change the length of the targent vector */
EX hyperpoint tangent_length(hyperpoint dir, ld length) {
ld r = hypot_d(GDIM, dir);
if(!r) return dir;
return dir * (length / r);
}
/** exponential function: follow the geodesic given by v */
EX hyperpoint direct_exp(hyperpoint v, int steps) {
2019-10-01 03:03:46 +00:00
if(solnih) return nisot::numerical_exp(v, steps);
2019-08-24 16:14:38 +00:00
if(nil) return nilv::formula_exp(v);
if(sl2) return slr::formula_exp(v);
if(prod) return product::direct_exp(v);
ld d = hypot_d(GDIM, v);
if(d > 0) for(int i=0; i<GDIM; i++) v[i] = v[i] * sin_auto(d) / d;
2019-08-25 18:08:35 +00:00
v[LDIM] = cos_auto(d);
2019-08-24 16:14:38 +00:00
return v;
}
#if HDR
enum iePrecision { iLazy, iTable };
#endif
/** inverse exponential function \see hr::direct_exp */
EX hyperpoint inverse_exp(const hyperpoint h, iePrecision p, bool just_direction IS(true)) {
2019-09-13 17:50:12 +00:00
#if CAP_SOLV
2019-10-03 18:10:48 +00:00
if(solnih) {
if(nih)
return solnihv::get_inverse_exp_nsym(h, p == iLazy, just_direction);
else
return solnihv::get_inverse_exp_symsol(h, p == iLazy, just_direction);
}
2019-09-13 17:50:12 +00:00
#endif
2019-08-24 16:14:38 +00:00
if(nil) return nilv::get_inverse_exp(h, p == iLazy ? 5 : 20);
if(sl2) return slr::get_inverse_exp(h);
if(prod) return product::inverse_exp(h);
ld d = acos_auto_clamp(h[GDIM]);
hyperpoint v;
if(d && sin_auto(d)) for(int i=0; i<GDIM; i++) v[i] = h[i] * d / sin_auto(d);
v[3] = 0;
return v;
}
EX ld geo_dist(const hyperpoint h1, const hyperpoint h2, iePrecision p) {
if(!nonisotropic) return hdist(h1, h2);
return hypot_d(3, inverse_exp(inverse(nisot::translate(h1)) * h2, p, false));
}
EX hyperpoint lp_iapply(const hyperpoint h) {
2019-08-24 16:14:38 +00:00
return nisot::local_perspective_used() ? inverse(nisot::local_perspective) * h : h;
}
EX hyperpoint lp_apply(const hyperpoint h) {
2019-08-24 16:14:38 +00:00
return nisot::local_perspective_used() ? nisot::local_perspective * h : h;
}
EX hyperpoint smalltangent() { return xtangent(.1); }
2019-08-24 16:14:38 +00:00
2019-10-21 20:43:03 +00:00
EX void cyclefix(ld& a, ld b) {
while(a > b + M_PI) a -= 2 * M_PI;
while(a < b - M_PI) a += 2 * M_PI;
}
EX ld raddif(ld a, ld b) {
ld d = a-b;
if(d < 0) d = -d;
if(d > 2*M_PI) d -= 2*M_PI;
if(d > M_PI) d = 2 * M_PI-d;
return d;
}
}