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