Example #1
0
        public void DoGreedyTspWithNoReturn_Test()
        {
            // arrange
            var s = new Node {
                Coord = new Coordinate {
                    X = 0, Y = 0
                }, Visited = false, IsStartOrEnd = true
            };
            var a = new Node {
                Coord = new Coordinate {
                    X = 1, Y = 1
                }, Visited = false, IsStartOrEnd = false
            };
            var b = new Node {
                Coord = new Coordinate {
                    X = 2, Y = 1
                }, Visited = false, IsStartOrEnd = false
            };
            var c = new Node {
                Coord = new Coordinate {
                    X = -1, Y = 6
                }, Visited = false, IsStartOrEnd = false
            };
            var t = new Node {
                Coord = new Coordinate {
                    X = 4, Y = 0
                }, Visited = false, IsStartOrEnd = true
            };

            var nodes = new List <Node> {
                s, a, b, c, t
            };

            // expected path
            var expectedPath = new Queue <Coordinate>();

            expectedPath.Enqueue(s.Coord);
            expectedPath.Enqueue(a.Coord);
            expectedPath.Enqueue(b.Coord);
            expectedPath.Enqueue(c.Coord);
            expectedPath.Enqueue(t.Coord);

            var axesRot = new AxisRotation();


            // act
            var greedyPath = axesRot.DoGreedyTspWithNoReturn(nodes);

            // assert
            Assert.Equal(greedyPath.Path, expectedPath);
        }
Example #2
0
        public void GetMappedCoord_Test()
        {
            // arrange
            var angle = -90;
            var dest  = new Coordinate {
                X = 3, Y = 4
            };                                          // this gives a map of (-4, 3) after -90deg rotation
            var axesRot = new AxisRotation();

            // act
            var map = axesRot.GetMappedCoord(angle, dest);

            // assert
            Assert.Equal(map.X, -4);
            Assert.Equal(map.Y, 3);
        }
Example #3
0
        public void GetRotationAngle_Test()
        {
            // arrange
            var curr = new Coordinate {
                X = 3, Y = 4
            };
            var dest = new Coordinate {
                X = 3, Y = 0
            };                                          // this gives rotation angle of -90deg
            var axesRot = new AxisRotation();

            // act
            var angle = axesRot.GetRotationAngle(curr, dest);

            // assert
            Assert.Equal(angle, -90);
        }
Example #4
0
        public static void Main(string[] args)
        {
            var axisRotation = new AxisRotation();
            // Load sample data
            var data1 = SampleData.LoadData();
            var data2 = SampleData.LoadData();

            // Get Greedy Path
            //var greedy = axisRotation.DoGreedyTspWithNoReturn(data1);
            //Console.WriteLine($"AxesRot Distance: {greedy.DistanceTravelled}");

            // Get Axes Rotation Path
            var axesRot = axisRotation.DoAxesRotationTspWithNoReturn(data2);

            axisRotation.PrintPath(axesRot.Path);
            Console.WriteLine($"AxesRot Distance: {axesRot.DistanceTravelled}");
        }
Example #5
0
        public void GetDistanceBetweenNodes_Test()
        {
            // arrange
            var a = new Coordinate {
                X = 0, Y = 0
            };
            var b = new Coordinate {
                X = 3, Y = 4
            };
            var axesRot = new AxisRotation();

            // act
            var d = axesRot.GetDistanceBetweenNodes(a, b);

            // assert
            Assert.Equal(d, 5);
        }
Example #6
0
        private void OnRingVisualMouseDown(object sender, MouseButtonEventArgs e)
        {
            var location      = e.GetPosition(Viewport);
            var meshHitResult = (RayMeshGeometry3DHitTestResult)VisualTreeHelper.HitTest(Viewport, location);

            AxisRotation.Axis = new Vector3D(
                -meshHitResult.PointHit.Y, meshHitResult.PointHit.X, 0);

            var animation = new DoubleAnimation
            {
                To = 40,
                DecelerationRatio = 1,
                Duration          = TimeSpan.FromSeconds(0.15),
                AutoReverse       = true
            };

            AxisRotation.BeginAnimation(AxisAngleRotation3D.AngleProperty, animation);
        }
