001/* An actor that converts Cartesian coordinates to a single complex token.
002
003 Copyright (c) 1998-2014 The Regents of the University of California.
004 All rights reserved.
005 Permission is hereby granted, without written agreement and without
006 license or royalty fees, to use, copy, modify, and distribute this
007 software and its documentation for any purpose, provided that the above
008 copyright notice and the following two paragraphs appear in all copies
009 of this software.
010
011 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
012 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
013 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
014 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
015 SUCH DAMAGE.
016
017 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
018 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
019 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
020 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
021 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
022 ENHANCEMENTS, OR MODIFICATIONS.
023
024 PT_COPYRIGHT_VERSION_2
025 COPYRIGHTENDKEY
026
027 */
028package ptolemy.actor.lib.conversions;
029
030import ptolemy.actor.TypedAtomicActor;
031import ptolemy.actor.TypedIOPort;
032import ptolemy.data.ComplexToken;
033import ptolemy.data.DoubleToken;
034import ptolemy.data.type.BaseType;
035import ptolemy.kernel.CompositeEntity;
036import ptolemy.kernel.util.IllegalActionException;
037import ptolemy.kernel.util.NameDuplicationException;
038import ptolemy.math.Complex;
039
040///////////////////////////////////////////////////////////////////
041/// CartesianToComplex
042
043/**
044 Convert a Cartesian pair (represented as two double tokens) to a single
045 complex token. At each firing of the actor, it will consume exactly one
046 token from each of the two input ports and produce a complex token on the
047 output port. The x input becomes the real output and the y input becomes the
048 imaginary output. If either input port is empty, nothing is produced.
049
050 @author Michael Leung, Jie Liu, Edward A. Lee, Paul Whitaker
051 @version $Id$
052 @since Ptolemy II 1.0
053 @Pt.ProposedRating Green (pwhitake)
054 @Pt.AcceptedRating Green (pwhitake)
055 */
056public class CartesianToComplex extends TypedAtomicActor {
057    /** Construct an actor with the given container and name.
058     *  @param container The container.
059     *  @param name The name of this actor.
060     *  @exception IllegalActionException If the actor cannot be contained
061     *   by the proposed container.
062     *  @exception NameDuplicationException If the container already has an
063     *   actor with this name.
064     */
065    public CartesianToComplex(CompositeEntity container, String name)
066            throws NameDuplicationException, IllegalActionException {
067        super(container, name);
068
069        x = new TypedIOPort(this, "x", true, false);
070        x.setTypeEquals(BaseType.DOUBLE);
071
072        y = new TypedIOPort(this, "y", true, false);
073        y.setTypeEquals(BaseType.DOUBLE);
074
075        output = new TypedIOPort(this, "output", false, true);
076        output.setTypeEquals(BaseType.COMPLEX);
077
078        _attachText("_iconDescription",
079                "<svg>\n" + "<polygon points=\"-15,-15 15,15 15,-15 -15,15\" "
080                        + "style=\"fill:white\"/>\n" + "</svg>\n");
081    }
082
083    ///////////////////////////////////////////////////////////////////
084    ////                         public variables                  ////
085
086    /** The input port for the x coordinate of the Cartesian pair, which
087     has type DoubleToken.
088     */
089    public TypedIOPort x;
090
091    /** The input port for the y coordinate of the Cartesian pair, which
092     has type DoubleToken.
093     */
094    public TypedIOPort y;
095
096    /** The port for the output, which has type ComplexToken.
097     */
098    public TypedIOPort output;
099
100    ///////////////////////////////////////////////////////////////////
101    ////                         public methods                    ////
102
103    /** Consume exactly one token from each input port and output the
104     *  converted complex token on the output port.
105     *  @exception IllegalActionException If there is no director.
106     */
107    @Override
108    public void fire() throws IllegalActionException {
109        super.fire();
110        double xValue = ((DoubleToken) x.get(0)).doubleValue();
111        double yValue = ((DoubleToken) y.get(0)).doubleValue();
112        ComplexToken token = new ComplexToken(new Complex(xValue, yValue));
113        output.send(0, token);
114    }
115
116    /** Return false if either of the input ports has no token, otherwise
117     *  return what the superclass returns (presumably true).
118     *  @exception IllegalActionException If there is no director.
119     */
120    @Override
121    public boolean prefire() throws IllegalActionException {
122        if (!x.hasToken(0) || !y.hasToken(0)) {
123            return false;
124        }
125
126        return super.prefire();
127    }
128}