Blender  V2.93
object_random.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2014 by Blender Foundation
17  * All rights reserved.
18  */
19 
24 #include "MEM_guardedalloc.h"
25 
26 #include "DNA_layer_types.h"
27 #include "DNA_object_types.h"
28 
29 #include "BLI_math.h"
30 #include "BLI_rand.h"
31 
32 #include "BKE_context.h"
33 #include "BKE_layer.h"
34 
35 #include "RNA_access.h"
36 #include "RNA_define.h"
37 
38 #include "WM_api.h"
39 #include "WM_types.h"
40 
41 #include "ED_transverts.h"
42 
43 #include "object_intern.h"
44 
50  const float offset,
51  const float uniform,
52  const float normal_factor,
53  const uint seed)
54 {
55  bool use_normal = (normal_factor != 0.0f);
56  struct RNG *rng;
57  TransVert *tv;
58  int a;
59 
60  if (!tvs || !(tvs->transverts)) {
61  return false;
62  }
63 
64  rng = BLI_rng_new(seed);
65 
66  tv = tvs->transverts;
67  for (a = 0; a < tvs->transverts_tot; a++, tv++) {
68  const float t = max_ff(0.0f, uniform + ((1.0f - uniform) * BLI_rng_get_float(rng)));
69  float vec[3];
71 
72  if (use_normal && (tv->flag & TX_VERT_USE_NORMAL)) {
73  float no[3];
74 
75  /* avoid >90d rotation to align with normal */
76  if (dot_v3v3(vec, tv->normal) < 0.0f) {
77  negate_v3_v3(no, tv->normal);
78  }
79  else {
80  copy_v3_v3(no, tv->normal);
81  }
82 
83  interp_v3_v3v3_slerp_safe(vec, vec, no, normal_factor);
84  }
85 
86  madd_v3_v3fl(tv->loc, vec, offset * t);
87  }
88 
90 
91  return true;
92 }
93 
95 {
96  ViewLayer *view_layer = CTX_data_view_layer(C);
97  Object *ob_active = CTX_data_edit_object(C);
98  const int ob_mode = ob_active->mode;
99 
100  const float offset = RNA_float_get(op->ptr, "offset");
101  const float uniform = RNA_float_get(op->ptr, "uniform");
102  const float normal_factor = RNA_float_get(op->ptr, "normal");
103  const uint seed = RNA_int_get(op->ptr, "seed");
104 
105  bool changed_multi = false;
106  uint objects_len = 0;
108  view_layer, CTX_wm_view3d(C), &objects_len, ob_mode);
109  for (uint ob_index = 0; ob_index < objects_len; ob_index++) {
110  Object *ob_iter = objects[ob_index];
111 
112  TransVertStore tvs = {NULL};
113 
114  if (ob_iter) {
115  int mode = TM_ALL_JOINTS;
116 
117  if (normal_factor != 0.0f) {
118  mode |= TX_VERT_USE_NORMAL;
119  }
120 
121  ED_transverts_create_from_obedit(&tvs, ob_iter, mode);
122  if (tvs.transverts_tot == 0) {
123  continue;
124  }
125 
126  int seed_iter = seed;
127  /* This gives a consistent result regardless of object order. */
128  if (ob_index) {
129  seed_iter += BLI_ghashutil_strhash_p(ob_iter->id.name);
130  }
131 
132  object_rand_transverts(&tvs, offset, uniform, normal_factor, seed_iter);
133 
134  ED_transverts_update_obedit(&tvs, ob_iter);
135  ED_transverts_free(&tvs);
136 
138  changed_multi = true;
139  }
140  }
141  MEM_freeN(objects);
142 
143  return changed_multi ? OPERATOR_FINISHED : OPERATOR_CANCELLED;
144 }
145 
147 {
148  /* identifiers */
149  ot->name = "Randomize";
150  ot->description = "Randomize vertices";
151  ot->idname = "TRANSFORM_OT_vertex_random";
152 
153  /* api callbacks */
156 
157  /* flags */
159 
160  /* props */
162  ot->srna, "offset", 0.0f, -FLT_MAX, FLT_MAX, "Amount", "Distance to offset", -10.0f, 10.0f);
164  "uniform",
165  0.0f,
166  0.0f,
167  1.0f,
168  "Uniform",
169  "Increase for uniform offset distance",
170  0.0f,
171  1.0f);
173  "normal",
174  0.0f,
175  0.0f,
176  1.0f,
177  "Normal",
178  "Align offset direction to normals",
179  0.0f,
180  1.0f);
181  RNA_def_int(
182  ot->srna, "seed", 0, 0, 10000, "Random Seed", "Seed for the random number generator", 0, 50);
183 
184  /* Set generic modal callbacks. */
186 }
struct Object * CTX_data_edit_object(const bContext *C)
Definition: context.c:1296
struct ViewLayer * CTX_data_view_layer(const bContext *C)
Definition: context.c:1044
struct View3D * CTX_wm_view3d(const bContext *C)
Definition: context.c:760
#define BKE_view_layer_array_from_objects_in_mode_unique_data(view_layer, v3d, r_len, mode)
Definition: BKE_layer.h:444
unsigned int BLI_ghashutil_strhash_p(const void *ptr)
MINLINE float max_ff(float a, float b)
MINLINE void madd_v3_v3fl(float r[3], const float a[3], float f)
MINLINE void copy_v3_v3(float r[3], const float a[3])
MINLINE void negate_v3_v3(float r[3], const float a[3])
void interp_v3_v3v3_slerp_safe(float target[3], const float a[3], const float b[3], const float t)
Definition: math_vector.c:121
MINLINE float dot_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
Random number functions.
void BLI_rng_free(struct RNG *rng) ATTR_NONNULL(1)
Definition: rand.cc:76
struct RNG * BLI_rng_new(unsigned int seed)
Definition: rand.cc:54
float BLI_rng_get_float(struct RNG *rng) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition: rand.cc:120
void void BLI_rng_get_float_unit_v3(struct RNG *rng, float v[3]) ATTR_NONNULL(1
unsigned int uint
Definition: BLI_sys_types.h:83
Object is a sort of wrapper for general info.
@ OPERATOR_CANCELLED
@ OPERATOR_FINISHED
@ TX_VERT_USE_NORMAL
Definition: ED_transverts.h:74
void ED_transverts_update_obedit(TransVertStore *tvs, struct Object *obedit)
Definition: ed_transverts.c:52
bool ED_transverts_poll(struct bContext *C)
void ED_transverts_free(TransVertStore *tvs)
@ TM_ALL_JOINTS
Definition: ED_transverts.h:64
void ED_transverts_create_from_obedit(TransVertStore *tvs, struct Object *obedit, const int mode)
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble u2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLdouble GLdouble v2 _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLdouble GLdouble nz _GL_VOID_RET _GL_VOID GLfloat GLfloat nz _GL_VOID_RET _GL_VOID GLint GLint nz _GL_VOID_RET _GL_VOID GLshort GLshort nz _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const GLfloat *values _GL_VOID_RET _GL_VOID GLsizei const GLushort *values _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID const GLuint const GLclampf *priorities _GL_VOID_RET _GL_VOID GLdouble y _GL_VOID_RET _GL_VOID GLfloat y _GL_VOID_RET _GL_VOID GLint y _GL_VOID_RET _GL_VOID GLshort y _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLfloat GLfloat z _GL_VOID_RET _GL_VOID GLint GLint z _GL_VOID_RET _GL_VOID GLshort GLshort z _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble w _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat w _GL_VOID_RET _GL_VOID GLint GLint GLint w _GL_VOID_RET _GL_VOID GLshort GLshort GLshort w _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble y2 _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat y2 _GL_VOID_RET _GL_VOID GLint GLint GLint y2 _GL_VOID_RET _GL_VOID GLshort GLshort GLshort y2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLuint *buffer _GL_VOID_RET _GL_VOID GLdouble t _GL_VOID_RET _GL_VOID GLfloat t _GL_VOID_RET _GL_VOID GLint t _GL_VOID_RET _GL_VOID GLshort t _GL_VOID_RET _GL_VOID GLdouble t
Read Guarded memory(de)allocation.
#define C
Definition: RandGen.cpp:39
#define ND_DRAW
Definition: WM_types.h:362
@ OPTYPE_UNDO
Definition: WM_types.h:155
@ OPTYPE_REGISTER
Definition: WM_types.h:153
#define NC_OBJECT
Definition: WM_types.h:280
static unsigned long seed
Definition: btSoftBody.h:39
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
static unsigned a[3]
Definition: RandGen.cpp:92
void TRANSFORM_OT_vertex_random(struct wmOperatorType *ot)
static int object_rand_verts_exec(bContext *C, wmOperator *op)
Definition: object_random.c:94
static bool object_rand_transverts(TransVertStore *tvs, const float offset, const float uniform, const float normal_factor, const uint seed)
Definition: object_random.c:49
int RNA_int_get(PointerRNA *ptr, const char *name)
Definition: rna_access.c:6308
float RNA_float_get(PointerRNA *ptr, const char *name)
Definition: rna_access.c:6355
PropertyRNA * RNA_def_float_distance(StructOrFunctionRNA *cont_, const char *identifier, float default_value, float hardmin, float hardmax, const char *ui_name, const char *ui_description, float softmin, float softmax)
Definition: rna_define.c:4041
PropertyRNA * RNA_def_float_factor(StructOrFunctionRNA *cont_, const char *identifier, float default_value, float hardmin, float hardmax, const char *ui_name, const char *ui_description, float softmin, float softmax)
Definition: rna_define.c:4133
PropertyRNA * RNA_def_int(StructOrFunctionRNA *cont_, const char *identifier, int default_value, int hardmin, int hardmax, const char *ui_name, const char *ui_description, int softmin, int softmax)
Definition: rna_define.c:3585
char name[66]
Definition: DNA_ID.h:283
Definition: rand.cc:48
blender::RandomNumberGenerator rng
Definition: rand.cc:49
struct TransVert * transverts
Definition: ED_transverts.h:40
float * loc
Definition: ED_transverts.h:33
float normal[3]
Definition: ED_transverts.h:35
const char * name
Definition: WM_types.h:721
const char * idname
Definition: WM_types.h:723
bool(* poll)(struct bContext *) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:776
struct StructRNA * srna
Definition: WM_types.h:802
const char * description
Definition: WM_types.h:726
int(* exec)(struct bContext *, struct wmOperator *) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:736
PropertyRNA * prop
Definition: WM_types.h:814
struct PointerRNA * ptr
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
wmOperatorType * ot
Definition: wm_files.c:3156
void WM_operator_type_modal_from_exec_for_object_edit_coords(wmOperatorType *ot)