/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
 *
 * SPDX-License-Identifier: GPL-2.0-or-later */

/** \file
 * \ingroup bke
 */

#include <algorithm>
#include <cmath>
#include <cstdarg>
#include <cstddef>
#include <cstdlib>

#include "MEM_guardedalloc.h"

#include "DNA_curve_types.h"
#include "DNA_listBase.h"
#include "DNA_mesh_types.h"
#include "DNA_object_force_types.h"
#include "DNA_object_types.h"
#include "DNA_particle_types.h"
#include "DNA_scene_types.h"
#include "DNA_texture_types.h"

#include "BLI_ghash.h"
#include "BLI_listbase.h"
#include "BLI_math_base_safe.h"
#include "BLI_math_matrix.h"
#include "BLI_math_rotation.h"
#include "BLI_math_vector.h"
#include "BLI_noise.h"
#include "BLI_rand.h"
#include "BLI_string.h"
#include "BLI_time.h"
#include "BLI_utildefines.h"

#include "BKE_anim_path.h" /* needed for where_on_path */
#include "BKE_bvhutils.hh"
#include "BKE_collection.hh"
#include "BKE_collision.h"
#include "BKE_curve.hh"
#include "BKE_displist.h"
#include "BKE_effect.h"
#include "BKE_fluid.h"
#include "BKE_global.hh"
#include "BKE_modifier.hh"
#include "BKE_object.hh"
#include "BKE_object_types.hh"
#include "BKE_particle.h"

#include "DEG_depsgraph.hh"
#include "DEG_depsgraph_physics.hh"
#include "DEG_depsgraph_query.hh"

#include "RE_texture.h"

EffectorWeights *BKE_effector_add_weights(Collection *collection)
{
  EffectorWeights *weights = MEM_callocN<EffectorWeights>("EffectorWeights");
  for (int i = 0; i < NUM_PFIELD_TYPES; i++) {
    weights->weight[i] = 1.0f;
  }

  weights->global_gravity = 1.0f;

  weights->group = collection;

  return weights;
}
PartDeflect *BKE_partdeflect_new(int type)
{
  PartDeflect *pd;

  pd = MEM_callocN<PartDeflect>("PartDeflect");

  pd->forcefield = type;
  pd->pdef_sbdamp = 0.1f;
  pd->pdef_sbift = 0.2f;
  pd->pdef_sboft = 0.02f;
  pd->pdef_cfrict = 5.0f;
  pd->seed = (uint(ceil(BLI_time_now_seconds())) + 1) % 128;
  pd->f_strength = 1.0f;
  pd->f_damp = 1.0f;

  /* set sensible defaults based on type */
  switch (type) {
    case PFIELD_VORTEX:
      pd->shape = PFIELD_SHAPE_PLANE;
      break;
    case PFIELD_WIND:
      pd->shape = PFIELD_SHAPE_PLANE;
      pd->f_flow = 1.0f;        /* realistic wind behavior */
      pd->f_wind_factor = 1.0f; /* only act perpendicularly to a surface */
      break;
    case PFIELD_TEXTURE:
      pd->f_size = 1.0f;
      break;
    case PFIELD_FLUIDFLOW:
      pd->f_flow = 1.0f;
      break;
  }
  pd->flag = PFIELD_DO_LOCATION | PFIELD_DO_ROTATION | PFIELD_CLOTH_USE_CULLING;

  return pd;
}

/************************ PARTICLES ***************************/

PartDeflect *BKE_partdeflect_copy(const PartDeflect *pd_src)
{
  if (pd_src == nullptr) {
    return nullptr;
  }
  PartDeflect *pd_dst = static_cast<PartDeflect *>(MEM_dupallocN(pd_src));
  return pd_dst;
}

void BKE_partdeflect_free(PartDeflect *pd)
{
  if (!pd) {
    return;
  }
  MEM_freeN(pd);
}

/******************** EFFECTOR RELATIONS ***********************/

static void precalculate_effector(Depsgraph *depsgraph, EffectorCache *eff)
{
  float ctime = DEG_get_ctime(depsgraph);
  uint cfra = uint(ctime >= 0 ? ctime : -ctime);

  eff->rng = BLI_rng_new(eff->pd->seed + cfra);

  if (eff->pd->forcefield == PFIELD_GUIDE && eff->ob->type == OB_CURVES_LEGACY) {
    Curve *cu = static_cast<Curve *>(eff->ob->data);
    if (cu->flag & CU_PATH) {
      if (eff->ob->runtime->curve_cache == nullptr ||
          eff->ob->runtime->curve_cache->anim_path_accum_length == nullptr)
      {
        BKE_displist_make_curveTypes(depsgraph, eff->scene, eff->ob, false);
      }

      if (eff->ob->runtime->curve_cache->anim_path_accum_length) {
        BKE_where_on_path(
            eff->ob, 0.0, eff->guide_loc, eff->guide_dir, nullptr, &eff->guide_radius, nullptr);
        mul_m4_v3(eff->ob->object_to_world().ptr(), eff->guide_loc);
        mul_mat3_m4_v3(eff->ob->object_to_world().ptr(), eff->guide_dir);
      }
    }
  }
  else if (eff->pd->shape == PFIELD_SHAPE_SURFACE) {
    eff->surmd = (SurfaceModifierData *)BKE_modifiers_findby_type(eff->ob, eModifierType_Surface);
    if (eff->ob->type == OB_CURVES_LEGACY) {
      eff->flag |= PE_USE_NORMAL_DATA;
    }
  }
  else if (eff->psys) {
    psys_update_particle_tree(eff->psys, ctime);
  }
}

static void add_effector_relation(ListBase *relations,
                                  Object *ob,
                                  ParticleSystem *psys,
                                  PartDeflect *pd)
{
  EffectorRelation *relation = MEM_callocN<EffectorRelation>("EffectorRelation");
  relation->ob = ob;
  relation->psys = psys;
  relation->pd = pd;

  BLI_addtail(relations, relation);
}

