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

   Program: ParaView
   Module:    $RCSfile$

   Copyright (c) 2005,2006 Sandia Corporation, Kitware Inc.
   All rights reserved.

   ParaView is a free software; you can redistribute it and/or modify it
   under the terms of the ParaView license version 1.2.

   See License_v1.2.txt for the full ParaView license.
   A copy of this license can be obtained by contacting
   Kitware Inc.
   28 Corporate Drive
   Clifton Park, NY 12065
   USA

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 MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

========================================================================*/
#include "vtkVRControlChangePositionStyle.h"

#include "vtkPVXMLElement.h"
#include "vtkSMProperty.h"
#include "vtkSMProxy.h"
#include "vtkSMDoubleVectorProperty.h"
#include "vtkSMProxyManager.h"
#include "vtkSMSessionProxyManager.h"
#include "vtkSMProxyLocator.h"
#include "vtkVRQueue.h"
#include <iostream>
#include <sstream>
#include <algorithm>
#include "pqView.h"
#include "pqActiveObjects.h"
#include "vtkSMRenderViewProxy.h"
#include "vtkSMPropertyHelper.h"
#include "vtkTransform.h"
#include "vtkMatrix4x4.h"

// ----------------------------------------------------------------------------
vtkStandardNewMacro(vtkVRControlChangePositionStyle)

// ----------------------------------------------------------------------------
vtkVRControlChangePositionStyle::vtkVRControlChangePositionStyle()
{
  this->Enabled = false;
  this->NeedsButton = false;
  this->NeedsTracker = false;
  this->NeedsAnalog = true;
}

// ----------------------------------------------------------------------------
void vtkVRControlChangePositionStyle::PrintSelf(ostream &os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os, indent);
  os << indent << "Enabled: " << this->Enabled << endl;
}

// ----------------------------------------------------------------------------
vtkVRControlChangePositionStyle::~vtkVRControlChangePositionStyle()
{
}

// ----------------------------------------------------------------------------
bool vtkVRControlChangePositionStyle::Configure(vtkPVXMLElement *child,
                                               vtkSMProxyLocator *locator)
{
  if (!this->Superclass::Configure(child, locator))
    {
    return false;
    }

  if (!this->AnalogName)
    {
    std::cerr << "vtkVRControlChangePositionStyle::Configure(): "
              << "Please Specify Tracker event" << std::endl
              << "<Tracker name=\"TrackerEventName\"/>"
              << std::endl;
    return false;
    }

  if (this->ControlledProxy == NULL ||
      this->ControlledPropertyName == NULL ||
      this->ControlledPropertyName[0] == '\0')
    {
    std::cerr << "vtkVRControlChangePositionStyle::Configure(): "
                 "Proxy/property undefined!" << std::endl;
    return false;
    }

  return true;
}

// ----------------------------------------------------------------------------
vtkPVXMLElement* vtkVRControlChangePositionStyle::SaveConfiguration() const
{
  return this->Superclass::SaveConfiguration();
}

// ----------------------------------------------------------------------------
bool vtkVRControlChangePositionStyle::Update()
{
  if (!this->ControlledProxy)
    {
    return false;
    }

  pqView *view = 0;
  vtkSMRenderViewProxy *proxy =0;
  view = pqActiveObjects::instance().activeView();
  if (view)
    {
    proxy = vtkSMRenderViewProxy::SafeDownCast(view->getViewProxy());
    if (proxy)
      {
      this->ControlledProxy->UpdateVTKObjects();
      proxy->UpdateVTKObjects();
      return true;
      }
    }
  return false;
}

