From a31143a635848914a26b6de28b4b6d33419fdc60 Mon Sep 17 00:00:00 2001
From: Michiel Cottaar <MichielCottaar@protonmail.com>
Date: Fri, 31 May 2024 12:01:10 +0100
Subject: [PATCH] Allow BuildingBlock to start/end with non-zero gradient

This is to support Pulseq, where this behaviour is allowed.
---
 src/containers/building_blocks.jl | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/src/containers/building_blocks.jl b/src/containers/building_blocks.jl
index 0f9c07f..c9dea4f 100644
--- a/src/containers/building_blocks.jl
+++ b/src/containers/building_blocks.jl
@@ -92,10 +92,18 @@ The gradient is linearly interpolated between these points (see [`waveform_seque
 """
 function waveform(bb::BaseBuildingBlock)
     ndim = ndim_grad(bb)
+    first_block = waveform_sequence(bb)[1][2]
+    if first_block isa NoGradient
+        first_grad = ndim == 3 ? zero(SVector{3, Float64}) : 0.
+    elseif first_block isa ConstantGradient
+        first_grad = first_block.gradient_strength
+    else
+        first_grad = first_block.gradient_strength_start
+    end
     if ndim == 3
-        result = Tuple{VariableType, SVector{3, VariableType}}[(0., zero(SVector{3, Float64}))]
+        result = Tuple{VariableType, SVector{3, VariableType}}[(0., first_grad)]
     elseif ndim == 1
-        result = Tuple{VariableType, VariableType}[(0., 0.)]
+        result = Tuple{VariableType, VariableType}[(0., first_grad)]
     else
         return []
     end
@@ -116,7 +124,6 @@ function waveform(bb::BaseBuildingBlock)
             error("Unrecognised block type in BuildingBlock: $(typeof(bb)).")
         end
     end
-    @assert all(abs.(result[end][2]) .<= 1e-12) "$(typeof(bb)) does not end up with a gradient of zero. This is probably caused by an improper implementation of this BuildingBlock."
     return result
 end
 
-- 
GitLab