static void add_effector_evaluation(ListBase **effectors,
                                    Depsgraph *depsgraph,
                                    Scene *scene,
                                    Object *ob,
                                    ParticleSystem *psys,
                                    PartDeflect *pd)
{
  if (*effectors == nullptr) {
    *effectors = MEM_callocN<ListBase>("effector effectors");
  }

  EffectorCache *eff = MEM_callocN<EffectorCache>("EffectorCache");
  eff->depsgraph = depsgraph;
  eff->scene = scene;
  eff->ob = ob;
  eff->psys = psys;
  eff->pd = pd;
  eff->frame = -1;
  BLI_addtail(*effectors, eff);

  precalculate_effector(depsgraph, eff);
}

ListBase *BKE_effector_relations_create(Depsgraph *depsgraph,
                                        const Scene *scene,
                                        ViewLayer *view_layer,
                                        Collection *collection)
{
  Base *base = BKE_collection_or_layer_objects(scene, view_layer, collection);
  const bool for_render = (DEG_get_mode(depsgraph) == DAG_EVAL_RENDER);
  const int base_flag = (for_render) ? BASE_ENABLED_RENDER : BASE_ENABLED_VIEWPORT;

  ListBase *relations = MEM_callocN<ListBase>("effector relations");

  for (; base; base = base->next) {
    if (!(base->flag & base_flag)) {
      continue;
    }

    Object *ob = base->object;

    if (ob->pd && ob->pd->forcefield) {
      add_effector_relation(relations, ob, nullptr, ob->pd);
    }

    LISTBASE_FOREACH (ParticleSystem *, psys, &ob->particlesystem) {
      ParticleSettings *part = psys->part;

      if (psys_check_enabled(ob, psys, for_render)) {
        if (part->pd && part->pd->forcefield) {
          add_effector_relation(relations, ob, psys, part->pd);
        }
        if (part->pd2 && part->pd2->forcefield) {
          add_effector_relation(relations, ob, psys, part->pd2);
        }
      }
    }
  }

  return relations;
}

void BKE_effector_relations_free(ListBase *lb)
{
  if (lb) {
    BLI_freelistN(lb);
    MEM_freeN(lb);
  }
}

/* Check that the force field isn't disabled via its flags. */
static bool is_effector_enabled(PartDeflect *pd, bool use_rotation)
{
  switch (pd->forcefield) {
    case PFIELD_BOID:
    case PFIELD_GUIDE:
      return true;

    case PFIELD_TEXTURE:
      return (pd->flag & PFIELD_DO_LOCATION) != 0 && pd->tex != nullptr;

    default:
      if (use_rotation) {
        return (pd->flag & (PFIELD_DO_LOCATION | PFIELD_DO_ROTATION)) != 0;
      }
      else {
        return (pd->flag & PFIELD_DO_LOCATION) != 0;
      }
  }
}

/* Check that the force field won't have zero effect due to strength settings. */
static bool is_effector_nonzero_strength(PartDeflect *pd)
{
  if (pd->f_strength != 0.0f) {
    return true;
  }

  if (pd->forcefield == PFIELD_TEXTURE) {
    return false;
  }

  if (pd->f_noise > 0.0f || pd->f_flow != 0.0f) {
    return true;
  }

  switch (pd->forcefield) {
    case PFIELD_BOID:
    case PFIELD_GUIDE:
      return true;

    case PFIELD_VORTEX:
      return pd->shape != PFIELD_SHAPE_POINT;

    case PFIELD_DRAG:
      return pd->f_damp != 0.0f;

    default:
      return false;
  }
}

/* Check if the force field will affect its user. */
static bool is_effector_relevant(PartDeflect *pd, EffectorWeights *weights, bool use_rotation)
{
  return (weights->weight[pd->forcefield] != 0.0f) && is_effector_enabled(pd, use_rotation) &&
         is_effector_nonzero_strength(pd);
}

ListBase *BKE_effectors_create(Depsgraph *depsgraph,
                               Object *ob_src,
                               ParticleSystem *psys_src,
                               EffectorWeights *weights,
                               bool use_rotation)
{
  Scene *scene = DEG_get_evaluated_scene(depsgraph);
  ListBase *relations = DEG_get_effector_relations(depsgraph, weights->group);
  ListBase *effectors = nullptr;

  if (!relations) {
    return nullptr;
  }

  LISTBASE_FOREACH (EffectorRelation *, relation, relations) {
    /* Get evaluated object. */
    Object *ob = DEG_get_evaluated(depsgraph, relation->ob);

    if (relation->psys) {
      /* Get evaluated particle system. */
      ParticleSystem *psys = static_cast<ParticleSystem *>(BLI_findstring(
          &ob->particlesystem, relation->psys->name, offsetof(ParticleSystem, name)));
      ParticleSettings *part = psys->part;

      if (psys == psys_src && (part->flag & PART_SELF_EFFECT) == 0) {
        continue;
      }

      PartDeflect *pd = (relation->pd == relation->psys->part->pd) ? part->pd : part->pd2;

      if (!is_effector_relevant(pd, weights, use_rotation)) {
        continue;
      }

      add_effector_evaluation(&effectors, depsgraph, scene, ob, psys, pd);
    }
    else {
      /* Object effector. */
      if (ob == ob_src) {
        continue;
      }
      if (!is_effector_relevant(ob->pd, weights, use_rotation)) {
        continue;
      }
      if (ob->pd->shape == PFIELD_SHAPE_POINTS && BKE_object_get_evaluated_mesh(ob) == nullptr) {
        continue;
      }

      add_effector_evaluation(&effectors, depsgraph, scene, ob, nullptr, ob->pd);
    }
  }

  return effectors;
}

