Debian Codegraph @

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