tags:

views:

150

answers:

3

Hi All,

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.

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.

Thanks in advance.

Öner YILMAZ

+2  A: 

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?

Simply do a "Find & Replace" of:

new mystruct(

and replace it with

new Point3D(

Then, change:

private mystruct[] mypts =

to:

private Point3D[] mypts =
John Rasch
A: 

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

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;
    }

    ...
}
Michael Todd
That would fail in p3D[x]=mypts[x] becaus you cannot assign your struct to a Point3D variable...
MartinStettner
Actually, I realized that about two minutes after I posted it. Hence the re-write.
Michael Todd
A: 

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...

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]

Martin Peck