-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_component_sample.rs
More file actions
194 lines (162 loc) · 5.95 KB
/
Copy pathcreate_component_sample.rs
File metadata and controls
194 lines (162 loc) · 5.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
//! Create a sample 3MF file with component hierarchy for testing the slicer
//!
//! This example creates a 3MF file with:
//! - A base cube object (10x10x10mm)
//! - A sphere object (radius 3mm)
//! - A component object that references the sphere with transforms
//! - A build item that references the component object with additional transform
use lib3mf::{BuildItem, Component, Mesh, Model, Object, Triangle, Vertex};
use std::f64::consts::PI;
fn create_cube_mesh(size: f64) -> Mesh {
let half = size / 2.0;
let vertices = vec![
// Bottom face
Vertex::new(-half, -half, -half),
Vertex::new(half, -half, -half),
Vertex::new(half, half, -half),
Vertex::new(-half, half, -half),
// Top face
Vertex::new(-half, -half, half),
Vertex::new(half, -half, half),
Vertex::new(half, half, half),
Vertex::new(-half, half, half),
];
let triangles = vec![
// Bottom face (z = -half, normal pointing down -Z)
Triangle::new(0, 2, 1),
Triangle::new(0, 3, 2),
// Top face (z = +half, normal pointing up +Z)
Triangle::new(4, 5, 6),
Triangle::new(4, 6, 7),
// Front face (y = -half, normal pointing -Y)
Triangle::new(0, 1, 5),
Triangle::new(0, 5, 4),
// Back face (y = +half, normal pointing +Y)
Triangle::new(2, 3, 7),
Triangle::new(2, 7, 6),
// Left face (x = -half, normal pointing -X)
Triangle::new(0, 4, 7),
Triangle::new(0, 7, 3),
// Right face (x = +half, normal pointing +X)
Triangle::new(1, 2, 6),
Triangle::new(1, 6, 5),
];
Mesh {
vertices,
triangles,
beamset: None,
}
}
fn create_sphere_mesh(radius: f64, segments: usize, rings: usize) -> Mesh {
let mut vertices = Vec::new();
let mut triangles = Vec::new();
// Top vertex
vertices.push(Vertex::new(0.0, 0.0, radius));
// Generate vertices for each ring
for ring in 1..rings {
let phi = PI * (ring as f64) / (rings as f64);
let sin_phi = phi.sin();
let cos_phi = phi.cos();
for seg in 0..segments {
let theta = 2.0 * PI * (seg as f64) / (segments as f64);
let sin_theta = theta.sin();
let cos_theta = theta.cos();
let x = radius * sin_phi * cos_theta;
let y = radius * sin_phi * sin_theta;
let z = radius * cos_phi;
vertices.push(Vertex::new(x, y, z));
}
}
// Bottom vertex
vertices.push(Vertex::new(0.0, 0.0, -radius));
// Top cap triangles
for seg in 0..segments {
let next = (seg + 1) % segments;
triangles.push(Triangle::new(0, seg + 1, next + 1));
}
// Middle triangles
for ring in 0..(rings - 2) {
let ring_start = 1 + ring * segments;
let next_ring_start = ring_start + segments;
for seg in 0..segments {
let next = (seg + 1) % segments;
let v0 = ring_start + seg;
let v1 = next_ring_start + seg;
let v2 = next_ring_start + next;
let v3 = ring_start + next;
triangles.push(Triangle::new(v0, v1, v2));
triangles.push(Triangle::new(v0, v2, v3));
}
}
// Bottom cap triangles
let last_ring_start = 1 + (rings - 2) * segments;
let bottom_vertex = vertices.len() - 1;
for seg in 0..segments {
let next = (seg + 1) % segments;
triangles.push(Triangle::new(
last_ring_start + seg,
bottom_vertex,
last_ring_start + next,
));
}
Mesh {
vertices,
triangles,
beamset: None,
}
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut model = Model::new();
// Object 1: Base cube (10x10x10mm)
let mut cube_obj = Object::new(1);
cube_obj.name = Some("Base Cube".to_string());
cube_obj.mesh = Some(create_cube_mesh(10.0));
// Object 2: Sphere (radius 3mm)
let mut sphere_obj = Object::new(2);
sphere_obj.name = Some("Sphere".to_string());
sphere_obj.mesh = Some(create_sphere_mesh(3.0, 16, 12));
// Object 3: Component assembly - contains 4 spheres positioned at cube corners
let mut component_obj = Object::new(3);
component_obj.name = Some("Sphere Assembly".to_string());
// Add 4 components (spheres at different positions)
let positions = vec![
(5.0, 5.0, 5.0), // Top-right-front
(-5.0, 5.0, 5.0), // Top-left-front
(5.0, -5.0, 5.0), // Top-right-back
(-5.0, -5.0, 5.0), // Top-left-back
];
for (x, y, z) in positions {
// Create transform: identity matrix with translation
let transform = [
1.0, 0.0, 0.0, // Row 0
0.0, 1.0, 0.0, // Row 1
0.0, 0.0, 1.0, // Row 2
x, y, z, // Translation
];
component_obj
.components
.push(Component::with_transform(2, transform));
}
// Add objects to model
model.resources.objects.push(cube_obj);
model.resources.objects.push(sphere_obj);
model.resources.objects.push(component_obj);
// Add build item - references the component object with a transform that moves it up
let build_transform = [
1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0,
20.0, // Move entire assembly up by 20mm
];
let mut build_item = BuildItem::new(3);
build_item.transform = Some(build_transform);
model.build.items.push(build_item);
// Write to file
let output_path = "tools/slicer/samples/components/components.3mf";
model.write_to_file(output_path)?;
println!("Created component sample 3MF file: {}", output_path);
println!(" - 1 cube object (10x10x10mm)");
println!(" - 1 sphere object (radius 3mm)");
println!(" - 1 component object with 4 sphere instances");
println!(" - Build item with transform (moved up 20mm)");
println!(" - Final Z range: approximately 17mm to 29mm");
Ok(())
}