normal function in surface.py cleaner

This commit is contained in:
Pilar 2021-01-20 16:22:58 -05:00
parent 9a55935add
commit 043eecf814

View File

@ -278,14 +278,12 @@ class Surface:
""" """
if self._normal is None: if self._normal is None:
points = self.points points = self.points
accepted_normal_difference = 0.01
# todo: IF THE FIRST ONE IS 0, START WITH THE NEXT # todo: IF THE FIRST ONE IS 0, START WITH THE NEXT
point_origin = points[len(points)-2] point_origin = points[len(points)-2]
vector_1 = points[len(points)-1] - point_origin vector_1 = points[len(points)-1] - point_origin
vector_2 = points[0] - point_origin vector_2 = points[0] - point_origin
vector_3 = points[1] - point_origin vector_3 = points[1] - point_origin
cross_product = np.cross(vector_1, vector_2) cross_product = np.cross(vector_1, vector_2)
cross_product_next = np.cross(vector_2, vector_3)
if np.linalg.norm(cross_product) != 0: if np.linalg.norm(cross_product) != 0:
cross_product = cross_product / np.linalg.norm(cross_product) cross_product = cross_product / np.linalg.norm(cross_product)
alpha = GeometryHelper.angle_between_vectors(vector_1, vector_2) alpha = GeometryHelper.angle_between_vectors(vector_1, vector_2)
@ -295,45 +293,38 @@ class Surface:
alpha = 0 alpha = 0
if len(points) == 3: if len(points) == 3:
return cross_product return cross_product
if np.linalg.norm(cross_product_next) != 0: alpha += self._angle(vector_2, vector_3, cross_product)
cross_product_next = cross_product_next / np.linalg.norm(cross_product_next)
beta = GeometryHelper.angle_between_vectors(vector_2, vector_3)
else:
cross_product_next = [0, 0, 0]
beta = 0
delta_normals = 0
for j in range(0, 3):
delta_normals += cross_product[j] - cross_product_next[j]
if np.abs(delta_normals) < accepted_normal_difference:
alpha += beta
else:
alpha -= beta
for i in range(0, len(points)-4): for i in range(0, len(points)-4):
vector_1 = points[i+1] - point_origin vector_1 = points[i+1] - point_origin
vector_2 = points[i+2] - point_origin vector_2 = points[i+2] - point_origin
cross_product_next = np.cross(vector_1, vector_2) alpha += self._angle(vector_1, vector_2, cross_product)
if np.linalg.norm(cross_product_next) != 0: vector_1 = points[len(points) - 1] - point_origin
cross_product_next = cross_product_next / np.linalg.norm(cross_product_next) vector_2 = points[0] - point_origin
beta = GeometryHelper.angle_between_vectors(vector_1, vector_2)
else:
cross_product_next = [0, 0, 0]
beta = 0
delta_normals = 0
for j in range(0, 3):
delta_normals += cross_product[j] - cross_product_next[j]
if np.abs(delta_normals) < accepted_normal_difference:
alpha += beta
else:
alpha -= beta
if alpha < 0: if alpha < 0:
cross_product = np.cross(points[0] - points[len(points) - 2], cross_product = np.cross(vector_2, vector_1)
points[len(points) - 1] - points[len(points) - 2])
else: else:
cross_product = np.cross(points[len(points) - 1] - points[len(points) - 2], cross_product = np.cross(vector_1, vector_2)
points[0] - points[len(points) - 2])
self._normal = cross_product / np.linalg.norm(cross_product) self._normal = cross_product / np.linalg.norm(cross_product)
return self._normal return self._normal
@staticmethod
def _angle(vector_1, vector_2, cross_product):
accepted_normal_difference = 0.01
cross_product_next = np.cross(vector_1, vector_2)
if np.linalg.norm(cross_product_next) != 0:
cross_product_next = cross_product_next / np.linalg.norm(cross_product_next)
alpha = GeometryHelper.angle_between_vectors(vector_1, vector_2)
else:
cross_product_next = [0, 0, 0]
alpha = 0
delta_normals = 0
for j in range(0, 3):
delta_normals += cross_product[j] - cross_product_next[j]
if np.abs(delta_normals) < accepted_normal_difference:
return alpha
else:
return -alpha
@property @property
def azimuth(self): def azimuth(self):
""" """