/*
 * @(#)AutomaticShape.java    1.0  24 August 2003
 *
 * Copyright 2004
 * College of Computer and Information Science
 * Northeastern University
 * Boston, MA  02115
 *
 * The Java Power Tools software may be used for educational
 * purposes as long as this copyright notice is retained intact
 * at the top of all source files.
 *
 * To discuss possible commercial use of this software, 
 * contact Richard Rasala at Northeastern University, 
 * College of Computer and Information Science,
 * 617-373-2462 or rasala@ccs.neu.edu.
 *
 * The Java Power Tools software has been designed and built
 * in collaboration with Viera Proulx and Jeff Raab.
 *
 * Should this software be modified, the words "Modified from 
 * Original" must be included as a comment below this notice.
 *
 * All publication rights are retained.  This software or its 
 * documentation may not be published in any media either
 * in whole or in part without explicit permission.
 *
 * This software was created with support from Northeastern 
 * University and from NSF grant DUE-9950829.
 */

package edu.neu.ccs.gui;

import edu.neu.ccs.util.*;

import java.awt.*;
import java.awt.geom.*;
import java.beans.*;

/**
 * <P>Class <CODE>AutomaticShape</CODE> implements a <CODE>BaseShape</CODE>
 * in which the tangent array is always computed from the vertex array
 * automatically using a <CODE>Tangent.Strategy</CODE> object.</P>
 *
 * <P>The default tangent strategy produces Bezier curves in which the
 * first and second derivatives of the curve are continuous at each
 * vertex.  This strategy is described in the article:</P>
 *
 * <P>Richard Rasala, Explicit Cubic Spline Interpolation Formulas, in
 * Andrew S. Glassner, Graphics Gems, Academic Press, 1990, 579-584.</P>
 * 
 * <P>To define a shape in which the tangent array varies independently
 * of the vertex array, use the class <CODE>TweakableShape</CODE>.</P>
 *
 * @see Path
 * @see Tangent
 * @see TweakableShape
 * @author  Richard Rasala
 * @version 2.3
 * @since   2.3
 */
public class AutomaticShape extends BaseShape {
    
    /** Bound property name to set the vertex data. */
    public static final String SET_VERTEX_DATA      = "set.vertex.data";
    
    /** Bound property name to set one vertex. */
    public static final String SET_VERTEX           = "set.vertex";
    
    /** Bound property name to add one vertex. */
    public static final String ADD_VERTEX           = "add.vertex";
    
    /** Bound property name to remove one vertex. */
    public static final String REMOVE_VERTEX        = "remove.vertex";
    
    /** Bound property name to set the tangent strategy. */
    public static final String SET_TANGENT_STRATEGY = "set.tangent.strategy";
    
    /** Bound property name to set both end tangents. */
    public static final String SET_END_TANGENT_DATA = "set.end.tangent.data";
    
    /** Bound property name to set one end tangent. */
    public static final String SET_END_TANGENT      = "set.end.tangent";
    
    
    /** The tangent strategy. */
    private Tangent.Strategy tangentstrategy = Tangent.bezierStrategy();
    
