### Creating a Cone Source: https://github.com/juliageometry/geometrybasics.jl/blob/master/docs/src/primitives.md Demonstrates the creation of a 3D Cone defined by its start point, tip point, and radius. ```julia using GeometryBasics c = Cone(Point3f(-1, 0, 0), Point3f(0, 0, 1), 0.3f0) # start point, tip point, radius ``` -------------------------------- ### TupleView Examples Source: https://context7.com/juliageometry/geometrybasics.jl/llms.txt Demonstrates creating TupleViews from an array with different tuple sizes and strides. TupleViews provide a way to view an array as a sequence of tuples. ```julia x = [1,2,3,4,5,6] tv2 = TupleView{2}(x) # [(1,2),(3,4),(5,6)] tv2_1 = TupleView{2,1}(x) # [(1,2),(2,3),(3,4),(4,5),(5,6)] ``` -------------------------------- ### Constructing and Manipulating Points, Vectors, and Matrices Source: https://context7.com/juliageometry/geometrybasics.jl/llms.txt Demonstrates construction of Point, Vec, and Mat types with different dimensions and types. Includes examples of arithmetic operations, utility checks, and dimensionality conversion using `to_ndim`. ```julia using GeometryBasics # Construction p1 = Point(1.0, 2.0, 3.0) # Point{3, Float64} p2 = Point2f(0.5, 0.5) # Point{2, Float32} p3 = Point{3, Int}(1, 2, 3) v1 = Vec(0.0, 0.0, 1.0) # Vec{3, Float64} — z-up direction v2 = Vec3f(1, 0, 0) # Vec{3, Float32} m = Mat{3,3,Float64}(1,0,0, 0,1,0, 0,0,1) # 3×3 identity # Arithmetic midpoint = (p1 + Point(p2[1], p2[2], 0.0)) ./ 2 scaled = v1 .* 5.0 rotated = m * Vec(p1) # Utility @assert !isnan(p1) @assert isfinite(v1) # to_ndim — promote/truncate to a different dimensionality p2d = Point2f(1, 2) p3d = to_ndim(Point3f, p2d, 0.0f0) # Point3f(1.0, 2.0, 0.0) ``` -------------------------------- ### Load and display a mesh with texture Source: https://github.com/juliageometry/geometrybasics.jl/blob/master/docs/src/meshes.md Loads an OBJ mesh and a diffuse texture using GLMakie and FileIO, then displays the mesh with the texture applied. Ensure GLMakie and GeometryBasics are installed and the asset files exist. ```julia using GLMakie, GLMakie.FileIO, GeometryBasics m = load(GLMakie.assetpath("cat.obj")) GLMakie.mesh(m; color=load(GLMakie.assetpath("diffusemap.png")), axis=(; show_axis=false)) ``` -------------------------------- ### Creating a Cylinder Source: https://github.com/juliageometry/geometrybasics.jl/blob/master/docs/src/primitives.md Illustrates the creation of a 3D Cylinder defined by its start and end points and its radius. ```julia using GeometryBasics c = Cylinder(Point3f(-1, 0, 0), Point3f(0, 0, 1), 0.3f0) # start point, end point, radius ``` -------------------------------- ### Usage of Custom Parallelepiped Source: https://context7.com/juliageometry/geometrybasics.jl/llms.txt Demonstrates how to use the custom `Parallelepiped` geometry primitive to create a mesh, get its faces, and compute its bounding box. ```julia # Usage pp = Parallelepiped(Point3f(0), Vec3f(1,0,0), Vec3f(0,1,0), Vec3f(0,0,1)) m = normal_mesh(pp) @show length(faces(m)) # 6 quad faces → 12 triangles after decompose bb = Rect(pp) # bounding box ``` -------------------------------- ### Tessellating a Cylinder Source: https://github.com/juliageometry/geometrybasics.jl/blob/master/docs/src/primitives.md Example of creating a tessellated Cylinder primitive, specifying the number of vertices for its circular bases. ```julia using GeometryBasics t = Tessellation(Cylinder(Point3f(0), Point3f(0,0,1), 0.2), 32) # 32 vertices for each circle normal_mesh(t) ``` -------------------------------- ### Encoding Vertex Connectivity with Face Types Source: https://context7.com/juliageometry/geometrybasics.jl/llms.txt Shows how to use `TriangleFace`, `QuadFace`, `GLTriangleFace`, and `LineFace` to represent polygon connectivity using vertex indices. Includes examples of decomposition, cyclic equality checks, and duplicate removal. ```julia using GeometryBasics # Triangle face with 1-based Int indices tf = TriangleFace(1, 2, 3) qf = QuadFace(1, 2, 3, 4) gl = GLTriangleFace(1, 2, 3) # uses GLIndex (UInt32) lf = LineFace{Int}(1, 2) # Convert quad → two triangles tris = decompose(TriangleFace{Int}, [QuadFace(1,2,3,4)]) # => [TriangleFace(1,2,3), TriangleFace(1,3,4)] # Cyclic equality check (useful for deduplication) f1 = GLTriangleFace(1, 2, 3) f2 = GLTriangleFace(2, 3, 1) @assert cyclic_equal(f1, f2) # true — same face, rotated @assert cyclic_hash(f1) == cyclic_hash(f2) # Remove duplicate faces from a list fs = [GLTriangleFace(1,2,3), GLTriangleFace(2,3,1), GLTriangleFace(4,5,6)] unique_fs = remove_duplicates(fs) # => 2 faces ``` -------------------------------- ### Instantiate and Develop Documentation Locally Source: https://github.com/juliageometry/geometrybasics.jl/blob/master/README.md Use these commands to set up the documentation project locally. Ensure you are in the `docs/` directory and use the `instantiate` command to add dependencies, then `dev .` to develop the package. ```julia julia --project=docs/ pkg> instantiate pkg> dev . julia --project=docs/ docs/make.jl ``` -------------------------------- ### Constructing Meshes from Primitives and Raw Data Source: https://context7.com/juliageometry/geometrybasics.jl/llms.txt Demonstrates creating meshes from predefined primitives like spheres and rectangles, as well as from raw position and face data. Shows how to generate meshes with different attribute sets (positions, normals, UVs). ```julia using GeometryBasics # From a primitive sphere = Sphere(Point3f(0), 1f0) m_tri = triangle_mesh(sphere) # positions + faces only m_n = normal_mesh(sphere) # + normals m_uv = uv_mesh(sphere) # + texture coordinates m_full = uv_normal_mesh(sphere) # + both # Inspect @show length(faces(m_full)) @show length(coordinates(m_full)) @show size(normals(m_full)) @show size(texturecoordinates(m_full)) # From raw data positions = Point3f[(0,0,0),(1,0,0),(0,1,0),(0,0,1)] fs = GLTriangleFace[(1,2,3),(1,2,4),(1,3,4),(2,3,4)] m_raw = Mesh(positions, fs) # Mesh from polygon (auto-triangulated) poly_pts = Point2f[(0,0),(1,0),(1,1),(0,1),(0,0)] m_poly = triangle_mesh(poly_pts) # Type conversion m_f64 = mesh(m_raw, pointtype = Point3d, facetype = QuadFace{Int}) ``` -------------------------------- ### Creating Bounding Box from Points and Matrix Transform Source: https://context7.com/juliageometry/geometrybasics.jl/llms.txt Illustrates how to create a tight bounding box from a set of points and how to apply matrix transformations to a rectangle. ```julia # Bounding box from geometry / points pts = [Point2f(0.1, 0.2), Point2f(0.8, 0.9), Point2f(0.3, 0.5)] bb = Rect(pts) # tight bounding box # Matrix transform m = Mat{3,3,Float32}(2,0,0, 0,2,0, 0,0,1) r_scaled = m * r1 ``` -------------------------------- ### Using FaceView for Per-Face Attributes Source: https://context7.com/juliageometry/geometrybasics.jl/llms.txt Illustrates how to assign attributes like colors or normals on a per-face basis using `FaceView` and `per_face`. Shows how to expand `FaceView` attributes to per-vertex data. ```julia using GeometryBasics positions = Point3f[(0,0,0),(1,0,0),(1,1,0),(0,1,0)] fs = QuadFace{Int}[QuadFace(1,2,3,4)] # Assign one color per face using FaceView colors = [:red] face_color = per_face(colors, fs) # FaceView with self-referential faces m = Mesh(positions, fs; color = face_color) # Manually build a FaceView for per-face normals normals_data = Vec3f[(0,0,1)] # one normal for the quad face normal_fv = FaceView(normals_data, QuadFace{Int}[QuadFace(1)]) m2 = Mesh(positions, fs; normal = normal_fv) # Expand FaceViews to per-vertex (duplicates data as needed) m_expanded = expand_faceviews(m2) @assert isempty(filter(v -> v isa FaceView, values(vertex_attributes(m_expanded)))) ``` -------------------------------- ### Creating Spheres and Circles Source: https://github.com/juliageometry/geometrybasics.jl/blob/master/docs/src/primitives.md Shows how to instantiate 4D HyperSpheres, 3D Spheres, and 2D Circles with specified origins and radii. ```julia using GeometryBasics s1 = HyperSphere{4, Int}(Point{4, Int}(0), 5) s2 = Sphere(Point3f(0, 0, 1), 1) s3 = Circle(Point2d(0), 2.0) ``` -------------------------------- ### Create Points in GeometryBasics.jl Source: https://github.com/juliageometry/geometrybasics.jl/blob/master/docs/src/index.md Demonstrates how to create Point objects using integer coordinates. Ensure 'using GeometryBasics' is called first. ```julia using GeometryBasics p1 = Point(3, 1) p2 = Point(1, 3) p3 = Point(4, 4) ``` -------------------------------- ### Creating a Pyramid Source: https://github.com/juliageometry/geometrybasics.jl/blob/master/docs/src/primitives.md Shows how to create a Pyramid primitive with a square base, defined by its center, height, and width. ```julia using GeometryBasics p = Pyramid(Point3f(0), 1f0, 0.3f0) # center, height, width ``` -------------------------------- ### Create Polygon from Points Source: https://github.com/juliageometry/geometrybasics.jl/blob/master/docs/src/index.md Demonstrates how to construct a Polygon using an array of Point objects. The first and last points should be identical to close the polygon. ```julia Polygon(Point{2, Int}[(3, 1), (4, 4), (2, 4), (1, 2), (3, 1)]) ``` -------------------------------- ### Bounding Box Properties and Distances Source: https://context7.com/juliageometry/geometrybasics.jl/llms.txt Demonstrates accessing properties of a bounding box like minimum, maximum, and widths, and calculating Euclidean distances between a bounding box and a point. ```julia # Properties @show minimum(bb), maximum(bb), widths(bb) # Distance from bounding box to a point p = Point3f(5, 5, 5) d_min = min_euclidean(bb, p) d_max = max_euclidean(bb, p) d_sq = min_euclideansq(bb, p) ``` -------------------------------- ### Creating and Triangulating Polygons Source: https://context7.com/juliageometry/geometrybasics.jl/llms.txt Demonstrates the creation of `Polygon` objects with and without holes, and their subsequent triangulation into a mesh using `triangle_mesh`. This process utilizes the EarCut algorithm for triangulation. ```julia using GeometryBasics # Simple convex polygon exterior = Point2f[(0,0), (1,0), (1,1), (0,1), (0,0)] poly = Polygon(exterior) # Polygon with a hole hole = Point2f[(0.2,0.2), (0.8,0.2), (0.8,0.8), (0.2,0.8), (0.2,0.2)] poly_h = Polygon(exterior, [hole]) # Triangulate to a mesh m = triangle_mesh(poly_h) @show length(faces(m)) # several triangles avoiding the hole ``` -------------------------------- ### HyperSphere, Circle, and Sphere Construction Source: https://context7.com/juliageometry/geometrybasics.jl/llms.txt Demonstrates the construction of N-dimensional spheres, specifically Circle (2D) and Sphere (3D), with specified center and radius. ```julia using GeometryBasics # Construction c = Circle(Point2f(0), 1.0f0) # 2D unit circle s = Sphere(Point3f(0, 0, 1), 0.5f0) # 3D sphere at z=1, r=0.5 ``` -------------------------------- ### Merging, Splitting, and Expanding Meshes Source: https://context7.com/juliageometry/geometrybasics.jl/llms.txt Demonstrates combining multiple meshes into a single `MetaMesh` using `merge`, splitting a `MetaMesh` back into individual meshes with `split_mesh`, and converting `FaceView` attributes to per-vertex data with `expand_faceviews`. ```julia using GeometryBasics m1 = normal_mesh(Sphere(Point3f(-2, 0, 0), 1f0)) m2 = normal_mesh(Sphere(Point3f( 2, 0, 0), 1f0)) m3 = normal_mesh(Rect3f(Point3f(0), Vec3f(1))) # Merge into one mesh with submesh views combined = merge([m1, m2, m3]) @show length(combined.views) # 3 submeshes @show length(faces(combined)) # sum of all faces # Split back into individual meshes parts = split_mesh(combined) @assert length(parts) == 3 # Expand FaceViews (needed for GPU upload / rendering) flat = expand_faceviews(combined) @show all(v -> !(v isa FaceView), values(vertex_attributes(flat))) # true ``` -------------------------------- ### Create Linestring from Multiple Points Source: https://github.com/juliageometry/geometrybasics.jl/blob/master/docs/src/index.md Illustrates the creation of a LineString by providing a list of Point objects. Assumes points p1, p2, and p3 are already defined. ```julia LineString([p1, p2, p3]) ``` -------------------------------- ### Bounding Box from Points Source: https://context7.com/juliageometry/geometrybasics.jl/llms.txt Shows how to create an axis-aligned bounding box (`Rect`) from a collection of 3D points. This is useful for spatial indexing and queries. ```julia using GeometryBasics pts = Point3f[(rand(3)...,) for _ in 1:50] bb = Rect(pts) # tight axis-aligned bounding box ``` -------------------------------- ### Connecting Points and Creating Tuple Views Source: https://context7.com/juliageometry/geometrybasics.jl/llms.txt Introduces `connect` for grouping raw arrays into polytopes and `TupleView` for creating N-tuples from arrays without copying data. These are useful for efficient data handling and view creation. ```julia using GeometryBasics ``` -------------------------------- ### Creating and Using MetaMesh for Metadata Source: https://context7.com/juliageometry/geometrybasics.jl/llms.txt Shows how to wrap a `Mesh` in a `MetaMesh` to add non-vertex metadata, such as material properties or IDs. Demonstrates accessing metadata and the inner mesh. ```julia using GeometryBasics m = normal_mesh(Sphere(Point3f(0), 1f0)) mm = MetaMesh(m; material = :glossy, id = 42) @show mm[:material] # :glossy @show mm[:id] # 42 mm[:material] = :matte @show keys(mm) # [:material, :id] # Vertex attributes accessible the same way @show length(mm.position) # Retrieve the inner Mesh inner = Mesh(mm) # Build directly from positions + faces mm2 = MetaMesh(coordinates(m), faces(m); source = "generated") ``` -------------------------------- ### Create Line Segments from Points Source: https://github.com/juliageometry/geometrybasics.jl/blob/master/docs/src/index.md Shows how to define Line objects by connecting pairs of Point objects. Requires previously defined points. ```julia l1 = Line(p1, p2) l2 = Line(p2, p3) ``` -------------------------------- ### Tessellating a Rectangle Source: https://github.com/juliageometry/geometrybasics.jl/blob/master/docs/src/primitives.md Demonstrates creating a tessellated 2D Rectangle primitive, specifying the number of vertices along its x and y dimensions. ```julia using GeometryBasics t = Tessellation(Rect2(Point2f(0), Vec2f(1)), (8, 6)) # 8 vertices in x direction by 6 in y direction triangle_mesh(t) ``` -------------------------------- ### Pyramid and Cone Construction Source: https://context7.com/juliageometry/geometrybasics.jl/llms.txt Shows the construction of a Pyramid with a square base and a Cone, defined by their geometric parameters. ```julia using GeometryBasics pyr = Pyramid(Point3f(0), 2.0f0, 1.0f0) # center, height, width cone = Cone(Point3f(0, 0, 0), Point3f(0, 0, 3), 0.5f0) # base center, tip, radius ``` -------------------------------- ### Connecting Coordinates to Points Source: https://context7.com/juliageometry/geometrybasics.jl/llms.txt Demonstrates connecting a flat array of floating-point coordinates into a sequence of 2D points. This is a common way to represent point clouds or vertex data. ```julia coords = Float32[0,0, 1,0, 1,1, 0,1] pts = connect(coords, Point2f) # [Point2f(0,0), Point2f(1,0), Point2f(1,1), Point2f(0,1)] ``` -------------------------------- ### Creating HyperRectangles Source: https://github.com/juliageometry/geometrybasics.jl/blob/master/docs/src/primitives.md Demonstrates the creation of multi-dimensional axis-aligned hyperrectangles using different dimension and element type shorthands. ```julia using GeometryBasics r1 = HyperRectangle{4, Float64}(Point{4, Float64}(0), Vec{4, Float64}(1)) r2 = Rect3f(Point3f(-1), Vec3f(2)) r3 = Rect2i(0, 0, 1, 1) ``` -------------------------------- ### Pyramid and Cone Mesh and Bounding Box Source: https://context7.com/juliageometry/geometrybasics.jl/llms.txt Generates meshes with normals for a Pyramid and a Cone, and calculates the bounding box for a Pyramid. ```julia m_pyr = normal_mesh(pyr) m_cone = normal_mesh(Tessellation(cone, 32)) # Bounding box bb_pyr = Rect(pyr) ``` -------------------------------- ### Rect/HyperRectangle Construction and Properties Source: https://context7.com/juliageometry/geometrybasics.jl/llms.txt Shows how to construct and query properties of N-dimensional axis-aligned bounding boxes (Rect/HyperRectangle). Supports 2D and 3D with different data types. ```julia using GeometryBasics # Construction r1 = Rect2f(0, 0, 1, 1) # origin=(0,0), widths=(1,1) r2 = Rect3f(Point3f(-1), Vec3f(2)) r3 = Rect2i(0, 0, 10, 5) # integer rect # Properties @show origin(r1) # Vec2f(0.0, 0.0) @show widths(r1) # Vec2f(1.0, 1.0) @show minimum(r1) # Vec2f(0.0, 0.0) @show maximum(r1) # Vec2f(1.0, 1.0) @show area(r1) # 1.0 @show volume(r2) # 8.0 ``` -------------------------------- ### Create Rectangle with GeometryBasics.jl Source: https://github.com/juliageometry/geometrybasics.jl/blob/master/docs/src/index.md Shows how to create a Rect object, defining its origin and dimensions using Vec objects. The rectangle is placed at the origin with unit width and height. ```julia rect = Rect(Vec(0.0, 0.0), Vec(1.0, 1.0)) ``` -------------------------------- ### LineString and MultiLineString Operations Source: https://context7.com/juliageometry/geometrybasics.jl/llms.txt Demonstrates the creation and usage of LineString and MultiLineString types for representing ordered sequences of points and collections of line strings. Includes segment intersection checks. ```julia using GeometryBasics pts = Point2f[(0,0),(1,1),(2,0),(3,1)] ls = LineString(pts) # Segment intersection between two 2D lines a = Line(Point2f(0,0), Point2f(1,1)) b = Line(Point2f(0,1), Point2f(1,0)) found, pt = intersects(a, b) @assert found println("Intersection at: ", pt) # Point2f(0.5, 0.5) # Build multi-linestring mls = MultiLineString([LineString(pts), LineString(Point2f[(5,5),(6,6)])]) @show length(mls) # 2 ``` -------------------------------- ### Defining Geometric Shapes with Polytope Types Source: https://context7.com/juliageometry/geometrybasics.jl/llms.txt Illustrates the creation and usage of `Line`, `Triangle`, and `Tetrahedron` for representing geometric shapes. Demonstrates accessing coordinates, calculating area, performing point-in-triangle tests, and computing volume. ```julia using GeometryBasics p1 = Point(0.0, 0.0) p2 = Point(1.0, 0.0) p3 = Point(0.5, 1.0) seg = Line(p1, p2) # Line{2, Float64} tri = Triangle(p1, p2, p3) # Ngon{2, Float64, 3} tet = Tetrahedron(Point3f(0), Point3f(1,0,0), Point3f(0,1,0), Point3f(0,0,1)) # Access vertices coords = coordinates(tri) # NTuple of Points @assert tri[1] == p1 # Area of a 2D polygon / face pts = Point2f[(0,0),(1,0),(1,1),(0,1)] a = area(pts) # 1.0 # Point-in-triangle test inside = Point2f(0.5, 0.4) in tri # true # Volume of 3D mesh m = normal_mesh(Sphere(Point3f(0), 1f0)) vol = volume(m) ``` -------------------------------- ### Tessellation for Mesh Resolution Control Source: https://context7.com/juliageometry/geometrybasics.jl/llms.txt Demonstrates how to use Tessellation to control the resolution of generated meshes for various geometric primitives like spheres, rectangles, and cylinders. ```julia using GeometryBasics sphere = Sphere(Point3f(0), 1f0) # Scalar tessellation — same resolution in all directions t1 = Tessellation(sphere, 32) m1 = normal_mesh(t1) @show length(coordinates(m1)) # ~32*32 # Tuple tessellation for grid-based primitives (width × height) rect = Rect2f(0, 0, 1, 1) t2 = Tessellation(rect, (10, 8)) # 10 x-vertices, 8 y-vertices m2 = triangle_mesh(t2) @show length(faces(m2)) # 2*(10-1)*(8-1) = 126 # Cylinder with denser rings t3 = Tessellation(Cylinder(Point3f(0), Point3f(0,0,1), 0.3f0), 64) m3 = normal_mesh(t3) ``` -------------------------------- ### Connecting Indices to Faces Source: https://context7.com/juliageometry/geometrybasics.jl/llms.txt Shows how to connect raw integer indices to form geometric faces, specifically TriangleFaces. This is useful for defining mesh connectivity. ```julia indices = [1,2,3, 1,3,4] tri_fs = connect(indices, TriangleFace{Int}) # [TriangleFace(1,2,3), TriangleFace(1,3,4)] ``` -------------------------------- ### Splitting Rectangles Source: https://context7.com/juliageometry/geometrybasics.jl/llms.txt Shows how to split a rectangle into two smaller rectangles along a specified axis at a given position. ```julia # Split along axis left, right = split(r1, 1, 0.5) # split on x-axis at 0.5 ``` -------------------------------- ### Centered Unit Sphere and Mesh Generation Source: https://context7.com/juliageometry/geometrybasics.jl/llms.txt Creates a centered unit sphere and generates triangle meshes from spheres at different resolutions, including options for UVs and normals. ```julia # Centered unit sphere unit_s = centered(Sphere{Float32}) # Mesh generation at different resolutions m_low = triangle_mesh(Tessellation(s, 12)) # 12 vertices per ring m_high = uv_normal_mesh(Tessellation(s, 64)) # 64 — dense, with UVs + normals @show length(faces(m_low)), length(faces(m_high)) ``` -------------------------------- ### Allen Interval Algebra Predicates Source: https://context7.com/juliageometry/geometrybasics.jl/llms.txt Illustrates the use of Allen interval algebra predicates like `before` and `overlaps` for 2D rectangles. These are useful for temporal or spatial reasoning. ```julia # Allen interval algebra predicates r1 = Rect2f(0, 0, 1, 1) r2 = Rect2f(2, 0, 1, 1) @assert before(r1, r2) r3 = Rect2f(0.5, 0, 1, 1) @assert overlaps(r1, r3) @assert !during(r1, r3) ``` -------------------------------- ### Connecting Points to Line Segments Source: https://context7.com/juliageometry/geometrybasics.jl/llms.txt Illustrates connecting a list of 2D points into a sequence of line segments. This is useful for defining polylines or paths. ```julia pts2 = Point2f[(0,0),(1,0),(1,1),(0,1)] lines = connect(pts2, Line) # [Line(Point2f(0,0),Point2f(1,0)), Line(Point2f(1,0),Point2f(1,1)), ...] ``` -------------------------------- ### Implement Parallelepiped Texture Coordinates Source: https://github.com/juliageometry/geometrybasics.jl/blob/master/docs/src/primitives.md Generates texture coordinates for a Parallelepiped by partitioning a 2D image into 2x3 rectangular sections based on normal directions. Uses FaceView to map coordinates to faces. ```julia function GeometryBasics.texturecoordinates(::Parallelepiped{T}) where {T} uvs = [Vec2f(x, y) for x in range(0, 1, length=4) for y in range(0, 1, 3)] fs = QuadFace{Int}[ (1, 2, 5, 4), (2, 3, 6, 5), (4, 5, 8, 7), (5, 6, 9, 8), (7, 8, 11, 10), (8, 9, 12, 11) ] return FaceView(uvs, fs) end ``` -------------------------------- ### Decomposing Geometry Data Source: https://context7.com/juliageometry/geometrybasics.jl/llms.txt Explains how to use the `decompose` function to extract and convert geometry data like positions, faces, normals, and UVs from various geometric primitives. Shows type conversions and polygon triangulation. ```julia using GeometryBasics rect = Rect2f(0, 0, 1, 1) s = Sphere(Point3f(0), 1f0) # Positions positions_f32 = decompose(Point2f, rect) positions_f64 = decompose(Point{2, Float64}, rect) # Faces tris = decompose(TriangleFace{Int}, rect) # [TriangleFace(1,2,4), TriangleFace(2,3,4)] # Normals and UVs ns = decompose_normals(Vec3f, s) uvs = decompose_uv(Vec2f, s) uvws = decompose_uvw(Vec3f, s) # Face type conversion quad_faces = QuadFace{Int}[(1,2,3,4),(5,6,7,8)] tri_faces = decompose(TriangleFace{Int}, quad_faces) # each quad → 2 triangles # Decompose a polygon into triangle indices poly = Polygon(Point2f[(0,0),(2,0),(2,1),(1,2),(0,1)]) tri_fs = faces(poly) # earcut triangulation → Vector{GLTriangleFace} ``` -------------------------------- ### HyperSphere Properties and Containment Source: https://context7.com/juliageometry/geometrybasics.jl/llms.txt Queries properties like radius, origin, and dimensions for spheres, and checks for point containment within a sphere. ```julia @show radius(s) # 0.5 @show origin(s) # Point3f(0.0, 0.0, 1.0) @show widths(c) # Vec2f(2.0, 2.0) # Containment @assert Point3f(0, 0, 1) in s ``` -------------------------------- ### Rect/HyperRectangle Containment and Set Operations Source: https://context7.com/juliageometry/geometrybasics.jl/llms.txt Demonstrates containment tests for points and other rectangles, as well as set operations like union and intersection for Rect/HyperRectangle types. ```julia # Containment @assert Point2f(0.5, 0.5) in r1 @assert r1 in Rect2f(-1, -1, 3, 3) # Set operations u = union(r1, Rect2f(0.5, 0.5, 1, 1)) i = intersect(r1, Rect2f(0.5, 0.5, 1, 1)) ``` -------------------------------- ### Implement Custom Bounding Box Method Source: https://github.com/juliageometry/geometrybasics.jl/blob/master/docs/src/boundingboxes.md Extend GeometryBasics by implementing `Rect{N, T}(geom)` for specialized bounding box generation. The `GeometryBasics.bbox_dim_check(user_dim, geom_dim)` function can be reused to ensure dimension compatibility. ```julia function Rect{N, T}(a::HyperSphere{N2}) where {N, N2, T} GeometryBasics.bbox_dim_check(N, N2) return Rect{N, T}(minimum(a), widths(a)) end ``` -------------------------------- ### Cylinder Mesh Generation Source: https://context7.com/juliageometry/geometrybasics.jl/llms.txt Generates simple and detailed triangle meshes for a cylinder, including options for normals and tessellation density. ```julia # Basic mesh m_simple = triangle_mesh(cyl) # Dense mesh with normals m_normal = normal_mesh(Tessellation(cyl, 48)) @show length(coordinates(m_normal)) ``` -------------------------------- ### Create Mesh Directly from Geometry Source: https://github.com/juliageometry/geometrybasics.jl/blob/master/docs/src/index.md Utilizes the 'GeometryBasics.mesh' function to generate a Mesh object directly from a geometry primitive like a Rect. This is a convenient shortcut. ```julia mesh = GeometryBasics.mesh(rect) ``` -------------------------------- ### Cylinder Construction and Properties Source: https://context7.com/juliageometry/geometrybasics.jl/llms.txt Defines a 3D cylinder by its base origin, extremity point, and radius, and retrieves its properties like height, radius, and direction. ```julia using GeometryBasics cyl = Cylinder(Point3f(0, 0, 0), Point3f(0, 0, 2), 0.5f0) @show height(cyl) # 2.0 @show radius(cyl) # 0.5 @show direction(cyl) # Vec3f(0.0, 0.0, 1.0) ``` -------------------------------- ### Combine Positions and Faces into a Mesh Source: https://github.com/juliageometry/geometrybasics.jl/blob/master/docs/src/index.md Constructs a Mesh object by combining a list of positions (vertices) and a list of faces (connectivity). Assumes 'rect_positions' and 'rect_faces' are defined. ```julia mesh = Mesh(rect_positions, rect_faces) ``` -------------------------------- ### Generate Bounding Boxes for Geometries Source: https://github.com/juliageometry/geometrybasics.jl/blob/master/docs/src/boundingboxes.md Use `Rect(geom)` to generate an axis-aligned bounding box for any `AbstractGeometry`. This can rely on `coordinates(geom)` or specialized methods. You can also specify the dimension and type of the bounding box. ```julia using GeometryBasics s = Circle(Point2f(0), 1f0) Rect(s) # specialized, exact bounding box Rect3(s) Rect3d(s) RectT{Float64}(s) Rect(GeometryBasics.mesh(s)) # using generated coordinates in mesh ``` -------------------------------- ### Implement Parallelepiped Normals Source: https://github.com/juliageometry/geometrybasics.jl/blob/master/docs/src/primitives.md Calculates and returns face normals for a Parallelepiped. Uses cross products of the defining vectors and FaceView to associate normals with faces. ```julia using LinearAlgebra function GeometryBasics.normals(primitive::Parallelepiped) n1 = normalize(cross(primitive.v2, primitive.v3)) n2 = normalize(cross(primitive.v3, primitive.v1)) n3 = normalize(cross(primitive.v1, primitive.v2)) ns = [-n3, n3, -n2, n2, -n1, n1] fs = QuadFace{Int}[1, 2, 3, 4, 5, 6] # = [QuadFace{Int}(1), QuadFace{Int}(2), ...] return FaceView(ns, fs) end ``` -------------------------------- ### Implement Parallelepiped Faces Source: https://github.com/juliageometry/geometrybasics.jl/blob/master/docs/src/primitives.md Defines the faces of a Parallelepiped using QuadFace, ensuring counter-clockwise winding for front-facing polygons. This is crucial for normal generation and backface culling. ```julia function GeometryBasics.faces(::Parallelepiped) return QuadFace{Int}[ (1, 2, 3, 4), (5, 8, 7, 6), # facing -n3, +n3 (n3 being the normal of v1 x v2) (1, 5, 6, 2), (4, 3, 7, 8), # facing -n2, +n2 (2, 6, 7, 3), (1, 4, 8, 5), # facing -n1, +n1 ] end ``` -------------------------------- ### Implement Parallelepiped Coordinates Source: https://github.com/juliageometry/geometrybasics.jl/blob/master/docs/src/primitives.md Calculates the 8 unique corner coordinates of a Parallelepiped. Ensure correct winding order for faces. ```julia function GeometryBasics.coordinates(primitive::Parallelepiped{T}) where {T} o = primitive.origin v1 = primitive.v1; v2 = primitive.v2; v3 = primitive.v3 return Point{3, T}[o, o+v2, o+v1+v2, o+v1, o+v3, o+v2+v3, o+v1+v2+v3, o+v1+v3] end ``` -------------------------------- ### Decompose Rectangle into Positions Source: https://github.com/juliageometry/geometrybasics.jl/blob/master/docs/src/index.md Demonstrates decomposing a Rect object into Point objects representing its positions. Specify the desired point type, e.g., Point{2, Float64}. ```julia rect_positions = decompose(Point{2, Float64}, rect) ``` -------------------------------- ### Distance Between Bounding Boxes Source: https://context7.com/juliageometry/geometrybasics.jl/llms.txt Calculates the minimum Euclidean distance between two 3D bounding boxes. This is useful for collision detection or spatial proximity analysis. ```julia # Two-rect distances bb2 = Rect3f(Point3f(10), Vec3f(2)) d_rects = min_euclidean(bb, bb2) ``` -------------------------------- ### Custom Parallelepiped Geometry Primitive Source: https://context7.com/juliageometry/geometrybasics.jl/llms.txt Defines a custom geometry primitive `Parallelepiped` and implements the `coordinates`, `faces`, and `normals` functions required by GeometryBasics.jl for mesh generation. ```julia using GeometryBasics, LinearAlgebra # Custom geometry primitive struct Parallelepiped{T} <: GeometryPrimitive{3, T} origin::Point{3, T} v1::Vec{3, T}; v2::Vec{3, T}; v3::Vec{3, T} end function GeometryBasics.coordinates(p::Parallelepiped{T}) where {T} o = p.origin; v1 = p.v1; v2 = p.v2; v3 = p.v3 return Point{3,T}[o, o+v2, o+v1+v2, o+v1, o+v3, o+v2+v3, o+v1+v2+v3, o+v1+v3] end function GeometryBasics.faces(::Parallelepiped) return QuadFace{Int}[(1,2,3,4),(5,8,7,6),(1,5,6,2),(4,3,7,8),(2,6,7,3),(1,4,8,5)] end function GeometryBasics.normals(p::Parallelepiped) n1 = normalize(cross(p.v2, p.v3)) n2 = normalize(cross(p.v3, p.v1)) n3 = normalize(cross(p.v1, p.v2)) ns = [-n3, n3, -n2, n2, -n1, n1] return FaceView(ns, QuadFace{Int}[1,2,3,4,5,6]) end ``` -------------------------------- ### Detect and Split Self-Intersections in Polygons Source: https://context7.com/juliageometry/geometrybasics.jl/llms.txt Detects self-intersections in a closed polygon and splits it into simple polygons. Requires a closed polygon represented by a sequence of points. ```julia pts = Point2f[(0,0),(1,1),(1,0),(0,1),(0,0)] # figure-eight idx, pts_i = self_intersections(pts) println("Intersections at: ", pts_i) # Split self-intersecting polygon into two simple polygons parts = split_intersections(pts) @assert length(parts) == 2 ``` -------------------------------- ### Define Parallelepiped Struct Source: https://github.com/juliageometry/geometrybasics.jl/blob/master/docs/src/primitives.md Defines a Parallelepiped struct in 3D space, requiring an origin point and three vectors defining its extent. ```julia struct Parallelepiped{T} <: GeometryPrimitive{3, T} origin::Point{3, T} v1::Vec{3, T} v2::Vec{3, T} v3::Vec{3, T} end ``` -------------------------------- ### Generating 2D Circle Coordinates Source: https://context7.com/juliageometry/geometrybasics.jl/llms.txt Extracts a specified number of points lying on the circumference of a 2D circle. ```julia # 2D circle coordinates pts = coordinates(c, 32) # 32 points on the circle ``` -------------------------------- ### Decompose Rectangle into Triangle Faces Source: https://github.com/juliageometry/geometrybasics.jl/blob/master/docs/src/index.md Uses the 'decompose' function to break down a Rect object into TriangleFace elements. Specify the desired element type, e.g., TriangleFace{Int}. ```julia rect_faces = decompose(TriangleFace{Int}, rect) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.