<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 plus MathML 2.0//EN" "http://www.w3.org/Math/DTD/mathml2/xhtml-math11-f.dtd" [ <!ENTITY mathml "http://www.w3.org/1998/Math/MathML">
]>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title> Cylinder and Cylinder Intersection </title>
<meta http-equiv="Content-Type" content="text/xml; charset=UTF-8" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<link rel="stylesheet" type="text/css" href="../../comsci/css/s011.css" />
<link rel='SHORTCUT ICON' href='../../comsci/images/FrameHome.ico' />
</head>

<body>

<div class="message_right">
  <a href="http://validator.w3.org/check/referer">

    <img src="http://www.w3.org/Icons/valid-xhtml11" alt="Valid
	XHTML 1.1!" height="31" width="88" />
  </a>

  <a href="http://jigsaw.w3.org/css-validator/">
    <img style="width:88px;height:31px"
       src="http://jigsaw.w3.org/css-validator/images/vcss" 
       alt="Valid CSS!" />
  </a>
  <br />
  Created 2007-04-10 &nbsp; Modified 

<!--UPDATE_DATE_BEGIN-->
2009-04-11<br />
<a class="reflocal" href="../../doc02.html">Chelton Evans</a>

<!--UPDATE_DATE_END-->
</div>


<h1> 
<a href="geom.html"> <img alt="proj" src="../../comsci/images/compgeom.png" /></a>
Cylinder and Cylinder Intersection
<a href="../../../index.html">
<img alt="home" src="../../comsci/images/Frame.gif" /> </a>
</h1>


<p>
<a class="reflocal2" href="#Intro">Intro</a><br />
<a class="reflocal2" href="#Brute_Force">Brute Force</a><br />
<a class="reflocal2" href="#Proposed_Algorithm">
  Proposed Algorithm</a><br />
  &nbsp; &nbsp;
  <a class="reflocal2" href="#Parallel_Cylinders">Parallel Cylinders</a><br />
  &nbsp; &nbsp;
  <a class="reflocal2" href="#Infinite_Cylinders">Infinite Cylinders</a><br />
  &nbsp; &nbsp;
  <a class="reflocal2" href="#Capped_End">Capped End</a><br />
  &nbsp; &nbsp;
  <a class="reflocal" href="../../misc/proj/intersectiontests/doc.html#Experiment">
    C++ Experiment</a><br />
<a class="reflocal2" href="#Algorithm2">Algorithm2</a>
</p>

<div class="float25">
<a id="Intro"></a>
<h2>Intro</h2>

<p>Exploring Cylinder to Cylinder intersection. Primarily
 interested in whether or not a collision between two cylinders
 has occurred. So the intersecting surface does not need to
 be found.
</p>

<p>Let the cylinders generally be finite - fixed in length.
 This does not stop discussing the infinite cylinders case
 as that is the simpler situation.
</p>

<p><a class="reflocal" href="g001.xml#Disk_and_Disk">Disk and Disk</a> intersections
 can be computed in constant time. This is used by the 
 cylinder and cylinder intersection algorithms.
</p>

</div>


<div class="float25">
<a id="Brute_Force"></a>
<h2>Brute Force</h2>

<p>This actually may be the best method. Express the
 cylinders as vector equations. Get a really good
 general purpose numerical equations optimizer. Then
 minimize the two points with a fixed number of iterations.
 If the distance between the two points is below a threshold
 then intersection has occurred.
</p>

<p>Describe a cylinder.</p>


<p class="equ">
Cylinder surface &nbsp; &nbsp; 
<math xmlns="&mathml;">
  <mi>C1</mi>
  <mo>(</mo>
  <mi>t</mi>
  <msub>
    <mi></mi>
    <mrow><mi>1</mi></mrow>
  </msub>
  <mo>,</mo>
  <mi>&theta;</mi>
  <msub>
    <mi></mi>
    <mrow><mi>1</mi></mrow>
  </msub>
  <mo>)</mo>
</math>
<br />
Solid Cylinder &nbsp; &nbsp; 

