| 1 |
package gov.fnal.eag.healpix; |
| 2 |
|
| 3 |
import java.util.ArrayList; |
| 4 |
|
| 5 |
import javax.vecmath.Vector3d; |
| 6 |
|
| 7 |
|
| 8 |
/** |
| 9 |
* @author kuropat |
| 10 |
* The class represents square region on a sphere defined by raMin, raMax |
| 11 |
* decMin,decMax in degrees. The dec varies from Pi/2 to -Pi/2. |
| 12 |
* The ra varies from 0. inclusive to 2Pi exclusive. Negative value of ra |
| 13 |
* will be corrected by adding 2Pi |
| 14 |
* |
| 15 |
*/ |
| 16 |
public class Region { |
| 17 |
private static final double TWOPI = 2.0*Math.PI; |
| 18 |
private double raMin; |
| 19 |
private double raMax; |
| 20 |
private double decMin; |
| 21 |
private double decMax; |
| 22 |
private double tetMin; |
| 23 |
private double tetMax; |
| 24 |
private double phiMin; |
| 25 |
private double phiMax; |
| 26 |
private static final double epsilon = 1.0e-10; |
| 27 |
// private boolean normalized = false; |
| 28 |
private ArrayList vertices; |
| 29 |
private double PI = Math.PI; |
| 30 |
private BitManipulation bm; |
| 31 |
/** |
| 32 |
* default constructor |
| 33 |
* @param xMin ra min in degrees |
| 34 |
* @param xMax ra max in degrees |
| 35 |
* @param yMin dec min in degrees |
| 36 |
* @param yMax dec max in degrees |
| 37 |
*/ |
| 38 |
public Region(double xMin, double xMax, double yMin, double yMax) { |
| 39 |
super(); |
| 40 |
bm = new BitManipulation(); |
| 41 |
this.raMin = xMin; |
| 42 |
this.raMax = xMax; |
| 43 |
this.phiMin = Math.toRadians(raMin); |
| 44 |
this.phiMax = Math.toRadians(raMax); |
| 45 |
if (phiMin < 0.) phiMin += TWOPI; |
| 46 |
if (phiMax < 0. ) phiMax += TWOPI; |
| 47 |
if (this.phiMax < this.phiMin) this.phiMax += TWOPI; |
| 48 |
this.phiMin = bm.MODULO(this.phiMin, TWOPI) - epsilon; // phi min, excluding 2pi period |
| 49 |
this.phiMax = bm.MODULO(this.phiMax, TWOPI) + epsilon; |
| 50 |
this.raMin = Math.toDegrees(this.phiMin); |
| 51 |
this.raMax = Math.toDegrees(this.phiMax); |
| 52 |
this.decMin = yMin; |
| 53 |
this.decMax = yMax; |
| 54 |
|
| 55 |
|
| 56 |
this.tetMax = PI/2. - Math.toRadians(decMin); |
| 57 |
this.tetMin = PI/2. - Math.toRadians(decMax); |
| 58 |
|
| 59 |
// create list of vertex vectors |
| 60 |
vertices = new ArrayList(); |
| 61 |
PixTools pt = new PixTools(); |
| 62 |
Vector3d vv = pt.Ang2Vec(tetMin,phiMin); |
| 63 |
vertices.add(vv); |
| 64 |
vv = pt.Ang2Vec(tetMin,phiMax); |
| 65 |
vertices.add(vv); |
| 66 |
vv = pt.Ang2Vec(tetMax,phiMin); |
| 67 |
vertices.add(vv); |
| 68 |
vv = pt.Ang2Vec(tetMax,phiMax); |
| 69 |
vertices.add(vv); |
| 70 |
|
| 71 |
} |
| 72 |
/** |
| 73 |
* return true if the point ra,dec is inside region |
| 74 |
* @param ra in degrees |
| 75 |
* @param dec in degrees |
| 76 |
* @return boolean true if inside the region |
| 77 |
*/ |
| 78 |
public boolean inReg(double ra, double dec) { |
| 79 |
boolean res = false; |
| 80 |
double racomp = ra; |
| 81 |
double phiComp = Math.toRadians(ra); |
| 82 |
if (phiComp < 0.) phiComp += TWOPI; |
| 83 |
phiComp = bm.MODULO(phiComp, TWOPI) - epsilon; |
| 84 |
|
| 85 |
racomp = Math.toDegrees(phiComp); |
| 86 |
|
| 87 |
|
| 88 |
if ((phiComp >= phiMin - epsilon) && (phiComp <= phiMax + epsilon) && (dec>= decMin - epsilon) && (dec <= decMax + epsilon)) res = true; |
| 89 |
return res; |
| 90 |
} |
| 91 |
/** |
| 92 |
* return true if point phi, theta is inside region |
| 93 |
* @param phi in radians |
| 94 |
* @param theta in radians |
| 95 |
* @return boolean true if in region |
| 96 |
*/ |
| 97 |
public boolean inRegPol(double phi, double theta) { |
| 98 |
boolean res = false; |
| 99 |
double phicomp = phi; |
| 100 |
if (phicomp < 0.) phicomp += TWOPI; |
| 101 |
phicomp = bm.MODULO(this.phiMin, TWOPI) - epsilon; |
| 102 |
if ((phicomp >= phiMin - epsilon) && (phicomp <= phiMax + epsilon) && (theta>= tetMin - epsilon) && (theta <= tetMax + epsilon)) res = true; |
| 103 |
return res; |
| 104 |
} |
| 105 |
/** |
| 106 |
* @return ArrayList of 3d vectors of vertices of the region |
| 107 |
*/ |
| 108 |
public ArrayList getVertices() { |
| 109 |
return vertices; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* divides region on HealPix pixels of given size including |
| 114 |
* pixels that only partly inside the region |
| 115 |
* @param precision - angular size of the division element in arcsec |
| 116 |
* @return ArrayList of pixel numbers in ring schema for specifyed resolution |
| 117 |
* @throws Exception |
| 118 |
*/ |
| 119 |
public ArrayList pixelize(double precision) throws Exception { |
| 120 |
ArrayList res = new ArrayList(); |
| 121 |
PixTools pt = new PixTools(); |
| 122 |
long nside = pt.GetNSide(precision); |
| 123 |
long rnmin = pt.RingNum(nside,Math.cos(tetMin)); |
| 124 |
long rnmax = pt.RingNum(nside,Math.cos(tetMax)); |
| 125 |
for (long ir = rnmin; ir < rnmax; ir++) { |
| 126 |
ArrayList strip = new ArrayList(); |
| 127 |
double phi = (phiMin + phiMax)/2.; |
| 128 |
double dphi = (phiMax - phiMin)/2.; |
| 129 |
strip = pt.InRing(nside, ir, phi, dphi, false); |
| 130 |
res.addAll(strip); |
| 131 |
} |
| 132 |
|
| 133 |
return res; |
| 134 |
} |
| 135 |
/** |
| 136 |
* provides polar coordinates of the region vertices |
| 137 |
* @return array of corner coordinates in form of polar thete,phi angles. |
| 138 |
*/ |
| 139 |
public double[][] getPolReg() { |
| 140 |
double[][] res = new double[4][2]; |
| 141 |
res[0][1] = phiMin; |
| 142 |
res[0][0] = tetMin; |
| 143 |
res[1][1] = phiMax; |
| 144 |
res[1][0] = tetMin; |
| 145 |
res[2][1] = phiMin; |
| 146 |
res[2][0] = tetMax; |
| 147 |
res[3][1] = phiMax; |
| 148 |
res[3][0] = tetMax; |
| 149 |
return res; |
| 150 |
} |
| 151 |
} |