LSSTApplications  1.1.2+25,10.0+13,10.0+132,10.0+133,10.0+224,10.0+41,10.0+8,10.0-1-g0f53050+14,10.0-1-g4b7b172+19,10.0-1-g61a5bae+98,10.0-1-g7408a83+3,10.0-1-gc1e0f5a+19,10.0-1-gdb4482e+14,10.0-11-g3947115+2,10.0-12-g8719d8b+2,10.0-15-ga3f480f+1,10.0-2-g4f67435,10.0-2-gcb4bc6c+26,10.0-28-gf7f57a9+1,10.0-3-g1bbe32c+14,10.0-3-g5b46d21,10.0-4-g027f45f+5,10.0-4-g86f66b5+2,10.0-4-gc4fccf3+24,10.0-40-g4349866+2,10.0-5-g766159b,10.0-5-gca2295e+25,10.0-6-g462a451+1
LSSTDataManagementBasePackage
Point.cc
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 
3 /*
4  * LSST Data Management System
5  * Copyright 2008, 2009, 2010 LSST Corporation.
6  *
7  * This product includes software developed by the
8  * LSST Project (http://www.lsst.org/).
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the LSST License Statement and
21  * the GNU General Public License along with this program. If not,
22  * see <http://www.lsstcorp.org/LegalNotices/>.
23  */
24 
25 
33 #include <cassert>
34 #include <cmath>
35 
36 #include <iostream>
37 
38 #include "lsst/ap/Common.h"
39 #include "lsst/ap/Point.h"
40 #include "lsst/ap/SpatialUtil.h"
41 #include "lsst/ap/Time.h"
42 #include "lsst/afw/geom/Angle.h"
43 
45 namespace afwGeom = lsst::afw::geom;
46 
47 namespace lsst { namespace ap { namespace {
48 
49 double randomDec(Random & rng, double const decMin, double const decMax) {
50 
51  assert(decMin < decMax && decMin < 90.0 && decMax > -90.0);
52 
53  double min = (decMin < -90.0) ? -90.0 : decMin;
54  double max = (decMax > 90.0) ? 90.0 : decMax;
55  double z = rng.flat(std::sin(afwGeom::degToRad(min)), std::sin(afwGeom::degToRad(max)));
56  double res = afwGeom::radToDeg(std::asin(z));
57  if (res < decMin) {
58  return decMin;
59  } else if (res > decMax) {
60  return decMax;
61  }
62  return res;
63 }
64 
65 }}} // end of namespace lsst::ap::<anonymous>
66 
67 
74  return perturb(rng, sigma, rng.uniform()*360.0);
75 }
76 
77 
82 lsst::ap::Point & lsst::ap::Point::perturb(Random & rng, double const sigma, double const pa) {
83 
84  double sra = std::sin(afwGeom::degToRad(_ra));
85  double cra = std::cos(afwGeom::degToRad(_ra));
86  double sdec = std::sin(afwGeom::degToRad(_dec));
87  double cdec = std::cos(afwGeom::degToRad(_dec));
88 
89  // original position p
90  double x = cra*cdec;
91  double y = sra*cdec;
92  double z = sdec;
93 
94  double spa = std::sin(afwGeom::degToRad(pa));
95  double cpa = std::cos(afwGeom::degToRad(pa));
96 
97  // north vector tangential to p
98  double nx = - cra*sdec;
99  double ny = - sra*sdec;
100  double nz = cdec;
101 
102  // east vector tangential to p
103  double ex = - sra;
104  double ey = cra;
105  double ez = 0.0;
106 
107  // rotate north vector at V by minus position angle
108  double tx = spa*ex + cpa*nx;
109  double ty = spa*ey + cpa*ny;
110  double tz = spa*ez + cpa*nz;
111 
112  // perturb in this direction by a random angle that is normally
113  // distributed with a standard deviation of sigma degrees
114  double mag = afwGeom::degToRad(rng.gaussian()*sigma);
115  double smag = std::sin(mag);
116  double cmag = std::cos(mag);
117 
118  // obtain the perturbed position
119  x = x*cmag + tx*smag;
120  y = y*cmag + ty*smag;
121  z = z*cmag + tz*smag;
122  // finally, convert back to spherical coordinates (in degrees)
123  _ra = afwGeom::radToDeg(std::atan2(y, x));
124  if (_ra < 0.0) {
125  _ra += 360.0;
126  }
127  _dec = afwGeom::radToDeg(std::asin(z));
128  if (_dec <= -90.0) {
129  _dec = -90.0;
130  } else if (_dec >= 90.0) {
131  _dec = 90.0;
132  }
133  return *this;
134 }
135 
136 
138 double lsst::ap::Point::distance(Point const & p) const {
139 
140  double sra = std::sin(afwGeom::degToRad(_ra));
141  double cra = std::cos(afwGeom::degToRad(_ra));
142  double sdec = std::sin(afwGeom::degToRad(_dec));
143  double cdec = std::cos(afwGeom::degToRad(_dec));
144 
145  double x = cra*cdec;
146  double y = sra*cdec;
147  double z = sdec;
148 
149  sra = std::sin(afwGeom::degToRad(p._ra));
150  cra = std::cos(afwGeom::degToRad(p._ra));
151  sdec = std::sin(afwGeom::degToRad(p._dec));
152  cdec = std::cos(afwGeom::degToRad(p._dec));
153 
154  x *= cra*cdec;
155  y *= sra*cdec;
156  z *= sdec;
157 
158  return afwGeom::radToDeg(std::acos(x + y + z));
159 }
160 
161 
164  double z = rng.flat(-1.0, 1.0);
165  return Point(rng.flat(0.0, 360.0), afwGeom::radToDeg(std::asin(z)));
166 }
167 
168 
171  Random & rng,
172  double const decMin,
173  double const decMax
174 ) {
175  return Point(rng.flat(0.0, 360.0), randomDec(rng, decMin, decMax));
176 }
177 
178 
181  Random & rng,
182  double const raMin,
183  double const raMax,
184  double const decMin,
185  double const decMax
186 ) {
187  assert(raMin >= 0.0 && raMin <= 360.0);
188  assert(raMax >= 0.0 && raMax <= 360.0);
189 
190  double ra;
191  if (raMin < raMax) {
192  ra = rng.flat(raMin, raMin);
193  if (ra > raMax) {
194  ra = raMax;
195  }
196  } else {
197  // wrap-around
198  double m = raMin - 360.0;
199  ra = rng.flat(m, raMax);
200  if (ra < 0) {
201  ra += 360.0;
202  }
203  }
204  return Point(ra, randomDec(rng, decMin, decMax));
205 }
206 
int y
Class for representing points on the sky, with support for random perturbations.
double flat(double const a, double const b)
Definition: Random.cc:358
Point & perturb(lsst::afw::math::Random &rng, double const sigma)
Definition: Point.cc:73
double min
Definition: attributes.cc:216
afw::table::Key< double > sigma
Definition: GaussianPsf.cc:42
double radToDeg(long double angleInRadians)
Definition: RaDecStr.cc:61
double distance(Point const &p) const
Definition: Point.cc:138
double max
Definition: attributes.cc:218
A point on the unit sphere (sky), specified in spherical polar coordinates.
Definition: Point.h:46
static Point const random(lsst::afw::math::Random &rng)
Definition: Point.cc:163
Convenience wrapper for the C library timespec struct and a simple profiling class.
Master header file for the association pipeline.
int x
double _dec
Definition: Point.h:49
double degToRad(long double angleInDegrees)
Definition: RaDecStr.cc:67
double _ra
Definition: Point.h:48
dictionary Point
Definition: __init__.py:38
Class and helper functions related to spatial partitioning.