<math xmlns="&mathml;">
  <mi>C1</mi>
  <mo>(</mo>
  <mi>t</mi>
  <msub>
    <mi></mi>
    <mrow><mi>1</mi></mrow>
  </msub>
  <mo>,</mo>
  <mi>&theta;</mi>
  <msub>
    <mi></mi>
    <mrow><mi>1</mi></mrow>
  </msub>
  <mo>,</mo>
  <mi>r</mi>
  <msub>
    <mi></mi>
    <mrow><mi>1</mi></mrow>
  </msub>
  <mo>)</mo>
</math>

</p>

<p>Solve the equations.</p>

<p class="equ">
Minimize &nbsp; &nbsp; 
<math xmlns="&mathml;">
  <mi>distance</mi>
  <mo>(</mo>
  <mi>C1</mi>
  <mo>-</mo>
  <mi>C2</mi>
  <mo>)</mo>
</math>

</p>

<p>An initial point may be minimizing a point on the lines
 along the cylinders axes, capping the points so they are
 within the cylinder.
</p>

<p>This is either a 4 variable problem with non-linear equations
 or a 6 variable problem with non-linear equations.
</p>

<p>
 I do not know which will perform better as I have not implemented
 these.
</p>

<p>The cylinders normal can be used to find a set of arms 
 perpendicular to the normal and each other. Let the arms
 be unit vectors. A point on
 or inside the cylinder is described by a vector equation.
</p>

<p class="equ">
<math xmlns="&mathml;">
  <mi>C1</mi>
  <mo>=</mo>
  <mi>C1.pos</mi>
  <mo>+</mo>
  <mi>t</mi>
  <msub>
    <mi></mi>
    <mrow><mi>1</mi></mrow>
  </msub>
  <mi>C1.nml</mi>
</math>

<br />
  &nbsp; &nbsp; 
  &nbsp; &nbsp; 
  &nbsp; &nbsp; 
<math xmlns="&mathml;">
  <mo>+</mo>
  <mi>r</mi>
  <msub>
    <mi></mi>
    <mrow><mi>1</mi></mrow>
  </msub>
  <mi>cos</mi>
  <mo>(</mo>
  <mo>&theta;</mo>
  <mo>)</mo>
  <mi>C1.arm1</mi>
</math>

<br />
  &nbsp; &nbsp; 
  &nbsp; &nbsp; 
  &nbsp; &nbsp; 
<math xmlns="&mathml;">
  <mo>+</mo>
  <mi>r</mi>
  <msub>
    <mi></mi>
    <mrow><mi>1</mi></mrow>
  </msub>
  <mi>sin</mi>
  <mo>(</mo>
  <mo>&theta;</mo>
  <mo>)</mo>
  <mi>C1.arm2</mi>
</math>

</p>

<p>The advantage of this method is that it is very easy to
 describe and get the equations.  However you are going
 to have to have access to a numerical solver that is really
 good. This is because the test needs to be as fast
 as possible. If hundreds of thousands of cylinders need to
 be tested then it does matter!
</p>

</div>
<div class="float25">

<a id="Proposed_Algorithm"></a>
<h2>Proposed Algorithm</h2>

<p>This is an algorithm where it may or may not work.
 Clearly the goal is to get it to work, but I am not
 going to prove the algorithm as the aim is to find one
 that detects collision in constant time.
</p>

<p>I will verify the algorithm by moving the cylinders
 in 3D space and looking for a collision which is not
 identified by the algorithm. When a collision occurs
 the cylinders turn red in color. If I find a situation
 where there is an unidentified collision I can save
 the state and reload it later for investigation.
</p>

<p>The trivial case is when the two cylinders are parallel.
 The next case is when the two cylinders are infinite.
 If minimizing the two lines does not result in an 
 end point being forced then the infinite case
 is also valid for this case too.
</p>

<p>Finally the capped end case is tested.
 This is difficult because we face solving inconsistent
 non linear equations. I am not sure how they are
 classified but the final equation can be reduced
 to a linear equation of two variables in one dimension.
 This can be solved in constant time.
 The difficulty lies in that inconsistent equations actually
 represent many different equations when considering if their
 coefficients are zero or not. So there is a danger of
 missing cases.