void BKE_effectors_free(ListBase *lb)
{
  if (lb) {
    LISTBASE_FOREACH (EffectorCache *, eff, lb) {
      if (eff->rng) {
        BLI_rng_free(eff->rng);
      }
      if (eff->guide_data) {
        MEM_freeN(eff->guide_data);
      }
    }

    BLI_freelistN(lb);
    MEM_freeN(lb);
  }
}

void pd_point_from_particle(ParticleSimulationData *sim,
                            ParticleData *pa,
                            ParticleKey *state,
                            EffectedPoint *point)
{
  ParticleSettings *part = sim->psys->part;
  point->loc = state->co;
  point->vel = state->vel;
  point->index = pa - sim->psys->particles;
  point->size = pa->size;
  point->charge = 0.0f;

  if (part->pd && part->pd->forcefield == PFIELD_CHARGE) {
    point->charge += part->pd->f_strength;
  }

  if (part->pd2 && part->pd2->forcefield == PFIELD_CHARGE) {
    point->charge += part->pd2->f_strength;
  }

  point->vel_to_sec = 1.0f;
  point->vel_to_frame = psys_get_timestep(sim);

  point->flag = 0;

  if (sim->psys->part->flag & PART_ROT_DYN) {
    point->ave = state->ave;
    point->rot = state->rot;
  }
  else {
    point->ave = point->rot = nullptr;
  }

  point->psys = sim->psys;
}

void pd_point_from_loc(Scene *scene, float *loc, float *vel, int index, EffectedPoint *point)
{
  point->loc = loc;
  point->vel = vel;
  point->index = index;
  point->size = 0.0f;

  point->vel_to_sec = float(scene->r.frs_sec);
  point->vel_to_frame = 1.0f;

  point->flag = 0;

  point->ave = point->rot = nullptr;
  point->psys = nullptr;
}
void pd_point_from_soft(Scene *scene, float *loc, float *vel, int index, EffectedPoint *point)
{
  point->loc = loc;
  point->vel = vel;
  point->index = index;
  point->size = 0.0f;

  point->vel_to_sec = float(scene->r.frs_sec);
  point->vel_to_frame = 1.0f;

  point->flag = PE_WIND_AS_SPEED;

  point->ave = point->rot = nullptr;

  point->psys = nullptr;
}
/************************************************/
/*          Effectors       */
/************************************************/

// triangle - ray callback function
static void eff_tri_ray_hit(void * /*user_data*/,
                            int /*index*/,
                            const BVHTreeRay * /*ray*/,
                            BVHTreeRayHit *hit)
{
  /* whenever we hit a bounding box, we don't check further */
  hit->dist = -1;
  hit->index = 1;
}

/**
 * Get visibility of a wind ray.
 */
static float eff_calc_visibility(ListBase *colliders,
                                 EffectorCache *eff,
                                 EffectorData *efd,
                                 EffectedPoint *point)
{
  const int raycast_flag = BVH_RAYCAST_DEFAULT & ~BVH_RAYCAST_WATERTIGHT;
  ListBase *colls = colliders;
  float norm[3], len = 0.0;
  float visibility = 1.0, absorption = 0.0;

  if (!(eff->pd->flag & PFIELD_VISIBILITY)) {
    return visibility;
  }
  if (!colls) {
    colls = BKE_collider_cache_create(eff->depsgraph, eff->ob, nullptr);
  }
  if (!colls) {
    return visibility;
  }

  negate_v3_v3(norm, efd->vec_to_point);
  len = normalize_v3(norm);

  /* check all collision objects */
  LISTBASE_FOREACH (ColliderCache *, col, colls) {
    CollisionModifierData *collmd = col->collmd;

    if (col->ob == eff->ob) {
      continue;
    }
    if (collmd->bvhtree) {
      BVHTreeRayHit hit;

      hit.index = -1;
      hit.dist = len + FLT_EPSILON;

      /* check if the way is blocked */
      if (BLI_bvhtree_ray_cast_ex(collmd->bvhtree,
                                  point->loc,
                                  norm,
                                  0.0f,
                                  &hit,
                                  eff_tri_ray_hit,
                                  nullptr,
                                  raycast_flag) != -1)
      {
        absorption = col->ob->pd->absorption;

        /* visibility is only between 0 and 1, calculated from 1-absorption */
        visibility *= std::clamp(1.0f - absorption, 0.0f, 1.0f);

        if (visibility <= 0.0f) {
          break;
        }
      }
    }
  }

  if (!colliders) {
    BKE_collider_cache_free(&colls);
  }

  return visibility;
}

/* Noise function for wind e.g. */
static float wind_func(RNG *rng, float strength)
{
  int random = (BLI_rng_get_int(rng) + 1) % 128; /* max 2357 */
  float force = BLI_rng_get_float(rng) + 1.0f;
  float ret;
  float sign = 0;

  /* Dividing by 2 is not giving equal sign distribution. */
  sign = (float(random) > 64.0f) ? 1.0f : -1.0f;

  ret = sign * (float(random) / force) * strength / 128.0f;

  return ret;
}

/* maxdist: zero effect from this distance outwards (if usemax) */
/* mindist: full effect up to this distance (if usemin) */
/* power: falloff with formula 1/r^power */
static float falloff_func(
    float fac, int usemin, float mindist, int usemax, float maxdist, float power)
{
  /* first quick checks */
  if (usemax && fac > maxdist) {
    return 0.0f;
  }

  if (usemin && fac < mindist) {
    return 1.0f;
  }

  if (!usemin) {
    mindist = 0.0;
  }

  return pow(double(1.0f + fac - mindist), double(-power));
}

