Merge branch 'master' into blender2.8
[blender.git] / source / blender / blenkernel / intern / effect.c
index a9571406b7eb314b45842c99761acf8cd5c5cfeb..af5089f742fe45512e11fa3e81bab27de8482842 100644 (file)
@@ -30,6 +30,7 @@
  */
 
 #include <stddef.h>
+#include <stdarg.h>
 
 #include <math.h>
 #include <stdlib.h>
 #include "MEM_guardedalloc.h"
 
 #include "DNA_curve_types.h"
-#include "DNA_effect_types.h"
 #include "DNA_group_types.h"
-#include "DNA_ipo_types.h"
-#include "DNA_key_types.h"
-#include "DNA_lattice_types.h"
 #include "DNA_listBase.h"
-#include "DNA_mesh_types.h"
 #include "DNA_meshdata_types.h"
-#include "DNA_material_types.h"
 #include "DNA_object_types.h"
 #include "DNA_object_force.h"
-#include "DNA_particle_types.h"
 #include "DNA_texture_types.h"
 #include "DNA_scene_types.h"
 
 #include "BLI_math.h"
 #include "BLI_blenlib.h"
 #include "BLI_noise.h"
-#include "BLI_jitter.h"
 #include "BLI_rand.h"
 #include "BLI_utildefines.h"
+#include "BLI_ghash.h"
 
 #include "PIL_time.h"
 
-#include "BKE_action.h"
 #include "BKE_anim.h"          /* needed for where_on_path */
-#include "BKE_armature.h"
-#include "BKE_blender.h"
 #include "BKE_collision.h"
-#include "BKE_constraint.h"
-#include "BKE_deform.h"
-#include "BKE_depsgraph.h"
+#include "BKE_curve.h"
 #include "BKE_displist.h"
 #include "BKE_DerivedMesh.h"
 #include "BKE_cdderivedmesh.h"
 #include "BKE_effect.h"
 #include "BKE_global.h"
-#include "BKE_group.h"
-#include "BKE_ipo.h"
-#include "BKE_key.h"
-#include "BKE_lattice.h"
-#include "BKE_mesh.h"
-#include "BKE_material.h"
-#include "BKE_main.h"
+#include "BKE_library.h"
 #include "BKE_modifier.h"
 #include "BKE_object.h"
-#include "BKE_particle.h"
 #include "BKE_scene.h"
 #include "BKE_smoke.h"
 
@@ -93,7 +75,6 @@
 
 /* fluid sim particle import */
 #ifdef WITH_MOD_FLUID
-#include "DNA_object_fluidsim.h"
 #include "LBM_fluidsim.h"
 #include <zlib.h>
 #include <string.h>
@@ -148,12 +129,6 @@ PartDeflect *object_add_collision_fields(int type)
        return pd;
 }
 
-/* temporal struct, used for reading return of mesh_get_mapped_verts_nors() */
-
-typedef struct VeNoCo {
-       float co[3], no[3];
-} VeNoCo;
-
 /* ***************** PARTICLES ***************** */
 
 /* -------------------------- Effectors ------------------ */
@@ -163,64 +138,21 @@ void free_partdeflect(PartDeflect *pd)
                return;
 
        if (pd->tex)
-               pd->tex->id.us--;
+               id_us_min(&pd->tex->id);
 
        if (pd->rng)
-               rng_free(pd->rng);
+               BLI_rng_free(pd->rng);
 
        MEM_freeN(pd);
 }
 
