Preserve fluence by value when pulse duration changes

This commit is contained in:
areumwoo
2026-03-08 14:26:35 +09:00
parent aa618627df
commit 7ec7d8674f

View File

@@ -134,6 +134,7 @@ import kotlinx.coroutines.joinAll
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import timber.log.Timber import timber.log.Timber
import kotlin.math.abs
import javax.inject.Inject import javax.inject.Inject
import kotlin.experimental.or import kotlin.experimental.or
import kotlin.time.Duration.Companion.milliseconds import kotlin.time.Duration.Companion.milliseconds
@@ -2132,8 +2133,15 @@ class MainViewModel @Inject constructor(
setFluenceList(newFluenceList) setFluenceList(newFluenceList)
} }
// 2-1. Resolve and apply fluence by value (not by old angle/index).
val resolvedFluence = newFluenceList.minByOrNull { abs(it - newFluence) } ?: 0f
val resolvedFluenceIndex = newFluenceList.indexOf(resolvedFluence).takeIf { it >= 0 } ?: 0
if (newFluenceList.isNotEmpty()) {
setFluenceAngle(resolvedFluenceIndex.stepToDegree(totalSteps = newFluenceList.size))
}
// 3. Safely Update Repetition List (Prevents NullPointerException) // 3. Safely Update Repetition List (Prevents NullPointerException)
val newHzType = hzTable.value.getValue(newPulseDuration, newFluence) val newHzType = hzTable.value.getValue(newPulseDuration, resolvedFluence)
val newRepetitionList = RepetitionsByColorKey[newHzType] ?: RepetitionsByColorKey[KEY_YELLOW]!! val newRepetitionList = RepetitionsByColorKey[newHzType] ?: RepetitionsByColorKey[KEY_YELLOW]!!
if (newRepetitionList != repetitionList.value) { if (newRepetitionList != repetitionList.value) {
setRepetitionList(newRepetitionList) setRepetitionList(newRepetitionList)
@@ -2168,7 +2176,7 @@ class MainViewModel @Inject constructor(
// 6. Any change invalidates the current preset selection. // 6. Any change invalidates the current preset selection.
setSelectedPresetIndex(0) setSelectedPresetIndex(0)
Timber.d("Updated Laser Parameters: pulse=$newPulseDuration, fluence=$newFluence -> newRepListSize=${newRepetitionList.size}") Timber.d("Updated Laser Parameters: pulse=$newPulseDuration, fluence=$resolvedFluence -> newRepListSize=${newRepetitionList.size}")
} }
/** /**
@@ -2188,13 +2196,16 @@ class MainViewModel @Inject constructor(
setPulseAngle(newPulseStep.stepToDegree(totalSteps = PulseDurations.size)) setPulseAngle(newPulseStep.stepToDegree(totalSteps = PulseDurations.size))
val newPulseDuration = PulseDurations[newPulseStep] val newPulseDuration = PulseDurations[newPulseStep]
// When pulse duration changes via slider, we use the first available fluence for the new list. // Keep the current fluence value when pulse duration changes via slider.
val firstFluence = energyTable.value.getKey2ListForKey1(newPulseDuration).firstOrNull() ?: 0f val currentFluenceStep = fluenceAngle.value.degreeToStep(totalSteps = fluenceList.value.size)
val currentFluence = fluenceList.value.getOrNull(currentFluenceStep)
?: energyTable.value.getKey2ListForKey1(newPulseDuration).firstOrNull()
?: 0f
// Call the centralized helper, resetting the fluence slider. // Call the centralized helper using the preserved fluence value.
updateLaserParameters( updateLaserParameters(
newPulseDuration = newPulseDuration, newPulseDuration = newPulseDuration,
newFluence = firstFluence, newFluence = currentFluence,
) )
} }
@@ -2206,12 +2217,17 @@ class MainViewModel @Inject constructor(
setPulseAngle(newStep.stepToDegree(totalSteps = PulseDurations.size)) setPulseAngle(newStep.stepToDegree(totalSteps = PulseDurations.size))
val newPulseDuration = PulseDurations[newStep] val newPulseDuration = PulseDurations[newStep]
val firstFluence = energyTable.value.getKey2ListForKey1(newPulseDuration).firstOrNull() ?: 0f // Keep the current fluence value when pulse duration changes via slider.
val currentFluenceStep = fluenceAngle.value.degreeToStep(totalSteps = fluenceList.value.size)
val currentFluence = fluenceList.value.getOrNull(currentFluenceStep)
?: energyTable.value.getKey2ListForKey1(newPulseDuration).firstOrNull()
?: 0f
// Call the centralized helper, resetting the fluence slider. // Call the centralized helper, resetting the fluence slider.
updateLaserParameters( updateLaserParameters(
newPulseDuration = newPulseDuration, newPulseDuration = newPulseDuration,
newFluence = firstFluence, newFluence = currentFluence,
) )
} }
} }