Example #1
0
        private void OrganizeBone(Bone bone)
        {
            if (bone.ParentBone != null)
            {
                float angleParent = GetAngle(new PointF(1, 0), bone.ParentBone.PositionEnd);

                float angleBone = bone.Angle;

                if (bone.ParentBone.ParentBone != null)
                    angleBone += angleParent - (float)Math.PI;

                bone.PositionEnd = new PointF(
                      (float)Math.Cos(angleBone) * (float)bone.Length,
                      (float)Math.Sin(angleBone) * (float)bone.Length);

                bone.AbsolutePositionEnd = new PointF(bone.ParentBone.AbsolutePositionEnd.X + bone.PositionEnd.X, bone.ParentBone.AbsolutePositionEnd.Y + bone.PositionEnd.Y);
            }

            if (bone.ChildBone != null)
                OrganizeBone(bone.ChildBone);
        }
Example #2
0
        private void CreateSkeleton()
        {
            rnd = new Random();
            ListSkeleton = new List<Skeleton>();

            for (int j = 0; j < nbSkeleton; j++)
            {
                Skeleton skeleton = new Skeleton();
                skeleton.Index = j;
                ListSkeleton.Add(skeleton);

                Bone prevBone = null;

                for (int i = 0; i <= maxBones; i++)
                {
                    Bone bone = new Bone();

                    bone.ParentBone = prevBone;
                    //bone.PositionEnd = new PointF(rnd.Next(-length, length), rnd.Next(-length, length));
                    bone.PositionEnd = new PointF(0, length);
                    bone.Level = i;
                    bone.Skeleton = skeleton;

                    if (prevBone != null)
                    {
                        prevBone.ChildBone = bone;

                        bone.AbsolutePositionEnd = new PointF(bone.ParentBone.AbsolutePositionEnd.X + bone.PositionEnd.X,
                                                             bone.ParentBone.AbsolutePositionEnd.Y + bone.PositionEnd.Y);

                        bone.Length = Distance(bone.AbsolutePositionEnd, bone.ParentBone.AbsolutePositionEnd);

                        if (bone.ParentBone.ParentBone != null)
                        {
                            bone.Angle = GetAngle(GetVector(bone.ParentBone.ParentBone.AbsolutePositionEnd, bone.ParentBone.AbsolutePositionEnd), GetVector(bone.AbsolutePositionEnd, bone.ParentBone.AbsolutePositionEnd));
                        }
                        else
                        {
                            bone.Angle = GetAngle(new PointF(1, 0), GetVector(bone.AbsolutePositionEnd, bone.ParentBone.AbsolutePositionEnd));
                        }

                        float constraintAngle = (float)Math.PI / 6f;

                        bone.AngleConstraintMin = Math.Abs(bone.Angle - constraintAngle * ((float)i / (float)maxBones));
                        bone.AngleConstraintMax = Math.Abs(bone.Angle + constraintAngle * ((float)i / (float)maxBones));
                    }
                    else
                    {
                        //bone.PositionEnd = new PointF((int)((float)pic.Width / 2f + 200f * (float)Math.Cos(6.28f / (float)nbSkeleton * (float)j)), (int)((float)pic.Height / 2f + 200f * (float)Math.Sin(6.28f / (float)nbSkeleton * (float)j)));
                        bone.PositionEnd = new PointF(0, this.Height / nbSkeleton * j);
                        bone.AbsolutePositionEnd = bone.PositionEnd;
                    }

                    prevBone = bone;

                    if (i == 0)
                        skeleton.RootBone = bone;

                    if (i == maxBones)
                        skeleton.LeafBone = bone;
                }
            }
        }
Example #3
0
        private void DrawBone(Bone bone)
        {
            if (bone.ChildBone != null)
            {
                int sizeBone = maxBones + 15 - bone.Level;

                //int n = (int)(255f-150f * ((float)bone.Level / (float)maxBones));
                int n = (int)(255f - 150f * ((float)bone.Level / (float)maxBones));

                //Brush brush = new SolidBrush(Color.FromArgb(255, n, n, 50 + 180 * bone.Skeleton.Index / nbSkeleton));
                Brush brush = new SolidBrush(Color.FromArgb(255, n, n, n));

                gBmp.FillEllipse(brush, bone.AbsolutePositionEnd.X, bone.AbsolutePositionEnd.Y, sizeBone, sizeBone);

                DrawBone(bone.ChildBone);
            }
        }
Example #4
0
        private void CalcBone(Bone bone, PointF PointF)
        {
            if (bone.ParentBone != null)
            {
                PointF vecBone = GetVector(bone.AbsolutePositionEnd, bone.ParentBone.AbsolutePositionEnd);
                PointF vecPointF = GetVector(PointF, bone.ParentBone.AbsolutePositionEnd);

                float angleBone = GetAngle(new PointF(1, 0), vecBone);
                float anglePointF = GetAngle(new PointF(1, 0), vecPointF);
                float prevAngle = bone.Angle;

                if (angleBone > anglePointF)
                {
                    angleBone = anglePointF + (1f - delta) * (angleBone - anglePointF);
                }
                else
                {
                    angleBone = angleBone + delta * (anglePointF - angleBone);
                }

                bone.PositionEnd = new PointF(
                    (float)Math.Cos(angleBone) * (float)bone.Length,
                    (float)Math.Sin(angleBone) * (float)bone.Length);

                bone.AbsolutePositionEnd = new PointF(bone.ParentBone.AbsolutePositionEnd.X + bone.PositionEnd.X,
                                                         bone.ParentBone.AbsolutePositionEnd.Y + bone.PositionEnd.Y);
                if (bone.ParentBone.ParentBone != null)
                    bone.Angle = -GetAngle(GetVector(bone.AbsolutePositionEnd, bone.ParentBone.AbsolutePositionEnd), GetVector(bone.ParentBone.ParentBone.AbsolutePositionEnd, bone.ParentBone.AbsolutePositionEnd));
                else
                    bone.Angle = angleBone;

                //--- Contrainte par les angles de torsion
                if (Math.Abs(bone.Angle) < bone.AngleConstraintMin || Math.Abs(bone.Angle) > bone.AngleConstraintMax)
                    bone.Angle = prevAngle;
                //---

                CalcBone(bone.ParentBone, PointF);
            }
        }