# How to convert the python code to Julia

**URL:** https://discourse.julialang.org/t/how-to-convert-the-python-code-to-julia/123689
**Category:** Performance
**Tags:** question, package, makie
**Created:** [December 11, 2024, 6:45am UTC](https://discourse.julialang.org/t/how-to-convert-the-python-code-to-julia/123689 "2024-12-11T06:45:04Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![govindanupam](https://avatars.discourse-cdn.com/v4/letter/g/ac8455/32.png) [@govindanupam](https://discourse.julialang.org/u/govindanupam)
#### Post date: [December 11, 2024, 6:45am UTC](https://discourse.julialang.org/t/how-to-convert-the-python-code-to-julia/123689/1 "2024-12-11T06:45:04Z")

</div>

Dear team,

How to convert below mentioned code to Julia only without using Pycall module or any Python modules. Please help

```julia
import os
import vtk
import numpy as np

def process_jaw_stl(input_file, output_file, jaw_type, plane_normal, translation_vector, middle_z_offset=5, ver=10, hor=10, xas=10, top_margin=0.1, bottom_margin=0.1):
    reader = vtk.vtkSTLReader()
    reader.SetFileName(input_file)
    reader.Update()
    mesh = reader.GetOutput()

    transform = vtk.vtkTransform()
    transform.RotateX(90)  
    transform.Translate(xas, ver, hor) 

    transform_filter = vtk.vtkTransformPolyDataFilter()
    transform_filter.SetTransform(transform)
    transform_filter.SetInputData(mesh)
    transform_filter.Update()
    transformed_mesh = transform_filter.GetOutput()

    if jaw_type == 'upper':
        slice_plane = vtk.vtkPlane()
        slice_plane.SetOrigin(transformed_mesh.GetCenter())
        slice_plane.SetNormal(plane_normal)

        slicer = vtk.vtkClipPolyData()
        slicer.SetInputData(transformed_mesh)
        slicer.SetClipFunction(slice_plane)
        slicer.InsideOutOn()  
        slicer.Update()
        sliced_mesh = slicer.GetOutput()

        middle_clipper = vtk.vtkClipPolyData()
        middle_plane = vtk.vtkPlane()
        middle_plane.SetOrigin(sliced_mesh.GetCenter()[0], sliced_mesh.GetCenter()[1], sliced_mesh.GetCenter()[2] + middle_z_offset)
        middle_plane.SetNormal(0, 0, -1)

        middle_clipper.SetInputData(sliced_mesh)
        middle_clipper.SetClipFunction(middle_plane)
        middle_clipper.Update()
        middle_removed_mesh = middle_clipper.GetOutput()

        hole_filler = vtk.vtkFillHolesFilter()
        hole_filler.SetInputData(middle_removed_mesh)
        hole_filler.SetHoleSize(50000)  
        hole_filler.Update()
        filled_mesh = hole_filler.GetOutput()

    elif jaw_type == 'lower':
        bounds = transformed_mesh.GetBounds()
        centroid = [(bounds[0] + bounds[1]) / 2, (bounds[2] + bounds[3]) / 2, (bounds[4] + bounds[5]) / 2]

        clipper_bottom = vtk.vtkClipPolyData()
        clipper_bottom.SetInputData(transformed_mesh)
        plane_bottom = vtk.vtkPlane()
        plane_bottom.SetOrigin(0, 0, bounds[4] + bottom_margin)
        plane_bottom.SetNormal(0, 0, 1)
        clipper_bottom.SetClipFunction(plane_bottom)
        clipper_bottom.Update()
        clipped_mesh_bottom = clipper_bottom.GetOutput()

        clipper_top = vtk.vtkClipPolyData()
        clipper_top.SetInputData(clipped_mesh_bottom)
        plane_top = vtk.vtkPlane()
        plane_top.SetOrigin(0, 0, bounds[5] - top_margin)
        plane_top.SetNormal(0, 0, -1)
        clipper_top.SetClipFunction(plane_top)
        clipper_top.Update()
        final_clipped_mesh = clipper_top.GetOutput()

        middle_clipper = vtk.vtkClipPolyData()
        middle_plane = vtk.vtkPlane()
        middle_plane.SetOrigin(centroid[0], centroid[1], centroid[2] + middle_z_offset / 2)
        middle_plane.SetNormal(0, 0, 1)

        middle_clipper.SetInputData(final_clipped_mesh)
        middle_clipper.SetClipFunction(middle_plane)
        middle_clipper.InsideOutOff()
        middle_clipper.Update()
        middle_removed_mesh = middle_clipper.GetOutput()

        hole_filler = vtk.vtkFillHolesFilter()
        hole_filler.SetInputData(middle_removed_mesh)
        hole_filler.SetHoleSize(1000.0)
        hole_filler.Update()
        filled_mesh = hole_filler.GetOutput()

    normals = vtk.vtkPolyDataNormals()
    normals.SetInputData(filled_mesh)
    normals.SetFeatureAngle(30.0)
    normals.SplittingOff()
    normals.Update()
    final_mesh = normals.GetOutput()

    writer = vtk.vtkSTLWriter()
    writer.SetFileName(output_file)
    writer.SetInputData(final_mesh)
    writer.Write()
    print(f"Final STL saved to {output_file}")

def automate_jaw_processing(directory):
    stl_files = [f for f in os.listdir(directory) if f.endswith('.stl')]
    
    if len(stl_files) == 2:
        for stl_file in stl_files:
            input_path = os.path.join(directory, stl_file)
            if 'UpperJaw' in stl_file:
                output_path = os.path.join(directory, 'UpperJaw_Cleaned_Filled.stl')
                process_jaw_stl(
                    input_file=input_path,
                    output_file=output_path,
                    jaw_type='upper',
                    plane_normal=[0, 0, 1],
                    translation_vector=[0, 0, 10],
                    middle_z_offset=5,
                    ver=10,
                    hor=6,
                    xas=10
                )
            elif 'LowerJaw' in stl_file:
                output_path = os.path.join(directory, 'LowerJaw_Cleaned_Filled.stl')
                process_jaw_stl(
                    input_file=input_path,
                    output_file=output_path,
                    jaw_type='lower',
                    plane_normal=[0, 0, 1],
                    translation_vector=[0, 0, 25],
                    top_margin=0.13,
                    bottom_margin=0.13
                )
    else:
        print("Please ensure there are exactly two STL files in the directory, named appropriately with 'UpperJaw' and 'LowerJaw'.")

directory_path = r'C:\Users\OMG\Desktop\Dental STL-TEST'
automate_jaw_processing(directory_path)

```

Attached is the sample screenshot after importing Original files to Blender software

 ![image](https://global.discourse-cdn.com/julialang/original/3X/d/1/d14404a875af0d4daa8d3ea4d0a4a1dc29b206d2.jpeg)

Thanks and regards

---

<div class="post-metadata">

### Author: ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)
#### Post date: [December 11, 2024, 7:52am UTC](https://discourse.julialang.org/t/how-to-convert-the-python-code-to-julia/123689/2 "2024-12-11T07:52:36Z")

</div>

What’s the practical intention for deviating from an officially supported wrapper language for an optimized C++ library?

---

<div class="post-metadata">

### Author: ![govindanupam](https://avatars.discourse-cdn.com/v4/letter/g/ac8455/32.png) [@govindanupam](https://discourse.julialang.org/u/govindanupam)
#### Post date: [December 11, 2024, 8:32am UTC](https://discourse.julialang.org/t/how-to-convert-the-python-code-to-julia/123689/3 "2024-12-11T08:32:31Z")

</div>

My main requirements are as follows:

1. There are 2 stl files inside the path: ‘C:\Users\OMG\Desktop\Dental STL-TEST’

a. File: 17253853412024-09-03\_001\_TNS-FABIAN\_BARAJA-LowerJaw.stl  
b. File: 17253853422024-09-03\_001\_TNS-FABIAN\_BARAJA-UpperJaw.stl

first stl file contains lower Jaw teeth model and second stl file contains upper Jaw teeth model

1. Using Julia code, open any stl file either Lower or Upper jaw teeth

a. rotate the 3d object from horizontal to vertical stage  
b. slice or cut the lower part (If used Lower Jaw stl file)  
below is attached screenshot:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/2/5/25a0659a1914f5e9c6a58f33d7a044f55dda1cc3.jpeg)

c. slice or cut the Upper part (If used Upper Jaw stl file)  
below is attached screenshot:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/5/d/5d843ea03118fb6da31ac331434a0fb8cdac18d1.jpeg)

