void CreateGeometry()
        {

            double r = Diameter / 2;
            double l = HeadLength * Diameter;

            // arrowhead
            var pc = new PointCollection();
            pc.Add(new Point(-l, r));
            pc.Add(new Point(-l, r * 2));
            pc.Add(new Point(0, 0));

            var headBuilder = new MeshBuilder();
            headBuilder.AddRevolvedGeometry(pc, new Point3D(0, 0, 0), new Vector3D(0, 0, 1), ThetaDiv);
            _head = headBuilder.ToMesh();
            _head.Freeze(); 
            

            // body
            pc = new PointCollection();
            pc.Add(new Point(0, 0));
            pc.Add(new Point(0, r));
            pc.Add(new Point(1, r));

            var bodyBuilder = new MeshBuilder();
            bodyBuilder.AddRevolvedGeometry(pc, new Point3D(0, 0, 0), new Vector3D(0, 0, 1), ThetaDiv);
            _body = bodyBuilder.ToMesh();
            _body.Freeze();
        }
示例#2
0
        protected override Geometry3D CreateMesh()
        {
            _radius = Radius;
            _position = Position;

            MeshGeometry3D mesh = new MeshGeometry3D();
            Point3D prevPoint = PointForAngle(0);
            Vector3D normal = new Vector3D(0, 0, 1);

            const int div = 180;
            for (int i = 1; i <= div; ++i)
            {
                double angle = 2 * Math.PI / div * i;
                Point3D newPoint = PointForAngle(angle);
                mesh.Positions.Add(prevPoint);
                mesh.Positions.Add(_position);
                mesh.Positions.Add(newPoint);
                mesh.Normals.Add(normal);
                mesh.Normals.Add(normal);
                mesh.Normals.Add(normal);
                prevPoint = newPoint;
            }

            mesh.Freeze();
            return mesh;
        }
示例#3
0
        protected override Geometry3D CreateMesh()
        {
            this.radius = this.Radius;
            this.position = this.Position;

            var mesh = new MeshGeometry3D();
            var prevPoint = this.PointForAngle(0);
            var normal = new Vector3D(0, 0, 1);

            int div = 180;

            for (var i = 1; i <= div; ++i)
            {
                var angle = 2 * Math.PI / div * i;
                var newPoint = this.PointForAngle(angle);
                mesh.Positions.Add(prevPoint);
                mesh.Positions.Add(this.position);
                mesh.Positions.Add(newPoint);
                mesh.Normals.Add(normal);
                mesh.Normals.Add(normal);
                mesh.Normals.Add(normal);
                prevPoint = newPoint;
            }

            mesh.Freeze();
            return mesh;
        }
 private void CreateGeometry()
 {
     var builder = new MeshBuilder();
     builder.AddSphere(new Point3D(0, 0, 0), Diameter/2, ThetaDiv, PhiDiv);
     _sphere = builder.ToMesh();
     _sphere.Freeze();
 }
示例#5
0
文件: Screen.cs 项目: Faham/emophiz
        private Geometry3D CreateGeometry()
        {
            MeshGeometry3D mesh = new MeshGeometry3D();

            mesh.Positions.Add(UL);
            mesh.Positions.Add(LL);
            mesh.Positions.Add(UR);

            mesh.TriangleIndices.Add(0);
            mesh.TriangleIndices.Add(1);
            mesh.TriangleIndices.Add(2);

            Vector3D verticalDirection = LL - UL;
            Vector3D horizontalDirection = UR - UL;

            Point3D LR = UL + verticalDirection + horizontalDirection;

            mesh.Positions.Add(UR);
            mesh.Positions.Add(LL);
            mesh.Positions.Add(LR);

            mesh.TriangleIndices.Add(3);
            mesh.TriangleIndices.Add(4);
            mesh.TriangleIndices.Add(5);

            mesh.Freeze();
            return mesh;
        }
        protected override Geometry3D CreateMesh()
        {
            this.radius = this.Radius;
            this.position = this.Position;

            MeshGeometry3D mesh = new MeshGeometry3D();
            Point3D prevPoint = this.PointForAngle(0);
            Vector3D normal = new Vector3D(0, 0, 1);

            for (int i = 1; i <= Divider; ++i)
            {
                double angle = 2 * Math.PI / Divider * i;
                Point3D newPoint = this.PointForAngle(angle);
                mesh.Positions.Add(prevPoint);
                mesh.Positions.Add(this.position);
                mesh.Positions.Add(newPoint);
                mesh.Normals.Add(normal);
                mesh.Normals.Add(normal);
                mesh.Normals.Add(normal);
                prevPoint = newPoint;
            }

            mesh.Freeze();
            return mesh;
        }
示例#7
0
        protected override Geometry3D CreateMesh()
        {
            radius = Radius;
            position = Position;

            const int angleSteps = 32;
            const double minAngle = 0;
            const double maxAngle = 2 * Math.PI;
            const double dAngle = (maxAngle-minAngle) / angleSteps;

            const int ySteps = 32;
            const double minY = -1.0;
            const double maxY = 1.0;
            const double dy = (maxY - minY) / ySteps;

            MeshGeometry3D mesh = new MeshGeometry3D();

            for (int yi = 0; yi <= ySteps; yi++)
            {
                double y = minY + yi * dy;

                for (int ai = 0; ai <= angleSteps; ai++)
                {
                    double angle = ai * dAngle;

                    mesh.Positions.Add(GetPosition(angle, y));
                    mesh.Normals.Add(GetNormal(angle, y));
                    mesh.TextureCoordinates.Add(GetTextureCoordinate(angle, y));
                }
            }

            for (int yi = 0; yi < ySteps; yi++)
            {
                for (int ai = 0; ai < angleSteps; ai++)
                {
                    int a1 = ai;
                    int a2 = (ai + 1);
                    int y1 = yi * (angleSteps + 1);
                    int y2 = (yi + 1) * (angleSteps + 1);

                    mesh.TriangleIndices.Add(y1 + a1);
                    mesh.TriangleIndices.Add(y2 + a1);
                    mesh.TriangleIndices.Add(y1 + a2);

                    mesh.TriangleIndices.Add(y1 + a2);
                    mesh.TriangleIndices.Add(y2 + a1);
                    mesh.TriangleIndices.Add(y2 + a2);
                }
            }

            mesh.Freeze();
            return mesh;
        }