Example #7
0
        public static void PrintSample(AxisRotation axisRotation, int sample)
        {
            var sampleData = SampleData.LoadDataFromJsonFile($"Data/sample_data_{sample}.json");

            // Result from Greedy Algorithm
            Console.WriteLine("********** GREEDY *********");
            var greedy = axisRotation.DoGreedyTspWithNoReturn(sampleData);

            axisRotation.DisplayData(greedy);
            Console.WriteLine("********** GREEDY *********");

            // Result from Axes Rotation Algorithm
            Console.WriteLine("********** AXES ROTATION *********");
            var axesRot = axisRotation.DoAxesRotationTspWithNoReturn(sampleData);

            axisRotation.DisplayData(axesRot);
            Console.WriteLine("********** AXES ROTATION *********");
        }
Example #8
0
        public static void MultiSampleStats(int sampleSize)
        {
            var sampleOfSamples = new List <List <Node> >();
            var stats           = new List <Tuple <double, double> >();

            for (int i = 0; i < sampleSize; i++)
            {
                var filename = $"Data/multi_{i}.json";
                var sample   = SampleData.GenerateRandomData(20, filename);

                // load data
                var smp = SampleData.LoadDataFromJsonFile(filename);

                // this here is what you should never do, but for now...
                var alg = new AxisRotation();
                var grd = alg.DoGreedyTspWithNoReturn(sample);
                var axr = alg.DoAxesRotationTspWithNoReturn(smp);

                var cmp = new Tuple <double, double>(grd.DistanceTravelled, axr.DistanceTravelled);
                stats.Add(cmp);
            }

            SaveToDisk(stats, $"Data/cmp_2.csv");
        }
Example #9
0
        public void Process(ThnEvent ev, Cutscene cs)
        {
            if (ev.Targets.Capacity == 0)
            {
                return;
            }
            ThnObject objA;

            if (!cs.Objects.TryGetValue((string)ev.Targets[0], out objA))
            {
                FLLog.Error("Thn", "Object does not exist " + (string)ev.Targets[0]);
                return;
            }

            bool       hasPos   = false;
            Quaternion?q_orient = null;

            if (ev.Targets.Capacity >= 1)
            {
                var     props = (LuaTable)ev.Properties["spatialprops"];
                Vector3 pos;
                object  tmp;
                if (props.TryGetValue("q_orient", out tmp))
                {
                    var tb = (LuaTable)tmp;
                    q_orient = new Quaternion((float)tb[1], (float)tb[2], (float)tb[3], (float)tb[0]);
                }

                if (props.TryGetValue("orient", out tmp))
                {
                    var orient = ThnScript.GetMatrix((LuaTable)tmp);
                    q_orient = orient.ExtractRotation();
                }

                AxisRotation axisRotation = null;
                if (props.TryGetValue("axisrot", out tmp))
                {
                    var axisRot_Table = (LuaTable)tmp;
                    axisRotation = new AxisRotation();
                    if (!axisRot_Table.TryGetVector3(1, out axisRotation.Axis))
                    {
                        FLLog.Error("Thn", "invalid axisrot");
                        return;
                    }
                    axisRotation.Axis           = Vector3.TransformNormal(axisRotation.Axis, objA.Rotate);
                    axisRotation.Degrees        = (float)axisRot_Table[0];
                    axisRotation.OriginalRotate = objA.Rotate;
                }

                hasPos = props.TryGetVector3("pos", out pos);
                if (ev.Targets.Capacity > 1)
                {
                    ThnObject objB;
                    if (!cs.Objects.TryGetValue((string)ev.Targets[1], out objB))
                    {
                        FLLog.Error("Thn", "Object does not exist " + (string)ev.Targets[1]);
                        return;
                    }
                    if (ev.Duration < float.Epsilon)
                    {
                        objA.Translate = objB.Translate;
                        objA.Rotate    = objB.Rotate;
                    }
                    else
                    {
                        cs.Coroutines.Add(new FollowSpatialRoutine()
                        {
                            Duration = ev.Duration,
                            HasPos   = hasPos,
                            HasQuat  = q_orient != null,
                            This     = objA,
                            Follow   = objB
                        });
                    }
                }
                else
                {
                    if (ev.Duration < float.Epsilon)
                    {
                        if (hasPos)
                        {
                            objA.Translate = pos;
                        }
                        if (q_orient != null)
                        {
                            objA.Rotate = Matrix4x4.CreateFromQuaternion(q_orient.Value);
                        }
                    }
                    else
                    {
                        cs.Coroutines.Add(new StaticSpatialRoutine()
                        {
                            Duration = ev.Duration,
                            HasPos   = hasPos,
                            HasQuat  = q_orient != null,
                            EndPos   = pos,
                            EndQuat  = q_orient ?? Quaternion.Identity,
                            This     = objA,
                            AxisRot  = axisRotation
                        });
                    }
                }
            }
        }
