mesh-tools 0.4.0

A Rust library for generating 3D meshes and exporting them to glTF/GLB files
Documentation
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
//! # Primitive Shape Generation
//!
//! This module provides functions for generating the geometry data (vertices, indices, normals, and UVs)
//! for basic 3D primitive shapes such as planes, boxes, spheres, cylinders, and more.
//!
//! Each generation function follows the same pattern of returning a tuple with the following components:
//! - `positions`: Vertex coordinates as a vector of Point3<f32>
//! - `indices`: Triangle indices using the Triangle struct
//! - `normals`: Normal vectors as a vector of Vector3<f32>
//! - `uvs`: Texture coordinates as a vector of Vector2<f32>
//!
//! These mesh components can be directly used with the `GltfBuilder` to create complete 3D meshes.
//!
//! ## Example
//!
//! ```rust
//! use mesh_tools::primitives::generate_sphere;
//! use mesh_tools::compat::{Point3, Vector3, Vector2};
//! 
//! // Generate a sphere with radius 1.0
//! let (positions, indices, normals, uvs) = generate_sphere(1.0, 16, 8);
//! ```

use std::f32::consts::PI;
use crate::compat::{Point3, Vector2, Vector3};
use crate::builder_primitives::Triangle;

/// Generate a plane (flat surface) with subdivisions
/// 
/// # Parameters
/// * `width` - Width of the plane along X axis
/// * `depth` - Depth of the plane along Z axis 
/// * `width_segments` - Number of subdivisions along width
/// * `depth_segments` - Number of subdivisions along depth
/// 
/// # Returns
/// Tuple of (positions, indices, normals, uvs)
pub fn generate_plane(
    width: f32,
    depth: f32,
    width_segments: usize,
    depth_segments: usize,
) -> (Vec<Point3<f32>>, Vec<Triangle>, Vec<Vector3<f32>>, Vec<Vector2<f32>>) {
    let width_half = width / 2.0;
    let depth_half = depth / 2.0;
    
    let grid_x = width_segments;
    let grid_z = depth_segments;
    
    // Segment dimensions not actually used directly in this implementation
    // but kept for clarity and possible future enhancements
    let _segment_width = width / grid_x as f32;
    let _segment_depth = depth / grid_z as f32;
    
    let mut positions = Vec::new();
    let mut normals = Vec::new();
    let mut uvs = Vec::new();
    let mut indices = Vec::new();
    
    // Generate vertices, normals and uvs
    for z in 0..=grid_z {
        let z_percent = z as f32 / grid_z as f32;
        
        for x in 0..=grid_x {
            let x_percent = x as f32 / grid_x as f32;
            
            let x_pos = x_percent * width - width_half;
            let z_pos = z_percent * depth - depth_half;
            
            // Position
            positions.push(crate::compat::point3::new(x_pos, 0.0, z_pos));
            
            // Normal
            normals.push(crate::compat::vector3::new(0.0, 1.0, 0.0));
            
            // UV
            uvs.push(crate::compat::vector2::new(x_percent, 1.0 - z_percent));  // Flip Y for texture coordinates
        }
    }
    
    // Generate indices
    let vertices_per_row = grid_x + 1;
    
    for z in 0..grid_z {
        for x in 0..grid_x {
            let a = (z * vertices_per_row + x) as u32;
            let b = (z * vertices_per_row + x + 1) as u32;
            let c = ((z + 1) * vertices_per_row + x + 1) as u32;
            let d = ((z + 1) * vertices_per_row + x) as u32;
            
            // Two triangles per grid cell
            indices.push(Triangle::new(a, b, d));
            indices.push(Triangle::new(b, c, d));
        }
    }
    
    (positions, indices, normals, uvs)
}

