finish implementing clothoid transition

This commit is contained in:
civilx64
2021-05-16 23:31:10 -04:00
committed by Thomas Krijnen
parent 38544e2ba9
commit 20a36c39fc
2 changed files with 113 additions and 105 deletions
+1
View File
@@ -0,0 +1 @@
tests/
@@ -18,12 +18,12 @@
############################################################################### ###############################################################################
from enum import Enum from enum import Enum
from dataclasses import DataClass from dataclasses import dataclass
import math import math
from OCC.gp import gp_Pnt2d from OCC.Core.gp import gp_Pnt2d
from OCC.BRepBuilderAPI import BRepBuilderAPI_MakeEdge from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_MakeEdge2d
from OCC.BRepBuilderAPI import BRepBuilderAPI_MakeWire from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_MakeWire
class IfcTransitionCurveType(Enum): class IfcTransitionCurveType(Enum):
@@ -40,71 +40,76 @@ class IfcTransitionCurveType(Enum):
SINECURVE = 6 # NOTE also referred to as Klein curve SINECURVE = 6 # NOTE also referred to as Klein curve
@DataClass @dataclass
class IfcTransitionSegment2D: class TransitionCurve:
"""IFC 4.1 Section 8.9.3.65 """
[https://standards.buildingsmart.org/IFC/RELEASE/IFC4_1/FINAL/HTML/schema/ifcgeometryresource/lexical/ifctransitioncurvesegment2d.htm] A curve that transitions between a straight line and a circular arc
(or the reverse).
A curve that transitions between a straight line and a circular arc (or the reverse).
""" """
StartPoint: tuple # IfcSchema::IfcCartesianPoint StartPoint: tuple # IfcSchema::IfcCartesianPoint
StartDirection: float # IfcSchema::IfcPlaneAngleMeasure StartDirection: float # IfcSchema::IfcPlaneAngleMeasure
SegmentLength: float # IfcSchema::IfcPositiveLengthMeasure SegmentLength: float # IfcSchema::IfcPositiveLengthMeasure
StartRadius: float = None # IfcSchema::IfcPositiveLengthMeasure
EndRadius: float = None # IfcSchema::IfcPositiveLengthMeasure
IsStartRadiusCCW: bool # IfcSchema::IfcBoolean IsStartRadiusCCW: bool # IfcSchema::IfcBoolean
IsEndRadiusCCW: bool # IfcSchema::IfcBoolean IsEndRadiusCCW: bool # IfcSchema::IfcBoolean
TransitionCurveType: IfcTransitionCurveType TransitionCurveType: IfcTransitionCurveType
StartRadius: float = None # IfcSchema::IfcPositiveLengthMeasure
EndRadius: float = None # IfcSchema::IfcPositiveLengthMeasure
def _calc_biquadratic_parabola_point(self, lpt, L, R, ccw):
def _calc_biquadratic_parabola_point(lpt, L, R, ccw):
x = lpt x = lpt
if x <= L / 2: if (x <= (L / 2)):
y = math.pow(x, 4) / (6 * R * math.pow(L, 2)) y = x**4 / (6 * R * L**2)
else: else:
y = ((-1 * math.pow(x, 4)) / (6 * R * math.pow(L, 2))) \
+ ((2 * math.pow(x, 3)) / (3 * R * L)) \ yterm_1 = (-1 * x**4) / (6 * R * L**2)
- ((math.pow(x, 2)) / (2 * R)) \ yterm_2 = (2 * x**3) / (3 * R * L)
+ ((L * x) / (6 * R)) \ yterm_3 = x**2 / (2 * R)
- ((math.pow(L, 2) / (48 * R)) yterm_4 = (L * x) / (6 * R)
yterm_5 = L**2 / (48 * R)
y = yterm_1 + yterm_2 - yterm_3 + yterm_4 - yterm_5
if not ccw: if not ccw:
y = -y y = -y
return gp_Pnt2d(x, y) return gp_Pnt2d(x, y)
def _calc_bloss_curve_point(self, lpt, L, R, ccw):
def _calc_bloss_curve_point(lpt, L, R, ccw):
pass pass
def _calc_clothoid_curve_point(self, lpt, L, R, ccw):
RL = R * L
xterm_1 = 1
xterm_2 = lpt**4 / (40 * RL**2)
xterm_3 = lpt**8 / (3456 * RL**4)
xterm_4 = lpt**12 / (599040 * RL**6)
x = lpt * (xterm_1 - xterm_2 + xterm_3 - xterm_4)
def _calc_clothoid_curve_point(lpt, L, R, ccw): factor = lpt**3 / (6 * RL)
x = lpt * (1 - (math.pow(lpt, 4) / (40 * math.pow(R, 2) * math.pow(L, 2))) \ yterm_1 = 1
+ (math.pow(lpt, 8) / 3456 * math.pow(R, 4) * math.pow(L, 4))) yterm_2 = lpt**4 / (56 * RL**2)
yterm_3 = lpt**8 / (7040 * RL**4)
yterm_4 = lpt**12 / (1612800 * RL**6)
y = ((math.pow(lpt, 3) / (6 * R * L)) \ y = factor * (yterm_1 - yterm_2 + yterm_3 - yterm_4)
* (1 - (math.pow(lpt, 4) / (56 * math.pow(R, 2) \
* math.pow(L, 2))) \
+ (math.pow(lpt, 8) / 7040 * math.pow(R, 4) * math.pow(L, 4))))
if not ccw: if not ccw:
y = -y y = -y
return gp_Pnt2d(x, y) return gp_Pnt2d(x, y)
def _calc_cosine_curve_point(self, lpt, L, R, ccw):
def _calc_cosine_curve_point(lpt, L, R, ccw):
pi = math.pi pi = math.pi
psi_x = (pi * lpt) / L psi_x = (pi * lpt) / L
terms = list()
terms.append(math.pow(L, 2) / (8.0 * math.pow(pi, 2) * math.pow(R, 2)))
terms.append(L / pi)
terms.append(math.pow(psi_x, 3) / 3.0)
terms.append(psi_x / 2.0)
terms.append((math.sin(psi_x) * math.cos(psi_x) / 2.0)
terms.append((psi_x * math.cos(psi_x))
x = (lpt - terms[0] * terms[1] * ( terms[2] + terms[3] - terms[4] - (2.0 * terms[5]))) xterm_1 = (L**2) / (8.0 * pi**2 * R**2)
xterm_2 = L / pi
xterm_3 = psi_x**3 / (3.0)
xterm_4 = psi_x / (2.0)
xterm_5 = (math.sin(psi_x) * math.cos(psi_x)) / (2.0)
xterm_6 = psi_x * math.cos(psi_x)
x = lpt - xterm_1 * xterm_2 * ( xterm_3 + xterm_4 - xterm_5 - (2.0 * xterm_6))
# TODO: code for y - coordinate # TODO: code for y - coordinate
y = 0 y = 0
@@ -114,8 +119,7 @@ def _calc_cosine_curve_point(lpt, L, R, ccw):
return gp_Pnt2d(x, y) return gp_Pnt2d(x, y)
def _calc_cubic_parabola_point(self, lpt, L, R, ccw):
def _calc_cubic_parabola_point(lpt, L, R, ccw):
x = lpt x = lpt
y = math.pow(x, 3) / (6 * R * L) y = math.pow(x, 3) / (6 * R * L)
@@ -124,58 +128,61 @@ def _calc_cubic_parabola_point(lpt, L, R, ccw):
return gp_Pnt2d(x, y) return gp_Pnt2d(x, y)
def _calc_sine_curve_point(self, lpt, L, R, ccw):
def _calc_sine_curve_point(lpt, L, R, ccw):
pass pass
def _calc_transition_curve_point(self, lpt, L, R, ccw, trans_type):
def _calc_transition_curve_point(lpt, L, R, ccw, trans_type):
if trans_type == "BIQUADRATICPARABOLA": if trans_type == "BIQUADRATICPARABOLA":
return _calc_cubic_parabola_point return self._calc_cubic_parabola_point(lpt, L, R, ccw)
elif trans_type == "BLOSSCURVE": elif trans_type == "BLOSSCURVE":
# return _calc_bloss_curve_point(lpt, L, R, ccw) # return _calc_bloss_curve_point(lpt, L, R, ccw)
raise ValueError(f"Transition Curve type '{trans_type}' not implemented yet.") raise ValueError(f"Transition Curve type '{trans_type}' not implemented yet.")
elif trans_type == "CLOTHOIDCURVE": elif trans_type == "CLOTHOIDCURVE":
return _calc_clothoid_curve_point(lpt, L, R, ccw) return self._calc_clothoid_curve_point(lpt, L, R, ccw)
elif trans_type == "COSINECURVE": elif trans_type == "COSINECURVE":
# return _calc_cosine_curve_point(lpt, L, R, ccw) # return _calc_cosine_curve_point(lpt, L, R, ccw)
raise ValueError(f"Transition Curve type '{trans_type}' not implemented yet.") raise ValueError(f"Transition Curve type '{trans_type}' not implemented yet.")
elif trans_type == "CUBICPARABOLA": elif trans_type == "CUBICPARABOLA":
return _calc_cubic_parabola_point(lpt, L, R, ccw) return self._calc_cubic_parabola_point(lpt, L, R, ccw)
elif trans_type == "SINECURVE": elif trans_type == "SINECURVE":
# return _calc_sine_curve_point(lpt, L, R, ccw) # return _calc_sine_curve_point(lpt, L, R, ccw)
raise ValueError(f"Transition Curve type '{trans_type}' not implemented yet.") raise ValueError(f"Transition Curve type '{trans_type}' not implemented yet.")
else: else:
raise ValueError(f"Invalid Transition Curve type '{trans_type}'.") raise ValueError(f"Invalid Transition Curve type '{trans_type}'.")
def to_wire(self, stroking_interval=5.0):
def convert_IfcTransitionSegment2D(segment, stroking_interval=5.0):
"""convert IfcTransitionSegment2D to OCC wire """convert IfcTransitionSegment2D to OCC wire
:param segment: ifc entity to be parsed into geometry
:type segment: IfcTransitionSegment2D
:param stroking_interval: maximum curve length between points to be calculated :param stroking_interval: maximum curve length between points to be calculated
:type stroking_interval: float :type stroking_interval: float
:return: OCC wire containing interpolated points :return: OCC wire containing interpolated points
""" """
points = list() points = list()
L = segment.SegmentLength L = self.SegmentLength
R = segment.EndRadius R = self.EndRadius
ccw = segment.IsStartRadiusCCW ccw = self.IsStartRadiusCCW
trans_type = segment.TransitionCurveType.name trans_type = self.TransitionCurveType.name
num_intervals = math.ceil(L / stroking_interval) num_intervals = math.ceil(L / stroking_interval)
interval_dist = L / num_intervals interval_dist = L / num_intervals
lpt = 0.0 # length along the curve at the point to be calculated lpt = 0.0 # length along the curve at the point to be calculated
for _ in num_intervals: for _ in range(num_intervals):
points.append(_calc_transition_curve_point( points.append(self._calc_transition_curve_point(
lpt, L, R, ccw, trans_type lpt, L, R, ccw, trans_type
)) ))
lpt += interval_dist lpt += interval_dist
e = BRepBuilderAPI_MakeEdge(points) edges = list()
for i in range(len(points) - 1):
edges.append(BRepBuilderAPI_MakeEdge2d(
points[i], points[i + 1]
))
return BRepBuilderAPI_MakeWire(e) wire = BRepBuilderAPI_MakeWire()
for e in edges:
wire.Add(e.Edge())
# return wire
return points