1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
| public static Geometry Create2DGeometryFromModel3D(Model3D model3D, Matrix3D transform2D, GeometryModel3D geom=null, MeshGeometry3D mesh=null, Point3D[] points=null, Int32[] indicies=null)
{
if (model3D is GeometryModel3D)
{
if (mesh != null)
{
Matrix3D screenTransform = multiplyMatrix3D(geom.Transform, transform2D);
screenTransform.Transform(points);
if (mesh.TriangleIndices == null || mesh.TriangleIndices.Count == 0)
{
// Combined all triangle into one geometry (union)
Geometry g1 = new StreamGeometry();
for (int i = 0; i < points.Length; i += 3)
{
var g2 = new StreamGeometry();
using (StreamGeometryContext ctx = g2.Open())
{
ctx.BeginFigure(new Point(points[i].X, points[i].Y), true, true);
ctx.LineTo(new Point(points[i + 1].X, points[i + 1].Y), true, false);
ctx.LineTo(new Point(points[i + 2].X, points[i + 2].Y), true, false);
}
g2.Freeze();
g1 = Geometry.Combine(g1, g2, GeometryCombineMode.Union, null);
}
g1.Freeze();
return g1;
}
else
{
// Combined all triangle into one geometry (union)
Geometry g1 = new StreamGeometry();
for (int i = 0; i < indicies.Length; i += 3)
{
var g2 = new StreamGeometry();
using (StreamGeometryContext ctx = g2.Open())
{
ctx.BeginFigure(new Point(points[indicies[i]].X, points[indicies[i]].Y), true, true);
ctx.LineTo(new Point(points[indicies[i + 1]].X, points[indicies[i + 1]].Y), true, false);
ctx.LineTo(new Point(points[indicies[i + 2]].X, points[indicies[i + 2]].Y), true, false);
}
g2.Freeze();
g1 = Geometry.Combine(g1, g2, GeometryCombineMode.Union, null);
}
g1.Freeze();
return g1;
}
}
}
else if (model3D is Model3DGroup)
{
ObservableCollection<Model3D> listChildModel3D = new ObservableCollection<Model3D>();
var group = (Model3DGroup)model3D;
GeometryGroup g1 = new GeometryGroup();
foreach (Model3D childModel3D in group.Children)
{
listChildModel3D.Add(childModel3D);
}
List<Task> tasks = new List<Task>();
for (int i = 0; i < listChildModel3D.Count; i++)
{
//Closure :changement d'indice important
int j = i;
var t = Task.Factory.StartNew(() =>
{
// 4 Invoke Dispatcher UI: pour obtenir une Instance d'un objet UI....
GeometryModel3D geom1 =
(GeometryModel3D)Application.Current.Dispatcher.Invoke(
(Func<GeometryModel3D>)(() =>
{
return (GeometryModel3D)listChildModel3D[j];
}));
MeshGeometry3D mesh1 = (MeshGeometry3D)Application.Current.Dispatcher.Invoke(
(Func<MeshGeometry3D>)(() =>
{
return geom1.Geometry as MeshGeometry3D;
}));
if (mesh1!=null)
{
Point3D[] points1 = (Point3D[])Application.Current.Dispatcher.Invoke(
(Func<Point3D[]>)(() =>
{
Point3D[] positions = new Point3D[mesh1.Positions.Count];
mesh1.Positions.CopyTo(positions, 0);
return positions;
}));
Geometry g2 = Create2DGeometryFromModel3D(listChildModel3D[j], multiplyMatrix3D(model3D.Transform, transform2D), geom1, mesh1, points1, null);
g1.Children.Add(g2);
}
else
{
Int32[] indicies1 = (Int32[])Application.Current.Dispatcher.Invoke(
(Func<Int32[]>)(() =>
{
Int32[] table = new Int32[mesh.TriangleIndices.Count];
mesh.TriangleIndices.CopyTo(table, 0);
return table;
}));
Geometry g2 = Create2DGeometryFromModel3D(listChildModel3D[j], multiplyMatrix3D(model3D.Transform, transform2D), geom1, mesh1, null, indicies1);
g1.Children.Add(g2);
}
});
tasks.Add(t);
}
//Invoke Dispatcher UI when all tasks are terminated
Task.Factory.ContinueWhenAll(tasks.ToArray(),
result =>
{
g1.Freeze();
return (Geometry)g1;
}
);
}
return new StreamGeometry();
} |
Partager