1 | # Python tools to manage netCDF files. |
---|
2 | # L. Fita, CIMA. Mrch 2019 |
---|
3 | # More information at: http://www.xn--llusfb-5va.cat/python/PyNCplot |
---|
4 | # |
---|
5 | # pyNCplot and its component geometry_tools.py comes with ABSOLUTELY NO WARRANTY. |
---|
6 | # This work is licendes under a Creative Commons |
---|
7 | # Attribution-ShareAlike 4.0 International License (http://creativecommons.org/licenses/by-sa/4.0) |
---|
8 | # |
---|
9 | ## Script for geometry calculations and operations as well as definition of different |
---|
10 | ### standard objects and shapes |
---|
11 | |
---|
12 | import numpy as np |
---|
13 | import matplotlib as mpl |
---|
14 | from mpl_toolkits.mplot3d import Axes3D |
---|
15 | import matplotlib.pyplot as plt |
---|
16 | import os |
---|
17 | import generic_tools as gen |
---|
18 | |
---|
19 | errormsg = 'ERROR -- error -- ERROR -- error' |
---|
20 | infmsg = 'INFORMATION -- information -- INFORMATION -- information' |
---|
21 | |
---|
22 | ####### Contents: |
---|
23 | # deg_deci: Function to pass from degrees [deg, minute, sec] to decimal angles [rad] |
---|
24 | # dist_points: Function to provide the distance between two points |
---|
25 | # join_circ_sec: Function to join aa series of points by circular segments |
---|
26 | # join_circ_sec_rand: Function to join aa series of points by circular segments with |
---|
27 | # random perturbations |
---|
28 | # max_coords_poly: Function to provide the extremes of the coordinates of a polygon |
---|
29 | # mirror_polygon: Function to reflex a polygon for a given axis |
---|
30 | # position_sphere: Function to tranform fom a point in lon, lat deg coordinates to |
---|
31 | # cartesian coordinates over an sphere |
---|
32 | # read_join_poly: Function to read an ASCII file with the combination of polygons |
---|
33 | # rotate_2D: Function to rotate a vector by a certain angle in the plain |
---|
34 | # rotate_polygon_2D: Function to rotate 2D plain the vertices of a polygon |
---|
35 | # rotate_line2D: Function to rotate a line given by 2 pairs of x,y coordinates by a |
---|
36 | # certain angle in the plain |
---|
37 | # rotate_lines2D: Function to rotate multiple lines given by mulitple pars of x,y |
---|
38 | # coordinates by a certain angle in the plain |
---|
39 | # spheric_line: Function to transform a series of locations in lon, lat coordinates |
---|
40 | # to x,y,z over an 3D spaceFunction to provide coordinates of a line on a 3D space |
---|
41 | # write_join_poly: Function to write an ASCII file with the combination of polygons |
---|
42 | |
---|
43 | ## Shapes/objects |
---|
44 | # buoy1: Function to draw a buoy as superposition of prism and section of ball |
---|
45 | # circ_sec: Function union of point A and B by a section of a circle |
---|
46 | # ellipse_polar: Function to determine an ellipse from its center and polar coordinates |
---|
47 | # p_doubleArrow: Function to provide an arrow with double lines |
---|
48 | # p_circle: Function to get a polygon of a circle |
---|
49 | # p_reg_polygon: Function to provide a regular polygon of Nv vertices |
---|
50 | # p_reg_star: Function to provide a regular star of Nv vertices |
---|
51 | # p_sinusiode: Function to get coordinates of a sinusoidal curve |
---|
52 | # p_square: Function to get a polygon square |
---|
53 | # p_spiral: Function to provide a polygon of an Archimedean spiral |
---|
54 | # p_triangle: Function to provide the polygon of a triangle from its 3 vertices |
---|
55 | # surface_sphere: Function to provide an sphere as matrix of x,y,z coordinates |
---|
56 | # z_boat: Function to define an schematic boat from the z-plane |
---|
57 | # zsailing_boat: Function to define an schematic sailing boat from the z-plane with sails |
---|
58 | # zisland1: Function to draw an island from z-axis as the union of a series of points by |
---|
59 | # circular segments |
---|
60 | ## Plotting |
---|
61 | # plot_sphere: Function to plot an sphere and determine which standard lines will be |
---|
62 | # also drawn |
---|
63 | |
---|
64 | def deg_deci(angle): |
---|
65 | """ Function to pass from degrees [deg, minute, sec] to decimal angles [rad] |
---|
66 | angle: list of [deg, minute, sec] to pass |
---|
67 | >>> deg_deci([41., 58., 34.]) |
---|
68 | 0.732621346072 |
---|
69 | """ |
---|
70 | fname = 'deg_deci' |
---|
71 | |
---|
72 | deg = np.abs(angle[0]) + np.abs(angle[1])/60. + np.abs(angle[2])/3600. |
---|
73 | |
---|
74 | if angle[0] < 0.: deg = -deg*np.pi/180. |
---|
75 | else: deg = deg*np.pi/180. |
---|
76 | |
---|
77 | return deg |
---|
78 | |
---|
79 | def position_sphere(radii, alpha, beta): |
---|
80 | """ Function to tranform fom a point in lon, lat deg coordinates to cartesian |
---|
81 | coordinates over an sphere |
---|
82 | radii: radii of the sphere |
---|
83 | alpha: longitude of the point |
---|
84 | beta: latitude of the point |
---|
85 | >>> position_sphere(10., 30., 45.) |
---|
86 | (0.81031678432964027, -5.1903473778327376, 8.5090352453411846 |
---|
87 | """ |
---|
88 | fname = 'position_sphere' |
---|
89 | |
---|
90 | xpt = radii*np.cos(beta)*np.cos(alpha) |
---|
91 | ypt = radii*np.cos(beta)*np.sin(alpha) |
---|
92 | zpt = radii*np.sin(beta) |
---|
93 | |
---|
94 | return xpt, ypt, zpt |
---|
95 | |
---|
96 | def spheric_line(radii,lon,lat): |
---|
97 | """ Function to transform a series of locations in lon, lat coordinates to x,y,z |
---|
98 | over an 3D space |
---|
99 | radii: radius of the sphere |
---|
100 | lon: array of angles along longitudes |
---|
101 | lat: array of angles along latitudes |
---|
102 | """ |
---|
103 | fname = 'spheric_line' |
---|
104 | |
---|
105 | Lint = lon.shape[0] |
---|
106 | coords = np.zeros((Lint,3), dtype=np.float) |
---|
107 | |
---|
108 | for iv in range(Lint): |
---|
109 | coords[iv,:] = position_sphere(radii, lon[iv], lat[iv]) |
---|
110 | |
---|
111 | return coords |
---|
112 | |
---|
113 | def rotate_2D(vector, angle): |
---|
114 | """ Function to rotate a vector by a certain angle in the plain |
---|
115 | vector= vector to rotate [y, x] |
---|
116 | angle= angle to rotate [rad] |
---|
117 | >>> rotate_2D(np.array([1.,0.]), np.pi/4.) |
---|
118 | [ 0.70710678 -0.70710678] |
---|
119 | """ |
---|
120 | fname = 'rotate_2D' |
---|
121 | |
---|
122 | rotmat = np.zeros((2,2), dtype=np.float) |
---|
123 | |
---|
124 | rotmat[0,0] = np.cos(angle) |
---|
125 | rotmat[0,1] = -np.sin(angle) |
---|
126 | rotmat[1,0] = np.sin(angle) |
---|
127 | rotmat[1,1] = np.cos(angle) |
---|
128 | |
---|
129 | rotvector = np.zeros((2), dtype=np.float) |
---|
130 | |
---|
131 | vecv = np.zeros((2), dtype=np.float) |
---|
132 | |
---|
133 | # Unifying vector |
---|
134 | modvec = vector[0]**2+vector[1]**2 |
---|
135 | if modvec != 0: |
---|
136 | vecv[0] = vector[1]/modvec |
---|
137 | vecv[1] = vector[0]/modvec |
---|
138 | |
---|
139 | rotvec = np.matmul(rotmat, vecv) |
---|
140 | rotvec = np.where(np.abs(rotvec) < 1.e-7, 0., rotvec) |
---|
141 | |
---|
142 | rotvector[0] = rotvec[1]*modvec |
---|
143 | rotvector[1] = rotvec[0]*modvec |
---|
144 | |
---|
145 | return rotvector |
---|
146 | |
---|
147 | def rotate_polygon_2D(vectors, angle): |
---|
148 | """ Function to rotate 2D plain the vertices of a polygon |
---|
149 | line= matrix of vectors to rotate |
---|
150 | angle= angle to rotate [rad] |
---|
151 | >>> square = np.zeros((4,2), dtype=np.float) |
---|
152 | >>> square[0,:] = [-0.5,-0.5] |
---|
153 | >>> square[1,:] = [0.5,-0.5] |
---|
154 | >>> square[2,:] = [0.5,0.5] |
---|
155 | >>> square[3,:] = [-0.5,0.5] |
---|
156 | >>> rotate_polygon_2D(square, np.pi/4.) |
---|
157 | [[-0.70710678 0. ] |
---|
158 | [ 0. -0.70710678] |
---|
159 | [ 0.70710678 0. ] |
---|
160 | [ 0. 0.70710678]] |
---|
161 | """ |
---|
162 | fname = 'rotate_polygon_2D' |
---|
163 | |
---|
164 | rotvecs = np.zeros(vectors.shape, dtype=np.float) |
---|
165 | |
---|
166 | Nvecs = vectors.shape[0] |
---|
167 | for iv in range(Nvecs): |
---|
168 | rotvecs[iv,:] = rotate_2D(vectors[iv,:], angle) |
---|
169 | |
---|
170 | return rotvecs |
---|
171 | |
---|
172 | def rotate_line2D(line, angle): |
---|
173 | """ Function to rotate a line given by 2 pairs of x,y coordinates by a certain |
---|
174 | angle in the plain |
---|
175 | line= line to rotate as couple of points [[y0,x0], [y1,x1]] |
---|
176 | angle= angle to rotate [rad] |
---|
177 | >>> rotate_line2D(np.array([[0.,0.], [1.,0.]]), np.pi/4.) |
---|
178 | [[ 0. 0. ] |
---|
179 | [0.70710678 -0.70710678]] |
---|
180 | """ |
---|
181 | fname = 'rotate_2D' |
---|
182 | |
---|
183 | rotline = np.zeros((2,2), dtype=np.float) |
---|
184 | rotline[0,:] = rotate_2D(line[0,:], angle) |
---|
185 | rotline[1,:] = rotate_2D(line[1,:], angle) |
---|
186 | |
---|
187 | return rotline |
---|
188 | |
---|
189 | def rotate_lines2D(lines, angle): |
---|
190 | """ Function to rotate multiple lines given by mulitple pars of x,y coordinates |
---|
191 | by a certain angle in the plain |
---|
192 | line= matrix of N couples of points [N, [y0,x0], [y1,x1]] |
---|
193 | angle= angle to rotate [rad] |
---|
194 | >>> square = np.zeros((4,2,2), dtype=np.float) |
---|
195 | >>> square[0,0,:] = [-0.5,-0.5] |
---|
196 | >>> square[0,1,:] = [0.5,-0.5] |
---|
197 | >>> square[1,0,:] = [0.5,-0.5] |
---|
198 | >>> square[1,1,:] = [0.5,0.5] |
---|
199 | >>> square[2,0,:] = [0.5,0.5] |
---|
200 | >>> square[2,1,:] = [-0.5,0.5] |
---|
201 | >>> square[3,0,:] = [-0.5,0.5] |
---|
202 | >>> square[3,1,:] = [-0.5,-0.5] |
---|
203 | >>> rotate_lines2D(square, np.pi/4.) |
---|
204 | [[[-0.70710678 0. ] |
---|
205 | [ 0. -0.70710678]] |
---|
206 | |
---|
207 | [[ 0. -0.70710678] |
---|
208 | [ 0.70710678 0. ]] |
---|
209 | |
---|
210 | [[ 0.70710678 0. ] |
---|
211 | [ 0. 0.70710678]] |
---|
212 | |
---|
213 | [[ 0. 0.70710678] |
---|
214 | [-0.70710678 0. ]]] |
---|
215 | """ |
---|
216 | fname = 'rotate_lines2D' |
---|
217 | |
---|
218 | rotlines = np.zeros(lines.shape, dtype=np.float) |
---|
219 | |
---|
220 | Nlines = lines.shape[0] |
---|
221 | for il in range(Nlines): |
---|
222 | line = np.zeros((2,2), dtype=np.float) |
---|
223 | line[0,:] = lines[il,0,:] |
---|
224 | line[1,:] = lines[il,1,:] |
---|
225 | |
---|
226 | rotlines[il,:,:] = rotate_line2D(line, angle) |
---|
227 | |
---|
228 | return rotlines |
---|
229 | |
---|
230 | def dist_points(ptA, ptB): |
---|
231 | """ Function to provide the distance between two points |
---|
232 | ptA: coordinates of the point A [yA, xA] |
---|
233 | ptB: coordinates of the point B [yB, xB] |
---|
234 | >>> dist_points([1.,1.], [-1.,-1.]) |
---|
235 | 2.82842712475 |
---|
236 | """ |
---|
237 | fname = 'dist_points' |
---|
238 | |
---|
239 | dist = np.sqrt( (ptA[0]-ptB[0])**2 + (ptA[1]-ptB[1])**2) |
---|
240 | |
---|
241 | return dist |
---|
242 | |
---|
243 | def max_coords_poly(polygon): |
---|
244 | """ Function to provide the extremes of the coordinates of a polygon |
---|
245 | polygon: coordinates [Nvertexs, 2] of a polygon |
---|
246 | >>> square = np.zeros((4,2), dtype=np.float) |
---|
247 | >>> square[0,:] = [-0.5,-0.5] |
---|
248 | >>> square[1,:] = [0.5,-0.5] |
---|
249 | >>> square[2,:] = [0.5,0.5] |
---|
250 | >>> square[3,:] = [-0.5,0.5] |
---|
251 | >>> max_coords_poly(square) |
---|
252 | [-0.5, 0.5], [-0.5, 0.5], [0.5, 0.5], 0.5 |
---|
253 | """ |
---|
254 | fname = 'max_coords_poly' |
---|
255 | |
---|
256 | # x-coordinate min/max |
---|
257 | nx = np.min(polygon[:,1]) |
---|
258 | xx = np.max(polygon[:,1]) |
---|
259 | |
---|
260 | # y-coordinate min/max |
---|
261 | ny = np.min(polygon[:,0]) |
---|
262 | xy = np.max(polygon[:,0]) |
---|
263 | |
---|
264 | # x/y-coordinate maximum of absolute values |
---|
265 | axx = np.max(np.abs(polygon[:,1])) |
---|
266 | ayx = np.max(np.abs(polygon[:,0])) |
---|
267 | |
---|
268 | # absolute maximum |
---|
269 | xyx = np.max([axx, ayx]) |
---|
270 | |
---|
271 | return [nx, xx], [ny, xy], [ayx, axx], xyx |
---|
272 | |
---|
273 | def mirror_polygon(polygon,axis): |
---|
274 | """ Function to reflex a polygon for a given axis |
---|
275 | polygon: polygon to mirror |
---|
276 | axis: axis at which mirror is located ('x' or 'y') |
---|
277 | """ |
---|
278 | fname = 'mirror_polygon' |
---|
279 | |
---|
280 | reflex = np.zeros(polygon.shape, dtype=np.float) |
---|
281 | |
---|
282 | N = polygon.shape[0] |
---|
283 | if axis == 'x': |
---|
284 | for iv in range(N): |
---|
285 | reflex[iv,:] = [-polygon[iv,0], polygon[iv,1]] |
---|
286 | elif axis == 'y': |
---|
287 | for iv in range(N): |
---|
288 | reflex[iv,:] = [polygon[iv,0], -polygon[iv,1]] |
---|
289 | |
---|
290 | return reflex |
---|
291 | |
---|
292 | def join_circ_sec(points, radfrac=3., N=200): |
---|
293 | """ Function to join aa series of points by circular segments |
---|
294 | points: main points of the island (clockwise ordered, to be joined by circular |
---|
295 | segments of radii as the radfrac factor of the distance between |
---|
296 | consecutive points) |
---|
297 | radfrac: multiplicative factor of the distance between consecutive points to |
---|
298 | draw the circular segment (3., default) |
---|
299 | N: number of points (200, default) |
---|
300 | """ |
---|
301 | fname = 'join_circ_sec' |
---|
302 | |
---|
303 | jcirc_sec = np.ones((N,2), dtype=np.float) |
---|
304 | |
---|
305 | # main points |
---|
306 | lpoints = list(points) |
---|
307 | Npts = len(lpoints) |
---|
308 | Np = int(N/(Npts+1)) |
---|
309 | for ip in range(Npts-1): |
---|
310 | p1 = lpoints[ip] |
---|
311 | p2 = lpoints[ip+1] |
---|
312 | dps = dist_points(p1, p2) |
---|
313 | jcirc_sec[Np*ip:Np*(ip+1),:] = circ_sec(p1, p2, dps*radfrac, 'short', Np) |
---|
314 | |
---|
315 | Np2 = N - (Npts-1)*Np |
---|
316 | p1 = lpoints[Npts-1] |
---|
317 | p2 = lpoints[0] |
---|
318 | dps = dist_points(p1, p2) |
---|
319 | jcirc_sec[(Npts-1)*Np:N,:] = circ_sec(p1, p2, dps*3., 'short', Np2) |
---|
320 | |
---|
321 | return jcirc_sec |
---|
322 | |
---|
323 | def join_circ_sec_rand(points, radfrac=3., Lrand=0.1, N=200): |
---|
324 | """ Function to join aa series of points by circular segments with random |
---|
325 | perturbations |
---|
326 | points: main points of the island (clockwise ordered, to be joined by circular |
---|
327 | segments of radii as the radfrac factor of the distance between |
---|
328 | consecutive points) |
---|
329 | radfrac: multiplicative factor of the distance between consecutive points to |
---|
330 | draw the circular segment (3., default) |
---|
331 | Lrand: maximum length of the random perturbation to be added perpendicularly to |
---|
332 | the direction of the union line between points (0.1, default) |
---|
333 | N: number of points (200, default) |
---|
334 | """ |
---|
335 | import random |
---|
336 | fname = 'join_circ_sec_rand' |
---|
337 | |
---|
338 | jcirc_sec = np.ones((N,2), dtype=np.float) |
---|
339 | |
---|
340 | # main points |
---|
341 | lpoints = list(points) |
---|
342 | Npts = len(lpoints) |
---|
343 | Np = int(N/(Npts+1)) |
---|
344 | for ip in range(Npts-1): |
---|
345 | p1 = lpoints[ip] |
---|
346 | p2 = lpoints[ip+1] |
---|
347 | dps = dist_points(p1, p2) |
---|
348 | angle = np.arctan2(p2[0]-p1[0], p2[1]-p1[1]) + np.pi/2. |
---|
349 | jcirc_sec[Np*ip:Np*(ip+1),:] = circ_sec(p1, p2, dps*radfrac, 'short', Np) |
---|
350 | drand = Lrand*np.array([np.sin(angle), np.cos(angle)]) |
---|
351 | for iip in range(Np*ip,Np*(ip+1)): |
---|
352 | jcirc_sec[iip,:] = jcirc_sec[iip,:] + drand*random.uniform(-1.,1.) |
---|
353 | |
---|
354 | Np2 = N - (Npts-1)*Np |
---|
355 | p1 = lpoints[Npts-1] |
---|
356 | p2 = lpoints[0] |
---|
357 | dps = dist_points(p1, p2) |
---|
358 | angle = np.arctan2(p2[0]-p1[0], p2[1]-p1[1]) + np.pi/2. |
---|
359 | jcirc_sec[(Npts-1)*Np:N,:] = circ_sec(p1, p2, dps*3., 'short', Np2) |
---|
360 | drand = Lrand*np.array([np.sin(angle), np.cos(angle)]) |
---|
361 | for iip in range(Np*(Npts-1),N): |
---|
362 | jcirc_sec[iip,:] = jcirc_sec[iip,:] + drand*random.uniform(-1.,1.) |
---|
363 | |
---|
364 | return jcirc_sec |
---|
365 | |
---|
366 | def write_join_poly(polys, flname='join_polygons.dat'): |
---|
367 | """ Function to write an ASCII file with the combination of polygons |
---|
368 | polys: dictionary with the names of the different polygons |
---|
369 | flname: name of the ASCII file |
---|
370 | """ |
---|
371 | fname = 'write_join_poly' |
---|
372 | |
---|
373 | of = open(flname, 'w') |
---|
374 | |
---|
375 | for polyn in polys.keys(): |
---|
376 | vertices = polys[polyn] |
---|
377 | Npts = vertices.shape[0] |
---|
378 | for ip in range(Npts): |
---|
379 | of.write(polyn+' '+str(vertices[ip,1]) + ' ' + str(vertices[ip,0]) + '\n') |
---|
380 | |
---|
381 | of.close() |
---|
382 | |
---|
383 | return |
---|
384 | |
---|
385 | def read_join_poly(flname='join_polygons.dat'): |
---|
386 | """ Function to read an ASCII file with the combination of polygons |
---|
387 | flname: name of the ASCII file |
---|
388 | """ |
---|
389 | fname = 'read_join_poly' |
---|
390 | |
---|
391 | of = open(flname, 'r') |
---|
392 | |
---|
393 | polys = {} |
---|
394 | polyn = '' |
---|
395 | poly = [] |
---|
396 | for line in of: |
---|
397 | if len(line) > 1: |
---|
398 | linevals = line.replace('\n','').split(' ') |
---|
399 | if polyn != linevals[0]: |
---|
400 | if len(poly) > 1: |
---|
401 | polys[polyn] = np.array(poly) |
---|
402 | polyn = linevals[0] |
---|
403 | poly = [] |
---|
404 | poly.append([np.float(linevals[2]), np.float(linevals[1])]) |
---|
405 | else: |
---|
406 | poly.append([np.float(linevals[2]), np.float(linevals[1])]) |
---|
407 | |
---|
408 | of.close() |
---|
409 | polys[polyn] = np.array(poly) |
---|
410 | |
---|
411 | return polys |
---|
412 | |
---|
413 | ####### ###### ##### #### ### ## # |
---|
414 | # Shapes/objects |
---|
415 | |
---|
416 | def surface_sphere(radii,Npts): |
---|
417 | """ Function to provide an sphere as matrix of x,y,z coordinates |
---|
418 | radii: radii of the sphere |
---|
419 | Npts: number of points to discretisize longitues (half for latitudes) |
---|
420 | """ |
---|
421 | fname = 'surface_sphere' |
---|
422 | |
---|
423 | sphereup = np.zeros((3,Npts/2,Npts), dtype=np.float) |
---|
424 | spheredown = np.zeros((3,Npts/2,Npts), dtype=np.float) |
---|
425 | for ia in range(Npts): |
---|
426 | alpha = ia*2*np.pi/(Npts-1) |
---|
427 | for ib in range(Npts/2): |
---|
428 | beta = ib*np.pi/(2.*(Npts/2-1)) |
---|
429 | sphereup[:,ib,ia] = position_sphere(radii, alpha, beta) |
---|
430 | for ib in range(Npts/2): |
---|
431 | beta = -ib*np.pi/(2.*(Npts/2-1)) |
---|
432 | spheredown[:,ib,ia] = position_sphere(radii, alpha, beta) |
---|
433 | |
---|
434 | return sphereup, spheredown |
---|
435 | |
---|
436 | def ellipse_polar(c, a, b, Nang=100): |
---|
437 | """ Function to determine an ellipse from its center and polar coordinates |
---|
438 | FROM: https://en.wikipedia.org/wiki/Ellipse |
---|
439 | c= coordinates of the center |
---|
440 | a= distance major axis |
---|
441 | b= distance minor axis |
---|
442 | Nang= number of angles to use |
---|
443 | """ |
---|
444 | fname = 'ellipse_polar' |
---|
445 | |
---|
446 | if np.mod(Nang,2) == 0: Nang=Nang+1 |
---|
447 | |
---|
448 | dtheta = 2*np.pi/(Nang-1) |
---|
449 | |
---|
450 | ellipse = np.zeros((Nang,2), dtype=np.float) |
---|
451 | for ia in range(Nang): |
---|
452 | theta = dtheta*ia |
---|
453 | rad = a*b/np.sqrt( (b*np.cos(theta))**2 + (a*np.sin(theta))**2 ) |
---|
454 | x = rad*np.cos(theta) |
---|
455 | y = rad*np.sin(theta) |
---|
456 | ellipse[ia,:] = [y+c[0],x+c[1]] |
---|
457 | |
---|
458 | return ellipse |
---|
459 | |
---|
460 | def hyperbola_polar(a, b, Nang=100): |
---|
461 | """ Fcuntion to determine an hyperbola in polar coordinates |
---|
462 | FROM: https://en.wikipedia.org/wiki/Hyperbola#Polar_coordinates |
---|
463 | x^2/a^2 - y^2/b^2 = 1 |
---|
464 | a= x-parameter |
---|
465 | y= y-parameter |
---|
466 | Nang= number of angles to use |
---|
467 | DOES NOT WORK!!!! |
---|
468 | """ |
---|
469 | fname = 'hyperbola_polar' |
---|
470 | |
---|
471 | dtheta = 2.*np.pi/(Nang-1) |
---|
472 | |
---|
473 | # Positive branch |
---|
474 | hyperbola_p = np.zeros((Nang,2), dtype=np.float) |
---|
475 | for ia in range(Nang): |
---|
476 | theta = dtheta*ia |
---|
477 | x = a*np.cosh(theta) |
---|
478 | y = b*np.sinh(theta) |
---|
479 | hyperbola_p[ia,:] = [y,x] |
---|
480 | |
---|
481 | # Negative branch |
---|
482 | hyperbola_n = np.zeros((Nang,2), dtype=np.float) |
---|
483 | for ia in range(Nang): |
---|
484 | theta = dtheta*ia |
---|
485 | x = -a*np.cosh(theta) |
---|
486 | y = b*np.sinh(theta) |
---|
487 | hyperbola_n[ia,:] = [y,x] |
---|
488 | |
---|
489 | return hyperbola_p, hyperbola_n |
---|
490 | |
---|
491 | def circ_sec(ptA, ptB, radii, arc='short', Nang=100): |
---|
492 | """ Function union of point A and B by a section of a circle |
---|
493 | ptA= coordinates od the point A [yA, xA] |
---|
494 | ptB= coordinates od the point B [yB, xB] |
---|
495 | radii= radi of the circle to use to unite the points |
---|
496 | arc= which arc to be used ('short', default) |
---|
497 | 'short': shortest angle between points |
---|
498 | 'long': largest angle between points |
---|
499 | Nang= amount of angles to use |
---|
500 | """ |
---|
501 | fname = 'circ_sec' |
---|
502 | |
---|
503 | distAB = dist_points(ptA,ptB) |
---|
504 | |
---|
505 | if distAB > radii: |
---|
506 | print errormsg |
---|
507 | print ' ' + fname + ': radii=', radii, " too small for the distance " + \ |
---|
508 | "between points !!" |
---|
509 | print ' distance between points:', distAB |
---|
510 | quit(-1) |
---|
511 | |
---|
512 | # Coordinate increments |
---|
513 | dAB = np.abs(ptA-ptB) |
---|
514 | |
---|
515 | # angle of the circular section joining points |
---|
516 | alpha = 2.*np.arcsin((distAB/2.)/radii) |
---|
517 | |
---|
518 | # center along coincident bisection of the union |
---|
519 | xcc = -radii |
---|
520 | ycc = 0. |
---|
521 | |
---|
522 | # Getting the arc of the circle at the x-axis |
---|
523 | if arc == 'short': |
---|
524 | dalpha = alpha/(Nang-1) |
---|
525 | else: |
---|
526 | dalpha = (2.*np.pi - alpha)/(Nang-1) |
---|
527 | circ_sec = np.zeros((Nang,2), dtype=np.float) |
---|
528 | for ia in range(Nang): |
---|
529 | alpha = dalpha*ia |
---|
530 | x = radii*np.cos(alpha) |
---|
531 | y = radii*np.sin(alpha) |
---|
532 | |
---|
533 | circ_sec[ia,:] = [y+ycc,x+xcc] |
---|
534 | |
---|
535 | # Angle of the points |
---|
536 | theta = np.arctan2(ptB[0]-ptA[0],ptB[1]-ptA[1]) |
---|
537 | |
---|
538 | # rotating angle of the circ |
---|
539 | rotangle = theta + 3.*np.pi/2. - alpha/2. |
---|
540 | |
---|
541 | #print 'alpha:', alpha*180./np.pi, 'theta:', theta*180./np.pi, 'rotangle:', rotangle*180./np.pi |
---|
542 | |
---|
543 | # rotating the arc along the x-axis |
---|
544 | rotcirc_sec = rotate_polygon_2D(circ_sec, rotangle) |
---|
545 | |
---|
546 | # Moving arc to the ptA |
---|
547 | circ_sec = rotcirc_sec + ptA |
---|
548 | |
---|
549 | return circ_sec |
---|
550 | |
---|
551 | def p_square(face, N=5): |
---|
552 | """ Function to get a polygon square |
---|
553 | face: length of the face of the square |
---|
554 | N: number of points of the polygon |
---|
555 | """ |
---|
556 | fname = 'p_square' |
---|
557 | |
---|
558 | square = np.zeros((N,2), dtype=np.float) |
---|
559 | |
---|
560 | f2 = face/2. |
---|
561 | N4 = N/4 |
---|
562 | df = face/(N4) |
---|
563 | # SW-NW |
---|
564 | for ip in range(N4): |
---|
565 | square[ip,:] = [-f2+ip*df,-f2] |
---|
566 | # NW-NE |
---|
567 | for ip in range(N4): |
---|
568 | square[ip+N4,:] = [f2,-f2+ip*df] |
---|
569 | # NE-SE |
---|
570 | for ip in range(N4): |
---|
571 | square[ip+2*N4,:] = [f2-ip*df,f2] |
---|
572 | N42 = N-3*N4-1 |
---|
573 | df = face/(N42) |
---|
574 | # SE-SW |
---|
575 | for ip in range(N42): |
---|
576 | square[ip+3*N4,:] = [-f2,f2-ip*df] |
---|
577 | square[N-1,:] = [-f2,-f2] |
---|
578 | |
---|
579 | return square |
---|
580 | |
---|
581 | def p_circle(radii, N=50): |
---|
582 | """ Function to get a polygon of a circle |
---|
583 | radii: length of the radii of the circle |
---|
584 | N: number of points of the polygon |
---|
585 | """ |
---|
586 | fname = 'p_circle' |
---|
587 | |
---|
588 | circle = np.zeros((N,2), dtype=np.float) |
---|
589 | |
---|
590 | dangle = 2.*np.pi/(N-1) |
---|
591 | |
---|
592 | for ia in range(N): |
---|
593 | circle[ia,:] = [radii*np.sin(ia*dangle), radii*np.cos(ia*dangle)] |
---|
594 | |
---|
595 | circle[N-1,:] = [0., radii] |
---|
596 | |
---|
597 | return circle |
---|
598 | |
---|
599 | def p_triangle(p1, p2, p3, N=4): |
---|
600 | """ Function to provide the polygon of a triangle from its 3 vertices |
---|
601 | p1: vertex 1 [y,x] |
---|
602 | p2: vertex 2 [y,x] |
---|
603 | p3: vertex 3 [y,x] |
---|
604 | N: number of vertices of the triangle |
---|
605 | """ |
---|
606 | fname = 'p_triangle' |
---|
607 | |
---|
608 | triangle = np.zeros((N,2), dtype=np.float) |
---|
609 | |
---|
610 | N3 = N / 3 |
---|
611 | # 1-2 |
---|
612 | dx = (p2[1]-p1[1])/N3 |
---|
613 | dy = (p2[0]-p1[0])/N3 |
---|
614 | for ip in range(N3): |
---|
615 | triangle[ip,:] = [p1[0]+ip*dy,p1[1]+ip*dx] |
---|
616 | # 2-3 |
---|
617 | dx = (p3[1]-p2[1])/N3 |
---|
618 | dy = (p3[0]-p2[0])/N3 |
---|
619 | for ip in range(N3): |
---|
620 | triangle[ip+N3,:] = [p2[0]+ip*dy,p2[1]+ip*dx] |
---|
621 | # 3-1 |
---|
622 | N32 = N - 2*N/3 |
---|
623 | dx = (p1[1]-p3[1])/N32 |
---|
624 | dy = (p1[0]-p3[0])/N32 |
---|
625 | for ip in range(N32): |
---|
626 | triangle[ip+2*N3,:] = [p3[0]+ip*dy,p3[1]+ip*dx] |
---|
627 | |
---|
628 | triangle[N-1,:] = p1 |
---|
629 | |
---|
630 | return triangle |
---|
631 | |
---|
632 | def p_spiral(loops, eradii, N=1000): |
---|
633 | """ Function to provide a polygon of an Archimedean spiral |
---|
634 | FROM: https://en.wikipedia.org/wiki/Spiral |
---|
635 | loops: number of loops of the spiral |
---|
636 | eradii: length of the radii of the final spiral |
---|
637 | N: number of points of the polygon |
---|
638 | """ |
---|
639 | fname = 'p_spiral' |
---|
640 | |
---|
641 | spiral = np.zeros((N,2), dtype=np.float) |
---|
642 | |
---|
643 | dangle = 2.*np.pi*loops/(N-1) |
---|
644 | dr = eradii*1./(N-1) |
---|
645 | |
---|
646 | for ia in range(N): |
---|
647 | radii = dr*ia |
---|
648 | spiral[ia,:] = [radii*np.sin(ia*dangle), radii*np.cos(ia*dangle)] |
---|
649 | |
---|
650 | return spiral |
---|
651 | |
---|
652 | def p_reg_polygon(Nv, lf, N=50): |
---|
653 | """ Function to provide a regular polygon of Nv vertices |
---|
654 | Nv: number of vertices |
---|
655 | lf: length of the face |
---|
656 | N: number of points |
---|
657 | """ |
---|
658 | fname = 'p_reg_polygon' |
---|
659 | |
---|
660 | reg_polygon = np.zeros((N,2), dtype=np.float) |
---|
661 | |
---|
662 | # Number of points per vertex |
---|
663 | Np = N/Nv |
---|
664 | # Angle incremental between vertices |
---|
665 | da = 2.*np.pi/Nv |
---|
666 | # Radii of the circle according to lf |
---|
667 | radii = lf*Nv/(2*np.pi) |
---|
668 | |
---|
669 | iip = 0 |
---|
670 | for iv in range(Nv-1): |
---|
671 | # Characteristics between vertices iv and iv+1 |
---|
672 | av1 = da*iv |
---|
673 | v1 = [radii*np.sin(av1), radii*np.cos(av1)] |
---|
674 | av2 = da*(iv+1) |
---|
675 | v2 = [radii*np.sin(av2), radii*np.cos(av2)] |
---|
676 | dx = (v2[1]-v1[1])/Np |
---|
677 | dy = (v2[0]-v1[0])/Np |
---|
678 | for ip in range(Np): |
---|
679 | reg_polygon[ip+iv*Np,:] = [v1[0]+dy*ip,v1[1]+dx*ip] |
---|
680 | |
---|
681 | # Characteristics between vertices Nv and 1 |
---|
682 | |
---|
683 | # Number of points per vertex |
---|
684 | Np2 = N - Np*(Nv-1) |
---|
685 | |
---|
686 | av1 = da*Nv |
---|
687 | v1 = [radii*np.sin(av1), radii*np.cos(av1)] |
---|
688 | av2 = 0. |
---|
689 | v2 = [radii*np.sin(av2), radii*np.cos(av2)] |
---|
690 | dx = (v2[1]-v1[1])/Np2 |
---|
691 | dy = (v2[0]-v1[0])/Np2 |
---|
692 | for ip in range(Np2): |
---|
693 | reg_polygon[ip+(Nv-1)*Np,:] = [v1[0]+dy*ip,v1[1]+dx*ip] |
---|
694 | |
---|
695 | return reg_polygon |
---|
696 | |
---|
697 | def p_reg_star(Nv, lf, freq, vs=0, N=50): |
---|
698 | """ Function to provide a regular star of Nv vertices |
---|
699 | Nv: number of vertices |
---|
700 | lf: length of the face of the regular polygon |
---|
701 | freq: frequency of union of vertices ('0', for just centered to zero arms) |
---|
702 | vs: vertex from which start (0 being first [0,lf]) |
---|
703 | N: number of points |
---|
704 | """ |
---|
705 | fname = 'p_reg_star' |
---|
706 | |
---|
707 | reg_star = np.zeros((N,2), dtype=np.float) |
---|
708 | |
---|
709 | # Number of arms of the star |
---|
710 | if freq != 0 and np.mod(Nv,freq) == 0: |
---|
711 | Na = Nv/freq + 1 |
---|
712 | else: |
---|
713 | Na = Nv |
---|
714 | |
---|
715 | # Number of points per arm |
---|
716 | Np = N/Na |
---|
717 | # Angle incremental between vertices |
---|
718 | da = 2.*np.pi/Nv |
---|
719 | # Radii of the circle according to lf |
---|
720 | radii = lf*Nv/(2*np.pi) |
---|
721 | |
---|
722 | iip = 0 |
---|
723 | av1 = vs*da |
---|
724 | for iv in range(Na-1): |
---|
725 | # Characteristics between vertices iv and iv+1 |
---|
726 | v1 = [radii*np.sin(av1), radii*np.cos(av1)] |
---|
727 | if freq != 0: |
---|
728 | av2 = av1 + da*freq |
---|
729 | v2 = [radii*np.sin(av2), radii*np.cos(av2)] |
---|
730 | else: |
---|
731 | v2 = [0., 0.] |
---|
732 | av2 = av1 + da |
---|
733 | dx = (v2[1]-v1[1])/(Np-1) |
---|
734 | dy = (v2[0]-v1[0])/(Np-1) |
---|
735 | for ip in range(Np): |
---|
736 | reg_star[ip+iv*Np,:] = [v1[0]+dy*ip,v1[1]+dx*ip] |
---|
737 | if av2 > 2.*np.pi: av1 = av2 - 2.*np.pi |
---|
738 | else: av1 = av2 + 0. |
---|
739 | |
---|
740 | iv = Na-1 |
---|
741 | # Characteristics between vertices Na and 1 |
---|
742 | Np2 = N-Np*iv |
---|
743 | v1 = [radii*np.sin(av1), radii*np.cos(av1)] |
---|
744 | if freq != 0: |
---|
745 | av2 = vs*da |
---|
746 | v2 = [radii*np.sin(av2), radii*np.cos(av2)] |
---|
747 | else: |
---|
748 | v2 = [0., 0.] |
---|
749 | dx = (v2[1]-v1[1])/(Np2-1) |
---|
750 | dy = (v2[0]-v1[0])/(Np2-1) |
---|
751 | for ip in range(Np2): |
---|
752 | reg_star[ip+iv*Np,:] = [v1[0]+dy*ip,v1[1]+dx*ip] |
---|
753 | |
---|
754 | return reg_star |
---|
755 | |
---|
756 | def p_sinusiode(length=10., amp=5., lamb=3., ival=0., func='sin', N=100): |
---|
757 | """ Function to get coordinates of a sinusoidal curve |
---|
758 | length: length of the line (default 10.) |
---|
759 | amp: amplitude of the peaks (default 5.) |
---|
760 | lamb: wave longitude (defalult 3.) |
---|
761 | ival: initial angle (default 0. in degree) |
---|
762 | func: function to use: (default sinus) |
---|
763 | 'sin': sinus |
---|
764 | 'cos': cosinus |
---|
765 | N: number of points (default 100) |
---|
766 | """ |
---|
767 | fname = 'p_sinusiode' |
---|
768 | availfunc = ['sin', 'cos'] |
---|
769 | |
---|
770 | dx = length/(N-1) |
---|
771 | ia = ival*np.pi/180. |
---|
772 | da = 2*np.pi*dx/lamb |
---|
773 | |
---|
774 | sinusoide = np.zeros((N,2), dtype=np.float) |
---|
775 | if func == 'sin': |
---|
776 | for ix in range(N): |
---|
777 | sinusoide[ix,:] = [amp*np.sin(ia+da*ix),dx*ix] |
---|
778 | elif func == 'cos': |
---|
779 | for ix in range(N): |
---|
780 | sinusoide[ix,:] = [amp*np.cos(ia+da*ix),dx*ix] |
---|
781 | else: |
---|
782 | print errormsg |
---|
783 | print ' ' + fname + ": function '" + func + "' not ready !!" |
---|
784 | print ' available ones:', availfunc |
---|
785 | quit(-1) |
---|
786 | |
---|
787 | sinusoidesecs = ['sinusoide'] |
---|
788 | sinusoidedic = {'sinusoide': [sinusoide, '-', '#000000', 1.]} |
---|
789 | |
---|
790 | return sinusoide, sinusoidesecs, sinusoidedic |
---|
791 | |
---|
792 | def p_doubleArrow(length=5., angle=45., width=1., alength=0.10, N=50): |
---|
793 | """ Function to provide an arrow with double lines |
---|
794 | length: length of the arrow (5. default) |
---|
795 | angle: angle of the head of the arrow (45., default) |
---|
796 | width: separation between the two lines (2., default) |
---|
797 | alength: length of the head (as percentage in excess of width, 0.1 default) |
---|
798 | N: number of points (50, default) |
---|
799 | """ |
---|
800 | import numpy.ma as ma |
---|
801 | function = 'p_doubleArrow' |
---|
802 | |
---|
803 | doubleArrow = np.zeros((50,2), dtype=np.float) |
---|
804 | N4 = int((N-3)/4) |
---|
805 | |
---|
806 | doublearrowdic = {} |
---|
807 | |
---|
808 | # Arms |
---|
809 | dx = length/(N4-1) |
---|
810 | for ix in range(N4-1): |
---|
811 | doubleArrow[ix,:] = [dx*ix,-width/2.] |
---|
812 | doublearrowdic['leftarm'] = [doubleArrow[0:N4-1,:], '-', '#000000', 2.] |
---|
813 | doubleArrow[N4-1,:] = [gen.fillValueF,gen.fillValueF] |
---|
814 | for ix in range(N4-1): |
---|
815 | doubleArrow[N4+ix,:] = [dx*ix,width/2.] |
---|
816 | doublearrowdic['rightarm'] = [doubleArrow[N4:2*N4-1,:], '-', '#000000', 2.] |
---|
817 | doubleArrow[2*N4-1,:] = [gen.fillValueF,gen.fillValueF] |
---|
818 | |
---|
819 | # Head |
---|
820 | N42 = int((N-2 - 2*N4)/2) |
---|
821 | dx = width*(1.+alength)/(N42-1) |
---|
822 | for ix in range(N42): |
---|
823 | doubleArrow[2*N4+ix,:] = [length-dx*ix,-dx*ix] |
---|
824 | doublearrowdic['lefthead'] = [doubleArrow[2*N4:2*N4+N42,:], '-', '#000000', 2.] |
---|
825 | doubleArrow[2*N4+N42,:] = [gen.fillValueF,gen.fillValueF] |
---|
826 | |
---|
827 | N43 = N-2 - 2*N4 - N42 + 1 |
---|
828 | dx = width*(1.+alength)/(N43-1) |
---|
829 | for ix in range(N43): |
---|
830 | doubleArrow[2*N4+N42+1+ix,:] = [length-dx*ix,dx*ix] |
---|
831 | doublearrowdic['rightthead'] = [doubleArrow[2*N4+N42:51,:], '-', '#000000', 2.] |
---|
832 | |
---|
833 | doubleArrow = ma.masked_equal(doubleArrow, gen.fillValueF) |
---|
834 | doublearrowsecs = ['leftarm', 'rightarm', 'lefthead', 'righthead'] |
---|
835 | |
---|
836 | return doubleArrow, doublearrowsecs, doublearrowdic |
---|
837 | |
---|
838 | # Combined objects |
---|
839 | ## |
---|
840 | |
---|
841 | # FROM: http://www.photographers1.com/Sailing/NauticalTerms&Nomenclature.html |
---|
842 | def zboat(length=10., beam=1., lbeam=0.4, sternbp=0.5): |
---|
843 | """ Function to define an schematic boat from the z-plane |
---|
844 | length: length of the boat (without stern, default 10) |
---|
845 | beam: beam of the boat (default 1) |
---|
846 | lbeam: length at beam (as percentage of length, default 0.4) |
---|
847 | sternbp: beam at stern (as percentage of beam, default 0.5) |
---|
848 | """ |
---|
849 | fname = 'zboat' |
---|
850 | |
---|
851 | bow = np.array([length, 0.]) |
---|
852 | maxportside = np.array([length*lbeam, -beam]) |
---|
853 | maxstarboardside = np.array([length*lbeam, beam]) |
---|
854 | portside = np.array([0., -beam*sternbp]) |
---|
855 | starboardside = np.array([0., beam*sternbp]) |
---|
856 | |
---|
857 | # forward section |
---|
858 | fportside = circ_sec(bow,maxportside, length*2) |
---|
859 | fstarboardside = circ_sec(maxstarboardside, bow, length*2) |
---|
860 | # aft section |
---|
861 | aportside = circ_sec(maxportside, portside, length*2) |
---|
862 | astarboardside = circ_sec(starboardside, maxstarboardside, length*2) |
---|
863 | # stern |
---|
864 | stern = circ_sec(portside, starboardside, length*2) |
---|
865 | |
---|
866 | dpts = stern.shape[0] |
---|
867 | boat = np.zeros((dpts*5,2), dtype=np.float) |
---|
868 | |
---|
869 | boat[0:dpts,:] = fportside |
---|
870 | boat[dpts:2*dpts,:] = aportside |
---|
871 | boat[2*dpts:3*dpts,:] = stern |
---|
872 | boat[3*dpts:4*dpts,:] = astarboardside |
---|
873 | boat[4*dpts:5*dpts,:] = fstarboardside |
---|
874 | |
---|
875 | fname = 'boat_L' + str(int(length*100.)) + '_B' + str(int(beam*100.)) + '_lb' + \ |
---|
876 | str(int(lbeam*100.)) + '_sb' + str(int(sternbp*100.)) + '.dat' |
---|
877 | if not os.path.isfile(fname): |
---|
878 | print infmsg |
---|
879 | print ' ' + fname + ": writting boat coordinates file '" + fname + "' !!" |
---|
880 | of = open(fname, 'w') |
---|
881 | of.write('# boat file with Length: ' + str(length) +' max_beam: '+str(beam)+ \ |
---|
882 | 'length_at_max_beam:' + str(lbeam) + '% beam at stern: ' + str(sternbp)+ \ |
---|
883 | ' %\n') |
---|
884 | for ip in range(dpts*5): |
---|
885 | of.write(str(boat[ip,0]) + ' ' + str(boat[ip,1]) + '\n') |
---|
886 | |
---|
887 | of.close() |
---|
888 | print fname + ": Successfull written '" + fname + "' !!" |
---|
889 | |
---|
890 | return boat |
---|
891 | |
---|
892 | def zsailing_boat(length=10., beam=1., lbeam=0.4, sternbp=0.5, lmast=0.6, wmast=0.1, \ |
---|
893 | hsd=5., msd=5., lheads=0.38, lmains=0.55): |
---|
894 | """ Function to define an schematic sailing boat from the z-plane with sails |
---|
895 | length: length of the boat (without stern, default 10) |
---|
896 | beam: beam of the boat (default 1) |
---|
897 | lbeam: length at beam (as percentage of length, default 0.4) |
---|
898 | sternbp: beam at stern (as percentage of beam, default 0.5) |
---|
899 | lmast: position of the mast (as percentage of length, default 0.6) |
---|
900 | wmast: width of the mast (default 0.1) |
---|
901 | hsd: head sail direction respect to center line (default 5., -999.99 for upwind) |
---|
902 | msd: main sail direction respect to center line (default 5., -999.99 for upwind) |
---|
903 | lheads: length of head sail (as percentage of legnth, defaul 0.38) |
---|
904 | lmains: length of main sail (as percentage of legnth, defaul 0.55) |
---|
905 | """ |
---|
906 | import numpy.ma as ma |
---|
907 | fname = 'zsailing_boat' |
---|
908 | |
---|
909 | bow = np.array([length, 0.]) |
---|
910 | maxportside = np.array([length*lbeam, -beam]) |
---|
911 | maxstarboardside = np.array([length*lbeam, beam]) |
---|
912 | portside = np.array([0., -beam*sternbp]) |
---|
913 | starboardside = np.array([0., beam*sternbp]) |
---|
914 | |
---|
915 | # forward section |
---|
916 | fportside = circ_sec(bow,maxportside, length*2) |
---|
917 | fstarboardside = circ_sec(maxstarboardside, bow, length*2) |
---|
918 | dpts = fportside.shape[0] |
---|
919 | |
---|
920 | # aft section |
---|
921 | aportside = circ_sec(maxportside, portside, length*2) |
---|
922 | astarboardside = circ_sec(starboardside, maxstarboardside, length*2) |
---|
923 | # stern |
---|
924 | stern = circ_sec(portside, starboardside, length*2) |
---|
925 | # mast |
---|
926 | mast = p_circle(wmast,N=dpts) |
---|
927 | mast = mast + [length*lmast, 0.] |
---|
928 | # head sails |
---|
929 | lsail = lheads*length |
---|
930 | if hsd != -999.99: |
---|
931 | sailsa = np.pi/2. - np.pi*hsd/180. |
---|
932 | endsail = np.array([lsail*np.sin(sailsa), lsail*np.cos(sailsa)]) |
---|
933 | endsail[0] = length - endsail[0] |
---|
934 | if bow[1] < endsail[1]: |
---|
935 | hsail = circ_sec(endsail, bow, lsail*2.15) |
---|
936 | else: |
---|
937 | hsail = circ_sec(bow, endsail, lsail*2.15) |
---|
938 | else: |
---|
939 | hsail0 = p_sinusiode(length=lsail, amp=0.2, lamb=0.75, N=dpts) |
---|
940 | hsail = np.zeros((dpts,2), dtype=np.float) |
---|
941 | hsail[:,0] = hsail0[:,1] |
---|
942 | hsail[:,1] = hsail0[:,0] |
---|
943 | hsail = bow - hsail |
---|
944 | |
---|
945 | # main sails |
---|
946 | lsail = lmains*length |
---|
947 | if msd != -999.99: |
---|
948 | sailsa = np.pi/2. - np.pi*msd/180. |
---|
949 | begsail = np.array([length*lmast, 0.]) |
---|
950 | endsail = np.array([lsail*np.sin(sailsa), lsail*np.cos(sailsa)]) |
---|
951 | endsail[0] = length*lmast - endsail[0] |
---|
952 | if endsail[1] < begsail[1]: |
---|
953 | msail = circ_sec(begsail, endsail, lsail*2.15) |
---|
954 | else: |
---|
955 | msail = circ_sec(endsail, begsail, lsail*2.15) |
---|
956 | else: |
---|
957 | msail0 = p_sinusiode(length=lsail, amp=0.25, lamb=1., N=dpts) |
---|
958 | msail = np.zeros((dpts,2), dtype=np.float) |
---|
959 | msail[:,0] = msail0[:,1] |
---|
960 | msail[:,1] = msail0[:,0] |
---|
961 | msail = [length*lmast,0] - msail |
---|
962 | |
---|
963 | sailingboat = np.zeros((dpts*8+4,2), dtype=np.float) |
---|
964 | |
---|
965 | sailingboat[0:dpts,:] = fportside |
---|
966 | sailingboat[dpts:2*dpts,:] = aportside |
---|
967 | sailingboat[2*dpts:3*dpts,:] = stern |
---|
968 | sailingboat[3*dpts:4*dpts,:] = astarboardside |
---|
969 | sailingboat[4*dpts:5*dpts,:] = fstarboardside |
---|
970 | sailingboat[5*dpts,:] = [gen.fillValueF, gen.fillValueF] |
---|
971 | sailingboat[5*dpts+1:6*dpts+1,:] = mast |
---|
972 | sailingboat[6*dpts+1,:] = [gen.fillValueF, gen.fillValueF] |
---|
973 | sailingboat[6*dpts+2:7*dpts+2,:] = hsail |
---|
974 | sailingboat[7*dpts+2,:] = [gen.fillValueF, gen.fillValueF] |
---|
975 | sailingboat[7*dpts+3:8*dpts+3,:] = msail |
---|
976 | sailingboat[8*dpts+3,:] = [gen.fillValueF, gen.fillValueF] |
---|
977 | |
---|
978 | sailingboat = ma.masked_equal(sailingboat, gen.fillValueF) |
---|
979 | |
---|
980 | # Center line extending [fcl] percentage from length on aft and stern |
---|
981 | fcl = 0.15 |
---|
982 | centerline = np.zeros((dpts,2), dtype=np.float) |
---|
983 | dl = length*(1.+fcl*2.)/(dpts-1) |
---|
984 | centerline[:,0] = np.arange(-length*fcl, length*(1. + fcl)+dl, dl) |
---|
985 | |
---|
986 | # correct order of sections |
---|
987 | sailingboatsecs = ['fportside', 'aportside', 'stern', 'astarboardside', \ |
---|
988 | 'fstarboardside', 'mast', 'hsail', 'msail', 'centerline'] |
---|
989 | # dictionary with sections [polygon_vertices, line_type, line_color, line_width] |
---|
990 | dicsailingboat = {'fportside': [fportside, '-', '#8A5900', 2.], \ |
---|
991 | 'aportside': [aportside, '-', '#8A5900', 2.], \ |
---|
992 | 'stern': [stern, '-', '#8A5900', 2.], \ |
---|
993 | 'astarboardside': [astarboardside, '-', '#8A5900', 2.], \ |
---|
994 | 'fstarboardside': [fstarboardside, '-', '#8A5900', 2.], \ |
---|
995 | 'mast': [mast, '-', '#8A5900', 2.], 'hsail': [hsail, '-', '#AAAAAA', 1.], \ |
---|
996 | 'msail': [msail, '-', '#AAAAAA', 1.], \ |
---|
997 | 'centerline': [centerline, '-.', '#AA6464', 1.5]} |
---|
998 | |
---|
999 | fname = 'sailboat_L' + str(int(length*100.)) + '_B' + str(int(beam*100.)) + \ |
---|
1000 | '_lb' + str(int(lbeam*100.)) + '_sb' + str(int(sternbp*100.)) + \ |
---|
1001 | '_lm' + str(int(lmast*100.)) + '_wm' + str(int(wmast)) + \ |
---|
1002 | '_hsd' + str(int(hsd)) + '_hs' + str(int(lheads*100.)) + \ |
---|
1003 | '_ms' + str(int(lheads*100.)) + '_msd' + str(int(msd)) +'.dat' |
---|
1004 | if not os.path.isfile(fname): |
---|
1005 | print infmsg |
---|
1006 | print ' ' + fname + ": writting boat coordinates file '" + fname + "' !!" |
---|
1007 | of = open(fname, 'w') |
---|
1008 | of.write('# boat file with Length: ' + str(length) +' max_beam: '+str(beam)+ \ |
---|
1009 | 'length_at_max_beam:' + str(lbeam) + '% beam at stern: ' + str(sternbp)+ \ |
---|
1010 | ' % mast position: '+ str(lmast) + ' % mast width: ' + str(wmast) + ' ' + \ |
---|
1011 | ' head sail direction:' + str(hsd) + ' head sail length: ' + str(lheads) + \ |
---|
1012 | ' %' + ' main sail length' + str(lmains) + ' main sail direction:' + \ |
---|
1013 | str(msd) +'\n') |
---|
1014 | for ip in range(dpts*5): |
---|
1015 | of.write(str(sailingboat[ip,0]) + ' ' + str(sailingboat[ip,1]) + '\n') |
---|
1016 | |
---|
1017 | of.close() |
---|
1018 | print fname + ": Successfull written '" + fname + "' !!" |
---|
1019 | |
---|
1020 | return sailingboat, sailingboatsecs, dicsailingboat |
---|
1021 | |
---|
1022 | def zisland1(mainpts= np.array([[-0.1,0.], [-1.,1.], [-0.8,1.2], [0.1,0.6], [1., 0.9],\ |
---|
1023 | [2.8, -0.1], [0.1,-0.6]], dtype=np.float), radfrac=3., N=200): |
---|
1024 | """ Function to draw an island from z-axis as the union of a series of points by |
---|
1025 | circular segments |
---|
1026 | mainpts: main points of the island (clockwise ordered, to be joined by |
---|
1027 | circular segments of radii as the radfrac factor of the distance between |
---|
1028 | consecutive points) |
---|
1029 | * default= np.array([[-0.1,0.], [-1.,1.], [-0.8,1.2], [0.1,0.6], [1., 0.9], |
---|
1030 | [2.8, -0.1], [0.1,-0.6]], dtype=np.float) |
---|
1031 | radfrac: multiplicative factor of the distance between consecutive points to |
---|
1032 | draw the circular segment (3., default) |
---|
1033 | N: number of points (200, default) |
---|
1034 | """ |
---|
1035 | import numpy.ma as ma |
---|
1036 | fname = 'zisland1' |
---|
1037 | |
---|
1038 | island1 = np.ones((N,2), dtype=np.float)*gen.fillValueF |
---|
1039 | |
---|
1040 | # Coastline |
---|
1041 | island1 = join_circ_sec_rand(mainpts) |
---|
1042 | |
---|
1043 | islandsecs = ['coastline'] |
---|
1044 | islanddic = {'coastline': [island1, '-', '#161616', 2.]} |
---|
1045 | |
---|
1046 | island1 = ma.masked_equal(island1, gen.fillValueF) |
---|
1047 | |
---|
1048 | return island1, islandsecs, islanddic |
---|
1049 | |
---|
1050 | def buoy1(height=5., width=10., bradii=1.75, bfrac=0.8, N=200): |
---|
1051 | """ Function to draw a buoy as superposition of prism and section of ball |
---|
1052 | height: height of the prism (5., default) |
---|
1053 | width: width of the prism (10., default) |
---|
1054 | bradii: radii of the ball (1.75, default) |
---|
1055 | bfrac: fraction of the ball above the prism (0.8, default) |
---|
1056 | N: total number of points of the buoy (200, default) |
---|
1057 | |
---|
1058 | """ |
---|
1059 | fname = 'buoy1' |
---|
1060 | |
---|
1061 | buoy = np.zeros((N,2), dtype=np.float) |
---|
1062 | |
---|
1063 | NNp = 0 |
---|
1064 | iip = 0 |
---|
1065 | # Base |
---|
1066 | ix = -width/2. |
---|
1067 | iy = 0. |
---|
1068 | Np = 4 |
---|
1069 | dx = width/(Np-1) |
---|
1070 | dy = 0. |
---|
1071 | for ip in range(Np): |
---|
1072 | buoy[iip+ip,:] = [iy+dy*ip,ix+dx*ip] |
---|
1073 | NNp = NNp + Np |
---|
1074 | iip = NNp |
---|
1075 | |
---|
1076 | # right lateral |
---|
1077 | ix = width/2. |
---|
1078 | iy = 0. |
---|
1079 | Np = 2 |
---|
1080 | dx = 0. |
---|
1081 | dy = height/(Np-1) |
---|
1082 | for ip in range(Np): |
---|
1083 | buoy[iip+ip,:] = [iy+dy*ip,ix+dx*ip] |
---|
1084 | NNp = NNp + Np |
---|
1085 | iip = NNp |
---|
1086 | |
---|
1087 | # right upper |
---|
1088 | ix = width/2. |
---|
1089 | iy = height |
---|
1090 | Np = 4 |
---|
1091 | dx = -(width/2.-bradii*bfrac)/(Np-1) |
---|
1092 | dy = 0. |
---|
1093 | for ip in range(Np): |
---|
1094 | buoy[iip+ip,:] = [iy+dy*ip,ix+dx*ip] |
---|
1095 | NNp = NNp + Np |
---|
1096 | iip = NNp |
---|
1097 | |
---|
1098 | # ball |
---|
1099 | p1 = np.array([height, -bradii*bfrac]) |
---|
1100 | p2 = np.array([height, bradii*bfrac]) |
---|
1101 | Np = N - 2*(NNp) |
---|
1102 | buoy[iip:iip+Np,:] = circ_sec(p2, p1, 2.*bradii, 'long', Np) |
---|
1103 | NNp = NNp + Np |
---|
1104 | iip = NNp |
---|
1105 | |
---|
1106 | # left upper |
---|
1107 | ix = -bradii*bfrac |
---|
1108 | iy = height |
---|
1109 | Np = 4 |
---|
1110 | dx = -(width/2.-bradii*bfrac)/(Np-1) |
---|
1111 | dy = 0. |
---|
1112 | for ip in range(Np): |
---|
1113 | buoy[iip+ip,:] = [iy+dy*ip,ix+dx*ip] |
---|
1114 | NNp = NNp + Np |
---|
1115 | iip = NNp |
---|
1116 | |
---|
1117 | # left lateral |
---|
1118 | ix = -width/2. |
---|
1119 | iy = height |
---|
1120 | Np = 2 |
---|
1121 | dx = 0. |
---|
1122 | dy = -height/(Np-1) |
---|
1123 | for ip in range(Np): |
---|
1124 | buoy[iip+ip,:] = [iy+dy*ip,ix+dx*ip] |
---|
1125 | NNp = NNp + Np |
---|
1126 | iip = NNp |
---|
1127 | |
---|
1128 | buoysecs = ['base'] |
---|
1129 | buoydic = {'base': [buoy, '-', 'k', 1.5]} |
---|
1130 | |
---|
1131 | return buoy, buoysecs, buoydic |
---|
1132 | |
---|
1133 | ####### ####### ##### #### ### ## # |
---|
1134 | # Plotting |
---|
1135 | |
---|
1136 | def plot_sphere(iazm=-60., iele=30., dist=10., Npts=100, radii=10, \ |
---|
1137 | drwsfc=[True,True], colsfc=['#AAAAAA','#646464'], \ |
---|
1138 | drwxline = True, linex=[':','b',2.], drwyline = True, liney=[':','r',2.], \ |
---|
1139 | drwzline = True, linez=['-.','g',2.], drwxcline=[True,True], \ |
---|
1140 | linexc=[['-','#646400',1.],['--','#646400',1.]], \ |
---|
1141 | drwequator=[True,True], lineeq=[['-','#AA00AA',1.],['--','#AA00AA',1.]], \ |
---|
1142 | drwgreeenwhich=[True,True], linegw=[['-','k',1.],['--','k',1.]]): |
---|
1143 | """ Function to plot an sphere and determine which standard lines will be also |
---|
1144 | drawn |
---|
1145 | iazm: azimut of the camera form the sphere |
---|
1146 | iele: elevation of the camera form the sphere |
---|
1147 | dist: distance of the camera form the sphere |
---|
1148 | Npts: Resolution for the sphere |
---|
1149 | radii: radius of the sphere |
---|
1150 | drwsfc: whether 'up' and 'down' portions of the sphere should be drawn |
---|
1151 | colsfc: colors of the surface of the sphere portions ['up', 'down'] |
---|
1152 | drwxline: whether x-axis line should be drawn |
---|
1153 | linex: properties of the x-axis line ['type', 'color', 'wdith'] |
---|
1154 | drwyline: whether y-axis line should be drawn |
---|
1155 | liney: properties of the y-axis line ['type', 'color', 'wdith'] |
---|
1156 | drwzline: whether z-axis line should be drawn |
---|
1157 | linez: properties of the z-axis line ['type', 'color', 'wdith'] |
---|
1158 | drwequator: whether 'front' and 'back' portions of the Equator should be drawn |
---|
1159 | lineeq: properties of the lines 'front' and 'back' of the Equator |
---|
1160 | drwgreeenwhich: whether 'front', 'back' portions of Greenqhich should be drawn |
---|
1161 | linegw: properties of the lines 'front' and 'back' Greenwhich |
---|
1162 | drwxcline: whether 'front', 'back' 90 line (lon=90., lon=270.) should be drawn |
---|
1163 | linexc: properties of the lines 'front' and 'back' for the 90 line |
---|
1164 | """ |
---|
1165 | fname = 'plot_sphere' |
---|
1166 | |
---|
1167 | iazmrad = iazm*np.pi/180. |
---|
1168 | ielerad = iele*np.pi/180. |
---|
1169 | |
---|
1170 | # 3D surface Sphere |
---|
1171 | sfcsphereu, sfcsphered = surface_sphere(radii,Npts) |
---|
1172 | |
---|
1173 | # greenwhich |
---|
1174 | if iazmrad > np.pi/2. and iazmrad < 3.*np.pi/2.: |
---|
1175 | ia=np.pi-ielerad |
---|
1176 | else: |
---|
1177 | ia=0.-ielerad |
---|
1178 | ea=ia+np.pi |
---|
1179 | da = (ea-ia)/(Npts-1) |
---|
1180 | beta = np.arange(ia,ea+da,da)[0:Npts] |
---|
1181 | alpha = np.zeros((Npts), dtype=np.float) |
---|
1182 | greenwhichc = spheric_line(radii,alpha,beta) |
---|
1183 | ia=ea+0. |
---|
1184 | ea=ia+np.pi |
---|
1185 | da = (ea-ia)/(Npts-1) |
---|
1186 | beta = np.arange(ia,ea+da,da)[0:Npts] |
---|
1187 | greenwhichd = spheric_line(radii,alpha,beta) |
---|
1188 | |
---|
1189 | # Equator |
---|
1190 | ia=np.pi-iazmrad/2. |
---|
1191 | ea=ia+np.pi |
---|
1192 | da = (ea-ia)/(Npts-1) |
---|
1193 | alpha = np.arange(ia,ea+da,da)[0:Npts] |
---|
1194 | beta = np.zeros((Npts), dtype=np.float) |
---|
1195 | equatorc = spheric_line(radii,alpha,beta) |
---|
1196 | ia=ea+0. |
---|
1197 | ea=ia+np.pi |
---|
1198 | da = (ea-ia)/(Npts-1) |
---|
1199 | alpha = np.arange(ia,ea+da,da)[0:Npts] |
---|
1200 | equatord = spheric_line(radii,alpha,beta) |
---|
1201 | |
---|
1202 | # 90 line |
---|
1203 | if iazmrad > np.pi and iazmrad < 2.*np.pi: |
---|
1204 | ia=3.*np.pi/2. + ielerad |
---|
1205 | else: |
---|
1206 | ia=np.pi/2. - ielerad |
---|
1207 | if ielerad < 0.: |
---|
1208 | ia = ia + np.pi |
---|
1209 | ea=ia+np.pi |
---|
1210 | da = (ea-ia)/(Npts-1) |
---|
1211 | beta = np.arange(ia,ea+da,da)[0:Npts] |
---|
1212 | alpha = np.ones((Npts), dtype=np.float)*np.pi/2. |
---|
1213 | xclinec = spheric_line(radii,alpha,beta) |
---|
1214 | ia=ea+0. |
---|
1215 | ea=ia+np.pi |
---|
1216 | da = (ea-ia)/(Npts-1) |
---|
1217 | beta = np.arange(ia,ea+da,da)[0:Npts] |
---|
1218 | xclined = spheric_line(radii,alpha,beta) |
---|
1219 | |
---|
1220 | # x line |
---|
1221 | xline = np.zeros((2,3), dtype=np.float) |
---|
1222 | xline[0,:] = position_sphere(radii, 0., 0.) |
---|
1223 | xline[1,:] = position_sphere(radii, np.pi, 0.) |
---|
1224 | |
---|
1225 | # y line |
---|
1226 | yline = np.zeros((2,3), dtype=np.float) |
---|
1227 | yline[0,:] = position_sphere(radii, np.pi/2., 0.) |
---|
1228 | yline[1,:] = position_sphere(radii, 3*np.pi/2., 0.) |
---|
1229 | |
---|
1230 | # z line |
---|
1231 | zline = np.zeros((2,3), dtype=np.float) |
---|
1232 | zline[0,:] = position_sphere(radii, 0., np.pi/2.) |
---|
1233 | zline[1,:] = position_sphere(radii, 0., -np.pi/2.) |
---|
1234 | |
---|
1235 | fig = plt.figure() |
---|
1236 | ax = fig.gca(projection='3d') |
---|
1237 | |
---|
1238 | # Sphere surface |
---|
1239 | if drwsfc[0]: |
---|
1240 | ax.plot_surface(sfcsphereu[0,:,:], sfcsphereu[1,:,:], sfcsphereu[2,:,:], \ |
---|
1241 | color=colsfc[0]) |
---|
1242 | if drwsfc[1]: |
---|
1243 | ax.plot_surface(sfcsphered[0,:,:], sfcsphered[1,:,:], sfcsphered[2,:,:], \ |
---|
1244 | color=colsfc[1]) |
---|
1245 | |
---|
1246 | # greenwhich |
---|
1247 | linev = linegw[0] |
---|
1248 | if drwgreeenwhich[0]: |
---|
1249 | ax.plot(greenwhichc[:,0], greenwhichc[:,1], greenwhichc[:,2], linev[0], \ |
---|
1250 | color=linev[1], linewidth=linev[2], label='Greenwich') |
---|
1251 | linev = linegw[1] |
---|
1252 | if drwgreeenwhich[1]: |
---|
1253 | ax.plot(greenwhichd[:,0], greenwhichd[:,1], greenwhichd[:,2], linev[0], \ |
---|
1254 | color=linev[1], linewidth=linev[2]) |
---|
1255 | |
---|
1256 | # Equator |
---|
1257 | linev = lineeq[0] |
---|
1258 | if drwequator[0]: |
---|
1259 | ax.plot(equatorc[:,0], equatorc[:,1], equatorc[:,2], linev[0], \ |
---|
1260 | color=linev[1], linewidth=linev[2], label='Equator') |
---|
1261 | linev = lineeq[1] |
---|
1262 | if drwequator[1]: |
---|
1263 | ax.plot(equatord[:,0], equatord[:,1], equatord[:,2], linev[0], \ |
---|
1264 | color=linev[1], linewidth=linev[2]) |
---|
1265 | |
---|
1266 | # 90line |
---|
1267 | linev = linexc[0] |
---|
1268 | if drwxcline[0]: |
---|
1269 | ax.plot(xclinec[:,0], xclinec[:,1], xclinec[:,2], linev[0], color=linev[1], \ |
---|
1270 | linewidth=linev[2], label='90-line') |
---|
1271 | linev = linexc[1] |
---|
1272 | if drwxcline[1]: |
---|
1273 | ax.plot(xclined[:,0], xclined[:,1], xclined[:,2], linev[0], color=linev[1], \ |
---|
1274 | linewidth=linev[2]) |
---|
1275 | |
---|
1276 | # x line |
---|
1277 | linev = linex |
---|
1278 | if drwxline: |
---|
1279 | ax.plot([xline[0,0],xline[1,0]], [xline[0,1],xline[1,1]], \ |
---|
1280 | [xline[0,2],xline[1,2]], linev[0], color=linev[1], linewidth=linev[2], label='xline') |
---|
1281 | |
---|
1282 | # y line |
---|
1283 | linev = liney |
---|
1284 | if drwyline: |
---|
1285 | ax.plot([yline[0,0],yline[1,0]], [yline[0,1],yline[1,1]], \ |
---|
1286 | [yline[0,2],yline[1,2]], linev[0], color=linev[1], linewidth=linev[2], label='yline') |
---|
1287 | |
---|
1288 | # z line |
---|
1289 | linev = linez |
---|
1290 | if drwzline: |
---|
1291 | ax.plot([zline[0,0],zline[1,0]], [zline[0,1],zline[1,1]], \ |
---|
1292 | [zline[0,2],zline[1,2]], linev[0], color=linev[1], linewidth=linev[2], label='zline') |
---|
1293 | |
---|
1294 | plt.legend() |
---|
1295 | |
---|
1296 | return fig, ax |
---|