Blender  V3.3
jitter_2d.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2001-2002 NaN Holding BV. All rights reserved. */
3 
9 #include "MEM_guardedalloc.h"
10 #include <math.h>
11 #include <string.h>
12 
13 #include "BLI_jitter_2d.h"
14 #include "BLI_rand.h"
15 
16 #include "BLI_strict_flags.h"
17 
18 void BLI_jitterate1(float (*jit1)[2], float (*jit2)[2], int num, float radius1)
19 {
20  int i, j, k;
21  float vecx, vecy, dvecx, dvecy, x, y, len;
22 
23  for (i = num - 1; i >= 0; i--) {
24  dvecx = dvecy = 0.0;
25  x = jit1[i][0];
26  y = jit1[i][1];
27  for (j = num - 1; j >= 0; j--) {
28  if (i != j) {
29  vecx = jit1[j][0] - x - 1.0f;
30  vecy = jit1[j][1] - y - 1.0f;
31  for (k = 3; k > 0; k--) {
32  if (fabsf(vecx) < radius1 && fabsf(vecy) < radius1) {
33  len = sqrtf(vecx * vecx + vecy * vecy);
34  if (len > 0 && len < radius1) {
35  len = len / radius1;
36  dvecx += vecx / len;
37  dvecy += vecy / len;
38  }
39  }
40  vecx += 1.0f;
41 
42  if (fabsf(vecx) < radius1 && fabsf(vecy) < radius1) {
43  len = sqrtf(vecx * vecx + vecy * vecy);
44  if (len > 0 && len < radius1) {
45  len = len / radius1;
46  dvecx += vecx / len;
47  dvecy += vecy / len;
48  }
49  }
50  vecx += 1.0f;
51 
52  if (fabsf(vecx) < radius1 && fabsf(vecy) < radius1) {
53  len = sqrtf(vecx * vecx + vecy * vecy);
54  if (len > 0 && len < radius1) {
55  len = len / radius1;
56  dvecx += vecx / len;
57  dvecy += vecy / len;
58  }
59  }
60  vecx -= 2.0f;
61  vecy += 1.0f;
62  }
63  }
64  }
65 
66  x -= dvecx / 18.0f;
67  y -= dvecy / 18.0f;
68  x -= floorf(x);
69  y -= floorf(y);
70  jit2[i][0] = x;
71  jit2[i][1] = y;
72  }
73  memcpy(jit1, jit2, 2 * (unsigned int)num * sizeof(float));
74 }
75 
76 void BLI_jitterate2(float (*jit1)[2], float (*jit2)[2], int num, float radius2)
77 {
78  int i, j;
79  float vecx, vecy, dvecx, dvecy, x, y;
80 
81  for (i = num - 1; i >= 0; i--) {
82  dvecx = dvecy = 0.0;
83  x = jit1[i][0];
84  y = jit1[i][1];
85  for (j = num - 1; j >= 0; j--) {
86  if (i != j) {
87  vecx = jit1[j][0] - x - 1.0f;
88  vecy = jit1[j][1] - y - 1.0f;
89 
90  if (fabsf(vecx) < radius2) {
91  dvecx += vecx * radius2;
92  }
93  vecx += 1.0f;
94  if (fabsf(vecx) < radius2) {
95  dvecx += vecx * radius2;
96  }
97  vecx += 1.0f;
98  if (fabsf(vecx) < radius2) {
99  dvecx += vecx * radius2;
100  }
101 
102  if (fabsf(vecy) < radius2) {
103  dvecy += vecy * radius2;
104  }
105  vecy += 1.0f;
106  if (fabsf(vecy) < radius2) {
107  dvecy += vecy * radius2;
108  }
109  vecy += 1.0f;
110  if (fabsf(vecy) < radius2) {
111  dvecy += vecy * radius2;
112  }
113  }
114  }
115 
116  x -= dvecx / 2.0f;
117  y -= dvecy / 2.0f;
118  x -= floorf(x);
119  y -= floorf(y);
120  jit2[i][0] = x;
121  jit2[i][1] = y;
122  }
123  memcpy(jit1, jit2, (unsigned int)num * sizeof(float[2]));
124 }
125 
126 void BLI_jitter_init(float (*jitarr)[2], int num)
127 {
128  float(*jit2)[2];
129  float number_fl, number_fl_sqrt;
130  float x, rad1, rad2, rad3;
131  RNG *rng;
132  int i;
133 
134  if (num == 0) {
135  return;
136  }
137 
138  number_fl = (float)num;
139  number_fl_sqrt = sqrtf(number_fl);
140 
141  jit2 = MEM_mallocN(12 + (unsigned int)num * sizeof(float[2]), "initjit");
142  rad1 = 1.0f / number_fl_sqrt;
143  rad2 = 1.0f / number_fl;
144  rad3 = number_fl_sqrt / number_fl;
145 
146  rng = BLI_rng_new(31415926 + (unsigned int)num);
147 
148  x = 0;
149  for (i = 0; i < num; i++) {
150  jitarr[i][0] = x + rad1 * (float)(0.5 - BLI_rng_get_double(rng));
151  jitarr[i][1] = (float)i / number_fl + rad1 * (float)(0.5 - BLI_rng_get_double(rng));
152  x += rad3;
153  x -= floorf(x);
154  }
155 
156  BLI_rng_free(rng);
157 
158  for (i = 0; i < 24; i++) {
159  BLI_jitterate1(jitarr, jit2, num, rad1);
160  BLI_jitterate1(jitarr, jit2, num, rad1);
161  BLI_jitterate2(jitarr, jit2, num, rad2);
162  }
163 
164  MEM_freeN(jit2);
165 
166  /* Finally, move jitter to be centered around (0, 0). */
167  for (i = 0; i < num; i++) {
168  jitarr[i][0] -= 0.5f;
169  jitarr[i][1] -= 0.5f;
170  }
171 }
typedef float(TangentPoint)[2]
Random number functions.
void BLI_rng_free(struct RNG *rng) ATTR_NONNULL(1)
Definition: rand.cc:58
struct RNG * BLI_rng_new(unsigned int seed)
Definition: rand.cc:39
double BLI_rng_get_double(struct RNG *rng) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition: rand.cc:88
Strict compiler flags for areas of code we want to ensure don't do conversions without us knowing abo...
_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 y
Read Guarded memory(de)allocation.
int len
Definition: draw_manager.c:108
void BLI_jitterate1(float(*jit1)[2], float(*jit2)[2], int num, float radius1)
Definition: jitter_2d.c:18
void BLI_jitter_init(float(*jitarr)[2], int num)
Definition: jitter_2d.c:126
void BLI_jitterate2(float(*jit1)[2], float(*jit2)[2], int num, float radius2)
Definition: jitter_2d.c:76
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:27
void *(* MEM_mallocN)(size_t len, const char *str)
Definition: mallocn.c:33
#define floorf(x)
Definition: metal/compat.h:224
#define fabsf(x)
Definition: metal/compat.h:219
#define sqrtf(x)
Definition: metal/compat.h:243
Definition: rand.cc:33