Debian Codegraph @

src/org/stathissideris/ascii2image/text/CellSet.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.HashMap;
24 import java.util.HashSet;
•25 import java.util.Hashtable;
26 import java.util.Iterator;
27 import java.util.Set;
28
29 /**
30 *
31 * @author Efstathios Sideris
32 */
33 public class CellSet implements Iterable<TextGrid.Cell> {
34
35 private static final boolean DEBUG = false;
36 private static final boolean VERBOSE_DEBUG = false;
37
38 public static final int TYPE_CLOSED = 0;
39 public static final int TYPE_OPEN = 1;
40 public static final int TYPE_MIXED = 2;
41 public static final int TYPE_HAS_CLOSED_AREA = 3;
42 public static final int TYPE_UNDETERMINED = 4;
43
44 Set<TextGrid.Cell> internalSet = new HashSet<TextGrid.Cell>();
45
46 private int type = TYPE_UNDETERMINED;
47 private boolean typeIsValid = false;
48
49 private static final Object FAKE = new Object();
50
51 public CellSet(){
52
53 }
54
55 public CellSet(CellSet other){
56 addAll(other);
57 }
58
59 public Iterator<TextGrid.Cell> iterator(){
60 return internalSet.iterator();
61 }
62
63 public Object add(TextGrid.Cell cell){
64 return internalSet.add(cell);
65 }
66
67 public void addAll(CellSet set){
68 internalSet.addAll(set.internalSet);
69 }
70
71 void clear(){
72 internalSet.clear();
73 }
74
75 public int size() {
76 return internalSet.size();
77 }
78
79 public TextGrid.Cell getFirst(){
80 //return internalSet.get(0);
81 return (TextGrid.Cell) internalSet.iterator().next();
82 }
83
84 public void printAsGrid(){
85 TextGrid grid = new TextGrid(getMaxX()+2, getMaxY()+2);
86 grid.fillCellsWith(this, '*');
87 grid.printDebug();
88 }
89
90 public void printDebug(){
91 Iterator<TextGrid.Cell> it = iterator();
92 while(it.hasNext()){
93 TextGrid.Cell cell = it.next();
94 System.out.print(cell);
95 if(it.hasNext()) System.out.print(" ");
96 }
97 System.out.println();
98 }
99
100 public String getCellsAsString(){
101 StringBuffer str = new StringBuffer();
102 Iterator<TextGrid.Cell> it = iterator();
103 while(it.hasNext()){
104 str.append(it.next().toString());
105 if(it.hasNext()) str.append("/");
106 }
107 return str.toString();
108 }
109
110 public String toString(){
111 TextGrid grid = new TextGrid(getMaxX()+2, getMaxY()+2);
112 grid.fillCellsWith(this, '*');
113 return grid.getDebugString();
114 }
115
116 /**
117 * Deep copy
118 *
119 * @param set
120 * @return
121 */
122 public static CellSet copyCellSet(CellSet set) {
123 TextGrid grid = new TextGrid();
124 CellSet newSet = new CellSet();
•125
126 Iterator<TextGrid.Cell> it = set.iterator();
127 while (it.hasNext()) {
128 TextGrid.Cell cell = (TextGrid.Cell) it.next();
129 TextGrid.Cell newCell = grid.new Cell(cell);
130 newSet.add(newCell);
131 }
132 return newSet;
133 }
134
135 /*public BoundarySet(BoundarySet set) {
136 Iterator it = set.iterator();
137 while(it.hasNext()){
138 TextGrid.Cell cell = (TextGrid.Cell) it.next();
139 add(new TextGrid.Cell(cell));
140 }
141 }*/
142
143 public int getType(TextGrid grid) {
144 if(typeIsValid) return type;
145 typeIsValid = true;
146 if(size() == 1) {
147 type = TYPE_OPEN;
148 return TYPE_OPEN;
149 }
150 int typeTrace = getTypeAccordingToTraceMethod(grid);
151
152 if(DEBUG){
153 System.out.println("trace: "+typeTrace);
154 }
155
156 if(typeTrace == TYPE_OPEN) {
157 type = TYPE_OPEN;
158 return TYPE_OPEN;
159 }
160 if(typeTrace == TYPE_CLOSED) {
161 type = TYPE_CLOSED;
162 return TYPE_CLOSED;
163 }
164
165 if(typeTrace == TYPE_UNDETERMINED) {
166 int typeFill = getTypeAccordingToFillMethod(grid);
167 if(typeFill == TYPE_HAS_CLOSED_AREA){
168 type = TYPE_MIXED;
169 return TYPE_MIXED;
170 } else if(typeFill == TYPE_OPEN){
171 type = TYPE_OPEN;
172 return TYPE_OPEN;
173 }
174 }
175
176 //in the case that both return undetermined:
177 type = TYPE_UNDETERMINED;
178 return TYPE_UNDETERMINED;
179 }
180
181 private int getTypeAccordingToTraceMethod(TextGrid grid) {
182 if(size() < 2) return TYPE_OPEN;
183
184 TextGrid workGrid = TextGrid.makeSameSizeAs(grid);
185 grid.copyCellsTo(this, workGrid);
186
187 //start with a line end if it exists or with a "random" cell if not
188 TextGrid.Cell start = null;
189 for(TextGrid.Cell cell : this)
190 if(workGrid.isLinesEnd(cell))
191 start = cell;
192 if(start == null) start = (TextGrid.Cell) getFirst();
193
194 if (DEBUG)
195 System.out.println("Tracing:\nStarting at "+start+" ("+grid.getCellTypeAsString(start)+")");
196 TextGrid.Cell previous = start;
197 TextGrid.Cell cell = null;
198 CellSet nextCells = workGrid.followCell(previous);
199 if(nextCells.size() == 0) return TYPE_OPEN;
200 cell = (TextGrid.Cell) nextCells.getFirst();
201 if (DEBUG)
202 System.out.println("\tat cell "+cell+" ("+grid.getCellTypeAsString(cell)+")");
203
204
205 while(!cell.equals(start)){
206 nextCells = workGrid.followCell(cell, previous);
207 if(nextCells.size() == 0) {
208 if (DEBUG)
209 System.out.println("-> Found dead-end, shape is open");
210 return TYPE_OPEN;
211 } if(nextCells.size() == 1) {
212 previous = cell;
213 cell = (TextGrid.Cell) nextCells.getFirst();
214 if (DEBUG)
215 System.out.println("\tat cell "+cell+" ("+grid.getCellTypeAsString(cell)+")");
216 } else if(nextCells.size() > 1) {
217 if (DEBUG)
218 System.out.println("-> Found intersection at cell "+cell);
219 return TYPE_UNDETERMINED;
220 }
221 }
222 if (DEBUG)
223 System.out.println("-> Arrived back to start, shape is closed");
224 return TYPE_CLOSED;
225
226 // boolean hasMoved = false;
227 //
228 // CellSet workSet;
229 // workSet = new CellSet(this);
230 //
231 // TextGrid.Cell start = (TextGrid.Cell) get(0);
232 //
233 // workSet.remove(start);
234 // TextGrid.Cell cell = workSet.findCellNextTo(start);
235 //
236 // while(true && cell != null){
237 //
238 // hasMoved = true;
239 // workSet.remove(cell);
240 //
241 // CellSet setOfNeighbours = workSet.findCellsNextTo(cell);
242 //
243 // if(setOfNeighbours.isEmpty()) break;
244 //
245 // TextGrid.Cell c = null;
246 // if(setOfNeighbours.size() == 1) c = (TextGrid.Cell) setOfNeighbours.get(0);
247 // if(setOfNeighbours.size() > 1) return TYPE_UNDETERMINED;
248 // if(c == null) break;
249 // else cell = c;
250 // }
251 // if(cell != null && start.isNextTo(cell) && hasMoved) return TYPE_CLOSED;
252 // else return TYPE_OPEN;
253 }
254
255 private int getTypeAccordingToFillMethod(TextGrid grid){
256 if(size() == 0) return TYPE_OPEN;
257
258 CellSet tempSet = copyCellSet(this);
259 tempSet.translate( -this.getMinX() + 1, -this.getMinY() + 1);
260 TextGrid subGrid = grid.getSubGrid(getMinX() - 1, getMinY() - 1, getWidth() + 3, getHeight() + 3);
261 AbstractionGrid abstraction = new AbstractionGrid(subGrid, tempSet);
262 TextGrid temp = abstraction.getCopyOfInternalBuffer();
263
264 int width = temp.getWidth();
265 int height = temp.getHeight();
266
267 TextGrid.Cell fillCell = null;
268 for(int y = 0; y < height; y++){
269 for(int x = 0; x < width; x++){
270 TextGrid.Cell cCell = temp.new Cell(x, y);
271 if(temp.isBlank(cCell)){
272 fillCell = cCell;
273 break;
274 }
275 }
276 }
277
278 if(fillCell == null){
279 System.err.println("Unexpected error: fill method cannot fill anywhere");
280 return TYPE_UNDETERMINED;
281 }
282
283 temp.fillContinuousArea(fillCell, '*');
284 if(VERBOSE_DEBUG) {System.out.println("Buffer after filling:"); temp.printDebug();}
285
286 if(temp.hasBlankCells()) return TYPE_HAS_CLOSED_AREA;
287 else return TYPE_OPEN;
288 }
289
290 public void translate(int dx, int dy){
291 typeIsValid = false;
•292 Iterator<TextGrid.Cell> it = iterator();
293 while(it.hasNext()){
•294 TextGrid.Cell cCell = it.next();
295 cCell.x += dx;
296 cCell.y += dy;
297 }
298 }
299
300 public TextGrid.Cell find(TextGrid.Cell cell){
301 Iterator<TextGrid.Cell> it = iterator();
302 while(it.hasNext()){
303 TextGrid.Cell cCell = it.next();
304 if(cCell.equals(cell)) return cCell;
305 }
306 return null;
307 }
308
309 public boolean contains(TextGrid.Cell cell){
310 if(cell == null) return false;
311 return internalSet.contains(cell);
312 }
313
314 // public boolean contains(TextGrid.Cell cell){
315 // Iterator<TextGrid.Cell> it = iterator();
316 // while(it.hasNext()){
317 // TextGrid.Cell cCell = it.next();
318 // if(cCell.equals(cell)) return true;
319 // }
320 // return false;
321 // }
322
323 public void addSet(CellSet set){
324 typeIsValid = false;
325 this.addAll(set);
326 }
327
328 public boolean hasCommonCells(CellSet otherSet){
329 Iterator<TextGrid.Cell> it = iterator();
330 while(it.hasNext()){
331 TextGrid.Cell cell = it.next();
332 if(otherSet.contains(cell)) return true;
333 }
334 return false;
335 }
336
337 public TextGrid.Cell find(int x, int y){
•338 Iterator<TextGrid.Cell> it = iterator();
339 while(it.hasNext()){
•340 TextGrid.Cell cCell = it.next();
341 if(cCell.x == x && cCell.y == y) return cCell;
342 }
343 return null;
344 }
345
346 public CellSet getFilledEquivalent(TextGrid textGrid){
347 if(this.getType(textGrid) == CellSet.TYPE_OPEN) return new CellSet(this);
348 TextGrid grid = new TextGrid(getMaxX()+2, getMaxY()+2);
349 grid.fillCellsWith(this, '*');
350
351 //find a cell that has a blank both on the east and the west
352 TextGrid.Cell cell = null;
353 boolean finished = false;
354 for(int y = 0; y < grid.getHeight() && !finished; y++){
355 for(int x = 0; x < grid.getWidth() && !finished; x++){
356 cell = grid.new Cell(x, y);
357 if(!grid.isBlank(cell)
358 && grid.isBlank(cell.getEast())
359 && grid.isBlank(cell.getWest())){
360 finished = true;
361 }
362 }
363 }
364 if(cell != null){
365 cell = cell.getEast();
366 if(grid.isOutOfBounds(cell)) return new CellSet(this);
367 grid.fillContinuousArea(cell, '*');
368 return grid.getAllNonBlank();
369 }
370 System.err.println("Unexpected error, cannot find the filled equivalent of CellSet");
371 return null;
372 }
373
374 /**
375 * Returns the first cell that is found to be next to <code>cell</code>.
376 *
377 * @param cell
378 * @return
379 */
380 public TextGrid.Cell findCellNextTo(TextGrid.Cell cell){
•381 Iterator<TextGrid.Cell> it = iterator();
382 while(it.hasNext()){
•383 TextGrid.Cell cCell = it.next();
384 if(cCell.isNextTo(cell)) return cCell;
385 }
386 return null;
387 }
388
389 /**
390 * Returns all the cells that are found to be next to <code>cell</code>.
391 *
392 * @param cell
393 * @return
394 */
395 public CellSet findCellsNextTo(TextGrid.Cell cell){
396 if(cell == null) throw new IllegalArgumentException("cell cannot be null");
397 CellSet set = new CellSet();
•398 Iterator<TextGrid.Cell> it = iterator();
399 while(it.hasNext()){
•400 TextGrid.Cell cCell = it.next();
401 if(cCell.isNextTo(cell)) set.add(cCell);
402 }
403 return set;
404 }
405
406 public void appendSet(CellSet set){
407 typeIsValid = false;
•408 Iterator<TextGrid.Cell> it = set.iterator();
409 while(it.hasNext()){
•410 TextGrid.Cell cell = it.next();
411 if(find(cell) == null) add(cell);
412 }
413 }
414
415 public void subtractSet(CellSet set){
416 typeIsValid = false;
•417 Iterator<TextGrid.Cell> it = set.iterator();
418 while(it.hasNext()){
•419 TextGrid.Cell cell = it.next();
420 TextGrid.Cell thisCell = find(cell);
421 if(thisCell != null) remove(thisCell);
422 }
423 }
424
425 public int getWidth(){
426 return getMaxX() - getMinX();
427 }
428
429
430 public int getHeight(){
431 return getMaxY() - getMinY();
432 }
433
434 public int getMaxX(){
435 int result = 0;
•436 Iterator<TextGrid.Cell> it = iterator();
437 while(it.hasNext()){
•438 TextGrid.Cell cell = it.next();
439 if(cell.x > result) result = cell.x;
440 }
441 return result;
442 }
443
444 public int getMinX(){
445 int result = Integer.MAX_VALUE;
•446 Iterator<TextGrid.Cell> it = iterator();
447 while(it.hasNext()){
•448 TextGrid.Cell cell = it.next();
449 if(cell.x < result) result = cell.x;
450 }
451 return result;
452 }
453
454
455 public int getMaxY(){
456 int result = 0;
•457 Iterator<TextGrid.Cell> it = iterator();
458 while(it.hasNext()){
•459 TextGrid.Cell cell = it.next();
460 if(cell.y > result) result = cell.y;
461 }
462 return result;
463 }
464
465 public int getMinY(){
466 int result = Integer.MAX_VALUE;
•467 Iterator<TextGrid.Cell> it = iterator();
468 while(it.hasNext()){
•469 TextGrid.Cell cell = it.next();
470 if(cell.y < result) result = cell.y;
471 }
472 return result;
473 }
474
475
476 public Object remove(TextGrid.Cell cell){
477 typeIsValid = false;
478 cell = find(cell);
479 if(cell != null) return internalSet.remove(cell);
480 else return null;
481 }
482
483 public boolean equals(Object o){
484 CellSet otherSet = (CellSet) o;
485 return internalSet.equals(otherSet.internalSet);
486 }
487
488
•489 public static ArrayList<CellSet> removeDuplicateSets(ArrayList<CellSet> list) {
490 ArrayList<CellSet> uniqueSets = new ArrayList<CellSet>();
491
•492 Iterator<CellSet> it = list.iterator();
493 while(it.hasNext()){
•494 CellSet set = it.next();
495 boolean isOriginal = true;
•496 Iterator<CellSet> uniquesIt = uniqueSets.iterator();
497 while(uniquesIt.hasNext()){
•498 CellSet uniqueSet = uniquesIt.next();
499 if(set.equals(uniqueSet)){
500 isOriginal = false;
501 }
502 }
503 if(isOriginal) uniqueSets.add(set);
504 }
505 return uniqueSets;
506 }
507
508
509 /**
510 * Takes into account character info from the grid
511 *
512 * @return ArrayList of distinct BoundarySetS
513 */
•514 public ArrayList<CellSet> breakIntoDistinctBoundaries(TextGrid grid){
515 ArrayList<CellSet> result;
516
517 AbstractionGrid temp = new AbstractionGrid(grid, this);
518 result = temp.getDistinctShapes();
519
520 return result;
521 }
522
523
524 /**
525 *
526 * @return ArrayList of distinct BoundarySetS
527 */
•528 public ArrayList<CellSet> breakIntoDistinctBoundaries(){
529 ArrayList<CellSet> result = new ArrayList<CellSet>();
530
531 //CellSet tempSet = copyCellSet(this);
532 //tempSet.translate( - this.getMinX() + 1, - this.getMinY() + 1);
533
534 // TextGrid boundaryGrid = new TextGrid(tempSet.getMaxX()+2, tempSet.getMaxY()+2);
535 // boundaryGrid.fillCellsWith(tempSet, '*');
536
537 TextGrid boundaryGrid = new TextGrid(getMaxX()+2, getMaxY()+2);
538 boundaryGrid.fillCellsWith(this, '*');
539
540
•541 Iterator<TextGrid.Cell> it = iterator();
542 while(it.hasNext()){
543 TextGrid.Cell cell = (TextGrid.Cell) it.next();
544 if(boundaryGrid.isBlank(cell.x, cell.y)) continue;
545 CellSet boundarySet = boundaryGrid.fillContinuousArea(cell.x, cell.y, ' ');
546 //boundarySet.translate( this.getMinX() - 1, this.getMinY() - 1);
547 result.add(boundarySet);
548 }
549 return result;
550 }
551
552
553 /**
554 *
555 * Breaks that:
556 * <pre>
557 * +-----+
558 * | |
559 * + ---+-------------------
560 * | |
561 * +-----+
562 * </pre>
563 *
564 * into the following 3:
565 *
566 * <pre>
567 * +-----+
568 * | |
569 * + +
570 * | |
571 * +-----+
572 *
573 * ---
574 * -------------------
575 * </pre>
576 *
577 * @param grid
578 * @return a list of boundaries that are either open or closed but not mixed
579 * and they are equivalent to the <code>this</code>
580 */
•581 public ArrayList<CellSet> breakTrulyMixedBoundaries(TextGrid grid){
582 ArrayList<CellSet> result = new ArrayList<CellSet>();
583 CellSet visitedEnds = new CellSet();
584
585 TextGrid workGrid = TextGrid.makeSameSizeAs(grid);
586 grid.copyCellsTo(this, workGrid);
587
588 if (DEBUG){
589 System.out.println("Breaking truly mixed boundaries below:");
590 workGrid.printDebug();
591 }
592
•593 Iterator<TextGrid.Cell> it = iterator();
594 while(it.hasNext()){
595 TextGrid.Cell start = (TextGrid.Cell) it.next();
596 if(workGrid.isLinesEnd(start) && !visitedEnds.contains(start)){
597
598 if (DEBUG)
599 System.out.println("Starting new subshape:");
600
601 CellSet set = new CellSet();
602 set.add(start);
603 if(DEBUG) System.out.println("Added boundary "+start);
604
605 TextGrid.Cell previous = start;
606 TextGrid.Cell cell = null;
607 CellSet nextCells = workGrid.followCell(previous);
608 if(nextCells.size() == 0)
609 throw new IllegalArgumentException("This shape is either open but multipart or has only one cell, and cannot be processed by this method");
610 cell = (TextGrid.Cell) nextCells.getFirst();
611 set.add(cell);
612 if(DEBUG) System.out.println("Added boundary "+cell);
613
614 boolean finished = false;
615 if(workGrid.isLinesEnd(cell)){
616 visitedEnds.add(cell);
617 finished = true;
618 }
619
620 while(!finished){
621 nextCells = workGrid.followCell(cell, previous);
622 if(nextCells.size() == 1) {
623 set.add(cell);
624 if(DEBUG) System.out.println("Added boundary " + cell);
625 previous = cell;
626 cell = (TextGrid.Cell) nextCells.getFirst();
627 //if(!cell.equals(start) && grid.isPointCell(cell))
628 // s.addToPoints(makePointForCell(cell, workGrid, cellWidth, cellHeight, allRound));
629 if(workGrid.isLinesEnd(cell)){
630 visitedEnds.add(cell);
631 finished = true;
632 }
633 } else if(nextCells.size() > 1) {
634 finished = true;
635 }
636 }
637 result.add(set);
638 }
639 }
640
641 //substract all boundary sets from this CellSet
642 CellSet whatsLeft = new CellSet(this);
•643 for(CellSet set : result) {
644 whatsLeft.subtractSet(set);
645 if(DEBUG) set.printAsGrid();
646 }
647 result.add(whatsLeft);
648 if(DEBUG) whatsLeft.printAsGrid();
649
650 return result;
651 }
652
653
654 public TextGrid makeIntoGrid(){
655 TextGrid grid = new TextGrid(getMaxX()+2, getMaxY()+2);
656 grid.fillCellsWith(this, '*');
657 return grid;
658 }
659
660 public CellSet makeScaledOneThirdEquivalent(){
661 TextGrid gridBig = this.makeIntoGrid();
662 gridBig.fillCellsWith(this, '*');
663 if (VERBOSE_DEBUG){
664 System.out.println("---> making ScaledOneThirdEquivalent of:");
665 gridBig.printDebug();
666 }
667
668
669 TextGrid gridSmall = new TextGrid((getMaxX() + 2) / 3, (getMaxY() + 2) / 3);
670
671
672 for(int y = 0; y < gridBig.getHeight(); y++){
673 for(int x = 0; x < gridBig.getWidth(); x++){
674 TextGrid.Cell cell = gridBig.new Cell(x, y);
675 if(!gridBig.isBlank(cell)) gridSmall.set(x/3, y/3, '*');
676 }
677 }
678
679 if (VERBOSE_DEBUG){
680 System.out.println("---> made into grid:");
681 gridSmall.printDebug();
682 }
683
684 return gridSmall.getAllNonBlank();
685 }
686
687 }