Skip to content

Commit

Permalink
Use CoordStack for INST Flooding (#676)
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianMestre authored and jacob1 committed Sep 7, 2019
1 parent c08b333 commit da5f806
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 88 deletions.
4 changes: 2 additions & 2 deletions src/simulation/CoordStack.h
Expand Up @@ -42,11 +42,11 @@ class CoordStack
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)
{
Expand Down
148 changes: 62 additions & 86 deletions src/simulation/Simulation.cpp
Expand Up @@ -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;

Expand All @@ -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;
if (TYP(pmap[y][x1-1])!=cm || parts[ID(pmap[y][x1-1])].life!=0)
{
break;
}
x1--;
}
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)
// go right as far as possible
while (x2<XRES-CELL)
{
break;
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;
}
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)
// 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))
{
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 up, 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>=CELL+1)
{
for (x=x1; x<=x2; x++)
else if (y>=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>=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))
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;
}

Expand Down

0 comments on commit da5f806

Please sign in to comment.