/// Generate a sphere with subdivisions
/// 
/// # Parameters
/// * `radius` - Radius of the sphere
/// * `width_segments` - Number of horizontal subdivisions
/// * `height_segments` - Number of vertical subdivisions
/// 
/// # Returns
/// Tuple of (positions, indices, normals, uvs)
pub fn generate_sphere(
    radius: f32,
    width_segments: usize,
    height_segments: usize,
) -> (Vec<Point3<f32>>, Vec<Triangle>, Vec<Vector3<f32>>, Vec<Vector2<f32>>) {
    let width_segments = width_segments.max(3);
    let height_segments = height_segments.max(2);
    
    let mut positions = Vec::new();
    let mut normals = Vec::new();
    let mut uvs = Vec::new();
    let mut indices = Vec::new();
    
    // Generate vertices, normals and uvs
    for y in 0..=height_segments {
        let v = y as f32 / height_segments as f32;
        let phi = v * PI;
        
        for x in 0..=width_segments {
            let u = x as f32 / width_segments as f32;
            let theta = u * 2.0 * PI;
            
            // Calculate vertex position on the sphere
            let x_pos = -radius * theta.sin() * phi.sin();
            let y_pos = radius * phi.cos();
            let z_pos = radius * theta.cos() * phi.sin();
            
            // Position
            positions.push(crate::compat::point3::new(x_pos, y_pos, z_pos));
            
            // Normal (normalized position)
            let length = (x_pos * x_pos + y_pos * y_pos + z_pos * z_pos).sqrt();
            normals.push(crate::compat::vector3::new(x_pos / length, y_pos / length, z_pos / length));
            
            // UV
            uvs.push(crate::compat::vector2::new(u, 1.0 - v));  // Flip V for texture coordinates
        }
    }
    
    // Generate indices
    let vertices_per_row = width_segments + 1;
    
    for y in 0..height_segments {
        for x in 0..width_segments {
            let a = (y * vertices_per_row + x) as u32;
            let b = (y * vertices_per_row + x + 1) as u32;
            let c = ((y + 1) * vertices_per_row + x + 1) as u32;
            let d = ((y + 1) * vertices_per_row + x) as u32;
            
            // Two triangles per grid cell
            // Except at the poles where we have single triangles
            if y != 0 {
                indices.push(Triangle::new(a, b, d));
            }
            
            if y != height_segments - 1 {
                indices.push(Triangle::new(b, c, d));
            }
        }
    }
    
    (positions, indices, normals, uvs)
}

/// Generate a cylinder
/// 
/// # Parameters
/// * `radius_top` - Radius at the top of the cylinder
/// * `radius_bottom` - Radius at the bottom of the cylinder
/// * `height` - Height of the cylinder
/// * `radial_segments` - Number of subdivisions around the circumference
/// * `height_segments` - Number of subdivisions along the height
/// * `open_ended` - Whether to include top and bottom caps
/// 
/// # Returns
/// Tuple of (positions, indices, normals, uvs)
pub fn generate_cylinder(
    radius_top: f32,
    radius_bottom: f32,
    height: f32,
    radial_segments: usize,
    height_segments: usize,
    open_ended: bool,
) -> (Vec<Point3<f32>>, Vec<Triangle>, Vec<Vector3<f32>>, Vec<Vector2<f32>>) {
    let radial_segments = radial_segments.max(3);
    let height_segments = height_segments.max(1);
    
    let mut positions = Vec::new();
    let mut normals = Vec::new();
    let mut uvs = Vec::new();
    let mut indices = Vec::new();
    
    // Helper function to calculate slope normals
    let get_slope_normal = |_radius: f32, slope_factor: f32, u: f32| -> (f32, f32, f32) {
        let sin_theta = (u * 2.0 * PI).sin();
        let cos_theta = (u * 2.0 * PI).cos();
        
        let nx = cos_theta;
        let ny = slope_factor;
        let nz = sin_theta;
        
        // Normalize
        let length = (nx * nx + ny * ny + nz * nz).sqrt();
        
        (nx / length, ny / length, nz / length)
    };
    
    // Calculate slope factor for cylinder sides
    let slope_factor = if radius_top == radius_bottom {
        0.0 // No slope for a perfect cylinder
    } else {
        height / (radius_bottom - radius_top)
    };
    
    // Generate vertices for the curved surface
    for y in 0..=height_segments {
        let v = y as f32 / height_segments as f32;
        let y_pos = v * height - height / 2.0;
        let radius = radius_bottom + v * (radius_top - radius_bottom);
        
        for x in 0..=radial_segments {
            let u = x as f32 / radial_segments as f32;
            let theta = u * 2.0 * PI;
            
            let sin_theta = theta.sin();
            let cos_theta = theta.cos();
            
            // Position
            positions.push(crate::compat::point3::new(radius * cos_theta, y_pos, radius * sin_theta));
            
            // Normal
            let (nx, ny, nz) = get_slope_normal(radius, slope_factor, u);
            normals.push(crate::compat::vector3::new(nx, ny, nz));
            
            // UV
            uvs.push(crate::compat::vector2::new(u, 1.0 - v));
        }
    }
    
    // Generate indices for the curved surface
    let vertices_per_row = radial_segments + 1;
    
    for y in 0..height_segments {
        for x in 0..radial_segments {
            let a = (y * vertices_per_row + x) as u32;
            let b = (y * vertices_per_row + x + 1) as u32;
            let c = ((y + 1) * vertices_per_row + x + 1) as u32;
            let d = ((y + 1) * vertices_per_row + x) as u32;
            
            // Two triangles per grid cell
            indices.push(Triangle::new(a, b, d));
            indices.push(Triangle::new(b, c, d));
        }
    }
    
    // If not open ended, add top and bottom caps
    if !open_ended {
        let start_index = positions.len() / 3;
        
        // Top cap
        // Center vertex
        positions.push(crate::compat::point3::new(0.0, height / 2.0, 0.0));
        
        normals.push(crate::compat::vector3::new(0.0, 1.0, 0.0));
        
        uvs.push(crate::compat::vector2::new(0.5, 0.5));
        
        // Cap vertices
        for x in 0..=radial_segments {
            let u = x as f32 / radial_segments as f32;
            let theta = u * 2.0 * PI;
            
            let cos_theta = theta.cos();
            let sin_theta = theta.sin();
            
            // Position
            positions.push(crate::compat::point3::new(radius_top * cos_theta, height / 2.0, radius_top * sin_theta));
            
            // Normal
            normals.push(crate::compat::vector3::new(0.0, 1.0, 0.0));
            
            // UV
            uvs.push(crate::compat::vector2::new(cos_theta * 0.5 + 0.5, sin_theta * 0.5 + 0.5));
        }
        
        // Top cap indices
        let center_index = start_index as u32;
        
        for x in 0..radial_segments {
            indices.push(Triangle::new(
                center_index,
                center_index + (x + 1) as u32,
                center_index + (x + 2) as u32
            ));
        }
        
        // Bottom cap
        let start_index = positions.len() / 3;
        
        // Center vertex
        positions.push(crate::compat::point3::new(0.0, -height / 2.0, 0.0));
        
        normals.push(crate::compat::vector3::new(0.0, -1.0, 0.0));
        
        uvs.push(crate::compat::vector2::new(0.5, 0.5));
        
        // Cap vertices
        for x in 0..=radial_segments {
            let u = x as f32 / radial_segments as f32;
            let theta = u * 2.0 * PI;
            
            let cos_theta = theta.cos();
            let sin_theta = theta.sin();
            
            // Position
            positions.push(crate::compat::point3::new(radius_bottom * cos_theta, -height / 2.0, radius_bottom * sin_theta));
            
            // Normal
            normals.push(crate::compat::vector3::new(0.0, -1.0, 0.0));
            
            // UV
            uvs.push(crate::compat::vector2::new(cos_theta * 0.5 + 0.5, sin_theta * 0.5 + 0.5));
        }
        
        // Bottom cap indices
        let center_index = start_index as u32;
        
        for x in 0..radial_segments {
            indices.push(Triangle::new(
                center_index,
                center_index + (x + 2) as u32,
                center_index + (x + 1) as u32
            ));
        }
    }
    
    (positions, indices, normals, uvs)
}