示例#8
0
        internal override Geometry3D Tessellate()
        {
            int tDiv = 32;
            int yDiv = 32;
            double maxTheta = DegToRad(360.0);
            double minY = -1.0;
            double maxY = 1.0;

            double dt = maxTheta / tDiv;
            double dy = (maxY - minY) / yDiv;

            MeshGeometry3D mesh = new MeshGeometry3D();

            for (int yi = 0; yi <= yDiv; yi++)
            {
                double y = minY + yi * dy;

                for (int ti = 0; ti <= tDiv; ti++)
                {
                    double t = ti * dt;

                    mesh.Positions.Add(GetPosition(t, y));
                    mesh.Normals.Add(GetNormal(t, y));
                    mesh.TextureCoordinates.Add(GetTextureCoordinate(t, y));
                }
            }

            for (int yi = 0; yi < yDiv; yi++)
            {
                for (int ti = 0; ti < tDiv; ti++)
                {
                    int x0 = ti;
                    int x1 = (ti + 1);
                    int y0 = yi * (tDiv + 1);
                    int y1 = (yi + 1) * (tDiv + 1);

                    mesh.TriangleIndices.Add(x0 + y0);
                    mesh.TriangleIndices.Add(x0 + y1);
                    mesh.TriangleIndices.Add(x1 + y0);

                    mesh.TriangleIndices.Add(x1 + y0);
                    mesh.TriangleIndices.Add(x0 + y1);
                    mesh.TriangleIndices.Add(x1 + y1);
                }
            }

            mesh.Freeze();
            return mesh;
        }
        void CreateGeometry()
        {

            double r = Diameter / 2;

            var pc = new PointCollection();
            pc.Add(new Point(0, 0));
            pc.Add(new Point(0, r));
            pc.Add(new Point(1, r));
            pc.Add(new Point(1, 0));

            var builder = new MeshBuilder();
            builder.AddRevolvedGeometry(pc, new Point3D(0, 0, 0), new Vector3D(0, 0, 1), ThetaDiv);
            _tube= builder.ToMesh();
            _tube.Freeze();
        }
        /// <summary>
        /// The create mesh.
        /// </summary>
        /// <returns>
        /// The <see cref="Geometry3D"/>.
        /// </returns>
        protected override Geometry3D CreateMesh()
        {
            this.radius = this.Radius;
            this.position = this.Position;

            const int AngleSteps = 32;
            const double MinAngle = 0;
            const double MaxAngle = 2 * Math.PI;
            const double DAngle = (MaxAngle - MinAngle) / AngleSteps;

            const int YSteps = 32;
            const double MinY = -1.0;
            const double MaxY = 1.0;
            const double Dy = (MaxY - MinY) / YSteps;

            MeshGeometry3D mesh = new MeshGeometry3D();

            for (int yi = 0; yi <= YSteps; yi++)
            {
                double y = MinY + (yi * Dy);

                for (int ai = 0; ai <= AngleSteps; ai++)
                {
                    double angle = ai * DAngle;

                    mesh.Positions.Add(this.GetPosition(angle, y));
                    mesh.Normals.Add(this.GetNormal(angle, y));
                    mesh.TextureCoordinates.Add(this.GetTextureCoordinate(angle, y));
                }
            }

            for (int yi = 0; yi < YSteps; yi++)
            {
                for (int ai = 0; ai < AngleSteps; ai++)
                {
                    int a1 = ai;
                    int a2 = ai + 1;
                    int y1 = yi * (AngleSteps + 1);
                    int y2 = (yi + 1) * (AngleSteps + 1);

                    mesh.TriangleIndices.Add(y1 + a1);
                    mesh.TriangleIndices.Add(y2 + a1);
                    mesh.TriangleIndices.Add(y1 + a2);

                    mesh.TriangleIndices.Add(y1 + a2);
                    mesh.TriangleIndices.Add(y2 + a1);
                    mesh.TriangleIndices.Add(y2 + a2);
                }
            }

            mesh.Freeze();
            return mesh;
        }
示例#11
0
        /// <summary>
        /// Initializes the image brush if need.
        /// </summary>
        public override void AddedToEmitter(Emitter emitter)
        {
            base.AddedToEmitter(emitter);

            if (m_image == null)
            {
                var uri =
                    (m_imageUri.IsAbsoluteUri
                    ? m_imageUri
                    : new Uri(m_baseUri, m_imageUri));

                m_image = GetImage(uri);
                m_mesh = Particle.CreateDefaultMesh(1.0, 1.0, m_image.Width, m_image.Height);
                m_mesh.Freeze();
            }
        }
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            var s = System.Windows.Application.GetResourceStream(Source);
            if (s == null)
                throw new ArgumentException("Object not Found", "Source");

            using (s.Stream)
            using (var sr = new StreamReader(s.Stream, Encoding.ASCII))
            {
                MeshGeometry3D geom = new MeshGeometry3D();

                List<Point3D> verts = new List<Point3D>();
                List<Vector3D> normals = new List<Vector3D>();
                List<Face3D> faces = new List<Face3D>();

                string line;
                while ((line = sr.ReadLine()) != null)
                {
                    if (line.Length == 0 || line[0] == '#')
                        continue;

                    var ls = line.Split(' ');
                    switch (ls[0])
                    {
                        case "v":
                            {
                                float x, y, z;
                                float.TryParse(ls[1], NumberStyles.Any, CultureInfo.InvariantCulture, out x);
                                float.TryParse(ls[2], NumberStyles.Any, CultureInfo.InvariantCulture, out y);
                                float.TryParse(ls[3], NumberStyles.Any, CultureInfo.InvariantCulture, out z);
                                Point3D p = new Point3D(x, y, z);
                                verts.Add(p);
                                break;
                            }
                        case "vn":
                            {
                                float x, y, z;
                                float.TryParse(ls[1], NumberStyles.Any, CultureInfo.InvariantCulture, out x);
                                float.TryParse(ls[2], NumberStyles.Any, CultureInfo.InvariantCulture, out y);
                                float.TryParse(ls[3], NumberStyles.Any, CultureInfo.InvariantCulture, out z);
                                Vector3D p = new Vector3D(x, y, z);
                                normals.Add(p);
                                break;
                            }
                        case "f":
                            {
                                var v0 = ls[1].Split('/');
                                var v1 = ls[2].Split('/');
                                var v2 = ls[3].Split('/');
                                Face3D f = new Face3D
                                {
                                    A = new Vertex3D(verts[int.Parse(v0[0]) - 1], normals[int.Parse(v0[2]) - 1], new Vector3D()),
                                    B = new Vertex3D(verts[int.Parse(v1[0]) - 1], normals[int.Parse(v1[2]) - 1], new Vector3D()),
                                    C = new Vertex3D(verts[int.Parse(v2[0]) - 1], normals[int.Parse(v2[2]) - 1], new Vector3D()),
                                };
                                faces.Add(f);
                                break;
                            }
                    }
                }

                int i = 0;
                foreach (var face3D in faces)
                {
                    geom.Positions.Add(face3D.A.Position);
                    geom.Positions.Add(face3D.B.Position);
                    geom.Positions.Add(face3D.C.Position);

                    geom.Normals.Add(face3D.A.Normal);
                    geom.Normals.Add(face3D.B.Normal);
                    geom.Normals.Add(face3D.C.Normal);

                    geom.TriangleIndices.Add(i++);
                    geom.TriangleIndices.Add(i++);
                    geom.TriangleIndices.Add(i++);
                }

                geom.Freeze();

                return geom;
            }
        }
