added math function isect_line_plane_v3(), use for window_to_3d rather then having it inline.
This commit is contained in:
@@ -96,6 +96,18 @@ int isect_line_line_strict_v3(const float v1[3], const float v2[3],
|
||||
int isect_ray_plane_v3(float p1[3], float d[3], float v0[3],
|
||||
float v1[3], float v2[3], float *lambda, int clip);
|
||||
|
||||
/**
|
||||
* Intersect line/plane, optionally treat line as directional (like a ray) with the no_flip argument.
|
||||
* @param out The intersection point.
|
||||
* @param l1 The first point of the line.
|
||||
* @param l2 The second point of the line.
|
||||
* @param plane_co A point on the plane to intersect with.
|
||||
* @param plane_no The direction of the plane (does not need to be normalized).
|
||||
* @param no_flip When true, the intersection point will always be from l1 to l2, even if this is not on the plane.
|
||||
*/
|
||||
int isect_line_plane_v3(float out[3], const float l1[3], const float l2[3],
|
||||
const float plane_co[3], const float plane_no[3], const short no_flip);
|
||||
|
||||
/* line/ray triangle */
|
||||
int isect_line_tri_v3(const float p1[3], const float p2[3],
|
||||
const float v0[3], const float v1[3], const float v2[3], float *lambda, float uv[2]);
|
||||
|
@@ -37,7 +37,7 @@
|
||||
#include "BLI_memarena.h"
|
||||
#include "BLI_utildefines.h"
|
||||
|
||||
|
||||
static float lambda_cp_line(const float p[3], const float l1[3], const float l2[3]);
|
||||
|
||||
/********************************** Polygons *********************************/
|
||||
|
||||
@@ -640,6 +640,48 @@ int isect_ray_tri_threshold_v3(const float p1[3], const float d[3], const float
|
||||
return 1;
|
||||
}
|
||||
|
||||
int isect_line_plane_v3(float out[3], const float l1[3], const float l2[3], const float plane_co[3], const float plane_no[3], const short no_flip)
|
||||
{
|
||||
float l_vec[3]; /* l1 -> l2 normalized vector */
|
||||
float p_no[3]; /* 'plane_no' normalized */
|
||||
float dot;
|
||||
|
||||
sub_v3_v3v3(l_vec, l2, l1);
|
||||
|
||||
normalize_v3(l_vec);
|
||||
normalize_v3_v3(p_no, plane_no);
|
||||
|
||||
dot= dot_v3v3(l_vec, p_no);
|
||||
if(dot == 0.0f) {
|
||||
return 0;
|
||||
}
|
||||
else {
|
||||
float l1_plane[3]; /* line point aligned with the plane */
|
||||
float dist; /* 'plane_no' aligned distance to the 'plane_co' */
|
||||
|
||||
/* for pradictable flipping since the plane is only used to
|
||||
* define a direction, ignore its flipping and aligned with 'l_vec' */
|
||||
if(dot < 0.0f) {
|
||||
dot= -dot;
|
||||
negate_v3(p_no);
|
||||
}
|
||||
|
||||
add_v3_v3v3(l1_plane, l1, p_no);
|
||||
|
||||
dist = lambda_cp_line(plane_co, l1, l1_plane);
|
||||
|
||||
/* treat line like a ray, when 'no_flip' is set */
|
||||
if(no_flip && dist < 0.0f) {
|
||||
dist= -dist;
|
||||
}
|
||||
|
||||
mul_v3_fl(l_vec, dist / dot);
|
||||
|
||||
add_v3_v3v3(out, l1, l_vec);
|
||||
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
/* Adapted from the paper by Kasper Fauerby */
|
||||
/* "Improved Collision detection and Response" */
|
||||
@@ -1075,16 +1117,14 @@ float closest_to_line_v2(float cp[2],const float p[2], const float l1[2], const
|
||||
return lambda;
|
||||
}
|
||||
|
||||
#if 0
|
||||
/* little sister we only need to know lambda */
|
||||
static float lambda_cp_line(float p[3], float l1[3], float l2[3])
|
||||
static float lambda_cp_line(const float p[3], const float l1[3], const float l2[3])
|
||||
{
|
||||
float h[3],u[3];
|
||||
sub_v3_v3v3(u, l2, l1);
|
||||
sub_v3_v3v3(h, p, l1);
|
||||
return(dot_v3v3(u,h)/dot_v3v3(u,u));
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Similar to LineIntersectsTriangleUV, except it operates on a quad and in 2d, assumes point is in quad */
|
||||
void isect_point_quad_uv_v2(const float v0[2], const float v1[2], const float v2[2], const float v3[2], const float pt[2], float *uv)
|
||||
|
@@ -607,17 +607,14 @@ void window_to_3d(ARegion *ar, float out[3], const float depth_pt[3], const int
|
||||
|
||||
if(rv3d->is_persp) {
|
||||
float mousevec[3];
|
||||
float view_z[3];
|
||||
float pt_mid[3];
|
||||
|
||||
window_to_3d_vector(ar, mousevec, mx, my);
|
||||
|
||||
copy_v3_v3(line_sta, rv3d->viewinv[3]);
|
||||
normalize_v3_v3(view_z, rv3d->viewinv[2]);
|
||||
add_v3_v3v3(line_end, line_sta, view_z);
|
||||
closest_to_line_v3(pt_mid, depth_pt, line_sta, line_end);
|
||||
mul_v3_fl(mousevec, shell_angle_to_dist(angle_normalized_v3v3(view_z, mousevec)) * len_v3v3(line_sta, pt_mid));
|
||||
add_v3_v3v3(out, line_sta, mousevec);
|
||||
window_to_3d_vector(ar, mousevec, mx, my);
|
||||
add_v3_v3v3(line_end, line_sta, mousevec);
|
||||
|
||||
if(isect_line_plane_v3(out, line_sta, line_end, depth_pt, rv3d->viewinv[2], TRUE) == 0) {
|
||||
/* highly unlikely to ever happen, mouse vec paralelle with view plane */
|
||||
zero_v3(out);
|
||||
}
|
||||
}
|
||||
else {
|
||||
const float dx= (2.0f * (float)mx / (float)ar->winx) - 1.0f;
|
||||
|
Reference in New Issue
Block a user