
/**************************************************************************
THIS IS A MODIFIED VERSION OF THE original vtkVRMLExporter class.
This version is implemented using C++ streams rather than that C FILE pointer
and should be more 'script-friendly'. For instance, when serving VRML files
over the web with one of the scripted languages, you don't have to write out a 
temporary file and then read it back in with some kind of file writer and then write
it back to the web server output stream. (What a mess!)

Now you can simply call vtkVRMLExporterY::SetWriteTargetToString to switch the 
output stream to an internal stream buffer and you can get the VRML from the VRMLString
property. No more file mess!

vtkVRMLExporterY export
export SetInput someInput
export SetWriteTargetToString
export Write
set output [export GetVRMLString]


-Maik
***************************************************************************/
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    $RCSfile: vtkVRMLExporterY.cxx,v $

  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notice for more information.

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

#include "vtkAssemblyNode.h"
#include "vtkAssemblyPath.h"
#include "vtkCamera.h"
#include "vtkCellArray.h"
#include "vtkGeometryFilter.h"
#include "vtkImageData.h"
#include "vtkLight.h"
#include "vtkLightCollection.h"
#include "vtkMath.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkPolyData.h"
#include "vtkPolyDataMapper.h"
#include "vtkProperty.h"
#include "vtkRenderWindow.h"
#include "vtkRendererCollection.h"
#include "vtkTexture.h"
#include "vtkTransform.h"

#include <sstream>
using vtkstd::ostringstream;
#include <string>
using vtkstd::string;

vtkCxxRevisionMacro(vtkVRMLExporterY, "$Revision: 1.79 $");
vtkStandardNewMacro(vtkVRMLExporterY);

vtkVRMLExporterY::vtkVRMLExporterY()
{
  this->Speed = 4.0;
  this->FileName = NULL;
  this->StreamPointer = NULL;
	this->StringPointer = new ostringstream();
	//this->StringPointer->re
	this->VRMLString = new char[1];
}

vtkVRMLExporterY::~vtkVRMLExporterY() 
{
  if ( this->FileName )
    {
    delete [] this->FileName;
    }
	delete this->StringPointer;
	delete [] this->VRMLString;
}

void vtkVRMLExporterY::SetStreamPointer(ostream* fp)
{
  if (fp != this->StreamPointer)
    {
    this->Modified();
    this->StreamPointer = fp;
    }
}

/*const char *vtkVRMLExporterY::GetVRMLString() {
	//cout << "strlen = " << this->StringPointer->str().length() << endl;
	//cout << this->StringPointer->str().c_str();
	return this->StringPointer->str().c_str();
}*/
 