static float falloff_func_dist(PartDeflect *pd, float fac)
{
  return falloff_func(fac,
                      pd->flag & PFIELD_USEMIN,
                      pd->mindist,
                      pd->flag & PFIELD_USEMAX,
                      pd->maxdist,
                      pd->f_power);
}

static float falloff_func_rad(PartDeflect *pd, float fac)
{
  return falloff_func(fac,
                      pd->flag & PFIELD_USEMINR,
                      pd->minrad,
                      pd->flag & PFIELD_USEMAXR,
                      pd->maxrad,
                      pd->f_power_r);
}

float effector_falloff(EffectorCache *eff,
                       EffectorData *efd,
                       EffectedPoint * /*point*/,
                       EffectorWeights *weights)
{
  float temp[3];
  float falloff = weights ? weights->weight[0] * weights->weight[eff->pd->forcefield] : 1.0f;
  float fac, r_fac;

  fac = dot_v3v3(efd->nor, efd->vec_to_point2);

  if (eff->pd->zdir == PFIELD_Z_POS && fac < 0.0f) {
    falloff = 0.0f;
  }
  else if (eff->pd->zdir == PFIELD_Z_NEG && fac > 0.0f) {
    falloff = 0.0f;
  }
  else {
    switch (eff->pd->falloff) {
      case PFIELD_FALL_SPHERE:
        falloff *= falloff_func_dist(eff->pd, efd->distance);
        break;

      case PFIELD_FALL_TUBE:
        falloff *= falloff_func_dist(eff->pd, fabsf(fac));
        if (falloff == 0.0f) {
          break;
        }

        madd_v3_v3v3fl(temp, efd->vec_to_point2, efd->nor, -fac);
        r_fac = len_v3(temp);
        falloff *= falloff_func_rad(eff->pd, r_fac);
        break;
      case PFIELD_FALL_CONE:
        falloff *= falloff_func_dist(eff->pd, fabsf(fac));
        if (falloff == 0.0f) {
          break;
        }

        r_fac = RAD2DEGF(safe_acosf(fac / len_v3(efd->vec_to_point2)));
        falloff *= falloff_func_rad(eff->pd, r_fac);

        break;
    }
  }

  return falloff;
}