示例#13
0
            private static void CreatePushpinGeometry()
            {
                MeshGeometry3D geometry = new MeshGeometry3D();

                // Create a collection of vertex positions for the MeshGeometry3D.
                Point3DCollection ptrVertices = new Point3DCollection();
                ptrVertices.Add(new Point3D(0, 0, 0));
                ptrVertices.Add(new Point3D(-0.01, -0.01, 1.2));
                ptrVertices.Add(new Point3D(0.01, -0.01, 1.2));
                ptrVertices.Add(new Point3D(0, 0.01, 1.2));
                geometry.Positions = ptrVertices;

                // Create a collection of triangle indices for the MeshGeometry3D.
                Int32Collection ptrTriangleIndices = new Int32Collection();
                ptrTriangleIndices.Add(1);
                ptrTriangleIndices.Add(2);
                ptrTriangleIndices.Add(3);

                ptrTriangleIndices.Add(0);
                ptrTriangleIndices.Add(1);
                ptrTriangleIndices.Add(3);

                ptrTriangleIndices.Add(0);
                ptrTriangleIndices.Add(3);
                ptrTriangleIndices.Add(2);

                ptrTriangleIndices.Add(0);
                ptrTriangleIndices.Add(2);
                ptrTriangleIndices.Add(1);
                geometry.TriangleIndices = ptrTriangleIndices;

                // create a collection of texture coordinates for the MeshGeometry3D
                PointCollection ptrTexCoordindates = new PointCollection();
                ptrTexCoordindates.Add(new Point(0.5, 0.5));
                ptrTexCoordindates.Add(new Point(0.5, 0.5));
                ptrTexCoordindates.Add(new Point(0.5, 0.5));

                ptrTexCoordindates.Add(new Point(0.5, 0.5));
                ptrTexCoordindates.Add(new Point(0.5, 0.5));
                ptrTexCoordindates.Add(new Point(0.5, 0.5));

                ptrTexCoordindates.Add(new Point(0.5, 0.5));
                ptrTexCoordindates.Add(new Point(0.5, 0.5));
                ptrTexCoordindates.Add(new Point(0.5, 0.5));

                ptrTexCoordindates.Add(new Point(0.5, 0.5));
                ptrTexCoordindates.Add(new Point(0.5, 0.5));
                ptrTexCoordindates.Add(new Point(0.5, 0.5));
                geometry.TextureCoordinates = ptrTexCoordindates;

                // Apply the mesh to the geometry model.
                geometry.Freeze();

                _geometry = geometry;
            }
示例#14
0
        private void BuildMeshes(OptFile opt)
        {
            this.meshes = null;

            if (opt == null)
            {
                return;
            }

            this.meshes = new MeshGeometry3D[opt.Meshes.Count][][][];

            for (int meshIndex = 0; meshIndex < opt.Meshes.Count; meshIndex++)
            {
                var mesh = opt.Meshes[meshIndex];

                var positions = new Point3DCollection(
                    mesh.Vertices
                    .Select(t => new Point3D(-t.Y, -t.X, t.Z)));

                var normals = new Vector3DCollection(
                    mesh.VertexNormals
                    .Select(t => new Vector3D(-t.Y, -t.X, t.Z)));

                var textureCoordinates = new PointCollection(
                    mesh.TextureCoordinates
                    .Select(t => new Point(t.U, t.V)));

                this.meshes[meshIndex] = new MeshGeometry3D[mesh.Lods.Count][][];

                for (int lodIndex = 0; lodIndex < mesh.Lods.Count; lodIndex++)
                {
                    var lod = mesh.Lods[lodIndex];

                    this.meshes[meshIndex][lodIndex] = new MeshGeometry3D[lod.FaceGroups.Count][];

                    for (int faceGroupIndex = 0; faceGroupIndex < lod.FaceGroups.Count; faceGroupIndex++)
                    {
                        var faceGroup = lod.FaceGroups[faceGroupIndex];

                        MeshGeometry3D[] geometries = new MeshGeometry3D[faceGroup.Faces.Count + 1];

                        for (int faceIndex = 0; faceIndex < faceGroup.Faces.Count; faceIndex++)
                        {
                            var face = faceGroup.Faces[faceIndex];

                            MeshGeometry3D geometry = new MeshGeometry3D();
                            int index = 0;

                            Index positionsIndex = face.VerticesIndex;
                            Index normalsIndex = face.VertexNormalsIndex;
                            Index textureCoordinatesIndex = face.TextureCoordinatesIndex;

                            geometry.Positions.Add(positions.ElementAtOrDefault(positionsIndex.A));
                            geometry.Normals.Add(normals.ElementAtOrDefault(normalsIndex.A));
                            geometry.TextureCoordinates.Add(textureCoordinates.ElementAtOrDefault(textureCoordinatesIndex.A));
                            geometry.TriangleIndices.Add(index);
                            index++;

                            geometry.Positions.Add(positions.ElementAtOrDefault(positionsIndex.B));
                            geometry.Normals.Add(normals.ElementAtOrDefault(normalsIndex.B));
                            geometry.TextureCoordinates.Add(textureCoordinates.ElementAtOrDefault(textureCoordinatesIndex.B));
                            geometry.TriangleIndices.Add(index);
                            index++;

                            geometry.Positions.Add(positions.ElementAtOrDefault(positionsIndex.C));
                            geometry.Normals.Add(normals.ElementAtOrDefault(normalsIndex.C));
                            geometry.TextureCoordinates.Add(textureCoordinates.ElementAtOrDefault(textureCoordinatesIndex.C));
                            geometry.TriangleIndices.Add(index);
                            index++;

                            if (positionsIndex.D >= 0)
                            {
                                geometry.TriangleIndices.Add(index - 3);
                                geometry.TriangleIndices.Add(index - 1);

                                geometry.Positions.Add(positions.ElementAtOrDefault(positionsIndex.D));
                                geometry.Normals.Add(normals.ElementAtOrDefault(normalsIndex.D));
                                geometry.TextureCoordinates.Add(textureCoordinates.ElementAtOrDefault(textureCoordinatesIndex.D));
                                geometry.TriangleIndices.Add(index);
                                index++;
                            }

                            geometry.Freeze();
                            geometries[1 + faceIndex] = geometry;
                        }

                        MeshGeometry3D geometryGroup = new MeshGeometry3D();

                        for (int i = 1; i < geometries.Length; i++)
                        {
                            MergeGeometry(geometryGroup, geometries[i]);
                        }

                        geometryGroup.Freeze();
                        geometries[0] = geometryGroup;

                        this.meshes[meshIndex][lodIndex][faceGroupIndex] = geometries;
                    }
                }
            }
        }
