package main

import (
	"math"
)

type SDF interface {
	Distance(Vector3) float64
	GetMaterial(Vector3) Material
}

func Gradient(sdf SDF, p Vector3, eps float64) Vector3 {
	dx := (sdf.Distance(p.Add(Vector3{X: eps})) - sdf.Distance(p.Add(Vector3{X: -eps}))) / (2 * eps)
	dy := (sdf.Distance(p.Add(Vector3{Y: eps})) - sdf.Distance(p.Add(Vector3{Y: -eps}))) / (2 * eps)
	dz := (sdf.Distance(p.Add(Vector3{Z: eps})) - sdf.Distance(p.Add(Vector3{Z: -eps}))) / (2 * eps)
	return Vector3{X: dx, Y: dy, Z: dz}
}

// Some transformations see https://iquilezles.org/articles/distfunctions/

type TranslatedSDF struct {
	primitive SDF
	translate Vector3
}

func (s TranslatedSDF) Distance(p Vector3) float64 {
	return s.primitive.Distance(p.Sub(s.translate))
}

func (s TranslatedSDF) GetMaterial(p Vector3) Material {
	return s.primitive.GetMaterial(p.Sub(s.translate))
}

type RotatedSDF struct {
	primitive SDF
	rotVector Vector3
	angle     float64
}

func (s RotatedSDF) Distance(p Vector3) float64 {
	rotated_p := Rotate(p, s.rotVector, s.angle)
	return s.primitive.Distance(rotated_p)
}
func (s RotatedSDF) GetMaterial(p Vector3) Material {
	rotated_p := Rotate(p, s.rotVector, s.angle)
	return s.primitive.GetMaterial(rotated_p)
}

type ScaledSDF struct {
	primitive SDF
	scale     float64
}

func (s ScaledSDF) Distance(p Vector3) float64 {
	return s.primitive.Distance(p.Scale(1 / s.scale))
}

func (s ScaledSDF) GetMaterial(p Vector3) Material {
	return s.primitive.GetMaterial(p.Scale(1 / s.scale))
}

type RepeatSDF struct {
	primitive SDF
	cellSize  Vector3
}

func (s RepeatSDF) Distance(p Vector3) float64 {
	x, y, z := p.Unpack()
	sx, sy, sz := s.cellSize.Unpack()
	round := math.RoundToEven
	nearest_cell := Vector3{sx * round(x/sx), sy * round(y/sy), sz * round(z/sz)}
	return s.primitive.Distance(p.Sub(nearest_cell))
}
func (s RepeatSDF) GetMaterial(p Vector3) Material {
	x, y, z := p.Unpack()
	sx, sy, sz := s.cellSize.Unpack()
	round := math.RoundToEven
	nearest_cell := Vector3{sx * round(x/sx), sy * round(y/sy), sz * round(z/sz)}
	return s.primitive.GetMaterial(p.Sub(nearest_cell))
}

type UnionSDF struct {
	primitive1 SDF
	primitive2 SDF
}

func (s UnionSDF) Distance(p Vector3) float64 {
	return min(s.primitive1.Distance(p), s.primitive2.Distance(p))
}

func (s UnionSDF) GetMaterial(p Vector3) Material {
	d1, color1 := s.primitive1.Distance(p), s.primitive1.GetMaterial(p)
	d2, color2 := s.primitive2.Distance(p), s.primitive2.GetMaterial(p)

	color := color1
	if d2 < d1 {
		color = color2
	}
	return color
}

type SubstractionSDF struct {
	primitive1 SDF
	primitive2 SDF
}

func (s SubstractionSDF) Distance(p Vector3) float64 {
	return max(s.primitive1.Distance(p), -s.primitive2.Distance(p))
}

func (s SubstractionSDF) GetMaterial(p Vector3) Material {
	return s.primitive1.GetMaterial(p)
}

type IntersectionSDF struct {
	primitive1 SDF
	primitive2 SDF
}

func (s IntersectionSDF) Distance(p Vector3) float64 {
	return max(s.primitive1.Distance(p), s.primitive2.Distance(p))
}

func (s IntersectionSDF) GetMaterial(p Vector3) Material {
	mat1, mat2 := s.primitive1.GetMaterial(p), s.primitive2.GetMaterial(p)
	return MixedMaterial{mat1, mat2, 0.5, p}
}

type SmoothUnionSDF struct {
	primitive1 SDF
	primitive2 SDF
	k          float64
}

func (s SmoothUnionSDF) Distance(p Vector3) float64 {
	k := 4 * s.k
	d1 := s.primitive1.Distance(p)
	d2 := s.primitive2.Distance(p)
	h := max(k-math.Abs(d1-d2), 0.0)
	return min(d1, d2) - h*h*0.25/k
}

func (s SmoothUnionSDF) GetMaterial(p Vector3) Material {
	k := 4 * s.k
	d1, mat1 := s.primitive1.Distance(p), s.primitive1.GetMaterial(p)
	d2, mat2 := s.primitive2.Distance(p), s.primitive2.GetMaterial(p)
	t := SmoothStep(d2-d1, -k, k)
	return MixedMaterial{mat2, mat1, t, p}
}

type SmoothSubstractionSDF struct {
	primitive1 SDF
	primitive2 SDF
	k          float64
}

func (s SmoothSubstractionSDF) Distance(p Vector3) float64 {
	k := 4 * s.k
	d1 := s.primitive1.Distance(p)
	d2 := s.primitive2.Distance(p)
	h := max(k-math.Abs(-d1-d2), 0.0)
	return max(d1, -d2) + h*h*0.25/k
}

func (s SmoothSubstractionSDF) GetMaterial(p Vector3) Material {
	k := 4 * s.k
	d1, mat1 := s.primitive1.Distance(p), s.primitive1.GetMaterial(p)
	d2, mat2 := s.primitive2.Distance(p), s.primitive2.GetMaterial(p)
	t := SmoothStep(d1-d2, -k, k)
	return MixedMaterial{mat1, mat2, t, p}
}

type SmoothIntersectionSDF struct {
	primitive1 SDF
	primitive2 SDF
	k          float64
}

func (s SmoothIntersectionSDF) Distance(p Vector3) float64 {
	k := 4 * s.k
	d1 := s.primitive1.Distance(p)
	d2 := s.primitive2.Distance(p)
	h := max(k-math.Abs(d1-d2), 0.0)
	return max(d1, d2) + h*h*0.25/k
}

func (s SmoothIntersectionSDF) GetMaterial(p Vector3) Material {
	k := 4 * s.k
	d1, mat1 := s.primitive1.Distance(p), s.primitive1.GetMaterial(p)
	d2, mat2 := s.primitive2.Distance(p), s.primitive2.GetMaterial(p)
	t := SmoothStep(d2-d1, -k, k)
	return MixedMaterial{mat1, mat2, t, p}
}
