changeset 1967:669361a8195a feature/grids/geometry_functions

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