bool closest_point_on_surface(SurfaceModifierData *surmd,
                              const float co[3],
                              float surface_co[3],
                              float surface_nor[3],
                              float surface_vel[3])
{
  blender::bke::BVHTreeFromMesh *bvhtree = surmd->runtime.bvhtree;
  BVHTreeNearest nearest;

  nearest.index = -1;
  nearest.dist_sq = FLT_MAX;

  BLI_bvhtree_find_nearest(bvhtree->tree, co, &nearest, bvhtree->nearest_callback, bvhtree);

  if (nearest.index != -1) {
    copy_v3_v3(surface_co, nearest.co);

    if (surface_nor) {
      copy_v3_v3(surface_nor, nearest.no);
    }

    if (surface_vel) {
      const int *corner_verts = bvhtree->corner_verts.data();
      const blender::int3 &tri = bvhtree->corner_tris[nearest.index];

      copy_v3_v3(surface_vel, surmd->runtime.vert_velocities[corner_verts[tri[0]]]);
      add_v3_v3(surface_vel, surmd->runtime.vert_velocities[corner_verts[tri[1]]]);
      add_v3_v3(surface_vel, surmd->runtime.vert_velocities[corner_verts[tri[2]]]);

      mul_v3_fl(surface_vel, (1.0f / 3.0f));
    }
    return true;
  }

  return false;
}
bool get_effector_data(EffectorCache *eff,
                       EffectorData *efd,
                       EffectedPoint *point,
                       int real_velocity)
{
  float cfra = DEG_get_ctime(eff->depsgraph);
  bool ret = false;

  /* In case surface object is in Edit mode when loading the .blend,
   * surface modifier is never executed and bvhtree never built, see #48415. */
  if (eff->pd && eff->pd->shape == PFIELD_SHAPE_SURFACE && eff->surmd &&
      eff->surmd->runtime.bvhtree)
  {
    /* closest point in the object surface is an effector */
    float vec[3];

    /* using velocity corrected location allows for easier sliding over effector surface */
    copy_v3_v3(vec, point->vel);
    mul_v3_fl(vec, point->vel_to_frame);
    add_v3_v3(vec, point->loc);

    ret = closest_point_on_surface(
        eff->surmd, vec, efd->loc, efd->nor, real_velocity ? efd->vel : nullptr);

    efd->size = 0.0f;
  }
  else if (eff->pd && eff->pd->shape == PFIELD_SHAPE_POINTS) {
    /* TODO: hair and points object support */
    const Mesh *mesh_eval = BKE_object_get_evaluated_mesh(eff->ob);
    if (mesh_eval != nullptr) {
      const blender::Span<blender::float3> positions = mesh_eval->vert_positions();
      const blender::Span<blender::float3> vert_normals = mesh_eval->vert_normals();
      copy_v3_v3(efd->loc, positions[*efd->index]);
      copy_v3_v3(efd->nor, vert_normals[*efd->index]);

      mul_m4_v3(eff->ob->object_to_world().ptr(), efd->loc);
      mul_mat3_m4_v3(eff->ob->object_to_world().ptr(), efd->nor);

      normalize_v3(efd->nor);

      efd->size = 0.0f;

      ret = true;
    }
  }
  else if (eff->psys) {
    ParticleData *pa = eff->psys->particles + *efd->index;
    ParticleKey state;

    /* exclude the particle itself for self effecting particles */
    if (eff->psys == point->psys && *efd->index == point->index) {
      /* pass */
    }
    else {
      ParticleSimulationData sim = {nullptr};
      sim.depsgraph = eff->depsgraph;
      sim.scene = eff->scene;
      sim.ob = eff->ob;
      sim.psys = eff->psys;

      /* TODO: time from actual previous calculated frame (step might not be 1) */
      state.time = cfra - 1.0f;
      ret = psys_get_particle_state(&sim, *efd->index, &state, false);

      /* TODO */
      // if (eff->pd->forcefiled == PFIELD_HARMONIC && ret==0) {
      //  if (pa->dietime < eff->psys->cfra)
      //      eff->flag |= PE_VELOCITY_TO_IMPULSE;
      //}

      copy_v3_v3(efd->loc, state.co);

      /* rather than use the velocity use rotated x-axis (defaults to velocity) */
      efd->nor[0] = 1.0f;
      efd->nor[1] = efd->nor[2] = 0.0f;
      mul_qt_v3(state.rot, efd->nor);

      if (real_velocity) {
        copy_v3_v3(efd->vel, state.vel);
      }
      efd->size = pa->size;
    }
  }
  else {
    /* use center of object for distance calculus */
    const Object *ob = eff->ob;

    /* Use z-axis as normal. */
    normalize_v3_v3(efd->nor, ob->object_to_world().ptr()[2]);

    if (eff->pd && ELEM(eff->pd->shape, PFIELD_SHAPE_PLANE, PFIELD_SHAPE_LINE)) {
      float temp[3], translate[3];
      sub_v3_v3v3(temp, point->loc, ob->object_to_world().location());
      project_v3_v3v3(translate, temp, efd->nor);

      /* for vortex the shape chooses between old / new force */
      if (eff->pd->forcefield == PFIELD_VORTEX || eff->pd->shape == PFIELD_SHAPE_LINE) {
        add_v3_v3v3(efd->loc, ob->object_to_world().location(), translate);
      }
      else { /* Normally `efd->loc` is closest point on effector XY-plane. */
        sub_v3_v3v3(efd->loc, point->loc, translate);
      }
    }
    else {
      copy_v3_v3(efd->loc, ob->object_to_world().location());
    }

    zero_v3(efd->vel);
    efd->size = 0.0f;

    ret = true;
  }

  if (ret) {
    sub_v3_v3v3(efd->vec_to_point, point->loc, efd->loc);
    efd->distance = len_v3(efd->vec_to_point);

    /* Rest length for harmonic effector,
     * will have to see later if this could be extended to other effectors. */
    if (eff->pd && eff->pd->forcefield == PFIELD_HARMONIC && eff->pd->f_size) {
      mul_v3_fl(efd->vec_to_point, (efd->distance - eff->pd->f_size) / efd->distance);
    }

    if (eff->flag & PE_USE_NORMAL_DATA) {
      copy_v3_v3(efd->vec_to_point2, efd->vec_to_point);
      copy_v3_v3(efd->nor2, efd->nor);
    }
    else {
      /* for some effectors we need the object center every time */
      sub_v3_v3v3(efd->vec_to_point2, point->loc, eff->ob->object_to_world().location());
      normalize_v3_v3(efd->nor2, eff->ob->object_to_world().ptr()[2]);
    }
  }

  return ret;
}
static void get_effector_tot(
    EffectorCache *eff, EffectorData *efd, EffectedPoint *point, int *tot, int *p, int *step)
{
  *p = 0;
  efd->index = p;

  if (eff->pd->shape == PFIELD_SHAPE_POINTS) {
    /* TODO: hair and points object support */
    const Mesh *mesh_eval = BKE_object_get_evaluated_mesh(eff->ob);
    *tot = mesh_eval != nullptr ? mesh_eval->verts_num : 1;

    if (*tot && eff->pd->forcefield == PFIELD_HARMONIC && point->index >= 0) {
      *p = point->index % *tot;
      *tot = *p + 1;
    }
  }
  else if (eff->psys) {
    *tot = eff->psys->totpart;

    if (eff->pd->forcefield == PFIELD_CHARGE) {
      /* Only the charge of the effected particle is used for
       * interaction, not fall-offs. If the fall-offs aren't the
       * same this will be nonphysical, but for animation this
       * could be the wanted behavior. If you want physical
       * correctness the fall-off should be spherical 2.0 anyways.
       */
      efd->charge = eff->pd->f_strength;
    }
    else if (eff->pd->forcefield == PFIELD_HARMONIC &&
             (eff->pd->flag & PFIELD_MULTIPLE_SPRINGS) == 0)
    {
      /* every particle is mapped to only one harmonic effector particle */
      *p = point->index % eff->psys->totpart;
      *tot = *p + 1;
    }

    if (eff->psys->part->effector_amount) {
      int totpart = eff->psys->totpart;
      int amount = eff->psys->part->effector_amount;

      *step = (totpart > amount) ? int(ceil(float(totpart) / float(amount))) : 1;
    }
  }
  else {
    *tot = 1;
  }
}
static void do_texture_effector(EffectorCache *eff,
                                EffectorData *efd,
                                EffectedPoint *point,
                                float *total_force)
{
  TexResult result[4];
  float tex_co[3], strength, force[3];
  float nabla = eff->pd->tex_nabla;
  int hasrgb;
  short mode = eff->pd->tex_mode;

  if (!eff->pd->tex) {
    return;
  }

  strength = eff->pd->f_strength * efd->falloff;

  copy_v3_v3(tex_co, point->loc);

  if (eff->pd->flag & PFIELD_TEX_OBJECT) {
    mul_m4_v3(eff->ob->world_to_object().ptr(), tex_co);

    if (eff->pd->flag & PFIELD_TEX_2D) {
      tex_co[2] = 0.0f;
    }
  }
  else if (eff->pd->flag & PFIELD_TEX_2D) {
    float fac = -dot_v3v3(tex_co, efd->nor);
    madd_v3_v3fl(tex_co, efd->nor, fac);
  }

  hasrgb = multitex_ext(
      eff->pd->tex, tex_co, nullptr, nullptr, 0, result, 0, nullptr, true, false);

  if (hasrgb && mode == PFIELD_TEX_RGB) {
    force[0] = (0.5f - result->trgba[0]) * strength;
    force[1] = (0.5f - result->trgba[1]) * strength;
    force[2] = (0.5f - result->trgba[2]) * strength;
  }
  else if (nabla != 0) {
    strength /= nabla;

    tex_co[0] += nabla;
    multitex_ext(eff->pd->tex, tex_co, nullptr, nullptr, 0, result + 1, 0, nullptr, true, false);

    tex_co[0] -= nabla;
    tex_co[1] += nabla;
    multitex_ext(eff->pd->tex, tex_co, nullptr, nullptr, 0, result + 2, 0, nullptr, true, false);

    tex_co[1] -= nabla;
    tex_co[2] += nabla;
    multitex_ext(eff->pd->tex, tex_co, nullptr, nullptr, 0, result + 3, 0, nullptr, true, false);

    if (mode == PFIELD_TEX_GRAD || !hasrgb) { /* if we don't have rgb fall back to grad */
      /* generate intensity if texture only has rgb value */
      if (hasrgb & TEX_RGB) {
        for (int i = 0; i < 4; i++) {
          result[i].tin = (1.0f / 3.0f) *
                          (result[i].trgba[0] + result[i].trgba[1] + result[i].trgba[2]);
        }
      }
      force[0] = (result[0].tin - result[1].tin) * strength;
      force[1] = (result[0].tin - result[2].tin) * strength;
      force[2] = (result[0].tin - result[3].tin) * strength;
    }
    else { /*PFIELD_TEX_CURL*/
      float dbdy, dgdz, drdz, dbdx, dgdx, drdy;

      dbdy = result[2].trgba[2] - result[0].trgba[2];
      dgdz = result[3].trgba[1] - result[0].trgba[1];
      drdz = result[3].trgba[0] - result[0].trgba[0];
      dbdx = result[1].trgba[2] - result[0].trgba[2];
      dgdx = result[1].trgba[1] - result[0].trgba[1];
      drdy = result[2].trgba[0] - result[0].trgba[0];

      force[0] = (dbdy - dgdz) * strength;
      force[1] = (drdz - dbdx) * strength;
      force[2] = (dgdx - drdy) * strength;
    }
  }
  else {
    zero_v3(force);
  }

  if (eff->pd->flag & PFIELD_TEX_2D) {
    float fac = -dot_v3v3(force, efd->nor);
    madd_v3_v3fl(force, efd->nor, fac);
  }

  if (eff->pd->flag & PFIELD_DO_LOCATION) {
    add_v3_v3(total_force, force);
  }
}
static void do_physical_effector(EffectorCache *eff,
                                 EffectorData *efd,
                                 EffectedPoint *point,
                                 float *total_force)
{
  PartDeflect *pd = eff->pd;
  RNG *rng = eff->rng;
  float force[3] = {0, 0, 0};
  float temp[3];
  float fac;
  float strength = pd->f_strength;
  float damp = pd->f_damp;
  float noise_factor = pd->f_noise;
  float flow_falloff = efd->falloff;

  if (noise_factor > 0.0f) {
    strength += wind_func(rng, noise_factor);

    if (ELEM(pd->forcefield, PFIELD_HARMONIC, PFIELD_DRAG)) {
      damp += wind_func(rng, noise_factor);
    }
  }

  copy_v3_v3(force, efd->vec_to_point);

  switch (pd->forcefield) {
    case PFIELD_WIND:
      copy_v3_v3(force, efd->nor);
      mul_v3_fl(force, strength * efd->falloff);
      break;
    case PFIELD_FORCE:
      normalize_v3(force);
      if (pd->flag & PFIELD_GRAVITATION) { /* Option: Multiply by 1/distance^2 */
        if (efd->distance < FLT_EPSILON) {
          strength = 0.0f;
        }
        else {
          strength *= powf(efd->distance, -2.0f);
        }
      }
      mul_v3_fl(force, strength * efd->falloff);
      break;
    case PFIELD_VORTEX:
      /* old vortex force */
      if (pd->shape == PFIELD_SHAPE_POINT) {
        cross_v3_v3v3(force, efd->nor, efd->vec_to_point);
        normalize_v3(force);
        mul_v3_fl(force, strength * efd->distance * efd->falloff);
      }
      else {
        /* new vortex force */
        cross_v3_v3v3(temp, efd->nor2, efd->vec_to_point2);
        mul_v3_fl(temp, strength * efd->falloff);

        cross_v3_v3v3(force, efd->nor2, temp);
        mul_v3_fl(force, strength * efd->falloff);

        madd_v3_v3fl(temp, point->vel, -point->vel_to_sec);
        add_v3_v3(force, temp);
      }
      break;
    case PFIELD_MAGNET:
      if (ELEM(eff->pd->shape, PFIELD_SHAPE_POINT, PFIELD_SHAPE_LINE)) {
        /* magnetic field of a moving charge */
        cross_v3_v3v3(temp, efd->nor, efd->vec_to_point);
      }
      else {
        copy_v3_v3(temp, efd->nor);
      }

      normalize_v3(temp);
      mul_v3_fl(temp, strength * efd->falloff);
      cross_v3_v3v3(force, point->vel, temp);
      mul_v3_fl(force, point->vel_to_sec);
      break;
    case PFIELD_HARMONIC:
      mul_v3_fl(force, -strength * efd->falloff);
      copy_v3_v3(temp, point->vel);
      mul_v3_fl(temp, -damp * 2.0f * sqrtf(fabsf(strength)) * point->vel_to_sec);
      add_v3_v3(force, temp);
      break;
    case PFIELD_CHARGE:
      mul_v3_fl(force, point->charge * strength * efd->falloff);
      break;
    case PFIELD_LENNARDJ:
      fac = pow((efd->size + point->size) / efd->distance, 6.0);

      fac = -fac * (1.0f - fac) / efd->distance;

      /* limit the repulsive term drastically to avoid huge forces */
      fac = ((fac > 2.0f) ? 2.0f : fac);

      mul_v3_fl(force, strength * fac);
      break;
    case PFIELD_BOID:
      /* Boid field is handled completely in boids code. */
      return;
    case PFIELD_TURBULENCE:
      if (pd->flag & PFIELD_GLOBAL_CO) {
        copy_v3_v3(temp, point->loc);
      }
      else {
        add_v3_v3v3(temp, efd->vec_to_point2, efd->nor2);
      }
      force[0] = -1.0f + 2.0f * BLI_noise_generic_turbulence(
                                    pd->f_size, temp[0], temp[1], temp[2], 2, false, 2);
      force[1] = -1.0f + 2.0f * BLI_noise_generic_turbulence(
                                    pd->f_size, temp[1], temp[2], temp[0], 2, false, 2);
      force[2] = -1.0f + 2.0f * BLI_noise_generic_turbulence(
                                    pd->f_size, temp[2], temp[0], temp[1], 2, false, 2);
      mul_v3_fl(force, strength * efd->falloff);
      break;
    case PFIELD_DRAG:
      copy_v3_v3(force, point->vel);
      fac = normalize_v3(force) * point->vel_to_sec;

      strength = std::min(strength, 2.0f);
      damp = std::min(damp, 2.0f);

      mul_v3_fl(force, -efd->falloff * fac * (strength * fac + damp));
      break;
    case PFIELD_FLUIDFLOW:
      zero_v3(force);
      flow_falloff = 0;
#ifdef WITH_FLUID
      if (pd->f_source) {
        const float density = BKE_fluid_get_velocity_at(pd->f_source, point->loc, force);
        if (density >= 0.0f) {
          float influence = strength * efd->falloff;
          if (pd->flag & PFIELD_SMOKE_DENSITY) {
            influence *= density;
          }
          mul_v3_fl(force, influence);
          flow_falloff = influence;
        }
      }
#endif
      break;
  }

  if (pd->flag & PFIELD_DO_LOCATION) {
    madd_v3_v3fl(total_force, force, 1.0f / point->vel_to_sec);

    if (!ELEM(pd->forcefield, PFIELD_HARMONIC, PFIELD_DRAG) && pd->f_flow != 0.0f) {
      madd_v3_v3fl(total_force, point->vel, -pd->f_flow * flow_falloff);
    }
  }

  if (pd->flag & PFIELD_DO_ROTATION && point->ave && point->rot) {
    float xvec[3] = {1.0f, 0.0f, 0.0f};
    float dave[3];
    mul_qt_v3(point->rot, xvec);
    cross_v3_v3v3(dave, xvec, force);
    if (pd->f_flow != 0.0f) {
      madd_v3_v3fl(dave, point->ave, -pd->f_flow * efd->falloff);
    }
    add_v3_v3(point->ave, dave);
  }
}

