107 edgehash.
add_or_modify({v1,
v2}, [](
int *value) { *value = 1; }, [](
int *value) { (*value)++; });
117 float t1, t2, t3, len1, len2, len3, area;
118 float *varea = sys->
varea, *v1, *
v2, *v3;
142 varea[i1] += (obtuse == 1) ? area : area * 0.5f;
143 varea[i2] += (obtuse == 2) ? area : area * 0.5f;
144 varea[i3] += (obtuse == 3) ? area : area * 0.5f;
155 varea[i1] += (t2 + t3) * 0.25f;
156 varea[i2] += (t1 + t3) * 0.25f;
157 varea[i3] += (t1 + t2) * 0.25f;
164 float *varea = sys->
varea, *v1, *
v2, *v3;
190 sys->
fweights[f][0] = t1 * varea[i1];
191 sys->
fweights[f][1] = t2 * varea[i2];
192 sys->
fweights[f][2] = t3 * varea[i3];
200 sys = MEM_new<LaplacianSystem>(__func__);
248 for (a = 0, face = sys->
faces; a < sys->faces_num; a++, face++) {
255 for (a = 0, face = sys->
faces; a < sys->faces_num; a++, face++) {
260 for (a = 0; a < verts_num; a++) {
262 if (sys->
varea[a] != 0.0f) {
267 sys->
varea[a] = 1.0f;
280 for (a = 0, face = sys->
faces; a < faces_num; a++, face++) {
284 MEM_delete(sys->
faces);
285 sys->
faces =
nullptr;
287 MEM_SAFE_DELETE(sys->
varea);
293 MEM_delete(sys->
verts);
296 MEM_delete(sys->
varea);
302 MEM_delete(sys->
faces);
354#define WEIGHT_LIMIT_START 0.05f
355#define WEIGHT_LIMIT_END 0.025f
356#define DISTANCE_EPSILON 1e-4f
369 float (*verts)[3] =
data->sys->heat.verts;
370 const float *vtri_co[3];
373 vtri_co[0] = verts[corner_verts[tri[0]]];
374 vtri_co[1] = verts[corner_verts[tri[1]]];
375 vtri_co[2] = verts[corner_verts[tri[2]]];
377#ifdef USE_KDOPBVH_WATERTIGHT
385 if (dist_test < hit->dist) {
390 hit->
dist = dist_test;
409 for (a = 0; a < tris_num; a++) {
410 const int3 &tri = corner_tris[a];
414 vtri[0] = corner_verts[tri[0]];
415 vtri[1] = corner_verts[tri[1]];
416 vtri[2] = corner_verts[tri[2]];
466 static_cast<void *
>(&
data)) == -1;
473 float closest[3], d[3], dist, cosine;
485 return dist / (0.5f * (cosine + 1.001f));
505 float dist, mindist, h;
506 int j, numclosest = 0;
514 mindist = std::min(dist, mindist);
526 sys->
heat.
p[
vertex] = (numclosest > 0) ? 1.0f / numclosest : 0.0f;
529 if (numclosest > 0) {
530 mindist =
max_ff(mindist, 1e-4f);
531 h = numclosest *
C_WEIGHT / (mindist * mindist);
543 int a, v1,
v2, v3, (*face)[3];
547 for (a = 0, face = sys->
faces; a < sys->faces_num; a++, face++) {
578 for (a = 0; a < verts_num; a++) {
582 for (a = 0; a < tris_num; a++) {
584 vtri[0] = corner_verts[corner_tris[a][0]];
585 vtri[1] = corner_verts[corner_tris[a][1]];
586 vtri[2] = corner_verts[corner_tris[a][2]];
593 for (a = 0; a < verts_num; a++) {
605 MEM_delete(sys->
heat.
H);
606 MEM_delete(sys->
heat.
p);
632 const bool *selected,
633 const char **r_error_str)
637 float solution, weight;
638 int *vertsflipped =
nullptr, *
mask =
nullptr;
639 int a, tris_num, j, bbone, firstsegment, lastsegment;
649 *r_error_str =
nullptr;
664 for (
const int vert : corner_verts.
slice(faces[i])) {
665 mask[vert] = select_vert[vert];
670 else if (use_face_sel) {
675 if (select_poly[i]) {
676 for (
const int vert : corner_verts.
slice(faces[i])) {
692 vert_positions, faces, corner_verts, {corner_tris, sys->
heat.
tris_num});
709 for (a = 0; a <
mesh->verts_num; a++) {
715 for (j = 0; j < numbones; j++) {
716 if (selected[j] ==
false) {
720 firstsegment = (j == 0 || dgrouplist[j - 1] != dgrouplist[j]);
721 lastsegment = (j == numbones - 1 || dgrouplist[j] != dgrouplist[j + 1]);
722 bbone = !(firstsegment && lastsegment);
725 if (bbone && firstsegment) {
726 for (a = 0; a <
mesh->verts_num; a++) {
732 if (vertsflipped && dgroupflip[j] && vertsflipped[a] >= 0) {
741 for (a = 0; a <
mesh->verts_num; a++) {
750 for (a = 0; a <
mesh->verts_num; a++) {
758 if (solution > 0.0f) {
773 if (vertsflipped && dgroupflip[j] && vertsflipped[a] >= 0) {
775 if (solution > 0.0f) {
777 ob, dgroupflip[j], vertsflipped[a], solution,
WEIGHT_ADD);
793 else if (*r_error_str ==
nullptr) {
794 *r_error_str =
N_(
"Bone Heat Weighting: failed to find solution for one or more bones");
799 if (bbone && lastsegment) {
800 for (a = 0; a <
mesh->verts_num; a++) {
807 if (weight <= 0.0f) {
811 if (vertsflipped && dgroupflip[j] && vertsflipped[a] >= 0) {
814 if (weight <= 0.0f) {
824 MEM_delete(vertsflipped);
840#define EPSILON 0.0001f
842#define MESHDEFORM_TAG_UNTYPED 0
843#define MESHDEFORM_TAG_BOUNDARY 1
844#define MESHDEFORM_TAG_INTERIOR 2
845#define MESHDEFORM_TAG_EXTERIOR 3
848#define MESHDEFORM_LEN_THRESHOLD 1e-6f
850#define MESHDEFORM_MIN_INFLUENCE 0.0005f
950 float no[3], co[3], dist;
955 face[0] = mdb->
cagecos[corner_verts[tri[0]]];
956 face[1] = mdb->
cagecos[corner_verts[tri[1]]];
957 face[2] = mdb->
cagecos[corner_verts[tri[2]]];
962 if (!isect_ray_tri || dist > isec->
vec_length) {
975 if (dist < hit->dist) {
995 float end[3], vec_normal[3];
998 if (mdb->
bvhtree ==
nullptr) [[unlikely]] {
1003 memset(&isect_mdef, 0,
sizeof(isect_mdef));
1004 isect_mdef.
lambda = 1e10f;
1045 for (
int i = 0; i < face.
size(); i++) {
1046 copy_v3_v3(mp_cagecos[i], cagecos[corner_verts[face[i]]]);
1050 reinterpret_cast<float (*)[3]
>(mp_cagecos.
data()),
1063 float outside[3], start[3], dir[3];
1066 for (i = 1; i <= 6; i++) {
1076 if (isect && !isect->
facing) {
1088 int size = mdb->
size;
1104 return x +
y * size +
z * size * size;
1122 float center[3], ncenter[3];
1129 for (i = 1; i <= 6; i++) {
1146 int *stack, *tag = mdb->
tag;
1147 int a,
b, i, xyz[3], stacksize, size = mdb->
size;
1157 while (stacksize > 0) {
1158 a = stack[--stacksize];
1160 xyz[2] = a / (size * size);
1161 xyz[1] = (a - xyz[2] * size * size) / size;
1162 xyz[0] = a - xyz[1] * size - xyz[2] * size * size;
1164 for (i = 1; i <= 6; i++) {
1172 stack[stacksize++] =
b;
1179 for (a = 0; a < size * size * size; a++) {
1188 tb = ti = te = ts = 0;
1189 for (a = 0; a < size * size * size; a++) {
1205 printf(
"interior %d exterior %d boundary %d semi-boundary %d\n", ti, te, tb, ts);
1219 for (
int i = 0; i < face.
size(); i++) {
1220 if (corner_verts[face[i]] == cagevert) {
1229 const float *gridvec,
1233 float dvec[3], ivec[3],
result = 0.0f;
1234 float totweight = 0.0f;
1236 for (
int i = 0; i < 3; i++) {
1237 ivec[i] = int(gridvec[i]);
1238 dvec[i] = gridvec[i] - ivec[i];
1241 for (
int i = 0; i < 8; i++) {
1251 wx = 1.0f - dvec[0];
1260 wy = 1.0f - dvec[1];
1269 wz = 1.0f - dvec[2];
1277 float weight = wx * wy * wz;
1279 totweight += weight;
1282 if (totweight > 0.0f) {
1298 for (i = 1; i <= 6; i++) {
1307 float weight, totweight = 0.0f;
1313 for (i = 1; i <= 6; i++) {
1322 weight = 1.0f / mdb->
width[0];
1328 totweight += weight;
1338 float weight, totweight;
1349 for (i = 1; i <= 6; i++) {
1357 weight = (1.0f / mdb->
width[0]) / totweight;
1367 float rhs, weight, totweight;
1376 for (i = 1; i <= 6; i++) {
1385 weight = (1.0f / isect->
len) / totweight;
1396 float rhs, weight, totweight;
1407 for (i = 1; i <= 6; i++) {
1411 weight = (1.0f / isect->
len) / totweight;
1421 float phi, totweight;
1431 for (i = 1; i <= 6; i++) {
1440 if (totweight != 0.0f) {
1441 mdb->
phi[acenter] = phi / totweight;
1448 float vec[3], gridvec[3];
1449 int a,
b,
x,
y,
z, totvar;
1454 for (a = 0, totvar = 0; a < mdb->
size3; a++) {
1469 for (
z = 0;
z < mdb->
size;
z++) {
1470 for (
y = 0;
y < mdb->
size;
y++) {
1471 for (
x = 0;
x < mdb->
size;
x++) {
1480 for (
z = 0;
z < mdb->
size;
z++) {
1481 for (
y = 0;
y < mdb->
size;
y++) {
1482 for (
x = 0;
x < mdb->
size;
x++) {
1489 for (
z = 0;
z < mdb->
size;
z++) {
1490 for (
y = 0;
y < mdb->
size;
y++) {
1491 for (
x = 0;
x < mdb->
size;
x++) {
1497 for (
z = 0;
z < mdb->
size;
z++) {
1498 for (
y = 0;
y < mdb->
size;
y++) {
1499 for (
x = 0;
x < mdb->
size;
x++) {
1543 mmd->
object, &mmd->
modifier,
"Failed to find bind solution (increase precision?)");
1544 error(
"Mesh Deform: failed to find bind solution.");
1557 printf(
"totalphi deficiency [%s|%d] %d: %.10f\n",
1578 float center[3], vec[3], maxwidth, totweight;
1579 int a,
b,
x,
y,
z, totinside, offset;
1624 for (a = 0; a < 3; a++) {
1625 maxwidth = std::max(mdb->
max[a] - mdb->
min[a], maxwidth);
1628 for (a = 0; a < 3; a++) {
1629 center[a] = (mdb->
min[a] + mdb->
max[a]) * 0.5f;
1630 mdb->
min[a] = center[a] - maxwidth * 0.5f;
1631 mdb->
max[a] = center[a] + maxwidth * 0.5f;
1634 mdb->
min[a] -= 2.1f * mdb->
width[a];
1635 mdb->
max[a] += 2.1f * mdb->
width[a];
1658 for (a = 0; a < mdb->
size3; a++) {
1663 for (
z = 0;
z < mdb->
size;
z++) {
1664 for (
y = 0;
y < mdb->
size;
y++) {
1665 for (
x = 0;
x < mdb->
size;
x++) {
1674 for (
z = 0;
z < mdb->
size;
z++) {
1675 for (
y = 0;
y < mdb->
size;
y++) {
1676 for (
x = 0;
x < mdb->
size;
x++) {
1688 for (a = 0; a < mdb->
size3; a++) {
1689 for (inf = mdb->
dyngrid[a]; inf; inf = inf->
next) {
1695 mmd->
dyngrid = MEM_new_array<MDefCell>(mdb->
size3,
"MDefDynGrid");
1700 for (a = 0; a < mdb->
size3; a++) {
1706 for (inf = mdb->
dyngrid[a]; inf; inf = inf->
next, mdinf++) {
1709 totweight += mdinf->
weight;
1713 if (totweight > 0.0f) {
1716 mdinf->
weight /= totweight;
1735 MEM_delete(mdb->
tag);
1736 MEM_delete(mdb->
phi);
1748 float cagemat[4][4])
#define BVH_RAYCAST_DIST_MAX
#define BLI_MEMARENA_STD_BUFSIZE
#define SNPRINTF_UTF8(dst, format,...)
Object is a sort of wrapper for general info.
Read Blender memory allocation and freeing.
#define INIT_MINMAX(min, max)
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
bool closest(btVector3 &v)
constexpr int64_t size() const
const Value & lookup(const Key &key) const
auto add_or_modify(const Key &key, const CreateValueF &create_value, const ModifyValueF &modify_value) -> decltype(create_value(nullptr))
constexpr Span slice(int64_t start, int64_t size) const
constexpr bool is_empty() const
GAttributeReader lookup_or_default(StringRef name, AttrDomain domain, AttrType data_type, const void *default_value=nullptr) const
IndexRange index_range() const
LinearSolver * EIG_linear_solver_new(int num_rows, int num_columns, int num_right_hand_sides)
void EIG_linear_solver_variable_set(LinearSolver *solver, int rhs, int index, double value)
void EIG_linear_solver_right_hand_side_add(LinearSolver *solver, int rhs, int index, double value)
LinearSolver * EIG_linear_least_squares_solver_new(int num_rows, int num_columns, int num_right_hand_sides)
void EIG_linear_solver_delete(LinearSolver *solver)
double EIG_linear_solver_variable_get(LinearSolver *solver, int rhs, int index)
void EIG_linear_solver_matrix_add(LinearSolver *solver, int row, int col, double value)
bool EIG_linear_solver_solve(LinearSolver *solver)
void EIG_linear_solver_variable_lock(LinearSolver *solver, int index)
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
#define WEIGHT_LIMIT_START
#define MESHDEFORM_MIN_INFLUENCE
#define MESHDEFORM_TAG_UNTYPED
#define MESHDEFORM_TAG_BOUNDARY
#define MESHDEFORM_TAG_INTERIOR
#define MESHDEFORM_LEN_THRESHOLD
#define MESHDEFORM_TAG_EXTERIOR
void corner_tris_calc(Span< float3 > vert_positions, OffsetIndices< int > faces, Span< int > corner_verts, MutableSpan< int3 > corner_tris)
float vgroup_vert_weight(Object *ob, bDeformGroup *dg, int vertnum)
void vgroup_vert_add(Object *ob, bDeformGroup *dg, int vertnum, float weight, int assignmode)
void vgroup_vert_remove(Object *ob, bDeformGroup *dg, int vertnum)
const ImplicitSharingInfo * info_for_mem_free(T *data)
MINLINE void madd_v3_v3v3fl(float r[3], const float a[3], const float b[3], float f)
static const int MESHDEFORM_OFFSET[7][3]
int laplacian_system_solve(LaplacianSystem *sys)
static void meshdeform_matrix_add_rhs(MeshDeformBind *mdb, LinearSolver *context, int x, int y, int z, int cagevert)
static void heat_ray_tree_create(LaplacianSystem *sys)
void BKE_modifier_set_error(const Object *ob, ModifierData *md, const char *format,...) ATTR_PRINTF_FORMAT(3
static void start_progress_bar()
void BLI_memarena_use_calloc(MemArena *ma) ATTR_NONNULL(1)
static void meshdeform_matrix_add_exterior_phi(MeshDeformBind *mdb, int x, int y, int z, int)
MemArena * BLI_memarena_new(size_t bufsize, const char *name) ATTR_WARN_UNUSED_RESULT ATTR_RETURNS_NONNULL ATTR_NONNULL(2) ATTR_MALLOC
static float meshdeform_boundary_phi(const MeshDeformBind *mdb, const MDefBoundIsect *isect, int cagevert)
static void laplacian_system_delete(LaplacianSystem *sys)
MINLINE void copy_v3_v3(float r[3], const float a[3])
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
static float heat_limit_weight(float weight)
void minmax_v3v3_v3(float min[3], float max[3], const float vec[3])
void laplacian_begin_solve(LaplacianSystem *sys, int index)
static void meshdeform_matrix_add_semibound_phi(MeshDeformBind *mdb, int x, int y, int z, int cagevert)
bool isect_ray_tri_watertight_v3(const float ray_origin[3], const struct IsectRayPrecalc *isect_precalc, const float v0[3], const float v1[3], const float v2[3], float *r_lambda, float r_uv[2])
void laplacian_add_right_hand_side(LaplacianSystem *sys, int v, float value)
void mul_v3_m4v3(float r[3], const float mat[4][4], const float vec[3])
static float heat_source_distance(LaplacianSystem *sys, int vertex, int source)
void BLI_bvhtree_free(BVHTree *tree)
float angle_v3v3v3(const float a[3], const float b[3], const float c[3]) ATTR_WARN_UNUSED_RESULT
static void meshdeform_add_intersections(MeshDeformBind *mdb, int x, int y, int z)
int BLI_bvhtree_ray_cast_ex(const BVHTree *tree, const float co[3], const float dir[3], float radius, BVHTreeRayHit *hit, BVHTree_RayCastCallback callback, void *userdata, int flag)
void laplacian_add_vertex(LaplacianSystem *sys, float *co, int pinned)
void BLI_bvhtree_balance(BVHTree *tree)
float closest_to_line_segment_v3(float r_close[3], const float p[3], const float l1[3], const float l2[3])
int mesh_get_x_mirror_vert(Object *ob, Mesh *mesh_eval, int index, bool use_topology)
void BKE_mesh_wrapper_ensure_mdata(Mesh *mesh)
static void meshdeform_bind_floodfill(MeshDeformBind *mdb)
static int laplacian_edge_count(const Map< OrderedEdge, int > &edgehash, int v1, int v2)
static void laplacian_system_construct_end(LaplacianSystem *sys)
ATTR_WARN_UNUSED_RESULT const BMVert * v
static float meshdeform_interp_w(MeshDeformBind *mdb, const float *gridvec, float *, int)
float normal_tri_v3(float n[3], const float v1[3], const float v2[3], const float v3[3])
static int heat_source_closest(LaplacianSystem *sys, int vertex, int source)
int BLI_bvhtree_ray_cast(const BVHTree *tree, const float co[3], const float dir[3], float radius, BVHTreeRayHit *hit, BVHTree_RayCastCallback callback, void *userdata)
static void meshdeform_matrix_solve(MeshDeformModifierData *mmd, MeshDeformBind *mdb)
static void heat_calc_vnormals(LaplacianSystem *sys)
MINLINE void mul_v3_fl(float r[3], float f)
void mul_m4_v3(const float M[4][4], float r[3])
static void heat_system_free(LaplacianSystem *sys)
static void bvh_callback(void *userdata, int index, const BVHTreeRay *ray, BVHTreeRayHit *hit)
static LaplacianSystem * laplacian_system_construct_begin(int verts_num, int faces_num, int lsq)
static void meshdeform_check_semibound(MeshDeformBind *mdb, int x, int y, int z)
VecBase< int32_t, 3 > int3
BMesh const char void * data
static void end_progress_bar()
static void error(const char *str)
static void harmonic_coordinates_bind(MeshDeformModifierData *mmd, MeshDeformBind *mdb)
static void harmonic_ray_callback(void *userdata, int index, const BVHTreeRay *ray, BVHTreeRayHit *hit)
BVHTree * BLI_bvhtree_new(int maxsize, float epsilon, char tree_type, char axis)
static int meshdeform_inside_cage(MeshDeformBind *mdb, float *co)
static void laplacian_triangle_weights(LaplacianSystem *sys, int f, int i1, int i2, int i3)
static MDefBoundIsect * meshdeform_ray_tree_intersect(MeshDeformBind *mdb, const float co1[3], const float co2[3])
static float meshdeform_boundary_total_weight(MeshDeformBind *mdb, int x, int y, int z)
void interp_weights_poly_v3(float w[], float v[][3], int n, const float co[3])
bool isect_ray_tri_v3(const float ray_origin[3], const float ray_direction[3], const float v0[3], const float v1[3], const float v2[3], float *r_lambda, float r_uv[2])
MINLINE int poly_to_tri_count(int poly_count, int corner_count)
static void waitcursor(int)
MINLINE float normalize_v3_v3(float r[3], const float a[3])
static void heat_laplacian_create(LaplacianSystem *sys)
void * BLI_memarena_alloc(MemArena *ma, size_t size) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC ATTR_ALLOC_SIZE(2)
MINLINE float normalize_v3(float n[3])
void ED_mesh_deform_bind_callback(Object *object, MeshDeformModifierData *mmd, Mesh *cagemesh, float *vertexcos, int verts_num, float cagemat[4][4])
void heat_bone_weighting(Object *ob, Mesh *mesh, float(*verts)[3], int numbones, bDeformGroup **dgrouplist, bDeformGroup **dgroupflip, float(*root)[3], float(*tip)[3], const bool *selected, const char **r_error_str)
MINLINE void add_v3_v3(float r[3], const float a[3])
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
float cotangent_tri_weight_v3(const float v1[3], const float v2[3], const float v3[3])
static int heat_ray_source_visible(LaplacianSystem *sys, int vertex, int source)
static void laplacian_triangle_area(LaplacianSystem *sys, int i1, int i2, int i3)
float laplacian_system_get_solution(LaplacianSystem *sys, int v)
ATTR_WARN_UNUSED_RESULT const BMVert * v2
MINLINE float max_ff(float a, float b)
float area_tri_v3(const float v1[3], const float v2[3], const float v3[3])
void BLI_bvhtree_insert(BVHTree *tree, int index, const float co[3], int numpoints)
static void heat_set_H(LaplacianSystem *sys, int vertex)
static void meshdeform_matrix_add_cell(MeshDeformBind *mdb, LinearSolver *context, int x, int y, int z)
void copy_m4_m4(float m1[4][4], const float m2[4][4])
BLI_INLINE int meshdeform_index(MeshDeformBind *mdb, int x, int y, int z, int n)
BLI_INLINE void meshdeform_cell_center(MeshDeformBind *mdb, int x, int y, int z, int n, float *center)
static void progress_bar(int, const char *)
MINLINE float len_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
static void laplacian_increase_edge_count(Map< OrderedEdge, int > &edgehash, int v1, int v2)
void laplacian_add_triangle(LaplacianSystem *sys, int v1, int v2, int v3)
ModifierData * BKE_modifier_get_original(const Object *object, ModifierData *md)
MINLINE float dot_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
void BKE_modifier_mdef_compact_influences(ModifierData *md)
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
void BLI_memarena_free(MemArena *ma) ATTR_NONNULL(1)
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
struct IsectRayPrecalc * isect_precalc
Map< OrderedEdge, int > edgehash
struct blender::LaplacianSystem::HeatWeighting heat