Ejemplo n.º 1
0
        private void dataGridViewDistances_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0)
            {
                var cell  = dataGridViewDistances[e.ColumnIndex, e.RowIndex];
                var value = cell.Value as string;
                cell.Style.BackColor = Color.White;
                if (value == null)
                {
                    return;
                }
                var system = SystemData.GetSystem(value);

                if (system == null)
                {
                    cell.Value = null;
                    //cell.Style.BackColor = Color.Salmon;
                    return;
                }

                if (value != system.name)
                {
                    cell.Value = system.name;
                }
                cell.Tag = system;
            }

            // reset any calculated distances
            dataGridViewDistances[2, e.RowIndex].Value = null;
            dataGridViewDistances[3, e.RowIndex].Value = null;

            // trigger trilateration calculation
            RunTrilateration();
        }
Ejemplo n.º 2
0
        private void TestTrileteration()
        {
            foreach (SystemClass System in SQLiteDBClass.globalSystems)
            {
                if (DateTime.Now.Subtract(System.CreateDate).TotalDays < 60)
                {
                    //var Distances = from SQLiteDBClass.globalDistances

                    var distances1 = from p in SQLiteDBClass.dictDistances where p.Value.NameA.ToLower() == System.SearchName select p.Value;
                    var distances2 = from p in SQLiteDBClass.dictDistances where p.Value.NameB.ToLower() == System.SearchName select p.Value;

                    int nr = distances1.Count();
                    //nr = distances2.Count();


                    if (nr > 4)
                    {
                        var trilateration = new Trilateration();
                        //                    trilateration.Logger = (s) => System.Console.WriteLine(s);

                        foreach (var item in distances1)
                        {
                            SystemClass distsys = SystemData.GetSystem(item.NameB);
                            if (distsys != null)
                            {
                                if (distsys.HasCoordinate)
                                {
                                    Trilateration.Entry entry = new Trilateration.Entry(distsys.x, distsys.y, distsys.z, item.Dist);
                                    trilateration.AddEntry(entry);
                                }
                            }
                        }

                        foreach (var item in distances2)
                        {
                            SystemClass distsys = SystemData.GetSystem(item.NameA);
                            if (distsys != null)
                            {
                                if (distsys.HasCoordinate)
                                {
                                    Trilateration.Entry entry = new Trilateration.Entry(distsys.x, distsys.y, distsys.z, item.Dist);
                                    trilateration.AddEntry(entry);
                                }
                            }
                        }


                        var csharpResult     = trilateration.Run(Trilateration.Algorithm.RedWizzard_Native);
                        var javascriptResult = trilateration.Run(Trilateration.Algorithm.RedWizzard_Emulated);
                        if (javascriptResult.State == Trilateration.ResultState.Exact)
                        {
                            nr++;
                        }
                    }
                }
            }
        }
Ejemplo n.º 3
0
        private void UpdateTo(bool updatename)
        {
            changesilence = true;

            if (textBox_To.ReadOnly == false)                // if entering system name..
            {
                EDDiscovery2.DB.ISystem ds1 = SystemData.GetSystem(SystemNameOnly(textBox_To.Text));

                if (ds1 != null)
                {
                    if (updatename)                          // can't fix it as you type.. so leave alone
                    {
                        textBox_To.Text = ds1.name;
                    }

                    textBox_ToX.Text = ds1.x.ToString("0.00");
                    textBox_ToY.Text = ds1.y.ToString("0.00");
                    textBox_ToZ.Text = ds1.z.ToString("0.00");
                }
                else
                {
                    textBox_ToX.Text = textBox_ToY.Text = textBox_ToZ.Text = "";
                }
            }
            else // Co-ords..
            {
                string  res = "";
                Point3D curpos;
                if (GetCoordsTo(out curpos))
                {
                    SystemClass nearest;
                    double      distance;
                    FindNearestSystem(curpos, out nearest, out distance);

                    if (distance < 0.1)
                    {
                        res = nearest.name;
                    }
                    else
                    {
                        res = nearest.name + " @ " + distance.ToString("0.00") + "ly";
                    }
                }

                textBox_To.Text = res;
            }

            UpdateDistance();

            changesilence        = false;
            button_Route.Enabled = IsValid();
        }
