/
space_ops.py
827 lines (682 loc) · 22.1 KB
/
space_ops.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
"""Utility functions for two- and three-dimensional vectors."""
__all__ = [
"quaternion_mult",
"quaternion_from_angle_axis",
"angle_axis_from_quaternion",
"quaternion_conjugate",
"rotate_vector",
"thick_diagonal",
"rotation_matrix",
"rotation_about_z",
"z_to_vector",
"angle_of_vector",
"angle_between_vectors",
"project_along_vector",
"normalize",
"get_unit_normal",
"compass_directions",
"regular_vertices",
"complex_to_R3",
"R3_to_complex",
"complex_func_to_R3_func",
"center_of_mass",
"midpoint",
"find_intersection",
"line_intersection",
"get_winding_number",
"cross2d",
"earclip_triangulation",
"cartesian_to_spherical",
"spherical_to_cartesian",
"perpendicular_bisector",
]
import itertools as it
import math
from functools import reduce
from typing import List, Optional, Sequence, Tuple, Union
import numpy as np
from mapbox_earcut import triangulate_float32 as earcut
from .. import config
from ..constants import DOWN, OUT, PI, RIGHT, TAU
from ..utils.iterables import adjacent_pairs
def norm_squared(v: float) -> float:
return np.dot(v, v)
# Quaternions
# TODO, implement quaternion type
def quaternion_mult(
*quats: Sequence[float],
) -> Union[np.ndarray, List[Union[float, np.ndarray]]]:
"""Gets the Hamilton product of the quaternions provided.
For more information, check `this Wikipedia page
<https://en.wikipedia.org/wiki/Quaternion>`__.
Returns
-------
Union[np.ndarray, List[Union[float, np.ndarray]]]
Returns a list of product of two quaternions.
"""
if config.renderer == "opengl":
if len(quats) == 0:
return [1, 0, 0, 0]
result = quats[0]
for next_quat in quats[1:]:
w1, x1, y1, z1 = result
w2, x2, y2, z2 = next_quat
result = [
w1 * w2 - x1 * x2 - y1 * y2 - z1 * z2,
w1 * x2 + x1 * w2 + y1 * z2 - z1 * y2,
w1 * y2 + y1 * w2 + z1 * x2 - x1 * z2,
w1 * z2 + z1 * w2 + x1 * y2 - y1 * x2,
]
return result
else:
q1 = quats[0]
q2 = quats[1]
w1, x1, y1, z1 = q1
w2, x2, y2, z2 = q2
return np.array(
[
w1 * w2 - x1 * x2 - y1 * y2 - z1 * z2,
w1 * x2 + x1 * w2 + y1 * z2 - z1 * y2,
w1 * y2 + y1 * w2 + z1 * x2 - x1 * z2,
w1 * z2 + z1 * w2 + x1 * y2 - y1 * x2,
],
)
def quaternion_from_angle_axis(
angle: float,
axis: np.ndarray,
axis_normalized: bool = False,
) -> List[float]:
"""Gets a quaternion from an angle and an axis.
For more information, check `this Wikipedia page
<https://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles>`__.
Parameters
----------
angle
The angle for the quaternion.
axis
The axis for the quaternion
axis_normalized : bool, optional
Checks whether the axis is normalized, by default False
Returns
-------
List[float]
Gives back a quaternion from the angle and axis
"""
if config.renderer == "opengl":
if not axis_normalized:
axis = normalize(axis)
return [math.cos(angle / 2), *(math.sin(angle / 2) * axis)]
else:
return np.append(np.cos(angle / 2), np.sin(angle / 2) * normalize(axis))
def angle_axis_from_quaternion(quaternion: Sequence[float]) -> Sequence[float]:
"""Gets angle and axis from a quaternion.
Parameters
----------
quaternion
The quaternion from which we get the angle and axis.
Returns
-------
Sequence[float]
Gives the angle and axis
"""
axis = normalize(quaternion[1:], fall_back=np.array([1, 0, 0]))
angle = 2 * np.arccos(quaternion[0])
if angle > TAU / 2:
angle = TAU - angle
return angle, axis
def quaternion_conjugate(quaternion: Sequence[float]) -> np.ndarray:
"""Used for finding the conjugate of the quaternion
Parameters
----------
quaternion
The quaternion for which you want to find the conjugate for.
Returns
-------
np.ndarray
The conjugate of the quaternion.
"""
result = np.array(quaternion)
result[1:] *= -1
return result
def rotate_vector(vector: np.ndarray, angle: int, axis: np.ndarray = OUT) -> np.ndarray:
"""Function for rotating a vector.
Parameters
----------
vector
The vector to be rotated.
angle
The angle to be rotated by.
axis
The axis to be rotated, by default OUT
Returns
-------
np.ndarray
The rotated vector with provided angle and axis.
Raises
------
ValueError
If vector is not of dimension 2 or 3.
"""
if len(vector) == 2:
# Use complex numbers...because why not
z = complex(*vector) * np.exp(complex(0, angle))
return np.array([z.real, z.imag])
elif len(vector) == 3:
# Use quaternions...because why not
quat = quaternion_from_angle_axis(angle, axis)
quat_inv = quaternion_conjugate(quat)
product = reduce(quaternion_mult, [quat, np.append(0, vector), quat_inv])
return product[1:]
else:
raise ValueError("vector must be of dimension 2 or 3")
def thick_diagonal(dim: int, thickness=2) -> np.ndarray:
row_indices = np.arange(dim).repeat(dim).reshape((dim, dim))
col_indices = np.transpose(row_indices)
return (np.abs(row_indices - col_indices) < thickness).astype("uint8")
def rotation_matrix_transpose_from_quaternion(quat: np.ndarray) -> List[np.ndarray]:
"""Converts the quaternion, quat, to an equivalent rotation matrix representation.
For more information, check `this page
<https://in.mathworks.com/help/driving/ref/quaternion.rotmat.html>`_.
Parameters
----------
quat
The quaternion which is to be converted.
Returns
-------
List[np.ndarray]
Gives back the Rotation matrix representation, returned as a 3-by-3
matrix or 3-by-3-by-N multidimensional array.
"""
quat_inv = quaternion_conjugate(quat)
return [
quaternion_mult(quat, [0, *basis], quat_inv)[1:]
for basis in [
[1, 0, 0],
[0, 1, 0],
[0, 0, 1],
]
]
def rotation_matrix_from_quaternion(quat: np.ndarray) -> np.ndarray:
return np.transpose(rotation_matrix_transpose_from_quaternion(quat))
def rotation_matrix_transpose(angle: float, axis: np.ndarray) -> np.ndarray:
if axis[0] == 0 and axis[1] == 0:
# axis = [0, 0, z] case is common enough it's worth
# having a shortcut
sgn = 1 if axis[2] > 0 else -1
cos_a = math.cos(angle)
sin_a = math.sin(angle) * sgn
return [
[cos_a, sin_a, 0],
[-sin_a, cos_a, 0],
[0, 0, 1],
]
quat = quaternion_from_angle_axis(angle, axis)
return rotation_matrix_transpose_from_quaternion(quat)
def rotation_matrix(
angle: float,
axis: np.ndarray,
homogeneous: bool = False,
) -> np.ndarray:
"""
Rotation in R^3 about a specified axis of rotation.
"""
about_z = rotation_about_z(angle)
z_to_axis = z_to_vector(axis)
axis_to_z = np.linalg.inv(z_to_axis)
inhomogeneous_rotation_matrix = reduce(np.dot, [z_to_axis, about_z, axis_to_z])
if not homogeneous:
return inhomogeneous_rotation_matrix
else:
rotation_matrix = np.eye(4)
rotation_matrix[:3, :3] = inhomogeneous_rotation_matrix
return rotation_matrix
def rotation_about_z(angle: float) -> List[List[float]]:
"""Returns a rotation matrix for a given angle.
Parameters
----------
angle : float
Angle for the rotation matrix.
Returns
-------
List[float]
Gives back the rotated matrix.
"""
return [
[np.cos(angle), -np.sin(angle), 0],
[np.sin(angle), np.cos(angle), 0],
[0, 0, 1],
]
def z_to_vector(vector: np.ndarray) -> np.ndarray:
"""
Returns some matrix in SO(3) which takes the z-axis to the
(normalized) vector provided as an argument
"""
norm = np.linalg.norm(vector)
if norm == 0:
return np.identity(3)
v = np.array(vector) / norm
phi = np.arccos(v[2])
if any(v[:2]):
# projection of vector to unit circle
axis_proj = v[:2] / np.linalg.norm(v[:2])
theta = np.arccos(axis_proj[0])
if axis_proj[1] < 0:
theta = -theta
else:
theta = 0
phi_down = np.array(
[[np.cos(phi), 0, np.sin(phi)], [0, 1, 0], [-np.sin(phi), 0, np.cos(phi)]],
)
return np.dot(rotation_about_z(theta), phi_down)
def angle_of_vector(vector: Sequence[float]) -> float:
"""Returns polar coordinate theta when vector is projected on xy plane.
Parameters
----------
vector
The vector to find the angle for.
Returns
-------
float
The angle of the vector projected.
"""
if config.renderer == "opengl":
return np.angle(complex(*vector[:2]))
else:
z = complex(*vector[:2])
if z == 0:
return 0
return np.angle(complex(*vector[:2]))
def angle_between_vectors(v1: np.ndarray, v2: np.ndarray) -> np.ndarray:
"""Returns the angle between two vectors.
This angle will always be between 0 and pi
Parameters
----------
v1
The first vector.
v2
The second vector.
Returns
-------
np.ndarray
The angle between the vectors.
"""
return 2 * np.arctan2(
np.linalg.norm(normalize(v1) - normalize(v2)),
np.linalg.norm(normalize(v1) + normalize(v2)),
)
def project_along_vector(point: float, vector: np.ndarray) -> np.ndarray:
"""Projects a vector along a point.
Parameters
----------
point
The point to be project from.
vector
The vector which has to projected.
Returns
-------
np.ndarray
A dot product of the point and vector.
"""
matrix = np.identity(3) - np.outer(vector, vector)
return np.dot(point, matrix.T)
def normalize(vect: Union[np.ndarray, Tuple[float]], fall_back=None) -> np.ndarray:
norm = np.linalg.norm(vect)
if norm > 0:
return np.array(vect) / norm
else:
if fall_back is not None:
return fall_back
else:
return np.zeros(len(vect))
def normalize_along_axis(array: np.ndarray, axis: np.ndarray) -> np.ndarray:
"""Normalizes an array with the provided axis.
Parameters
----------
array
The array which has to be normalized.
axis
The axis to be normalized to.
Returns
-------
np.ndarray
Array which has been normalized according to the axis.
"""
norms = np.sqrt((array * array).sum(axis))
norms[norms == 0] = 1
buffed_norms = np.repeat(norms, array.shape[axis]).reshape(array.shape)
array /= buffed_norms
return array
def get_unit_normal(v1: np.ndarray, v2: np.ndarray, tol: float = 1e-6) -> np.ndarray:
"""Gets the unit normal of the vectors.
Parameters
----------
v1
The first vector.
v2
The second vector
tol
[description], by default 1e-6
Returns
-------
np.ndarray
The normal of the two vectors.
"""
if config.renderer == "opengl":
v1 = normalize(v1)
v2 = normalize(v2)
cp = np.cross(v1, v2)
cp_norm = np.linalg.norm(cp)
if cp_norm < tol:
# Vectors align, so find a normal to them in the plane shared with the z-axis
new_cp = np.cross(np.cross(v1, OUT), v1)
new_cp_norm = np.linalg.norm(new_cp)
if new_cp_norm < tol:
return DOWN
return new_cp / new_cp_norm
return cp / cp_norm
else:
return normalize(np.cross(v1, v2))
###
def compass_directions(n: int = 4, start_vect: np.ndarray = RIGHT) -> np.ndarray:
"""Finds the cardinal directions using tau.
Parameters
----------
n
The amount to be rotated, by default 4
start_vect
The direction for the angle to start with, by default RIGHT
Returns
-------
np.ndarray
The angle which has been rotated.
"""
angle = TAU / n
return np.array([rotate_vector(start_vect, k * angle) for k in range(n)])
def regular_vertices(
n: int, *, radius: float = 1, start_angle: Optional[float] = None
) -> Tuple[np.ndarray, float]:
"""Generates regularly spaced vertices around a circle centered at the origin.
Parameters
----------
n
The number of vertices
radius
The radius of the circle that the vertices are placed on.
start_angle
The angle the vertices start at.
If unspecified, for even ``n`` values, ``0`` will be used.
For odd ``n`` values, 90 degrees is used.
Returns
-------
vertices : :class:`numpy.ndarray`
The regularly spaced vertices.
start_angle : :class:`float`
The angle the vertices start at.
"""
if start_angle is None:
if n % 2 == 0:
start_angle = 0
else:
start_angle = TAU / 4
start_vector = rotate_vector(RIGHT * radius, start_angle)
vertices = compass_directions(n, start_vector)
return vertices, start_angle
def complex_to_R3(complex_num: complex) -> np.ndarray:
return np.array((complex_num.real, complex_num.imag, 0))
def R3_to_complex(point: Sequence[float]) -> np.ndarray:
return complex(*point[:2])
def complex_func_to_R3_func(complex_func):
return lambda p: complex_to_R3(complex_func(R3_to_complex(p)))
def center_of_mass(points: Sequence[float]) -> np.ndarray:
"""Gets the center of mass of the points in space.
Parameters
----------
points
The points to find the center of mass from.
Returns
-------
np.ndarray
The center of mass of the points.
"""
points = [np.array(point).astype("float") for point in points]
return sum(points) / len(points)
def midpoint(
point1: Sequence[float],
point2: Sequence[float],
) -> Union[float, np.ndarray]:
"""Gets the midpoint of two points.
Parameters
----------
point1
The first point.
point2
The second point.
Returns
-------
Union[float, np.ndarray]
The midpoint of the points
"""
return center_of_mass([point1, point2])
def line_intersection(line1: Sequence[float], line2: Sequence[float]) -> np.ndarray:
"""Returns intersection point of two lines, each defined with
a pair of vectors determining the end points.
Parameters
----------
line1
The first line.
line2
The second line.
Returns
-------
np.ndarray
The intersection points of the two lines which are intersecting.
Raises
------
ValueError
Error is produced if the two lines don't intersect with each other
"""
x_diff = (line1[0][0] - line1[1][0], line2[0][0] - line2[1][0])
y_diff = (line1[0][1] - line1[1][1], line2[0][1] - line2[1][1])
def det(a, b):
return a[0] * b[1] - a[1] * b[0]
div = det(x_diff, y_diff)
if div == 0:
raise ValueError("Lines do not intersect")
d = (det(*line1), det(*line2))
x = det(d, x_diff) / div
y = det(d, y_diff) / div
return np.array([x, y, 0])
def find_intersection(p0, v0, p1, v1, threshold=1e-5) -> np.ndarray:
"""
Return the intersection of a line passing through p0 in direction v0
with one passing through p1 in direction v1. (Or array of intersections
from arrays of such points/directions).
For 3d values, it returns the point on the ray p0 + v0 * t closest to the
ray p1 + v1 * t
"""
p0 = np.array(p0, ndmin=2)
v0 = np.array(v0, ndmin=2)
p1 = np.array(p1, ndmin=2)
v1 = np.array(v1, ndmin=2)
m, n = np.shape(p0)
assert n in [2, 3]
numerator = np.cross(v1, p1 - p0)
denominator = np.cross(v1, v0)
if n == 3:
d = len(np.shape(numerator))
new_numerator = np.multiply(numerator, numerator).sum(d - 1)
new_denominator = np.multiply(denominator, numerator).sum(d - 1)
numerator, denominator = new_numerator, new_denominator
denominator[abs(denominator) < threshold] = np.inf # So that ratio goes to 0 there
ratio = numerator / denominator
ratio = np.repeat(ratio, n).reshape((m, n))
return p0 + ratio * v0
def get_winding_number(points: Sequence[float]) -> float:
total_angle = 0
for p1, p2 in adjacent_pairs(points):
d_angle = angle_of_vector(p2) - angle_of_vector(p1)
d_angle = ((d_angle + PI) % TAU) - PI
total_angle += d_angle
return total_angle / TAU
def shoelace(x_y: np.ndarray) -> float:
"""2D implementation of the shoelace formula.
Returns
-------
:class:`float`
Returns signed area.
"""
x = x_y[:, 0]
y = x_y[:, 1]
area = 0.5 * np.array(np.dot(x, np.roll(y, 1)) - np.dot(y, np.roll(x, 1)))
return area
def shoelace_direction(x_y: np.ndarray) -> str:
"""
Uses the area determined by the shoelace method to determine whether
the input set of points is directed clockwise or counterclockwise.
Returns
-------
:class:`str`
Either ``"CW"`` or ``"CCW"``.
"""
area = shoelace(x_y)
return "CW" if area > 0 else "CCW"
def cross2d(a, b):
if len(a.shape) == 2:
return a[:, 0] * b[:, 1] - a[:, 1] * b[:, 0]
else:
return a[0] * b[1] - b[0] * a[1]
def earclip_triangulation(verts: np.ndarray, ring_ends: list) -> list:
"""Returns a list of indices giving a triangulation
of a polygon, potentially with holes.
Parameters
----------
verts
verts is a numpy array of points.
ring_ends
ring_ends is a list of indices indicating where
the ends of new paths are.
Returns
-------
list
A list of indices giving a triangulation of a polygon.
"""
# First, connect all the rings so that the polygon
# with holes is instead treated as a (very convex)
# polygon with one edge. Do this by drawing connections
# between rings close to each other
rings = [list(range(e0, e1)) for e0, e1 in zip([0, *ring_ends], ring_ends)]
attached_rings = rings[:1]
detached_rings = rings[1:]
loop_connections = {}
while detached_rings:
i_range, j_range = (
list(
filter(
# Ignore indices that are already being
# used to draw some connection
lambda i: i not in loop_connections,
it.chain(*ring_group),
),
)
for ring_group in (attached_rings, detached_rings)
)
# Closest point on the attached rings to an estimated midpoint
# of the detached rings
tmp_j_vert = midpoint(verts[j_range[0]], verts[j_range[len(j_range) // 2]])
i = min(i_range, key=lambda i: norm_squared(verts[i] - tmp_j_vert))
# Closest point of the detached rings to the aforementioned
# point of the attached rings
j = min(j_range, key=lambda j: norm_squared(verts[i] - verts[j]))
# Recalculate i based on new j
i = min(i_range, key=lambda i: norm_squared(verts[i] - verts[j]))
# Remember to connect the polygon at these points
loop_connections[i] = j
loop_connections[j] = i
# Move the ring which j belongs to from the
# attached list to the detached list
new_ring = next(filter(lambda ring: ring[0] <= j < ring[-1], detached_rings))
detached_rings.remove(new_ring)
attached_rings.append(new_ring)
# Setup linked list
after = []
end0 = 0
for end1 in ring_ends:
after.extend(range(end0 + 1, end1))
after.append(end0)
end0 = end1
# Find an ordering of indices walking around the polygon
indices = []
i = 0
for _ in range(len(verts) + len(ring_ends) - 1):
# starting = False
if i in loop_connections:
j = loop_connections[i]
indices.extend([i, j])
i = after[j]
else:
indices.append(i)
i = after[i]
if i == 0:
break
meta_indices = earcut(verts[indices, :2], [len(indices)])
return [indices[mi] for mi in meta_indices]
def cartesian_to_spherical(vec: Sequence[float]) -> np.ndarray:
"""Returns an array of numbers corresponding to each
polar coordinate value (distance, phi, theta).
Parameters
----------
vec
A numpy array ``[x, y, z]``.
"""
norm = np.linalg.norm(vec)
if norm == 0:
return 0, 0, 0
r = norm
phi = np.arccos(vec[2] / r)
theta = np.arctan2(vec[1], vec[0])
return np.array([r, phi, theta])
def spherical_to_cartesian(spherical: Sequence[float]) -> np.ndarray:
"""Returns a numpy array ``[x, y, z]`` based on the spherical
coordinates given.
Parameters
----------
spherical
A list of three floats that correspond to the following:
r - The distance between the point and the origin.
theta - The azimuthal angle of the point to the positive x-axis.
phi - The vertical angle of the point to the positive z-axis.
"""
r, theta, phi = spherical
return np.array(
[
r * np.cos(theta) * np.sin(phi),
r * np.sin(theta) * np.sin(phi),
r * np.cos(phi),
],
)
def perpendicular_bisector(
line: Sequence[np.ndarray],
norm_vector=OUT,
) -> Sequence[np.ndarray]:
"""Returns a list of two points that correspond
to the ends of the perpendicular bisector of the
two points given.
Parameters
----------
line
a list of two numpy array points (corresponding
to the ends of a line).
norm_vector
the vector perpendicular to both the line given
and the perpendicular bisector.
Returns
-------
list
A list of two numpy array points that correspond
to the ends of the perpendicular bisector
"""
p1 = line[0]
p2 = line[1]
direction = np.cross(p1 - p2, norm_vector)
m = midpoint(p1, p2)
return [m + direction, m - direction]