void BKE_effectors_apply(ListBase *effectors,
                         ListBase *colliders,
                         EffectorWeights *weights,
                         EffectedPoint *point,
                         float *force,
                         float *wind_force,
                         float *impulse)
{
  /* WARNING(@ideasman42): historic comment?
   * Many of these parameters don't exist!
   *
   * `scene`      = scene where it runs in, for time and stuff.
   * `lb`         = listbase with objects that take part in effecting.
   * `opco`       = global coord, as input.
   * `force`      = accumulator for force.
   * `wind_force` = accumulator for force only acting perpendicular to a surface.
   * `speed`      = actual current speed which can be altered.
   * `cur_time`   = "external" time in frames, is constant for static particles.
   * `loc_time`   = "local" time in frames, range <0-1> for the lifetime of particle.
   * `par_layer`  = layer the caller is in.
   * `flags`      = only used for soft-body wind now.
   * `guide`      = old speed of particle.
   */

  /*
   * Modifies the force on a particle according to its
   * relation with the effector object
   * Different kind of effectors include:
   * - Force-fields: Gravity-like attractor
   *   (force power is related to the inverse of distance to the power of a falloff value)
   * - Vortex fields: swirling effectors
   *   (particles rotate around Z-axis of the object. otherwise, same relation as)
   *   (Force-fields, but this is not done through a force/acceleration)
   * - Guide: particles on a path
   *   (particles are guided along a curve bezier or old nurbs)
   *   (is independent of other effectors)
   */
  EffectorData efd;
  int p = 0, tot = 1, step = 1;

  /* Cycle through collected objects, get total of (1/(gravity_strength * dist^gravity_power)) */
  /* Check for min distance here? (yes would be cool to add that, ton) */

  if (effectors) {
    LISTBASE_FOREACH (EffectorCache *, eff, effectors) {
      /* object effectors were fully checked to be OK to evaluate! */

      get_effector_tot(eff, &efd, point, &tot, &p, &step);

      for (; p < tot; p += step) {
        if (get_effector_data(eff, &efd, point, 0)) {
          efd.falloff = effector_falloff(eff, &efd, point, weights);

          if (efd.falloff > 0.0f) {
            efd.falloff *= eff_calc_visibility(colliders, eff, &efd, point);
          }
          if (efd.falloff > 0.0f) {
            float out_force[3] = {0, 0, 0};

            if (eff->pd->forcefield == PFIELD_TEXTURE) {
              do_texture_effector(eff, &efd, point, out_force);
            }
            else {
              do_physical_effector(eff, &efd, point, out_force);

              /* for softbody backward compatibility */
              if (point->flag & PE_WIND_AS_SPEED && impulse) {
                sub_v3_v3v3(impulse, impulse, out_force);
              }
            }

            if (wind_force) {
              madd_v3_v3fl(force, out_force, 1.0f - eff->pd->f_wind_factor);
              madd_v3_v3fl(wind_force, out_force, eff->pd->f_wind_factor);
            }
            else {
              add_v3_v3(force, out_force);
            }
          }
        }
        else if (eff->flag & PE_VELOCITY_TO_IMPULSE && impulse) {
          /* special case for harmonic effector */
          add_v3_v3v3(impulse, impulse, efd.vel);
        }
      }
    }
  }
}

