Mentors4EDU commited on
Commit
b6a0fc3
·
verified ·
1 Parent(s): f585f9c

Upload 6 files

Browse files
Geometry/BsplineMitchellNetravali.cs ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ using System;
2
+
3
+ namespace FastPrint.Geometry
4
+ {
5
+ public static class BsplineMitchellNetravali
6
+ {
7
+ // Cubic B-spline basis function
8
+ public static double BSpline(double t)
9
+ {
10
+ t = Math.Abs(t);
11
+ if (t < 1)
12
+ return (3 * t * t * t - 6 * t * t + 4) / 6.0;
13
+ else if (t < 2)
14
+ return (-t * t * t + 6 * t * t - 12 * t + 8) / 6.0;
15
+ return 0.0;
16
+ }
17
+
18
+ // Mitchell-Netravali filter
19
+ public static double MitchellNetravali(double x, double B = 1.0 / 3.0, double C = 1.0 / 3.0)
20
+ {
21
+ x = Math.Abs(x);
22
+ if (x < 1)
23
+ return ((12 - 9 * B - 6 * C) * Math.Pow(x, 3) +
24
+ (-18 + 12 * B + 6 * C) * Math.Pow(x, 2) +
25
+ (6 - 2 * B)) / 6.0;
26
+ else if (x < 2)
27
+ return ((-B - 6 * C) * Math.Pow(x, 3) +
28
+ (6 * B + 30 * C) * Math.Pow(x, 2) +
29
+ (-12 * B - 48 * C) * x +
30
+ (8 * B + 24 * C)) / 6.0;
31
+ return 0.0;
32
+ }
33
+ }
34
+ }
Model/STLModel.cs ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FastPrint/
2
+
3
+ ├── Geometry/
4
+ │ └── BsplineMitchellNetravali.cs
5
+
6
+ ├── Slicing/
7
+ │ └── SliceAccelerator.cs
8
+
9
+ ├── Printer/
10
+ │ └── MarlinConnector.cs
11
+
12
+ ├── Model/
13
+ │ └── STLModel.cs
14
+
15
+ ├── UI/
16
+ │ ├── MainWindow.xaml
17
+ │ └── MainWindow.xaml.cs
18
+
19
+ └── README.md
20
+
21
+ using System.Collections.Generic;
22
+ using System.IO;
23
+ using System.Globalization;
24
+
25
+ namespace FastPrint.Model
26
+ {
27
+ public class STLModel
28
+ {
29
+ public List<float[]> Triangles { get; } = new List<float[]>();
30
+
31
+ public void LoadAscii(string path)
32
+ {
33
+ using var reader = new StreamReader(path);
34
+ string line;
35
+ var currentTriangle = new List<float>();
36
+ while ((line = reader.ReadLine()) != null)
37
+ {
38
+ if (line.Trim().StartsWith("vertex"))
39
+ {
40
+ var parts = line.Trim().Split(' ');
41
+ for (int i = 1; i < 4; i++)
42
+ currentTriangle.Add(float.Parse(parts[i], CultureInfo.InvariantCulture));
43
+ }
44
+ if (currentTriangle.Count == 9)
45
+ {
46
+ Triangles.Add(currentTriangle.ToArray());
47
+ currentTriangle.Clear();
48
+ }
49
+ }
50
+ }
51
+ }
52
+ }
Printer/MarlinConnector.cs ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ using System.IO.Ports;
2
+ using System.Threading.Tasks;
3
+
4
+ namespace FastPrint.Printer
5
+ {
6
+ public class MarlinConnector
7
+ {
8
+ private SerialPort port;
9
+
10
+ public MarlinConnector(string portName, int baudRate = 115200)
11
+ {
12
+ port = new SerialPort(portName, baudRate);
13
+ }
14
+
15
+ public async Task ConnectAsync()
16
+ {
17
+ if (!port.IsOpen)
18
+ port.Open();
19
+ await SendCommandAsync("M115"); // Get firmware info
20
+ }
21
+
22
+ public async Task SendCommandAsync(string gcode)
23
+ {
24
+ if (port.IsOpen)
25
+ await port.BaseStream.WriteAsync(System.Text.Encoding.ASCII.GetBytes(gcode + "\n"));
26
+ }
27
+
28
+ public void Disconnect()
29
+ {
30
+ if (port.IsOpen)
31
+ port.Close();
32
+ }
33
+ }
34
+ }
Slicing/SliceAccelerator.cs ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ using ILGPU;
2
+ using ILGPU.Runtime;
3
+ using System;
4
+
5
+ namespace FastPrint.Slicing
6
+ {
7
+ public class SliceAccelerator : IDisposable
8
+ {
9
+ private Context context;
10
+ private Accelerator accelerator;
11
+
12
+ public SliceAccelerator()
13
+ {
14
+ context = Context.CreateDefault();
15
+ accelerator = context.GetPreferredDevice(preferCPU: false).CreateAccelerator(context);
16
+ }
17
+
18
+ // Example kernel for slicing
19
+ public void Slice(float[] vertices, float layerHeight, Action<float[]> onSliced)
20
+ {
21
+ using var buffer = accelerator.Allocate1D(vertices);
22
+ accelerator.Synchronize();
23
+ // Placeholder: actual slicing logic should be implemented here
24
+ onSliced(vertices);
25
+ }
26
+
27
+ public void Dispose()
28
+ {
29
+ accelerator.Dispose();
30
+ context.Dispose();
31
+ }
32
+ }
33
+ }
UI/MainWindow.cs ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ using System.Windows;
2
+ using Microsoft.Win32;
3
+ using FastPrint.Printer;
4
+ using FastPrint.Slicing;
5
+ using FastPrint.Model;
6
+
7
+ namespace FastPrint.UI
8
+ {
9
+ public partial class MainWindow : Window
10
+ {
11
+ private MarlinConnector printer;
12
+ private SliceAccelerator slicer;
13
+ private STLModel model;
14
+
15
+ public MainWindow()
16
+ {
17
+ InitializeComponent();
18
+ slicer = new SliceAccelerator();
19
+ model = new STLModel();
20
+ }
21
+
22
+ private void OpenSTL_Click(object sender, RoutedEventArgs e)
23
+ {
24
+ var dlg = new OpenFileDialog { Filter = "STL Files (*.stl)|*.stl" };
25
+ if (dlg.ShowDialog() == true)
26
+ {
27
+ model.LoadAscii(dlg.FileName);
28
+ // Visualization logic can be added here
29
+ }
30
+ }
31
+
32
+ private void Slice_Click(object sender, RoutedEventArgs e)
33
+ {
34
+ // Flatten triangles to vertex array
35
+ var vertices = new System.Collections.Generic.List<float>();
36
+ foreach (var tri in model.Triangles)
37
+ vertices.AddRange(tri);
38
+
39
+ slicer.Slice(vertices.ToArray(), 0.2f, result => {
40
+ // Handle sliced result (e.g., display, export G-code)
41
+ });
42
+ }
43
+
44
+ private async void Connect_Click(object sender, RoutedEventArgs e)
45
+ {
46
+ printer = new MarlinConnector(PortSelector.SelectedItem?.ToString() ?? "COM3");
47
+ await printer.ConnectAsync();
48
+ }
49
+ }
50
+ }
UI/MainWindow.xaml ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <Window x:Class="FastPrint.UI.MainWindow"
2
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4
+ Title="FastPrint 3D Slicer" Height="600" Width="900">
5
+ <DockPanel>
6
+ <ToolBar DockPanel.Dock="Top">
7
+ <Button Content="Open STL" Click="OpenSTL_Click"/>
8
+ <Button Content="Slice" Click="Slice_Click"/>
9
+ <ComboBox x:Name="PortSelector" Width="120"/>
10
+ <Button Content="Connect" Click="Connect_Click"/>
11
+ </ToolBar>
12
+ <Grid>
13
+ <Viewport3D x:Name="viewport"/>
14
+ </Grid>
15
+ </DockPanel>
16
+ </Window>