"""
cart.py --                                                                                            
                                                                                                                                                                                                        
It sets a tube's geometric properties.                                              
                                                                                                                                                                                                            
Date of creation: 2007-03-17                                                                        
                                                                                                                                                                                                
Copyright  Robotics and Automation Group, Pontificia Universidad Javeriana - Cali.                  
    Freddy Naranjo Perez, fnaranjo@puj.edu.co                                                                   
    Antonio Alejandro Matta Gomez amatta@puj.edu.co                                      
    Julian David Colorado, jdcolorado@puj.edu.co                           
    Juan Camilo Acosta Mejia, jcacosta@puj.edu.co                                    
                                                                                                                                                                                                        
See the file "license.terms" for information on usage and redistribution of this file, and for a    
DISCLAIMER OF ALL WARRANTIES.
"""

import vtk
from vtk.util.colors import *

class Cart:
  def __init__( self, ren, geomdim, posorient, lighting, color ):
        """ Inits a tube's 3d object and its associated color and lighting"""
        
        #top points
        cartpoints = vtk.vtkPoints()
        cartpoints.InsertPoint(0, 0.0, 0.0, 0.0)
        cartpoints.InsertPoint(1, 0.0, 0.0, geomdim[2])
        cartpoints.InsertPoint(2, geomdim[0]-.5, 0.0, 0.0)
        cartpoints.InsertPoint(3, geomdim[0]-.5, 0.0, geomdim[2])
        cartpoints.InsertPoint(4, geomdim[0], 0.0, 0.0)
        cartpoints.InsertPoint(5, geomdim[0], 0.0, geomdim[2])
        cartpoints.InsertPoint(6, 0.0, 0.01, 0.0)
        cartpoints.InsertPoint(7, 0.0, geomdim[1], geomdim[2])

        # The cell array is a connectivity list. For each cell, the number of points is defined 
        # followed by the point ids. This is repeated as many times as there are primitives in
        # the list.
        cartstrip = vtk.vtkCellArray()
        cartstrip.InsertNextCell(8) # number of points
        cartstrip.InsertCellPoint(0)
        cartstrip.InsertCellPoint(1)
        cartstrip.InsertCellPoint(2)
        cartstrip.InsertCellPoint(3)
        cartstrip.InsertCellPoint(4)
        cartstrip.InsertCellPoint(5)
        cartstrip.InsertCellPoint(6)
        cartstrip.InsertCellPoint(7)
        profile = vtk.vtkPolyData()
        profile.SetPoints(cartpoints)
        profile.SetStrips(cartstrip)

        #Now the Implicit Modeller and the Contour Filter are used in order
        #to to model the top Strip Cell Array into the figure that we need.
        cartimp = vtk.vtkImplicitModeller()
        cartimp.SetInput(profile)
        cartimp.SetSampleDimensions(40, 10, 40)
        cartimp.SetMaximumDistance(0.9)
        cartimp.SetModelBounds(-geomdim[0]/2+0.02, geomdim[0]/2-0.02, -0.01, geomdim[1], 0.0, geomdim[2] )

        contour = vtk.vtkContourFilter()
        contour.SetInputConnection(cartimp.GetOutputPort())
        contour.SetValue(0, 0.05)

        #Smoothing applied in order to get a better surface.
        smooth= vtk.vtkSmoothPolyDataFilter()
        smooth.SetInputConnection(contour.GetOutputPort())
        smooth.SetNumberOfIterations(20)

        #Normal's computation.
        normals = vtk.vtkPolyDataNormals()
        normals.SetInputConnection(smooth.GetOutputPort())
        normals.FlipNormalsOn()

        #Normal's passed to the poly data mapper.
        topMapper = vtk.vtkPolyDataMapper()
        topMapper.SetInputConnection(normals.GetOutputPort())
        topMapper.ScalarVisibilityOff()

        self.cartActor = vtk.vtkActor()
        self.cartActor.SetMapper(topMapper)
        self.cartActor.GetProperty().SetInterpolationToGouraud()
        self.cartActor.GetProperty().SetDiffuseColor(color)
        self.cartActor.GetProperty().SetSpecular(.3)
        self.cartActor.GetProperty().SetSpecularPower(30)
        self.cartActor.AddPosition( posorient[0], posorient[1], posorient[2] )
        self.cartActor.AddOrientation( posorient[3], posorient[4], posorient[5] )
            
        
    
        
    
    

        
        
            
        
    

    
  
  