Need to display a 3D cloud in a .NET window
#1
I have a VB.NET Windows Forms program which produces a 3D cloud of points. I need to add a window in the .NET Forms application to display these points, then I want to be able to rotate, pan and zoom that model (nothing fancy).  I do not need shading etc. Which package do I need to do this?

Is it possible to see sample code on:

1) Displaying a point cloud stored in an array as X, Y, Z coordinates. (There is no connectivity between those points).

2) C# sample code to load and display a .XYZ file from disk?

Thanks in advance.
#2
1)
The Ab3d.PowerToys library does not provide an efficient way to show point clouds. It would be possible to show each position in a point cloud as a 3D box or a 3D sphere but this would be very inefficient.

To show a point cloud in a much more efficient way you should use Ab3d.DXEngine library that can render may 3D positions are pixels with custom size and color. This is done by using a PixelVisual3D object that can render millions of 3D positions (on a decent graphics card). 

See an example:
[Image: https://www.ab4d.com/images/DXEngineGall...Engine.png]

You can also control the size and color of individual pixel:
[Image: https://www.ab4d.com/images/DXEngineGall...ptions.png]


What is more, there is also an OptimizedPointMesh class that can be used in case of even bigger points clouds. This class can optimized the number of pixels that are actually sent to the graphics card and can improve performance by reducing the number of rendered pixels:


[Image: https://www.ab4d.com/images/DXEngineGall...Engine.PNG]


Please check the Ab3d.DXEngine samples from more info - the samples can be get from GitHub: https://github.com/ab4d/Ab3d.DXEngine.Wpf.Samples


Note that for camera controls (rotation, zooming, etc.) you will also need a Ab3d.PowerToys library that provides camera and other helper functions.


Both Ab3d.PowerToys and Ab3d.DXEngine libraries are built for WPF applications. But you can also integrate both libraries into WinForms applications. See the following sample on how to do that: https://github.com/ab4d/Ab3d.DXEngine.WinForms.Sample


2)
I do not know the exact structure of the .xyz file format, but if this is a text file where each 3D position is defined in one row, then you can use the following code:

Code:
// Reads file with a 3D positions in each line. The x, y and z values are separated by space, comma or semi-column.
// Lines with comments should start by hash ('#')
private List<Point3D> ReadXyzFile(string fileName)
{
    var allLines = System.IO.File.ReadAllLines(fileName);

    var splitChars = new char[] { ' ', ',', ';' };

    var allPositions = new List<Point3D>(allLines.Length); // preallocate list to number of lines so the list does not need to be resized when elements are added to it

    foreach (var oneLine in allLines)
    {
        var trimmedLine = oneLine.Trim();

        if (trimmedLine.Length == 0 || trimmedLine[0] == '#') // skip empty lines and comments
            continue;

        var lineParts = trimmedLine.Split(splitChars);

        if (lineParts.Length != 3) // Invalid number of line parts
            continue;

        var x = float.Parse(lineParts[0], NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture);
        var y = float.Parse(lineParts[1], NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture);
        var z = float.Parse(lineParts[2], NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture);

        allPositions.Add(new Point3D(x, y, z));
    }

    return allPositions;
}

For example, this can read the following file:
Code:
#  Corners of a tetrahedron
#
0.0  0.0  0.0
1.0  0.0  0.0
0.0  1.0  0.0
0.0  0.0  1.0
Andrej Benedik
#3
Thank you Andrej.

I read your code for the readXyzFile function.    If I use this function within PowerToys, how can I display the array allPositions

Do I still need the Ab3d.DXEngine library?  I am asking because my point clouds are relatively small (< 100,000 points).
#4
As I have written the Ab3d.PowerToys and WPF 3D do not support rendering individual positions. So you need to create an individual 3D object for each position - for example a BoxVisual3D or SphereVisual3D. With SphereVisual3D you can set a number of segments to a low number.

But I think that if you need to render 100.000 objects that this will be very slow. 

I would advise you to test that option and then also check how is the performance with using Ab3d.DXEngine. Then you will be able to decide what to use.
Andrej Benedik
#5
I have tried to run the DXEngine examples (PictureBoxSample) but it crashes at the statement with the error below:

            _dxScene = _dxDevice.CreateDXSceneWithSwapChain(pictureBox1.Handle, pictureBox1.Width, pictureBox1.Height, preferedMultisampleCount: 4);



A first chance exception of type 'SharpDX.SharpDXException' occurred in SharpDX.dll

A first chance exception of type 'System.NullReferenceException' occurred in Ab3d.DXEngine.WinForms.Sample.exe




Thanks in advance.
#6
I cannot reproduce this exception.

Are you running the sample in a virtual machine that does not support hardware accelerated DirectX 11?
If you want to support that case, then add try ... catch around the CreateDXSceneWithSwapChain call and in case when _dxDevice is null or in case of an exception set the dxDeviceConfiguration.DriverType to DriverType.Software and then create the DXDevice again and call the CreateDXSceneWithSwapChain method; but this will work much slower.

If there is some other reason for the exception, please provide more information about the exception (full call stack) and check the values of parameters (is any of them null?).
Andrej Benedik
#7
Thank you.

I downloaded a fresh copy of the sample and this time used VS2019 and it worked.

I then converted the sample to VB.NET as I know it better.  I am getting 3 compile errors (same error).  Please see attached.

Can you help me with the correct syntax.

Thanks in advance.


Attached Files Thumbnail(s)
   
  


Forum Jump:


Users browsing this thread:
1 Guest(s)