#pragma once
#include "cuda_helper.hpp"

/* change these values to set the importance values of the volume
 * (Middle only used by SDF importance to render different values for pelvis)*/
constexpr float importanceHigh = 1;
constexpr float importanceMiddle = 0.75;
constexpr float importanceLow = 0;

__device__ inline float sampleImp(float val, float thresholdMin, float thresholdMax) {
    if (val > thresholdMin && val <= thresholdMax) {
        return importanceHigh;
    }
    return importanceLow;
}

__device__ inline float sampleImpPos(float3 p, float3 p01, cudaTextureObject_t volumeData, float thresholdMin,
                                     float thresholdMax, bool position) {
    float val = tex3D<float>(volumeData, p01.x, p01.y, p01.z);
    if (position) {
        if (val > thresholdMin && val <= thresholdMax) {
            // used for magnetic reconnection
            if (fabsf(p.x) < 0.3) {
                return importanceHigh;
            }
        }
        return importanceLow;
    }
    return sampleImp(val, thresholdMin, thresholdMax);
}

__device__ inline float sampleImpSDF(float3 p, cudaTextureObject_t volumeData, float3 bbMin, float3 bbMax,
                                     float thresholdMin, float thresholdMax, bool sdf, cudaTextureObject_t sdfTexture,
                                     bool position) {
    float x = shiftInterval01(p.x, bbMin.x, bbMax.x);
    float y = shiftInterval01(p.y, bbMin.y, bbMax.y);
    float z = shiftInterval01(p.z, bbMin.z, bbMax.z);
    if (sdf) {
        float ex = tex3D<float>(sdfTexture, x, y, z);
        if (ex > 0.5) {
            if (p.x >= 0.018) {
                return importanceMiddle;
            } else {
                return importanceHigh;
            }
        } else {
            return importanceLow;
        }
    }
    float3 p01 = make_float3(x, y, z);

    return sampleImpPos(p, p01, volumeData, thresholdMin, thresholdMax, position);
}