Ejemplo n.º 1
0
        private void ConditionPatch(AccPatch patch)
        {
            var neighborPoints = new List<Point>();
            var others = new Point[3];

            var v0 = patch.Points[0];
            var v1 = patch.Points[1];
            var v2 = patch.Points[2];
            var v3 = patch.Points[3];

            patch.Valences.Clear();
            patch.Prefixes.Clear();

            // v0
            others[0] = v1; others[1] = v2; others[2] = v3;
            patch.Valences.Add(ConditionPoint(v0, others, neighborPoints));
            patch.Prefixes.Add(neighborPoints.Count + 4);

            // v1
            others[0] = v2; others[1] = v3; others[2] = v0;
            patch.Valences.Add(ConditionPoint(v1, others, neighborPoints));
            patch.Prefixes.Add(neighborPoints.Count + 4);

            // v2
            others[0] = v3; others[1] = v0; others[2] = v1;
            patch.Valences.Add(ConditionPoint(v2, others, neighborPoints));
            patch.Prefixes.Add(neighborPoints.Count + 4);

            // v3
            others[0] = v0; others[1] = v1; others[2] = v2;
            patch.Valences.Add(ConditionPoint(v3, others, neighborPoints));
            patch.Prefixes.Add(neighborPoints.Count + 4);

            patch.Points.AddRange(neighborPoints);
        }
Ejemplo n.º 2
0
        private void ConditionPatch(AccPatch patch)
        {
            var neighborPoints = new List <Point>();
            var others         = new Point[3];

            var v0 = patch.Points[0];
            var v1 = patch.Points[1];
            var v2 = patch.Points[2];
            var v3 = patch.Points[3];

            patch.Valences.Clear();
            patch.Prefixes.Clear();

            // v0
            others[0] = v1; others[1] = v2; others[2] = v3;
            patch.Valences.Add(ConditionPoint(v0, others, neighborPoints));
            patch.Prefixes.Add(neighborPoints.Count + 4);

            // v1
            others[0] = v2; others[1] = v3; others[2] = v0;
            patch.Valences.Add(ConditionPoint(v1, others, neighborPoints));
            patch.Prefixes.Add(neighborPoints.Count + 4);

            // v2
            others[0] = v3; others[1] = v0; others[2] = v1;
            patch.Valences.Add(ConditionPoint(v2, others, neighborPoints));
            patch.Prefixes.Add(neighborPoints.Count + 4);

            // v3
            others[0] = v0; others[1] = v1; others[2] = v2;
            patch.Valences.Add(ConditionPoint(v3, others, neighborPoints));
            patch.Prefixes.Add(neighborPoints.Count + 4);

            patch.Points.AddRange(neighborPoints);
        }
Ejemplo n.º 3
0
        private void Load(string path)
        {
            var numberRegex = new Regex(@"-?[\d]+(?:\.[\d]*)?(e[-|+]?\d+)?", RegexOptions.IgnoreCase);
            var faceRegex = new Regex(@"(\d+)/([\d]*)/([\d]*)", RegexOptions.IgnoreCase);

            Vertices.Clear();
            Normals.Clear();
            Textures.Clear();
            Faces.Clear();

            foreach (var line in File.ReadLines(path).Where(line => numberRegex.IsMatch(line)))
            {
                var matches = numberRegex.Matches(line);

                if (line.StartsWith("v "))
                {
                    if (matches.Count != 3)
                        throw new FileLoadException("Error parsing vertices.");

                    Vertices.Add(
                        new Vector4 {
                            X = float.Parse(matches[0].Value),
                            Y = float.Parse(matches[1].Value),
                            Z = float.Parse(matches[2].Value),
                            W = 1f
                        });
                }
                else if (line.StartsWith("vt "))
                {
                    if (matches.Count != 2)
                        throw new FileLoadException("Error parsing textures.");

                    Textures.Add(
                        new Vector2 {
                            X = float.Parse(matches[0].Value),
                            Y = float.Parse(matches[1].Value)
                        });
                }
                else if (line.StartsWith("vn "))
                {
                    if (matches.Count != 3)
                        throw new FileLoadException("Error parsing normals.");

                    Normals.Add(
                        Vector3.Normalize(new Vector3 {
                            X = float.Parse(matches[0].Value),
                            Y = float.Parse(matches[1].Value),
                            Z = float.Parse(matches[2].Value)
                        }));
                }
                else if (line.StartsWith("f "))
                {
                    matches = faceRegex.Matches(line);

                    if (matches.Count != 4)
                        throw new FileLoadException("Error parsing faces.");

                    var face = new Face();
                    foreach (var i in Enumerable.Range(0, 4))
                    {
                        var point =
                            new Point {
                                PositionIndex = int.Parse(matches[i].Groups[1].Value) - 1,
                                TextureIndex = matches[i].Groups[2].Value == String.Empty ? 0 : int.Parse(matches[i].Groups[2].Value) - 1,
                                NormalIndex = int.Parse(matches[i].Groups[3].Value) - 1
                            };

                        face.Points.Add(point);
                        _pointIndexLookUp[point] = (uint) (Faces.Count * 4 + i);
                    }

                    Faces.Add(face);

                    var patch = new AccPatch {
                            Points = new List<Point>(face.Points),
                            Prefixes = new List<int>(),
                            Valences = new List<int>()
                        };

                    foreach (var point in face.Points)
                    {
                        if (_pointNeighborLookUp.ContainsKey(point.PositionIndex))
                            _pointNeighborLookUp[point.PositionIndex].Add(patch);
                        else
                            _pointNeighborLookUp[point.PositionIndex] = new List<AccPatch> { patch };
                    }

                    AccPatches.Add(patch);
                }
               }
        }
