# A G-Code Generator for Volumetric Models
## 3 Materials and Methods
### Orientation  
In an LM process, selecting a good printing direction may improve the fabrication time, surface quality, mechanical strength, and support structures of the finished part [19; 20]. Hence, orientation is important for LM. However, the aforementioned criteria are mutually conflicted [18]. Finding a printing direction to optimize all these factors is, thus, impractical. In the proposed slicer, we oriented the model to maximize its stability such that its height and build time were reduced. Intuitively, a stable model needs less support structures than an unstable one. Thus, our orientation method may also reduce the volume of support structures [20]. In dynamics, the moment of inertia is used to measure how large a torque is required to rotate an object [21]. (The magnitude of the torque is linearly dependent on the moment of inertia.) In this work, we explored the mass distribution of the input model to find a direction, in which the moment of inertia of the input model is maximized. We treated this direction as the printing direction. Therefore, the model will attend maximum stability during the printing process. Besides using the moment of inertia to find the printing direction, we regarded the orientation procedure as a coordinate system transformation process. In this work, we assumed that the origin of the input model space was at (0, 0, 0), and the voxels were at the grid points of a 3D lattice and can be indexed by (\(i,j,k\)). At first, the mass center, \(G\), of the input model is computed:  
\[G=\sum_{i}\sum_{j}\sum_{k}\rho_{i,j,k}V_{i,j,k}/M, \tag{1}\]  
Figure 1: Pipeline of the proposed slicer. where \(M\) represents the mass of the input model, \(V_{i,j,k}\) is the coordinates of the voxel at grid point \((i,j,k)\), and \(\rho_{i,j,k}\) represents the mass of this voxel. If this voxel is a model voxel, then \(\rho_{i,j,k}\) is 1; otherwise, \(\rho_{i,j,k}\) is 0. Once \(G\) has been computed, the origin of the model space is shifted to \(G\), and then the mass distribution of the input model is explored to calculate the new axes of the model space. Since the input model may not be symmetric, its mass distribution should be expressed by an inertia tensor \(I\)[21]. The component tensors of the input model about \(G\) are calculated by  
\[ \begin{array}{l}I_{xx}=\sum\limits_{i,j,k}(y^{2}+z^{2})\rho_{i,j,k}I_{yy}= \sum\limits_{i,j,k}(x^{2}+z^{2})\rho_{i,j,k},I_{zz}=\sum\limits_{i,j,k}(x^{2}+ y^{2})\rho_{i,j,k}\\ I_{xy}=I_{yx}=\sum\limits_{i,j,k}xy\rho_{i,j,k},I_{xz}=I_{xx}=\sum\limits_{i,j, k}xz\rho_{i,j,k},I_{yz}=I_{zy}=\sum\limits_{i,j,k}yz\rho_{i,j,k}\end{array}  \tag{2}\]  
where \(x\), \(y\), and \(z\) stand for the shifted coordinates of the voxel indexed by \((i,j,k)\). The inertia tensor of the input model is represented by the following matrix:  
\[I=\left[ \begin{array}{ccc}I_{xx}&-I_{xy}&-I_{xz}\\ -I_{yx}&I_{yy}&-I_{yz}\\ -I_{zx}&-I_{zy}&I_{zz}\end{array} \right], \tag{3}\]  
Then, we diagonalize \(I\) by using the Jacobi method [22],  
\[I=PDP^{-1},\,P=\left[ \begin{array}{ccc}\overset{\rightarrow}{e}_{0}& \overset{\rightarrow}{e}_{1}&\overset{\rightarrow}{e}_{2}\end{array} \right], \,D=\left[ \begin{array}{ccc}I_{0}&0&0\\ 0&I_{1}&0\\ 0&0&I_{2}\end{array} \right], \tag{4}\]  
where \(P\) is a column matrix of the principal axes of inertia, and \(D\) is a diagonal matrix of the principal inertias of the input model [21]. Then, we searched the principal axis associated with the maximum principal inertia. About this axis, we had to apply the largest torque to rotate the input model [21]. Hence, we chose this axis as the \(z\) axis such that the input model achieved the maximum stability. The other two principal axes were treated as the \(x\) and \(y\) axes of the new data coordinate system. Subsequently, we computed the extents of the input model in both the positive and negative z directions.