def main(): print("Step-by-step transformation:") pt = Point3D(3, 1, 5) print(pt) pt = Transform3D.shear(pt, yx=3, yz=-2) print(pt) pt = Transform3D.translate(pt, -3, 10, 7) print(pt) pt = Transform3D.scale(pt, 3, 3, 3) print(pt) pt = Transform3D.rotate(pt, RotationAxis.x, math.pi / 4) print(pt) pt = Transform3D.reflect(pt, ReflectionPlane.origin) print(pt) print("\nCombined transformation:") builder = Transform3DBuilder() builder.shear(yx=3, yz=-2)\ .translate(-3, 10, 7)\ .scale(3, 3, 3)\ .rotate(RotationAxis.x, math.pi/4)\ .reflect(ReflectionPlane.origin) mtx = builder.build() rmtx = builder.build_reverse() pt2 = Point3D(3, 1, 5) print("Original Point") print(pt2) pt3 = builder.apply(pt2) print("Transformed Point") print(pt3) print("Transformation Reversed") print(builder.apply_reverse(pt3))
def main(): tb = Transform3DBuilder().rotate(RotationAxis.y, math.pi / 4) tb.build() pts = [] for _ in range(100000): pts.append(Point3D(random.random(), random.random(), random.random())) test_with_objects(pts, tb) test_with_primitives(pts, tb)
def reflect(pt: "Point3D", plane: "ReflectionPlane") -> "Point3D": """ Reflect the given point over the specified reflection plane :param pt: a 3D point to reflect :param plane: a ReflectionPlane3D value specifying the plane of reflection in 3D space. :return: A reflected point """ pmx = Matrix.from_point_with_padding(pt) tmx = Transform3D.reflection_matrix(plane) return Point3D(*(tmx * pmx).as_list()[:3])
def apply(self, pt: "Point3D") -> "Point3D": """ :param pt: the point to which the transformation should be applied :return: the point after transformation """ if self._mtx is None: raise Exception('Matrix not built yet') pmx = Matrix.from_point_with_padding(pt) x, y, z, w = (self._mtx * pmx).as_list() # homogenize before returning point return Point3D(x / w, y / w, z / w)
def rotate(pt: "Point3D", axis: "RotationAxis", rads: SupportsFloat = 0) -> "Point3D": """ Rotate a given point by the amount specified (in radians) :param pt: a 3D Point to rotate :param axis: the axis around which to perform the rotation :param rads: the amount of desired rotation (in radians). Default: 0. :return: A point that has been rotated by the given amount """ pmx = Matrix.from_point_with_padding(pt) tmx = Transform3D.rotation_matrix(axis, rads) return Point3D(*(tmx * pmx).as_list()[:3])
def translate(pt: "Point3D", dx: Number = 0, dy: Number = 0, dz: Number = 0) -> "Point3D": """ Translate the given point by the x and y amounts specified :param pt: a 3D point to translate :param dx: change in the x direction. Default: 0. :param dy: change in the y direction. Default: 0. :param dz: change in the z direction. Default: 0. :return: A translated point """ pmx = Matrix.from_point_with_padding(pt) tmx = Transform3D.translation_matrix(dx, dy, dz) return Point3D(*(tmx * pmx).as_list()[:3])
def scale(pt: "Point3D", x: Number = 1, y: Number = 1, z: Number = 1) -> "Point3D": """ Scale a given point by the x, y, and z amounts :param pt: a 3D Point to scale :param x: the amount of scaling in the x direction. Default: 1. :param y: the amount of scaling in the y direction. Default: 1. :param z: the amount of scaling in the z direction. Default: 1. :return: A scaled point """ pmx = Matrix.from_point_with_padding(pt) tmx = Transform3D.scaling_matrix(x, y, z) return Point3D(*(tmx * pmx).as_list()[:3])
def shear(pt: "Point3D", xy: Number = 0, xz: Number = 0, yx: Number = 0, yz: Number = 0, zx: Number = 0, zy: Number = 0) -> "Point3D": """ Shear a given point by the amounts specified :param pt: a 3D Point to shear. Default: 0. :parameter xy: amount of shearing on x values in the y direction. Default: 0. :parameter xz: amount of shearing on x values in the z direction. Default: 0. :parameter yx: amount of shearing on y values in the x direction. Default: 0. :parameter yz: amount of shearing on y values in the z direction. Default: 0. :parameter zy: amount of shearing on z values in the y direction. Default: 0. :parameter zx: amount of shearing on z values in the x direction. Default: 0. :return: A sheared point """ pmx = Matrix.from_point_with_padding(pt) tmx = Transform3D.shearing_matrix(xy, xz, yx, yz, zx, zy) return Point3D(*(tmx * pmx).as_list()[:3])
def main(): builder = Transform3DBuilder() builder.shear(yx=3, yz=-2)\ .translate(-3, 10, 7)\ .scale(3, 3, 3)\ .rotate(RotationAxis.x, math.pi/4)\ .reflect(ReflectionPlane.origin) mtx = builder.build() rmtx = builder.build_reverse() pt1 = Point3D(3, 1, 5) print("Original Point") print(pt1) print("Transformed Point") pt2 = builder.apply(pt1) print(pt2) print("Transformation Reversed") pt3 = builder.apply_reverse(pt2) print(pt3) print("Forward Matrix") print(builder.build()) print("Forward Matrix Inversed") print(builder.build().inverse()) print("Reverse Matrix") print(builder.build_reverse()) m1 = Matrix(4, 4, [2, 5, 0, 8, 1, 4, 2, 6, 7, 8, 9, 3, 1, 5, 7, 8]) print(m1) print(m1.inverse()) m2 = m1 * m1.inverse() print(m2)
import test_context from pythonista3d.graphics.graphics_factory import GraphicsFactory from pythonista3d.points import Point3D from pythonista3d.vectors import Vector3D from pythonista3d.fileformats.stl_file import STLFile, STLMode from pythonista3d.scene3d import Scene3D from pythonista3d.camera import Camera from pythonista3d.mesh import Mesh d = GraphicsFactory.get_delegate() cam = Camera(pos=Point3D(1, -3, -0.1), look_dir=Vector3D(-1, 3, 0), up_dir=Vector3D(0, 0, 1), n_dist=0.01, f_dist=10, fov=75) scn = Scene3D(cam, d) file = STLFile("rsc/monkey_ascii.stl", STLMode.ascii) file.load() obj = Mesh(file) scn.add_mesh(obj) scn.show() scn.render()
import test_context from pythonista3d.graphics.graphics_factory import GraphicsFactory from pythonista3d.points import Point3D from pythonista3d.vectors import Vector3D from pythonista3d.fileformats.stl_file import STLFile, STLMode from pythonista3d.scene3d import Scene3D from pythonista3d.camera import Camera from pythonista3d.mesh import Mesh d = GraphicsFactory.get_delegate() cam = Camera( pos=Point3D(1.3, 1, 3.5), look_dir=Vector3D(-1, -1, -3.5), up_dir=Vector3D(0, 1, 0), n_dist=0.01, f_dist=10, fov=75) scn = Scene3D(cam, d) file = STLFile("rsc/thanks_ascii.stl", STLMode.ascii) file.load() obj = Mesh(file) scn.add_mesh(obj) scn.show() scn.render()
import test_context from pythonista3d.graphics.graphics_factory import GraphicsFactory from pythonista3d.points import Point3D from pythonista3d.vectors import Vector3D from pythonista3d.fileformats.stl_file import STLFile, STLMode from pythonista3d.scene3d import Scene3D from pythonista3d.camera import Camera from pythonista3d.mesh import Mesh d = GraphicsFactory.get_delegate() cam = Camera(pos=Point3D(2, -4, 0), look_dir=Vector3D(-2, 4, 0), up_dir=Vector3D(0, 0, 1), n_dist=0.01, f_dist=10, fov=75) scn = Scene3D(cam, d) file = STLFile("rsc/cube_ascii.stl", STLMode.ascii) file.load() obj = Mesh(file) scn.add_mesh(obj) scn.show() scn.render()
import test_context from pythonista3d.graphics.graphics_factory import GraphicsFactory from pythonista3d.points import Point3D from pythonista3d.vectors import Vector3D from pythonista3d.fileformats.stl_file import STLFile, STLMode from pythonista3d.scene3d import Scene3D from pythonista3d.camera import Camera from pythonista3d.mesh import Mesh d = GraphicsFactory.get_delegate() cam = Camera(pos=Point3D(0, -3, 0), look_dir=Vector3D(0, 3, 0), up_dir=Vector3D(0, 0, 1), n_dist=0.01, f_dist=10, fov=75) scn = Scene3D(cam, d) file = STLFile("rsc/sphere_ascii.stl", STLMode.ascii) file.load() obj = Mesh(file) scn.add_mesh(obj) scn.show() scn.render()
def apply_custom_matrix(pt: "Point3D", mtx: "Matrix") -> "Point3D": pmx = Matrix.from_point_with_padding(pt) return Point3D(*(mtx * pmx).as_list()[:3])
def add_vertex(self, vstr: str): """ Add a vertex to the facet object :param vstr: the string containing the data from a vertex to be parsed and added to the facet object. """ self.vertices.append(Point3D(*[float(n) for n in vstr.split()[-3:]]))