Blender  V2.93
node_geo_mesh_primitive_circle.cc
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 
17 #include "DNA_mesh_types.h"
18 #include "DNA_meshdata_types.h"
19 
20 #include "BKE_mesh.h"
21 
22 #include "UI_interface.h"
23 #include "UI_resources.h"
24 
25 #include "node_geometry_util.hh"
26 
28  {SOCK_INT, N_("Vertices"), 32, 0.0f, 0.0f, 0.0f, 3, 4096},
29  {SOCK_FLOAT, N_("Radius"), 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, FLT_MAX, PROP_DISTANCE},
30  {-1, ""},
31 };
32 
34  {SOCK_GEOMETRY, N_("Geometry")},
35  {-1, ""},
36 };
37 
39  bContext *UNUSED(C),
40  PointerRNA *ptr)
41 {
42  uiLayoutSetPropSep(layout, true);
43  uiLayoutSetPropDecorate(layout, false);
44  uiItemR(layout, ptr, "fill_type", 0, nullptr, ICON_NONE);
45 }
46 
48 {
50  sizeof(NodeGeometryMeshCircle), __func__);
51 
53 
54  node->storage = node_storage;
55 }
56 
57 namespace blender::nodes {
58 
59 static int circle_vert_total(const GeometryNodeMeshCircleFillType fill_type, const int verts_num)
60 {
61  switch (fill_type) {
64  return verts_num;
66  return verts_num + 1;
67  }
69  return 0;
70 }
71 
72 static int circle_edge_total(const GeometryNodeMeshCircleFillType fill_type, const int verts_num)
73 {
74  switch (fill_type) {
77  return verts_num;
79  return verts_num * 2;
80  }
82  return 0;
83 }
84 
85 static int circle_corner_total(const GeometryNodeMeshCircleFillType fill_type, const int verts_num)
86 {
87  switch (fill_type) {
89  return 0;
91  return verts_num;
93  return verts_num * 3;
94  }
96  return 0;
97 }
98 
99 static int circle_face_total(const GeometryNodeMeshCircleFillType fill_type, const int verts_num)
100 {
101  switch (fill_type) {
103  return 0;
105  return 1;
107  return verts_num;
108  }
110  return 0;
111 }
112 
113 static Mesh *create_circle_mesh(const float radius,
114  const int verts_num,
115  const GeometryNodeMeshCircleFillType fill_type)
116 {
117  Mesh *mesh = BKE_mesh_new_nomain(circle_vert_total(fill_type, verts_num),
118  circle_edge_total(fill_type, verts_num),
119  0,
120  circle_corner_total(fill_type, verts_num),
121  circle_face_total(fill_type, verts_num));
126 
127  float angle = 0.0f;
128  const float angle_delta = 2.0f * M_PI / static_cast<float>(verts_num);
129  for (MVert &vert : verts) {
130  copy_v3_v3(vert.co, float3(std::cos(angle) * radius, std::sin(angle) * radius, 0.0f));
131  angle += angle_delta;
132  }
133  if (fill_type == GEO_NODE_MESH_CIRCLE_FILL_TRIANGLE_FAN) {
134  copy_v3_v3(verts.last().co, float3(0));
135  }
136 
137  /* Point all vertex normals in the up direction. */
138  const short up_normal[3] = {0, 0, SHRT_MAX};
139  for (MVert &vert : verts) {
140  copy_v3_v3_short(vert.no, up_normal);
141  }
142 
143  /* Create outer edges. */
144  for (const int i : IndexRange(verts_num)) {
145  MEdge &edge = edges[i];
146  edge.v1 = i;
147  edge.v2 = (i + 1) % verts_num;
148  }
149 
150  /* Set loose edge flags. */
151  if (fill_type == GEO_NODE_MESH_CIRCLE_FILL_NONE) {
152  for (const int i : IndexRange(verts_num)) {
153  MEdge &edge = edges[i];
154  edge.flag |= ME_LOOSEEDGE;
155  }
156  }
157 
158  /* Create triangle fan edges. */
159  if (fill_type == GEO_NODE_MESH_CIRCLE_FILL_TRIANGLE_FAN) {
160  for (const int i : IndexRange(verts_num)) {
161  MEdge &edge = edges[verts_num + i];
162  edge.v1 = verts_num;
163  edge.v2 = i;
164  edge.flag = ME_EDGEDRAW | ME_EDGERENDER;
165  }
166  }
167 
168  /* Create corners and faces. */
169  if (fill_type == GEO_NODE_MESH_CIRCLE_FILL_NGON) {
170  MPoly &poly = polys[0];
171  poly.loopstart = 0;
172  poly.totloop = loops.size();
173 
174  for (const int i : IndexRange(verts_num)) {
175  MLoop &loop = loops[i];
176  loop.e = i;
177  loop.v = i;
178  }
179  }
180  else if (fill_type == GEO_NODE_MESH_CIRCLE_FILL_TRIANGLE_FAN) {
181  for (const int i : IndexRange(verts_num)) {
182  MPoly &poly = polys[i];
183  poly.loopstart = 3 * i;
184  poly.totloop = 3;
185 
186  MLoop &loop_a = loops[3 * i];
187  loop_a.e = i;
188  loop_a.v = i;
189  MLoop &loop_b = loops[3 * i + 1];
190  loop_b.e = verts_num + ((i + 1) % verts_num);
191  loop_b.v = (i + 1) % verts_num;
192  MLoop &loop_c = loops[3 * i + 2];
193  loop_c.e = verts_num + i;
194  loop_c.v = verts_num;
195  }
196  }
197 
198  return mesh;
199 }
200 
202 {
203  const bNode &node = params.node();
204  const NodeGeometryMeshCircle &storage = *(const NodeGeometryMeshCircle *)node.storage;
205 
207  storage.fill_type;
208 
209  const float radius = params.extract_input<float>("Radius");
210  const int verts_num = params.extract_input<int>("Vertices");
211  if (verts_num < 3) {
212  params.set_output("Geometry", GeometrySet());
213  return;
214  }
215 
216  Mesh *mesh = create_circle_mesh(radius, verts_num, fill_type);
217 
219 
220  params.set_output("Geometry", GeometrySet::create_with_mesh(mesh));
221 }
222 
223 } // namespace blender::nodes
224 
226 {
227  static bNodeType ntype;
228 
234  &ntype, "NodeGeometryMeshCircle", node_free_standard_storage, node_copy_standard_storage);
237  nodeRegisterType(&ntype);
238 }
bool BKE_mesh_is_valid(struct Mesh *me)
struct Mesh * BKE_mesh_new_nomain(int verts_len, int edges_len, int tessface_len, int loops_len, int polys_len)
Definition: mesh.c:877
void node_type_socket_templates(struct bNodeType *ntype, struct bNodeSocketTemplate *inputs, struct bNodeSocketTemplate *outputs)
Definition: node.cc:4527
void node_type_init(struct bNodeType *ntype, void(*initfunc)(struct bNodeTree *ntree, struct bNode *node))
Definition: node.cc:4559
#define NODE_CLASS_GEOMETRY
Definition: BKE_node.h:359
void node_type_storage(struct bNodeType *ntype, const char *storagename, void(*freefunc)(struct bNode *node), void(*copyfunc)(struct bNodeTree *dest_ntree, struct bNode *dest_node, const struct bNode *src_node))
Definition: node.cc:4599
void nodeRegisterType(struct bNodeType *ntype)
Definition: node.cc:1298
#define BLI_assert_unreachable()
Definition: BLI_assert.h:96
#define BLI_assert(a)
Definition: BLI_assert.h:58
#define M_PI
Definition: BLI_math_base.h:38
MINLINE void copy_v3_v3(float r[3], const float a[3])
MINLINE void copy_v3_v3_short(short r[3], const short a[3])
#define UNUSED(x)
#define N_(msgid)
@ ME_EDGEDRAW
@ ME_EDGERENDER
@ ME_LOOSEEDGE
GeometryNodeMeshCircleFillType
@ GEO_NODE_MESH_CIRCLE_FILL_NGON
@ GEO_NODE_MESH_CIRCLE_FILL_TRIANGLE_FAN
@ GEO_NODE_MESH_CIRCLE_FILL_NONE
@ SOCK_INT
@ SOCK_FLOAT
@ SOCK_GEOMETRY
Group RGB to Bright Vector Camera Vector Combine Material Light Line Style Layer Add Ambient Diffuse Glossy Refraction Transparent Toon Principled Hair Volume Principled Light Particle Volume Image Sky Noise Wave Voronoi Brick Texture Vector Combine Vertex Separate Vector White RGB Map Separate Set Z Dilate Combine Combine Color Channel Split ID Combine Luminance Directional Alpha Distance Hue Movie Ellipse Bokeh View Corner Anti Mix RGB Hue Separate TEX_NODE_PROC TEX_NODE_PROC TEX_NODE_PROC TEX_NODE_PROC TEX_NODE_PROC Boolean Random Edge Subdivision Point Object Attribute Attribute Attribute Color Attribute Attribute Vector Point Attribute Sample Collection Attribute Attribute Combine Attribute GEO_NODE_MESH_PRIMITIVE_CIRCLE
@ PROP_DISTANCE
Definition: RNA_types.h:135
#define C
Definition: RandGen.cpp:39
void uiLayoutSetPropSep(uiLayout *layout, bool is_sep)
void uiItemR(uiLayout *layout, struct PointerRNA *ptr, const char *propname, int flag, const char *name, int icon)
void uiLayoutSetPropDecorate(uiLayout *layout, bool is_sep)
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition: btVector3.h:356
OperationNode * node
bNodeTree * ntree
static float verts[][3]
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:45
INLINE Rall1d< T, V, S > cos(const Rall1d< T, V, S > &arg)
Definition: rall1d.h:319
INLINE Rall1d< T, V, S > sin(const Rall1d< T, V, S > &arg)
Definition: rall1d.h:311
static int circle_vert_total(const GeometryNodeMeshCircleFillType fill_type, const int verts_num)
static void geo_node_mesh_primitive_circle_exec(GeoNodeExecParams params)
static int circle_face_total(const GeometryNodeMeshCircleFillType fill_type, const int verts_num)
static int circle_corner_total(const GeometryNodeMeshCircleFillType fill_type, const int verts_num)
static int circle_edge_total(const GeometryNodeMeshCircleFillType fill_type, const int verts_num)
static Mesh * create_circle_mesh(const float radius, const int verts_num, const GeometryNodeMeshCircleFillType fill_type)
static bNodeSocketTemplate geo_node_mesh_primitive_circle_in[]
static void geo_node_mesh_primitive_circle_layout(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
static void geo_node_mesh_primitive_circle_init(bNodeTree *UNUSED(ntree), bNode *node)
void register_node_type_geo_mesh_primitive_circle()
static bNodeSocketTemplate geo_node_mesh_primitive_circle_out[]
void geo_node_type_base(bNodeType *ntype, int type, const char *name, short nclass, short flag)
void node_copy_standard_storage(bNodeTree *UNUSED(dest_ntree), bNode *dest_node, const bNode *src_node)
Definition: node_util.c:67
void node_free_standard_storage(bNode *node)
Definition: node_util.c:55
static GeometrySet create_with_mesh(Mesh *mesh, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
unsigned int v1
unsigned int v2
unsigned int e
unsigned int v
struct MEdge * medge
struct MVert * mvert
int totedge
int totvert
struct MLoop * mloop
int totpoly
int totloop
struct MPoly * mpoly
Compact definition of a node socket.
Definition: BKE_node.h:95
Defines a node type.
Definition: BKE_node.h:221
NodeGeometryExecFunction geometry_node_execute
Definition: BKE_node.h:327
void(* draw_buttons)(struct uiLayout *, struct bContext *C, struct PointerRNA *ptr)
Definition: BKE_node.h:253
PointerRNA * ptr
Definition: wm_files.c:3157