Debian Codegraph @

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