Skip to content

Commit

Permalink
implement export VTK
Browse files Browse the repository at this point in the history
  • Loading branch information
HoBeZwe committed Nov 3, 2024
1 parent f8b3d2c commit 683812e
Show file tree
Hide file tree
Showing 13 changed files with 146 additions and 8 deletions.
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
Suppressor = "fd094767-a336-5f1f-9728-57cf17d0bbfb"
UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"
WriteVTK = "64499a7a-5c06-52f2-abe2-ccb03c286192"

[weakdeps]
PlotlyJS = "f0f68f2c-4968-5e81-91da-67840de0976a"
Expand Down
1 change: 1 addition & 0 deletions docs/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
PlotlyBase = "a03496cd-edff-5a9b-9e67-9cda94a718b5"
PlotlyKaleido = "f2990250-8cf9-495f-b13a-cce12b45703c"
WebIO = "0f1e0344-ec1d-5b48-a673-e5cf874b6c29"
WriteVTK = "64499a7a-5c06-52f2-abe2-ccb03c286192"
Binary file added docs/src/assets/paraview.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/src/assets/paraviewCol.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
48 changes: 47 additions & 1 deletion docs/src/fileio.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,50 @@ savefig(t, "sphere.html"); nothing # hide

```@raw html
<object data="../sphere.html" type="text/html" style="width:100%;height:50vh;"> </object>
```
```


## VTK Files (ParaView)

To display surfaces in [ParaView](https://www.paraview.org/) the function [`saveVtk`](@ref saveVtk) is provided.
It converts the surface to an unstructured grid and exports a .vtu file which can be opened by ParaView.

```@example vtk
using NURBS, FileIO
Patches = load("assets/torus.stp")
saveVtk("path/filename", Patches; resolution=0.01) # filename without extension
nothing # hide
```

```@raw html
<div align="center">
<img src="../assets/paraview.png" width="750"/>
</div>
<br/>
```



To add color information the function [`vtk`](@ref NURBS.vtk) can be used together with the [WriteVKT.jl](https://juliavtk.github.io/WriteVTK.jl/stable/) package.
To this end, the color values at the four corner points of each cell have to be provided in a Vector.

```@example vtk
using NURBS, FileIO, WriteVTK
Patches = load("assets/torus.stp")
cellV, x, y, z = NURBS.vtk(Patches, 0.01)
vtk_grid("/home/bernd/Dokumente/vtk/cells", x, y, z, cellV) do vtk
vtk["dataName"] = 2 * x + y
end
nothing # hide
```

```@raw html
<div align="center">
<img src="../assets/paraviewCol.png" width="750"/>
</div>
<br/>
```

2 changes: 1 addition & 1 deletion docs/src/meshes.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ nothing # hide
```

!!! tip
To determine all connectivity information, the [`Connectivity`](@ref Connectivity) function can be used, which returns a struct containing the interface vector, the patchwise vectors and the Bezier adjacency:
To determine all connectivity information, the [`Connectivity`](@ref NURBS.Connectivity) function can be used, which returns a struct containing the interface vector, the patchwise vectors and the Bezier adjacency:

```@example meshes
using NURBS
Expand Down
4 changes: 2 additions & 2 deletions docs/src/trafos.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ nothing # hide

## Translating

To translate a curve or a surface, the functions [`translate`](@ref translate) and [`translate!`](@ref translate!) are provided.
To translate a curve or a surface, the functions [`translate`](@ref NURBS.translate) and [`translate!`](@ref NURBS.translate!) are provided.

```@example trafos
using NURBS, StaticArrays
Expand All @@ -32,7 +32,7 @@ nothing # hide

## Rotating

To rotate a curve or a surface around a rotation axis by a given angle, the functions [`rotate`](@ref rotate) and [`rotate!`](@ref rotate!) are provided.
To rotate a curve or a surface around a rotation axis by a given angle, the functions [`rotate`](@ref NURBS.rotate) and [`rotate!`](@ref NURBS.rotate!) are provided.

