Пример #1
0
        public void Refresh(bool evolution = true)
        {
            if (Destroyed)
            {
                return;
            }

            for (int i = 0; i < Segments.Count; i++)
            {
                if (Segments[i] == null)
                {
                    Segments.RemoveAt(i);
                    i--;
                }
            }
            Refresh(transform);
            Root = FindSegment(transform);

            if (evolution && Application.isPlaying)
            {
                DeInitialise();
                Initialise();
                Solution = new double[Evolution.GetModel().GetDoF()];
            }
        }
Пример #2
0
 private bool StopDraw(BioSegment segment)
 {
     if (segment.Parent == null)
     {
         return(false);
     }
     return(false);
     //return segment.Parent.name == "Kopf" && segment.name != "Head" && segment.name != "Ohr_R" && segment.name != "Ohr_L" || segment.name == "Ohr_L_end" || segment.name == "Ohr_R_end";
 }
Пример #3
0
        public Model(BioIK character)
        {
            Character = character;

            //Set Root
            Root = Character.FindSegment(Character.transform);

            //Create Root
            AddNode(Root);

            //Build Model
            BioObjective[] objectives = CollectObjectives(Root, new List <BioObjective>());
            for (int i = 0; i < objectives.Length; i++)
            {
                List <BioSegment> chain = Character.GetChain(Root, objectives[i].Segment);
                for (int j = 1; j < chain.Count; j++)
                {
                    AddNode(chain[j]);
                }
            }

            //Assign DoF
            DoF = MotionPtrs.Length;

            //Initialise arrays for single transform modifications
            for (int i = 0; i < Nodes.Length; i++)
            {
                Nodes[i].ObjectiveImpacts = new bool[ObjectivePtrs.Length];
            }
            PX              = new double[ObjectivePtrs.Length];
            PY              = new double[ObjectivePtrs.Length];
            PZ              = new double[ObjectivePtrs.Length];
            RX              = new double[ObjectivePtrs.Length];
            RY              = new double[ObjectivePtrs.Length];
            RZ              = new double[ObjectivePtrs.Length];
            RW              = new double[ObjectivePtrs.Length];
            Configuration   = new double[MotionPtrs.Length];
            Gradient        = new double[MotionPtrs.Length];
            Losses          = new double[ObjectivePtrs.Length];
            SimulatedLosses = new double[ObjectivePtrs.Length];

            //Assigns references to all objective nodes that are affected by a parenting node
            for (int i = 0; i < ObjectivePtrs.Length; i++)
            {
                Node node = ObjectivePtrs[i].Node;
                while (node != null)
                {
                    node.ObjectiveImpacts[i] = true;
                    node = node.Parent;
                }
            }

            Refresh();

            //DebugSetup();
        }
Пример #4
0
 public void RenewRelations()
 {
     Parent = null;
     System.Array.Resize(ref Childs, 0);
     if (Transform != Character.transform)
     {
         Parent = Character.FindSegment(Transform.parent);
         Parent.AddChild(this);
     }
 }
Пример #5
0
        //Adds a segment node into the model
        private void AddNode(BioSegment segment)
        {
            if (FindNode(segment.Transform) == null)
            {
                Node node = new Node(this, FindNode(segment.Transform.parent), segment);

                if (node.Joint != null)
                {
                    if (node.Joint.GetDoF() == 0 || !node.Joint.enabled)
                    {
                        node.Joint = null;
                    }
                    else
                    {
                        if (node.Joint.X.IsEnabled())
                        {
                            MotionPtr motionPtr = new MotionPtr(node.Joint.X, node, MotionPtrs.Length);
                            System.Array.Resize(ref MotionPtrs, MotionPtrs.Length + 1);
                            MotionPtrs[MotionPtrs.Length - 1] = motionPtr;
                            node.XEnabled = true;
                            node.XIndex   = motionPtr.Index;
                        }
                        if (node.Joint.Y.IsEnabled())
                        {
                            MotionPtr motionPtr = new MotionPtr(node.Joint.Y, node, MotionPtrs.Length);
                            System.Array.Resize(ref MotionPtrs, MotionPtrs.Length + 1);
                            MotionPtrs[MotionPtrs.Length - 1] = motionPtr;
                            node.YEnabled = true;
                            node.YIndex   = motionPtr.Index;
                        }
                        if (node.Joint.Z.IsEnabled())
                        {
                            MotionPtr motionPtr = new MotionPtr(node.Joint.Z, node, MotionPtrs.Length);
                            System.Array.Resize(ref MotionPtrs, MotionPtrs.Length + 1);
                            MotionPtrs[MotionPtrs.Length - 1] = motionPtr;
                            node.ZEnabled = true;
                            node.ZIndex   = motionPtr.Index;
                        }
                    }
                }

                BioObjective[] objectives = segment.Objectives;
                for (int i = 0; i < objectives.Length; i++)
                {
                    if (objectives[i].enabled)
                    {
                        System.Array.Resize(ref ObjectivePtrs, ObjectivePtrs.Length + 1);
                        ObjectivePtrs[ObjectivePtrs.Length - 1] = new ObjectivePtr(objectives[i], node, ObjectivePtrs.Length);
                    }
                }

                System.Array.Resize(ref Nodes, Nodes.Length + 1);
                Nodes[Nodes.Length - 1] = node;
            }
        }
