## Algorithm example 4 - Cone quad intersection

A cone is a geometrical shape similar to the waffle under ice cream and a quad is a surface polygon defined by four points in three dimensions. Parallelism inside an ordinary cone quad intersection will be utilized instead of intersecting cone against four quads or four cones against quad. This has the advantage, that nothing outside of the function has to be changed and the disadvantage, that not all parts of the algorithm can be vectorized.

To intersect a cone against a quad three things have to be checked: First whether any of the quad's vertices are inside the cone. Secondly whether any edge of the quad intersects the cone. Thirdly whether the axis of the cone intersects the quad. These can be done in arbitrary order and the order should be selected on the basis of probability for a test to succeed in an application.

Implementation here is based on "Intersection of a Triangle and a Cone by David Eberly", a copy of which is distributed with the source package. The SSE version was implemented before the scalar version:

```//	D = cone direction
//	L = cone starting position
//	cphi2 = cosine power 2 of cone angle
//
noinline bool ConeQuadIntersection(const vec3& D, const vec3& L, const float& cphi2, mat4x3& P)
{
vec4 LP0 = P - L;
vec4 LP1 = P - L;
vec4 LP2 = P - L;
vec4 LPLP = LP0*LP0 + LP1*LP1 + LP2*LP2;
vec4 cLPLP = cphi2*LPLP;
vec4 LP0D = LP0*D + LP1*D + LP2*D;
vec4 LP0D2= LP0D*LP0D;

// check if any vertex is inside cone
vec4b conesidemask = LP0D >= 0.0f;
{
vec4b insidemask = LP0D2 >= cLPLP;

return true;
}
else
return false; // all on other side of the plane

// Check if any edge intersects cone
vec4 E0,E1,E2; // Edge vectors
E0 = P - P;
E0 = P - P;
E0 = P - P;
E0 = P - P;

E1 = P - P;
E1 = P - P;
E1 = P - P;
E1 = P - P;

E2 = P - P;
E2 = P - P;
E2 = P - P;
E2 = P - P;

vec4 DE = D*E0 + D*E1 + D*E2;
vec4 c0 = LP0D2 - cLPLP;
vec4 c1 = DE * LP0D - cphi2 * (E0*LP0 + E1*LP1 + E2*LP2 );
vec4 Elen2 = E0*E0 + E1*E1 + E2*E2;
vec4 c2 = DE*DE - cphi2 * Elen2;

vec4b mask = c2 < 0.0f && c1*c1 >= c0*c2;

if (ForAny(mask)) // some edge can intersect
{
if (ForAll(conesidemask)) // all on the cone side
{
vec4b mask2 = mask && 0.0f <= c1 && c1 <= (-1.0f*c2);
return true;
}
else
{
// unimplemented
}
}

// Check if cone axis intersects the quad
vec3 hitpoint;
vec3 planepoint;
planepoint=P;
planepoint=P;
planepoint=P;
vec3 planenormal;
planenormal=E1*E2-E1*E2;
planenormal=-E0*E2+E0*E2;
planenormal=E0*E1-E0*E1;
float length = 1.0f/sqrtf(planenormal*planenormal + planenormal*planenormal + planenormal*planenormal);
planenormal *= length;

if (!RayPlaneIntersection(L, D, planenormal, planepoint, hitpoint))
return false;

vec3 point = hitpoint - planepoint;

float p1 = ( point * E0 + point * E1 + point * E2 ) / Elen2;
float p2 = ( point * E0 + point * E1 + point * E2 ) / Elen2;

if (p1 >= 0.0f && p1 <= 1.0f && p2 >= 0.0f && p2 <= 1.0f) // cone inside quad?
return true;
else
return false;
}
```

Basically the first part (vertices inside cone) is vectorized, the second part (edges intersect cone) is vectorized and the last part (cone axis intersects quad) is not. The parallelism comes from there being four vertices and four edges to check in identical way. However an optimal implementation is little different between the scalar and vectorized version. The optimal vectorized version was perhaps even easier to code than the scalar one, because it is very straightforward. In the scalar version if the first vertex or edge test succeeds then the function can immediately return, which lowers the benefit from the vectorized version. The optimal edges to test are also different between the two.

