Exemplo n.º 1
0
        public static BvhDocument Parse(string text)
        {
            var rt        = new BvhDocument();
            var tokenizer = new BvhTokenizer(text);
            var header    = new[] { "HIERARCHY" };

            if (!tokenizer.Take(header.Length).Select(_ => _.Text).SequenceEqual(header))
            {
                throw new InvalidOperationException("invalid format");
            }

            foreach (var i in tokenizer)
            {
                if (i.Kind == BvhTokenizer.IdentifierTokenKind)
                {
                    switch (i.Text)
                    {
                    case "ROOT":
                        rt.Root = BvhJoint.Parse(BvhData.Parse(tokenizer));

                        break;

                    case "MOTION":
                        rt.Motion = BvhMotion.Parse(rt, tokenizer);

                        break;
                    }
                }
            }

            return(rt);
        }
Exemplo n.º 2
0
        public static BvhDocument Parse(string text)
        {
            var rt = new BvhDocument();
            var tokenizer = new BvhTokenizer(text);
            var header = new[] { "HIERARCHY" };

            if (!tokenizer.Take(header.Length).Select(_ => _.Text).SequenceEqual(header))
                throw new InvalidOperationException("invalid format");

            foreach (var i in tokenizer)
                if (i.Kind == BvhTokenizer.IdentifierTokenKind)
                {
                    switch (i.Text)
                    {
                        case "ROOT":
                            rt.Root = BvhJoint.Parse(BvhData.Parse(tokenizer));

                            break;
                        case "MOTION":
                            rt.Motion = BvhMotion.Parse(rt, tokenizer);

                            break;
                    }
                }

            return rt;
        }
Exemplo n.º 3
0
        public static BvhMotion Parse(BvhDocument doc, BvhTokenizer tokenizer)
        {
            var rt = new BvhMotion();

            if (tokenizer.Current.EnsureKind(BvhTokenizer.IdentifierTokenKind).Text == "MOTION")
                tokenizer.MoveNext();

            tokenizer.MoveNext(BvhTokenizer.NewLineTokenKind);

            var frames = tokenizer.Current.EnsureKind(BvhTokenizer.IdentifierTokenKind).Text == "Frames" ? int.Parse(tokenizer.MoveNext().Text) : 0;

            tokenizer.MoveNext();
            tokenizer.MoveNext(BvhTokenizer.NewLineTokenKind);

            if (tokenizer.Current.EnsureKind(BvhTokenizer.IdentifierTokenKind).Text == "Frame")
                tokenizer.MoveNext();

            rt.FrameTime = tokenizer.Current.EnsureKind(BvhTokenizer.IdentifierTokenKind).Text == "Time" ? float.Parse(tokenizer.MoveNext().Text) : 0;
            tokenizer.MoveNext();

            var joints = GetJoints(doc.Root).ToArray();

            for (int i = 0; i < frames; i++)
            {
                var values = tokenizer.TakeWhile(_ => _.Kind != BvhTokenizer.NewLineTokenKind).Select(_ => _.Text).Select(float.Parse).ToArray();
                var j = 0;

                rt.Frames.Add(joints.ToDictionary(_ => _, _ =>
                {
                    var jm = new BvhJointMotion();
                    var position = new float[3];
                    var rotation = new float[3];

                    foreach (var c in _.Channels)
                        switch (c)
                        {
                            case BvhChannel.XPosition:
                            case BvhChannel.YPosition:
                            case BvhChannel.ZPosition:
                                jm.Position = position;
                                position[(int)c] = values[j++];

                                break;
                            case BvhChannel.XRotation:
                            case BvhChannel.YRotation:
                            case BvhChannel.ZRotation:
                                jm.Rotation = rotation;
                                rotation[c - BvhChannel.XRotation] = values[j++];

                                break;
                        }

                    return jm;
                }));
            }

            return rt;
        }
Exemplo n.º 4
0
        public static BvhMotion Parse(BvhDocument doc, BvhTokenizer tokenizer)
        {
            var rt = new BvhMotion();

            if (tokenizer.Current.EnsureKind(BvhTokenizer.IdentifierTokenKind).Text == "MOTION")
            {
                tokenizer.MoveNext();
            }

            tokenizer.MoveNext(BvhTokenizer.NewLineTokenKind);

            var frames = tokenizer.Current.EnsureKind(BvhTokenizer.IdentifierTokenKind).Text == "Frames" ? int.Parse(tokenizer.MoveNext().Text) : 0;

            tokenizer.MoveNext();
            tokenizer.MoveNext(BvhTokenizer.NewLineTokenKind);

            if (tokenizer.Current.EnsureKind(BvhTokenizer.IdentifierTokenKind).Text == "Frame")
            {
                tokenizer.MoveNext();
            }

            rt.FrameTime = tokenizer.Current.EnsureKind(BvhTokenizer.IdentifierTokenKind).Text == "Time" ? float.Parse(tokenizer.MoveNext().Text) : 0;
            tokenizer.MoveNext();

            var joints = GetJoints(doc.Root).ToArray();

            for (int i = 0; i < frames; i++)
            {
                var values = tokenizer.TakeWhile(_ => _.Kind != BvhTokenizer.NewLineTokenKind).Select(_ => _.Text).Select(float.Parse).ToArray();
                var j      = 0;

                rt.Frames.Add(joints.ToDictionary(_ => _, _ =>
                {
                    var jm       = new BvhJointMotion();
                    var position = new float[3];
                    var rotation = new float[3];

                    foreach (var c in _.Channels)
                    {
                        switch (c)
                        {
                        case BvhChannel.XPosition:
                        case BvhChannel.YPosition:
                        case BvhChannel.ZPosition:
                            jm.Position      = position;
                            position[(int)c] = values[j++];

                            break;

                        case BvhChannel.XRotation:
                        case BvhChannel.YRotation:
                        case BvhChannel.ZRotation:
                            jm.Rotation = rotation;
                            rotation[c - BvhChannel.XRotation] = values[j++];

                            break;
                        }
                    }

                    return(jm);
                }));
            }

            return(rt);
        }