Пример #6
0
 private void DrawSetup(BioSegment segment, bool final)
 {
     if (!StopDraw(segment))
     {
         DrawSegment(segment, final);
         for (int i = 0; i < segment.Childs.Length; i++)
         {
             DrawSetup(segment.Childs[i], final);
         }
     }
 }
Пример #7
0
        public List <BioSegment> GetChain(Transform start, Transform end)
        {
            BioSegment a = FindSegment(start);
            BioSegment b = FindSegment(end);

            if (a == null || b == null)
            {
                Debug.Log("Could not generate chain for given transforms");
                return(null);
            }
            return(GetChain(a, b));
        }
Пример #8
0
 private void DrawSkeleton(BioSegment segment)
 {
     if (!StopDraw(segment))
     {
         if (segment.Parent != null)
         {
             DrawLine(segment.Parent.GetAnchoredPosition(), segment.GetAnchoredPosition(), 5f, Color.cyan);
         }
         for (int i = 0; i < segment.Childs.Length; i++)
         {
             DrawSkeleton(segment.Childs[i]);
         }
     }
 }
Пример #9
0
 private void PostcaptureAnimation(BioSegment segment)
 {
     if (segment.Joint != null)
     {
         if (segment.Joint.enabled)
         {
             segment.Joint.PostcaptureAnimation();
         }
     }
     for (int i = 0; i < segment.Childs.Length; i++)
     {
         PostcaptureAnimation(segment.Childs[i]);
     }
 }
Пример #10
0
 private void ProcessMotion(BioSegment segment)
 {
     if (segment.Joint != null)
     {
         if (segment.Joint.enabled)
         {
             segment.Joint.ProcessMotion();
         }
     }
     for (int i = 0; i < segment.Childs.Length; i++)
     {
         ProcessMotion(segment.Childs[i]);
     }
 }
Пример #11
0
 public static BioJoint AddBioJoint(BioSegment segment)
 {
                 #if UNITY_EDITOR
     if (Application.isPlaying)
     {
         return((segment.gameObject.AddComponent(typeof(BioJoint)) as BioJoint).Create(segment));
     }
     else
     {
         return((Undo.AddComponent(segment.gameObject, typeof(BioJoint)) as BioJoint).Create(segment));
     }
                 #else
     return((segment.gameObject.AddComponent(typeof(BioJoint)) as BioJoint).Create(segment));
                 #endif
 }
Пример #12
0
 //Returns all objectives which are childs in the hierarcy, beginning from the root
 private BioObjective[] CollectObjectives(BioSegment segment, List <BioObjective> objectives)
 {
     for (int i = 0; i < segment.Objectives.Length; i++)
     {
         if (segment.Objectives[i].enabled)
         {
             objectives.Add(segment.Objectives[i]);
         }
     }
     for (int i = 0; i < segment.Childs.Length; i++)
     {
         CollectObjectives(segment.Childs[i], objectives);
     }
     return(objectives.ToArray());
 }
Пример #13
0
        private void Refresh(Transform t)
        {
            BioSegment segment = FindSegment(t);

            if (segment == null)
            {
                segment = Utility.AddBioSegment(this, t);
                Segments.Add(segment);
            }
            segment.Character = this;
            segment.RenewRelations();

            for (int i = 0; i < t.childCount; i++)
            {
                Refresh(t.GetChild(i));
            }
        }
