Debian Codegraph @

src/org/stathissideris/ascii2image/graphics/CompositeDiagramShape.java

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
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.graphics;
22
23 import java.util.ArrayList;
24 import java.util.Iterator;
25 import java.util.List;
26
27 import org.stathissideris.ascii2image.core.DebugUtils;
28 import org.stathissideris.ascii2image.text.CellSet;
29 import org.stathissideris.ascii2image.text.TextGrid;
30
31 /**
32 *
33 * @author Efstathios Sideris
34 */
35 public class CompositeDiagramShape extends DiagramComponent {
36
37 private static final boolean DEBUG = false;
38
39 private ArrayList<DiagramShape> shapes = new ArrayList<DiagramShape>();
40
41 public static void main(String[] args) {
42 }
43
44 public static DiagramComponent createFromBoundaryCells(
45 final TextGrid grid,
46 final CellSet boundaryCells,
47 final int cellWidth,
48 final int cellHeight) {
49 return createOpenFromBoundaryCells(
50 grid,
51 boundaryCells,
52 cellWidth, cellHeight,
53 false);
54 }
55
56
57 public static DiagramComponent createOpenFromBoundaryCells(
58 final TextGrid grid,
59 final CellSet boundaryCells,
60 final int cellWidth,
61 final int cellHeight,
62 boolean allRound) {
63
64 if(boundaryCells.getType(grid) != CellSet.TYPE_OPEN) throw new IllegalArgumentException("This shape is closed and cannot be handled by this method");
65 if(boundaryCells.size() == 0) return null;
66
67
68 CompositeDiagramShape compositeShape = new CompositeDiagramShape();
69 TextGrid workGrid = new TextGrid(grid.getWidth(), grid.getHeight());
70 grid.copyCellsTo(boundaryCells, workGrid);
71
72 if(DEBUG) {
73 System.out.println("Making composite shape from grid:");
74 workGrid.printDebug();
75 }
76
77
78 CellSet visitedCells = new CellSet();
79
80 List<DiagramShape> shapes = new ArrayList<DiagramShape>(100);
81
82 for(TextGrid.Cell cell : boundaryCells) {
83 if(workGrid.isLinesEnd(cell)) {
84 CellSet nextCells = workGrid.followCell(cell);
85 shapes.addAll(growEdgesFromCell(workGrid, cellWidth, cellHeight, allRound, nextCells.getFirst(), cell, visitedCells));
86 break;
87 }
88 }
89
90 //dashed shapes should "infect" the rest of the shapes
91 boolean dashedShapeExists = false;
92 for(DiagramShape shape : shapes)
93 if(shape.isStrokeDashed())
94 dashedShapeExists = true;
95
96 for(DiagramShape shape : shapes) {
97 if(dashedShapeExists) shape.setStrokeDashed(true);
98 compositeShape.addToShapes(shape);
99 }
100
101 return compositeShape;
102 }
103
104
105 private static List<DiagramShape> growEdgesFromCell(
106 TextGrid workGrid,
107 final int cellWidth,
108 final int cellHeight,
109 boolean allRound,
110 TextGrid.Cell cell,
111 TextGrid.Cell previousCell,
112 CellSet visitedCells) {
113
114 List<DiagramShape> result = new ArrayList<DiagramShape>(50);
115
116 visitedCells.add(previousCell);
117
118 DiagramShape shape = new DiagramShape();
119
120 shape.addToPoints(makePointForCell(previousCell, workGrid, cellWidth, cellHeight, allRound));
121 if(DEBUG) System.out.println("point at "+previousCell+" (call from line: "+DebugUtils.getLineNumber()+")");
122 if(workGrid.cellContainsDashedLineChar(previousCell)) shape.setStrokeDashed(true);
123
124 boolean finished = false;
125 while(!finished) {
126 visitedCells.add(cell);
127 if(workGrid.isPointCell(cell)) {
128 if(DEBUG) System.out.println("point at "+cell+" (call from line: "+DebugUtils.getLineNumber()+")");
129 shape.addToPoints(makePointForCell(cell, workGrid, cellWidth, cellHeight, allRound));
130 }
131
132 if(workGrid.cellContainsDashedLineChar(cell)) shape.setStrokeDashed(true);
133
134 if(workGrid.isLinesEnd(cell)){
135 finished = true;
136 if(DEBUG) System.out.println("finished shape");
137 }
138
139 CellSet nextCells = workGrid.followCell(cell, previousCell);
140 if(nextCells.size() == 1) {
141 previousCell = cell;
142 cell = (TextGrid.Cell) nextCells.getFirst();
143 } else if(nextCells.size() > 1) {//3- or 4- way intersection
144 finished = true;
145 if(DEBUG) System.out.println("finished shape");
146 for(TextGrid.Cell nextCell : nextCells)
147 result.addAll(growEdgesFromCell(workGrid, cellWidth, cellHeight, allRound, nextCell, cell, visitedCells));
148 }
149 }
150
151 result.add(shape);
152 return result;
153 }
154
155 /**
156 * Returns a new diagram component with the lines of
157 * this CompositeDiagramShape connected. It can a composite
158 * or simple shape
159 *
160 * @return
161 */
162 public DiagramComponent connectLines(){
163 CompositeDiagramShape result = new CompositeDiagramShape();
164
165 //find all lines
166 ArrayList<DiagramShape> lines = new ArrayList<DiagramShape>();
167 Iterator it = shapes.iterator();
168 while(it.hasNext()){
169 DiagramShape shape = (DiagramShape) it.next();
170 if(shape.getPoints().size() == 2){
171 lines.add(shape);
172 }
173 }
174
175 it = lines.iterator();
176 while(it.hasNext()){
177 DiagramShape line1 = (DiagramShape) it.next();
178 Iterator it2 = lines.iterator();
179 while(it2.hasNext()){
180 DiagramShape line2 = (DiagramShape) it.next();
181 ShapePoint commonPoint = null;
182 ShapePoint line1UncommonPoint = null;
183 ShapePoint line2UncommonPoint = null;
184 if(line1.getPoint(0).equals(line2.getPoint(0))){
185 commonPoint = line1.getPoint(0);
186 line1UncommonPoint = line1.getPoint(1);
187 line2UncommonPoint = line2.getPoint(1);
188 }
189 if(line1.getPoint(0).equals(line2.getPoint(1))){
190 commonPoint = line1.getPoint(0);
191 line1UncommonPoint = line1.getPoint(1);
192 line2UncommonPoint = line2.getPoint(0);
193 }
194 if(line1.getPoint(1).equals(line2.getPoint(0))){
195 commonPoint = line1.getPoint(1);
196 line1UncommonPoint = line1.getPoint(0);
197 line2UncommonPoint = line2.getPoint(1);
198 }
199 if(line1.getPoint(1).equals(line2.getPoint(1))){
200 commonPoint = line1.getPoint(1);
201 line1UncommonPoint = line1.getPoint(0);
202 line2UncommonPoint = line2.getPoint(0);
203 }
204 if(commonPoint != null){
205
206 }
207 }
208 }
209
210 return result;
211 }
212
213 public void connectEndsToAnchors(TextGrid grid, Diagram diagram){
214 Iterator it = shapes.iterator();
215 while (it.hasNext()) {
216 DiagramShape shape = (DiagramShape) it.next();
217 if(!shape.isClosed()){
218 shape.connectEndsToAnchors(grid, diagram);
219 }
220 }
221 }
222
223 private static DiagramShape makeLine(TextGrid grid, TextGrid.Cell start, TextGrid.Cell end, int cellWidth, int cellHeight){
224 DiagramShape line = new DiagramShape();
225
226 if(grid.isHorizontalLine(start)){
227 if(start.isWestOf(end)){
228 line.addToPoints(new ShapePoint(
229 Diagram.getCellMinX(start, cellWidth),
230 Diagram.getCellMidY(start, cellHeight)));
231 } else {
232 line.addToPoints(new ShapePoint(
233 Diagram.getCellMaxX(start, cellWidth),
234 Diagram.getCellMidY(start, cellHeight)));
235 }
236 } else if(grid.isVerticalLine(start)){
237 if(start.isNorthOf(end)){
238 line.addToPoints(new ShapePoint(
239 Diagram.getCellMidX(start, cellWidth),
240 Diagram.getCellMinY(start, cellHeight)));
241 } else {
242 line.addToPoints(new ShapePoint(
243 Diagram.getCellMidX(start, cellWidth),
244 Diagram.getCellMaxY(start, cellHeight)));
245 }
246 } else { //corner
247 if(DEBUG) System.out.println("Corner");
248 int type = (grid.isRoundCorner(start))?ShapePoint.TYPE_ROUND:ShapePoint.TYPE_NORMAL;
249 line.addToPoints(new ShapePoint(
250 Diagram.getCellMidX(start, cellWidth),
251 Diagram.getCellMidY(start, cellHeight),
252 type));
253
254 }
255
256 if(grid.isHorizontalLine(end)){
257 if(start.isWestOf(start)){
258 line.addToPoints(new ShapePoint(
259 Diagram.getCellMinX(end, cellWidth),
260 Diagram.getCellMidY(end, cellHeight)));
261 } else {
262 line.addToPoints(new ShapePoint(
263 Diagram.getCellMaxX(end, cellWidth),
264 Diagram.getCellMidY(end, cellHeight)));
265 }
266 } else if(grid.isVerticalLine(end)){
267 if(start.isNorthOf(start)){
268 line.addToPoints(new ShapePoint(
269 Diagram.getCellMidX(end, cellWidth),
270 Diagram.getCellMinY(end, cellHeight)));
271 } else {
272 line.addToPoints(new ShapePoint(
273 Diagram.getCellMidX(end, cellWidth),
274 Diagram.getCellMaxY(end, cellHeight)));
275 }
276 } else { //corner
277 int type = (grid.isRoundCorner(end))?ShapePoint.TYPE_ROUND:ShapePoint.TYPE_NORMAL;
278 if(DEBUG) System.out.println("Corner");
279 line.addToPoints(new ShapePoint(
280 Diagram.getCellMidX(end, cellWidth),
281 Diagram.getCellMidY(end, cellHeight),
282 type));
283
284 }
285
286
287 return line;
288 }
289
290 public void addToShapes(DiagramShape shape){
291 shapes.add(shape);
292 }
293
294 private Iterator getShapesIterator(){
295 return shapes.iterator();
296 }
297
298 public void scale(float factor){
299 Iterator it = getShapesIterator();
300 while(it.hasNext()){
301 DiagramShape shape = (DiagramShape) it.next();
302 shape.scale(factor);
303 }
304 }
305 /**
306 * @return
307 */
308 public ArrayList getShapes() {
309 return shapes;
310 }
311
312 }
313