Cleanup: remove BLI_frand from GreasePencil code.
authorBastien Montagne <montagne29@wanadoo.fr>
Sat, 16 Jun 2018 16:02:02 +0000 (18:02 +0200)
committerBastien Montagne <montagne29@wanadoo.fr>
Sat, 16 Jun 2018 16:02:23 +0000 (18:02 +0200)
source/blender/editors/gpencil/gpencil_brush.c
source/blender/editors/gpencil/gpencil_intern.h
source/blender/editors/gpencil/gpencil_paint.c
source/blender/editors/gpencil/gpencil_utils.c

index c2e532b..f9284d7 100644 (file)
@@ -44,6 +44,8 @@
 #include "BLI_rand.h"
 #include "BLI_utildefines.h"
 
+#include "PIL_time.h"
+
 #include "BLT_translation.h"
 
 #include "DNA_scene_types.h"
@@ -138,6 +140,8 @@ typedef struct tGP_BrushEditData {
        /* Timer for in-place accumulation of brush effect */
        wmTimer *timer;
        bool timerTick; /* is this event from a timer */
+
+       RNG *rng;
 } tGP_BrushEditData;
 
 
@@ -658,7 +662,7 @@ static bool gp_brush_randomize_apply(tGP_BrushEditData *gso, bGPDstroke *gps, in
         * as well as the strength of the brush
         */
        const float inf = gp_brush_influence_calc(gso, radius, co) / 2.0f;
-       const float fac = BLI_frand() * inf;
+       const float fac = BLI_rng_get_float(gso->rng) * inf;
        /* need one flag enabled by default */
        if ((gso->settings->flag & (GP_BRUSHEDIT_FLAG_APPLY_POSITION |
                                    GP_BRUSHEDIT_FLAG_APPLY_STRENGTH |
@@ -685,7 +689,7 @@ static bool gp_brush_randomize_apply(tGP_BrushEditData *gso, bGPDstroke *gps, in
                svec[1] =  mvec[0];
 
                /* scale the displacement by the random displacement, and apply */
-               if (BLI_frand() > 0.5f) {
+               if (BLI_rng_get_float(gso->rng) > 0.5f) {
                        mul_v2_fl(svec, -fac);
                }
                else {
@@ -724,7 +728,7 @@ static bool gp_brush_randomize_apply(tGP_BrushEditData *gso, bGPDstroke *gps, in
        }
        /* apply random to strength */
        if (gso->settings->flag & GP_BRUSHEDIT_FLAG_APPLY_STRENGTH) {
-               if (BLI_frand() > 0.5f) {
+               if (BLI_rng_get_float(gso->rng) > 0.5f) {
                        pt->strength += fac;
                }
                else {
@@ -735,7 +739,7 @@ static bool gp_brush_randomize_apply(tGP_BrushEditData *gso, bGPDstroke *gps, in
        }
        /* apply random to thickness (use pressure) */
        if (gso->settings->flag & GP_BRUSHEDIT_FLAG_APPLY_THICKNESS) {
-               if (BLI_frand() > 0.5f) {
+               if (BLI_rng_get_float(gso->rng) > 0.5f) {
                        pt->pressure += fac;
                }
                else {
@@ -1061,6 +1065,10 @@ static bool gpsculpt_brush_init(bContext *C, wmOperator *op)
 
        gso->brush_type = gso->settings->brushtype;
 
+       /* Random generator, only init once. */
+       uint rng_seed = (uint)(PIL_check_seconds_timer_i() & UINT_MAX);
+       rng_seed ^= GET_UINT_FROM_POINTER(gso);
+       gso->rng = BLI_rng_new(rng_seed);
 
        gso->is_painting = false;
        gso->first = true;
@@ -1163,6 +1171,10 @@ static void gpsculpt_brush_exit(bContext *C, wmOperator *op)
                WM_event_remove_timer(CTX_wm_manager(C), win, gso->timer);
        }
 
+       if (gso->rng != NULL) {
+               BLI_rng_free(gso->rng);
+       }
+
        /* disable cursor and headerprints */
        ED_area_headerprint(CTX_wm_area(C), NULL);
        WM_cursor_modal_restore(win);
index 33af6ca..67b88ef 100644 (file)
@@ -41,6 +41,7 @@ struct bGPDstroke;
 struct bGPDspoint;
 
 struct GHash;
+struct RNG;
 
 struct ARegion;
 struct View2D;
@@ -117,7 +118,7 @@ bool gp_smooth_stroke(bGPDstroke *gps, int i, float inf, bool affect_pressure);
 bool gp_smooth_stroke_strength(bGPDstroke *gps, int i, float inf);
 bool gp_smooth_stroke_thickness(bGPDstroke *gps, int i, float inf);
 void gp_subdivide_stroke(bGPDstroke *gps, const int new_totpoints);
-void gp_randomize_stroke(bGPDstroke *gps, bGPDbrush *brush);
+void gp_randomize_stroke(bGPDstroke *gps, bGPDbrush *brush, struct RNG *rng);
 
 /* Layers Enums -------------------------------------- */
 
index 6afa6f4..d536721 100644 (file)
@@ -169,6 +169,8 @@ typedef struct tGPsdata {
        short straight[2];   /* 1: line horizontal, 2: line vertical, other: not defined, second element position */
        int lock_axis;       /* lock drawing to one axis */
 
+       RNG *rng;
+
        short keymodifier;   /* key used for invoking the operator */
 } tGPsdata;
 
@@ -408,7 +410,8 @@ static void gp_stroke_convertcoords(tGPsdata *p, const int mval[2], float out[3]
 }
 
 /* apply jitter to stroke */
-static void gp_brush_jitter(bGPdata *gpd, bGPDbrush *brush, tGPspoint *pt, const int mval[2], int r_mval[2])
+static void gp_brush_jitter(
+        bGPdata *gpd, bGPDbrush *brush, tGPspoint *pt, const int mval[2], int r_mval[2], RNG *rng)
 {
        float pressure = pt->pressure;
        float tmp_pressure = pt->pressure;
@@ -417,7 +420,7 @@ static void gp_brush_jitter(bGPdata *gpd, bGPDbrush *brush, tGPspoint *pt, const
                tmp_pressure = curvef * brush->draw_sensitivity;
        }
        const float exfactor = (brush->draw_jitter + 2.0f) * (brush->draw_jitter + 2.0f); /* exponential value */
-       const float fac = BLI_frand() * exfactor * tmp_pressure;
+       const float fac = BLI_rng_get_float(rng) * exfactor * tmp_pressure;
        /* Jitter is applied perpendicular to the mouse movement vector (2D space) */
        float mvec[2], svec[2];
        /* mouse movement in ints -> floats */
@@ -434,7 +437,7 @@ static void gp_brush_jitter(bGPdata *gpd, bGPDbrush *brush, tGPspoint *pt, const
        svec[0] = -mvec[1];
        svec[1] = mvec[0];
        /* scale the displacement by the random, and apply */
-       if (BLI_frand() > 0.5f) {
+       if (BLI_rng_get_float(rng) > 0.5f) {
                mul_v2_fl(svec, -fac);
        }
        else {
@@ -550,7 +553,7 @@ static short gp_stroke_addpoint(
                /* Apply jitter to position */
                if (brush->draw_jitter > 0.0f) {
                        int r_mval[2];
-                       gp_brush_jitter(gpd, brush, pt, mval, r_mval);
+                       gp_brush_jitter(gpd, brush, pt, mval, r_mval, p->rng);
                        copy_v2_v2_int(&pt->x, r_mval);
                }
                else {
@@ -560,11 +563,11 @@ static short gp_stroke_addpoint(
                if ((brush->draw_random_press > 0.0f) && (brush->flag & GP_BRUSH_USE_RANDOM_PRESSURE)) {
                        float curvef = curvemapping_evaluateF(brush->cur_sensitivity, 0, pressure);
                        float tmp_pressure = curvef * brush->draw_sensitivity;
-                       if (BLI_frand() > 0.5f) {
-                               pt->pressure -= tmp_pressure * brush->draw_random_press * BLI_frand();
+                       if (BLI_rng_get_float(p->rng) > 0.5f) {
+                               pt->pressure -= tmp_pressure * brush->draw_random_press * BLI_rng_get_float(p->rng);
                        }
                        else {
-                               pt->pressure += tmp_pressure * brush->draw_random_press * BLI_frand();
+                               pt->pressure += tmp_pressure * brush->draw_random_press * BLI_rng_get_float(p->rng);
                        }
                        CLAMP(pt->pressure, GPENCIL_STRENGTH_MIN, 1.0f);
                }
@@ -588,11 +591,11 @@ static short gp_stroke_addpoint(
 
                /* apply randomness to color strength */
                if ((brush->draw_random_press > 0.0f) && (brush->flag & GP_BRUSH_USE_RANDOM_STRENGTH)) {
-                       if (BLI_frand() > 0.5f) {
-                               pt->strength -= pt->strength * brush->draw_random_press * BLI_frand();
+                       if (BLI_rng_get_float(p->rng) > 0.5f) {
+                               pt->strength -= pt->strength * brush->draw_random_press * BLI_rng_get_float(p->rng);
                        }
                        else {
-                               pt->strength += pt->strength * brush->draw_random_press * BLI_frand();
+                               pt->strength += pt->strength * brush->draw_random_press * BLI_rng_get_float(p->rng);
                        }
                        CLAMP(pt->strength, GPENCIL_STRENGTH_MIN, 1.0f);
                }
@@ -978,7 +981,7 @@ static void gp_stroke_newfrombuffer(tGPsdata *p)
                }
                /* apply randomness to stroke */
                if (brush->draw_random_sub > 0.0f) {
-                       gp_randomize_stroke(gps, brush);
+                       gp_randomize_stroke(gps, brush, p->rng);
                }
 
                /* smooth stroke after subdiv - only if there's something to do
@@ -1583,6 +1586,11 @@ static tGPsdata *gp_session_initpaint(bContext *C)
         */
        p->radius = U.gp_eraser;
 
+       /* Random generator, only init once. */
+       uint rng_seed = (uint)(PIL_check_seconds_timer_i() & UINT_MAX);
+       rng_seed ^= GET_UINT_FROM_POINTER(p);
+       p->rng = BLI_rng_new(rng_seed);
+
        /* return context data for running paint operator */
        return p;
 }
@@ -1609,6 +1617,14 @@ static void gp_session_cleanup(tGPsdata *p)
        p->inittime = 0.0;
 }
 
+static void gp_session_free(tGPsdata *p) {
+       if (p->rng != NULL) {
+               BLI_rng_free(p->rng);
+       }
+       MEM_freeN(p);
+}
+
+
 /* init new stroke */
 static void gp_paint_initstroke(tGPsdata *p, eGPencil_PaintModes paintmode, Depsgraph *depsgraph)
 {
@@ -1949,9 +1965,7 @@ static void gpencil_draw_exit(bContext *C, wmOperator *op)
                /* cleanup */
                gp_paint_cleanup(p);
                gp_session_cleanup(p);
-
-               /* finally, free the temp data */
-               MEM_freeN(p);
+               gp_session_free(p);
        }
 
        op->customdata = NULL;
index f6d72d9..4ee3bdd 100644 (file)
@@ -965,7 +965,7 @@ void gp_subdivide_stroke(bGPDstroke *gps, const int new_totpoints)
  * \param gps           Stroke data
  * \param brush         Brush data
  */
-void gp_randomize_stroke(bGPDstroke *gps, bGPDbrush *brush)
+void gp_randomize_stroke(bGPDstroke *gps, bGPDbrush *brush, RNG *rng)
 {
        bGPDspoint *pt1, *pt2, *pt3;
        float v1[3];
@@ -998,10 +998,10 @@ void gp_randomize_stroke(bGPDstroke *gps, bGPDbrush *brush)
        for (int i = 1; i < gps->totpoints - 1; ++i) {
                bGPDspoint *pt = &gps->points[i];
                /* get vector with shift (apply a division because random is too sensitive */
-               const float fac = BLI_frand() * (brush->draw_random_sub / 10.0f);
+               const float fac = BLI_rng_get_float(rng) * (brush->draw_random_sub / 10.0f);
                float svec[3];
                copy_v3_v3(svec, ortho);
-               if (BLI_frand() > 0.5f) {
+               if (BLI_rng_get_float(rng) > 0.5f) {
                        mul_v3_fl(svec, -fac);
                }
                else {