Blender  V3.3
pbvh.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
7 #include "MEM_guardedalloc.h"
8 
9 #include "BLI_utildefines.h"
10 
11 #include "BLI_bitmap.h"
12 #include "BLI_ghash.h"
13 #include "BLI_index_range.hh"
14 #include "BLI_math.h"
15 #include "BLI_rand.h"
16 #include "BLI_span.hh"
17 #include "BLI_task.h"
18 
19 #include "DNA_mesh_types.h"
20 #include "DNA_meshdata_types.h"
21 
22 #include "BKE_attribute.h"
23 #include "BKE_ccg.h"
24 #include "BKE_mesh.h"
25 #include "BKE_mesh_mapping.h"
26 #include "BKE_paint.h"
27 #include "BKE_pbvh.h"
28 #include "BKE_subdiv_ccg.h"
29 
30 #include "PIL_time.h"
31 
32 #include "GPU_buffers.h"
33 
34 #include "bmesh.h"
35 
36 #include "atomic_ops.h"
37 
38 #include "pbvh_intern.h"
39 
40 #include <climits>
41 
43 
44 namespace blender::bke {
45 
46 template<typename Func>
47 inline void to_static_color_type(const eCustomDataType type, const Func &func)
48 {
49  switch (type) {
50  case CD_PROP_COLOR:
51  func(MPropCol());
52  break;
53  case CD_PROP_BYTE_COLOR:
54  func(MLoopCol());
55  break;
56  default:
58  break;
59  }
60 }
61 
62 template<typename T> void to_float(const T &src, float dst[4]);
63 
64 template<> void to_float(const MLoopCol &src, float dst[4])
65 {
66  rgba_uchar_to_float(dst, reinterpret_cast<const unsigned char *>(&src));
67  srgb_to_linearrgb_v3_v3(dst, dst);
68 }
69 template<> void to_float(const MPropCol &src, float dst[4])
70 {
71  copy_v4_v4(dst, src.color);
72 }
73 
74 template<typename T> void from_float(const float src[4], T &dst);
75 
76 template<> void from_float(const float src[4], MLoopCol &dst)
77 {
78  float temp[4];
80  temp[3] = src[3];
81  rgba_float_to_uchar(reinterpret_cast<unsigned char *>(&dst), temp);
82 }
83 template<> void from_float(const float src[4], MPropCol &dst)
84 {
85  copy_v4_v4(dst.color, src);
86 }
87 
88 template<typename T>
89 static void pbvh_vertex_color_get(const PBVH &pbvh, int vertex, float r_color[4])
90 {
91  if (pbvh.color_domain == ATTR_DOMAIN_CORNER) {
92  const MeshElemMap &melem = pbvh.pmap[vertex];
93 
94  int count = 0;
95  zero_v4(r_color);
96  for (const int i_poly : Span(melem.indices, melem.count)) {
97  const MPoly &mp = pbvh.mpoly[i_poly];
98  Span<T> colors{static_cast<const T *>(pbvh.color_layer->data) + mp.loopstart, mp.totloop};
99  Span<MLoop> loops{pbvh.mloop + mp.loopstart, mp.totloop};
100 
101  for (const int i_loop : IndexRange(mp.totloop)) {
102  if (loops[i_loop].v == vertex) {
103  float temp[4];
104  to_float(colors[i_loop], temp);
105 
106  add_v4_v4(r_color, temp);
107  count++;
108  }
109  }
110  }
111 
112  if (count) {
113  mul_v4_fl(r_color, 1.0f / (float)count);
114  }
115  }
116  else {
117  to_float(static_cast<T *>(pbvh.color_layer->data)[vertex], r_color);
118  }
119 }
120 
121 template<typename T>
122 static void pbvh_vertex_color_set(PBVH &pbvh, int vertex, const float color[4])
123 {
124  if (pbvh.color_domain == ATTR_DOMAIN_CORNER) {
125  const MeshElemMap &melem = pbvh.pmap[vertex];
126 
127  for (const int i_poly : Span(melem.indices, melem.count)) {
128  const MPoly &mp = pbvh.mpoly[i_poly];
129  MutableSpan<T> colors{static_cast<T *>(pbvh.color_layer->data) + mp.loopstart, mp.totloop};
130  Span<MLoop> loops{pbvh.mloop + mp.loopstart, mp.totloop};
131 
132  for (const int i_loop : IndexRange(mp.totloop)) {
133  if (loops[i_loop].v == vertex) {
134  from_float(color, colors[i_loop]);
135  }
136  }
137  }
138  }
139  else {
140  from_float(color, static_cast<T *>(pbvh.color_layer->data)[vertex]);
141  }
142 }
143 
144 } // namespace blender::bke
145 
146 extern "C" {
147 void BKE_pbvh_vertex_color_get(const PBVH *pbvh, int vertex, float r_color[4])
148 {
150  using T = decltype(dummy);
151  blender::bke::pbvh_vertex_color_get<T>(*pbvh, vertex, r_color);
152  });
153 }
154 
155 void BKE_pbvh_vertex_color_set(PBVH *pbvh, int vertex, const float color[4])
156 {
158  using T = decltype(dummy);
159  blender::bke::pbvh_vertex_color_set<T>(*pbvh, vertex, color);
160  });
161 }
162 
164  const int *indices,
165  const int indices_num,
166  float (*r_colors)[4])
167 {
169  using T = decltype(dummy);
170  T *pbvh_colors = static_cast<T *>(pbvh->color_layer->data);
171  for (const int i : IndexRange(indices_num)) {
172  T temp = pbvh_colors[indices[i]];
173  blender::bke::from_float(r_colors[i], pbvh_colors[indices[i]]);
174  blender::bke::to_float(temp, r_colors[i]);
175  }
176  });
177 }
178 
180  const int *indices,
181  const int indices_num,
182  float (*r_colors)[4])
183 {
185  using T = decltype(dummy);
186  T *pbvh_colors = static_cast<T *>(pbvh->color_layer->data);
187  for (const int i : IndexRange(indices_num)) {
188  blender::bke::to_float(pbvh_colors[indices[i]], r_colors[i]);
189  }
190  });
191 }
192 
194  const int *indices,
195  const int indices_num,
196  float (*r_colors)[4])
197 {
198  if (pbvh->color_domain == ATTR_DOMAIN_POINT) {
199  BKE_pbvh_store_colors(pbvh, indices, indices_num, r_colors);
200  }
201  else {
203  using T = decltype(dummy);
204  for (const int i : IndexRange(indices_num)) {
205  blender::bke::pbvh_vertex_color_get<T>(*pbvh, indices[i], r_colors[i]);
206  }
207  });
208  }
209 }
210 }
Generic geometry attributes built on CustomData.
@ ATTR_DOMAIN_POINT
Definition: BKE_attribute.h:27
@ ATTR_DOMAIN_CORNER
Definition: BKE_attribute.h:30
A BVH for high poly meshes.
#define BLI_assert_unreachable()
Definition: BLI_assert.h:93
MINLINE void srgb_to_linearrgb_v3_v3(float linear[3], const float srgb[3])
void rgba_uchar_to_float(float r_col[4], const unsigned char col_ub[4])
Definition: math_color.c:383
void rgba_float_to_uchar(unsigned char r_col[4], const float col_f[4])
Definition: math_color.c:396
MINLINE void linearrgb_to_srgb_v3_v3(float srgb[3], const float linear[3])
MINLINE void mul_v4_fl(float r[4], float f)
MINLINE void add_v4_v4(float r[4], const float a[4])
MINLINE void copy_v4_v4(float r[4], const float a[4])
MINLINE void zero_v4(float r[4])
Random number functions.
eCustomDataType
@ CD_PROP_BYTE_COLOR
@ CD_PROP_COLOR
struct MLoopCol MLoopCol
struct MPropCol MPropCol
_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 type
Read Guarded memory(de)allocation.
Group Output data from inside of a node group A color picker Mix two input colors RGB to Convert a color s luminance to a grayscale value Generate a normal vector and a dot product Bright Control the brightness and contrast of the input color Vector Map an input vectors to used to fine tune the interpolation of the input Camera Retrieve information about the camera and how it relates to the current shading point s position Clamp a value between a minimum and a maximum Vector Perform vector math operation Invert a color
Platform independent time functions.
Provides wrapper around system-specific atomic primitives, and some extensions (faked-atomic operatio...
ATTR_WARN_UNUSED_RESULT const BMVert * v
SyclQueue void void * src
int count
ccl_gpu_kernel_postfix int ccl_global int * indices
#define T
void to_float(const T &src, float dst[4])
void from_float(const float src[4], T &dst)
static void pbvh_vertex_color_get(const PBVH &pbvh, int vertex, float r_color[4])
Definition: pbvh.cc:89
void to_static_color_type(const eCustomDataType type, const Func &func)
Definition: pbvh.cc:47
static void pbvh_vertex_color_set(PBVH &pbvh, int vertex, const float color[4])
Definition: pbvh.cc:122
void BKE_pbvh_store_colors_vertex(PBVH *pbvh, const int *indices, const int indices_num, float(*r_colors)[4])
Definition: pbvh.cc:193
void BKE_pbvh_store_colors(PBVH *pbvh, const int *indices, const int indices_num, float(*r_colors)[4])
Definition: pbvh.cc:179
void BKE_pbvh_swap_colors(PBVH *pbvh, const int *indices, const int indices_num, float(*r_colors)[4])
Definition: pbvh.cc:163
void BKE_pbvh_vertex_color_set(PBVH *pbvh, int vertex, const float color[4])
Definition: pbvh.cc:155
void BKE_pbvh_vertex_color_get(const PBVH *pbvh, int vertex, float r_color[4])
Definition: pbvh.cc:147
float color[4]
const struct MPoly * mpoly
Definition: pbvh_intern.h:152
const struct MeshElemMap * pmap
Definition: pbvh_intern.h:198
const struct MLoop * mloop
Definition: pbvh_intern.h:153
eAttrDomain color_domain
Definition: pbvh_intern.h:201
CustomDataLayer * color_layer
Definition: pbvh_intern.h:200