feat: more sane arguments

This commit is contained in:
s3lph 2024-12-18 06:52:39 +01:00
parent 4f0d3ce83d
commit 6398b4f987
4 changed files with 51 additions and 18 deletions

45
map.py
View file

@ -1,9 +1,11 @@
from math import * import argparse
import numpy as np
import sys
import json import json
from math import *
import os
import sys
import numpy as np
from tqdm import tqdm, trange from tqdm import tqdm, trange
@ -108,22 +110,17 @@ def map_poly(tri, poly, ref=None):
return mapped return mapped
if __name__ == '__main__': def main(ns):
fs = [Face([a, b, c, d]) for a, b, c, d in [ with open(ns.faces, 'r') as f:
(np.array([1,1,1]), np.array([-1,1,1]), np.array([-1,-1,1]), np.array([1,-1,1])), # top
(np.array([1,1,-1]), np.array([-1,1,-1]), np.array([-1,-1,-1]), np.array([1,-1,-1])), # bottom
(np.array([-1,-1,-1]), np.array([-1,1,-1]), np.array([-1,1,1]), np.array([-1,-1,1])), # left
(np.array([1,-1,-1]), np.array([1,1,-1]), np.array([1,1,1]), np.array([1,-1,1])), # right
(np.array([-1,-1,-1]), np.array([1,-1,-1]), np.array([1,-1,1]), np.array([-1,-1,1])), # front
(np.array([-1,1,-1]), np.array([1,1,-1]), np.array([1,1,1]), np.array([-1,1,1])), # back
]]
with open('countries.geojson', 'r') as f:
j = json.load(f) j = json.load(f)
faces = [Face([np.array(v)*ns.scale for v in vs]) for vs in j]
with open(ns.geojson, 'r') as f:
geojson = json.load(f)
for i, face in tqdm(list(enumerate(fs)), desc='Faces '): for i, face in tqdm(list(enumerate(faces)), desc='Faces '):
countries = {} countries = {}
for t in face.tris: for t in face.tris:
for f in tqdm(j['features'], desc='Features', total=len(face.tris)*len(j['features'])): for f in tqdm(geojson['features'], desc='Features', total=len(face.tris)*len(geojson['features'])):
cc = f['properties']['ADMIN'] cc = f['properties']['ADMIN']
if f['geometry']['type'] == 'MultiPolygon': if f['geometry']['type'] == 'MultiPolygon':
for poly in f['geometry']['coordinates']: for poly in f['geometry']['coordinates']:
@ -136,9 +133,11 @@ if __name__ == '__main__':
maxy = max(v[1] for v in face.plane_coords) maxy = max(v[1] for v in face.plane_coords)
w = maxx - minx w = maxx - minx
h = maxy - miny h = maxy - miny
with open(f'face{i}.svg', 'w') as svg: outfile = ns.out.format(faces=os.path.basename(ns.faces).rsplit('.', 1)[0], geojson=ns.geojson, face=i)
os.makedirs(os.path.dirname(outfile), exist_ok=True)
with open(outfile, 'w') as svg:
svg.write(f'<svg xmlns="http://www.w3.org/2000/svg" viewBox="{minx} {miny} {w} {h}" width="{w}" height="{h}" id="face{i}">\n') svg.write(f'<svg xmlns="http://www.w3.org/2000/svg" viewBox="{minx} {miny} {w} {h}" width="{w}" height="{h}" id="face{i}">\n')
svg.write(f' <g id="cut">\n <path id="outline" fill="none" stroke="black" stroke-width="0.01" d="M {face.plane_coords[0][0]} {face.plane_coords[0][1]}') svg.write(f' <g id="cut">\n <path id="outline" fill="none" stroke="black" stroke-width="{0.01*ns.scale}" d="M {face.plane_coords[0][0]} {face.plane_coords[0][1]}')
for v in face.plane_coords[1:]: for v in face.plane_coords[1:]:
svg.write(f' L {v[0]} {v[1]}') svg.write(f' L {v[0]} {v[1]}')
svg.write(' Z" />\n </g>\n <g id="engrave">\n') svg.write(' Z" />\n </g>\n <g id="engrave">\n')
@ -146,9 +145,19 @@ if __name__ == '__main__':
if l: if l:
svg.write(f' <g id="{c}">\n') svg.write(f' <g id="{c}">\n')
for ls in l: for ls in l:
svg.write(f' <path fill="none" stroke="red" stroke-width="0.01" d="M {ls[0][0]} {ls[0][1]}') svg.write(f' <path fill="none" stroke="red" stroke-width="{0.01*ns.scale}" d="M {ls[0][0]} {ls[0][1]}')
for x, y in ls[1:]: for x, y in ls[1:]:
svg.write(f' L {x} {y}') svg.write(f' L {x} {y}')
svg.write('" />\n') svg.write('" />\n')
svg.write(' </g>\n') svg.write(' </g>\n')
svg.write(' </g>\n</svg>\n') svg.write(' </g>\n</svg>\n')
if __name__ == '__main__':
ap = argparse.ArgumentParser()
ap.add_argument('--geojson', '-g', default='countries.geojson')
ap.add_argument('--faces', '-f', default='shapes/cube.json')
ap.add_argument('--out', '-o', default='faces/{faces}/face{face}.svg')
ap.add_argument('--scale', '-s', type=int, default=1)
ns = ap.parse_args()
main(ns)

8
shapes/cube.json Normal file
View file

@ -0,0 +1,8 @@
[
[[1,1,1], [-1,1,1], [-1,-1,1], [1,-1,1]],
[[1,1,-1], [-1,1,-1], [-1,-1,-1], [1,-1,-1]],
[[-1,-1,-1], [-1,1,-1], [-1,1,1], [-1,-1,1]],
[[1,-1,-1], [1,1,-1], [1,1,1], [1,-1,1]],
[[-1,-1,-1], [1,-1,-1], [1,-1,1], [-1,-1,1]],
[[-1,1,-1], [1,1,-1], [1,1,1], [-1,1,1]]
]

10
shapes/octahedron.json Normal file
View file

@ -0,0 +1,10 @@
[
[[0,0,1], [-1,0,0], [0,-1,0]],
[[0,0,1], [0,-1,0], [1,0,0]],
[[0,0,1], [1,0,0], [0,1,0]],
[[0,0,1], [0,1,0], [-1,0,0]],
[[0,0,-1], [-1,0,0], [0,-1,0]],
[[0,0,-1], [0,-1,0], [1,0,0]],
[[0,0,-1], [1,0,0], [0,1,0]],
[[0,0,-1], [0,1,0], [-1,0,0]]
]

6
shapes/tetrahedron.json Normal file
View file

@ -0,0 +1,6 @@
[
[[1,1,1], [1,-1,-1], [-1,1,-1]],
[[1,1,1], [1,-1,-1], [-1,-1,1]],
[[1,1,1], [-1,1,-1], [-1,-1,1]],
[[1,-1,-1], [-1,1,-1], [-1,-1,1]]
]