public void CreateObservation()
        {
            float baseline              = 120;
            int   image_width           = 320;
            int   image_height          = 240;
            float FOV_degrees           = 68;
            int   no_of_stereo_features = 10;

            float[] stereo_features        = new float[no_of_stereo_features * 4];
            byte[]  stereo_features_colour = new byte[no_of_stereo_features * 3];
            bool    translate = false;

            for (int i = 0; i < no_of_stereo_features; i++)
            {
                stereo_features[i * 4]            = 1;
                stereo_features[i * 4 + 1]        = i * image_width / no_of_stereo_features;
                stereo_features[i * 4 + 2]        = image_height / 2;
                stereo_features[i * 4 + 3]        = 1;
                stereo_features_colour[i * 3]     = 200;
                stereo_features_colour[i * 3 + 1] = 200;
                stereo_features_colour[i * 3 + 2] = 200;
            }

            for (int rotation_degrees = 0; rotation_degrees < 360; rotation_degrees += 90)
            {
                stereoModel model    = new stereoModel();
                pos3D       observer = new pos3D(0, 0, 0);
                observer = observer.rotate(rotation_degrees / 180.0f * (float)Math.PI, 0, 0);
                List <evidenceRay> rays = model.createObservation(
                    observer,
                    baseline,
                    image_width,
                    image_height,
                    FOV_degrees,
                    stereo_features,
                    stereo_features_colour,
                    translate);

                float tx = float.MaxValue;
                float ty = float.MaxValue;
                float bx = float.MinValue;
                float by = float.MinValue;
                for (int i = 0; i < no_of_stereo_features; i++)
                {
                    //float pan_degrees = rays[i].pan_angle * 180 / (float)Math.PI;
                    //Console.WriteLine(pan_degrees.ToString());
                    for (int j = 0; j < rays[i].vertices.Length; j++)
                    {
                        Console.WriteLine("Vertex " + j.ToString());
                        Console.WriteLine("xyz: " + rays[i].vertices[j].x.ToString() + " " + rays[i].vertices[j].y.ToString() + " " + rays[i].vertices[j].z.ToString());

                        if (rays[i].vertices[j].x < tx)
                        {
                            tx = rays[i].vertices[j].x;
                        }
                        if (rays[i].vertices[j].x > bx)
                        {
                            bx = rays[i].vertices[j].x;
                        }

                        if (rays[i].vertices[j].y < ty)
                        {
                            ty = rays[i].vertices[j].y;
                        }
                        if (rays[i].vertices[j].y > by)
                        {
                            by = rays[i].vertices[j].y;
                        }
                    }
                }

                int    img_width = 640;
                Bitmap bmp       = new Bitmap(img_width, img_width, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
                byte[] img       = new byte[img_width * img_width * 3];
                for (int i = 0; i < img.Length; i++)
                {
                    img[i] = 255;
                }

                for (int i = 0; i < no_of_stereo_features; i++)
                {
                    int x0 = (int)((rays[i].vertices[0].x - tx) * img_width / (bx - tx));
                    int y0 = (int)((rays[i].vertices[0].y - ty) * img_width / (by - ty));
                    int x1 = (int)((rays[i].vertices[1].x - tx) * img_width / (bx - tx));
                    int y1 = (int)((rays[i].vertices[1].y - ty) * img_width / (by - ty));
                    drawing.drawLine(img, img_width, img_width, x0, y0, x1, y1, 0, 0, 0, 0, false);
                }

                BitmapArrayConversions.updatebitmap_unsafe(img, bmp);
                bmp.Save("dpslam_tests_createobservation_" + rotation_degrees.ToString() + ".bmp", System.Drawing.Imaging.ImageFormat.Bmp);
                Console.WriteLine("dpslam_tests_createobservation_" + rotation_degrees.ToString() + ".bmp");
            }
        }
        public void InsertRays()
        {
            int   no_of_stereo_features    = 2000;
            int   image_width              = 640;
            int   image_height             = 480;
            int   no_of_stereo_cameras     = 1;
            int   localisationRadius_mm    = 16000;
            int   maxMappingRange_mm       = 16000;
            int   cellSize_mm              = 32;
            int   dimension_cells          = 16000 / cellSize_mm;
            int   dimension_cells_vertical = dimension_cells / 2;
            float vacancyWeighting         = 0.5f;
            float FOV_horizontal           = 78 * (float)Math.PI / 180.0f;

            // create a grid
            Console.WriteLine("Creating grid");
            occupancygridSimple grid =
                new occupancygridSimple(
                    dimension_cells,
                    dimension_cells_vertical,
                    cellSize_mm,
                    localisationRadius_mm,
                    maxMappingRange_mm,
                    vacancyWeighting);

            Assert.AreNotEqual(grid, null, "object occupancygridSimple was not created");

            Console.WriteLine("Creating sensor models");
            stereoModel inverseSensorModel = new stereoModel();

            inverseSensorModel.FOV_horizontal = FOV_horizontal;
            inverseSensorModel.FOV_vertical   = FOV_horizontal * image_height / image_width;
            inverseSensorModel.createLookupTable(cellSize_mm, image_width, image_height);

            //Assert.AreNotEqual(0, inverseSensorModel.ray_model.probability[1][5], "Ray model probabilities not updated");

            // observer parameters
            int   pan_angle_degrees = 0;
            pos3D observer          = new pos3D(0, 0, 0);

            observer.pan = pan_angle_degrees * (float)Math.PI / 180.0f;
            float stereo_camera_baseline_mm = 100;
            pos3D left_camera_location      = new pos3D(stereo_camera_baseline_mm * 0.5f, 0, 0);
            pos3D right_camera_location     = new pos3D(-stereo_camera_baseline_mm * 0.5f, 0, 0);

            left_camera_location  = left_camera_location.rotate(observer.pan, observer.tilt, observer.roll);
            right_camera_location = right_camera_location.rotate(observer.pan, observer.tilt, observer.roll);
            left_camera_location  = left_camera_location.translate(observer.x, observer.y, observer.z);
            right_camera_location = right_camera_location.translate(observer.x, observer.y, observer.z);
            float FOV_degrees = 78;

            float[] stereo_features = new float[no_of_stereo_features * 3];
            byte[,] stereo_features_colour = new byte[no_of_stereo_features, 3];
            float[] stereo_features_uncertainties = new float[no_of_stereo_features];

            // create some stereo disparities within the field of view
            Console.WriteLine("Adding disparities");
            //MersenneTwister rnd = new MersenneTwister(0);
            Random rnd = new Random(0);

            for (int correspondence = 0; correspondence < no_of_stereo_features; correspondence++)
            {
                float x         = rnd.Next(image_width - 1);
                float y         = rnd.Next(image_height / 50) + (image_height / 2);
                float disparity = 7;
                if ((x < image_width / 5) || (x > image_width * 4 / 5))
                {
                    disparity = 7;                     //15;
                }
                byte colour_red   = (byte)rnd.Next(255);
                byte colour_green = (byte)rnd.Next(255);
                byte colour_blue  = (byte)rnd.Next(255);

                stereo_features[correspondence * 3]           = x;
                stereo_features[(correspondence * 3) + 1]     = y;
                stereo_features[(correspondence * 3) + 2]     = disparity;
                stereo_features_colour[correspondence, 0]     = colour_red;
                stereo_features_colour[correspondence, 1]     = colour_green;
                stereo_features_colour[correspondence, 2]     = colour_blue;
                stereo_features_uncertainties[correspondence] = 0;
            }

            // create an observation as a set of rays from the stereo correspondence results
            List <evidenceRay>[] stereo_rays = new List <evidenceRay> [no_of_stereo_cameras];
            for (int cam = 0; cam < no_of_stereo_cameras; cam++)
            {
                Console.WriteLine("Creating rays");
                stereo_rays[cam] =
                    inverseSensorModel.createObservation(
                        observer,
                        stereo_camera_baseline_mm,
                        image_width,
                        image_height,
                        FOV_degrees,
                        stereo_features,
                        stereo_features_colour,
                        stereo_features_uncertainties,
                        true);

                // insert rays into the grid
                Console.WriteLine("Throwing rays");
                for (int ray = 0; ray < stereo_rays[cam].Count; ray++)
                {
                    grid.Insert(stereo_rays[cam][ray], inverseSensorModel.ray_model, left_camera_location, right_camera_location, false);
                }
            }

            // save the result as an image
            Console.WriteLine("Saving grid");
            int debug_img_width  = 640;
            int debug_img_height = 480;

            byte[] debug_img = new byte[debug_img_width * debug_img_height * 3];
            Bitmap bmp       = new Bitmap(debug_img_width, debug_img_height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);

            grid.Show(debug_img, debug_img_width, debug_img_height, false, false);
            BitmapArrayConversions.updatebitmap_unsafe(debug_img, bmp);
            bmp.Save("tests_occupancygrid_simple_InsertRays_overhead.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

            grid.ShowFront(debug_img, debug_img_width, debug_img_height, true);
            BitmapArrayConversions.updatebitmap_unsafe(debug_img, bmp);
            bmp.Save("tests_occupancygrid_simple_InsertRays_front.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

            // side view of the probabilities
            float max_prob = -1;
            float min_prob = 1;

            float[] probs       = new float[dimension_cells / 2];
            float[] mean_colour = new float[3];
            for (int y = dimension_cells / 2; y < dimension_cells; y++)
            {
                float p = grid.GetProbability(dimension_cells / 2, y, mean_colour);
                probs[y - (dimension_cells / 2)] = p;
                if (p != occupancygridSimple.NO_OCCUPANCY_EVIDENCE)
                {
                    if (p < min_prob)
                    {
                        min_prob = p;
                    }
                    if (p > max_prob)
                    {
                        max_prob = p;
                    }
                }
            }
            for (int i = 0; i < debug_img.Length; i++)
            {
                debug_img[i] = 255;
            }
            int prev_x = -1;
            int prev_y = debug_img_height / 2;

            for (int i = 0; i < probs.Length; i++)
            {
                if (probs[i] != occupancygridSimple.NO_OCCUPANCY_EVIDENCE)
                {
                    int x = i * (debug_img_width - 1) / probs.Length;
                    int y = debug_img_height - 1 - (int)((probs[i] - min_prob) / (max_prob - min_prob) * (debug_img_height - 1));
                    int n = ((y * debug_img_width) + x) * 3;
                    if (prev_x > -1)
                    {
                        int r = 255;
                        int g = 0;
                        int b = 0;
                        if (probs[i] > 0.5f)
                        {
                            r = 0;
                            g = 255;
                            b = 0;
                        }
                        drawing.drawLine(debug_img, debug_img_width, debug_img_height, prev_x, prev_y, x, y, r, g, b, 0, false);
                    }
                    prev_x = x;
                    prev_y = y;
                }
            }
            int y_zero = debug_img_height - 1 - (int)((0.5f - min_prob) / (max_prob - min_prob) * (debug_img_height - 1));

            drawing.drawLine(debug_img, debug_img_width, debug_img_height, 0, y_zero, debug_img_width - 1, y_zero, 0, 0, 0, 0, false);

            BitmapArrayConversions.updatebitmap_unsafe(debug_img, bmp);
            bmp.Save("tests_occupancygrid_simple_InsertRays_probs.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
        }