def clickedMatching(): fs_rateFirst, signalFirst = wavfile.read("output1.wav") fs_rateSecond, signalSecond = wavfile.read("output2.wav") print("Frequency sampling", fs_rateFirst, ",", fs_rateSecond) l_audioFirst = len(signalFirst.shape) l_audioSecond = len(signalSecond.shape) print("Channels", l_audioFirst) if l_audioFirst == 2 & l_audioFirst == 2: signalFirst = signalFirst.sum(axis=1) / 2 signalSecond = signalSecond.sum(axis=1) / 2 FFTFirst = abs(scipy.fft.rfft(signalFirst)) FFTSecond = abs(scipy.fft.rfft(signalSecond)) norm1 = np.round_(vg.normalize(FFTFirst), 7) norm2 = np.round_(vg.normalize(FFTSecond), 7) tani = np.round_(tanimoto( norm1, norm2, ), 3) livenshtein = Levenshtein( norm1, norm2, ) messagebox.showinfo( "Результаты сравнения", "Коэффициент Танимото:" + str(tani * 100) + "%" + "\n" + "\n" + "Расстояние Левенштейна:" + str(livenshtein) + "%") print("Коэффициент Танимото:", tani * 100, "%") print("Расстояние Левенштейна:", livenshtein, "%")
def appearanceAffinity(d1, d2): # max[1 - a d(f_1, f_2), 0] # d(.) can be the scalar product of descriptor vectors ALFA = 1 flatten = itertools.chain.from_iterable d1_desc = [k for [k, v] in d1['joints'].items()] d2_desc = [k for [k, v] in d2['joints'].items()] # compute the set of common keys common = list(set(d1_desc).intersection(set(d2_desc))) # # 6 is the position of desc values a = vg.normalize( np.array( list( flatten([ v[6] for [k, v] in d1['joints'].items() if k in common ])))) b = vg.normalize( np.array( list( flatten([ v[6] for [k, v] in d2['joints'].items() if k in common ])))) try: r = np.fabs(np.einsum('ij, ij->i', a, b)) return np.median(r) except: #print("wrong eisum") return 0
def test_project_to_line(): p1 = np.array([5.0, 5.0, 4.0]) p2 = np.array([10.0, 10.0, 6.0]) along_line = p2 - p1 common_kwargs = dict(reference_points_of_lines=p1, vectors_along_lines=along_line) np.testing.assert_array_almost_equal( project_to_line(points=p1, **common_kwargs), p1) np.testing.assert_array_almost_equal( project_to_line(points=p2, **common_kwargs), p2) other_point_on_line = np.array([0.0, 0.0, 2.0]) np.testing.assert_array_almost_equal( project_to_line(points=other_point_on_line, **common_kwargs), other_point_on_line, ) example_perpendicular_displacement = [ k * vg.perpendicular(vg.normalize(along_line), vg.basis.x) for k in [0.1, 0.5, -2.0] ] for point_on_line in [p1, p2, other_point_on_line]: for displacement in example_perpendicular_displacement: np.testing.assert_array_almost_equal( project_to_line(points=point_on_line + displacement, **common_kwargs), point_on_line, )
def test_project_to_line_stacked_both(): p1 = np.array([5.0, 5.0, 4.0]) p2 = np.array([10.0, 10.0, 6.0]) along_line = p2 - p1 common_kwargs = dict( reference_points_of_lines=np.array([p1, p1, p1]), vectors_along_lines=np.array([along_line, along_line, along_line]), ) other_point_on_line = np.array([0.0, 0.0, 2.0]) example_perpendicular_displacement = [ k * vg.perpendicular(vg.normalize(along_line), vg.basis.x) for k in [0.1, 0.5, -2.0] ] example_points = np.vstack([p1, p2, other_point_on_line]) expected_projected_points = np.vstack([p1, p2, other_point_on_line]) np.testing.assert_array_almost_equal( project_to_line(points=example_points, **common_kwargs), expected_projected_points, ) np.testing.assert_array_almost_equal( project_to_line(points=example_points + example_perpendicular_displacement, **common_kwargs), expected_projected_points, )
def percentile(points, axis, percentile): """ Given a cloud of points and an axis, find a point along that axis from the centroid at the given percentile. Args: points (np.arraylike): A `kx3` stack of points. axis (np.arraylike): A 3D vector specifying the direction of interest. percentile (float): The desired percentile. Returns: np.ndarray: A 3D point at the requested percentile. """ k = vg.shape.check(locals(), "points", (-1, 3)) if k < 1: raise ValueError("At least one point is needed") vg.shape.check(locals(), "axis", (3, )) if vg.almost_zero(axis): raise ValueError("Axis must be non-zero") axis = vg.normalize(axis) coords_on_axis = points.dot(axis) selected_coord_on_axis = np.percentile(coords_on_axis, percentile) centroid = np.average(points, axis=0) return vg.reject(centroid, axis) + selected_coord_on_axis * axis
def __init__(self, point_on_plane, unit_normal): vg.shape.check(locals(), "point_on_plane", (3,)) vg.shape.check(locals(), "unit_normal", (3,)) if vg.almost_zero(unit_normal): raise ValueError("unit_normal should not be the zero vector") unit_normal = vg.normalize(unit_normal) self._r0 = np.asarray(point_on_plane) self._n = np.asarray(unit_normal)
def surface_normal(points, normalize=True): """ Compute the surface normal of a triangle. Can provide three points or an array of points. """ p1 = points[..., 0, :] p2 = points[..., 1, :] p3 = points[..., 2, :] normal = np.cross(p2 - p1, p3 - p1) return vg.normalize(normal) if normalize else normal
def test_sliced_by_plane_open(): original = Polyline( np.array( [ [0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [1.0, 1.0, 0.0], [1.0, 7.0, 0.0], [1.0, 8.0, 0.0], ] ), is_closed=False, ) expected_vs = np.array([[1.0, 7.5, 0.0], [1.0, 8.0, 0.0]]) actual = original.sliced_by_plane( Plane(point_on_plane=np.array([0.0, 7.5, 0.0]), unit_normal=vg.basis.y) ) np.testing.assert_array_almost_equal(actual.v, expected_vs) assert actual.is_closed is False expected_vs = np.array( [ [0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [1.0, 1.0, 0.0], [1.0, 7.0, 0.0], [1.0, 7.5, 0.0], ] ) actual = original.sliced_by_plane( Plane(point_on_plane=np.array([0.0, 7.5, 0.0]), unit_normal=vg.basis.neg_y) ) np.testing.assert_array_almost_equal(actual.v, expected_vs) assert actual.is_closed is False with pytest.raises(ValueError): original.sliced_by_plane( Plane(point_on_plane=np.array([0.0, 15.0, 0.0]), unit_normal=vg.basis.neg_y) ) actual = original.sliced_by_plane( Plane( point_on_plane=np.array([0.5, 0.0, 0.0]), unit_normal=vg.normalize(np.array([1.0, -1.0, 0.0])), ) ) expected_vs = np.array([[0.5, 0.0, 0.0], [1.0, 0.0, 0.0], [1.0, 0.5, 0.0]]) np.testing.assert_array_almost_equal(actual.v, expected_vs) assert actual.is_closed is False
def test_render_longest_xsection_to_svg(): mesh = lacecore.load_obj( "examples/vitra/vitra_without_materials.obj", triangulate=True ) plane = Plane( point_on_plane=np.array([-0.869231, 60.8882, -20.1071]), unit_normal=vg.normalize(np.array([0.0, 0.1, -1.0])), ) xs = render_longest_xsection_to_svg( mesh=mesh, plane=plane, filename="vitra_cross_section.svg" ) Scene().add_meshes(mesh).add_lines(xs).write("vitra_with_cross_section.dae")
def test_signed_distances_for_diagonal_plane(): np.testing.assert_array_almost_equal( signed_distance_to_plane( points=np.array([[425.0, 425.0, 25.0], [-500.0, -500.0, 25.0]]), # Diagonal plane @ origin - draw a picture! plane_equations=Plane( point_on_plane=np.array([1.0, 1.0, 0.0]), unit_normal=vg.normalize(np.array([1.0, 1.0, 0.0])), ).equation, ), np.array([ math.sqrt(2 * (425.0 - 1.0)**2), -math.sqrt(2 * (500.0 + 1.0)**2) ]), )
def world_to_view(position, target, up=vg.basis.y, inverse=False): """ Create a transform matrix which sends world-space coordinates to view-space coordinates. Args: position (np.ndarray): The camera's position in world coordinates. target (np.ndarray): The camera's target in world coordinates. `target - position` is the "look at" vector. up (np.ndarray): The approximate up direction, in world coordinates. inverse (bool): When `True`, return the inverse transform instead. Returns: np.ndarray: The `4x4` transformation matrix, which can be used with `polliwog.transform.apply_transform()`. See also: https://cseweb.ucsd.edu/classes/wi18/cse167-a/lec4.pdf http://www.songho.ca/opengl/gl_camera.html """ vg.shape.check(locals(), "position", (3, )) vg.shape.check(locals(), "target", (3, )) look = vg.normalize(target - position) left = vg.normalize(vg.cross(look, up)) recomputed_up = vg.cross(left, look) rotation = transform_matrix_for_rotation( np.array([left, recomputed_up, look])) if inverse: inverse_rotation = rotation.T inverse_translation = transform_matrix_for_translation(position) return compose_transforms(inverse_rotation, inverse_translation) else: translation = transform_matrix_for_translation(-position) return compose_transforms(translation, rotation)
def plane_normal_from_points(points): """ Given a set of three points, compute the normal of the plane which passes through them. Also works on stacked inputs (i.e. many sets of three points). """ points, _, transform_result = columnize(points, (-1, 3, 3), name="points") p1s = points[:, 0] p2s = points[:, 1] p3s = points[:, 2] v1s = p2s - p1s v2s = p3s - p1s unit_normals = vg.normalize(vg.cross(v1s, v2s)) return transform_result(unit_normals)
def _rotmat(self, vector, points): """ Rotates a 3xn array of 3D coordinates from the +z normal to an arbitrary new normal vector. """ vector = vg.normalize(vector) axis = vg.perpendicular(vg.basis.z, vector) angle = vg.angle(vg.basis.z, vector, units='rad') a = np.hstack((axis, (angle, ))) R = matrix_from_axis_angle(a) r = Rot.from_matrix(R) rotmat = r.apply(points) return rotmat
def test_surface_normals_from_points_vectorized(): from ..shapes import triangular_prism p1 = np.array([3.0, 0.0, 0.0]) p2 = np.array([0.0, 3.0, 0.0]) p3 = np.array([0.0, 0.0, 3.0]) vertices = triangular_prism(p1, p2, p3, 1.0) expected_normals = vg.normalize( np.array([ [1.0, 1.0, 1.0], [1.0, 1.0, -2.0], [1.0, 1.0, -2.0], [-2.0, 1.0, 1.0], [-2.0, 1.0, 1.0], [1.0, -2.0, 1.0], [1.0, -2.0, 1.0], [-1.0, -1.0, -1.0], ])) np.testing.assert_allclose(surface_normals(vertices), expected_normals)
def surface_normals(points, normalize=True): """ Compute the surface normal of a triangle. The direction of the normal follows conventional counter-clockwise winding and the right-hand rule. Also works on stacked inputs (i.e. many sets of three points). """ points, _, transform_result = columnize(points, (-1, 3, 3), name="points") p1s = points[:, 0] p2s = points[:, 1] p3s = points[:, 2] v1s = p2s - p1s v2s = p3s - p1s normals = vg.cross(v1s, v2s) if normalize: normals = vg.normalize(normals) return transform_result(normals)
def normalized(self) -> "Vector": """A vector parallel to this one, with a magnitude of 1.0""" return self.from_components(vg.normalize(self.array))
def test_normalize_stacked(): vs = np.array([[1, 1, 0], [-1, 0, 0], [0, 0, 5]]) expected = np.array( [[math.sqrt(2) / 2.0, math.sqrt(2) / 2.0, 0], [-1, 0, 0], [0, 0, 1]] ) np.testing.assert_array_almost_equal(vg.normalize(vs), expected)
def test_normalized_wrong_dim(): with pytest.raises(ValueError, match="Not sure what to do with 3 dimensions"): vg.normalize(np.array([[[1, 1, 0], [0, 1, 0]], [[0, 0, 0], [0, 1, 0]]]))
def test_normalize(): v = np.array([1, 1, 0]) expected = np.array([math.sqrt(2) / 2.0, math.sqrt(2) / 2.0, 0]) np.testing.assert_array_almost_equal(vg.normalize(v), expected)
import numpy as np import vg import pytest from ..plane.plane import Plane from .cut_by_plane import cut_open_polyline_by_plane point_on_plane = np.array([1.0, 2.0, 3.0]) plane_normal = vg.normalize(np.array([3.0, 4.0, 5.0])) plane = Plane(point_on_plane=point_on_plane, unit_normal=plane_normal) def rand_nonzero(*shape): return 128 * np.random.rand(*shape) + 1e-6 def vertices_with_signs(signs): num_verts = len(signs) random_points_on_plane = plane.project_point(rand_nonzero(num_verts, 3)) random_displacement_along_normal = ( rand_nonzero(num_verts).reshape(-1, 1) * plane_normal) vertices = (random_points_on_plane + signs.reshape(-1, 1) * random_displacement_along_normal) # Because of rounding, the random points don't necessarily return 0 for # sign, so pick one that does. vertices[signs == 0] = plane.reference_point np.testing.assert_array_equal(plane.sign(vertices), signs) return vertices def intersect_segment_with_plane(p1, p2): from ..plane.intersections import (