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

Program:   Orbit Visualization Tool
Source:    $Source: /stor/devel/ovt2g/ovt/object/OrbitDataModule.java,v $
Date:      $Date: 2001/03/21 13:33:54 $
Version:   $Revision: 1.3 $


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

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

package ovt.object;

/*
 * OrbitDataModule.java
 *
 * Created on November 26, 2000, 5:50 PM
 */


import vtk.*;

import ovt.*;
import ovt.gui.*;
import ovt.mag.*;
import ovt.beans.*;
import ovt.util.*;
import ovt.event.*;
import ovt.datatype.*;
import ovt.interfaces.*;

import java.beans.*;
import java.util.*;
import java.awt.*;
import javax.swing.*;
import java.io.*;

/**
 *
 * @author  ko
 * @version 
 */
public class OrbitDataModule extends SingleActorSatModule {
    private static final int TIME  = 0;
    private static final int VALUE = 1;
    private static final int X = 0;
    private static final int Y = 1;
    private static final int Z = 2;
    DataModule dataModule;
    double height = 0.1;
   
public OrbitDataModule(DataModule module) {
  super(module.getSat(),  "OnOrbit", OVTCore.getImagesDir() + "orbit.gif");
  this.dataModule = module;
  module.addPropertyChangeListener("file", new PropertyChangeListener() {
      public void propertyChange(PropertyChangeEvent evt) {
          //if (!getDataTimeSet().overlaps(getTimeSet())) {
            invalidate();
            if (isVisible()) {
                hide();
                show();
            }
        //} 
        setEnabled(true);
      }
  });
}

private double[][] getData() {
    return dataModule.getData();
}

private TimeSet getDataTimeSet() {
    return dataModule.getDataTimeSet();
}


protected void validate() {
   
   double[][] data = getData();
   if (data == null) return;

   double min = dataModule.getMin();
   double max = dataModule.getMax();
   
   int numberOfPoints = data.length;
   vtkPoints points = new vtkPoints();
   vtkScalars scalars = new vtkScalars();
   double[] r, r2, r3;
   double y = 1, value;
   
   for (int i=0; i<numberOfPoints; i++) {
        r = getPosition(data[i][TIME]);
        //System.out.println("" + new Time(data[i][TIME]));
        if (max >=0 && min <= 0) value = data[i][VALUE] / (max - min) * height;
        else value = (data[i][VALUE] - min) / (max - min) * height;
        
        r3 = Vect.multiply(sat.getTrajectoryNormal(data[i][TIME]), value);
        r2 = Vect.add(r, r3);
        
        //System.out.println(Vect.toString(r));
        //System.out.println("Value=" + value + " orig=" + data[i][VALUE]);
        
        points.InsertPoint(i, r2[X], r2[Y], r2[Z]); // add data point
        points.InsertPoint(numberOfPoints + i, r[X], r[Y], r[Z]); // add orbit point
        // add orbit points
        scalars.InsertNextScalar(data[i][VALUE]);
   }
   
   vtkCellArray polys = new vtkCellArray();   
        /*polys.InsertNextCell(points.GetNumberOfPoints());
    
   for (int i=0; i<points.GetNumberOfPoints(); i++) {
       polys.InsertCellPoint(i);
   }*/
        
   for (int i=0; i<numberOfPoints - 1; i++) {
       polys.InsertNextCell(4);
       polys.InsertCellPoint(i);
       polys.InsertCellPoint(i + 1);
       polys.InsertCellPoint(numberOfPoints + i + 1);
       polys.InsertCellPoint(numberOfPoints + i);
   }

   vtkPolyData profile = new vtkPolyData();
        profile.SetPoints(points);
        profile.SetPolys(polys);
        //profile.SetLines(polys);
        //profile.GetPointData().SetScalars(scalars);
        profile.GetCellData().SetScalars(scalars);

   vtkLookupTable lut  = new vtkLookupTable();
    lut.SetHueRange(0.6667, 0);
    
        
    /*vtkRibbonFilter ribbon = new vtkRibbonFilter();
        ribbon.SetInput(profile);
        ribbon.VaryWidthOn();
        ribbon.SetWidthFactor(0.005);
        ribbon.SetDefaultNormal(0, 1, 0);
        ribbon.UseDefaultNormalOn();
        
    vtkLinearExtrusionFilter extrusion = new vtkLinearExtrusionFilter();
         extrusion.SetInput(ribbon.GetOutput());
         extrusion.SetVector(0, 1, 0);
         extrusion.SetExtrusionType(1);
         extrusion.SetScaleFactor(0.05);

    vtkTubeFilter tube = new vtkTubeFilter();
        tube.SetInput(profile);
        tube.SetNumberOfSides(8);
        tube.SetRadius(0.05);
        tube.SetRadiusFactor(5);
        //tube.SetRadiusFactor(10000);
        tube.SetVaryRadiusToVaryRadiusByScalar(); */
    
    vtkPolyDataMapper mapper = new vtkPolyDataMapper();
    mapper.SetInput(profile);
    //mapper.SetScalarModeToUsePointData();
        mapper.SetScalarModeToUseCellData();
        mapper.ScalarVisibilityOn();
    mapper.SetScalarRange(min, max);
    mapper.SetLookupTable(lut);
        
        
    actor = new vtkActor();
        actor.SetMapper(mapper);
        actor.GetProperty().SetOpacity(0.6);

    super.validate();
}

public void timeChanged(TimeEvent evt) {
    if (evt.getPropertyName().equals("time")) {
        if (!getDataTimeSet().overlaps(getTimeSet())) {
            invalidate();
            if (isVisible()) setVisible(false);
            setEnabled(false);
        } else setEnabled(true);
    }
}

}