001/* An actor that converts a complex token to Cartesian coordinates.
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/// ComplexToCartesian
042
043/**
044 Read a complex token and output double tokens that represent the real and
045 imaginary parts to two different output ports.
046
047 @author Michael Leung, Edward A. Lee, Paul Whitaker
048 @version $Id$
049 @since Ptolemy II 1.0
050 @Pt.ProposedRating Green (pwhitake)
051 @Pt.AcceptedRating Green (pwhitake)
052 */
053public class ComplexToCartesian extends TypedAtomicActor {
054    /** Construct an actor with the given container and name.
055     *  @param container The container.
056     *  @param name The name of this actor.
057     *  @exception IllegalActionException If the actor cannot be contained
058     *   by the proposed container.
059     *  @exception NameDuplicationException If the container already has an
060     *   actor with this name.
061     */
062    public ComplexToCartesian(CompositeEntity container, String name)
063            throws NameDuplicationException, IllegalActionException {
064        super(container, name);
065
066        input = new TypedIOPort(this, "input", true, false);
067        input.setTypeEquals(BaseType.COMPLEX);
068
069        x = new TypedIOPort(this, "x", false, true);
070        x.setTypeEquals(BaseType.DOUBLE);
071
072        y = new TypedIOPort(this, "y", false, true);
073        y.setTypeEquals(BaseType.DOUBLE);
074
075        _attachText("_iconDescription",
076                "<svg>\n" + "<polygon points=\"-15,-15 15,15 15,-15 -15,15\" "
077                        + "style=\"fill:white\"/>\n" + "</svg>\n");
078    }
079
080    ///////////////////////////////////////////////////////////////////
081    ////                         public variables                  ////
082
083    /** The port for the input, which has type ComplexToken. */
084    public TypedIOPort input;
085
086    /** The output port for x coordinate, which has type DoubleToken. */
087    public TypedIOPort x;
088
089    /** The output port for the imaginary coordinate, which has type
090     DoubleToken. */
091    public TypedIOPort y;
092
093    ///////////////////////////////////////////////////////////////////
094    ////                         public methods                    ////
095
096    /** Consume a complex token from the input port and output a new double
097     *  token on each of the two output ports (the real and imaginary parts
098     *  of the input complex token).
099     *
100     *  @exception IllegalActionException If there is no director.
101     */
102    @Override
103    public void fire() throws IllegalActionException {
104        super.fire();
105        Complex complexNumber = ((ComplexToken) input.get(0)).complexValue();
106
107        x.send(0, new DoubleToken(complexNumber.real));
108        y.send(0, new DoubleToken(complexNumber.imag));
109    }
110
111    /** Return false if the input port has no token, otherwise return
112     *  what the superclass returns (presumably true).
113     *  @exception IllegalActionException If there is no director.
114     */
115    @Override
116    public boolean prefire() throws IllegalActionException {
117        if (!input.hasToken(0)) {
118            return false;
119        }
120
121        return super.prefire();
122    }
123}