<commit_before>#extension GL_ARB_texture_rectangle : enable
#ifdef GL_EXT_gpu_shader4
#extension GL_EXT_gpu_shader4 : enable
#endif

uniform sampler2DRect infoPrevious;
uniform sampler2DRect infoCurrent;
uniform sampler2DRect normals;
uniform sampler2DRect colors;

uniform int layer;
uniform float viewportX;
uniform float viewportY;

uniform sampler3D volume[NQVTK_RAYCASTER_VOLUMECOUNT];
uniform float volumeDataShift[NQVTK_RAYCASTER_VOLUMECOUNT];
uniform float volumeDataScale[NQVTK_RAYCASTER_VOLUMECOUNT];
uniform vec3 volumeOrigin[NQVTK_RAYCASTER_VOLUMECOUNT];
uniform vec3 volumeSize[NQVTK_RAYCASTER_VOLUMECOUNT];
uniform vec3 volumeSpacing[NQVTK_RAYCASTER_VOLUMECOUNT];

uniform float tfStart[NQVTK_RAYCASTER_VOLUMECOUNT];
uniform float tfEnd[NQVTK_RAYCASTER_VOLUMECOUNT];

uniform float stepSize;
uniform float kernelSize;

uniform float isoOpacity;

// Prototypes from LibUtility
vec3 phongShading(vec3 matColor, vec3 normal);
bool getBit(float byte, int bit);
float setBit(float byte, int bit, bool on);

// Get vector from volume
vec3 getVector(int v, vec3 pos) {
	vec4 tpos = gl_TextureMatrixInverse[v] * vec4(pos, 1.0);
	vec3 p = ((tpos.xyz / tpos.w) - volumeOrigin[v]) / volumeSize[v];
	return vec3(volumeDataShift[v]) + volumeDataScale[v] * texture3D(volume[v], p).xyz;
}

// Linear Jacobian
mat3 jacobian(int v, vec3 pos, float delta, vec3 val) {
#ifdef NQVTK_RAYCASTER_CENTRALDIFFERENCES
	vec3 dx = (getVector(v, pos + vec3(0.5 * delta, 0.0, 0.0)) - 
		getVector(v, pos - vec3(0.5 * delta, 0.0, 0.0))) / delta;
	vec3 dy = (getVector(v, pos + vec3(0.0, 0.5 * delta, 0.0)) - 
		getVector(v, pos - vec3(0.0, 0.5 * delta, 0.0))) / delta;
	vec3 dz = (getVector(v, pos + vec3(0.0, 0.0, 0.5 * delta)) - 
		getVector(v, pos - vec3(0.0, 0.0, 0.5 * delta))) / delta;
#else
	vec3 dx = (getVector(v, pos + vec3(delta, 0.0, 0.0)) - val) / delta;
	vec3 dy = (getVector(v, pos + vec3(0.0, delta, 0.0)) - val) / delta;
	vec3 dz = (getVector(v, pos + vec3(0.0, 0.0, delta)) - val) / delta;
#endif
	// Add identity (because we should have used pos + vec instead of vec)
	return mat3(dx, dy, dz) + mat3(1.0);
}

// Compute determinant of matrix
float determinant(mat3 m) {
	return m[0][0] * m[1][1] * m[2][2] 
		+ m[0][1] * m[1][2] * m[2][0] 
		+ m[0][2] * m[1][0] * m[2][1] 
		- m[0][2] * m[1][1] * m[2][0] 
		- m[0][1] * m[1][0] * m[2][2] 
		- m[0][0] * m[1][2] * m[2][1];
}

float getValue(int v, vec3 pos) {
	//vec4 tpos = gl_TextureMatrixInverse[v] * vec4(pos, 1.0);
	//vec3 p = ((tpos.xyz / tpos.w) - volumeOrigin[v]) / volumeSize[v];
	//return texture3D(volume[v], p).x;
#ifdef NQVTK_RAYCASTER_CENTRALDIFFERENCES
	vec3 val = vec3(0.0);
#else
	vec3 val = getVector(v, pos);
#endif
	float detJ = abs(determinant(jacobian(v, pos, kernelSize, val)));
	float g = 0.0;
	if (detJ > 1.0) 
		g = 1.0 - (1.0 / detJ);
	else
		g = detJ - 1.0;
	return g + 0.5;
}

