Esempio n. 1
0
        private void ellipseDetectionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if(ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {

                // load image
                imMat = FxMatrixF.Load(ofd.FileName);
                ieEllipseImage = new ImageElement(imMat, new ColorMap(ColorMapDefaults.Jet));
                ieEllipseImage.lockMoving = true;
                canvas_ellipse.AddElement(ieEllipseImage);

                // add plot element 
                gpeEllipseImage = new GeometryPlotElement();
                canvas_ellipse.AddElement(gpeEllipseImage);


                var contours = new FxContour(imMat<0.2f);

                WriteLine("Num Contours : " + contours.NumChains);

                int i = 0;
                float pe_pos_y = ieEllipseImage._Position.y;
                foreach (var cont in contours.ChainList)
                {
                    // draw the rectanges in main image
                    FxVector2f start = cont.RectStart ;
                    FxVector2f end = start + cont.RectSize;
                    FxMaths.Geometry.Rectangle r = new FxMaths.Geometry.Rectangle(start, end);
                    gpeEllipseImage.AddGeometry(r, false);


                    // draw the centroids 
                    FxVector2f cen = cont.GetCentroid();
                    var l = new FxMaths.Geometry.Line(cen - new FxVector2f(0, 2), cen + new FxVector2f(0, 2));
                    l.LineWidth = 0.5f;
                    gpeEllipseImage.AddGeometry(l, false);
                    l = new FxMaths.Geometry.Line(cen - new FxVector2f(2, 0), cen + new FxVector2f(2, 0));
                    l.LineWidth = 0.5f;
                    gpeEllipseImage.AddGeometry(l, false);


                    // add the numbering of the contours
                    var t = new TextElement(i.ToString());
                    t._Position = cen;
                    t.FontColor = SharpDX.Color.Green;
                    t._TextFormat.fontSize = 16.0f;
                    canvas_ellipse.AddElement(t);

                    // show the chain vector in plot
                    {
                        FxVectorF vec_i = new FxVectorF(cont.Count);
                        FxVectorF vec_r = new FxVectorF(cont.Count);
                        vec_i[0] = cont[0].i;
                        vec_r[0] = cont[0].r;
                        for (int j = 1; j < cont.Count; j++)
                        {
                            vec_i[j] = vec_i[j - 1] + cont[j].i;
                            vec_r[j] = vec_r[j - 1] + cont[j].r;
                        }

                        // show  the plot of this vector
                        var pe = new PloterElement(vec_i, PlotType.Lines, System.Drawing.Color.Blue);
                        pe._Position.x = ieEllipseImage._Position.x + ieEllipseImage.Size.x;
                        pe._Position.y = pe_pos_y;
                        pe.AddPlot(vec_r, PlotType.Lines, System.Drawing.Color.Red);
                        pe.CenterYOrigin();
                        pe.FitPlots();
                        canvas_ellipse.AddElement(pe);

                        // update the y of pe for the next one
                        pe_pos_y += pe.Size.y;

                        // debug the ellipse

                        for (int j = 0; j < cont.Count; j++)
                        {
                            imMat[(int)(vec_r[j] + cont.StartPoint.x), (int)(vec_i[j] + cont.StartPoint.y)] = 0.8f/contours.NumChains + 0.1f;
                        }
                        ieEllipseImage.UpdateInternalImage(imMat, new ColorMap(ColorMapDefaults.Jet));

                    }



                    i++;
                }

                canvas_ellipse.ReDraw();
            }
        }
