math api edits - replace point-normal form for a plane with dist_to_plane_v3()

also correct python mathutils api, was missing vector checks.
This commit is contained in:
Campbell Barton
2013-08-23 14:37:22 +00:00
parent 01e22d1b9f
commit 09ff49755f
5 changed files with 40 additions and 25 deletions

View File

@@ -456,6 +456,7 @@ void BKE_camera_view_frame(Scene *scene, Camera *camera, float r_vec[4][3])
typedef struct CameraViewFrameData {
float plane_tx[4][4]; /* 4 planes (not 4x4 matrix)*/
float frame_tx[4][3];
float normal_tx[4][3];
float dist_vals[4];
@@ -468,7 +469,7 @@ static void camera_to_frame_view_cb(const float co[3], void *user_data)
unsigned int i;
for (i = 0; i < 4; i++) {
float nd = dist_to_plane_v3(co, data->frame_tx[i], data->normal_tx[i]);
float nd = dist_to_plane_v3(co, data->plane_tx[i]);
if (nd < data->dist_vals[i]) {
data->dist_vals[i] = nd;
}
@@ -514,8 +515,8 @@ int BKE_camera_view_frame_fit_to_scene(Scene *scene, struct View3D *v3d, Object
}
for (i = 0; i < 4; i++) {
normal_tri_v3(data_cb.normal_tx[i],
zero, data_cb.frame_tx[i], data_cb.frame_tx[(i + 1) % 4]);
normal_tri_v3(data_cb.normal_tx[i], zero, data_cb.frame_tx[i], data_cb.frame_tx[(i + 1) % 4]);
plane_from_point_normal_v3(data_cb.plane_tx[i], data_cb.frame_tx[i], data_cb.normal_tx[i]);
}
/* initialize callback data */

View File

@@ -81,8 +81,7 @@ float dist_squared_to_line_segment_v2(const float p[2], const float l1[2], const
float dist_to_line_segment_v2(const float p[2], const float l1[2], const float l2[2]);
void closest_to_line_segment_v2(float closest[2], const float p[2], const float l1[2], const float l2[2]);
float dist_to_plane_normalized_v3(const float p[3], const float plane_co[3], const float plane_no_unit[3]);
float dist_to_plane_v3(const float p[3], const float plane_co[3], const float plane_no[3]);
float dist_to_plane_v3(const float p[3], const float plane[4]);
float dist_squared_to_line_segment_v3(const float p[3], const float l1[3], const float l2[3]);
float dist_to_line_segment_v3(const float p[3], const float l1[3], const float l2[3]);
float dist_to_line_v3(const float p[3], const float l1[3], const float l2[3]);

View File

@@ -321,25 +321,17 @@ void closest_to_plane_v3(float close_r[3], const float plane[4], const float pt[
madd_v3_v3v3fl(close_r, pt, plane, -side / length);
}
/* signed distance from the point to the plane in 3D */
float dist_to_plane_normalized_v3(const float p[3], const float plane_co[3], const float plane_no_unit[3])
float dist_to_plane_v3(const float pt[3], const float plane[4])
{
float plane_co_other[3];
float close[3];
add_v3_v3v3(plane_co_other, plane_co, plane_no_unit);
/* same logic as closest_to_plane_v3() */
const float length = len_squared_v3(plane);
const float side = plane_point_side_v3(plane, pt);
madd_v3_v3v3fl(close, pt, plane, -side / length);
/* end same logic */
return line_point_factor_v3(p, plane_co, plane_co_other);
}
float dist_to_plane_v3(const float p[3], const float plane_co[3], const float plane_no[3])
{
float plane_no_unit[3];
float plane_co_other[3];
normalize_v3_v3(plane_no_unit, plane_no);
add_v3_v3v3(plane_co_other, plane_co, plane_no_unit);
return line_point_factor_v3(p, plane_co, plane_co_other);
return copysign(len_v3v3(pt, close), side);
}
/* distance v1 to line-piece l1-l2 in 3D */

View File

@@ -4486,7 +4486,6 @@ static int mesh_bisect_exec(bContext *C, wmOperator *op)
invert_m4_m4(imat, obedit->obmat);
mul_m4_v3(imat, plane_co);
mul_mat3_m4_v3(imat, plane_no);
normalize_v3(plane_no);
EDBM_op_init(em, &bmop, op,
"bisect_plane geom=%hvef plane_co=%v plane_no=%v dist=%f clear_inner=%b clear_outer=%b",
@@ -4553,9 +4552,11 @@ void MESH_OT_bisect(struct wmOperatorType *ot)
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
prop = RNA_def_float_vector(ot->srna, "plane_co", 3, NULL, -FLT_MAX, FLT_MAX, "Point", "", -FLT_MAX, FLT_MAX);
prop = RNA_def_float_vector(ot->srna, "plane_co", 3, NULL, -FLT_MAX, FLT_MAX,
"Plane Point", "A point on the plane", -FLT_MAX, FLT_MAX);
RNA_def_property_flag(prop, PROP_SKIP_SAVE);
prop = RNA_def_float_vector(ot->srna, "plane_no", 3, NULL, -FLT_MAX, FLT_MAX, "Direction", "", -FLT_MAX, FLT_MAX);
prop = RNA_def_float_vector(ot->srna, "plane_no", 3, NULL, -FLT_MAX, FLT_MAX,
"Plane Normal", "The direction the plane points", -FLT_MAX, FLT_MAX);
RNA_def_property_flag(prop, PROP_SKIP_SAVE);
RNA_def_boolean(ot->srna, "use_fill", false, "Fill", "Fill in the cut");

View File

@@ -981,6 +981,7 @@ PyDoc_STRVAR(M_Geometry_distance_point_to_plane_doc,
static PyObject *M_Geometry_distance_point_to_plane(PyObject *UNUSED(self), PyObject *args)
{
VectorObject *pt, *plene_co, *plane_no;
float plane[4];
if (!PyArg_ParseTuple(args, "O!O!O!:distance_point_to_plane",
&vector_Type, &pt,
@@ -990,6 +991,15 @@ static PyObject *M_Geometry_distance_point_to_plane(PyObject *UNUSED(self), PyOb
return NULL;
}
if (pt->size != 3 ||
plene_co->size != 3 ||
plane_no->size != 3)
{
PyErr_SetString(PyExc_ValueError,
"One of more of the vector arguments wasn't a 3D vector");
return NULL;
}
if (BaseMath_ReadCallback(pt) == -1 ||
BaseMath_ReadCallback(plene_co) == -1 ||
BaseMath_ReadCallback(plane_no) == -1)
@@ -997,7 +1007,8 @@ static PyObject *M_Geometry_distance_point_to_plane(PyObject *UNUSED(self), PyOb
return NULL;
}
return PyFloat_FromDouble(dist_to_plane_v3(pt->vec, plene_co->vec, plane_no->vec));
plane_from_point_normal_v3(plane, plene_co->vec, plane_no->vec);
return PyFloat_FromDouble(dist_to_plane_v3(pt->vec, plane));
}
PyDoc_STRVAR(M_Geometry_barycentric_transform_doc,
@@ -1054,6 +1065,17 @@ static PyObject *M_Geometry_barycentric_transform(PyObject *UNUSED(self), PyObje
return NULL;
}
if (BaseMath_ReadCallback(vec_pt) == -1 ||
BaseMath_ReadCallback(vec_t1_src) == -1 ||
BaseMath_ReadCallback(vec_t2_src) == -1 ||
BaseMath_ReadCallback(vec_t3_src) == -1 ||
BaseMath_ReadCallback(vec_t1_tar) == -1 ||
BaseMath_ReadCallback(vec_t2_tar) == -1 ||
BaseMath_ReadCallback(vec_t3_tar) == -1)
{
return NULL;
}
barycentric_transform(vec, vec_pt->vec,
vec_t1_tar->vec, vec_t2_tar->vec, vec_t3_tar->vec,
vec_t1_src->vec, vec_t2_src->vec, vec_t3_src->vec);