changeset 1966:478b233999c5 feature/grids/geometry_functions

Add tests and better constructors for Line
author Jonatan Werpers <jonatan@werpers.com>
date Wed, 12 Feb 2025 15:33:48 +0100
parents eedb5d7b90b4
children 669361a8195a
files src/Grids/geometry.jl test/Grids/geometry_test.jl
diffstat 2 files changed, 40 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/Grids/geometry.jl	Wed Feb 12 15:30:47 2025 +0100
+++ b/src/Grids/geometry.jl	Wed Feb 12 15:33:48 2025 +0100
@@ -1,6 +1,26 @@
 struct Line{PT}
     p::PT
     tangent::PT
+
+    Line{PT}(p::PT, tangent::PT) where PT = new{PT}(p,tangent)
+end
+
+function Line(p, t)
+    S = length(p)
+    T = promote_type(eltype(p), eltype(t))
+
+    PT = SVector{S,T}
+    return Line{PT}(
+        convert(PT, p),
+        convert(PT, t),
+    )
+end
+
+function Line(p::Tuple, t::Tuple)
+    p = promote(p...)
+    t = promote(t...)
+
+    return Line(SVector(p), SVector(t))
 end
 
 (c::Line)(s) = c.p + s*c.tangent
--- a/test/Grids/geometry_test.jl	Wed Feb 12 15:30:47 2025 +0100
+++ b/test/Grids/geometry_test.jl	Wed Feb 12 15:33:48 2025 +0100
@@ -1,6 +1,25 @@
+using Diffinitive.Grids
+using Diffinitive.Grids: Line
+using StaticArrays
+
 @testset "Line" begin
-    @test_broken false
     @testset "Constructors" begin
+        @test Line([1,2],[2,3]) isa Line{SVector{2,Int}}
+        @test Line((1,2),(2,3)) isa Line{SVector{2,Int}}
+        @test Line(@SVector[1,2],[2,3]) isa Line{SVector{2,Int}}
+        @test Line(@SVector[1,2],@SVector[2,3]) isa Line{SVector{2,Int}}
+
+        @test Line([1,2],[2.,3]) isa Line{SVector{2,Float64}}
+        @test Line(@SVector[1,2.],@SVector[2,3]) isa Line{SVector{2,Float64}}
+        @test Line((1,2.),(2,3)) isa Line{SVector{2,Float64}}
+    end
+
+    @testset "Evaluation" begin
+        l = Line([1,2],[2,3])
+
+        @test l(0) == [1,2]
+        @test l(1) == [1,2] + [2,3]
+        @test l(1/2) == [1,2] + [2,3]/2
     end
 end