Debian Codegraph @

src/org/stathissideris/ascii2image/text/AbstractionGrid.java

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