</p>


</div>

<div class="float25">

<a id="Parallel_Cylinders"></a>
<h3>Parallel Cylinders</h3>

<p>
Use one of the normals as a line equation, find the other
 cylinders t values where its endpoint planes intersect
 the line. If the two line intervals do not intersect
 there is no intersection.
</p>

<p>
Pick a common t point in the intersection and find
 the distance between the two planes. If it is less
 that or equal to the sum of the cylinders radius's then
 intersection has occurred, else there is no intersection.
</p>


<a id="Infinite_Cylinders"></a>
<h3>Infinite Cylinders</h3>

<p>
Minimize the distance between a point on each line
 through the cylinders normals. Then construct
 two discs representing a cylinder slice.
</p>

<p>Let the varying disk be described by a normalized
 cylinder where t=0 is the start of the cylinder
 and t=1 the finish. Cap the t values so if the
 t value is greater than 1 it is made 1, if it is
 less than 0 it is made 0.
</p>

<p>
If these disks intersect then the two cylinders
 are intersecting.
</p>

<p>I do not have proof for this. I did a visual check
 and it seemed ok.
</p>



<p>It is my opinion that this test <b>always</b> works for the
 infinite cylinder case. If wrong then the algorithm will fail.
 The reasoning is that the intersection is going to
 occur where the two lines are closest.
</p>

</div>

<div class="spacer" />

<div class="float25">

<a id="Capped_End"></a>
<h3>Capped End</h3>

<p>The other cases have shown no intersection so one or two
 of the end points are capped.  However from the perspective
 of collision only one of the ends is capped, and the other
 cylinder is as an infinite case (it can freely be varied).
 This is because the test now focuses on finding an intersection
 with the capped disk.
</p>

<p class="equ">
Let 
&nbsp; 
<math xmlns="&mathml;">
  <mi>q</mi>
  <mo>=</mo>
  <mi>A</mi>
  <msub>
    <mi></mi>
    <mrow><mi>1</mi></mrow>
  </msub>
  <mo>+</mo>
  <mi>B</mi>
  <msub>
    <mi></mi>
    <mrow><mi>1</mi></mrow>
  </msub>
  <mi>t</mi>
</math>
 &nbsp; 
 be a point on the infinite cylinders normal. <br />

Let &nbsp; 
<math xmlns="&mathml;">
  <mi>p</mi>
</math>
 &nbsp; be a point in the center of the cap. <br />
Let 
&nbsp; 
<math xmlns="&mathml;">
  <mi>w</mi>
</math>
 &nbsp; 
 if it exists be a point on the infinite cylinders surface. <br />
Let 
&nbsp; 
<math xmlns="&mathml;">
  <mi>r</mi>
</math>
 &nbsp; 
 be the infinite cylinders radius. <br />

Let 
&nbsp; 
<math xmlns="&mathml;">
  <mi>B</mi>
  <msub>
    <mi></mi>
    <mrow><mi>2</mi></mrow>
  </msub>
</math>
 &nbsp; 
 be the capped cylinders normal.
</p>

<p>The equations describing this are given.
 In words the line to the point of intersection
 is at right angles to the line itself.
 The distance of the infinite cylinder to the point
 on the cylinders surface is fixed and hence gives
 the third equation.
</p>

<p>Now this situation is when the cylinders intersect.
 So if the cylinders do not intersect this set of equations
 has no solution.
</p>

</div>

<div class="float25">

<img src="diagg05601.png" alt="diagg056.png" />

<p class="equ">
<math xmlns="&mathml;">
  <mi>B</mi>
  <msub>
    <mi></mi>
    <mrow><mi>1</mi></mrow>
  </msub>
  <mo>&CenterDot;</mo>
  <mo>(</mo>
  <mi>w</mi>
  <mo>-</mo>
  <mi>q</mi>
  <mo>)</mo>
  <mo>=</mo>
  <mi>0</mi>
</math>

<br />

<math xmlns="&mathml;">
  <mi>B</mi>
  <msub>
    <mi></mi>
    <mrow><mi>1</mi></mrow>
  </msub>
  <mo>(</mo>
  <mi>w</mi>
  <mo>-</mo>
  <mi>A</mi>
  <msub>
    <mi></mi>
    <mrow><mi>1</mi></mrow>
  </msub>
  <mo>-</mo>
  <mi>B</mi>
  <msub>
    <mi></mi>
    <mrow><mi>1</mi></mrow>
  </msub>
  <mi>t</mi>
  <mo>)</mo>
  <mo>=</mo>
  <mi>0</mi>
</math>

<br />

<math xmlns="&mathml;">
  <mi>B</mi>
  <msub>
    <mi></mi>
    <mrow><mi>1</mi></mrow>
  </msub>
  <mi>w</mi>
  <mo>-</mo>
  <mi>B</mi>
  <msub>
    <mi></mi>
    <mrow><mi>1</mi></mrow>
  </msub>
  <msup>
    <mi></mi>
    <mrow><mi>2</mi></mrow>
  </msup>
  <mi>t</mi>
  <mo>=</mo>
  <mi>A</mi>
  <msub>
    <mi></mi>
    <mrow><mi>1</mi></mrow>
  </msub>
  <mi>B</mi>
  <msub>
    <mi></mi>
    <mrow><mi>1</mi></mrow>
  </msub>
</math>

</p> 


<p class="equ">
<math xmlns="&mathml;">
  <mi>B</mi>
  <msub>
    <mi></mi>
    <mrow><mi>2</mi></mrow>
  </msub>
  <mo>&CenterDot;</mo>
  <mo>(</mo>
  <mi>w</mi>
  <mo>-</mo>
  <mi>p</mi>
  <mo>)</mo>
  <mo>=</mo>
  <mi>0</mi>
</math>

<br />

<math xmlns="&mathml;">
  <mi>B</mi>
  <msub>
    <mi></mi>
    <mrow><mi>2</mi></mrow>
  </msub>
  <mi>w</mi>
  <mo>=</mo>
  <mi>B</mi>
  <msub>
    <mi></mi>
    <mrow><mi>2</mi></mrow>
  </msub>
  <mi>p</mi>
</math>

</p>


<p class="equ">
<math xmlns="&mathml;">
  <mo>(</mo>
  <mi>q</mi>
  <mo>-</mo>
  <mi>w</mi>
  <mo>)</mo>
  <mo>&CenterDot;</mo>
  <mo>(</mo>
  <mi>q</mi>
  <mo>-</mo>
  <mi>w</mi>
  <mo>)</mo>
  <mo>=</mo>
  <mi>r</mi>
  <msup>
    <mi></mi>
    <mrow><mi>2</mi></mrow>
  </msup>
</math>


</p>

<p>This gives 3 equations with 4 unknowns. Because
 the last equation is a quadratic, substitute the other
 two equations in to it. When eliminating a variable
 test that its coefficient is non-zero. So the elimination
 will have to by dynamic else there are 7 by 7 cases to consider
 (49 different equations to solve for).
 Then solve for t in the quadratic investigating the two solutions
 if the first fails. This last equation is also
 be a quadratic in the solving variable. The last equation has
 two variables in 1D, hopefully the choice of value in the
 second variable does not matter.
</p>

<p>As you can see this is not easy to implement. However if
 the last equation is easy to implement (which it may be),
 then it should work.
</p>

</div>
<div class="float25">

<a id="Algorithm2"></a>
<h2>Algorithm2</h2>

<p>Subdivide a bounding box from 4,8,16,32,.. sides.
 Perhaps choose the cutting planes intersection point
 randomly so that a fixed pattern can not be found.
 If after k iterations no collision is detected then 
 the cylinders to not intersect.
</p>

<p>This is effectively partitioning the circle over time.
</p>

<p>The algorithm is quadratic for each iteration so limit
 the number of possible collisions in these cases.
</p>

<p>This is the easiest algorithm so far to implement.</p>



</div>


</body>
</html>