/* ======== Simulation Debugging ======== */

SimDebugData *_sim_debug_data = nullptr;

uint BKE_sim_debug_data_hash(int i)
{
  return BLI_ghashutil_uinthash(uint(i));
}

uint BKE_sim_debug_data_hash_combine(uint kx, uint ky)
{
#define rot(x, k) (((x) << (k)) | ((x) >> (32 - (k))))

  uint a, b, c;

  a = b = c = 0xdeadbeef + (2 << 2) + 13;
  a += kx;
  b += ky;

  c ^= b;
  c -= rot(b, 14);
  a ^= c;
  a -= rot(c, 11);
  b ^= a;
  b -= rot(a, 25);
  c ^= b;
  c -= rot(b, 16);
  a ^= c;
  a -= rot(c, 4);
  b ^= a;
  b -= rot(a, 14);
  c ^= b;
  c -= rot(b, 24);

  return c;

#undef rot
}

static uint debug_element_hash(const void *key)
{
  const SimDebugElement *elem = static_cast<const SimDebugElement *>(key);
  return elem->hash;
}

static bool debug_element_compare(const void *a, const void *b)
{
  const SimDebugElement *elem1 = static_cast<const SimDebugElement *>(a);
  const SimDebugElement *elem2 = static_cast<const SimDebugElement *>(b);

  if (elem1->hash == elem2->hash) {
    return false;
  }
  return true;
}