```// define scalar versions of vec4 and mat4x3
typedef n_std::cvalarray<n_std::cvalarray<float,4>,3> smat4x3;
typedef n_std::cvalarray<float,4> svec4;
typedef n_std::cvalarray<bool,4> svec4b;

bool ConeQuadIntersection(const vec3& dir, const vec3& pos, const float cosphi2, const smat4x3& P)
{
svec4 pospx,pospy,pospz;
svec4 pospdir;
svec4 posp2;
svec4 pospdir2;

bool anyin=false;
svec4b otherside;

// Check if any of the points in inside the cone
for (int i=0;i<4;i++)
{
pospx[i]=P[i] - pos;
pospy[i]=P[i] - pos;
pospz[i]=P[i] - pos;
pospdir[i] = pospx[i] * dir + pospy[i] * dir + pospz[i] * dir;

if (pospdir[i] > 0)
{
anyin=true;
otherside[i]=false;
posp2[i] = pospx[i]*pospx[i] + pospy[i]*pospy[i] + pospz[i]*pospz[i];
pospdir2[i] = pospdir[i]*pospdir[i];
if (pospdir2[i] >= cosphi2 * posp2[i])
return true;
}
else
otherside[i]=true;
}

// all on other side of cone
if (!anyin)
return false;

// Next test edges of the quad for intersection

vec3 edge1;
edge1 = P - P;
edge1 = P - P;
edge1 = P - P;
float edge1l2 = edge1*edge1 + edge1*edge1 + edge1*edge1;

if (!otherside && !otherside)
{
float diredge = dir*edge1 + dir*edge1 + dir*edge1;
float c0 = pospdir2 - cosphi2*posp2;
float c1 = diredge * pospdir - cosphi2 * (edge1*pospx + edge1*pospy + edge1*pospz);
float c2 = diredge*diredge - cosphi2 * edge1l2;

if (c2 < 0.0f && 0.0f <= c1 && c1 <= (-1.0f)*c2 && c1*c1 >= c0*c2)
return true;
}

vec3 edge2;
edge2 = P - P;
edge2 = P - P;
edge2 = P - P;
float edge2l2 = edge2*edge2 + edge2*edge2 + edge2*edge2;

if (!otherside && !otherside)
{
float diredge = dir*edge2 + dir*edge2 + dir*edge2;
float c0 = pospdir2 - cosphi2*posp2;
float c1 = diredge * pospdir - cosphi2 * (edge2*pospx + edge2*pospy + edge2*pospz);
float c2 = diredge*diredge - cosphi2 * edge2l2;

if (c2 < 0.0f && 0.0f <= c1 && c1 <= (-1.0f)*c2 && c1*c1 >= c0*c2;
return true;
}

for (int i=1;i<=2;i++)
{
if (!otherside && !otherside[i])
{
vec3 edge;
edge = P[i] - P;
edge = P[i] - P;
edge = P[i] - P;
float diredge = dir*edge + dir*edge + dir*edge;
float c0 = pospdir2 - cosphi2*posp2;
float c1 = diredge * pospdir - cosphi2 * (edge*pospx + edge*pospy + edge*pospz);
float c2 = diredge*diredge - cosphi2 * (edge*edge + edge*edge + edge*edge);

if (c2 < 0.0f && 0.0f <= c1 && c1 <= (-1.0f)*c2 && c1*c1 >= c0*c2)
return true;
}
}

// Next check if cone axis intersects quad

vec3 hitpoint;
vec3 planepoint;
planepoint=P;
planepoint=P;
planepoint=P;
vec3 planenormal;

planenormal=edge1*edge2-edge2*edge1;
planenormal=-edge1*edge2+edge2*edge1;
planenormal=edge1*edge2-edge2*edge1;
float length = 1.0f/sqrtf(planenormal*planenormal + planenormal*planenormal + planenormal*planenormal);
planenormal *= length;

if (!RayPlaneIntersection(pos, dir, planenormal, planepoint, hitpoint))
return false;

vec3 point = hitpoint - planepoint;

float p1 = ( point * edge1 + point * edge1 + point * edge1 ) / edge1l2;
float p2 = ( point * edge2 + point * edge2 + point * edge2 ) / edge2l2;

if (p1 >= 0.0f && p1 <= 1.0f && p2 >= 0.0f && p2 <= 1.0f) // cone inside quad?
return true;
else
return false;

}
```

The measured speedup from the vectorized version is summarized in the following table. The different scenarios mean the data is created so that the condition listed on the left column always happens for all the quads tested.

 Speedup factor for intersecting a dataset of 10000 quads against a cone First tested vertex was inside 0.95x Second tested vertex was inside 1.26x Third tested vertex was inside 1.49x Fourth tested was vertex inside 1.79x First tested edge intersected 1.10x Second tested edge intersected 1.22x Third tested edge intersected 1.58x Fourth tested edge intersected 2.00x Cone axis intersects quad 1.85x

The vectorized version gives a definite benefit, however, at best only doubling the performance. The vectorized version was not any harder to implement than the scalar version.