Пример #1
0
        public static Mat4 LookAt(Vect3 eye, Vect3 target, Vect3 up)
        {
            var vector31 = (eye - target).Normalize();
            var right    = up.CrossProduct(vector31).Normalize();
            var vector32 = vector31.CrossProduct(right).Normalize();

            return(new Mat4(new[, ]
            {
                { right.X, right.Y, right.Z, 0.0 },
                { vector32.X, vector32.Y, vector32.Z, 0.0 },
                { vector31.X, vector31.Y, vector31.Z, 0.0 },
                { 0.0, 0.0, 0.0, 1.0 }
            }) * Translate(-eye));
        }
Пример #2
0
        public static Camera CreateCamera(Vect3 position, Vect3 viewPaneCenter, Vect3 camUp, double viewPaneWidth,
                                          double viewPaneHeight)
        {
            var viewPaneHeightVector = camUp;

            viewPaneHeightVector *= -viewPaneHeight;

            Vect3 temp1 = position - viewPaneCenter;
            Vect3 viewPaneWidthVector = temp1.CrossProduct(viewPaneHeightVector);

            viewPaneWidthVector  = viewPaneWidthVector.Normalize();
            viewPaneWidthVector *= viewPaneWidth;

            viewPaneWidthVector.CopyDataTo(ref temp1);
            temp1 = temp1 / 2;
            Vect3 viewPaneEdge = viewPaneCenter - temp1;

            viewPaneHeightVector.CopyDataTo(ref temp1);
            temp1         = temp1 / 2;
            viewPaneEdge -= temp1;

            return(new Camera(position, viewPaneEdge, viewPaneWidthVector, viewPaneHeightVector));
        }
Пример #3
0
        public static double GetHitPointRayTriangleDistance(Vect3 rayPosition, Vect3 rayDirection, Vect3 trianglePos,
                                                            Vect3 triangleVect1, Vect3 triangleVect2)
        {
            Vect3  tmp = triangleVect1.CrossProduct(triangleVect2);
            double ret = RayPlane.GetHitPointRayPlaneDistance(rayPosition, rayDirection, trianglePos, tmp);

            if (double.IsPositiveInfinity(ret) || ret < Constants.EPS)
            {
                return(double.PositiveInfinity);
            }

            tmp  = rayPosition + rayDirection * ret;
            tmp -= trianglePos;

            double uu = triangleVect1 * triangleVect1;
            double uv = triangleVect1 * triangleVect2;
            double vv = triangleVect2 * triangleVect2;
            double wu = triangleVect1 * tmp;
            double wv = triangleVect2 * tmp;
            double d  = uv * uv - uu * vv;

            double s = (uv * wv - vv * wu) / d;

            if (s < -TriEPS || s > 1 + TriEPS)
            {
                return(double.PositiveInfinity);
            }

            double t = (uv * wu - uu * wv) / d;

            if (t < -TriEPS || s + t > 1 + TriEPS)
            {
                return(double.PositiveInfinity);
            }

            return(ret);
        }
Пример #4
0
        public static Vect3 GetCircumScribedCircleCenter(Vect3 a, Vect3 b, Vect3 c)
        {
            Vect3 mab    = b - a;
            Vect3 mac    = c - a;
            Vect3 normal = mab.CrossProduct(mac);
            Vect3 dab    = mab.CrossProduct(normal);
            Vect3 dac    = mac.CrossProduct(normal);

            mab = a + mab / 2;
            mac = a + mac / 2;
            dab = dab.Normalize();
            dac = dac.Normalize();


            if (System.Math.Abs(dac.X) > Constants.EPS &&
                System.Math.Abs(dac.Y) > Constants.EPS &&
                System.Math.Abs(dab.X / dac.X - dab.Y / dac.Y) > Constants.EPS)
            {
                double x = ((mab.Y - mac.Y) / dac.Y - (mab.X - mac.X) / dac.X) / (dab.X / dac.X - dab.Y / dac.Y);
                return(mab + dab * x);
            }

            if (System.Math.Abs(dac.X) > Constants.EPS &&
                System.Math.Abs(dac.Z) > Constants.EPS &&
                System.Math.Abs(dab.Z / dac.Z - dab.X / dac.X) > Constants.EPS)
            {
                double x = ((mab.X - mac.X) / dac.X - (mab.Z - mac.Z) / dac.Z) / (dab.Z / dac.Z - dab.X / dac.X);
                return(mab + dab * x);
            }

            if (System.Math.Abs(dac.Y) > Constants.EPS &&
                System.Math.Abs(dac.Z) > Constants.EPS &&
                System.Math.Abs(dab.Y / dac.Y - dab.Z / dac.Z) > Constants.EPS)
            {
                double x = ((mab.Z - mac.Z) / dac.Z - (mab.Y - mac.Y) / dac.Y) / (dab.Y / dac.Y - dab.Z / dac.Z);
                return(mab + dab * x);
            }

            if (System.Math.Abs(dab.Y) > Constants.EPS &&
                System.Math.Abs(dab.X) > Constants.EPS &&
                System.Math.Abs(dac.X / dab.X - dac.Y / dab.Y) > Constants.EPS)
            {
                double y = ((mac.Y - mab.Y) / dab.Y - (mac.X - mab.X) / dab.X) / (dac.X / dab.X - dac.Y / dab.Y);
                return(mac + dac * y);
            }

            if (System.Math.Abs(dab.Z) > Constants.EPS &&
                System.Math.Abs(dab.X) > Constants.EPS &&
                System.Math.Abs(dac.Z / dab.Z - dac.X / dab.X) > Constants.EPS)
            {
                double y = ((mac.X - mab.X) / dab.X - (mac.Z - mab.Z) / dab.Z) / (dac.Z / dab.Z - dac.X / dab.X);
                return(mac + dac * y);
            }

            if (System.Math.Abs(dab.Z) > Constants.EPS &&
                System.Math.Abs(dab.Y) > Constants.EPS &&
                System.Math.Abs(dac.Y / dab.Y - dac.Z / dab.Z) > Constants.EPS)
            {
                double y = ((mac.Z - mab.Z) / dab.Z - (mac.Y - mab.Y) / dab.Y) / (dac.Y / dab.Y - dac.Z / dab.Z);
                return(mac + dac * y);
            }

            throw new Exception("implement more cases...");
        }
Пример #5
0
 public static Mat4 LookAt(Vect3 eye, Vect3 target, Vect3 up)
 {
     var vector31 = (eye - target).Normalize();
     var right = up.CrossProduct(vector31).Normalize();
     var vector32 = vector31.CrossProduct(right).Normalize();
     return new Mat4(new[,]
         {
             {right.X, right.Y, right.Z, 0.0},
             {vector32.X, vector32.Y, vector32.Z, 0.0},
             {vector31.X, vector31.Y, vector31.Z, 0.0},
             {0.0, 0.0, 0.0, 1.0}
         }) * Translate(-eye);
 }