/// Generate a cone (special case of cylinder)
/// 
/// # Parameters
/// * `radius` - Radius at the base of the cone
/// * `height` - Height of the cone
/// * `radial_segments` - Number of subdivisions around the circumference
/// * `height_segments` - Number of subdivisions along the height
/// * `open_ended` - Whether to include the base cap
/// 
/// # Returns
/// Tuple of (positions, indices, normals, uvs)
pub fn generate_cone(
    radius: f32,
    height: f32,
    radial_segments: usize,
    height_segments: usize,
    open_ended: bool,
) -> (Vec<Point3<f32>>, Vec<Triangle>, Vec<Vector3<f32>>, Vec<Vector2<f32>>) {
    generate_cylinder(0.0, radius, height, radial_segments, height_segments, open_ended)
}

/// Generate a torus (donut shape)
/// 
/// # Parameters
/// * `radius` - Distance from the center of the tube to the center of the torus
/// * `tube` - Radius of the tube
/// * `radial_segments` - Number of subdivisions around the main circle
/// * `tubular_segments` - Number of subdivisions around the tube
/// 
/// # Returns
/// Tuple of (positions, indices, normals, uvs)
pub fn generate_torus(
    radius: f32,
    tube: f32,
    radial_segments: usize,
    tubular_segments: usize,
) -> (Vec<Point3<f32>>, Vec<Triangle>, Vec<Vector3<f32>>, Vec<Vector2<f32>>) {
    let radial_segments = radial_segments.max(2);
    let tubular_segments = tubular_segments.max(3);
    
    let mut positions = Vec::new();
    let mut normals = Vec::new();
    let mut uvs = Vec::new();
    let mut indices = Vec::new();
    
    // Generate vertices
    for j in 0..=radial_segments {
        for i in 0..=tubular_segments {
            let u = i as f32 / tubular_segments as f32 * 2.0 * PI;
            let v = j as f32 / radial_segments as f32 * 2.0 * PI;
            
            // Position
            let x = (radius + tube * v.cos()) * u.cos();
            let y = (radius + tube * v.cos()) * u.sin();
            let z = tube * v.sin();
            
            positions.push(crate::compat::point3::new(x, y, z));
            
            // Normal
            let center_x = radius * u.cos();
            let center_y = radius * u.sin();
            
            let nx = x - center_x;
            let ny = y - center_y;
            let nz = z;
            
            // Normalize
            let length = (nx * nx + ny * ny + nz * nz).sqrt();
            normals.push(crate::compat::vector3::new(nx / length, ny / length, nz / length));
            
            // UV
            uvs.push(crate::compat::vector2::new(i as f32 / tubular_segments as f32, j as f32 / radial_segments as f32));
        }
    }
    
    // Generate indices
    for j in 0..radial_segments {
        for i in 0..tubular_segments {
            let a = (j * (tubular_segments + 1) + i) as u32;
            let b = (j * (tubular_segments + 1) + i + 1) as u32;
            let c = ((j + 1) * (tubular_segments + 1) + i + 1) as u32;
            let d = ((j + 1) * (tubular_segments + 1) + i) as u32;
            
            // Two triangles per cell
            indices.push(Triangle::new(a, b, d));
            indices.push(Triangle::new(b, c, d));
        }
    }
    
    (positions, indices, normals, uvs)
}