Ejemplo n.º 4
0
        /* Tries to load the system data for the given name. If no system data is available, but the system is known,
         * it creates a new System entity, otherwise logs it and returns null. */
        private SystemClass getSystemForTrilateration(string systemName)
        {
            var system = SystemData.GetSystem(systemName);

            if (system == null)
            {
                if (!edsm.IsKnownSystem(systemName))
                {
                    LogText("Only systems with coordinates or already known to EDSM can be added" + Environment.NewLine, Color.Red);
                }
                else
                {
                    system = new SystemClass(systemName);
                }
            }
            return(system);
        }
Ejemplo n.º 5
0
 private void checkForUnknownSystemsNowKnown()
 {
     for (int i = 0, count = dataGridViewDistances.Rows.Count - 1; i < count; i++)
     {
         var systemCell = dataGridViewDistances[0, i];
         var oldSystem  = (SystemClass)systemCell.Tag;
         if (!oldSystem.HasCoordinate)
         {
             var value     = systemCell.Value as string;
             var newSystem = SystemData.GetSystem(value);
             if (newSystem != null && newSystem.HasCoordinate)
             {
                 systemCell.Tag = newSystem;
                 dataGridViewDistances[3, i].Style.ForeColor = Color.Green;
                 dataGridViewDistances[3, i].Value           = "Position found";
             }
         }
     }
 }
Ejemplo n.º 6
0
        private bool IsValid()                          // have we star names or co-ords ready to go
        {
            bool    readytocalc = true;
            Point3D pos;

            if (textBox_From.ReadOnly == false)          // if enabled, we are doing star names
            {
                if (SystemData.GetSystem(SystemNameOnly(textBox_From.Text)) == null)
                {
                    readytocalc = false;
                }
            }
            else // check co-ords
            {
                if (!GetCoordsFrom(out pos))
                {
                    readytocalc = false;
                }
            }
            if (textBox_To.ReadOnly == false)          // if enabled, we are doing star names
            {
                if (SystemData.GetSystem(SystemNameOnly(textBox_To.Text)) == null)
                {
                    readytocalc = false;
                }
            }
            else // check co-ords
            {
                if (!GetCoordsTo(out pos))
                {
                    readytocalc = false;
                }
            }

            if (comboBoxRoutingMetric.SelectedIndex < 0)
            {
                readytocalc = false;
            }

            return(readytocalc);
        }
Ejemplo n.º 7
0
        private void dataGridViewDistances_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            if (e.ColumnIndex == 0)
            {
                var value = e.FormattedValue.ToString();
                var cell  = dataGridViewDistances[e.ColumnIndex, e.RowIndex];

                if (value == "" && (cell.Value == null || cell.Value.ToString() == ""))
                {
                    return;
                }

                var system         = SystemData.GetSystem(value);
                var enteredSystems = GetEnteredSystems();
                if (cell.Value != null)
                {
                    enteredSystems.RemoveAll(s => s.name == cell.Value.ToString());
                }

                if (system == null || (enteredSystems.Contains(system)))
                {
                    //e.Cancel = true;
                    return;
                }
            }

            if (e.ColumnIndex == 1)
            {
                var value = e.FormattedValue.ToString().Trim();

                if (value == "")
                {
                    return;
                }

                var regex = new Regex(@"^\d{1,5}([,.]\d{1,2})?$");
                e.Cancel = !regex.Match(e.FormattedValue.ToString()).Success;
            }
        }
