Advertisement
Benjamin_Loison

Collision between a segment and a triangle (draft)

Mar 8th, 2019
491
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Latex 9.38 KB | None | 0 0
  1. \documentclass{article}
  2. \usepackage{listings}
  3. \usepackage{amssymb}
  4. \usepackage[left=1cm, right=1cm, top=1cm, bottom=2cm]{geometry}
  5. \usepackage{xcolor}
  6. \lstset{
  7.    frame=tb,
  8.    tabsize=4,
  9.    showstringspaces=false,
  10.    numbers=left,
  11.    commentstyle=\color{gray},
  12.    keywordstyle=\color{blue},
  13.    stringstyle=\color{red}
  14. }
  15.  
  16. \begin{document}
  17.  
  18.    \title{Collision between a segment and a triangle}
  19.    \author{Benjamin Loison}
  20.    \date{8 march 2018}
  21.    \maketitle
  22.  
  23.    \tableofcontents
  24.    \clearpage
  25.  
  26.    \section{Opening speech}
  27.  
  28.         The modus operandi (way of proceeding) is composed of four steps (if one fails, we can directly declare that there isn't any collision between the segment and the triangle):\\
  29.        
  30.         \begin{enumerate}
  31.  
  32.             \item Check whether or not the straight line which supports the segment intersects the plane which supports the triangle (it is an easy mathematic logical operation). Likewise the direction is checked.
  33.             \item Check whether or not the intersection point has a positive scalar factor multiplied by the relative target vector (if we state an endpoint of the segment as the beginning of the target vector and the other endpoint as the end of the target vector). Likewise the way is checked.
  34.           \item Check whether or not the intersection point, inside the triangle plane, is inside the triangle. Likewise this checks whether or not the precise intersection point of the line which supports the segment is in the triangle.
  35.             \item Check whether or not the distance between the origin (of the target vector) and the intersection point is lower or equal to the distance between the origin and the other endpoint of the segment.
  36.            
  37.         \end{enumerate}
  38.        
  39.    \section{Let's dive into mathematics}
  40.        
  41.     \subsection{Check if there is an intersection point}
  42.    
  43.         - The cartesian equation of a line is: $\alpha * x + \beta * y + \gamma * z + \lambda = 0$. Where $\lambda$ is a constant which depends of $\alpha$, $\beta$ and $\gamma$.
  44.         \\Where here:\\
  45.         $\left\{\begin{array}{r c l}
  46.             \alpha=B_x - A_x\\
  47.             \beta=B_y - A_y\\
  48.             \gamma=B_z - A_z\\
  49.         \end{array}\right.$\\
  50.         If we consider $A$ and $B$ the two points of the segment.\\
  51.         Likewise: $\lambda = -(\alpha * A_x + \beta * A_y + \gamma * A_z)$.\\
  52.         (If we consider $A$ as the origin point of the segment (the origin can be an arbitrary choice).)\\
  53.         So finally: $\alpha * x + \beta * y + \gamma * z - (\alpha * A_x + \beta * A_y + \gamma * A_z) = 0$\\
  54.         Id est: $\alpha(x - A_x) + \beta(y - A_y) + \gamma(z - A_z) = 0$\\
  55.         Which is equivalent to:\\
  56.         $$(B_x - A_x)(x - A_x) + (B_y - A_y)(y - A_y) + (B_z - A_z)(z - A_z) = 0$$
  57.         Ie:
  58.         $$\alpha x + \beta y + \gamma z = \alpha A_x + \beta A_y + \gamma A_z$$
  59.         Ie:
  60.         $$x (B_x - A_x) + y (B_y - A_y) + z (B_z - A_z) = A_x (B_x - A_x) + A_y (B_y - A_y) + A_z (B_z - A_z)$$
  61.        
  62.         %Id est: $(B_x - A_x) * x + (B_y - A_y) * y + (B_z - A_z) * z - ((B_x - A_x) * A_x + (B_y - A_y) * A_y + (B_z - A_z) * A_z) = 0$
  63.    
  64.     - The system of equations of the line is:\\
  65.     $\overrightarrow{AM}$ (with $M(x, y, z)$ a point of the line) and $\overrightarrow{AB}$ are collinears if and only if it exists $k \in \mathbb{Z}$, such as:\\
  66.    $\left\{\begin{array}{r c l}
  67.             x - A_x = k * (B_x - A_x)\\
  68.             y - A_y = k * (B_y - A_y)\\
  69.             z - A_z = k * (B_z - A_z)
  70.    \end{array}\right.$\\\\
  71.    
  72.    Which involves: $k = \frac{x - A_x}{B_x - A_x}$
  73.    
  74.     %- The system of equations of the triangle plane is:\\
  75.     %$\left\{\begin{array}{r c l}
  76.             %(C_x - D_x) * x + (C_y - D_y) * y + (C_z - D_z) * z = 0\\
  77.             %(E_x - D_x) * x + (E_y - D_y) * y + (E_z - D_z) * z = 0\\
  78.             %z = 1 (arbitrary)
  79.     %\end{array}\right.$\\\\
  80.     %
  81.     %We are going to use the Cramer's determinant:\\
  82.     %The system is equivalent to:\\
  83.     %$\left\{\begin{array}{r c l}
  84.             %(C_x - D_x) * x + (C_y - D_y) * y + (C_z - D_z) = 0\\
  85.             %(E_x - D_x) * x + (E_y - D_y) * y + (E_z - D_z) = 0\\
  86.     %\end{array}\right.$\\\\
  87.    
  88.     - The cartesian equation of the triangle plane is:\\
  89.     $n_x * x + n_y * y + n_z * z + d' = 0$ where $\vec{n}$ is a normal vector to the triangle plane and $d'$ is a constant.\\
  90.    We compute $\vec{n}$ by using the vector product of two vectors of the triangle like $\overrightarrow{CD}$ and $\overrightarrow{CE}$.\\
  91.    $\vec{n} = CD \wedge CE = (CD_y * CE_z - CD_z * CE_y, CE_x * CD_z - CD_x * CE_z, CD_x * CE_y - CE_x * CD_y)$\\
  92.     Then to calculate $d'$ we inject for instance the coordinates of the point $C$.\\
  93.     $d' = -(n_x * C_x + n_y * C_y + n_z * C_z) = -((CD_y * CE_z - CD_z * CE_y) * C_x + (CE_x * CD_z - CD_x * CE_z) * C_y + (CD_x * CE_y - CE_x * CD_y) * C_z)$\\
  94.     Finally we got: $$n_x * x + n_y * y + n_z * z - (n_x * C_x + n_y * C_y + n_z * C_z) = 0$$ which is equivalent to:\\
  95.    $$n_x * x + n_y * y + n_z * z = n_x * C_x + n_y * C_y + n_z * C_z$$\\
  96.     %Ie:
  97.     %$$x (CD_y * CE_z - CD_z * CE_y) + y (CE_x * CD_z - CD_x * CE_z) + z (CD_x * CE_y - CE_x * CD_y) = (C_x (CD_y * CE_z - CD_z * CE_y) + C_y (CE_x * CD_z - CD_x * CE_z) + C_z (CD_x * CE_y - CE_x * CD_y))$$
  98.        
  99.     %To find a point or points which are in the triangle plane and in the line, we combine both system of equations which gives:\\
  100.     %$\left\{\begin{array}{r c l}
  101.     %       (C_x - D_x) * x + (C_y - D_y) * y + (C_z - D_z) = 0\\
  102.     %       (E_x - D_x) * x + (E_y - D_y) * y + (E_z - D_z) = 0\\
  103.     %\end{array}\right.$\\\\
  104.    
  105.     Let set $z$ to 1, likewise both equations give:\\
  106.     $\left\{\begin{array}{r c l}
  107.             n_x * x + n_y * y = n_x * C_x + n_y * C_y + n_z * C_z - n_z\\
  108.             \alpha x + \beta y = \alpha A_x + \beta A_y + \gamma A_z - \gamma
  109.    \end{array}\right.$\\\\
  110.    
  111.    Which is equivalent to:\\
  112.    $\left\{\begin{array}{r c l}
  113.             n_x x + n_y y = n_x C_x + n_y C_y + n_z (C_z - 1)\\
  114.             \alpha x + \beta y = \alpha A_x + \beta A_y + \gamma (A_z - 1)
  115.    \end{array}\right.$\\\\
  116.    
  117.    Now we use the Cramer's determinant to check whether or not there is a or multiple intersection point:\\
  118.    $\Delta = n_x * \beta - n_y * \alpha$\\
  119.    If $\Delta$ is equal to 0, we need to check if the line is in the triangle plane or is parallel to this plane. (easy with coordinates)\\
  120.    Else we got:\\
  121.    $\left\{\begin{array}{r c l}
  122.             x = \frac{(n_x C_x + n_y C_y + n_z (C_z - 1)) * \beta - n_y * (\alpha A_x + \beta A_y + \gamma (A_z - 1))}{\Delta}\\
  123.             y = \frac{n_x * (\alpha A_x + \beta A_y + \gamma (A_z - 1)) - \alpha * (n_x C_x + n_y C_y + n_z (C_z - 1))}{\Delta}
  124.    \end{array}\right.$\\\\
  125.    
  126.         \begin{lstlisting}[language=C++, caption={Implementation in C++ to check (and compute) if there is an intersection point}]
  127. bool isCollisionSegmentTriangle(Vector3D A, Vector3D B, Vector3D C, Vector3D D,Vector3D E)
  128. {
  129.    Vector3D CD = Vector3D(C, D), CE = Vector3D(C, E), n = crossProduct(CD, CE);
  130.    double alpha = B.X - A.X, beta = B.Y - A.Y, gamma = B.Z - A.Z,
  131.           delta = n.X * beta - n.Y * alpha;
  132.                                
  133.    if(delta == 0)
  134.    {
  135.        // check whether or not the triangle plane contains the line or is strictly parallel
  136.        // we check whether or not a point of which defines the line is in the triangle plane
  137.        return n.X * A.X + n.Y * A.Y + n.Z * A.Z == n.X * C.X + n.Y * C.Y + n.Z * C.Z;
  138.    }
  139.     else
  140.     {
  141.        double x = ((n.X * C.X + n.Y * C.Y + n.Z * (C.Z - 1)) * beta
  142.                   - n.Y * (alpha * A.X + beta * A.Y + gamma * (A.Z - 1)) / delta,
  143.               y = (n.X * (alpha * A.X + beta * A.y + gamma * (A.Z - 1))
  144.                   - alpha * (n.X * C.X + n.Y * C.Y + n.Z * (C.Z - 1))) / delta;
  145.        // TODO: to continue
  146.    }
  147. }
  148.         \end{lstlisting}
  149.    
  150.    \subsection{Check if the intersection point is in the right way}
  151.    
  152.         We need to check if $k$ is superior or equal to 0. (the formula to compute $k$ was given above)
  153.    
  154.                 \begin{lstlisting}[language=C++, caption={Implementation to check if the intersection point is in the right way}]
  155. double k = (x - A.X) / (B.X - A.X);
  156. if(k < 0) return false;
  157. // TODO: to continue...
  158.         \end{lstlisting}
  159.    
  160.    \subsection{For a triangle}
  161.    
  162.         I leave it to the reader to complete by doing a simple test if the intersection point is in the triangle (in the triangle plane) by using a triple verification if the point is on the left if we run all the triangle around. Or by using a simple test like decomposing the coordinates of the intersection point with two vectors of the triangle (which define the triangle plane with the coordinates of a point of the triangle). Likewise there is just a test to perform on the components of the new system of coordinates of the intersection point checking if positive and with a linearization to test if it is in the triangle.
  163.    
  164.    \subsection{For a rectangle}
  165.    
  166.         It is important to deal with a rectangle and not a quadrilateral because we assume that two "random" vectors (of the segments) of the rectangle are perpendicular.
  167.    
  168.         \begin{enumerate}
  169.    
  170.             \item By using twice the algorithm defined above for the triangle (reminder: use the four points in the right order)
  171.    
  172.             \item By decomposing like for the triangle modus operandi and checking if the components of the new syste of coordinates are between (with equal meaning) 0 and 1.
  173.            
  174.         \end{enumerate}
  175.            
  176. \end{document}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement