"""
cylinder.py --                                                                                            
                                                                                                                                                                                                        
It sets a Base'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 Base:
  def __init__( self, ren, geomdim, posorient, lighting, color ):
        """ Inits a Base's 3d object and its associated color and lighting"""
        
        #Cylinder's configuration.
        #Main cylinder of infinital length
        cylinder = vtk.vtkCylinder()
        cylinder.SetRadius(geomdim[1])
        #top plane
        topPlane = vtk.vtkPlane()
        topPlane.SetOrigin(0, geomdim[0], 0)
        topPlane.SetNormal(0, 1, 0)
        #bottom plane
        bottomPlane = vtk.vtkPlane()
        bottomPlane.SetOrigin(0, 0.0, 0)
        bottomPlane.SetNormal(0, -1, 0)
        #right plane
        rightPlane = vtk.vtkPlane()
        rightPlane.SetOrigin(geomdim[1] -0.005, 0, 0)
        rightPlane.SetNormal(1, 0, 0)
        #left plane
        leftPlane = vtk.vtkPlane()
        leftPlane.SetOrigin(-geomdim[1]+0.006, 0, 0)
        leftPlane.SetNormal(-1, 0, 0)
        
        #Base's boolean function: Intersection between Cylinder
        #and the two planes.
        theBase = vtk.vtkImplicitBoolean()
        theBase.SetOperationTypeToIntersection()
        theBase.AddFunction(cylinder)
        theBase.AddFunction(topPlane)
        theBase.AddFunction(bottomPlane)
        theBase.AddFunction(leftPlane)
        theBase.AddFunction(rightPlane)
        
        # The sample function generates a distance function from the implicit
        # function (which in this case is the BaseHole). This is then contoured to
        # get a polygonal surface.
        theBaseSample = vtk.vtkSampleFunction()
        theBaseSample.SetImplicitFunction(theBase)
        theBaseSample.SetModelBounds(-geomdim[1], geomdim[1], -0.1, geomdim[0]+0.01, -geomdim[1]-0.03, geomdim[1]+0.03)
        theBaseSample.SetSampleDimensions(60, 60, 60)
        theBaseSample.ComputeNormalsOff()
        
        #vtk.vtkContourFilter applied in order to get a polygonal surface.
        theBaseSurface = vtk.vtkContourFilter()
        theBaseSurface.SetInputConnection(theBaseSample.GetOutputPort())
        theBaseSurface.SetValue(0, 0.005)

        #Decimation algorithm applied in order to optimaze polygonal surface.
        deci = vtk.vtkDecimatePro()
        deci.SetInputConnection(theBaseSurface.GetOutputPort())
        deci.SetTargetReduction(0.30)
        deci.PreserveTopologyOn()

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

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

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

        self.baseActor = vtk.vtkActor()
        self.baseActor.SetMapper(BaseMapper)
        self.baseActor.GetProperty().SetInterpolationToGouraud()
        self.baseActor.GetProperty().SetColor(color)
        self.baseActor.GetProperty().SetSpecular(.3)
        self.baseActor.GetProperty().SetSpecularPower(30)
        self.baseActor.SetPosition( posorient[0], posorient[1], posorient[2] )
        self.baseActor.SetOrientation( posorient[3], posorient[4], posorient[5] )
       
            
        
    
        
    
    

        
        
            
        
    

    
  
  