public static void VerifyIcosahedron()
        {
            // Get the points.
            Point3D A, B, C, D, E, F, G, H, I, J, K, L;

            G3.IcosahedronPoints(
                out A, out B, out C, out D, out E, out F,
                out G, out H, out I, out J, out K, out L);

            // Verify the points.
            G3.VerifyPoints(A, B, C, D, E, F, G, H, I, J, K, L);

            // Verify the faces.
            G3.VerifyPolygon(A, C, B);
            G3.VerifyPolygon(A, D, C);
            G3.VerifyPolygon(A, E, D);
            G3.VerifyPolygon(A, F, E);
            G3.VerifyPolygon(A, B, F);
            G3.VerifyPolygon(D, K, C);
            G3.VerifyPolygon(C, K, J);
            G3.VerifyPolygon(C, J, B);
            G3.VerifyPolygon(B, J, I);
            G3.VerifyPolygon(B, I, F);
            G3.VerifyPolygon(F, I, H);
            G3.VerifyPolygon(F, H, E);
            G3.VerifyPolygon(E, H, G);
            G3.VerifyPolygon(E, G, D);
            G3.VerifyPolygon(D, G, K);
            G3.VerifyPolygon(L, J, K);
            G3.VerifyPolygon(L, I, J);
            G3.VerifyPolygon(L, H, I);
            G3.VerifyPolygon(L, G, H);
            G3.VerifyPolygon(L, K, G);
        }
        public static void VerifyDodecahedron()
        {
            // Get the points.
            Point3D A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T;

            G3.DodecahedronPoints(
                out A, out B, out C, out D, out E,
                out F, out G, out H, out I, out J,
                out K, out L, out M, out N, out O,
                out P, out Q, out R, out S, out T);

            // Verify the points.
            G3.VerifyPoints(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T);

            // Verify the faces.
            G3.VerifyPolygon(E, D, C, B, A);
            G3.VerifyPolygon(A, B, G, K, F);
            G3.VerifyPolygon(A, F, O, J, E);
            G3.VerifyPolygon(E, J, N, I, D);
            G3.VerifyPolygon(D, I, M, H, C);
            G3.VerifyPolygon(C, H, L, G, B);
            G3.VerifyPolygon(K, P, T, O, F);
            G3.VerifyPolygon(O, T, S, N, J);
            G3.VerifyPolygon(N, S, R, M, I);
            G3.VerifyPolygon(M, R, Q, L, H);
            G3.VerifyPolygon(L, Q, P, K, G);
            G3.VerifyPolygon(P, Q, R, S, T);
        }
        // Make an icosahedron without texture coordinates or smoothing.
        public static void AddIcosahedron(this MeshGeometry3D mesh)
        {
            // Get the points.
            Point3D A, B, C, D, E, F, G, H, I, J, K, L;

            G3.IcosahedronPoints(
                out A, out B, out C, out D, out E, out F,
                out G, out H, out I, out J, out K, out L);

            // Make the faces.
            mesh.AddPolygon(A, C, B);
            mesh.AddPolygon(A, D, C);
            mesh.AddPolygon(A, E, D);
            mesh.AddPolygon(A, F, E);
            mesh.AddPolygon(A, B, F);
            mesh.AddPolygon(D, K, C);
            mesh.AddPolygon(C, K, J);
            mesh.AddPolygon(C, J, B);
            mesh.AddPolygon(B, J, I);
            mesh.AddPolygon(B, I, F);
            mesh.AddPolygon(F, I, H);
            mesh.AddPolygon(F, H, E);
            mesh.AddPolygon(E, H, G);
            mesh.AddPolygon(E, G, D);
            mesh.AddPolygon(D, G, K);
            mesh.AddPolygon(L, J, K);
            mesh.AddPolygon(L, I, J);
            mesh.AddPolygon(L, H, I);
            mesh.AddPolygon(L, G, H);
            mesh.AddPolygon(L, K, G);
        }
        // Make a dodecahedron without texture coordinates or smoothing.
        public static void AddDodecahedron(this MeshGeometry3D mesh)
        {
            // Get the points.
            Point3D A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T;

            G3.DodecahedronPoints(
                out A, out B, out C, out D, out E,
                out F, out G, out H, out I, out J,
                out K, out L, out M, out N, out O,
                out P, out Q, out R, out S, out T);

            // Make the faces.
            mesh.AddPolygon(E, D, C, B, A);
            mesh.AddPolygon(A, B, G, K, F);
            mesh.AddPolygon(A, F, O, J, E);
            mesh.AddPolygon(E, J, N, I, D);
            mesh.AddPolygon(D, I, M, H, C);
            mesh.AddPolygon(C, H, L, G, B);
            mesh.AddPolygon(K, P, T, O, F);
            mesh.AddPolygon(O, T, S, N, J);
            mesh.AddPolygon(N, S, R, M, I);
            mesh.AddPolygon(M, R, Q, L, H);
            mesh.AddPolygon(L, Q, P, K, G);
            mesh.AddPolygon(P, Q, R, S, T);
        }
        public static void AddRegularPolygon(this MeshGeometry3D mesh,
                                             int numSides, Point3D center, Vector3D vx, Vector3D vy,
                                             Dictionary <Point3D, int> pointDict = null,
                                             Point[] textureCoords = null)
        {
            // Generate the points.
            Point3D[] points = G3.MakePolygonPoints(numSides, center, vx, vy);

            // Make the polygon.
            mesh.AddPolygon(points, textureCoords);
        }
        // Make a tetrahedron without texture coordinates or smoothing.
        public static void AddTetrahedron(this MeshGeometry3D mesh, bool centered = true)
        {
            // Get the points.
            Point3D A, B, C, D;

            G3.TetrahedronPoints(out A, out B, out C, out D, centered);

            // Make the faces.
            mesh.AddPolygon(A, B, C);
            mesh.AddPolygon(A, C, D);
            mesh.AddPolygon(A, D, B);
            mesh.AddPolygon(D, C, B);
        }
        // Make a cube without texture coordinates or smoothing.
        public static void AddCube(this MeshGeometry3D mesh)
        {
            // Get the points.
            Point3D A, B, C, D, E, F, G, H;

            G3.CubePoints(out A, out B, out C, out D, out E, out F, out G, out H);

            // Make the faces.
            mesh.AddPolygon(A, B, C, D);
            mesh.AddPolygon(A, D, H, E);
            mesh.AddPolygon(A, E, F, B);
            mesh.AddPolygon(G, C, B, F);
            mesh.AddPolygon(G, F, E, H);
            mesh.AddPolygon(G, H, D, C);
        }
        public static void VerifyTetrahedron()
        {
            // Get the points.
            Point3D A, B, C, D;

            G3.TetrahedronPoints(out A, out B, out C, out D, true);

            // Verify the points.
            G3.VerifyPoints(A, B, C, D);

            // Verify the faces.
            G3.VerifyPolygon(A, B, C);
            G3.VerifyPolygon(A, C, D);
            G3.VerifyPolygon(A, D, B);
            G3.VerifyPolygon(D, C, B);
        }
        // Make an octahedron without texture coordinates or smoothing.
        public static void AddOctahedron(this MeshGeometry3D mesh)
        {
            // Get the points.
            Point3D A, B, C, D, E, F;

            G3.OctahedronPoints(out A, out B, out C, out D, out E, out F);

            // Make the faces.
            mesh.AddPolygon(A, B, C);
            mesh.AddPolygon(A, C, D);
            mesh.AddPolygon(A, D, E);
            mesh.AddPolygon(A, E, B);
            mesh.AddPolygon(F, B, E);
            mesh.AddPolygon(F, C, B);
            mesh.AddPolygon(F, D, C);
            mesh.AddPolygon(F, E, D);
        }
        public static void VerifyCube()
        {
            // Get the points.
            Point3D A, B, C, D, E, F, G, H;

            G3.CubePoints(out A, out B, out C, out D, out E, out F, out G, out H);

            // Verify the points.
            G3.VerifyPoints(A, B, C, D, E, F, G, H);

            // Verify the faces.
            G3.VerifyPolygon(A, B, C, D);
            G3.VerifyPolygon(A, D, H, E);
            G3.VerifyPolygon(A, E, F, B);
            G3.VerifyPolygon(G, C, B, F);
            G3.VerifyPolygon(G, F, E, H);
            G3.VerifyPolygon(G, H, D, C);
        }
        public static void VerifyOctahedron()
        {
            // Get the points.
            Point3D A, B, C, D, E, F;

            G3.OctahedronPoints(out A, out B, out C, out D, out E, out F);

            // Verify the points.
            G3.VerifyPoints(A, B, C, D);

            // Verify the faces.
            G3.VerifyPolygon(A, B, C);
            G3.VerifyPolygon(A, C, D);
            G3.VerifyPolygon(A, D, E);
            G3.VerifyPolygon(A, E, B);
            G3.VerifyPolygon(F, B, E);
            G3.VerifyPolygon(F, C, B);
            G3.VerifyPolygon(F, D, C);
            G3.VerifyPolygon(F, E, D);
        }
        // Make a torus without texture coordinates.
        public static void AddTorus(this MeshGeometry3D mesh,
                                    Point3D center, double R, double r, int numTheta, int numPhi,
                                    bool smooth = false)
        {
            // Make a point dictionary if needed.
            Dictionary <Point3D, int> pointDict = null;

            if (smooth)
            {
                pointDict = new Dictionary <Point3D, int>();
            }

            // Generate the points.
            double dtheta = 2 * Math.PI / numTheta;
            double dphi   = 2 * Math.PI / numPhi;
            double theta  = 0;

            for (int t = 0; t < numTheta; t++)
            {
                double phi = 0;
                for (int p = 0; p < numPhi; p++)
                {
                    // Find this piece's points.
                    Point3D[] points =
                    {
                        G3.TorusPoint(center, R, r, theta + dtheta, phi),
                        G3.TorusPoint(center, R, r, theta + dtheta, phi + dphi),
                        G3.TorusPoint(center, R, r, theta,          phi + dphi),
                        G3.TorusPoint(center, R, r, theta,          phi),
                    };

                    // Make the polygon.
                    mesh.AddPolygon(pointDict, points);

                    phi += dphi;
                }
                theta += dtheta;
            }
        }