Ejemplo n.º 8
0
        internal void NewPosition(object source)
        {
            try
            {
                string name = netlog.visitedSystems.Last().Name;
                Invoke((MethodInvoker) delegate
                {
                    LogText("Arrived to system: ");
                    SystemClass sys1 = SystemData.GetSystem(name);
                    if (sys1 == null || sys1.HasCoordinate == false)
                    {
                        LogTextHighlight(name);
                    }
                    else
                    {
                        LogText(name);
                    }


                    int count = GetVisitsCount(name);

                    LogText("  : Vist nr " + count.ToString() + Environment.NewLine);
                    System.Diagnostics.Trace.WriteLine("Arrived to system: " + name + " " + count.ToString() + ":th visit.");

                    var result = visitedSystems.OrderByDescending(a => a.time).ToList <SystemPosition>();

                    //if (TrilaterationControl.Visible)
                    //{
                    //    CloseTrilateration();
                    //    MessageBox.Show("You have arrived to another system while trilaterating."
                    //                    + " As a pre-caution to prevent any mistakes with submitting wrong systems or distances"
                    //                    + ", your trilateration was aborted.");
                    //}


                    SystemPosition item = result[0];
                    SystemPosition item2;

                    if (result.Count > 1)
                    {
                        item2 = result[1];
                    }
                    else
                    {
                        item2 = null;
                    }

                    // grab distance to next (this) system
                    textBoxDistanceToNextSystem.Enabled = false;
                    if (textBoxDistanceToNextSystem.Text.Length > 0 && item2 != null)
                    {
                        SystemClass currentSystem = null, previousSystem = null;
                        SystemData.SystemList.ForEach(s =>
                        {
                            if (s.name == item.Name)
                            {
                                currentSystem = s;
                            }
                            if (s.name == item2.Name)
                            {
                                previousSystem = s;
                            }
                        });

                        if (currentSystem == null || previousSystem == null || !currentSystem.HasCoordinate || !previousSystem.HasCoordinate)
                        {
                            var presetDistance = DistanceAsDouble(textBoxDistanceToNextSystem.Text.Trim(), 45);
                            if (presetDistance.HasValue)
                            {
                                var distance = new DistanceClass
                                {
                                    Dist            = presetDistance.Value,
                                    CreateTime      = DateTime.UtcNow,
                                    CommanderCreate = EDDiscoveryForm.EDDConfig.CurrentCommander.Name,
                                    NameA           = item.Name,
                                    NameB           = item2.Name,
                                    Status          = DistancsEnum.EDDiscovery
                                };
                                Console.Write("Pre-set distance " + distance.NameA + " -> " + distance.NameB + " = " + distance.Dist);
                                distance.Store();
                                SQLiteDBClass.AddDistanceToCache(distance);
                            }
                        }
                    }
                    textBoxDistanceToNextSystem.Clear();
                    textBoxDistanceToNextSystem.Enabled = true;

                    AddHistoryRow(true, item, item2);
                    StoreSystemNote();
                });
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine("Exception NewPosition: " + ex.Message);
                System.Diagnostics.Trace.WriteLine("Trace: " + ex.StackTrace);
            }
        }