Пример #14
0
        public List <BioSegment> GetChain(BioSegment start, BioSegment end)
        {
            List <BioSegment> chain   = new List <BioSegment>();
            BioSegment        segment = end;

            while (true)
            {
                chain.Add(segment);
                if (segment.Transform == transform || segment.Parent == null)
                {
                    break;
                }
                else
                {
                    segment = segment.Parent;
                }
            }
            chain.Reverse();
            return(chain);
        }
Пример #15
0
 public void ResetPosture(BioSegment segment)
 {
     if (segment.Joint != null)
     {
         segment.Joint.X.SetTargetValue(0f);
         segment.Joint.Y.SetTargetValue(0f);
         segment.Joint.Z.SetTargetValue(0f);
         if (!Application.isPlaying)
         {
             segment.Joint.PrecaptureAnimation();
             segment.Joint.PostcaptureAnimation();
             segment.Joint.UpdateData();
             segment.Joint.ProcessMotion();
         }
     }
     for (int i = 0; i < segment.Childs.Length; i++)
     {
         ResetPosture(segment.Childs[i]);
     }
 }
Пример #16
0
 public void UpdateData(BioSegment segment)
 {
     if (segment.Joint != null)
     {
         if (segment.Joint.enabled)
         {
             segment.Joint.UpdateData();
         }
     }
     for (int i = 0; i < segment.Objectives.Length; i++)
     {
         if (segment.Objectives[i].enabled)
         {
             segment.Objectives[i].UpdateData();
         }
     }
     for (int i = 0; i < segment.Childs.Length; i++)
     {
         UpdateData(segment.Childs[i]);
     }
 }
Пример #17
0
        private void DrawSegment(BioSegment segment, bool final)
        {
            Vector3 P = segment.GetAnchoredPosition();

            if (Target.SelectedSegment == segment && final)
            {
                DrawSphere(P, 0.25f, new Color(0f, 0f, 0f, 0.4f));
                DrawSphere(P, 0.02f, Color5);
            }
            else if (Target.SelectedSegment != segment)
            {
                DrawSphere(P, 0.02f, Color.cyan);
            }
            if (segment.Joint != null)
            {
                DrawJoint(segment.Joint, final);
            }
            for (int i = 0; i < segment.Objectives.Length; i++)
            {
                DrawObjective(segment.Objectives[i], final);
            }
        }
Пример #18
0
        public BioJoint Create(BioSegment segment)
        {
            Segment = segment;
            Segment.Transform.hideFlags = HideFlags.NotEditable;
            hideFlags = HideFlags.HideInInspector;

            X = new Motion(this, Vector3.right);
            Y = new Motion(this, Vector3.up);
            Z = new Motion(this, Vector3.forward);

            SetDefaultFrame(transform.localPosition, transform.localRotation);
            SetAnchor(Anchor);

            Vector3 forward = Vector3.zero;

            if (Segment.Childs.Length == 1)
            {
                forward = Segment.Childs[0].Transform.localPosition;
            }
            else if (Segment.Parent != null)
            {
                forward = Quaternion.Inverse(Segment.Transform.localRotation) * Segment.Transform.localPosition;
            }
            if (forward.magnitude != 0f)
            {
                SetOrientation(Quaternion.LookRotation(forward, Vector3.up).eulerAngles);
            }
            else
            {
                SetOrientation(Orientation);
            }

            LastPosition = transform.localPosition;
            LastRotation = transform.localRotation;

            return(this);
        }
Пример #19
0
            public bool[] ObjectiveImpacts;                                     //Boolean values to represent which objective indices in the whole kinematic tree are affected (TODO: Refactor this)

            //Setup for the node
            public Node(Model model, Node parent, BioSegment segment)
            {
                Model  = model;
                Parent = parent;
                if (Parent != null)
                {
                    Parent.AddChild(this);
                }
                Transform = segment.Transform;
                Joint     = segment.Joint;

                List <Transform> reverseChain = new List <Transform>();

                reverseChain.Add(Transform);
                Node p = parent;

                while (p != null)
                {
                    reverseChain.Add(p.Transform);
                    p = p.Parent;
                }
                reverseChain.Reverse();
                Chain = reverseChain.ToArray();
            }
