-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvec3d.py
69 lines (54 loc) · 2.07 KB
/
vec3d.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
from math import sqrt
from numbers import Number
class Vec3d:
def __init__(self, x: float = 0.0, y: float = 0.0, z: float = 0.0, w: float = 1.0):
self.x: float = x
self.y: float = y
self.z: float = z
self.w: float = w
def coords2d(self):
return self.x, self.y
def coords3d(self):
return self.x, self.y, self.z
def __add__(self, other):
if isinstance(other, Vec3d):
return Vec3d(self.x + other.x, self.y + other.y, self.z + other.z)
return NotImplemented
def __sub__(self, other):
if isinstance(other, Vec3d):
return Vec3d(self.x - other.x, self.y - other.y, self.z - other.z)
return NotImplemented
def __mul__(self, other):
if isinstance(other, Number):
return Vec3d(self.x * other, self.y * other, self.z * other)
return NotImplemented
def __truediv__(self, other):
return Vec3d(self.x / other, self.y / other, self.z / other)
def DotProduct(self, other):
return self.x * other.x + self.y * other.y + self.z * other.z
def CrossProduct(self, other):
v = Vec3d()
v.x = self.y * other.z - self.z * other.y
v.y = self.z * other.x - self.x * other.z
v.z = self.x * other.y - self.y * other.x
return v
def length(self):
return sqrt(self.DotProduct(self))
def Normalise(self):
length = self.length()
if length == 0:
return Vec3d(float("inf"), float("inf"), float("inf"))
return Vec3d(self.x / length, self.y / length, self.z / length)
def __repr__(self):
return f"Vec3d({self.x}, {self.y}, {self.z})"
def Vector_IntersectPlane(
plane_p: Vec3d, plane_n: Vec3d, lineStart: Vec3d, lineEnd: Vec3d
) -> Vec3d:
plane_n = plane_n.Normalise()
plane_d = -(plane_n.DotProduct(plane_p))
ad = lineStart.DotProduct(plane_n)
bd = lineEnd.DotProduct(plane_n)
t = (-plane_d - ad) / (bd - ad)
lineStartToEnd = lineEnd - lineStart
lineToIntersect = lineStartToEnd * t
return lineStart + lineToIntersect