Sunday, September 19, 2010

I have made the first demo

This has been a great week, although I feel so tired now, as I've finished 3 big assignments and most importantly I've made our first string creating demo:
When A key is pressed, user can continually add control-points, by clicking left-mouse-botton, to form a string, or a knot.


Now, it's 3D version, and the Natural Cubic Spline seems very good to use in our probject.
On Wednesday, I started looking for some good curve-drawing algorithms for Knot_A_Problem. 4 hours at that day and I got nothing. Those curve-drawing algorithms in the internet seem just complex mathematical stuff and hard to understand. Some C source code are available online but they look just another mathematical formulation due to its lack of documentation. On Thursday, I got understanding of how the 2D DrawCurve method works in DotNet Framework for WinForm. Then I decided to write our own 3D version of CubicSpline class for further use in our project. Meeting with other group members on Friday, talking about the Technical Manual, I thought that it was actually in urgent need for us to get one demo works, otherwise, we are gonna have to face some big problems in doing Technical Manual. So I've been working from yesterday (Saturday) morning until this milestone, a working demo.

Here is the CubicSpline class, sorry for not providing documentation for it yet, I'll add them later on:

namespace Knot_A_Problem
{
    class CubicSpline
    {
         int controlPointNo = 2;
         double[,] controlPoints;
         pspline.pspline3interpolant interpolantP;
         pspline.pspline3interpolant interpolantClosedP;
         public CubicSpline()
         {
             controlPoints = new double[controlPointNo, 3];
             interpolantP = new pspline.pspline3interpolant();
             interpolantClosedP = new pspline.pspline3interpolant();
         }
         public CubicSpline(List<Vector3> pointList)
         {
             controlPointNo = pointList.Count;
             controlPoints = new double[controlPointNo, 3];
             interpolantP = new pspline.pspline3interpolant();
             interpolantClosedP = new pspline.pspline3interpolant();

             for (int i = 0; i < controlPointNo; i++)
             {
                 controlPoints[i, 0] = (double)(pointList[i].X);
                 controlPoints[i, 1] = (double)(pointList[i].Y);
                 controlPoints[i, 2] = (double)(pointList[i].Z);
             }
             if (controlPointNo >= 3)
             {
                 pspline.pspline3build(controlPoints, controlPointNo, 2, 2, ref interpolantP);
                 pspline.pspline3buildperiodic(controlPoints, controlPointNo, 2, 2, ref interpolantClosedP);
             }
         }
        public List<Vector3> GetCubicSpline(int n)
        {
            double xGet = 0, yGet = 0, zGet = 0;
            double internalT = 0;
            if(n != 0)
                internalT = 1 / ((double)n);
            List<Vector3> calVectors = new List<Vector3>();
            if (controlPointNo >= 3)
            {
                for (int i = 0; i <= n; i++)
                {
                    pspline.pspline3calc(ref interpolantP, internalT * i, ref xGet, ref yGet, ref zGet);
                    calVectors.Add(new Vector3((float)xGet, (float)yGet, (float)zGet));
                }
            }
            return calVectors;
        }
        public List<Vector3> GetClosedCubicSpline(int n)
        {
            double xGet = 0, yGet = 0, zGet = 0;
            double internalT = 0;
            if (n != 0)
                internalT = 1 / ((double)n);
            List<Vector3> calVectors = new List<Vector3>();
            if (controlPointNo >= 3)
            {
                for (int i = 0; i <= n; i++)
                {
                    pspline.pspline3calc(ref interpolantClosedP, internalT * i, ref xGet, ref yGet, ref zGet);
                    calVectors.Add(new Vector3((float)xGet, (float)yGet, (float)zGet));
                }
            }
            return calVectors;
        }
    }
}


Another StringGeometry class has been defined and implemented to draw geometries around the central curve to form the string. It's very long, I'm not going to post it here.

No comments:

Post a Comment