Blender  V2.93
editmesh_cache.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 
23 #include "MEM_guardedalloc.h"
24 
25 #include "BLI_math_vector.h"
26 
27 #include "DNA_mesh_types.h"
28 
29 #include "BKE_editmesh.h"
30 #include "BKE_editmesh_cache.h" /* own include */
31 
32 /* -------------------------------------------------------------------- */
37 {
38  if (!(emd->vertexCos && (emd->polyNos == NULL))) {
39  return;
40  }
41 
42  BMesh *bm = em->bm;
43  const float(*vertexCos)[3];
44  float(*polyNos)[3];
45 
46  BMFace *efa;
47  BMIter fiter;
48  int i;
49 
51 
52  polyNos = MEM_mallocN(sizeof(*polyNos) * bm->totface, __func__);
53 
54  vertexCos = emd->vertexCos;
55 
56  BM_ITER_MESH_INDEX (efa, &fiter, bm, BM_FACES_OF_MESH, i) {
57  BM_elem_index_set(efa, i); /* set_inline */
58  BM_face_calc_normal_vcos(bm, efa, polyNos[i], vertexCos);
59  }
61 
62  emd->polyNos = (const float(*)[3])polyNos;
63 }
64 
66 {
67  if (!(emd->vertexCos && (emd->vertexNos == NULL))) {
68  return;
69  }
70 
71  BMesh *bm = em->bm;
72  const float(*vertexCos)[3], (*polyNos)[3];
73  float(*vertexNos)[3];
74 
75  /* calculate vertex normals from poly normals */
77 
79 
80  polyNos = emd->polyNos;
81  vertexCos = emd->vertexCos;
82  vertexNos = MEM_callocN(sizeof(*vertexNos) * bm->totvert, __func__);
83 
84  BM_verts_calc_normal_vcos(bm, polyNos, vertexCos, vertexNos);
85 
86  emd->vertexNos = (const float(*)[3])vertexNos;
87 }
88 
90 {
91  if (emd->polyCos != NULL) {
92  return;
93  }
94  BMesh *bm = em->bm;
95  float(*polyCos)[3];
96 
97  BMFace *efa;
98  BMIter fiter;
99  int i;
100 
101  polyCos = MEM_mallocN(sizeof(*polyCos) * bm->totface, __func__);
102 
103  if (emd->vertexCos) {
104  const float(*vertexCos)[3];
105  vertexCos = emd->vertexCos;
106 
108 
109  BM_ITER_MESH_INDEX (efa, &fiter, bm, BM_FACES_OF_MESH, i) {
110  BM_face_calc_center_median_vcos(bm, efa, polyCos[i], vertexCos);
111  }
112  }
113  else {
114  BM_ITER_MESH_INDEX (efa, &fiter, bm, BM_FACES_OF_MESH, i) {
115  BM_face_calc_center_median(efa, polyCos[i]);
116  }
117  }
118 
119  emd->polyCos = (const float(*)[3])polyCos;
120 }
121 
124 /* -------------------------------------------------------------------- */
129  struct EditMeshData *emd,
130  float min[3],
131  float max[3])
132 {
133  BMesh *bm = em->bm;
134  BMVert *eve;
135  BMIter iter;
136  int i;
137 
138  if (bm->totvert) {
139  if (emd->vertexCos) {
140  BM_ITER_MESH_INDEX (eve, &iter, bm, BM_VERTS_OF_MESH, i) {
141  minmax_v3v3_v3(min, max, emd->vertexCos[i]);
142  }
143  }
144  else {
145  BM_ITER_MESH (eve, &iter, bm, BM_VERTS_OF_MESH) {
146  minmax_v3v3_v3(min, max, eve->co);
147  }
148  }
149  return true;
150  }
151 
152  zero_v3(min);
153  zero_v3(max);
154  return false;
155 }
156 
typedef float(TangentPoint)[2]
void minmax_v3v3_v3(float min[3], float max[3], const float vec[3])
Definition: math_vector.c:1020
MINLINE void zero_v3(float r[3])
Read Guarded memory(de)allocation.
@ BM_FACE
Definition: bmesh_class.h:386
@ BM_VERT
Definition: bmesh_class.h:383
#define BM_elem_index_set(ele, index)
Definition: bmesh_inline.h:125
#define BM_ITER_MESH(ele, iter, bm, itype)
#define BM_ITER_MESH_INDEX(ele, iter, bm, itype, indexvar)
@ BM_VERTS_OF_MESH
@ BM_FACES_OF_MESH
ATTR_WARN_UNUSED_RESULT BMesh * bm
void BM_mesh_elem_index_ensure(BMesh *bm, const char htype)
Definition: bmesh_mesh.c:2152
void BM_verts_calc_normal_vcos(BMesh *bm, const float(*fnos)[3], const float(*vcos)[3], float(*vnos)[3])
BMesh Compute Normals from/to external data.
Definition: bmesh_mesh.c:538
void BM_face_calc_center_median_vcos(const BMesh *bm, const BMFace *f, float r_cent[3], float const (*vertexCos)[3])
void BM_face_calc_center_median(const BMFace *f, float r_cent[3])
float BM_face_calc_normal_vcos(const BMesh *bm, const BMFace *f, float r_no[3], float const (*vertexCos)[3])
void BKE_editmesh_cache_ensure_vert_normals(BMEditMesh *em, EditMeshData *emd)
void BKE_editmesh_cache_ensure_poly_normals(BMEditMesh *em, EditMeshData *emd)
void BKE_editmesh_cache_ensure_poly_centers(BMEditMesh *em, EditMeshData *emd)
bool BKE_editmesh_cache_calc_minmax(struct BMEditMesh *em, struct EditMeshData *emd, float min[3], float max[3])
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:45
void *(* MEM_mallocN)(size_t len, const char *str)
Definition: mallocn.c:47
#define min(a, b)
Definition: sort.c:51
struct BMesh * bm
Definition: BKE_editmesh.h:52
float co[3]
Definition: bmesh_class.h:99
int totvert
Definition: bmesh_class.h:297
char elem_index_dirty
Definition: bmesh_class.h:305
int totface
Definition: bmesh_class.h:297
float const (* polyNos)[3]
const float(* polyCos)[3]
float const (* vertexNos)[3]
const float(* vertexCos)[3]
float max