-static void precalculate_effector(EffectorCache *eff)
-{
-       unsigned int cfra = (unsigned int)(eff->scene->r.cfra >= 0 ? eff->scene->r.cfra : -eff->scene->r.cfra);
-       if (!eff->pd->rng)
-               eff->pd->rng = rng_new(eff->pd->seed + cfra);
-       else
-               rng_srandom(eff->pd->rng, eff->pd->seed + cfra);
-
-       if (eff->pd->forcefield == PFIELD_GUIDE && eff->ob->type==OB_CURVE) {
-               Curve *cu= eff->ob->data;
-               if (cu->flag & CU_PATH) {
-                       if (cu->path==NULL || cu->path->data==NULL)
-                               BKE_displist_make_curveTypes(eff->scene, eff->ob, 0);
-
-                       if (cu->path && cu->path->data) {
-                               where_on_path(eff->ob, 0.0, eff->guide_loc, eff->guide_dir, NULL, &eff->guide_radius, NULL);
-                               mul_m4_v3(eff->ob->obmat, eff->guide_loc);
-                               mul_mat3_m4_v3(eff->ob->obmat, eff->guide_dir);
-                       }
-               }
-       }
-       else if (eff->pd->shape == PFIELD_SHAPE_SURFACE) {
-               eff->surmd = (SurfaceModifierData *)modifiers_findByType ( eff->ob, eModifierType_Surface );
-               if (eff->ob->type == OB_CURVE)
-                       eff->flag |= PE_USE_NORMAL_DATA;
-       }
-       else if (eff->psys)
-               psys_update_particle_tree(eff->psys, eff->scene->r.cfra);
-
-       /* Store object velocity */
-       if (eff->ob) {
-               float old_vel[3];
-
-               BKE_object_where_is_calc_time(eff->scene, eff->ob, cfra - 1.0f);
-               copy_v3_v3(old_vel, eff->ob->obmat[3]); 
-               BKE_object_where_is_calc_time(eff->scene, eff->ob, cfra);
-               sub_v3_v3v3(eff->velocity, eff->ob->obmat[3], old_vel);
-       }
-}
-static EffectorCache *new_effector_cache(Scene *scene, Object *ob, ParticleSystem *psys, PartDeflect *pd)
+static EffectorCache *new_effector_cache(Scene *scene, Object *ob, PartDeflect *pd)
 {
        EffectorCache *eff = MEM_callocN(sizeof(EffectorCache), "EffectorCache");
        eff->scene = scene;
        eff->ob = ob;
-       eff->psys = psys;
        eff->pd = pd;
        eff->frame = -1;
-
-       precalculate_effector(eff);
-
        return eff;
 }
 static void add_object_to_effectors(ListBase **effectors, Scene *scene, EffectorWeights *weights, Object *ob, Object *ob_src)
@@ -236,40 +168,17 @@ static void add_object_to_effectors(ListBase **effectors, Scene *scene, Effector
        if (*effectors == NULL)
                *effectors = MEM_callocN(sizeof(ListBase), "effectors list");
 
-       eff = new_effector_cache(scene, ob, NULL, ob->pd);
+       eff = new_effector_cache(scene, ob, ob->pd);
 
        /* make sure imat is up to date */
        invert_m4_m4(ob->imat, ob->obmat);
 
        BLI_addtail(*effectors, eff);
 }
-static void add_particles_to_effectors(ListBase **effectors, Scene *scene, EffectorWeights *weights, Object *ob, ParticleSystem *psys, ParticleSystem *psys_src)
-{
-       ParticleSettings *part= psys->part;
-
-       if ( !psys_check_enabled(ob, psys) )
-               return;
-
-       if ( psys == psys_src && (part->flag & PART_SELF_EFFECT) == 0)
-               return;
-
-       if ( part->pd && part->pd->forcefield && weights->weight[part->pd->forcefield] != 0.0f) {
-               if (*effectors == NULL)
-                       *effectors = MEM_callocN(sizeof(ListBase), "effectors list");
-
-               BLI_addtail(*effectors, new_effector_cache(scene, ob, psys, part->pd));
-       }
-
-       if (part->pd2 && part->pd2->forcefield && weights->weight[part->pd2->forcefield] != 0.0f) {
-               if (*effectors == NULL)
-                       *effectors = MEM_callocN(sizeof(ListBase), "effectors list");
-
-               BLI_addtail(*effectors, new_effector_cache(scene, ob, psys, part->pd2));
-       }
-}
 
 /* returns ListBase handle with objects taking part in the effecting */
