view gnv-artifacts/src/main/java/de/intevation/gnv/raster/IsoProducer.java @ 605:e8ebdbc7f1e3

First step of removing the cache blob. The static part of the describe document will be created by using the input data stored at each state. Some TODOs left (see ChangeLog). gnv-artifacts/trunk@671 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Tue, 09 Feb 2010 14:27:55 +0000
parents f7038820df2e
children 9a828e5a2390
line wrap: on
line source
package de.intevation.gnv.raster;

import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;

import gnu.trove.TIntObjectHashMap;
import gnu.trove.TIntHashSet;
import gnu.trove.TObjectProcedure;

import de.intevation.gnv.math.IJKey;

import de.intevation.gnv.raster.Vectorizer.Edge;

/**
 * @author Sascha L. Teichmann (sascha.teichmann@intevation.de)
 */
public class IsoProducer
extends      AbstractProducer
{
    public interface AttributeGenerator {

        Object generateAttribute(int neighbor1, int neighbor2);

    } // interface AttributeGenerator

    protected HashMap<Edge, Integer>            open;
    protected HashMap<IJKey, TIntObjectHashMap> commonOpen;
    protected HashMap<IJKey, ArrayList<Edge>>   complete;

    protected int    width;
    protected int    height;

    public IsoProducer(
        double minX, double minY,
        double maxX, double maxY
    ) {
        super(minX, minY, maxX, maxY);

        open       = new HashMap<Edge, Integer>();
        commonOpen = new HashMap<IJKey, TIntObjectHashMap>();
        complete   = new HashMap<IJKey, ArrayList<Edge>>();
    }

    public void handleRings(
        List<Edge> rings, 
        int        value, 
        int        width,
        int        height
    ) {
        if (value == -1) {
            return;
        }
        this.width  = width;
        this.height = height;

        Integer v = Integer.valueOf(value);

        for (Edge head: rings) {
            Edge current = head;
            do {
                Integer neighbor = open.remove(current);

                if (neighbor != null) {
                    IJKey pair = new IJKey(value, neighbor.intValue());
                    pair.sort();

                    TIntObjectHashMap co = commonOpen.get(pair);

                    if (co == null) {
                        commonOpen.put(pair, co = new TIntObjectHashMap());
                    }

                    Edge edge = new Edge(current);

                    Edge otherA = (Edge)co.remove(edge.a);
                    if (otherA != null) {
                        otherA.chain(edge, edge.a);
                    }

                    Edge otherB = (Edge)co.remove(edge.b);
                    if (otherB != null) {
                        otherB.chain(edge, edge.b);
                    }

                    if (edge.isComplete()) {
                        ArrayList list = complete.get(pair);
                        if (list == null) {
                            complete.put(pair, list = new ArrayList());
                        }
                        list.add(Vectorizer.simplify(edge, width));
                    }
                    else {
                        if (otherA == null) {
                            co.put(edge.a, edge);
                        }
                        if (otherB == null) {
                            co.put(edge.b, edge);
                        }
                    }
                }
                else {
                    Edge edge = new Edge(current.b, current.a);
                    open.put(edge, v);
                }

                current = current.next;
            }
            while (current != head);
        } // for all rings

    } // handleRings

    protected HashSet<IJKey> joinPairs() {
        HashSet<IJKey> pairs = new HashSet<IJKey>();
        pairs.addAll(complete  .keySet());
        pairs.addAll(commonOpen.keySet());
        return pairs;
    }

    protected static ArrayList<Edge> headList(TIntObjectHashMap map) {
        final ArrayList<Edge> headList = new ArrayList<Edge>();
        map.forEachValue(new TObjectProcedure() {
            TIntHashSet headSet = new TIntHashSet();
            public boolean execute(Object value) {
                Edge head = ((Edge)value).head();
                if (headSet.add(head.a)) {
                    headList.add(head);
                }
                return true;
            }
        });
        return headList;
    }

    public void clear() {
        open.clear();
        complete.clear();
        commonOpen.clear();
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org