-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeometries.py
236 lines (179 loc) · 6.43 KB
/
geometries.py
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
import numpy as np
import scipy.linalg
import math
__all__ = ['elliptical', 'ellipsoidal','linear','linear2','curvilinear']
def _rot_matrix(axis, theta):
return scipy.linalg.expm(np.cross(np.eye(3), axis / np.linalg.norm(axis) * theta))
def elliptical(num, radius, centre):
"""
Generate a 2D elliptical geometry for a number of points ``num``, centred
on ``centre`` and with radius ``radius``.
Parameters
----------
num : int
Number of points on the geometry.
radius : array-like
List or array with each of the two radii of the ellipsis.
centre : array-like
List or array with the coordinates of the centre of the ellipsis.
Returns
-------
2d-array
Array containing the coordinates of points in the geometry, with shape (num, 2).
"""
angles = np.linspace(0, 2*np.pi, num, endpoint=False)
geometry = np.zeros((num, 2))
for index, angle in zip(range(num), angles):
geometry[index, 0] = radius[0] * np.cos(angle) + centre[0]
geometry[index, 1] = radius[1] * np.sin(angle) + centre[1]
return geometry
def linear(num,radius, centre):
geometry = np.zeros((num, 2))
i = 0.0
for index in zip(range(num)):
i = i+1
geometry[index, 0] = radius[0]*i + centre[0]
geometry[index, 1] = centre[1]
return geometry
def linear2(num,radius, centre):
geometry = np.zeros((num, 2))
i = 0.0
j = 0.0
for index in zip(range(num)):
i = i+1
#geometry[index, 0] = radius[0]*i + centre[0]
if i<=num//2:
geometry[index, 0] = radius[0]*i + centre[0]
geometry[index, 1] = centre[1]
else:
j = j+1
geometry[index, 0] = radius[0]*j + centre[0]
geometry[index, 1] = 0.1800
return geometry
def curvilinear(num,start,chord,pixel_size):
geometry = np.zeros((num,2))
#k = 1.0
#j = 1.0
#for index in zip(range(num)):
#geometry[index,1] = start[1] + (j*0.0003)#*(pixel_size)
#geometry[index,0] = start[0] + k*(chord/num)
#k = k+1
#if k<33:
# j = j+1
#else:
# j = j-1
Range = np.arange(55,125,1.09375)#(50,130,1.25)
index = 0
for a in Range:
geometry[index,1]=0.040925*np.sin((a*math.pi)/180)-0.033#-0.042#0.0496*np.sin((a*math.pi)/180)-0.05#0.038
geometry[index,0]=0.040925*np.cos((a*math.pi)/180)+0.06#0.0496*np.cos((a*math.pi)/180)+0.06
index = index+1
return geometry
def ellipsoidal(num, radius, centre, theta=0., axis=None, threshold=0., angle_range=np.pi):
"""
Generate a 3D ellipsoidal geometry for a number of points ``num``, centred
on ``centre`` and with radius ``radius``. The geometry can be rotated by
an amount ``theta``, and thresholded by eliminating ``threshold`` percent of it.
Parameters
----------
num : int
Number of points on the geometry.
radius : array-like
List or array with each of the two radii of the ellipsis.
centre : array-like
List or array with the coordinates of the centre of the ellipsis.
theta
axis
threshold
angle_range
Returns
-------
3d-array
Array containing the coordinates of points in the geometry, with shape (num, 3).
"""
increment = np.pi * (3. - np.sqrt(5.))
start_angle = np.pi / 2 - angle_range / 2
start_offset = np.sin(start_angle)
threshold = threshold or start_offset
num = int(np.round(num / (1 - threshold)))
offset = 2. / num
axis = axis or [1, 0, 0]
geometry = np.zeros((num, 3))
index = 0
for sample in range(num):
z = ((sample * offset) - 1) + offset / 2
r = np.sqrt(1 - z ** 2)
phi = ((sample + 1) % num) * increment
y = np.cos(phi) * r
x = np.sin(phi) * r
if z + 1 < threshold*2:
continue
x *= radius[0]
y *= radius[1]
z *= radius[2]
[x, y, z] = np.dot(_rot_matrix(axis, theta), [x, y, z])
point = np.array([x, y, z])
point[0] += centre[0]
point[1] += centre[1]
point[2] += centre[2]
geometry[index, :] = point
index += 1
geometry = geometry[:index, :]
return geometry
def disk(num, radius, centre, orientation, boundary_points=2):
"""
Generate a 3D disk for a number of points ``num``, centred
on ``centre``, with orientation vector ``orientation``, and with radius ``radius``.
Parameters
----------
num : int
Number of points on the geometry.
radius : array-like
List or array with each of the two radii of the ellipsis.
centre : array-like
List or array with the coordinates of the centre of the ellipsis.
boundary_points
orientation
Returns
-------
3d-array
Array containing the coordinates of points in the geometry, with shape (num, 3).
"""
golden_angle = (np.sqrt(5) + 1) / 2
boundary_points = boundary_points or 2.
boundary_points = round(boundary_points * np.sqrt(num))
theta = 2 * np.pi * np.arange(num, dtype=np.float32) / golden_angle**2
# Calculate discretisation in polar coordinates
r = np.zeros_like(theta)
for sample in range(1, num):
r[sample] = 1 if sample > num - boundary_points \
else np.sqrt(sample - 0.5) / np.sqrt(num - (boundary_points/0.99 + 1) / 2)
# Change to Cartesian coordinates
r *= radius
x = np.zeros((num,))
y = np.cos(theta) * r
z = np.sin(theta) * r
geometry = np.stack((x, y, z))
# Calculate orthonormal basis of orientation
x_prime = np.array(orientation)
x_prime = x_prime / np.linalg.norm(x_prime)
if x_prime[0] == 0.:
y_prime = np.array([0., x_prime[2], -x_prime[1]])
elif x_prime[1] == 0.:
y_prime = np.array([x_prime[2], 0., -x_prime[0]])
elif x_prime[2] == 0.:
y_prime = np.array([x_prime[1], -x_prime[0], 0.])
else:
y_prime = np.array([x_prime[2], 0., -x_prime[0]])
z_prime = np.cross(y_prime, x_prime)
y_prime = y_prime / np.linalg.norm(y_prime)
z_prime = z_prime / np.linalg.norm(z_prime)
# Construct transformation matrix
T = np.vstack((x_prime, y_prime, z_prime))
# Change back to normal basis
geometry = np.dot(np.transpose(T), geometry).T
# Displace shot to its centre
geometry[:, 0] += centre[0]
geometry[:, 1] += centre[1]
geometry[:, 2] += centre[2]
return geometry