1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
| package de.L19.math2;
public class Ray {
public Vector4D Origin;
public Vector4D Direction;
public Ray(){ this.Origin = new Vector4D(0,0,0,1); this.Direction = new Vector4D(0,0,1,1); }
public Vector4D getPoint(double t) { return this.Origin._doAdd(this.Direction._doMultiplyBy(t)); }
public double RayIntersectionPlane(Ray ray,Vector4D Position,Vector4D Normal){ return -((Normal.doDotProduct(ray.Origin) + (-Normal.doDotProduct(Position))) / Normal.doDotProduct(ray.Direction)); }
public Vector4D RayIntersectionPlane(Vector4D Position,Vector4D Normal){ return this.getPoint(this.RayIntersectionPlane(this,Position, Normal)); }
public double RayIntersectionPlaneInAABB(Vector4D Position,Vector4D Normal,double ax,double ay,double az,double bx,double by,double bz,boolean invertPlane){ if(invertPlane) Normal = Normal._doNegate(); final double d = this.RayIntersectionPlane(this, Position, Normal) + 0.0001D; final Vector4D v = this.getPoint(d); if(v.x < ax)return Double.POSITIVE_INFINITY; if(v.y < ay)return Double.POSITIVE_INFINITY; if(v.z < az)return Double.POSITIVE_INFINITY; if(v.x > bx)return Double.POSITIVE_INFINITY; if(v.y > by)return Double.POSITIVE_INFINITY; if(v.z > bz)return Double.POSITIVE_INFINITY; return d; }
public double RayIntersectionSphere(final Ray ray,final Vector4D position,final double radius){ final Vector4D s = position._doSubtract(ray.Origin);
final double A = ray.Direction.doDotProduct(ray.Direction); final double B = -2.0F * s.doDotProduct(ray.Direction); final double C = s.doDotProduct(s) - (radius*radius); final double D = (B*B) - (4F*A*C);
if (D > 0.0){ final double sign = (C < -0.00001) ? 1 : -1; final double lDist = (-B + (sign * Math.sqrt(D))) / (2 * A);
return lDist; } else return Float.POSITIVE_INFINITY; }
public double RayIntersectionTriangle(Ray ray,Vector4D point1,Vector4D point2,Vector4D point3){ Vector4D edge1 = point2._doSubtract(point1); Vector4D edge2 = point3._doSubtract(point1);
Vector4D s1 = ray.Direction._getCrossProduct(edge2);
final double divisor = s1.doDotProduct(edge1); if (divisor == 0.0) return Double.POSITIVE_INFINITY;
final double invDivisor = 1 / divisor;
final Vector4D distance = ray.Origin._doSubtract(point1); final double barycCoord_1 = distance.doDotProduct(s1) * invDivisor;
if ((barycCoord_1 < 0.0) || (barycCoord_1 > 1.0)) return Double.POSITIVE_INFINITY;
final Vector4D s2 = distance._getCrossProduct(edge1); final double barycCoord_2 = ray.Direction.doDotProduct(s2) * invDivisor;
if ((barycCoord_2 < 0.0) || ((barycCoord_1 + barycCoord_2) > 1.0)) return Double.POSITIVE_INFINITY;
final double intersectionDistance = edge2.doDotProduct(s2) * invDivisor;
return intersectionDistance >= 0 ? intersectionDistance : Double.POSITIVE_INFINITY; }
public double RayIntersectionAABB(double ax,double ay,double az,double bx,double by,double bz){ final double plane_top = this.RayIntersectionPlaneInAABB(new Vector4D(0,by,0),new Vector4D(0,+1,0), ax, ay, az, bx, by, bz, false); final double plane_bottom= this.RayIntersectionPlaneInAABB(new Vector4D(0,ay,0),new Vector4D(0,-1,0), ax, ay, az, bx, by, bz, false);
final double plane_left = this.RayIntersectionPlaneInAABB(new Vector4D(ax,0,0),new Vector4D(-1,0,0), ax, ay, az, bx, by, bz, false); final double plane_right = this.RayIntersectionPlaneInAABB(new Vector4D(bx,0,0),new Vector4D(+1,0,0), ax, ay, az, bx, by, bz, false);
final double plane_front = this.RayIntersectionPlaneInAABB(new Vector4D(0,0,az),new Vector4D(0,0,+1), ax, ay, az, bx, by, bz, false); final double plane_back = this.RayIntersectionPlaneInAABB(new Vector4D(0,0,bz),new Vector4D(0,0,-1), ax, ay, az, bx, by, bz, false);
double t = Double.POSITIVE_INFINITY; t = Math.min(t, plane_top); t = Math.min(t, plane_bottom); t = Math.min(t, plane_left); t = Math.min(t, plane_right); t = Math.min(t, plane_front); t = Math.min(t, plane_back); return t; } }
|