Skip to content

Commit

Permalink
Merge branch 'master' into bcourter/warp-fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
bcourter committed Jul 8, 2024
2 parents 0428a0b + 84fba98 commit a313548
Show file tree
Hide file tree
Showing 15 changed files with 126 additions and 105 deletions.
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"

[compat]
Aqua = "0.8"
CoordinateTransformations = "0.6"
Combinatorics = "1"
CoordinateTransformations = "0.6"
FileIO = "1"
ForwardDiff = "0.10"
GeometryBasics = "0.4"
LinearAlgebra = "1"
Meshing = "0.5"
MeshIO = "0.4"
Meshing = "0.7"
Revise = "3"
Rotations = "1"
StaticArrays = "1"
Expand Down
2 changes: 1 addition & 1 deletion docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ using Descartes
makedocs(
sitename = "Descartes",
format = Documenter.HTML(),
modules = [Descartes],
#modules = [Descartes],
authors = "Steve Kelly",
pages = ["Primitives" => "primitives.md",
"Design" => "design.md"]
Expand Down
45 changes: 5 additions & 40 deletions docs/src/design.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,25 @@

Descartes current uses implicit modeling to create solid models. This is a brief overview of the system internals.


## Implicit Geometry Pipeline

## API

## UX

The UX (API) is designed to be familiar to those who use OpenSCAD. Let's start with a simple example to show the similarities.
The API is designed to be familiar to those who use OpenSCAD. Let's start with a simple example to show the similarities.

This can all be executed in the Julia REPL, so follow along:

```julia
using Descartes
using GeometryBasics

c = Cuboid([5,5,5])

h = translate([2.5,2.5,0])Cylinder(1,5)

obj = diff(c,h)

m = HomogenousMesh(obj)
m = GeometryBasics.Mesh(obj)

save("cube_with_hole.stl", m)
```
Expand Down Expand Up @@ -53,7 +52,7 @@ obj = diff(c,h)
Here we difference the cylinder from the cube. The corresponding set operations are `union` and `intersect`.

```julia
m = HomogenousMesh(obj)
m = GeometryBasics.Mesh(obj)
```

At this point we convert our object to a mesh. Prior to this point the model is simply a data structure. In the next section we will discuss the process of meshign more in-depth.
Expand All @@ -64,48 +63,14 @@ save("cube_with_hole.stl", m)

Finally we save the mesh as an STL. Other formats such as OBJ, PLY, and OFF are also supported.


## Meshing

In the prior section we mentioned that the "model" we create is just a data structure until we mesh it. So let's explore what happens in the meshing process.

The code to convert our data structure to a mesh is actually fairly short:

[Current version](https://github.com/sjkelly/Descartes.jl/blob/master/src/meshes.jl)


```julia
function (::Type{MT})(primitives::AbstractPrimitive{3, T}...;
samples=(128,128,128),
algorithm=MarchingCubes()
) where {T, MT <: AbstractMesh}

f(x) = FRep(primitives[1], x)
mesh = MT(f, HyperRectangle(primitives[1]), samples, algorithm)

for i = 2:length(primitives)
b = HyperRectangle(primitives[i])
lm = MT(x -> FRep(primitives[i], x), b, samples, algorithm)
mesh = merge(mesh, lm)
end

return mesh
end
```

The first two lines here are where the meshing happens:

```julia
f(x) = FRep(primitives[1], x)
mesh = MT(f, HyperRectangle(primitives[1]), samples, algorithm)
```

Any primitve or operation must implement two core operations; `FRep` and `HyperRectangle`.

`FRep` is the functional representation (implicit representation) of the model. The first argument to `FRep` is always the primitive. The second is a generic `AbstractVector`. In julia we have type inference, so we do not need to annotate, so this will always be fast. FRep by convention must use 0 as the surface of the model, positive outside the model, and negative inside.

`HyperRectangle` should return the extents of the primitive. The underlying API will handle transformations and basic set operations for us.

In the next line we actually perform the meshing operation. We call the meshtype with the implicit function we created, `f`, in the bounds generated by `HyperRectangle`, uniformly sample the space by `samples`, and actually generate the triangular meshing using `algorithm`. In this case, the defaults are `samples=(128,128,128)` and `algorithm=MarchingCubes()`.

In the loop `for i = 2:length(primitives) ... end` we handle additional primitives passed to our meshing function so you can create a single mesh output from several different objects. In order to maintain performance and resolution these are not `union`ed.
70 changes: 34 additions & 36 deletions examples/Manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

julia_version = "1.10.4"
manifest_format = "2.0"
project_hash = "45f0ef80ca12ef806f32247cd0d5651dd69dc4de"
project_hash = "5711fc8d77eed703586d928736ce229a69d0c5a2"

[[deps.ADTypes]]
git-tree-sha1 = "fa0822e5baee6e23081c2685ae27265dabee23d8"
git-tree-sha1 = "ae44a0c3d68a420d4ac0fa1f7e0c034ccecb6dc5"
uuid = "47edcb42-4c32-4615-8424-f2b9edc5f35b"
version = "1.4.0"
version = "1.5.2"

[deps.ADTypes.extensions]
ADTypesChainRulesCoreExt = "ChainRulesCore"
Expand Down Expand Up @@ -139,14 +139,14 @@ uuid = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
version = "1.5.0"

[[deps.Bijections]]
git-tree-sha1 = "c9b163bd832e023571e86d0b90d9de92a9879088"
git-tree-sha1 = "95f5c7e2d177b7ba1a240b0518038b975d72a8c0"
uuid = "e2ed5e7c-b2de-5872-ae92-c73ca462fb04"
version = "0.1.6"
version = "0.1.7"

[[deps.BitFlags]]
git-tree-sha1 = "2dc09997850d68179b69dafb58ae806167a32b1b"
git-tree-sha1 = "0691e34b3bb8be9307330f88d1a3c3f25466c24d"
uuid = "d1d4a3ce-64b1-5f1a-9ba4-7e7e69966f35"
version = "0.1.8"
version = "0.1.9"

[[deps.Bonito]]
deps = ["Base64", "CodecZlib", "Colors", "Dates", "Deno_jll", "HTTP", "Hyperscript", "LinearAlgebra", "Markdown", "MsgPack", "Observables", "RelocatableFolders", "SHA", "Sockets", "Tables", "ThreadPools", "URIs", "UUIDs", "WidgetsBase"]
Expand Down Expand Up @@ -258,6 +258,11 @@ git-tree-sha1 = "7b8a93dba8af7e3b42fecabf646260105ac373f7"
uuid = "bbf7d656-a473-5ed7-a52c-81e309532950"
version = "0.3.0"

[[deps.CommonWorldInvalidations]]
git-tree-sha1 = "ae52d1c52048455e85a387fbee9be553ec2b68d0"
uuid = "f70d9fcc-98c5-4d4a-abd7-e4cdeebd8ca8"
version = "1.0.0"

[[deps.Compat]]
deps = ["TOML", "UUIDs"]
git-tree-sha1 = "b1c55339b7c6c350ee89f2c1604299660525b248"
Expand Down Expand Up @@ -582,22 +587,16 @@ version = "0.1.6"

[[deps.GeoInterface]]
deps = ["Extents"]
git-tree-sha1 = "801aef8228f7f04972e596b09d4dba481807c913"
git-tree-sha1 = "9fff8990361d5127b770e3454488360443019bb3"
uuid = "cf35fbd7-0cd7-5166-be24-54bfbe79505f"
version = "1.3.4"
version = "1.3.5"

[[deps.GeometryBasics]]
deps = ["EarCut_jll", "Extents", "GeoInterface", "IterTools", "LinearAlgebra", "StaticArrays", "StructArrays", "Tables"]
git-tree-sha1 = "b62f2b2d76cee0d61a2ef2b3118cd2a3215d3134"
uuid = "5c1252a2-5f33-56bf-86c9-59e7332b4326"
version = "0.4.11"

[[deps.GeometryTypes]]
deps = ["ColorTypes", "FixedPointNumbers", "LinearAlgebra", "StaticArrays"]
git-tree-sha1 = "d796f7be0383b5416cd403420ce0af083b0f9b28"
uuid = "4d00f742-c7ba-57c2-abde-4428a4b178cb"
version = "0.8.5"

[[deps.Gettext_jll]]
deps = ["Artifacts", "CompilerSupportLibraries_jll", "JLLWrappers", "Libdl", "Libiconv_jll", "Pkg", "XML2_jll"]
git-tree-sha1 = "9b02998aba7bf074d14de89f9d37ca24a1a0b046"
Expand Down Expand Up @@ -1024,10 +1023,9 @@ uuid = "c8ffd9c3-330d-5841-b78e-0817d7145fa1"
version = "2.28.2+1"

[[deps.Meshing]]
deps = ["GeometryBasics", "GeometryTypes", "StaticArrays"]
git-tree-sha1 = "b32d34f3e3ca44391ca7261ca4eb96af71e022b9"
path = "/Users/sjkelly/.julia/dev/Meshing"
uuid = "e6723b4c-ebff-59f1-b4b7-d97aa5274f73"
version = "0.5.7"
version = "0.7.0"

[[deps.Missings]]
deps = ["DataAPI"]
Expand Down Expand Up @@ -1435,9 +1433,9 @@ version = "0.3.8"

[[deps.SciMLStructures]]
deps = ["ArrayInterface"]
git-tree-sha1 = "6ab4beaf88dcdd2639bead916f2347f81dcacd0e"
git-tree-sha1 = "cfdd1200d150df1d3c055cc72ee6850742e982d7"
uuid = "53ae85a6-f571-4167-b2af-e1d143709226"
version = "1.3.0"
version = "1.4.1"

[[deps.Scratch]]
deps = ["Dates"]
Expand Down Expand Up @@ -1525,9 +1523,9 @@ version = "0.1.1"

[[deps.StaticArrays]]
deps = ["LinearAlgebra", "PrecompileTools", "Random", "StaticArraysCore"]
git-tree-sha1 = "6e00379a24597be4ae1ee6b2d882e15392040132"
git-tree-sha1 = "20833c5b7f7edf0e5026f23db7f268e4f23ec577"
uuid = "90137ffa-7385-5640-81b9-e52037218182"
version = "1.9.5"
version = "1.9.6"
weakdeps = ["ChainRulesCore", "Statistics"]

[deps.StaticArrays.extensions]
Expand Down Expand Up @@ -1603,15 +1601,15 @@ version = "0.2.1"

[[deps.SymbolicUtils]]
deps = ["AbstractTrees", "Bijections", "ChainRulesCore", "Combinatorics", "ConstructionBase", "DataStructures", "DocStringExtensions", "DynamicPolynomials", "IfElse", "LabelledArrays", "LinearAlgebra", "MultivariatePolynomials", "NaNMath", "Setfield", "SparseArrays", "SpecialFunctions", "StaticArrays", "SymbolicIndexingInterface", "TermInterface", "TimerOutputs", "Unityper"]
git-tree-sha1 = "79813de27af70906d223fbd89ad90dba3d88a2b0"
git-tree-sha1 = "cc049913a449be8c7ac00979831f26891e01add2"
uuid = "d1185830-fcd6-423d-90d6-eec64667417b"
version = "2.0.2"
version = "2.1.0"

[[deps.Symbolics]]
deps = ["ADTypes", "ArrayInterface", "Bijections", "ConstructionBase", "DataStructures", "DiffRules", "Distributions", "DocStringExtensions", "DomainSets", "DynamicPolynomials", "ForwardDiff", "IfElse", "LaTeXStrings", "LambertW", "Latexify", "Libdl", "LinearAlgebra", "LogExpFunctions", "MacroTools", "Markdown", "NaNMath", "PrecompileTools", "RecipesBase", "Reexport", "Requires", "RuntimeGeneratedFunctions", "SciMLBase", "Setfield", "SparseArrays", "SpecialFunctions", "StaticArrays", "SymbolicIndexingInterface", "SymbolicLimits", "SymbolicUtils", "TermInterface"]
git-tree-sha1 = "3669f8eba2720c595e16b038f33cb880c12ef7f7"
deps = ["ADTypes", "ArrayInterface", "Bijections", "CommonWorldInvalidations", "ConstructionBase", "DataStructures", "DiffRules", "Distributions", "DocStringExtensions", "DomainSets", "DynamicPolynomials", "ForwardDiff", "IfElse", "LaTeXStrings", "LambertW", "Latexify", "Libdl", "LinearAlgebra", "LogExpFunctions", "MacroTools", "Markdown", "NaNMath", "PrecompileTools", "RecipesBase", "Reexport", "Requires", "RuntimeGeneratedFunctions", "SciMLBase", "Setfield", "SparseArrays", "SpecialFunctions", "StaticArraysCore", "SymbolicIndexingInterface", "SymbolicLimits", "SymbolicUtils", "TermInterface"]
git-tree-sha1 = "8539a734b448ca27707709c62420b1bf115ed7d9"
uuid = "0c5d862f-8b57-4792-8d23-62f2024744c7"
version = "5.30.3"
version = "5.33.0"

[deps.Symbolics.extensions]
SymbolicsGroebnerExt = "Groebner"
Expand Down Expand Up @@ -1681,9 +1679,9 @@ uuid = "a759f4b9-e2f1-59dc-863e-4aeb61b1ea8f"
version = "0.5.24"

[[deps.TranscodingStreams]]
git-tree-sha1 = "a947ea21087caba0a798c5e494d0bb78e3a1a3a0"
git-tree-sha1 = "d73336d81cafdc277ff45558bb7eaa2b04a8e472"
uuid = "3bb67fe8-82b1-5028-8e26-92a6c54297fa"
version = "0.10.9"
version = "0.10.10"
weakdeps = ["Random", "Test"]

[deps.TranscodingStreams.extensions]
Expand Down Expand Up @@ -1749,15 +1747,15 @@ version = "1.0.0"

[[deps.XML2_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Libiconv_jll", "Zlib_jll"]
git-tree-sha1 = "52ff2af32e591541550bd753c0da8b9bc92bb9d9"
git-tree-sha1 = "d9717ce3518dc68a99e6b96300813760d887a01d"
uuid = "02c8fc9c-b97f-50b9-bbe4-9be30ff0a78a"
version = "2.12.7+0"
version = "2.13.1+0"

[[deps.XSLT_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Libgcrypt_jll", "Libgpg_error_jll", "Libiconv_jll", "Pkg", "XML2_jll", "Zlib_jll"]
git-tree-sha1 = "91844873c4085240b95e795f692c4cec4d805f8a"
deps = ["Artifacts", "JLLWrappers", "Libdl", "Libgcrypt_jll", "Libgpg_error_jll", "Libiconv_jll", "XML2_jll", "Zlib_jll"]
git-tree-sha1 = "a54ee957f4c86b526460a720dbc882fa5edcbefc"
uuid = "aed1982a-8fda-507f-9586-7b0439959a61"
version = "1.1.34+0"
version = "1.1.41+0"

[[deps.Xorg_libX11_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Xorg_libxcb_jll", "Xorg_xtrans_jll"]
Expand Down Expand Up @@ -1797,9 +1795,9 @@ version = "0.1.1+0"

[[deps.Xorg_libxcb_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "XSLT_jll", "Xorg_libXau_jll", "Xorg_libXdmcp_jll", "Xorg_libpthread_stubs_jll"]
git-tree-sha1 = "b4bfde5d5b652e22b9c790ad00af08b6d042b97d"
git-tree-sha1 = "bcd466676fef0878338c61e655629fa7bbc69d8e"
uuid = "c7cfdc94-dc32-55de-ac96-5a1b8d977c5b"
version = "1.15.0+0"
version = "1.17.0+0"

[[deps.Xorg_xtrans_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl"]
Expand Down
1 change: 1 addition & 0 deletions examples/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
GeometryBasics = "5c1252a2-5f33-56bf-86c9-59e7332b4326"
HCubature = "19dc6840-f33b-545b-b366-655c7e3ffd49"
Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a"
Meshing = "e6723b4c-ebff-59f1-b4b7-d97aa5274f73"
QuadGK = "1fd47b50-473d-5c70-9696-f719f8f3bcdc"
Revise = "295af30f-e4ad-537b-8983-00126c2a3abe"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
Expand Down
12 changes: 12 additions & 0 deletions examples/gyroid.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Revise
using Descartes: Gyroid, Cuboid, PolarWarp, Cylinder, FRep
using GeometryBasics: GeometryBasics, mesh
using Meshing: MarchingTetrahedra

p = intersect(PolarWarp(Gyroid(0.5), 200), Cuboid([50,50,50]))

@time m = mesh(p; samples=(100,100,100), algorithm=MarchingTetrahedra());

using WGLMakie
WGLMakie.activate!(resize_to=:body)
WGLMakie.mesh(m)
4 changes: 2 additions & 2 deletions examples/piping.jl
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
using Descartes: Piping
using GeometryBasics: Mesh
using GeometryBasics: mesh

p = Piping(1.0, [[0,0,0],
[10,0,0],
[10,10,0],
[10,10,10],
[5,5,5]])

m = Mesh(p)
m = mesh(p)

using WGLMakie
mesh(m)
Expand Down
6 changes: 3 additions & 3 deletions examples/triangle_wave.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Revise
using Descartes: Descartes, TriangleWave, FRep, Grid, PolarWarp, Cuboid, union
using Descartes: Descartes, TriangleWave, FRep, PolarWarp, Cuboid, union
using WGLMakie
using CoordinateTransformations
using StaticArrays
Expand All @@ -19,7 +19,7 @@ y2 = [FRep(t2, e) for e in x]

lines(x,y2)

g = union(TriangleWave(1., 1), TriangleWave(2., 2))
g = union(TriangleWave(5., 1), TriangleWave(1., 2))

x_vals = -10:0.1:10
y_vals = -10:0.1:10
Expand All @@ -33,7 +33,7 @@ heatmap(x_vals, y_vals, z, alpha=0.6)
FRep(g, (x, y))

# Apply warp
g_polar = PolarWarp(g, 8)
g_polar = PolarWarp(g, 2)

#FRep(g_polar, (x, y))

Expand Down
4 changes: 3 additions & 1 deletion src/Descartes.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
module Descartes

import Base: *, union, diff, intersect
using GeometryBasics: GeometryBasics
import GeometryBasics: AbstractMesh,
HyperRectangle,
Vec,
Mesh
mesh


using StaticArrays,
Meshing,
Expand Down
10 changes: 8 additions & 2 deletions src/frep.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ function FRep(u::CSGIntersect, v)
f
end

_gyroid(v) = cos(v[1])*sin(v[2])+cos(v[2])*sin(v[3])+cos(v[3])*sin(v[1])

function FRep(p::Gyroid, v)
max(_gyroid(v)-p.width,-_gyroid(v)-p.width)
end

function FRep(p::Sphere,v)
norm(v) - p.radius
end
Expand Down Expand Up @@ -124,10 +130,10 @@ function FRep(p::PolarWarp, v)
end

function inner_warp(p::AbstractPrimitive, w, v)
T(a) = SVector(hypot(a...), atan(a[2], a[1])*w/2π) #cartesian to polar
T(a) = SVector(hypot(a[1], a[2]), atan(a[2], a[1])*w/2π, a[3]) #cartesian to polar
hr = gradient(a -> FRep(p, T(a)), SVector(v...))
mr, mt = T(v)
FRep(p, SVector(mr, mt))/norm(hr)
FRep(p, SVector(mr, mt, v[3]))/norm(hr)
end

function inner_warp(p::CSGUnion, w, v)
Expand Down
Loading

0 comments on commit a313548

Please sign in to comment.