Commits

Robin Harper committed bad93d0

updated some logic in the path tester to release resources sooner

Comments (0)

Files changed (5)

AstarPath/AstarPathfinder.cs

     public class AstarPathfinder
     {
         private const double Alpha = 0.4999;
-        private const double DefaultCost = 1.0;
-        private const int MaxEstimatedPathLength = 1500;
+        private const double DefaultCost = 4.5567;
+        private const int MaxEstimatedPathLength = 2000;
         private readonly int _directionalMove = 1;
         private const double Nudge = DefaultCost / MaxEstimatedPathLength;
         public readonly Grid Grid;
         /// <returns></returns>
         private double CalculateSimpleNudge(double h)
         {
-            return h *= 1.0 + Nudge;
+            return (1.0 + Nudge) * h;
         }
 
         /// <summary>

AstarPath/bin/Debug/AstarPath.dll

Binary file modified.

PathTester/MainWindow.xaml

                 <Label Content="Status:" VerticalAlignment="Center" FontWeight="Bold"/>
                 <Label Content="" Name="StatusLabel" VerticalAlignment="Center" FontWeight="Bold"/>
             </WrapPanel>
+            <WrapPanel>
+                <Label Content="Path Length:" VerticalAlignment="Center" FontWeight="Bold"/>
+                <Label Content="" Name="PathLength" VerticalAlignment="Center" FontWeight="Bold"/>
+            </WrapPanel>
+            <WrapPanel>
+                <Label Content="Grid Size:" VerticalAlignment="Center" FontWeight="Bold"/>
+                <Label Content="" Name="GridSize" VerticalAlignment="Center" FontWeight="Bold"/>
+            </WrapPanel>
         </Menu>
         <StackPanel Grid.Row="2" Margin="5" HorizontalAlignment="Stretch" Grid.Column="2">
             <Grid>

PathTester/MainWindow.xaml.cs

 using System.Windows;
 using System.Windows.Media;
 using System.Drawing;
-using System.Collections.Generic;
 using System.Windows.Controls;
 using System.Windows.Forms;
 using System.IO;
-using System.Xml.Linq;
-using System.Threading;
 using System.Windows.Shapes;
 using AstarPath;
 using System.Windows.Media.Imaging;
+using MessageBox = System.Windows.MessageBox;
 
 namespace PathTester
 {
         private void SetupBackground()
         {
             var bitmap = ConvertUriToBitmapImage(layoutFile);
-            LayoutCanvas.Background = new ImageBrush(bitmap);
-            LayoutCanvas.Width = bitmap.PixelWidth;
-            LayoutCanvas.Height = bitmap.PixelHeight;
 
-            byte[] rawBackground = File.ReadAllBytes(layoutFile);
+            if (bitmap != null)
+            {
+                LayoutCanvas.Background = new ImageBrush(bitmap);
+                LayoutCanvas.Width = bitmap.PixelWidth;
+                LayoutCanvas.Height = bitmap.PixelHeight;
 
-            var temporary = ConvertUriToBitmap(layoutFile);
-            _processedBitmap = PathfinderUtilities.CleanBitmap(temporary);
+                var temporary = ConvertUriToBitmap(layoutFile);
+                _processedBitmap = PathfinderUtilities.CleanBitmap(temporary);
+            }
         }
 
         private void RunBtn_Click(object sender, RoutedEventArgs e)
             }
             else
             {
-                System.Windows.MessageBox.Show("Choose a start and goal point");
+                MessageBox.Show("Choose a start and goal point");
             }
         }
 
                 }
 
                 StatusLabel.Content = "Path found!";
+                PathLength.Content = results.Count;
+                GridSize.Content = results[0].Size;
             }
             else
             {
                 StatusLabel.Content = "No path found!";
+                PathLength.Content = 0;
+                GridSize.Content = 0;
             }
         }
 
             return;
         }
 
-        private System.Windows.Controls.TextBlock CreateText(GridNode point, string text)
-        {
-            var tb = new System.Windows.Controls.TextBlock();
-            tb.SetValue(Canvas.LeftProperty, (double)point.PixelX);
-            tb.SetValue(Canvas.TopProperty, (double)point.PixelY);
-            tb.Text = text; 
-
-            return tb;
-        }
-
         private System.Windows.Shapes.Rectangle CreateRectangle(GridNode point, System.Windows.Media.Color color)
         {
             var rect = new System.Windows.Shapes.Rectangle();
 
         public static Bitmap ConvertUriToBitmap(string imagePath)
         {
-            if (File.Exists(imagePath))
+            var fi = new FileInfo(imagePath);
+
+            if (!fi.Exists || !fi.Extension.Contains("bmp"))
             {
-                var bitmapSource = ConvertUriToBitmapImage(imagePath);
+                return null;
+            }
+            var bitmapSource = ConvertUriToBitmapImage(imagePath);
 
-                using (var outStream = new MemoryStream())
-                {
-                    var enc = new BmpBitmapEncoder();
-                    enc.Frames.Add(BitmapFrame.Create(bitmapSource));
-                    enc.Save(outStream);
+            using (var outStream = new MemoryStream())
+            {
+                var enc = new BmpBitmapEncoder();
+                enc.Frames.Add(BitmapFrame.Create(bitmapSource));
+                enc.Save(outStream);
 
-                    return new Bitmap(outStream);
-                }
-            }
-            else
-            {
-                throw new FileNotFoundException();
+                return new Bitmap(outStream);
             }
         }
 
         public static BitmapImage ConvertUriToBitmapImage(string pathToBitmap)
         {
-            if (File.Exists(pathToBitmap))
+            try
             {
+                var fi = new FileInfo(pathToBitmap);
+
+                if (!fi.Exists || !fi.Extension.Contains("bmp"))
+                {
+                    return null;
+                }
                 var bitmapSource = new BitmapImage();
                 bitmapSource.BeginInit();
                 bitmapSource.CreateOptions = BitmapCreateOptions.PreservePixelFormat;
 
                 return bitmapSource;
             }
-            else
+            catch (Exception ex)
             {
-                throw new FileNotFoundException();
+                MessageBox.Show(ex.ToString());
             }
+            return null;
         }
     }
 }

PathTester/bin/Debug/AstarPath.dll

Binary file modified.