-ListBase *pdInitEffectors(Scene *scene, Object *ob_src, ParticleSystem *psys_src, EffectorWeights *weights)
+ListBase *pdInitEffectors(Scene *scene, Object *ob_src,
+                          EffectorWeights *weights, bool precalc)
 {
        Base *base;
        unsigned int layer= ob_src->lay;
@@ -282,13 +191,6 @@ ListBase *pdInitEffectors(Scene *scene, Object *ob_src, ParticleSystem *psys_src
                        if ( (go->ob->lay & layer) ) {
                                if ( go->ob->pd && go->ob->pd->forcefield )
                                        add_object_to_effectors(&effectors, scene, weights, go->ob, ob_src);
-
-                               if ( go->ob->particlesystem.first ) {
-                                       ParticleSystem *psys= go->ob->particlesystem.first;
-
-                                       for ( ; psys; psys=psys->next )
-                                               add_particles_to_effectors(&effectors, scene, weights, go->ob, psys, psys_src);
-                               }
                        }
                }
        }
@@ -296,17 +198,14 @@ ListBase *pdInitEffectors(Scene *scene, Object *ob_src, ParticleSystem *psys_src
                for (base = scene->base.first; base; base= base->next) {
                        if ( (base->lay & layer) ) {
                                if ( base->object->pd && base->object->pd->forcefield )
-                               add_object_to_effectors(&effectors, scene, weights, base->object, ob_src);
-
-                               if ( base->object->particlesystem.first ) {
-                                       ParticleSystem *psys= base->object->particlesystem.first;
-
-                                       for ( ; psys; psys=psys->next )
-                                               add_particles_to_effectors(&effectors, scene, weights, base->object, psys, psys_src);
-                               }
+                                       add_object_to_effectors(&effectors, scene, weights, base->object, ob_src);
                        }
                }
        }
+       
+       if (precalc)
+               pdPrecalculateEffectors(effectors);
+       
        return effectors;
 }
 
@@ -326,37 +225,54 @@ void pdEndEffectors(ListBase **effectors)
        }
 }
 
