#!/usr/bin/env jython
# VisAD Tutorial
# Copyright (C) 2000 Ugo Taddei
#
# Jython translation
# Copyright (C) 2002 Frank Gibbons.

try:
    import java
    from javax.swing import *
    from visad import *
    from visad.python.JPythonMethods import *
    from visad.java2d import DisplayImplJ2D
except:
    pass

false = 0
true = not false

#  VisAD Tutorial example 2_06
#  The same old parabola, but with a different look.
#  We use a MathType ( time -> (height, speed) ),
#  draw time and height along the x- and y-axis, respectively, and
#  map the speed to the line colour. For that we use a ScalarMap with
#  speed as RealType and Display.RGB as DisplayRealType
#  Use the GraphicsModeControl to change line thickness of
#  display


class p2_06:
    def __init__(self):
        # Create the quantities
        # Use RealType(String name, Unit u,  Set set), set is null
        time = RealType("time")
        height = RealType("height")
        
        # Create speed, but without a unit
        speed = RealType("speed")
        
        # Pack height and speed in a Tuple
        h_s_tuple = RealTupleType(height, speed)
        
        # Create a FunctionType
        # Use FunctionType(MathType domain, MathType range)
        func_t_tuple = FunctionType(time, h_s_tuple)
        
        # Create the time_set, with 5 values, but this time using a
        # Linear1DSet(MathType type, double first, double last, int length)
        LENGTH = 32
        time_set = Linear1DSet(time, -3.0, 3.0, LENGTH)
        
        # ...then we use a method of Set to get the samples from time_set
        # this call will get the time values
        # "true" means we get a copy from the samples
        t_vals  = time_set.getSamples( true)
        
        # finally generate height and speed values
        # height is given by the parabola height = 45 - 5 * time^2
        # and speed by its first derivative speed = -10 * time
        h_s_vals = [[], []] # List of (two) lists
        h_s_vals[0] = [ 45.0 - 5.0 * (t_vals[0][i]*t_vals[0][i]) for i in range(LENGTH) ]
        h_s_vals[1] = [ - 10.0 * t_vals[0][i] for i in range(LENGTH) ]
        
        # Create a FlatField
        # Use FlatField(FunctionType type, Set domain_set)
        h_s_ff = FlatField( func_t_tuple, time_set)
        
        # and put the height values above in it
        h_s_ff.setSamples(h_s_vals )
        
        # Create a 2D display
        display = DisplayImplJ2D("display1")
        
        # Get display's graphics mode control draw scales
        dispGMC = display.getGraphicsModeControl()
        dispGMC.setScaleEnable(true)
        
        # Create the ScalarMaps: quantity time is to be displayed along XAxis
        # and height along YAxis; speed is mapped to RGB color
        # Use ScalarMap(ScalarType scalar, DisplayRealType display_scalar)
        timeMap = ScalarMap( time, Display.XAxis )
        heightYMap = ScalarMap( height, Display.YAxis )
        speedRGBMap = ScalarMap( speed, Display.RGB )
        
        # Add maps to display
        for m in (timeMap, heightYMap, speedRGBMap):
            display.addMap(m)
            
        # Scale yMap. This will scale the y-axis, because heightYMap
        # has DisplayRealType YAxis
        # we simply choose the range from 0.0 to 50.0
        heightYMap.setRange( 0.0, 50.0)
        
        # Create a data reference and set the FlatField as our data
        h_s_ref = DataReferenceImpl("h_s_ref")
        h_s_ref.setData( h_s_ff )
        
        # Add reference to display
        display.addReference( h_s_ref )
        
        # Create application window, put display into it
        jframe = JFrame("VisAD Tutorial example 2_06")
        jframe.getContentPane().add(display.getComponent())
        
        # Set window size and make it visible
        jframe.setSize(300, 300)
        jframe.setVisible(true)


p2_06()
