private void EnergyGrad(double[] x, ref double func, double[] grad, object obj)
        {
            Tuple <Story, PositionTable <double>, int, int> tuple = obj as Tuple <Story, PositionTable <double>, int, int>;
            Story story = tuple.Item1;
            PositionTable <double> position = tuple.Item2;
            int frame = tuple.Item3;
            int i     = tuple.Item4;

            func    = 0;
            grad[0] = 0;

            for (int j = 0; j < story.Characters.Count; ++j)
            {
                if (i != j && story.SessionTable[j, frame] != -1)
                {
                    if (story.SessionTable[i, frame] == story.SessionTable[j, frame])
                    {
                        var t = InnerEnergy(x[0], position[j, frame], 1.0, 1.0 * (Ultities.GetGroupCount(story, i, frame) - 1));
                        func    += InnerDistanceConstraintWeight * t.Item1;
                        grad[0] += InnerDistanceConstraintWeight * t.Item2;
                    }
                    else
                    {
                        var t = OuterEnergy(x[0], position[j, frame], 5.0);
                        func    += OutterDistanceConstraintWeight * t.Item1;
                        grad[0] += OutterDistanceConstraintWeight * t.Item2;
                    }
                }
            }

            if (LineStraightFunction == 1 && frame > 0 && frame < story.FrameCount - 1 && story.SessionTable[i, frame - 1] != -1 && story.SessionTable[i, frame + 1] != -1)
            {
                var t = StraightEnergy(x[0], position[i, frame - 1], position[i, frame + 1]);
                func    += LineStraightWeight * t.Item1;
                grad[0] += LineStraightWeight * t.Item2;
            }
            else
            {
                if (frame > 0 && story.SessionTable[i, frame - 1] != -1)
                {
                    var t = StraightEnergy(x[0], position[i, frame - 1]);
                    func    += LineStraightWeight * t.Item1;
                    grad[0] += LineStraightWeight * t.Item2;
                }
                else if (frame < story.FrameCount - 1 && story.SessionTable[i, frame + 1] != -1)
                {
                    var t = StraightEnergy(x[0], position[i, frame + 1]);
                    func    += LineStraightWeight * t.Item1;
                    grad[0] += LineStraightWeight * t.Item2;
                }
                else
                {
                }
            }
        }
Beispiel #2
0
        private void EnergyGrad(double[] x, ref double func, double[] grad, object obj)
        {
            Tuple <Story, PositionTable <double>, Dictionary <Tuple <int, int>, int> > tuple = obj as Tuple <Story, PositionTable <double>, Dictionary <Tuple <int, int>, int> >;
            Story story = tuple.Item1;
            PositionTable <double>             position = tuple.Item2;
            Dictionary <Tuple <int, int>, int> dict     = tuple.Item3;


            // calculate func
            func = 0;
            Tuple <double, double, double> tupleInnerDistance;
            Tuple <double, double, double> tupleOuterDistance;
            Tuple <double, double, double> tupleLineStraight;

            // calculate grad
            for (int frame = 0; frame < story.FrameCount; ++frame)
            {
                for (int i = 0; i < story.Characters.Count; ++i)
                {
                    if (story.SessionTable[i, frame] != -1)
                    {
                        int index = dict[new Tuple <int, int>(i, frame)];
                        grad[index] = 0;

                        for (int j = 0; j < story.Characters.Count; ++j)
                        {
                            if (i != j && story.SessionTable[j, frame] != -1)
                            {
                                if (story.SessionTable[i, frame] == story.SessionTable[j, frame])
                                {
                                    var t = InnerEnergy(x[index], x[dict[new Tuple <int, int>(j, frame)]], 1.0, 1.0 * (Ultities.GetGroupCount(story, i, frame) - 1));
                                    func        += InnerDistanceConstraintWeight * t.Item1;
                                    grad[index] += InnerDistanceConstraintWeight * t.Item2;
                                }
                                else
                                {
                                    var t = OuterEnergy(x[index], x[dict[new Tuple <int, int>(j, frame)]], 5.0);
                                    func        += OutterDistanceConstraintWeight * t.Item1;
                                    grad[index] += OutterDistanceConstraintWeight * t.Item2;
                                }
                            }
                        }

                        if (LineStraightFunction == 1 && frame > 0 && frame < story.FrameCount - 1 && story.SessionTable[i, frame - 1] != -1 && story.SessionTable[i, frame + 1] != -1)
                        {
                            var t = StraightEnergy(x[index], x[dict[new Tuple <int, int>(i, frame - 1)]], x[dict[new Tuple <int, int>(i, frame + 1)]]);
                            func        += LineStraightWeight * t.Item1;
                            grad[index] += LineStraightWeight * t.Item2;
                        }
                        else
                        {
                            if (frame > 0 && story.SessionTable[i, frame - 1] != -1)
                            {
                                var t = StraightEnergy(x[index], x[dict[new Tuple <int, int>(i, frame - 1)]]);
                                func        += LineStraightWeight * t.Item1;
                                grad[index] += LineStraightWeight * t.Item2;
                            }
                            else if (frame < story.FrameCount - 1 && story.SessionTable[i, frame + 1] != -1)
                            {
                                var t = StraightEnergy(x[index], x[dict[new Tuple <int, int>(i, frame + 1)]]);
                                func        += LineStraightWeight * t.Item1;
                                grad[index] += LineStraightWeight * t.Item2;
                            }
                            else
                            {
                            }
                        }
                    }
                }
            }
        }