    001/* ===========================================================
    002 * Orson Charts : a 3D chart library for the Java(tm) platform
    003 * ===========================================================
    004 *
    005 * (C)opyright 2013-2022, by David Gilbert.  All rights reserved.
    006 *
    007 * https://github.com/jfree/orson-charts
    008 *
    009 * This program is free software: you can redistribute it and/or modify
    010 * it under the terms of the GNU General Public License as published by
    011 * the Free Software Foundation, either version 3 of the License, or
    012 * (at your option) any later version.
    013 *
    014 * This program is distributed in the hope that it will be useful,
    015 * but WITHOUT ANY WARRANTY; without even the implied warranty of
    016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    017 * GNU General Public License for more details.
    018 *
    019 * You should have received a copy of the GNU General Public License
    020 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    021 *
    022 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates.
    023 * Other names may be trademarks of their respective owners.]
    024 *
    025 * If you do not wish to be bound by the terms of the GPL, an alternative
    026 * commercial license can be purchased.  For details, please see visit the
    027 * Orson Charts home page:
    028 *
    029 * http://www.object-refinery.com/orsoncharts/index.html
    030 *
    031 */
    032
    033package org.jfree.chart3d.renderer.xyz;
    034
    035import java.awt.Color;
    036import java.io.Serializable;
    037
    038import org.jfree.chart3d.axis.Axis3D;
    039import org.jfree.chart3d.data.xyz.XYZDataset;
    040import org.jfree.chart3d.graphics3d.Dimension3D;
    041import org.jfree.chart3d.graphics3d.Line3D;
    042import org.jfree.chart3d.graphics3d.LineObject3D;
    043import org.jfree.chart3d.graphics3d.World;
    044import org.jfree.chart3d.plot.XYZPlot;
    045
    046/**
    047 * A renderer that draws 3D lines on an {@link XYZPlot} using data from an
    048 * {@link XYZDataset}.  Here is a sample:
    049 * <div>
    050 * <img src="../../../../../../doc-files/XYZLineChart3DDemo1.svg"
    051 * alt="XYZLineChart3DDemo1.svg" width="500" height="359">
    052 * </div>
    053 * (refer to {@code XYZLineChart3DDemo1.java} for the code to generate
    054 * the above chart).
    055 * <br><br>
    056 * NOTE: This class is serializable, but the serialization format is subject
    057 * to change in future releases and should not be relied upon for persisting
    058 * instances of this class.
    059 *
    060 * @since 1.5
    061 */
    062@SuppressWarnings("serial")
    063public class LineXYZRenderer extends AbstractXYZRenderer implements XYZRenderer,
    064        Serializable {
    065
    066    /**
    067     * Creates a new default instance.
    068     */
    069    public LineXYZRenderer() {
    070    }
    071
    072    /**
    073     * Adds a single line representing one item from the dataset.
    074     *
    075     * @param dataset  the dataset.
    076     * @param series  the series index.
    077     * @param item  the item index.
    078     * @param world  the world used to model the 3D chart.
    079     * @param dimensions  the plot dimensions in 3D.
    080     * @param xOffset  the x-offset.
    081     * @param yOffset  the y-offset.
    082     * @param zOffset  the z-offset.
    083     */
    084    @Override
    085    public void composeItem(XYZDataset dataset, int series, int item,
    086            World world, Dimension3D dimensions, double xOffset, double yOffset,
    087            double zOffset) {
    088
    089        if (item == 0) { // we are connecting lines between points, so there
    090            return;      // is nothing to do for item 0
    091        }
    092        XYZPlot plot = getPlot();
    093        Axis3D xAxis = plot.getXAxis();
    094        Axis3D yAxis = plot.getYAxis();
    095        Axis3D zAxis = plot.getZAxis();
    096        double x1 = dataset.getX(series, item);
    097        double y1 = dataset.getY(series, item);
    098        double z1 = dataset.getZ(series, item);
    099
    100        double x0 = dataset.getX(series, item - 1);
    101        double y0 = dataset.getY(series, item - 1);
    102        double z0 = dataset.getZ(series, item - 1);
    103
    104        double wx0 = xAxis.translateToWorld(x0, dimensions.getWidth());
    105        double wx1 = xAxis.translateToWorld(x1, dimensions.getWidth());
    106        double wy0 = yAxis.translateToWorld(y0, dimensions.getHeight());
    107        double wy1 = yAxis.translateToWorld(y1, dimensions.getHeight());
    108        double wz0 = zAxis.translateToWorld(z0, dimensions.getDepth());
    109        double wz1 = zAxis.translateToWorld(z1, dimensions.getDepth());
    110        Line3D line = new Line3D(wx0, wy0, wz0, wx1, wy1, wz1);
    111        line = Line3D.cropLineToAxisAlignedBoundingBox(line, 0,
    112                dimensions.getWidth(), 0, dimensions.getHeight(), 0,
    113                dimensions.getDepth());
    114        if (line != null) {
    115            Color color = getColorSource().getColor(series, item);
    116            LineObject3D line3D = new LineObject3D(
    117                    (float) (line.getStart().getX() + xOffset),
    118                    (float) (line.getStart().getY() + yOffset),
    119                    (float) (line.getStart().getZ() + zOffset),
    120                    (float) (line.getEnd().getX() + xOffset),
    121                    (float) (line.getEnd().getY() + yOffset),
    122                    (float) (line.getEnd().getZ() + zOffset), color);
    123            world.add(line3D);
    124        }
    125    }
    126
    127    /**
    128     * Tests this renderer for equality with an arbitrary object.
    129     *
    130     * @param obj  the object ({@code null} permitted).
    131     *
    132     * @return A boolean.
    133     */
    134    @Override
    135    public boolean equals(Object obj) {
    136        if (obj == this) {
    137            return true;
    138        }
    139        if (!(obj instanceof LineXYZRenderer)) {
    140            return false;
    141        }
    142        LineXYZRenderer that = (LineXYZRenderer) obj;
    143        return super.equals(obj);
    144    }
    145
    146}
    147