Пример #20
0
        private void InspectBody(BioSegment segment, int indent, int maxIndent)
        {
            SetGUIColor(Color11);
            using (new EditorGUILayout.VerticalScope("Box")) {
                EditorGUILayout.BeginHorizontal();

                if (Target.SelectedSegment != segment)
                {
                    if (indent > 0)
                    {
                        SetGUIColor(Color13);
                        using (new EditorGUILayout.VerticalScope("Box")) {
                            int width = 10 * (indent - 1);
                            EditorGUILayout.LabelField("", GUILayout.Width(width));
                        }
                    }
                }

                GUI.skin.button.alignment = TextAnchor.MiddleLeft;
                if (segment == Target.SelectedSegment)
                {
                    SetGUIColor(Color5);
                }
                else
                {
                    SetGUIColor(Color.Lerp(Color4, Color8, (float)indent / (float)maxIndent));
                }
                if (GUILayout.Button(segment.Transform.name, GUILayout.Height(25f), GUILayout.ExpandWidth(true)))
                {
                    if (Target.SelectedSegment == segment)
                    {
                        Target.SelectedSegment = null;
                        ChosingObjectiveType   = false;
                    }
                    else
                    {
                        Target.SelectedSegment = segment;
                    }
                }

                EditorGUILayout.EndHorizontal();

                if (Target.SelectedSegment == segment)
                {
                    InspectSegment(segment);
                }
                else
                {
                    GUILayout.BeginHorizontal();
                    GUILayout.FlexibleSpace();
                    if (segment.Joint != null)
                    {
                        SetGUIColor(Color6);
                        GUILayout.Box(" Joint ");
                    }
                    foreach (BioObjective objective in segment.Objectives)
                    {
                        SetGUIColor(Color9);
                        GUILayout.Box(" " + objective.GetObjectiveType().ToString() + " ");
                    }
                    GUILayout.FlexibleSpace();
                    GUILayout.EndHorizontal();
                }
            }

            for (int i = 0; i < segment.Childs.Length; i++)
            {
                InspectBody(segment.Childs[i], indent + 1, maxIndent);
            }
        }
Пример #21
0
        private void InspectSegment(BioSegment segment)
        {
            Undo.RecordObject(segment, segment.name);

            SetGUIColor(Color13);
            using (new EditorGUILayout.VerticalScope("Box")) {
                SetGUIColor(Color4);
                Vector3 A = segment.Parent == null?segment.GetAnchoredPosition() : segment.Parent.GetAnchoredPosition();

                Vector3 B = segment.GetAnchoredPosition();
                EditorGUILayout.HelpBox("Link Length: " + Vector3.Distance(A, B), MessageType.None);

                SetGUIColor(Color6);
                using (new EditorGUILayout.VerticalScope("Box")) {
                    if (segment.Joint == null)
                    {
                        GUI.skin.button.alignment = TextAnchor.MiddleCenter;
                        SetGUIColor(Color1);
                        if (GUILayout.Button("Add Joint"))
                        {
                            segment.AddJoint();
                        }
                    }
                    else
                    {
                        InspectJoint(segment.Joint);
                    }
                }
            }

            for (int i = 0; i < segment.Objectives.Length; i++)
            {
                SetGUIColor(Color13);
                using (new EditorGUILayout.VerticalScope("Box")) {
                    SetGUIColor(Color9);
                    using (new EditorGUILayout.VerticalScope("Box")) {
                        InspectObjective(segment.Objectives[i]);
                    }
                }
            }

            SetGUIColor(Color13);
            using (new EditorGUILayout.VerticalScope("Box")) {
                SetGUIColor(Color9);
                using (new EditorGUILayout.VerticalScope("Box")) {
                    GUI.skin.button.alignment = TextAnchor.MiddleCenter;
                    SetGUIColor(ChosingObjectiveType ? Color14 : Color1);
                    if (GUILayout.Button("Add Objective"))
                    {
                        ChosingObjectiveType = !ChosingObjectiveType;
                    }
                    if (ChosingObjectiveType)
                    {
                        SetGUIColor(Color8);
                        using (new EditorGUILayout.VerticalScope("Box")) {
                            int      count = System.Enum.GetValues(typeof(ObjectiveType)).Length;
                            string[] names = System.Enum.GetNames(typeof(ObjectiveType));
                            for (int i = 0; i < count; i++)
                            {
                                SetGUIColor(Color1);
                                if (GUILayout.Button(names[i]))
                                {
                                    ChosingObjectiveType = false;
                                    segment.AddObjective((ObjectiveType)i);
                                }
                            }
                        }
                    }
                }
            }

            if (segment != null)
            {
                EditorUtility.SetDirty(segment);
            }
        }
