Blender  V3.3
editmesh_cache.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
9 #include "MEM_guardedalloc.h"
10 
11 #include "BLI_bounds.hh"
12 #include "BLI_math_vector.h"
13 #include "BLI_span.hh"
14 
15 #include "DNA_mesh_types.h"
16 
17 #include "BKE_editmesh.h"
18 #include "BKE_editmesh_cache.h" /* own include */
19 
20 /* -------------------------------------------------------------------- */
25 {
26  if (!(emd->vertexCos && (emd->polyNos == nullptr))) {
27  return;
28  }
29 
30  BMesh *bm = em->bm;
31  BMFace *efa;
32  BMIter fiter;
33  int i;
34 
36 
37  float(*polyNos)[3] = static_cast<float(*)[3]>(
38  MEM_mallocN(sizeof(*polyNos) * bm->totface, __func__));
39 
40  const float(*vertexCos)[3] = emd->vertexCos;
41 
42  BM_ITER_MESH_INDEX (efa, &fiter, bm, BM_FACES_OF_MESH, i) {
43  BM_elem_index_set(efa, i); /* set_inline */
44  BM_face_calc_normal_vcos(bm, efa, polyNos[i], vertexCos);
45  }
47 
48  emd->polyNos = (const float(*)[3])polyNos;
49 }
50 
52 {
53  if (!(emd->vertexCos && (emd->vertexNos == nullptr))) {
54  return;
55  }
56 
57  BMesh *bm = em->bm;
58  const float(*vertexCos)[3], (*polyNos)[3];
59  float(*vertexNos)[3];
60 
61  /* Calculate vertex normals from poly normals. */
63 
65 
66  polyNos = emd->polyNos;
67  vertexCos = emd->vertexCos;
68  vertexNos = static_cast<float(*)[3]>(MEM_callocN(sizeof(*vertexNos) * bm->totvert, __func__));
69 
70  BM_verts_calc_normal_vcos(bm, polyNos, vertexCos, vertexNos);
71 
72  emd->vertexNos = (const float(*)[3])vertexNos;
73 }
74 
76 {
77  if (emd->polyCos != nullptr) {
78  return;
79  }
80  BMesh *bm = em->bm;
81 
82  BMFace *efa;
83  BMIter fiter;
84  int i;
85 
86  float(*polyCos)[3] = static_cast<float(*)[3]>(
87  MEM_mallocN(sizeof(*polyCos) * bm->totface, __func__));
88 
89  if (emd->vertexCos) {
90  const float(*vertexCos)[3];
91  vertexCos = emd->vertexCos;
92 
94 
95  BM_ITER_MESH_INDEX (efa, &fiter, bm, BM_FACES_OF_MESH, i) {
96  BM_face_calc_center_median_vcos(bm, efa, polyCos[i], vertexCos);
97  }
98  }
99  else {
100  BM_ITER_MESH_INDEX (efa, &fiter, bm, BM_FACES_OF_MESH, i) {
101  BM_face_calc_center_median(efa, polyCos[i]);
102  }
103  }
104 
105  emd->polyCos = (const float(*)[3])polyCos;
106 }
107 
110 /* -------------------------------------------------------------------- */
115  struct EditMeshData *emd,
116  float min[3],
117  float max[3])
118 {
119  using namespace blender;
120  BMesh *bm = em->bm;
121 
122  if (bm->totvert) {
123  if (emd->vertexCos) {
124  Span<float3> vert_coords(reinterpret_cast<const float3 *>(emd->vertexCos), bm->totvert);
125  std::optional<bounds::MinMaxResult<float3>> bounds = bounds::min_max(vert_coords);
126  BLI_assert(bounds.has_value());
129  }
130  else {
131  BMVert *eve;
132  BMIter iter;
133  BM_ITER_MESH (eve, &iter, bm, BM_VERTS_OF_MESH) {
134  minmax_v3v3_v3(min, max, eve->co);
135  }
136  }
137  return true;
138  }
139 
140  zero_v3(min);
141  zero_v3(max);
142  return false;
143 }
144 
typedef float(TangentPoint)[2]
#define BLI_assert(a)
Definition: BLI_assert.h:46
void minmax_v3v3_v3(float min[3], float max[3], const float vec[3])
Definition: math_vector.c:867
MINLINE void copy_v3_v3(float r[3], const float a[3])
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:111
#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.cc:446
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.
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])
static btDbvtVolume bounds(btDbvtNode **leaves, int count)
Definition: btDbvt.cpp:299
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:31
void *(* MEM_mallocN)(size_t len, const char *str)
Definition: mallocn.c:33
static std::optional< MinMaxResult< T > > min_max(Span< T > values)
Definition: BLI_bounds.hh:26
T min(const T &a, const T &b)
T max(const T &a, const T &b)
#define min(a, b)
Definition: sort.c:35
struct BMesh * bm
Definition: BKE_editmesh.h:40
float co[3]
Definition: bmesh_class.h:87
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