view gnv-artifacts/src/main/java/de/intevation/gnv/state/profile/verticalcrosssection/OutputHelper.java @ 1115:f953c9a559d8

Added license file and license headers. gnv-artifacts/trunk@1260 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Tue, 02 Nov 2010 17:46:55 +0000
parents 9058c08eac3a
children
line wrap: on
line source
/*
 * Copyright (c) 2010 by Intevation GmbH
 *
 * This program is free software under the LGPL (>=v2.1)
 * Read the file LGPL.txt coming with the software for details
 * or visit http://www.gnu.org/licenses/ if it does not exist.
 */

package de.intevation.gnv.state.profile.verticalcrosssection;

import de.intevation.gnv.jfreechart.CompactXYItems;
import de.intevation.gnv.jfreechart.PolygonSeries;

import de.intevation.gnv.math.Interpolation3D;

import gnu.trove.TDoubleArrayList;

import org.apache.log4j.Logger;

/**
 * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a>
 */
public class OutputHelper
{
    private static Logger log = Logger.getLogger(OutputHelper.class);

    public static final double EPS = 1e-5d;

    private OutputHelper() {
    }


    /**
     * Creates a PolygonSeries representing the seabed used to be displayed in a
     * 2D-chart.
     *
     * @param interpolation The interpolation which supports information about
     * the max depth, the cell width and height.
     * @param fill The fill color as integer.
     * @return a PolygonSeries representing the seabed.
     */
    public static PolygonSeries createSeabedPolygon(
        Interpolation3D interpolation,
        Integer         fill
    ) {
        double maxDepth   = interpolation.getMaxDepth();
        double cellWidth  = interpolation.getCellWidth();
        double cellHeight = interpolation.getCellHeight();

        double [] depths = interpolation.getDepths();

        double x = 0d;

        TDoubleArrayList vertices = new TDoubleArrayList();

        PolygonSeries ps = new PolygonSeries();

        for (int i = 0; i < depths.length; ++i, x += cellWidth) {
            double depth = depths[i];

            if (vertices.isEmpty()) {
                if (Double.isNaN(depth) || depth == maxDepth) {
                    continue;
                }
                if (depth > 0d) depth = 0d;
                vertices.add(x); vertices.add(maxDepth);
                vertices.add(x); vertices.add(depth);
                vertices.add(x+cellWidth); vertices.add(depth);
            }
            else { // in polygon
                if (Double.isNaN(depth) || depth == maxDepth) {
                    vertices.add(x); vertices.add(maxDepth);
                    ps.addRing(new CompactXYItems(vertices.toNativeArray()));
                    vertices.reset();
                }
                else {
                    if (depth > 0d) depth = 0d;
                    int N = vertices.size();
                    if (N > 2 && Math.abs(depth - vertices.get(N-1)) < EPS) {
                        vertices.set(N-2, x+cellWidth);
                    }
                    else {
                        vertices.add(vertices.get(N-2)); vertices.add(depth);
                        vertices.add(x+cellWidth); vertices.add(depth);
                    }
                }
            }
        } // for all depths

        if (!vertices.isEmpty()) {
            vertices.add(vertices.get(vertices.size()-2));
            vertices.add(maxDepth);
            ps.addRing(new CompactXYItems(vertices.toNativeArray()));
        }

        if (ps.getItemCount() == 0) {
            return null;
        }

        ps.setAttribute("fill", fill);

        return ps;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org