### Create a Tetrahedron Sphere Source: https://context7.com/optimisticpeach/hexasphere/llms.txt Initializes a TetraSphere, which is best suited for higher subdivision counts to ensure uniform distribution. The example demonstrates storing both position and normal data per vertex. ```rust use hexasphere::shapes::TetraSphere; use glam::Vec3A; // Tetrahedron base - use higher subdivisions for better uniformity let tetra_sphere = TetraSphere::new(15, |point: Vec3A| { // Store both position and normal (same for unit sphere) (point, point.normalize()) }); let points = tetra_sphere.raw_points(); let indices = tetra_sphere.get_all_indices(); // Access custom vertex data (position, normal tuples) for (i, (pos, normal)) in tetra_sphere.raw_data().iter().enumerate() { println!("Vertex {}: pos={:?}, normal={:?}", i, pos, normal); } ``` -------------------------------- ### Create an Icosphere with Custom Vertex Data Source: https://context7.com/optimisticpeach/hexasphere/llms.txt Initializes an IcoSphere with a specified subdivision level and a closure to generate custom vertex data. Use raw_points, raw_data, and get_all_indices to access the generated geometry for rendering or physics. ```rust use hexasphere::shapes::IcoSphere; use glam::Vec3A; // Create an icosphere with 20 subdivisions // The closure generates custom data for each vertex (here we store the position itself) let sphere = IcoSphere::new(20, |point: Vec3A| { // point is a normalized Vec3A on the unit sphere // Return any custom data to associate with this vertex point }); // Access the raw vertex positions (all normalized to unit sphere) let points: &[Vec3A] = sphere.raw_points(); println!("Total vertices: {}", points.len()); // Access the custom data generated for each vertex let data: &[Vec3A] = sphere.raw_data(); // Get triangle indices for rendering (every 3 indices form a triangle) let indices: Vec = sphere.get_all_indices(); println!("Total triangles: {}", indices.len() / 3); // Process triangles for triangle in indices.chunks(3) { let v0 = points[triangle[0] as usize]; let v1 = points[triangle[1] as usize]; let v2 = points[triangle[2] as usize]; // Use vertices for rendering, physics, etc. } ``` -------------------------------- ### Configure Hexasphere for std and no_std Builds Source: https://github.com/optimisticpeach/hexasphere/blob/master/readme.md This configuration in `Cargo.toml` allows your project to support both `std` and `no_std` builds for the hexasphere library. It defines features for `std` and `libm`. ```toml [features] default = ["std"] std = ["hexasphere/std"] libm = ["hexasphere/libm"] [dependencies] hexasphere = { version = "15", default-features = false } ``` -------------------------------- ### Use Interpolation Functions Source: https://context7.com/optimisticpeach/hexasphere/llms.txt Utilize built-in interpolation functions like slerp, nlerp, and lerp for geometric calculations on spherical or flat surfaces. ```rust use hexasphere::interpolation::{ geometric_slerp, geometric_slerp_half, geometric_slerp_multiple, normalized_lerp, normalized_lerp_half, lerp, lerp_half, }; use glam::Vec3A; let a = Vec3A::new(1.0, 0.0, 0.0); let b = Vec3A::new(0.0, 1.0, 0.0); // Geometric spherical interpolation (most accurate for spheres) let mid_slerp = geometric_slerp(a, b, 0.5); let mid_slerp_optimized = geometric_slerp_half(a, b); println!("Slerp midpoint: {:?}", mid_slerp); // Normalized linear interpolation (faster, less accurate) let mid_nlerp = normalized_lerp(a, b, 0.5); let mid_nlerp_optimized = normalized_lerp_half(a, b); println!("Nlerp midpoint: {:?}", mid_nlerp); // Simple linear interpolation (for flat shapes) let mid_lerp = lerp(a, b, 0.5); let mid_lerp_optimized = lerp_half(a, b); println!("Lerp midpoint: {:?}", mid_lerp); // Multiple interpolation for batch processing let mut results = [Vec3A::ZERO; 3]; let indices = [0u32, 1, 2]; geometric_slerp_multiple(a, b, &indices, &mut results); // Results contain points at 25%, 50%, 75% along the arc ``` -------------------------------- ### Create a Cube Sphere with UV Mapping Source: https://context7.com/optimisticpeach/hexasphere/llms.txt Generates a sphere from a cube base, useful for mapping textures via UV coordinates. The closure calculates UVs based on the normalized sphere position. ```rust use hexasphere::shapes::CubeSphere; use glam::Vec3A; // Create a cube-based sphere with 10 subdivisions let cube_sphere = CubeSphere::new(10, |point: Vec3A| { // Calculate UV coordinates from sphere position let u = 0.5 + (point.z.atan2(point.x) / (2.0 * std::f32::consts::PI)); let v = 0.5 - (point.y.asin() / std::f32::consts::PI); (u, v) }); // Access UV data for texture mapping let uv_data: &[(f32, f32)] = cube_sphere.raw_data(); let points = cube_sphere.raw_points(); let indices = cube_sphere.get_all_indices(); // Get subdivision count println!("Subdivisions: {}", cube_sphere.subdivisions()); // Calculate vertices per triangle statistics println!("Indices per main triangle: {}", cube_sphere.indices_per_main_triangle()); println!("Shared vertices: {}", cube_sphere.shared_vertices()); ``` -------------------------------- ### Enable no_std Support for Hexasphere Source: https://github.com/optimisticpeach/hexasphere/blob/master/readme.md To enable `no_std` support, compile with `--no-default-features` to disable `std` and `--features libm` for math functions. This configuration is useful for embedded systems or environments without a standard library. ```toml [dependencies] hexasphere = { version = "15", default-features = false, features = ["libm"] } ``` -------------------------------- ### Create Flat Plane Shapes Source: https://context7.com/optimisticpeach/hexasphere/llms.txt Generates subdivided square and triangle planes using linear interpolation. ```rust use hexasphere::shapes::{SquarePlane, TrianglePlane}; use glam::Vec3A; // Create a subdivided square plane on the XZ plane let square = SquarePlane::new(5, |point: Vec3A| point); // The square spans from (-1, 0, -1) to (1, 0, 1) let points = square.raw_points(); let indices = square.get_all_indices(); println!("Square vertices: {}", points.len()); println!("Square triangles: {}", indices.len() / 3); // Create a subdivided equilateral triangle let triangle = TrianglePlane::new(8, |point: Vec3A| { // point.y is always 0 for the triangle plane point }); let tri_points = triangle.raw_points(); println!("Triangle vertices: {}", tri_points.len()); ``` -------------------------------- ### Generate Normalized Linear Interpolation Sphere Source: https://context7.com/optimisticpeach/hexasphere/llms.txt Creates a sphere using nlerp, which is faster than slerp but less accurate for distant points. ```rust use hexasphere::shapes::NormIcoSphere; use glam::Vec3A; // Faster sphere generation with normalized linear interpolation let norm_sphere = NormIcoSphere::new(12, |point: Vec3A| { // All points are still on the unit sphere (normalized) point }); let points = norm_sphere.raw_points(); let indices = norm_sphere.get_all_indices(); // Verify points are normalized for point in points { let length = point.length(); assert!((length - 1.0).abs() < 0.001); } ``` -------------------------------- ### Generate Wireframe Line Indices Source: https://context7.com/optimisticpeach/hexasphere/llms.txt Creates line strip indices for wireframe rendering, including support for primitive restart markers. ```rust use hexasphere::shapes::IcoSphere; use glam::Vec3A; let sphere = IcoSphere::new(3, |_| ()); // Get line indices with a delta offset and break callback // delta is added to all indices (useful for index 0 as a special marker) // breaks is called at each line strip break point let delta = 1; let line_indices = sphere.get_all_line_indices(delta, |buffer| { // Push a break marker (e.g., 0 or u32::MAX) for primitive restart buffer.push(0); }); println!("Line strip indices: {}", line_indices.len()); // Get wireframe for only the major edges (base shape edges) let mut major_edges = Vec::new(); sphere.get_major_edges_line_indices(&mut major_edges, 1, |buffer| { buffer.push(0); // Break marker }); ``` -------------------------------- ### Calculate Point Distances Source: https://context7.com/optimisticpeach/hexasphere/llms.txt Compute Euclidean distances between points on a subdivided shape. ```rust use hexasphere::shapes::IcoSphere; use glam::Vec3A; let sphere = IcoSphere::new(10, |_| ()); // Linear (Euclidean) distance between two points let linear_dist = sphere.linear_distance(0, 1, 1.0); // radius = 1.0 println!("Linear distance: {}", linear_dist); // For spherical distance, use with shape-extras feature // let spherical_dist = sphere.spherical_distance(0, 1, 1.0); ``` -------------------------------- ### Perform Incremental Subdivision Source: https://context7.com/optimisticpeach/hexasphere/llms.txt Subdivides an existing shape and recalculates vertex data without full reconstruction. ```rust use hexasphere::shapes::IcoSphere; use glam::Vec3A; // Start with low subdivision let mut sphere = IcoSphere::new(2, |point: Vec3A| point); println!("Initial vertices: {}", sphere.raw_points().len()); // Add more subdivisions sphere.subdivide(3); // Must recalculate vertex data after subdivision sphere.calculate_values(|point: Vec3A| point); println!("After subdivision vertices: {}", sphere.raw_points().len()); println!("Current subdivision level: {}", sphere.subdivisions()); ``` -------------------------------- ### Track Vertex Adjacency Source: https://context7.com/optimisticpeach/hexasphere/llms.txt Build an adjacency map to identify neighboring vertices, requiring the 'adjacency' feature enabled. ```rust // Requires: features = ["adjacency"] use hexasphere::shapes::IcoSphere; use hexasphere::AdjacencyBuilder; use glam::Vec3A; let sphere = IcoSphere::new(5, |_| ()); // Get all indices let mut indices = Vec::new(); for i in 0..20 { // Icosahedron has 20 main triangles sphere.get_indices(i, &mut indices); } // Build adjacency map let mut builder = AdjacencyBuilder::new(sphere.raw_points().len()); builder.add_indices(&indices); let neighbors = builder.finish(); // neighbors[i] contains up to 6 neighbor vertex indices for vertex i // Most vertices have 6 neighbors (hexagons), some have 5 (pentagons) for (vertex_idx, vertex_neighbors) in neighbors.iter().enumerate() { println!("Vertex {} has {} neighbors: {:?}", vertex_idx, vertex_neighbors.len(), vertex_neighbors); } ``` -------------------------------- ### Implement Custom BaseShape Source: https://context7.com/optimisticpeach/hexasphere/llms.txt Define a custom shape by implementing the BaseShape trait, allowing for alternative interpolation methods like linear interpolation instead of spherical. ```rust use hexasphere::{BaseShape, Triangle, Subdivided}; use hexasphere::shapes::IcoSphereBase; use hexasphere::interpolation; use glam::Vec3A; use alloc::boxed::Box; use alloc::vec::Vec; // Custom shape using linear interpolation instead of spherical struct FlatIcosahedron; impl BaseShape for FlatIcosahedron { fn initial_points(&self) -> Vec { // Reuse icosahedron vertices IcoSphereBase.initial_points() } fn triangles(&self) -> Box<[Triangle]> { IcoSphereBase.triangles() } const EDGES: usize = IcoSphereBase::EDGES; fn interpolate(&self, a: Vec3A, b: Vec3A, p: f32) -> Vec3A { // Use linear interpolation (creates flat faces) interpolation::lerp(a, b, p) } fn interpolate_half(&self, a: Vec3A, b: Vec3A) -> Vec3A { interpolation::lerp_half(a, b) } fn interpolate_multiple(&self, a: Vec3A, b: Vec3A, indices: &[u32], points: &mut [Vec3A]) { interpolation::lerp_multiple(a, b, indices, points); } } // Use custom shape let flat_ico: Subdivided<(), FlatIcosahedron> = Subdivided::new(5, |_| ()); let points = flat_ico.raw_points(); let indices = flat_ico.get_all_indices(); ``` -------------------------------- ### Retrieve Triangle Indices Source: https://context7.com/optimisticpeach/hexasphere/llms.txt Accesses indices for specific main triangles or the entire mesh, useful for partial rendering. ```rust use hexasphere::shapes::IcoSphere; use glam::Vec3A; let sphere = IcoSphere::new(5, |_| ()); // Get indices for a specific main triangle (icosahedron has 20 main triangles) let mut buffer = Vec::new(); sphere.get_indices(0, &mut buffer); // First main triangle println!("Triangle 0 indices: {} indices", buffer.len()); buffer.clear(); sphere.get_indices(5, &mut buffer); // Sixth main triangle println!("Triangle 5 indices: {} indices", buffer.len()); // Get all indices at once let all_indices = sphere.get_all_indices(); println!("Total indices: {}", all_indices.len()); // Access main triangle information let main_triangles = sphere.main_triangles(); println!("Number of main triangles: {}", main_triangles.len()); ``` -------------------------------- ### Modify Vertex Data Source: https://context7.com/optimisticpeach/hexasphere/llms.txt Access and update custom vertex data after the shape has been initialized. ```rust use hexasphere::shapes::IcoSphere; use glam::Vec3A; let mut sphere = IcoSphere::new(5, |point: Vec3A| { // Initial color based on height if point.y > 0.0 { 1.0 } else { 0.0 } }); // Get mutable access to vertex data let data = sphere.raw_data_mut(); // Modify vertex data (e.g., apply noise or other transformations) for (i, value) in data.iter_mut().enumerate() { let point = sphere.raw_points()[i]; // Create gradient based on position *value = (point.y + 1.0) / 2.0; } // Read modified data for (point, color) in sphere.raw_points().iter().zip(sphere.raw_data().iter()) { println!("Point {:?} has color value {}", point, color); } ``` -------------------------------- ### Calculate Icosphere Shape Radius Source: https://context7.com/optimisticpeach/hexasphere/llms.txt Determines the circumscribed radius from the center of a hexagon or pentagon to its vertices, useful for tile sizing and texture coordinate calculations. ```rust use hexasphere::shapes::IcoSphere; use glam::Vec3A; let sphere = IcoSphere::new(10, |_| ()); // Get the radius from center of a hex/pentagon to its vertices let shape_radius = sphere.radius_shapes(); println!("Shape circumscribed radius: {}", shape_radius); // This is useful for determining tile sizes in hex-based games // or for calculating texture coordinates ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.