void vtkVRMLExporterY::WriteDataInternal(ostream& fp) {
	vtkRenderer *ren;
  vtkActorCollection *ac;
  vtkActor *anActor, *aPart;
  vtkLightCollection *lc;
  vtkLight *aLight;
  vtkCamera *cam;
  double *tempd;

	/*stringstream fp;
	ostream &tempppp = fp;*/
	ren = this->RenderWindow->GetRenderers()->GetFirstRenderer();
	//
  //  Write header
  //
	vtkDebugMacro("Writing VRML file");
	fp << "#VRML V2.0 utf8" << endl;
  fp << "# VRML-Y file written by the visualization toolkit" << endl << endl;

  // Start write the Background
  double background[3];
  ren->GetBackground(background);
  fp << "    Background {" << endl << " ";
  fp << "   skyColor [" << background[0] << " " << background[1] << " " << background[2] << ", ]" << endl;
  fp << "    }" << endl << " ";
  // End of Background
  
  // do the camera
  cam = ren->GetActiveCamera();
  fp << "    Viewpoint\n      {\n      fieldOfView " << cam->GetViewAngle()*3.1415926/180.0 << endl;
  fp << "      position " << setprecision(9) << cam->GetPosition()[0] << " " << setprecision(9) << cam->GetPosition()[1] << " " << setprecision(9) << cam->GetPosition()[2] << endl;
  fp << "      description \"Default View\"\n";
  tempd = cam->GetOrientationWXYZ();
  fp << "      orientation " << tempd[1] << " "<< tempd[2] << " " << tempd[3] << " " << tempd[0]*3.1415926/180.0 << "\n      }\n";

  // do the lights first the ambient then the others
  fp << "    NavigationInfo {\n      type [\"EXAMINE\",\"FLY\"]\n      speed " << this->Speed << endl;
  if (ren->GetLights()->GetNumberOfItems() == 0)
    {
		fp << "      headlight TRUE}\n\n";
    }
  else
    {
    fp << "      headlight FALSE}\n\n";
    }
  fp << "    DirectionalLight { ambientIntensity 1 intensity 0 # ambient light\n";
  fp << "      color " << ren->GetAmbient()[0] << " " << ren->GetAmbient()[1] << " " << ren->GetAmbient()[2] << " }\n\n";
  
  // make sure we have a default light
  // if we dont then use a headlight
  lc = ren->GetLights();
  vtkCollectionSimpleIterator lsit;
  for (lc->InitTraversal(lsit); (aLight = lc->GetNextLight(lsit)); )
    {
    this->WriteALight(aLight, fp);
    }

  // do the actors now
  ac = ren->GetActors();
  vtkAssemblyPath *apath;
  vtkCollectionSimpleIterator ait;
  for (ac->InitTraversal(ait); (anActor = ac->GetNextActor(ait)); )
    {
    for (anActor->InitPathTraversal(); (apath=anActor->GetNextPath()); )
      {
      aPart=(vtkActor *)apath->GetLastNode()->GetProp();
      this->WriteAnActor(aPart, fp);
      }
    }
}

void vtkVRMLExporterY::WriteData()
{
  vtkRenderer *ren;

  // first make sure there is only one renderer in this rendering window
  if (this->RenderWindow->GetRenderers()->GetNumberOfItems() > 1)
    {
    vtkErrorMacro(<< "VRML files only support one renderer per window.");
    return;
    }

  // get the renderer
  ren = this->RenderWindow->GetRenderers()->GetFirstRenderer();
  
  // make sure it has at least one actor
  if (ren->GetActors()->GetNumberOfItems() < 1)
    {
    vtkErrorMacro(<< "no actors found for writing VRML file.");
    return;
    }	
	ofstream ofp;
	switch(this->WriteMode) {
		case 0:
			if (this->FileName) {
				ofp.open(this->FileName);
				if (!ofp) {
					vtkErrorMacro(<< "unable to open VRML file " << this->FileName);
					return;
				}
				this->WriteDataInternal(ofp);
				ofp.close();
			}
			break;
		case 1:
			this->StringPointer->clear();
			this->WriteDataInternal(*(this->StringPointer));
			this->SetVRMLString(this->StringPointer->str().c_str());
			break;
		case 2:
			//let them clear the stream themselves if they want to...
			this->WriteDataInternal(*(this->StreamPointer));
			break;
	}
}

void vtkVRMLExporterY::WriteALight(vtkLight *aLight,ostream& fp)
{
  double *pos, *focus, *color;
  double dir[3];
  
  pos = aLight->GetPosition();
  focus = aLight->GetFocalPoint();
  color = aLight->GetColor();

  dir[0] = focus[0] - pos[0];
  dir[1] = focus[1] - pos[1];
  dir[2] = focus[2] - pos[2];
  vtkMath::Normalize(dir);
    
  if (aLight->GetPositional())
    {
    double *attn;
    
    if (aLight->GetConeAngle() >= 180.0)
      {
      fp << "    PointLight {\n";
      }
    else
      { 
      fp << "    SpotLight {\n";
      fp << "      direction "<< dir[0] << " " << dir[1] << " " << dir[2] << endl;
      fp << "      cutOffAngle " << aLight->GetConeAngle() << endl;
      }
    fp << "      location " << pos[0] << " " << pos[1] << " " <<  pos[2] << endl;
    attn = aLight->GetAttenuationValues();
    fp << "      attenuation " << attn[0] << " " << attn[1] << " " << attn[2] << "\n";
    }
  else
    {
    fp << "    DirectionalLight {\n";
    fp << "      direction %f %f %f\n",dir[0], dir[1], dir[2];
    }

  fp << "      color %f %f %f\n", color[0], color[1], color[2];
  fp << "      intensity %f\n", aLight->GetIntensity();
  if (aLight->GetSwitch())
    {
    fp << "      on TRUE\n      }\n";
    }
  else
    {
    fp << "      on FALSE\n      }\n";
    }
}

void vtkVRMLExporterY::WriteAnActor(vtkActor *anActor, ostream& fp)
{
  vtkDataSet *ds;
  vtkPolyData *pd;
  vtkGeometryFilter *gf = NULL;
  vtkPointData *pntData;
  vtkPoints *points;
  vtkDataArray *normals = NULL;
  vtkDataArray *tcoords = NULL;
  int i, i1, i2;
  vtkProperty *prop;
  double *tempd;
  vtkCellArray *cells;
  vtkIdType npts = 0;
  vtkIdType *indx = 0;
  double tempf2;
  int pointDataWritten = 0;
  vtkPolyDataMapper *pm;
  vtkUnsignedCharArray *colors;
  double *p;
  unsigned char *c;
  vtkTransform *trans;
  int totalValues;
  
  // see if the actor has a mapper. it could be an assembly
  if (anActor->GetMapper() == NULL)
    {
    return;
    }

  // first stuff out the transform
  trans = vtkTransform::New();
  trans->SetMatrix(anActor->vtkProp3D::GetMatrix());
  
  fp << "    Transform {\n";
  tempd = trans->GetPosition();
  fp << "      translation " << tempd[0] << " " << tempd[1] << " " << tempd[2] << "\n";
  tempd = trans->GetOrientationWXYZ();
	fp << "      rotation " << tempd[1] << " " << tempd[2] << " " <<
          tempd[3] << " "<< tempd[0]*3.1415926/180.0 << "\n";
  tempd = trans->GetScale();
  fp << "      scale " << tempd[0] << " " << tempd[1] << " " << tempd[2] <<"\n";
  fp << "      children [\n";
  trans->Delete();
  
  // get the mappers input and matrix
  ds = anActor->GetMapper()->GetInput();
  
  // we really want polydata
  if ( ds->GetDataObjectType() != VTK_POLY_DATA )
    {
    gf = vtkGeometryFilter::New();
    gf->SetInput(ds);
    gf->Update();
    pd = gf->GetOutput();
    }
  else
    {
    pd = (vtkPolyData *)ds;
    }

  pm = vtkPolyDataMapper::New();
  pm->SetInput(pd);
  pm->SetScalarRange(anActor->GetMapper()->GetScalarRange());
  pm->SetScalarVisibility(anActor->GetMapper()->GetScalarVisibility());
  pm->SetLookupTable(anActor->GetMapper()->GetLookupTable());
  pm->SetScalarMode(anActor->GetMapper()->GetScalarMode());

  if ( pm->GetScalarMode() == VTK_SCALAR_MODE_USE_POINT_FIELD_DATA ||
       pm->GetScalarMode() == VTK_SCALAR_MODE_USE_CELL_FIELD_DATA )
    {
    if ( anActor->GetMapper()->GetArrayAccessMode() == VTK_GET_ARRAY_BY_ID )
      {
      pm->ColorByArrayComponent(anActor->GetMapper()->GetArrayId(),
        anActor->GetMapper()->GetArrayComponent());
      }
    else
      {
      pm->ColorByArrayComponent(anActor->GetMapper()->GetArrayName(),
        anActor->GetMapper()->GetArrayComponent());
      }
    }

  points = pd->GetPoints();
  pntData = pd->GetPointData();
  normals = pntData->GetNormals();
  tcoords = pntData->GetTCoords();
  colors  = pm->MapScalars(1.0);
  
  fp << "        Shape {\n";
  
  // write out the material properties to the mat file
  fp << "          appearance Appearance {\n";
  fp << "            material Material {\n";
  prop = anActor->GetProperty();
  fp << "              ambientIntensity " << prop->GetAmbient() << "\n";
  // if we don't have colors and we have only lines & points
  // use emissive to color them
  if (!(normals || colors || pd->GetNumberOfPolys() || 
        pd->GetNumberOfStrips()))
    {
    tempf2 = prop->GetAmbient();
    tempd = prop->GetAmbientColor();
    fp << "              emissiveColor " << tempd[0]*tempf2 << " " << tempd[1]*tempf2 << " " << tempd[2]*tempf2 << "\n";
    }
  tempf2 = prop->GetDiffuse();
  tempd = prop->GetDiffuseColor();
  fp << "              diffuseColor " << tempd[0]*tempf2 << " " << tempd[1]*tempf2 << " " << tempd[2]*tempf2 <<"\n";
  tempf2 = prop->GetSpecular();
  tempd = prop->GetSpecularColor();
  fp << "              specularColor " << tempd[0]*tempf2 << " " << tempd[1]*tempf2 << " " << tempd[2]*tempf2 << "\n";
	fp << "              shininess " << prop->GetSpecularPower()/128.0 << "\n";
  fp << "              transparency " << 1.0 - prop->GetOpacity() << "\n";
  fp << "              }\n"; // close matrial

  // is there a texture map
  if (anActor->GetTexture())
    {
    vtkTexture *aTexture = anActor->GetTexture();
    int *size, xsize, ysize, bpp;
    vtkDataArray *scalars;
    vtkDataArray *mappedScalars;
    unsigned char *txtrData;
    
    // make sure it is updated and then get some info
    if (aTexture->GetInput() == NULL)
      {
      vtkErrorMacro(<< "texture has no input!\n");
      return;
      }
    aTexture->GetInput()->Update();
    size = aTexture->GetInput()->GetDimensions();
    scalars = aTexture->GetInput()->GetPointData()->GetScalars();

    // make sure scalars are non null
    if (!scalars) 
      {
      vtkErrorMacro(<< "No scalar values found for texture input!\n");
      return;
      }

    // make sure using unsigned char data of color scalars type
    if (aTexture->GetMapColorScalarsThroughLookupTable () ||
        (scalars->GetDataType() != VTK_UNSIGNED_CHAR) )
      {
      mappedScalars = aTexture->GetMappedScalars ();
      }
    else
      {
      mappedScalars = scalars;
      }

    // we only support 2d texture maps right now
    // so one of the three sizes must be 1, but it 
    // could be any of them, so lets find it
    if (size[0] == 1)
      {
      xsize = size[1]; ysize = size[2];
      }
    else
      {
      xsize = size[0];
      if (size[1] == 1)
        {
        ysize = size[2];
        }
      else
        {
        ysize = size[1];
        if (size[2] != 1)
          {
          vtkErrorMacro(<< "3D texture maps currently are not supported!\n");
          return;
          }
        }
      }

    fp << "            texture PixelTexture {\n";
    bpp = mappedScalars->GetNumberOfComponents();
    fp << "              image " << xsize << " " << ysize << " " << bpp <<"\n";
    txtrData = static_cast<vtkUnsignedCharArray*>(mappedScalars)->GetPointer(0);
    totalValues = xsize*ysize;
    for (i = 0; i < totalValues; i++)
      {
      fp << "0x" << hex << setprecision(2) << *txtrData;
      txtrData++;
      if (bpp > 1) 
        {
        fp << hex << setprecision(2) << *txtrData;
        txtrData++;
        }
      if (bpp > 2) 
        {
        fp << hex << setprecision(2) << *txtrData;
        txtrData++;
        }
      if (bpp > 3) 
        {
        fp << hex << setprecision(2) << *txtrData;
        txtrData++;
        }
      if (i%8 == 0)
        {
        fp << "\n";
        }
      else
        {
        fp << " ";
        }
      }
    if (!(aTexture->GetRepeat()))
      {
      fp << "              repeatS FALSE\n";
      fp << "              repeatT FALSE\n";
      }
    fp << "              }\n"; // close texture
    }
  fp << "            }\n"; // close appearance

  // write out polys if any
  if (pd->GetNumberOfPolys() > 0)
    {
    fp << "          geometry IndexedFaceSet {\n";
    // two sided lighting ? for now assume it is on
    fp << "            solid FALSE\n";
    if (!pointDataWritten)
      {
      this->WritePointData(points, normals, tcoords, colors, fp);
      pointDataWritten = 1;
      }
    else
      {
      fp << "            coord  USE VTKcoordinates\n";
      if (normals)
        {
        fp << "            normal  USE VTKnormals\n";
        }
      if (tcoords)
        {
        fp << "            texCoord  USE VTKtcoords\n";
        }
      if (colors)
        {
        fp << "            color  USE VTKcolors\n";
        }
      }
    
    fp << "            coordIndex  [\n";
    
    cells = pd->GetPolys();
    for (cells->InitTraversal(); cells->GetNextCell(npts,indx); )
      {
      fp << "              ";
      for (i = 0; i < npts; i++)
        {
        // treating vtkIdType as int
        fp << (int)indx[i] <<  ", ";
        }
      fp << "-1,\n";
      }
    fp << "            ]\n";
    fp << "          }\n";
    }

  // write out tstrips if any
  if (pd->GetNumberOfStrips() > 0)
    {
    fp << "          geometry IndexedFaceSet {\n";
    if (!pointDataWritten)
      {
      this->WritePointData(points, normals, tcoords, colors, fp);
      pointDataWritten = 1;
      }
    else
      {
      fp << "            coord  USE VTKcoordinates\n";
      if (normals)
        {
        fp << "            normal  USE VTKnormals\n";
        }
      if (tcoords)
        {
        fp << "            texCoord  USE VTKtcoords\n";
        }
      if (colors)
        {
        fp << "            color  USE VTKcolors\n";
        }
      }
    fp << "            coordIndex  [\n";
    cells = pd->GetStrips();
    for (cells->InitTraversal(); cells->GetNextCell(npts,indx); )
      {
      for (i = 2; i < npts; i++)
        {
        if (i%2)
          {
          i1 = i - 1;
          i2 = i - 2;
          }
        else
          {
          i1 = i - 2;
          i2 = i - 1;
          }
        // treating vtkIdType as int
        fp << "              " << (int)indx[i1] << ", " << (int)indx[i2] << ", " << (int)indx[i];
        }
      }
    fp << "            ]\n";
    fp << "          }\n";
    }
  
  // write out lines if any
  if (pd->GetNumberOfLines() > 0)
    {
    fp << "          geometry IndexedLineSet {\n";
    if (!pointDataWritten)
      {
      this->WritePointData(points, NULL, NULL, colors, fp);
      pointDataWritten = 1;
      }
    else
      {
      fp << "            coord  USE VTKcoordinates\n";
      if (colors)
        {
        fp << "            color  USE VTKcolors\n";
        }
      }
    
    fp << "            coordIndex  [\n";
    
    cells = pd->GetLines();
    for (cells->InitTraversal(); cells->GetNextCell(npts,indx); )
      {
      fp << "              ";
      for (i = 0; i < npts; i++)
        {
        // treating vtkIdType as int
        fp << (int)indx[i] << ", ";
        }
      fp << "-1,\n";
      }
    fp << "            ]\n";
    fp << "          }\n";
    }

  // write out verts if any
  if (pd->GetNumberOfVerts() > 0)
    {
    fp << "          geometry PointSet {\n";
    cells = pd->GetVerts();
    fp << "            coord Coordinate {";
    fp << "              point [";
    for (cells->InitTraversal(); cells->GetNextCell(npts,indx); )
      {
      fp << "              ";
      for (i = 0; i < npts; i++)
        {
        p = points->GetPoint(indx[i]);
        fp << "              " << setprecision(9) << p[0] << " " << setprecision(9) << p[1] << " "<< p[2] << ",\n";
        }
      }
    fp << "              ]\n";
    fp << "            }\n";
    if (colors)
        {
        fp << "            color Color {";
        fp << "              color [";
        for (cells->InitTraversal(); cells->GetNextCell(npts,indx); )
          {
          fp << "              ";
          for (i = 0; i < npts; i++)
            {
            c = colors->GetPointer(4*indx[i]);
            fp << "           " << c[0]/255.0 << " " << c[1]/255.0 << " " << c[2]/255.0 << ",\n";
            }
          }
        fp << "              ]\n";
        fp << "            }\n";
        }
  
    fp << "          }\n";
    }

  fp << "        }\n"; // close the  Shape
  fp << "      ]\n"; // close the original transforms children
  fp << "    }\n"; // close the original transform
  
  if (gf)
    {
    gf->Delete();
    }
  pm->Delete();
}

void vtkVRMLExporterY::WritePointData(vtkPoints *points, vtkDataArray *normals,
                                     vtkDataArray *tcoords, 
                                     vtkUnsignedCharArray *colors, ostream& fp)
{
  double *p;
  int i;
  unsigned char *c;
  
  // write out the points
  fp << "            coord DEF VTKcoordinates Coordinate {\n";
  fp << "              point [\n";
  for (i = 0; i < points->GetNumberOfPoints(); i++)
    {
    p = points->GetPoint(i);
    fp << "              " << p[0] << " " << p[1] << " " << p[2] << ",\n";
    }
  fp << "              ]\n";
  fp << "            }\n";
  
  // write out the point data
  if (normals)
    {
    fp << "            normal DEF VTKnormals Normal {\n";
    fp << "              vector [\n";
    for (i = 0; i < normals->GetNumberOfTuples(); i++)
      {
      p = normals->GetTuple(i);
      fp << "           " << p[0] << " " << p[1] << " " << p[2] << ",\n";
      }
    fp << "            ]\n";
    fp << "          }\n";
    }

  // write out the point data
  if (tcoords)
    {
    fp << "            texCoord DEF VTKtcoords TextureCoordinate {\n";
    fp << "              point [\n";
    for (i = 0; i < tcoords->GetNumberOfTuples(); i++)
      {
      p = tcoords->GetTuple(i);
      fp << "           " << p[0] << " " << p[1] << ",\n";
      }
    fp << "            ]\n";
    fp << "          }\n";
    }

  // write out the point data
  if (colors)
    {
    fp << "            color DEF VTKcolors Color {\n";
    fp << "              color [\n";
    for (i = 0; i < colors->GetNumberOfTuples(); i++)
      {
      c = colors->GetPointer(4*i);
      fp << "           " << c[0]/255.0 << " " << c[1]/255.0 << " " << c[2]/255.0 << ",\n";
      }
    fp << "            ]\n";
    fp << "          }\n";
    }
}


void vtkVRMLExporterY::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os,indent);
 
  if (this->FileName)
    {
    os << indent << "FileName: " << this->FileName << "\n";
    }
  else
    {
    os << indent << "FileName: (null)\n";
    }
  os << indent << "Speed: " << this->Speed << "\n";
}

