public static BoundsF operator -(Vector3F v, BoundsF b) { BoundsF result; Vector3F.Subtract(ref v, ref b.Minimum, out result.Minimum); Vector3F.Subtract(ref v, ref b.Maximum, out result.Maximum); return(result); }
public static BoundsF Subtract(ref BoundsF b, ref Vector3F v) { BoundsF result; Vector3F.Subtract(ref b.Minimum, ref v, out result.Minimum); Vector3F.Subtract(ref b.Maximum, ref v, out result.Maximum); return(result); }
public bool Intersects(ref TriangleF triangle) { Vector3F point; ClosestPtPointTriangle(ref Origin, ref triangle.A, ref triangle.B, ref triangle.C, out point); Vector3F v; Vector3F.Subtract(ref point, ref Origin, out v); return(v.LengthSquared() <= Radius * Radius); }
internal bool LineIntersection(ref Vector3F start, ref Vector3F end) { //unsafe { Vector3F center; GetCenter(out center); Vector3F extents; Vector3F.Subtract(ref Maximum, ref center, out extents); //Vec3 extents = Maximum - center; Vector3F lineDir; lineDir.X = 0.5f * (end.X - start.X); lineDir.Y = 0.5f * (end.Y - start.Y); lineDir.Z = 0.5f * (end.Z - start.Z); //Vec3 lineDir = 0.5f * ( end - start ); Vector3F lineCenter; Vector3F.Add(ref start, ref lineDir, out lineCenter); //Vec3 lineCenter = start + lineDir; Vector3F dir; Vector3F.Subtract(ref lineCenter, ref center, out dir); //Vec3 dir = lineCenter - center; float ld0, ld1, ld2; ld0 = Math.Abs(lineDir.X); if (Math.Abs(dir.X) > extents.X + ld0) { return(false); } ld1 = Math.Abs(lineDir.Y); if (Math.Abs(dir.Y) > extents.Y + ld1) { return(false); } ld2 = Math.Abs(lineDir.Z); if (Math.Abs(dir.Z) > extents.Z + ld2) { return(false); } Vector3F cross; Vector3F.Cross(ref lineDir, ref dir, out cross); if (Math.Abs(cross.X) > extents.Y * ld2 + extents.Z * ld1) { return(false); } if (Math.Abs(cross.Y) > extents.X * ld2 + extents.Z * ld0) return(false); } if (Math.Abs(cross.Z) > extents.X * ld1 + extents.Y * ld0) return(false); }
/// <summary> /// Returns side of the plane that the given box lies on. /// The box is defined as bounds. /// </summary> /// <param name="bounds">The given bounds.</param> /// <returns>The resulting side.</returns> public Side GetSide(ref BoundsF bounds) { Vector3F boundsCenter; bounds.GetCenter(out boundsCenter); //Vec3 boundsHalfSize = boundsCenter - bounds.Minimum; Vector3F boundsHalfSize; Vector3F.Subtract(ref boundsCenter, ref bounds.Minimum, out boundsHalfSize); return(GetSide(ref boundsCenter, ref boundsHalfSize)); }
/// <summary> /// Creates an instance of <see cref="PlaneF"/> that contains the three given points. /// </summary> /// <param name="point0">The first point defining the plane.</param> /// <param name="point1">The second point defining the plane.</param> /// <param name="point2">The third point defining the plane.</param> /// <param name="result">When the method completes, contains the resulting plane.</param> public static void FromPoints(ref Vector3F point0, ref Vector3F point1, ref Vector3F point2, out PlaneF result) { Vector3F edge1; Vector3F.Subtract(ref point1, ref point0, out edge1); Vector3F edge2; Vector3F.Subtract(ref point2, ref point0, out edge2); Vector3F normal; Vector3F.Cross(ref edge1, ref edge2, out normal); normal.Normalize(); result.A = normal.X; result.B = normal.Y; result.C = normal.Z; result.D = -(normal.X * point0.X + normal.Y * point0.Y + normal.Z * point0.Z); }
public void GetDirection(out Vector3F result) { Vector3F.Subtract(ref Point2, ref Point1, out result); result.Normalize(); }
//!!!!было. сложно. //public bool Add( Vec3F p ) //{ // if( radius < 0.0f ) // { // origin = p; // radius = 0.0f; // return true; // } // else // { // float x = p.x - origin.x; // float y = p.y - origin.y; // float z = p.z - origin.z; // float lengthSqr = x * x + y * y + z * z; // float r = lengthSqr; // //float r = ( p - origin ).LengthSqr(); // if( r > radius * radius ) // { // r = MathEx.Sqrt( r ); // float coef = .5f * ( 1.0f - radius / r ); // origin.x += x * coef; // origin.y += y * coef; // origin.z += z * coef; // //origin += ( p - origin ) * 0.5f * ( 1.0f - radius / r ); // radius += .5f * ( r - radius ); // return true; // } // return false; // } //} static void ClosestPtPointTriangle(ref Vector3F p, ref Vector3F a, ref Vector3F b, ref Vector3F c, out Vector3F result) { Vector3F ab; Vector3F.Subtract(ref b, ref a, out ab); //Vec3 ab = b - a; Vector3F ac; Vector3F.Subtract(ref c, ref a, out ac); //Vec3 ac = c - a; Vector3F ap; Vector3F.Subtract(ref p, ref a, out ap); //Vec3 ap = p - a; float d1; Vector3F.Dot(ref ab, ref ap, out d1); float d2; Vector3F.Dot(ref ac, ref ap, out d2); if (d1 <= 0.0f && d2 <= 0.0f) { result = a; return; } //check if P in vertex region outside B Vector3F bp; Vector3F.Subtract(ref p, ref b, out bp); //Vec3 bp = p - b; float d3; Vector3F.Dot(ref ab, ref bp, out d3); float d4; Vector3F.Dot(ref ac, ref bp, out d4); if (d3 >= 0.0f && d4 <= d3) { result = b; return; } float vc = d1 * d4 - d3 * d2; if (vc <= 0.0f && d1 >= 0.0f && d3 <= 0.0f) { float v = d1 / (d1 - d3); result = a + v * ab; return; } Vector3F cp; Vector3F.Subtract(ref p, ref c, out cp); //Vec3 cp = p - c; float d5; Vector3F.Dot(ref ab, ref cp, out d5); float d6; Vector3F.Dot(ref ac, ref cp, out d6); if (d6 >= 0.0f && d5 <= d6) { result = c; return; } float vb = d5 * d2 - d1 * d6; if (vb <= 0.0f && d2 >= 0.0f && d6 <= 0.0f) { float w = d2 / (d2 - d6); result = a + w * ac; return; } float va = d3 * d6 - d5 * d4; if (va <= 0.0f && (d4 - d3) >= 0.0f && (d5 - d6) >= 0.0f) { float w = (d4 - d3) / ((d4 - d3) + (d5 - d6)); result = b + w * (c - b); return; } { float denom = 1.0f / (va + vb + vc); float v = vb * denom; float w = vc * denom; result = a + ab * v + ac * w; return; } }
public static void Subtract(ref Vector3F v, ref BoundsF b, out BoundsF result) { Vector3F.Subtract(ref v, ref b.Minimum, out result.Minimum); Vector3F.Subtract(ref v, ref b.Maximum, out result.Maximum); }
public void GetSize(out Vector3F result) { Vector3F.Subtract(ref Maximum, ref Minimum, out result); }