Esempio n. 2
0
        private void DrawResults()
        {
            // draw points
            GeometryPlotElement plotElement = new GeometryPlotElement();
            foreach (FxVector2f v in sorted_Vertex)
            {
                Circle c = new Circle(v, 2);
                plotElement.AddGeometry(c, false);

                if (false)
                {
                    TextElement text = new TextElement(v.ToString("0.00"));
                    text.Position = v;
                    canvas1.AddElements(text, false);
                }
            }

            // draw regions
            for (int i = 0; i < regionDebugH.Length; i++)
            {
                RegionInfoDebug r = regionDebugH[i];

                Line l = new Line(new FxVector2f(r.start.x, 0), new FxVector2f(r.start.x, 2000));
                l.LineColor = SharpDX.Color.AntiqueWhite;
                l.UseDefaultColor = false;
                plotElement.AddGeometry(l, false);
                l = new Line(new FxVector2f(r.end.x, 2000), new FxVector2f(r.end.x, 0));
                l.LineColor = SharpDX.Color.AntiqueWhite;
                l.UseDefaultColor = false;
                plotElement.AddGeometry(l, false);

            }

            // copy back the results of d_regions
            regionInfo = d_regionInfo;

            for (int i = 0; i < regionInfo.Length; i++)
            {
                RegionInfoDebug r = regionDebugH[(int)Math.Floor((float)i / VerticalRegions)];
                RegionInfo ri = regionInfo[i];

                FxVector2f v_start = sorted_Vertex[ri.VertexOffset];
                FxVector2f v_end = sorted_Vertex[ri.VertexOffset + ri.VertexNum];

                Line l = new Line(new FxVector2f(r.start.x, v_start.Y), new FxVector2f(r.end.x, v_start.Y));
                l.LineColor = SharpDX.Color.GreenYellow;
                l.UseDefaultColor = false;
                plotElement.AddGeometry(l, false);

                l = new Line(new FxVector2f(r.start.x, v_end.Y), new FxVector2f(r.end.x, v_end.Y));
                l.LineColor = SharpDX.Color.HotPink;
                l.UseDefaultColor = false;
                plotElement.AddGeometry(l, false);

            }

            /////// Draw triangles
            threadInfo = d_threadInfo;
            csFace[] faceList = d_FaceList;
            csHalfEdge[] heList = d_HalfEdgeList;

            for (int t = 0; t < threadInfo.Length; t++)
            {
                csThreadInfo tInfo = threadInfo[t];

                for (int f = 0; f < tInfo.lastFaceID.x; f++)
                {
                    csFace face = faceList[f + tInfo.lastFaceID.y];

                    if (face.halfEdgeID < heList.Length)
                    {
                        csHalfEdge he1 = heList[face.halfEdgeID];
                        csHalfEdge he2 = heList[he1.nextEdgeID];
                        csHalfEdge he3 = heList[he2.nextEdgeID];

                        FxVector2f v1 = sorted_Vertex[he1.startVertexID];
                        FxVector2f v2 = sorted_Vertex[he2.startVertexID];
                        FxVector2f v3 = sorted_Vertex[he3.startVertexID];

                        Line l = new Line(v1, v2);
                        l.LineColor = SharpDX.Color.BlanchedAlmond;
                        l.UseDefaultColor = false;
                        l.LineWidth = 0.5f;
                        plotElement.AddGeometry(l, false);

                        l = new Line(v2, v3);
                        l.LineColor = SharpDX.Color.BlanchedAlmond;
                        l.UseDefaultColor = false;
                        l.LineWidth = 0.5f;
                        plotElement.AddGeometry(l, false);

                        l = new Line(v3, v1);
                        l.LineColor = SharpDX.Color.BlanchedAlmond;
                        l.UseDefaultColor = false;
                        l.LineWidth = 0.5f;
                        plotElement.AddGeometry(l, false);
                    }
                }
            }

            canvas1.AddElements(plotElement, false);
            canvas1.ReDraw();
        }