static void debug_element_free(void *val)
{
  SimDebugElement *elem = static_cast<SimDebugElement *>(val);
  MEM_freeN(elem);
}

void BKE_sim_debug_data_set_enabled(bool enable)
{
  if (enable) {
    if (!_sim_debug_data) {
      _sim_debug_data = MEM_callocN<SimDebugData>("sim debug data");
      _sim_debug_data->gh = BLI_ghash_new(
          debug_element_hash, debug_element_compare, "sim debug element hash");
    }
  }
  else {
    BKE_sim_debug_data_free();
  }
}

bool BKE_sim_debug_data_get_enabled()
{
  return _sim_debug_data != nullptr;
}

void BKE_sim_debug_data_free()
{
  if (_sim_debug_data) {
    if (_sim_debug_data->gh) {
      BLI_ghash_free(_sim_debug_data->gh, nullptr, debug_element_free);
    }
    MEM_freeN(_sim_debug_data);
  }
}

static void debug_data_insert(SimDebugData *debug_data, SimDebugElement *elem)
{
  SimDebugElement *old_elem = static_cast<SimDebugElement *>(
      BLI_ghash_lookup(debug_data->gh, elem));
  if (old_elem) {
    *old_elem = *elem;
    MEM_freeN(elem);
  }
  else {
    BLI_ghash_insert(debug_data->gh, elem, elem);
  }
}

void BKE_sim_debug_data_add_element(int type,
                                    const float v1[3],
                                    const float v2[3],
                                    const char *str,
                                    float r,
                                    float g,
                                    float b,
                                    const char *category,
                                    uint hash)
{
  uint category_hash = BLI_ghashutil_strhash_p(category);
  SimDebugElement *elem;

  if (!_sim_debug_data) {
    if (G.debug & G_DEBUG_SIMDATA) {
      BKE_sim_debug_data_set_enabled(true);
    }
    else {
      return;
    }
  }

  elem = MEM_callocN<SimDebugElement>("sim debug data element");
  elem->type = type;
  elem->category_hash = category_hash;
  elem->hash = hash;
  elem->color[0] = r;
  elem->color[1] = g;
  elem->color[2] = b;
  if (v1) {
    copy_v3_v3(elem->v1, v1);
  }
  else {
    zero_v3(elem->v1);
  }
  if (v2) {
    copy_v3_v3(elem->v2, v2);
  }
  else {
    zero_v3(elem->v2);
  }
  if (str) {
    STRNCPY(elem->str, str);
  }
  else {
    elem->str[0] = '\0';
  }

  debug_data_insert(_sim_debug_data, elem);
}

void BKE_sim_debug_data_remove_element(uint hash)
{
  SimDebugElement dummy;
  if (!_sim_debug_data) {
    return;
  }
  dummy.hash = hash;
  BLI_ghash_remove(_sim_debug_data->gh, &dummy, nullptr, debug_element_free);
}

void BKE_sim_debug_data_clear()
{
  if (!_sim_debug_data) {
    return;
  }
  if (_sim_debug_data->gh) {
    BLI_ghash_clear(_sim_debug_data->gh, nullptr, debug_element_free);
  }
}

void BKE_sim_debug_data_clear_category(const char *category)
{
  int category_hash = int(BLI_ghashutil_strhash_p(category));

  if (!_sim_debug_data) {
    return;
  }

  if (_sim_debug_data->gh) {
    GHashIterator iter;
    BLI_ghashIterator_init(&iter, _sim_debug_data->gh);
    while (!BLI_ghashIterator_done(&iter)) {
      const SimDebugElement *elem = static_cast<const SimDebugElement *>(
          BLI_ghashIterator_getValue(&iter));

      /* Removing invalidates the current iterator, so step before removing. */
      BLI_ghashIterator_step(&iter);

      if (elem->category_hash == category_hash) {
        BLI_ghash_remove(_sim_debug_data->gh, elem, nullptr, debug_element_free);
      }
    }
  }
}
