Blender  V2.93
MOD_triangulate.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 
21 #include <string.h>
22 
23 #include "MEM_guardedalloc.h"
24 
25 #include "BLI_utildefines.h"
26 
27 #include "BLT_translation.h"
28 
29 #include "DNA_defaults.h"
30 #include "DNA_mesh_types.h"
31 #include "DNA_meshdata_types.h"
32 #include "DNA_object_types.h"
33 #include "DNA_screen_types.h"
34 
35 #include "BKE_context.h"
36 #include "BKE_mesh.h"
37 #include "BKE_modifier.h"
38 #include "BKE_screen.h"
39 
40 #include "UI_interface.h"
41 #include "UI_resources.h"
42 
43 #include "RNA_access.h"
44 
45 #include "bmesh.h"
46 #include "bmesh_tools.h"
47 
48 #include "MOD_modifiertypes.h"
49 #include "MOD_ui_common.h"
50 
52  const int quad_method,
53  const int ngon_method,
54  const int min_vertices,
55  const int flag);
56 
58  const int quad_method,
59  const int ngon_method,
60  const int min_vertices,
61  const int flag)
62 {
63  Mesh *result;
64  BMesh *bm;
65  int total_edges, i;
66  MEdge *me;
67  CustomData_MeshMasks cd_mask_extra = {
69 
70  bool keep_clnors = (flag & MOD_TRIANGULATE_KEEP_CUSTOMLOOP_NORMALS) != 0;
71 
72  if (keep_clnors) {
74  /* We need that one to 'survive' to/from BMesh conversions. */
76  cd_mask_extra.lmask |= CD_MASK_NORMAL;
77  }
78 
80  &((struct BMeshCreateParams){0}),
81  &((struct BMeshFromMeshParams){
82  .calc_face_normal = true,
83  .cd_mask_extra = cd_mask_extra,
84  }));
85 
86  BM_mesh_triangulate(bm, quad_method, ngon_method, min_vertices, false, NULL, NULL, NULL);
87 
90 
91  if (keep_clnors) {
92  float(*lnors)[3] = CustomData_get_layer(&result->ldata, CD_NORMAL);
93  BLI_assert(lnors != NULL);
94 
96 
97  /* Do some cleanup, we do not want those temp data to stay around. */
100  }
101 
102  total_edges = result->totedge;
103  me = result->medge;
104 
105  /* force drawing of all edges (seems to be omitted in CDDM_from_bmesh) */
106  for (i = 0; i < total_edges; i++, me++) {
107  me->flag |= ME_EDGEDRAW | ME_EDGERENDER;
108  }
109 
110  result->runtime.cd_dirty_vert |= CD_MASK_NORMAL;
111 
112  return result;
113 }
114 
115 static void initData(ModifierData *md)
116 {
118 
119  BLI_assert(MEMCMP_STRUCT_AFTER_IS_ZERO(tmd, modifier));
120 
122 
123  /* Enable in editmode by default */
125 }
126 
128 {
130  Mesh *result;
131  if (!(result = triangulate_mesh(
132  mesh, tmd->quad_method, tmd->ngon_method, tmd->min_vertices, tmd->flag))) {
133  return mesh;
134  }
135 
136  return result;
137 }
138 
139 static void panel_draw(const bContext *UNUSED(C), Panel *panel)
140 {
141  uiLayout *layout = panel->layout;
142 
143  PointerRNA ob_ptr;
145 
146  uiLayoutSetPropSep(layout, true);
147 
148  uiItemR(layout, ptr, "quad_method", 0, NULL, ICON_NONE);
149  uiItemR(layout, ptr, "ngon_method", 0, NULL, ICON_NONE);
150  uiItemR(layout, ptr, "min_vertices", 0, NULL, ICON_NONE);
151  uiItemR(layout, ptr, "keep_custom_normals", 0, NULL, ICON_NONE);
152 
153  modifier_panel_end(layout, ptr);
154 }
155 
156 static void panelRegister(ARegionType *region_type)
157 {
159 }
160 
162  /* name */ "Triangulate",
163  /* structName */ "TriangulateModifierData",
164  /* structSize */ sizeof(TriangulateModifierData),
165  /* srna */ &RNA_TriangulateModifier,
170  /* icon */ ICON_MOD_TRIANGULATE,
171 
172  /* copyData */ BKE_modifier_copydata_generic,
173 
174  /* deformVerts */ NULL,
175  /* deformMatrices */ NULL,
176  /* deformVertsEM */ NULL,
177  /* deformMatricesEM */ NULL,
178  /* modifyMesh */ modifyMesh,
179  /* modifyHair */ NULL,
180  /* modifyGeometrySet */ NULL,
181  /* modifyVolume */ NULL,
182 
183  /* initData */ initData,
184  /* requiredDataMask */ NULL, // requiredDataMask,
185  /* freeData */ NULL,
186  /* isDisabled */ NULL,
187  /* updateDepsgraph */ NULL,
188  /* dependsOnTime */ NULL,
189  /* dependsOnNormals */ NULL,
190  /* foreachIDLink */ NULL,
191  /* foreachTexLink */ NULL,
192  /* freeRuntimeData */ NULL,
193  /* panelRegister */ panelRegister,
194  /* blendWrite */ NULL,
195  /* blendRead */ NULL,
196 };
typedef float(TangentPoint)[2]
void CustomData_clear_layer_flag(struct CustomData *data, int type, int flag)
Definition: customdata.c:2484
void * CustomData_get_layer(const struct CustomData *data, int type)
void CustomData_set_layer_flag(struct CustomData *data, int type, int flag)
Definition: customdata.c:2475
struct BMesh * BKE_mesh_to_bmesh_ex(const struct Mesh *me, const struct BMeshCreateParams *create_params, const struct BMeshFromMeshParams *convert_params)
struct Mesh * BKE_mesh_from_bmesh_for_eval_nomain(struct BMesh *bm, const struct CustomData_MeshMasks *cd_mask_extra, const struct Mesh *me_settings)
void BKE_mesh_calc_normals_split(struct Mesh *mesh)
Definition: mesh.c:1865
void BKE_mesh_set_custom_normals(struct Mesh *mesh, float(*r_custom_loopnors)[3])
@ eModifierTypeFlag_AcceptsCVs
Definition: BKE_modifier.h:81
@ eModifierTypeFlag_SupportsMapping
Definition: BKE_modifier.h:82
@ eModifierTypeFlag_EnableInEditmode
Definition: BKE_modifier.h:92
@ eModifierTypeFlag_SupportsEditmode
Definition: BKE_modifier.h:83
@ eModifierTypeFlag_AcceptsMesh
Definition: BKE_modifier.h:80
void BKE_modifier_copydata_generic(const struct ModifierData *md, struct ModifierData *md_dst, const int flag)
@ eModifierTypeType_Constructive
Definition: BKE_modifier.h:61
#define BLI_assert(a)
Definition: BLI_assert.h:58
#define UNUSED(x)
#define MEMCMP_STRUCT_AFTER_IS_ZERO(struct_var, member)
#define MEMCPY_STRUCT_AFTER(struct_dst, struct_src, member)
#define CD_MASK_NORMAL
#define CD_MASK_ORIGINDEX
@ CD_FLAG_TEMPORARY
#define DNA_struct_default_get(struct_name)
Definition: DNA_defaults.h:44
@ ME_EDGEDRAW
@ ME_EDGERENDER
@ eModifierMode_Editmode
@ eModifierType_Triangulate
struct TriangulateModifierData TriangulateModifierData
@ MOD_TRIANGULATE_KEEP_CUSTOMLOOP_NORMALS
Object is a sort of wrapper for general info.
Read Guarded memory(de)allocation.
ModifierTypeInfo modifierType_Triangulate
Mesh * triangulate_mesh(Mesh *mesh, const int quad_method, const int ngon_method, const int min_vertices, const int flag)
static Mesh * modifyMesh(ModifierData *md, const ModifierEvalContext *UNUSED(ctx), Mesh *mesh)
static void panel_draw(const bContext *UNUSED(C), Panel *panel)
static void initData(ModifierData *md)
static void panelRegister(ARegionType *region_type)
PointerRNA * modifier_panel_get_property_pointers(Panel *panel, PointerRNA *r_ob_ptr)
void modifier_panel_end(uiLayout *layout, PointerRNA *ptr)
PanelType * modifier_panel_register(ARegionType *region_type, ModifierType type, PanelDrawFn draw)
StructRNA RNA_TriangulateModifier
#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)
ATTR_WARN_UNUSED_RESULT BMesh * bm
void BM_mesh_free(BMesh *bm)
BMesh Free Mesh.
Definition: bmesh_mesh.c:307
void BM_mesh_triangulate(BMesh *bm, const int quad_method, const int ngon_method, const int min_vertices, const bool tag_only, BMOperator *op, BMOpSlot *slot_facemap_out, BMOpSlot *slot_facemap_double_out)
struct CustomData pdata ldata
struct uiLayout * layout
PointerRNA * ptr
Definition: wm_files.c:3157