Пример #22
0
 public void AddChild(BioSegment child)
 {
     System.Array.Resize(ref Childs, Childs.Length + 1);
     Childs[Childs.Length - 1] = child;
 }
Пример #23
0
        public static BioObjective AddObjective(BioSegment segment, ObjectiveType type)
        {
                        #if UNITY_EDITOR
            if (Application.isPlaying)
            {
                switch (type)
                {
                case ObjectiveType.Position:
                    return((segment.gameObject.AddComponent(typeof(Position)) as BioObjective).Create(segment));

                case ObjectiveType.Orientation:
                    return((segment.gameObject.AddComponent(typeof(Orientation)) as BioObjective).Create(segment));

                case ObjectiveType.LookAt:
                    return((segment.gameObject.AddComponent(typeof(LookAt)) as BioObjective).Create(segment));;

                case ObjectiveType.Distance:
                    return((segment.gameObject.AddComponent(typeof(Distance)) as BioObjective).Create(segment));

                case ObjectiveType.JointValue:
                    return((segment.gameObject.AddComponent(typeof(JointValue)) as BioObjective).Create(segment));

                case ObjectiveType.Displacement:
                    return((segment.gameObject.AddComponent(typeof(Displacement)) as BioObjective).Create(segment));

                case ObjectiveType.Projection:
                    return((segment.gameObject.AddComponent(typeof(Projection)) as BioObjective).Create(segment));
                }
            }
            else
            {
                switch (type)
                {
                case ObjectiveType.Position:
                    return((Undo.AddComponent(segment.gameObject, typeof(Position)) as BioObjective).Create(segment));

                case ObjectiveType.Orientation:
                    return((Undo.AddComponent(segment.gameObject, typeof(Orientation)) as BioObjective).Create(segment));

                case ObjectiveType.LookAt:
                    return((Undo.AddComponent(segment.gameObject, typeof(LookAt)) as BioObjective).Create(segment));

                case ObjectiveType.Distance:
                    return((Undo.AddComponent(segment.gameObject, typeof(Distance)) as BioObjective).Create(segment));

                case ObjectiveType.JointValue:
                    return((Undo.AddComponent(segment.gameObject, typeof(JointValue)) as BioObjective).Create(segment));

                case ObjectiveType.Displacement:
                    return((Undo.AddComponent(segment.gameObject, typeof(Displacement)) as BioObjective).Create(segment));

                case ObjectiveType.Projection:
                    return((Undo.AddComponent(segment.gameObject, typeof(Projection)) as BioObjective).Create(segment));
                }
            }
            return(null);
                        #else
            switch (type)
            {
            case ObjectiveType.Position:
                return((segment.gameObject.AddComponent(typeof(Position)) as BioObjective).Create(segment));

            case ObjectiveType.Orientation:
                return((segment.gameObject.AddComponent(typeof(Orientation)) as BioObjective).Create(segment));

            case ObjectiveType.LookAt:
                return((segment.gameObject.AddComponent(typeof(LookAt)) as BioObjective).Create(segment));;

            case ObjectiveType.Distance:
                return((segment.gameObject.AddComponent(typeof(Distance)) as BioObjective).Create(segment));

            case ObjectiveType.JointValue:
                return((segment.gameObject.AddComponent(typeof(JointValue)) as BioObjective).Create(segment));

            case ObjectiveType.Displacement:
                return((segment.gameObject.AddComponent(typeof(Displacement)) as BioObjective).Create(segment));

            case ObjectiveType.Projection:
                return((segment.gameObject.AddComponent(typeof(Projection)) as BioObjective).Create(segment));
            }
            return(null);
                        #endif
        }
Пример #24
0
 public BioObjective Create(BioSegment segment)
 {
     Segment   = segment;
     hideFlags = HideFlags.HideInInspector;
     return(this);
 }