Blender V4.3
rna_pointcloud.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
9#include <cstdlib>
10
11#include "RNA_define.hh"
12#include "RNA_enum_types.hh"
13
14#include "rna_internal.hh"
15
17
18#include "BKE_attribute.h"
19
20#include "BLI_math_base.h"
21#include "BLI_string.h"
22
23#ifdef RNA_RUNTIME
24
25# include <fmt/format.h>
26
27# include "BLI_math_vector.h"
28
29# include "BKE_customdata.hh"
30# include "BKE_pointcloud.hh"
31
32# include "DEG_depsgraph.hh"
33
34# include "WM_api.hh"
35# include "WM_types.hh"
36
37static PointCloud *rna_pointcloud(const PointerRNA *ptr)
38{
39 return (PointCloud *)ptr->owner_id;
40}
41
42static float (*get_pointcloud_positions(PointCloud *pointcloud))[3]
43{
44 return (float(*)[3])CustomData_get_layer_named_for_write(
45 &pointcloud->pdata, CD_PROP_FLOAT3, "position", pointcloud->totpoint);
46}
47
48static const float (*get_pointcloud_positions_const(const PointCloud *pointcloud))[3]
49{
50 return (const float(*)[3])CustomData_get_layer_named(
51 &pointcloud->pdata, CD_PROP_FLOAT3, "position");
52}
53
54static int rna_Point_index_get_const(const PointerRNA *ptr)
55{
56 const PointCloud *pointcloud = rna_pointcloud(ptr);
57 const float(*co)[3] = static_cast<const float(*)[3]>(ptr->data);
58 const float(*positions)[3] = get_pointcloud_positions_const(pointcloud);
59 return int(co - positions);
60}
61
62static int rna_Point_index_get(PointerRNA *ptr)
63{
64 return rna_Point_index_get_const(ptr);
65}
66
67static int rna_PointCloud_points_length(PointerRNA *ptr)
68{
69 const PointCloud *pointcloud = rna_pointcloud(ptr);
70 return pointcloud->totpoint;
71}
72
73static void rna_PointCloud_points_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
74{
75 PointCloud *pointcloud = rna_pointcloud(ptr);
77 get_pointcloud_positions(pointcloud),
78 sizeof(float[3]),
79 pointcloud->totpoint,
80 false,
81 nullptr);
82}
83
84bool rna_PointCloud_points_lookup_int(PointerRNA *ptr, int index, PointerRNA *r_ptr)
85{
86 PointCloud *pointcloud = rna_pointcloud(ptr);
87 if (index < 0 || index >= pointcloud->totpoint) {
88 return false;
89 }
90 r_ptr->owner_id = &pointcloud->id;
91 r_ptr->type = &RNA_Point;
92 r_ptr->data = &get_pointcloud_positions(pointcloud)[index];
93 return true;
94}
95
96static void rna_Point_location_get(PointerRNA *ptr, float value[3])
97{
98 copy_v3_v3(value, (const float *)ptr->data);
99}
100
101static void rna_Point_location_set(PointerRNA *ptr, const float value[3])
102{
103 copy_v3_v3((float *)ptr->data, value);
104}
105
106static float rna_Point_radius_get(PointerRNA *ptr)
107{
108 const PointCloud *pointcloud = rna_pointcloud(ptr);
109 const float *radii = (const float *)CustomData_get_layer_named(
110 &pointcloud->pdata, CD_PROP_FLOAT, "radius");
111 if (radii == nullptr) {
112 return 0.0f;
113 }
114 return radii[rna_Point_index_get_const(ptr)];
115}
116
117static void rna_Point_radius_set(PointerRNA *ptr, float value)
118{
119 PointCloud *pointcloud = rna_pointcloud(ptr);
120 float *radii = (float *)CustomData_get_layer_named_for_write(
121 &pointcloud->pdata, CD_PROP_FLOAT, "radius", pointcloud->totpoint);
122 if (radii == nullptr) {
123 return;
124 }
125 radii[rna_Point_index_get_const(ptr)] = value;
126}
127
128static std::optional<std::string> rna_Point_path(const PointerRNA *ptr)
129{
130 return fmt::format("points[{}]", rna_Point_index_get_const(ptr));
131}
132
133static void rna_PointCloud_update_data(Main * /*bmain*/, Scene * /*scene*/, PointerRNA *ptr)
134{
135 ID *id = ptr->owner_id;
136
137 /* cheating way for importers to avoid slow updates */
138 if (id->us > 0) {
139 DEG_id_tag_update(id, 0);
141 }
142}
143
144#else
145
146static void rna_def_point(BlenderRNA *brna)
147{
148 StructRNA *srna;
149 PropertyRNA *prop;
150
151 srna = RNA_def_struct(brna, "Point", nullptr);
152 RNA_def_struct_ui_text(srna, "Point", "Point in a point cloud");
153 RNA_def_struct_path_func(srna, "rna_Point_path");
154
155 prop = RNA_def_property(srna, "co", PROP_FLOAT, PROP_TRANSLATION);
156 RNA_def_property_array(prop, 3);
157 RNA_def_property_float_funcs(prop, "rna_Point_location_get", "rna_Point_location_set", nullptr);
158 RNA_def_property_ui_text(prop, "Location", "");
159 RNA_def_property_update(prop, 0, "rna_PointCloud_update_data");
160
161 prop = RNA_def_property(srna, "radius", PROP_FLOAT, PROP_DISTANCE);
162 RNA_def_property_float_funcs(prop, "rna_Point_radius_get", "rna_Point_radius_set", nullptr);
163 RNA_def_property_ui_text(prop, "Radius", "");
164 RNA_def_property_update(prop, 0, "rna_PointCloud_update_data");
165
166 prop = RNA_def_property(srna, "index", PROP_INT, PROP_UNSIGNED);
168 RNA_def_property_int_funcs(prop, "rna_Point_index_get", nullptr, nullptr);
169 RNA_def_property_ui_text(prop, "Index", "Index of this point");
170}
171
173{
174 StructRNA *srna;
175 PropertyRNA *prop;
176
177 srna = RNA_def_struct(brna, "PointCloud", "ID");
178 RNA_def_struct_ui_text(srna, "Point Cloud", "Point cloud data-block");
179 RNA_def_struct_ui_icon(srna, ICON_POINTCLOUD_DATA);
180
181 /* geometry */
182 prop = RNA_def_property(srna, "points", PROP_COLLECTION, PROP_NONE);
183 RNA_def_property_struct_type(prop, "Point");
186 "rna_PointCloud_points_begin",
187 "rna_iterator_array_next",
188 "rna_iterator_array_end",
189 "rna_iterator_array_get",
190 "rna_PointCloud_points_length",
191 "rna_PointCloud_points_lookup_int",
192 nullptr,
193 nullptr);
194 RNA_def_property_ui_text(prop, "Points", "");
195
196 /* materials */
197 prop = RNA_def_property(srna, "materials", PROP_COLLECTION, PROP_NONE);
198 RNA_def_property_collection_sdna(prop, nullptr, "mat", "totcol");
199 RNA_def_property_struct_type(prop, "Material");
200 RNA_def_property_ui_text(prop, "Materials", "");
201 RNA_def_property_srna(prop, "IDMaterials"); /* see rna_ID.cc */
203 nullptr,
204 nullptr,
205 nullptr,
206 nullptr,
207 nullptr,
208 nullptr,
209 nullptr,
210 "rna_IDMaterials_assign_int");
211
213
214 /* common */
216}
217
219{
220 rna_def_point(brna);
221 rna_def_pointcloud(brna);
222}
223
224#endif
Generic geometry attributes built on CustomData.
CustomData interface, see also DNA_customdata_types.h.
void * CustomData_get_layer_named_for_write(CustomData *data, eCustomDataType type, blender::StringRef name, int totelem)
const void * CustomData_get_layer_named(const CustomData *data, eCustomDataType type, blender::StringRef name)
General operations for point clouds.
MINLINE void copy_v3_v3(float r[3], const float a[3])
void DEG_id_tag_update(ID *id, unsigned int flags)
@ CD_PROP_FLOAT
@ CD_PROP_FLOAT3
@ PROP_FLOAT
Definition RNA_types.hh:67
@ PROP_INT
Definition RNA_types.hh:66
@ PROP_COLLECTION
Definition RNA_types.hh:71
@ PROPOVERRIDE_IGNORE
Definition RNA_types.hh:375
@ PROP_EDITABLE
Definition RNA_types.hh:207
@ PROP_DISTANCE
Definition RNA_types.hh:159
@ PROP_NONE
Definition RNA_types.hh:136
@ PROP_TRANSLATION
Definition RNA_types.hh:164
@ PROP_UNSIGNED
Definition RNA_types.hh:152
#define NC_GEOM
Definition WM_types.hh:360
#define ND_DATA
Definition WM_types.hh:475
draw_view in_light_buf[] float
draw_view push_constant(Type::INT, "radiance_src") .push_constant(Type capture_info_buf storage_buf(1, Qualifier::READ, "ObjectBounds", "bounds_buf[]") .push_constant(Type draw_view int
void rna_iterator_array_begin(CollectionPropertyIterator *iter, void *ptr, int itemsize, int length, bool free_ptr, IteratorSkipFunc skip)
void rna_def_animdata_common(StructRNA *srna)
void rna_def_attributes_common(StructRNA *srna, const AttributeOwnerType type)
void RNA_def_struct_path_func(StructRNA *srna, const char *path)
void RNA_def_property_float_funcs(PropertyRNA *prop, const char *get, const char *set, const char *range)
void RNA_def_property_ui_text(PropertyRNA *prop, const char *name, const char *description)
void RNA_def_property_srna(PropertyRNA *prop, const char *type)
void RNA_def_property_collection_funcs(PropertyRNA *prop, const char *begin, const char *next, const char *end, const char *get, const char *length, const char *lookupint, const char *lookupstring, const char *assignint)
void RNA_def_struct_ui_text(StructRNA *srna, const char *name, const char *description)
void RNA_def_property_array(PropertyRNA *prop, int length)
void RNA_def_property_struct_type(PropertyRNA *prop, const char *type)
void RNA_def_property_collection_sdna(PropertyRNA *prop, const char *structname, const char *propname, const char *lengthpropname)
void RNA_def_property_update(PropertyRNA *prop, int noteflag, const char *func)
PropertyRNA * RNA_def_property(StructOrFunctionRNA *cont_, const char *identifier, int type, int subtype)
StructRNA * RNA_def_struct(BlenderRNA *brna, const char *identifier, const char *from)
void RNA_def_property_clear_flag(PropertyRNA *prop, PropertyFlag flag)
void RNA_def_property_int_funcs(PropertyRNA *prop, const char *get, const char *set, const char *range)
void RNA_def_struct_ui_icon(StructRNA *srna, int icon)
void RNA_def_property_override_flag(PropertyRNA *prop, PropertyOverrideFlag flag)
static void rna_def_point(BlenderRNA *brna)
void RNA_def_pointcloud(BlenderRNA *brna)
static void rna_def_pointcloud(BlenderRNA *brna)
Definition DNA_ID.h:413
struct CustomData pdata
ID * owner_id
Definition RNA_types.hh:40
StructRNA * type
Definition RNA_types.hh:41
void * data
Definition RNA_types.hh:42
void WM_main_add_notifier(uint type, void *reference)
PointerRNA * ptr
Definition wm_files.cc:4126