예제 #1
0
 /// <summary>Transform a direction vector by the given Matrix
 /// Assumes the matrix has a bottom row of (0,0,0,1), that is the translation part is ignored.
 /// </summary>
 /// <param name="vec">The vector to transform</param>
 /// <param name="mat">The desired transformation</param>
 /// <returns>The transformed vector</returns>
 public static Vector3Float TransformVector(this Vector3Float vec, Matrix4X4 mat)
 {
     return(new Vector3Float(
                vec.Dot(new Vector3Float(mat.Column0)),
                vec.Dot(new Vector3Float(mat.Column1)),
                vec.Dot(new Vector3Float(mat.Column2))));
 }
예제 #2
0
 /// <summary>Transform a Normal by the (transpose of the) given Matrix</summary>
 /// <remarks>
 /// This version doesn't calculate the inverse matrix.
 /// Use this version if you already have the inverse of the desired transform to hand
 /// </remarks>
 /// <param name="norm">The normal to transform</param>
 /// <param name="invMat">The inverse of the desired transformation</param>
 /// <returns>The transformed normal</returns>
 public static Vector3Float TransformNormalInverse(this Vector3Float norm, Matrix4X4 invMat)
 {
     return(new Vector3Float(
                norm.Dot(new Vector3Float(invMat.Row0)),
                norm.Dot(new Vector3Float(invMat.Row1)),
                norm.Dot(new Vector3Float(invMat.Row2))));
 }
예제 #3
0
        /// <summary>Calculates the angle (in radians) between two vectors.</summary>
        /// <param name="first">The first vector.</param>
        /// <param name="second">The second vector.</param>
        /// <param name="result">Angle (in radians) between the vectors.</param>
        /// <remarks>Note that the returned angle is never bigger than the constant Pi.</remarks>
        public static void CalculateAngle(this Vector3Float first, ref Vector3Float second, out float result)
        {
            float temp;

            first.Dot(ref second, out temp);
            result = (float)Math.Acos(temp / (first.Length * second.Length));
        }
예제 #4
0
        public bool RayHitPlane(Ray ray, out float distanceToHit, out bool hitFrontOfPlane)
        {
            distanceToHit   = float.PositiveInfinity;
            hitFrontOfPlane = false;

            float normalDotRayDirection = Vector3Float.Dot(Normal, new Vector3Float(ray.directionNormal));

            if (normalDotRayDirection < TreatAsZero && normalDotRayDirection > -TreatAsZero)             // the ray is parallel to the plane
            {
                return(false);
            }

            if (normalDotRayDirection < 0)
            {
                hitFrontOfPlane = true;
            }

            float distanceToRayOriginFromOrigin = Vector3Float.Dot(Normal, new Vector3Float(ray.origin));

            float distanceToPlaneFromRayOrigin = DistanceFromOrigin - distanceToRayOriginFromOrigin;

            bool originInFrontOfPlane = distanceToPlaneFromRayOrigin < 0;

            bool originAndHitAreOnSameSide = originInFrontOfPlane == hitFrontOfPlane;

            if (!originAndHitAreOnSameSide)
            {
                return(false);
            }

            distanceToHit = distanceToPlaneFromRayOrigin / normalDotRayDirection;
            return(true);
        }
예제 #5
0
        /// <summary>Transform a Position by the given Matrix</summary>
        /// <param name="pos">The position to transform</param>
        /// <param name="mat">The desired transformation</param>
        /// <returns>The transformed position</returns>
        public static Vector3Float TransformPosition(this Vector3Float pos, Matrix4X4 mat)
        {
#if true
            throw new NotImplementedException();
#else
            return(new Vector3Float(
                       Vector3Float.Dot(pos, new Vector3Float((float)mat.Column0)) + mat.Row3.x,
                       Vector3Float.Dot(pos, new Vector3Float((float)mat.Column1)) + mat.Row3.y,
                       Vector3Float.Dot(pos, new Vector3Float((float)mat.Column2)) + mat.Row3.z));
#endif
        }