Esempio n. 3
0
        public void RunTheAlgorithm(Canvas canvas)
        {
            float time;
            TimeStatistics.StartClock();
            FXConstantBuffer<csMergeVThreadParam> cbMVTP;
            csMergeVThreadParam local_cbMVTP;

            FXConstantBuffer<csMergeHThreadParam> cbMHTP;
            csMergeHThreadParam local_cbMHTP;

            /////////////////////////////   Create regions

            WriteLine("============== Split =================");

            bitonicSort.Split(MaxPointsPerRegion);

            //time = TimeStatistics.ClockLap("Split regions");

            /////////////////////////////   Triangulate the regions

            #region Triangulation of the regions
            WriteLine("============== Triangulation =================");

            #region Exec

            #region Bind and update the threadParam constant buffer

            FXConstantBuffer<cbThreadParam> cbTP;
            cbThreadParam local_cbTP;

            // init the cb
            local_cbTP.maxFacesPerThread = (uint)maxFacesPerThread;
            local_cbTP.maxHalfEdgePerThread = (uint)maxHalfEdgePerThread;
            local_cbTP.maxBoundaryNodesPerThread = (uint)maxBoundaryNodesPerThread;
            local_cbTP.RegionsNum = (uint)NumRegions;

            /// Bind the constant buffer with local buffer
            cbTP = CSSubRegions.m_effect.GetConstantBufferByName<cbThreadParam>("threadParam");

            // update the value of cb
            cbTP.UpdateValue(local_cbTP);

            #endregion

            CSSubRegions.Execute(kenrelNum, 1);

            #endregion

            //time = TimeStatistics.ClockLap("Triangulate");
            #endregion

            /////////////////////////////   Merge Vertical

            #region Vertical Merging

            local_cbMVTP = new csMergeVThreadParam();
            local_cbMVTP.ThreadNum = (uint)mergeVthreadNum;
            local_cbMVTP.ThreadNumPerRow = (uint)(mergeVXthreadNum);
            local_cbMVTP.HorizontalThreadNum = (uint)(mergeVYthreadNum);
            local_cbMVTP.stackMaxSize = stackMaxSize;
            local_cbMVTP.depth = (uint)0;

            /// Bind the constant buffer with local buffer
            cbMVTP = CSVMerging.m_effect.GetConstantBufferByName<csMergeVThreadParam>("threadParam");

            // update the value of cb
            cbMVTP.UpdateValue(local_cbMVTP);

            WriteLine("============== Vertical Merging =================");

            int maxDepth = (int)Math.Ceiling(Math.Log(mergeVXthreadNum + 1, 2));

            WriteLine("maxDepth:" + maxDepth.ToString());

            for (int i = 0; i < maxDepth; i++)
            {
                // calc the number the number of the thread that we need for the merging
                int threadNumX = (int)Math.Ceiling((float)mergeVXthreadNum / Math.Pow(2, i + 1));
                int kernelNumX = (int)Math.Ceiling((float)threadNumX / MergeVXThread);

                // calc the number the number of the thread that we need for the merging
                int threadNumY = (int)Math.Ceiling((float)mergeVYthreadNum / Math.Pow(2, i + 1));
                int kernelNumY = (int)Math.Ceiling((float)threadNumY / MergeVYThread);

                //WriteLine("threadNumX:" + threadNumX.ToString() + "    kernelNumX:" + kernelNumX.ToString());
                //WriteLine("mergeVYkernelNum:" + mergeVYkernelNum.ToString() + "    mergeVXkernelNum:" + mergeVXkernelNum.ToString());

                local_cbMVTP.depth = (uint)i;

                // update the value of cb
                cbMVTP.UpdateValue(local_cbMVTP);

                #region Exec

                CSVMerging.Execute(kernelNumX, mergeVYkernelNum);

                #endregion

            }

            #endregion

            /////////////////////////////   Merge Horizontal

            #region Horizontal Merge

            WriteLine("============== Horizontal Merging =================");

            maxDepth = (int)Math.Ceiling(Math.Log(mergeHthreadNum + 1, 2));

            WriteLine("maxDepth:" + maxDepth.ToString());

            local_cbMHTP = new csMergeHThreadParam();
            local_cbMHTP.depth = (uint)0;
            local_cbMHTP.stackMaxSize = stackMaxSize;
            local_cbMHTP.ThreadNum = (uint)mergeHthreadNum;
            local_cbMHTP.ThreadNumPerRow = local_cbMVTP.ThreadNumPerRow;

            /// Bind the constant buffer with local buffer
            cbMHTP = CSHMerging.m_effect.GetConstantBufferByName<csMergeHThreadParam>("threadParam");

            // update the value of cb
            cbMHTP.UpdateValue(local_cbMHTP);

            for (int i = 0; i < maxDepth; i++)
            {
                // calc the number the number of the thread that we need for the merging
                int threadNum = (int)Math.Ceiling((float)mergeHthreadNum / Math.Pow(2, i + 1));
                int kernelNum = (int)Math.Ceiling((float)threadNum / MergeThread);

                //WriteLine("threadNum:" + threadNum.ToString() + "    kernelNum:" + kernelNum.ToString());

                local_cbMHTP.depth = (uint)i;

                // update the value of cb
                cbMHTP.UpdateValue(local_cbMHTP);

                #region Exec H

                CSHMerging.Execute(kernelNum, 1);

                #endregion

                //break;
            }
            #endregion

            /////////////////////////////  Read result  the regions

            #region Read the threadInfo buffer

            csThreadInfo[] csThreadListResult = new csThreadInfo[ThreadInfoListBuffer.Description.SizeInBytes / ThreadInfoListBuffer.Description.StructureByteStride];
            FXResourceVariable.ReadBuffer<csThreadInfo>(Engine.g_device, stagingThreadInfoListBuffer, ThreadInfoListBuffer, ref csThreadListResult);

            #endregion

            time = TimeStatistics.ClockLap("ExecFinish:");
            WriteLine("ExecFinish:" + time.ToString());

            #if true

            #region Read the threadInfo buffer

            IVertex<float>[] listPoints = new IVertex<float>[NumVertex];
            FXResourceVariable.ReadBufferVector<float>(Engine.g_device, stagingInputPointsBuffer, InputPoints, ref listPoints);

            #endregion

            #region Read the HalfEdge buffer

            csHalfEdge[] csHalfEdgeListResult = new csHalfEdge[HalfEdgeListBuffer.Description.SizeInBytes / HalfEdgeListBuffer.Description.StructureByteStride];
            FXResourceVariable.ReadBuffer<csHalfEdge>(Engine.g_device, stagingHalfEdgeListBuffer, HalfEdgeListBuffer, ref csHalfEdgeListResult);

            #endregion

            #region Read the boundary buffer

            csBoundaryNode[] csBoundaryListResult = new csBoundaryNode[BoundaryListBuffer.Description.SizeInBytes / BoundaryListBuffer.Description.StructureByteStride];
            FXResourceVariable.ReadBuffer<csBoundaryNode>(Engine.g_device, stagingBoundaryListBuffer, BoundaryListBuffer, ref csBoundaryListResult);

            #endregion

            #region Read the FaceList buffer

            csFace[] csFaceListResult = new csFace[maxFacesPerThread * NumRegions];
            FXResourceVariable.ReadBuffer<csFace>(Engine.g_device, stagingFaceListBuffer, FaceListBuffer, ref csFaceListResult);

            #endregion

            time = TimeStatistics.ClockLap("ExecFinish:");
            WriteLine("ExecFinish:" + time.ToString());

            #region show Result to 2d Canvas

            if (canvas != null)
            {
                Color[] lineColor = { Color.Aqua, Color.Beige, Color.Aqua, Color.Red, Color.Orchid };
                // pass all regions
                for (int i = 0; i < NumRegions; i++)
                {
                    //if (i < 0 * (local_cbMVTP.ThreadNumPerRow + 1))
                    //  continue;
                    //if (i > 10 * (local_cbMVTP.ThreadNumPerRow + 1))
                    //  continue;

                    // pass all the faces pre region
                    for (int j = maxFacesPerThread * i; j < maxFacesPerThread * (i + 1); j++)
                    {

                        csFace tmpFace = csFaceListResult[j];

                        if (tmpFace.halfEdgeID != uint.MaxValue)
                        {
                            GeometryPlotElement trianglesPlot = new GeometryPlotElement();
                            canvas.AddElement(trianglesPlot, false);
                            float lineWidth = 1.5f;

                            //csHalfEdge he = csHalfEdgeListResult[tmpFace.halfEdgeID + i * maxHalfEdgePerThread];
                            int he1ID = (int)tmpFace.halfEdgeID;
                            csHalfEdge he = csHalfEdgeListResult[he1ID];
                            int vert1ID = (int)he.startVertexID;
                            IVertex<float> vert1 = listPoints[vert1ID];

                            // move to next edge
                            int he2ID = (int)he.nextEdgeID;
                            he = csHalfEdgeListResult[he2ID];
                            int vert2ID = (int)he.startVertexID;
                            IVertex<float> vert2 = listPoints[vert2ID];

                            // move to next edgex
                            int he3ID = (int)he.nextEdgeID;
                            he = csHalfEdgeListResult[he3ID];
                            int vert3ID = (int)he.startVertexID;
                            IVertex<float> vert3 = listPoints[vert3ID];

                            FxMaths.Geometry.Line line = new FxMaths.Geometry.Line(vert1, vert2);
                            line.UseDefaultColor = false;
                            line.LineColor = lineColor[2];
                            line.LineWidth = lineWidth;
                            trianglesPlot.AddGeometry(line, false);

                            line = new FxMaths.Geometry.Line(vert2, vert3);
                            line.UseDefaultColor = false;
                            line.LineColor = lineColor[2];
                            line.LineWidth = lineWidth;
                            trianglesPlot.AddGeometry(line, false);

                            line = new FxMaths.Geometry.Line(vert3, vert1);
                            line.UseDefaultColor = false;
                            line.LineColor = lineColor[2];
                            line.LineWidth = lineWidth;
                            trianglesPlot.AddGeometry(line, false);

                            if (false)// || he1ID == 1755525 || he2ID == 1755525 || he3ID == 1755525)
                            {
                                FxMaths.GUI.TextElement text1;
                                float fontSize = 7;
                                FxVector2f tmp1, tmp2, tmp3;
                                tmp1 = vert1 as FxVector2f? ?? new FxVector2f(0, 0);
                                tmp2 = vert2 as FxVector2f? ?? new FxVector2f(0, 0);
                                tmp3 = vert3 as FxVector2f? ?? new FxVector2f(0, 0);

                                text1 = new TextElement(vert1ID.ToString() + "->" + tmp1.ToString("0.00"));
                                text1.Position = vert1 as FxVector2f? ?? new FxVector2f(0, 0);
                                text1._TextFormat.fontSize = fontSize;
                                canvas.AddElement(text1, false);

                                text1 = new TextElement(vert2ID.ToString() + "->" + tmp2.ToString("0.00"));
                                text1.Position = vert2 as FxVector2f? ?? new FxVector2f(0, 0);
                                text1._TextFormat.fontSize = fontSize;
                                canvas.AddElement(text1, false);

                                text1 = new TextElement(vert3ID.ToString() + "->" + tmp3.ToString("0.00"));
                                text1.Position = vert3 as FxVector2f? ?? new FxVector2f(0, 0);
                                text1._TextFormat.fontSize = fontSize;
                                canvas.AddElement(text1, false);

                                text1 = new TextElement(he1ID.ToString());
                                text1.FontColor = new Color4(Color.Brown.R, Color.Brown.G, Color.Brown.B, 1.0f);
                                text1._TextFormat.fontSize = fontSize;
                                text1.Position = (2 * tmp1 + 2 * tmp2 + tmp3) / 5.0f;
                                canvas.AddElement(text1, false);

                                text1 = new TextElement(he2ID.ToString());
                                text1.FontColor = new Color4(Color.Brown.R, Color.Brown.G, Color.Brown.B, 1.0f);
                                text1._TextFormat.fontSize = fontSize;
                                text1.Position = (tmp1 + 2 * tmp2 + 2 * tmp3) / 5.0f;
                                canvas.AddElement(text1, false);

                                text1 = new TextElement(he3ID.ToString());
                                text1.FontColor = new Color4(Color.Brown.R, Color.Brown.G, Color.Brown.B, 1.0f);
                                text1._TextFormat.fontSize = fontSize;
                                text1.Position = (2 * tmp1 + tmp2 + 2 * tmp3) / 5.0f;
                                canvas.AddElement(text1, false);

                                text1 = new TextElement(j.ToString());
                                text1.FontColor = new Color4(Color.Yellow.R, Color.Yellow.G, Color.Yellow.B, 1.0f);
                                text1._TextFormat.fontSize = fontSize;
                                text1.Position = (tmp1 + tmp2 + tmp3) / 3.0f;
                                canvas.AddElement(text1, false);
                            }

                        }
                    }

                }

                canvas.ReDraw();
            }

            #endregion

            #endif
        }
