[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [32969] branches/particles-2010/source/ blender: Improved random node: Now has a built-in mean value and variance to rescale to more useful ranges .
Lukas Toenne
lukas.toenne at googlemail.com
Tue Nov 9 18:07:17 CET 2010
Revision: 32969
http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=32969
Author: lukastoenne
Date: 2010-11-09 18:07:17 +0100 (Tue, 09 Nov 2010)
Log Message:
-----------
Improved random node: Now has a built-in mean value and variance to rescale to more useful ranges. There are now different distribution types too, currently uniform, gaussian and triangular. Still not thread-safe, needs separate rng per thread for this.
Modified Paths:
--------------
branches/particles-2010/source/blender/editors/space_node/drawnode.c
branches/particles-2010/source/blender/makesdna/DNA_node_types.h
branches/particles-2010/source/blender/makesrna/intern/rna_nodetree.c
branches/particles-2010/source/blender/makesrna/intern/rna_nodetree_types.h
branches/particles-2010/source/blender/nodes/intern/SIM_nodes/SIM_random.c
Modified: branches/particles-2010/source/blender/editors/space_node/drawnode.c
===================================================================
--- branches/particles-2010/source/blender/editors/space_node/drawnode.c 2010-11-09 15:56:26 UTC (rev 32968)
+++ branches/particles-2010/source/blender/editors/space_node/drawnode.c 2010-11-09 17:07:17 UTC (rev 32969)
@@ -1509,11 +1509,17 @@
uiLayout *col;
col= uiLayoutColumn(layout, 0);
- uiLayoutSetContextPointer(col, "node", ptr);
-
uiItemR(col, ptr, "path", 0, NULL, 0);
}
+static void node_simulation_buts_random(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
+{
+ uiLayout *col;
+
+ col= uiLayoutColumn(layout, 0);
+ uiItemR(col, ptr, "distribution", 0, NULL, 0);
+}
+
static void node_simulation_buts_while(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
{
uiLayout *col;
@@ -1540,6 +1546,9 @@
case SIM_NODE_INDEX:
ntype->uifunc = node_simulation_buts_index;
break;
+ case SIM_NODE_RANDOM:
+ ntype->uifunc = node_simulation_buts_random;
+ break;
case SIM_NODE_WHILE:
ntype->uifunc = node_simulation_buts_while;
break;
Modified: branches/particles-2010/source/blender/makesdna/DNA_node_types.h
===================================================================
--- branches/particles-2010/source/blender/makesdna/DNA_node_types.h 2010-11-09 15:56:26 UTC (rev 32968)
+++ branches/particles-2010/source/blender/makesdna/DNA_node_types.h 2010-11-09 17:07:17 UTC (rev 32969)
@@ -395,10 +395,6 @@
int pad;
} SimDataNodeSocket;
-#define SIM_DATA_RNA 1
-#define SIM_DATA_IDPROP 2
-#define SIM_DATA_PARPROP 3
-
typedef struct SimDataNode {
struct StructRNA *type; /* context type of the properties */
/* only used during read/write file! */
@@ -414,4 +410,14 @@
#define CMP_NODE_CHANNEL_MATTE_CS_YUV 3
#define CMP_NODE_CHANNEL_MATTE_CS_YCC 4
+/* data node property socket type */
+#define SIM_DATA_RNA 1
+#define SIM_DATA_IDPROP 2
+#define SIM_DATA_PARPROP 3
+
+/* random number node distribution type */
+#define SIM_RANDOM_UNIFORM 0
+#define SIM_RANDOM_GAUSSIAN 1
+#define SIM_RANDOM_TRIANGULAR 2
+
#endif
Modified: branches/particles-2010/source/blender/makesrna/intern/rna_nodetree.c
===================================================================
--- branches/particles-2010/source/blender/makesrna/intern/rna_nodetree.c 2010-11-09 15:56:26 UTC (rev 32968)
+++ branches/particles-2010/source/blender/makesrna/intern/rna_nodetree.c 2010-11-09 17:07:17 UTC (rev 32969)
@@ -2292,6 +2292,25 @@
RNA_def_property_update(prop, NC_NODE|NA_EDITED, "rna_Node_update");
}
+static EnumPropertyItem sim_random_distribution_items[] = {
+ { SIM_RANDOM_UNIFORM, "SIM_RANDOM_UNIFORM", 0, "Uniform", "" },
+ { SIM_RANDOM_GAUSSIAN, "SIM_RANDOM_GAUSSIAN", 0, "Gaussian", "" },
+ { SIM_RANDOM_TRIANGULAR, "SIM_RANDOM_TRIANGULAR", 0, "Triangular", "" },
+ { -1, NULL, 0, "", "" }
+};
+
+static void def_sim_random(StructRNA *srna)
+{
+ PropertyRNA *prop;
+
+ prop = RNA_def_property(srna, "distribution", PROP_ENUM, PROP_NONE);
+ RNA_def_property_enum_sdna(prop, NULL, "custom1");
+ RNA_def_property_enum_items(prop, sim_random_distribution_items);
+ RNA_def_property_enum_default(prop, SIM_RANDOM_UNIFORM);
+ RNA_def_property_ui_text(prop, "Distribution", "Type of random number distribution");
+ RNA_def_property_update(prop, NC_NODE|NA_EDITED, "rna_Node_update");
+}
+
static void def_sim_while(StructRNA *srna)
{
PropertyRNA *prop;
Modified: branches/particles-2010/source/blender/makesrna/intern/rna_nodetree_types.h
===================================================================
--- branches/particles-2010/source/blender/makesrna/intern/rna_nodetree_types.h 2010-11-09 15:56:26 UTC (rev 32968)
+++ branches/particles-2010/source/blender/makesrna/intern/rna_nodetree_types.h 2010-11-09 17:07:17 UTC (rev 32969)
@@ -167,7 +167,7 @@
DefNode( SimulationNode, SIM_NODE_SETDATA, def_sim_setdata, "SETDATA", SetData, "Set Data", "" )
DefNode( SimulationNode, SIM_NODE_INDEX, def_sim_index, "INDEX", Index, "Index", "" )
DefNode( SimulationNode, SIM_NODE_ADDPARTICLE, 0, "ADDPARTICLE", AddParticle, "Add Particle", "" )
-DefNode( SimulationNode, SIM_NODE_RANDOM, 0, "RANDOM", Random, "Random", "" )
+DefNode( SimulationNode, SIM_NODE_RANDOM, def_sim_random, "RANDOM", Random, "Random", "" )
DefNode( SimulationNode, SIM_NODE_PARTICLEDYNAMICS, 0, "PARTICLEDYNAMICS", ParticleDynamics, "Particle Dynamics", "" )
DefNode( SimulationNode, SIM_NODE_ADDPARTICLEFORCE, 0, "ADDPARTICLEFORCE", AddParticleForce, "Add Particle Force", "" )
DefNode( SimulationNode, SIM_NODE_TIMESTEP, 0, "TIMESTEP", TimeStep, "Time Step", "" )
Modified: branches/particles-2010/source/blender/nodes/intern/SIM_nodes/SIM_random.c
===================================================================
--- branches/particles-2010/source/blender/nodes/intern/SIM_nodes/SIM_random.c 2010-11-09 15:56:26 UTC (rev 32968)
+++ branches/particles-2010/source/blender/nodes/intern/SIM_nodes/SIM_random.c 2010-11-09 17:07:17 UTC (rev 32969)
@@ -35,6 +35,8 @@
static bNodeSocketDefinition inputs[]= {
{ SOCK_INT, 1, "Seed", 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1000000.0f },
{ SOCK_INT, 1, "ID", 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1000000.0f },
+ { SOCK_FLOAT, 1, "Mean Value", 0.0f, 0.0f, 0.0f, 0.0f, -1000000.0f, 1000000.0f },
+ { SOCK_FLOAT, 1, "Variance", 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1000000.0f },
{ -1, 0, "" }
};
@@ -54,11 +56,30 @@
}
static void kernel_random(int global_id, void **args, SimNodeStack *node)
{
- unsigned int offset = SIM_INPUT_INT(1, global_id) + 123456;
- offset *= offset;
- BLI_srandom(SIM_INPUT_INT(0, global_id) + offset);
- /* TODO this is probably NOT threadsafe!! */
- *SIM_OUTPUT_FLOAT(0, global_id) = BLI_frand();
+ float u1, u2, r;
+ /* TODO this is probably NOT threadsafe!!
+ * need to make per-thread info available in kernels and put a RNG for each thread in there.
+ * currently there might be undefined results when a thread switch occurs between seeding and generating.
+ */
+ /* do a double seed to make the output seeded by both "Seed" and "ID" inputs */
+ BLI_srandom(SIM_INPUT_INT(1, global_id));
+ BLI_srandom(SIM_INPUT_INT(0, global_id) + BLI_rand());
+ switch (node->base->custom1) {
+ case SIM_RANDOM_UNIFORM:
+ r = BLI_frand();
+ break;
+ case SIM_RANDOM_GAUSSIAN:
+ /* Box-Muller method */
+ u1 = BLI_frand();
+ u2 = BLI_frand();
+ r = sqrt(-2.0f*log(u1)) * cos(6.28318530717958647693 * u2);
+ break;
+ case SIM_RANDOM_TRIANGULAR:
+ u1 = BLI_frand();
+ r = 1.0f - sqrt(1.0f - u1);
+ break;
+ }
+ *SIM_OUTPUT_FLOAT(0, global_id) = SIM_INPUT_FLOAT(2, global_id) + (2.0f*r - 1.0f) * SIM_INPUT_FLOAT(3, global_id);
}
static void enqueue(SimExecData *execdata, SimNodeStack *node, SimDataContext *self)
@@ -86,7 +107,7 @@
type.nclass = NODE_CLASS_DATA;
/* optional */
-// type.flag = NODE_OPTIONS;
+ type.flag = NODE_OPTIONS;
type.inputs = inputs;
type.outputs = outputs;
More information about the Bf-blender-cvs
mailing list