/// <summary> /// Convert a Nucleus mesh to a WPF MeshGeometry3D /// </summary> /// <param name="mesh"></param> /// <returns></returns> public static Media.Media3D.MeshGeometry3D Convert(Mesh mesh) { var result = new Media.Media3D.MeshGeometry3D(); mesh.AssignVertexNumbers(); var positions = new System.Windows.Media.Media3D.Point3DCollection(); foreach (Vertex v in mesh.Vertices) { positions.Add(Convert(v)); } result.Positions = positions; var indices = new Media.Int32Collection(); foreach (MeshFace face in mesh.Faces) { // Triangulate face: for (int i = 0; i < face.Count - 2; i++) { indices.Add(face[0].Number); indices.Add(face[i + 1].Number); indices.Add(face[i + 2].Number); } } result.TriangleIndices = indices; return(result); }
private void CreateModel() { var points = new Point3DCollection(); var edges = new Int32Collection(); var triangles = new Int32Collection(); switch (CurrentModelType) { case ModelTypes.StellatedOctahedron: case ModelTypes.Tetrahedron: points.Add(+1, +1, +1); points.Add(-1, -1, 1); points.Add(-1, +1, -1); points.Add(+1, -1, -1); edges.Add(0, 1, 1, 2, 2, 0, 0, 3, 1, 3, 2, 3); triangles.Add(0, 1, 2, 0, 3, 1, 1, 3, 2, 2, 3, 0); break; } switch (CurrentModelType) { case ModelTypes.StellatedOctahedron: // http://en.wikipedia.org/wiki/Compound_of_two_tetrahedra points.Add(-1, +1, +1); points.Add(1, -1, 1); points.Add(1, +1, -1); points.Add(-1, -1, -1); edges.Add(4, 5, 5, 6, 6, 4, 4, 7, 5, 7, 6, 7); triangles.Add(4, 5, 6, 4, 7, 5, 5, 7, 6, 6, 7, 4); break; } var m = new Model3DGroup(); // Add the nodes var gm = new MeshBuilder(); foreach (var p in points) { gm.AddSphere(p, 0.1); } m.Children.Add(new GeometryModel3D(gm.ToMesh(), Materials.Gold)); // Add the triangles var tm = new MeshBuilder(); for (int i = 0; i < triangles.Count; i += 3) { tm.AddTriangle(points[triangles[i]], points[triangles[i + 1]], points[triangles[i + 2]]); } m.Children.Add(new GeometryModel3D(tm.ToMesh(), Materials.Red) { BackMaterial = Materials.Blue }); // Add the edges var em = new MeshBuilder(); for (int i = 0; i < edges.Count; i += 2) { em.AddCylinder(points[edges[i]], points[edges[i + 1]], 0.08, 10); } m.Children.Add(new GeometryModel3D(em.ToMesh(), Materials.Gray)); Model = m; }
public static Int32Collection CombineIndexCollection(Int32Collection initialIndices, List<int> addingIndices, int offset) { var ret = new Int32Collection(initialIndices.Count + addingIndices.Count); foreach (var origIndex in initialIndices) { ret.Add(origIndex); } foreach (var origIndex in addingIndices) { ret.Add(origIndex + offset); } return ret; }
protected override void Triangulate(DependencyPropertyChangedEventArgs args, Point3DCollection vertices, Vector3DCollection normals, Int32Collection indices, PointCollection textures) { vertices.Clear(); normals.Clear(); indices.Clear(); textures.Clear(); MeshGeometry3D mesh = MeshGenerator.Geometry; foreach (Point3D vertex in mesh.Positions) vertices.Add(vertex); foreach (Vector3D normal in mesh.Normals) normals.Add(normal); foreach (int index in mesh.TriangleIndices) indices.Add(index); foreach (Point texture in mesh.TextureCoordinates) textures.Add(texture); }
public void updateMesh( Point3DCollection verticies, Vector3DCollection normals) { Int32Collection triangleIndices = new Int32Collection(); for (int i = 0; i < verticies.Count/3; i++) { triangleIndices.Add(i*3); triangleIndices.Add(i*3+1); triangleIndices.Add(i*3+2); } this.mesh.TriangleIndices = triangleIndices; this.mesh.Positions = verticies; this.mesh.Normals = normals; this.InvalidateVisual(); }
protected override void CalculateGeometry() { int numberOfSeparators = 4 * n + 4; points = new Point3DCollection(numberOfSeparators + 1); triangleIndices = new Int32Collection((numberOfSeparators + 1) * 3); points.Add(new Point3D(0, 0, 0)); for (int divider = 0; divider < numberOfSeparators; divider++) { double alpha = Math.PI / 2 / (n + 1) * divider; points.Add(new Point3D(r * Math.Cos(alpha), 0, -1 * r * Math.Sin(alpha))); triangleIndices.Add(0); triangleIndices.Add(divider + 1); triangleIndices.Add((divider == (numberOfSeparators - 1)) ? 1 : (divider + 2)); } }
/// <summary> /// This method returns a 3D representation of this area /// </summary> /// <param name="nodesDict">List of all the nodes on the map</param> /// <param name="map">bounds of the map</param> /// <param name="brush">Color of this area</param> /// <returns>ModelUIElement3D of this area</returns> public virtual ModelUIElement3D get3DSurface(Dictionary<long, OsmSharp.Osm.Node> nodesDict, Map map, System.Windows.Media.SolidColorBrush brush) { List<PointF> ptlist = getScaledPointsSurface(nodesDict, map); // Divide the polygons in triangles, this is code (and these two classes) are from: https://polygontriangulation.codeplex.com/ PolygonData poly = new PolygonData(ptlist); List<PointF[]> triangles = Triangulation2D.Triangulate(poly); // Surrounding tags of the mesh ModelUIElement3D model = new ModelUIElement3D(); GeometryModel3D geometryModel = new GeometryModel3D(); // Mesh and his his properties MeshGeometry3D mesh = new MeshGeometry3D(); DiffuseMaterial material = new DiffuseMaterial((System.Windows.Media.Brush)brush); Point3DCollection positions = new Point3DCollection(); Int32Collection indices = new Int32Collection(); // Add points and indices to their collection foreach (PointF[] points in triangles) { foreach (PointF point in points) { positions.Add(new Point3D(point.X, point.Y, height)); } int count = positions.Count; indices.Add(count - 3); indices.Add(count - 2); indices.Add(count - 1); } // Add these collections to the mesh mesh.Positions = positions; mesh.TriangleIndices = indices; // Set the color of front and back of the triangle geometryModel.Material = material; geometryModel.BackMaterial = material; // Add the mesh to the model geometryModel.Geometry = mesh; model.Model = geometryModel; return model; }
private void Display() { viewport.Children.Remove(modViz); var CVPoints = new Point3DCollection(); foreach (var chV in convexHullVertices) { CVPoints.Add(chV.Center); } var faceTris = new Int32Collection(); foreach (var f in faces) { // The vertices are stored in clockwise order. faceTris.Add(convexHullVertices.IndexOf(f.Vertices[0])); faceTris.Add(convexHullVertices.IndexOf(f.Vertices[1])); faceTris.Add(convexHullVertices.IndexOf(f.Vertices[2])); } var mg3d = new MeshGeometry3D { Positions = CVPoints, TriangleIndices = faceTris }; var material = new MaterialGroup { Children = new MaterialCollection { new DiffuseMaterial(Brushes.Red), new SpecularMaterial(Brushes.Beige, 2.0) } }; var geoMod = new GeometryModel3D { Geometry = mg3d, Material = material }; modViz = new ModelVisual3D { Content = geoMod }; viewport.Children.Add(modViz); }
public static void AddGeometry(this ModelVisual3D visual, GeometryModel3D geometry) { if (visual.Content == null) visual.Content = geometry; else { if (visual.Content is Model3DGroup) { GeometryModel3D m3d = (GeometryModel3D)((Model3DGroup)visual.Content).Children.First(); MeshGeometry3D main = (MeshGeometry3D)(m3d.Geometry); MeshGeometry3D toAdd = (MeshGeometry3D)(geometry.Geometry); Point3DCollection pc = new Point3DCollection(main.Positions.Count + toAdd.Positions.Count); foreach (var pt in main.Positions) pc.Add(pt); foreach (var pt in toAdd.Positions) { pc.Add(geometry.Transform.Transform(pt)); } main.Positions = pc; Vector3DCollection vc = new Vector3DCollection(main.Normals.Count + toAdd.Normals.Count); foreach (var v in main.Normals) vc.Add(v); foreach (var norm in toAdd.Normals) vc.Add(norm); main.Normals = vc; int maxIndices = main.Positions.Count; //we need to increment all indices by this amount foreach (var i in toAdd.TriangleIndices) main.TriangleIndices.Add(i + maxIndices); Int32Collection tc = new Int32Collection(main.TriangleIndices.Count + toAdd.TriangleIndices.Count); foreach (var i in main.TriangleIndices) tc.Add(i); foreach (var i in toAdd.TriangleIndices) tc.Add(i + maxIndices); main.TriangleIndices = tc; } //it is not a group but now needs to be else { Model3DGroup m3dGroup = new Model3DGroup(); m3dGroup.Children.Add(visual.Content); m3dGroup.Children.Add(geometry); visual.Content = m3dGroup; } } }
public Int32Collection GetArrowIndices() { Int32Collection cArrowIndices = new Int32Collection(); for (int i = 0; i < number_of_segments; i++) { if (i < number_of_segments - 1) { cArrowIndices.Add(0); cArrowIndices.Add(i + 2); cArrowIndices.Add(i + 1); } else // last { cArrowIndices.Add(0); cArrowIndices.Add(1); cArrowIndices.Add(i + 1); } } // annulus for (int i = 0; i < number_of_segments; i++) { if (i < number_of_segments - 1) { CreateRectangle_CCW(cArrowIndices, i + 1, i + 2, i + 2 + number_of_segments, i + 1 + number_of_segments); } else // last { CreateRectangle_CCW(cArrowIndices, i + 1, 1, 1 + number_of_segments, i + 1 + number_of_segments); } } return cArrowIndices; }
private static void OnNumberOfSidesChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { WheelMesh wheel = (WheelMesh)d; Point3DCollection points = new Point3DCollection(3 + wheel.NumberOfSides - 2); Int32Collection triangles = new Int32Collection(wheel.NumberOfSides); // Center points.Add(new Point3D(wheel.Radius, wheel.Radius, wheel.Height)); // Top points.Add(new Point3D(wheel.Radius, 0, 0)); for (int i = 1; i < wheel.NumberOfSides; i++) { double beta = 2 * Math.PI / wheel.NumberOfSides * i; double alpha = (Math.PI - beta) / 2; double length = 2 * wheel.Radius * Math.Sin(beta / 2); double x = length * Math.Cos(Math.PI / 2 - alpha); double y = length * Math.Sin(Math.PI / 2 - alpha); points.Add(new Point3D(wheel.Radius + x, y, 0)); triangles.Add(0); triangles.Add(i); triangles.Add(i+1); } triangles.Add(0); triangles.Add(wheel.NumberOfSides); triangles.Add(1); wheel.SetValue(PointsProperty, points); wheel.SetValue(TriangleIndicesProperty, triangles); }
public static MeshGeometry3D CreateRectangle(double height, double width) { var vertices = new Point3DCollection(); var normals = new Vector3DCollection(); var facets = new Int32Collection(); var textureCoords = new PointCollection(); vertices.Add(new Point3D(-width / 2, 0, -height / 2)); vertices.Add(new Point3D(-width / 2, 0, height / 2)); vertices.Add(new Point3D(width / 2, 0, -height / 2)); vertices.Add(new Point3D(width / 2, 0, height / 2)); normals.Add(new Vector3D(0, -1, 0)); normals.Add(new Vector3D(0, -1, 0)); normals.Add(new Vector3D(0, -1, 0)); normals.Add(new Vector3D(0, -1, 0)); textureCoords.Add(new Point(1,1)); textureCoords.Add(new Point(1, 0)); textureCoords.Add(new Point(0,1)); textureCoords.Add(new Point(0,0)); facets.Add(0); facets.Add(1); facets.Add(2); facets.Add(3); facets.Add(2); facets.Add(1); var rectangle = new MeshGeometry3D(); rectangle.Positions = vertices; rectangle.Normals = normals; rectangle.TriangleIndices = facets; rectangle.TextureCoordinates = textureCoords; return rectangle; }
static Terrain3D() { { SX = new double[17]; SZ = new double[17]; int i = 0; for (double d = 0; d < 2; d += 0.125) { i++; SX[i] = Math.Cos(Math.PI * d); SZ[i] = Math.Sin(Math.PI * d); } } { TEXTURE_COORDINATES = new PointCollection(17); TEXTURE_COORDINATES.Add(new Point(0, 0)); Point p = new Point(1, 0); int i = -1; while (++i < 16) TEXTURE_COORDINATES.Add(p); TEXTURE_COORDINATES.Freeze(); } { TRIANGLE_INDICES = new Int32Collection(48); for (int i = 1; i < 16; i++) { TRIANGLE_INDICES.Add(0); TRIANGLE_INDICES.Add(i + 1); TRIANGLE_INDICES.Add(i); } TRIANGLE_INDICES.Add(0); TRIANGLE_INDICES.Add(1); TRIANGLE_INDICES.Add(16); TRIANGLE_INDICES.Freeze(); } }
protected override void ProcessFigure(CircularList<Point> list, Point3DCollection vertices, Vector3DCollection normals, Int32Collection indices, PointCollection textures) { int offset = vertices.Count; for (int i = 0; i <= list.Count; i++) { Point pt = list[i]; // Set vertices. vertices.Add(new Point3D(pt.X, pt.Y, 0)); vertices.Add(new Point3D(pt.X, pt.Y, -Depth)); // Set texture coordinates. textures.Add(new Point((double)i / list.Count, 0)); textures.Add(new Point((double)i / list.Count, 1)); // Set triangle indices. if (i < list.Count) { indices.Add(offset + i * 2 + 0); indices.Add(offset + i * 2 + 2); indices.Add(offset + i * 2 + 1); indices.Add(offset + i * 2 + 1); indices.Add(offset + i * 2 + 2); indices.Add(offset + i * 2 + 3); } } }
private GeometryModel3D Triangle(double x, double y, double s, SolidColorBrush color) { Point3DCollection corners = new Point3DCollection(); corners.Add(new Point3D(x, y, 0)); corners.Add(new Point3D(x, y + s, 0)); corners.Add(new Point3D(x + s, y + s, 0)); Int32Collection Triangles = new Int32Collection(); Triangles.Add(0); Triangles.Add(1); Triangles.Add(2); MeshGeometry3D tmesh = new MeshGeometry3D(); tmesh.Positions = corners; tmesh.TriangleIndices = Triangles; tmesh.Normals.Add(new Vector3D(0, 0, -1)); GeometryModel3D msheet = new GeometryModel3D(); msheet.Geometry = tmesh; msheet.Material = new DiffuseMaterial(color); return msheet; }
public TriangleMeshAdapater(MFnMesh mesh) { MIntArray indices = new MIntArray(); MIntArray triangleCounts = new MIntArray(); MPointArray points = new MPointArray(); mesh.getTriangles(triangleCounts, indices); mesh.getPoints(points); // Get the triangle indices Indices = new Int32Collection((int)indices.length); for (int i = 0; i < indices.length; ++i) Indices.Add(indices[i]); // Get the control points (vertices) Points = new Point3DCollection((int)points.length); for (int i = 0; i < (int)points.length; ++i) { MPoint pt = points[i]; Points.Add(new Point3D(pt.x, pt.y, pt.z)); } // Get the number of triangle faces and polygon faces Debug.Assert(indices.length % 3 == 0); int triFaces = (int)indices.length / 3; int polyFaces = mesh.numPolygons; // We have normals per polygon, we want one per triangle. Normals = new Vector3DCollection(triFaces); int nCurrentTriangle = 0; // Iterate over each polygon for (int i = 0; i < polyFaces; ++i) { // Get the polygon normal var maya_normal = new MVector(); mesh.getPolygonNormal((int)i, maya_normal); System.Windows.Media.Media3D.Vector3D normal = new System.Windows.Media.Media3D.Vector3D(maya_normal.x, maya_normal.y, maya_normal.z); // Iterate over each tri in the current polygon int nTrisAtFace = triangleCounts[i]; for (int j = 0; j < nTrisAtFace; ++j) { Debug.Assert(nCurrentTriangle < triFaces); Normals.Add(normal); nCurrentTriangle++; } } Debug.Assert(nCurrentTriangle == triFaces); }
public void CreateRectangle_CW(Int32Collection ArrowIndices, int i0, int i1, int i2, int i3) { ArrowIndices.Add(i0); ArrowIndices.Add(i2); ArrowIndices.Add(i1); ArrowIndices.Add(i0); ArrowIndices.Add(i3); ArrowIndices.Add(i2); }
/// <summary> /// Parse - returns an instance converted from the provided string /// using the current culture /// <param name="source"> string with Int32Collection data </param> /// </summary> public static Int32Collection Parse(string source) { IFormatProvider formatProvider = System.Windows.Markup.TypeConverterHelper.InvariantEnglishUS; TokenizerHelper th = new TokenizerHelper(source, formatProvider); Int32Collection resource = new Int32Collection(); int value; while (th.NextToken()) { value = Convert.ToInt32(th.GetCurrentToken(), formatProvider); resource.Add(value); } return(resource); }
/// <summary> /// This method returns a 3D representation of this building walls /// </summary> /// <param name="nodesDict">List of all the nodes on the map</param> /// <param name="map">bounds of the map</param> /// <param name="brush">Color of these walls</param> /// <returns>ModelUIElement3D of these walls</returns> public ModelUIElement3D get3DWalls(Dictionary<long, OsmSharp.Osm.Node> nodesDict, Map map, ImageBrush brush) { // Surrounding tags of the mesh ModelUIElement3D model = new ModelUIElement3D(); GeometryModel3D geometryModel = new GeometryModel3D(); // Mesh and his his properties MeshGeometry3D mesh = new MeshGeometry3D(); DiffuseMaterial material = new DiffuseMaterial((System.Windows.Media.Brush)brush); Point3DCollection positions = new Point3DCollection(); PointCollection texturePoints = new PointCollection(); Int32Collection indices = new Int32Collection(); // Add Points of surface and with add points of surface with height 0 positions = getScaledPositionsWall(nodesDict, map); // Add indices to the collection for (int i = 0; i < positions.Count - 2; i += 2) { indices.Add(i); indices.Add(i + 2); indices.Add(i + 1); indices.Add(i + 3); indices.Add(i + 1); indices.Add(i + 2); // Get the width and height of a wall float widthWall = (float)Math.Sqrt(Math.Pow(positions[i].X - positions[i + 2].X, 2) + Math.Pow(positions[i].Y - positions[i + 2].Y, 2)); int imageWidth = (int)(brush.ImageSource.Width * widthWall); int imageHeight = (int)(brush.ImageSource.Height * height); // Add texture coordinates texturePoints.Add(new System.Windows.Point(0, imageHeight)); texturePoints.Add(new System.Windows.Point(0, 0)); texturePoints.Add(new System.Windows.Point(imageWidth, imageHeight)); texturePoints.Add(new System.Windows.Point(imageWidth, 0)); } // Add these collections to the mesh mesh.Positions = positions; mesh.TriangleIndices = indices; mesh.TextureCoordinates = texturePoints; // Set the color of front and back of the triangle geometryModel.Material = material; geometryModel.BackMaterial = material; // Add the mesh to the model geometryModel.Geometry = mesh; model.Model = geometryModel; return model; }
public VideoSurface(string mediaSource) { this.ModelVisual3D = new ModelVisual3D(); var geometryModel = new GeometryModel3D(); this.ModelVisual3D.Content = geometryModel; this.geometry = new MeshGeometry3D(); geometryModel.Geometry = geometry; var positions = new Point3DCollection(); positions.Add(new Point3D(0, 0, 0)); positions.Add(new Point3D(640, 0, 0)); positions.Add(new Point3D(640, 480, 0)); positions.Add(new Point3D(0, 480, 0)); this.geometry.Positions = positions; var textureCoordinates = new PointCollection(); textureCoordinates.Add(new System.Windows.Point(0, 1)); textureCoordinates.Add(new System.Windows.Point(1, 1)); textureCoordinates.Add(new System.Windows.Point(1, 0)); textureCoordinates.Add(new System.Windows.Point(0, 0)); this.geometry.TextureCoordinates = textureCoordinates; var triangleIndices = new Int32Collection(); triangleIndices.Add(0); triangleIndices.Add(1); triangleIndices.Add(2); triangleIndices.Add(2); triangleIndices.Add(3); triangleIndices.Add(0); this.geometry.TriangleIndices = triangleIndices; var material = new EmissiveMaterial(); var brush = new VisualBrush(); this.border = new Border(); this.border.BorderBrush = Brushes.White; this.border.BorderThickness = new Thickness(10); this.border.Opacity = 0; this.mediaElement = new MediaElement(); mediaElement.LoadedBehavior = MediaState.Manual; mediaElement.Source = new Uri(mediaSource); this.border.Child = mediaElement; brush.Visual = border; material.Brush = brush; geometryModel.Material = material; this.mediaElement.MediaEnded += new RoutedEventHandler(mediaElement_MediaEnded); }
/// <summary> /// /// </summary> /// <param name="args"></param> /// <param name="vertices"></param> /// <param name="normals"></param> /// <param name="indices"></param> /// <param name="textures"></param> protected override void Triangulate( DependencyPropertyChangedEventArgs args, Point3DCollection vertices, Vector3DCollection normals, Int32Collection indices, PointCollection textures) { // Clear all four collections. vertices.Clear(); normals.Clear(); indices.Clear(); textures.Clear(); // Fill the vertices, normals, and textures collections. for (int stack = 0; stack <= Stacks; stack++) { double y = Length - stack * Length / Stacks; for (int slice = 0; slice <= Slices; slice++) { double theta = slice * 2 * Math.PI / Slices; double x = -Radius * Math.Sin(theta); double z = -Radius * Math.Cos(theta); normals.Add(new Vector3D(x, 0, z)); vertices.Add(new Point3D(x, y, z)); textures.Add(new Point((double)slice / Slices, (double)stack / Stacks)); } } // Fill the indices collection. for (int stack = 0; stack < Stacks; stack++) { for (int slice = 0; slice < Slices; slice++) { indices.Add((stack + 0) * (Slices + 1) + slice); indices.Add((stack + 1) * (Slices + 1) + slice); indices.Add((stack + 0) * (Slices + 1) + slice + 1); indices.Add((stack + 0) * (Slices + 1) + slice + 1); indices.Add((stack + 1) * (Slices + 1) + slice); indices.Add((stack + 1) * (Slices + 1) + slice + 1); } } }
/// <summary> /// Creates the triangle indices. /// </summary> /// <param name="n"> /// The number of points. /// </param> /// <returns> /// The triangle indices. /// </returns> public Int32Collection CreateIndices(int n) { var indices = new Int32Collection(n * 6); for (int i = 0; i < n; i++) { indices.Add(i * 4 + 2); indices.Add(i * 4 + 1); indices.Add(i * 4 + 0); indices.Add(i * 4 + 2); indices.Add(i * 4 + 3); indices.Add(i * 4 + 1); } indices.Freeze(); return indices; }
public static void BuildMeshData(Func<int, int, double> data, int width, int height, out Point3DCollection points, out PointCollection textureCoordinates, out Int32Collection triangleIndices, double textureWidth = 1, double textureHeight = 1) { int pointCount = width * height; points = new Point3DCollection(pointCount); textureCoordinates = new PointCollection(pointCount); int triangleCount = 2 * (width - 1) * (height - 1); triangleIndices = new Int32Collection(3 * triangleCount); for (int iy = 0; iy < height; ++iy) { double yProportion = iy / (height - 1.0); double outY = 0.5 - yProportion; double textureY = textureHeight * yProportion; for (int ix = 0; ix < width; ++ix) { double xProportion = ix / (width - 1.0); double outX = xProportion - 0.5; double textureX = textureWidth * xProportion; points.Add(new Point3D(outX, outY, data(ix, iy))); textureCoordinates.Add(new Point(textureX, textureY)); if (ix < (width - 1) && iy < (height - 1)) { int topLeftIndex = ix + iy * width; int bottomLeftIndex = topLeftIndex + width; triangleIndices.Add(bottomLeftIndex); triangleIndices.Add(bottomLeftIndex + 1); triangleIndices.Add(topLeftIndex); triangleIndices.Add(bottomLeftIndex + 1); triangleIndices.Add(topLeftIndex + 1); triangleIndices.Add(topLeftIndex); } } } Debug.WriteLine("In MeshHelper"); }
/// <summary> /// Creates the triangle indices. /// </summary> /// <param name="n"> /// The number of points. /// </param> /// <returns> /// The triangle indices. /// </returns> public static Int32Collection CreateIndices(int n) { var indices = new Int32Collection(n * 6); for (int i = 0; i < n; i++) { // bottom right triangle indices.Add((i * 4) + 0); indices.Add((i * 4) + 1); indices.Add((i * 4) + 2); // top left triangle indices.Add((i * 4) + 2); indices.Add((i * 4) + 3); indices.Add((i * 4) + 0); } indices.Freeze(); return indices; }
/// <summary> /// 単純な四角形のジオメトリを作成します。 /// </summary> public static MeshGeometry3D CreateCellMesh(IEnumerable<Square> squares, double widen = 0.0) { var points = new Point3DCollection(); var indices = new Int32Collection(); var texCoords = new PointCollection(); foreach (var square in squares.Where(_ => _ != null)) { var x = Board.BoardSize - square.File; var y = square.Rank - 1; var c = points.Count; // 各マスの座標追加 points.Add(new Point3D(x + 0 - widen, y + 0 - widen, 0)); points.Add(new Point3D(x + 1 + widen, y + 0 - widen, 0)); points.Add(new Point3D(x + 0 - widen, y + 1 + widen, 0)); points.Add(new Point3D(x + 1 + widen, y + 1 + widen, 0)); // テクスチャ位置を追加 texCoords.Add(new Point(0, 0)); texCoords.Add(new Point(1, 0)); texCoords.Add(new Point(0, 1)); texCoords.Add(new Point(1, 1)); // 頂点追加 indices.Add(c + 0); indices.Add(c + 2); indices.Add(c + 1); indices.Add(c + 1); indices.Add(c + 2); indices.Add(c + 3); } return new MeshGeometry3D { Positions = points, TriangleIndices = indices, TextureCoordinates = texCoords, }; }
private static Int32Collection GetTriangleIndices(int xVertices, int yVertices) { Int32Collection indices = new Int32Collection(); for (int y = 0; y < yVertices - 1; y++) { for (int x = 0; x < xVertices - 1; x++) { int v1 = x + y*xVertices; int v2 = v1 + 1; int v3 = v1 + xVertices; int v4 = v3 + 1; indices.Add(v1); indices.Add(v3); indices.Add(v4); indices.Add(v1); indices.Add(v4); indices.Add(v2); } } return indices; }
private GeometryModel3D Triangle(double x , double y, double s) { //define the geometry as a set of points and order the points are connected up Point3DCollection corners = new Point3DCollection(); corners.Add(new Point3D(x, y, 0)); corners.Add(new Point3D(x, y + s, 0)); corners.Add(new Point3D(x + s, y + s, 0)); Int32Collection Triangles = new Int32Collection(); Triangles.Add(0); Triangles.Add(1); Triangles.Add(2); //After the geometry is defined MeshGeometry3D tmesh = new MeshGeometry3D(); tmesh.Positions = corners; tmesh.TriangleIndices = Triangles; //Defines the normal vector tmesh.Normals.Add(new Vector3D(0, 0, -1)); //Puts the geometry together with some material properties GeometryModel3D msheet = new GeometryModel3D(); msheet.Geometry = tmesh; return msheet; }
/// <summary> /// Triangulate a polygon using the cutting ears algorithm. /// </summary> /// <remarks> /// The algorithm does not support holes. /// </remarks> /// <param name="contour"> /// the polygon contour /// </param> /// <returns> /// collection of triangle points /// </returns> public static Int32Collection Triangulate(IList<Point> contour) { // allocate and initialize list of indices in polygon var result = new Int32Collection(); int n = contour.Count; if (n < 3) { return null; } var V = new int[n]; // we want a counter-clockwise polygon in V if (Area(contour) > 0) { for (int v = 0; v < n; v++) { V[v] = v; } } else { for (int v = 0; v < n; v++) { V[v] = (n - 1) - v; } } int nv = n; // remove nv-2 Vertices, creating 1 triangle every time int count = 2 * nv; // error detection for (int v = nv - 1; nv > 2;) { // if we loop, it is probably a non-simple polygon if (0 >= (count--)) { // ERROR - probable bad polygon! return null; } // three consecutive vertices in current polygon, <u,v,w> int u = v; if (nv <= u) { u = 0; // previous } v = u + 1; if (nv <= v) { v = 0; // new v } int w = v + 1; if (nv <= w) { w = 0; // next } if (Snip(contour, u, v, w, nv, V)) { int s, t; // true names of the vertices int a = V[u]; int b = V[v]; int c = V[w]; // output Triangle result.Add(a); result.Add(b); result.Add(c); // remove v from remaining polygon for (s = v, t = v + 1; t < nv; s++, t++) { V[s] = V[t]; } nv--; // resest error detection counter count = 2 * nv; } } return result; }
void UpdateMesh(FaceTrackFrame faceTrackingFrame) { EnumIndexableCollection<FeaturePoint, Vector3DF> shapePoints = faceTrackingFrame.Get3DShape(); EnumIndexableCollection<FeaturePoint, PointF> projectedShapePoints = faceTrackingFrame.GetProjected3DShape(); if (this.triangleIndices == null) { // Update stuff that doesn't change from frame to frame this.triangleIndices = faceTrackingFrame.GetTriangles(); var indices = new Int32Collection(this.triangleIndices.Length * 3); foreach (FaceTriangle triangle in this.triangleIndices) { indices.Add(triangle.Third); indices.Add(triangle.Second); indices.Add(triangle.First); } this.theGeometry.TriangleIndices = indices; this.theGeometry.Normals = null; // Let WPF3D calculate these. this.theGeometry.Positions = new Point3DCollection(shapePoints.Count); this.theGeometry.TextureCoordinates = new PointCollection(projectedShapePoints.Count); for (int pointIndex = 0; pointIndex < shapePoints.Count; pointIndex++) { this.theGeometry.Positions.Add(new Point3D()); this.theGeometry.TextureCoordinates.Add(new Point()); } } //Vector3DF point=shapePoints[3]; // Update the 3D model's vertices and texture coordinates for (int pointIndex = 0; pointIndex < shapePoints.Count; pointIndex++) { Vector3DF point = shapePoints[pointIndex]; this.theGeometry.Positions[pointIndex] = new Point3D(point.X, point.Y, -point.Z); PointF projected = projectedShapePoints[pointIndex]; data[dataindex, pointIndex, 0] = point.X; data[dataindex, pointIndex, 1] = point.Y; data[dataindex, pointIndex, 2] = point.Z; this.theGeometry.TextureCoordinates[pointIndex] = new Point( projected.X / (double)this.colorImageWritableBitmap.PixelWidth, projected.Y / (double)this.colorImageWritableBitmap.PixelHeight); } textBlock1.Text = data[dataindex, 4, 2].ToString(); if (data[dataindex, 4, 2] > 1.2 && data[dataindex, 4, 2] < 1.4) { dataindex++; //StreamWriter fw = File.AppendText("e:\\newnewstart4.txt"); //fw.Write("z=" + ); //fw.Close(); } }
/// <summary> /// Finds edges that are only connected to one triangle. /// </summary> /// <param name="mesh"> /// A mesh geometry. /// </param> /// <returns> /// The edge indices for the edges that are only used by one triangle. /// </returns> public static Int32Collection FindBorderEdges(MeshGeometry3D mesh) { var dict = new Dictionary<ulong, int>(); for (int i = 0; i < mesh.TriangleIndices.Count / 3; i++) { int i0 = i * 3; for (int j = 0; j < 3; j++) { int index0 = mesh.TriangleIndices[i0 + j]; int index1 = mesh.TriangleIndices[i0 + ((j + 1) % 3)]; int minIndex = Math.Min(index0, index1); int maxIndex = Math.Max(index1, index0); ulong key = CreateKey((uint)minIndex, (uint)maxIndex); if (dict.ContainsKey(key)) { dict[key] = dict[key] + 1; } else { dict.Add(key, 1); } } } var edges = new Int32Collection(); foreach (var kvp in dict) { // find edges only used by 1 triangle if (kvp.Value == 1) { uint i0, i1; ReverseKey(kvp.Key, out i0, out i1); edges.Add((int)i0); edges.Add((int)i1); } } return edges; }
/// <summary> /// Simplifies the specified mesh. /// </summary> /// <param name="mesh"> /// The mesh. /// </param> /// <param name="eps"> /// The tolerance. /// </param> /// <returns> /// A simplified mesh. /// </returns> public static MeshGeometry3D Simplify(MeshGeometry3D mesh, double eps) { // Find common positions var dict = new Dictionary<int, int>(); // map position index to first occurence of same position for (int i = 0; i < mesh.Positions.Count; i++) { for (int j = i + 1; j < mesh.Positions.Count; j++) { if (dict.ContainsKey(j)) { continue; } double l2 = (mesh.Positions[i] - mesh.Positions[j]).LengthSquared; if (l2 < eps) { dict.Add(j, i); } } } var p = new Point3DCollection(); var ti = new Int32Collection(); // create new positions array var newIndex = new Dictionary<int, int>(); // map old index to new index for (int i = 0; i < mesh.Positions.Count; i++) { if (!dict.ContainsKey(i)) { newIndex.Add(i, p.Count); p.Add(mesh.Positions[i]); } } // Update triangle indices foreach (int index in mesh.TriangleIndices) { int j; ti.Add(dict.TryGetValue(index, out j) ? newIndex[j] : newIndex[index]); } var result = new MeshGeometry3D { Positions = p, TriangleIndices = ti }; return result; }
/// <summary> /// Creates a new mesh where no vertices are shared. /// </summary> /// <param name="input"> /// The input mesh. /// </param> /// <returns> /// A new mesh. /// </returns> public static MeshGeometry3D NoSharedVertices(MeshGeometry3D input) { var p = new Point3DCollection(); var ti = new Int32Collection(); Vector3DCollection n = null; if (input.Normals != null) { n = new Vector3DCollection(); } PointCollection tc = null; if (input.TextureCoordinates != null) { tc = new PointCollection(); } for (int i = 0; i < input.TriangleIndices.Count; i += 3) { int i0 = i; int i1 = i + 1; int i2 = i + 2; int index0 = input.TriangleIndices[i0]; int index1 = input.TriangleIndices[i1]; int index2 = input.TriangleIndices[i2]; var p0 = input.Positions[index0]; var p1 = input.Positions[index1]; var p2 = input.Positions[index2]; p.Add(p0); p.Add(p1); p.Add(p2); ti.Add(i0); ti.Add(i1); ti.Add(i2); if (n != null) { n.Add(input.Normals[index0]); n.Add(input.Normals[index1]); n.Add(input.Normals[index2]); } if (tc != null) { tc.Add(input.TextureCoordinates[index0]); tc.Add(input.TextureCoordinates[index1]); tc.Add(input.TextureCoordinates[index2]); } } return new MeshGeometry3D { Positions = p, TriangleIndices = ti, Normals = n, TextureCoordinates = tc }; }