--- title: Session. 3D Objects in Python categories: session --- # Learning Objectives **Key learning outcome** Improve the understanding of the mathematical descriptions of 3D Motion, by testing implementations in Python. **Secondary outcomes** if you have time, it is worth browsing different libraries and frameworks to build, visualise, and animate scenes using 3D objects. # Briefing ## Recap: a simple object Remember, last week we worked with this data structure in Python: ``` In [1]: print(vertices) [[-1. 0.5 0.5] [ 1. 0.5 0.5] [ 0. -0.5 0.5] [-1. 0.5 0.5] [ 1. 0.5 0.5] [ 0. 0.5 -0.5] [-1. 0.5 0.5] [ 0. -0.5 0.5] [ 0. 0.5 -0.5] [ 1. 0.5 0.5] [ 0. -0.5 0.5] [ 0. 0.5 -0.5]] ``` The rows are points in 3D. Note that there are only four distinct points. If we divide the matrix into sets of three rows, each triplet defines a triangle. These four triangles form the faces of an irregular tetrahedron. This is a standard way to define a 3D object. More complex objects need more triangles. Note that the textbook have vertices as *column* vectors, while we here use row vectors. This means that we need to transpose matrices when we translate textbook formulæ to python formulæ. Consider, for instance a rotation matrix $$ \begin{align} R = \begin{bmatrix} \cos(\pi/6) & -\sin(\pi/6) & 0 \\ \sin(\pi/6) & \cos(\pi/6) & 0 \\ 0 & 0 & 1 \end{bmatrix} \end{align} $$ ```python from numpy import * R = array( [[ cos(pi/6), -sin(pi/6), 0], [ sin(pi/6), cos(pi/6), 0], [0,0,1]] ) ``` This matrix is orthonormal, which we can check: ```python matmul( R, transpose(R) ) ``` To rotate a vector $\vec{v}$, we would calculate $\vec{u}= R\cdot \vec{v}$, where $\vec{u}$ and $\vec{v}$ are column vectors. If you have a shape $V$ where the columns are points, it could be rotated as $U=R\cdot V$. In python this has to become ```python V = transpose(vertices) U = matmul(R, V) newvertices = transpose(U) ``` ## Homogeneous co-ordinates ## STL files and the STL library # Exercise ## Rotations and translations ## Homogenous Coordinates Motion defined by homogenous matrix ## STL files and the STL library + Load Model + View Model + Change Model + Save Model # Additional Materials + [Other relevant python libraries](Python3D)