<commit_before>#!/usr/bin/env python
"""
.. module:: pulley-system
   :synopsis: The pulley-system function.

.. moduleauthor:: <NAME>

"""

import math
import numbers
import numpy

import belt
import curveFunctions
import functions
import geometry
import interpreter as interp
import lcadExceptions
import lcadTypes

lcad_functions = {}


class LCadPulleySystem(functions.LCadFunction):
    """
    **pulley-system** - Creates a pulley-system function.
    
    This function creates and returns a function that parametrizes a 
    pulley-system making it easier to add pulleys and string to a MOC.
    The function is very similar to belt(), except that the first
    element specifies a drum on which the string is wound and the
    last specifies either the end point of the string, or the direction
    that string goes after the last sprocket. All units are LDU.

    A pulley-system must have at least two elements (including the initial
    drum and the final end point). Like belt, the pulleys are specified by 
    a 4 element list consisting of *(position orientation radius winding-direction)*.

    :param position: A 3 element list specifying the location of the pulley.
    :param orientation: A 3 element list specifying the vector perpendicular to the plane of the pulley.
    :param radius: The radius of the pulley in LDU.
    :param winding-direction: Which way the string goes around the pulley (1 = counter-clockwise, -1 = clockwise).
 
    The string goes around the pulleys in the order in which they are specified.

    The initial drum is specified by the 7 element list *(position
    orientation radius winding-direction drum-width string-thickness
    string-length)*.

    :param position: A 3 element list specifying the location of the drum.
    :param orientation: A 3 element list specifying the vector perpendicular to the plane of the drum.
    :param radius: The radius of the drum in LDU.
    :param winding-direction: Which way the string goes around the drum (1 = counter-clockwise, -1 = clockwise).
    :param drum-width: The width of the drum in LDU.
    :param string-thickness: The diameter of the string.
    :param string-length: The amount of string wound around the drum, **not** the total string length.

    The final end point is specified by a 2 element position list,
    *(position/direction type)*.

    :param position/direction: A 3 element list specifying the end point of the pulley system, or a tangent vector.
    :param type: This is either the string "point" or "tangent".

    When you call the created pulley-system function you will get a 4 x 4 
    transform matrix which will translate to the requested position on the 
    string and orient to a coordinate system where the z-axis is pointing 
    along the string, the y-axis is in the plane of the string and the 
    x-axis is perpendicular to the plane of the string, pointing in the 
    direction of the pulley perpendicular vector.

    If you call the created pulley-system function with the argument 
    **t** it will return the length of the string.

    Usage::

     ; Drum at 0,0,0 rotating counter-clockwise in the x-y plane with radius
     ; 5, width 50 and 50 LDU of 1 LDU diameter string. The string then goes
     ; around a pulley 20,0,1 rotating counter-clockwise in the x-y plane
     ; with radius 5. After passing the pulley the string continues in the
     ; -x direction.
     (def ps1 (pulley-system (list (list (list 0 0 0) (list 0 0 1) 5.0 1 5.0 1 50)
                                   (list (list 20 0 1) (list 0 0 1) 5.0 1)
                                   (list (list -1 0 0) "tangent"))))

     ; Drum at 0,0,0 rotating counter-clockwise in the x-y plane with radius
     ; 5, width 50 and 50 LDU of 1 LDU diameter string. The string then passes 
     ; through the point 20,0,0.
     (def ps2 (pulley-system (list (list (list 0 0 0) (list 0 0 1) 5.0 1 5.0 1 50)
                                   (list (list 20 0 0) "point"))))

     (def m (ps1 1))   ; m is a 4 x 4 transform matrix.
     (ps1 t)           ; Returns the length of pulley system ps1 including the 
                       ;  string on the drum.
     (ps2 t)           ; Returns the length of pulley system ps2 including the
                       ;  string on the drum.

    """
    def __init__(self):
        functions.LCadFunction.__init__(self, "pulley-system")
        self.setSignature([[list]])

    def call(self, model, pulley_list):
        if (len(pulley_list) < 2):
            raise NumberPulleysException(len(pulley_list))

        # Create pulley-system.
        p_system = belt.Belt(False)

        # Starting drum.
        drum = pulley_list[0]
        pulley_list = pulley_list[1:]

        if not isinstance(drum, list):
            raise lcadExceptions.WrongTypeException("list", type(drum))

        if (len(drum) != 7):
            DrumException(len(drum))

        drum_pos = geometry.parseArgs(drum[0])
        drum_zvec = geometry.parseArgs(drum[1])
        for arg in drum[2:]:
            if not isinstance(arg, numbers.Number):
                raise lcadExceptions.WrongTypeException("number", type(arg))

        # Ending point.
        end_point = pulley_list[-1]
        pulley_list = pulley_list[:-1]

        if not isinstance(end_point, list):
            raise lcadExceptions.WrongTypeException("list", type(end_point))

        if (len(end_point) != 2):
            EndPointException(len(end_point))

        end_vec = geometry.parseArgs(end_point[0])
        end_type = end_point[1]
        if not isinstance(end_type, basestring):
            raise lcadExceptions.WrongTypeException("string", type(end_type))

        if not (end_type in ["point", "tangent"]):
            raise EndPointTypeException(end_type)

        # Drum only pulley system.
        if (len(pulley_list) == 0):
            p_system.addSprocket(EndDrum(drum_pos, drum_zvec, drum[2], drum[3], drum[4], drum[5], drum[6], end_vec, end_type))

        # "Standard" pulley system.
        else:
            p_system.addSprocket(StdDrum(drum_pos, drum_zvec, drum[2], drum[3], drum[4], drum[5], drum[6]))<commit_msg>Correct to raise and not just create the exceptions.<commit_after>#!/usr/bin/env python
