sgp_math.c 6.04 KB
Newer Older
Jeroen Vreeken's avatar
Jeroen Vreeken committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
 * Unit SGP_Math
 *       Author:  Dr TS Kelso
 * Original Version:  1991 Oct 30
 * Current Revision:  1998 Mar 17
 *          Version:  3.00
 *        Copyright:  1991-1998, All Rights Reserved
 *
 *   ported to C by:  Neoklis Kyriazis  April 9 2001
 */

#include "sgp4sdp4.h"

/* Returns sign of a double */
int
Sign(double arg)
{
  if( arg > 0 )
    return( 1 );
  else if( arg < 0 )
    return( -1 );
  else
    return( 0 );
} /* Function Sign*/

/*------------------------------------------------------------------*/

/* Returns square of a double */
double
Sqr(double arg)
{
  return( arg*arg );
} /* Function Sqr */

/*------------------------------------------------------------------*/

/* Returns cube of a double */
double
Cube(double arg)
{
  return( arg*arg*arg );
} /*Function Cube*/

/*------------------------------------------------------------------*/

/* Returns angle in radians from arg id degrees */
double
Radians(double arg)
{
  return( arg*de2ra );
} /*Function Radians*/

/*------------------------------------------------------------------*/

/* Returns angle in degrees from arg in rads */
double
Degrees(double arg)
{
  return( arg/de2ra );
} /*Function Degrees*/

/*------------------------------------------------------------------*/

/* Returns the arcsine of the argument */
double
ArcSin(double arg)
{
  if( fabs(arg) >= 1 )
    return( Sign(arg)*pio2 );
  else
    return( atan(arg/sqrt(1-arg*arg)) );
} /*Function ArcSin*/

/*------------------------------------------------------------------*/

/* Returns orccosine of rgument */
double
ArcCos(double arg)
{
  return( pio2 - ArcSin(arg) );
} /*Function ArcCos*/

/*------------------------------------------------------------------*/

/* Calculates scalar magnitude of a vector_t argument */
void
Magnitude(vector_t *v)
{
  v->w = sqrt(Sqr(v->x) + Sqr(v->y) + Sqr(v->z));
} /*Procedure Magnitude*/

/*------------------------------------------------------------------*/

/* Adds vectors v1 and v2 together to produce v3 */
void
Vec_Add(vector_t *v1, vector_t *v2, vector_t *v3)
{
  v3->x = v1->x + v2->x;
  v3->y = v1->y + v2->y;
  v3->z = v1->z + v2->z;

  Magnitude(v3);
} /*Procedure Vec_Add*/

/*------------------------------------------------------------------*/

/* Subtracts vector v2 from v1 to produce v3 */
void
Vec_Sub(vector_t *v1, vector_t *v2, vector_t *v3)
{
  v3->x = v1->x - v2->x;
  v3->y = v1->y - v2->y;
  v3->z = v1->z - v2->z;

  Magnitude(v3);
} /*Procedure Vec_Sub*/

/*------------------------------------------------------------------*/

/* Multiplies the vector v1 by the scalar k to produce the vector v2 */
void
Scalar_Multiply(double k, vector_t *v1, vector_t *v2)
{
  v2->x = k * v1->x;
  v2->y = k * v1->y;
  v2->z = k * v1->z;
  v2->w = fabs(k) * v1->w;
} /*Procedure Scalar_Multiply*/

/*------------------------------------------------------------------*/

/* Multiplies the vector v1 by the scalar k */
void
Scale_Vector(double k, vector_t *v)
{ 
  v->x *= k;
  v->y *= k;
  v->z *= k;
  Magnitude(v);
} /* Procedure Scale_Vector */

/*------------------------------------------------------------------*/

/* Returns the dot product of two vectors */
double
Dot(vector_t *v1, vector_t *v2)
{
  return( v1->x*v2->x + v1->y*v2->y + v1->z*v2->z );
}  /*Function Dot*/

/*------------------------------------------------------------------*/

/* Calculates the angle between vectors v1 and v2 */
double
Angle(vector_t *v1, vector_t *v2)
{
  Magnitude(v1);
  Magnitude(v2);
  return( ArcCos(Dot(v1,v2)/(v1->w*v2->w)) );
} /*Function Angle*/

/*------------------------------------------------------------------*/

/* Produces cross product of v1 and v2, and returns in v3 */
void
Cross(vector_t *v1, vector_t *v2 ,vector_t *v3)
{
  v3->x = v1->y*v2->z - v1->z*v2->y;
  v3->y = v1->z*v2->x - v1->x*v2->z;
  v3->z = v1->x*v2->y - v1->y*v2->x;
  Magnitude(v3);
} /*Procedure Cross*/

/*------------------------------------------------------------------*/

/* Normalizes a vector */
void
Normalize( vector_t *v )
{
  v->x /= v->w;
  v->y /= v->w;
  v->z /= v->w;
} /*Procedure Normalize*/

/*------------------------------------------------------------------*/

/* Four-quadrant arctan function */
double
AcTan(double sinx, double cosx)
{
  if(cosx == 0)
    {
      if(sinx > 0)
	return (pio2);
      else
	return (x3pio2);
    }
  else
    {
      if(cosx > 0)
	{
	  if(sinx > 0)
	    return ( atan(sinx/cosx) );
	  else
	    return ( twopi + atan(sinx/cosx) );
	}
      else
	return ( pi + atan(sinx/cosx) );
    }

} /* Function AcTan */

/*------------------------------------------------------------------*/

/* Returns mod 2pi of argument */
double
FMod2p(double x)
{
  int i;
  double ret_val;

  ret_val = x;
  i = ret_val/twopi;
  ret_val -= i*twopi;
  if (ret_val < 0) ret_val += twopi;

  return (ret_val);
} /* fmod2p */

/*------------------------------------------------------------------*/

/* Returns arg1 mod arg2 */
double
Modulus(double arg1, double arg2)
{
  int i;
  double ret_val;

  ret_val = arg1;
  i = ret_val/arg2;
  ret_val -= i*arg2;
  if (ret_val < 0) ret_val += arg2;

  return (ret_val);
} /* modulus */

/*------------------------------------------------------------------*/

/* Returns fractional part of double argument */
double
Frac( double arg )
{
  return( arg - floor(arg) );
} /* Frac */

/*------------------------------------------------------------------*/

/* Returns argument rounded up to nearest integer */
int
Round( double arg )
{
  return( (int) floor(arg + 0.5) );
} /* Round */

/*------------------------------------------------------------------*/

/* Returns the floor integer of a double arguement, as double */
double
Int( double arg )
{
  return( floor(arg) );
} /* Int */

/*------------------------------------------------------------------*/

/* Converts the satellite's position and velocity  */
/* vectors from normalised values to km and km/sec */ 
void
Convert_Sat_State( vector_t *pos, vector_t *vel )
{
      Scale_Vector( xkmper, pos );
      Scale_Vector( xkmper*xmnpda/secday, vel );

} /* Procedure Convert_Sat_State */

/*------------------------------------------------------------------*/