mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-08-10 09:48:32 +00:00
more work on transitions for MEP
- support transitions from and to circle profiles - reworked transition length algorithm now it should be more accurate - added support for creating transitions between profiles that are parallel but not collinear
This commit is contained in:
@@ -19,12 +19,17 @@
|
||||
import collections
|
||||
import ifcopenshell
|
||||
import ifcopenshell.api
|
||||
from math import cos, sin, pi, tan, radians
|
||||
from math import cos, sin, pi, tan, radians, degrees, atan, sqrt
|
||||
from mathutils import Vector, Matrix
|
||||
from itertools import chain
|
||||
|
||||
V = lambda *x: Vector([float(i) for i in x])
|
||||
sign = lambda x: x and (1, -1)[x < 0]
|
||||
PRECISION = 1.0e-5
|
||||
is_x = lambda value, x: (x + PRECISION) > value > (x - PRECISION)
|
||||
round_to_precision = lambda x, si_conversion: round(x * si_conversion, 5) / si_conversion
|
||||
round_vector_to_precision = lambda v, si_conversion: Vector([round_to_precision(i, si_conversion) for i in v])
|
||||
|
||||
|
||||
# Note: using ShapeBuilder try not to reuse IFC elements in the process
|
||||
# otherwise you might run into situation where builder.mirror or other operation
|
||||
@@ -840,7 +845,9 @@ class ShapeBuilder:
|
||||
|
||||
return face_set
|
||||
|
||||
def mep_transition_shape(self, start_segment, end_segment, start_length, end_length, angle=30.0):
|
||||
def mep_transition_shape(
|
||||
self, start_segment, end_segment, start_length, end_length, angle=30.0, profile_offset=None
|
||||
):
|
||||
"""
|
||||
returns tuple of Model/Body/MODEL_VIEW IfcRepresentation and transition shape data
|
||||
"""
|
||||
@@ -853,68 +860,285 @@ class ShapeBuilder:
|
||||
if material and material.is_a("IfcMaterialProfileSet") and len(material.MaterialProfiles) == 1:
|
||||
return material.MaterialProfiles[0].Profile
|
||||
|
||||
def get_circle_points(radius, segments=16):
|
||||
"""starting from (R,0), going counter-clockwise"""
|
||||
angle_d = 2 * pi / segments
|
||||
verts = []
|
||||
for i in range(segments):
|
||||
angle = angle_d * i
|
||||
verts.append(V(cos(angle), sin(angle), 0) * radius)
|
||||
return verts
|
||||
|
||||
def get_rectangle_points(dim):
|
||||
"""Starting from (+X/2, +Y/2) going counter-clockwise"""
|
||||
dim = dim / 2
|
||||
points = [
|
||||
dim * V(1, 1, 0),
|
||||
dim * V(-1, 1, 0),
|
||||
dim * V(-1, -1, 0),
|
||||
dim * V(1, -1, 0),
|
||||
]
|
||||
return points
|
||||
|
||||
# TODO: support more profiles
|
||||
def get_dim(profile, depth):
|
||||
if profile.is_a("IfcRectangleProfileDef"):
|
||||
return V(profile.XDim / 2, profile.YDim / 2, depth)
|
||||
elif profile.is_a("IfcCircleProfileDef"):
|
||||
return V(profile.Radius, profile.Radius, depth)
|
||||
return None
|
||||
|
||||
def get_profile_faceset(points, length, offset=None):
|
||||
# prevent mutating arguments, deepcopy doesn't work
|
||||
start_points = [p.copy() if not offset else (p + offset) for p in points]
|
||||
end_points = [p.copy() for p in start_points]
|
||||
for p in end_points:
|
||||
p.z += length
|
||||
|
||||
points = start_points + end_points
|
||||
faces = []
|
||||
n_verts = len(start_points)
|
||||
last_vert_i = n_verts - 1
|
||||
for i in range(last_vert_i):
|
||||
face = (i, i + 1, n_verts + i + 1, n_verts + i)
|
||||
faces.append(face)
|
||||
faces.append((last_vert_i, 0, n_verts + 0, n_verts + last_vert_i)) # close the loop
|
||||
|
||||
# if there is offset we put a cap at the end
|
||||
# otherwise at the start
|
||||
if offset:
|
||||
faces.append(tuple(range(n_verts, n_verts * 2)))
|
||||
else:
|
||||
faces.append(tuple(reversed(range(n_verts))))
|
||||
|
||||
face_set = self.polygonal_face_set(points, faces)
|
||||
return face_set
|
||||
|
||||
start_profile = get_profile(start_segment)
|
||||
end_profile = get_profile(end_segment)
|
||||
|
||||
# TODO: support more profiles
|
||||
if not start_profile.is_a("IfcRectangleProfileDef") or not end_profile.is_a("IfcRectangleProfileDef"):
|
||||
# Non rectangular profiles are not yet supported
|
||||
start_half_dim = get_dim(start_profile, start_length)
|
||||
end_half_dim = get_dim(end_profile, end_length)
|
||||
|
||||
# if profile types are not supported
|
||||
if not start_half_dim or not end_half_dim:
|
||||
return None, None
|
||||
|
||||
start_half_dim = V(start_profile.XDim / 2, start_profile.YDim / 2, start_length)
|
||||
end_half_dim = V(end_profile.XDim / 2, end_profile.YDim / 2, end_length)
|
||||
|
||||
transition_items = []
|
||||
end_extrusion_offset = V(0, 0, start_length)
|
||||
start_offset = V(0, 0, start_length)
|
||||
end_extrusion_offset = start_offset.copy()
|
||||
|
||||
si_conversion = ifcopenshell.util.unit.calculate_unit_scale(self.file)
|
||||
|
||||
# TODO: support offseted profiles
|
||||
def get_transition_length(start_half_dim, end_half_dim, angle, profile_offset=None):
|
||||
# NOTE: transition_length == 0 when profiles have the same dimensions
|
||||
|
||||
# holy grail of the transition length:
|
||||
|
||||
def get_transition_legth(start_half_dim, end_half_dim, angle):
|
||||
diff = start_half_dim.xy - end_half_dim.xy
|
||||
diff = Vector([abs(i) for i in diff])
|
||||
c = diff.x * tan(radians(90 - angle / 2))
|
||||
a = diff.y
|
||||
b = (c**2 - a**2) ** 0.5
|
||||
return b
|
||||
|
||||
transition_length = get_transition_legth(start_half_dim, end_half_dim, angle)
|
||||
def calculate_transition(diff, profile_offset, end_profile=False, angle=None, length=None):
|
||||
"""will return transition length based on the profile dimension differences and offset.
|
||||
|
||||
If `length` is provided will return transition angle"""
|
||||
|
||||
# offsets tend to have bunch of float point garbage
|
||||
# that can result in errors when we're calculating value for square root below
|
||||
offset = V(0, 0) if profile_offset is None else round_vector_to_precision(profile_offset, si_conversion)
|
||||
if end_profile:
|
||||
diff, offset = diff.yx, offset.yx
|
||||
|
||||
a = diff.x + offset.x
|
||||
b = diff.x - offset.x
|
||||
if length is None:
|
||||
if diff.x == 0:
|
||||
return 0
|
||||
|
||||
t = tan(radians(angle))
|
||||
l1 = (a + b + sqrt(a**2 + 4 * a * b * t**2 + 2 * a * b + b**2)) / (2 * t)
|
||||
length = sqrt(l1**2 - offset.y**2)
|
||||
|
||||
# TODO: remove after debug, move somewhere to tests?
|
||||
if True:
|
||||
A = (end_profile if end_profile else start_half_dim) * V(1, 0, 0)
|
||||
end_profile_offset = offset.to_3d() + V(0, 0, length)
|
||||
D = (start_half_dim if end_profile else end_half_dim) * V(1, 0, 0)
|
||||
B, C = -A, -D
|
||||
C += end_profile_offset
|
||||
D += end_profile_offset
|
||||
tested_angle = degrees((A - D).angle(B - C))
|
||||
print(f"length = {length}, requested angle = {angle}, tested angle = {tested_angle}")
|
||||
return length
|
||||
|
||||
elif angle is None:
|
||||
# TODO: need to handle angle differently for that case
|
||||
# it occurs when diff == 0
|
||||
if length == 0:
|
||||
return 0
|
||||
|
||||
l1 = sqrt(length**2 + offset.y**2)
|
||||
t = -l1 * (a + b) / (a * b - l1**2)
|
||||
angle = atan(t)
|
||||
return angle
|
||||
|
||||
transition_lengths = [
|
||||
calculate_transition(diff, profile_offset, angle=angle),
|
||||
calculate_transition(diff, profile_offset, angle=angle, end_profile=True),
|
||||
]
|
||||
|
||||
other_side_angles = [
|
||||
calculate_transition(diff, profile_offset, length=transition_lengths[0]),
|
||||
calculate_transition(diff, profile_offset, length=transition_lengths[1], end_profile=True),
|
||||
]
|
||||
|
||||
# NOTE: debug values
|
||||
print(f"offset = {profile_offset}")
|
||||
print(f"diff = {diff}")
|
||||
print(f"lengths = {transition_lengths}")
|
||||
print(f"other angles = {other_side_angles}")
|
||||
print(f"measurable angles = {[(180 - deg)/2 for deg in other_side_angles]}")
|
||||
|
||||
# need to make sure that the worst angle (maximum angle)
|
||||
# for this transition angle is `angle`
|
||||
for transition_length, other_side_angle in zip(transition_lengths, other_side_angles):
|
||||
if other_side_angle < angle or is_x(other_side_angle, angle):
|
||||
print(f"final length = {transition_length}") # TODO: remove after debug
|
||||
return transition_length
|
||||
|
||||
transition_length = get_transition_length(start_half_dim, end_half_dim, angle, profile_offset)
|
||||
if transition_length is None:
|
||||
return None, None
|
||||
|
||||
faces = []
|
||||
if transition_length != 0:
|
||||
end_extrusion_offset.z += transition_length
|
||||
end_extrusion_offset.z += transition_length
|
||||
if profile_offset:
|
||||
end_extrusion_offset.xy += profile_offset
|
||||
|
||||
if start_profile.is_a("IfcRectangleProfileDef") and end_profile.is_a("IfcRectangleProfileDef"):
|
||||
# no transitions for exactly the same profiles
|
||||
if transition_length == 0:
|
||||
return None, None
|
||||
|
||||
faces += [(3, 4, 7, 0), (11, 8, 15, 12), (3, 11, 12, 4), (7, 15, 8, 0)]
|
||||
|
||||
# NOTE: clockwise order for correct face orientation
|
||||
faces += [
|
||||
# start extrusion
|
||||
(0, 1, 2, 3),
|
||||
(8, 11, 10, 9),
|
||||
(0, 8, 9, 1),
|
||||
(1, 9, 10, 2),
|
||||
(2, 10, 11, 3),
|
||||
# end extrusion
|
||||
(4, 5, 6, 7),
|
||||
(12, 15, 14, 13),
|
||||
(4, 12, 13, 5),
|
||||
(5, 13, 14, 6),
|
||||
(6, 14, 15, 7),
|
||||
]
|
||||
points = [
|
||||
start_half_dim * V(-1, -1, 1),
|
||||
start_half_dim * V(-1, -1, 0),
|
||||
start_half_dim * V(1, -1, 0),
|
||||
start_half_dim * V(1, -1, 1),
|
||||
end_half_dim * V(1, -1, 0) + end_extrusion_offset,
|
||||
end_half_dim * V(1, -1, 1) + end_extrusion_offset,
|
||||
end_half_dim * V(-1, -1, 1) + end_extrusion_offset,
|
||||
end_half_dim * V(-1, -1, 0) + end_extrusion_offset,
|
||||
start_half_dim * V(-1, 1, 1),
|
||||
start_half_dim * V(-1, 1, 0),
|
||||
start_half_dim * V(1, 1, 0),
|
||||
start_half_dim * V(1, 1, 1),
|
||||
end_half_dim * V(1, 1, 0) + end_extrusion_offset,
|
||||
end_half_dim * V(1, 1, 1) + end_extrusion_offset,
|
||||
end_half_dim * V(-1, 1, 1) + end_extrusion_offset,
|
||||
end_half_dim * V(-1, 1, 0) + end_extrusion_offset,
|
||||
]
|
||||
# NOTE: clockwise order for correct face orientation
|
||||
faces += [
|
||||
# start extrusion
|
||||
(0, 1, 2, 3),
|
||||
(8, 11, 10, 9),
|
||||
(0, 8, 9, 1),
|
||||
(1, 9, 10, 2),
|
||||
(2, 10, 11, 3),
|
||||
# end extrusion
|
||||
(4, 5, 6, 7),
|
||||
(12, 15, 14, 13),
|
||||
(4, 12, 13, 5),
|
||||
(5, 13, 14, 6),
|
||||
(6, 14, 15, 7),
|
||||
]
|
||||
points = [
|
||||
start_half_dim * V(-1, -1, 1),
|
||||
start_half_dim * V(-1, -1, 0),
|
||||
start_half_dim * V(1, -1, 0),
|
||||
start_half_dim * V(1, -1, 1),
|
||||
end_half_dim * V(1, -1, 0) + end_extrusion_offset,
|
||||
end_half_dim * V(1, -1, 1) + end_extrusion_offset,
|
||||
end_half_dim * V(-1, -1, 1) + end_extrusion_offset,
|
||||
end_half_dim * V(-1, -1, 0) + end_extrusion_offset,
|
||||
start_half_dim * V(-1, 1, 1),
|
||||
start_half_dim * V(-1, 1, 0),
|
||||
start_half_dim * V(1, 1, 0),
|
||||
start_half_dim * V(1, 1, 1),
|
||||
end_half_dim * V(1, 1, 0) + end_extrusion_offset,
|
||||
end_half_dim * V(1, 1, 1) + end_extrusion_offset,
|
||||
end_half_dim * V(-1, 1, 1) + end_extrusion_offset,
|
||||
end_half_dim * V(-1, 1, 0) + end_extrusion_offset,
|
||||
]
|
||||
elif start_profile.is_a("IfcCircleProfileDef") and end_profile.is_a("IfcCircleProfileDef"):
|
||||
# no transitions for exactly the same profiles
|
||||
if transition_length == 0:
|
||||
return None, None
|
||||
|
||||
n_segments = 16
|
||||
first_profile_points = get_circle_points(start_profile.Radius, n_segments)
|
||||
second_profile_points = get_circle_points(end_profile.Radius, n_segments)
|
||||
|
||||
faces = []
|
||||
for i in range(n_segments):
|
||||
# For wrapping around the circle
|
||||
next_i = (i + 1) % n_segments
|
||||
face = [i, next_i, next_i + n_segments, i + n_segments]
|
||||
faces.append(face)
|
||||
|
||||
transition_items.append(get_profile_faceset(first_profile_points, start_length))
|
||||
transition_items.append(get_profile_faceset(second_profile_points, end_length, end_extrusion_offset))
|
||||
|
||||
first_profile_points = [p + start_offset for p in first_profile_points]
|
||||
second_profile_points = [p + end_extrusion_offset for p in second_profile_points]
|
||||
|
||||
points = first_profile_points + second_profile_points
|
||||
|
||||
else: # one is circular, another one is rectangular
|
||||
# support transition from rectangle to circle of the same dimensions
|
||||
if transition_length == 0:
|
||||
transition_length = (start_length + end_length) / 2
|
||||
end_extrusion_offset.z += transition_length
|
||||
|
||||
starting_with_circle = start_profile.is_a("IfcCircleProfileDef")
|
||||
if starting_with_circle:
|
||||
circle_profile, rect_profile = start_profile, end_profile
|
||||
else:
|
||||
circle_profile, rect_profile = end_profile, start_profile
|
||||
|
||||
circle_points = get_circle_points(circle_profile.Radius)
|
||||
rect_points = get_rectangle_points(V(rect_profile.XDim, rect_profile.YDim, 0))
|
||||
|
||||
if starting_with_circle:
|
||||
start_points, end_points = circle_points, rect_points
|
||||
else:
|
||||
start_points, end_points = rect_points, circle_points
|
||||
|
||||
transition_items.append(get_profile_faceset(start_points, start_length))
|
||||
transition_items.append(get_profile_faceset(end_points, end_length, end_extrusion_offset))
|
||||
|
||||
# offset verts
|
||||
if starting_with_circle:
|
||||
circle_points = [p + start_offset for p in circle_points]
|
||||
rect_points = [p + end_extrusion_offset for p in rect_points]
|
||||
else:
|
||||
rect_points = [p + start_offset for p in rect_points]
|
||||
circle_points = [p + end_extrusion_offset for p in circle_points]
|
||||
|
||||
# circle verts are 0-15, rect verts are 16-19
|
||||
points = circle_points + rect_points
|
||||
transition_faces = [
|
||||
(0, 19, 16), # base
|
||||
(0, 16, 1),
|
||||
(1, 16, 2),
|
||||
(2, 16, 3),
|
||||
(3, 16, 4),
|
||||
(4, 16, 17), # base
|
||||
(4, 17, 5),
|
||||
(5, 17, 6),
|
||||
(6, 17, 7),
|
||||
(7, 17, 8),
|
||||
(8, 17, 18), # base
|
||||
(8, 18, 9),
|
||||
(9, 18, 10),
|
||||
(10, 18, 11),
|
||||
(11, 18, 12),
|
||||
(12, 18, 19), # base
|
||||
(12, 19, 13),
|
||||
(13, 19, 14),
|
||||
(14, 19, 15),
|
||||
(15, 19, 0),
|
||||
]
|
||||
# revert them in case it's starting with circle profile to keep the face orientation
|
||||
if starting_with_circle:
|
||||
transition_faces = [f[::-1] for f in transition_faces]
|
||||
faces += transition_faces
|
||||
|
||||
face_set = self.polygonal_face_set(points, faces)
|
||||
transition_items.append(face_set)
|
||||
|
||||
Reference in New Issue
Block a user