1. Once slicing is done, for Lower Jaw, It should look like attached (This one is working perfectly with python code):

 ![image](https://global.discourse-cdn.com/julialang/original/3X/1/c/1ccae735a404e181626074dba02bf0cf8187e9a7.png)  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/9/7/9765364466d0cf08421310ec3d976762d30a0328.jpeg)  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/4/7/478a6bb5065ebb6eb5429165d404f195112d70fe.jpeg)  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/e/7/e7d90f66423b19b35810b58d2e0008123bec264c.png)

1. Once slicing is done, for Upper Jaw, It should look like attached (This one is working perfectly with python code):

 ![image](https://global.discourse-cdn.com/julialang/original/3X/2/7/27c18d9db26c5fcf4fb8e36f825a5ebf315b6c64.png)  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/5/0/50b78117706848b27940f9fe0f685ff0ce84e3c2.png)  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/8/8/88a33da2b353b7151d3dd318615b0f079214dd1e.png)  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/1/1/110de3864be5ff6d665a878e6a767b929130819c.png)

Please note:  
Same thing i wanted to do with Julia too and not using Pycall package

Please help  
thanks and regards

---

<div class="post-metadata">

### Author: ![lazarusA](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lazarusa/32/6571_2.png) [@lazarusA](https://discourse.julialang.org/u/lazarusA)
#### Post date: [December 11, 2024, 8:48am UTC](https://discourse.julialang.org/t/how-to-convert-the-python-code-to-julia/123689/4 "2024-12-11T08:48:15Z")

</div>

you could use Makie to load those files, see [mesh | Makie](https://docs.makie.org/stable/reference/plots/mesh#examples) (the brain example).

note: for more formats see [GitHub - JuliaIO/MeshIO.jl: IO for Meshes](https://github.com/JuliaIO/MeshIO.jl)

- cutting and slicing is another story :D.

---

<div class="post-metadata">

### Author: ![govindanupam](https://avatars.discourse-cdn.com/v4/letter/g/ac8455/32.png) [@govindanupam](https://discourse.julialang.org/u/govindanupam)
#### Post date: [December 15, 2024, 4:55am UTC](https://discourse.julialang.org/t/how-to-convert-the-python-code-to-julia/123689/5 "2024-12-15T04:55:32Z")

</div>

Thanks,  
I tried using below code for rotating the object which is working fine.

```julia
using FileIO
using GeometryBasics

rotate_x(point::GeometryBasics.Point{3, Float32}, angle_deg::Float64) = GeometryBasics.Point3f(
    point[1],
    cos(deg2rad(angle_deg)) * point[2] - sin(deg2rad(angle_deg)) * point[3],
    sin(deg2rad(angle_deg)) * point[2] + cos(deg2rad(angle_deg)) * point[3]
)

function rotate_stl(input_file::String, output_file::String, rotation_angle_x::Float64)
    println("Processing STL file: $input_file")
    mesh = load(input_file)
    rotated_vertices = [rotate_x(v.position, rotation_angle_x) for v in GeometryBasics.coordinates(mesh)]
    save(output_file, GeometryBasics.Mesh(rotated_vertices, GeometryBasics.faces(mesh)))
    println("Rotated STL saved to: $output_file")
end

rotate_stl(
    "LowerJaw.stl",
    "rotated_lower_jaw.stl",
    90.0
)

```

Below is the output of rotated\_lower\_jaw.stl:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/1/0/1088d008008b1f25866dfa1c0b63103b2ed416bd.jpeg)

Please help with the slice / remove the bottom area of the 3d object Lower jaw teeth inside the stl file.

thanks and regards
