<commit_before>#!/usr/bin/env python
"""
@brief A base class for constructing shapes
"""
import os
import vtk
import numpy
# We need the following to handle expressions received from callers.
from numpy import linspace
from math import sin, cos, tan, log, exp, pi, asin, acos, atan, atan2, e
from csg.geom import Vector, Vertex, Polygon, BSPNode
from csg.core import CSG
from icqShape import Box, Cone, Cylinder, Sphere
from icqShape import DEFAULTS, CompositeShape
from icqsol.color.icqColorMap import ColorMap


class ShapeManager(object):

    def createShape(self, type, origin=None, lengths=None, radius=None,
                    angle=None, n_theta=None, n_phi=None):
        origin = DEFAULTS['origin']
        lengths = DEFAULTS['lengths']
        radius = DEFAULTS['radius']
        angle = DEFAULTS['angle']
        n_theta = DEFAULTS['n_theta']
        n_phi = DEFAULTS['n_phi']
        if type == 'box':
            return Box(origin, lengths)
        if type == 'cone':
            return Cone(radius, origin, lengths, n_theta)
        if type == 'cylinder':
            return Cylinder(radius, origin, lengths, n_theta)
        if type == 'sphere':
            return Sphere(radius, origin, n_theta, n_phi)
        return None

    def addSurfaceFieldFromExpression(self, shape, field_name, expression, time_points):
        """
        Add a surface field to a shape using an expression consisting of
        legal variables x,y,z (shape point coordinates) and t (time).
        @param shape
        @param field_name, the name of the surface field
        @param expression, expression consisting of legal variables x, y, z, and t
        @param time_points, list of floating point values defining
               snapshots in a time sequence
        @return pdata, VTKPolyData converted from shape with added surface field
        """
        # This class incorporates features from 2 primary classes: CSG
        # and vtkPolyData.  It uses CSG to assemble shapes which have no
        # knowledge of fields.  VtkPolyData objects are grids with fields
        # typically attached to them, but not always.  So when we attach
        # a surface field, we can no longer use CSG objects, and so this
        # method returns just the data.  The save() method will accept
        # either a shape or a VtkPolyData object, so all is well.
        valid_field_name = field_name.replace( ' ', '_' )
        # Get the points from the shape.
        pdata = self.shapeToVTKPolyData(shape)
        points = pdata.GetPoints()
        num_points = points.GetNumberOfPoints()
        # Define the data.
        data = vtk.vtkDoubleArray()
        data.SetName(valid_field_name)
        # Handle time points.
        num_time_points = len(time_points)
        # Update the data.
        data.SetNumberOfComponents(num_time_points)
        data.SetNumberOfTuples(num_points)
        # Add the surface field.
        for i in range(num_points):
            x, y, z = points.GetPoint(i)
            for j in range(num_time_points):
                t = time_points[ j ]
                field_value = eval(expression)
                data.SetComponent(i, j, field_value)
        pdata.GetPointData().SetScalars(data)
        return pdata

    def colorSurfaceField(self, vtk_poly_data, color_map, field_name=None):
        """
        Color a selected surface field of a shape.
        @param vtk_poly_data, data defining the shape with surface field information
        @param field_name, the name of the surface field to color
        @param color_map, name of the color map to use
        @return colored_vtk_poly_data, color applied to vtk_poly_data
        """
        # Copy the received vtk_poly_data, creating another
        # vtkPolyData object with the same points and cells.
        vtk_poly_data_copy = vtk.vtkPolyData()
        vtk_poly_data_copy.SetPoints(vtk_poly_data.GetPoints())
        vtk_poly_data_copy.SetPolys(vtk_poly_data.GetPolys())
        # Get information from the data.
        pointData = vtk_poly_data.GetPointData()
        numComps = pointData.GetNumberOfComponents()
        numPoints = vtk_poly_data.GetPoints().GetNumberOfPoints()
        numArrays = pointData.GetNumberOfArrays()
        assert field_name is not None or numArrays == 1, "Field name must be specified since data has %d arrays" % numArrays
        # Get the min/max field values.
        array = pointData.GetScalars(args.name)
        fmin, fmax = array.GetRange()
        # Prepare for coloring the points.
        redArray = vtk.vtkUnsignedCharArray()
        greenArray = vtk.vtkUnsignedCharArray()
        blueArray = vtk.vtkUnsignedCharArray()
        redArray.SetNumberOfComponents(numComps)
        greenArray.SetNumberOfComponents(numComps)
        blueArray.SetNumberOfComponents(numComps)
        redArray.SetNumberOfTuples(numPoints)
        greenArray.SetNumberOfTuples(numPoints)
        blueArray.SetNumberOfTuples(numPoints)
        rs = numpy.zeros((numComps,), numpy.uint8)
        gs = numpy.zeros((numComps,), numpy.uint8)
        bs = numpy.zeros((numComps,), numpy.uint8)
        # Build the color map.
        colorMap = ColorMap(fmin, fmax)
        colorMethod = eval('colormap.' + color_map)
        # Color the points.<commit_msg>Fix paste error ShapeManager.colorSurfaceField() method.<commit_after>#!/usr/bin/env python
