Example #1
0
def construct_cylinder(
    prop: CSProperties,
    start: C3Tuple,
    stop: C3Tuple,
    radius: float,
    priority: int,
    transform: CSTransform = None,
) -> CSPrimCylinder:
    """
    """
    start = c3_maybe_tuple(start)
    stop = c3_maybe_tuple(stop)

    start = start.coordinate_list()
    stop = stop.coordinate_list()
    position = np.average([start, stop], axis=0)
    start = np.subtract(start, position)
    stop = np.subtract(stop, position)
    cyl = prop.AddCylinder(
        start=start, stop=stop, radius=radius, priority=priority
    )
    apply_transform(cyl, transform)

    translate = CSTransform()
    translate.AddTransform("Translate", position)
    apply_transform(cyl, translate)

    return cyl
Example #2
0
def construct_cylindrical_shell(
    prop: CSProperties,
    start: C3Tuple,
    stop: C3Tuple,
    inner_radius: float,
    outer_radius: float,
    priority: int,
    transform: CSTransform = None,
) -> CSPrimCylindricalShell:
    """
    """
    start = c3_maybe_tuple(start)
    stop = c3_maybe_tuple(stop)

    start = start.coordinate_list()
    stop = stop.coordinate_list()
    position = np.average([start, stop], axis=0)
    start = np.subtract(start, position)
    stop = np.subtract(stop, position)
    cyl = prop.AddCylindricalShell(
        start=start,
        stop=stop,
        radius=np.average([inner_radius, outer_radius]),
        shell_width=outer_radius - inner_radius,
        priority=priority,
    )
    apply_transform(cyl, transform)

    translate = CSTransform()
    translate.AddTransform("Translate", position)
    apply_transform(cyl, translate)

    return cyl
Example #3
0
def construct_box(
    prop: CSProperties,
    box: Box3,
    priority: int,
    transform: CSTransform = None,
) -> CSPrimitives:
    """
    """
    if transform is None:
        box = _add_box(
            prop=prop, priority=priority, start=box.start(), stop=box.stop()
        )
        return box

    if box.has_zero_dim():
        fp_warning(construct_box)

    box = _add_box(
        prop=prop,
        priority=priority,
        start=box.origin_start(),
        stop=box.origin_stop(),
    )
    apply_transform(box, transform)
    translate = CSTransform()
    translate.AddTransform("Translate", box.center().coordinate_list())
    apply_transform(box, translate)
    return box
Example #4
0
def construct_polygon(
    prop: CSProperties,
    points: List[Coordinate2],
    normal: Axis,
    elevation: float,
    priority: int,
    transform: CSTransform = None,
):
    """
    """
    poly_points = _poly_points(points)
    if transform is None:
        prim = _add_polygon(
            prop=prop,
            priority=priority,
            points=poly_points,
            norm_dir=normal.intval(),
            elevation=elevation,
        )
        return prim

    fp_warning(construct_polygon)

    first_coord_center = np.average(poly_points[0])
    second_coord_center = np.average(poly_points[1])
    if normal.intval() == 0:
        center = Coordinate3(
            elevation, first_coord_center, second_coord_center
        )
    elif normal.intval() == 1:
        center = Coordinate3(
            first_coord_center, elevation, second_coord_center
        )
    else:
        center = Coordinate3(
            first_coord_center, second_coord_center, elevation
        )

    centered_pts = [
        np.subtract(pts, cent)
        for pts, cent in zip(
            poly_points, [first_coord_center, second_coord_center]
        )
    ]

    prim = _add_polygon(
        prop=prop,
        priority=priority,
        points=centered_pts,
        norm_dir=normal.intval(),
        elevation=0,
    )
    apply_transform(prim, transform)
    tr = CSTransform()
    tr.AddTransform("Translate", center.coordinate_list())
    apply_transform(prim, tr)

    return prim
Example #5
0
 def transform(self, transform: CSTransform) -> Coordinate3:
     """
     Transform the coordinate.  This does not update the coordinate, it
     simply returns a new transformed coordinate.  If you want to
     replace the old coordinate you must assign it to the result of
     this function.
     """
     clist = self.coordinate_list()
     tclist = transform.Transform(clist)
     return Coordinate3(tclist[0], tclist[1], tclist[2])
Example #6
0
def apply_transform(prim: CSPrimitives,
                    transform: CSTransform = None,
                    replace: bool = False) -> None:
    """
    Apply a transformation to a primitive.  This allows you to pass
    around and apply CSTransform objects directly, rather than having
    to set all transforms via prim.AddTransform.

    :param prim: The primitive to which the transform should be
        applied.
    :param transform: The transformation to apply.
    :param replace: Replace any transforms previously applied to the
        primitive.  Defaults to False, meaning any transforms applied
        here will be applied on top of any existing transforms already
        applied.
    """
    if transform is not None:
        tr = prim.GetTransform()
        concatenate = not replace
        tr.SetMatrix(transform.GetMatrix(), concatenate)
Example #7
0
def append_transform(tr1: CSTransform, tr2: CSTransform) -> CSTransform:
    """
    Append two transforms.

    :param tr1: First transform.
    :param tr2: Transform to append to first transform.  The first
        transform will be applied to the primitive first, followed by
        this transform.

    :returns: Combined transform.
    """
    if tr1 is None and tr2 is None:
        return None

    tr_ret = CSTransform()
    if tr1 is not None:
        tr_ret.SetMatrix(tr1.GetMatrix(), True)
    if tr2 is not None:
        tr_ret.SetMatrix(tr2.GetMatrix(), True)
    return tr_ret