//
// CursorUtil.java
//

/*
VisAD system for interactive analysis and visualization of numerical
data.  Copyright (C) 1996 - 2017 Bill Hibbard, Curtis Rueden, Tom
Rink, Dave Glowacki, Steve Emmerson, Tom Whittaker, Don Murray, and
Tommy Jasmin.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Library General Public License for more details.

You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA
*/

package visad.util;

import java.rmi.RemoteException;
import java.util.Vector;
import visad.*;

/**
 * Utility methods for cursor-related functions, including converting between
 * pixel, cursor and domain coordinates, and evaluating functions at a given
 * cursor location.
 */
public class CursorUtil {

  /** Converts the given cursor coordinates to domain coordinates. */
  public static double[] cursorToDomain(DisplayImpl d,
    RealType[] types, double[] cursor)
  {
    if (d == null) return null;
    double[][] scale = getScaleValues(d, types);
    double[] domain = new double[3];
    for (int i=0; i<3; i++) {
      domain[i] = scale[i] == null ? 0 :
        (cursor[i] - scale[i][1]) / scale[i][0];
    }
    return domain;
  }

  /** Converts the given domain coordinates to cursor coordinates. */
  public static double[] domainToCursor(DisplayImpl d,
    RealType[] types, double[] domain)
  {
    if (d == null) return null;
    double[][] scale = getScaleValues(d, types);
    double[] cursor = new double[3];
    for (int i=0; i<3; i++) {
      cursor[i] = scale[i] == null ? 0 :
        scale[i][0] * domain[i] + scale[i][1];
    }
    return cursor;
  }

  /** Converts the given cursor coordinates to domain coordinates. */
  public static double[] cursorToDomain(DisplayImpl d, double[] cursor) {
    return cursorToDomain(d, null, cursor);
  }

  /** Converts the given domain coordinates to cursor coordinates. */
  public static double[] domainToCursor(DisplayImpl d, double[] domain) {
    return domainToCursor(d, null, domain);
  }

  /** Converts the given pixel coordinates to cursor coordinates. */
  public static double[] pixelToCursor(DisplayImpl d, int x, int y) {
    if (d == null) return null;
    MouseBehavior mb = d.getDisplayRenderer().getMouseBehavior();
    VisADRay ray = mb.findRay(x, y);
    return ray.position;
  }

  /** Converts the given cursor coordinates to pixel coordinates. */
  public static int[] cursorToPixel(DisplayImpl d, double[] cursor) {
    if (d == null) return null;
    MouseBehavior mb = d.getDisplayRenderer().getMouseBehavior();
    return mb.getScreenCoords(cursor);
  }

  /** Converts the given pixel coordinates to domain coordinates. */
  public static double[] pixelToDomain(DisplayImpl d, int x, int y) {
    return cursorToDomain(d, pixelToCursor(d, x, y));
  }

  /** Converts the given domain coordinates to pixel coordinates. */
  public static int[] domainToPixel(DisplayImpl d, double[] domain) {
    return cursorToPixel(d, domainToCursor(d, domain));
  }

  /** Evaluates the given function at the specified domain coordinates. */
  public static double[] evaluate(FunctionImpl data, double[] domain)
    throws VisADException, RemoteException
  {
    // build data objects
    FunctionType functionType = (FunctionType) data.getType();
    RealTupleType domainType = functionType.getDomain();
    RealType[] domainTypes = domainType.getRealComponents();
    int len = domainTypes.length < domain.length ?
      domainTypes.length : domain.length;
    Real[] v = new Real[len];
    for (int i=0; i 

Ads help maintain this website.