### ===================================================================================================================
### viia_merge_openings
### ===================================================================================================================
# Copyright ©2026 Haskoning Nederland B.V.
# For use by VIIA
### ===================================================================================================================
### 1. Import modules
### ===================================================================================================================
# General imports
from typing import List
# References for functions and classes in the haskoning_datafusr_py_base package
from haskoning_datafusr_py_base.math import distance_points, smaller, unit_vector, vector_between_points
### ===================================================================================================================
### 2. Function to merge the openings
### ===================================================================================================================
[docs]def viia_merge_openings(sorted_openings, local_axis, min_distance=0.0) -> List[List[float]]:
"""
This function merges openings that overlap in the direction of the localAxis. The openings must be sorted with
respect to the defined local axis and also be aligned on this axis. It returns the sorted list that contains the
extreme x- and y-coordinates of the merged openings.
Input:
- sorted_openings (list with lists of lists of 3 floats): openings sorted and aligned with respect to the
local_axis.
- local_axis (list of 3 floats): ntb
- min_distance (float): between consecutive openings, if less or equal distance, openings are merged.
Output:
- Returns a list with lists of 3 floats.
For example:
>>> viia_merge_openings([[[1.0, 0.0, 0.0], [2.0, 0.0, 0.0]], [[1.8, 0.0, 0.0], [3.0, 0.0, 0.0]]], [1.0, 0.0, 0.0])
Returns the list: [[[1.0, 0.0, 0.0], [3.0, 0.0, 0.0]]]
"""
if len(sorted_openings) > 1:
merge_counter = 0
# Iterate through sortedOpenings
for i in range(len(sorted_openings) - 1):
# Compute distance and unit vector between end point of current opening and points of next opening
distance_openings10 = distance_points(
point1=sorted_openings[i - merge_counter][1], point2=sorted_openings[i + 1 - merge_counter][0])
distance_openings11 = distance_points(
point1=sorted_openings[i - merge_counter][1], point2=sorted_openings[i + 1 - merge_counter][1])
if distance_openings10 != 0:
uv_openings10 = unit_vector(a=vector_between_points(
point1=sorted_openings[i - merge_counter][1], point2=sorted_openings[i + 1 - merge_counter][0]))
else:
uv_openings10 = [0.0, 0.0, 0.0]
if distance_openings11 != 0:
uv_openings11 = unit_vector(a=vector_between_points(
point1=sorted_openings[i - merge_counter][1], point2=sorted_openings[i + 1 - merge_counter][1]))
else:
uv_openings11 = [0.0, 0.0, 0.0]
# If distanceOpenings zero
# Merge sortedOpenings
if distance_openings10 == 0:
sorted_openings[i - merge_counter] = \
[sorted_openings[i - merge_counter][0], sorted_openings[i + 1 - merge_counter][1]]
del sorted_openings[i + 1 - merge_counter]
merge_counter += 1
continue
# If uv-openings10 is the reversed localXAxis, i.e. consecutive sorted openings overlap
elif uv_openings10 == unit_vector(a=[-local_axis[0], -local_axis[1], -local_axis[2]]):
# If uv-openings11 also the reversed localXAxis, endpoint of opening nr i lies behind endpoint of
# opening i+1
# Only remove opening i+1
if uv_openings11 == unit_vector(a=[-local_axis[0], -local_axis[1], -local_axis[2]]):
del sorted_openings[i + 1 - merge_counter]
merge_counter += 1
continue
# Else, merge sortedOpenings
else:
sorted_openings[i - merge_counter] = \
[sorted_openings[i - merge_counter][0], sorted_openings[i + 1 - merge_counter][1]]
del sorted_openings[i + 1 - merge_counter]
merge_counter += 1
continue
# If distanceOpenings10 lower than minDistance, merge openings
elif smaller(a=distance_openings10, b=min_distance):
sorted_openings[i - merge_counter] = \
[sorted_openings[i - merge_counter][0], sorted_openings[i + 1 - merge_counter][1]]
del sorted_openings[i + 1 - merge_counter]
merge_counter += 1
continue
return sorted_openings
### ===================================================================================================================
### 3. End of script
### ===================================================================================================================