### ===================================================================================================================
### viia_sort_openings
### ===================================================================================================================
# Copyright ©VIIA 2024
### ===================================================================================================================
### 1. Import modules
### ===================================================================================================================
# References for functions and classes in the fem-client
from rhdhv_fem.fem_math import fem_unit_vector_2_points, fem_dot_product_vector
### ===================================================================================================================
### 2. Function to sort the openings
### ===================================================================================================================
[docs]def viia_sort_openings(openings, axis):
"""
This function sorts a list of openings with respect to a defined axis. It returns the sorted list that contains
the extreme coordinates of the openings in the direction of the provided axis.
Input:
- openings (list with lists with lists of 3 floats)
- localAxis (list of 3 floats)
Output:
- Returns a list with lists of lists of 3 floats
For example:
>>> viia_sort_openings([[[2.0, 0.0, 0.0], [1.0, 0.0, 0.0]], [[3.8, 0.0, 0.0], [3.0, 0.0, 0.0]]], [-1.0, 0.0, 0.0])
Returns the list: [[[3.8, 0.0, 0.0], [3.0, 0.0, 0.0]], [[2.0, 0.0, 0.0], [1.0, 0.0, 0.0]]]
"""
# Sort openings with respect to the localXAxis
if len(openings) > 1:
# Create new list sorted_openings and add first opening to this list
sorted_openings = [openings[0]]
# Iterate through other original (not sorted) openings
for i in range(1, len(openings)):
opening = openings[i]
# Iterate through sorted_openings
for j in range(len(sorted_openings)):
sorted_opening = sorted_openings[j]
# Compute unit vector between first points of sorted_opening and opening
if not sorted_opening[0] == opening[0]:
uv = fem_unit_vector_2_points((sorted_opening[0]), opening[0])
elif not sorted_opening[1] == opening[1]:
uv = fem_unit_vector_2_points((sorted_opening[1]), opening[1])
else:
uv = [0.0, 0.0, 0.0]
# If the dot product is -1, opening lies before sorted_opening on the localXAxis,
# insert at current index j
if fem_dot_product_vector(axis, uv) == -1:
sorted_openings.insert(j, opening)
break
# If last sorted_opening in list, opening lies behind all sorted_openings on the localXAxis,
# append at end of the list
elif j == len(sorted_openings) - 1:
sorted_openings.append(opening)
break
else:
# Only one opening, sorted_openings equal to openings
sorted_openings = openings
return sorted_openings
### ===================================================================================================================
### 3. End of script
### ===================================================================================================================