Use CoordStack for INST Flooding (#676)

This commit is contained in:
Sebastián Mestre 2019-09-07 20:56:23 -03:00 committed by jacob1
parent c08b333909
commit da5f8068c0
2 changed files with 68 additions and 92 deletions

View File

@ -42,11 +42,11 @@ public:
stack(NULL),
stack_size(0)
{
stack = (unsigned short(*)[2])(malloc(sizeof(unsigned short)*2*stack_limit));
stack = new unsigned short[stack_limit][2];
}
~CoordStack()
{
free(stack);
delete stack;
}
void push(int x, int y)
{

View File

@ -723,7 +723,6 @@ int Simulation::FloodINST(int x, int y, int fullc, int cm)
int c = TYP(fullc);
int x1, x2;
int coord_stack_limit = XRES*YRES;
unsigned short (*coord_stack)[2];
int coord_stack_size = 0;
int created_something = 0;
@ -745,124 +744,101 @@ int Simulation::FloodINST(int x, int y, int fullc, int cm)
if (TYP(pmap[y][x])!=cm || parts[ID(pmap[y][x])].life!=0)
return 1;
coord_stack = (short unsigned int (*)[2])malloc(sizeof(unsigned short)*2*coord_stack_limit);
coord_stack[coord_stack_size][0] = x;
coord_stack[coord_stack_size][1] = y;
coord_stack_size++;
CoordStack cs;
do
cs.push(x, y);
try
{
coord_stack_size--;
x = coord_stack[coord_stack_size][0];
y = coord_stack[coord_stack_size][1];
x1 = x2 = x;
// go left as far as possible
while (x1>=CELL)
do
{
if (TYP(pmap[y][x1-1])!=cm || parts[ID(pmap[y][x1-1])].life!=0)
cs.pop(x, y);
x1 = x2 = x;
// go left as far as possible
while (x1>=CELL)
{
break;
}
x1--;
}
// go right as far as possible
while (x2<XRES-CELL)
{
if (TYP(pmap[y][x2+1])!=cm || parts[ID(pmap[y][x2+1])].life!=0)
{
break;
}
x2++;
}
// fill span
for (x=x1; x<=x2; x++)
{
if (create_part(-1, x, y, c, ID(fullc))>=0)
created_something = 1;
}
// add vertically adjacent pixels to stack
// (wire crossing for INST)
if (y>=CELL+1 && x1==x2 &&
PMAP_CMP_CONDUCTIVE(pmap[y-1][x1-1], cm) && PMAP_CMP_CONDUCTIVE(pmap[y-1][x1], cm) && PMAP_CMP_CONDUCTIVE(pmap[y-1][x1+1], cm) &&
!PMAP_CMP_CONDUCTIVE(pmap[y-2][x1-1], cm) && PMAP_CMP_CONDUCTIVE(pmap[y-2][x1], cm) && !PMAP_CMP_CONDUCTIVE(pmap[y-2][x1+1], cm))
{
// travelling vertically up, skipping a horizontal line
if (TYP(pmap[y-2][x1])==cm && !parts[ID(pmap[y-2][x1])].life)
{
coord_stack[coord_stack_size][0] = x1;
coord_stack[coord_stack_size][1] = y-2;
coord_stack_size++;
if (coord_stack_size>=coord_stack_limit)
if (TYP(pmap[y][x1-1])!=cm || parts[ID(pmap[y][x1-1])].life!=0)
{
free(coord_stack);
return -1;
break;
}
x1--;
}
}
else if (y>=CELL+1)
{
// go right as far as possible
while (x2<XRES-CELL)
{
if (TYP(pmap[y][x2+1])!=cm || parts[ID(pmap[y][x2+1])].life!=0)
{
break;
}
x2++;
}
// fill span
for (x=x1; x<=x2; x++)
{
if (TYP(pmap[y-1][x])==cm && !parts[ID(pmap[y-1][x])].life)
if (create_part(-1, x, y, c, ID(fullc))>=0)
created_something = 1;
}
// add vertically adjacent pixels to stack
// (wire crossing for INST)
if (y>=CELL+1 && x1==x2 &&
PMAP_CMP_CONDUCTIVE(pmap[y-1][x1-1], cm) && PMAP_CMP_CONDUCTIVE(pmap[y-1][x1], cm) && PMAP_CMP_CONDUCTIVE(pmap[y-1][x1+1], cm) &&
!PMAP_CMP_CONDUCTIVE(pmap[y-2][x1-1], cm) && PMAP_CMP_CONDUCTIVE(pmap[y-2][x1], cm) && !PMAP_CMP_CONDUCTIVE(pmap[y-2][x1+1], cm))
{
// travelling vertically up, skipping a horizontal line
if (TYP(pmap[y-2][x1])==cm && !parts[ID(pmap[y-2][x1])].life)
{
if (x==x1 || x==x2 || y>=YRES-CELL-1 || !PMAP_CMP_CONDUCTIVE(pmap[y+1][x], cm) || PMAP_CMP_CONDUCTIVE(pmap[y+1][x+1], cm) || PMAP_CMP_CONDUCTIVE(pmap[y+1][x-1], cm))
cs.push(x1, y-2);
}
}
else if (y>=CELL+1)
{
for (x=x1; x<=x2; x++)
{
if (TYP(pmap[y-1][x])==cm && !parts[ID(pmap[y-1][x])].life)
{
// if at the end of a horizontal section, or if it's a T junction or not a 1px wire crossing
coord_stack[coord_stack_size][0] = x;
coord_stack[coord_stack_size][1] = y-1;
coord_stack_size++;
if (coord_stack_size>=coord_stack_limit)
if (x==x1 || x==x2 || y>=YRES-CELL-1 || !PMAP_CMP_CONDUCTIVE(pmap[y+1][x], cm) || PMAP_CMP_CONDUCTIVE(pmap[y+1][x+1], cm) || PMAP_CMP_CONDUCTIVE(pmap[y+1][x-1], cm))
{
free(coord_stack);
return -1;
// if at the end of a horizontal section, or if it's a T junction or not a 1px wire crossing
cs.push(x, y-1);
}
}
}
}
}
if (y<YRES-CELL-1 && x1==x2 &&
PMAP_CMP_CONDUCTIVE(pmap[y+1][x1-1], cm) && PMAP_CMP_CONDUCTIVE(pmap[y+1][x1], cm) && PMAP_CMP_CONDUCTIVE(pmap[y+1][x1+1], cm) &&
!PMAP_CMP_CONDUCTIVE(pmap[y+2][x1-1], cm) && PMAP_CMP_CONDUCTIVE(pmap[y+2][x1], cm) && !PMAP_CMP_CONDUCTIVE(pmap[y+2][x1+1], cm))
{
// travelling vertically down, skipping a horizontal line
if (TYP(pmap[y+2][x1])==cm && !parts[ID(pmap[y+2][x1])].life)
if (y<YRES-CELL-1 && x1==x2 &&
PMAP_CMP_CONDUCTIVE(pmap[y+1][x1-1], cm) && PMAP_CMP_CONDUCTIVE(pmap[y+1][x1], cm) && PMAP_CMP_CONDUCTIVE(pmap[y+1][x1+1], cm) &&
!PMAP_CMP_CONDUCTIVE(pmap[y+2][x1-1], cm) && PMAP_CMP_CONDUCTIVE(pmap[y+2][x1], cm) && !PMAP_CMP_CONDUCTIVE(pmap[y+2][x1+1], cm))
{
coord_stack[coord_stack_size][0] = x1;
coord_stack[coord_stack_size][1] = y+2;
coord_stack_size++;
if (coord_stack_size>=coord_stack_limit)
// travelling vertically down, skipping a horizontal line
if (TYP(pmap[y+2][x1])==cm && !parts[ID(pmap[y+2][x1])].life)
{
free(coord_stack);
return -1;
cs.push(x1, y+2);
}
}
}
else if (y<YRES-CELL-1)
{
for (x=x1; x<=x2; x++)
else if (y<YRES-CELL-1)
{
if (TYP(pmap[y+1][x])==cm && !parts[ID(pmap[y+1][x])].life)
for (x=x1; x<=x2; x++)
{
if (x==x1 || x==x2 || y<0 || !PMAP_CMP_CONDUCTIVE(pmap[y-1][x], cm) || PMAP_CMP_CONDUCTIVE(pmap[y-1][x+1], cm) || PMAP_CMP_CONDUCTIVE(pmap[y-1][x-1], cm))
if (TYP(pmap[y+1][x])==cm && !parts[ID(pmap[y+1][x])].life)
{
// if at the end of a horizontal section, or if it's a T junction or not a 1px wire crossing
coord_stack[coord_stack_size][0] = x;
coord_stack[coord_stack_size][1] = y+1;
coord_stack_size++;
if (coord_stack_size>=coord_stack_limit)
if (x==x1 || x==x2 || y<0 || !PMAP_CMP_CONDUCTIVE(pmap[y-1][x], cm) || PMAP_CMP_CONDUCTIVE(pmap[y-1][x+1], cm) || PMAP_CMP_CONDUCTIVE(pmap[y-1][x-1], cm))
{
free(coord_stack);
return -1;
// if at the end of a horizontal section, or if it's a T junction or not a 1px wire crossing
cs.push(x, y+1);
}
}
}
}
}
}
} while (coord_stack_size>0);
free(coord_stack);
} while (cs.getSize()>0);
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
return -1;
}
return created_something;
}