"""
.. module:: pulley-system
   :synopsis: The pulley-system function.

.. moduleauthor:: <NAME>

"""

import math
import numbers
import numpy

import belt
import curveFunctions
import functions
import geometry
import interpreter as interp
import lcadExceptions
import lcadTypes

lcad_functions = {}


class LCadPulleySystem(functions.LCadFunction):
    """
    **pulley-system** - Creates a pulley-system function.
    
    This function creates and returns a function that parametrizes a 
    pulley-system making it easier to add pulleys and string to a MOC.
    The function is very similar to belt(), except that the first
    element specifies a drum on which the string is wound and the
    last specifies either the end point of the string, or the direction
    that string goes after the last sprocket. All units are LDU.

    A pulley-system must have at least two elements (including the initial
    drum and the final end point). Like belt, the pulleys are specified by 
    a 4 element list consisting of *(position orientation radius winding-direction)*.

    :param position: A 3 element list specifying the location of the pulley.
    :param orientation: A 3 element list specifying the vector perpendicular to the plane of the pulley.
    :param radius: The radius of the pulley in LDU.
    :param winding-direction: Which way the string goes around the pulley (1 = counter-clockwise, -1 = clockwise).
 
    The string goes around the pulleys in the order in which they are specified.

    The initial drum is specified by the 7 element list *(position
    orientation radius winding-direction drum-width string-thickness
    string-length)*.

    :param position: A 3 element list specifying the location of the drum.
    :param orientation: A 3 element list specifying the vector perpendicular to the plane of the drum.
    :param radius: The radius of the drum in LDU.
    :param winding-direction: Which way the string goes around the drum (1 = counter-clockwise, -1 = clockwise).
    :param drum-width: The width of the drum in LDU.
    :param string-thickness: The diameter of the string.
    :param string-length: The amount of string wound around the drum, **not** the total string length.

    The final end point is specified by a 2 element position list,
    *(position/direction type)*.

    :param position/direction: A 3 element list specifying the end point of the pulley system, or a tangent vector.
    :param type: This is either the string "point" or "tangent".

    When you call the created pulley-system function you will get a 4 x 4 
    transform matrix which will translate to the requested position on the 
    string and orient to a coordinate system where the z-axis is pointing 
    along the string, the y-axis is in the plane of the string and the 
    x-axis is perpendicular to the plane of the string, pointing in the 
    direction of the pulley perpendicular vector.

    If you call the created pulley-system function with the argument 
    **t** it will return the length of the string.

    Usage::

     ; Drum at 0,0,0 rotating counter-clockwise in the x-y plane with radius
     ; 5, width 50 and 50 LDU of 1 LDU diameter string. The string then goes
     ; around a pulley 20,0,1 rotating counter-clockwise in the x-y plane
     ; with radius 5. After passing the pulley the string continues in the
     ; -x direction.
     (def ps1 (pulley-system (list (list (list 0 0 0) (list 0 0 1) 5.0 1 5.0 1 50)
                                   (list (list 20 0 1) (list 0 0 1) 5.0 1)
                                   (list (list -1 0 0) "tangent"))))

     ; Drum at 0,0,0 rotating counter-clockwise in the x-y plane with radius
     ; 5, width 50 and 50 LDU of 1 LDU diameter string. The string then passes 
     ; through the point 20,0,0.
     (def ps2 (pulley-system (list (list (list 0 0 0) (list 0 0 1) 5.0 1 5.0 1 50)
                                   (list (list 20 0 0) "point"))))

     (def m (ps1 1))   ; m is a 4 x 4 transform matrix.
     (ps1 t)           ; Returns the length of pulley system ps1 including the 
                       ;  string on the drum.
     (ps2 t)           ; Returns the length of pulley system ps2 including the
                       ;  string on the drum.

    """
    def __init__(self):
        functions.LCadFunction.__init__(self, "pulley-system")
        self.setSignature([[list]])

    def call(self, model, pulley_list):
        if (len(pulley_list) < 2):
            raise NumberPulleysException(len(pulley_list))

        # Create pulley-system.
        p_system = belt.Belt(False)

        # Starting drum.
        drum = pulley_list[0]
        pulley_list = pulley_list[1:]

        if not isinstance(drum, list):
            raise lcadExceptions.WrongTypeException("list", type(drum))

        if (len(drum) != 7):
            raise DrumException(len(drum))

        drum_pos = geometry.parseArgs(drum[0])
        drum_zvec = geometry.parseArgs(drum[1])
        for arg in drum[2:]:
            if not isinstance(arg, numbers.Number):
                raise lcadExceptions.WrongTypeException("number", type(arg))

        # Ending point.
        end_point = pulley_list[-1]
        pulley_list = pulley_list[:-1]

        if not isinstance(end_point, list):
            raise lcadExceptions.WrongTypeException("list", type(end_point))

        if (len(end_point) != 2):
            raise EndPointException(len(end_point))

        end_vec = geometry.parseArgs(end_point[0])
        end_type = end_point[1]
        if not isinstance(end_type, basestring):
            raise lcadExceptions.WrongTypeException("string", type(end_type))

        if not (end_type in ["point", "tangent"]):
            raise EndPointTypeException(end_type)

        # Drum only pulley system.
        if (len(pulley_list) == 0):
            p_system.addSprocket(EndDrum(drum_pos, drum_zvec, drum[2], drum[3], drum[4], drum[5], drum[6], end_vec, end_type))

        # "Standard" pulley system.
        else:
            p_system.addSprocket(StdDrum(drum_pos, drum_zvec, drum[2], drum[3], drum[4], drum[5], drum[6]))
{'fraction_non_alphanumeric': 0.07985407377381434, 'fraction_numerical': 0.019294689906769354, 'mean_word_length': 2.645390070921986, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7235226', 'n_tokens_mistral': 3754, 'n_tokens_neox': 3451, 'n_words': 1471}