public static Mesh Compute(Ellipsoid ellipsoid, int numberOfPartitions, CubeMapEllipsoidVertexAttributes vertexAttributes)
        {
            if (numberOfPartitions < 0)
            {
                throw new ArgumentOutOfRangeException("numberOfPartitions");
            }

            if ((vertexAttributes & CubeMapEllipsoidVertexAttributes.Position) != CubeMapEllipsoidVertexAttributes.Position)
            {
                throw new ArgumentException("Positions must be provided.", "vertexAttributes");
            }

            Mesh mesh = new Mesh();
            mesh.PrimitiveType = PrimitiveType.Triangles;
            mesh.FrontFaceWindingOrder = WindingOrder.Counterclockwise;

            int numberOfVertices = NumberOfVertices(numberOfPartitions);
            VertexAttributeDoubleVector3 positionsAttribute = new VertexAttributeDoubleVector3("position", numberOfVertices);
            mesh.Attributes.Add(positionsAttribute);

            IndicesUnsignedInt indices = new IndicesUnsignedInt(3 * NumberOfTriangles(numberOfPartitions));
            mesh.Indices = indices;

            CubeMapMesh CubeMapMesh = new CubeMapMesh();
            CubeMapMesh.Ellipsoid = ellipsoid;
            CubeMapMesh.NumberOfPartitions = numberOfPartitions;
            CubeMapMesh.Positions = positionsAttribute.Values;
            CubeMapMesh.Indices = indices;

            if ((vertexAttributes & CubeMapEllipsoidVertexAttributes.Normal) == CubeMapEllipsoidVertexAttributes.Normal)
            {
                VertexAttributeHalfFloatVector3 normalsAttribute = new VertexAttributeHalfFloatVector3("normal", numberOfVertices);
                mesh.Attributes.Add(normalsAttribute);
                CubeMapMesh.Normals = normalsAttribute.Values;
            }

            if ((vertexAttributes & CubeMapEllipsoidVertexAttributes.TextureCoordinate) == CubeMapEllipsoidVertexAttributes.TextureCoordinate)
            {
                VertexAttributeHalfFloatVector2 textureCoordinateAttribute = new VertexAttributeHalfFloatVector2("textureCoordinate", numberOfVertices);
                mesh.Attributes.Add(textureCoordinateAttribute);
                CubeMapMesh.TextureCoordinate = textureCoordinateAttribute.Values;
            }

            //
            // Initial cube.  In the plane, z = -1:
            //
            //                   +y
            //                    |
            //             Q2     * p3     Q1
            //                  / | \
            //              p0 *--+--* p2   +x
            //                  \ | /
            //             Q3     * p1     Q4
            //                    |
            //
            // Similarly, p4 to p7 are in the plane z = 1.
            //
            CubeMapMesh.Positions.Add(new Vector3D(-1, 0, -1));
            CubeMapMesh.Positions.Add(new Vector3D(0, -1, -1));
            CubeMapMesh.Positions.Add(new Vector3D(1, 0, -1));
            CubeMapMesh.Positions.Add(new Vector3D(0, 1, -1));
            CubeMapMesh.Positions.Add(new Vector3D(-1, 0, 1));
            CubeMapMesh.Positions.Add(new Vector3D(0, -1, 1));
            CubeMapMesh.Positions.Add(new Vector3D(1, 0, 1));
            CubeMapMesh.Positions.Add(new Vector3D(0, 1, 1));

            //
            // Edges
            //
            // 0 -> 1, 1 -> 2, 2 -> 3, 3 -> 0.  Plane z = -1
            // 4 -> 5, 5 -> 6, 6 -> 7, 7 -> 4.  Plane z = 1
            // 0 -> 4, 1 -> 5, 2 -> 6, 3 -> 7.  From plane z = -1 to plane z - 1
            //
            int[] edge0to1 = AddEdgePositions(0, 1, CubeMapMesh);
            int[] edge1to2 = AddEdgePositions(1, 2, CubeMapMesh);
            int[] edge2to3 = AddEdgePositions(2, 3, CubeMapMesh);
            int[] edge3to0 = AddEdgePositions(3, 0, CubeMapMesh);

            int[] edge4to5 = AddEdgePositions(4, 5, CubeMapMesh);
            int[] edge5to6 = AddEdgePositions(5, 6, CubeMapMesh);
            int[] edge6to7 = AddEdgePositions(6, 7, CubeMapMesh);
            int[] edge7to4 = AddEdgePositions(7, 4, CubeMapMesh);

            int[] edge0to4 = AddEdgePositions(0, 4, CubeMapMesh);
            int[] edge1to5 = AddEdgePositions(1, 5, CubeMapMesh);
            int[] edge2to6 = AddEdgePositions(2, 6, CubeMapMesh);
            int[] edge3to7 = AddEdgePositions(3, 7, CubeMapMesh);

            AddFaceTriangles(edge0to4, edge0to1, edge1to5, edge4to5, CubeMapMesh); // Q3 Face
            AddFaceTriangles(edge1to5, edge1to2, edge2to6, edge5to6, CubeMapMesh); // Q4 Face
            AddFaceTriangles(edge2to6, edge2to3, edge3to7, edge6to7, CubeMapMesh); // Q1 Face
            AddFaceTriangles(edge3to7, edge3to0, edge0to4, edge7to4, CubeMapMesh); // Q2 Face
            AddFaceTriangles(ReversedArray(edge7to4), edge4to5, edge5to6, ReversedArray(edge6to7), CubeMapMesh); // Plane z = 1
            AddFaceTriangles(edge1to2, ReversedArray(edge0to1), ReversedArray(edge3to0), edge2to3, CubeMapMesh); // Plane z = -1

            CubeToEllipsoid(CubeMapMesh);
            return mesh;
        }
        public static Mesh Compute(Ellipsoid ellipsoid, int numberOfPartitions, CubeMapEllipsoidVertexAttributes vertexAttributes)
        {
            if (numberOfPartitions < 0)
            {
                throw new ArgumentOutOfRangeException("numberOfPartitions");
            }

            if ((vertexAttributes & CubeMapEllipsoidVertexAttributes.Position) != CubeMapEllipsoidVertexAttributes.Position)
            {
                throw new ArgumentException("Positions must be provided.", "vertexAttributes");
            }

            Mesh mesh = new Mesh();

            mesh.PrimitiveType         = PrimitiveType.Triangles;
            mesh.FrontFaceWindingOrder = WindingOrder.Counterclockwise;

            int numberOfVertices = NumberOfVertices(numberOfPartitions);
            VertexAttributeDoubleVector3 positionsAttribute = new VertexAttributeDoubleVector3("position", numberOfVertices);

            mesh.Attributes.Add(positionsAttribute);

            IndicesUnsignedInt indices = new IndicesUnsignedInt(3 * NumberOfTriangles(numberOfPartitions));

            mesh.Indices = indices;

            CubeMapMesh CubeMapMesh = new CubeMapMesh();

            CubeMapMesh.Ellipsoid          = ellipsoid;
            CubeMapMesh.NumberOfPartitions = numberOfPartitions;
            CubeMapMesh.Positions          = positionsAttribute.Values;
            CubeMapMesh.Indices            = indices;

            if ((vertexAttributes & CubeMapEllipsoidVertexAttributes.Normal) == CubeMapEllipsoidVertexAttributes.Normal)
            {
                VertexAttributeHalfFloatVector3 normalsAttribute = new VertexAttributeHalfFloatVector3("normal", numberOfVertices);
                mesh.Attributes.Add(normalsAttribute);
                CubeMapMesh.Normals = normalsAttribute.Values;
            }

            if ((vertexAttributes & CubeMapEllipsoidVertexAttributes.TextureCoordinate) == CubeMapEllipsoidVertexAttributes.TextureCoordinate)
            {
                VertexAttributeHalfFloatVector2 textureCoordinateAttribute = new VertexAttributeHalfFloatVector2("textureCoordinate", numberOfVertices);
                mesh.Attributes.Add(textureCoordinateAttribute);
                CubeMapMesh.TextureCoordinate = textureCoordinateAttribute.Values;
            }

            //
            // Initial cube.  In the plane, z = -1:
            //
            //                   +y
            //                    |
            //             Q2     * p3     Q1
            //                  / | \
            //              p0 *--+--* p2   +x
            //                  \ | /
            //             Q3     * p1     Q4
            //                    |
            //
            // Similarly, p4 to p7 are in the plane z = 1.
            //
            CubeMapMesh.Positions.Add(new Vector3D(-1, 0, -1));
            CubeMapMesh.Positions.Add(new Vector3D(0, -1, -1));
            CubeMapMesh.Positions.Add(new Vector3D(1, 0, -1));
            CubeMapMesh.Positions.Add(new Vector3D(0, 1, -1));
            CubeMapMesh.Positions.Add(new Vector3D(-1, 0, 1));
            CubeMapMesh.Positions.Add(new Vector3D(0, -1, 1));
            CubeMapMesh.Positions.Add(new Vector3D(1, 0, 1));
            CubeMapMesh.Positions.Add(new Vector3D(0, 1, 1));

            //
            // Edges
            //
            // 0 -> 1, 1 -> 2, 2 -> 3, 3 -> 0.  Plane z = -1
            // 4 -> 5, 5 -> 6, 6 -> 7, 7 -> 4.  Plane z = 1
            // 0 -> 4, 1 -> 5, 2 -> 6, 3 -> 7.  From plane z = -1 to plane z - 1
            //
            int[] edge0to1 = AddEdgePositions(0, 1, CubeMapMesh);
            int[] edge1to2 = AddEdgePositions(1, 2, CubeMapMesh);
            int[] edge2to3 = AddEdgePositions(2, 3, CubeMapMesh);
            int[] edge3to0 = AddEdgePositions(3, 0, CubeMapMesh);

            int[] edge4to5 = AddEdgePositions(4, 5, CubeMapMesh);
            int[] edge5to6 = AddEdgePositions(5, 6, CubeMapMesh);
            int[] edge6to7 = AddEdgePositions(6, 7, CubeMapMesh);
            int[] edge7to4 = AddEdgePositions(7, 4, CubeMapMesh);

            int[] edge0to4 = AddEdgePositions(0, 4, CubeMapMesh);
            int[] edge1to5 = AddEdgePositions(1, 5, CubeMapMesh);
            int[] edge2to6 = AddEdgePositions(2, 6, CubeMapMesh);
            int[] edge3to7 = AddEdgePositions(3, 7, CubeMapMesh);

            AddFaceTriangles(edge0to4, edge0to1, edge1to5, edge4to5, CubeMapMesh);                               // Q3 Face
            AddFaceTriangles(edge1to5, edge1to2, edge2to6, edge5to6, CubeMapMesh);                               // Q4 Face
            AddFaceTriangles(edge2to6, edge2to3, edge3to7, edge6to7, CubeMapMesh);                               // Q1 Face
            AddFaceTriangles(edge3to7, edge3to0, edge0to4, edge7to4, CubeMapMesh);                               // Q2 Face
            AddFaceTriangles(ReversedArray(edge7to4), edge4to5, edge5to6, ReversedArray(edge6to7), CubeMapMesh); // Plane z = 1
            AddFaceTriangles(edge1to2, ReversedArray(edge0to1), ReversedArray(edge3to0), edge2to3, CubeMapMesh); // Plane z = -1

            CubeToEllipsoid(CubeMapMesh);
            return(mesh);
        }