Esempio n. 4
0
        private void button6_Click( object sender, EventArgs e )
        {
            FxVectorF convResultFFT;

            // create a filter for testing
            FxVectorF filter = new FxVectorF( 1024 );
            filter[0] = 0.1f;
            filter[1] = 0.5f;
            filter[2] = 1.5f;
            filter[3] = 0.5f;
            filter[4] = 0.1f;
            TimeStatistics.StartClock();

            // execute the conv
            SignalTools.Convolution_FFT_F( signal, filter, out convResultFFT );
            //SignalTools.Convolution_DFT_F( signal, filter, out convResultDFT );

            TimeStatistics.StopClock( 1 );

            // create a plot base on signal
            PloterElement plot = new PloterElement( signal );
            plot.Position.X = 600;
            plot.Position.Y = 50;
            plot.Origin = new FxVector2f(10, 100);
            plot.FitPlots();

            // add the signal to the same plot
            plot.AddPlot( convResultFFT, PlotType.Lines, Color.RoyalBlue );
            //plot.AddPlot( convResultDFT, PlotType.Lines, Color.MistyRose );

            // add the signal to canva
            Signal_Canva.AddElements( plot );

            // add text for the signal plot
            TextElement text = new TextElement( "Conv Signal:" );
            text.Position.X = 610;
            text.Position.Y = 10;

            // add the text to canva
            Signal_Canva.AddElements( text );
        }
