Go to the documentation of this file.
23 # define PI (3.14159265358979323846264338327950288)
27 # define NAN (std::numeric_limits<double>::quiet_NaN())
31 #define drand48() ((double)rand()/RAND_MAX)
32 #define srand48(s) srand(s)
36 # define snprintf _snprintf
39 #define GLSLMATH_PRECISION 7
43 inline std::string to_space(
int space=0)
46 for (
int i=0; i<space; i++) s +=
" ";
50 inline std::string to_string(
int v,
int space=0)
53 snprintf(buf,
sizeof(buf),
"%i",v);
56 if ((
unsigned int)space > s.size())
58 int pad_left = (space-s.size())/2;
59 int pad_right = space-pad_left-s.size();
61 return(to_space(pad_left) + s + to_space(pad_right));
67 inline std::string to_string(
unsigned int v,
int space=0)
70 snprintf(buf,
sizeof(buf),
"%u",v);
73 if ((
unsigned int)space > s.size())
75 int pad_left = (space-s.size())/2;
76 int pad_right = space-pad_left-s.size();
78 return(to_space(pad_left) + s + to_space(pad_right));
84 inline std::string to_string(
double v,
int space=0)
87 snprintf(buf,
sizeof(buf),
"%.7g",v);
90 if ((
unsigned int)space > s.size())
92 int pad_left = (space-s.size())/2;
93 int pad_right = space-pad_left-s.size();
95 return(to_space(pad_left) + s + to_space(pad_right));
119 vec2(
const double vx,
const double vy) {x=vx; y=vy;}
129 operator const double *()
const
133 double length()
const {
return(sqrt(x*x+y*y));}
136 double norm()
const {
return(x*x+y*y);}
143 {
return(x*v.x+y*v.y);}
147 {
return((v.x-x)*(v.x-x)+(v.y-y)*(v.y-y)<e*e);}
151 {x+=v.x; y+=v.y;
return(*
this);}
155 {x-=v.x; y-=v.y;
return(*
this);}
159 {x*=v; y*=v;
return(*
this);}
163 {x*=v.x; y*=v.y;
return(*
this);}
170 operator std::string()
171 {
return(
"(" + glslmath::to_string(x) +
", " + glslmath::to_string(y) +
")");}
174 std::string
to_string(std::string delimiter =
", ",
int space=0)
175 {
return(glslmath::to_string(x,space) + delimiter + glslmath::to_string(y,space));}
179 struct {
double x,y;};
186 {
return(
vec2(a.x+b.x,a.y+b.y));}
190 {
return(
vec2(a.x-b.x,a.y-b.y));}
194 {
return(
vec2(-v.x,-v.y));}
198 {
return(
vec2(a*b.x,a*b.y));}
202 {
return(
vec2(a.x*b,a.y*b));}
206 {
return(
vec2(a.x/b,a.y/b));}
210 {
return(
vec2(a.x*b.x,a.y*b.y));}
214 {
return(a.x==b.x && a.y==b.y);}
218 {
return(a.x!=b.x || a.y!=b.y);}
232 if (l2>0.0 && l2!=1.0)
return(*
this/sqrt(l2));
246 {
return((1-w)*a+w*b);}
250 {
return((1-w)*a+w*b);}
254 {
return(out << std::setprecision(GLSLMATH_PRECISION) <<
"(" << v.x <<
", " << v.y <<
")");}
275 vec2f(
const float vx,
const float vy) {x=vx; y=vy;}
289 operator const float *()
const
294 {x+=v.x; y+=v.y;
return(*
this);}
298 {x-=v.x; y-=v.y;
return(*
this);}
302 {x*=v; y*=v;
return(*
this);}
306 {x*=v.x; y*=v.y;
return(*
this);}
310 {
return(
vec2f(y,x));}
313 operator std::string()
314 {
return(
"(" + glslmath::to_string(x) +
", " + glslmath::to_string(y) +
")");}
317 std::string
to_string(std::string delimiter =
", ",
int space=0)
318 {
return(glslmath::to_string(x,space) + delimiter + glslmath::to_string(y,space));}
329 {
return(
vec2f(a.x+b.x,a.y+b.y));}
333 {
return(
vec2f(a.x-b.x,a.y-b.y));}
337 {
return(
vec2f(-v.x,-v.y));}
341 {
return(
vec2f(a*b.x,a*b.y));}
345 {
return(
vec2f(a.x*b,a.y*b));}
349 {
return(
vec2f(a.x/b,a.y/b));}
353 {
return(
vec2f(a.x*b.x,a.y*b.y));}
357 {
return(a.x==b.x && a.y==b.y);}
361 {
return(a.x!=b.x || a.y!=b.y);}
365 {
return(out << std::setprecision(GLSLMATH_PRECISION) <<
"(" << v.x <<
", " << v.y <<
")");}
383 vec3(
const vec2 &v,
const double vz=0.0) {x=v.x; y=v.y; z=vz;}
386 vec3(
const double vx,
const double vy,
const double vz) {x=vx; y=vy; z=vz;}
389 vec3(
const double v) {x=y=z=v;}
400 operator const double *()
const
405 {x+=v.x; y+=v.y; z+=v.z;
return(*
this);}
409 {x-=v.x; y-=v.y; z-=v.z;
return(*
this);}
413 {x*=v; y*=v; z*=v;
return(*
this);}
417 {x*=v.x; y*=v.y; z*=v.z;
return(*
this);}
420 double length()
const {
return(sqrt(x*x+y*y+z*z));}
423 double norm()
const {
return(x*x+y*y+z*z);}
430 {
return(x*v.x+y*v.y+z*v.z);}
434 {
return(
vec3(y*v.z-z*v.y,z*v.x-x*v.z,x*v.y-y*v.x));}
447 {
return((v.x-x)*(v.x-x)+(v.y-y)*(v.y-y)+(v.z-z)*(v.z-z)<e*e);}
455 {
return(
vec3(z,y,x));}
459 {
return(
vec3(b,g,r));}
465 operator std::string()
466 {
return(
"(" + glslmath::to_string(x) +
", " + glslmath::to_string(y) +
", " + glslmath::to_string(z) +
")");}
469 std::string
to_string(std::string delimiter =
", ",
int space=0)
470 {
return(glslmath::to_string(x,space) + delimiter + glslmath::to_string(y,space) + delimiter + glslmath::to_string(z,space));}
475 union {
double x;
double r;};
476 union {
double y;
double g;};
477 union {
double z;
double b;};
485 {
return(
vec3(a.x+b.x,a.y+b.y,a.z+b.z));}
489 {
return(
vec3(a.x-b.x,a.y-b.y,a.z-b.z));}
493 {
return(
vec3(-v.x,-v.y,-v.z));}
497 {
return(
vec3(a*b.x,a*b.y,a*b.z));}
501 {
return(
vec3(a.x*b,a.y*b,a.z*b));}
505 {
return(
vec3(a.x/b,a.y/b,a.z/b));}
509 {
return(
vec3(a.x*b.x,a.y*b.y,a.z*b.z));}
513 {
return(a.x==b.x && a.y==b.y && a.z==b.z);}
517 {
return(a.x!=b.x || a.y!=b.y || a.z!=b.z);}
531 if (l2>0.0 && l2!=1.0)
return(*
this/sqrt(l2));
545 {
return(a.
cross(b));}
549 {
return((1-w)*a+w*b);}
553 {
return((1-w)*a+w*b);}
557 {
return((*
this)-2*n*
dot(n));}
573 {
return((1-alpha)*(*
this)+alpha*rgb);}
577 {
return(out << std::setprecision(GLSLMATH_PRECISION) <<
"(" << v.x <<
", " << v.y <<
", " << v.z <<
")");}
595 vec3f(
const vec3 &v) {x=(float)v.x; y=(
float)v.y; z=(float)v.z;}
598 vec3f(
const vec2f &v,
const float vz=0.0f) {x=v.x; y=v.y; z=vz;}
601 vec3f(
const vec2 &v,
const float vz=0.0f) {x=(float)v.x; y=(
float)v.y; z=vz;}
604 vec3f(
const float vx,
const float vy,
const float vz) {x=vx; y=vy; z=vz;}
611 {
return(
vec3(x,y,z));}
618 operator const float *()
const
623 {x+=v.x; y+=v.y; z+=v.z;
return(*
this);}
627 {x-=v.x; y-=v.y; z-=v.z;
return(*
this);}
631 {x*=v; y*=v; z*=v;
return(*
this);}
635 {x*=v.x; y*=v.y; z*=v.z;
return(*
this);}
639 {
return(
vec2f(x,y));}
643 {
return(
vec3f(z,y,x));}
647 {
return(
vec3f(b,g,r));}
650 operator std::string()
651 {
return(
"(" + glslmath::to_string(x) +
", " + glslmath::to_string(y) +
", " + glslmath::to_string(z) +
")");}
654 std::string
to_string(std::string delimiter =
", ",
int space=0)
655 {
return(glslmath::to_string(x,space) + delimiter + glslmath::to_string(y,space) + delimiter + glslmath::to_string(z,space));}
660 union {
float x;
float r;};
661 union {
float y;
float g;};
662 union {
float z;
float b;};
670 {
return(
vec3f(a.x+b.x,a.y+b.y,a.z+b.z));}
674 {
return(
vec3f(a.x-b.x,a.y-b.y,a.z-b.z));}
678 {
return(
vec3f(-v.x,-v.y,-v.z));}
682 {
return(
vec3f(a*b.x,a*b.y,a*b.z));}
686 {
return(
vec3f(a.x*b,a.y*b,a.z*b));}
690 {
return(
vec3f(a.x/b,a.y/b,a.z/b));}
694 {
return(
vec3f(a.x*b.x,a.y*b.y,a.z*b.z));}
698 {
return(a.x==b.x && a.y==b.y && a.z==b.z);}
702 {
return(a.x!=b.x || a.y!=b.y || a.z!=b.z);}
706 {
return(out << std::setprecision(GLSLMATH_PRECISION) <<
"(" << v.x <<
", " << v.y <<
", " << v.z <<
")");}
724 vec4(
const vec3 &v,
const double vw=1.0) {x=v.x; y=v.y; z=v.z; w=vw;}
727 vec4(
const vec2 &v,
const double vz=0.0,
const double vw=1.0) {x=v.x; y=v.y; z=vz; w=vw;}
733 vec4(
const double vx,
const double vy,
const double vz,
const double vw=1.0) {x=vx; y=vy; z=vz; w=vw;}
736 vec4(
const double v,
const double vw=1.0) {x=y=z=v; w=vw;}
747 return(
vec3(x*c,y*c,z*c));
758 operator const double *()
const
763 {x+=v.x; y+=v.y; z+=v.z; w+=v.w;
return(*
this);}
767 {x-=v.x; y-=v.y; z-=v.z; w-=v.w;
return(*
this);}
771 {x*=v; y*=v; z*=v; w*=v;
return(*
this);}
775 {x*=v.x; y*=v.y; z*=v.z; w*=v.w;
return(*
this);}
778 double length()
const {
return(sqrt(x*x+y*y+z*z+w*w));}
781 double norm()
const {
return(x*x+y*y+z*z+w*w);}
788 {
return(x*v.x+y*v.y+z*v.z+w*v.w);}
793 assert(w==0.0 && v.w==0.0);
794 return(
vec4(y*v.z-z*v.y,z*v.x-x*v.z,x*v.y-y*v.x,0.0));
808 {
return((v.x-x)*(v.x-x)+(v.y-y)*(v.y-y)+(v.z-z)*(v.z-z)+(v.w-w)*(v.w-w)<e*e);}
820 {
return(
vec3(x,y,z));}
824 {
return(
vec3(r,g,b));}
828 {
return(
vec4(w,z,y,x));}
832 {
return(
vec4(b,g,r,a));}
838 operator std::string()
839 {
return(
"(" + glslmath::to_string(x) +
", " + glslmath::to_string(y) +
", " + glslmath::to_string(z) +
", " + glslmath::to_string(w) +
")");}
842 std::string
to_string(std::string delimiter =
", ",
int space=0)
843 {
return(glslmath::to_string(x,space) + delimiter + glslmath::to_string(y,space) + delimiter + glslmath::to_string(z,space) + delimiter + glslmath::to_string(w,space));}
848 union {
double x;
double r;};
849 union {
double y;
double g;};
850 union {
double z;
double b;};
851 union {
double w;
double a;};
859 {
return(
vec4(a.x+b.x,a.y+b.y,a.z+b.z,a.w+b.w));}
863 {
return(
vec4(a.x-b.x,a.y-b.y,a.z-b.z,a.w-b.w));}
867 {
return(
vec4(-v.x,-v.y,-v.z,-v.w));}
871 {
return(
vec4(a*b.x,a*b.y,a*b.z,a*b.w));}
875 {
return(
vec4(a.x*b,a.y*b,a.z*b,a.w*b));}
879 {
return(
vec4(a.x/b,a.y/b,a.z/b,a.w/b));}
883 {
return(
vec4(a.x*b.x,a.y*b.y,a.z*b.z,a.w*b.w));}
887 {
return(a.x==b.x && a.y==b.y && a.z==b.z && a.w==b.w);}
891 {
return(a.x!=b.x || a.y!=b.y || a.z!=b.z || a.w!=b.w);}
905 if (l2>0.0 && l2!=1.0)
return(*
this/sqrt(l2));
919 {
return(a.
cross(b));}
923 {
return((1-w)*a+w*b);}
927 {
return((1-w)*a+w*b);}
932 assert(w==0.0 && n.w==0.0);
933 return((*
this)-2*n*
dot(n));
950 {
return(
vec4((1-rgba.a)*this->rgb()+rgba.a*rgba.
rgb(),1-(1-rgba.a)*(1-a)));}
954 {
return(out << std::setprecision(GLSLMATH_PRECISION) <<
"(" << v.x <<
", " << v.y <<
", " << v.z <<
", " << v.w <<
")");}
972 vec4f(
const vec4 &v) {x=(float)v.x; y=(
float)v.y; z=(float)v.z; w=(
float)v.w;}
975 vec4f(
const vec3f &v,
const float vw=1.0f) {x=v.x; y=v.y; z=v.z; w=vw;}
978 vec4f(
const vec3 &v,
const float vw=1.0f) {x=(float)v.x; y=(
float)v.y; z=(float)v.z; w=vw;}
981 vec4f(
const vec2f &v,
const float vz=0.0f,
const float vw=1.0f) {x=v.x; y=v.y; z=vz; w=vw;}
984 vec4f(
const vec2 &v,
const float vz=0.0f,
const float vw=1.0f) {x=(float)v.x; y=(
float)v.y; z=vz; w=vw;}
993 vec4f(
const float vx,
const float vy,
const float vz,
const float vw=1.0f) {x=vx; y=vy; z=vz; w=vw;}
996 vec4f(
const float v,
const float vw=1.0f) {x=y=z=v; w=vw;}
1000 {
return(
vec4(x,y,z,w));}
1011 return(
vec3f(x*c,y*c,z*c));
1014 return(
vec3f(x,y,z));
1022 operator const float *()
const
1027 {x+=v.x; y+=v.y; z+=v.z; w+=v.w;
return(*
this);}
1031 {x-=v.x; y-=v.y; z-=v.z; w-=v.w;
return(*
this);}
1035 {x*=v; y*=v; z*=v; w*=v;
return(*
this);}
1039 {x*=v.x; y*=v.y; z*=v.z; w*=v.w;
return(*
this);}
1043 {
return(
vec2f(x,y));}
1047 {
return(
vec2f(z,w));}
1051 {
return(
vec3f(x,y,z));}
1055 {
return(
vec3f(r,g,b));}
1059 {
return(
vec4f(w,z,y,x));}
1063 {
return(
vec4f(b,g,r,a));}
1066 operator std::string()
1067 {
return(
"(" + glslmath::to_string(x) +
", " + glslmath::to_string(y) +
", " + glslmath::to_string(z) +
", " + glslmath::to_string(w) +
")");}
1070 std::string
to_string(std::string delimiter =
", ",
int space=0)
1071 {
return(glslmath::to_string(x,space) + delimiter + glslmath::to_string(y,space) + delimiter + glslmath::to_string(z,space) + delimiter + glslmath::to_string(w,space));}
1076 union {
float x;
float r;};
1077 union {
float y;
float g;};
1078 union {
float z;
float b;};
1079 union {
float w;
float a;};
1087 {
return(
vec4f(a.x+b.x,a.y+b.y,a.z+b.z,a.w+b.w));}
1091 {
return(
vec4f(a.x-b.x,a.y-b.y,a.z-b.z,a.w-b.w));}
1095 {
return(
vec4f(-v.x,-v.y,-v.z,-v.w));}
1099 {
return(
vec4f(a*b.x,a*b.y,a*b.z,a*b.w));}
1103 {
return(
vec4f(a.x*b,a.y*b,a.z*b,a.w*b));}
1107 {
return(
vec4f(a.x/b,a.y/b,a.z/b,a.w/b));}
1111 {
return(
vec4f(a.x*b.x,a.y*b.y,a.z*b.z,a.w*b.w));}
1115 {
return(a.x==b.x && a.y==b.y && a.z==b.z && a.w==b.w);}
1119 {
return(a.x!=b.x || a.y!=b.y || a.z!=b.z || a.w!=b.w);}
1123 {
return(out << std::setprecision(GLSLMATH_PRECISION) <<
"(" << v.x <<
", " << v.y <<
", " << v.z <<
", " << v.w <<
")");}
1201 assert(i>=0 && i<2);
1202 return(
vec2(mtx[i][0],mtx[i][1]));
1208 assert(i>=0 && i<2);
1209 return(
vec2(mtx[0][i],mtx[1][i]));
1215 assert(i>=0 && i<2);
1216 return(
vec2(mtx[i][0],mtx[i][1]));
1224 operator const double *()
const
1239 {
return(mtx[0][0]*mtx[1][1]-mtx[0][1]*mtx[1][0]);}
1263 if (d==0.0)
return(
mat2(0));
1267 m.mtx[0][0]=d*mtx[1][1];
1268 m.mtx[1][0]=-d*mtx[1][0];
1269 m.mtx[0][1]=-d*mtx[0][1];
1270 m.mtx[1][1]=d*mtx[0][0];
1321 operator std::string()
1322 {
return(
"(" + std::string(
row(0)) +
", " + std::string(
row(1)) +
")");}
1336 return(
mat2(m1[0]+m2[0],
1381 if (((
const double *)a)[i]!=((
const double *)b)[i])
return(
false);
1392 if (((
const double *)a)[i]!=((
const double *)b)[i])
return(
true);
1403 {
return(out <<
"(" << m.
row(0) <<
", " << m.
row(1) <<
")");}
1436 mtx[0][0]=(float)m[0][0];
1437 mtx[0][1]=(float)m[0][1];
1439 mtx[1][0]=(float)m[1][0];
1440 mtx[1][1]=(float)m[1][1];
1476 assert(i>=0 && i<2);
1477 return(
vec2f(mtx[i][0],mtx[i][1]));
1483 assert(i>=0 && i<2);
1484 return(
vec2f(mtx[0][i],mtx[1][i]));
1490 assert(i>=0 && i<2);
1491 return(
vec2f(mtx[i][0],mtx[i][1]));
1499 operator const float *()
const
1513 operator std::string()
1514 {
return(
"(" + std::string(
row(0)) +
", " + std::string(
row(1)) +
")");}
1527 {
return(out <<
"(" << m.
row(0) <<
", " << m.
row(1) <<
")");}
1621 vec3(c1.y,c2.y,c3.y),
1622 vec3(c1.z,c2.z,c3.z)));
1627 double d,
double e,
double f,
1628 double g,
double h,
double i)
1645 double d,
double e,
double f,
1646 double g,
double h,
double i)
1656 assert(i>=0 && i<3);
1657 return(
vec3(mtx[i][0],mtx[i][1],mtx[i][2]));
1663 assert(i>=0 && i<3);
1664 return(
vec3(mtx[0][i],mtx[1][i],mtx[2][i]));
1670 assert(i>=0 && i<3);
1671 return(
vec3(mtx[i][0],mtx[i][1],mtx[i][2]));
1683 operator const double *()
const
1705 return(mtx[0][0]*(mtx[1][1]*mtx[2][2]-mtx[2][1]*mtx[1][2])+
1706 mtx[0][1]*(mtx[2][0]*mtx[1][2]-mtx[1][0]*mtx[2][2])+
1707 mtx[0][2]*(mtx[1][0]*mtx[2][1]-mtx[2][0]*mtx[1][1]));
1732 if (d==0.0)
return(
mat3(0));
1736 m.mtx[0][0]=d*(mtx[1][1]*mtx[2][2]-mtx[2][1]*mtx[1][2]);
1737 m.mtx[1][0]=d*(mtx[2][0]*mtx[1][2]-mtx[1][0]*mtx[2][2]);
1738 m.mtx[2][0]=d*(mtx[1][0]*mtx[2][1]-mtx[2][0]*mtx[1][1]);
1739 m.mtx[0][1]=d*(mtx[2][1]*mtx[0][2]-mtx[0][1]*mtx[2][2]);
1740 m.mtx[1][1]=d*(mtx[0][0]*mtx[2][2]-mtx[2][0]*mtx[0][2]);
1741 m.mtx[2][1]=d*(mtx[2][0]*mtx[0][1]-mtx[0][0]*mtx[2][1]);
1742 m.mtx[0][2]=d*(mtx[0][1]*mtx[1][2]-mtx[1][1]*mtx[0][2]);
1743 m.mtx[1][2]=d*(mtx[1][0]*mtx[0][2]-mtx[0][0]*mtx[1][2]);
1744 m.mtx[2][2]=d*(mtx[0][0]*mtx[1][1]-mtx[1][0]*mtx[0][1]);
1799 return(
mat3(
vec3(x*x*c1+c,x*y*c1-z*s,x*z*c1+y*s),
1800 vec3(y*x*c1+z*s,y*y*c1+c,y*z*c1-x*s),
1801 vec3(z*x*c1-y*s,z*y*c1+x*s,z*z*c1+c)));
1829 return(
mat3(
vec3(x*x*c1+c,x*y*c1-z*s,x*z*c1+y*s),
1830 vec3(y*x*c1+z*s,y*y*c1+c,y*z*c1-x*s),
1831 vec3(z*x*c1-y*s,z*y*c1+x*s,z*z*c1+c)));
1847 operator std::string()
1848 {
return(
"(" + std::string(
row(0)) +
", " + std::string(
row(1)) +
", " + std::string(
row(2)) +
")");}
1862 return(
mat3(m1[0]+m2[0],
1909 if (((
const double *)a)[i]!=((
const double *)b)[i])
return(
false);
1920 if (((
const double *)a)[i]!=((
const double *)b)[i])
return(
true);
1931 {
return(out <<
"(" << m.
row(0) <<
", " << m.
row(1) <<
", " << m.
row(2) <<
")");}
1980 mtx[0][0]=(float)m[0][0];
1981 mtx[0][1]=(float)m[0][1];
1982 mtx[0][2]=(float)m[0][2];
1984 mtx[1][0]=(float)m[1][0];
1985 mtx[1][1]=(float)m[1][1];
1986 mtx[1][2]=(float)m[1][2];
1988 mtx[2][0]=(float)m[2][0];
1989 mtx[2][1]=(float)m[2][1];
1990 mtx[2][2]=(float)m[2][2];
1997 vec3f(c1.y,c2.y,c3.y),
1998 vec3f(c1.z,c2.z,c3.z)));
2003 float d,
float e,
float f,
2004 float g,
float h,
float i)
2021 float d,
float e,
float f,
2022 float g,
float h,
float i)
2032 assert(i>=0 && i<3);
2033 return(
vec3f(mtx[i][0],mtx[i][1],mtx[i][2]));
2039 assert(i>=0 && i<3);
2040 return(
vec3f(mtx[0][i],mtx[1][i],mtx[2][i]));
2046 assert(i>=0 && i<3);
2047 return(
vec3f(mtx[i][0],mtx[i][1],mtx[i][2]));
2059 operator const float *()
const
2079 operator std::string()
2080 {
return(
"(" + std::string(
row(0)) +
", " + std::string(
row(1)) +
", " + std::string(
row(2)) +
")");}
2093 {
return(out <<
"(" << m.
row(0) <<
", " << m.
row(1) <<
", " << m.
row(2) <<
")");}
2225 return(
mat4(
vec4(c1.x,c2.x,c3.x,c4.x),
2226 vec4(c1.y,c2.y,c3.y,c4.y),
2227 vec4(c1.z,c2.z,c3.z,c4.z),
2228 vec4(c1.w,c2.w,c3.w,c4.w)));
2233 double e,
double f,
double g,
double h,
2234 double i,
double j,
double k,
double l,
2235 double m,
double n,
double o,
double p)
2254 double e,
double f,
double g,
double h,
2255 double i,
double j,
double k,
double l,
2256 double m,
double n,
double o,
double p)
2267 assert(i>=0 && i<4);
2268 return(
vec4(mtx[i][0],mtx[i][1],mtx[i][2],mtx[i][3]));
2274 assert(i>=0 && i<4);
2275 return(
vec4(mtx[0][i],mtx[1][i],mtx[2][i],mtx[3][i]));
2281 assert(i>=0 && i<4);
2282 return(
vec4(mtx[i][0],mtx[i][1],mtx[i][2],mtx[i][3]));
2291 {
return(
mat3(*
this));}
2302 operator const double *()
const
2332 return(mtx[0][3]*mtx[1][2]*mtx[2][1]*mtx[3][0]-
2333 mtx[0][2]*mtx[1][3]*mtx[2][1]*mtx[3][0]-
2334 mtx[0][3]*mtx[1][1]*mtx[2][2]*mtx[3][0]+
2335 mtx[0][1]*mtx[1][3]*mtx[2][2]*mtx[3][0]+
2336 mtx[0][2]*mtx[1][1]*mtx[2][3]*mtx[3][0]-
2337 mtx[0][1]*mtx[1][2]*mtx[2][3]*mtx[3][0]-
2338 mtx[0][3]*mtx[1][2]*mtx[2][0]*mtx[3][1]+
2339 mtx[0][2]*mtx[1][3]*mtx[2][0]*mtx[3][1]+
2340 mtx[0][3]*mtx[1][0]*mtx[2][2]*mtx[3][1]-
2341 mtx[0][0]*mtx[1][3]*mtx[2][2]*mtx[3][1]-
2342 mtx[0][2]*mtx[1][0]*mtx[2][3]*mtx[3][1]+
2343 mtx[0][0]*mtx[1][2]*mtx[2][3]*mtx[3][1]+
2344 mtx[0][3]*mtx[1][1]*mtx[2][0]*mtx[3][2]-
2345 mtx[0][1]*mtx[1][3]*mtx[2][0]*mtx[3][2]-
2346 mtx[0][3]*mtx[1][0]*mtx[2][1]*mtx[3][2]+
2347 mtx[0][0]*mtx[1][3]*mtx[2][1]*mtx[3][2]+
2348 mtx[0][1]*mtx[1][0]*mtx[2][3]*mtx[3][2]-
2349 mtx[0][0]*mtx[1][1]*mtx[2][3]*mtx[3][2]-
2350 mtx[0][2]*mtx[1][1]*mtx[2][0]*mtx[3][3]+
2351 mtx[0][1]*mtx[1][2]*mtx[2][0]*mtx[3][3]+
2352 mtx[0][2]*mtx[1][0]*mtx[2][1]*mtx[3][3]-
2353 mtx[0][0]*mtx[1][2]*mtx[2][1]*mtx[3][3]-
2354 mtx[0][1]*mtx[1][0]*mtx[2][2]*mtx[3][3]+
2355 mtx[0][0]*mtx[1][1]*mtx[2][2]*mtx[3][3]);
2380 if (d==0.0)
return(
mat4(0));
2384 m.mtx[0][0]=d*(mtx[1][2]*mtx[2][3]*mtx[3][1]-mtx[1][3]*mtx[2][2]*mtx[3][1]+mtx[1][3]*mtx[2][1]*mtx[3][2]-mtx[1][1]*mtx[2][3]*mtx[3][2]-mtx[1][2]*mtx[2][1]*mtx[3][3]+mtx[1][1]*mtx[2][2]*mtx[3][3]);
2385 m.mtx[0][1]=d*(mtx[0][3]*mtx[2][2]*mtx[3][1]-mtx[0][2]*mtx[2][3]*mtx[3][1]-mtx[0][3]*mtx[2][1]*mtx[3][2]+mtx[0][1]*mtx[2][3]*mtx[3][2]+mtx[0][2]*mtx[2][1]*mtx[3][3]-mtx[0][1]*mtx[2][2]*mtx[3][3]);
2386 m.mtx[0][2]=d*(mtx[0][2]*mtx[1][3]*mtx[3][1]-mtx[0][3]*mtx[1][2]*mtx[3][1]+mtx[0][3]*mtx[1][1]*mtx[3][2]-mtx[0][1]*mtx[1][3]*mtx[3][2]-mtx[0][2]*mtx[1][1]*mtx[3][3]+mtx[0][1]*mtx[1][2]*mtx[3][3]);
2387 m.mtx[0][3]=d*(mtx[0][3]*mtx[1][2]*mtx[2][1]-mtx[0][2]*mtx[1][3]*mtx[2][1]-mtx[0][3]*mtx[1][1]*mtx[2][2]+mtx[0][1]*mtx[1][3]*mtx[2][2]+mtx[0][2]*mtx[1][1]*mtx[2][3]-mtx[0][1]*mtx[1][2]*mtx[2][3]);
2388 m.mtx[1][0]=d*(mtx[1][3]*mtx[2][2]*mtx[3][0]-mtx[1][2]*mtx[2][3]*mtx[3][0]-mtx[1][3]*mtx[2][0]*mtx[3][2]+mtx[1][0]*mtx[2][3]*mtx[3][2]+mtx[1][2]*mtx[2][0]*mtx[3][3]-mtx[1][0]*mtx[2][2]*mtx[3][3]);
2389 m.mtx[1][1]=d*(mtx[0][2]*mtx[2][3]*mtx[3][0]-mtx[0][3]*mtx[2][2]*mtx[3][0]+mtx[0][3]*mtx[2][0]*mtx[3][2]-mtx[0][0]*mtx[2][3]*mtx[3][2]-mtx[0][2]*mtx[2][0]*mtx[3][3]+mtx[0][0]*mtx[2][2]*mtx[3][3]);
2390 m.mtx[1][2]=d*(mtx[0][3]*mtx[1][2]*mtx[3][0]-mtx[0][2]*mtx[1][3]*mtx[3][0]-mtx[0][3]*mtx[1][0]*mtx[3][2]+mtx[0][0]*mtx[1][3]*mtx[3][2]+mtx[0][2]*mtx[1][0]*mtx[3][3]-mtx[0][0]*mtx[1][2]*mtx[3][3]);
2391 m.mtx[1][3]=d*(mtx[0][2]*mtx[1][3]*mtx[2][0]-mtx[0][3]*mtx[1][2]*mtx[2][0]+mtx[0][3]*mtx[1][0]*mtx[2][2]-mtx[0][0]*mtx[1][3]*mtx[2][2]-mtx[0][2]*mtx[1][0]*mtx[2][3]+mtx[0][0]*mtx[1][2]*mtx[2][3]);
2392 m.mtx[2][0]=d*(mtx[1][1]*mtx[2][3]*mtx[3][0]-mtx[1][3]*mtx[2][1]*mtx[3][0]+mtx[1][3]*mtx[2][0]*mtx[3][1]-mtx[1][0]*mtx[2][3]*mtx[3][1]-mtx[1][1]*mtx[2][0]*mtx[3][3]+mtx[1][0]*mtx[2][1]*mtx[3][3]);
2393 m.mtx[2][1]=d*(mtx[0][3]*mtx[2][1]*mtx[3][0]-mtx[0][1]*mtx[2][3]*mtx[3][0]-mtx[0][3]*mtx[2][0]*mtx[3][1]+mtx[0][0]*mtx[2][3]*mtx[3][1]+mtx[0][1]*mtx[2][0]*mtx[3][3]-mtx[0][0]*mtx[2][1]*mtx[3][3]);
2394 m.mtx[2][2]=d*(mtx[0][1]*mtx[1][3]*mtx[3][0]-mtx[0][3]*mtx[1][1]*mtx[3][0]+mtx[0][3]*mtx[1][0]*mtx[3][1]-mtx[0][0]*mtx[1][3]*mtx[3][1]-mtx[0][1]*mtx[1][0]*mtx[3][3]+mtx[0][0]*mtx[1][1]*mtx[3][3]);
2395 m.mtx[2][3]=d*(mtx[0][3]*mtx[1][1]*mtx[2][0]-mtx[0][1]*mtx[1][3]*mtx[2][0]-mtx[0][3]*mtx[1][0]*mtx[2][1]+mtx[0][0]*mtx[1][3]*mtx[2][1]+mtx[0][1]*mtx[1][0]*mtx[2][3]-mtx[0][0]*mtx[1][1]*mtx[2][3]);
2396 m.mtx[3][0]=d*(mtx[1][2]*mtx[2][1]*mtx[3][0]-mtx[1][1]*mtx[2][2]*mtx[3][0]-mtx[1][2]*mtx[2][0]*mtx[3][1]+mtx[1][0]*mtx[2][2]*mtx[3][1]+mtx[1][1]*mtx[2][0]*mtx[3][2]-mtx[1][0]*mtx[2][1]*mtx[3][2]);
2397 m.mtx[3][1]=d*(mtx[0][1]*mtx[2][2]*mtx[3][0]-mtx[0][2]*mtx[2][1]*mtx[3][0]+mtx[0][2]*mtx[2][0]*mtx[3][1]-mtx[0][0]*mtx[2][2]*mtx[3][1]-mtx[0][1]*mtx[2][0]*mtx[3][2]+mtx[0][0]*mtx[2][1]*mtx[3][2]);
2398 m.mtx[3][2]=d*(mtx[0][2]*mtx[1][1]*mtx[3][0]-mtx[0][1]*mtx[1][2]*mtx[3][0]-mtx[0][2]*mtx[1][0]*mtx[3][1]+mtx[0][0]*mtx[1][2]*mtx[3][1]+mtx[0][1]*mtx[1][0]*mtx[3][2]-mtx[0][0]*mtx[1][1]*mtx[3][2]);
2399 m.mtx[3][3]=d*(mtx[0][1]*mtx[1][2]*mtx[2][0]-mtx[0][2]*mtx[1][1]*mtx[2][0]+mtx[0][2]*mtx[1][0]*mtx[2][1]-mtx[0][0]*mtx[1][2]*mtx[2][1]-mtx[0][1]*mtx[1][0]*mtx[2][2]+mtx[0][0]*mtx[1][1]*mtx[2][2]);
2483 static mat4 ortho(
double l,
double r,
double b,
double t,
double n=-1,
double f=1);
2486 static mat4 frustum(
double l,
double r,
double b,
double t,
double n,
double f);
2492 static mat4 perspective(
double fovy,
double aspect,
double znear,
double zfar);
2510 operator std::string()
2511 {
return(
"(" + std::string(
row(0)) +
", " + std::string(
row(1)) +
", " + std::string(
row(2)) +
", " + std::string(
row(3)) +
")");}
2525 return(
mat4(m1[0]+m2[0],
2573 for (i=0; i<16; i++)
2574 if (((
const double *)a)[i]!=((
const double *)b)[i])
return(
false);
2584 for (i=0; i<16; i++)
2585 if (((
const double *)a)[i]!=((
const double *)b)[i])
return(
true);
2644 {
return(out <<
"(" << m.
row(0) <<
", " << m.
row(1) <<
", " << m.
row(2) <<
", " << m.
row(3) <<
")");}
2679 assert(r!=l && t!=b && f!=n);
2681 return(
mat4(
vec4(2.0/(r-l),0,0,-(r+l)/(r-l)),
2682 vec4(0,2.0/(t-b),0,-(t+b)/(t-b)),
2683 vec4(0,0,-2.0/(f-n),-(f+n)/(f-n))));
2689 assert(r>l && t>b && f>n && n>0 && f>0);
2691 return(
mat4(
vec4(2.0*n/(r-l),0,(r+l)/(r-l),0),
2692 vec4(0,2.0*n/(t-b),(t+b)/(t-b),0),
2693 vec4(0,0,-(f+n)/(f-n),-2.0*f*n/(f-n)),
2703 double D = -p.
dot(n);
2705 return(
mat4(
vec4(B*d.y+C*d.z,-B*d.x ,-C*d.x ,-D*d.x),
2706 vec4(-A*d.y ,A*d.x+C*d.z,-C*d.y ,-D*d.y),
2707 vec4(-A*d.z ,-B*d.z ,A*d.x+B*d.y,-D*d.z),
2708 vec4(0 ,0 ,0 ,A*d.x+B*d.y+C*d.z)));
2718 assert(fovy>0.0 && fovy<PI);
2719 assert(znear>0.0 && zfar>znear);
2722 f=1.0/tan(0.5*fovy);
2726 vec4(0,0,(zfar+znear)/(znear-zfar),(2*zfar*znear)/(znear-zfar)),
2741 top=right.
cross(dir);
2810 mtx[0][0]=(float)m[0][0];
2811 mtx[0][1]=(float)m[0][1];
2812 mtx[0][2]=(float)m[0][2];
2813 mtx[0][3]=(float)m[0][3];
2815 mtx[1][0]=(float)m[1][0];
2816 mtx[1][1]=(float)m[1][1];
2817 mtx[1][2]=(float)m[1][2];
2818 mtx[1][3]=(float)m[1][3];
2820 mtx[2][0]=(float)m[2][0];
2821 mtx[2][1]=(float)m[2][1];
2822 mtx[2][2]=(float)m[2][2];
2823 mtx[2][3]=(float)m[2][3];
2825 mtx[3][0]=(float)m[3][0];
2826 mtx[3][1]=(float)m[3][1];
2827 mtx[3][2]=(float)m[3][2];
2828 mtx[3][3]=(float)m[3][3];
2835 vec4f(c1.y,c2.y,c3.y,c4.y),
2836 vec4f(c1.z,c2.z,c3.z,c4.z),
2837 vec4f(c1.w,c2.w,c3.w,c4.w)));
2842 float e,
float f,
float g,
float h,
2843 float i,
float j,
float k,
float l,
2844 float m,
float n,
float o,
float p)
2863 float e,
float f,
float g,
float h,
2864 float i,
float j,
float k,
float l,
2865 float m,
float n,
float o,
float p)
2876 assert(i>=0 && i<4);
2877 return(
vec4f(mtx[i][0],mtx[i][1],mtx[i][2],mtx[i][3]));
2883 assert(i>=0 && i<4);
2884 return(
vec4f(mtx[0][i],mtx[1][i],mtx[2][i],mtx[3][i]));
2890 assert(i>=0 && i<4);
2891 return(
vec4f(mtx[i][0],mtx[i][1],mtx[i][2],mtx[i][3]));
2903 operator const float *()
const
2931 operator std::string()
2932 {
return(
"(" + std::string(
row(0)) +
", " + std::string(
row(1)) +
", " + std::string(
row(2)) +
", " + std::string(
row(3)) +
")");}
2945 {
return(out <<
"(" << m.
row(0) <<
", " << m.
row(1) <<
", " << m.
row(2) <<
", " << m.
row(3) <<
")");}
2982 quat(
double x,
double y,
double z,
double w)
2993 {
return(q.
dot(r.q));}
3007 {
return(
vec4(-
vec3(q.x,q.y,q.z),q.w));}
3029 return(
mat3(
vec3(a2+b2-c2-d2,2*(b*c-a*d),2*(b*d+a*c)),
3030 vec3(2*(b*c+a*d),a2-b2+c2-d2,2*(c*d-a*b)),
3031 vec3(2*(b*d-a*c),2*(c*d+a*b),a2-b2-c2+d2)));
3039 return(
vec4(sin(angle)*v,cos(angle)));
3055 operator std::string()
3056 {
return(
"quat" + std::string(q));}
3059 std::string
to_string(std::string delimiter =
", ",
int space=0)
3060 {
return(
"quat" + q.
to_string(delimiter,space));}
3093 {
return(
vec4(-
vec3(r.q.x,r.q.y,r.q.z),r.q.w));}
3098 double v=a.q.w,w=b.q.w;
3099 vec3 p(a.q.x,a.q.y,a.q.z),q(b.q.x,b.q.y,b.q.z);
3117 return(
vec3(w.q.x,w.q.y,w.q.z));
3122 {
return(out << std::setprecision(GLSLMATH_PRECISION) <<
"(" <<
vec4(r).x <<
", " <<
vec4(r).y <<
", " <<
vec4(r).z <<
", " <<
vec4(r).w <<
")");}
3124 namespace glslmath {
3127 inline void print(
const char *s =
"",std::string prefix =
"",std::ostream &out = std::cout)
3129 out << prefix << s << std::endl;
3133 inline void print(std::string s,std::string prefix =
"",std::ostream &out = std::cout)
3135 out << prefix << s << std::endl;
3139 inline void print(
char c,std::string name =
"",std::string prefix =
"",std::ostream &out = std::cout)
3141 if (name.size() > 0) name = name +
" = ";
3143 out << name <<
"'" << c <<
"'" << std::endl;
3145 out << std::hex << name <<
"0x" << (int)c << std::endl;
3149 inline void print(
int v,std::string name =
"",std::string prefix =
"",std::ostream &out = std::cout)
3151 if (name.size() > 0) name = name +
" = ";
3152 out << prefix << name << v << std::endl;
3156 inline void print(
unsigned int v,std::string name =
"",std::string prefix =
"",std::ostream &out = std::cout)
3158 if (name.size() > 0) name = name +
" = ";
3159 out << prefix << name << v << std::endl;
3163 inline void print(
double v,std::string name =
"",std::string prefix =
"",std::ostream &out = std::cout)
3165 if (name.size() > 0) name = name +
" = ";
3166 out << std::setprecision(GLSLMATH_PRECISION) << prefix << name << v << std::endl;
3170 inline void print(
vec2 v,std::string name =
"",std::string prefix =
"",std::ostream &out = std::cout)
3172 if (name.size() > 0) name = name +
" = ";
3173 out << prefix << name << std::string(v) << std::endl;
3177 inline void print(
vec2f v,std::string name =
"",std::string prefix =
"",std::ostream &out = std::cout)
3179 if (name.size() > 0) name = name +
" = ";
3180 out << prefix << name << std::string(v) << std::endl;
3184 inline void print(
vec3 v,std::string name =
"",std::string prefix =
"",std::ostream &out = std::cout)
3186 if (name.size() > 0) name = name +
" = ";
3187 out << prefix << name << std::string(v) << std::endl;
3191 inline void print(
vec3f v,std::string name =
"",std::string prefix =
"",std::ostream &out = std::cout)
3193 if (name.size() > 0) name = name +
" = ";
3194 out << prefix << name << std::string(v) << std::endl;
3198 inline void print(
vec4 v,std::string name =
"",std::string prefix =
"",std::ostream &out = std::cout)
3200 if (name.size() > 0) name = name +
" = ";
3201 out << prefix << name << std::string(v) << std::endl;
3205 inline void print(
vec4f v,std::string name =
"",std::string prefix =
"",std::ostream &out = std::cout)
3207 if (name.size() > 0) name = name +
" = ";
3208 out << prefix << name << std::string(v) << std::endl;
3212 inline void print(
mat2 m,std::string name =
"",std::string prefix =
"",
int space=13,std::ostream &out = std::cout)
3214 if (name.size() > 0) name = name +
" = ";
3215 out << prefix << name <<
"/ " << m.
row(0).
to_string(
" ",space) <<
" \\" << std::endl;
3216 out << prefix << to_space(name.size()) <<
"\\ " << m.
row(1).
to_string(
" ",space) <<
" /" << std::endl;
3220 inline void print(
mat2f m,std::string name =
"",std::string prefix =
"",
int space=13,std::ostream &out = std::cout)
3222 if (name.size() > 0) name = name +
" = ";
3223 out << prefix <<
"/ " << m.
row(0).
to_string(
" ",space) <<
" \\" << std::endl;
3224 out << prefix << to_space(name.size()) <<
"\\ " << m.
row(1).
to_string(
" ",space) <<
" /" << std::endl;
3228 inline void print(
mat3 m,std::string name =
"",std::string prefix =
"",
int space=13,std::ostream &out = std::cout)
3230 if (name.size() > 0) name = name +
" = ";
3231 out << prefix << to_space(name.size()) <<
"/ " << m.
row(0).
to_string(
" ",space) <<
" \\" << std::endl;
3232 out << prefix << name <<
"| " << m.
row(1).
to_string(
" ",space) <<
" |" << std::endl;
3233 out << prefix << to_space(name.size()) <<
"\\ " << m.
row(2).
to_string(
" ",space) <<
" /" << std::endl;
3237 inline void print(
mat3f m,std::string name =
"",std::string prefix =
"",
int space=13,std::ostream &out = std::cout)
3239 if (name.size() > 0) name = name +
" = ";
3240 out << prefix << to_space(name.size()) <<
"/ " << m.
row(0).
to_string(
" ",space) <<
" \\" << std::endl;
3241 out << prefix << name <<
"| " << m.
row(1).
to_string(
" ",space) <<
" |" << std::endl;
3242 out << prefix << to_space(name.size()) <<
"\\ " << m.
row(2).
to_string(
" ",space) <<
" /" << std::endl;
3246 inline void print(
mat4 m,std::string name =
"",std::string prefix =
"",
int space=13,std::ostream &out = std::cout)
3248 if (name.size() > 0) name = name +
" = ";
3249 out << prefix << to_space(name.size()) <<
"/ " << m.
row(0).
to_string(
" ",space) <<
" \\" << std::endl;
3250 out << prefix << name <<
"| " << m.
row(1).
to_string(
" ",space) <<
" |" << std::endl;
3251 out << prefix << to_space(name.size()) <<
"| " << m.
row(2).
to_string(
" ",space) <<
" |" << std::endl;
3252 out << prefix << to_space(name.size()) <<
"\\ " << m.
row(3).
to_string(
" ",space) <<
" /" << std::endl;
3256 inline void print(
mat4f m,std::string name =
"",std::string prefix =
"",
int space=13,std::ostream &out = std::cout)
3258 if (name.size() > 0) name = name +
" = ";
3259 out << prefix << to_space(name.size()) <<
"/ " << m.
row(0).
to_string(
" ",space) <<
" \\" << std::endl;
3260 out << prefix << name <<
"| " << m.
row(1).
to_string(
" ",space) <<
" |" << std::endl;
3261 out << prefix << to_space(name.size()) <<
"| " << m.
row(2).
to_string(
" ",space) <<
" |" << std::endl;
3262 out << prefix << to_space(name.size()) <<
"\\ " << m.
row(3).
to_string(
" ",space) <<
" /" << std::endl;
3266 inline void print(
quat q,std::string name =
"",std::string prefix =
"",std::ostream &out = std::cout)
3268 if (name.size() > 0) name = name +
" = ";
3269 out << prefix << name << std::string(q) << std::endl;
3279 if (hue<0.0) hue=0.0;
3280 if (sat<0.0) sat=0.0;
3281 else if (sat>1.0) sat=1.0;
3282 if (val<0.0) val=0.0;
3283 else if (val>1.0) val=1.0;
3286 hue=hue-6.0*floor(hue/6.0);
3287 hue6=hue-floor(hue);
3290 s=val*(1.0-sat*hue6);
3291 t=val*(1.0-sat*(1.0-hue6));
3293 switch ((
int)floor(hue))
3323 double maxv,minv,diff,rdist,gdist,bdist;
3326 else if (r>1.0) r=1.0;
3329 else if (g>1.0) g=1.0;
3332 else if (b>1.0) b=1.0;
3346 if (maxv!=0.0) s=diff/maxv;
3352 rdist=(maxv-r)/diff;
3353 gdist=(maxv-g)/diff;
3354 bdist=(maxv-b)/diff;
3356 if (r==maxv) h=bdist-gdist;
3357 else if (g==maxv) h=2.0+rdist-bdist;
3358 else h=4.0+gdist-rdist;
3361 if (h<0.0) h+=360.0;
3364 return(
vec3(h,s,v));
3369 {
return(
hsv2rgb(hsv.x,hsv.y,hsv.z));}
3373 {
return(
rgb2hsv(rgb.x,rgb.y,rgb.z));}
3384 scoped_push(
const T &m)
3392 static void load_matrix(
const T &m=T(1))
3394 if (stack_.begin()==stack_.end())
3395 stack_.push_back(m);
3397 *(--stack_.end())=m;
3403 if (stack_.begin()==stack_.end())
3404 stack_.push_back(T(1));
3406 stack_.push_back(top());
3410 void push_matrix(
const T &m)
3412 if (stack_.begin()==stack_.end())
3413 stack_.push_back(m);
3415 stack_.push_back(top()*m);
3419 static void mult_matrix(
const T &m)
3421 if (stack_.begin()==stack_.end())
3422 stack_.push_back(m);
3424 *(--stack_.end())=top()*m;
3428 static const T top()
3430 assert(stack_.begin()!=stack_.end());
3431 return(*(--stack_.end()));
3437 assert(stack_.begin()!=stack_.end());
3443 static std::vector<T> stack_;
3447 #define load_matrix(m) scoped_push<mat4>::load_matrix(m)
3448 #define push_matrix() scoped_push<mat4>::push_matrix()
3449 #define mult_matrix(m) scoped_push<mat4>::mult_matrix(m)
3450 #define mult_matrix_scoped(m) scoped_push<mat4> __scoped_push__(m)
3451 #define top_matrix() scoped_push<mat4>::top()
3452 #define pop_matrix() scoped_push<mat4>::pop_matrix()
3455 std::vector<T> scoped_push<T>::stack_;
3458 class glScopedMatrixStack
3462 glScopedMatrixStack()
3464 #ifdef GL_VERSION_1_2
3469 ~glScopedMatrixStack()
3471 #ifdef GL_VERSION_1_2
3479 #ifndef glPushMatrixScoped
3480 #define glPushMatrixScoped() glScopedMatrixStack __glScopedMatrixStack__
3483 namespace glslmath {
3486 inline unsigned int gcd(
unsigned int a,
unsigned int b)
3501 inline unsigned int lcm(
unsigned int a,
unsigned int b)
3502 {
return(a*b/
gcd(a,b));}
3509 double l=(p-o).
dot(d);
3512 return((p-o).length());
3520 double l=(p-o).
dot(d);
3523 return((p-o).norm());
3532 double l=(p-a).
dot(n);
3535 double dh=(p-h).
norm();
3536 double da=(p-a).
norm();
3537 double db=(p-b).
norm();
3539 if (dh<da && dh<db)
return(sqrt(dh));
3540 if (da<db)
return(sqrt(da));
3541 else return(sqrt(db));
3550 double l=(p-a).
dot(n);
3553 double dh=(p-h).
norm();
3554 double da=(p-a).
norm();
3555 double db=(p-b).
norm();
3557 if (dh<da && dh<db)
return(dh);
3558 if (da<db)
return(da);
3564 const vec3 ¢er1,
const double radius1)
3566 vec3 d=center1-center0;
3571 if (radius1>radius0+r)
3576 else if (radius1+r>radius0)
3578 vec3 a=center1+d*radius1;
3579 vec3 b=center0-d*radius0;
3582 radius0=(a-b).
length()/2.0;
3604 if (r<0.0)
return(DBL_MAX);
3622 if (t2>-t1)
return(t1);
3628 vec3 o,
double r1,
double r2,
double r3)
3649 static const double epsilon=1E-10;
3653 if (fabs(c)<=epsilon)
return(DBL_MAX);
3655 return(n.
dot(o-p)/c);
3663 static const double epsilon=1E-5;
3666 vec3 edge1,edge2,tvec,pvec,qvec;
3674 pvec=d.
cross(edge2);
3677 det=edge1.
dot(pvec);
3680 if (fabs(det)<epsilon)
return(0);
3689 u=tvec.
dot(pvec)*inv_det;
3690 if (u<0.0 || u>1.0)
return(0);
3693 qvec=tvec.
cross(edge1);
3696 v=d.
dot(qvec)*inv_det;
3697 if (v<0.0 || u+v>1.0)
return(0);
3700 t=edge2.
dot(qvec)*inv_det;
3719 {
return((p-b).
norm()<=r2);}
3723 const vec3 &b,
const double r2)
3734 if (bmo2<r2)
return(1);
3737 if (bmod<0.0)
return(0);
3738 if (r2+bmod*bmod>bmo2)
return(1);
3745 const vec3 &b,
const double r)
3755 if (l0<-r)
return(0);
3759 l=(b-p).
length()-r*sqrt(1.0+cone*cone);
3771 if (d.x<0.0 && o.x<b.x-r.x)
return(0);
3772 if (d.x>0.0 && o.x>b.x+r.x)
return(0);
3773 if (d.y<0.0 && o.y<b.y-r.y)
return(0);
3774 if (d.y>0.0 && o.y>b.y+r.y)
return(0);
3775 if (d.z<0.0 && o.z<b.z-r.z)
return(0);
3776 if (d.z>0.0 && o.z>b.z+r.z)
return(0);
3780 if (d.x>0.0) l=(b.x-r.x-o.x)/d.x;
3781 else l=(b.x+r.x-o.x)/d.x;
3786 if (h.y<b.y-r.y && o.y>b.y-r.y)
return(0);
3787 if (h.y>b.y+r.y && o.y<b.y+r.y)
return(0);
3788 if (h.z<b.z-r.z && o.z>b.z-r.z)
return(0);
3789 if (h.z>b.z+r.z && o.z<b.z+r.z)
return(0);
3795 if (d.y>0.0) l=(b.y-r.y-o.y)/d.y;
3796 else l=(b.y+r.y-o.y)/d.y;
3801 if (h.x<b.x-r.x && o.x>b.x-r.x)
return(0);
3802 if (h.x>b.x+r.x && o.x<b.x+r.x)
return(0);
3803 if (h.z<b.z-r.z && o.z>b.z-r.z)
return(0);
3804 if (h.z>b.z+r.z && o.z<b.z+r.z)
return(0);
3810 if (d.z>0.0) l=(b.z-r.z-o.z)/d.z;
3811 else l=(b.z+r.z-o.z)/d.z;
3816 if (h.x<b.x-r.x && o.x>b.x-r.x)
return(0);
3817 if (h.x>b.x+r.x && o.x<b.x+r.x)
return(0);
3818 if (h.y<b.y-r.y && o.y>b.y-r.y)
return(0);
3819 if (h.y>b.y+r.y && o.y<b.y+r.y)
return(0);
3828 const vec3 &b,
const double r2)
3836 if (l*l>r2)
return(0);
3837 if (h.
dot(h)>2.0*(radius*radius+r2))
return(0);
3846 return(
dot(point-o,n)>0);
3851 const vec3 ¢er,
double radius)
3853 return(
dot(center-o,n)>-radius);
3858 const vec3 ¢er,
const vec3 &extent)
3860 return(
dot(center-o,n)>-0.5*
dot(extent,
vec3(fabs(n.x),fabs(n.y),fabs(n.z))));
3865 namespace glslnoise {
3874 int start=4,
float persist=0.5f,
3881 data=noise(sx,sy,sz,start,persist,seed);
3886 {
if (data)
delete data;}
3893 if (data) v=
interpolate(data,sizex,sizey,sizez,c1,c2,c3);
3897 else if (v>1.0f) v=1.0f;
3904 int sizex,sizey,sizez;
3907 inline static float get(
const float *data,
3908 int sx,
int sy,
int sz,
3911 assert(x>=0 && x<sx && y>=0 && y<sy && z>=0 && z<sz);
3912 return(data[x+(y+z*sy)*sx]);
3915 inline static void set(
float *data,
3916 int sx,
int sy,
int sz,
3920 assert(x>=0 && x<sx && y>=0 && y<sy && z>=0 && z<sz);
3921 data[x+(y+z*sy)*sx]=v;
3924 inline static void add(
float *data,
3925 int sx,
int sy,
int sz,
3929 assert(x>=0 && x<sx && y>=0 && y<sy && z>=0 && z<sz);
3930 data[x+(y+z*sy)*sx]+=v;
3933 inline static float interpolate(
float v0,
float v1,
float v2,
float v3,
float x)
3941 return(((p*x+q)*x+r)*x+v1);
3944 inline static float interpolatex(
const float *data,
3945 int sx,
int sy,
int sz,
3946 int k1,
int k2,
int k3,
3951 v1=get(data,sx,sy,sz,k1,k2,k3);
3952 v0=(k1>0)?get(data,sx,sy,sz,k1-1,k2,k3):v1;
3953 v2=(k1<sx-1)?get(data,sx,sy,sz,k1+1,k2,k3):v1;
3954 v3=(k1<sx-2)?get(data,sx,sy,sz,k1+2,k2,k3):v2;
3959 inline static float interpolatey(
const float *data,
3960 int sx,
int sy,
int sz,
3961 int k1,
int k2,
int k3,
3966 v1=interpolatex(data,sx,sy,sz,k1,k2,k3,wx);
3967 v0=(k2>0)?interpolatex(data,sx,sy,sz,k1,k2-1,k3,wx):v1;
3968 v2=(k2<sy-1)?interpolatex(data,sx,sy,sz,k1,k2+1,k3,wx):v1;
3969 v3=(k2<sy-2)?interpolatex(data,sx,sy,sz,k1,k2+2,k3,wx):v2;
3974 inline static float interpolatez(
const float *data,
3975 int sx,
int sy,
int sz,
3976 int k1,
int k2,
int k3,
3977 float wx,
float wy,
float wz)
3981 v1=interpolatey(data,sx,sy,sz,k1,k2,k3,wx,wy);
3982 v0=(k3>0)?interpolatey(data,sx,sy,sz,k1,k2,k3-1,wx,wy):v1;
3983 v2=(k3<sz-1)?interpolatey(data,sx,sy,sz,k1,k2,k3+1,wx,wy):v1;
3984 v3=(k3<sz-2)?interpolatey(data,sx,sy,sz,k1,k2,k3+2,wx,wy):v2;
3989 inline static float interpolate(
const float *data,
3990 int sx,
int sy,
int sz,
3991 float c1,
float c2,
float c3)
3996 c1=c1-(int)floorf(c1);
3997 c2=c2-(int)floorf(c2);
3998 c3=c3-(int)floorf(c3);
4000 k1=(int)floorf(c1*(sx-1));
4003 k2=(int)floorf(c2*(sy-1));
4006 k3=(int)floorf(c3*(sz-1));
4009 return(interpolatez(data,sx,sy,sz,k1,k2,k3,w1,w2,w3));
4012 inline static void average(
float *data,
4013 int sx,
int sy,
int sz,
4014 int x1,
int y1,
int z1,
4015 int x2,
int y2,
int z2,
4016 int dx,
int dy,
int dz)
4022 for (x=0; x<dx; x++)
4023 for (y=0; y<dy; y++)
4024 for (z=0; z<dz; z++)
4026 v1=get(data,sx,sy,sz,x1+x,y1+y,z1+z);
4027 v2=get(data,sx,sy,sz,x2+x,y2+y,z2+z);
4029 set(data,sx,sy,sz,x1+x,y1+y,z1+z,v);
4030 set(data,sx,sy,sz,x2+x,y2+y,z2+z,v);
4036 inline static float *noise(
int sx=64,
int sy=64,
int sz=64,
4037 int start=4,
float persist=0.5f,
4048 float *noise,*octave;
4052 if ((sx&(sx-1))!=0 || sx<1)
return(NULL);
4053 if ((sy&(sy-1))!=0 || sy<1)
return(NULL);
4054 if ((sz&(sz-1))!=0 || sz<1)
return(NULL);
4056 if ((start&(start-1))!=0 || start<1)
return(NULL);
4058 if ((noise=
new(std::nothrow)
float[sx*sy*sz])==NULL)
return(NULL);
4060 for (x=0; x<sx; x++)
4061 for (y=0; y<sy; y++)
4062 for (z=0; z<sz; z++) set(noise,sx,sy,sz,x,y,z,0.0f);
4064 if ((octave=
new(std::nothrow)
float[sx*sy*sz])==NULL)
4071 if (sy>size) size=sy;
4072 if (sz>size) size=sz;
4078 for (i=start; i<=size; i*=2)
4088 for (x=0; x<dx; x++)
4089 for (y=0; y<dy; y++)
4090 for (z=0; z<dz; z++)
4091 set(octave,dx,dy,dz,x,y,z,
4092 getrandom()*scaling);
4095 if (dx>1) average(octave,dx,dy,dz,0,0,0,dx-1,0,0,1,dy,dz);
4096 if (dy>1) average(octave,dx,dy,dz,0,0,0,0,dy-1,0,dx,1,dz);
4097 if (dz>1) average(octave,dx,dy,dz,0,0,0,0,0,dz-1,dx,dy,1);
4099 for (x=0; x<sx; x++)
4100 for (y=0; y<sy; y++)
4101 for (z=0; z<sz; z++)
4102 add(noise,sx,sy,sz,x,y,z,
4104 (sx>1)?(
float)x/(sx-1):0.0f,
4105 (sy>1)?(
float)y/(sy-1):0.0f,
4106 (sz>1)?(
float)z/(sz-1):0.0f));
4116 for (x=0; x<sx; x++)
4117 for (y=0; y<sy; y++)
4118 for (z=0; z<sz; z++)
4120 v=get(noise,sx,sy,sz,x,y,z);
4125 if (minv==maxv) maxv++;
4128 for (x=0; x<sx; x++)
4129 for (y=0; y<sy; y++)
4130 for (z=0; z<sz; z++)
4131 set(noise,sx,sy,sz,x,y,z,
4132 (get(noise,sx,sy,sz,x,y,z)-minv)/(maxv-minv));
4137 inline static float getrandom(
float seed=-1.0f)
4139 static const long long maxbits=50;
4140 static const long long maxnum=1ull<<maxbits;
4142 static long long number=0;
4144 if (seed>=0.0f && seed<=1.0f) number=(
long long)(seed*(maxnum-1));
4146 number=271*(number+331);
4147 number=(number<<(maxbits/3))+(number>>(2*maxbits/3));
4150 return((
float)number/(maxnum-1));
4155 inline static double noise(
vec3f coord,
int size=64,
int start=4)
4157 static perlinnoise N(size,size,size,start);
4159 return(N.interpolate(coord.x,coord.y,coord.z));
4162 inline static vec3 noise3D(
vec3f coord,
int size=64,
int start=4)
4164 static perlinnoise Nx(size,size,size,start,0.5f,0.25f),
4165 Ny(size,size,size,start,0.5f,0.5f),
4166 Nz(size,size,size,start,0.5f,0.75f);
4168 return(
vec3(Nx.interpolate(coord.x,coord.y,coord.z),
4169 Ny.interpolate(coord.x,coord.y,coord.z),
4170 Nz.interpolate(coord.x,coord.y,coord.z)));
4173 inline static vec4 noise4D(
vec3f coord,
int size=64,
int start=4)
4175 static perlinnoise Nx(size,size,size,start,0.5f,0.2f),
4176 Ny(size,size,size,start,0.5f,0.4f),
4177 Nz(size,size,size,start,0.5f,0.6f),
4178 Nw(size,size,size,start,0.5f,0.8f);
4180 return(
vec4(Nx.interpolate(coord.x,coord.y,coord.z),
4181 Ny.interpolate(coord.x,coord.y,coord.z),
4182 Nz.interpolate(coord.x,coord.y,coord.z),
4183 Nw.interpolate(coord.x,coord.y,coord.z)));
4188 namespace glslmath {
4198 if (!(v.length()==5)) errors++;
4199 vec3 a(-10,0,10),b(10,10,10);
4200 if (!(0.5*(a+b)==
vec3(0,5,10))) errors++;
4202 if (!(r==
vec4(1,1,0,0))) errors++;
4204 vec4 c1(1,0,0),c2(0,0,1,0.25);
4205 if (c1.blend(c2)!=
vec4(0.75,0,0.25,1)) errors++;
4216 if (!(M*M.invert()==
mat4())) errors++;
4228 if (v!=
vec4(3,4,3,1)) errors++;
4238 if (!(fromto*
vec4(0,1,0)==
vec4(1,0,0))) errors++;
4240 if (!(fabs(1-perspective[0].x)<1E-10 && fabs(-3-perspective[2].z)<1E-10)) errors++;
4242 if (
vec3(parallel*
vec4(1,2,3))!=
vec3(-1,2,3)) errors++;
4260 x1=top_matrix()[3].x;
4262 x2=top_matrix()[3].x;
4264 if (!(x1==0.5 && x2==0.0)) errors++;
4276 if (!(v.approx(w) && p.
approx(
vec3(0,-1,0)))) errors++;
4286 vec3 p(0,0,0),d(1,1,1);
4287 vec3 o(1,0,0),n(1,0,0);
4289 if (fabs(l-sqrt(3.0))>1E-10) errors++;
4294 vec3 coord(0.5,0.5,0.5);
4295 double noise_value=glslnoise::noise(coord,32);
4296 vec3 noise_vector=glslnoise::noise3D(coord,32);
4297 if (fabs(noise_value-0.4302484)>1E-5) errors++;
4298 if (!noise_vector.
approx(
vec3(0.4040824,0.6377566,0.6551602),1E-5)) errors++;
vec4f wzyx() const
swizzeling operator
Definition: glslmath.h:1058
mat4 inverse(const mat4 &m)
inline inverse
Definition: glslmath.h:2631
3D float vector
Definition: glslmath.h:584
static mat2f rows(const vec2f &r1, const vec2f &r2)
construct matrix from row vectors
Definition: glslmath.h:1459
static mat2 transpose(const mat2 &m)
static version of transpose operation
Definition: glslmath.h:1250
vec2f(const vec2 &v)
copy constructor
Definition: glslmath.h:272
double norm() const
get squared vector length
Definition: glslmath.h:136
static quat rotate(const vec3 &from, const vec3 &to)
create rotating quaternion from two vectors
Definition: glslmath.h:3043
mat4(const mat3 &m)
copy constructor
Definition: glslmath.h:2196
vec3f(const vec2 &v, const float vz=0.0f)
copy constructor
Definition: glslmath.h:601
vec2f & operator+=(const vec2f &v)
inplace addition
Definition: glslmath.h:293
mat3(const vec3 &diag)
custom constructor
Definition: glslmath.h:1565
static mat4 rotate(double angle, const vec3 &v)
create rotation matrix
Definition: glslmath.h:2451
vec3 row(const int i) const
row getter
Definition: glslmath.h:1661
vec4 operator[](const int i) const
subscript operator (column getter)
Definition: glslmath.h:2265
int vtest_plane_bbox(const vec3 &o, const vec3 &n, const vec3 ¢er, const vec3 &extent)
bounding box visibility test
Definition: glslmath.h:3857
mat4 transpose() const
transpose 4x4 matrix
Definition: glslmath.h:2363
vec4()
default constructor
Definition: glslmath.h:718
vec4 & operator-=(const vec4 &v)
inplace subtraction
Definition: glslmath.h:766
vec3f(const vec2f &v, const float vz=0.0f)
copy constructor
Definition: glslmath.h:598
mat4 & operator*=(const mat4 &m)
inplace multiplication
Definition: glslmath.h:2548
vec2(const double vx, const double vy)
component-wise constructor
Definition: glslmath.h:119
mat4 translate(const mat4 &m, const vec4 &v)
inline translate
Definition: glslmath.h:2603
vec2f row(const int i) const
row getter
Definition: glslmath.h:1481
float interpolate(float c1, float c2=0.0f, float c3=0.0f)
cubic interpolation of perlin noise
Definition: glslmath.h:3889
quat normalize() const
normalization
Definition: glslmath.h:3002
mat2f(const vec2f &r1, const vec2f &r2)
custom constructor
Definition: glslmath.h:1423
vec3 zyx() const
swizzeling operator
Definition: glslmath.h:454
vec4f & operator+=(const vec4f &v)
inplace addition
Definition: glslmath.h:1026
const float * c_ptr() const
const pointer to column-major array
Definition: glslmath.h:2055
static mat3f rows(float a, float b, float c, float d, float e, float f, float g, float h, float i)
construct matrix from row vectors
Definition: glslmath.h:2020
static mat2 scale(const vec2 &c)
create scaling matrix
Definition: glslmath.h:1287
static mat2f columns(const vec2f &c1, const vec2f &c2)
construct matrix from column vectors
Definition: glslmath.h:1444
vec3 hsv2rgb(double hue, double sat, double val)
hsv to rgb conversion
Definition: glslmath.h:3273
vec4 wzyx() const
swizzeling operator
Definition: glslmath.h:827
double norm() const
get squared vector length
Definition: glslmath.h:781
vec3f(const vec3f &v)
copy constructor
Definition: glslmath.h:592
static mat3 scale(double s, double t, double r)
create scaling matrix
Definition: glslmath.h:1754
mat2 & operator*=(const mat2 &m)
inplace multiplication
Definition: glslmath.h:1355
static mat3 rows(const vec3 &r1, const vec3 &r2, const vec3 &r3)
construct matrix from row vectors
Definition: glslmath.h:1636
mat2 invert() const
invert 2x2 matrix
Definition: glslmath.h:1254
static mat3f columns(const vec3f &c1, const vec3f &c2, const vec3f &c3)
construct matrix from column vectors
Definition: glslmath.h:1994
vec4(const double vx, const double vy, const double vz, const double vw=1.0)
component-wise constructor
Definition: glslmath.h:733
static double det(const mat2 &m)
static version of determinant operation
Definition: glslmath.h:1242
vec3 blend(const vec3 &rgb, double alpha) const
blending operator
Definition: glslmath.h:572
mat4f(const vec4f &r1, const vec4f &r2, const vec4f &r3, const vec4f &r4=vec4f(0, 0, 0, 1))
custom constructor
Definition: glslmath.h:2781
vec2f(const float v)
single-component constructor
Definition: glslmath.h:278
vec2 yx() const
swizzeling operator
Definition: glslmath.h:166
static mat3 rotate(double angle, const vec3 &v)
create rotation matrix
Definition: glslmath.h:1778
mat4f(const mat4 &m)
copy constructor
Definition: glslmath.h:2808
vec3 rgb() const
swizzeling operator
Definition: glslmath.h:823
vec3 & operator-=(const vec3 &v)
inplace subtraction
Definition: glslmath.h:408
std::string to_string(std::string delimiter=", ", int space=0)
conversion to string
Definition: glslmath.h:3059
quat(const vec4 &v=vec4(0, 0, 0, 1))
default constructor
Definition: glslmath.h:2977
void fromOpenGL(const double m[16])
convert from 16-element column-major OpenGL matrix
Definition: glslmath.h:2306
static mat4 perspective(double fovy, double aspect, double znear, double zfar)
create perspective matrix
Definition: glslmath.h:2712
mat2 & operator+=(const mat2 &m)
inplace addition
Definition: glslmath.h:1348
vec2 xy() const
swizzeling operator
Definition: glslmath.h:450
double dot(const vec3 &v) const
inner product
Definition: glslmath.h:429
mat3 & operator*=(const mat3 &m)
inplace multiplication
Definition: glslmath.h:1883
quat & operator*=(const quat &q)
inplace multiplication
Definition: glslmath.h:3105
vec3 normalize() const
normalize vector to unit length
Definition: glslmath.h:528
friend quat operator*(const quat &a, const quat &b)
multiplication of two quaternions
Definition: glslmath.h:3096
vec3 & operator+=(const vec3 &v)
inplace addition
Definition: glslmath.h:404
mat4(double diag=1.0)
default constructor
Definition: glslmath.h:2121
mat4(const vec4 &r1, const vec4 &r2, const vec4 &r3, const vec4 &r4=vec4(0, 0, 0, 1))
custom constructor
Definition: glslmath.h:2169
vec2 col(const int i) const
column getter
Definition: glslmath.h:1213
vec2f & operator-=(const vec2f &v)
inplace subtraction
Definition: glslmath.h:297
2D double vector
Definition: glslmath.h:108
static vec4 normal(const vec4 &a, const vec4 &b, const vec4 &c)
calculate triangle normal
Definition: glslmath.h:941
static double area(const vec3 &a, const vec3 &b, const vec3 &c)
calculate triangle area
Definition: glslmath.h:568
double norm() const
get quaternion norm
Definition: glslmath.h:2999
std::string to_string(std::string delimiter=", ", int space=0)
conversion to string
Definition: glslmath.h:654
const double * c_ptr() const
const pointer to linear array
Definition: glslmath.h:754
vec4 normalize() const
normalize vector to unit length
Definition: glslmath.h:902
std::string to_string(std::string delimiter=", ", int space=0)
conversion to string
Definition: glslmath.h:317
2x2 float matrix
Definition: glslmath.h:1408
vec4 & operator+=(const vec4 &v)
inplace addition
Definition: glslmath.h:762
static mat2f columns(float a, float b, float c, float d)
construct matrix from column vectors
Definition: glslmath.h:1451
double distance2ray2(vec3 p, vec3 o, vec3 d)
calculate the squared distance of a point p from a ray
Definition: glslmath.h:3516
4D double vector
Definition: glslmath.h:713
int itest_ray_sphere(const vec3 &o, const vec3 &d, const vec3 &b, const double r2)
geometric ray/sphere intersection test
Definition: glslmath.h:3722
static mat4 rotate(double angle, double vx, double vy, double vz)
create rotation matrix
Definition: glslmath.h:2446
vec4f(const float vx, const float vy, const float vz, const float vw=1.0f)
component-wise constructor
Definition: glslmath.h:993
vec4f()
default constructor
Definition: glslmath.h:966
vec4f(const vec4f &v)
copy constructor
Definition: glslmath.h:969
mat3 & operator+=(const mat3 &m)
inplace addition
Definition: glslmath.h:1876
vec4f(const vec3 &v, const float vw=1.0f)
copy constructor
Definition: glslmath.h:978
vec2f zw() const
swizzeling operator
Definition: glslmath.h:1046
const double * c_ptr() const
const pointer to column-major array
Definition: glslmath.h:1220
unsigned int lcm(unsigned int a, unsigned int b)
smallest common multiple
Definition: glslmath.h:3501
const float * c_ptr() const
const pointer to linear array
Definition: glslmath.h:285
bool approx(const vec2 &v, const double e=1E-10) const
test for approximate equality
Definition: glslmath.h:146
double norm(const vec2 &v)
get squared vector length
Definition: glslmath.h:225
static mat4 scale(const vec4 &c)
create scaling matrix
Definition: glslmath.h:2418
static mat4 columns(double a, double b, double c, double d, double e, double f, double g, double h, double i, double j, double k, double l, double m, double n, double o, double p)
construct matrix from column vectors
Definition: glslmath.h:2232
unsigned int gcd(unsigned int a, unsigned int b)
greatest common divisor
Definition: glslmath.h:3486
vec3f bgr() const
swizzeling operator
Definition: glslmath.h:646
void merge_spheres(vec3 ¢er0, double &radius0, const vec3 ¢er1, const double radius1)
merge two spheres
Definition: glslmath.h:3563
vec4 row(const int i) const
row getter
Definition: glslmath.h:2272
double det() const
calculate determinant of 3x3 matrix
Definition: glslmath.h:1703
mat2f(const mat2 &m)
copy constructor
Definition: glslmath.h:1434
vec4f(const vec2 &xy, const vec2 &zw)
copy constructor
Definition: glslmath.h:990
vec3f & operator+=(const vec3f &v)
inplace addition
Definition: glslmath.h:622
perlinnoise(int sx=64, int sy=64, int sz=64, int start=4, float persist=0.5f, float seed=0.0f)
default constructor
Definition: glslmath.h:3873
vec3f(const float v)
single-component constructor
Definition: glslmath.h:607
static mat2f rows(float a, float b, float c, float d)
construct matrix from row vectors
Definition: glslmath.h:1466
static mat4 columns(const vec4 &c1, const vec4 &c2, const vec4 &c3, const vec4 &c4)
construct matrix from column vectors
Definition: glslmath.h:2223
vec4f(const float v, const float vw=1.0f)
single-component constructor
Definition: glslmath.h:996
vec4f bgra() const
swizzeling operator
Definition: glslmath.h:1062
vec2 zw() const
swizzeling operator
Definition: glslmath.h:815
vec2 & operator-=(const vec2 &v)
inplace subtraction
Definition: glslmath.h:154
vec4f row(const int i) const
row getter
Definition: glslmath.h:2881
vec2 operator*(const double a, const vec2 &b)
left-hand side scalar multiplication
Definition: glslmath.h:197
int itest_point_sphere(const vec3 &p, const vec3 &b, const double r2)
geometric point/sphere intersection test
Definition: glslmath.h:3718
std::string to_string(std::string delimiter=", ", int space=0)
conversion to string
Definition: glslmath.h:1070
vec2f yx() const
swizzeling operator
Definition: glslmath.h:309
vec2f & operator*=(float v)
inplace multiplication
Definition: glslmath.h:301
4x4 float matrix
Definition: glslmath.h:2752
~perlinnoise()
destructor
Definition: glslmath.h:3885
vec2 & operator+=(const vec2 &v)
inplace addition
Definition: glslmath.h:150
const float * c_ptr() const
const pointer to column-major array
Definition: glslmath.h:1495
vec3f row(const int i) const
row getter
Definition: glslmath.h:2037
mat3 & operator>>=(const mat3 &m)
inplace multiplication (right-hand side)
Definition: glslmath.h:1897
vec2 lerp(double w, const vec2 &a, const vec2 &b)
linear interpolation
Definition: glslmath.h:245
vec3 rgb2hsv(double r, double g, double b)
rgb to hsv conversion
Definition: glslmath.h:3320
static mat3 columns(const vec3 &c1, const vec3 &c2, const vec3 &c3)
construct matrix from column vectors
Definition: glslmath.h:1618
double intersect_ray_plane(vec3 p, vec3 d, vec3 o, vec3 n)
ray/plane intersection
Definition: glslmath.h:3644
const double * c_ptr() const
const pointer to linear array
Definition: glslmath.h:125
vec4(const vec4 &v)
copy constructor
Definition: glslmath.h:721
mat4 & operator<<=(const mat4 &m)
inplace multiplication (left-hand side)
Definition: glslmath.h:2555
static mat3 rows(double a, double b, double c, double d, double e, double f, double g, double h, double i)
construct matrix from row vectors
Definition: glslmath.h:1644
vec3(const double v)
single-component constructor
Definition: glslmath.h:389
double length(const vec2 &v)
get vector length
Definition: glslmath.h:221
vec2 row(const int i) const
row getter
Definition: glslmath.h:1206
mat2 & operator>>=(const mat2 &m)
inplace multiplication (right-hand side)
Definition: glslmath.h:1369
mat3(double diag=1.0)
default constructor
Definition: glslmath.h:1549
vec2 operator/(const vec2 &a, const double b)
right-hand side scalar division
Definition: glslmath.h:205
mat4 inverse_transpose(const mat4 &m)
inline inverse transpose
Definition: glslmath.h:2635
vec2 normalize() const
normalize vector to unit length
Definition: glslmath.h:229
static mat4 frustum(double l, double r, double b, double t, double n, double f)
create frustum matrix
Definition: glslmath.h:2687
mat4(const vec4 &diag)
custom constructor
Definition: glslmath.h:2145
mat3(const mat2 &m)
copy constructor
Definition: glslmath.h:1599
vec2 xy() const
swizzeling operator
Definition: glslmath.h:811
vec4 bgra() const
swizzeling operator
Definition: glslmath.h:831
mat2(double diag=1.0)
default constructor
Definition: glslmath.h:1135
void fromOpenGL(const float m[4])
convert from 4-element column-major OpenGL matrix
Definition: glslmath.h:1503
vec2 & operator*=(double v)
inplace multiplication
Definition: glslmath.h:158
double norm() const
get squared vector length
Definition: glslmath.h:423
vec2()
default constructor
Definition: glslmath.h:113
2D float vector
Definition: glslmath.h:261
mat3f(const mat3 &m)
copy constructor
Definition: glslmath.h:1978
double determinant(const mat4 &m)
inline determinant
Definition: glslmath.h:2623
vec4f(const vec3f &v, const float vw=1.0f)
copy constructor
Definition: glslmath.h:975
void fromOpenGL(const float m[9])
convert from 9-element column-major OpenGL matrix
Definition: glslmath.h:2063
mat3(const vec3 &r1, const vec3 &r2, const vec3 &r3=vec3(0, 0, 1))
custom constructor
Definition: glslmath.h:1581
3D double vector
Definition: glslmath.h:372
2x2 double matrix
Definition: glslmath.h:1130
int itest_ray_bbox(const vec3 &o, const vec3 &d, const vec3 &b, const vec3 &r)
geometric ray/bbox intersection test
Definition: glslmath.h:3765
double length() const
get vector length
Definition: glslmath.h:133
int vtest_plane_sphere(const vec3 &o, const vec3 &n, const vec3 ¢er, double radius)
bounding sphere visibility test
Definition: glslmath.h:3850
vec3f col(const int i) const
column getter
Definition: glslmath.h:2044
vec4f(const vec2f &v, const float vz=0.0f, const float vw=1.0f)
copy constructor
Definition: glslmath.h:981
4x4 double matrix
Definition: glslmath.h:2116
int itest_cone_sphere(const vec3 &o, const vec3 &d, double cone, const vec3 &b, const double r)
geometric cone/sphere intersection test
Definition: glslmath.h:3744
vec2(const vec2 &v)
copy constructor
Definition: glslmath.h:116
vec4 blend(const vec4 &rgba) const
blending operator
Definition: glslmath.h:949
static mat3 scale(const vec3 &c)
create scaling matrix
Definition: glslmath.h:1762
mat3 getRotation() const
get 3x3 rotation submatrix (matrix is assumed to be orthonormal)
Definition: glslmath.h:2290
double ray_triangle_dist(const vec3 &o, const vec3 &d, const vec3 &v1, const vec3 &v2, const vec3 &v3)
calculate hit distance on ray to triangle
Definition: glslmath.h:3708
bool approx(const vec3 &v, const double e=1E-10) const
test for approximate equality
Definition: glslmath.h:446
4D float vector
Definition: glslmath.h:961
vec2 operator+(const vec2 &a, const vec2 &b)
addition of two vectors
Definition: glslmath.h:185
mat3f(float diag=1.0f)
default constructor
Definition: glslmath.h:1944
vec3 reflect(const vec3 &v, const vec3 &n)
reflect incident vector at normalized surface normal
Definition: glslmath.h:560
bool operator==(const vec2 &a, const vec2 &b)
comparison
Definition: glslmath.h:213
vec3(const vec2 &v, const double vz=0.0)
copy constructor
Definition: glslmath.h:383
quat invert() const
invert normalized quaternion
Definition: glslmath.h:3010
vec3f operator[](const int i) const
subscript operator (column getter)
Definition: glslmath.h:2030
static double area(const vec4 &a, const vec4 &b, const vec4 &c)
calculate triangle area
Definition: glslmath.h:945
static mat3f columns(float a, float b, float c, float d, float e, float f, float g, float h, float i)
construct matrix from column vectors
Definition: glslmath.h:2002
vec3(const vec3 &v)
copy constructor
Definition: glslmath.h:380
vec4f operator[](const int i) const
subscript operator (column getter)
Definition: glslmath.h:2874
vec3f & operator*=(float v)
inplace multiplication
Definition: glslmath.h:630
static mat4 invert(const mat4 &m)
static version of invert operation
Definition: glslmath.h:2405
bool approx(const vec4 &v, const double e=1E-10) const
test for approximate equality
Definition: glslmath.h:807
const float * c_ptr() const
const pointer to column-major array
Definition: glslmath.h:2899
static mat4f rows(float a, float b, float c, float d, float e, float f, float g, float h, float i, float j, float k, float l, float m, float n, float o, float p)
construct matrix from row vectors
Definition: glslmath.h:2862
vec3f(const float vx, const float vy, const float vz)
component-wise constructor
Definition: glslmath.h:604
void fromOpenGL(const float m[16])
convert from 16-element column-major OpenGL matrix
Definition: glslmath.h:2907
mat2 transpose() const
transpose 2x2 matrix
Definition: glslmath.h:1246
static vec3 normal(const vec3 &a, const vec3 &b, const vec3 &c)
calculate triangle normal
Definition: glslmath.h:564
vec3 cross(const vec3 &v) const
cross product (0,0,-1)/(-1,0,0)=(0,1,0)
Definition: glslmath.h:433
std::string to_string(std::string delimiter=", ", int space=0)
conversion to string
Definition: glslmath.h:174
friend quat operator+(const quat &a, const quat &b)
addition of two quaternions
Definition: glslmath.h:3076
const float * c_ptr() const
const pointer to linear array
Definition: glslmath.h:614
mat4 rotate(const mat4 &m, double angle, const vec3 &v)
inline rotate
Definition: glslmath.h:2611
vec3f & operator-=(const vec3f &v)
inplace subtraction
Definition: glslmath.h:626
static mat4 rows(const vec4 &r1, const vec4 &r2, const vec4 &r3, const vec4 &r4)
construct matrix from row vectors
Definition: glslmath.h:2244
vec4f & operator-=(const vec4f &v)
inplace subtraction
Definition: glslmath.h:1030
static mat4 translate(const vec4 &v)
create translation matrix
Definition: glslmath.h:2436
vec4 col(const int i) const
column getter
Definition: glslmath.h:2279
double distance2line(vec3 p, vec3 a, vec3 b)
calculate the distance of a point p from a line segment between vectors a and b
Definition: glslmath.h:3527
static mat4f rows(const vec4f &r1, const vec4f &r2, const vec4f &r3, const vec4f &r4)
construct matrix from row vectors
Definition: glslmath.h:2853
vec2f xy() const
swizzeling operator
Definition: glslmath.h:638
vec2f(const vec2f &v)
copy constructor
Definition: glslmath.h:269
int itest_plane_sphere(const vec3 &o, const vec3 &n, const double radius, const vec3 &b, const double r2)
geometric plane/sphere intersection test
Definition: glslmath.h:3827
static quat rotate(double angle, const vec3 &v)
create rotating quaternion
Definition: glslmath.h:3036
vec4 & operator*=(double v)
inplace multiplication
Definition: glslmath.h:770
static mat2 columns(const vec2 &c1, const vec2 &c2)
construct matrix from column vectors
Definition: glslmath.h:1169
vec4(const vec2 &v, const double vz=0.0, const double vw=1.0)
copy constructor
Definition: glslmath.h:727
double det() const
calculate determinant of 4x4 matrix
Definition: glslmath.h:2330
quaternion
Definition: glslmath.h:2972
static mat4 rows(double a, double b, double c, double d, double e, double f, double g, double h, double i, double j, double k, double l, double m, double n, double o, double p)
construct matrix from row vectors
Definition: glslmath.h:2253
static double det(const mat3 &m)
static version of determinant operation
Definition: glslmath.h:1711
static mat2 scale(double s, double t)
create scaling matrix
Definition: glslmath.h:1280
static mat4f columns(const vec4f &c1, const vec4f &c2, const vec4f &c3, const vec4f &c4)
construct matrix from column vectors
Definition: glslmath.h:2832
vec2f xy() const
swizzeling operator
Definition: glslmath.h:1042
vec3 & operator*=(double v)
inplace multiplication
Definition: glslmath.h:412
vec3f zyx() const
swizzeling operator
Definition: glslmath.h:642
mat2(const vec2 &diag)
custom constructor
Definition: glslmath.h:1145
double dot(const vec2 &a, const vec2 &b)
inner product
Definition: glslmath.h:241
double length() const
get vector length
Definition: glslmath.h:420
vec4(const vec2 &xy, const vec2 &zw)
copy constructor
Definition: glslmath.h:730
static mat4 transpose(const mat4 &m)
static version of transpose operation
Definition: glslmath.h:2367
std::ostream & operator<<(std::ostream &out, const vec2 &v)
output operator
Definition: glslmath.h:253
static mat2 rows(const vec2 &r1, const vec2 &r2)
construct matrix from row vectors
Definition: glslmath.h:1184
static mat2 rows(double a, double b, double c, double d)
construct matrix from row vectors
Definition: glslmath.h:1191
vec3 reflect(const vec3 &n) const
reflect incidental vector at normalized surface normal
Definition: glslmath.h:556
static mat4 scale(double s, double t, double r, double w=1.0)
create scaling matrix
Definition: glslmath.h:2409
vec3 xyz() const
swizzeling operator
Definition: glslmath.h:819
double dot(const vec4 &v) const
inner product
Definition: glslmath.h:787
vec4f col(const int i) const
column getter
Definition: glslmath.h:2888
double length() const
get quaternion length
Definition: glslmath.h:2996
static mat3 invert(const mat3 &m)
static version of invert operation
Definition: glslmath.h:1750
std::string to_string(std::string delimiter=", ", int space=0)
conversion to string
Definition: glslmath.h:842
double dot(const vec2 &v) const
inner product
Definition: glslmath.h:142
vec3f(const vec3 &v)
copy constructor
Definition: glslmath.h:595
perlin noise
Definition: glslmath.h:3868
3x3 double matrix
Definition: glslmath.h:1544
static mat3 transpose(const mat3 &m)
static version of transpose operation
Definition: glslmath.h:1719
int vtest_plane_point(const vec3 &o, const vec3 &n, const vec3 &point)
point visibility test
Definition: glslmath.h:3843
vec3 cross(const vec3 &a, const vec3 &b)
cross product
Definition: glslmath.h:544
const double * c_ptr() const
const pointer to column-major array
Definition: glslmath.h:1679
int intersect_ray_triangle(const vec3 &o, const vec3 &d, const vec3 &v0, const vec3 &v1, const vec3 &v2, vec3 &tuv)
Moeller-Trumbore ray/triangle intersection.
Definition: glslmath.h:3659
double distance2ray(vec3 p, vec3 o, vec3 d)
calculate the distance of a point p from a ray
Definition: glslmath.h:3505
std::string to_string(std::string delimiter=", ", int space=0)
conversion to string
Definition: glslmath.h:469
vec2 normalize(const vec2 &v)
normalization to unit length
Definition: glslmath.h:237
const double * c_ptr() const
const pointer to linear array
Definition: glslmath.h:396
mat3f(const vec3f &r1, const vec3f &r2, const vec3f &r3=vec3f(0, 0, 1))
custom constructor
Definition: glslmath.h:1960
vec3 bgr() const
swizzeling operator
Definition: glslmath.h:458
double length() const
get vector length
Definition: glslmath.h:778
mat4 transpose(const mat4 &m)
inline transpose
Definition: glslmath.h:2627
mat2f(float diag=1.0f)
default constructor
Definition: glslmath.h:1413
double dot(const quat &r) const
inner product
Definition: glslmath.h:2992
vec2f(const float vx, const float vy)
component-wise constructor
Definition: glslmath.h:275
static mat2 invert(const mat2 &m)
static version of invert operation
Definition: glslmath.h:1276
quat conjugate() const
conjugation
Definition: glslmath.h:3006
mat4 scale(const mat4 &m, const vec4 &c)
inline scale
Definition: glslmath.h:2591
3x3 float matrix
Definition: glslmath.h:1939
vec4f(const vec2 &v, const float vz=0.0f, const float vw=1.0f)
copy constructor
Definition: glslmath.h:984
vec4 reflect(const vec4 &n) const
reflect incident vector at normalized surface normal
Definition: glslmath.h:930
double det() const
calculate determinant of 2x2 matrix
Definition: glslmath.h:1238
vec4f & operator*=(float v)
inplace multiplication
Definition: glslmath.h:1034
static mat3f rows(const vec3f &r1, const vec3f &r2, const vec3f &r3)
construct matrix from row vectors
Definition: glslmath.h:2012
vec4(const double v, const double vw=1.0)
single-component constructor
Definition: glslmath.h:736
vec4f(const vec2f &xy, const vec2f &zw)
copy constructor
Definition: glslmath.h:987
mat4f(float diag=1.0f)
default constructor
Definition: glslmath.h:2757
vec3f rgb() const
swizzeling operator
Definition: glslmath.h:1054
mat4 invert() const
invert 4x4 matrix
Definition: glslmath.h:2371
vec3 getTranslation() const
get translation column vector (matrix is assumed to be affine)
Definition: glslmath.h:2294
vec3f()
default constructor
Definition: glslmath.h:589
mat3 invert() const
invert 3x3 matrix
Definition: glslmath.h:1723
static mat4 lookat(const vec3 &eye, const vec3 ¢er, const vec3 &up=vec3(0, 1, 0))
create lookat matrix
Definition: glslmath.h:2731
vec4(const vec3 &v, const double vw=1.0)
copy constructor
Definition: glslmath.h:724
static mat4 translate(double x, double y, double z)
create translation matrix
Definition: glslmath.h:2427
mat3 transpose() const
transpose 3x3 matrix
Definition: glslmath.h:1715
friend quat operator-(const quat &a, const quat &b)
subtraction of two quaternions
Definition: glslmath.h:3080
static mat2 rotate(double angle)
create rotation matrix
Definition: glslmath.h:1295
bool operator!=(const vec2 &a, const vec2 &b)
negated comparison
Definition: glslmath.h:217
double distance2line2(vec3 p, vec3 a, vec3 b)
calculate the squared distance of a point p from a line segment between vectors a and b
Definition: glslmath.h:3545
vec2f()
default constructor
Definition: glslmath.h:266
static mat3 rotate(double angle, double vx, double vy, double vz)
create rotation matrix
Definition: glslmath.h:1771
static mat4 rigid(double a, const vec3 &v, const vec3 &t)
create rigid-body transformation matrix
Definition: glslmath.h:2463
void fromOpenGL(const double m[4])
convert from 4-element column-major OpenGL matrix
Definition: glslmath.h:1228
static mat4f columns(float a, float b, float c, float d, float e, float f, float g, float h, float i, float j, float k, float l, float m, float n, float o, float p)
construct matrix from column vectors
Definition: glslmath.h:2841
vec4 cross(const vec4 &v) const
cross product (0,0,-1,0)/(-1,0,0,0)=(0,1,0,0)
Definition: glslmath.h:791
double intersect_ray_ellipsoid(vec3 p, vec3 d, vec3 o, double r1, double r2, double r3)
ray/ellipsoid intersection
Definition: glslmath.h:3627
mat2(const vec2 &r1, const vec2 &r2)
custom constructor
Definition: glslmath.h:1155
vec3f xyz() const
swizzeling operator
Definition: glslmath.h:1050
vec3 col(const int i) const
column getter
Definition: glslmath.h:1668
mat4 & operator>>=(const mat4 &m)
inplace multiplication (right-hand side)
Definition: glslmath.h:2562
const float * c_ptr() const
const pointer to linear array
Definition: glslmath.h:1018
static mat3 rotate(const vec3 &from, const vec3 &to)
create rotation matrix
Definition: glslmath.h:1806
vec3(const double vx, const double vy, const double vz)
component-wise constructor
Definition: glslmath.h:386
double intersect_ray_unitsphere(vec3 p, vec3 d)
ray/unitsphere intersection
Definition: glslmath.h:3587
static mat4 parallel(const vec3 &p, const vec3 &n, const vec3 &d)
create parallel projection matrix
Definition: glslmath.h:2698
mat2 & operator<<=(const mat2 &m)
inplace multiplication (left-hand side)
Definition: glslmath.h:1362
vec2 operator[](const int i) const
subscript operator (column getter)
Definition: glslmath.h:1199
void fromOpenGL(const double m[9])
convert from 9-element column-major OpenGL matrix
Definition: glslmath.h:1687
quat(double x, double y, double z, double w)
custom constructor
Definition: glslmath.h:2982
static double det(const mat4 &m)
static version of determinant operation
Definition: glslmath.h:2359
mat4 & operator+=(const mat4 &m)
inplace addition
Definition: glslmath.h:2541
static mat4 transform(const mat3 &m, const vec3 &v)
create affine (resp.
Definition: glslmath.h:2647
static mat3 columns(double a, double b, double c, double d, double e, double f, double g, double h, double i)
construct matrix from column vectors
Definition: glslmath.h:1626
vec2f col(const int i) const
column getter
Definition: glslmath.h:1488
vec2f operator[](const int i) const
subscript operator (column getter)
Definition: glslmath.h:1474
vec2 operator-(const vec2 &a, const vec2 &b)
subtraction of two vectors
Definition: glslmath.h:189
vec3 operator[](const int i) const
subscript operator (column getter)
Definition: glslmath.h:1654
static mat2 columns(double a, double b, double c, double d)
construct matrix from column vectors
Definition: glslmath.h:1176
const double * c_ptr() const
const pointer to column-major array
Definition: glslmath.h:2298
mat3 & operator<<=(const mat3 &m)
inplace multiplication (left-hand side)
Definition: glslmath.h:1890
vec2(const double v)
single-component constructor
Definition: glslmath.h:122
vec3()
default constructor
Definition: glslmath.h:377
void print(const char *s="", std::string prefix="", std::ostream &out=std::cout)
pretty print
Definition: glslmath.h:3127
quat(const vec3 &v, double w)
custom constructor
Definition: glslmath.h:2987
vec4f(const vec4 &v)
copy constructor
Definition: glslmath.h:972
static mat4 ortho(double l, double r, double b, double t, double n=-1, double f=1)
create orthographic matrix
Definition: glslmath.h:2677
static mat4 rotate(const vec3 &from, const vec3 &to)
create rotation matrix
Definition: glslmath.h:2456
vec2 mix(const vec2 &a, const vec2 &b, double w)
linear interpolation (GLSL-style)
Definition: glslmath.h:249