[Bf-blender-cvs] [436bbb05ce2] soc-2019-adaptive-cloth: Cloth: use better system for checking equality
ishbosamiya
noreply at git.blender.org
Fri Jun 21 14:32:17 CEST 2019
Commit: 436bbb05ce2a3b4e38c8535beba3f29701223791
Author: ishbosamiya
Date: Fri Jun 21 17:59:25 2019 +0530
Branches: soc-2019-adaptive-cloth
https://developer.blender.org/rB436bbb05ce2a3b4e38c8535beba3f29701223791
Cloth: use better system for checking equality
Due to floating point precision errors, equality of floats are not always precise, this means we need to use some epsilon value which acts as the range for the allowable errors.
===================================================================
M source/blender/blenkernel/intern/cloth.c
===================================================================
diff --git a/source/blender/blenkernel/intern/cloth.c b/source/blender/blenkernel/intern/cloth.c
index 9f39baa64b4..61c75adf76a 100644
--- a/source/blender/blenkernel/intern/cloth.c
+++ b/source/blender/blenkernel/intern/cloth.c
@@ -512,64 +512,49 @@ static void cloth_copy_cloth_vertex(ClothVertex *r, ClothVertex *src)
r->shrink_factor = src->shrink_factor;
}
-/* factor: first vertex properties factor over finding the mean, given between 0.0f and 1.0f
- * Simply put, v1 properties * factor + v2 properties * (1.0f - factor)
- */
-static ClothVertex cloth_remeshing_mean_cloth_vert(ClothVertex v1, ClothVertex v2, float factor)
+static ClothVertex cloth_remeshing_mean_cloth_vert(ClothVertex *v1, ClothVertex *v2)
{
ClothVertex new_vert;
- float inv_factor = 1.0f - factor;
/* TODO(Ish): flags */
- mul_v3_fl(v1.v, factor);
- mul_v3_fl(v2.v, inv_factor);
- add_v3_v3v3(new_vert.v, v1.v, v2.v);
-
- mul_v3_fl(v1.xconst, factor);
- mul_v3_fl(v2.xconst, inv_factor);
- add_v3_v3v3(new_vert.xconst, v1.xconst, v2.xconst);
-
- mul_v3_fl(v1.x, factor);
- mul_v3_fl(v2.x, inv_factor);
- add_v3_v3v3(new_vert.x, v1.x, v2.x);
-
- mul_v3_fl(v1.xold, factor);
- mul_v3_fl(v2.xold, inv_factor);
- add_v3_v3v3(new_vert.xold, v1.xold, v2.xold);
-
- mul_v3_fl(v1.tx, factor);
- mul_v3_fl(v2.tx, inv_factor);
- add_v3_v3v3(new_vert.tx, v1.tx, v2.tx);
-
- mul_v3_fl(v1.txold, factor);
- mul_v3_fl(v2.txold, inv_factor);
- add_v3_v3v3(new_vert.txold, v1.txold, v2.txold);
-
- mul_v3_fl(v1.tv, factor);
- mul_v3_fl(v2.tv, inv_factor);
- add_v3_v3v3(new_vert.tv, v1.tv, v2.tv);
-
- mul_v3_fl(v1.impulse, factor);
- mul_v3_fl(v2.impulse, inv_factor);
- add_v3_v3v3(new_vert.impulse, v1.impulse, v2.impulse);
-
- mul_v3_fl(v1.xrest, factor);
- mul_v3_fl(v2.xrest, inv_factor);
- add_v3_v3v3(new_vert.xrest, v1.xrest, v2.xrest);
-
- mul_v3_fl(v1.dcvel, factor);
- mul_v3_fl(v2.dcvel, inv_factor);
- add_v3_v3v3(new_vert.dcvel, v1.dcvel, v2.dcvel);
-
- new_vert.mass = ((v1.mass * factor) + (v2.mass * inv_factor));
- new_vert.goal = ((v1.goal * factor) + (v2.goal * inv_factor));
- new_vert.impulse_count = ((v1.impulse_count * factor) + (v2.impulse_count * inv_factor));
- /* new_vert.avg_spring_len = ((v1.avg_spring_len * factor) + (v2.avg_spring_len * inv_factor));
- */
- new_vert.struct_stiff = ((v1.struct_stiff * factor) + (v2.struct_stiff * inv_factor));
- new_vert.bend_stiff = ((v1.bend_stiff * factor) + (v2.bend_stiff * inv_factor));
- new_vert.shear_stiff = ((v1.shear_stiff * factor) + (v2.shear_stiff * inv_factor));
- /* new_vert.spring_count = ((v1.spring_count * factor) + (v2.spring_count * inv_factor)); */
- new_vert.shrink_factor = ((v1.shrink_factor * factor) + (v2.shrink_factor * inv_factor));
+ add_v3_v3v3(new_vert.v, v1->v, v2->v);
+ mul_v3_fl(new_vert.v, 0.5f);
+
+ add_v3_v3v3(new_vert.xconst, v1->xconst, v2->xconst);
+ mul_v3_fl(new_vert.xconst, 0.5f);
+
+ add_v3_v3v3(new_vert.x, v1->x, v2->x);
+ mul_v3_fl(new_vert.x, 0.5f);
+
+ add_v3_v3v3(new_vert.xold, v1->xold, v2->xold);
+ mul_v3_fl(new_vert.xold, 0.5f);
+
+ add_v3_v3v3(new_vert.tx, v1->tx, v2->tx);
+ mul_v3_fl(new_vert.tx, 0.5f);
+
+ add_v3_v3v3(new_vert.txold, v1->txold, v2->txold);
+ mul_v3_fl(new_vert.txold, 0.5f);
+
+ add_v3_v3v3(new_vert.tv, v1->tv, v2->tv);
+ mul_v3_fl(new_vert.tv, 0.5f);
+
+ add_v3_v3v3(new_vert.impulse, v1->impulse, v2->impulse);
+ mul_v3_fl(new_vert.impulse, 0.5f);
+
+ add_v3_v3v3(new_vert.xrest, v1->xrest, v2->xrest);
+ mul_v3_fl(new_vert.xrest, 0.5f);
+
+ add_v3_v3v3(new_vert.dcvel, v1->dcvel, v2->dcvel);
+ mul_v3_fl(new_vert.dcvel, 0.5f);
+
+ new_vert.mass = (v1->mass + v2->mass) * 0.5;
+ new_vert.goal = (v1->goal + v2->goal) * 0.5;
+ new_vert.impulse_count = (v1->impulse_count + v2->impulse_count) * 0.5;
+ /* new_vert.avg_spring_len = (v1->avg_spring_len + v2->avg_spring_len) * 0.5; */
+ new_vert.struct_stiff = (v1->struct_stiff + v2->struct_stiff) * 0.5;
+ new_vert.bend_stiff = (v1->bend_stiff + v2->bend_stiff) * 0.5;
+ new_vert.shear_stiff = (v1->shear_stiff + v2->shear_stiff) * 0.5;
+ /* new_vert.spring_count = (v1->spring_count + v2->spring_count) * 0.5; */
+ new_vert.shrink_factor = (v1->shrink_factor + v2->shrink_factor) * 0.5;
return new_vert;
}
@@ -649,7 +634,7 @@ static void cloth_remeshing_update_cloth_object_mesh(ClothModifierData *clmd, Me
break;
}
}
- new_cloth->verts[i] = cloth_remeshing_mean_cloth_vert(prev_vert, next_vert, factor);
+ new_cloth->verts[i] = cloth_remeshing_mean_cloth_vert(&prev_vert, &next_vert);
}
/* if old vert */
else {
@@ -1065,12 +1050,16 @@ static void cloth_remeshing_export_obj(BMesh *bm, char *file_name)
printf("File %s written\n", file_name);
}
+#define EPSILON_CLOTH 0.01
static ClothVertex *cloth_remeshing_find_cloth_vertex(BMVert *v, ClothVertex *verts, int vert_num)
{
ClothVertex *cv = NULL;
for (int i = 0; i < vert_num; i++) {
- if (equals_v3v3(v->co, verts[i].x)) {
+ /* if (equals_v3v3(v->co, verts[i].xold)) { */
+ if (fabs(v->co[0] - verts[i].xold[0]) < EPSILON_CLOTH &&
+ fabs(v->co[1] - verts[i].xold[1]) < EPSILON_CLOTH &&
+ fabs(v->co[2] - verts[i].xold[2]) < EPSILON_CLOTH) {
cv = &verts[i];
break;
}
@@ -1082,7 +1071,7 @@ static ClothVertex *cloth_remeshing_find_cloth_vertex(BMVert *v, ClothVertex *ve
static void cloth_remeshing_print_all_verts(ClothVertex *verts, int vert_num)
{
for (int i = 0; i < vert_num; i++) {
- printf("%f %f %f\n", verts[i].x[0], verts[i].x[1], verts[i].x[2]);
+ printf("%f %f %f\n", verts[i].xold[0], verts[i].xold[1], verts[i].xold[2]);
}
}
@@ -1110,15 +1099,15 @@ static bool cloth_remeshing_split_edges(ClothModifierData *clmd, LinkNodePair *s
v1 = cloth_remeshing_find_cloth_vertex(old_edge.v1, cloth->verts, cloth->mvert_num);
v2 = cloth_remeshing_find_cloth_vertex(old_edge.v2, cloth->verts, cloth->mvert_num);
#if 0
- printf("v: %f %f %f\n", old_edge.v1->co[0], old_edge.v1->co[1], old_edge.v1->co[2]);
+ printf("v1: %f %f %f\n", old_edge.v1->co[0], old_edge.v1->co[1], old_edge.v1->co[2]);
cloth_remeshing_print_all_verts(cloth->verts, cloth->mvert_num);
- printf("v: %f %f %f\n", old_edge.v2->co[0], old_edge.v2->co[1], old_edge.v2->co[2]);
+ printf("v2: %f %f %f\n", old_edge.v2->co[0], old_edge.v2->co[1], old_edge.v2->co[2]);
cloth_remeshing_print_all_verts(cloth->verts, cloth->mvert_num);
#endif
BLI_assert(v1 != NULL);
BLI_assert(v2 != NULL);
cloth->mvert_num += 1;
- cloth->verts[cloth->mvert_num - 1] = cloth_remeshing_mean_cloth_vert(*v1, *v2, 0.5);
+ cloth->verts[cloth->mvert_num - 1] = cloth_remeshing_mean_cloth_vert(v1, v2);
}
MEM_freeN(bad_edges);
return true;
More information about the Bf-blender-cvs
mailing list