Exemple #1
0
        public static G3D ReadG3d(this Stream stream, Func <string, string> renameFunc = null)
        {
            var header = G3dHeader.Default;

            GeometryAttribute ReadAttribute(Stream s2, string name, long size)
            {
                var rename = renameFunc?.Invoke(name) ?? name;

                // Check for the G3dHeader
                if (rename == "meta")
                {
                    if (size == 8)
                    {
                        var buffer = s2.ReadArray <byte>((int)size);
                        if (buffer[0] == G3dHeader.MagicA && buffer[1] == G3dHeader.MagicB)
                        {
                            header = G3dHeader.FromBytes(buffer);
                        }
                    }
                    else
                    {
                        s2.ReadArray <byte>((int)size);
                    }
                    return(null);
                }
                else
                {
                    // Figure out the correct type and then read it in
                    var desc = AttributeDescriptor.Parse(rename);
                    var dflt = desc.ToDefaultAttribute(0);
                    return(dflt.Read(stream, size));
                }
            }

            var results = stream.ReadBFast(ReadAttribute).Select(r => r.Item2);

            return(new G3D(results.Where(x => x != null), header));
        }
Exemple #2
0
        public static int ExpectedElementCount(this IGeometryAttributes self, AttributeDescriptor desc)
        {
            switch (desc.Association)
            {
            case Association.assoc_all:
                return(1);

            case Association.assoc_none:
                return(0);

            case Association.assoc_vertex:
                return(self.NumVertices);

            case Association.assoc_face:
                return(self.NumFaces);

            case Association.assoc_corner:
                return(self.NumCorners);

            case Association.assoc_edge:
                return(self.NumCorners);

            case Association.assoc_mesh:
                return(self.NumMeshes);

            case Association.assoc_instance:
                return(self.NumInstances);

            case Association.assoc_shapevertex:
                return(self.NumShapeVertices);

            case Association.assoc_shape:
                return(self.NumShapes);
            }
            return(-1);
        }
Exemple #3
0
 /// <summary>
 /// Constructor.
 /// </summary>
 protected GeometryAttribute(AttributeDescriptor descriptor, int count)
 => (Descriptor, ElementCount) = (descriptor, count);
Exemple #4
0
 public bool Equals(AttributeDescriptor other)
 => ToString() == other.ToString();
        public static GeometryAttribute ToDefaultAttribute(this AttributeDescriptor desc, int count)
        {
            switch (desc.DataType)
            {
            case DataType.dt_int8:
                if (desc.DataArity == 1)
                {
                    return(default(byte).Repeat(count).ToAttribute(desc));
                }
                if (desc.DataArity == 2)
                {
                    return(default(Byte2).Repeat(count).ToAttribute(desc));
                }
                if (desc.DataArity == 3)
                {
                    return(default(Byte3).Repeat(count).ToAttribute(desc));
                }
                if (desc.DataArity == 4)
                {
                    return(default(Byte4).Repeat(count).ToAttribute(desc));
                }
                break;

            case DataType.dt_int16:
                if (desc.DataArity == 1)
                {
                    return(default(short).Repeat(count).ToAttribute(desc));
                }
                break;

            case DataType.dt_int32:
                if (desc.DataArity == 1)
                {
                    return(default(int).Repeat(count).ToAttribute(desc));
                }
                if (desc.DataArity == 2)
                {
                    return(default(Int2).Repeat(count).ToAttribute(desc));
                }
                if (desc.DataArity == 3)
                {
                    return(default(Int3).Repeat(count).ToAttribute(desc));
                }
                if (desc.DataArity == 4)
                {
                    return(default(Int4).Repeat(count).ToAttribute(desc));
                }
                break;

            case DataType.dt_int64:
                if (desc.DataArity == 1)
                {
                    return(default(long).Repeat(count).ToAttribute(desc));
                }
                break;

            case DataType.dt_float32:
                if (desc.DataArity == 1)
                {
                    return(default(float).Repeat(count).ToAttribute(desc));
                }
                if (desc.DataArity == 2)
                {
                    return(default(Vector2).Repeat(count).ToAttribute(desc));
                }
                if (desc.DataArity == 3)
                {
                    return(default(Vector3).Repeat(count).ToAttribute(desc));
                }
                if (desc.DataArity == 4)
                {
                    return(default(Vector4).Repeat(count).ToAttribute(desc));
                }
                if (desc.DataArity == 16)
                {
                    return(default(Matrix4x4).Repeat(count).ToAttribute(desc));
                }
                break;

            case DataType.dt_float64:
                if (desc.DataArity == 1)
                {
                    return(default(double).Repeat(count).ToAttribute(desc));
                }
                if (desc.DataArity == 2)
                {
                    return(default(DVector2).Repeat(count).ToAttribute(desc));
                }
                if (desc.DataArity == 3)
                {
                    return(default(DVector3).Repeat(count).ToAttribute(desc));
                }
                if (desc.DataArity == 4)
                {
                    return(default(DVector4).Repeat(count).ToAttribute(desc));
                }
                break;
            }

            throw new Exception($"Could not create a default attribute for {desc}");
        }
 public static GeometryAttribute <T> ToAttribute <T>(this IArray <T> self, string desc, int index) where T : unmanaged
 => self.ToAttribute(AttributeDescriptor.Parse(desc).SetIndex(index));
 public static GeometryAttribute <T> ToAttribute <T>(this IArray <T> self, AttributeDescriptor desc) where T : unmanaged
 => new GeometryAttribute <T>(self, desc);
 public static GeometryAttribute <T> ToAttribute <T>(this IList <T> self, AttributeDescriptor desc) where T : unmanaged
 => self.ToIArray().ToAttribute(desc);
 public static GeometryAttribute GetOrDefaultAttribute(this IGeometryAttributes self, AttributeDescriptor desc)
 => self.GetAttribute(desc.ToString()) ?? desc.ToDefaultAttribute(self.ExpectedElementCount(desc));
 public static GeometryAttribute DefaultAttribute(this IGeometryAttributes self, AttributeDescriptor desc)
 => desc.ToDefaultAttribute(self.ExpectedElementCount(desc));
 public static GeometryAttribute DefaultAttribute(this IGeometryAttributes self, string name)
 => self.DefaultAttribute(AttributeDescriptor.Parse(name));
 public static IGeometryAttributes SetAttribute <ValueT>(this IGeometryAttributes self, IArray <ValueT> values, AttributeDescriptor desc) where ValueT : unmanaged
 => self.SetAttribute(values.ToAttribute(desc));