Esempio n. 5
0
        private void button5_Click( object sender, EventArgs e )
        {
            // calc the DFT of the signal
            //SignalTools.DFT_F( dftReal, dftImag, true, out re_signal, out dftImag );
            SignalTools.FFT_Safe_F( dftReal, dftImag, true, out re_signal, out dftImag );

            // create a plot base on signal
            PloterElement plot = new PloterElement( re_signal );
            plot.Position.X = 600;
            plot.Position.Y = 50;
            plot.Origin = new FxVector2f(10, 100);
            plot.FitPlots();

            // add the signal to canva
            Signal_Canva.AddElements( plot );

            // add text for the signal plot
            TextElement text = new TextElement( "Recostructed Signal:" );
            text.Position.X = 610;
            text.Position.Y = 10;

            // add the text to canva
            Signal_Canva.AddElements( text );
        }
Esempio n. 6
0
        private void button4_Click( object sender, EventArgs e )
        {
            // init the signal
            signal = new FxMaths.Vector.FxVectorF( 1024, new Fx1DGeneratorF( SignalGenerator ), 0.1f );

            // create a plot base on signal
            PloterElement plot = new PloterElement( signal );
            plot.Position.X = 0;
            plot.Position.Y = 50;
            plot.Origin = new FxVector2f(10, 100);
            plot.FitPlots();

            // add the signal to canva
            Signal_Canva.AddElements( plot );

            // add text for the signal plot
            TextElement text= new TextElement( "Signal Plot:" );
            text.Position.X = 10;
            text.Position.Y = 10;

            // add the text to canva
            Signal_Canva.AddElements( text );
        }
