Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在C#中将结构数组转换为Point3D对象数组?_C#_Struct - Fatal编程技术网

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

如何在C#中将结构数组转换为Point3D对象数组?,c#,struct,C#,Struct,我打算形成三维网格对象。 网格对象具有一个包含约50000个项目的三维点阵列。 由于3D点的数量,阵列必须在堆上初始化 简而言之,所需代码如下: class MyMesh { public MeshGeometry3D Mesh3D // Properties tanimlaniyor { get { return GetMesh3D(); } } public struct mystruct { public int

我打算形成三维网格对象。 网格对象具有一个包含约50000个项目的三维点阵列。 由于3D点的数量,阵列必须在堆上初始化

简而言之,所需代码如下:

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)     
    };
}
你能给我解释一下如何转换上面
mystruct
中的三维坐标吗 转换为
系统.Windows.Media.Media3D.Point3D结构的三维坐标

提前谢谢


Öner YILMAZ

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

只需“查找并替换”以下内容:

新mystruct(

并将其替换为

新Point3D(

然后,改变:

private mystruct[] mypts = 
致:


如果需要保留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;
    }

    ...
}
classmymesh{
...
公共点3D[]拓扑点3D()
{
Point3D[]p3D=null;//或根据需要将其设置为空的Point3D数组
如果(mpts.Length>0)
{
p3D=新点3D[mpts.Length]
对于(int x=0;x
你在找这样的东西吗

List<Point3D> points = mypts.Select<mystruct, Point3D> (x => 
                                      new Point3D(x.m_i, x.m_j, x.m_k))
                            .ToList();
public IEnumerable<Point3D> Points()
{
    foreach(var point in mypts)
    {
        yield return new Point3D(point.m_i, point.m_j, point.m_k, );
    }
}
List points=mypts.选择(x=>
新Point3D(x.m_i,x.m_j,x.m_k))
.ToList();
或者,您可以公开一个迭代器,该迭代器返回如下IEnumerable

List<Point3D> points = mypts.Select<mystruct, Point3D> (x => 
                                      new Point3D(x.m_i, x.m_j, x.m_k))
                            .ToList();
public IEnumerable<Point3D> Points()
{
    foreach(var point in mypts)
    {
        yield return new Point3D(point.m_i, point.m_j, point.m_k, );
    }
}
public IEnumerable Points()
{
foreach(mypts中的var点)
{
收益率返回新的Point3D(point.m_i,point.m_j,point.m_k,);
}
}

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

在p3D[x]=mypts[x]中会失败,因为您无法将结构分配给Point3D变量……事实上,我在发布它大约两分钟后意识到了这一点。因此重新编写。