示例#1
0
 public bool FindSideByPoint(Point point, Scene scene, Matrix parentWorld,
     out CubeCoords coords, out CubeSide side)
 {
     var worldView = view.World * World * parentWorld * scene.View;
     return view.FindSideByPoint(point, scene, worldView, scene.Projection,
         out coords, out side);
 }
示例#2
0
        public Rotation CreateRotationFromSides(
            CubeCoords coords1, CubeSide side1,
            CubeCoords coords2, CubeSide side2)
        {
            int left1 = coords1.Left;
            int top1 = coords1.Top;
            int depth1 = coords1.Depth;

            int left2 = coords2.Left;
            int top2 = coords2.Top;
            int depth2 = coords2.Depth;

            if (side1 == side2 && coords1 != coords2)
            {
                bool invert;
                Axis? equalsWay = GetEqualsWay(side1,
                    left1, top1, depth1,
                    left2, top2, depth2,
                    out invert);

                if (equalsWay == null)
                    return null;

                Axis rotation = (Axis)equalsWay;
                int layer = CoordsHelper.GetLayerFromAxis(rotation, coords1);

                return new Rotation(rotation, layer, !invert);
            }
            else if (coords1 == coords2 && side1 != side2)
            {
                int sum = (int)Axis.Left + (int)Axis.Top + (int)Axis.Depth;
                Axis axis1 = CoordsHelper.GetAxisFromSide(side1);
                Axis axis2 = CoordsHelper.GetAxisFromSide(side2);

                Axis rotation = (Axis)(sum - (int)axis1 - (int)axis2);
                int layer = CoordsHelper.GetLayerFromAxis(rotation, coords1);

                bool clockwise = false;
                for (int i = 0; i < rotationRules.GetLength(0); i++)
                {
                    if (rotationRules[i, 0] == side1 &&
                        rotationRules[i, 1] == side2)
                    {
                        clockwise = true;
                        break;
                    }
                }

                return new Rotation(rotation, layer, clockwise);
            }

            return null;
        }
示例#3
0
        public static Vector3 GetPositionFromCoords(CubeCoords coords, int cubeSize)
        {
            if (cubeSize <= 0)
            {
                throw new ArgumentOutOfRangeException("cubeSize");
            }

            float shift = ((float)cubeSize - 1) / 2;

            return(new Vector3(
                       coords.Left - shift,
                       -coords.Top + shift,
                       -coords.Depth + shift) * 2f / cubeSize);
        }
示例#4
0
        public static int GetLayerFromAxis(Axis axis, CubeCoords coords)
        {
            switch (axis)
            {
            case Axis.Left:
                return(coords.Left);

            case Axis.Top:
                return(coords.Top);

            case Axis.Depth:
                return(coords.Depth);

            default:
                throw new ArgumentException("Invalid axis value.", "axis");
            }
        }
示例#5
0
        public void ReplaceCube(DataCube<SmallCube> newCube)
        {
            if (newCube == null)
                throw new ArgumentNullException("newCube");
            if (newCube.Size != Size)
                throw new ArgumentException("newCube.Size must equals Size.", "newCube");

            var replacer = new DataCube<SmallCube>(newCube.Size);
            int maxIndex = newCube.Size - 1;

            foreach (var coords in CubeCoords.EnumerateCube(Size))
            {
                bool isOuterCube = 
                    coords.Left  == 0 || coords.Left  == maxIndex ||
                    coords.Top   == 0 || coords.Top   == maxIndex ||
                    coords.Depth == 0 || coords.Depth == maxIndex;

                if (isOuterCube)
                {
                    if (newCube[coords] == null)
                    {
                        throw new ArgumentException(
                            "Outer cube element at (" + coords.ToString() + ") is null.", "newCube");
                    }

                    replacer[coords] = (SmallCube)newCube[coords].Clone();
                }
            }

            if (view.AnimationInProgress)
                view.EndAnimation();

            var viewReplacer = new CubeView<DefaultCubePart>(
                replacer, colorMap, true);

            viewReplacer.World = view.World;
            viewReplacer.Up = view.Up;
            viewReplacer.Look = view.Look;
            viewReplacer.Right = viewReplacer.Right;

            dataCube = replacer;
            view = viewReplacer;
        }
 public T this[CubeCoords coords]
 {
     get { return(this[coords.Left, coords.Top, coords.Depth]); }
     set { this[coords.Left, coords.Top, coords.Depth] = value; }
 }
 public SearchCandidate(CubeCoords coords, CubeSide side, Matrix wvp)
 {
     Coords = coords;
     Side   = side;
     Wvp    = wvp;
 }
        public bool FindSideByPoint(Point point, Scene scene, Matrix worldView, Matrix projection,
                                    out CubeCoords coords, out CubeSide side)
        {
            coords = new CubeCoords();
            side   = CubeSide.Left;

            Matrix  wvp = worldView * projection;
            Vector3 p   = new Vector3(
                (float)point.X * 2f / scene.Viewport.Width - 1f,
                -(float)point.Y * 2f / scene.Viewport.Height + 1f,
                0);

            bool[] visibility = new bool[]
            {
                Vector3.TransformNormal(Vector3.Left, ref worldView).Z > 0,
                Vector3.TransformNormal(Vector3.Right, ref worldView).Z > 0,
                Vector3.TransformNormal(Vector3.Up, ref worldView).Z > 0,
                Vector3.TransformNormal(Vector3.Down, ref worldView).Z > 0,
                Vector3.TransformNormal(Vector3.Backward, ref worldView).Z > 0,
                Vector3.TransformNormal(Vector3.Forward, ref worldView).Z > 0,
            };

            var candidates = new List <SearchCandidate>();

            CubeCoords[] indices = new CubeCoords[6];

            int maxIndex = viewCube.Size - 1;

            for (int i = 0; i < viewCube.Size; i++)
            {
                for (int j = 0; j < viewCube.Size; j++)
                {
                    indices[(int)CubeSide.Left]  = new CubeCoords(0, i, j);
                    indices[(int)CubeSide.Right] = new CubeCoords(maxIndex, i, j);
                    indices[(int)CubeSide.Up]    = new CubeCoords(i, 0, j);
                    indices[(int)CubeSide.Down]  = new CubeCoords(i, maxIndex, j);
                    indices[(int)CubeSide.Front] = new CubeCoords(i, j, 0);
                    indices[(int)CubeSide.Back]  = new CubeCoords(i, j, maxIndex);

                    for (int k = 0; k < indices.Length; k++)
                    {
                        coords = indices[k];
                        side   = (CubeSide)k;
                        Matrix localWvp = viewCube[coords].World * wvp;

                        if (visibility[(int)side] &&
                            IsSideContainsPoint(viewCube[coords], side, p, scene, localWvp))
                        {
                            candidates.Add(new SearchCandidate(coords, side, localWvp));
                        }
                    }
                }
            }

            var cubes = candidates.OrderBy(c => Vector3.Transform(Vector3.Zero, ref c.Wvp).Z);

            if (cubes.Any())
            {
                coords = cubes.First().Coords;
                side   = cubes.First().Side;
                return(true);
            }

            return(false);
        }
示例#9
0
 public bool Equals(CubeCoords other)
 {
     return(Left == other.Left && Top == other.Top && Depth == other.Depth);
 }