All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.jetbrains.datalore.plot.base.scale.MapperUtil.kt Maven / Gradle / Ivy

There is a newer version: 4.5.3-alpha1
Show newest version
/*
 * Copyright (c) 2019. JetBrains s.r.o.
 * Use of this source code is governed by the MIT license that can be found in the LICENSE file.
 */

package jetbrains.datalore.plot.base.scale

import jetbrains.datalore.base.interval.DoubleSpan
import jetbrains.datalore.plot.base.ContinuousTransform
import jetbrains.datalore.plot.base.ScaleMapper
import kotlin.math.max
import kotlin.math.min

object MapperUtil {
    fun map(r: DoubleSpan, mapper: ScaleMapper): DoubleSpan {
        val a = mapper(r.lowerEnd)!!
        val b = mapper(r.upperEnd)!!
        return DoubleSpan(min(a, b), max(a, b))
    }

    fun rangeWithLimitsAfterTransform(
        dataRange: DoubleSpan,
        trans: ContinuousTransform
    ): DoubleSpan {
        check(trans.isInDomain(dataRange.lowerEnd)) {
            "[${trans::class.simpleName}] Lower end ${dataRange.lowerEnd} is outside of transform's domain."
        }
        check(trans.isInDomain(dataRange.upperEnd)) {
            "[${trans::class.simpleName}] Upper end ${dataRange.upperEnd} is outside of transform's domain."
        }

        val transformedLimits = listOf(
            trans.apply(trans.definedLimits().first),
            trans.apply(trans.definedLimits().second),
            trans.apply(dataRange.lowerEnd),
            trans.apply(dataRange.upperEnd),
        )

        return DoubleSpan.encloseAll(transformedLimits)
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy