comparison src/Grids/geometry.jl @ 1978:d7a5129517d9 feature/grids/geometry_functions

Add docstrings
author Jonatan Werpers <jonatan@werpers.com>
date Wed, 26 Feb 2025 15:40:19 +0100
parents 270675bb97be
children db3383581b9f
comparison
equal deleted inserted replaced
1977:270675bb97be 1978:d7a5129517d9
3 tangent::PT 3 tangent::PT
4 4
5 Line{PT}(p::PT, tangent::PT) where PT = new{PT}(p,tangent) 5 Line{PT}(p::PT, tangent::PT) where PT = new{PT}(p,tangent)
6 end 6 end
7 7
8 """
9 Line(p,t)
10
11 A line, as a callable object, starting at `p` with tangent `t`.
12
13 # Example
14 ```julia-repl
15 julia> l = Grids.Line([1,1],[2,1])
16 Diffinitive.Grids.Line{StaticArraysCore.SVector{2, Int64}}([1, 1], [2, 1])
17
18 julia> l(0)
19 2-element StaticArraysCore.SVector{2, Int64} with indices SOneTo(2):
20 1
21 1
22
23 julia> l(1)
24 2-element StaticArraysCore.SVector{2, Int64} with indices SOneTo(2):
25 3
26 2
27 ```
28
29 See also: [`LineSegment`](@ref).
30 """
8 function Line(p, t) 31 function Line(p, t)
9 p = SVector{length(p)}(p) 32 p = SVector{length(p)}(p)
10 t = SVector{length(t)}(t) 33 t = SVector{length(t)}(t)
11 p, t = promote(p, t) 34 p, t = promote(p, t)
12 35
22 b::PT 45 b::PT
23 46
24 LineSegment{PT}(p::PT, tangent::PT) where PT = new{PT}(p,tangent) 47 LineSegment{PT}(p::PT, tangent::PT) where PT = new{PT}(p,tangent)
25 end 48 end
26 49
50 """
51 LineSegment(a,b)
52
53 A line segment, as a callable object, from `a` to `b`.
54
55 # Example
56 ```julia-repl
57 julia> l = Grids.LineSegment([1,1],[2,1])
58 Diffinitive.Grids.LineSegment{StaticArraysCore.SVector{2, Int64}}([1, 1], [2, 1])
59
60 julia> l(0)
61 2-element StaticArraysCore.SVector{2, Int64} with indices SOneTo(2):
62 1
63 1
64
65 julia> l(0.5)
66 2-element StaticArraysCore.SVector{2, Float64} with indices SOneTo(2):
67 1.5
68 1.0
69
70 julia> l(1)
71 2-element StaticArraysCore.SVector{2, Int64} with indices SOneTo(2):
72 2
73 1
74 ```
75
76 See also: [`Line`](@ref).
77 """
27 function LineSegment(a, b) 78 function LineSegment(a, b)
28 a = SVector{length(a)}(a) 79 a = SVector{length(a)}(a)
29 b = SVector{length(b)}(b) 80 b = SVector{length(b)}(b)
30 a, b = promote(a, b) 81 a, b = promote(a, b)
31 82
34 85
35 (c::LineSegment)(s) = (1-s)*c.a + s*c.b 86 (c::LineSegment)(s) = (1-s)*c.a + s*c.b
36 87
37 Grids.jacobian(c::LineSegment, s) = c.b - c.a 88 Grids.jacobian(c::LineSegment, s) = c.b - c.a
38 89
90 """
91 linesegments(ps...)
92
93 An array of line segments between the points `ps[1]`, `ps[2]`, and so on.
94
95 See also: [`polygon_edges`](@ref).
96 """
39 function linesegments(ps...) 97 function linesegments(ps...)
40 return [LineSegment(ps[i], ps[i+1]) for i ∈ 1:length(ps)-1] 98 return [LineSegment(ps[i], ps[i+1]) for i ∈ 1:length(ps)-1]
41 end 99 end
42 100
43 101
102 """
103 polygon_edges(ps...)
104
105 An array of line segments between the points `ps[1]`, `ps[2]`, and so on
106 including the segment between `ps[end]` and `ps[1]`.
107
108 See also: [`linesegments`](@ref).
109 """
44 function polygon_edges(ps...) 110 function polygon_edges(ps...)
45 n = length(ps) 111 n = length(ps)
46 return [LineSegment(ps[i], ps[mod1(i+1,n)]) for i ∈ eachindex(ps)] 112 return [LineSegment(ps[i], ps[mod1(i+1,n)]) for i ∈ eachindex(ps)]
47 end 113 end
48 114
51 r::T 117 r::T
52 118
53 Circle{PT,T}(c,r) where {PT,T} = new{PT,T}(c,r) 119 Circle{PT,T}(c,r) where {PT,T} = new{PT,T}(c,r)
54 end 120 end
55 121
122 """
123 Circle(c,r)
124
125 A circle with center `c` and radius `r` paramatrized with the angle to the x-axis.
126
127 # Example
128 ```julia-repl
129 julia> c = Grids.Circle([1,1], 2)
130 Diffinitive.Grids.Circle{StaticArraysCore.SVector{2, Int64}, Int64}([1, 1], 2)
131
132 julia> c(0)
133 2-element StaticArraysCore.SVector{2, Float64} with indices SOneTo(2):
134 3.0
135 1.0
136
137 julia> c(π/2)
138 2-element StaticArraysCore.SVector{2, Float64} with indices SOneTo(2):
139 1.0000000000000002
140 3.0
141 ```
142 """
56 function Circle(c,r) 143 function Circle(c,r)
57 c = SVector{2}(c) 144 c = SVector{2}(c)
58 return Circle{typeof(c), typeof(r)}(c,r) 145 return Circle{typeof(c), typeof(r)}(c,r)
59 end 146 end
60 147
66 function Grids.jacobian(C::Circle, θ) 153 function Grids.jacobian(C::Circle, θ)
67 (;r) = C 154 (;r) = C
68 r*@SVector[-sin(θ), cos(θ)] 155 r*@SVector[-sin(θ), cos(θ)]
69 end 156 end
70 157
158 """
159 TransfiniteInterpolationSurface(c₁, c₂, c₃, c₄)
160
161 A surface defined by the transfinite interpolation of the curves `c₁`, `c₂`, `c₃`, and `c₄`.
162 """
71 struct TransfiniteInterpolationSurface{T1,T2,T3,T4} 163 struct TransfiniteInterpolationSurface{T1,T2,T3,T4}
72 c₁::T1 164 c₁::T1
73 c₂::T2 165 c₂::T2
74 c₃::T3 166 c₃::T3
75 c₄::T4 167 c₄::T4
88 180
89 function (s::TransfiniteInterpolationSurface)(ξ̄::AbstractArray) 181 function (s::TransfiniteInterpolationSurface)(ξ̄::AbstractArray)
90 s(ξ̄...) 182 s(ξ̄...)
91 end 183 end
92 184
185 """
186 check_transfiniteinterpolation(s::TransfiniteInterpolationSurface)
187
188 Throw an error if the ends of the curves in the transfinite interpolation do not match.
189 """
93 function check_transfiniteinterpolation(s::TransfiniteInterpolationSurface) 190 function check_transfiniteinterpolation(s::TransfiniteInterpolationSurface)
94 if check_transfiniteinterpolation(Bool, s) 191 if check_transfiniteinterpolation(Bool, s)
95 return nothing 192 return nothing
96 else 193 else
97 error("The end of each curve in the transfinite interpolation should be the same as the beginning of the next curve.") 194 error("The end of each curve in the transfinite interpolation should be the same as the beginning of the next curve.")
98 end 195 end
99 end 196 end
100 197
198 """
199 check_transfiniteinterpolation(::Type{Bool}, s::TransfiniteInterpolationSurface)
200
201 Return true if the ends of the curves in the transfinite interpolation match.
202 """
101 function check_transfiniteinterpolation(::Type{Bool}, s::TransfiniteInterpolationSurface) 203 function check_transfiniteinterpolation(::Type{Bool}, s::TransfiniteInterpolationSurface)
102 if !isapprox(s.c₁(1), s.c₂(0)) 204 if !isapprox(s.c₁(1), s.c₂(0))
103 return false 205 return false
104 end 206 end
105 207
141 -(1-u)*P₀₀ - u*P₁₀ + u*P₁₁ + (1-u)*P₀₁ 243 -(1-u)*P₀₀ - u*P₁₀ + u*P₁₁ + (1-u)*P₀₁
142 ) 244 )
143 245
144 return [∂x̄∂ξ₁ ∂x̄∂ξ₂] 246 return [∂x̄∂ξ₁ ∂x̄∂ξ₂]
145 end 247 end
146
147 # TODO: Implement jacobian() for the different mapping helpers
148 # TODO: Add doc strings