H3C HEALPix library for PostgreSQL  (version 1.2)
h3c_math.c
Go to the documentation of this file.
1 /*
2  Copyright (C) 2012 Gilles Landais (CDS)
3 
4  Author: Gilles Landais, Strasbourg astronomical Data Center (CDS)
5  Email: gilles.landais@unistra.fr
6 
7  This file is part of H3C.
8 
9  H3C is free software; you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation; either version 2 of the License, or
12  (at your option) any later version.
13 
14  H3C is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with H3C; if not, write to the Free Software
21  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23 
29 #include "common.h"
30 #include "h3c_math.h"
31 
32 /*****************************************************************************/
38 /*****************************************************************************/
39 void h3c_2xyz(double lon, double lat, _h3c_vector vect)
40 {
41  double coslat = cos(lat*H3C_DEGRA);
42  vect[0] = coslat * cos(lon*H3C_DEGRA);
43  vect[1] = coslat * sin(lon*H3C_DEGRA);
44  vect[2] = sin(lat*H3C_DEGRA);
45 }
46 
47 /*****************************************************************************/
52 /*****************************************************************************/
53 void h3c_2radec(double *in, h3c_coord_t *out)
54 {
55  double r2 = in[0]*in[0] + in[1]*in[1];
56  out[0] = 0.0;
57  if (r2 == 0.0) { /* in case of poles */
58  if (in[2] == 0.0) {
59  out[0] = 0./0.; out[1] = 0./0.;
60  }
61  else
62  out[1] = (in[2] > 0.0) ? 90.0 : -90.0;
63  }
64  else {
65  out[0] = atan2(in[1], in[0])*H3C_RADEG;
66  out[1] = atan2(in[2], sqrt(r2))*H3C_RADEG;
67  if (out[0] < 0.0)
68  out[0] += 360.0;
69  }
70 }
71 
72 /*****************************************************************************/
78 /*****************************************************************************/
79 void h3c_vect_prod(_h3c_vector v1, _h3c_vector v2, _h3c_vector r) {
80  r[0] = v1[1]*v2[2] - v1[2]*v2[1];
81  r[1] = v1[2]*v2[0] - v1[0]*v2[2];
82  r[2] = v1[0]*v2[1] - v1[1]*v2[0];
83 }
84 
85 /*****************************************************************************/
91 /*****************************************************************************/
92 double h3c_scalar_product(_h3c_vector v1, _h3c_vector v2) {
93  return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
94 }
95 
96 /*****************************************************************************/
100 /*****************************************************************************/
101 void h3c_normalize(_h3c_vector u) {
102  double n = sqrt(u[0]*u[0] + u[1]*u[1] + u[2]*u[2]);
103  if (n > 0) {
104  u[0] = u[0]/n; u[1] = u[1]/n; u[2] = u[2]/n;
105  }
106 }
107 
108 /*****************************************************************************/
113 /*****************************************************************************/
114 double h3c_vect_length(_h3c_vector v) {
115  return sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);
116 }
117 
118 /*****************************************************************************/
124 /*****************************************************************************/
125 void h3c_unit_vector(double z, double phi, _h3c_vector res) {
126  double sintheta = sqrt((1.-z)*(1.+z));
127  res[0] = sintheta*cos(phi);
128  res[1] = sintheta*sin(phi);
129  res[2] = z;
130 }
131 
h3c_vect_length
double h3c_vect_length(_h3c_vector v)
get the length of a vector
Definition: h3c_math.c:114
h3c_2xyz
void h3c_2xyz(double lon, double lat, _h3c_vector vect)
compute coord in cartesian
Definition: h3c_math.c:39
h3c_2radec
void h3c_2radec(double *in, h3c_coord_t *out)
cartesian to polar
Definition: h3c_math.c:53
h3c_normalize
void h3c_normalize(_h3c_vector u)
normalize in the united vector
Definition: h3c_math.c:101
common.h
h3c_unit_vector
void h3c_unit_vector(double z, double phi, _h3c_vector res)
create a unit vector from a z coordinate and an azimuthal angle.
Definition: h3c_math.c:125
h3c_scalar_product
double h3c_scalar_product(_h3c_vector v1, _h3c_vector v2)
scalar product
Definition: h3c_math.c:92
h3c_vect_prod
void h3c_vect_prod(_h3c_vector v1, _h3c_vector v2, _h3c_vector r)
vectorial product
Definition: h3c_math.c:79
h3c_math.h
mathematique functions