/*=========================================================================

  Program:   Orbit Visualization Tool
  Source:    $Source: /stor/devel/ovt2g/ovt/object/Magnetopause.java,v $
  Date:      $Date: 2001/03/27 17:17:19 $
  Version:   $Revision: 1.9 $


Copyright (c) 2000 OVT Team (Kristof Stasiewicz, Mykola Khotyaintsev, 
Yuri Khotyaintsev)
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification is permitted provided that the following conditions are met:

 * No part of the software can be included in any commercial package without
written consent from the OVT team.

 * Redistributions of the source or binary code must retain the above
copyright notice, this list of conditions and the following disclaimer.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT OR
INDIRECT DAMAGES  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE.

OVT Team (http://ovt.irfu.se)   K. Stasiewicz, M. Khotyaintsev, Y.
Khotyaintsev

=========================================================================*/

/*
 * Magnetopause.java
 *
 * Created on April 7, 2000, 2:37 PM
 */
 
package ovt.object;

/** 
 *
 * @author  ko
 * @version 
 */
import ovt.*;
import ovt.mag.*;
import ovt.gui.*;
import ovt.model.magnetopause.*;
import ovt.util.*;
import ovt.event.*;
import ovt.beans.*;
import ovt.object.editor.*;
import ovt.datatype.*;
import ovt.interfaces.*;

import vtk.*;

import java.beans.*;
import java.lang.Math;
import java.util.*;

import java.awt.event.*;

import javax.swing.*;

public class Magnetopause extends SingleActorObject implements 
    TimeChangeListener, CoordinateSystemChangeListener, 
    MagPropsChangeListener, MenuItemsSource {
      
  public static final int SURFACE = 0;
  public static final int WIREFRAME = 1;

/** Holds value of property representation. */
  private int representation;
  private int[] activityDependsOn = { MagProps.SWP, MagProps.IMF_Z };
  /** Holds Characteristics of this object */
  private Characteristics characteristics = new Characteristics(-1);

public Magnetopause(OVTCore core) { 
  super(core, "Magnetopause", core.getImagesDir()+"magnetopause.gif");
  Log.log("Magnetopause :: init ...", 3);
}


protected void show() {
  super.show();
  if (representation == SURFACE) setRepresentationToSurface();
  else setRepresentationToWireframe();
  rotate();
}

protected void setRepresentationToWireframe() 
	{ actor.GetProperty().SetRepresentationToWireframe(); }

protected void setRepresentationToSurface() 
	{ actor.GetProperty().SetRepresentationToSurface(); }


protected void validate() {
    
	// create actor
	// Here we go!
	Log.log("Recalculating Magnetopause ...", 5);
        
	double[] gsm = new double[3];
	double[] rv = new double[3];
	
	double thetaMax = 150*Const.D_TO_R;
	
	double phi, theta, dPhi, dTheta, r;
        int phiResolution = 50, thetaResolution = 60;
	double x, y, z;
	
	dPhi   = 2.*Math.PI/(phiResolution );
	dTheta = thetaMax/(thetaResolution );
	
	vtkPoints points = new vtkPoints();
        
        double swp = getMagProps().getSWP(getMjd());
        double bz = getMagProps().getIMF(getMjd())[2];
        
        // save characteristics
        characteristics.setMjd(getMjd());
        characteristics.put(MagProps.SWP, swp);
        characteristics.put(MagProps.IMF_Z, bz);
        
        double cosTheta, sinTheta, cosPhi, sinPhi;
        
	int i, j;
        int sizex = phiResolution + 1;
        int sizey = thetaResolution + 1;
        
	for (theta=0, i=0; i<sizey; theta+=dTheta, i++) {
          sinTheta = Math.sin(theta);
          cosTheta = Math.cos(theta);
	  for (phi=0, j=0; j<sizex; phi+=dPhi, j++) {
            sinPhi = Math.sin(phi);
            cosPhi = Math.cos(phi);
            
            r = Shue97.getR(cosTheta, swp, bz);
            
            x = r * cosTheta;
            z = r * sinTheta * cosPhi;
            y = r * sinTheta * sinPhi;
            points.InsertNextPoint(x, y, z);
	  }
	}
        
	vtkStructuredGrid sgrid = new vtkStructuredGrid();
			sgrid.SetDimensions(sizex, sizey,1);
			sgrid.SetPoints(points);
		
	vtkStructuredGridGeometryFilter gfilter = new vtkStructuredGridGeometryFilter();
			gfilter.SetInput(sgrid);
			gfilter.SetExtent(0,sizex,0,sizey,0,0);
                        

	vtkPolyDataMapper mapper = new vtkPolyDataMapper();
			mapper.SetInput(gfilter.GetOutput());
		
        actor = new vtkActor();
        	actor.SetMapper(mapper);
        	actor.GetProperty().SetColor(0,0,1);
                actor.GetProperty().SetOpacity(0.1);		
        super.validate();
}

public void update() {
  actor = null;
  valid = true;
}

public void rotate() {
    Matrix3x3 m3x3 = getTrans(getMjd()).gsm_trans_matrix(getCS());
    actor.SetUserMatrix(m3x3.getVTKMatrix()); 
}




/** Getter for property representation.
 * @return Value of property representation.
 */
public int getRepresentation() {
  return representation;
}
/** Setter for property representation.
 * @param representation New value of property representation.
 */
public void setRepresentation(int representation) {
  this.representation = representation;
}
public void timeChanged(TimeEvent evt) {
  // check if SWP and MachNumber Changed
  Characteristics newCh = getMagProps().getCharacteristics(activityDependsOn, getMjd());
  if (!characteristics.equals(newCh)) {
      invalidate();
      if (isVisible()) {
            hide();
            show();
      }
  } else if (isVisible()) rotate();
}

public void coordinateSystemChanged(CoordinateSystemEvent evt) {
  if (isVisible()) rotate();
}
public void magPropsChanged(MagPropsEvent evt) {
    if (evt.whatChanged() == MagProps.SWP  || evt.whatChanged() == MagProps.IMF) {
    // check if SWP and IMF_Bz changed
      Characteristics newCh = getMagProps().getCharacteristics(activityDependsOn, getMjd());
      if (!characteristics.equals(newCh)) {
        invalidate();
        if (isVisible()) {
            hide();
            show();
        }
      }
  }
}


public JMenuItem[] getMenuItems() {
    JMenu menu = new JMenu("Depends on");
        menu.setFont(Style.getMenuFont());
    JMenuItem item = new JMenuItem("SWP...");
        item.setFont(Style.getMenuFont());
        item.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                    getMagProps().activityEditors[MagProps.SWP].setVisible(true);
            }
        });
        menu.add(item);
    item = new JMenuItem("IMF [Bz]...");
        item.setFont(Style.getMenuFont());
        item.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                    getMagProps().activityEditors[MagProps.IMF].setVisible(true);
            }
        });
        menu.add(item);
    return new JMenuItem[] { menu };
}

}