Esempio n. 7
0
        private void button3_Click( object sender, EventArgs e )
        {
            // calc the DFT of the signal
            //SignalTools.DFT_F( signal, new FxVectorF( signal.Size ), true, out dftReal, out dftImag );
            SignalTools.FFT_Safe_F( signal, new FxVectorF( signal.Size ), true, out dftReal, out dftImag );

            // create a plot base on real part of DFT
            PloterElement plotDFT_Real = new PloterElement( dftReal );
            plotDFT_Real.Position.X = 0;
            plotDFT_Real.Position.Y = 450;
            plotDFT_Real.Origin = new FxVector2f(10, 100);
            plotDFT_Real.FitPlots();

            // add the signal to canva
            Signal_Canva.AddElements( plotDFT_Real );

            // add text for the signal plot
            TextElement text= new TextElement( "FFT Real:" );
            text.Position.X = 10;
            text.Position.Y = 400;

            // add the text to canva
            Signal_Canva.AddElements( text );

            // ----------------------------------------------------------------------------------------------- //

            // create a plot base on  imag part of DFT
            PloterElement plotDFT_Imag = new PloterElement( dftImag );
            plotDFT_Imag.Position.X = 600;
            plotDFT_Imag.Position.Y = 450;
            plotDFT_Imag.Origin = new FxVector2f(10, 100);
            plotDFT_Imag.FitPlots();

            // add the signal to canva
            Signal_Canva.AddElements( plotDFT_Imag );

            // add text for the signal plot
            text = new TextElement( "FFT Imag:" );
            text.Position.X = 610;
            text.Position.Y = 400;

            // add the text to canva
            Signal_Canva.AddElements( text );
        }