Plotting data given from a serial port in ILNumerics

687 views Asked by At

I am currently working dearlessly making a Win32 (.net) program that plots data coming from the serial port. I have been able to get the data from the serial port, and set up the ilnumerics plot cube, etc. But what I can't do is convert the (x,y,z) to one ILAray to plot points.

    public void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e) 
    {
        if (serialPort1.IsOpen == true)
        {
            Varriables.numberOfSerialBytes = serialPort1.BytesToRead;

            if (Varriables.numberOfSerialBytes >= Varriables.numberOfSerialBytesPerLine) //change 13 to number of bytes, changes for number of stars (stars x3 x BytesPerInt)
            {
                string serialText = serialPort1.ReadExisting();
                if (serialText != "") RecievedText(serialText);
                else { }
            }
        }
    }

    public void RecievedText(string text)
    {
        if ( richTextBox1.InvokeRequired)
        {
            SetTextCallBack x = new SetTextCallBack(RecievedText);
            this.Invoke(x, new object[] { text });
        }
        else
        {
            richTextBox1.Text = text;
            try { CalculatePoints(text); }
            catch { }
        }
    }

    public void CalculatePoints(string positionsString)
    {
        string[] starpositionStringList = positionsString.Split("   ".ToCharArray());
        List<float> starPositionFloatListX = new List<float>();
        List<float> starPositionFloatListY = new List<float>();
        List<float> starPositionFloatListZ = new List<float>();
        System.Console.WriteLine(starpositionStringList.Count());

        for (int i = 0; i < starpositionStringList.Count() / 3; i += 3)
        {
            int iy = i + 1;
            int iz = i + 2;

            float x = float.Parse(starpositionStringList[i]);
            float y = float.Parse(starpositionStringList[iy]);
            float z = float.Parse(starpositionStringList[iz]);

            starPositionFloatListX.Add(x);
            starPositionFloatListY.Add(y);
            starPositionFloatListZ.Add(z);
        }

        float[] xArray = starPositionFloatListX.ToArray<float>();
        float[] yArray = starPositionFloatListY.ToArray<float>();
        float[] zArray = starPositionFloatListZ.ToArray<float>();

        PlotPoints(xArray, yArray, zArray);
    }

    public void PlotPoints(float[] x, float[] y, float[] z)
    {
        //could send data through PlotPoints(x,y,z);
        //or make a long array using a for loop and send that
        //either way i still need to make an array to plot and use ilPlanel2.Scene = Scene to continue upadating the scene

        ILArray<float> starPositionsX = x;
        ILArray<float> starPositionsY = y;
        ILArray<float> starPositionsZ = z;
        ILArray<float> starPositions = ???????????????; //need to convert (x,y,z) to ILArray<float>

        ilPanel2.Scene.First<ILPlotCube>().Animations.AsParallel();

        if(Varriables.pointsPlotted == false)
        {
            //ilPanel2.Scene.First<ILPlotCube>(){ new ILPoints { Positions = starPositions, Colors  } };
            //Cube.Add(new ILPoints { Positions = starPositions });
            ilPanel2.Scene.First<ILPlotCube>().Add<ILPoints>(new ILPoints{ Positions = starPositions });

            Varriables.pointsPlotted = true;
        }

        else
        {
            ilPanel2.Scene.First<ILPoints>().Positions.Update(starPositions);
        }
        ilPanel2.Refresh();
    }

is there a IL.Math function that I can use to do this?

1

There are 1 answers

0
Haymo Kutschbach On BEST ANSWER

Create an ILArray of the needed size and use the incoming float[] System.Arrays directly in order to fill the values:

public void PlotPoints(float[] x, float[] y, float[] z) {

    using (ILScope.Enter()) {

        ILArray<float> starPositions = ILMath.zeros<float>(3, x.Length);
        starPositions["0;:"] = x;
        starPositions["1;:"] = y;
        starPositions["2;:"] = z;

        ilPanel1.Scene.First<ILPlotCube>().Add<ILPoints>(new ILPoints { Positions = starPositions });
        // (... or update accordingly, if the shape exists already)

        // at the end of all modifications, call Configure()

        ilPanel1.Scene.Configure(); 
        ilPanel1.Refresh();
    }
}

Don't forget to call Configure() on the modified shape (or any parent node on the path to the root) afterwards! Also, I usually wrap all ILArray related code in an using (ILScope.Enter()) { .. block. It helps to increase the performance for frequent updates significantly.