Re: FlatField.evaluate() returning missing

Hi Bill,

Unfortunately the problem is not any of your sugestions.

I made a sample code to demonstrate the problem. But since it is a random 
problem, the test requires some interaction. 

You must click the button on the top of the window and then move the cusor 
over the collored field to check the "Value" field on the status bar. You may 
have to repeat this procedure some times (maby more then ten). If the problem 
"work" on your machine one time you will see a "Value: NaN" on the status 
bar.

I know its not easy, but it is the best I could think off. This is like 
convincing someone I have seen a UFO  :^)

I am sending the code both atached and pasted in the message becase I don't 
know if the lista accepts atachments.

By the way, I am using a Linux box with jdk1.5.0_04.

Thanks in advance,
Cicero

-------------------------------------------------------------

import java.awt.BorderLayout;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;
import javax.media.j3d.GraphicsConfigTemplate3D;
import javax.swing.*;
import visad.*;
import visad.java3d.DisplayImplJ3D;
import visad.java3d.TwoDDisplayRendererJ3D;
/*
 * TestEvaluate.java
 *
 * Created on 2 de Setembro de 2005, 15:53
 *
 */

/**
 *
 * @author cicero
 */
public class TestEvaluate implements DisplayListener, ActionListener {
    
    public static final int DIM_DATA = 512;
    
    private JButton changeFiled = new JButton("Generate new field and 
domain");
    
    private JLabel lbValue = new JLabel("Value");
    
    ScalarMap latMap, lonMap;
    DisplayImpl display;
    Gridded2DSet domain_set;
    FlatField vals_ff;
    RealTupleType domain_tuple;
    FunctionType func_dom_pix;
    DataReferenceImpl radar_ref;
    
    /** Creates a new instance of TestEvaluate */
    public TestEvaluate() throws RemoteException, VisADException {
        
        //Creates the GUI
        JFrame frame = new JFrame("Test Evaluate");
        frame.add(changeFiled, BorderLayout.NORTH);
        frame.add(lbValue, BorderLayout.SOUTH);
        changeFiled.addActionListener(this);
        frame.setSize(500, 500);
        
        
        GraphicsConfigTemplate3D gct = new GraphicsConfigTemplate3D();
        GraphicsConfiguration gc = gct.getBestConfiguration(
                
GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getConfigurations());
        display = new DisplayImplJ3D("display1", new TwoDDisplayRendererJ3D(), 
gc);
        frame.add(display.getComponent(), BorderLayout.CENTER);
        
        //Maps
        latMap = new ScalarMap(RealType.Latitude, Display.YAxis);
        display.addMap(latMap);
        lonMap = new ScalarMap(RealType.Longitude, Display.XAxis);
        display.addMap(lonMap);
        
        domain_tuple = new RealTupleType(RealType.Longitude, 
RealType.Latitude);
        RealType vradar = RealType.getRealType("VALOR");
        func_dom_pix = new FunctionType( domain_tuple, vradar);
        
        //mapas do radar
        ScalarMap radarMap = new ScalarMap( vradar,  Display.RGBA);
        display.addMap( radarMap );
        
        display.enableEvent(DisplayEvent.MOUSE_MOVED);
        
        display.addDisplayListener(this);
        
        
        
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        
        change();
    }
    
    private static float[][] createData() {
        float[][] data = new float[1][DIM_DATA*DIM_DATA];
        
        for(int c = 0; c < DIM_DATA; c++)
            for(int r = 0; r < DIM_DATA; r++)
                data[0][ c * DIM_DATA + r ] = (float) Math.sin(r);
        return data;
    }
    
    final static float RAIO_TERRA = (float) 6.374E3;
    final static float RAD = (float) Data.DEGREES_TO_RADIANS;
    final static float DEG = 1.0f/RAD;
    
    //This was a projection procedure, but I simplified
    //for clarity
    private float[][] createDomain(float range){
        //Matriz que terá a localizacao de cada pixel
        float [][] value = new float[2][DIM_DATA*DIM_DATA];
        
        final float DELTA = range*2/DIM_DATA;
        final float HALFDELTA = DELTA/2;
        final int HALFDIM = DIM_DATA/2;
        
        //distance to the center
        float dx, dy;
        
        for (int i=0; i<DIM_DATA; i++){
            for (int j=0; j<DIM_DATA; j++){
                if (i>HALFDIM) dx = ((i-(HALFDIM+1))*DELTA) + HALFDELTA;
                else dx = -1*(((HALFDIM-i-1)*DELTA) + HALFDELTA);
                if (j>HALFDIM) dy = ((j-(HALFDIM+1))*DELTA) + HALFDELTA;
                else dy = -1*(((HALFDIM-j-1)*DELTA) + HALFDELTA);
                
                value[1][j + i*DIM_DATA] = DEG*dy/RAIO_TERRA;
                
                value[0][j + i*DIM_DATA] = DEG*dx/RAIO_TERRA;
            }
        }
        
        return value;
    }
    
    private void localize(int x, int y) throws RemoteException, VisADException 
{
        
        VisADRay ray = 
display.getDisplayRenderer().getMouseBehavior().findRay(x, y);
        
        float[] lon = lonMap.inverseScaleValues(new float[] {(float) 
ray.position[0]});
        float[] lat = latMap.inverseScaleValues(new float[] {(float) 
ray.position[1]});
        
        RealTuple tuple = new RealTuple(new Real[] {
            new Real(RealType.Longitude, lon[0]),
                    new Real(RealType.Latitude, lat[0])
        });
        
        if (vals_ff!=null)
            //This is here it does not work (sometimes)
            lbValue.setText("X: " + x + " Y: " + y + " Lat: " + lat[0] +
                    " Lon: " + lon[0]+ " Value: " +
                    ((Real) vals_ff.evaluate(tuple)).getValue());
    }
    
    public void displayChanged(DisplayEvent displayEvent)
    throws VisADException, RemoteException {
        
        if (displayEvent.getId() == DisplayEvent.MOUSE_MOVED) {
            localize(displayEvent.getX(), displayEvent.getY());
        }
    }
    
    private void change() throws RemoteException, VisADException {
        
        float range = (float) (1000*Math.random());
        
        System.out.println("Range: " + range);
        
        domain_set = new Gridded2DSet(domain_tuple, createDomain(range), 
DIM_DATA, DIM_DATA);
        vals_ff = new FlatField( func_dom_pix, domain_set);
        
        if (radar_ref == null) {
            radar_ref = new DataReferenceImpl("radar_ref");
            display.addReference(radar_ref);
        }
        
        radar_ref.setData( vals_ff );
        
        vals_ff.setSamples(createData());
        
        //The funny thing is that it always works here
        RealTuple tuple = new RealTuple(new Real[] {
            new Real(RealType.Longitude, 0f),
                    new Real(RealType.Latitude, 0f)
        });
        System.out.println("Value: " + ((Real) 
vals_ff.evaluate(tuple)).getValue());
    }
    
    public void actionPerformed(java.awt.event.ActionEvent e)  {
        try {
            change();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        
    }
    
    public static void main(String args[]) {
        try {
            new TestEvaluate();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    
    
}