!!! tip
The rotation axis is normalized by the rotate functions.
Expand Down
9 changes: 7 additions & 2 deletions ext/plotting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ end
Plot multipatch geometry.
"""
function NURBS.plotPatches(
Patches; mesh=[], pos=[], plotControlPoints=true, localVertices=false, patchID=false, enforceRatio=true, resolution=0.05
Patches; mesh=[], pos=[], plotControlPoints=true, localVertices=false, patchID=false, enforceRatio=true, resolution=0.05, color=[]
)

uEvalpoints = collect(0:resolution:1.0)
Expand All @@ -193,7 +193,12 @@ function NURBS.plotPatches(
)
else
maxi, trace = plotSurface(
S; returnTrace=true, surfaceColor=col[ind] * ones(length(uEvalpoints), length(vEvalpoints)), cmin=1, cmax=maximum(col)
S;
returnTrace=true,
surfaceColor=col[ind] * ones(length(uEvalpoints), length(vEvalpoints)),
cmin=1,
cmax=maximum(col),
#S; returnTrace=true, surfaceColor=color[ind], cmin=0, cmax=2
)
end
[push!(traces, trace[i]) for i in eachindex(trace)]
Expand Down
4 changes: 3 additions & 1 deletion src/NURBS.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ using StaticArrays
using Suppressor
using FileIO
using UUIDs
using WriteVTK



Expand All @@ -25,7 +26,7 @@ export insertKnot!, insertKnot
export coarsen, refine # NOTE: split is an extension of Base.split, i.e., it is publicly available without export
export removeKnot!, removeKnot, removeKnotU, removeKnotV

export readMultipatch, readStep
export readMultipatch, readStep, saveVtk
export generateKnotVec, numBasisFunctions, spanRanges
export greville, anchors
export scale, scale!
Expand Down Expand Up @@ -66,6 +67,7 @@ include("fundamentalOperations/knotRemoval.jl")
include("utils.jl")
include("fileIO/multipatch.jl")
include("fileIO/step.jl")
include("fileIO/vtk.jl")
include("fileIO/fileIO.jl")

function __init__() # locally initialize file formats for FileIO load and save functions
Expand Down
8 changes: 8 additions & 0 deletions src/connectivity/bezierMesh.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ function cellCart2LinTruncated(cellU, cellV, pInd, cU, cV)
return cellCart2Lin(cellU, cellV, pInd, cU, cV)
end

function cellLin2Cart(cell, cU, cV)

pInd, rem = divrem(cell - 1, cU * cV)
cellU, cellV = divrem(rem, cV)

return pInd + 1, cellU + 1, cellV + 1
end


# for a single patch cU ≠ cV is ok, for multipatch cU = cV is assumed
function bezierAdjacency(interfaces, commonVtxs, cU, cV, nP)
Expand Down
71 changes: 71 additions & 0 deletions src/fileIO/vtk.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@

function coordis(S)
x = [S[i, j][1] for i in eachindex(S[:, 1]), j in eachindex(S[1, :])]
y = [S[i, j][2] for i in eachindex(S[:, 1]), j in eachindex(S[1, :])]
z = [S[i, j][3] for i in eachindex(S[:, 1]), j in eachindex(S[1, :])]

return x[:], y[:], z[:]
end


"""
vtk(patches, resolution=0.01)
Prepare the patches to be displayed by Paraview:
The output can be written to the VTK format using the WriteVTK.jl package.
using WriteVTK
vtk_grid("path/filename", x, y, z, cellV) do vtk
end
"""
function vtk(patches, resolution=0.01)

uEvalpoints = collect(0:resolution:1.0)
vEvalpoints = collect(0:resolution:1.0)

Nv = length(vEvalpoints)
Nu = length(uEvalpoints)
Nc = length(patches)

cellV = typeof(MeshCell(VTKCellTypes.VTK_QUAD, [2, 4, 3, 5]))[]
for cell in 0:(Nc - 1)
for j in 0:(Nu - 2), i in 1:(Nv - 1)
ind = j * Nv + i + cell * Nu * Nv
push!(cellV, MeshCell(VTKCellTypes.VTK_QUAD, [ind + Nv, ind + Nv + 1, ind + 1, ind]))
end
end

x = Vector{Float64}()
y = Vector{Float64}()
z = Vector{Float64}()

for (_, patch) in enumerate(patches)

S = patch(uEvalpoints, vEvalpoints)'
xs, ys, zs = coordis(S)

append!(x, xs)
append!(y, ys)
append!(z, zs)
end

return cellV, x, y, z
end


"""
saveVtk(filename::String, patches; resolution=0.01)
Save as an unstructured grid in a .vtu file to be displayed by ParaView.
"""
function saveVtk(filename::String, patches; resolution=0.01)

cellV, x, y, z = vtk(patches, resolution)

vtk_grid(filename, x, y, z, cellV) do vtk
end

return nothing
end
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ using PlotlyJS
using FileIO

# --- testsets
@testset "Testing NURBS functionality" begin
@testset verbose = true "Testing NURBS functionality" begin

@testset "Bases" begin
include("bases.jl")
Expand Down
4 changes: 4 additions & 0 deletions test/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ end

Patches = readStep("assets/sphere.stp")
@test length(Patches) == 6


# --- .vtk files
@test_nowarn Patches = saveVtk("test", Patches; resolution=0.02)
end

@testset "Plotting" begin
Expand Down

0 comments on commit 683812e

Please sign in to comment.