示例#15
0
        protected override Geometry3D CreateMesh()
        {
            this.radius = this.Radius;
            this.position = this.Position;

            int angleSteps = 32;
            double minAngle = 0;
            double maxAngle = 2 * Math.PI;
            double angle = (maxAngle - minAngle) / angleSteps;

            int steps = 32;
            double minY = -1.0;
            double maxY = 1.0;
            double dy = (maxY - minY) / steps;

            var mesh = new MeshGeometry3D();

            for (var yi = 0; yi <= steps; yi++)
            {
                var y = minY + (yi * dy);

                for (var ai = 0; ai <= angleSteps; ai++)
                {
                    var ang = ai * angle;

                    mesh.Positions.Add(this.GetPosition(ang, y));
                    mesh.Normals.Add(this.GetNormal(ang, y));
                    mesh.TextureCoordinates.Add(this.GetTextureCoordinate(ang, y));
                }
            }

            for (var yi = 0; yi < steps; yi++)
            {
                for (var ai = 0; ai < angleSteps; ai++)
                {
                    var a1 = ai;
                    var a2 = ai + 1;
                    var y1 = yi * (angleSteps + 1);
                    var y2 = (yi + 1) * (angleSteps + 1);

                    mesh.TriangleIndices.Add(y1 + a1);
                    mesh.TriangleIndices.Add(y2 + a1);
                    mesh.TriangleIndices.Add(y1 + a2);

                    mesh.TriangleIndices.Add(y1 + a2);
                    mesh.TriangleIndices.Add(y2 + a1);
                    mesh.TriangleIndices.Add(y2 + a2);
                }
            }

            mesh.Freeze();
            return mesh;
        }
