private void DivideCell(SurfaceRenderStackFrame[] stackFrames, SurfaceFunc func)
{
int depth = 0;
stackFrames[0].Step = 0;
while (depth >= 0)
{
var currentFrame = stackFrames[depth];
if (currentFrame.Step == 0)
{
if (depth == stackFrames.Length - 1 || !IsDivisionRequired(currentFrame, func)) // no division required or division limit reached?
{
// save calculated point projections
for (int x = 0; x <= 2; x++)
for (int y = 0; y <= 2; y++)
if (currentFrame.Points[x, y].Z != null)
AdjustNearest(currentFrame.Points[x, y].XLeftProj, currentFrame.Points[x, y].XRightProj, currentFrame.Points[x, y].YProj);
depth--;
}
else
{
// calculate additional point coordinates and projections (these values will be used for grid subcells)
double[] xCoords = { currentFrame.Points[0, 0].X, (currentFrame.Points[0, 0].X + currentFrame.Points[2, 0].X) / 2, currentFrame.Points[2, 0].X };
double[] yCoords = { currentFrame.Points[0, 0].Y, (currentFrame.Points[0, 0].Y + currentFrame.Points[0, 2].Y) / 2, currentFrame.Points[0, 2].Y };
for (int x = 0; x <= 2; x++)
for (int y = 0; y <= 2; y++)
if (x == 1 || y == 1)
InitPoint(ref currentFrame.Points[x, y], xCoords[x], yCoords[y], func(xCoords[x], yCoords[y]));
currentFrame.Step++;
}
}
else if (currentFrame.Step >= 1 && currentFrame.Step <= 4)
{
// initialize next grid subcell and pass to processing of it
var nextFrame = stackFrames[depth + 1];
nextFrame.Step = 0;
for (int x = 0; x <= 1; x++)
for (int y = 0; y <= 1; y++)
nextFrame.Points[x * 2, y * 2] = currentFrame.Points[(currentFrame.Step - 1) / 2 + x, (currentFrame.Step - 1) % 2 + y];
currentFrame.Step++;
depth++;
}
else
depth--;
}
}