如何在C#中将结构数组转换为Point3D对象数组?

时间:2022-05-31 21:21:50

I intend to form 3D mesh object. The mesh object has an 3D point array of approx. 50.000 items. Due to the number of 3D points, the array must be initialized on the heap.

我打算形成3D网格物体。网格对象具有大约的3D点阵列。 50.000项。由于3D点的数量,必须在堆上初始化该数组。

The required code is, shortly, as follows:

不久,所需的代码如下:

class MyMesh
{
    public MeshGeometry3D Mesh3D  // Properties tanimlaniyor
    {
        get { return GetMesh3D(); }
    }

    public struct mystruct
    {
        public int m_i;
        public int m_j;
        public int m_k;

        public mystruct(int i, int j, int k)
        {
            m_i = i;
            m_j = j;
            m_i = k;
        }
    }

    private mystruct[] mypts = 
    {
        new mystruct(20 , 7 , 7),   
        .
        .
        new mystruct(23 , 5 , 7)     
    };
}

Could you explain me how 3D Coordinates in mystruct above can be converted into 3D coordinates of a System.Windows.Media.Media3D.Point3D structure.

你能解释一下上面mystruct中的3D坐标如何转换成System.Windows.Media.Media3D.Point3D结构的3D坐标。

Thanks in advance.

提前致谢。

Öner YILMAZ

3 个解决方案

#1


If you have an actual list of 50,000 mystruct objects, would it be better to just create them as Point3D structs in the first place?

如果你有一个50,000个mystruct对象的实际列表,那么首先将它们创建为Point3D结构会更好吗?

Simply do a "Find & Replace" of:

只需做一个“查找和替换”:

new mystruct(

and replace it with

并替换它

new Point3D(

Then, change:

private mystruct[] mypts = 

to:

private Point3D[] mypts =

#2


Are you looking for something like this...

你在找这样的东西......

List<Point3D> points = mypts.Select<mystruct, Point3D> (x => 
                                      new Point3D(x.m_i, x.m_j, x.m_k))
                            .ToList();

Alternatively, you could expose an iterator that returned an IEnumerable like this...

或者,你可以暴露一个像这样返回IEnumerable的迭代器......

public IEnumerable<Point3D> Points()
{
    foreach(var point in mypts)
    {
        yield return new Point3D(point.m_i, point.m_j, point.m_k, );
    }
}

[add validation/error handling code as appropriate]

[根据需要添加验证/错误处理代码]

#3


If you need to retain your mystruct objects as well as enable Point3D functionality, you could use something like:

如果您需要保留mystruct对象以及启用Point3D功能,您可以使用以下内容:

class MyMesh {
    ...

    public Point3D[] ToPoint3D()
    {
        Point3D[] p3D = null;   // or set it to an Empty Point3D array, if necessary

        if (mpts.Length > 0)
        {
            p3D = new Point3D[mpts.Length]

            for (int x = 0; x < mypts.Length; x++)
            {
                p3D[x].X = new Point3D(mypts[x].m_i;
                p3D[x].Y = new Point3D(mypts[x].m_j;
                p3D[x].Z = new Point3D(mypts[x].m_k;
            }
        }

        return p3D;
    }

    ...
}

#1


If you have an actual list of 50,000 mystruct objects, would it be better to just create them as Point3D structs in the first place?

如果你有一个50,000个mystruct对象的实际列表,那么首先将它们创建为Point3D结构会更好吗?

Simply do a "Find & Replace" of:

只需做一个“查找和替换”:

new mystruct(

and replace it with

并替换它

new Point3D(

Then, change:

private mystruct[] mypts = 

to:

private Point3D[] mypts =

#2


Are you looking for something like this...

你在找这样的东西......

List<Point3D> points = mypts.Select<mystruct, Point3D> (x => 
                                      new Point3D(x.m_i, x.m_j, x.m_k))
                            .ToList();

Alternatively, you could expose an iterator that returned an IEnumerable like this...

或者,你可以暴露一个像这样返回IEnumerable的迭代器......

public IEnumerable<Point3D> Points()
{
    foreach(var point in mypts)
    {
        yield return new Point3D(point.m_i, point.m_j, point.m_k, );
    }
}

[add validation/error handling code as appropriate]

[根据需要添加验证/错误处理代码]

#3


If you need to retain your mystruct objects as well as enable Point3D functionality, you could use something like:

如果您需要保留mystruct对象以及启用Point3D功能,您可以使用以下内容:

class MyMesh {
    ...

    public Point3D[] ToPoint3D()
    {
        Point3D[] p3D = null;   // or set it to an Empty Point3D array, if necessary

        if (mpts.Length > 0)
        {
            p3D = new Point3D[mpts.Length]

            for (int x = 0; x < mypts.Length; x++)
            {
                p3D[x].X = new Point3D(mypts[x].m_i;
                p3D[x].Y = new Point3D(mypts[x].m_j;
                p3D[x].Z = new Point3D(mypts[x].m_k;
            }
        }

        return p3D;
    }

    ...
}