rename in DividePoly

This commit is contained in:
ikpil 2023-06-03 22:13:27 +09:00
parent f29fefb149
commit bad1d7590a
1 changed files with 59 additions and 69 deletions

View File

@ -25,7 +25,6 @@ using static DotRecast.Recast.RecastConstants;
namespace DotRecast.Recast namespace DotRecast.Recast
{ {
public class RecastRasterization public class RecastRasterization
{ {
/** /**
@ -151,79 +150,79 @@ namespace DotRecast.Recast
} }
} }
/** /// Divides a convex polygon of max 12 vertices into two convex polygons
* Divides a convex polygon of max 12 vertices into two convex polygons across a separating axis. /// across a separating axis.
* ///
* @param inVerts /// @param[in] inVerts The input polygon vertices
* The input polygon vertices /// @param[in] inVertsCount The number of input polygon vertices
* @param inVertsOffset /// @param[out] outVerts1 Resulting polygon 1's vertices
* The offset of the first polygon vertex /// @param[out] outVerts1Count The number of resulting polygon 1 vertices
* @param inVertsCount /// @param[out] outVerts2 Resulting polygon 2's vertices
* The number of input polygon vertices /// @param[out] outVerts2Count The number of resulting polygon 2 vertices
* @param outVerts1 /// @param[in] axisOffset THe offset along the specified axis
* The offset of the resulting polygon 1's vertices /// @param[in] axis The separating axis
* @param outVerts2 private static void DividePoly(float[] inVerts, int inVertsOffset, int inVertsCount,
* The offset of the resulting polygon 2's vertices int outVerts1, out int outVerts1Count,
* @param axisOffset int outVerts2, out int outVerts2Count,
* The offset along the specified axis float axisOffset, int axis)
* @param axis
* The separating axis
* @return The number of resulting polygon 1 and polygon 2 vertices
*/
private static int[] DividePoly(float[] inVerts, int inVertsOffset, int inVertsCount, int outVerts1, int outVerts2, float axisOffset,
int axis)
{ {
float[] d = new float[12]; float[] d = new float[12];
for (int i = 0; i < inVertsCount; ++i)
d[i] = axisOffset - inVerts[inVertsOffset + i * 3 + axis];
int m = 0, n = 0; // How far positive or negative away from the separating axis is each vertex.
for (int i = 0, j = inVertsCount - 1; i < inVertsCount; j = i, ++i) for (int inVert = 0; inVert < inVertsCount; ++inVert)
{ {
bool ina = d[j] >= 0; d[inVert] = axisOffset - inVerts[inVertsOffset + inVert * 3 + axis];
bool inb = d[i] >= 0; }
int poly1Vert = 0;
int poly2Vert = 0;
for (int inVertA = 0, inVertB = inVertsCount - 1; inVertA < inVertsCount; inVertB = inVertA, ++inVertA)
{
bool ina = d[inVertB] >= 0;
bool inb = d[inVertA] >= 0;
if (ina != inb) if (ina != inb)
{ {
float s = d[j] / (d[j] - d[i]); float s = d[inVertB] / (d[inVertB] - d[inVertA]);
inVerts[outVerts1 + m * 3 + 0] = inVerts[inVertsOffset + j * 3 + 0] inVerts[outVerts1 + poly1Vert * 3 + 0] = inVerts[inVertsOffset + inVertB * 3 + 0] +
+ (inVerts[inVertsOffset + i * 3 + 0] - inVerts[inVertsOffset + j * 3 + 0]) * s; (inVerts[inVertsOffset + inVertA * 3 + 0] - inVerts[inVertsOffset + inVertB * 3 + 0]) * s;
inVerts[outVerts1 + m * 3 + 1] = inVerts[inVertsOffset + j * 3 + 1] inVerts[outVerts1 + poly1Vert * 3 + 1] = inVerts[inVertsOffset + inVertB * 3 + 1] +
+ (inVerts[inVertsOffset + i * 3 + 1] - inVerts[inVertsOffset + j * 3 + 1]) * s; (inVerts[inVertsOffset + inVertA * 3 + 1] - inVerts[inVertsOffset + inVertB * 3 + 1]) * s;
inVerts[outVerts1 + m * 3 + 2] = inVerts[inVertsOffset + j * 3 + 2] inVerts[outVerts1 + poly1Vert * 3 + 2] = inVerts[inVertsOffset + inVertB * 3 + 2] +
+ (inVerts[inVertsOffset + i * 3 + 2] - inVerts[inVertsOffset + j * 3 + 2]) * s; (inVerts[inVertsOffset + inVertA * 3 + 2] - inVerts[inVertsOffset + inVertB * 3 + 2]) * s;
RcVec3f.Copy(inVerts, outVerts2 + n * 3, inVerts, outVerts1 + m * 3); RcVec3f.Copy(inVerts, outVerts2 + poly2Vert * 3, inVerts, outVerts1 + poly1Vert * 3);
m++; poly1Vert++;
n++; poly2Vert++;
// add the i'th point to the right polygon. Do NOT add points that are on the dividing line // add the i'th point to the right polygon. Do NOT add points that are on the dividing line
// since these were already added above // since these were already added above
if (d[i] > 0) if (d[inVertA] > 0)
{ {
RcVec3f.Copy(inVerts, outVerts1 + m * 3, inVerts, inVertsOffset + i * 3); RcVec3f.Copy(inVerts, outVerts1 + poly1Vert * 3, inVerts, inVertsOffset + inVertA * 3);
m++; poly1Vert++;
} }
else if (d[i] < 0) else if (d[inVertA] < 0)
{ {
RcVec3f.Copy(inVerts, outVerts2 + n * 3, inVerts, inVertsOffset + i * 3); RcVec3f.Copy(inVerts, outVerts2 + poly2Vert * 3, inVerts, inVertsOffset + inVertA * 3);
n++; poly2Vert++;
} }
} }
else // same side else // same side
{ {
// add the i'th point to the right polygon. Addition is done even for points on the dividing line // add the i'th point to the right polygon. Addition is done even for points on the dividing line
if (d[i] >= 0) if (d[inVertA] >= 0)
{ {
RcVec3f.Copy(inVerts, outVerts1 + m * 3, inVerts, inVertsOffset + i * 3); RcVec3f.Copy(inVerts, outVerts1 + poly1Vert * 3, inVerts, inVertsOffset + inVertA * 3);
m++; poly1Vert++;
if (d[i] != 0) if (d[inVertA] != 0)
continue; continue;
} }
RcVec3f.Copy(inVerts, outVerts2 + n * 3, inVerts, inVertsOffset + i * 3); RcVec3f.Copy(inVerts, outVerts2 + poly2Vert * 3, inVerts, inVertsOffset + inVertA * 3);
n++; poly2Vert++;
} }
} }
return new int[] { m, n }; outVerts1Count = poly1Vert;
outVerts2Count = poly2Vert;
} }
/** /**
@ -300,14 +299,9 @@ namespace DotRecast.Recast
{ {
// Clip polygon to row. Store the remaining polygon as well // Clip polygon to row. Store the remaining polygon as well
float cellZ = hfBBMin.z + z * cellSize; float cellZ = hfBBMin.z + z * cellSize;
int[] nvrowin = DividePoly(buf, @in, nvIn, inRow, p1, cellZ + cellSize, 2); DividePoly(buf, @in, nvIn, inRow, out nvRow, p1, out nvIn, cellZ + cellSize, 2);
nvRow = nvrowin[0]; (@in, p1) = (p1, @in);
nvIn = nvrowin[1];
{
int temp = @in;
@in = p1;
p1 = temp;
}
if (nvRow < 3) if (nvRow < 3)
continue; continue;
@ -340,16 +334,12 @@ namespace DotRecast.Recast
{ {
// Clip polygon to column. store the remaining polygon as well // Clip polygon to column. store the remaining polygon as well
float cx = hfBBMin.x + x * cellSize; float cx = hfBBMin.x + x * cellSize;
int[] nvnv2 = DividePoly(buf, inRow, nv2, p1, p2, cx + cellSize, 0); DividePoly(buf, inRow, nv2, p1, out nv, p2, out nv2, cx + cellSize, 0);
nv = nvnv2[0]; (inRow, p2) = (p2, inRow);
nv2 = nvnv2[1];
{
int temp = inRow;
inRow = p2;
p2 = temp;
}
if (nv < 3) if (nv < 3)
continue; continue;
if (x < 0) if (x < 0)
{ {
continue; continue;