# Fast Overlap Detection between Hard-Core Colloidal Cuboids and Spheres. The OCSI Algorithm
## 2 Algorithms  
In geometry, a sphere \(\mathcal{S}\) is identified by its radius, \(R\), and the position of its centre, \(\mathbf{r_{S}}\), with respect to a reference point. Similarly, a cuboid \(\mathcal{C}\) can be defined by the extension of its thickness, \(2c_{T}\), length, \(2c_{L}\) and width, \(2c_{W}\), the position of its centre of mass, \(\mathbf{r_{C}}\) and the unit vectors \(\mathbf{\hat{e}_{T}}\), \(\mathbf{\hat{e}_{L}}\) and \(\mathbf{\hat{e}_{W}}\) that indicate the orientation of its three orthogonal axes. As a result, all the points within the volume occupied by the cuboid can be indicated by a vector \(\mathbf{C}\) that reads  
\[\mathbf{C}=\mathbf{r_{C}}+\sum_{i=T,L,W}\alpha_{i}c_{i}\mathbf{\hat{e}_{i}}, \tag{1}\]  
where \(T\), \(L\) and \(W\) indicate, respectively, the cuboid thickness, length and width, whereas \(\alpha=\big{[}-1,1\big{]}\) is a scalar interval. With these essential definitions, the minimum distance, \(d_{min}\), between the surface of a randomly oriented cuboid and the centre of a sphere can be calculated as follows:  
\[d_{min}=\sqrt{\sum_{i=T,L,W}\Theta\Big{(}\big{|}\mathbf{r_{SC}}\cdot\mathbf{ \hat{e}_{i}}\big{|}-c_{i}\Big{)}\Big{\{}\big{|}\mathbf{r_{SC}}\cdot\mathbf{ \hat{e}_{i}}\big{|}-c_{i}\Big{\}}^{2}}, \tag{2}\]  
where \(\mathbf{r_{SC}}=\mathbf{r_{S}}-\mathbf{r_{C}}\) and \(\Theta\) is the Heaviside step function:  
\[\Theta(x)= \begin{cases}0&x\leq 0\\ 1&x>0\end{cases}  \tag{3}\]  
The interested reader is referred to Appendix A for a formal derivation of Equation (2). To the best of our knowledge, [PERSON] was the first to propose an algorithm detecting the intersection between a sphere and an axis-aligned cuboid, that is a cuboid whose orientation matches that of the reference axes [5]. For this specific case, we assume that the cuboid thickness is aligned with the \(x\) axis, i.e., \(\mathbf{\hat{e}_{T}}=\mathbf{\hat{x}}\), its length with the \(y\) axis, i.e., \(\mathbf{\hat{e}_{L}}=\mathbf{\hat{y}}\) and its width with the \(z\) axis, i.e., \(\mathbf{\hat{e}_{W}}=\mathbf{\hat{z}}\). Following this assumption, Equation (1) can be rewritten as  
\[\mathbf{C}=\mathbf{r_{C}}+\mathbf{\hat{a}}_{T}c_{T}\mathbf{\hat{ x}}+\mathbf{\hat{a}}_{L}c_{L}\mathbf{\hat{y}}+\mathbf{\hat{a}}_{W}c_{W}\mathbf{ \hat{z}}= \tag{4}\] \[=\mathbf{r_{C}}+\big{[}-c_{T},c_{T}\big{]}\mathbf{\hat{x}}+\big{[} -c_{L},c_{L}\big{]}\mathbf{\hat{y}}+\big{[}-c_{W},c_{W}\big{]}\mathbf{\hat{z}}=\] \[=\big{[}r_{C,x}-c_{T},r_{C,x}+c_{T}\big{]}\mathbf{\hat{x}}+\big{[} r_{C,y}-c_{L},r_{C,y}+c_{L}\big{]}\mathbf{\hat{y}}+\] \[+\big{[}r_{C,z}-c_{W},r_{C,z}+c_{W}\big{]}\mathbf{\hat{z}}=\] \[=\sum_{i=x,y,z}B_{i}\mathbf{\hat{z}}\]  
where \(\mathbf{\hat{z}}=\mathbf{\hat{x}},\mathbf{\hat{y}},\mathbf{\hat{z}}\) are the reference axes for \(T\), \(L\) and \(W\), respectively, and \(B_{x}=\big{[}r_{C,x}-c_{T},r_{C,x}+c_{T}\big{]}\), \(B_{y}=\big{[}r_{C,y}-c_{L},r_{C,y}+c_{L}\big{]}\) and \(B_{z}=\big{[}r_{C,z}-c_{W},r_{C,z}+c_{W}\big{]}\). Therefore, for an axis-aligned cuboid, \(d_{min}\) can be calculated as  
\[d_{min}=\sqrt{\sum_{i=x,y,z}\Big{\{}\min\big{(}r_{S,i}-B_{i}\big{)}\Big{\}}^{2}}. \tag{5}\]  
By using the infimum and supremum of \(B_{i}\), the terms in the above sum can be easily calculated:  
1. if \(r_{S,i}<B_{i,inf}\), then \(\min\big{(}r_{S,i}-B_{i}\big{)}=B_{i,inf}-r_{S,i}\),
2. if \(r_{S,i}>B_{i,sup}\), then \(\min\big{(}r_{S,i}-B_{i}\big{)}=r_{S,i}-B_{i,sup}\),
3. if \(r_{S,i}\in B_{i}\), then \(\min\big{(}r_{S,i}-B_{i}\big{)}=0\). Consequently, the algorithm proposed by [PERSON] only requires the extreme values of \(B_{x},B_{y},B_{z}\) along with the sphere radius and position and detects cuboid-sphere collisions if \(d_{min}\leq R\). An illustrative example of a pseudocode describing its main steps is reported in Algorithm 1. The alignment of the cuboid unit vectors with the reference axes is a particular case of a more general scenario with the cuboid randomly oriented.