"""
@brief A base class for constructing shapes
"""
import os
import vtk
import numpy
# We need the following to handle expressions received from callers.
from numpy import linspace
from math import sin, cos, tan, log, exp, pi, asin, acos, atan, atan2, e
from csg.geom import Vector, Vertex, Polygon, BSPNode
from csg.core import CSG
from icqShape import Box, Cone, Cylinder, Sphere
from icqShape import DEFAULTS, CompositeShape
from icqsol.color.icqColorMap import ColorMap


class ShapeManager(object):

    def createShape(self, type, origin=None, lengths=None, radius=None,
                    angle=None, n_theta=None, n_phi=None):
        origin = DEFAULTS['origin']
        lengths = DEFAULTS['lengths']
        radius = DEFAULTS['radius']
        angle = DEFAULTS['angle']
        n_theta = DEFAULTS['n_theta']
        n_phi = DEFAULTS['n_phi']
        if type == 'box':
            return Box(origin, lengths)
        if type == 'cone':
            return Cone(radius, origin, lengths, n_theta)
        if type == 'cylinder':
            return Cylinder(radius, origin, lengths, n_theta)
        if type == 'sphere':
            return Sphere(radius, origin, n_theta, n_phi)
        return None

    def addSurfaceFieldFromExpression(self, shape, field_name, expression, time_points):
        """
        Add a surface field to a shape using an expression consisting of
        legal variables x,y,z (shape point coordinates) and t (time).
        @param shape
        @param field_name, the name of the surface field
        @param expression, expression consisting of legal variables x, y, z, and t
        @param time_points, list of floating point values defining
               snapshots in a time sequence
        @return pdata, VTKPolyData converted from shape with added surface field
        """
        # This class incorporates features from 2 primary classes: CSG
        # and vtkPolyData.  It uses CSG to assemble shapes which have no
        # knowledge of fields.  VtkPolyData objects are grids with fields
        # typically attached to them, but not always.  So when we attach
        # a surface field, we can no longer use CSG objects, and so this
        # method returns just the data.  The save() method will accept
        # either a shape or a VtkPolyData object, so all is well.
        valid_field_name = field_name.replace( ' ', '_' )
        # Get the points from the shape.
        pdata = self.shapeToVTKPolyData(shape)
        points = pdata.GetPoints()
        num_points = points.GetNumberOfPoints()
        # Define the data.
        data = vtk.vtkDoubleArray()
        data.SetName(valid_field_name)
        # Handle time points.
        num_time_points = len(time_points)
        # Update the data.
        data.SetNumberOfComponents(num_time_points)
        data.SetNumberOfTuples(num_points)
        # Add the surface field.
        for i in range(num_points):
            x, y, z = points.GetPoint(i)
            for j in range(num_time_points):
                t = time_points[ j ]
                field_value = eval(expression)
                data.SetComponent(i, j, field_value)
        pdata.GetPointData().SetScalars(data)
        return pdata

    def colorSurfaceField(self, vtk_poly_data, color_map, field_name=None):
        """
        Color a selected surface field of a shape.
        @param vtk_poly_data, data defining the shape with surface field information
        @param field_name, the name of the surface field to color
        @param color_map, name of the color map to use
        @return colored_vtk_poly_data, color applied to vtk_poly_data
        """
        # Copy the received vtk_poly_data, creating another
        # vtkPolyData object with the same points and cells.
        vtk_poly_data_copy = vtk.vtkPolyData()
        vtk_poly_data_copy.SetPoints(vtk_poly_data.GetPoints())
        vtk_poly_data_copy.SetPolys(vtk_poly_data.GetPolys())
        # Get information from the data.
        pointData = vtk_poly_data.GetPointData()
        numComps = pointData.GetNumberOfComponents()
        numPoints = vtk_poly_data.GetPoints().GetNumberOfPoints()
        numArrays = pointData.GetNumberOfArrays()
        assert field_name is not None or numArrays == 1, "Field name must be specified since data has %d arrays" % numArrays
        # Get the min/max field values.
        array = pointData.GetScalars(field_name)
        fmin, fmax = array.GetRange()
        # Prepare for coloring the points.
        redArray = vtk.vtkUnsignedCharArray()
        greenArray = vtk.vtkUnsignedCharArray()
        blueArray = vtk.vtkUnsignedCharArray()
        redArray.SetNumberOfComponents(numComps)
        greenArray.SetNumberOfComponents(numComps)
        blueArray.SetNumberOfComponents(numComps)
        redArray.SetNumberOfTuples(numPoints)
        greenArray.SetNumberOfTuples(numPoints)
        blueArray.SetNumberOfTuples(numPoints)
        rs = numpy.zeros((numComps,), numpy.uint8)
        gs = numpy.zeros((numComps,), numpy.uint8)
        bs = numpy.zeros((numComps,), numpy.uint8)
        # Build the color map.
        colorMap = ColorMap(fmin, fmax)
        colorMethod = eval('colormap.' + color_map)
        # Color the points.
{'fraction_non_alphanumeric': 0.07773149024805907, 'fraction_numerical': 0.0011361484567316796, 'mean_word_length': 2.6550173010380624, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2455756', 'n_tokens_mistral': 3037, 'n_tokens_neox': 3011, 'n_words': 953}