Example #10
0
        public override void Run(ThnScriptInstance instance)
        {
            if (Targets.Length == 0)
            {
                return;
            }
            bool hasPos  = (SetFlags & AnimVars.Pos) == AnimVars.Pos;
            bool hasQuat = (SetFlags & AnimVars.Orient) == AnimVars.Orient ||
                           (SetFlags & AnimVars.QOrient) == AnimVars.QOrient;

            Quaternion quat = Q_Orient;

            if ((SetFlags & AnimVars.Orient) == AnimVars.Orient)
            {
                quat = Orient.ExtractRotation();
            }

            ThnObject objA;

            if (!instance.Objects.TryGetValue(Targets[0], out objA))
            {
                FLLog.Error("Thn", $"Object does not exist {Targets[0]}");
                return;
            }

            AxisRotation trAxisRot = AxisRot;

            if ((SetFlags & AnimVars.AxisRot) == AnimVars.AxisRot)
            {
                trAxisRot.Axis = Vector3.TransformNormal(trAxisRot.Axis, objA.Rotate);
            }

            if (Targets.Length > 1)
            {
                if (!instance.Objects.TryGetValue(Targets[1], out var objB))
                {
                    FLLog.Error("Thn", $"Object does not exist {Targets[1]}");
                    return;
                }
                if (Duration < float.Epsilon)
                {
                    objA.Translate = objB.Translate;
                    objA.Rotate    = objB.Rotate;
                }
                else
                {
                    instance.AddProcessor(new FollowSpatialRoutine()
                    {
                        Event          = this,
                        HasPos         = hasPos,
                        HasQuat        = hasQuat,
                        This           = objA,
                        Follow         = objB,
                        OriginalRotate = objA.Rotate
                    });
                }
            }
            else
            {
                if (Duration < float.Epsilon)
                {
                    if (hasPos)
                    {
                        objA.Translate = Pos;
                    }
                    if (hasQuat)
                    {
                        objA.Rotate = Matrix4x4.CreateFromQuaternion(quat);
                    }
                    if ((SetFlags & AnimVars.AxisRot) == AnimVars.AxisRot)
                    {
                        objA.Rotate = objA.Rotate * Matrix4x4.CreateFromAxisAngle(trAxisRot.Axis, trAxisRot.GetRads(1));
                    }
                }
                else
                {
                    instance.AddProcessor(new StaticSpatialRoutine()
                    {
                        Event          = this,
                        HasPos         = hasPos,
                        HasQuat        = hasQuat,
                        EndPos         = Pos,
                        EndQuat        = quat,
                        This           = objA,
                        AxisRot        = trAxisRot,
                        OriginalRotate = objA.Rotate
                    });
                }
            }
        }