How can I convert an array of structures to an array of Point3D objects in C #?
I intend to create a 3D mesh object. The mesh object has a 3D point array of approx. 50,000 units. Due to the number of 3D points, the array must be initialized on the heap.
The required code is, in short, the following:
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 please explain how one can convert the 3D coordinates in the mystruct
above to 3D structure coordinates System.Windows.Media.Media3D.Point3D
.
Thanks in advance.
Öner YILMAZ
0
Oner YILMAZ
a source
to share
3 answers
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 can expose the iterator that returned 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]
0
a source to share
If you need to keep hoax objects and also enable Point3D features, you can 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;
}
...
}
-1
a source to share