9#ifndef __GLAMER__geometry__
10#define __GLAMER__geometry__
29template <
typename T =
double>
34 r(r),theta(theta),phi(phi){};
55 if(&p ==
this)
return true;
56 return (r==p.r)*(theta==p.theta)*(phi==p.phi);
86 double s1 = sin((theta - p.theta)/2);
87 double s2 = sin((phi - p.phi)/2);
89 return 2*asin(sqrt( s1*s1 + cos(theta)*cos(p.theta)*s2*s2 ) );
105 p[0] = -sin(theta)*cos(phi);
106 p[1] = -sin(theta)*sin(phi);
173template <
typename T =
double>
177 v[0] = v[1] = v[2] = v[3] = 0;
179 Quaternion(T s,T x,T y,T z){
185 Quaternion(
const Quaternion &q){
197 Quaternion(SphericalPoint<T> sp){
202 Quaternion &operator=(
const Quaternion &q){
211 Quaternion operator*(
const Quaternion &q)
const{
213 v[0]*q.v[0] - v[1]*q.v[1] - v[2]*q.v[2] - v[3]*q.v[3],
214 v[0]*q.v[1] + v[1]*q.v[0] + v[2]*q.v[3] - v[3]*q.v[2],
215 v[0]*q.v[2] - v[1]*q.v[3] + v[2]*q.v[0] + v[3]*q.v[1],
216 v[0]*q.v[3] + v[1]*q.v[2] - v[2]*q.v[1] + v[3]*q.v[0]);
220 Quaternion conj()
const{
221 return Quaternion(v[0],-v[1],-v[2],-v[3]);
228 Quaternion inverse()
const{
229 return this->conj()/(v[0]*v[0] + v[1]*v[1] + v[2]*v[2] + v[3]*v[3]);
234 return sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2] + v[3]*v[3]);
238 Quaternion RotateX(T theta){
239 Quaternion R = q_x_rotation(theta);
240 return R*(*this)*R.conj();
243 Quaternion RotateY(T theta){
244 Quaternion R = q_y_rotation(theta);
245 return R*(*this)*R.conj();
248 Quaternion RotateZ(T theta){
249 Quaternion R = q_z_rotation(theta);
250 return R*(*this)*R.conj();
256 Quaternion RotateYZ(T theta,T phi){
257 Quaternion R = q_z_rotation(theta)*q_z_rotation(phi);
258 return R*(*this)*R.conj();
263 Quaternion Rotate(
const Quaternion &R)
const{
264 return R*(*this)*R.conj();
269 void RotInplace(
const Quaternion &R){
270 *
this = R*(*this)*R.conj();
276 ,
const Quaternion &R){
279 return q.to_point_3d();
284 static SphericalPoint<T> Rotate(
const SphericalPoint<T> &p
285 ,
const Quaternion &R){
288 return q.to_SpericalPoint();
291 Quaternion operator+(
const Quaternion &q)
const{
292 return Quaternion(v[0] + q.v[0] , v[1] + q.v[1] , v[2] + q.v[2] , v[3] + q.v[3] );
295 Quaternion operator-(
const Quaternion &q)
const{
296 return Quaternion(v[0] - q.v[0] , v[1] - q.v[1] , v[2] - q.v[2] , v[3] - q.v[3] );
300 Quaternion operator/(
double s)
const{
301 return Quaternion(v[0]/s,v[1]/s,v[2]/s,v[3]/s);
304 Quaternion operator*(
double s)
const{
305 return Quaternion(v[0]*s,v[1]*s,v[2]*s,v[3]*s);
310 return (*
this*q).to_point_3d();
314 T scalor(
const Quaternion &q)
const{
315 return v[1]*q.v[1] + v[2]*q.v[2] + v[3]*q.v[3];
324 SphericalPoint<T> to_SpericalPoint()
const{
325 SphericalPoint<T> sp;
326 sp.cartisianTOspherical(v+1);
334 T & operator[](
int i){
return v[i];}
337 static Quaternion q_x_rotation(T theta){
338 return Quaternion(cos(theta/2),sin(theta/2),0,0);
341 static Quaternion q_y_rotation(T theta){
342 return Quaternion(cos(theta/2),0,-sin(theta/2),0);
345 static Quaternion q_z_rotation(T theta){
346 return Quaternion(cos(theta/2),0,0,sin(theta/2));
352bool intersect(
const PosType a1[],
const PosType a2[]
353 ,
const PosType b1[],
const PosType b2[]);
355int intersect(
const std::vector<Point_2d> &curve);
356int intersections(
const std::vector<Point_2d> &curve,std::vector<Point_2d> &intersections,std::vector<std::pair<int,int> > &segments);
365int orientation(
const PosType p[],
const PosType q[],
const PosType r[]);
369bool onSegment(
const PosType p[],
const PosType q[],
const PosType r[]);
372double AngleBetween2d(
double v1[],
double v2[]);
388 x[0] = r*cos(theta)*cos(phi);
389 x[1] = r*cos(theta)*sin(phi);
397 ,r*cos(theta)*sin(phi),r*sin(theta) );
403 r = sqrt( x[0]*x[0] + x[1]*x[1] +x[2]*x[2]);
404 theta = asin(x[2]/r);
405 phi = atan2(x[1],x[0]);
410 r = sqrt( x[0]*x[0] + x[1]*x[1] +x[2]*x[2]);
411 theta = asin(x[2]/r);
412 phi = atan2(x[1],x[0]);
426 double ct = cos(theta),st = sin(theta);
427 double cosphi = cos(phi - central.phi);
428 double so = sin(central.theta);
429 double co = cos(central.theta);
431 PosType k = 2/( 1 + so*st + co*ct*cosphi );
433 x[0] = k*(ct*sin(phi - central.phi));
434 x[1] = k*(co*st - so*ct*cosphi);
441 double ct = cos(theta),st = sin(theta);
442 double cosphi = cos(phi - central.phi);
443 double so = sin(central.theta);
444 double co = cos(central.theta);
446 PosType k = 2/( 1 + so*st + co*ct*cosphi );
448 x[0] = k*(ct*sin(phi - central.phi));
449 x[1] = k*(co*st - so*ct*cosphi);
456 double ct = cos(theta),st = sin(theta);
457 double cosphi = cos(phi - central.phi);
458 double so = sin(central.theta);
459 double co = cos(central.theta);
461 PosType k = 2/( 1 + so*st + co*ct*cosphi );
463 return Point_2d(k*(ct*sin(phi - central.phi)),k*(co*st - so*ct*cosphi));
478 double ct = cos(theta),st = sin(theta);
479 double cosphi = cos(phi - central.phi);
480 double so = sin(central.theta);
481 double co = cos(central.theta);
483 x[0] = ct*sin(phi - central.phi);
484 x[1] = co*st - so*ct*cosphi;
486 if(st*so + ct*co*cosphi < 0 ){
487 double s = sqrt( x[0]*x[0] + x[1]*x[0] );
497 double c = cos(theta);
498 return Point_2d( c*sin(phi - central.phi),
499 cos(central.theta)*sin(theta) - sin(central.theta)*c*cos(phi - central.phi) );
509 PosType rho = sqrt(x[0]*x[0] + x[1]*x[1]);
510 PosType c = asin(rho);
512 theta = asin( cos(c)*sin(central.theta) + x[1]*cos(central.theta) );
513 phi = central.phi + atan2(x[0] , cos(central.theta)*cos(c)
514 - x[1]*sin(central.theta) );
522 PosType rho = sqrt(x[0]*x[0] + x[1]*x[1]);
523 PosType c = asin(rho);
525 double at=cos(c)*sin(central.theta) + x[1]*cos(central.theta);
526 at = (at > 1) ? 1 : at;
527 at = (at < -1) ? -1 : at;
529 phi = central.phi + atan2(x[0] , cos(central.theta)*cos(c)
530 - x[1]*sin(central.theta) );
537 return atan2( sin(central.theta) * sin(phi - central.phi)
538 , cos(phi - central.phi) );
545 return atan2( cos(central.theta) * cos(theta) + sin(central.theta) * sin(central.theta)
546 *cos(phi - central.phi)
547 , -sin(phi - central.phi) * sin(phi - central.phi) );
555 PosType rho = sqrt(x[0]*x[0] + x[1]*x[1]);
556 if(rho==0)
return *
this;
558 PosType c = asin(rho);
559 T new_theta = asin( cos(c)*sin(theta) + x[1]*sin(c)*cos(theta)/rho );
560 T new_phi = phi + atan2(x[0]*sin(c),rho*cos(theta)*cos(c)
561 - x[1]*sin(theta)*sin(c) );
573 return Point_3d<T>(-cos(phi)*sin(theta),-sin(theta)*sin(phi),cos(theta));
584 return sqrt( (x1[0]-x2[0])*(x1[0]-x2[0]) + (x1[1]-x2[1])*(x1[1]-x2[1])
585 + (x1[2]-x2[2])*(x1[2]-x2[2]) );
592 if(p1 == p2)
return 0;
593 double ans = sin(p1.theta)*sin(p2.theta) + cos(p1.theta)*cos(p2.theta)*cos(p1.phi-p2.phi);
594 if(fabs(ans) > 1)
return 0;
595 return acos(sin(p1.theta)*sin(p2.theta) + cos(p1.theta)*cos(p2.theta)*cos(p1.phi-p2.phi));
604 long operator[](
long i){
618std::vector<Point_2d> DeInterset(
const std::vector<Point_2d> &points);
624 return os <<
"r: " << p.r <<
" theta: " << p.theta <<
" phi: " << p.phi;
represents a point in spherical coordinates, theta = 0 is equator
Definition geometry.h:30
Point_3d< T > unitPhi()
unit vector in phi direction
Definition geometry.h:567
void cartisianTOspherical(T const x[])
set the spherical coordinates of the point from the cartisian coordinates
Definition geometry.h:402
T OrthographicAnglePhi(const SphericalPoint ¢ral)
the angle between the orthographic x-axis and the constant Phi curve
Definition geometry.h:542
T angular_separation(SphericalPoint &p)
angle between points. This uses the haversine formula that is more stable for small angles than the m...
Definition geometry.h:85
void OrthographicProjection(const SphericalPoint ¢ral, T x[]) const
Calculates the orthographic projection of the point onto a plane.
Definition geometry.h:474
Point_3d< T > TOcartisian() const
output cartisian coordinates of the point
Definition geometry.h:395
Point_2d StereographicProjection(const SphericalPoint ¢ral) const
Definition geometry.h:453
SphericalPoint< T > InverseOrthographicProjection(const Point_2d &x)
Definition geometry.h:552
void StereographicProjection(const SphericalPoint ¢ral, Point_2d &x) const
Definition geometry.h:437
Point_3d< T > unitTheta()
unit vector in theta direction
Definition geometry.h:572
void StereographicProjection(const SphericalPoint ¢ral, T x[]) const
Calculates the stereographic projection of the point onto a plane.
Definition geometry.h:422
void InverseOrthographicProjection(const SphericalPoint ¢ral, T const x[])
Convert from an orthographic projection of the plane onto the unit sphere.
Definition geometry.h:505
void TOcartisian(T x[]) const
output Cartesian coordinates of the point
Definition geometry.h:387
T OrthographicAngleTheta(const SphericalPoint ¢ral)
the angle between the orthographic x-axis and the constant theta curve
Definition geometry.h:534
Point_2d OrthographicProjection(const SphericalPoint ¢ral) const
Definition geometry.h:494
void InverseOrthographicProjection(const SphericalPoint ¢ral, const Point_2d &x)
Definition geometry.h:518
T AngleSeporation(const SphericalPoint< T > &p1, const SphericalPoint< T > &p2)
Angular seporation between points.
Definition geometry.h:590
T Seporation(const SphericalPoint< T > &p1, const SphericalPoint< T > &p2)
3 dimensional distance between points
Definition geometry.h:578
Definition utilities.h:39
Class for representing points or vectors in 2 dimensions. Not that the dereferencing operator is over...
Definition point.h:48
Class for representing points or vectors in 3 dimensions. Not that the dereferencing operator is over...
Definition point.h:883
used so that an index will not go out of bounds, ie. CYCLIC[0]=0, CYCLIC[n]=0, CYCLIC[-1]=n-1,...
Definition geometry.h:600