/// Generate an icosahedron (20-sided polyhedron)
/// 
/// # Parameters
/// * `radius` - Radius of the circumscribed sphere
/// 
/// # Returns
/// Tuple of (positions, indices, normals, uvs)
pub fn generate_icosahedron(radius: f32) -> (Vec<Point3<f32>>, Vec<Triangle>, Vec<Vector3<f32>>, Vec<Vector2<f32>>) {
    // Constants for icosahedron construction
    let t = (1.0 + 5.0_f32.sqrt()) / 2.0;
    
    let mut positions = Vec::new();
    let mut normals = Vec::new();
    let mut uvs = Vec::new();
    let mut indices = Vec::new();
    
    // The 12 vertices of the icosahedron
    let vertices = [
        [-1.0, t, 0.0],
        [1.0, t, 0.0],
        [-1.0, -t, 0.0],
        [1.0, -t, 0.0],
        
        [0.0, -1.0, t],
        [0.0, 1.0, t],
        [0.0, -1.0, -t],
        [0.0, 1.0, -t],
        
        [t, 0.0, -1.0],
        [t, 0.0, 1.0],
        [-t, 0.0, -1.0],
        [-t, 0.0, 1.0],
    ];
    
    // Normalize and scale vertices
    for vertex in &vertices {
        let length = (vertex[0] * vertex[0] + vertex[1] * vertex[1] + vertex[2] * vertex[2]).sqrt();
        let normalized = [
            vertex[0] / length * radius,
            vertex[1] / length * radius,
            vertex[2] / length * radius,
        ];
        
        positions.push(crate::compat::point3::new(normalized[0], normalized[1], normalized[2]));
        
        // Normals (same as normalized positions)
        normals.push(crate::compat::vector3::new(normalized[0] / radius, normalized[1] / radius, normalized[2] / radius));
        
        // Basic UV mapping (spherical projection)
        let u = 0.5 + (normalized[0] / radius).atan2(normalized[2] / radius) / (2.0 * PI);
        let v = 0.5 - (normalized[1] / radius).asin() / PI;
        
        uvs.push(crate::compat::vector2::new(u, v));
    }
    
    // The 20 triangles of the icosahedron
    let triangle_indices = [
        [0, 11, 5],
        [0, 5, 1],
        [0, 1, 7],
        [0, 7, 10],
        [0, 10, 11],
        
        [1, 5, 9],
        [5, 11, 4],
        [11, 10, 2],
        [10, 7, 6],
        [7, 1, 8],
        
        [3, 9, 4],
        [3, 4, 2],
        [3, 2, 6],
        [3, 6, 8],
        [3, 8, 9],
        
        [4, 9, 5],
        [2, 4, 11],
        [6, 2, 10],
        [8, 6, 7],
        [9, 8, 1],
    ];
    
    // Add triangle indices
    for tri in &triangle_indices {
        indices.push(Triangle::new(tri[0] as u32, tri[1] as u32, tri[2] as u32));
    }
    
    (positions, indices, normals, uvs)
}