    /** The end tangent data. */
    private float[][] endTangent = null;
    
    
    /**
     * <P>The default constructor with an empty shape.</P>
     *
     * <P>The default settings are as follows:</P>
     *
     * <UL>
     *   <LI>Vertex data:      <CODE>new float[0][2]</CODE></LI>
     *   <LI>Tangent data:     <CODE>new float[0][2]</CODE></LI>
     *   <LI>End tangent data: <CODE>null</CODE></LI>
     *   <LI>Tangent strategy: <CODE>Tangent.bezierStrategy()</CODE></LI>
     *   <LI>Path strategy:    <CODE>Path.POLYGON</CODE></LI>
     *   <LI>Closure mode:     <CODE>ClosureMode.CLOSED</CODE></LI>
     *   <LI>Winding rule:     <CODE>WindingRule.WIND_NON_ZERO</CODE>.</LI>
     * </UL>
     *
     * @see #AutomaticShape(float[][])
     * @see #AutomaticShape(float[][], float[][])
     * @see #AutomaticShape(float[][], float[][], Tangent.Strategy)
     * @see #AutomaticShape(float[][], float[][], Tangent.Strategy, Path.Strategy)
     * @see #AutomaticShape(float[][], float[][], Tangent.Strategy, Path.Strategy, ClosureMode)
     * @see #AutomaticShape(float[][], float[][], Tangent.Strategy, Path.Strategy, ClosureMode, WindingRule)
     * @see #setVertexData(float[][])
     * @see #setEndTangentData(float[][])
     */
    public AutomaticShape() {
        this(null, null, null, null, null, null);
    }
    
    
    /**
     * <P>The constructor with the given vertex data.</P>
     *
     * <P>Precondition:</P>
     * <UL>
     *   <LI>For some integer N, the given vertex array is
     *          float[N][2] with non-<CODE>null</CODE> entries.</LI>
     * </UL>
     * 
     * @param vertex the vertex data
     * @see #AutomaticShape()
     * @see #AutomaticShape(float[][], float[][])
     * @see #AutomaticShape(float[][], float[][], Tangent.Strategy)
     * @see #AutomaticShape(float[][], float[][], Tangent.Strategy, Path.Strategy)
     * @see #AutomaticShape(float[][], float[][], Tangent.Strategy, Path.Strategy, ClosureMode)
     * @see #AutomaticShape(float[][], float[][], Tangent.Strategy, Path.Strategy, ClosureMode, WindingRule)
     * @see #setVertexData(float[][])
     * @see #setEndTangentData(float[][])
     */
    public AutomaticShape(float[][] vertex) {
        this(vertex, null, null, null, null, null);
    }
    
    
    /**
     * <P>The constructor with the given vertex data
     * and the given end tangent data.</P>
     *
     * <P>Precondition 1:</P>
     * <UL>
     *   <LI>For some integer N, the given vertex array is
     *          float[N][2] with non-<CODE>null</CODE> entries.</LI>
     * </UL>
     * 
     * <P>Precondition 2:</P>
     * <UL>
     *   <LI>The given endTangent array is
     *          either <CODE>null</CODE> or
     *          float[2][2] with non-<CODE>null</CODE> entries.</LI>
     * </UL>
     *
     * @param vertex     the vertex data
     * @param endTangent the end tangent data
     * @see #AutomaticShape()
     * @see #AutomaticShape(float[][])
     * @see #AutomaticShape(float[][], float[][], Tangent.Strategy)
     * @see #AutomaticShape(float[][], float[][], Tangent.Strategy, Path.Strategy)
     * @see #AutomaticShape(float[][], float[][], Tangent.Strategy, Path.Strategy, ClosureMode)
     * @see #AutomaticShape(float[][], float[][], Tangent.Strategy, Path.Strategy, ClosureMode, WindingRule)
     * @see #setVertexData(float[][])
     * @see #setEndTangentData(float[][])
     */
    public AutomaticShape(float[][] vertex, float[][] endTangent) {
        this(vertex, endTangent, null, null, null, null);
    }
    
    
    /**
     * <P>The constructor with the given vertex data,
     * the given end tangent data,
     * and the given tangent strategy.</P>
     *
     * <P>Precondition 1:</P>
     * <UL>
     *   <LI>For some integer N, the given vertex array is
     *          float[N][2] with non-<CODE>null</CODE> entries.</LI>
     * </UL>
     * 
     * <P>Precondition 2:</P>
     * <UL>
     *   <LI>The given endTangent array is
     *          either <CODE>null</CODE> or
     *          float[2][2] with non-<CODE>null</CODE> entries.</LI>
     * </UL>
     *
     * @param vertex          the vertex data
     * @param endTangent      the end tangent data
     * @param tangentstrategy the tangent strategy
     * @see #AutomaticShape()
     * @see #AutomaticShape(float[][])
     * @see #AutomaticShape(float[][], float[][])
     * @see #AutomaticShape(float[][], float[][], Tangent.Strategy, Path.Strategy)
     * @see #AutomaticShape(float[][], float[][], Tangent.Strategy, Path.Strategy, ClosureMode)
     * @see #AutomaticShape(float[][], float[][], Tangent.Strategy, Path.Strategy, ClosureMode, WindingRule)
     * @see #setVertexData(float[][])
     * @see #setEndTangentData(float[][])
     */
    public AutomaticShape
        (float[][]        vertex,
         float[][]        endTangent,
         Tangent.Strategy tangentstrategy)
    {
        this(vertex, endTangent, tangentstrategy, null, null, null);
    }
    
    
    /**
     * <P>The constructor with the given vertex data,
     * the given end tangent data,
     * the given tangent strategy,
     * and the given path strategy.</P>
     *
     * <P>Precondition 1:</P>
     * <UL>
     *   <LI>For some integer N, the given vertex array is
     *          float[N][2] with non-<CODE>null</CODE> entries.</LI>
     * </UL>
     * 
     * <P>Precondition 2:</P>
     * <UL>
     *   <LI>The given endTangent array is
     *          either <CODE>null</CODE> or
     *          float[2][2] with non-<CODE>null</CODE> entries.</LI>
     * </UL>
     *
     * @param vertex          the vertex data
     * @param endTangent      the end tangent data
     * @param tangentstrategy the tangent strategy
     * @param pathstrategy    the path strategy
     * @see #AutomaticShape()
     * @see #AutomaticShape(float[][])
     * @see #AutomaticShape(float[][], float[][])
     * @see #AutomaticShape(float[][], float[][], Tangent.Strategy)
     * @see #AutomaticShape(float[][], float[][], Tangent.Strategy, Path.Strategy, ClosureMode)
     * @see #AutomaticShape(float[][], float[][], Tangent.Strategy, Path.Strategy, ClosureMode, WindingRule)
     * @see #setVertexData(float[][])
     * @see #setEndTangentData(float[][])
     */
    public AutomaticShape
        (float[][]        vertex,
         float[][]        endTangent,
         Tangent.Strategy tangentstrategy,
         Path.Strategy    pathstrategy)
    {
        this(vertex, endTangent, tangentstrategy, pathstrategy, null, null);
    }
    
    
    /**
     * <P>The constructor with the given vertex data,
     * the given end tangent data,
     * the given tangent strategy,
     * the given path strategy,
     * and the given closure mode.</P>
     *
     * <P>Precondition 1:</P>
     * <UL>
     *   <LI>For some integer N, the given vertex array is
     *          float[N][2] with non-<CODE>null</CODE> entries.</LI>
     * </UL>
     * 
     * <P>Precondition 2:</P>
     * <UL>
     *   <LI>The given endTangent array is
     *          either <CODE>null</CODE> or
     *          float[2][2] with non-<CODE>null</CODE> entries.</LI>
     * </UL>
     *
     * @param vertex          the vertex data
     * @param endTangent      the end tangent data
     * @param tangentstrategy the tangent strategy
     * @param pathstrategy    the path strategy
     * @param closuremode     the closure mode
     * @see #AutomaticShape()
     * @see #AutomaticShape()
     * @see #AutomaticShape(float[][])
     * @see #AutomaticShape(float[][], float[][])
     * @see #AutomaticShape(float[][], float[][], Tangent.Strategy)
     * @see #AutomaticShape(float[][], float[][], Tangent.Strategy, Path.Strategy)
     * @see #AutomaticShape(float[][], float[][], Tangent.Strategy, Path.Strategy, ClosureMode, WindingRule)
     * @see #setVertexData(float[][])
     * @see #setEndTangentData(float[][])
     */
    public AutomaticShape
        (float[][]        vertex,
         float[][]        endTangent,
         Tangent.Strategy tangentstrategy,
         Path.Strategy    pathstrategy,
         ClosureMode      closuremode)
    {
        this(vertex, endTangent, tangentstrategy, pathstrategy, closuremode, null);
    }
    
    
    /**
     * <P>The constructor with the given vertex data,
     * the given end tangent data,
     * the given tangent strategy,
     * the given path strategy,
     * the given closure mode,
     * and the given winding rule.</P>
     *
     * <P>Precondition 1:</P>
     * <UL>
     *   <LI>For some integer N, the given vertex array is
     *          float[N][2] with non-<CODE>null</CODE> entries.</LI>
     * </UL>
     * 
     * <P>Precondition 2:</P>
     * <UL>
     *   <LI>The given endTangent array is
     *          either <CODE>null</CODE> or
     *          float[2][2] with non-<CODE>null</CODE> entries.</LI>
     * </UL>
     *
     * @param vertex          the vertex data
     * @param endTangent      the end tangent data
     * @param tangentstrategy the tangent strategy
     * @param pathstrategy    the path strategy
     * @param closuremode     the closure mode
     * @param windingrule     the winding rule
     * @see #AutomaticShape()
     * @see #AutomaticShape(float[][])
     * @see #AutomaticShape(float[][], float[][])
     * @see #AutomaticShape(float[][], float[][], Tangent.Strategy)
     * @see #AutomaticShape(float[][], float[][], Tangent.Strategy, Path.Strategy)
     * @see #AutomaticShape(float[][], float[][], Tangent.Strategy, Path.Strategy, ClosureMode)
     * @see #setVertexData(float[][])
     * @see #setEndTangentData(float[][])
     */
    public AutomaticShape
        (float[][]        vertex,
         float[][]        endTangent,
         Tangent.Strategy tangentstrategy,
         Path.Strategy    pathstrategy,
         ClosureMode      closuremode,
         WindingRule      windingrule)
    {
        setTangentStrategy(tangentstrategy);
        setPathStrategy(pathstrategy);
        setClosureMode(closuremode);
        setWindingRule(windingrule);
        setEndTangentData(endTangent);
        setVertexData(vertex);
    }
    
    
    /**
     * <P>Sets the vertex data of the shape using a 2-dimensional array
     * of single precision numbers and makes a new path.</P>
     *
     * <P>Sets the tangents to be used when the shape is displayed as
     * a curve.</P>
     *
     * <P>Precondition:</P>
     * <UL>
     *   <LI>For some integer N, the given vertex array is
     *          float[N][2] with non-<CODE>null</CODE> entries.</LI>
     * </UL>
     * 
     * <P>Does nothing if the precondition fails.</P>
     *
     * <P>Fires property change: SET_VERTEX_DATA.</P>
     * 
     * @param vertex the vertex data to set
     * @see #setVertex(int, float, float)
     * @see #setVertex(int, float[])
     */
    public final void setVertexData(float[][] vertex) {
        if (!FloatArray.checkArray(vertex, 2))
            return;
        
        if (FloatArray.equals(this.vertex, vertex))
            return;
        
        this.vertex = FloatArray.deepclone(vertex);
        
        makeAutomaticPath();
        
        firePropertyChange(SET_VERTEX_DATA, null, null);
    }
    
    
    /**
     * <P>Sets the vertex at the given index to the given point coordinates
     * and makes a new path.</P>
     *
     * <P>Does nothing if the index is not in [0, length()).</P>
     *
     * <P>Sets the tangents to be used when the shape is displayed as
     * a curve.</P>
     *
     * <P>Fires property change: SET_VERTEX.
     *
     * @param index the vertex index
     * @param x the new x-coordinate for the vertex
     * @param y the new y-coordinate for the vertex
     * @see #setVertexData(float[][])
     * @see #setVertex(int, float[])
     */
    public final void setVertex(int index, float x, float y) {
        if ((index < 0) || (index >= vertex.length))
            return;
        
        if ((vertex[index][0] == x) && (vertex[index][1] == y))
            return;
        
        vertex[index][0] = x;
        vertex[index][1] = y;
        
        makeAutomaticPath();
        
        firePropertyChange(SET_VERTEX, null, null);
    }
    
    
    /**
     * <P>Sets the vertex at the given index to the given point
     * and makes a new path.</P>
     *
     * <P>Does nothing if the index is not in [0, length()).</P>
     *
     * <P>Does nothing if point is not float[2].</P>
     *
     * <P>Sets the tangents to be used when the shape is displayed as
     * a curve.</P>
     *
     * <P>Fires property change: SET_VERTEX.
     *
     * @param index the vertex index
     * @param point the new point for the vertex at the given index
     * @see #setVertexData(float[][])
     * @see #setVertex(int, float, float)
     */
    public final void setVertex(int index, float[] point) {
        if ((point == null) || (point.length != 2))
            return;
        
        setVertex(index, point[0], point[1]);
    }
    
    
    /**
     * <P>Add a new vertex at the given index with the given point
     * coordinates.</P>
     *
     * <P>Does nothing if the index is not in [0, length()].</P>
     *
     * <P>If index is 0, the insertion is at the front.</P>
     *
     * <P>If index is length(), the insertion is at the end.</P>
     *
     * <P>Sets the tangents to be used when the shape is displayed as
     * a curve.</P>
     *
     * <P>Fires property change: ADD_VERTEX.</P>
     *
     * @param index the vertex index
     * @param x the x-coordinate for the new vertex to add
     * @param y the y-coordinate for the new vertex to add
     * @see #addVertex(int, float[])
     * @see #removeVertex(int)
     */
    public final void addVertex(int index, float x, float y) {
        int N = vertex.length;
        int M = N + 1;
        
        if ((index < 0) || (index > N))
            return;
        
        float[][] oldvertex = vertex;
        
        vertex = new float[M][2];
        
        vertex[index][0] = x;
        vertex[index][1] = y;
        
        for (int i = 0; i < M; i++) {
            if (i < index) {
                vertex[i][0] = oldvertex[i][0];
                vertex[i][1] = oldvertex[i][1];
            }
            else if (i > index) {
                int j = i - 1;
                vertex[i][0] = oldvertex[j][0];
                vertex[i][1] = oldvertex[j][1];
            }
            
        }
        
        makeAutomaticPath();
        
        firePropertyChange(ADD_VERTEX, null, null);
    }
    
    
    /**
     * <P>Add a new vertex at the given index with the given point.</P>
     *
     * <P>Does nothing if the index is not in [0, length()] or point is
     * not float[2].</P>
     *
     * <P>If index is 0, the insertion is at the front.</P>
     *
     * <P>If index is length(), the insertion is at the end.</P>
     *
     * <P>Sets the tangents to be used when the shape is displayed as
     * a curve.</P>
     *
     * <P>Fires property change: ADD_VERTEX.</P>
     *
     * @param index the vertex index
     * @param point the new vertex to add
     * @see #addVertex(int, float, float)
     * @see #removeVertex(int)
     */
    public final void addVertex(int index, float[] point) {
        if ((point == null) || (point.length != 2))
            return;
        
        addVertex(index, point[0], point[1]);
    }
    
    
    /**
     * <P>Remove the vertex at the given index.</P>
     *
     * <P>Does nothing if the index is not in [0, length()).</P>
     *
     * <P>Sets the tangents to be used when the shape is displayed as
     * a curve.</P>
     *
     * <P>Fires property change: REMOVE_VERTEX.</P>
     *
     * @param index the vertex index
     * @see #addVertex(int, float[])
     * @see #addVertex(int, float, float)
     */
    public final void removeVertex(int index) {
        int N = vertex.length;
        int M = N - 1;
        
        if ((index < 0) || (index >= N))
                return;
        
        float[][] oldvertex = vertex;
        
        vertex = new float[M][2];
        
        for (int i = 0; i < N; i++) {
            if (i < index) {
                vertex[i][0] = oldvertex[i][0];
                vertex[i][1] = oldvertex[i][1];
            }
            else if (i > index) {
                int j = i - 1;
                vertex[j][0] = oldvertex[i][0];
                vertex[j][1] = oldvertex[i][1];
            }
            
        }
        
        makeAutomaticPath();
        
        firePropertyChange(REMOVE_VERTEX, null, null);
    }
    
    
    /**
     * <P>Sets the end tangent data of the shape using a 2-by-2 array
     * of single precision numbers and makes a new path.</P>
     *
     * <P>Sets the tangents to be used when the shape is displayed as
     * a curve.</P>
     *
     * <P>Precondition:</P>
     * <UL>
     *   <LI>The given endTangent array is
     *          either <CODE>null</CODE> or
     *          float[2][2] with non-<CODE>null</CODE> entries.</LI>
     * </UL>
     *
     * <P>Does nothing if the precondition fails.</P>
     *
     * <P>Fires property change: SET_END_TANGENT_DATA.</P>
     * 
     * @param endTangent the endTangent data to set
     * @see #getEndTangentData()
     * @see #setEndTangent(int, float, float)
     * @see #setEndTangent(int, float[])
     */
    public final void setEndTangentData(float[][] endTangent) {
        if (endTangent == null) {
            if (this.endTangent == null)
                return;
            
            this.endTangent = null;
        }
        else {
            if (! (FloatArray.checkArray(endTangent, 2) && endTangent.length == 2))
                return;
            
            if (FloatArray.equals(this.endTangent, endTangent))
                return;
            
            this.endTangent = FloatArray.deepclone(endTangent);
        }
        
        makeAutomaticPath();
        
        firePropertyChange(SET_END_TANGENT_DATA, null, null);
    }
    
    
    /**
     * <P>Returns a copy of the endTangent data.</P>
     *
     * <P>The returned value will either be <CODE>null</CODE>
     * or have the form float[2][2].</P>
     *
     * @return a copy of the endTangent data
     * @see #setEndTangentData(float[][])
     * @see #setEndTangent(int, float, float)
     * @see #setEndTangent(int, float[])
     */
    public final float[][] getEndTangentData() {
        return FloatArray.deepclone(endTangent); 
    }
    
    
    /**
     * <P>Sets the end tangent at the given index (0, 1) to the given
     * delta coordinates dx, dy and makes a new path.</P>
     *
     * <P>Does nothing if the index is not in [0, 1].</P>
     *
     * <P>If the current end tangent array is <CODE>null</CODE>, then
     * this array will be set to new float[2][2] before setting the
     * delta value.
     *
     * <P>Sets the tangents to be used when the shape is displayed as
     * a curve.</P>
     *
     * <P>Fires property change: SET_END_TANGENT.
     *
     * @param index the end tangent index
     * @param dx the new x-coordinate for the end tangent
     * @param dy the new y-coordinate for the end tangent
     * @see #setEndTangentData(float[][])
     * @see #getEndTangentData()
     * @see #setEndTangent(int, float[])
     */
    public final void setEndTangent(int index, float dx, float dy) {
        if ((index < 0) || (index > 1))
            return;
        
        if (endTangent == null) {
            endTangent = new float[2][2];
        }
        else {
            if ((endTangent[index][0] == dx) && (endTangent[index][1] == dy))
                return;
        }
        
        endTangent[index][0] = dx;
        endTangent[index][1] = dy;
        
        makeAutomaticPath();
        
        firePropertyChange(SET_END_TANGENT, null, null);
    }
    
    
    /**
     * <P>Sets the end tangent at the given index (0, 1) to the given
     * delta and makes a new path.</P>
     *
     * <P>Does nothing if the index is not in [0, 1].</P>
     *
     * <P>Does nothing if delta is not float[2].</P>
     *
     * <P>If the current end tangent array is <CODE>null</CODE>, then
     * this array will be set to new float[2][2] before setting the
     * delta value.
     *
     * <P>Sets the tangents to be used when the shape is displayed as
     * a curve.</P>
     *
     * <P>Fires property change: SET_END_TANGENT.
     *
     * @param index the vertex index
     * @param delta the new delta for the vertex at the given index
     * @see #setEndTangentData(float[][])
     * @see #getEndTangentData()
     * @see #setEndTangent(int, float, float)
     */
    public final void setEndTangent(int index, float[] delta) {
        if ((delta == null) || (delta.length != 2))
            return;
        
        setEndTangent(index, delta[0], delta[1]);
    }
    
    
    /**
     * <P>Sets the tangent strategy of the shape and makes a new path.</P>
     *
     * <P>Does nothing if its parameter is <CODE>null</CODE>.</P>
     *
     * <P>Also sets the tangents to be used when the shape is displayed
     * as a curve.</P>
     *
     * <P>Fires property change: SET_TANGENT_STRATEGY.</P>
     *
     * @param strategy the tangent strategy
     * @see #getTangentStrategy()
     */
    public final void setTangentStrategy(Tangent.Strategy tangentstrategy) {
        if ((tangentstrategy == null) || (tangentstrategy == this.tangentstrategy))
            return;
        
        this.tangentstrategy = tangentstrategy;
        
        makeAutomaticPath();
        
        firePropertyChange(SET_TANGENT_STRATEGY, null, null);
    }
    
    
    /**
     * Returns the tangent strategy.
     *
     * @return the tangent strategy
     * @see #setTangentStrategy(Tangent.Strategy)
     */
    public final Tangent.Strategy getTangentStrategy() {
        return tangentstrategy;
    }
    
    
    /**
     * Make the path for this AutomaticShape taking into account
     * the vertex and end tangent arrays and the closure mode.
     */
    protected final void makeAutomaticPath() {
        tangent = (getClosureMode() == ClosureMode.CLOSED)
            ? tangentstrategy.makeTangents(vertex)
            : tangentstrategy.makeTangents(vertex, endTangent);
        
        makePath();
    }
}