-
-void pd_point_from_particle(ParticleSimulationData *sim, ParticleData *pa, ParticleKey *state, EffectedPoint *point)
+static void precalculate_effector(EffectorCache *eff)
 {
-       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;
+       unsigned int cfra = (unsigned int)(eff->scene->r.cfra >= 0 ? eff->scene->r.cfra : -eff->scene->r.cfra);
+       if (!eff->pd->rng)
+               eff->pd->rng = BLI_rng_new(eff->pd->seed + cfra);
+       else
+               BLI_rng_srandom(eff->pd->rng, eff->pd->seed + cfra);
 
-       if (part->pd2 && part->pd2->forcefield == PFIELD_CHARGE)
-               point->charge += part->pd2->f_strength;
+       if (eff->pd->forcefield == PFIELD_GUIDE && eff->ob->type==OB_CURVE) {
+               Curve *cu= eff->ob->data;
+               if (cu->flag & CU_PATH) {
+                       if (eff->ob->curve_cache == NULL || eff->ob->curve_cache->path==NULL || eff->ob->curve_cache->path->data==NULL)
+                               BKE_displist_make_curveTypes(eff->scene, eff->ob, 0);
 
-       point->vel_to_sec = 1.0f;
-       point->vel_to_frame = psys_get_timestep(sim);
+                       if (eff->ob->curve_cache->path && eff->ob->curve_cache->path->data) {
+                               where_on_path(eff->ob, 0.0, eff->guide_loc, eff->guide_dir, NULL, &eff->guide_radius, NULL);
+                               mul_m4_v3(eff->ob->obmat, eff->guide_loc);
+                               mul_mat3_m4_v3(eff->ob->obmat, eff->guide_dir);
+                       }
+               }
+       }
+       else if (eff->pd->shape == PFIELD_SHAPE_SURFACE) {
+               eff->surmd = (SurfaceModifierData *)modifiers_findByType( eff->ob, eModifierType_Surface );
+               if (eff->ob->type == OB_CURVE)
+                       eff->flag |= PE_USE_NORMAL_DATA;
+       }
 
-       point->flag = 0;
+       /* Store object velocity */
+       if (eff->ob) {
+               float old_vel[3];
 
-       if (sim->psys->part->flag & PART_ROT_DYN) {
-               point->ave = state->ave;
-               point->rot = state->rot;
+               BKE_object_where_is_calc_time(eff->scene, eff->ob, cfra - 1.0f);
+               copy_v3_v3(old_vel, eff->ob->obmat[3]);
+               BKE_object_where_is_calc_time(eff->scene, eff->ob, cfra);
+               sub_v3_v3v3(eff->velocity, eff->ob->obmat[3], old_vel);
        }
-       else
-               point->ave = point->rot = NULL;
+}
 
-       point->psys = sim->psys;
+void pdPrecalculateEffectors(ListBase *effectors)
+{
+       if (effectors) {
+               EffectorCache *eff = effectors->first;
+               for (; eff; eff=eff->next)
+                       precalculate_effector(eff);
+       }
 }
 
+
 void pd_point_from_loc(Scene *scene, float *loc, float *vel, int index, EffectedPoint *point)
 {
        point->loc = loc;
@@ -370,7 +286,6 @@ void pd_point_from_loc(Scene *scene, float *loc, float *vel, int index, Effected
        point->flag = 0;
 
        point->ave = point->rot = NULL;
-       point->psys = NULL;
 }
 void pd_point_from_soft(Scene *scene, float *loc, float *vel, int index, EffectedPoint *point)
 {
@@ -385,8 +300,6 @@ void pd_point_from_soft(Scene *scene, float *loc, float *vel, int index, Effecte
        point->flag = PE_WIND_AS_SPEED;
 
        point->ave = point->rot = NULL;
-
-       point->psys = NULL;
 }
 /************************************************/
 /*                     Effectors               */
@@ -403,6 +316,7 @@ static void eff_tri_ray_hit(void *UNUSED(userData), int UNUSED(index), const BVH
 // 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;
        ColliderCache *col;
        float norm[3], len = 0.0;
@@ -434,7 +348,10 @@ static float eff_calc_visibility(ListBase *colliders, EffectorCache *eff, Effect
                        hit.dist = len + FLT_EPSILON;
 
                        /* check if the way is blocked */
-                       if (BLI_bvhtree_ray_cast(collmd->bvhtree, point->loc, norm, 0.0f, &hit, eff_tri_ray_hit, NULL)>=0) {
+                       if (BLI_bvhtree_ray_cast_ex(
+                               collmd->bvhtree, point->loc, norm, 0.0f, &hit,
+                               eff_tri_ray_hit, NULL, raycast_flag) != -1)
+                       {
                                absorption= col->ob->pd->absorption;
 
                                /* visibility is only between 0 and 1, calculated from 1-absorption */
@@ -455,8 +372,8 @@ static float eff_calc_visibility(ListBase *colliders, EffectorCache *eff, Effect
 // noise function for wind e.g.
 static float wind_func(struct RNG *rng, float strength)
 {
-       int random = (rng_getInt(rng)+1) % 128; // max 2357
-       float force = rng_getFloat(rng) + 1.0f;
+       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;
        
@@ -495,7 +412,7 @@ 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 *UNUSED(point), EffectorWeights *weights)
+static float effector_falloff(EffectorCache *eff, EffectorData *efd, EffectedPoint *UNUSED(point), EffectorWeights *weights)
 {
        float temp[3];
        float falloff = weights ? weights->weight[0] * weights->weight[eff->pd->forcefield] : 1.0f;
@@ -518,7 +435,7 @@ float effector_falloff(EffectorCache *eff, EffectorData *efd, EffectedPoint *UNU
                        if (falloff == 0.0f)
                                break;
 
-                       madd_v3_v3v3fl(temp, efd->vec_to_point, efd->nor, -fac);
+                       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;
@@ -542,7 +459,7 @@ int closest_point_on_surface(SurfaceModifierData *surmd, const float co[3], floa
        BVHTreeNearest nearest;
 
        nearest.index = -1;
-       nearest.dist = FLT_MAX;
+       nearest.dist_sq = FLT_MAX;
 
        BLI_bvhtree_find_nearest(surmd->bvhtree->tree, co, &nearest, surmd->bvhtree->nearest_callback, surmd->bvhtree);
 
@@ -554,15 +471,14 @@ int closest_point_on_surface(SurfaceModifierData *surmd, const float co[3], floa
                }
 
                if (surface_vel) {
-                       MFace *mface = CDDM_get_tessface(surmd->dm, nearest.index);
+                       const MLoop *mloop = surmd->bvhtree->loop;
+                       const MLoopTri *lt = &surmd->bvhtree->looptri[nearest.index];
                        
-                       copy_v3_v3(surface_vel, surmd->v[mface->v1].co);
-                       add_v3_v3(surface_vel, surmd->v[mface->v2].co);
-                       add_v3_v3(surface_vel, surmd->v[mface->v3].co);
-                       if (mface->v4)
-                               add_v3_v3(surface_vel, surmd->v[mface->v4].co);
+                       copy_v3_v3(surface_vel, surmd->v[mloop[lt->tri[0]].v].co);
+                       add_v3_v3(surface_vel, surmd->v[mloop[lt->tri[1]].v].co);
+                       add_v3_v3(surface_vel, surmd->v[mloop[lt->tri[2]].v].co);
 
-                       mul_v3_fl(surface_vel, mface->v4 ? 0.25f : 0.333f);
+                       mul_v3_fl(surface_vel, (1.0f / 3.0f));
                }
                return 1;
        }
@@ -571,7 +487,6 @@ int closest_point_on_surface(SurfaceModifierData *surmd, const float co[3], floa
 }
 int get_effector_data(EffectorCache *eff, EffectorData *efd, EffectedPoint *point, int real_velocity)
 {
-       float cfra = eff->scene->r.cfra;
        int ret = 0;
 
        if (eff->pd && eff->pd->shape==PFIELD_SHAPE_SURFACE && eff->surmd) {
@@ -606,47 +521,9 @@ int get_effector_data(EffectorCache *eff, EffectorData *efd, EffectedPoint *poin
                        ret = 1;
                }
        }
-       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= {NULL};
-                       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, 0);
-
-                       /* 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.f;
-                       efd->nor[1] = efd->nor[2] = 0.f;
-                       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 */
-               Object *ob = eff->ob;
-               Object obcopy = *ob;
+               const Object *ob = eff->ob;
 
                /* use z-axis as normal*/
                normalize_v3_v3(efd->nor, ob->obmat[2]);
@@ -669,8 +546,6 @@ int get_effector_data(EffectorCache *eff, EffectorData *efd, EffectedPoint *poin
                if (real_velocity)
                        copy_v3_v3(efd->vel, eff->velocity);
 
-               *eff->ob = obcopy;
-
                efd->size = 0.0f;
 
                ret = 1;
@@ -697,12 +572,12 @@ int get_effector_data(EffectorCache *eff, EffectorData *efd, EffectedPoint *poin
 
        return ret;
 }
-static void get_effector_tot(EffectorCache *eff, EffectorData *efd, EffectedPoint *point, int *tot, int *p, int *step)
+static void get_effector_tot(EffectorCache *eff, EffectorData *efd, EffectedPoint *point, int *tot, int *p)
 {
-       if (eff->pd->shape == PFIELD_SHAPE_POINTS) {
-               efd->index = p;
+       *p = 0;
+       efd->index = p;
 
-               *p = 0;
+       if (eff->pd->shape == PFIELD_SHAPE_POINTS) {
                *tot = eff->ob->derivedFinal ? eff->ob->derivedFinal->numVertData : 1;
 
                if (*tot && eff->pd->forcefield == PFIELD_HARMONIC && point->index >= 0) {
@@ -710,36 +585,7 @@ static void get_effector_tot(EffectorCache *eff, EffectorData *efd, EffectedPoin
                        *tot = *p+1;
                }
        }
-       else if (eff->psys) {
-               efd->index = p;
-
-               *p = 0;
-               *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 unphysical, 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) ? totpart/amount : 1;
-               }
-       }
        else {
-               *p = 0;
                *tot = 1;
        }
 }
@@ -750,6 +596,7 @@ static void do_texture_effector(EffectorCache *eff, EffectorData *efd, EffectedP
        float nabla = eff->pd->tex_nabla;
        int hasrgb;
        short mode = eff->pd->tex_mode;
+       bool scene_color_manage;
 
        if (!eff->pd->tex)
                return;
@@ -760,16 +607,20 @@ static void do_texture_effector(EffectorCache *eff, EffectorData *efd, EffectedP
 
        copy_v3_v3(tex_co, point->loc);
 
-       if (eff->pd->flag & PFIELD_TEX_2D) {
+       if (eff->pd->flag & PFIELD_TEX_OBJECT) {
+               mul_m4_v3(eff->ob->imat, 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);
        }
 
-       if (eff->pd->flag & PFIELD_TEX_OBJECT) {
-               mul_m4_v3(eff->ob->imat, tex_co);
-       }
+       scene_color_manage = BKE_scene_check_color_management_enabled(eff->scene);
 
-       hasrgb = multitex_ext(eff->pd->tex, tex_co, NULL, NULL, 0, result);
+       hasrgb = multitex_ext(eff->pd->tex, tex_co, NULL, NULL, 0, result, 0, NULL, scene_color_manage, false);
 
        if (hasrgb && mode==PFIELD_TEX_RGB) {
                force[0] = (0.5f - result->tr) * strength;
@@ -780,15 +631,15 @@ static void do_texture_effector(EffectorCache *eff, EffectorData *efd, EffectedP
                strength/=nabla;
 
                tex_co[0] += nabla;
-               multitex_ext(eff->pd->tex, tex_co, NULL, NULL, 0, result+1);
+               multitex_ext(eff->pd->tex, tex_co, NULL, NULL, 0, result+1, 0, NULL, scene_color_manage, false);
 
                tex_co[0] -= nabla;
                tex_co[1] += nabla;
-               multitex_ext(eff->pd->tex, tex_co, NULL, NULL, 0, result+2);
+               multitex_ext(eff->pd->tex, tex_co, NULL, NULL, 0, result+2, 0, NULL, scene_color_manage, false);
 
                tex_co[1] -= nabla;
                tex_co[2] += nabla;
-               multitex_ext(eff->pd->tex, tex_co, NULL, NULL, 0, result+3);
+               multitex_ext(eff->pd->tex, tex_co, NULL, NULL, 0, result+3, 0, NULL, scene_color_manage, 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 */
@@ -828,7 +679,7 @@ static void do_physical_effector(EffectorCache *eff, EffectorData *efd, Effected
 {
        PartDeflect *pd = eff->pd;
        RNG *rng = pd->rng;
-       float force[3]={0, 0, 0};
+       float force[3] = {0, 0, 0};
        float temp[3];
        float fac;
        float strength = pd->f_strength;
@@ -887,7 +738,7 @@ static void do_physical_effector(EffectorCache *eff, EffectorData *efd, Effected
                case PFIELD_HARMONIC:
                        mul_v3_fl(force, -strength * efd->falloff);
                        copy_v3_v3(temp, point->vel);
-                       mul_v3_fl(temp, -damp * 2.0f * (float)sqrt(fabs(strength)) * point->vel_to_sec);
+                       mul_v3_fl(temp, -damp * 2.0f * sqrtf(fabsf(strength)) * point->vel_to_sec);
                        add_v3_v3(force, temp);
                        break;
                case PFIELD_CHARGE:
@@ -947,7 +798,7 @@ static void do_physical_effector(EffectorCache *eff, EffectorData *efd, Effected
        if (pd->flag & PFIELD_DO_LOCATION) {
                madd_v3_v3fl(total_force, force, 1.0f/point->vel_to_sec);
 
-               if (ELEM3(pd->forcefield, PFIELD_HARMONIC, PFIELD_DRAG, PFIELD_SMOKEFLOW)==0 && pd->f_flow != 0.0f) {
+               if (ELEM(pd->forcefield, PFIELD_HARMONIC, PFIELD_DRAG, PFIELD_SMOKEFLOW)==0 && pd->f_flow != 0.0f) {
                        madd_v3_v3fl(total_force, point->vel, -pd->f_flow * efd->falloff);
                }
        }
@@ -996,7 +847,7 @@ void pdDoEffectors(ListBase *effectors, ListBase *colliders, EffectorWeights *we
  */
        EffectorCache *eff;
        EffectorData efd;
-       int p=0, tot = 1, step = 1;
+       int p=0, tot = 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) */
@@ -1004,21 +855,23 @@ void pdDoEffectors(ListBase *effectors, ListBase *colliders, EffectorWeights *we
        if (effectors) for (eff = effectors->first; eff; eff=eff->next) {
                /* object effectors were fully checked to be OK to evaluate! */
 
-               get_effector_tot(eff, &efd, point, &tot, &p, &step);
+               get_effector_tot(eff, &efd, point, &tot, &p);
 
-               for (; p<tot; p+=step) {
+               for (; p<tot; p++) {
                        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)
-                                       ;       /* don't do anything */
-                               else if (eff->pd->forcefield == PFIELD_TEXTURE)
+                               if (efd.falloff <= 0.0f) {
+                                       /* don't do anything */
+                               }
+                               else if (eff->pd->forcefield == PFIELD_TEXTURE) {
                                        do_texture_effector(eff, &efd, point, force);
+                               }
                                else {
-                                       float temp1[3]={0, 0, 0}, temp2[3];
+                                       float temp1[3] = {0, 0, 0}, temp2[3];
                                        copy_v3_v3(temp1, force);
 
                                        do_physical_effector(eff, &efd, point, force);
@@ -1037,3 +890,160 @@ void pdDoEffectors(ListBase *effectors, ListBase *colliders, EffectorWeights *we
                }
        }
 }
+
+/* ======== Simulation Debugging ======== */
+
+SimDebugData *_sim_debug_data = NULL;
+
+unsigned int BKE_sim_debug_data_hash(int i)
+{
+       return BLI_ghashutil_uinthash((unsigned int)i);
+}
+
+unsigned int BKE_sim_debug_data_hash_combine(unsigned int kx, unsigned int ky)
+{
+#define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
+
+       unsigned int 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 unsigned int debug_element_hash(const void *key)
+{
+       const SimDebugElement *elem = key;
+       return elem->hash;
+}
+
+static bool debug_element_compare(const void *a, const void *b)
+{
+       const SimDebugElement *elem1 = a;
+       const SimDebugElement *elem2 = b;
+
+       if (elem1->hash == elem2->hash) {
+               return 0;
+       }
+       return 1;
+}
+
+static void debug_element_free(void *val)
+{
+       SimDebugElement *elem = val;
+       MEM_freeN(elem);
+}
+
+void BKE_sim_debug_data_set_enabled(bool enable)
+{
+       if (enable) {
+               if (!_sim_debug_data) {
+                       _sim_debug_data = MEM_callocN(sizeof(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(void)
+{
+       return _sim_debug_data != NULL;
+}
+
+void BKE_sim_debug_data_free(void)
+{
+       if (_sim_debug_data) {
+               if (_sim_debug_data->gh)
+                       BLI_ghash_free(_sim_debug_data->gh, NULL, debug_element_free);
+               MEM_freeN(_sim_debug_data);
+       }
+}
+
+static void debug_data_insert(SimDebugData *debug_data, SimDebugElement *elem)
+{
+       SimDebugElement *old_elem = 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], float r, float g, float b, const char *category, unsigned int hash)
+{
+       unsigned int 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(sizeof(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;
+       copy_v3_v3(elem->v1, v1);
+       copy_v3_v3(elem->v2, v2);
+       
+       debug_data_insert(_sim_debug_data, elem);
+}
+
+void BKE_sim_debug_data_remove_element(unsigned int hash)
+{
+       SimDebugElement dummy;
+       if (!_sim_debug_data)
+               return;
+       
+       dummy.hash = hash;
+       BLI_ghash_remove(_sim_debug_data->gh, &dummy, NULL, debug_element_free);
+}
+
+void BKE_sim_debug_data_clear(void)
+{
+       if (!_sim_debug_data)
+               return;
+       
+       if (_sim_debug_data->gh)
+               BLI_ghash_clear(_sim_debug_data->gh, NULL, 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 = BLI_ghashIterator_getValue(&iter);
+                       BLI_ghashIterator_step(&iter); /* removing invalidates the current iterator, so step before removing */
+                       
+                       if (elem->category_hash == category_hash)
+                               BLI_ghash_remove(_sim_debug_data->gh, elem, NULL, debug_element_free);
+               }
+       }
+}