66 # define TIMEIT_BENCH(expr, id) (expr)
70 #define OUT_OF_MEMORY() ((void)printf("Shrinkwrap: Out of memory\n"))
190 const float edge_dir[3],
194 float *direction = vdata[index].
direction;
200 if (status[index] >= 0 ? status[index] == side :
dot_v3v3(direction, edge_dir) < 0) {
207 status[index] = (status[index] == 0) ? side : -1;
220 unsigned int eidx = mloop[i].
e;
222 if (edge_mode[eidx] < 2) {
229 "ShrinkwrapBoundaryData::edge_is_boundary");
230 unsigned int num_boundary_edges = 0;
233 edge_mode[i] = (edge_mode[i] == 1);
237 num_boundary_edges++;
242 if (num_boundary_edges == 0) {
250 "ShrinkwrapBoundaryData");
259 "ShrinkwrapBoundaryData::looptri_is_boundary");
261 for (
int i = 0; i < totlooptri; i++) {
265 for (
int j = 0; j < 3; j++) {
266 if (edges[j] >= 0 && edge_mode[edges[j]]) {
277 (
size_t)
mesh->
totvert,
sizeof(
int),
"ShrinkwrapBoundaryData::vert_boundary_id");
281 const MEdge *edge = &medge[i];
305 const MEdge *edge = &medge[i];
369 weight = 1.0f - weight;
372 if (weight == 0.0f) {
390 if (nearest->
index != -1) {
400 if (nearest->
index != -1) {
403 if (nearest->
dist_sq > FLT_EPSILON) {
405 weight *= (dist - calc->
keepDist) / dist;
448 const float ray_radius,
458 float tmp_co[3], tmp_no[3];
459 const float *co, *no;
463 memcpy(&hit_tmp, hit,
sizeof(hit_tmp));
475 #ifdef USE_DIST_CORRECT
487 tree->bvh, co, no, ray_radius, &hit_tmp,
tree->treeData.raycast_callback, &
tree->treeData);
489 if (hit_tmp.
index != -1) {
507 #ifdef USE_DIST_CORRECT
514 memcpy(hit, &hit_tmp,
sizeof(hit_tmp));
530 float *proj_axis =
data->proj_axis;
537 float tmp_co[3], tmp_no[3];
541 weight = 1.0f - weight;
544 if (weight == 0.0f) {
608 if (proj_limit_squared != 0.0f) {
614 if (hit->
index != -1) {
645 float proj_axis[3] = {0.0f, 0.0f, 0.0f};
704 aux_tree = &aux_tree_stack;
711 .aux_tree = aux_tree,
712 .proj_axis = proj_axis,
713 .local2aux = &local2aux,
762 const float w[3] = {
x[0],
x[1], 1.0f -
x[0] -
x[1]};
792 if (
x[0] +
x[1] > 1.0f) {
793 x[0] =
x[0] / (
x[0] +
x[1]);
807 const float dir_epsilon = 0.5f;
808 bool fixed =
false, locked =
false;
813 float sum =
x[0] +
x[1];
814 float sstep = -(step[0] + step[1]);
816 if (
sum + sstep > 1.0f) {
817 float ldist = 1.0f -
sum;
821 float step_len =
len_v2(step);
824 if (step_len >
epsilon && sstep > step_len * dir_epsilon * (
float)
M_SQRT2) {
830 fixed = locked =
true;
840 for (
int i = 0; i < 2; i++) {
841 if (step[i] >
x[i]) {
844 float step_len =
len_v2(step);
847 if (step_len >
epsilon && (locked || step[i] > step_len * dir_epsilon)) {
873 const float vtri_no[3][3],
874 const float point_co[3],
875 const float hit_co[3],
881 float dist =
sqrtf(hit_dist_sq);
884 float epsilon = magnitude_estimate * 1.0e-6f;
892 x[2] = (
dot_v3v3(tmp, r_hit_no) < 0) ? -dist : dist;
898 .point_co = point_co,
908 #ifdef TRACE_TARGET_PROJECT
909 const bool trace =
true;
911 const bool trace =
false;
937 const float hit_co[3],
938 const float hit_no[3])
942 if (dist_sq < nearest->dist_sq) {
943 #ifdef TRACE_TARGET_PROJECT
945 "#=#=#> %d (%.3f,%.3f,%.3f) %g < %g\n", index,
UNPACK3(hit_co), dist_sq, nearest->
dist_sq);
947 nearest->
index = index;
966 const MEdge *edge = &
tree->mesh->medge[eidx];
967 const float *vedge_co[2] = {
data->vert[edge->
v1].co,
data->vert[edge->
v2].co};
969 #ifdef TRACE_TARGET_PROJECT
970 printf(
"EDGE %d (%.3f,%.3f,%.3f) (%.3f,%.3f,%.3f)\n",
977 const int *vert_boundary_id =
tree->boundary->vert_boundary_id;
978 int bid1 = vert_boundary_id[edge->
v1], bid2 = vert_boundary_id[edge->
v2];
980 if (bid1 < 0 || bid2 < 0) {
986 float vedge_dir[2][3], dir[3];
988 copy_v3_v3(vedge_dir[0], boundary_verts[bid1].normal_plane);
989 copy_v3_v3(vedge_dir[1], boundary_verts[bid2].normal_plane);
993 if (
dot_v3v3(boundary_verts[bid1].direction, dir) < 0) {
996 if (
dot_v3v3(boundary_verts[bid2].direction, dir) < 0) {
1001 float d0v0 =
dot_v3v3(vedge_dir[0], vedge_co[0]), d0v1 =
dot_v3v3(vedge_dir[0], vedge_co[1]);
1002 float d1v0 =
dot_v3v3(vedge_dir[1], vedge_co[0]), d1v1 =
dot_v3v3(vedge_dir[1], vedge_co[1]);
1003 float d0co =
dot_v3v3(vedge_dir[0], co);
1005 float a = d0v1 - d0v0 + d1v0 - d1v1;
1006 float b = 2 * d0v0 - d0v1 - d0co - d1v0 +
dot_v3v3(vedge_dir[1], co);
1007 float c = d0co - d0v0;
1008 float det = b * b - 4 *
a *
c;
1012 float sdet =
sqrtf(det);
1013 float hit_co[3], hit_no[3];
1015 for (
int i = (det > 0 ? 2 : 0); i >= 0; i -= 2) {
1016 float x = (-b + ((
float)i - 1) * sdet) / (2 *
a);
1021 float vedge_no[2][3];
1028 update_hit(nearest, index, co, hit_co, hit_no);
1043 const MLoop *loop[3] = {
1045 const MVert *vtri[3] = {
1046 &
data->vert[loop[0]->
v], &
data->vert[loop[1]->
v], &
data->vert[loop[2]->
v]};
1047 const float *vtri_co[3] = {vtri[0]->
co, vtri[1]->
co, vtri[2]->
co};
1048 float raw_hit_co[3], hit_co[3], hit_no[3], dist_sq, vtri_no[3][3];
1054 #ifdef TRACE_TARGET_PROJECT
1055 printf(
"TRIANGLE %d (%.3f,%.3f,%.3f) (%.3f,%.3f,%.3f) (%.3f,%.3f,%.3f) %g %g\n",
1064 if (dist_sq >= nearest->
dist_sq) {
1075 update_hit(nearest, index, co, hit_co, hit_no);
1079 const BLI_bitmap *is_boundary =
tree->boundary->edge_is_boundary;
1084 for (
int i = 0; i < 3; i++) {
1103 #ifdef TRACE_TARGET_PROJECT
1104 printf(
"\n====== TARGET PROJECT START ======\n");
1110 #ifdef TRACE_TARGET_PROJECT
1111 printf(
"====== TARGET PROJECT END: %d %g ======\n\n", nearest->
index, nearest->
dist_sq);
1114 if (nearest->
index < 0) {
1144 weight = 1.0f - weight;
1147 if (weight == 0.0f) {
1165 if (nearest->
index != -1) {
1168 nearest->
index = -1;
1182 if (nearest->
index != -1) {
1209 const float hit_co[3],
1210 const float hit_no[3],
1223 float w[3], no[3][3], tmp_co[3];
1269 const float point_co[3],
1270 const float hit_co[3],
1271 const float hit_no[3],
1279 float dist =
len_v3(delta);
1282 if (dist < FLT_EPSILON) {
1283 if (forcesnap || goal_dist > 0) {
1284 madd_v3_v3v3fl(r_point_co, hit_co, hit_no, goal_dist * forcesign);
1294 if (forcesign == 0.0f) {
1299 if (forcesnap || dsign * dist * forcesign < goal_dist) {
1305 if (dist < dist_epsilon) {
1306 #ifdef TRACE_TARGET_PROJECT
1307 printf(
"zero_factor %g = %g / %g\n", dist / dist_epsilon, dist, dist_epsilon);
1313 madd_v3_v3v3fl(r_point_co, hit_co, delta, goal_dist * forcesign);
1332 const float hit_co[3],
1333 const float hit_no[3],
1335 const float point_co[3],
1336 float r_point_co[3])
1343 if (goal_dist != 0) {
1360 if (goal_dist != 0) {
1370 if (goal_dist != 0) {
1380 printf(
"Unknown Shrinkwrap surface snap mode: %d\n", mode);
1414 const int defgrp_index,
1415 float (*vertexCos)[3],
1436 calc.
vgroup = defgrp_index;
1554 const float projLimitTolerance = 5.0f;
typedef float(TangentPoint)[2]
BVHTree * BKE_bvhtree_from_mesh_get(struct BVHTreeFromMesh *data, struct Mesh *mesh, const BVHCacheType bvh_cache_type, const int tree_type)
void free_bvhtree_from_mesh(struct BVHTreeFromMesh *data)
struct DerivedMesh * CDDM_from_mesh(struct Mesh *mesh)
struct Scene * CTX_data_scene(const bContext *C)
struct Depsgraph * CTX_data_depsgraph_pointer(const bContext *C)
void * CustomData_get_layer(const struct CustomData *data, int type)
#define BKE_MESH_OMP_LIMIT
void BKE_mesh_vert_coords_apply(struct Mesh *mesh, const float(*vert_coords)[3])
void BKE_mesh_looptri_get_real_edges(const struct Mesh *mesh, const struct MLoopTri *looptri, int r_edges[3])
float(* BKE_mesh_vert_coords_alloc(const struct Mesh *mesh, int *r_vert_len))[3]
const struct MLoopTri * BKE_mesh_runtime_looptri_ensure(struct Mesh *mesh)
int BKE_mesh_runtime_looptri_len(const struct Mesh *mesh)
void BKE_mesh_wrapper_ensure_mdata(struct Mesh *me)
struct Mesh * BKE_modifier_get_evaluated_mesh_from_evaluated_object(struct Object *ob_eval, const bool get_cage_mesh)
#define NULL_BVHTreeNearest
#define NULL_ShrinkwrapCalcData
struct DerivedMesh * subsurf_make_derived_from_derived(struct DerivedMesh *dm, struct SubsurfModifierData *smd, const struct Scene *scene, float(*vertCos)[3], SubsurfFlags flags)
#define BLI_BITMAP_TEST(_bitmap, _index)
#define BLI_BITMAP_ENABLE(_bitmap, _index)
#define BLI_BITMAP_NEW(_tot, _alloc_string)
@ BVH_NEAREST_OPTIMAL_ORDER
#define BVH_RAYCAST_DIST_MAX
int BLI_bvhtree_find_nearest_ex(BVHTree *tree, const float co[3], BVHTreeNearest *nearest, BVHTree_NearestPointCallback callback, void *userdata, int flag)
int BLI_bvhtree_ray_cast(BVHTree *tree, const float co[3], const float dir[3], float radius, BVHTreeRayHit *hit, BVHTree_RayCastCallback callback, void *userdata)
int BLI_bvhtree_find_nearest(BVHTree *tree, const float co[3], BVHTreeNearest *nearest, BVHTree_NearestPointCallback callback, void *userdata)
MINLINE float signf(float f)
void interp_weights_tri_v3(float w[3], const float v1[3], const float v2[3], const float v3[3], const float co[3])
void closest_on_tri_to_point_v3(float r[3], const float p[3], const float v1[3], const float v2[3], const float v3[3])
void BLI_space_transform_apply_normal(const struct SpaceTransform *data, float no[3])
void BLI_space_transform_apply(const struct SpaceTransform *data, float co[3])
float mat4_to_scale(const float M[4][4])
void BLI_space_transform_invert_normal(const struct SpaceTransform *data, float no[3])
void BLI_space_transform_invert(const struct SpaceTransform *data, float co[3])
#define BLI_SPACE_TRANSFORM_SETUP(data, local, target)
bool BLI_newton3d_solve(Newton3D_DeltaFunc func_delta, Newton3D_JacobianFunc func_jacobian, Newton3D_CorrectionFunc func_correction, void *userdata, float epsilon, int max_iterations, bool trace, const float x_init[3], float result[3])
Solve a generic f(x) = 0 equation using Newton's method.
void interp_v3_v3v3(float r[3], const float a[3], const float b[3], const float t)
MINLINE float len_squared_v3(const float v[3]) ATTR_WARN_UNUSED_RESULT
MINLINE float len_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
MINLINE void madd_v3_v3fl(float r[3], const float a[3], float f)
MINLINE float normalize_v3(float r[3])
MINLINE void sub_v3_v3(float r[3], const float a[3])
void interp_v3_v3v3v3(float p[3], const float v1[3], const float v2[3], const float v3[3], const float w[3])
MINLINE float len_squared_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
MINLINE void normal_short_to_float_v3(float r[3], const short n[3])
MINLINE void mul_v3_fl(float r[3], float f)
MINLINE void copy_v3_v3(float r[3], const float a[3])
MINLINE void negate_v3_v3(float r[3], const float a[3])
MINLINE float dot_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
MINLINE void cross_v3_v3v3(float r[3], const float a[3], const float b[3])
MINLINE void add_v2_fl(float r[2], float f)
MINLINE void negate_v3(float r[3])
MINLINE float normalize_v3_v3(float r[3], const float a[3])
MINLINE float len_manhattan_v3(const float v[3]) ATTR_WARN_UNUSED_RESULT
MINLINE void madd_v3_v3v3fl(float r[3], const float a[3], const float b[3], float f)
MINLINE float len_v2(const float a[2]) ATTR_WARN_UNUSED_RESULT
MINLINE void add_v3_v3(float r[3], const float a[3])
MINLINE float len_v3(const float a[3]) ATTR_WARN_UNUSED_RESULT
Strict compiler flags for areas of code we want to ensure don't do conversions without us knowing abo...
void BLI_task_parallel_range(const int start, const int stop, void *userdata, TaskParallelRangeFunc func, const TaskParallelSettings *settings)
BLI_INLINE void BLI_parallel_range_settings_defaults(TaskParallelSettings *settings)
struct Depsgraph Depsgraph
struct Object * DEG_get_evaluated_object(const struct Depsgraph *depsgraph, struct Object *object)
@ MOD_SHRINKWRAP_ON_SURFACE
@ MOD_SHRINKWRAP_ABOVE_SURFACE
@ MOD_SHRINKWRAP_OUTSIDE_SURFACE
#define MOD_SHRINKWRAP_CULL_TARGET_MASK
@ MOD_SHRINKWRAP_PROJECT_OVER_X_AXIS
@ MOD_SHRINKWRAP_PROJECT_OVER_Y_AXIS
@ MOD_SHRINKWRAP_PROJECT_OVER_Z_AXIS
@ MOD_SHRINKWRAP_PROJECT_OVER_NORMAL
@ MOD_SHRINKWRAP_TARGET_PROJECT
@ MOD_SHRINKWRAP_NEAREST_VERTEX
@ MOD_SHRINKWRAP_NEAREST_SURFACE
@ MOD_SHRINKWRAP_PROJECT_ALLOW_POS_DIR
@ MOD_SHRINKWRAP_CULL_TARGET_FRONTFACE
@ MOD_SHRINKWRAP_PROJECT_ALLOW_NEG_DIR
@ MOD_SHRINKWRAP_CULL_TARGET_BACKFACE
@ MOD_SHRINKWRAP_INVERT_VGROUP
@ MOD_SHRINKWRAP_INVERT_CULL_TARGET
Object is a sort of wrapper for general info.
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum type
Read Guarded memory(de)allocation.
Group RGB to Bright Vector Camera CLAMP
Utility defines for timing/benchmarks.
SIMD_FORCE_INLINE btVector3 transform(const btVector3 &point) const
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
static T sum(const btAlignedObjectArray< T > &items)
CCL_NAMESPACE_BEGIN struct Options options
const Depsgraph * depsgraph
void(* MEM_freeN)(void *vmemh)
void *(* MEM_calloc_arrayN)(size_t len, size_t size, const char *str)
void *(* MEM_callocN)(size_t len, const char *str)
struct ShrinkwrapCalcData ShrinkwrapCalcData
static void target_project_edge(const ShrinkwrapTreeData *tree, int index, const float co[3], BVHTreeNearest *nearest, int eidx)
static void target_project_tri_clamp(float x[3])
static bool target_project_solve_point_tri(const float *vtri_co[3], const float vtri_no[3][3], const float point_co[3], const float hit_co[3], float hit_dist_sq, float r_hit_co[3], float r_hit_no[3])
void BKE_shrinkwrap_free_tree(ShrinkwrapTreeData *data)
static void shrinkwrap_calc_nearest_vertex(ShrinkwrapCalcData *calc)
static ShrinkwrapBoundaryData * shrinkwrap_build_boundary_data(struct Mesh *mesh)
struct ShrinkwrapCalcCBData ShrinkwrapCalcCBData
static void mesh_looptri_target_project(void *userdata, int index, const float co[3], BVHTreeNearest *nearest)
void BKE_shrinkwrap_find_nearest_surface(struct ShrinkwrapTreeData *tree, BVHTreeNearest *nearest, float co[3], int type)
static void shrinkwrap_calc_nearest_surface_point(ShrinkwrapCalcData *calc)
static bool update_hit(BVHTreeNearest *nearest, int index, const float co[3], const float hit_co[3], const float hit_no[3])
static void shrinkwrap_snap_with_side(float r_point_co[3], const float point_co[3], const float hit_co[3], const float hit_no[3], float goal_dist, float forcesign, bool forcesnap)
bool BKE_shrinkwrap_project_normal(char options, const float vert[3], const float dir[3], const float ray_radius, const SpaceTransform *transf, ShrinkwrapTreeData *tree, BVHTreeRayHit *hit)
static void target_project_tri_deviation(void *userdata, const float x[3], float r_delta[3])
static void merge_vert_dir(ShrinkwrapBoundaryVertData *vdata, signed char *status, int index, const float edge_dir[3], signed char side)
static void shrinkwrap_calc_nearest_vertex_cb_ex(void *__restrict userdata, const int i, const TaskParallelTLS *__restrict tls)
struct TargetProjectTriData TargetProjectTriData
void BKE_shrinkwrap_mesh_nearest_surface_deform(struct bContext *C, Object *ob_source, Object *ob_target)
static void shrinkwrap_calc_normal_projection_cb_ex(void *__restrict userdata, const int i, const TaskParallelTLS *__restrict tls)
void BKE_shrinkwrap_remesh_target_project(Mesh *src_me, Mesh *target_me, Object *ob_target)
void BKE_shrinkwrap_compute_smooth_normal(const struct ShrinkwrapTreeData *tree, const struct SpaceTransform *transform, int looptri_idx, const float hit_co[3], const float hit_no[3], float r_no[3])
#define TIMEIT_BENCH(expr, id)
bool BKE_shrinkwrap_needs_normals(int shrinkType, int shrinkMode)
void BKE_shrinkwrap_compute_boundary_data(struct Mesh *mesh)
static void target_project_tri_jacobian(void *userdata, const float x[3], float r_jacobian[3][3])
bool BKE_shrinkwrap_init_tree(ShrinkwrapTreeData *data, Mesh *mesh, int shrinkType, int shrinkMode, bool force_normals)
void BKE_shrinkwrap_discard_boundary_data(struct Mesh *mesh)
void shrinkwrapModifier_deform(ShrinkwrapModifierData *smd, const ModifierEvalContext *ctx, struct Scene *scene, Object *ob, Mesh *mesh, MDeformVert *dvert, const int defgrp_index, float(*vertexCos)[3], int numVerts)
static bool target_project_tri_correct(void *UNUSED(userdata), const float x[3], float step[3], float x_next[3])
static void shrinkwrap_calc_nearest_surface_point_cb_ex(void *__restrict userdata, const int i, const TaskParallelTLS *__restrict tls)
static void shrinkwrap_calc_normal_projection(ShrinkwrapCalcData *calc)
void BKE_shrinkwrap_snap_point_to_surface(const struct ShrinkwrapTreeData *tree, const struct SpaceTransform *transform, int mode, int hit_idx, const float hit_co[3], const float hit_no[3], float goal_dist, const float point_co[3], float r_point_co[3])
const struct MLoop * loop
const struct MVert * vert
BVHTree_NearestPointCallback nearest_callback
const struct MLoopTri * looptri
void *(* getVertDataArray)(DerivedMesh *dm, int type)
int(* getNumVerts)(DerivedMesh *dm)
void(* release)(DerivedMesh *dm)
struct ShrinkwrapBoundaryData * shrinkwrap_data
struct CustomData pdata ldata
struct Depsgraph * depsgraph
const int * vert_boundary_id
const ShrinkwrapBoundaryVertData * boundary_verts
const BLI_bitmap * looptri_has_boundary
const BLI_bitmap * edge_is_boundary
unsigned int num_boundary_verts
SpaceTransform * local2aux
ShrinkwrapTreeData * tree
ShrinkwrapCalcData * calc
ShrinkwrapTreeData * aux_tree
struct SpaceTransform local2target
struct ShrinkwrapTreeData * tree
struct Object * aux_target
ShrinkwrapModifierData * smd
struct MDeformVert * dvert
struct Object * auxTarget
const float(* vtri_no)[3]
size_t userdata_chunk_size