Exemplo n.º 13
0
        // Define the model.
        private void DefineModel(Model3DGroup group)
        {
            // Axes.
            MeshExtensions.AddAxes(group);

            MeshGeometry3D mesh1 = new MeshGeometry3D();
            MeshGeometry3D mesh2 = new MeshGeometry3D();
            MeshGeometry3D mesh3 = new MeshGeometry3D();
            MeshGeometry3D mesh4 = new MeshGeometry3D();

            double  x0     = -5;
            Point3D center = new Point3D(x0, 0, 4.5);

            // Box.
            mesh1.AddBox(center + new Vector3D(-0.5, 0, -1),
                         D3.XVector(1), D3.YVector(3), D3.ZVector(2));
            center.X += 2;

            // Box wrapped.
            mesh2.AddBoxWrapped(center + new Vector3D(-0.5, 0, -1),
                                D3.XVector(1), D3.YVector(3), D3.ZVector(2));
            center.X += 2;

            // Cone.
            Point3D[] circle = G3.MakePolygonPoints(20, center, D3.XVector(1), D3.ZVector(-1));
            mesh1.AddCone(center, circle, D3.YVector(4));
            center.X += 2;

            // Cone frustum.
            circle = G3.MakePolygonPoints(20, center, D3.XVector(1), D3.ZVector(-1));
            mesh1.AddConeFrustum(center, circle, D3.YVector(4), 3);
            center.X += 2;

            // Cone frustum w/cutting plane.
            circle = G3.MakePolygonPoints(20, center, D3.XVector(1), D3.ZVector(-1));
            mesh1.AddConeFrustum(center, circle, D3.YVector(4),
                                 center + new Vector3D(0, 2, 0), new Vector3D(0, 1, 1));
            center.X += 2;

            // Start a new row.
            center.X  = x0;
            center.Z -= 3;

            // Cube.
            MeshGeometry3D cubeMesh = new MeshGeometry3D();

            cubeMesh.AddCube();
            cubeMesh.ApplyTransformation(new ScaleTransform3D(1, 1.5, 0.5));
            cubeMesh.ApplyTransformation(
                new TranslateTransform3D(center.X, center.Y + 1.5, 0));
            mesh3.Merge(cubeMesh);
            center.X += 2;

            // Cylinder.
            Point3D[] polygon = G3.MakePolygonPoints(7, center, D3.XVector(1), D3.ZVector(-1));
            mesh1.AddCylinder(polygon, new Vector3D(0, 2, -1));
            center.X += 2;

            // Cylinder.
            circle = G3.MakePolygonPoints(20, center, D3.XVector(1), D3.ZVector(-1));
            mesh1.AddCylinder(circle, new Vector3D(0, 2, -1), true);
            center.X += 2;

            // Cylinder w/cutting planes.
            polygon = G3.MakePolygonPoints(7, center, D3.XVector(1), D3.ZVector(-1));
            mesh1.AddCylinder(polygon, new Vector3D(0, 1, 0),
                              center + new Vector3D(0, 2, 0), new Vector3D(0, 2, 1),
                              center + new Vector3D(0, -2, 0), new Vector3D(0, 2, -1));
            center.X += 2;

            // Cylinder w/cutting planes.
            polygon = G3.MakePolygonPoints(7, center, D3.XVector(1), D3.ZVector(-1));
            mesh1.AddCylinder(polygon, new Vector3D(0, 1, 0),
                              center + new Vector3D(0, 2, 0), new Vector3D(0, 2, 1),
                              center + new Vector3D(0, -2, 0), new Vector3D(0, 2, -1), true);
            center.X += 2;

            // Start a new row.
            center.X  = x0;
            center.Z -= 3;

            // Dodecahedron.
            MeshGeometry3D dodMesh = new MeshGeometry3D();

            dodMesh.AddDodecahedron();
            double dodScale = 1 / G3.DodecahedronCircumradius();

            dodMesh.ApplyTransformation(new ScaleTransform3D(dodScale, dodScale, dodScale));
            dodMesh.ApplyTransformation(
                new TranslateTransform3D(center.X, center.Y + 0.5, center.Z));
            mesh3.Merge(dodMesh);
            center.X += 2;

            // Frustum.
            polygon = G3.MakePolygonPoints(5, center, D3.XVector(1), D3.ZVector(-1));
            mesh1.AddFrustum(center, polygon, D3.YVector(4), 2);
            center.X += 2;

            // Icosahedron.
            MeshGeometry3D icoMesh = new MeshGeometry3D();

            icoMesh.AddIcosahedron();
            double icoScale = 1 / G3.IcosahedronCircumradius();

            icoMesh.ApplyTransformation(new ScaleTransform3D(icoScale, icoScale, icoScale));
            icoMesh.ApplyTransformation(
                new TranslateTransform3D(center.X, center.Y + 0.5, center.Z));
            mesh3.Merge(icoMesh);
            center.X += 2;

            // Octahedron.
            MeshGeometry3D octMesh = new MeshGeometry3D();

            octMesh.AddOctahedron();
            double octScale = 1 / G3.OctahedronCircumradius();

            octMesh.ApplyTransformation(new ScaleTransform3D(octScale, octScale, octScale));
            octMesh.ApplyTransformation(
                new TranslateTransform3D(center.X, center.Y + 0.5, center.Z));
            mesh3.Merge(octMesh);
            center.X += 2;

            // Pyramid.
            polygon = G3.MakePolygonPoints(6, center, D3.XVector(1), D3.ZVector(-1));
            mesh1.AddPyramid(center, polygon, D3.YVector(3));
            center.X += 2;

            // Start a new row.
            center.X  = x0;
            center.Z -= 3;

            // Sphere.
            mesh1.AddSphere(center + new Vector3D(0, 1, 0), 1, 20, 10);
            center.X += 2;

            // Tetrahedron.
            MeshGeometry3D tetMesh = new MeshGeometry3D();

            tetMesh.AddTetrahedron();
            double tetScale = 1 / G3.TetrahedronCircumradius();

            tetMesh.ApplyTransformation(new ScaleTransform3D(tetScale, tetScale, tetScale));
            tetMesh.ApplyTransformation(
                new TranslateTransform3D(center.X, center.Y + 0.5, center.Z));
            mesh3.Merge(tetMesh);
            center.X += 2;

            // Textured sphere.
            mesh4.Positions.Add(new Point3D());
            mesh4.TextureCoordinates.Add(new Point(1.01, 1.01));
            mesh4.AddTexturedSphere(center + new Vector3D(0, 1, 0), 1, 20, 10);
            center.X += 2;

            // Textured torus.
            mesh4.AddTexturedTorus(center + new Vector3D(0, 1, 0), 0.6, 0.4, 30, 15);
            center.X += 2;

            // Torus.
            mesh1.AddTorus(center + new Vector3D(0, 1, 0), 0.6, 0.4, 30, 15);
            center.X += 2;

            // Start a new row.
            center.X  = x0;
            center.Z -= 3;

            group.Children.Add(mesh1.MakeModel(Brushes.LightGreen));
            group.Children.Add(mesh2.MakeModel("wrapper.png"));
            group.Children.Add(mesh3.MakeModel(Brushes.LightBlue));
            group.Children.Add(mesh4.MakeModel("world.jpg"));
        }
        // Add a textured torus.
        public static void AddTexturedTorus(this MeshGeometry3D mesh,
                                            Point3D center, double R, double r, int numTheta, int numPhi,
                                            bool smooth = false)
        {
            double dtheta = 2 * Math.PI / numTheta;
            double dphi   = 2 * Math.PI / numPhi;
            double theta  = Math.PI;        // Puts the texture's top/bottom on the inside.

            for (int t = 0; t < numTheta; t++)
            {
                double phi = 0;
                for (int p = 0; p < numPhi; p++)
                {
                    // Find this piece's points.
                    Point3D point1 = G3.TorusPoint(center, R, r, theta, phi).Round();
                    Point3D point2 = G3.TorusPoint(center, R, r, theta + dtheta, phi).Round();
                    Point3D point3 = G3.TorusPoint(center, R, r, theta + dtheta, phi + dphi).Round();
                    Point3D point4 = G3.TorusPoint(center, R, r, theta, phi + dphi).Round();

                    // Find this piece's normals.
                    Vector3D normal1 = G3.TorusNormal(D3.Origin, R, r, theta, phi);
                    Vector3D normal2 = G3.TorusNormal(D3.Origin, R, r, theta + dtheta, phi);
                    Vector3D normal3 = G3.TorusNormal(D3.Origin, R, r, theta + dtheta, phi + dphi);
                    Vector3D normal4 = G3.TorusNormal(D3.Origin, R, r, theta, phi + dphi);

                    // Find this piece's texture coordinates.
                    Point coords1 = new Point(1 - (double)p / numPhi, 1 - (double)t / numTheta);
                    Point coords2 = new Point(1 - (double)p / numPhi, 1 - (double)(t + 1) / numTheta);
                    Point coords3 = new Point(1 - (double)(p + 1) / numPhi, 1 - (double)(t + 1) / numTheta);
                    Point coords4 = new Point(1 - (double)(p + 1) / numPhi, 1 - (double)t / numTheta);

                    // Make the first triangle.
                    int index = mesh.Positions.Count;
                    mesh.Positions.Add(point1);
                    if (smooth)
                    {
                        mesh.Normals.Add(normal1);
                    }
                    mesh.TextureCoordinates.Add(coords1);

                    mesh.Positions.Add(point2);
                    if (smooth)
                    {
                        mesh.Normals.Add(normal2);
                    }
                    mesh.TextureCoordinates.Add(coords2);

                    mesh.Positions.Add(point3);
                    if (smooth)
                    {
                        mesh.Normals.Add(normal3);
                    }
                    mesh.TextureCoordinates.Add(coords3);

                    mesh.TriangleIndices.Add(index++);
                    mesh.TriangleIndices.Add(index++);
                    mesh.TriangleIndices.Add(index++);

                    // Make the second triangle.
                    mesh.Positions.Add(point1);
                    if (smooth)
                    {
                        mesh.Normals.Add(normal1);
                    }
                    mesh.TextureCoordinates.Add(coords1);

                    mesh.Positions.Add(point3);
                    if (smooth)
                    {
                        mesh.Normals.Add(normal3);
                    }
                    mesh.TextureCoordinates.Add(coords3);

                    mesh.Positions.Add(point4);
                    if (smooth)
                    {
                        mesh.Normals.Add(normal4);
                    }
                    mesh.TextureCoordinates.Add(coords4);

                    mesh.TriangleIndices.Add(index++);
                    mesh.TriangleIndices.Add(index++);
                    mesh.TriangleIndices.Add(index++);

                    phi += dphi;
                }
                theta += dtheta;
            }

            // Add texture coordinates 1.01 to prevent "seams."
            mesh.Positions.Add(new Point3D());
            mesh.TextureCoordinates.Add(new Point(1.01, 1.01));
        }
        // Add a sphere with texture coordinates.
        public static void AddTexturedSphere(this MeshGeometry3D mesh,
                                             Point3D center, double radius, int numTheta, int numPhi,
                                             bool smooth = false)
        {
            double dtheta = 2 * Math.PI / numTheta;
            double dphi   = Math.PI / numPhi;
            double theta  = 0;

            for (int t = 0; t < numTheta; t++)
            {
                double phi = 0;
                for (int p = 0; p < numPhi; p++)
                {
                    // Find this piece's points.
                    Point3D point1 = G3.SpherePoint(center, radius, theta, phi).Round();
                    Point3D point2 = G3.SpherePoint(center, radius, theta, phi + dphi).Round();
                    Point3D point3 = G3.SpherePoint(center, radius, theta + dtheta, phi + dphi).Round();
                    Point3D point4 = G3.SpherePoint(center, radius, theta + dtheta, phi).Round();

                    // Find this piece's texture coordinates.
                    Point coords1 = new Point((double)t / numTheta, (double)p / numPhi);
                    Point coords2 = new Point((double)t / numTheta, (double)(p + 1) / numPhi);
                    Point coords3 = new Point((double)(t + 1) / numTheta, (double)(p + 1) / numPhi);
                    Point coords4 = new Point((double)(t + 1) / numTheta, (double)p / numPhi);

                    // Find this piece's normals.
                    Vector3D normal1 = (Vector3D)G3.SpherePoint(D3.Origin, 1, theta, phi).Round();
                    Vector3D normal2 = (Vector3D)G3.SpherePoint(D3.Origin, 1, theta, phi + dphi).Round();
                    Vector3D normal3 = (Vector3D)G3.SpherePoint(D3.Origin, 1, theta + dtheta, phi + dphi).Round();
                    Vector3D normal4 = (Vector3D)G3.SpherePoint(D3.Origin, 1, theta + dtheta, phi).Round();

                    // Make the first triangle.
                    int index = mesh.Positions.Count;
                    mesh.Positions.Add(point1);
                    if (smooth)
                    {
                        mesh.Normals.Add(normal1);
                    }
                    mesh.TextureCoordinates.Add(coords1);

                    mesh.Positions.Add(point2);
                    if (smooth)
                    {
                        mesh.Normals.Add(normal2);
                    }
                    mesh.TextureCoordinates.Add(coords2);

                    mesh.Positions.Add(point3);
                    if (smooth)
                    {
                        mesh.Normals.Add(normal3);
                    }
                    mesh.TextureCoordinates.Add(coords3);

                    mesh.TriangleIndices.Add(index++);
                    mesh.TriangleIndices.Add(index++);
                    mesh.TriangleIndices.Add(index++);

                    // Make the second triangle.
                    mesh.Positions.Add(point1);
                    if (smooth)
                    {
                        mesh.Normals.Add(normal1);
                    }
                    mesh.TextureCoordinates.Add(coords1);

                    mesh.Positions.Add(point3);
                    if (smooth)
                    {
                        mesh.Normals.Add(normal3);
                    }
                    mesh.TextureCoordinates.Add(coords3);

                    mesh.Positions.Add(point4);
                    if (smooth)
                    {
                        mesh.Normals.Add(normal4);
                    }
                    mesh.TextureCoordinates.Add(coords4);

                    mesh.TriangleIndices.Add(index++);
                    mesh.TriangleIndices.Add(index++);
                    mesh.TriangleIndices.Add(index++);

                    phi += dphi;
                }
                theta += dtheta;
            }
        }