File: BezierCurve.cs

package info (click to toggle)
opentk 1.0.20101006%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 38,896 kB
  • ctags: 68,704
  • sloc: cs: 424,330; xml: 96,546; ansic: 3,597; makefile: 24
file content (262 lines) | stat: -rw-r--r-- 9,758 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#region --- License ---
/* Licensed under the MIT/X11 license.
 * Copyright (c) 2006-2008 the OpenTK Team.
 * This notice may not be removed from any source distribution.
 * See license.txt for licensing detailed licensing details.
 * 
 * Contributions by Georg W�chter.
 */
#endregion

using System;
using System.Collections.Generic;
using System.Text;

namespace OpenTK.Math
{
    /// <summary>
    /// Represents a bezier curve with as many points as you want.
    /// </summary>
    [Obsolete("OpenTK.Math functions have been moved to the root OpenTK namespace (reason: XNA compatibility")]
    [Serializable]
    public struct BezierCurve
    {
        #region Fields

        private List<Vector2> points;

        /// <summary>
        /// The parallel value.
        /// </summary>
        /// <remarks>This value defines whether the curve should be calculated as a
        /// parallel curve to the original bezier curve. A value of 0.0f represents
        /// the original curve, 5.0f i.e. stands for a curve that has always a distance
        /// of 5.0f to the orignal curve at any point.</remarks>
        public float Parallel;

        #endregion

        #region Properties

        /// <summary>
        /// Gets the points of this curve.
        /// </summary>
        /// <remarks>The first point and the last points represent the anchor points.</remarks>
        public IList<Vector2> Points
        {
            get
            {
                return points;
            }
        }

        #endregion

        #region Constructors

        /// <summary>
        /// Constructs a new <see cref="BezierCurve"/>.
        /// </summary>
        /// <param name="points">The points.</param>
        public BezierCurve(IEnumerable<Vector2> points)
        {
            if (points == null)
                throw new ArgumentNullException("points", "Must point to a valid list of Vector2 structures.");

            this.points = new List<Vector2>(points);
            this.Parallel = 0.0f;
        }

        /// <summary>
        /// Constructs a new <see cref="BezierCurve"/>.
        /// </summary>
        /// <param name="points">The points.</param>
        public BezierCurve(params Vector2[] points)
        {
            if (points == null)
                throw new ArgumentNullException("points", "Must point to a valid list of Vector2 structures.");

            this.points = new List<Vector2>(points);
            this.Parallel = 0.0f;
        }

        /// <summary>
        /// Constructs a new <see cref="BezierCurve"/>.
        /// </summary>
        /// <param name="parallel">The parallel value.</param>
        /// <param name="points">The points.</param>
        public BezierCurve(float parallel, params Vector2[] points)
        {
            if (points == null)
                throw new ArgumentNullException("points", "Must point to a valid list of Vector2 structures.");

            this.Parallel = parallel;
            this.points = new List<Vector2>(points);
        }

        /// <summary>
        /// Constructs a new <see cref="BezierCurve"/>.
        /// </summary>
        /// <param name="parallel">The parallel value.</param>
        /// <param name="points">The points.</param>
        public BezierCurve(float parallel, IEnumerable<Vector2> points)
        {
            if (points == null)
                throw new ArgumentNullException("points", "Must point to a valid list of Vector2 structures.");

            this.Parallel = parallel;
            this.points = new List<Vector2>(points);
        }

        #endregion

        #region Functions


        /// <summary>
        /// Calculates the point with the specified t.
        /// </summary>
        /// <param name="t">The t value, between 0.0f and 1.0f.</param>
        /// <returns>Resulting point.</returns>
        public Vector2 CalculatePoint(float t)
        {
            return BezierCurve.CalculatePoint(points, t, Parallel);
        }

        /// <summary>
        /// Calculates the length of this bezier curve.
        /// </summary>
        /// <param name="precision">The precision.</param>
        /// <returns>Length of curve.</returns>
        /// <remarks>The precision gets better as the <paramref name="precision"/>
        /// value gets smaller.</remarks>
        public float CalculateLength(float precision)
        {
            return BezierCurve.CalculateLength(points, precision, Parallel);
        }

        #region Static methods