Ejemplo n.º 9
0
        private void ShowClosestSystems(string name)
        {
            sysDist = new List <SystemDist>();
            SystemClass LastSystem = null;
            float       dx, dy, dz;
            double      dist;

            try
            {
                if (name == null)
                {
                    var result = visitedSystems.OrderByDescending(a => a.time).ToList <SystemPosition>();


                    for (int ii = 0; ii < result.Count; ii++) //foreach (var item in result)
                    {
                        SystemPosition item = result[ii];

                        LastSystem = SystemData.GetSystem(item.Name);
                        name       = item.Name;
                        if (LastSystem != null)
                        {
                            break;
                        }
                    }
                }
                else
                {
                    LastSystem = SystemData.GetSystem(name);
                }

                if (name != null)
                {
                    label3.Text = "Closest systems from " + name.ToString();
                }

                dataGridViewNearest.Rows.Clear();

                if (LastSystem == null)
                {
                    return;
                }

                foreach (SystemClass pos in SystemData.SystemList)
                {
                    dx   = (float)(pos.x - LastSystem.x);
                    dy   = (float)(pos.y - LastSystem.y);
                    dz   = (float)(pos.z - LastSystem.z);
                    dist = dx * dx + dy * dy + dz * dz;

                    //distance = (float)((system.x - arcsystem.x) * (system.x - arcsystem.x) + (system.y - arcsystem.y) * (system.y - arcsystem.y) + (system.z - arcsystem.z) * (system.z - arcsystem.z));

                    if (dist > 0)
                    {
                        SystemDist sdist = new SystemDist();
                        sdist.name = pos.name;
                        sdist.dist = Math.Sqrt(dist);
                        sysDist.Add(sdist);
                    }
                }

                var list = (from t in sysDist orderby t.dist select t).Take(50);

                foreach (SystemDist sdist in list)
                {
                    object[] rowobj = { sdist.name, sdist.dist.ToString("0.00") };
                    dataGridViewNearest.Rows.Add(rowobj);
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine("Exception : " + ex.Message);
                System.Diagnostics.Trace.WriteLine(ex.StackTrace);
            }
        }
Ejemplo n.º 10
0
        private void AddHistoryRow(bool insert, SystemPosition item, SystemPosition item2)
        {
            SystemClass sys1 = null, sys2;
            double      dist;


            sys1 = SystemData.GetSystem(item.Name);
            if (sys1 == null)
            {
                sys1 = new SystemClass(item.Name);
                if (SQLiteDBClass.globalSystemNotes.ContainsKey(sys1.SearchName))
                {
                    sys1.Note = SQLiteDBClass.globalSystemNotes[sys1.SearchName].Note;
                }
            }
            if (item2 != null)
            {
                sys2 = SystemData.GetSystem(item2.Name);
                if (sys2 == null)
                {
                    sys2 = new SystemClass(item2.Name);
                }
            }
            else
            {
                sys2 = null;
            }

            item.curSystem  = sys1;
            item.prevSystem = sys2;
            if (!insert)
            {
                if (item.vs == null)
                {
                    SystemPosition known = visitedSystems.First(x => x.Name == item.Name);
                    if (known != null)
                    {
                        item.vs = known.vs;
                    }
                }
            }

            string diststr = "";

            dist = 0;
            if (sys2 != null)
            {
                if (sys1.HasCoordinate && sys2.HasCoordinate)
                {
                    dist = SystemData.Distance(sys1, sys2);
                }
                else
                {
                    dist = DistanceClass.Distance(sys1, sys2);
                }

                if (dist > 0)
                {
                    diststr = dist.ToString("0.00");
                }
            }

            item.strDistance = diststr;

            //richTextBox_History.AppendText(item.time + " " + item.Name + Environment.NewLine);

            object[] rowobj = { item.time, item.Name, diststr, item.curSystem.Note, "█" };
            int      rownr;

            if (insert)
            {
                dataGridViewTravel.Rows.Insert(0, rowobj);
                rownr = 0;
            }
            else
            {
                dataGridViewTravel.Rows.Add(rowobj);
                rownr = dataGridViewTravel.Rows.Count - 1;
            }

            var cell = dataGridViewTravel.Rows[rownr].Cells[1];

            cell.Tag = item;

            dataGridViewTravel.Rows[rownr].DefaultCellStyle.ForeColor = (sys1.HasCoordinate) ? _discoveryForm.theme.VisitedSystemColor : _discoveryForm.theme.NonVisitedSystemColor;

            cell = dataGridViewTravel.Rows[rownr].Cells[4];
            cell.Style.ForeColor = (item.vs == null) ? Color.FromArgb(defaultMapColour) : Color.FromArgb(item.vs.MapColour);
        }
Ejemplo n.º 11
0
        public void RefreshHistory()
        {
            Stopwatch sw1 = new Stopwatch();

            //richTextBox_History.Clear();


            sw1.Start();


            TimeSpan maxDataAge = TimeSpan.Zero;
            int      atMost     = 0;

            switch (comboBoxHistoryWindow.SelectedIndex)
            {
            case 0:
                maxDataAge = new TimeSpan(6, 0, 0);     // 6 hours
                break;

            case 1:
                maxDataAge = new TimeSpan(12, 0, 0);     // 12 hours
                break;

            case 2:
                maxDataAge = new TimeSpan(24, 0, 0);     // 24 hours
                break;

            case 3:
                maxDataAge = new TimeSpan(3 * 24, 0, 0);     // 3 days
                break;

            case 4:
                maxDataAge = new TimeSpan(7 * 24, 0, 0);     // 1 week
                break;

            case 5:
                maxDataAge = new TimeSpan(14 * 24, 0, 0);     // 2 weeks
                break;

            case 6:
                maxDataAge = new TimeSpan(30, 0, 0, 0);     // 30 days (month)
                break;

            case 7:
                atMost = 20;     // Last 20
                break;

            case 8:
                maxDataAge = new TimeSpan(100000, 24, 0, 0);     // all
                break;

            default:
                maxDataAge = new TimeSpan(7 * 24, 0, 0);     // 1 week (default)
                break;
            }


            if (visitedSystems == null || visitedSystems.Count == 0)
            {
                GetVisitedSystems();
            }

            if (visitedSystems == null)
            {
                return;
            }

            List <SystemPosition> result;

            if (atMost > 0)
            {
                result = visitedSystems.OrderByDescending(s => s.time).Take(atMost).ToList();
            }
            else
            {
                var oldestData = DateTime.Now.Subtract(maxDataAge);
                result = (from systems in visitedSystems where systems.time > oldestData orderby systems.time descending select systems).ToList();
            }

            dataGridViewTravel.Rows.Clear();

            System.Diagnostics.Trace.WriteLine("SW1: " + (sw1.ElapsedMilliseconds / 1000.0).ToString("0.000"));

            for (int ii = 0; ii < result.Count; ii++) //foreach (var item in result)
            {
                SystemPosition item = result[ii];
                SystemPosition item2;

                if (ii < result.Count - 1)
                {
                    item2 = result[ii + 1];
                }
                else
                {
                    item2 = null;
                }

                AddHistoryRow(false, item, item2);
            }


            if (result.Count != visitedSystems.Count)
            {
                // we didn't put all the systems in the history grid
                // make sure that the LastKnown system is properly loaded if it's not visible so trilateration can find it...
                var lastKnown = (from systems
                                 in visitedSystems
                                 where systems.curSystem != null && systems.curSystem.HasCoordinate
                                 orderby systems.time descending
                                 select systems.curSystem).FirstOrDefault();
                if (lastKnown == null)
                {
                    for (int ii = visitedSystems.Count - 1; ii > 0; ii--)
                    {
                        SystemClass sys = SystemData.GetSystem(visitedSystems[ii].Name);
                        if (visitedSystems[ii].curSystem == null && sys != null)
                        {
                            visitedSystems[ii].curSystem = sys;
                            if (sys.HasCoordinate)
                            {
                                break;
                            }
                        }
                    }
                }
            }

            System.Diagnostics.Trace.WriteLine("SW2: " + (sw1.ElapsedMilliseconds / 1000.0).ToString("0.000"));

            if (dataGridViewTravel.Rows.Count > 0)
            {
                ShowSystemInformation((SystemPosition)(dataGridViewTravel.Rows[0].Cells[1].Tag));
            }
            System.Diagnostics.Trace.WriteLine("SW3: " + (sw1.ElapsedMilliseconds / 1000.0).ToString("0.000"));
            sw1.Stop();

            if (textBoxFilter.TextLength > 0)
            {
                FilterGridView();
            }
        }
Ejemplo n.º 12
0
        private void AddHistoryRow(bool insert, SystemPosition item, SystemPosition item2)
        {
            SystemClass sys1 = null, sys2;
            double      dist;

            sys1 = SystemData.GetSystem(item.Name);
            if (sys1 == null)
            {
                sys1 = new SystemClass(item.Name);
                if (SQLiteDBClass.globalSystemNotes.ContainsKey(sys1.SearchName))
                {
                    sys1.Note = SQLiteDBClass.globalSystemNotes[sys1.SearchName].Note;
                }
            }
            if (item2 != null)
            {
                sys2 = SystemData.GetSystem(item2.Name);
                if (sys2 == null)
                {
                    sys2 = new SystemClass(item2.Name);
                }
            }
            else
            {
                sys2 = null;
            }

            item.curSystem  = sys1;
            item.prevSystem = sys2;


            string diststr = "";

            dist = 0;
            if (sys2 != null)
            {
                if (sys1.HasCoordinate && sys2.HasCoordinate)
                {
                    dist = SystemData.Distance(sys1, sys2);
                }
                else
                {
                    dist = DistanceClass.Distance(sys1, sys2);
                }

                if (dist > 0)
                {
                    diststr = dist.ToString("0.00");
                }
            }

            item.strDistance = diststr;

            //richTextBox_History.AppendText(item.time + " " + item.Name + Environment.NewLine);

            object[] rowobj = { item.time, item.Name, diststr, item.curSystem.Note };
            int      rownr;

            if (insert)
            {
                dataGridView1.Rows.Insert(0, rowobj);
                rownr = 0;
            }
            else
            {
                dataGridView1.Rows.Add(rowobj);
                rownr = dataGridView1.Rows.Count - 1;
            }

            var cell = dataGridView1.Rows[rownr].Cells[1];

            cell.Tag = item;

            if (!sys1.HasCoordinate)      // Mark all systems without coordinates
            {
                cell.Style.ForeColor = Color.Blue;
            }
        }
Ejemplo n.º 13
0
        private void SyncAllEDSMSystems()
        {
            try
            {
                EDDBClass eddb = new EDDBClass();
                EDSMClass edsm = new EDSMClass();

                string edsmsystems   = Path.Combine(Tools.GetAppDataDirectory(), "edsmsystems.json");
                bool   newfile       = false;
                string rwsysfiletime = "2014-01-01 00:00:00";
                LogText("Get systems from EDSM." + Environment.NewLine);

                eddb.DownloadFile("http://www.edsm.net/dump/systemsWithCoordinates.json", edsmsystems, out newfile);

                if (newfile)
                {
                    LogText("Adding EDSM systems." + Environment.NewLine);
                    _db.GetAllSystems();
                    string             json    = LoadJsonFile(edsmsystems);
                    List <SystemClass> systems = SystemClass.ParseEDSM(json, ref rwsysfiletime);


                    List <SystemClass> systems2Store = new List <SystemClass>();

                    foreach (SystemClass system in systems)
                    {
                        // Check if sys exists first
                        SystemClass sys = SystemData.GetSystem(system.name);
                        if (sys == null)
                        {
                            systems2Store.Add(system);
                        }
                        else if (!sys.name.Equals(system.name) || sys.x != system.x || sys.y != system.y || sys.z != system.z)  // Case or position changed
                        {
                            systems2Store.Add(system);
                        }
                    }
                    SystemClass.Store(systems2Store);
                    systems.Clear();
                    systems = null;
                    systems2Store.Clear();
                    systems2Store = null;
                    json          = null;

                    _db.PutSettingString("EDSMLastSystems", rwsysfiletime);
                    _db.GetAllSystems();
                }
                else
                {
                    LogText("No new file." + Environment.NewLine);
                }

                string retstr = edsm.GetNewSystems(_db);
                Invoke((MethodInvoker) delegate
                {
                    TravelHistoryControl.LogText(retstr);
                });

                GC.Collect();
            }
            catch (Exception ex)
            {
                Invoke((MethodInvoker) delegate
                {
                    TravelHistoryControl.LogText("GetAllEDSMSystems exception:" + ex.Message + Environment.NewLine);
                });
            }
        }
Ejemplo n.º 14
0
        private void RunTrilaterationWorker()
        {
            var systemsEntries = new Dictionary <SystemClass, Trilateration.Entry>();

            for (int i = 0, count = dataGridViewDistances.Rows.Count - 1; i < count; i++)
            {
                var systemCell   = dataGridViewDistances[0, i];
                var distanceCell = dataGridViewDistances[1, i];

                if (systemCell.Tag == null || distanceCell.Value == null)
                {
                    continue;
                }

                var system = (SystemClass)systemCell.Tag;
                if (system != null && system.HasCoordinate)
                {
                    var culture  = new CultureInfo("en-US");
                    var distance = double.Parse(distanceCell.Value.ToString().Replace(",", "."), culture);

                    var entry = new Trilateration.Entry(system.x, system.y, system.z, distance);

                    systemsEntries.Add(system, entry);
                }
            }

            if (systemsEntries.Count < 3)
            {
                return;
            }

            Invoke((MethodInvoker) delegate
            {
                LogText("Starting trilateration..." + Environment.NewLine);
                labelStatus.Text      = "Calculating…";
                labelStatus.BackColor = Color.Gold;
            });

            var trilateration = new Trilateration {
                Logger = Console.WriteLine
            };

            foreach (var item in systemsEntries)
            {
                trilateration.AddEntry(item.Value);
            }

            //var trilaterationResultCS = trilateration.RunCSharp();
            var trilaterationAlgorithm = radioButtonAlgorithmJs.Checked
                ? Trilateration.Algorithm.RedWizzard_Emulated
                : Trilateration.Algorithm.RedWizzard_Native;

            var stopwatch = new Stopwatch();

            stopwatch.Start();

            var trilaterationResult = trilateration.Run(trilaterationAlgorithm);

            stopwatch.Stop();
            var spentTimeString = (stopwatch.ElapsedMilliseconds / 1000.0).ToString("0.0000") + "ms";

            lastTrilatelationResult  = trilaterationResult;
            lastTrilatelationEntries = systemsEntries;

            if (trilaterationResult.State == Trilateration.ResultState.Exact)
            {
                Invoke((MethodInvoker) delegate
                {
                    SystemClass s1, s2, s3;

                    s1 = SystemData.GetSystem("Sol");
                    s2 = SystemData.GetSystem("Sagittarius A*");
                    s3 = new SystemClass();

                    s3.x = trilaterationResult.Coordinate.X;
                    s3.y = trilaterationResult.Coordinate.Y;
                    s3.z = trilaterationResult.Coordinate.Z;

                    LogText("Trilateration successful (" + spentTimeString + "), exact coordinates found." + Environment.NewLine);
                    LogText("x=" + trilaterationResult.Coordinate.X + ", y=" + trilaterationResult.Coordinate.Y + ", z=" + trilaterationResult.Coordinate.Z + " Sol: " + SystemData.Distance(s1, s3).ToString("0.0") + " Sag A* " + SystemData.Distance(s2, s3).ToString("0.0") + Environment.NewLine);
                    labelStatus.Text      = "Success, coordinates found!";
                    labelStatus.BackColor = Color.LawnGreen;
                });
            }
            else if (trilaterationResult.State == Trilateration.ResultState.NotExact || trilaterationResult.State == Trilateration.ResultState.MultipleSolutions)
            {
                Invoke((MethodInvoker) delegate
                {
                    LogText("Trilateration not successful (" + spentTimeString + "), only approximate coordinates found." + Environment.NewLine);
                    //LogText("x=" + trilaterationResult.Coordinate.X + ", y=" + trilaterationResult.Coordinate.Y + ", z=" + trilaterationResult.Coordinate.Z + Environment.NewLine);
                    LogText("Enter more distances." + Environment.NewLine);
                    labelStatus.Text      = "Enter More Distances";
                    labelStatus.BackColor = Color.Orange;
                });
            }
            else if (trilaterationResult.State == Trilateration.ResultState.NeedMoreDistances)
            {
                Invoke((MethodInvoker) delegate
                {
                    LogText("Trilateration not successful (" + spentTimeString + "), coordinates not found." + Environment.NewLine);
                    LogText("Enter more distances." + Environment.NewLine);
                    labelStatus.Text      = "Enter More Distances";
                    labelStatus.BackColor = Color.Red;
                    ClearCalculatedDataGridViewDistancesRows();
                });
            }

            // update trilaterated coordinates
            if (trilaterationResult.Coordinate != null)
            {
                Invoke((MethodInvoker) delegate
                {
                    textBoxCoordinateX.Text = trilaterationResult.Coordinate.X.ToString();
                    textBoxCoordinateY.Text = trilaterationResult.Coordinate.Y.ToString();
                    textBoxCoordinateZ.Text = trilaterationResult.Coordinate.Z.ToString();
                    if (TargetSystem != null)
                    {
                        TargetSystem.x = trilaterationResult.Coordinate.X;
                        TargetSystem.y = trilaterationResult.Coordinate.Y;
                        TargetSystem.z = trilaterationResult.Coordinate.Z;
                    }
                    toolStripButtonMap.Enabled = (TargetSystem != null);
                });


                var suggestedSystems = GetListOfSuggestedSystems(trilaterationResult.Coordinate.X,
                                                                 trilaterationResult.Coordinate.Y,
                                                                 trilaterationResult.Coordinate.Z, 16);

                Invoke((MethodInvoker)(() => PopulateSuggestedSystems(suggestedSystems)));
            }
            else
            {
                Invoke((MethodInvoker) delegate
                {
                    textBoxCoordinateX.Text = "?";
                    textBoxCoordinateY.Text = "?";
                    textBoxCoordinateZ.Text = "?";
                });
            }

            //var hasInvalidDistances = false;

            // update dataGrid with calculated distances and status
            var entriesDistances = trilaterationResult.EntriesDistances;

            for (int i = 0, count = dataGridViewDistances.Rows.Count - 1; i < count; i++)
            {
                var systemCell             = dataGridViewDistances[0, i];
                var calculatedDistanceCell = dataGridViewDistances[2, i];
                var statusCell             = dataGridViewDistances[3, i];

                var system = (SystemClass)systemCell.Tag;

                if (system == null)
                {
                    continue;
                }

                if (system.HasCoordinate)
                {
                    calculatedDistanceCell.Value = null;
                    statusCell.Value             = null;
                }
                if (entriesDistances == null || systemCell.Value == null || systemCell.Tag == null)
                {
                    continue;
                }

                if (!systemsEntries.ContainsKey(system)) // calculated without this system, so skip the row
                {
                    continue;
                }

                var systemEntry        = systemsEntries[system];
                var calculatedDistance = entriesDistances[systemEntry];

                calculatedDistanceCell.Value = calculatedDistance.ToString();

                if (systemEntry.Distance == calculatedDistance)
                {
                    calculatedDistanceCell.Style.ForeColor = Color.Green;
                    statusCell.Value           = "OK";
                    statusCell.Style.ForeColor = Color.Green;
                }
                else
                {
                    //hasInvalidDistances = true;
                    calculatedDistanceCell.Style.ForeColor = Color.Salmon;
                    statusCell.Value           = "Wrong distance?";
                    statusCell.Style.ForeColor = Color.Salmon;
                }
            }
        }
Ejemplo n.º 15
0
        private void RouteIterative(string fromsys, bool usingcoordsfrom, Point3D coordsfrom,
                                    string tosys, bool usingcoordsto, Point3D coordsto,
                                    float maxrange, int routemethod)
        {
            double traveldistance = Point3D.DistanceBetween(coordsfrom, coordsto);      // its based on a percentage of the traveldistance

            routeSystems = new List <SystemClass>();
            routeSystems.Add(SystemData.GetSystem(textBox_From.Text));

            AppendText("Searching route from " + fromsys + " to " + tosys + " using " + metric_options[routemethod] + " metric" + Environment.NewLine);
            AppendText("Total distance: " + traveldistance.ToString("0.00") + " in " + maxrange.ToString("0.00") + "ly jumps" + Environment.NewLine);

            AppendText(Environment.NewLine + string.Format("{0,-40}    Depart          @ {1,9:0.00},{2,8:0.00},{3,9:0.00}" + Environment.NewLine, fromsys, coordsfrom.X, coordsfrom.Y, coordsfrom.Z));

            Point3D curpos         = coordsfrom;
            int     jump           = 1;
            double  actualdistance = 0;

#if DEBUG
            Console.WriteLine("-------------------------- BEGIN");
#endif
            do
            {
                double distancetogo = Point3D.DistanceBetween(coordsto, curpos);      // to go

                if (distancetogo <= maxrange)                                         // within distance, we can go directly
                {
                    break;
                }

                Point3D travelvector      = new Point3D(coordsto.X - curpos.X, coordsto.Y - curpos.Y, coordsto.Z - curpos.Z);                         // vector to destination
                Point3D travelvectorperly = new Point3D(travelvector.X / distancetogo, travelvector.Y / distancetogo, travelvector.Z / distancetogo); // per ly travel vector

                Point3D nextpos = new Point3D(curpos.X + maxrange * travelvectorperly.X,
                                              curpos.Y + maxrange * travelvectorperly.Y,
                                              curpos.Z + maxrange * travelvectorperly.Z);   // where we would like to be..

#if DEBUG
                Console.WriteLine("Curpos " + curpos.X + "," + curpos.Y + "," + curpos.Z);
                Console.WriteLine(" next" + nextpos.X + "," + nextpos.Y + "," + nextpos.Z);
#endif
                SystemClass bestsystem;
                Point3D     bestposition;

                FindBestSystem(curpos, nextpos, maxrange, maxrange - 0.5, routemethod, out bestsystem, out bestposition);
                string sysname           = "WAYPOINT";
                double deltafromwaypoint = 0;
                double deviation         = 0;

                if (bestsystem != null)
                {
                    deltafromwaypoint = Point3D.DistanceBetween(bestposition, nextpos);     // how much in error
                    deviation         = Point3D.DistanceBetween(curpos.InterceptPoint(nextpos, bestposition), bestposition);
                    nextpos           = bestposition;
                    sysname           = bestsystem.name;
                    routeSystems.Add(bestsystem);
                }

                AppendText(string.Format("{0,-40}{1,3} Dist:{2,8:0.00}ly @ {3,9:0.00},{4,8:0.00},{5,9:0.00} WPd:{6,8:0.00}ly Dev:{7,8:0.00}ly" + Environment.NewLine,
                                         sysname, jump, Point3D.DistanceBetween(curpos, nextpos), nextpos.X, nextpos.Y, nextpos.Z, deltafromwaypoint, deviation));

                actualdistance += Point3D.DistanceBetween(curpos, nextpos);
                curpos          = nextpos;
                jump++;
            } while (true);
            routeSystems.Add(SystemData.GetSystem(textBox_To.Text));
            actualdistance += Point3D.DistanceBetween(curpos, coordsto);
            AppendText(string.Format("{0,-40}{1,3} Dist:{2,8:0.00}ly @ {3,9:0.00},{4,8:0.00},{5,9:0.00}" + Environment.NewLine, tosys, jump, Point3D.DistanceBetween(curpos, coordsto), coordsto.X, coordsto.Y, coordsto.Z));
            AppendText(string.Format(Environment.NewLine + "Straight Line Distance {0,8:0.00}ly vs Travelled Distance {1,8:0.00}ly" + Environment.NewLine, traveldistance, actualdistance));
        }