Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ###########################
- # x-, y-, and z-Functions #
- ###########################
- x_eval <- function(PFX,t){
- return(0.5*PFX$ax*t^2 + PFX$vx0*t + PFX$x0);
- }
- y_eval <- function(PFX,t){
- return(0.5*PFX$ay*t^2 + PFX$vy0*t + PFX$y0);
- }
- z_eval <- function(PFX,t){
- return(0.5*PFX$az*t^2 + PFX$vz0*t + PFX$z0);
- }
- ##############################
- # vx-, vy-, and vz-Functions #
- ##############################
- vx_eval <- function(PFX,t){
- return(PFX$ax*t + PFX$vx0);
- }
- vy_eval <- function(PFX,t){
- return(PFX$ay*t + PFX$vy0);
- }
- vz_eval <- function(PFX,t){
- return(PFX$az*t + PFX$vz0);
- }
- ######################
- # Distance to a Face #
- ######################
- min_face_x <- function(PFX,x_f,t_min,t_max){
- dist_t_min <- dist_face_x(PFX,x_f,t_min);
- dist_t_max <- dist_face_x(PFX,x_f,t_max);
- dist_sz_min <- min(dist_t_min,dist_t_max);
- disc <- PFX$vx0^2 - 2*PFX$ax*(PFX$x0-x_f);
- if (PFX$ax != 0){
- if(disc > 0){
- root_1 <- (-PFX$vx0 - sqrt(disc))/PFX$ax;
- if (root_1 > t_min && root_1 < t_max){
- dist_root_1 <- dist_face_x(PFX,x_f,root_1);
- if (dist_root_1 < dist_sz_min){
- dist_sz_min <- dist_root_1;
- }
- }
- root_2 <- (-PFX$vx0 + sqrt(disc))/PFX$ax;
- if (root_2 > t_min && root_2 < t_max){
- dist_root_2 <- dist_face_x(PFX,x_f,root_2);
- if (dist_root_2 < dist_sz_min){
- dist_sz_min <- dist_root_2;
- }
- }
- }
- root <- -PFX$vx0/PFX$ax;
- if (root > t_min && root < t_max){
- dist_root <- dist_face_x(PFX,x_f,root);
- if (dist_root < dist_sz_min){
- dist_sz_min <- dist_root;
- }
- }
- } else {
- root <- -(PFX$x0-x_f)/PFX$vx0;
- if (root > t_min && root < t_max){
- dist_root <- dist_face_x(PFX,x_f,root);
- if (dist_root < dist_sz_min){
- dist_sz_min <- dist_root;
- }
- }
- }
- return(dist_sz_min);
- }
- dist_face_x <- function(PFX,x_f,t_f){
- return(abs(x_eval(PFX,t_f) - x_f));
- }
- min_face_y <- function(PFX,y_f,t_min,t_max){
- dist_t_min <- dist_face_y(PFX,y_f,t_min);
- dist_t_max <- dist_face_y(PFX,y_f,t_max);
- dist_sz_min <- min(dist_t_min,dist_t_max);
- disc <- PFX$vy0^2 - 2*PFX$ay*(PFX$y0-y_f);
- if(PFX$ay != 0){
- if(disc > 0){
- root_1 <- (-PFX$vy0 - sqrt(disc))/PFX$ay;
- if (root_1 > t_min && root_1 < t_max){
- dist_root_1 <- dist_face_y(PFX,y_f,root_1);
- if (dist_root_1 < dist_sz_min){
- dist_sz_min <- dist_root_1;
- }
- }
- root_2 <- (-PFX$vy0 + sqrt(disc))/PFX$ay;
- if (root_2 > t_min && root_2 < t_max){
- dist_root_2 <- dist_face_y(PFX,y_f,root_2);
- if (dist_root_2 < dist_sz_min){
- dist_sz_min <- dist_root_2;
- }
- }
- }
- root <- -PFX$vy0/PFX$ay;
- if (root > t_min && root < t_max){
- dist_root <- dist_face_y(PFX,y_f,root);
- if (dist_root < dist_sz_min){
- dist_sz_min <- dist_root;
- }
- }
- } else {
- root <- -(PFX$y0-y_f)/PFX$vy0;
- if (root > t_min && root < t_max){
- dist_root <- dist_face_y(PFX,y_f,root);
- if (dist_root < dist_sz_min){
- dist_sz_min <- dist_root;
- }
- }
- }
- return(dist_sz_min);
- }
- dist_face_y <- function(PFX,y_f,t_f){
- return(abs(y_eval(PFX,t_f) - y_f));
- }
- min_face_z <- function(PFX,z_f,t_min,t_max){
- dist_t_min <- dist_face_z(PFX,z_f,t_min);
- dist_t_max <- dist_face_z(PFX,z_f,t_max);
- dist_sz_min <- min(dist_t_min,dist_t_max);
- disc <- PFX$vz0^2 - 2*PFX$az*(PFX$z0-z_f);
- if(PFX$az != 0){
- if(disc > 0){
- root_1 <- (-PFX$vz0 - sqrt(disc))/PFX$az;
- if (root_1 > t_min && root_1 < t_max){
- dist_root_1 <- dist_face_z(PFX,z_f,root_1);
- if (dist_root_1 < dist_sz_min){
- dist_sz_min <- dist_root_1;
- }
- }
- root_2 <- (-PFX$vz0 + sqrt(disc))/PFX$az;
- if (root_2 > t_min && root_2 < t_max){
- dist_root_2 <- dist_face_z(PFX,z_f,root_2);
- if (dist_root_2 < dist_sz_min){
- dist_sz_min <- dist_root_2;
- }
- }
- }
- root <- -PFX$vz0/PFX$az;
- if (root > t_min && root < t_max){
- dist_root <- dist_face_z(PFX,z_f,root);
- if (dist_root < dist_sz_min){
- dist_sz_min <- dist_root;
- }
- }
- } else {
- root <- -(PFX$z0-z_f)/PFX$vz0;
- if (root > t_min && root < t_max){
- dist_root <- dist_face_z(PFX,z_f,root);
- if (dist_root < dist_sz_min){
- dist_sz_min <- dist_root;
- }
- }
- }
- return(dist_sz_min);
- }
- dist_face_z <- function(PFX,z_f,t_f){
- return(abs(z_eval(PFX,t_f) - z_f));
- }
- ###############################
- # Distance to a Diagonal Face #
- ###############################
- min_diag_face_xy <- function(PFX,n_x,n_y,n_z,x_1,y_1,z_1,t_min,t_max){
- n_c <- -(n_x*x_1 + n_y*y_1 + n_z*z_1);
- p_2 <- 0.5*(n_x*PFX$ax + n_y*PFX$ay + n_z*PFX$az);
- p_1 <- n_x*PFX$vx0 + n_y*PFX$vy0 + n_z*PFX$vz0;
- p_0 <- n_x*PFX$x0 + n_y*PFX$y0 + n_z*PFX$z0 + n_c;
- q_1 <- n_x*PFX$ax + n_y*PFX$ay + n_z*PFX$az;
- q_0 <- n_x*PFX$vx0 + n_y*PFX$vy0 + n_z*PFX$vz0;
- c_3 <- p_2*q_1;
- c_2 <- p_2*q_0 + p_1*q_1;
- c_1 <- p_1*q_0 + p_0*q_1;
- c_0 <- p_0*q_0;
- return(cubic_zero_diag_face_xy(c_2/c_3,c_1/c_3,c_0/c_3,PFX,n_x,n_y,n_z,n_c,x_1,y_1,z_1,t_min,t_max));
- }
- dist_diag_face_xy <- function(PFX,n_x,n_y,n_z,n_c,t_f){
- return(abs(n_x*x_eval(PFX,t_f) + n_y*y_eval(PFX,t_f) + n_z*z_eval(PFX,t_f) + n_c)/sqrt(n_x^2 + n_y^2 + n_z^2));
- }
- #######################
- # Distance to an Edge #
- #######################
- min_edge_xz <- function(PFX,x_e,z_e,t_min,t_max){
- c_3 <- 0.5*(PFX$ax^2 + PFX$az^2);
- c_2 <- 1.5*(PFX$ax*PFX$vx0^2 + PFX$az*PFX$vz0^2);
- c_1 <- PFX$vx0^2 + PFX$vz0^2 + PFX$ax*(PFX$x0-x_e) + PFX$az*(PFX$z0-z_e);
- c_0 <- PFX$vx0*(PFX$x0-x_e) + PFX$vz0*(PFX$z0-z_e);
- return(cubic_zero_edge_xz(c_2/c_3,c_1/c_3,c_0/c_3,PFX,x_e,z_e,t_min,t_max));
- }
- dist_edge_xz <- function(PFX,x_e,z_e,t_e){
- return(sqrt((x_eval(PFX,t_e) - x_e)^2 + (z_eval(PFX,t_e) - z_e)^2));
- }
- min_edge_yz <- function(PFX,y_e,z_e,t_min,t_max){
- c_3 <- 0.5*(PFX$ay^2 + PFX$az^2);
- c_2 <- 1.5*(PFX$ay*PFX$vy0^2 + PFX$az*PFX$vz0^2);
- c_1 <- PFX$vy0^2 + PFX$vz0^2 + PFX$ay*(PFX$y0-y_e) + PFX$az*(PFX$z0-z_e);
- c_0 <- PFX$vy0*(PFX$y0-y_e) + PFX$vz0*(PFX$z0-z_e);
- return(cubic_zero_edge_yz(c_2/c_3,c_1/c_3,c_0/c_3,PFX,y_e,z_e,t_min,t_max));
- }
- dist_edge_yz <- function(PFX,y_e,z_e,t_e){
- return(sqrt((y_eval(PFX,t_e) - y_e)^2 + (z_eval(PFX,t_e) - z_e)^2));
- }
- min_edge_xy <- function(PFX,x_e,y_e,t_min,t_max){
- c_3 <- 0.5*(PFX$ax^2 + PFX$ay^2);
- c_2 <- 1.5*(PFX$ax*PFX$vx0^2 + PFX$ay*PFX$vy0^2);
- c_1 <- PFX$vx0^2 + PFX$vy0^2 + PFX$ax*(PFX$x0-x_e) + PFX$ay*(PFX$y0-y_e);
- c_0 <- PFX$vx0*(PFX$x0-x_e) + PFX$vy0*(PFX$y0-y_e);
- return(cubic_zero_edge_xy(c_2/c_3,c_1/c_3,c_0/c_3,PFX,x_e,y_e,t_min,t_max));
- }
- dist_edge_xy <- function(PFX,x_e,y_e,t_e){
- return(sqrt((x_eval(PFX,t_e) - x_e)^2 + (y_eval(PFX,t_e) - y_e)^2));
- }
- ###############################
- # Distance to a Diagonal Edge #
- ###############################
- min_diag_edge_xy <- function(PFX,x_1,y_1,z_1,x_2,y_2,t_min,t_max){
- p_2 <- -0.5*(PFX$ax*(x_2 - x_1) + PFX$ay*(y_2 - y_1));
- p_1 <- -PFX$vx0*(x_2 - x_1) - PFX$vy0*(y_2 - y_1);
- p_0 <- (x_1 - PFX$x0)*(x_2 - x_1) + (y_1 - PFX$y0)*(y_2 - y_1);
- q_1 <- PFX$ax*(x_2 - x_1) + PFX$ay*(y_2 - y_1);
- q_0 <- PFX$vx0*(x_2 - x_1) + PFX$vy0*(y_2 - y_1);
- c_3 <- 0.5*(PFX$ax^2 + PFX$ay^2 + PFX$az^2) + p_2*q_1;
- c_2 <- 1.5*(PFX$ax*PFX$vx0 + PFX$ay*PFX$vy0 + PFX$az*PFX$vz0) + p_2*q_0 + p_1*q_1;
- c_1 <- PFX$ax*(PFX$x0 - x_1) + PFX$vx0^2 + PFX$ay*(PFX$y0 - y_1) + PFX$vy0^2 + PFX$az*(PFX$z0 - z_1) + PFX$vz0^2 + p_1*q_0 + p_0*q_1;
- c_0 <- PFX$vx0*(PFX$x0 - x_1) + PFX$vy0*(PFX$y0 - y_1) + PFX$vz0*(PFX$z0 - z_1) + p_0*q_0;
- return(cubic_zero_diag_edge_xy(c_2/c_3,c_1/c_3,c_0/c_3,PFX,x_1,y_1,z_1,x_2,y_2,t_min,t_max));
- }
- dist_diag_edge_xy <- function(PFX,x_1,y_1,z_1,x_2,y_2,t_e){
- x2mx1 <- c(x_2 - x_1,y_2 - y_1,0);
- x1mx0 <- c(x_1 - x_eval(PFX,t_e),y_1 - y_eval(PFX,t_e),z_1 - z_eval(PFX,t_e));
- return(sqrt(((x1mx0%*%x1mx0)*(x2mx1%*%x2mx1) - (x1mx0%*%x2mx1)^2)/(x2mx1%*%x2mx1)));
- }
- ########################
- # Distance to a Corner #
- ########################
- min_corner <- function(PFX,x_c,y_c,z_c,t_min,t_max){
- c_3 <- 0.5*(PFX$ax^2 + PFX$ay^2 + PFX$az^2);
- c_2 <- 1.5*(PFX$ax*PFX$vx0 + PFX$ay*PFX$vy0 + PFX$az*PFX$vz0);
- c_1 <- PFX$vx0^2 + PFX$vy0^2 + PFX$vz0^2 + PFX$ax*(PFX$x0-x_c) + PFX$ay*(PFX$y0-y_c) + PFX$az*(PFX$z0-z_c);
- c_0 <- PFX$vx0*(PFX$x0-x_c) + PFX$vy0*(PFX$y0-y_c) + PFX$vz0*(PFX$z0-z_c);
- return(cubic_zero_corner(c_2/c_3,c_1/c_3,c_0/c_3,PFX,x_c,y_c,z_c,t_min,t_max));
- }
- dist_corner <- function(PFX,x_c,y_c,z_c,t_c){
- return(sqrt((x_eval(PFX,t_c) - x_c)^2 + (y_eval(PFX,t_c) - y_c)^2 + (z_eval(PFX,t_c) - z_c)^2));
- }
- ########################
- # Check the Pitch Zone #
- ########################
- zone_check <- function(PFX,sz_left_x,sz_right_x,sz_top_z,sz_bottom_z){
- # Check in the z-direction
- if (PFX$z0 > sz_top_z){
- zone_start <- 0;
- } else if (PFX$z0 == sz_top_z) {
- if (PFX$vz0 > 0){
- zone_start <- 0;
- } else {
- zone_start <- 3;
- }
- } else if (PFX$z0 > sz_bottom_z){
- zone_start <- 3;
- } else if (PFX$z0 == sz_bottom_z){
- if (PFX$vz0 > 0){
- zone_start <- 3;
- } else {
- zone_start <- 6;
- }
- } else {
- zone_start <- 6;
- }
- # Check in the x-direction
- if (PFX$x0 < sz_left_x){
- zone_start <- zone_start + 1;
- } else if (PFX$x0 == sz_left_x) {
- if (PFX$vx0 < 0){
- zone_start <- zone_start + 1;
- } else {
- zone_start <- zone_start + 2;
- }
- } else if (PFX$x0 < sz_right_x){
- zone_start <- zone_start + 2;
- } else if (PFX$x0 == sz_right_x){
- if (PFX$vx0 < 0){
- zone_start <- zone_start + 2;
- } else {
- zone_start <- zone_start + 3;
- }
- } else {
- zone_start <- zone_start + 3;
- }
- return(zone_start);
- }
- zone_check_2D <- function(px,pz,vx,vz,sz_left_x,sz_right_x,sz_top_z,sz_bottom_z){
- # Check in the z-direction
- if (pz > sz_top_z){
- zone_start <- 0;
- } else if (pz == sz_top_z) {
- if (vz > 0){
- zone_start <- 0;
- } else {
- zone_start <- 3;
- }
- } else if (pz > sz_bottom_z){
- zone_start <- 3;
- } else if (pz == sz_bottom_z){
- if (vz > 0){
- zone_start <- 3;
- } else {
- zone_start <- 6;
- }
- } else {
- zone_start <- 6;
- }
- # Check in the x-direction
- if (px < sz_left_x){
- zone_start <- zone_start + 1;
- } else if (px == sz_left_x) {
- if (vx < 0){
- zone_start <- zone_start + 1;
- } else {
- zone_start <- zone_start + 2;
- }
- } else if (px < sz_right_x){
- zone_start <- zone_start + 2;
- } else if (px == sz_right_x){
- if (vx < 0){
- zone_start <- zone_start + 2;
- } else {
- zone_start <- zone_start + 3;
- }
- } else {
- zone_start <- zone_start + 3;
- }
- return(zone_start);
- }
- ##############################
- # Solution to Cubic Equation #
- ##############################
- cubic_zero_corner <- function(a,b,c,PFX,x_c,y_c,z_c,t_min,t_max){
- dist_t_min <- dist_corner(PFX,x_c,y_c,z_c,t_min);
- dist_t_max <- dist_corner(PFX,x_c,y_c,z_c,t_max);
- dist_sz_min <- min(dist_t_min,dist_t_max);
- Q <- (a^2 - 3*b)/9;
- R <- (2*a^3 - 9*a*b + 27*c)/54;
- M <- R^2 - Q^3;
- if (M > 0){
- S <- -R + sqrt(M);
- S <- sign(S)*abs(S)^(1/3);
- T <- -R - sqrt(M);
- T <- sign(T)*abs(T)^(1/3);
- root <- -(a/3) + S + T;
- if (root < t_max && root > t_min){
- dist_root <- dist_corner(PFX,x_c,y_c,z_c,root);
- if (dist_root < dist_sz_min){
- dist_sz_min <- dist_root;
- }
- }
- } else if (M < 0) {
- theta <- acos(sign(Q)*R/abs(Q)^(3/2));
- root_1 <- -2*sqrt(Q)*cos(theta/3) - (a/3);
- if (root_1 < t_max && root_1 > t_min){
- dist_root_1 <- dist_corner(PFX,x_c,y_c,z_c,root_1);
- if (dist_root_1 < dist_sz_min){
- dist_sz_min <- dist_root_1;
- }
- }
- root_2 <- -2*sqrt(Q)*cos((theta+2*pi)/3) - (a/3);
- if (root_2 < t_max && root_2 > t_min){
- dist_root_2 <- dist_corner(PFX,x_c,y_c,z_c,root_2);
- if (dist_root_2 < dist_sz_min){
- dist_sz_min <- dist_root_2;
- }
- }
- root_3 <- -2*sqrt(Q)*cos((theta-2*pi)/3) - (a/3);
- if (root_3 < t_max && root_3 > t_min){
- dist_root_3 <- dist_corner(PFX,x_c,y_c,z_c,root_3);
- if (dist_root_3 < dist_sz_min){
- dist_sz_min <- dist_root_3;
- }
- }
- }
- return(dist_sz_min);
- }
- cubic_zero_edge_xz <- function(a,b,c,PFX,x_e,z_e,t_min,t_max){
- dist_t_min <- dist_edge_xz(PFX,x_e,z_e,t_min);
- dist_t_max <- dist_edge_xz(PFX,x_e,z_e,t_max);
- dist_sz_min <- min(dist_t_min,dist_t_max);
- Q <- (a^2 - 3*b)/9;
- R <- (2*a^3 - 9*a*b + 27*c)/54;
- M <- R^2 - Q^3;
- if (M > 0){
- S <- -R + sqrt(M);
- S <- sign(S)*abs(S)^(1/3);
- T <- -R - sqrt(M);
- T <- sign(T)*abs(T)^(1/3);
- root <- -(a/3) + S + T;
- if(root < t_max && root > t_min){
- dist_root <- dist_edge_xz(PFX,x_e,z_e,root);
- if(dist_root < dist_sz_min){
- dist_sz_min <- dist_root;
- }
- }
- } else if (M < 0) {
- theta <- acos(sign(Q)*R/abs(Q)^(3/2));
- root_1 <- -2*sqrt(Q)*cos(theta/3) - (a/3);
- if(root_1 < t_max && root_1 > t_min){
- dist_root_1 <- dist_edge_xz(PFX,x_e,z_e,root_1);
- if(dist_root_1 < dist_sz_min){
- dist_sz_min <- dist_root_1;
- }
- }
- root_2 <- -2*sqrt(Q)*cos((theta+2*pi)/3) - (a/3);
- if(root_2 < t_max && root_2 > t_min){
- dist_root_2 <- dist_edge_xz(PFX,x_e,z_e,root_2);
- if(dist_root_2 < dist_sz_min){
- dist_sz_min <- dist_root_2;
- }
- }
- root_3 <- -2*sqrt(Q)*cos((theta-2*pi)/3) - (a/3);
- if(root_3 < t_max && root_3 > t_min){
- dist_root_3 <- dist_edge_xz(PFX,x_e,z_e,root_3);
- if(dist_root_3 < dist_sz_min){
- dist_sz_min <- dist_root_3;
- }
- }
- }
- return(dist_sz_min);
- }
- cubic_zero_edge_yz <- function(a,b,c,PFX,y_e,z_e,t_min,t_max){
- dist_t_min <- dist_edge_yz(PFX,y_e,z_e,t_min);
- dist_t_max <- dist_edge_yz(PFX,y_e,z_e,t_max);
- dist_sz_min <- min(dist_t_min,dist_t_max);
- Q <- (a^2 - 3*b)/9;
- R <- (2*a^3 - 9*a*b + 27*c)/54;
- M <- R^2 - Q^3;
- if (M > 0){
- S <- -R + sqrt(M);
- S <- sign(S)*abs(S)^(1/3);
- T <- -R - sqrt(M);
- T <- sign(T)*abs(T)^(1/3);
- root <- -(a/3) + S + T;
- if(root < t_max && root > t_min){
- dist_root <- dist_edge_yz(PFX,y_e,z_e,root);
- if(dist_root < dist_sz_min){
- dist_sz_min <- dist_root;
- }
- }
- } else if (M < 0) {
- theta <- acos(sign(Q)*R/abs(Q)^(3/2));
- root_1 <- -2*sqrt(Q)*cos(theta/3) - (a/3);
- if(root_1 < t_max && root_1 > t_min){
- dist_root_1 <- dist_edge_yz(PFX,y_e,z_e,root_1);
- if(dist_root_1 < dist_sz_min){
- dist_sz_min <- dist_root_1;
- }
- }
- root_2 <- -2*sqrt(Q)*cos((theta+2*pi)/3) - (a/3);
- if(root_2 < t_max && root_2 > t_min){
- dist_root_2 <- dist_edge_yz(PFX,y_e,z_e,root_2);
- if(dist_root_2 < dist_sz_min){
- dist_sz_min <- dist_root_2;
- }
- }
- root_3 <- -2*sqrt(Q)*cos((theta-2*pi)/3) - (a/3);
- if(root_3 < t_max && root_3 > t_min){
- dist_root_3 <- dist_edge_yz(PFX,y_e,z_e,root_3);
- if(dist_root_3 < dist_sz_min){
- dist_sz_min <- dist_root_3;
- }
- }
- }
- return(dist_sz_min);
- }
- cubic_zero_edge_xy <- function(a,b,c,PFX,x_e,y_e,t_min,t_max){
- dist_t_min <- dist_edge_xy(PFX,x_e,y_e,t_min);
- dist_t_max <- dist_edge_xy(PFX,x_e,y_e,t_max);
- dist_sz_min <- min(dist_t_min,dist_t_max);
- Q <- (a^2 - 3*b)/9;
- R <- (2*a^3 - 9*a*b + 27*c)/54;
- M <- R^2 - Q^3;
- if (M > 0){
- S <- -R + sqrt(M);
- S <- sign(S)*abs(S)^(1/3);
- T <- -R - sqrt(M);
- T <- sign(T)*abs(T)^(1/3);
- root <- -(a/3) + S + T;
- if(root < t_max && root > t_min){
- dist_root <- dist_edge_xy(PFX,x_e,y_e,root);
- if(dist_root < dist_sz_min){
- dist_sz_min <- dist_root;
- }
- }
- } else if (M < 0) {
- theta <- acos(sign(Q)*R/abs(Q)^(3/2));
- root_1 <- -2*sqrt(Q)*cos(theta/3) - (a/3);
- if(root_1 < t_max && root_1 > t_min){
- dist_root_1 <- dist_edge_xy(PFX,x_e,y_e,root_1);
- if(dist_root_1 < dist_sz_min){
- dist_sz_min <- dist_root_1;
- }
- }
- root_2 <- -2*sqrt(Q)*cos((theta+2*pi)/3) - (a/3);
- if(root_2 < t_max && root_2 > t_min){
- dist_root_2 <- dist_edge_xy(PFX,x_e,y_e,root_2);
- if(dist_root_2 < dist_sz_min){
- dist_sz_min <- dist_root_2;
- }
- }
- root_3 <- -2*sqrt(Q)*cos((theta-2*pi)/3) - (a/3);
- if(root_3 < t_max && root_3 > t_min){
- dist_root_3 <- dist_edge_xy(PFX,x_e,y_e,root_3);
- if(dist_root_3 < dist_sz_min){
- dist_sz_min <- dist_root_3;
- }
- }
- }
- return(dist_sz_min);
- }
- cubic_zero_diag_edge_xy <- function(a,b,c,PFX,x_1,y_1,z_1,x_2,y_2,t_min,t_max){
- dist_t_min <- dist_diag_edge_xy(PFX,x_1,y_1,z_1,x_2,y_2,t_min);
- dist_t_max <- dist_diag_edge_xy(PFX,x_1,y_1,z_1,x_2,y_2,t_max);
- dist_sz_min <- min(dist_t_min,dist_t_max);
- Q <- (a^2 - 3*b)/9;
- R <- (2*a^3 - 9*a*b + 27*c)/54;
- M <- R^2 - Q^3;
- if (M > 0){
- S <- -R + sqrt(M);
- S <- sign(S)*abs(S)^(1/3);
- T <- -R - sqrt(M);
- T <- sign(T)*abs(T)^(1/3);
- root <- -(a/3) + S + T;
- if(root < t_max && root > t_min){
- dist_root <- dist_diag_edge_xy(PFX,x_1,y_1,z_1,x_2,y_2,root);
- if(dist_root < dist_sz_min){
- dist_sz_min <- dist_root;
- }
- }
- } else if (M < 0) {
- theta <- acos(sign(Q)*R/abs(Q)^(3/2));
- root_1 <- -2*sqrt(Q)*cos(theta/3) - (a/3);
- if(root_1 < t_max && root_1 > t_min){
- dist_root_1 <- dist_diag_edge_xy(PFX,x_1,y_1,z_1,x_2,y_2,root_1);
- if(dist_root_1 < dist_sz_min){
- dist_sz_min <- dist_root_1;
- }
- }
- root_2 <- -2*sqrt(Q)*cos((theta+2*pi)/3) - (a/3);
- if(root_2 < t_max && root_2 > t_min){
- dist_root_2 <- dist_diag_edge_xy(PFX,x_1,y_1,z_1,x_2,y_2,root_2);
- if(dist_root_2 < dist_sz_min){
- dist_sz_min <- dist_root_2;
- }
- }
- root_3 <- -2*sqrt(Q)*cos((theta-2*pi)/3) - (a/3);
- if(root_3 < t_max && root_3 > t_min){
- dist_root_3 <- dist_diag_edge_xy(PFX,x_1,y_1,z_1,x_2,y_2,root_3);
- if(dist_root_3 < dist_sz_min){
- dist_sz_min <- dist_root_3;
- }
- }
- }
- return(dist_sz_min);
- }
- cubic_zero_diag_face_xy <- function(a,b,c,PFX,n_x,n_y,n_z,n_c,x_1,y_1,z_1,t_min,t_max){
- dist_t_min <- dist_diag_face_xy(PFX,n_x,n_y,n_z,n_c,t_min);
- dist_t_max <- dist_diag_face_xy(PFX,n_x,n_y,n_z,n_c,t_max);
- dist_sz_min <- min(dist_t_min,dist_t_max);
- Q <- (a^2 - 3*b)/9;
- R <- (2*a^3 - 9*a*b + 27*c)/54;
- M <- R^2 - Q^3;
- if (M > 0){
- S <- -R + sqrt(M);
- S <- sign(S)*abs(S)^(1/3);
- T <- -R - sqrt(M);
- T <- sign(T)*abs(T)^(1/3);
- root <- -(a/3) + S + T;
- if(root < t_max && root > t_min){
- dist_root <- dist_diag_face_xy(PFX,n_x,n_y,n_z,n_c,root);
- if(dist_root < dist_sz_min){
- dist_sz_min <- dist_root;
- }
- }
- } else if (M < 0) {
- theta <- acos(sign(Q)*R/abs(Q)^(3/2));
- root_1 <- -2*sqrt(Q)*cos(theta/3) - (a/3);
- if(root_1 < t_max && root_1 > t_min){
- dist_root_1 <- dist_diag_face_xy(PFX,n_x,n_y,n_z,n_c,root_1);
- if(dist_root_1 < dist_sz_min){
- dist_sz_min <- dist_root_1;
- }
- }
- root_2 <- -2*sqrt(Q)*cos((theta+2*pi)/3) - (a/3);
- if(root_2 < t_max && root_2 > t_min){
- dist_root_2 <- dist_diag_face_xy(PFX,n_x,n_y,n_z,n_c,root_2);
- if(dist_root_2 < dist_sz_min){
- dist_sz_min <- dist_root_2;
- }
- }
- root_3 <- -2*sqrt(Q)*cos((theta-2*pi)/3) - (a/3);
- if(root_3 < t_max && root_3 > t_min){
- dist_root_3 <- dist_diag_face_xy(PFX,n_x,n_y,n_z,n_c,root_3);
- if(dist_root_3 < dist_sz_min){
- dist_sz_min <- dist_root_3;
- }
- }
- }
- return(dist_sz_min);
- }
- #############################
- # xy-Intersection of a Line #
- #############################
- xy_intersect <- function(PFX,x_f,x_b,y_f,y_b){
- M <- (y_f-y_b)/(x_f-x_b);
- B <- y_f - M*x_f;
- a <- 0.5*(PFX$ay - M*PFX$ax);
- b <- PFX$vy0 - M*PFX$vx0;
- c <- PFX$y0 - (M*PFX$x0 + B);
- return(list(a = a, b = b, c = c, M = M));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement