Skip to content

Commit fabc9a9

Browse files
authored
Merge pull request #73 from JuliaGeometry/sjk/gb2
cleanup GB API and Makie viz by defaulting to GL primitives
2 parents fbb9d3e + a127ea9 commit fabc9a9

File tree

3 files changed

+33
-60
lines changed

3 files changed

+33
-60
lines changed

docs/src/examples.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ algo = MarchingCubes(iso=100, insidepositive=true)
2424
# algo = NaiveSurfaceNets(iso=100, insidepositive=true)
2525

2626
# generate the mesh using marching cubes
27-
mc = HomogenousMesh{Point{3,Float32},TriangleFace{Int}}(ctacardio, algo)
27+
mc = Mesh(ctacardio, algo)
2828

2929
# we can call isosurface to get a vector of points and vector of faces indexing to the points
3030
# vertices, faces = isosurface(ctacardio, algo, Point{3,Float32}, TriangleFace{Int})
@@ -45,16 +45,17 @@ using GeometryBasics
4545
gyroid(v) = cos(v[1])*sin(v[2])+cos(v[2])*sin(v[3])+cos(v[3])*sin(v[1])
4646
gyroid_shell(v) = max(gyroid(v)-0.4,-gyroid(v)-0.4)
4747

48-
# generate directly using GeometryBasics API, normals are computed by GeometryBasics
49-
gy_mesh = GLNormalMesh(gyroid_shell, Rect(Vec(0,0,0),Vec(pi*4,pi*4,pi*4)),
48+
# generate directly using GeometryBasics API
49+
# Rect specifies the sampling intervals
50+
gy_mesh = Mesh(gyroid_shell, Rect(Vec(0,0,0),Vec(pi*4,pi*4,pi*4)),
5051
MarchingCubes(), samples=(50,50,50))
5152

5253
save("gyroid.ply", gy_mesh)
5354

5455
# view with Makie
5556
import Makie
5657
using LinearAlgebra
57-
Makie.mesh(gy_mesh, color=[norm(v) for v in gy_mesh.vertices])
58+
Makie.mesh(gy_mesh, color=[norm(v) for v in coordinates(gy_mesh)])
5859
```
5960

6061
![gyroid](./img/gyroid.png)

src/geometrybasics_api.jl

Lines changed: 27 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,39 @@
1+
function GB.Mesh(df::GT.SignedDistanceField{3,ST,FT},
2+
method::AbstractMeshingAlgorithm;
3+
pointtype=GB.Point{3,Float32},
4+
facetype=GB.GLTriangleFace) where {ST, FT}
15

2-
"""
3-
_determine_types_gb(meshtype, fieldtype=Float64, facelen=3)
4-
Given a subtype of AbstractMesh, determine the
5-
type of vertex/point and face to use for internal computations.
6-
Preference is given to the types specified by the Mesh call,
7-
and will default to the `FieldType` for `SignedDistanceField`,
8-
and Point{3,Float64}/TriangleFace{Int} for direct function sampling.
9-
"""
10-
function _determine_types_gb(pointtype, facetype, fieldtype=Float64, facelen=3)
11-
# determine the point and face types
12-
# preference is given to the Mesh types
13-
# followed by SDF if unspecified
14-
VertType = if pointtype isa Nothing
15-
GB.Point{3, fieldtype}
16-
else
17-
pointtype
18-
end
19-
20-
FaceType = if facetype isa Nothing
21-
GB.NgonFace{facelen, Int}
22-
else
23-
facetype
24-
end
25-
26-
return VertType, FaceType
27-
end
28-
29-
function mesh(df::GT.SignedDistanceField{3,ST,FT}, method::AbstractMeshingAlgorithm;
30-
pointtype=nothing, facetype=nothing, ) where {ST, FT}
31-
32-
vertex_eltype = promote_type(FT, typeof(method.iso), typeof(method.eps))
33-
VertType, FaceType = _determine_types_gb(pointtype, facetype, vertex_eltype,
34-
GB.default_face_length(method))
356
h = df.bounds
36-
vts, fcs = isosurface(df.data, method, VertType, FaceType, origin=VertType(GB.origin(h)), widths=VertType(GB.widths(h)))
7+
vts, fcs = isosurface(df.data, method, pointtype, facetype, origin=pointtype(GB.origin(h)), widths=pointtype(GB.widths(h)))
378
return GB.Mesh(vts, fcs)
389
end
3910

40-
function GB.mesh(f::Function, h::GB.Rect, samples::NTuple{3,T}, method::AbstractMeshingAlgorithm;
41-
pointtype=nothing, facetype=nothing) where {T <: Integer}
42-
vertex_eltype = promote_type(T, typeof(method.iso), typeof(method.eps))
43-
VertType, FaceType = _determine_types_gb(pointtype, facetype, vertex_eltype,
44-
default_face_length(method))
45-
vts, fcs = isosurface(f, method, VertType, FaceType; samples=samples,
46-
origin=VertType(GB.origin(h)), widths=VertType(GB.widths(h)))
11+
function GB.Mesh(f::Function,
12+
h::GB.Rect,
13+
samples::NTuple{3,T},
14+
method::AbstractMeshingAlgorithm;
15+
pointtype=GB.Point{3,Float32},
16+
facetype=GB.GLTriangleFace) where {T <: Integer}
17+
vts, fcs = isosurface(f, method, pointtype, facetype; samples=samples,
18+
origin=pointtype(GB.origin(h)), widths=pointtype(GB.widths(h)))
19+
4720
return GB.Mesh(vts, fcs)
4821
end
4922

50-
function GB.mesh(f::Function, h::GB.Rect, method::AbstractMeshingAlgorithm;
51-
samples::NTuple{3,T}=_DEFAULT_SAMPLES, pointtype=nothing,
52-
facetype=nothing) where {T <: Integer}
53-
return mesh(f, h, samples, method; pointtype=pointtype, facetype=facetype)
23+
function GB.Mesh(f::Function, h::GB.Rect, method::AbstractMeshingAlgorithm;
24+
samples::NTuple{3,T}=_DEFAULT_SAMPLES,
25+
pointtype=GB.Point{3,Float32},
26+
facetype=GB.GLTriangleFace) where {T <: Integer}
27+
28+
return GB.Mesh(f, h, samples, method; pointtype=pointtype, facetype=facetype)
5429
end
5530

56-
function GB.mesh(volume::AbstractArray{T, 3}, method::AbstractMeshingAlgorithm; pointtype=nothing, facetype=nothing, kwargs...) where {T}
57-
vertex_eltype = promote_type(T, typeof(method.iso), typeof(method.eps))
58-
VertType, FaceType = _determine_types_gb(pointtype, facetype, vertex_eltype,
59-
default_face_length(method))
60-
vts, fcs = isosurface(volume, method, VertType, FaceType; kwargs...)
31+
function GB.Mesh(volume::AbstractArray{T, 3},
32+
method::AbstractMeshingAlgorithm;
33+
pointtype=GB.Point{3,Float32},
34+
facetype=GB.GLTriangleFace,
35+
kwargs...) where {T}
36+
37+
vts, fcs = isosurface(volume, method, pointtype, facetype; kwargs...)
6138
return GB.Mesh(vts, fcs)
6239
end

test/runtests.jl

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,6 @@ end
3030
@test dt(HomogenousMesh{Point{3, Float64}, Face{3, UInt32}}) == (Point{3,Float64}, Face{3,UInt32})
3131
@test dt(HomogenousMesh{Point{3, Float32}, Face{3, UInt32}}, Float64) == (Point{3,Float32}, Face{3,UInt32})
3232
@test dt(HomogenousMesh, Float64, 4) == (Point{3,Float64}, Face{4,Int64})
33-
dt = Meshing._determine_types_gb
34-
@test dt(GB.Point3{Float64}, GB.TriangleFace{Int}) == (GB.Point{3,Float64}, GB.TriangleFace{Int64})
35-
@test dt(GB.Point3f0, GB.GLTriangleFace) == (GB.Point3f0, GB.GLTriangleFace)
36-
@test dt(nothing, nothing, Float16) == (GB.Point3{Float16}, GB.TriangleFace{Int64})
37-
@test dt(nothing, nothing, Float64, 4) == (GB.Point{3,Float64}, GB.QuadFace{Int64})
3833
end
3934

4035
@testset "surface nets" begin
@@ -354,7 +349,7 @@ end
354349
@testset "GeometryBasics API" begin
355350
@testset "vararg passing" begin
356351
A = rand(20,20,20)
357-
m = GB.mesh(A,MarchingTetrahedra(1.0),origin=Point(Float32(0),Float32(0),Float32(0)),widths=Point(Float32(1),Float32(1),Float32(1)))
352+
m = GB.Mesh(A,MarchingTetrahedra(1.0),origin=Point(Float32(0),Float32(0),Float32(0)),widths=Point(Float32(1),Float32(1),Float32(1)))
358353
@test m isa GB.Mesh
359354
end
360355
end

0 commit comments

Comments
 (0)