sascha@465: package de.intevation.gnv.raster; sascha@465: sascha@465: import java.util.List; sascha@465: import java.util.ArrayList; sascha@465: import java.util.HashMap; sascha@465: import java.util.HashSet; sascha@465: sascha@465: import gnu.trove.TIntObjectHashMap; sascha@465: import gnu.trove.TIntHashSet; sascha@465: import gnu.trove.TObjectProcedure; sascha@465: sascha@465: import de.intevation.gnv.math.IJKey; sascha@465: sascha@465: import de.intevation.gnv.raster.Vectorizer.Edge; sascha@465: sascha@465: /** sascha@465: * @author Sascha L. Teichmann (sascha.teichmann@intevation.de) sascha@465: */ sascha@465: public class IsoProducer sascha@465: extends AbstractProducer sascha@465: { sascha@465: public interface AttributeGenerator { sascha@465: sascha@465: Object generateAttribute(int neighbor1, int neighbor2); sascha@465: sascha@465: } // interface AttributeGenerator sascha@465: sascha@465: protected HashMap open; sascha@465: protected HashMap commonOpen; sascha@465: protected HashMap> complete; sascha@465: sascha@465: protected int width; sascha@465: protected int height; sascha@465: sascha@465: public IsoProducer( sascha@465: double minX, double minY, sascha@465: double maxX, double maxY sascha@465: ) { sascha@465: super(minX, minY, maxX, maxY); sascha@465: sascha@465: open = new HashMap(); sascha@465: commonOpen = new HashMap(); sascha@465: complete = new HashMap>(); sascha@465: } sascha@465: sascha@465: public void handleRings( sascha@465: List rings, sascha@465: int value, sascha@465: int width, sascha@465: int height sascha@465: ) { sascha@465: if (value == -1) { sascha@465: return; sascha@465: } sascha@465: this.width = width; sascha@465: this.height = height; sascha@465: sascha@465: Integer v = Integer.valueOf(value); sascha@465: sascha@465: for (Edge head: rings) { sascha@465: Edge current = head; sascha@465: do { sascha@465: Integer neighbor = open.remove(current); sascha@465: sascha@465: if (neighbor != null) { sascha@465: IJKey pair = new IJKey(value, neighbor.intValue()); sascha@465: pair.sort(); sascha@465: sascha@465: TIntObjectHashMap co = commonOpen.get(pair); sascha@465: sascha@465: if (co == null) { sascha@465: commonOpen.put(pair, co = new TIntObjectHashMap()); sascha@465: } sascha@465: sascha@465: Edge edge = new Edge(current); sascha@465: sascha@465: Edge otherA = (Edge)co.remove(edge.a); sascha@465: if (otherA != null) { sascha@465: otherA.chain(edge, edge.a); sascha@465: } sascha@465: sascha@465: Edge otherB = (Edge)co.remove(edge.b); sascha@465: if (otherB != null) { sascha@465: otherB.chain(edge, edge.b); sascha@465: } sascha@465: sascha@465: if (edge.isComplete()) { sascha@465: ArrayList list = complete.get(pair); sascha@465: if (list == null) { sascha@465: complete.put(pair, list = new ArrayList()); sascha@465: } sascha@465: list.add(Vectorizer.simplify(edge, width)); sascha@465: } sascha@465: else { sascha@465: if (otherA == null) { sascha@465: co.put(edge.a, edge); sascha@465: } sascha@465: if (otherB == null) { sascha@465: co.put(edge.b, edge); sascha@465: } sascha@465: } sascha@465: } sascha@465: else { sascha@465: Edge edge = new Edge(current.b, current.a); sascha@465: open.put(edge, v); sascha@465: } sascha@465: sascha@465: current = current.next; sascha@465: } sascha@465: while (current != head); sascha@465: } // for all rings sascha@465: sascha@465: } // handleRings sascha@465: sascha@465: protected HashSet joinPairs() { sascha@465: HashSet pairs = new HashSet(); sascha@465: pairs.addAll(complete .keySet()); sascha@465: pairs.addAll(commonOpen.keySet()); sascha@465: return pairs; sascha@465: } sascha@465: sascha@465: protected static ArrayList headList(TIntObjectHashMap map) { sascha@465: final ArrayList headList = new ArrayList(); sascha@465: map.forEachValue(new TObjectProcedure() { sascha@465: TIntHashSet headSet = new TIntHashSet(); sascha@465: public boolean execute(Object value) { sascha@465: Edge head = ((Edge)value).head(); sascha@465: if (headSet.add(head.a)) { sascha@465: headList.add(head); sascha@465: } sascha@465: return true; sascha@465: } sascha@465: }); sascha@465: return headList; sascha@465: } sascha@465: sascha@465: public void clear() { sascha@465: open.clear(); sascha@465: complete.clear(); sascha@465: commonOpen.clear(); sascha@465: } sascha@465: } sascha@465: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :