Skip to content

Instantly share code, notes, and snippets.

@mikedh
Created December 6, 2018 17:51
Show Gist options
  • Save mikedh/1ec968830eeed591989fd962aead2369 to your computer and use it in GitHub Desktop.
Save mikedh/1ec968830eeed591989fd962aead2369 to your computer and use it in GitHub Desktop.
import numpy as np
import trimesh
import timeit
import time
from collections import deque
import matplotlib.pyplot as plt
from scipy.spatial import cKDTree as KDTree
from shapely.geometry import Polygon, Point, LineString
from trimesh import util, grouping
from trimesh.constants import tol
from scipy.sparse import coo_matrix
from scipy.sparse import csgraph
from scipy import spatial
import trimesh.transformations as tr
import networkx as nx
import numpy as np
import os
import subprocess
def tile(a, b):
dim = a.shape[1]
v_tile = np.tile(b, len(a)).reshape((-1, dim))
# tile a per voronoi vertex
p_tile = np.tile(a, (len(b), 1)).reshape((-1, dim))
r2 = ((p_tile - v_tile)**2).sum(axis=1).reshape((len(b), -1)).max(axis=1)
return r2
def cdist(a, b):
radii_c = spatial.distance.cdist(
b, a, metric='sqeuclidean').max(axis=1)
return radii_c
def loop(a, b):
radii_b = np.array([((a - v)**2).sum(axis=1).max()
for v in b])
return radii_b
if __name__ == '__main__':
trimesh.util.attach_to_log()
m = trimesh.load('models/fuze.obj')
b = trimesh.load('models/fuze.zip')
globs = {'cdist': cdist,
'loop': loop,
'tile': tile,
'np': np}
for s in [(10, 10), (10, 100), (100, 10),
(1000, 1000), (10000, 1000), (10000, 10000)]:
a = np.random.random((s[0], 3))
b = np.random.random((s[1], 3))
setup = 'a=np.random.random(({},3));'
setup += 'b=np.random.random(({},3))'
setup = setup.format(*s)
r = []; tic=[]; label = []
for func in [tile, cdist, loop]:
label.append(func.__name__)
stmt = '{}(a,b)'.format(func.__name__)
try:
tic.append(min(timeit.repeat(stmt,
setup=setup,
number=3,
globals=globs)))
except MemoryError:
tic.append(np.inf)
continue
r.append(func(a,b))
# results should be the same
assert np.array(r).ptp(axis=0).max() < 1e-10
print('\na.shape: {} b.shape: {}'.format(a.shape, b.shape))
print('\n'.join('{}: {}'.format(l, t) for t,l in zip(tic, label)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment