Exemple #1
0
        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);
                }
            }
        }
Exemple #2
0
 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();
       }
 }
Exemple #3
0
        /// <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);
        }
Exemple #4
0
        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;
        }
Exemple #5
0
		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);
		}
        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);
        }
Exemple #7
0
        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();

            // Convert TextGeometry to series of closed polylines.
            PathGeometry path =
                TextGeometry.GetFlattenedPathGeometry(0.001,
                                                ToleranceType.Relative);

            foreach (PathFigure fig in path.Figures)
            {
                list.Clear();
                list.Add(fig.StartPoint);

                foreach (PathSegment seg in fig.Segments)
                {
                    if (seg is LineSegment)
                    {
                        LineSegment lineseg = seg as LineSegment;
                        list.Add(lineseg.Point);
                    }

                    else if (seg is PolyLineSegment)
                    {
                        PolyLineSegment polyline = seg as PolyLineSegment;

                        for (int i = 0; i < polyline.Points.Count; i++)
                            list.Add(polyline.Points[i]);
                    }
                }

                // Figure is complete. Post-processing follows.
                if (list.Count > 0)
                {
                    // Remove last point if it's the same as the first.
                    if (list[0] == list[list.Count - 1])
                        list.RemoveAt(list.Count - 1);

                    // Convert points to Y increasing up.
                    for (int i = 0; i < list.Count; i++)
                    {
                        Point pt = list[i];
                        pt.Y = 2 * Origin.Y - pt.Y;
                        list[i] = pt;
                    }

                    // For each figure, process the points.
                    ProcessFigure(list, vertices, normals, indices, textures);
                }
            }
        }
        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;
        }
Exemple #9
0
 static Pokemon3D()
 {
     TEXTURE_COORDINATES = new PointCollection(new Point[]
     { new Point(0, 1), new Point(0, 0), new Point(1, 1), new Point(1, 0) });
       TRIANGLE_INDICES = new Int32Collection(new int[] { 0, 2, 1, 3, 1, 2 });
       TEXTURE_COORDINATES.Freeze();
       TRIANGLE_INDICES.Freeze();
 }
        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 AdaptableDateTimeAxis()
 {
     SecondIncrements = new Int32Collection(new int[] { 1, 2, 5, 15, 30 });
     MinuteIncrements = new Int32Collection(new int[] { 1, 2, 5, 15, 30 });
     HourIncrements = new Int32Collection(new int[] { 1, 2, 4, 6, 12 });
     DayIncrements = new Int32Collection(new int[] { 1, 2, 5, 10 });
     MonthIncrements = new Int32Collection(new int[] { 1, 2, 4, 6 });
     YearIncrements = new Int32Collection(new int[] { 1, 2, 5 });
 }
Exemple #12
0
 public static void ReverseNormals(MeshGeometry3D inputmesh)
 {
     System.Windows.Media.Int32Collection indices = inputmesh.TriangleIndices.Clone();
     inputmesh.TriangleIndices.Clear();
     foreach (var v in indices.Reverse())
     {
         inputmesh.TriangleIndices.Add(v);
     }
     indices = null;
 }
        /// <summary>
        /// Initializes a new instance of the <see cref="MeshBuilder"/> class.
        /// </summary>
        /// <param name="generateNormals">generate normals</param>
        /// <param name="generateTextureCoordinates">generate texture coordinates</param>
        public MeshBuilder(bool generateNormals, bool generateTextureCoordinates)
        {
            positions = new Point3DCollection();
            triangleIndices = new Int32Collection();

            if (generateNormals)
                normals = new Vector3DCollection();
            if (generateTextureCoordinates)
                textureCoordinates = new PointCollection();
        }
Exemple #14
0
        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);
        }
Exemple #15
0
        // ProcessFigure override.
        protected override void ProcessFigure(CircularList<Point> list,
            Point3DCollection vertices,
            Vector3DCollection normals,
            Int32Collection indices,
            PointCollection textures)
        {
            int k = Slices * list.Count;
            int offset = vertices.Count;

            for (int i = 0; i < list.Count; i++)
            {
                Point ptBefore = list[i - 1];
                Point pt = list[i];
                Point ptAfter = list[i + 1];

                Vector v1 = pt - ptBefore;
                v1.Normalize();
                Vector v1Rotated = new Vector(-v1.Y, v1.X);

                Vector v2 = ptAfter - pt;
                v2.Normalize();
                Vector v2Rotated = new Vector(-v2.Y, v2.X);

                Line2D line1 = new Line2D(pt, ptBefore);
                Line2D line2 = new Line2D(pt, ptAfter);

                for (int slice = 0; slice < Slices; slice++)
                {
                    // Angle ranges from 0 to 360 degrees.
                    double angle = slice * 2 * Math.PI / Slices;
                    double scale = EllipseWidth / 2 * Math.Sin(angle);
                    double depth = -Depth / 2 * (1 - Math.Cos(angle));

                    Line2D line1Shifted = line1 + scale * v1Rotated;
                    Line2D line2Shifted = line2 + scale * v2Rotated;
                    Point ptIntersect = line1Shifted * line2Shifted;

                    // Set vertex.
                    vertices.Add(new Point3D(ptIntersect.X, ptIntersect.Y, depth));

                    // Set texture coordinate.
                    textures.Add(new Point((double)i / list.Count,
                                           Math.Sin(angle / 2)));

                    // Set triangle indices.
                    indices.Add(offset + (Slices * i + slice + 0) % k);
                    indices.Add(offset + (Slices * i + slice + 1) % k);
                    indices.Add(offset + (Slices * i + slice + 0 + Slices) % k);

                    indices.Add(offset + (Slices * i + slice + 0 + Slices) % k);
                    indices.Add(offset + (Slices * i + slice + 1) % k);
                    indices.Add(offset + (Slices * i + slice + 1 + Slices) % k);
                }
            }
        }
        public SegmentCollectionVisual3D()
        {
            Positions = new Point3DCollection();
            SegmentIndices = new Int32Collection();
            Fill = Brushes.Blue;
            Diameter = 0.1;
            ThetaDiv = 37;

            _element = new ModelVisual3D();
            Children.Add(_element);
        }
        /// <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;
        }
Exemple #18
0
        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);
        }
Exemple #19
0
        protected override void CalculateGeometry()
        {
            int e;
            double segmentRad = Math.PI / 2 / (n + 1);

            points = new Point3DCollection();
            triangleIndices = new Int32Collection();

            for (e = -n; e <= n; e++)
            {
                double r_e = r * Math.Cos(segmentRad * e);
                double y_e = r * Math.Sin(segmentRad * e);

                for (int s = 0; s <= (4 * n + 4 - 1); s++)
                {
                    double z_s = r_e * Math.Sin(segmentRad * s) * (-1);
                    double x_s = r_e * Math.Cos(segmentRad * s);
                    points.Add(new Point3D(x_s, y_e, z_s));
                }
            }
            points.Add(new Point3D(0, r, 0));
            points.Add(new Point3D(0, -1 * r, 0));

            for (e = 0; e < 2 * n; e++)
            {
                for (int i = 0; i < (4 * n + 4); i++)
                {
                    triangleIndices.Add(e * (4 * n + 4) + i);
                    triangleIndices.Add(e * (4 * n + 4) + i + (4 * n + 4));
                    triangleIndices.Add(e * (4 * n + 4) + (i + 1) % (4 * n + 4) + (4 * n + 4));

                    triangleIndices.Add(e * (4 * n + 4) + (i + 1) % (4 * n + 4) + (4 * n + 4));
                    triangleIndices.Add(e * (4 * n + 4) + (i + 1) % (4 * n + 4));
                    triangleIndices.Add(e * (4 * n + 4) + i);
                }
            }

            for (int i = 0; i < (4 * n + 4); i++)
            {
                triangleIndices.Add(e * (4 * n + 4) + i);
                triangleIndices.Add(e * (4 * n + 4) + (i + 1) % (4 * n + 4));
                triangleIndices.Add((4 * n + 4) * (2 * n + 1));
            }

            for (int i = 0; i < (4 * n + 4); i++)
            {
                triangleIndices.Add(i);
                triangleIndices.Add((i + 1) % (4 * n + 4));
                triangleIndices.Add((4 * n + 4) * (2 * n + 1) + 1);
            }
        }
        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);
        }
Exemple #21
0
        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;
        }
 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();
 }
        /// <summary>
        /// Construct new grid of a given dimension
        /// </summary>
        ///<param name="Dimension"></param>
        public WaveGrid(int Dimension)
        {
            if (Dimension < MinDimension)
                throw new ApplicationException(string.Format("Dimension must be at least {0}", MinDimension.ToString()));

            _ptBuffer1 = new Point3DCollection(Dimension * Dimension);
            _ptBuffer2 = new Point3DCollection(Dimension * Dimension);
            _triangleIndices = new Int32Collection((Dimension - 1) * (Dimension - 1) * 2);

            _dimension = Dimension;

            InitializePointsAndTriangles();

            _currBuffer = _ptBuffer2;
            _oldBuffer = _ptBuffer1;
        }
        /// <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;
        }
Exemple #26
0
        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));
            }
        }
		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");
		}
        private void UpdateGeometry()
        {
            var positions = new Point3DCollection();
            var indices = new Int32Collection();
            var texcoords = new PointCollection();

            for (var i = 0; i < _particleList.Count; ++i)
            {
                var positionIndex = i*4;
                var indexIndex = i*6;
                var p = _particleList[i];

                var p1 = new Point3D(p.Position.X, p.Position.Y, p.Position.Z);
                var p2 = new Point3D(p.Position.X, p.Position.Y + p.Size, p.Position.Z);
                var p3 = new Point3D(p.Position.X + p.Size, p.Position.Y + p.Size, p.Position.Z);
                var p4 = new Point3D(p.Position.X + p.Size, p.Position.Y, p.Position.Z);

                positions.Add(p1);
                positions.Add(p2);
                positions.Add(p3);
                positions.Add(p4);

                var t1 = new Point(0.0, 0.0);
                var t2 = new Point(0.0, 1.0);
                var t3 = new Point(1.0, 1.0);
                var t4 = new Point(1.0, 0.0);

                texcoords.Add(t1);
                texcoords.Add(t2);
                texcoords.Add(t3);
                texcoords.Add(t4);

                indices.Add(positionIndex);
                indices.Add(positionIndex + 2);
                indices.Add(positionIndex + 1);
                indices.Add(positionIndex);
                indices.Add(positionIndex + 3);
                indices.Add(positionIndex + 2);
            }

            ((MeshGeometry3D) _particleModel.Geometry).Positions = positions;
            ((MeshGeometry3D) _particleModel.Geometry).TriangleIndices = indices;
            ((MeshGeometry3D) _particleModel.Geometry).TextureCoordinates = texcoords;
        }
        /// <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>
        /// 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;
        }
        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;
                }
            }
        }
Exemple #32
0
        /// <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,
            };
        }