|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Predict_Math |
|
4
|
|
|
* |
|
5
|
|
|
* Ported to PHP by Bill Shupp. Original comments below |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
//require_once 'Predict.php'; |
|
9
|
|
|
|
|
10
|
|
|
/* |
|
11
|
|
|
* Unit SGP_Math |
|
12
|
|
|
* Author: Dr TS Kelso |
|
13
|
|
|
* Original Version: 1991 Oct 30 |
|
14
|
|
|
* Current Revision: 1998 Mar 17 |
|
15
|
|
|
* Version: 3.00 |
|
16
|
|
|
* Copyright: 1991-1998, All Rights Reserved |
|
17
|
|
|
* |
|
18
|
|
|
* ported to C by: Neoklis Kyriazis April 9 2001 |
|
19
|
|
|
*/ |
|
20
|
|
|
class Predict_Math |
|
21
|
|
|
{ |
|
22
|
|
|
/* Returns sign of a float */ |
|
23
|
|
|
public static function Sign($arg) |
|
24
|
|
|
{ |
|
25
|
|
|
if ($arg > 0 ) { |
|
26
|
|
|
return 1; |
|
27
|
|
|
} else if ($arg < 0 ) { |
|
28
|
|
|
return -1; |
|
29
|
|
|
} else { |
|
30
|
|
|
return 0; |
|
31
|
|
|
} |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/* Returns the arcsine of the argument */ |
|
35
|
|
|
public static function ArcSin($arg) |
|
36
|
|
|
{ |
|
37
|
|
|
if (abs($arg) >= 1 ) { |
|
38
|
|
|
return (self::Sign($arg) * Predict::pio2); |
|
39
|
|
|
} else { |
|
40
|
|
|
return(atan($arg / sqrt(1 - $arg * $arg))); |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/* Returns arccosine of rgument */ |
|
45
|
|
|
public static function ArcCos($arg) |
|
46
|
|
|
{ |
|
47
|
|
|
return Predict::pio2 - self::ArcSin($arg); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/* Adds vectors v1 and v2 together to produce v3 */ |
|
51
|
|
|
public static function Vec_Add(Predict_Vector $v1, Predict_Vector $v2, Predict_Vector $v3) |
|
52
|
|
|
{ |
|
53
|
|
|
$v3->x = $v1->x + $v2->x; |
|
54
|
|
|
$v3->y = $v1->y + $v2->y; |
|
55
|
|
|
$v3->z = $v1->z + $v2->z; |
|
56
|
|
|
|
|
57
|
|
|
$v3->w = sqrt($v3->x * $v3->x + $v3->y * $v3->y + $v3->z * $v3->z); |
|
|
|
|
|
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/* Subtracts vector v2 from v1 to produce v3 */ |
|
61
|
|
|
public static function Vec_Sub(Predict_Vector $v1, Predict_Vector $v2, Predict_Vector $v3) |
|
62
|
|
|
{ |
|
63
|
|
|
$v3->x = $v1->x - $v2->x; |
|
64
|
|
|
$v3->y = $v1->y - $v2->y; |
|
65
|
|
|
$v3->z = $v1->z - $v2->z; |
|
66
|
|
|
|
|
67
|
|
|
$v3->w = sqrt($v3->x * $v3->x + $v3->y * $v3->y + $v3->z * $v3->z); |
|
|
|
|
|
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/* Multiplies the vector v1 by the scalar k to produce the vector v2 */ |
|
71
|
|
|
public static function Scalar_Multiply($k, Predict_Vector $v1, Predict_Vector $v2) |
|
72
|
|
|
{ |
|
73
|
|
|
$v2->x = $k * $v1->x; |
|
|
|
|
|
|
74
|
|
|
$v2->y = $k * $v1->y; |
|
|
|
|
|
|
75
|
|
|
$v2->z = $k * $v1->z; |
|
|
|
|
|
|
76
|
|
|
$v2->w = abs($k) * $v1->w; |
|
|
|
|
|
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/* Multiplies the vector v1 by the scalar k */ |
|
80
|
|
|
public static function Scale_Vector($k, Predict_Vector $v) |
|
81
|
|
|
{ |
|
82
|
|
|
$v->x *= $k; |
|
83
|
|
|
$v->y *= $k; |
|
84
|
|
|
$v->z *= $k; |
|
85
|
|
|
|
|
86
|
|
|
$v->w = sqrt($v->x * $v->x + $v->y * $v->y + $v->z * $v->z); |
|
|
|
|
|
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/* Returns the dot product of two vectors */ |
|
90
|
|
|
public static function Dot(Predict_Vector $v1, Predict_Vector $v2) |
|
91
|
|
|
{ |
|
92
|
|
|
return ($v1->x * $v2->x + $v1->y * $v2->y + $v1->z * $v2->z); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/* Calculates the angle between vectors v1 and v2 */ |
|
96
|
|
|
public static function Angle(Predict_Vector $v1, Predict_Vector $v2) |
|
97
|
|
|
{ |
|
98
|
|
|
$v1->w = sqrt($v1->x * $v1->x + $v1->y * $v1->y + $v1->z * $v1->z); |
|
|
|
|
|
|
99
|
|
|
$v2->w = sqrt($v2->x * $v2->x + $v2->y * $v2->y + $v2->z * $v2->z); |
|
|
|
|
|
|
100
|
|
|
return (self::ArcCos(self::Dot($v1, $v2) / ($v1->w * $v2->w))); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/* Produces cross product of v1 and v2, and returns in v3 */ |
|
104
|
|
|
public static function Cross(Predict_Vector $v1, Predict_Vector $v2 ,Predict_Vector $v3) |
|
105
|
|
|
{ |
|
106
|
|
|
$v3->x = $v1->y * $v2->z - $v1->z * $v2->y; |
|
107
|
|
|
$v3->y = $v1->z * $v2->x - $v1->x * $v2->z; |
|
108
|
|
|
$v3->z = $v1->x * $v2->y - $v1->y * $v2->x; |
|
109
|
|
|
|
|
110
|
|
|
$v3->w = sqrt($v3->x * $v3->x + $v3->y * $v3->y + $v3->z * $v3->z); |
|
|
|
|
|
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/* Normalizes a vector */ |
|
114
|
|
|
public static function Normalize(Predict_Vector $v ) |
|
115
|
|
|
{ |
|
116
|
|
|
$v->x /= $v->w; |
|
117
|
|
|
$v->y /= $v->w; |
|
118
|
|
|
$v->z /= $v->w; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/* Four-quadrant arctan function */ |
|
122
|
|
|
public static function AcTan($sinx, $cosx) |
|
123
|
|
|
{ |
|
124
|
|
|
if ($cosx == 0) { |
|
125
|
|
|
if ($sinx > 0) { |
|
126
|
|
|
return Predict::pio2; |
|
127
|
|
|
} else { |
|
128
|
|
|
return Predict::x3pio2; |
|
129
|
|
|
} |
|
130
|
|
|
} else { |
|
131
|
|
|
if ($cosx > 0) { |
|
132
|
|
|
if ($sinx > 0) { |
|
133
|
|
|
return atan($sinx / $cosx); |
|
134
|
|
|
} else { |
|
135
|
|
|
return Predict::twopi + atan($sinx / $cosx); |
|
136
|
|
|
} |
|
137
|
|
|
} else { |
|
138
|
|
|
return Predict::pi + atan($sinx / $cosx); |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/* Returns mod 2pi of argument */ |
|
144
|
|
|
public static function FMod2p($x) |
|
145
|
|
|
{ |
|
146
|
|
|
$ret_val = $x; |
|
147
|
|
|
$i = (int) ($ret_val / Predict::twopi); |
|
148
|
|
|
$ret_val -= $i * Predict::twopi; |
|
149
|
|
|
|
|
150
|
|
|
if ($ret_val < 0) { |
|
151
|
|
|
$ret_val += Predict::twopi; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
return $ret_val; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/* Returns arg1 mod arg2 */ |
|
158
|
|
|
public static function Modulus($arg1, $arg2) |
|
159
|
|
|
{ |
|
160
|
|
|
$ret_val = $arg1; |
|
161
|
|
|
$i = (int) ($ret_val / $arg2); |
|
162
|
|
|
$ret_val -= $i * $arg2; |
|
163
|
|
|
|
|
164
|
|
|
if ($ret_val < 0) { |
|
165
|
|
|
$ret_val += $arg2; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
return $ret_val; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/* Returns fractional part of double argument */ |
|
172
|
|
|
public static function Frac($arg) |
|
173
|
|
|
{ |
|
174
|
|
|
return $arg - floor($arg); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/* Converts the satellite's position and velocity */ |
|
178
|
|
|
/* vectors from normalised values to km and km/sec */ |
|
179
|
|
|
public static function Convert_Sat_State(Predict_Vector $pos, Predict_Vector $vel) |
|
180
|
|
|
{ |
|
181
|
|
|
self::Scale_Vector(Predict::xkmper, $pos); |
|
182
|
|
|
self::Scale_Vector(Predict::xkmper * Predict::xmnpda / Predict::secday, $vel); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/* Returns angle in radians from arg in degrees */ |
|
186
|
|
|
public static function Radians($arg) |
|
187
|
|
|
{ |
|
188
|
|
|
return $arg * Predict::de2ra; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/* Returns angle in degrees from arg in rads */ |
|
192
|
|
|
public static function Degrees($arg) |
|
193
|
|
|
{ |
|
194
|
|
|
return $arg / Predict::de2ra; |
|
195
|
|
|
} |
|
196
|
|
|
} |
|
197
|
|
|
|
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.