### Vector Swizzling Example (cgmath) Source: https://github.com/rustgd/cgmath/blob/master/README.md Demonstrates the use of the swizzle feature to create new vectors from components of an existing vector. Requires the 'swizzle' feature to be enabled. ```rust let v = Vector3::new(1.0, 2.0, 3.0); v.xyxz() v.zy() ``` -------------------------------- ### Perspective and Orthographic Projection Matrices in Rust Source: https://context7.com/rustgd/cgmath/llms.txt Shows how to create perspective and orthographic projection matrices using cgmath functions like `perspective`, `frustum`, and `ortho`. Includes examples using both function calls and struct initializations. ```rust use cgmath::{perspective, frustum, ortho, Rad, Deg, Matrix4, PerspectiveFov, Perspective, Ortho}; use std::f32::consts::PI; // Perspective projection (gluPerspective style) let fovy = Deg(60.0_f32); let aspect = 16.0_f32 / 9.0; let near = 0.1_f32; let far = 100.0_f32; let persp_matrix: Matrix4 = perspective(fovy, aspect, near, far); // Perspective from struct let persp_fov = PerspectiveFov { fovy: Rad(PI / 3.0), aspect: 16.0 / 9.0, near: 0.1, far: 100.0, }; let persp_matrix2: Matrix4 = persp_fov.into(); // Frustum projection (glFrustum style) let frustum_matrix: Matrix4 = frustum( -1.0, // left 1.0, // right -1.0, // bottom 1.0, // top 0.1, // near 100.0, // far ); // Orthographic projection (glOrtho style) let ortho_matrix: Matrix4 = ortho( -10.0, // left 10.0, // right -10.0, // bottom 10.0, // top 0.1, // near 100.0, // far ); // Ortho from struct let ortho_struct = Ortho { left: -10.0, right: 10.0, bottom: -10.0, top: 10.0, near: 0.1, far: 100.0, }; let ortho_matrix2: Matrix4 = ortho_struct.into(); // Convert PerspectiveFov to Perspective (explicit frustum planes) let persp_planes: Perspective = persp_fov.to_perspective(); ``` -------------------------------- ### Angle Types and Operations in Rust Source: https://context7.com/rustgd/cgmath/llms.txt Demonstrates creating, converting, and performing trigonometric operations with Rad and Deg angle types. Includes examples of angle arithmetic and accessing full turn constants. ```rust use cgmath::{Rad, Deg, Angle}; use std::f32::consts::PI; // Creating angles let radians = Rad(PI / 2.0_f32); let degrees = Deg(90.0_f32); // Conversion between units let to_deg: Deg = radians.into(); // Deg(90.0) let to_rad: Rad = degrees.into(); // Rad(PI/2) // Trigonometric functions let sin_val = radians.sin(); // 1.0 let cos_val = radians.cos(); // ~0.0 let tan_val = Rad(PI / 4.0_f32).tan(); // 1.0 let (sin, cos) = radians.sin_cos(); // Inverse trig functions let asin = Rad::asin(1.0_f32); // Rad(PI/2) let acos = Rad::acos(0.0_f32); // Rad(PI/2) let atan = Rad::atan(1.0_f32); // Rad(PI/4) let atan2 = Rad::atan2(1.0_f32, 1.0_f32); // Rad(PI/4) // Angle arithmetic let sum = Deg(45.0_f32) + Deg(45.0_f32); // Deg(90.0) let scaled = Deg(45.0_f32) * 2.0; // Deg(90.0) let ratio = Deg(90.0_f32) / Deg(45.0_f32); // 2.0 (scalar) // Full turn constants let full_turn = Rad::::full_turn(); // 2*PI radians let half_turn = Rad::::turn_div_2(); // PI radians let quarter_turn = Rad::::turn_div_4(); // PI/2 radians // Normalize angle to [-PI, PI] range let normalized = radians.normalize_signed(); ``` -------------------------------- ### Create and Use Matrix Types in Rust Source: https://context7.com/rustgd/cgmath/llms.txt Demonstrates creating identity, translation, scaling, and rotation matrices, as well as performing matrix multiplication and transforming vectors and points. Ensure correct column-major order when initializing matrices. ```rust use cgmath::{Matrix2, Matrix3, Matrix4, Vector3, Vector4, Point3, Rad, Deg, SquareMatrix, One, Zero}; use std::f32::consts::PI; // Identity matrices let identity2 = Matrix2::::identity(); let identity3 = Matrix3::::identity(); let identity4 = Matrix4::::identity(); // Creating matrices from values (column-major order) let m2 = Matrix2::new( 1.0, 0.0, // column 0 0.0, 1.0, // column 1 ); // Creating from column vectors let m3 = Matrix3::from_cols( Vector3::new(1.0_f32, 0.0, 0.0), Vector3::new(0.0_f32, 1.0, 0.0), Vector3::new(0.0_f32, 0.0, 1.0), ); // Transformation matrices let translation = Matrix4::from_translation(Vector3::new(1.0_f32, 2.0, 3.0)); let scale_uniform = Matrix4::from_scale(2.0_f32); let scale_nonuniform = Matrix4::from_nonuniform_scale(1.0_f32, 2.0, 3.0); // Rotation matrices let rot_x = Matrix4::from_angle_x(Rad(PI / 2.0)); let rot_y = Matrix4::from_angle_y(Deg(90.0_f32)); let rot_z = Matrix4::from_angle_z(Rad(PI / 4.0)); // Rotation around arbitrary axis (axis must be normalized) let axis = Vector3::new(0.0_f32, 1.0, 0.0); let rot_axis = Matrix4::from_axis_angle(axis, Rad(PI / 2.0)); // Matrix multiplication let combined = translation * rot_y; // First rotate, then translate // Transform a vector (direction, no translation) let direction = Vector3::new(1.0_f32, 0.0, 0.0); let transformed_dir = (combined * direction.extend(0.0)).truncate(); // Transform a point (includes translation) let point = Point3::new(1.0_f32, 0.0, 0.0); let transformed_point = Point3::from_homogeneous(combined * point.to_homogeneous()); // Matrix operations let determinant = m3.determinant(); let transposed = m3.transpose(); let inverted = m3.invert(); // Returns Option // Look-at matrix for camera let eye = Point3::new(0.0_f32, 0.0, 5.0); let center = Point3::new(0.0_f32, 0.0, 0.0); let up = Vector3::new(0.0_f32, 1.0, 0.0); let view_matrix = Matrix4::look_at_rh(eye, center, up); ``` -------------------------------- ### Using CGMath Prelude for Common Imports in Rust Source: https://context7.com/rustgd/cgmath/llms.txt Demonstrates importing common CGMath traits and types using `cgmath::prelude::*`. This allows direct use of trait methods like `magnitude`, `normalize`, and `distance` without explicit trait imports. ```rust use cgmath::prelude::*; // This imports: // - Array, ElementWise // - EuclideanSpace, InnerSpace, MetricSpace, VectorSpace // - Matrix, SquareMatrix // - Rotation, Rotation2, Rotation3 // - Transform, Transform2, Transform3 // - Angle // - One, Zero // - approx traits (AbsDiffEq, RelativeEq, UlpsEq) // Now you can use trait methods without explicit imports use cgmath::{Vector3, Point3}; let v = Vector3::new(3.0_f32, 4.0, 0.0); let magnitude = v.magnitude(); // From InnerSpace let normalized = v.normalize(); // From InnerSpace let p1 = Point3::new(0.0_f32, 0.0, 0.0); let p2 = Point3::new(3.0_f32, 4.0, 0.0); let distance = p1.distance(p2); // From MetricSpace ``` -------------------------------- ### Vector Operations in CGMath Source: https://context7.com/rustgd/cgmath/llms.txt Demonstrates creating, manipulating, and performing operations on 2D, 3D, and 4D vectors using CGMath. Includes arithmetic, dot/cross products, normalization, and conversions. ```rust use cgmath::{Vector2, Vector3, Vector4, vec2, vec3, vec4, InnerSpace, MetricSpace, Zero}; // Creating vectors using constructors let v2 = Vector2::new(3.0_f32, 4.0); let v3 = Vector3::new(1.0_f32, 2.0, 3.0); let v4 = Vector4::new(1.0_f32, 0.0, 0.0, 1.0); // Short constructors let a = vec3(1.0_f32, 0.0, 0.0); let b = vec3(0.0_f32, 1.0, 0.0); // Vector arithmetic let sum = a + b; // Vector3 { x: 1.0, y: 1.0, z: 0.0 } let scaled = a * 2.0; // Vector3 { x: 2.0, y: 0.0, z: 0.0 } let diff = a - b; // Vector3 { x: 1.0, y: -1.0, z: 0.0 } // Dot product let dot = a.dot(b); // 0.0 // Cross product (3D only) let cross = a.cross(b); // Vector3 { x: 0.0, y: 0.0, z: 1.0 } // Magnitude and normalization let magnitude = v2.magnitude(); // 5.0 let normalized = v2.normalize(); // Vector2 { x: 0.6, y: 0.8 } // Distance between vectors let dist = a.distance(b); // sqrt(2) // Extending/truncating vectors let v3_extended = v2.extend(5.0); // Vector3 { x: 3.0, y: 4.0, z: 5.0 } let v2_truncated = v3.truncate(); // Vector2 { x: 1.0, y: 2.0 } // Unit vectors let unit_x = Vector3::::unit_x(); // Vector3 { x: 1.0, y: 0.0, z: 0.0 } let unit_y = Vector3::::unit_y(); // Vector3 { x: 0.0, y: 1.0, z: 0.0 } let unit_z = Vector3::::unit_z(); // Vector3 { x: 0.0, y: 0.0, z: 1.0 } // Zero vector let zero = Vector3::::zero(); // Array/tuple conversions let arr: [f32; 3] = v3.into(); let from_arr = Vector3::from([1.0_f32, 2.0, 3.0]); ``` -------------------------------- ### Approximate Floating-Point Comparisons in Rust Source: https://context7.com/rustgd/cgmath/llms.txt Shows how to use the `approx` crate integration for comparing floating-point types like Vector3, Matrix4, and Quaternion. Use `abs_diff_eq` for absolute differences, `relative_eq` for relative differences, and `ulps_eq` for ULPs comparison. ```rust use cgmath::{Vector3, Matrix4, Quaternion, abs_diff_eq, relative_eq, ulps_eq}; let v1 = Vector3::new(1.0_f32, 2.0, 3.0); let v2 = Vector3::new(1.0000001_f32, 2.0, 3.0); // Absolute difference comparison let are_close = abs_diff_eq!(v1, v2, epsilon = 0.001); // true // Relative comparison (better for large values) let are_relative_close = relative_eq!(v1, v2, max_relative = 0.001); // ULPs (Units in Last Place) comparison let are_ulps_close = ulps_eq!(v1, v2, max_ulps = 10); // Works with all cgmath types let m1 = Matrix4::::identity(); let m2 = Matrix4::::identity(); let matrices_equal = abs_diff_eq!(m1, m2); let q1 = Quaternion::new(1.0_f32, 0.0, 0.0, 0.0); let q2 = Quaternion::new(1.0_f32, 0.0, 0.0, 0.0); let quats_equal = ulps_eq!(q1, q2); ``` -------------------------------- ### Create and Convert Euler Angles in Rust Source: https://context7.com/rustgd/cgmath/llms.txt Demonstrates creating Euler angles using radians and degrees, and converting them to rotation matrices and quaternions. Ensure correct units (Rad or Deg) are used for initialization. ```rust use cgmath::{Euler, Rad, Deg, Matrix3, Matrix4, Quaternion}; use std::f32::consts::PI; // Create Euler angles (x=pitch, y=yaw, z=roll) let euler = Euler { x: Rad(PI / 4.0_f32), // pitch: 45 degrees y: Rad(PI / 2.0_f32), // yaw: 90 degrees z: Rad(0.0_f32), // roll: 0 degrees }; // Using degrees let euler_deg = Euler { x: Deg(45.0_f32), y: Deg(90.0_f32), z: Deg(0.0_f32), }; // Convert to rotation matrix let rot_matrix3: Matrix3 = euler.into(); let rot_matrix4: Matrix4 = euler.into(); // Convert to quaternion let quaternion: Quaternion = euler.into(); ``` -------------------------------- ### Point Operations in CGMath Source: https://context7.com/rustgd/cgmath/llms.txt Illustrates the creation and manipulation of 2D and 3D points in CGMath, emphasizing their distinction from vectors. Covers translation, displacement, and conversions. ```rust use cgmath::{Point2, Point3, point2, point3, Vector2, Vector3, EuclideanSpace, MetricSpace}; // Creating points let p1 = Point3::new(1.0_f32, 2.0, 3.0); let p2 = Point3::new(4.0_f32, 5.0, 6.0); // Short constructor let p = point3(0.0_f32, 0.0, 0.0); // Origin point let origin = Point3::::origin(); // Point3 { x: 0.0, y: 0.0, z: 0.0 } // Point - Point = Vector (displacement) let displacement: Vector3 = p2 - p1; // Vector3 { x: 3.0, y: 3.0, z: 3.0 } // Point + Vector = Point (translation) let translated = p1 + Vector3::new(1.0_f32, 1.0, 1.0); // Point3 { x: 2.0, y: 3.0, z: 4.0 } // Point - Vector = Point let moved_back = p2 - Vector3::new(1.0_f32, 1.0, 1.0); // Point3 { x: 3.0, y: 4.0, z: 5.0 } // Distance between points let distance = p1.distance(p2); // sqrt(27) // Convert to/from vector let vec = p1.to_vec(); // Vector3 { x: 1.0, y: 2.0, z: 3.0 } let point = Point3::from_vec(vec); // Point3 { x: 1.0, y: 2.0, z: 3.0 } // Midpoint between two points let mid = p1.midpoint(p2); // Point3 { x: 2.5, y: 3.5, z: 4.5 } // Homogeneous coordinates (for 3D points) let homogeneous = p1.to_homogeneous(); // Vector4 { x: 1.0, y: 2.0, z: 3.0, w: 1.0 } let from_homo = Point3::from_homogeneous(homogeneous); ``` -------------------------------- ### Decomposed Affine Transformations in Rust Source: https://context7.com/rustgd/cgmath/llms.txt Illustrates using the `Decomposed` struct and `Transform` trait for affine transformations, including combining, inverting, and converting to `Matrix4`. Shows creation of identity and look-at transforms. ```rust use cgmath::{Decomposed, Vector3, Quaternion, Rad, Transform, Point3, Matrix4, Rotation3, One}; use std::f32::consts::PI; // Create a decomposed transform let transform: Decomposed, Quaternion> = Decomposed { scale: 2.0, rot: Quaternion::from_axis_angle(Vector3::unit_y(), Rad(PI / 2.0)), disp: Vector3::new(1.0, 2.0, 3.0), }; // Identity transform let identity: Decomposed, Quaternion> = Decomposed::one(); // Transform a point let point = Point3::new(1.0_f32, 0.0, 0.0); let transformed_point = transform.transform_point(point); // Transform a vector (direction only, no translation) let direction = Vector3::new(1.0_f32, 0.0, 0.0); let transformed_dir = transform.transform_vector(direction); // Combine transforms let transform2: Decomposed, Quaternion> = Decomposed { scale: 0.5, rot: Quaternion::from_axis_angle(Vector3::unit_x(), Rad(PI / 4.0)), disp: Vector3::new(-1.0, 0.0, 0.0), }; let combined = transform.concat(&transform2); // Inverse transform let inverse = transform.inverse_transform(); // Returns Option // Convert to Matrix4 let matrix: Matrix4 = transform.into(); // Look-at transform let eye = Point3::new(0.0_f32, 0.0, 5.0); let center = Point3::new(0.0_f32, 0.0, 0.0); let up = Vector3::new(0.0_f32, 1.0, 0.0); let look_at: Decomposed, Quaternion> = Decomposed::look_at_rh(eye, center, up); ``` -------------------------------- ### Create and Use Quaternions in Rust Source: https://context7.com/rustgd/cgmath/llms.txt Shows how to create identity, axis-angle, and scalar-vector quaternions, and perform operations like rotation, conjugation, normalization, and interpolation. Quaternions are useful for gimbal-lock-free 3D rotations. ```rust use cgmath::{Quaternion, Vector3, Matrix3, Matrix4, Rad, Deg, Rotation, Rotation3, InnerSpace, One}; use std::f32::consts::PI; // Identity quaternion (no rotation) let identity = Quaternion::::one(); // From axis-angle let axis = Vector3::new(0.0_f32, 1.0, 0.0); // Y-axis let angle = Rad(PI / 2.0); // 90 degrees let q = Quaternion::from_axis_angle(axis, angle); // From scalar and vector parts let q2 = Quaternion::new(1.0_f32, 0.0, 0.0, 0.0); // w, x, y, z let q3 = Quaternion::from_sv(1.0_f32, Vector3::new(0.0, 0.0, 0.0)); // scalar, vector // Quaternion between two vectors let v1 = Vector3::new(1.0_f32, 0.0, 0.0); let v2 = Vector3::new(0.0_f32, 1.0, 0.0); let rotation_between = Quaternion::between_vectors(v1, v2); // Quaternion from arc (closest rotation from src to dst) let q_arc = Quaternion::from_arc(v1, v2, None); // Rotate a vector let rotated = q.rotate_vector(v1); // Quaternion operations let conjugate = q.conjugate(); let normalized = q.normalize(); let magnitude = q.magnitude(); // Quaternion multiplication (compose rotations) let combined = q * q2; // Apply q2 first, then q // Inverse rotation let inverse = q.invert(); // Interpolation let q_start = Quaternion::from_axis_angle(Vector3::unit_y(), Rad(0.0_f32)); let q_end = Quaternion::from_axis_angle(Vector3::unit_y(), Rad(PI)); // Normalized linear interpolation (faster, good for small angles) let nlerped = q_start.nlerp(q_end, 0.5); // Spherical linear interpolation (constant angular velocity) let slerped = q_start.slerp(q_end, 0.5); // Convert to rotation matrix let rotation_matrix: Matrix3 = q.into(); let rotation_matrix4: Matrix4 = q.into(); // Convert matrix to quaternion let q_from_matrix: Quaternion = rotation_matrix.into(); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.