Initial commit

This commit is contained in:
redclawsystems
2026-03-04 00:08:42 +00:00
commit 4d88dc0584
4449 changed files with 1556714 additions and 0 deletions
@@ -0,0 +1,193 @@
//! Geometric primitives for mesh topology
use super::nodes::NodeId;
use serde::{Deserialize, Serialize};
/// Edge connecting two nodes
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Edge {
pub n1: NodeId,
pub n2: NodeId,
}
impl Edge {
/// Create a new edge
pub fn new(n1: NodeId, n2: NodeId) -> Self {
// Ensure consistent ordering for edge comparison
if n1.0 < n2.0 {
Self { n1, n2 }
} else {
Self { n1: n2, n2: n1 }
}
}
/// Check if the edge contains a specific node
pub fn contains(&self, node: NodeId) -> bool {
self.n1 == node || self.n2 == node
}
/// Get the other node of the edge
pub fn other_node(&self, node: NodeId) -> Option<NodeId> {
if self.n1 == node {
Some(self.n2)
} else if self.n2 == node {
Some(self.n1)
} else {
None
}
}
}
/// Face in a 3D mesh (can be triangular or quadrilateral)
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Face {
Triangle(NodeId, NodeId, NodeId),
Quad(NodeId, NodeId, NodeId, NodeId),
}
impl Face {
/// Create a new face from a list of nodes
pub fn new(nodes: Vec<NodeId>) -> Self {
match nodes.len() {
3 => Self::Triangle(nodes[0], nodes[1], nodes[2]),
4 => Self::Quad(nodes[0], nodes[1], nodes[2], nodes[3]),
_ => panic!("Face must have 3 or 4 nodes"),
}
}
/// Get all nodes in the face
pub fn nodes(&self) -> Vec<NodeId> {
match self {
Self::Triangle(n1, n2, n3) => vec![*n1, *n2, *n3],
Self::Quad(n1, n2, n3, n4) => vec![*n1, *n2, *n3, *n4],
}
}
/// Check if the face contains a specific node
pub fn contains(&self, node: NodeId) -> bool {
match self {
Self::Triangle(n1, n2, n3) => *n1 == node || *n2 == node || *n3 == node,
Self::Quad(n1, n2, n3, n4) => *n1 == node || *n2 == node || *n3 == node || *n4 == node,
}
}
/// Get the edges of the face
pub fn edges(&self) -> Vec<Edge> {
match self {
Self::Triangle(n1, n2, n3) => vec![
Edge::new(*n1, *n2),
Edge::new(*n2, *n3),
Edge::new(*n3, *n1),
],
Self::Quad(n1, n2, n3, n4) => vec![
Edge::new(*n1, *n2),
Edge::new(*n2, *n3),
Edge::new(*n3, *n4),
Edge::new(*n4, *n1),
],
}
}
}
/// Rectangle geometry for 2D mesh generation
#[derive(Debug, Clone)]
pub struct Rectangle {
pub width: f64,
pub height: f64,
pub origin: [f64; 2],
}
impl Rectangle {
/// Create a new rectangle with given width and height
pub fn new(width: f64, height: f64) -> Self {
Self {
width,
height,
origin: [0.0, 0.0],
}
}
/// Create a rectangle with custom origin
pub fn with_origin(width: f64, height: f64, origin: [f64; 2]) -> Self {
Self {
width,
height,
origin,
}
}
}
/// Circle geometry for 2D mesh generation
#[derive(Debug, Clone)]
pub struct Circle {
pub radius: f64,
pub center: [f64; 2],
}
impl Circle {
/// Create a new circle with given radius
pub fn new(radius: f64) -> Self {
Self {
radius,
center: [0.0, 0.0],
}
}
/// Create a circle with custom center
pub fn with_center(radius: f64, center: [f64; 2]) -> Self {
Self { radius, center }
}
}
/// 3D Box geometry for 3D mesh generation
#[derive(Debug, Clone)]
pub struct Box3D {
pub width: f64,
pub height: f64,
pub depth: f64,
pub origin: [f64; 3],
}
impl Box3D {
/// Create a new box with given dimensions
pub fn new(width: f64, height: f64, depth: f64) -> Self {
Self {
width,
height,
depth,
origin: [0.0, 0.0, 0.0],
}
}
/// Create a box with custom origin
pub fn with_origin(width: f64, height: f64, depth: f64, origin: [f64; 3]) -> Self {
Self {
width,
height,
depth,
origin,
}
}
}
/// Sphere geometry for 3D mesh generation
#[derive(Debug, Clone)]
pub struct Sphere {
pub radius: f64,
pub center: [f64; 3],
}
impl Sphere {
/// Create a new sphere with given radius
pub fn new(radius: f64) -> Self {
Self {
radius,
center: [0.0, 0.0, 0.0],
}
}
/// Create a sphere with custom center
pub fn with_center(radius: f64, center: [f64; 3]) -> Self {
Self { radius, center }
}
}