Skip to content

Instantly share code, notes, and snippets.

@lukicdarkoo
Last active September 2, 2021 07:02
Show Gist options
  • Select an option

  • Save lukicdarkoo/7925a3eee3b83c9b04f6aac1b45bb1d7 to your computer and use it in GitHub Desktop.

Select an option

Save lukicdarkoo/7925a3eee3b83c9b04f6aac1b45bb1d7 to your computer and use it in GitHub Desktop.

Revisions

  1. lukicdarkoo revised this gist Sep 2, 2021. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion convert.py
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@
    # sudo apt install xclip
    #
    # GNOME SHORTCUT
    # In GNOME, you can set to run this script as a keyboard shortcut command.
    # In GNOME, you can run the script as a keyboard shortcut.
    # Go to `Settings > Keyboard Shortcuts > +`

    import clipboard
    @@ -38,6 +38,8 @@ def vector_to_string_array(vector, decimals, zero_one_decimals=None):
    new_str.append('0')
    elif abs(value - 1) < 1 / (10**zero_one_decimals):
    new_str.append('1')
    elif abs(value + 1) < 1 / (10**zero_one_decimals):
    new_str.append('-1')
    else:
    new_str.append(str(round(value, decimals)))
    return new_str
  2. lukicdarkoo revised this gist Sep 2, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion convert.py
    Original file line number Diff line number Diff line change
    @@ -21,7 +21,7 @@
    TRANSLATE_VECTOR = [-2, 0, 0]

    ROTATION = [np.pi/2, -np.pi/2, 0] # RUB
    ROTATION = [np.pi/2, np.pi/2, 0] # RUB with x-inverted
    # ROTATION = [np.pi/2, np.pi/2, 0] # RUB with x-inverted
    # ROTATION = [np.pi/2, 0, 0]
    if GEOMETRY:
    ROTATION = [-np.pi/2, 0, 0] # Plane for sure
  3. lukicdarkoo revised this gist Sep 2, 2021. 1 changed file with 8 additions and 2 deletions.
    10 changes: 8 additions & 2 deletions convert.py
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,11 @@
    # Converts rotation and translation VRML fields.
    # It takes the clipboard content, applies transforms, and pastes the transformed VRML to the clipboard.
    #
    # Dependencies:
    #
    # pip3 install clipboard numpy transforms3d
    # sudo apt install xclip
    #
    # GNOME SHORTCUT
    # In GNOME, you can set to run this script as a keyboard shortcut command.
    # Go to `Settings > Keyboard Shortcuts > +`
    @@ -16,9 +21,10 @@
    TRANSLATE_VECTOR = [-2, 0, 0]

    ROTATION = [np.pi/2, -np.pi/2, 0] # RUB
    # ROTATION = [0, 0, np.pi]
    ROTATION = [np.pi/2, np.pi/2, 0] # RUB with x-inverted
    # ROTATION = [np.pi/2, 0, 0]
    if GEOMETRY:
    ROTATION = [np.pi/2, 0, 0]
    ROTATION = [-np.pi/2, 0, 0] # Plane for sure
    ROTATION_MATRIX = transforms3d.euler.euler2mat(ROTATION[0], ROTATION[1], ROTATION[2], 'rxyz')


  4. lukicdarkoo revised this gist Sep 1, 2021. 1 changed file with 14 additions and 16 deletions.
    30 changes: 14 additions & 16 deletions convert.py
    Original file line number Diff line number Diff line change
    @@ -11,23 +11,16 @@
    import re


    # ROTATION = [0.577351, -0.577351, 0.577351, 2.09439]
    # ROTATION = [0.577351, -0.577351, -0.577351, -2.09439]
    ROTATION = [-0.5773516025189619, 0.5773476025217157, 0.5773516025189619, -2.094405307179586] # From RUB to FLU
    ROTATION = [0, -0.707, 0.707, 3.140466] # Plane, from RUB to FLU
    # ROTATION = [1, 0, 0, -np.pi/2] # Plane, from RUB to FLU

    # ROTATION = [0, 1, 0, -np.pi/2] # Cylinder after conversion from RUB

    ROTATION_MATRIX = transforms3d.axangles.axangle2mat(ROTATION[:3], ROTATION[3])


    ROTATION = [-np.pi/2, np.pi, 0]
    GEOMETRY = False
    TRANSLATE_ONLY = False
    TRANSLATE_VECTOR = [-2, 0, 0]

    ROTATION = [np.pi/2, -np.pi/2, 0] # RUB
    # ROTATION = [0, 0, np.pi]
    if GEOMETRY:
    ROTATION = [np.pi/2, 0, 0]
    ROTATION_MATRIX = transforms3d.euler.euler2mat(ROTATION[0], ROTATION[1], ROTATION[2], 'rxyz')

    ROTATION_MATRIX = transforms3d.axangles.axangle2mat(
    [-0.5773516025189619, 0.5773476025217157, 0.5773516025189619], -2.094405307179586)


    def vector_to_string_array(vector, decimals, zero_one_decimals=None):
    if zero_one_decimals is None:
    @@ -46,6 +39,8 @@ def vector_to_string_array(vector, decimals, zero_one_decimals=None):

    def convert_translation(translation):
    translation = [float(value) for value in translation]
    if TRANSLATE_ONLY:
    return (np.array(translation) + TRANSLATE_VECTOR).flatten()
    return (ROTATION_MATRIX @ np.array(translation)).flatten()


    @@ -59,7 +54,10 @@ def convert_mesh(geometry_points):
    def convert_orientation(rotation_angle_axis):
    rotation_angle_axis = [float(value) for value in rotation_angle_axis]
    orientation = transforms3d.axangles.axangle2mat(rotation_angle_axis[:3], rotation_angle_axis[3])
    new_rotation = ROTATION_MATRIX @ orientation
    if GEOMETRY:
    new_rotation = orientation @ ROTATION_MATRIX
    else:
    new_rotation = ROTATION_MATRIX @ orientation
    new_rotation_axis, new_rotation_angle = transforms3d.axangles.mat2axangle(new_rotation)
    return ' '.join(vector_to_string_array(list(new_rotation_axis) + list([new_rotation_angle]), 6))

  5. lukicdarkoo revised this gist Aug 24, 2021. 1 changed file with 72 additions and 38 deletions.
    110 changes: 72 additions & 38 deletions convert.py
    Original file line number Diff line number Diff line change
    @@ -8,53 +8,87 @@
    import clipboard
    import numpy as np
    import transforms3d
    import re


    EPSILON = 1e-4
    # ROTATION = [0.577351, -0.577351, 0.577351, 2.09439]
    # ROTATION = [0.577351, -0.577351, -0.577351, -2.09439]
    ROTATION = [-0.5773516025189619, 0.5773476025217157, 0.5773516025189619, -2.094405307179586] # From RUB to FLU
    ROTATION = [0, -0.707, 0.707, 3.140466] # Plane, from RUB to FLU
    # ROTATION = [1, 0, 0, -np.pi/2] # Plane, from RUB to FLU

    # ROTATION = [0, 1, 0, -np.pi/2] # Cylinder after conversion from RUB

    ROTATION = [1, 0, 0, np.pi/2]
    ROTATION_MATRIX = transforms3d.axangles.axangle2mat(ROTATION[:3], ROTATION[3])


    def main():
    text = clipboard.paste()
    ROTATION = [-np.pi/2, np.pi, 0]
    ROTATION_MATRIX = transforms3d.euler.euler2mat(ROTATION[0], ROTATION[1], ROTATION[2], 'rxyz')

    ROTATION_MATRIX = transforms3d.axangles.axangle2mat(
    [-0.5773516025189619, 0.5773476025217157, 0.5773516025189619], -2.094405307179586)

    lines = text.split('\n')
    lines_out = []
    for index, line in enumerate(lines):
    args = line.strip().split(' ')
    identation = ' ' * (len(line) - len(line.lstrip()))

    if args[0] == 'rotation':
    rotation_raw = [float(x) for x in args[1:]]
    rotation_matrix = transforms3d.axangles.axangle2mat(rotation_raw[:3], rotation_raw[3])
    rotated_matrix = ROTATION_MATRIX @ rotation_matrix
    axis, angle = transforms3d.axangles.mat2axangle(rotated_matrix)

    for i in range(3):
    if abs(axis[i]) < EPSILON:
    axis[i] = int(0)
    if abs(angle) < EPSILON:
    angle = int(0)

    lines_out.append(f'{identation}rotation {axis[0]} {axis[1]} {axis[2]} {angle}')

    elif args[0] == 'translation':
    translation = np.array([float(x) for x in args[1:]])
    tranlsation_rotated = ROTATION_MATRIX @ translation

    for i in range(3):
    if abs(tranlsation_rotated[i]) < EPSILON:
    tranlsation_rotated[i] = int(0)

    lines_out.append(
    f'{identation}translation {tranlsation_rotated[0]} {tranlsation_rotated[1]} {tranlsation_rotated[2]}')
    if 'rotation' not in lines[index + 1]:
    lines_out.append(f'{identation}rotation {ROTATION[0]} {ROTATION[1]} {ROTATION[2]} {ROTATION[3]}')

    def vector_to_string_array(vector, decimals, zero_one_decimals=None):
    if zero_one_decimals is None:
    # Zero and one are special cases and typically it is fine to be more agressive when rounding
    zero_one_decimals = int(0.7 * decimals)
    new_str = []
    for value in vector:
    if abs(value) < 1 / (10**zero_one_decimals):
    new_str.append('0')
    elif abs(value - 1) < 1 / (10**zero_one_decimals):
    new_str.append('1')
    else:
    lines_out.append(line)
    new_str.append(str(round(value, decimals)))
    return new_str


    def convert_translation(translation):
    translation = [float(value) for value in translation]
    return (ROTATION_MATRIX @ np.array(translation)).flatten()


    clipboard.copy('\n'.join(lines_out))
    def convert_mesh(geometry_points):
    for i in range(0, len(geometry_points), 3):
    new_point = convert_translation(geometry_points[i:i+3])
    geometry_points[i:i+3] = vector_to_string_array(new_point, 7)
    return ' '.join(geometry_points)


    def convert_orientation(rotation_angle_axis):
    rotation_angle_axis = [float(value) for value in rotation_angle_axis]
    orientation = transforms3d.axangles.axangle2mat(rotation_angle_axis[:3], rotation_angle_axis[3])
    new_rotation = ROTATION_MATRIX @ orientation
    new_rotation_axis, new_rotation_angle = transforms3d.axangles.mat2axangle(new_rotation)
    return ' '.join(vector_to_string_array(list(new_rotation_axis) + list([new_rotation_angle]), 6))


    def is_number(string):
    try:
    float(string)
    return True
    except ValueError:
    return False


    def main():
    text = clipboard.paste()
    vector = [x for x in re.compile('\n|,| ').split(text) if is_number(x)]
    out = 'ERROR'

    if len(vector) == 0:
    new_rotation_axis, new_rotation_angle = transforms3d.axangles.mat2axangle(ROTATION_MATRIX)
    out = ' '.join(vector_to_string_array(list(new_rotation_axis) + list([new_rotation_angle]), 6))
    elif len(vector) == 3:
    out = convert_translation(vector)
    out = ' '.join(vector_to_string_array(out, 4))
    elif len(vector) == 4:
    out = convert_orientation(vector)
    else:
    out = convert_mesh(vector)

    clipboard.copy(out)


    if __name__ == '__main__':
  6. lukicdarkoo revised this gist Aug 9, 2021. 1 changed file with 5 additions and 3 deletions.
    8 changes: 5 additions & 3 deletions convert.py
    Original file line number Diff line number Diff line change
    @@ -10,6 +10,8 @@
    import transforms3d


    EPSILON = 1e-4

    ROTATION = [1, 0, 0, np.pi/2]
    ROTATION_MATRIX = transforms3d.axangles.axangle2mat(ROTATION[:3], ROTATION[3])

    @@ -30,9 +32,9 @@ def main():
    axis, angle = transforms3d.axangles.mat2axangle(rotated_matrix)

    for i in range(3):
    if abs(axis[i]) < 0.0001:
    if abs(axis[i]) < EPSILON:
    axis[i] = int(0)
    if abs(angle) < 0.0001:
    if abs(angle) < EPSILON:
    angle = int(0)

    lines_out.append(f'{identation}rotation {axis[0]} {axis[1]} {axis[2]} {angle}')
    @@ -42,7 +44,7 @@ def main():
    tranlsation_rotated = ROTATION_MATRIX @ translation

    for i in range(3):
    if abs(tranlsation_rotated[i]) < 0.0001:
    if abs(tranlsation_rotated[i]) < EPSILON:
    tranlsation_rotated[i] = int(0)

    lines_out.append(
  7. lukicdarkoo revised this gist Aug 9, 2021. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions convert.py
    Original file line number Diff line number Diff line change
    @@ -10,8 +10,6 @@
    import transforms3d


    # ROTATION = [0.577351, -0.577351, 0.577351, 2.09439]
    # ROTATION = [0.577351, -0.577351, -0.577351, -2.09439]
    ROTATION = [1, 0, 0, np.pi/2]
    ROTATION_MATRIX = transforms3d.axangles.axangle2mat(ROTATION[:3], ROTATION[3])

  8. lukicdarkoo renamed this gist Aug 9, 2021. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  9. lukicdarkoo created this gist Aug 9, 2021.
    61 changes: 61 additions & 0 deletions conver.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,61 @@
    # Converts rotation and translation VRML fields.
    # It takes the clipboard content, applies transforms, and pastes the transformed VRML to the clipboard.
    #
    # GNOME SHORTCUT
    # In GNOME, you can set to run this script as a keyboard shortcut command.
    # Go to `Settings > Keyboard Shortcuts > +`

    import clipboard
    import numpy as np
    import transforms3d


    # ROTATION = [0.577351, -0.577351, 0.577351, 2.09439]
    # ROTATION = [0.577351, -0.577351, -0.577351, -2.09439]
    ROTATION = [1, 0, 0, np.pi/2]
    ROTATION_MATRIX = transforms3d.axangles.axangle2mat(ROTATION[:3], ROTATION[3])


    def main():
    text = clipboard.paste()

    lines = text.split('\n')
    lines_out = []
    for index, line in enumerate(lines):
    args = line.strip().split(' ')
    identation = ' ' * (len(line) - len(line.lstrip()))

    if args[0] == 'rotation':
    rotation_raw = [float(x) for x in args[1:]]
    rotation_matrix = transforms3d.axangles.axangle2mat(rotation_raw[:3], rotation_raw[3])
    rotated_matrix = ROTATION_MATRIX @ rotation_matrix
    axis, angle = transforms3d.axangles.mat2axangle(rotated_matrix)

    for i in range(3):
    if abs(axis[i]) < 0.0001:
    axis[i] = int(0)
    if abs(angle) < 0.0001:
    angle = int(0)

    lines_out.append(f'{identation}rotation {axis[0]} {axis[1]} {axis[2]} {angle}')

    elif args[0] == 'translation':
    translation = np.array([float(x) for x in args[1:]])
    tranlsation_rotated = ROTATION_MATRIX @ translation

    for i in range(3):
    if abs(tranlsation_rotated[i]) < 0.0001:
    tranlsation_rotated[i] = int(0)

    lines_out.append(
    f'{identation}translation {tranlsation_rotated[0]} {tranlsation_rotated[1]} {tranlsation_rotated[2]}')
    if 'rotation' not in lines[index + 1]:
    lines_out.append(f'{identation}rotation {ROTATION[0]} {ROTATION[1]} {ROTATION[2]} {ROTATION[3]}')
    else:
    lines_out.append(line)

    clipboard.copy('\n'.join(lines_out))


    if __name__ == '__main__':
    main()