Use CoordStack for INST Flooding (#676)
This commit is contained in:
parent
c08b333909
commit
da5f8068c0
@ -42,11 +42,11 @@ public:
|
|||||||
stack(NULL),
|
stack(NULL),
|
||||||
stack_size(0)
|
stack_size(0)
|
||||||
{
|
{
|
||||||
stack = (unsigned short(*)[2])(malloc(sizeof(unsigned short)*2*stack_limit));
|
stack = new unsigned short[stack_limit][2];
|
||||||
}
|
}
|
||||||
~CoordStack()
|
~CoordStack()
|
||||||
{
|
{
|
||||||
free(stack);
|
delete stack;
|
||||||
}
|
}
|
||||||
void push(int x, int y)
|
void push(int x, int y)
|
||||||
{
|
{
|
||||||
|
@ -723,7 +723,6 @@ int Simulation::FloodINST(int x, int y, int fullc, int cm)
|
|||||||
int c = TYP(fullc);
|
int c = TYP(fullc);
|
||||||
int x1, x2;
|
int x1, x2;
|
||||||
int coord_stack_limit = XRES*YRES;
|
int coord_stack_limit = XRES*YRES;
|
||||||
unsigned short (*coord_stack)[2];
|
|
||||||
int coord_stack_size = 0;
|
int coord_stack_size = 0;
|
||||||
int created_something = 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)
|
if (TYP(pmap[y][x])!=cm || parts[ID(pmap[y][x])].life!=0)
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
coord_stack = (short unsigned int (*)[2])malloc(sizeof(unsigned short)*2*coord_stack_limit);
|
CoordStack cs;
|
||||||
coord_stack[coord_stack_size][0] = x;
|
|
||||||
coord_stack[coord_stack_size][1] = y;
|
|
||||||
coord_stack_size++;
|
|
||||||
|
|
||||||
do
|
cs.push(x, y);
|
||||||
|
|
||||||
|
try
|
||||||
{
|
{
|
||||||
coord_stack_size--;
|
do
|
||||||
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)
|
|
||||||
{
|
{
|
||||||
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;
|
if (TYP(pmap[y][x1-1])!=cm || parts[ID(pmap[y][x1-1])].life!=0)
|
||||||
}
|
|
||||||
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)
|
|
||||||
{
|
{
|
||||||
free(coord_stack);
|
break;
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
|
x1--;
|
||||||
}
|
}
|
||||||
}
|
// go right as far as possible
|
||||||
else if (y>=CELL+1)
|
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++)
|
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
|
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))
|
||||||
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)
|
|
||||||
{
|
{
|
||||||
free(coord_stack);
|
// if at the end of a horizontal section, or if it's a T junction or not a 1px wire crossing
|
||||||
return -1;
|
cs.push(x, y-1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (y<YRES-CELL-1 && x1==x2 &&
|
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+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))
|
!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)
|
|
||||||
{
|
{
|
||||||
coord_stack[coord_stack_size][0] = x1;
|
// travelling vertically down, skipping a horizontal line
|
||||||
coord_stack[coord_stack_size][1] = y+2;
|
if (TYP(pmap[y+2][x1])==cm && !parts[ID(pmap[y+2][x1])].life)
|
||||||
coord_stack_size++;
|
|
||||||
if (coord_stack_size>=coord_stack_limit)
|
|
||||||
{
|
{
|
||||||
free(coord_stack);
|
cs.push(x1, y+2);
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
else if (y<YRES-CELL-1)
|
||||||
else if (y<YRES-CELL-1)
|
|
||||||
{
|
|
||||||
for (x=x1; x<=x2; x++)
|
|
||||||
{
|
{
|
||||||
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
|
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))
|
||||||
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)
|
|
||||||
{
|
{
|
||||||
free(coord_stack);
|
// if at the end of a horizontal section, or if it's a T junction or not a 1px wire crossing
|
||||||
return -1;
|
cs.push(x, y+1);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
} while (cs.getSize()>0);
|
||||||
} while (coord_stack_size>0);
|
}
|
||||||
free(coord_stack);
|
catch (std::exception& e)
|
||||||
|
{
|
||||||
|
std::cerr << e.what() << std::endl;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
return created_something;
|
return created_something;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user