// Compute gradient of the volume
vec3 computeGradient(int v, vec3 pos, float delta, float val) {
#ifdef NQVTK_RAYCASTER_CENTRALDIFFERENCES<commit_msg>Added simple implicit function test.<commit_after>#extension GL_ARB_texture_rectangle : enable
#ifdef GL_EXT_gpu_shader4
#extension GL_EXT_gpu_shader4 : enable
#endif

uniform sampler2DRect infoPrevious;
uniform sampler2DRect infoCurrent;
uniform sampler2DRect normals;
uniform sampler2DRect colors;

uniform int layer;
uniform float viewportX;
uniform float viewportY;

uniform sampler3D volume[NQVTK_RAYCASTER_VOLUMECOUNT];
uniform float volumeDataShift[NQVTK_RAYCASTER_VOLUMECOUNT];
uniform float volumeDataScale[NQVTK_RAYCASTER_VOLUMECOUNT];
uniform vec3 volumeOrigin[NQVTK_RAYCASTER_VOLUMECOUNT];
uniform vec3 volumeSize[NQVTK_RAYCASTER_VOLUMECOUNT];
uniform vec3 volumeSpacing[NQVTK_RAYCASTER_VOLUMECOUNT];

uniform float tfStart[NQVTK_RAYCASTER_VOLUMECOUNT];
uniform float tfEnd[NQVTK_RAYCASTER_VOLUMECOUNT];

uniform float stepSize;
uniform float kernelSize;

uniform float isoOpacity;

// Prototypes from LibUtility
vec3 phongShading(vec3 matColor, vec3 normal);
bool getBit(float byte, int bit);
float setBit(float byte, int bit, bool on);

// Get vector from volume
vec3 getVector(int v, vec3 pos) {
	vec4 tpos = gl_TextureMatrixInverse[v] * vec4(pos, 1.0);
	vec3 p = ((tpos.xyz / tpos.w) - volumeOrigin[v]) / volumeSize[v];
	return vec3(volumeDataShift[v]) + volumeDataScale[v] * texture3D(volume[v], p).xyz;
}

// Linear Jacobian
mat3 jacobian(int v, vec3 pos, float delta, vec3 val) {
#ifdef NQVTK_RAYCASTER_CENTRALDIFFERENCES
	vec3 dx = (getVector(v, pos + vec3(0.5 * delta, 0.0, 0.0)) - 
		getVector(v, pos - vec3(0.5 * delta, 0.0, 0.0))) / delta;
	vec3 dy = (getVector(v, pos + vec3(0.0, 0.5 * delta, 0.0)) - 
		getVector(v, pos - vec3(0.0, 0.5 * delta, 0.0))) / delta;
	vec3 dz = (getVector(v, pos + vec3(0.0, 0.0, 0.5 * delta)) - 
		getVector(v, pos - vec3(0.0, 0.0, 0.5 * delta))) / delta;
#else
	vec3 dx = (getVector(v, pos + vec3(delta, 0.0, 0.0)) - val) / delta;
	vec3 dy = (getVector(v, pos + vec3(0.0, delta, 0.0)) - val) / delta;
	vec3 dz = (getVector(v, pos + vec3(0.0, 0.0, delta)) - val) / delta;
#endif
	// Add identity (because we should have used pos + vec instead of vec)
	return mat3(dx, dy, dz) + mat3(1.0);
}

// Compute determinant of matrix
float determinant(mat3 m) {
	return m[0][0] * m[1][1] * m[2][2] 
		+ m[0][1] * m[1][2] * m[2][0] 
		+ m[0][2] * m[1][0] * m[2][1] 
		- m[0][2] * m[1][1] * m[2][0] 
		- m[0][1] * m[1][0] * m[2][2] 
		- m[0][0] * m[1][2] * m[2][1];
}

float plane(vec3 n, float d, vec3 pos)
{
	return dot(pos, n) - d;
}

float column(vec3 pos)
{
	float p1 = plane(vec3(1,0,0), 2, pos);
	float p2 = plane(vec3(-1,0,0), 2, pos);
	float p3 = plane(vec3(0,0,1), 2, pos);
	float p4 = plane(vec3(0,0,-1), 2, pos);
	return max(max(p1, p2), max(p3, p4));
}

vec3 twist(vec3 pos)
{
	return vec3(
		pos.x * sin(pos.y) + pos.z * cos(pos.y), 
		pos.y, 
		pos.x * -cos(pos.y) + pos.z * sin(pos.y));
}

float distance(vec3 pos)
{
	float c1 = column(twist(pos));
	float c2 = column(twist(pos + vec3(5.0, 0.0, 5.0)));
	float c3 = column(twist(pos + vec3(-5.0, 0.0, 5.0)));
	float c4 = column(twist(pos + vec3(-5.0, 0.0, -5.0)));
	float c5 = column(twist(pos + vec3(5.0, 0.0, -5.0)));
	return min(c1, min(min(c2, c3), min(c4, c5)));
}

float getValue(int v, vec3 pos) {
	//vec4 tpos = gl_TextureMatrixInverse[v] * vec4(pos, 1.0);
	//vec3 p = ((tpos.xyz / tpos.w) - volumeOrigin[v]) / volumeSize[v];
	//return texture3D(volume[v], p).x;
	return distance(pos);
	/*
#ifdef NQVTK_RAYCASTER_CENTRALDIFFERENCES
	vec3 val = vec3(0.0);
#else
	vec3 val = getVector(v, pos);
#endif
	float detJ = abs(determinant(jacobian(v, pos, kernelSize, val)));
	float g = 0.0;
	if (detJ > 1.0) 
		g = 1.0 - (1.0 / detJ);
	else
		g = detJ - 1.0;
	return g + 0.5;
	*/
}

// Compute gradient of the volume
vec3 computeGradient(int v, vec3 pos, float delta, float val) {
#ifdef NQVTK_RAYCASTER_CENTRALDIFFERENCES
{'fraction_non_alphanumeric': 0.16279069767441862, 'fraction_numerical': 0.05601872166154746, 'mean_word_length': 4.333853354134165, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1546408', 'n_tokens_mistral': 3060, 'n_tokens_neox': 2942, 'n_words': 708}