1 /* 2 * This file is part of Healpix Java. 3 * 4 * This code is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation; either version 2 of the License, or 7 * (at your option) any later version. 8 * 9 * This code is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this code; if not, write to the Free Software 16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 17 * 18 * For more information about HEALPix, see http://healpix.sourceforge.net 19 */ 20 21 package healpix.essentials; 22 23 /** An angular position on the unit sphere. 24 25 @copyright 2011 Max-Planck-Society 26 @author Martin Reinecke */ 27 public class Pointing 28 { 29 /** Colatitude in radians (0 is North Pole; Pi is South Pole) */ 30 public double theta; 31 32 /** Longitude in radians */ 33 public double phi; 34 35 /** Default constructor */ Pointing()36 public Pointing() {} 37 Pointing(Pointing ptg)38 public Pointing(Pointing ptg) 39 { this.theta = ptg.theta; this.phi = ptg.phi; } 40 41 /** Simple constructor initializing both values. 42 @param theta in radians [0,Pi] 43 @param phi in radians [0,2*Pi] */ Pointing(double theta, double phi)44 public Pointing(double theta, double phi) 45 { this.theta = theta; this.phi = phi; } 46 47 /** Conversion from {@link Vec3} */ Pointing(Vec3 vec)48 public Pointing(Vec3 vec) 49 { 50 theta = FastMath.atan2(Math.sqrt(vec.x*vec.x+vec.y*vec.y),vec.z); 51 phi = FastMath.atan2 (vec.y,vec.x); 52 if (phi<0.) phi += 2*Math.PI; 53 if (phi>=2*Math.PI) phi -= 2*Math.PI; 54 } 55 56 /** Conversion from {@link Zphi} */ Pointing(Zphi zphi)57 public Pointing (Zphi zphi) 58 { 59 double xy=Math.sqrt((1.-zphi.z)*(1.+zphi.z)); 60 theta = FastMath.atan2(xy,zphi.z); phi=zphi.phi; 61 } 62 // for some reason, the alternative below is much slower... 63 //{ theta=FastMath.acos(zphi.z); phi=zphi.phi; } 64 65 /** Normalize theta range */ normalizeTheta()66 public void normalizeTheta() 67 { 68 theta=HealpixUtils.fmodulo(theta,2*Math.PI); 69 if (theta>Math.PI) 70 { 71 phi+=Math.PI; 72 theta=2*Math.PI-theta; 73 } 74 } 75 76 /** Normalize theta and phi ranges */ normalize()77 public void normalize() 78 { 79 normalizeTheta(); 80 phi=HealpixUtils.fmodulo(phi,2*Math.PI); 81 } 82 toString()83 public String toString() 84 { 85 StringBuilder s = new StringBuilder(); 86 s.append("ptg(");s.append(theta); 87 s.append(",");s.append(phi); 88 s.append(")"); 89 return s.toString(); 90 } 91 equals(Object o)92 public boolean equals(Object o) 93 { 94 if (this==o) return true; 95 if ((o==null) || (getClass()!=o.getClass())) return false; 96 Pointing pointing = (Pointing) o; 97 if (Double.compare(pointing.phi, phi) != 0) return false; 98 if (Double.compare(pointing.theta, theta) != 0) return false; 99 return true; 100 } 101 hashCode()102 public int hashCode() 103 { 104 int result = Double.valueOf(theta).hashCode(); 105 result = 31 * result + Double.valueOf(phi).hashCode(); 106 return result; 107 } 108 } 109