예제 #6
0
        public override (double u, double v) GetUv(IntersectInfo info)
        {
            Vector3Float normal = Plane.Normal;
            Vector3Float vecU   = new Vector3Float(normal.y, normal.z, -normal.x);
            Vector3Float vecV   = Vector3Float.Cross(vecU, Plane.Normal);

            var u = Vector3Float.Dot(new Vector3Float(info.HitPosition), vecU);
            var v = Vector3Float.Dot(new Vector3Float(info.HitPosition), vecV);

            return(u, v);
        }
예제 #7
0
        public float GetDistanceToIntersection(Vector3Float pointOnLine, Vector3Float lineDirection)
        {
            float normalDotRayDirection = Vector3Float.Dot(Normal, lineDirection);

            if (normalDotRayDirection < TreatAsZero && normalDotRayDirection > -TreatAsZero)             // the ray is parallel to the plane
            {
                return(float.PositiveInfinity);
            }

            float planeNormalDotPointOnLine = Vector3Float.Dot(Normal, pointOnLine);

            return((DistanceFromOrigin - planeNormalDotPointOnLine) / normalDotRayDirection);
        }
예제 #8
0
        public override RGBA_Floats GetColor(IntersectInfo info)
        {
            if (Material.HasTexture)
            {
                Vector3Float Position = plane.planeNormal;
                Vector3Float vecU     = new Vector3Float(Position.y, Position.z, -Position.x);
                Vector3Float vecV     = Vector3Float.Cross(vecU, plane.planeNormal);

                double u = Vector3Float.Dot(new Vector3Float(info.hitPosition), vecU);
                double v = Vector3Float.Dot(new Vector3Float(info.hitPosition), vecV);
                return(Material.GetColor(u, v));
            }
            else
            {
                return(Material.GetColor(0, 0));
            }
        }
예제 #9
0
        public float GetDistanceToIntersection(Ray ray, out bool inFront)
        {
            inFront = false;
            float normalDotRayDirection = Vector3Float.Dot(Normal, new Vector3Float(ray.directionNormal));

            if (normalDotRayDirection < TreatAsZero && normalDotRayDirection > -TreatAsZero)             // the ray is parallel to the plane
            {
                return(float.PositiveInfinity);
            }

            if (normalDotRayDirection < 0)
            {
                inFront = true;
            }

            return((DistanceFromOrigin - Vector3Float.Dot(Normal, new Vector3Float(ray.origin))) / normalDotRayDirection);
        }
예제 #10
0
        public ColorF GetColor(IntersectInfo info)
        {
            if (Material.HasTexture)
            {
                Vector3Float normalF = new Vector3Float(Vector3.TransformNormal(face.Normal, worldMatrix));
                Vector3Float vecU    = new Vector3Float(normalF.y, normalF.z, -normalF.x);
                Vector3Float vecV    = Vector3Float.Cross(vecU, normalF);

                double u = Vector3Float.Dot(new Vector3Float(info.HitPosition), vecU);
                double v = Vector3Float.Dot(new Vector3Float(info.HitPosition), vecV);
                return(Material.GetColor(u, v));
            }
            else
            {
                return(Material.GetColor(0, 0));
            }
        }
예제 #11
0
 /// <summary>
 /// Calculates the angle (in radians) between two vectors.
 /// </summary>
 /// <param name="first">The first vector.</param>
 /// <param name="second">The second vector.</param>
 /// <returns>Angle (in radians) between the vectors.</returns>
 /// <remarks>Note that the returned angle is never bigger than the constant Pi.</remarks>
 public static float CalculateAngle(this Vector3Float first, Vector3Float second)
 {
     return((float)Math.Acos((first.Dot(second)) / (first.Length * second.Length)));
 }
예제 #12
0
        public float GetDistanceFromPlane(Vector3Float positionToCheck)
        {
            float distanceToPointFromOrigin = Vector3Float.Dot(positionToCheck, Normal);

            return(distanceToPointFromOrigin - DistanceFromOrigin);
        }
예제 #13
0
 public PlaneFloat(Vector3Float point0, Vector3Float point1, Vector3Float point2)
 {
     this.Normal             = Vector3Float.Cross((point1 - point0), (point2 - point0)).GetNormal();
     this.DistanceFromOrigin = Vector3Float.Dot(Normal, point0);
 }