fix: triangle normal vector length and direction

This commit is contained in:
s3lph 2024-12-17 00:30:29 +01:00
parent e0a85d2cfe
commit c51dcd6514
Signed by: s3lph
GPG key ID: 0AA29A52FB33CFB5

8
map.py
View file

@ -15,13 +15,17 @@ class Triangle:
self.v1 = v1 self.v1 = v1
self.v2 = v2 self.v2 = v2
self.v3 = v3 self.v3 = v3
# Sort triangle vertices so that so that the normal points "outward"
if np.dot(self.v1, np.cross(self.v2 - self.v1, self.v3 - self.v1)) < 0:
self.v3, self.v2 = self.v2, self.v3
# Edges of the origin vertex and their L2 norms # Edges of the origin vertex and their L2 norms
self.e1 = v2 - v1 self.e1 = self.v2 - self.v1
self.e2 = v3 - v1 self.e2 = self.v3 - self.v1
self.e1n = np.linalg.norm(self.e1) self.e1n = np.linalg.norm(self.e1)
self.e2n = np.linalg.norm(self.e2) self.e2n = np.linalg.norm(self.e2)
# Normal vector of the triangle's plane # Normal vector of the triangle's plane
self.n = np.cross(self.e1 / self.e1n, self.e2 / self.e2n) self.n = np.cross(self.e1 / self.e1n, self.e2 / self.e2n)
self.n = self.n / np.linalg.norm(self.n)
# The vertex coordinates in plane coordinates # The vertex coordinates in plane coordinates
self.v1p = self.plane_coords(self.v1) self.v1p = self.plane_coords(self.v1)
self.v2p = self.plane_coords(self.v2) self.v2p = self.plane_coords(self.v2)