/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
 *
 * SPDX-License-Identifier: Apache-2.0 */

#include "node_fractal_voronoi.h"
#include "stdcycles.h"
#include "vector2.h"
#include "vector4.h"

#define vector3 point

shader node_voronoi_texture(
    int use_mapping = 0,
    matrix mapping = matrix(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
    string dimensions = "3D",
    string feature = "f1",
    string metric = "euclidean",
    int use_normalize = 0,
    vector3 Vector = P,
    float WIn = 0.0,
    float Scale = 5.0,
    float Detail = 0.0,
    float Roughness = 0.5,
    float Lacunarity = 2.0,
    float Smoothness = 5.0,
    float Exponent = 1.0,
    float Randomness = 1.0,
    output float Distance = 0.0,
    output color Color = 0.0,
    output vector3 Position = P,
    output float WOut = 0.0,
    output float Radius = 0.0)
{
  VoronoiParams params;

  params.feature = feature;
  params.metric = metric;
  params.scale = Scale;
  params.detail = clamp(Detail, 0.0, 15.0);
  params.roughness = clamp(Roughness, 0.0, 1.0);
  params.lacunarity = Lacunarity;
  params.smoothness = clamp(Smoothness / 2.0, 0.0, 0.5);
  params.exponent = Exponent;
  params.randomness = clamp(Randomness, 0.0, 1.0);
  params.max_distance = 0.0;
  params.normalize = use_normalize;

  vector3 coord = Vector;
  if (use_mapping) {
    coord = transform(mapping, coord);
  }

  float w = WIn * Scale;
  coord *= Scale;

  if (feature == "distance_to_edge") {
    params.max_distance = 0.5 + 0.5 * params.randomness;
    if (dimensions == "1D") {
      Distance = fractal_voronoi_distance_to_edge(params, w);
    }
    else if (dimensions == "2D") {
      Distance = fractal_voronoi_distance_to_edge(params, vector2(coord.x, coord.y));
    }
    else if (dimensions == "3D") {
      Distance = fractal_voronoi_distance_to_edge(params, coord);
    }
    else if (dimensions == "4D") {
      Distance = fractal_voronoi_distance_to_edge(params, vector4(coord.x, coord.y, coord.z, w));
    }
    else {
      error("Unknown dimension!");
    }
  }
  else if (feature == "n_sphere_radius") {
    if (dimensions == "1D") {
      Radius = voronoi_n_sphere_radius(params, w);
    }
    else if (dimensions == "2D") {
      Radius = voronoi_n_sphere_radius(params, vector2(coord.x, coord.y));
    }
    else if (dimensions == "3D") {
      Radius = voronoi_n_sphere_radius(params, coord);
    }
    else if (dimensions == "4D") {
      Radius = voronoi_n_sphere_radius(params, vector4(coord.x, coord.y, coord.z, w));
    }
    else {
      error("Unknown dimension!");
    }
  }
  else {
    VoronoiOutput Output;
    if (dimensions == "1D") {
      params.max_distance = (0.5 + 0.5 * params.randomness) *
                            ((params.feature == "f2") ? 2.0 : 1.0);
      Output = fractal_voronoi_x_fx(params, w);
      Distance = Output.Distance;
      Color = Output.Color;
      WOut = Output.Position.w;
    }
    else if (dimensions == "2D") {
      params.max_distance = voronoi_distance(vector2(0.0, 0.0),
                                             vector2(0.5 + 0.5 * params.randomness,
                                                     0.5 + 0.5 * params.randomness),
                                             params) *
                            ((params.feature == "f2") ? 2.0 : 1.0);
      Output = fractal_voronoi_x_fx(params, vector2(coord.x, coord.y));
      Distance = Output.Distance;
      Color = Output.Color;
      Position = vector3(Output.Position.x, Output.Position.y, 0.0);
    }
    else if (dimensions == "3D") {
      params.max_distance = voronoi_distance(vector3(0.0, 0.0, 0.0),
                                             vector3(0.5 + 0.5 * params.randomness,
                                                     0.5 + 0.5 * params.randomness,
                                                     0.5 + 0.5 * params.randomness),
                                             params) *
                            ((params.feature == "f2") ? 2.0 : 1.0);
      Output = fractal_voronoi_x_fx(params, coord);
      Distance = Output.Distance;
      Color = Output.Color;
      Position = vector3(Output.Position.x, Output.Position.y, Output.Position.z);
    }
    else if (dimensions == "4D") {
      params.max_distance = voronoi_distance(vector4(0.0, 0.0, 0.0, 0.0),
                                             vector4(0.5 + 0.5 * params.randomness,
                                                     0.5 + 0.5 * params.randomness,
                                                     0.5 + 0.5 * params.randomness,
                                                     0.5 + 0.5 * params.randomness),
                                             params) *
                            ((params.feature == "f2") ? 2.0 : 1.0);
      Output = fractal_voronoi_x_fx(params, vector4(coord.x, coord.y, coord.z, w));
      Distance = Output.Distance;
      Color = Output.Color;
      Position = vector3(Output.Position.x, Output.Position.y, Output.Position.z);
      WOut = Output.Position.w;
    }
    else {
      error("Unknown dimension!");
    }
  }
}
