<commit_before># tdObjects.py copyright (c) 2003 by <NAME> <<EMAIL>>
# $Id$
# class that controls the 3-D objects list

import genUtils
reload(genUtils)
import math
import module_kits
from module_kits.vtk_kit import misc
from modules.viewers.slice3dVWRmodules.shared import s3dcGridMixin
import operator
import vtk
import vtkdevide
import wx
import wx.grid
from wx.lib import colourdb

class tdObjects(s3dcGridMixin):
    """Class for keeping track and controlling everything to do with
    3d objects in a slice viewer.  A so-called tdObject can be a vtkPolyData
    or a vtkVolume at this stage.  The internal dict is keyed on the
    tdObject binding itself.
    """

    _objectColours = ['LIMEGREEN', 'SKYBLUE', 'PERU', 'CYAN', 
                      'GOLD',  'MAGENTA', 'GREY80',
                      'PURPLE']

    _gridCols = [('Object Name', 0), ('Colour', 110), ('Visible', 0),
                 ('Contour', 0), ('Motion', 0), ('Scalars',0)]
    
    _gridNameCol = 0
    _gridColourCol = 1
    _gridVisibleCol = 2
    _gridContourCol = 3
    _gridMotionCol = 4
    _gridScalarVisibilityCol = 5

    def __init__(self, slice3dVWRThingy, grid):
        self._tdObjectsDict = {}
        self._objectId = 0
        
        self.slice3dVWR = slice3dVWRThingy
        self._grid = grid
        self._initialiseGrid()
        self._bindEvents()

        # fill out our drop-down menu
        self._disableMenuItems = self._appendGridCommandsToMenu(
            self.slice3dVWR.controlFrame.objectsMenu,
            self.slice3dVWR.controlFrame, disable=True)


        # some GUI events that have to do with the objects list
        wx.EVT_BUTTON(self.slice3dVWR.objectAnimationFrame,
                      self.slice3dVWR.objectAnimationFrame.resetButtonId,
                      self._handlerObjectAnimationReset)

        wx.EVT_SLIDER(self.slice3dVWR.objectAnimationFrame,
                      self.slice3dVWR.objectAnimationFrame.frameSliderId,
                      self._handlerObjectAnimationSlider)
        

        # this will fix up wxTheColourDatabase
        colourdb.updateColourDB()

    def close(self):
        self._tdObjectsDict.clear()
        self.slice3dVWR = None
        self._grid.ClearGrid()
        self._grid = None

    def addObject(self, tdObject):
        """Takes care of all the administration of adding a new 3-d object
        to the list and updating it in the list control.
        """

        if tdObject not in self._tdObjectsDict:

            # we get to pick a colour and a name
            colourName = self._objectColours[
                self._objectId % len(self._objectColours)]
            # objectName HAS TO BE unique!
            objectName = "%s%d" % ('obj', self._objectId)
            self._objectId += 1

            # create a colour as well
            colour = wx.TheColourDatabase.FindColour(colourName).asTuple()
            # normalise
            nColour = tuple([c / 255.0 for c in colour])

            # we'll need this later (when we decide about scalar vis)
            scalarsName = None
            
            # now actually create the necessary thingies and add the object
            # to the scene
            if hasattr(tdObject, 'GetClassName') and \
               callable(tdObject.GetClassName):

                if tdObject.GetClassName() == 'vtkVolume':
                    self.slice3dVWR._threedRenderer.AddVolume(tdObject)
                    self._tdObjectsDict[tdObject] = {'tdObject' : tdObject,
                                                     'type' : 'vtkVolume'}

                    # we have to test this here already... if there's
                    # a VTK error (for instance volume rendering the wrong
                    # type) we remove the volume and re-raise the exception
                    # the calling code will act as if no successful add
                    # was performed, thus feeding back the error
                    try:
                        self.slice3dVWR._threedRenderer.ResetCamera()
                        self.slice3dVWR.render3D()
                    except RuntimeError:
                        self.slice3dVWR._threedRenderer.RemoveVolume(tdObject)
                        raise

                elif tdObject.GetClassName() == 'vtkPolyData':
                    mapper = vtk.vtkPolyDataMapper()
                    mapper.ImmediateModeRenderingOn()
                    mapper.SetInput(tdObject)
                    actor = vtk.vtkActor()
                    actor.SetMapper(mapper)
                    self.slice3dVWR._threedRenderer.AddActor(actor)
                    self._tdObjectsDict[tdObject] = {'tdObject' : tdObject,
                                                     'type' : 'vtkPolyData',
                                                     'vtkActor' : actor}

                    # to get the name of the scalars we need to do this.
                    tdObject.Update()
                
                    if tdObject.GetPointData().GetScalars():
                        scalarsName = tdObject.GetPointData().GetScalars().\
                                      GetName()
                    else:
                        scalarsName = None

                    if scalarsName:
                        mapper.SetScalarRange(tdObject.GetScalarRange())

                    #mapper.ScalarVisibilityOff()
                
#                     if sname and sname.lower().find("curvature") >= 0:
#                         # if the active scalars have "curvature" somewhere in
#                         # their name, activate flat shading and scalar vis
#                         property = actor.GetProperty()<commit_msg>Added NIIIICE shading settings to tdObjects.py<commit_after># tdObjects.py copyright (c) 2003 by <NAME> <<EMAIL>>
# $Id$
# class that controls the 3-D objects list

import genUtils
reload(genUtils)
import math
import module_kits
from module_kits.vtk_kit import misc
from modules.viewers.slice3dVWRmodules.shared import s3dcGridMixin
import operator
import vtk
import vtkdevide
import wx
import wx.grid
from wx.lib import colourdb

