#######################################################################
#
# $Id: Plot2D_vtk.py,v 1.2 2008-02-07 05:45:25 lorenz Exp $
# 
# Copyright (C) 2004-2008 Petr Lorenz
# All rights reserved.
# 
# ##### BEGIN LICENSE BLOCK #####
#
# Version: MPL 1.1 / GPL 2.0 / LGPL 2.1
#
# The contents of this file are subject to the Mozilla Public License
# Version 1.1 (the "License"); you may not use this file except in
# compliance with the License. You may obtain a copy of the License at
# http://www.mozilla.org/MPL/
# 
# Software distributed under the License is distributed on an "AS IS"
# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
# License for the specific language governing rights and limitations
# under the License.
# 
# The Original Code is Petr Lorenz emGine Graphical User Interface (GUI).
# 
# The Initial Developer of the Original Code is Petr Lorenz.
# Portions created by the Initial Developer are Copyright (C) 2004-2008.
# All Rights Reserved.
# 
# Alternatively, the contents of this file may be used under the terms of
# either the GNU General Public License Version 2 or later (the  "GPL"), or the
# GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which
# case the provisions of the GPL or the LGPL are applicable instead of those
# above.  If you wish to allow use of your version of this file only under the
# terms of either the GPL or the LGPL and not to allow others to use your
# version of this file under the MPL, indicate your decision by deleting the
# provisions above and replace them with the notice and other provisions
# required by the GPL or LGPL.  If you do not delete the provisions above, a
# recipient may use your version of this file under the terms of any one of the
# MPL License, the GPL or the LGPL.
#
# ##### END LICENSE BLOCK #####
#
#######################################################################
# 
# $Log: Plot2D_vtk.py,v $
# Revision 1.2  2008-02-07 05:45:25  lorenz
# * Fixed 'Simulation Boundary View' check problem
#   when closing a project.
#
# * Fixed Bounding Box problems when moving/editing a geometry.
#
# Revision 1.1.1.1  2008-01-28 06:15:35  lorenz
# Import emGine GUI v. 0.7.1
#
# Revision 1.2  2007-07-02 06:37:18  lorenz
# Clean up of old code.
#
# Revision 1.1.1.1  2007-05-08 06:22:35  lorenz
# Initial Import
#
#######################################################################

#
# June-May 2006
#
# 2D plotting with vtk
#
import wx

import vtk
from vtk.wx.wxVTKRenderWindow import wxVTKRenderWindow

import XYDataSource

class Plot2D:
  def __init__(self, master):
    self.polydata = None
    self.x_data = vtk.vtkFloatArray()
    self.y_data = vtk.vtkFloatArray()
    self.hold = 0
    
    # create the VTK-based Plot
    vtk_render_widget = self.vtk_render_widget = wxVTKRenderWindow(master, -1)
    self.hide()		# hide the widget

    xyplot = self.xyplot = vtk.vtkXYPlotActor()
    xyplot.GetProperty().SetColor(1, 0, 0)
    xyplot.GetProperty().SetLineWidth(2)
    xyplot.GetPositionCoordinate().SetValue(0.05, 0.15, 0.0)
    xyplot.GetPosition2Coordinate().SetValue(0.9, 0.7, 1.0)
    xyplot.SetXTitle("")
    xyplot.SetYTitle("")
    tprop = xyplot.GetAxisLabelTextProperty()
    tprop.SetColor(0, 0, 0)
    tprop.ShadowOff()
    tprop.SetFontFamilyToArial()
    #tprop.ItalicOff()
    tprop.ItalicOn()
    tprop.BoldOff()

    ren = self.ren = vtk.vtkRenderer()
    ren.SetBackground(1.00, 1.00, 1.00)
    #ren.SetViewport(0.5, 0.0, 1.0, 1.0)

    self.rw = vtk_render_widget.GetRenderWindow()
    self.rw.AddRenderer(ren) 

  def hold_on(self):
    self.hold = 1

  def hold_off(self):
    self.hold = 0

  def set_dimensions(self, x1, y1, x2, y2):
    self.vtk_render_widget.SetDimensions(x1, y1, x2, y2)

  def show(self):
    self.vtk_render_widget.Show(True)

  def hide(self):
    self.vtk_render_widget.Show(False)

  # Description:
  # Set the floating point values x and y
  def set_data_1(self, xvalues, yvalues):
    if (len(xvalues) != len(yvalues)):
      print "!! Error! Something is wrong with your data to plot"
      return

    #
    # TODO: check if data already set, if so, reallocate (?)
    #
    len_data = len(xvalues)
    self.x_data.SetNumberOfTuples(len_data)
    self.y_data.SetNumberOfTuples(len_data)

    # assign the data
    # TODO: could we use SetArray method directly ?
    for i in range(len_data):
      self.x_data.SetTuple1(i, xvalues[i])
      self.y_data.SetTuple1(i, yvalues[i])


  def plot_data(self, xvalues, yvalues):
    if (self.polydata):
      self.ren.RemoveActor2D(self.xyplot)
      self.xyplot.RemoveInput(self.polydata)
      #return

    if (len(xvalues) != len(yvalues)):
      print "!! Error! Something is wrong with your data to plot"
      return

    len_data = len(xvalues)
    points = vtk.vtkPoints()
    vals = vtk.vtkFloatArray()
    for i in range(len_data):
      points.InsertPoint(i, xvalues[i], 0, 0)
      vals.InsertTuple1(i, yvalues[i])

    self.polydata = polydata = vtk.vtkPolyData()
    polydata.SetPoints(points)
    polydata.GetPointData().SetScalars(vals)

    self.xyplot.AddInput(polydata)
    self.xyplot.SetXValuesToValue()
    self.ren.AddActor2D(self.xyplot)

  def plot_ascii_file(self, fname, column_y = 2, column_x = 1):
    xydata = XYDataSource.XYDataSource()
    xydata.read_from_ascii_file(fname, column_y, column_x)
    self.plot_data(xydata.XYData.get_xlist(), xydata.XYData.get_ylist())

    ## xydata.XYData.fft()
    ## #y_data = xydata.XYData.get_yarray().real.tolist()
    ## y_data = xydata.XYData.get_ylist_abs()
    ## self.plot_data(xydata.XYData.get_xlist(), y_data)

    # TODO: below is the old code, delete it, no more needed
    #f = open(fname)
    #data = f.read().split("\n")
    #x_data = list()
    #y_data = list()
    #for entry in data:
    #  if (entry != ''):
    #	e = entry.split()
    #	x_data.append(float(e[column_x-1])) 
    #	y_data.append(float(e[column_y-1])) 
    #
    #self.plot_data(x_data, y_data)

  def plot_dummy(self):
    x_data = [0.0e-2, 1.0e-2, 2.0e-2, 3.0e-2, 4.0e-2, 5.0e-2, 6.0e-2, 7.0e-2, 8.0e-2, 9.0e-2, 10.0e-2]
    y_data = [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 4.0, 3.0, 2.0, 1.0, 0.0]
    self.plot_data(x_data, y_data)



###############################################
if __name__ == '__main__':
  print "Testing Plot2D Module..." 


  #### root = Tk()
  #### plot = Plot2D(root)

  #### root.grid_rowconfigure(0, weight = 1)
  #### root.grid_columnconfigure(0, weight = 1) 

  #### plot.plot_dummy()
  #### #plot.plot_data(x_data, y_data)
  #### #plot.plot_ascii_file("EH.tst_")

  #### plot.show()