Ejemplo n.º 4
0
        private void Load(string path)
        {
            var numberRegex = new Regex(@"-?[\d]+(?:\.[\d]*)?(e[-|+]?\d+)?", RegexOptions.IgnoreCase);
            var faceRegex   = new Regex(@"(\d+)/([\d]*)/([\d]*)", RegexOptions.IgnoreCase);

            Vertices.Clear();
            Normals.Clear();
            Textures.Clear();
            Faces.Clear();

            foreach (var line in File.ReadLines(path).Where(line => numberRegex.IsMatch(line)))
            {
                var matches = numberRegex.Matches(line);

                if (line.StartsWith("v "))
                {
                    if (matches.Count != 3)
                    {
                        throw new FileLoadException("Error parsing vertices.");
                    }

                    Vertices.Add(
                        new Vector4 {
                        X = float.Parse(matches[0].Value),
                        Y = float.Parse(matches[1].Value),
                        Z = float.Parse(matches[2].Value),
                        W = 1f
                    });
                }
                else if (line.StartsWith("vt "))
                {
                    if (matches.Count != 2)
                    {
                        throw new FileLoadException("Error parsing textures.");
                    }

                    Textures.Add(
                        new Vector2 {
                        X = float.Parse(matches[0].Value),
                        Y = float.Parse(matches[1].Value)
                    });
                }
                else if (line.StartsWith("vn "))
                {
                    if (matches.Count != 3)
                    {
                        throw new FileLoadException("Error parsing normals.");
                    }

                    Normals.Add(
                        Vector3.Normalize(new Vector3 {
                        X = float.Parse(matches[0].Value),
                        Y = float.Parse(matches[1].Value),
                        Z = float.Parse(matches[2].Value)
                    }));
                }
                else if (line.StartsWith("f "))
                {
                    matches = faceRegex.Matches(line);

                    if (matches.Count != 4)
                    {
                        throw new FileLoadException("Error parsing faces.");
                    }

                    var face = new Face();
                    foreach (var i in Enumerable.Range(0, 4))
                    {
                        var point =
                            new Point {
                            PositionIndex = int.Parse(matches[i].Groups[1].Value) - 1,
                            TextureIndex  = matches[i].Groups[2].Value == String.Empty ? 0 : int.Parse(matches[i].Groups[2].Value) - 1,
                            NormalIndex   = int.Parse(matches[i].Groups[3].Value) - 1
                        };

                        face.Points.Add(point);
                        _pointIndexLookUp[point] = (uint)(Faces.Count * 4 + i);
                    }

                    Faces.Add(face);

                    var patch = new AccPatch {
                        Points   = new List <Point>(face.Points),
                        Prefixes = new List <int>(),
                        Valences = new List <int>()
                    };

                    foreach (var point in face.Points)
                    {
                        if (_pointNeighborLookUp.ContainsKey(point.PositionIndex))
                        {
                            _pointNeighborLookUp[point.PositionIndex].Add(patch);
                        }
                        else
                        {
                            _pointNeighborLookUp[point.PositionIndex] = new List <AccPatch> {
                                patch
                            }
                        };
                    }

                    AccPatches.Add(patch);
                }
            }
        }