示例#16
0
        private MeshGeometry3D CircleMesh(Vector3D inpNormal, Point3D startPoint, double radius, Point[] texturePoint)
        {
            try
            {
                double maxTheta = (360 / 180.0) * Math.PI;
                double dt = maxTheta / 32;
                int point1 = 0;
                int point2 = 0;

                Point3D center = startPoint;

                MeshGeometry3D mesh = new MeshGeometry3D();
                mesh.Positions.Add(center);
                mesh.TextureCoordinates.Add(texturePoint[0]);
                mesh.TextureCoordinates.Add(texturePoint[1]);
                mesh.TextureCoordinates.Add(texturePoint[2]);

                if (inpNormal.Y == 1)
                {
                    point1 = 2;
                    point2 = 1;
                }
                else if (inpNormal.Y == -1)
                {
                    point1 = 1;
                    point2 = 2;
                }

                for (int ti = 0; ti <= 360; ti++)
                {
                    double t = ti * dt;

                    if (ti % 3 == 0 && ti > 0)
                    {
                        mesh.TriangleIndices.Add(0);
                        mesh.TriangleIndices.Add(ti + point1);
                        mesh.TriangleIndices.Add(ti + point2);
                        mesh.Normals.Add(inpNormal);
                    }

                    mesh.Positions.Add(GetPositionCirCle(t, radius, startPoint));
                }

                mesh.Freeze();
                return mesh;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
示例#17
0
        /// <returns></returns>
        internal static MeshGeometry3D Tessellate(double x,double y,double z)
        {
           int tDiv = 15;
           int pDiv = 25;
           double radius = 1.5;
            double dt = DegToRad(360.0) / tDiv;
            double dp = DegToRad(180.0) / pDiv;

            MeshGeometry3D meshh = new MeshGeometry3D();

            for (int pi = 0; pi <= pDiv; pi++)
            {
                double phi = pi * dp;

                for (int ti = 0; ti <= tDiv; ti++)
                {
                    // we want to start the mesh on the x axis
                    double theta = ti * dt;

                    meshh.Positions.Add(GetPosition(theta, phi, radius,x,y,z));
                    meshh.Normals.Add(GetNormal(theta, phi));
                  meshh.TextureCoordinates.Add(GetTextureCoordinate(theta, phi));
                }
            }

            for (int pi = 0; pi < pDiv; pi++)
            {
                for (int ti = 0; ti < tDiv; ti++)
                {
                    int x0 = ti;
                    int x1 = (ti + 1);
                    int y0 = pi * (tDiv + 1);
                    int y1 = (pi + 1) * (tDiv + 1);

                    meshh.TriangleIndices.Add(x0 + y0);
                    meshh.TriangleIndices.Add(x0 + y1);
                    meshh.TriangleIndices.Add(x1 + y0);

                    meshh.TriangleIndices.Add(x1 + y0);
                    meshh.TriangleIndices.Add(x0 + y1);
                    meshh.TriangleIndices.Add(x1 + y1);
                }
            }

            meshh.Freeze();
             return meshh;
        }
示例#18
0
        internal static MeshGeometry3D Tessellate(int tDiv,
                                                  int pDiv,
                                                  double radius,
                                                  double thetaStart,
                                                  double thetaWidth,
                                                  double phiStart,
                                                  double phiWidth,
                                                  bool invert)
        {
            double dt = DegToRad(thetaWidth) / tDiv;
            double dp = DegToRad(phiWidth) / pDiv;

            MeshGeometry3D mesh = new MeshGeometry3D();

            for (int pi = 0; pi <= pDiv; pi++)
            {
                double phi = DegToRad(phiStart) + pi * dp;

                for (int ti = 0; ti <= tDiv; ti++)
                {
                    // we want to start the mesh on the x axis
                    double theta = DegToRad(thetaStart) + ti * dt;

                    mesh.Positions.Add(GetPosition(theta, phi, radius));
                    mesh.Normals.Add(GetNormal(theta, phi));
                    mesh.TextureCoordinates.Add(GetTextureCoordinate(Math.Abs(theta - DegToRad(thetaStart)) / Math.Abs(DegToRad(thetaWidth)) * 2 * Math.PI,
                                                                     Math.Abs(phi - DegToRad(phiStart)) / Math.Abs(DegToRad(phiWidth)) * Math.PI));
                }
            }

            for (int pi = 0; pi < pDiv; pi++)
            {
                for (int ti = 0; ti < tDiv; ti++)
                {
                    int x0 = ti;
                    int x1 = (ti + 1);
                    int y0 = pi * (tDiv + 1);
                    int y1 = (pi + 1) * (tDiv + 1);

                    if (invert)
                    {
                        mesh.TriangleIndices.Add(x0 + y0);
                        mesh.TriangleIndices.Add(x1 + y0);
                        mesh.TriangleIndices.Add(x0 + y1);

                        mesh.TriangleIndices.Add(x1 + y0);
                        mesh.TriangleIndices.Add(x1 + y1);
                        mesh.TriangleIndices.Add(x0 + y1);
                    }
                    else
                    {
                        mesh.TriangleIndices.Add(x0 + y0);
                        mesh.TriangleIndices.Add(x0 + y1);
                        mesh.TriangleIndices.Add(x1 + y0);

                        mesh.TriangleIndices.Add(x1 + y0);
                        mesh.TriangleIndices.Add(x0 + y1);
                        mesh.TriangleIndices.Add(x1 + y1);
                    }
                }
            }

            mesh.Freeze();
            return mesh;
        }
示例#19
0
        /// <summary>
        /// Converts the geometry to a <see cref="MeshGeometry3D"/> .
        /// </summary>
        /// <param name="freeze">
        /// freeze the mesh if set to <c>true</c> .
        /// </param>
        /// <returns>
        /// A mesh geometry.
        /// </returns>
        public MeshGeometry3D ToMesh(bool freeze = false)
        {
            if (this.triangleIndices.Count == 0)
            {
                var emptyGeometry = new MeshGeometry3D();
                if (freeze)
                {
                    emptyGeometry.Freeze();
                }

                return emptyGeometry;
            }

            if (this.normals != null && this.positions.Count != this.normals.Count)
            {
                throw new InvalidOperationException(WrongNumberOfNormals);
            }

            if (this.textureCoordinates != null && this.positions.Count != this.textureCoordinates.Count)
            {
                throw new InvalidOperationException(WrongNumberOfTextureCoordinates);
            }

            var mg = new MeshGeometry3D
            {
                Positions = new Point3DCollection(this.positions),
                TriangleIndices = new Int32Collection(this.triangleIndices)
            };
            if (this.normals != null)
            {
                mg.Normals = new Vector3DCollection(this.normals);
            }

            if (this.textureCoordinates != null)
            {
                mg.TextureCoordinates = new PointCollection(this.textureCoordinates);
            }

            if (freeze)
            {
                mg.Freeze();
            }

            return mg;
        }
示例#20
0
        private static MeshGeometry3D CreateBackGeometry(MeshGeometry3D frontMesh)
        {
            if (frontMesh == null)
            {
                return null;
            }

            MeshGeometry3D backMesh = new MeshGeometry3D();

            // Simply share the same (frozen) collections for positions and
            // texture coordinates.
            backMesh.Positions = frontMesh.Positions;
            backMesh.TextureCoordinates = frontMesh.TextureCoordinates;

            // Make a copy of the triangle indices and wind them backwards.
            backMesh.TriangleIndices = new Int32Collection(frontMesh.TriangleIndices);
            int numTriangles = backMesh.TriangleIndices.Count/3;
            for (int iTriangle = 0; iTriangle < numTriangles; iTriangle++)
            {
                int temp = backMesh.TriangleIndices[iTriangle * 3];
                backMesh.TriangleIndices[iTriangle * 3] = backMesh.TriangleIndices[iTriangle * 3 + 2];
                backMesh.TriangleIndices[iTriangle * 3 + 2] = temp;
            }

            // Make a copy of the normals and reverse their direction.
            backMesh.Normals = new Vector3DCollection(frontMesh.Normals);
            int numNormals = backMesh.Normals.Count;
            for (int iNormal = 0; iNormal < numNormals; iNormal++)
            {
                backMesh.Normals[iNormal] *= -1.0;
            }

            backMesh.Freeze();
            return backMesh;
        }
        protected override Geometry3D CreateMesh()
        {
            this.radius = this.Radius;
            this.position = this.Position;
            MeshGeometry3D mesh = new MeshGeometry3D();

            for (int yi = 0; yi <= StepsY; yi++)
            {
                double y = MinY + (yi * Dy);

                for (int ai = 0; ai <= AngleSteps; ai++)
                {
                    double angle = ai * AngleD;

                    mesh.Positions.Add(this.GetPosition(angle, y));
                    mesh.Normals.Add(this.GetNormal(angle, y));
                    mesh.TextureCoordinates.Add(this.GetTextureCoordinate(angle, y));
                }
            }

            for (int yi = 0; yi < StepsY; yi++)
            {
                for (int ai = 0; ai < AngleSteps; ai++)
                {
                    int a1 = ai;
                    int a2 = ai + 1;
                    int y1 = yi * (AngleSteps + 1);
                    int y2 = (yi + 1) * (AngleSteps + 1);

                    mesh.TriangleIndices.Add(y1 + a1);
                    mesh.TriangleIndices.Add(y2 + a1);
                    mesh.TriangleIndices.Add(y1 + a2);

                    mesh.TriangleIndices.Add(y1 + a2);
                    mesh.TriangleIndices.Add(y2 + a1);
                    mesh.TriangleIndices.Add(y2 + a2);
                }
            }

            mesh.Freeze();
            return mesh;
        }
示例#22
0
        public static MeshGeometry3D GetSphere(int separators, double radius)
        {
            double segmentRad = Math.PI / 2 / (separators + 1);
            int numberOfSeparators = 4 * separators + 4;

            MeshGeometry3D retVal = new MeshGeometry3D();

            // Calculate all the positions
            for (int e = -separators; e <= separators; e++)
            {
                double r_e = radius * Math.Cos(segmentRad * e);
                double y_e = radius * Math.Sin(segmentRad * e);

                for (int s = 0; s <= (numberOfSeparators - 1); s++)
                {
                    double z_s = r_e * Math.Sin(segmentRad * s) * (-1);
                    double x_s = r_e * Math.Cos(segmentRad * s);
                    retVal.Positions.Add(new Point3D(x_s, y_e, z_s));
                }
            }
            retVal.Positions.Add(new Point3D(0, radius, 0));
            retVal.Positions.Add(new Point3D(0, -1 * radius, 0));

            // Main Body
            int maxIterate = 2 * separators;
            for (int y = 0; y < maxIterate; y++)      // phi?
            {
                for (int x = 0; x < numberOfSeparators; x++)      // theta?
                {
                    retVal.TriangleIndices.Add(y * numberOfSeparators + (x + 1) % numberOfSeparators + numberOfSeparators);
                    retVal.TriangleIndices.Add(y * numberOfSeparators + x + numberOfSeparators);
                    retVal.TriangleIndices.Add(y * numberOfSeparators + x);

                    retVal.TriangleIndices.Add(y * numberOfSeparators + x);
                    retVal.TriangleIndices.Add(y * numberOfSeparators + (x + 1) % numberOfSeparators);
                    retVal.TriangleIndices.Add(y * numberOfSeparators + (x + 1) % numberOfSeparators + numberOfSeparators);
                }
            }

            // Top Cap
            for (int i = 0; i < numberOfSeparators; i++)
            {
                retVal.TriangleIndices.Add(maxIterate * numberOfSeparators + i);
                retVal.TriangleIndices.Add(maxIterate * numberOfSeparators + (i + 1) % numberOfSeparators);
                retVal.TriangleIndices.Add(numberOfSeparators * (2 * separators + 1));
            }

            // Bottom Cap
            for (int i = 0; i < numberOfSeparators; i++)
            {
                retVal.TriangleIndices.Add(numberOfSeparators * (2 * separators + 1) + 1);
                retVal.TriangleIndices.Add((i + 1) % numberOfSeparators);
                retVal.TriangleIndices.Add(i);

            }

            // Exit Function
            retVal.Freeze();
            return retVal;
        }
示例#23
0
        public static MeshGeometry3D GetCube(double size)
        {
            double halfSize = size / 2d;

            // Define 3D mesh object
            MeshGeometry3D retVal = new MeshGeometry3D();

            retVal.Positions.Add(new Point3D(-halfSize, -halfSize, halfSize));
            retVal.Positions.Add(new Point3D(halfSize, -halfSize, halfSize));
            retVal.Positions.Add(new Point3D(halfSize, halfSize, halfSize));
            retVal.Positions.Add(new Point3D(-halfSize, halfSize, halfSize));

            retVal.Positions.Add(new Point3D(-halfSize, -halfSize, -halfSize));
            retVal.Positions.Add(new Point3D(halfSize, -halfSize, -halfSize));
            retVal.Positions.Add(new Point3D(halfSize, halfSize, -halfSize));
            retVal.Positions.Add(new Point3D(-halfSize, halfSize, -halfSize));

            // Front face
            retVal.TriangleIndices.Add(0);
            retVal.TriangleIndices.Add(1);
            retVal.TriangleIndices.Add(2);
            retVal.TriangleIndices.Add(2);
            retVal.TriangleIndices.Add(3);
            retVal.TriangleIndices.Add(0);

            // Back face
            retVal.TriangleIndices.Add(6);
            retVal.TriangleIndices.Add(5);
            retVal.TriangleIndices.Add(4);
            retVal.TriangleIndices.Add(4);
            retVal.TriangleIndices.Add(7);
            retVal.TriangleIndices.Add(6);

            // Right face
            retVal.TriangleIndices.Add(1);
            retVal.TriangleIndices.Add(5);
            retVal.TriangleIndices.Add(2);
            retVal.TriangleIndices.Add(5);
            retVal.TriangleIndices.Add(6);
            retVal.TriangleIndices.Add(2);

            // Top face
            retVal.TriangleIndices.Add(2);
            retVal.TriangleIndices.Add(6);
            retVal.TriangleIndices.Add(3);
            retVal.TriangleIndices.Add(3);
            retVal.TriangleIndices.Add(6);
            retVal.TriangleIndices.Add(7);

            // Bottom face
            retVal.TriangleIndices.Add(5);
            retVal.TriangleIndices.Add(1);
            retVal.TriangleIndices.Add(0);
            retVal.TriangleIndices.Add(0);
            retVal.TriangleIndices.Add(4);
            retVal.TriangleIndices.Add(5);

            // Right face
            retVal.TriangleIndices.Add(4);
            retVal.TriangleIndices.Add(0);
            retVal.TriangleIndices.Add(3);
            retVal.TriangleIndices.Add(3);
            retVal.TriangleIndices.Add(7);
            retVal.TriangleIndices.Add(4);

            // Exit Function
            retVal.Freeze();
            return retVal;
        }
示例#24
0
 /// <summary>
 ///     Generates the geometries that compose the sphere.
 /// </summary>
 private Geometry3D Calculate(int Latitudes, int Longitudes, int Radius)
 {
     var geometry = new MeshGeometry3D();
     for (int latitude = 0; latitude <= Latitudes; latitude++)
     {
         double phi = Math.PI/2 - latitude*Math.PI/Latitudes;
         double y = Math.Sin(phi);
         double radius = -Math.Cos(phi);
         for (int longitude = 0; longitude <= Longitudes; longitude++)
         {
             double theta = longitude*2*Math.PI/Longitudes;
             double x = radius*Math.Sin(theta);
             double z = radius*Math.Cos(theta);
             geometry.Positions.Add(new Point3D(x, y, z));
             geometry.Normals.Add(new Vector3D(x, y, z));
             geometry.TextureCoordinates.Add(new Point((double) longitude/Longitudes, (double) latitude/Latitudes));
         }
     }
     for (int latitude = 0; latitude < Latitudes; latitude++)
     {
         for (int longitude = 0; longitude < Longitudes; longitude++)
         {
             geometry.TriangleIndices.Add(latitude*(Longitudes + 1) + longitude);
             geometry.TriangleIndices.Add((latitude + 1)*(Longitudes + 1) + longitude);
             geometry.TriangleIndices.Add(latitude*(Longitudes + 1) + longitude + 1);
             geometry.TriangleIndices.Add(latitude*(Longitudes + 1) + longitude + 1);
             geometry.TriangleIndices.Add((latitude + 1)*(Longitudes + 1) + longitude);
             geometry.TriangleIndices.Add((latitude + 1)*(Longitudes + 1) + longitude + 1);
         }
     }
     geometry.Freeze();
     return geometry;
 }
        /// <summary> 
        /// Constructor
        /// </summary> 
        /// <param name="visual3D">The Visual3D that contains the 2D visual</param> 
        /// <param name="fromVisual">The visual on the Visual3D</param>
        internal GeneralTransform2DTo3DTo2D(Viewport2DVisual3D visual3D, Visual fromVisual) 
        {
            IsInverse = false;

            // get a copy of the geometry information - we store our own model to reuse hit 
            // test code on the GeometryModel3D
            _geometry = new MeshGeometry3D(); 
            _geometry.Positions = visual3D.InternalPositionsCache; 
            _geometry.TextureCoordinates = visual3D.InternalTextureCoordinatesCache;
            _geometry.TriangleIndices = visual3D.InternalTriangleIndicesCache; 
            _geometry.Freeze();

            Visual visual3Dchild = visual3D.Visual;
 
            // Special case - Setting CacheMode on V2DV3D causes an internal switch from using a VisualBrush
            // to using a BitmapCacheBrush.  It also introduces an extra 2D Visual in the Visual tree above 
            // the V2DV3D.Visual, but this extra node has no effect on transforms and can safely be ignored. 
            // The transform returned will be identical to the one created for calling TransformTo* with
            // the V2DV3D.Visual itself. 
            Visual descendentVisual = (fromVisual == visual3Dchild._parent) ? visual3Dchild : fromVisual;

            // get a copy of the size of the visual brush and the rect on the
            // visual that the transform is going to/from 

            _visualBrushBounds = visual3Dchild.CalculateSubgraphRenderBoundsOuterSpace(); 
            _visualBounds = descendentVisual.CalculateSubgraphRenderBoundsInnerSpace(); 

            // get the transform that will let us go from the fromVisual to its last 2D 
            // parent before it reaches the 3D part of the graph (i.e. visual3D.Child)
            GeneralTransformGroup transformGroup = new GeneralTransformGroup();
            transformGroup.Children.Add(descendentVisual.TransformToAncestor(visual3Dchild));
            transformGroup.Children.Add(visual3Dchild.TransformToOuterSpace()); 
            transformGroup.Freeze();
            _transform2D = transformGroup; 
 
            // store the inverse as well
            _transform2DInverse = (GeneralTransform)_transform2D.Inverse; 
            if (_transform2DInverse != null)
            {
                _transform2DInverse.Freeze();
            } 

            // make a copy of the camera and other values on the Viewport3D 
            Viewport3DVisual viewport3D = (Viewport3DVisual)VisualTreeHelper.GetContainingVisual2D(visual3D); 

            _camera = viewport3D.Camera; 
            if (_camera != null)
            {
                _camera = (Camera)viewport3D.Camera.GetCurrentValueAsFrozen();
            } 

            _viewSize = viewport3D.Viewport.Size; 
            _boundingRect = viewport3D.ComputeSubgraphBounds3D(); 
            _objectToViewport = visual3D.TransformToAncestor(viewport3D);
 
            // if the transform was not possible, it could be null - check before freezing
            if (_objectToViewport != null)
            {
                _objectToViewport.Freeze(); 
            }
 
            // store the needed transformations for the various operations 
            _worldTransformation = M3DUtil.GetWorldTransformationMatrix(visual3D);
 
            _validEdgesCache = null;
        }
示例#26
0
        private static MeshGeometry3D BuildMesh(Point3D p0, Point3D p1, Point3D p2, Point3D p3,
                                                Point q0, Point q1, Point q2, Point q3)
        {
            var mesh = new MeshGeometry3D();
            mesh.Positions.Add(p0);
            mesh.Positions.Add(p1);
            mesh.Positions.Add(p2);
            mesh.Positions.Add(p3);

            var normal = CalculateNormal(p0, p1, p2);
            mesh.TriangleIndices.Add(0);
            mesh.TriangleIndices.Add(1);
            mesh.TriangleIndices.Add(2);
            mesh.Normals.Add(normal);
            mesh.Normals.Add(normal);
            mesh.Normals.Add(normal);
            mesh.TextureCoordinates.Add(q3);
            mesh.TextureCoordinates.Add(q2);
            mesh.TextureCoordinates.Add(q1);

            normal = CalculateNormal(p2, p3, p0);
            mesh.TriangleIndices.Add(2);
            mesh.TriangleIndices.Add(3);
            mesh.TriangleIndices.Add(0);
            mesh.Normals.Add(normal);
            mesh.Normals.Add(normal);
            mesh.Normals.Add(normal);
            mesh.TextureCoordinates.Add(q0);
            mesh.TextureCoordinates.Add(q1);
            mesh.TextureCoordinates.Add(q2);

            mesh.Freeze();
            return mesh;
        }
示例#27
0
        /// <summary>
        /// Populates the vertex and index buffers.
        /// </summary>
        /// <param name="capOffset">The distance from the end of the cylinder to the end of the
        /// pseudo-rounded end.</param>
        /// <returns>The mesh.</returns>
        private static MeshGeometry3D CreateMesh(double capOffset)
        {
            MeshGeometry3D mesh = new MeshGeometry3D();

            int ic1 = 2 * divisions;
            int ic2 = ic1 + 1;

            for (int division = 0; division < divisions; division++)
            {
                double theta = 2 * Math.PI * division / divisions;

                double z = Stick.radius * Math.Cos(theta);
                double y = Stick.radius * Math.Sin(theta);

                mesh.Positions.Add(new Point3D(0, y, z));
                mesh.Positions.Add(new Point3D(1, y, z));

                mesh.Normals.Add(new Vector3D(0, y, z));
                mesh.Normals.Add(new Vector3D(0, y, z));

                int i1 = 2 * division;
                int i2 = i1 + 1;
                int i3 = 2 * ((division + 1) % divisions);
                int i4 = i3 + 1;

                mesh.TriangleIndices.Add(i1);
                mesh.TriangleIndices.Add(i2);
                mesh.TriangleIndices.Add(i3);

                mesh.TriangleIndices.Add(i3);
                mesh.TriangleIndices.Add(i2);
                mesh.TriangleIndices.Add(i4);

                mesh.TriangleIndices.Add(i2);
                mesh.TriangleIndices.Add(ic1);
                mesh.TriangleIndices.Add(i4);

                mesh.TriangleIndices.Add(i3);
                mesh.TriangleIndices.Add(ic2);
                mesh.TriangleIndices.Add(i1);
            }

            mesh.Positions.Add(new Point3D(1, 0, 0));
            mesh.Normals.Add(new Vector3D(1, 0, 0));

            mesh.Positions.Add(new Point3D(-capOffset, 0, 0));
            mesh.Normals.Add(new Vector3D(-1, 0, 0));

            mesh.Freeze();

            return mesh;
        }
示例#28
0
文件: Ball.cs 项目: atosorigin/Kinect
        internal Geometry3D Tessellate()
        {
            int tDiv = 750;
            int yDiv = 750;
            double maxTheta = DegToRad(360);
            double minY = -1.0;
            double maxY = 1.0;

            double dt = maxTheta/tDiv;
            double dy = (maxY - minY)/yDiv;

            var mesh = new MeshGeometry3D();

            for (int yi = 0; yi <= yDiv; yi++)
            {
                double y = minY + yi*dy;

                for (int ti = 0; ti <= tDiv; ti++)
                {
                    double t = ti*dt;
                    Point3D p = GetPosition(t, y);
                    if (p.Z > 0 && p.X > -.5 && p.X < .5 && p.Y > -.5 && p.Y < .5)
                    {
                        mesh.Positions.Add(p);
                        mesh.Normals.Add(GetNormal(t, y));
                        mesh.TextureCoordinates.Add(GetTextureCoordinate(t, y));
                    }
                }
            }

            for (int yi = 0; yi < yDiv; yi++)
            {
                for (int ti = 0; ti < tDiv; ti++)
                {
                    int x0 = ti;
                    int x1 = (ti + 1);
                    int y0 = yi*(tDiv + 1);
                    int y1 = (yi + 1)*(tDiv + 1);

                    mesh.TriangleIndices.Add(x0 + y0);
                    mesh.TriangleIndices.Add(x0 + y1);
                    mesh.TriangleIndices.Add(x1 + y0);

                    mesh.TriangleIndices.Add(x1 + y0);
                    mesh.TriangleIndices.Add(x0 + y1);
                    mesh.TriangleIndices.Add(x1 + y1);
                }
            }

            mesh.Freeze();
            return mesh;
        }
示例#29
0
        private MeshGeometry3D CylinderMech(int tDiv, int yDiv, double rad, Point3D startPoint, double maxHigh, Point[] texturePoint)
        {
            try
            {
                double maxTheta = (360 / 180.0) * Math.PI;
                double minY = 0.0;
                double maxY = maxHigh;

                double dt = maxTheta / tDiv;
                double dy = (maxY - minY) / yDiv;

                MeshGeometry3D mesh = new MeshGeometry3D();

                mesh.TextureCoordinates.Add(texturePoint[0]);
                mesh.TextureCoordinates.Add(texturePoint[1]);
                mesh.TextureCoordinates.Add(texturePoint[2]);

                for (int yi = 0; yi <= yDiv; yi++)
                {
                    double y = minY + yi * dy;

                    for (int ti = 0; ti <= tDiv; ti++)
                    {
                        double t = ti * dt;

                        mesh.Positions.Add(GetPosition(t, y, rad, startPoint));
                        mesh.Normals.Add(GetNormal(t, y));
                        mesh.TextureCoordinates.Add(GetTextureCoordinate(t, y));

                        int x0 = ti;
                        int x1 = (ti + 1);
                        int y0 = yi * (tDiv + 1);
                        int y1 = (yi + 1) * (tDiv + 1);

                        mesh.TriangleIndices.Add(x0 + y0);
                        mesh.TriangleIndices.Add(x0 + y1);
                        mesh.TriangleIndices.Add(x1 + y0);

                        mesh.TriangleIndices.Add(x1 + y0);
                        mesh.TriangleIndices.Add(x0 + y1);
                        mesh.TriangleIndices.Add(x1 + y1);
                    }
                }

                mesh.Freeze();
                return mesh;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
示例#30
0
        public void loadMesh(String filename, String relativePath)
        {
            int c = m_fbx.loadMesh(filename);
            ObservableCollection<Pair<String, MeshGeometry3D>> meshList = new ObservableCollection<Pair<string, MeshGeometry3D>>();
            while (m_fbx.hasMoreMeshes())
            {
                Mesh m = new Mesh(new List<Vector3D>(), new List<Vector3D>(), new List<Vector3D>(), new List<int>());
                m_fbx.getNextMesh(m);

                MeshGeometry3D model = new MeshGeometry3D();
                for (int i = 0; i < m.vertices.Count; ++i)
                {
                    model.Positions.Add(new Point3D(m.vertices[i].X, m.vertices[i].Y, m.vertices[i].Z));
                    model.Normals.Add(m.normals[i]);
                    model.TextureCoordinates.Add(new Point(m.texcoords[i].X, m.texcoords[i].Y));
                }

                for (int i = 0; i < m.indices.Count; ++i)
                {
                    model.TriangleIndices.Add(m.indices[i]);

                }
                model.Freeze();

                String name = m.name;

                meshList.Add(new Pair<String, MeshGeometry3D>(name, model));
            }
            if (c > 0)
            {
                String name = filename.Substring(relativePath.Length, filename.Length - relativePath.Length).Trim('\\');
                m_meshes.Add(new Pair<String, ObservableCollection<Pair<String, MeshGeometry3D>>>(name, meshList));
            }
        }