-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_convert_to_cartesian.metal
185 lines (152 loc) · 4.81 KB
/
parse_convert_to_cartesian.metal
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
//
// convert_to_radians.metal
// GPU-Metal-DistanceCalc
//
// Created by Tyler Hilbert on 7/24/24.
//
#include <metal_stdlib>
#include "EquatorialPoint.hpp"
#include "XYZPoint.hpp"
#define M_PI 3.14159265358979323846
using namespace metal;
int atoi(const char str[100]);
float atof(const char str[50]);
void convertRadToCart(const thread EquatorialPointRadians& pr, device XYZPoint& pointXYZ);
// Packed conversion of equatorial radians to cartesian
void convertRadToCart(const thread EquatorialPointRadians& pr, device XYZPoint& pointXYZ){
float cosDEC = cos(pr.DEC);
float3 sphericalCoords = float3(
cosDEC * cos(pr.RA),
cosDEC * sin(pr.RA),
sin(pr.DEC)
);
int3 cartesianCoords = int3(sphericalCoords * pr.lightYears);
pointXYZ = XYZPoint{cartesianCoords.x, cartesianCoords.y, cartesianCoords.z};
}
// Parses equatorial points from char array, converts to radians, and then converts to cartesian
kernel void parse_convert_to_cartesian(
constant EquatorialPoint* points [[ buffer(0) ]],
device XYZPoint* pointsXYZ [[ buffer(1) ]],
uint id [[ thread_position_in_grid ]]
) {
//// Parsing
EquatorialPoint p = points[id];
EquatorialPointRadians pr;
// Right Ascension (RA)
int tempIndex = 0;
char temp[50];
int mode = 0; // 0 for hours, 1 for minutes, 2 for seconds
int h = 0, m = 0;
float s = 0.0;
for (int i = 0 ;; i++) {
char c = p.RA[i];
if ((c >= '0' && c <= '9') || (c == '.' && mode != 2)) {
temp[tempIndex++] = c;
} else {
temp[tempIndex] = '\0';
tempIndex = 0;
if (mode == 0) {
h = atoi(temp);
mode = 1;
} else if (mode == 1) {
m = atoi(temp);
mode = 2;
} else if (mode == 2) {
s = atof(temp);
break;
}
}
}
pr.RA = (h + m/60.0 + s/3600.0) * (M_PI / 12.0) ;
// Declination (DEC)
int d = 0;
m = 0;
s = 0.0;
char temp2[50];
tempIndex = 0;
mode = 0; // 0 for degrees, 1 for minutes, 2 for seconds
int sign = 1;
for (int i = 0 ;; i++) {
char c = p.DEC[i];
if (c == '-') {
sign = -1;
}
if ((c >= '0' && c <= '9') || (c == '.' && mode != 2)) {
temp2[tempIndex++] = c;
} else {
if (tempIndex > 0) {
temp2[tempIndex] = '\0';
tempIndex = 0;
if (mode == 0) {
d = atoi(temp2) * sign;
mode = 1;
} else if (mode == 1) {
m = atoi(temp2);
mode = 2;
} else if (mode == 2) {
s = atof(temp2);
break;
}
}
}
}
pr.DEC = (d + m/60.0 + s / 3600.0) * (M_PI / 180.0);
// Light Years
char temp3[50];
tempIndex = 0;
for (int i = 0 ;; i++) {
char c = p.lightYears[i];
if (c >= '0' && c <= '9') {
temp3[tempIndex++] = c;
} else {
break;
}
}
pr.lightYears = atoi(temp3);
for (int i = 0; i < 50; i++) {
pr.name[i] = p.name[i];
}
//// Conversion
convertRadToCart(pr, pointsXYZ[id]);
}
// Definition of convert string to int as Metal doesn't include it
int atoi(const char str[100]) {
int res = 0; // Initialize result
int sign = 1; // Initialize sign as positive
int i = 0; // Initialize index of first digit
// If number is negative, then update sign
if (str[0] == '-') {
sign = -1;
i++; // Also update index of first digit
}
// Iterate through all digits and update the result
for (; str[i] != '\0'; ++i)
res = res * 10 + str[i] - '0';
// Return result with sign
return sign * res;
}
// Definition of convert string to float as Metal doesn't include it
float atof(const char str[50]) {
float res = 0.0; // Initialize result
float factor = 1.0;
int sign = 1; // Initialize sign as positive
int i = 0; // Initialize index of first digit
// If number is negative, then update sign
if (str[0] == '-') {
sign = -1;
i++; // Also update index of first digit
}
// Iterate through all digits before the decimal point and update the result
for (; str[i] != '\0' && str[i] != '.'; ++i)
res = res * 10.0 + str[i] - '0';
// If there is a decimal point, iterate through all digits after the decimal point
if (str[i] == '.') {
i++;
for (; str[i] != '\0'; ++i) {
factor *= 0.1;
res = res + (str[i] - '0') * factor;
}
}
// Return result with sign
return sign * res;
}