| 1 |
/* |
| 2 |
* DiTAA - Diagrams Through Ascii Art |
| 3 |
* |
| 4 |
* Copyright (C) 2004 Efstathios Sideris |
| 5 |
* |
| 6 |
* This program is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU General Public License |
| 8 |
* as published by the Free Software Foundation; either version 2 |
| 9 |
* of the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* This program is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with this program; if not, write to the Free Software |
| 18 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 19 |
* |
| 20 |
*/ |
| 21 |
package org.stathissideris.ascii2image.text; |
| 22 |
|
| 23 |
import java.util.ArrayList; |
| 24 |
import java.util.Iterator; |
| 25 |
|
| 26 |
/** |
| 27 |
* |
| 28 |
* @author Efstathios Sideris |
| 29 |
*/ |
| 30 |
public class AbstractionGrid { |
| 31 |
|
| 32 |
private static final boolean DEBUG = false; |
| 33 |
|
| 34 |
private TextGrid grid; |
| 35 |
|
| 36 |
/** |
| 37 |
* Makes an AbstractionGrid using <code>internalGrid</code> as |
| 38 |
* its internal buffer |
| 39 |
* |
| 40 |
* @param internalGrid |
| 41 |
* @return |
| 42 |
*/ |
| 43 |
public static AbstractionGrid makeUsingBuffer(TextGrid internalGrid){ |
| 44 |
if(internalGrid.getWidth() % 3 != 0 |
| 45 |
|| internalGrid.getHeight() % 3 != 0) throw new IllegalArgumentException("Passed TextGrid must have dimensions that are divisible by 3."); |
| 46 |
AbstractionGrid result = new AbstractionGrid(internalGrid.getWidth() / 3, internalGrid.getHeight() / 3); |
| 47 |
result.setInternalBuffer(internalGrid); |
| 48 |
return result; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Makes an AbstractionGrid using the <code>cellSet</code> |
| 53 |
* of <code>textGrid</code>. |
| 54 |
* |
| 55 |
* @param textGrid |
| 56 |
* @param cellSet |
| 57 |
*/ |
| 58 |
public AbstractionGrid(TextGrid textGrid, CellSet cellSet){ |
| 59 |
this(textGrid.getWidth(), textGrid.getHeight()); |
| 60 |
/*this(cellSet.getWidth(), cellSet.getHeight()); |
| 61 |
|
| 62 |
cellSet = new CellSet(cellSet); |
| 63 |
cellSet.translate( - cellSet.getMinX(), - cellSet.getMinY());*/ |
| 64 |
|
| 65 |
if(DEBUG){ |
| 66 |
System.out.println("Making AbstractionGrid using buffer:"); |
| 67 |
textGrid.printDebug(); |
| 68 |
System.out.println("...and the following CellSet:"); |
| 69 |
cellSet.printAsGrid(); |
| 70 |
} |
| 71 |
|
| 72 |
Iterator it = cellSet.iterator(); |
| 73 |
while(it.hasNext()){ |
| 74 |
TextGrid.Cell cell = (TextGrid.Cell) it.next(); |
| 75 |
if(textGrid.isBlank(cell)) continue; |
| 76 |
if(textGrid.isCross(cell)){ |
| 77 |
set(cell.x, cell.y, AbstractCell.makeCross()); |
| 78 |
} else if(textGrid.isT(cell)){ |
| 79 |
set(cell.x, cell.y, AbstractCell.makeT()); |
| 80 |
} else if(textGrid.isK(cell)){ |
| 81 |
set(cell.x, cell.y, AbstractCell.makeK()); |
| 82 |
} else if(textGrid.isInverseT(cell)){ |
| 83 |
set(cell.x, cell.y, AbstractCell.makeInverseT()); |
| 84 |
} else if(textGrid.isInverseK(cell)){ |
| 85 |
set(cell.x, cell.y, AbstractCell.makeInverseK()); |
| 86 |
} else if(textGrid.isCorner1(cell)){ |
| 87 |
set(cell.x, cell.y, AbstractCell.makeCorner1()); |
| 88 |
} else if(textGrid.isCorner2(cell)){ |
| 89 |
set(cell.x, cell.y, AbstractCell.makeCorner2()); |
| 90 |
} else if(textGrid.isCorner3(cell)){ |
| 91 |
set(cell.x, cell.y, AbstractCell.makeCorner3()); |
| 92 |
} else if(textGrid.isCorner4(cell)){ |
| 93 |
set(cell.x, cell.y, AbstractCell.makeCorner4()); |
| 94 |
} else if(textGrid.isHorizontalLine(cell)){ |
| 95 |
set(cell.x, cell.y, AbstractCell.makeHorizontalLine()); |
| 96 |
} else if(textGrid.isVerticalLine(cell)){ |
| 97 |
set(cell.x, cell.y, AbstractCell.makeVerticalLine()); |
| 98 |
} else if(textGrid.isCrossOnLine(cell)){ |
| 99 |
set(cell.x, cell.y, AbstractCell.makeCross()); |
| 100 |
} else if(textGrid.isStarOnLine(cell)){ |
| 101 |
set(cell.x, cell.y, AbstractCell.makeStar()); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
if(DEBUG){ |
| 106 |
System.out.println("...the resulting AbstractionGrid is:"); |
| 107 |
grid.printDebug(); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
private AbstractionGrid(int width, int height){ |
| 112 |
grid = new TextGrid(width*3, height*3); |
| 113 |
} |
| 114 |
|
| 115 |
public TextGrid getCopyOfInternalBuffer(){ |
| 116 |
return new TextGrid(grid); |
| 117 |
} |
| 118 |
|
| 119 |
private void setInternalBuffer(TextGrid grid){ |
| 120 |
this.grid = grid; |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
public int getWidth(){ |
| 125 |
return grid.getWidth() / 3; |
| 126 |
} |
| 127 |
|
| 128 |
public int getHeight(){ |
| 129 |
return grid.getHeight() / 3; |
| 130 |
} |
| 131 |
|
| 132 |
public TextGrid getAsTextGrid(){ |
| 133 |
TextGrid result = new TextGrid(getWidth(), getHeight()); |
| 134 |
for(int y = 0; y < grid.getHeight(); y++){ |
| 135 |
for(int x = 0; x < grid.getWidth(); x++){ |
| 136 |
TextGrid.Cell cell = grid.new Cell(x, y); |
| 137 |
if(!grid.isBlank(cell)) result.set(x/3, y/3, '*'); |
| 138 |
} |
| 139 |
} |
| 140 |
if (DEBUG){ |
| 141 |
System.out.println("Getting AbstractionGrid as textGrid.\nAbstractionGrid:"); |
| 142 |
grid.printDebug(); |
| 143 |
System.out.println("...as text grid:"); |
| 144 |
result.printDebug(); |
| 145 |
} |
| 146 |
|
| 147 |
return result; |
| 148 |
} |
| 149 |
|
| 150 |
public ArrayList getDistinctShapes(){ |
| 151 |
ArrayList result = new ArrayList(); |
| 152 |
|
| 153 |
CellSet nonBlank = grid.getAllNonBlank(); |
| 154 |
ArrayList distinct = nonBlank.breakIntoDistinctBoundaries(); |
| 155 |
|
| 156 |
Iterator it = distinct.iterator(); |
| 157 |
while (it.hasNext()) { |
| 158 |
CellSet set = (CellSet) it.next(); |
| 159 |
AbstractionGrid temp = new AbstractionGrid(this.getWidth(), this.getHeight()); |
| 160 |
temp.fillCells(set); |
| 161 |
result.add(temp.getAsTextGrid().getAllNonBlank()); |
| 162 |
} |
| 163 |
|
| 164 |
return result; |
| 165 |
} |
| 166 |
|
| 167 |
protected void fillCells(CellSet cells){ |
| 168 |
grid.fillCellsWith(cells, '*'); |
| 169 |
} |
| 170 |
|
| 171 |
public void set(int xPos, int yPos, AbstractCell cell){ |
| 172 |
xPos *= 3; |
| 173 |
yPos *= 3; |
| 174 |
for(int y = 0; y < 3; y++){ |
| 175 |
for(int x = 0; x < 3; x++){ |
| 176 |
if(cell.rows[x][y] == 1){ |
| 177 |
grid.set(xPos + x, yPos + y, '*'); |
| 178 |
} |
| 179 |
} |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
} |