// ----------------------------------------------------------------------------
void vtkVRControlChangePositionStyle::HandleAnalog( const vtkVREventData& data )
{
  if (data.name.compare(this->AnalogName) != 0)
    {
    return;
    }

  vtkSMRenderViewProxy *proxy =0;
  vtkSMDoubleVectorProperty *prop =0;
  pqView *view = pqActiveObjects::instance().activeView();
  if (view)
    {
    proxy = vtkSMRenderViewProxy::SafeDownCast(view->getViewProxy());
    if (proxy)
      {
	if (vtkSMPropertyHelper( proxy, "CameraParallelProjection" ).GetAsInt(0)) {
      	//ParallelProjection Mode
        double trans[3];
	double rot[3];
	double pos[3];
	double dphi[3];
	double CPS=vtkSMPropertyHelper( proxy, "CameraParallelScale" ).GetAsDouble(0); //near and far

         vtkSMPropertyHelper( proxy, "CameraFocalPoint" ).Get( trans, 3 ); //translation
         vtkSMPropertyHelper( proxy, "CameraPosition" ).Get( pos, 3 ); //position
         vtkSMPropertyHelper( proxy, "CameraViewUp" ).Get( rot, 3 ); //rotation

         trans[0] -= data.data.analog.channel[0]/10;
         //trans[1] += data.data.analog.channel[1]/10;
         trans[2] += data.data.analog.channel[2]/10;

	 pos[0] -= data.data.analog.channel[0]/10;
	 //pos[1] += data.data.analog.channel[1]/10;
	 pos[2] += data.data.analog.channel[2]/10;

	 CPS+=data.data.analog.channel[1]/10;

         vtkSMPropertyHelper( proxy, "CameraFocalPoint" ).Set( trans, 3 );
         vtkSMPropertyHelper( proxy, "CameraPosition" ).Set( pos, 3 );
         vtkSMPropertyHelper( proxy, "CameraParallelScale" ).Set(0, CPS );

         phi+= data.data.analog.channel[3]*M_PI/1800;
         theta+= data.data.analog.channel[4]*M_PI/1800;
         psi+= data.data.analog.channel[5]*M_PI/1800;

	 double x=rot[0];
	 double y=rot[1];
	 double z=rot[2];

	 double tt[3][3];
	 tt[0][0]=cos(theta)*cos(psi);
	 tt[0][1]=-cos(phi)*sin(psi)+sin(phi)*sin(theta)*cos(psi);
	 tt[0][2]=sin(phi)*sin(psi)+cos(phi)*sin(theta)*cos(psi);

	 tt[1][0]=cos(theta)*sin(psi);
	 tt[1][1]=cos(phi)*cos(psi)+sin(phi)*sin(theta)*cos(psi);
	 tt[1][2]=-sin(phi)*cos(psi)+cos(phi)*sin(theta)*sin(psi);

	 tt[2][0]=-sin(theta);
	 tt[2][1]=sin(phi)*cos(theta);
	 tt[2][2]=cos(phi)*cos(theta);

	 rot[0] = x*tt[0][0]+y*tt[0][1]+z*tt[0][2];
	 rot[1] = x*tt[1][0]+y*tt[1][1]+z*tt[1][2];
	 rot[2] = x*tt[2][0]+y*tt[2][1]+z*tt[2][2];

          //vtkSMPropertyHelper( proxy, "CameraViewUp" ).Set( rot, 3 );
	 }
	 else {
	 //orthogonal projection
	 double trans[3];
	 double rot[3];
	 double pos[3];
	 double dphi[3];
	
         vtkSMPropertyHelper( proxy, "CameraFocalPoint" ).Get( trans, 3 ); //translation
         vtkSMPropertyHelper( proxy, "CameraPosition" ).Get( pos, 3 ); //position
         vtkSMPropertyHelper( proxy, "CameraViewUp" ).Get( rot, 3 ); //rotation

         trans[0] -= data.data.analog.channel[0]/10;
         trans[1] += data.data.analog.channel[1]/10;
         trans[2] += data.data.analog.channel[2]/10;

	 pos[0] -= data.data.analog.channel[0]/10;
	 pos[1] += data.data.analog.channel[1]/10;
	 pos[2] += data.data.analog.channel[2]/10;

	 
         vtkSMPropertyHelper( proxy, "CameraFocalPoint" ).Set( trans, 3 );
         vtkSMPropertyHelper( proxy, "CameraPosition" ).Set( pos, 3 );
         
         phi= data.data.analog.channel[3]*M_PI/180;
         theta= data.data.analog.channel[4]*M_PI/180;
         psi= data.data.analog.channel[5]*M_PI/180;

	 double x=rot[0];
	 double y=rot[1];
	 double z=rot[2];

	 double tt[3][3];
	 tt[0][0]=cos(theta)*cos(psi);
	 tt[0][1]=-cos(phi)*sin(psi)+sin(phi)*sin(theta)*cos(psi);
	 tt[0][2]=sin(phi)*sin(psi)+cos(phi)*sin(theta)*cos(psi);

	 tt[1][0]=cos(theta)*sin(psi);
	 tt[1][1]=cos(phi)*cos(psi)+sin(phi)*sin(theta)*cos(psi);
	 tt[1][2]=-sin(phi)*cos(psi)+cos(phi)*sin(theta)*sin(psi);

	 tt[2][0]=-sin(theta);
	 tt[2][1]=sin(phi)*cos(theta);
	 tt[2][2]=cos(phi)*cos(theta);

	 rot[0] = x*tt[0][0]+y*tt[0][1]+z*tt[0][2];
	 rot[1] = x*tt[1][0]+y*tt[1][1]+z*tt[1][2];
	 rot[2] = x*tt[2][0]+y*tt[2][1]+z*tt[2][2];

         vtkSMPropertyHelper( proxy, "CameraViewUp" ).Set( rot, 3 );

	 }
        }
      }
}
