Пример #1
0
        public void ParseElementBinaryLittleEndian(byte[] bytes, int bytesOffset,
                                                   out int bytesUsed, out List <int> triangles)
        {
            if (NElement == 0)
            {
                bytesUsed = 0;
                triangles = null;
                return;
            }

            var listProperty = DictProperties["vertex_indices"] as PlyListProperty;

            bytesUsed = PlyProperty.GetDataBytes(listProperty.CountNumFormat) + NElement * 3 * PlyProperty.GetDataBytes(listProperty.ValueNumFormat);

            triangles = new List <int>();

            int            facesRead             = 0;
            int            bytesRead             = 0;
            int            bytesPerTriangleIndex = 4;
            CoordinateType coordType             = _plyHeader.GlobalMeshInfoElement.GetCoordinateType();

            int[] triangle = new int[3];
            while (facesRead < NElement)
            {
                var faceIndex  = bytesOffset + bytesRead;
                var indexCount = bytes[faceIndex];
                if (indexCount == 3)
                {
                    for (int i = 0; i < indexCount; ++i)
                    {
                        triangle[i] = System.BitConverter.ToInt32(PlyElement.GetBytesSubarray(bytes, faceIndex + 1 + i * bytesPerTriangleIndex, bytesPerTriangleIndex), 0);
                    }
                    if (coordType == CoordinateType.Left)
                    {
                        triangles.AddRange(triangle);
                    }
                    else // Coordinate.Right
                    {
                        int tmp = triangle[1];
                        triangle[1] = triangle[2];
                        triangle[2] = tmp;
                        triangles.AddRange(triangle);
                    }

                    bytesRead += 1 + indexCount * bytesPerTriangleIndex;
                }
                else
                {
                    Debug.LogWarning("Warning: Found a face is not a triangle face, skipping...");
                }

                facesRead++;
            }
        }
Пример #2
0
        // TODO: test
        public void ParseElementBinaryLittleEndian(byte[] bytes, int bytesOffset, out int bytesUsed, out List <List <int> > lines)
        {
            if (NElement == 0)
            {
                bytesUsed = 0;
                lines     = null;
                return;
            }

            lines = new List <List <int> >();

            int linesRead         = 0;
            int bytesRead         = 0;
            int bytesPerLineIndex = 4;

            int[] triangle = new int[3];
            while (linesRead < NElement)
            {
                var lineIndex = bytesOffset + bytesRead;

                var count = bytes[lineIndex];
                bytesRead += 1 + count * bytesPerLineIndex;
                if (count < 2)
                {
                    Debug.LogWarning("Warning: Found a line points < 2, skipping...");
                    continue;
                }

                var line = new List <int>();
                for (int i = 0; i < count; ++i)
                {
                    line.Add(System.BitConverter.ToInt32(PlyElement.GetBytesSubarray(bytes, lineIndex + 1 + i * bytesPerLineIndex, bytesPerLineIndex), 0));
                }
                lines.Add(line);
                linesRead++;
            }
            bytesUsed = bytesRead;
        }
Пример #3
0
        public void ParseElementBinaryLittleEndian(byte[] bytes, out int bytesUsed,
                                                   out IList <Vector3> vertices, out IList <Vector3> normals, out IList <Color32> colors)
        {
            if (NElement == 0)
            {
                bytesUsed = 0;
                vertices  = null;
                normals   = null;
                colors    = null;
                return;
            }

            vertices = HasPosition ? new List <Vector3>() : null;
            normals  = HasNormal ? new List <Vector3>() : null;
            colors   = HasColor ? new List <Color32>() : null;

            int bytesPerVertex = 0;

            foreach (var keyvalue in DictProperties)
            {
                bytesPerVertex += keyvalue.Value.ValueDataBytes;
            }
            bytesUsed = bytesPerVertex * NElement;

            int[] coordinateTranslate;
            int[] signs;
            _header.GlobalMeshInfoElement.GetCoordinateTransalte(out coordinateTranslate, out signs);
            float ratioToMeter = _header.GlobalMeshInfoElement.GetUnitToMeterRatio();

            float[] value = new float[3];
            for (int i = 0; i < NElement; ++i)
            {
                int byteIndex = i * bytesPerVertex;

                if (HasPosition)
                {
                    PlyMultiProperty xmultiProperty = (DictProperties["x"] as PlyMultiProperty);
                    PlyMultiProperty ymultiProperty = (DictProperties["y"] as PlyMultiProperty);
                    PlyMultiProperty zmultiProperty = (DictProperties["z"] as PlyMultiProperty);
                    value[0] = System.BitConverter.ToSingle(PlyElement.GetBytesSubarray(bytes, byteIndex + xmultiProperty.BytesOffset, xmultiProperty.ValueDataBytes), 0);
                    value[1] = System.BitConverter.ToSingle(PlyElement.GetBytesSubarray(bytes, byteIndex + ymultiProperty.BytesOffset, ymultiProperty.ValueDataBytes), 0);
                    value[2] = System.BitConverter.ToSingle(PlyElement.GetBytesSubarray(bytes, byteIndex + zmultiProperty.BytesOffset, zmultiProperty.ValueDataBytes), 0);
                    vertices.Add(new Vector3(value[coordinateTranslate[0]] * signs[0], value[coordinateTranslate[1]] * signs[1], value[coordinateTranslate[2]] * signs[2]) * ratioToMeter);
                }

                if (HasNormal)
                {
                    PlyMultiProperty xmultiProperty = (DictProperties["nx"] as PlyMultiProperty);
                    PlyMultiProperty ymultiProperty = (DictProperties["ny"] as PlyMultiProperty);
                    PlyMultiProperty zmultiProperty = (DictProperties["nz"] as PlyMultiProperty);
                    value[0] = System.BitConverter.ToSingle(PlyElement.GetBytesSubarray(bytes, byteIndex + xmultiProperty.BytesOffset, xmultiProperty.ValueDataBytes), 0);
                    value[1] = System.BitConverter.ToSingle(PlyElement.GetBytesSubarray(bytes, byteIndex + ymultiProperty.BytesOffset, ymultiProperty.ValueDataBytes), 0);
                    value[2] = System.BitConverter.ToSingle(PlyElement.GetBytesSubarray(bytes, byteIndex + zmultiProperty.BytesOffset, zmultiProperty.ValueDataBytes), 0);
                    Vector3 normal = new Vector3(value[coordinateTranslate[0]] * signs[0], value[coordinateTranslate[1]] * signs[1], value[coordinateTranslate[2]] * signs[2]) * ratioToMeter;
                    normal.Normalize();
                    normals.Add(normal);
                }

                if (HasColor)
                {
                    PlyMultiProperty redmultiProperty   = (DictProperties["red"] as PlyMultiProperty);
                    PlyMultiProperty greenmultiProperty = (DictProperties["green"] as PlyMultiProperty);
                    PlyMultiProperty bluemultiProperty  = (DictProperties["blue"] as PlyMultiProperty);
                    PlyMultiProperty alphamultiProperty = (DictProperties["alpha"] as PlyMultiProperty);
                    byte             r = bytes[byteIndex + redmultiProperty.BytesOffset];
                    byte             g = bytes[byteIndex + greenmultiProperty.BytesOffset];
                    byte             b = bytes[byteIndex + bluemultiProperty.BytesOffset];
                    byte             a = bytes[byteIndex + alphamultiProperty.BytesOffset];
                    colors.Add(new Color32(r, g, b, a));
                }
            }
        }