        /// <summary>
        /// Calculates the length of the specified bezier curve.
        /// </summary>
        /// <param name="points">The points.</param>
        /// <param name="precision">The precision value.</param>
        /// <returns>The precision gets better as the <paramref name="precision"/>
        /// value gets smaller.</returns>
        public static float CalculateLength(IList<Vector2> points, float precision)
        {
            return BezierCurve.CalculateLength(points, precision, 0.0f);
        }

        /// <summary>
        /// Calculates the length of the specified bezier curve.
        /// </summary>
        /// <param name="points">The points.</param>
        /// <param name="precision">The precision value.</param>
        /// <param name="parallel">The parallel value.</param>
        /// <returns>Length of curve.</returns>
        /// <remarks><para>The precision gets better as the <paramref name="precision"/>
        /// value gets smaller.</para>
        /// <para>The <paramref name="parallel"/> parameter defines whether the curve should be calculated as a
        /// parallel curve to the original bezier curve. A value of 0.0f represents
        /// the original curve, 5.0f represents a curve that has always a distance
        /// of 5.0f to the orignal curve.</para></remarks>
        public static float CalculateLength(IList<Vector2> points, float precision, float parallel)
        {
            float length = 0.0f;
            Vector2 old = BezierCurve.CalculatePoint(points, 0.0f, parallel);

            for (float i = precision; i < (1.0f + precision); i += precision)
            {
                Vector2 n = CalculatePoint(points, i, parallel);
                length += (n - old).Length;
                old = n;
            }

            return length;
        }

        /// <summary>
        /// Calculates the point on the given bezier curve with the specified t parameter.
        /// </summary>
        /// <param name="points">The points.</param>
        /// <param name="t">The t parameter, a value between 0.0f and 1.0f.</param>
        /// <returns>Resulting point.</returns>
        public static Vector2 CalculatePoint(IList<Vector2> points, float t)
        {
            return BezierCurve.CalculatePoint(points, t, 0.0f);
        }

        /// <summary>
        /// Calculates the point on the given bezier curve with the specified t parameter.
        /// </summary>
        /// <param name="points">The points.</param>
        /// <param name="t">The t parameter, a value between 0.0f and 1.0f.</param>
        /// <param name="parallel">The parallel value.</param>
        /// <returns>Resulting point.</returns>
        /// <remarks>The <paramref name="parallel"/> parameter defines whether the curve should be calculated as a
        /// parallel curve to the original bezier curve. A value of 0.0f represents
        /// the original curve, 5.0f represents a curve that has always a distance
        /// of 5.0f to the orignal curve.</remarks>
        public static Vector2 CalculatePoint(IList<Vector2> points, float t, float parallel)
        {
            Vector2 r = new Vector2();
            double c = 1.0d - (double)t;
            float temp;
            int i = 0;

            foreach (Vector2 pt in points)
            {
                temp = (float)Functions.BinomialCoefficient(points.Count - 1, i) * (float)(System.Math.Pow(t, i) *
                        System.Math.Pow(c, (points.Count - 1) - i));

                r.X += temp * pt.X;
                r.Y += temp * pt.Y;
                i++;
            }

            if (parallel == 0.0f)
                return r;

            Vector2 perpendicular = new Vector2();

            if (t != 0.0f)
                perpendicular = r - BezierCurve.CalculatePointOfDerivative(points, t);
            else
                perpendicular = points[1] - points[0];

            return r + Vector2.Normalize(perpendicular).PerpendicularRight * parallel;
        }

        /// <summary>
        /// Calculates the point with the specified t of the derivative of the given bezier function.
        /// </summary>
        /// <param name="points">The points.</param>
        /// <param name="t">The t parameter, value between 0.0f and 1.0f.</param>
        /// <returns>Resulting point.</returns>
        private static Vector2 CalculatePointOfDerivative(IList<Vector2> points, float t)
        {
            Vector2 r = new Vector2();
            double c = 1.0d - (double)t;
            float temp;
            int i = 0;

            foreach (Vector2 pt in points)
            {
                temp = (float)Functions.BinomialCoefficient(points.Count - 2, i) * (float)(System.Math.Pow(t, i) *
                        System.Math.Pow(c, (points.Count - 2) - i));

                r.X += temp * pt.X;
                r.Y += temp * pt.Y;
                i++;
            }

            return r;
        }

        #endregion

        #endregion
    }
}