class tdObjects(s3dcGridMixin):
    """Class for keeping track and controlling everything to do with
    3d objects in a slice viewer.  A so-called tdObject can be a vtkPolyData
    or a vtkVolume at this stage.  The internal dict is keyed on the
    tdObject binding itself.
    """

    _objectColours = ['LIMEGREEN', 'SKYBLUE', 'PERU', 'CYAN', 
                      'GOLD',  'MAGENTA', 'GREY80',
                      'PURPLE']

    _gridCols = [('Object Name', 0), ('Colour', 110), ('Visible', 0),
                 ('Contour', 0), ('Motion', 0), ('Scalars',0)]
    
    _gridNameCol = 0
    _gridColourCol = 1
    _gridVisibleCol = 2
    _gridContourCol = 3
    _gridMotionCol = 4
    _gridScalarVisibilityCol = 5

    def __init__(self, slice3dVWRThingy, grid):
        self._tdObjectsDict = {}
        self._objectId = 0
        
        self.slice3dVWR = slice3dVWRThingy
        self._grid = grid
        self._initialiseGrid()
        self._bindEvents()

        # fill out our drop-down menu
        self._disableMenuItems = self._appendGridCommandsToMenu(
            self.slice3dVWR.controlFrame.objectsMenu,
            self.slice3dVWR.controlFrame, disable=True)


        # some GUI events that have to do with the objects list
        wx.EVT_BUTTON(self.slice3dVWR.objectAnimationFrame,
                      self.slice3dVWR.objectAnimationFrame.resetButtonId,
                      self._handlerObjectAnimationReset)

        wx.EVT_SLIDER(self.slice3dVWR.objectAnimationFrame,
                      self.slice3dVWR.objectAnimationFrame.frameSliderId,
                      self._handlerObjectAnimationSlider)
        

        # this will fix up wxTheColourDatabase
        colourdb.updateColourDB()

    def close(self):
        self._tdObjectsDict.clear()
        self.slice3dVWR = None
        self._grid.ClearGrid()
        self._grid = None

    def addObject(self, tdObject):
        """Takes care of all the administration of adding a new 3-d object
        to the list and updating it in the list control.
        """

        if tdObject not in self._tdObjectsDict:

            # we get to pick a colour and a name
            colourName = self._objectColours[
                self._objectId % len(self._objectColours)]
            # objectName HAS TO BE unique!
            objectName = "%s%d" % ('obj', self._objectId)
            self._objectId += 1

            # create a colour as well
            colour = wx.TheColourDatabase.FindColour(colourName).asTuple()
            # normalise
            nColour = tuple([c / 255.0 for c in colour])

            # we'll need this later (when we decide about scalar vis)
            scalarsName = None
            
            # now actually create the necessary thingies and add the object
            # to the scene
            if hasattr(tdObject, 'GetClassName') and \
               callable(tdObject.GetClassName):

                if tdObject.GetClassName() == 'vtkVolume':
                    self.slice3dVWR._threedRenderer.AddVolume(tdObject)
                    self._tdObjectsDict[tdObject] = {'tdObject' : tdObject,
                                                     'type' : 'vtkVolume'}

                    # we have to test this here already... if there's
                    # a VTK error (for instance volume rendering the wrong
                    # type) we remove the volume and re-raise the exception
                    # the calling code will act as if no successful add
                    # was performed, thus feeding back the error
                    try:
                        self.slice3dVWR._threedRenderer.ResetCamera()
                        self.slice3dVWR.render3D()
                    except RuntimeError:
                        self.slice3dVWR._threedRenderer.RemoveVolume(tdObject)
                        raise

                elif tdObject.GetClassName() == 'vtkPolyData':
                    mapper = vtk.vtkPolyDataMapper()
                    mapper.ImmediateModeRenderingOn()
                    mapper.SetInput(tdObject)
                    actor = vtk.vtkActor()
                    actor.SetMapper(mapper)

                    #actor.GetProperty().LoadMaterial('GLSLTwisted')
                    #actor.GetProperty().ShadingOn()
                    #actor.GetProperty().AddShaderVariableFloat('Rate', 1.0)

                    # now we are going to set the PERFECT material
                    p = actor.GetProperty()

                    # i prefer flat shading, okay?
                    p.SetInterpolationToFlat()

                    # Ka, background lighting coefficient
                    p.SetAmbient(0.1)
                    # light reflectance
                    p.SetDiffuse(0.6)
                    # the higher Ks, the more intense the highlights
                    p.SetSpecular(0.4)
                    # the higher the power, the more localised the
                    # highlights
                    p.SetSpecularPower(100)

                    self.slice3dVWR._threedRenderer.AddActor(actor)
                    self._tdObjectsDict[tdObject] = {'tdObject' : tdObject,
                                                     'type' : 'vtkPolyData',
                                                     'vtkActor' : actor}

                    # to get the name of the scalars we need to do this.
                    tdObject.Update()
                
                    if tdObject.GetPointData().GetScalars():
                        scalarsName = tdObject.GetPointData().GetScalars().\
                                      GetName()
                    else:
                        scalarsName = None

                    if scalarsName:
                        mapper.SetScalarRange(tdObject.GetScalarRange())

                    #mapper.ScalarVisibilityOff()
                
#                     if sname and sname.lower().find("curvature") >= 0:
#                         # if the active scalars have "curvature" somewhere in
#                         # their name, activate flat shading and scalar vis
#                         property = actor.GetProperty()
{'fraction_non_alphanumeric': 0.0750490516677567, 'fraction_numerical': 0.008911052975801177, 'mean_word_length': 1.5496040016673613, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3622979', 'n_tokens_mistral': 3276, 'n_tokens_neox': 3059, 'n_words': 850}