Mercurial > repos > public > sbplib
annotate +parametrization/Curve.m @ 1097:eec03e78c6f2 feature/dataspline
Merge in default
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Mon, 08 Apr 2019 22:30:47 +0200 |
parents | ad3089f04e0b |
children | 36d092a00040 |
rev | line source |
---|---|
0 | 1 classdef Curve |
2 properties | |
3 g | |
4 gp | |
5 transformation | |
6 end | |
107
06c3034966b7
Added and changed some comments. Also my text editor removed a bunch of whitespace.
Jonatan Werpers <jonatan@werpers.com>
parents:
105
diff
changeset
|
7 |
0 | 8 methods |
9 %TODO: | |
10 % Concatenation of curves | |
11 % Subsections of curves | |
105
1df4f3704b76
Added error checking for all methods that use the derivative gp. Removed that from the to do list. Updated some comments.
Martin Almquist <martin.almquist@it.uu.se>
parents:
104
diff
changeset
|
12 % Stretching of curve parameter - done for arc length. |
0 | 13 % Curve to cell array of linesegments |
14 | |
107
06c3034966b7
Added and changed some comments. Also my text editor removed a bunch of whitespace.
Jonatan Werpers <jonatan@werpers.com>
parents:
105
diff
changeset
|
15 % Returns a curve object. |
06c3034966b7
Added and changed some comments. Also my text editor removed a bunch of whitespace.
Jonatan Werpers <jonatan@werpers.com>
parents:
105
diff
changeset
|
16 % g -- curve parametrization for parameter between 0 and 1 |
06c3034966b7
Added and changed some comments. Also my text editor removed a bunch of whitespace.
Jonatan Werpers <jonatan@werpers.com>
parents:
105
diff
changeset
|
17 % gp -- parametrization of curve derivative |
103
bc5db54f9efd
fzero_vec, integral_vec and spline are now local functions in Curve. Renamed arcLengthStretch to arcLengthParametrization. Removed plot_derivative. Added some comments and extra lines + removed unneccesary lines. arcLength is now a method and not static. Constructor does not accept difference operator anymore.
Martin Almquist <martin.almquist@it.uu.se>
parents:
99
diff
changeset
|
18 function obj = Curve(g,gp,transformation) |
0 | 19 default_arg('gp',[]); |
20 default_arg('transformation',[]); | |
21 p_test = g(0); | |
22 assert(all(size(p_test) == [2,1]), 'A curve parametrization must return a 2x1 vector.'); | |
107
06c3034966b7
Added and changed some comments. Also my text editor removed a bunch of whitespace.
Jonatan Werpers <jonatan@werpers.com>
parents:
105
diff
changeset
|
23 |
0 | 24 if ~isempty(transformation) |
25 transformation.base_g = g; | |
26 transformation.base_gp = gp; | |
248
81e0ead29431
Fixed package names in +parametrization
Jonatan Werpers <jonatan@werpers.com>
parents:
151
diff
changeset
|
27 [g,gp] = parametrization.Curve.transform_g(g,gp,transformation); |
0 | 28 end |
107
06c3034966b7
Added and changed some comments. Also my text editor removed a bunch of whitespace.
Jonatan Werpers <jonatan@werpers.com>
parents:
105
diff
changeset
|
29 |
0 | 30 obj.g = g; |
31 obj.gp = gp; | |
32 obj.transformation = transformation; | |
107
06c3034966b7
Added and changed some comments. Also my text editor removed a bunch of whitespace.
Jonatan Werpers <jonatan@werpers.com>
parents:
105
diff
changeset
|
33 |
0 | 34 end |
35 | |
36 function n = normal(obj,t) | |
105
1df4f3704b76
Added error checking for all methods that use the derivative gp. Removed that from the to do list. Updated some comments.
Martin Almquist <martin.almquist@it.uu.se>
parents:
104
diff
changeset
|
37 assert(~isempty(obj.gp),'Curve has no derivative!'); |
0 | 38 deriv = obj.gp(t); |
39 normalization = sqrt(sum(deriv.^2,1)); | |
40 n = [-deriv(2,:)./normalization; deriv(1,:)./normalization]; | |
41 end | |
42 | |
43 | |
87
0a29a60e0b21
In Curve: Rearranged for speed. arc_length_fun is now a property of Curve. If it is not supplied, it is computed via the derivative and spline fitting. Switching to the arc length parameterization is much faster now. The new stuff can be tested with testArcLength.m (which should be deleted after that).
Martin Almquist <martin.almquist@it.uu.se>
parents:
86
diff
changeset
|
44 % Plots a curve g(t) for 0<t<1, using n points. Returns a handle h to the plotted curve. |
0 | 45 % h = plot_curve(g,n) |
98
b30f3d8845f4
Removed arclength property. Constructor compatible again. Translation and rotation are fast now, but ti is slow.
Martin Almquist <martin.almquist@it.uu.se>
parents:
87
diff
changeset
|
46 function h = plot(obj,n) |
0 | 47 default_arg('n',100); |
48 | |
49 t = linspace(0,1,n); | |
50 p = obj.g(t); | |
98
b30f3d8845f4
Removed arclength property. Constructor compatible again. Translation and rotation are fast now, but ti is slow.
Martin Almquist <martin.almquist@it.uu.se>
parents:
87
diff
changeset
|
51 h = line(p(1,:),p(2,:)); |
87
0a29a60e0b21
In Curve: Rearranged for speed. arc_length_fun is now a property of Curve. If it is not supplied, it is computed via the derivative and spline fitting. Switching to the arc length parameterization is much faster now. The new stuff can be tested with testArcLength.m (which should be deleted after that).
Martin Almquist <martin.almquist@it.uu.se>
parents:
86
diff
changeset
|
52 end |
0 | 53 |
54 function h= plot_normals(obj,l,n,m) | |
55 default_arg('l',0.1); | |
56 default_arg('n',10); | |
57 default_arg('m',100); | |
58 t_n = linspace(0,1,n); | |
59 | |
60 normals = obj.normal(t_n)*l; | |
61 | |
62 n0 = obj.g(t_n); | |
63 n1 = n0 + normals; | |
64 | |
65 h = line([n0(1,:); n1(1,:)],[n0(2,:); n1(2,:)]); | |
66 set(h,'Color',Color.red); | |
67 obj.plot(m); | |
68 end | |
69 | |
70 function h= show(obj,name) | |
333
0fd4b259af9e
Fixed some bugs in parametrization.Curve.
Jonatan Werpers <jonatan@werpers.com>
parents:
248
diff
changeset
|
71 default_arg('name', '') |
0 | 72 p = obj.g(1/2); |
73 n = obj.normal(1/2); | |
74 p = p + n*0.1; | |
75 | |
76 % Add arrow | |
77 | |
78 | |
333
0fd4b259af9e
Fixed some bugs in parametrization.Curve.
Jonatan Werpers <jonatan@werpers.com>
parents:
248
diff
changeset
|
79 if ~isempty(name) |
0fd4b259af9e
Fixed some bugs in parametrization.Curve.
Jonatan Werpers <jonatan@werpers.com>
parents:
248
diff
changeset
|
80 h = text(p(1),p(2),name); |
0fd4b259af9e
Fixed some bugs in parametrization.Curve.
Jonatan Werpers <jonatan@werpers.com>
parents:
248
diff
changeset
|
81 h.HorizontalAlignment = 'center'; |
0fd4b259af9e
Fixed some bugs in parametrization.Curve.
Jonatan Werpers <jonatan@werpers.com>
parents:
248
diff
changeset
|
82 h.VerticalAlignment = 'middle'; |
0fd4b259af9e
Fixed some bugs in parametrization.Curve.
Jonatan Werpers <jonatan@werpers.com>
parents:
248
diff
changeset
|
83 end |
0fd4b259af9e
Fixed some bugs in parametrization.Curve.
Jonatan Werpers <jonatan@werpers.com>
parents:
248
diff
changeset
|
84 |
0fd4b259af9e
Fixed some bugs in parametrization.Curve.
Jonatan Werpers <jonatan@werpers.com>
parents:
248
diff
changeset
|
85 h = obj.plot(); |
0 | 86 end |
87 % Shows curve with name and arrow for direction. | |
88 | |
103
bc5db54f9efd
fzero_vec, integral_vec and spline are now local functions in Curve. Renamed arcLengthStretch to arcLengthParametrization. Removed plot_derivative. Added some comments and extra lines + removed unneccesary lines. arcLength is now a method and not static. Constructor does not accept difference operator anymore.
Martin Almquist <martin.almquist@it.uu.se>
parents:
99
diff
changeset
|
89 % Length of arc from parameter t0 to t1 (which may be vectors). |
bc5db54f9efd
fzero_vec, integral_vec and spline are now local functions in Curve. Renamed arcLengthStretch to arcLengthParametrization. Removed plot_derivative. Added some comments and extra lines + removed unneccesary lines. arcLength is now a method and not static. Constructor does not accept difference operator anymore.
Martin Almquist <martin.almquist@it.uu.se>
parents:
99
diff
changeset
|
90 % Computed using derivative. |
bc5db54f9efd
fzero_vec, integral_vec and spline are now local functions in Curve. Renamed arcLengthStretch to arcLengthParametrization. Removed plot_derivative. Added some comments and extra lines + removed unneccesary lines. arcLength is now a method and not static. Constructor does not accept difference operator anymore.
Martin Almquist <martin.almquist@it.uu.se>
parents:
99
diff
changeset
|
91 function L = arcLength(obj,t0,t1) |
bc5db54f9efd
fzero_vec, integral_vec and spline are now local functions in Curve. Renamed arcLengthStretch to arcLengthParametrization. Removed plot_derivative. Added some comments and extra lines + removed unneccesary lines. arcLength is now a method and not static. Constructor does not accept difference operator anymore.
Martin Almquist <martin.almquist@it.uu.se>
parents:
99
diff
changeset
|
92 assert(~isempty(obj.gp),'Curve has no derivative!'); |
bc5db54f9efd
fzero_vec, integral_vec and spline are now local functions in Curve. Renamed arcLengthStretch to arcLengthParametrization. Removed plot_derivative. Added some comments and extra lines + removed unneccesary lines. arcLength is now a method and not static. Constructor does not accept difference operator anymore.
Martin Almquist <martin.almquist@it.uu.se>
parents:
99
diff
changeset
|
93 speed = @(t) sqrt(sum(obj.gp(t).^2,1)); |
bc5db54f9efd
fzero_vec, integral_vec and spline are now local functions in Curve. Renamed arcLengthStretch to arcLengthParametrization. Removed plot_derivative. Added some comments and extra lines + removed unneccesary lines. arcLength is now a method and not static. Constructor does not accept difference operator anymore.
Martin Almquist <martin.almquist@it.uu.se>
parents:
99
diff
changeset
|
94 L = integral_vec(speed,t0,t1); |
bc5db54f9efd
fzero_vec, integral_vec and spline are now local functions in Curve. Renamed arcLengthStretch to arcLengthParametrization. Removed plot_derivative. Added some comments and extra lines + removed unneccesary lines. arcLength is now a method and not static. Constructor does not accept difference operator anymore.
Martin Almquist <martin.almquist@it.uu.se>
parents:
99
diff
changeset
|
95 end |
107
06c3034966b7
Added and changed some comments. Also my text editor removed a bunch of whitespace.
Jonatan Werpers <jonatan@werpers.com>
parents:
105
diff
changeset
|
96 |
06c3034966b7
Added and changed some comments. Also my text editor removed a bunch of whitespace.
Jonatan Werpers <jonatan@werpers.com>
parents:
105
diff
changeset
|
97 % Creates the arc length parameterization of a curve. |
06c3034966b7
Added and changed some comments. Also my text editor removed a bunch of whitespace.
Jonatan Werpers <jonatan@werpers.com>
parents:
105
diff
changeset
|
98 % N -- number of points used to approximate the arclength function |
103
bc5db54f9efd
fzero_vec, integral_vec and spline are now local functions in Curve. Renamed arcLengthStretch to arcLengthParametrization. Removed plot_derivative. Added some comments and extra lines + removed unneccesary lines. arcLength is now a method and not static. Constructor does not accept difference operator anymore.
Martin Almquist <martin.almquist@it.uu.se>
parents:
99
diff
changeset
|
99 function curve = arcLengthParametrization(obj,N) |
98
b30f3d8845f4
Removed arclength property. Constructor compatible again. Translation and rotation are fast now, but ti is slow.
Martin Almquist <martin.almquist@it.uu.se>
parents:
87
diff
changeset
|
100 default_arg('N',100); |
105
1df4f3704b76
Added error checking for all methods that use the derivative gp. Removed that from the to do list. Updated some comments.
Martin Almquist <martin.almquist@it.uu.se>
parents:
104
diff
changeset
|
101 assert(~isempty(obj.gp),'Curve has no derivative!'); |
107
06c3034966b7
Added and changed some comments. Also my text editor removed a bunch of whitespace.
Jonatan Werpers <jonatan@werpers.com>
parents:
105
diff
changeset
|
102 |
98
b30f3d8845f4
Removed arclength property. Constructor compatible again. Translation and rotation are fast now, but ti is slow.
Martin Almquist <martin.almquist@it.uu.se>
parents:
87
diff
changeset
|
103 % Construct arcLength function using splines |
b30f3d8845f4
Removed arclength property. Constructor compatible again. Translation and rotation are fast now, but ti is slow.
Martin Almquist <martin.almquist@it.uu.se>
parents:
87
diff
changeset
|
104 tvec = linspace(0,1,N); |
103
bc5db54f9efd
fzero_vec, integral_vec and spline are now local functions in Curve. Renamed arcLengthStretch to arcLengthParametrization. Removed plot_derivative. Added some comments and extra lines + removed unneccesary lines. arcLength is now a method and not static. Constructor does not accept difference operator anymore.
Martin Almquist <martin.almquist@it.uu.se>
parents:
99
diff
changeset
|
105 arcVec = obj.arcLength(0,tvec); |
1094
ad3089f04e0b
Revert changes in Curve so that spline() is not a static method.
Martin Almquist <malmquist@stanford.edu>
parents:
1090
diff
changeset
|
106 tFunc = spline(arcVec,tvec); % t as a function of arcLength |
104
ffd9e68f63cc
Splined in the other direction! Works great. Runs faster. Removed ugly fix outside [0,1] in spline(). Camelcased numerical_derivative().
Martin Almquist <martin.almquist@it.uu.se>
parents:
103
diff
changeset
|
107 L = obj.arcLength(0,1); |
ffd9e68f63cc
Splined in the other direction! Works great. Runs faster. Removed ugly fix outside [0,1] in spline(). Camelcased numerical_derivative().
Martin Almquist <martin.almquist@it.uu.se>
parents:
103
diff
changeset
|
108 arcPar = @(s) tFunc(s*L); |
107
06c3034966b7
Added and changed some comments. Also my text editor removed a bunch of whitespace.
Jonatan Werpers <jonatan@werpers.com>
parents:
105
diff
changeset
|
109 |
98
b30f3d8845f4
Removed arclength property. Constructor compatible again. Translation and rotation are fast now, but ti is slow.
Martin Almquist <martin.almquist@it.uu.se>
parents:
87
diff
changeset
|
110 % New function and derivative |
b30f3d8845f4
Removed arclength property. Constructor compatible again. Translation and rotation are fast now, but ti is slow.
Martin Almquist <martin.almquist@it.uu.se>
parents:
87
diff
changeset
|
111 g_new = @(t)obj.g(arcPar(t)); |
103
bc5db54f9efd
fzero_vec, integral_vec and spline are now local functions in Curve. Renamed arcLengthStretch to arcLengthParametrization. Removed plot_derivative. Added some comments and extra lines + removed unneccesary lines. arcLength is now a method and not static. Constructor does not accept difference operator anymore.
Martin Almquist <martin.almquist@it.uu.se>
parents:
99
diff
changeset
|
112 gp_new = @(t) normalize(obj.gp(arcPar(t))); |
248
81e0ead29431
Fixed package names in +parametrization
Jonatan Werpers <jonatan@werpers.com>
parents:
151
diff
changeset
|
113 curve = parametrization.Curve(g_new,gp_new); |
107
06c3034966b7
Added and changed some comments. Also my text editor removed a bunch of whitespace.
Jonatan Werpers <jonatan@werpers.com>
parents:
105
diff
changeset
|
114 |
86
3c39dd714fb6
In Curve: Added numerical FD differentiation if derivative is not supplied. Added arc length computation based on the derivative. Added arc length parameterization (but this function is very slow.). In +util: Added fzero_vec.m and integral_vec.m, which call fzero and integral but take vector arguments.
Martin Almquist <martin.almquist@it.uu.se>
parents:
0
diff
changeset
|
115 end |
107
06c3034966b7
Added and changed some comments. Also my text editor removed a bunch of whitespace.
Jonatan Werpers <jonatan@werpers.com>
parents:
105
diff
changeset
|
116 |
0 | 117 % how to make it work for methods without returns |
118 function p = subsref(obj,S) | |
119 %Should i add error checking here? | |
120 %Maybe if you want performance you fetch obj.g and then use that | |
121 switch S(1).type | |
122 case '()' | |
123 p = obj.g(S.subs{1}); | |
124 % case '.' | |
125 | |
126 % p = obj.(S.subs); | |
127 otherwise | |
128 p = builtin('subsref',obj,S); | |
129 % error() | |
130 end | |
131 end | |
132 | |
133 | |
134 %% TRANSFORMATION OF A CURVE | |
135 function D = reverse(C) | |
136 % g = C.g; | |
137 % gp = C.gp; | |
248
81e0ead29431
Fixed package names in +parametrization
Jonatan Werpers <jonatan@werpers.com>
parents:
151
diff
changeset
|
138 % D = parametrization.Curve(@(t)g(1-t),@(t)-gp(1-t)); |
0 | 139 D = C.transform([],[],-1); |
140 end | |
141 | |
142 function D = transform(C,A,b,flip) | |
143 default_arg('A',[1 0; 0 1]); | |
144 default_arg('b',[0; 0]); | |
145 default_arg('flip',1); | |
146 if isempty(C.transformation) | |
147 g = C.g; | |
148 gp = C.gp; | |
149 transformation.A = A; | |
150 transformation.b = b; | |
151 transformation.flip = flip; | |
152 else | |
153 g = C.transformation.base_g; | |
154 gp = C.transformation.base_gp; | |
155 A_old = C.transformation.A; | |
156 b_old = C.transformation.b; | |
157 flip_old = C.transformation.flip; | |
158 | |
159 transformation.A = A*A_old; | |
160 transformation.b = A*b_old + b; | |
161 transformation.flip = flip*flip_old; | |
162 end | |
163 | |
248
81e0ead29431
Fixed package names in +parametrization
Jonatan Werpers <jonatan@werpers.com>
parents:
151
diff
changeset
|
164 D = parametrization.Curve(g,gp,transformation); |
0 | 165 |
166 end | |
167 | |
168 function D = translate(C,a) | |
169 g = C.g; | |
170 gp = C.gp; | |
171 | |
172 % function v = g_fun(t) | |
173 % x = g(t); | |
174 % v(1,:) = x(1,:)+a(1); | |
175 % v(2,:) = x(2,:)+a(2); | |
176 % end | |
177 | |
248
81e0ead29431
Fixed package names in +parametrization
Jonatan Werpers <jonatan@werpers.com>
parents:
151
diff
changeset
|
178 % D = parametrization.Curve(@g_fun,gp); |
0 | 179 |
180 D = C.transform([],a); | |
181 end | |
182 | |
183 function D = mirror(C, a, b) | |
777
0776fa4754ff
Switch to assertSize from assert_size
Jonatan Werpers <jonatan@werpers.com>
parents:
333
diff
changeset
|
184 assertSize(a,[2,1]); |
0776fa4754ff
Switch to assertSize from assert_size
Jonatan Werpers <jonatan@werpers.com>
parents:
333
diff
changeset
|
185 assertSize(b,[2,1]); |
0 | 186 |
187 g = C.g; | |
188 gp = C.gp; | |
189 | |
190 l = b-a; | |
191 lx = l(1); | |
192 ly = l(2); | |
193 | |
194 | |
195 % fprintf('Singular?\n') | |
196 | |
197 A = [lx^2-ly^2 2*lx*ly; 2*lx*ly ly^2-lx^2]/(l'*l); | |
198 | |
199 % function v = g_fun(t) | |
200 % % v = a + A*(g(t)-a) | |
201 % x = g(t); | |
202 | |
203 % ax1 = x(1,:)-a(1); | |
204 % ax2 = x(2,:)-a(2); | |
205 % v(1,:) = a(1)+A(1,:)*[ax1;ax2]; | |
206 % v(2,:) = a(2)+A(2,:)*[ax1;ax2]; | |
207 % end | |
208 | |
209 % function v = gp_fun(t) | |
210 % v = A*gp(t); | |
211 % end | |
212 | |
248
81e0ead29431
Fixed package names in +parametrization
Jonatan Werpers <jonatan@werpers.com>
parents:
151
diff
changeset
|
213 % D = parametrization.Curve(@g_fun,@gp_fun); |
0 | 214 |
215 % g = A(g-a)+a = Ag - Aa + a; | |
216 b = - A*a + a; | |
217 D = C.transform(A,b); | |
218 | |
219 end | |
220 | |
221 function D = rotate(C,a,rad) | |
777
0776fa4754ff
Switch to assertSize from assert_size
Jonatan Werpers <jonatan@werpers.com>
parents:
333
diff
changeset
|
222 assertSize(a, [2,1]); |
0776fa4754ff
Switch to assertSize from assert_size
Jonatan Werpers <jonatan@werpers.com>
parents:
333
diff
changeset
|
223 assertSize(rad, [1,1]); |
0 | 224 g = C.g; |
225 gp = C.gp; | |
226 | |
227 | |
228 A = [cos(rad) -sin(rad); sin(rad) cos(rad)]; | |
229 | |
230 % function v = g_fun(t) | |
231 % % v = a + A*(g(t)-a) | |
232 % x = g(t); | |
233 | |
234 % ax1 = x(1,:)-a(1); | |
235 % ax2 = x(2,:)-a(2); | |
236 % v(1,:) = a(1)+A(1,:)*[ax1;ax2]; | |
237 % v(2,:) = a(2)+A(2,:)*[ax1;ax2]; | |
238 % end | |
239 | |
240 % function v = gp_fun(t) | |
241 % v = A*gp(t); | |
242 % end | |
243 | |
248
81e0ead29431
Fixed package names in +parametrization
Jonatan Werpers <jonatan@werpers.com>
parents:
151
diff
changeset
|
244 % D = parametrization.Curve(@g_fun,@gp_fun); |
0 | 245 |
246 | |
247 % g = A(g-a)+a = Ag - Aa + a; | |
248 b = - A*a + a; | |
249 D = C.transform(A,b); | |
250 end | |
251 end | |
252 | |
253 methods (Static) | |
107
06c3034966b7
Added and changed some comments. Also my text editor removed a bunch of whitespace.
Jonatan Werpers <jonatan@werpers.com>
parents:
105
diff
changeset
|
254 |
06c3034966b7
Added and changed some comments. Also my text editor removed a bunch of whitespace.
Jonatan Werpers <jonatan@werpers.com>
parents:
105
diff
changeset
|
255 % Computes the derivative of g: R -> R^2 using an operator D1 |
104
ffd9e68f63cc
Splined in the other direction! Works great. Runs faster. Removed ugly fix outside [0,1] in spline(). Camelcased numerical_derivative().
Martin Almquist <martin.almquist@it.uu.se>
parents:
103
diff
changeset
|
256 function gp_out = numericalDerivative(g,D1) |
107
06c3034966b7
Added and changed some comments. Also my text editor removed a bunch of whitespace.
Jonatan Werpers <jonatan@werpers.com>
parents:
105
diff
changeset
|
257 m = length(D1); |
06c3034966b7
Added and changed some comments. Also my text editor removed a bunch of whitespace.
Jonatan Werpers <jonatan@werpers.com>
parents:
105
diff
changeset
|
258 t = linspace(0,1,m); |
103
bc5db54f9efd
fzero_vec, integral_vec and spline are now local functions in Curve. Renamed arcLengthStretch to arcLengthParametrization. Removed plot_derivative. Added some comments and extra lines + removed unneccesary lines. arcLength is now a method and not static. Constructor does not accept difference operator anymore.
Martin Almquist <martin.almquist@it.uu.se>
parents:
99
diff
changeset
|
259 gVec = g(t)'; |
bc5db54f9efd
fzero_vec, integral_vec and spline are now local functions in Curve. Renamed arcLengthStretch to arcLengthParametrization. Removed plot_derivative. Added some comments and extra lines + removed unneccesary lines. arcLength is now a method and not static. Constructor does not accept difference operator anymore.
Martin Almquist <martin.almquist@it.uu.se>
parents:
99
diff
changeset
|
260 gpVec = (D1*gVec)'; |
87
0a29a60e0b21
In Curve: Rearranged for speed. arc_length_fun is now a property of Curve. If it is not supplied, it is computed via the derivative and spline fitting. Switching to the arc length parameterization is much faster now. The new stuff can be tested with testArcLength.m (which should be deleted after that).
Martin Almquist <martin.almquist@it.uu.se>
parents:
86
diff
changeset
|
261 |
1094
ad3089f04e0b
Revert changes in Curve so that spline() is not a static method.
Martin Almquist <malmquist@stanford.edu>
parents:
1090
diff
changeset
|
262 gp1_fun = spline(t,gpVec(1,:)); |
ad3089f04e0b
Revert changes in Curve so that spline() is not a static method.
Martin Almquist <malmquist@stanford.edu>
parents:
1090
diff
changeset
|
263 gp2_fun = spline(t,gpVec(2,:)); |
86
3c39dd714fb6
In Curve: Added numerical FD differentiation if derivative is not supplied. Added arc length computation based on the derivative. Added arc length parameterization (but this function is very slow.). In +util: Added fzero_vec.m and integral_vec.m, which call fzero and integral but take vector arguments.
Martin Almquist <martin.almquist@it.uu.se>
parents:
0
diff
changeset
|
264 gp_out = @(t) [gp1_fun(t);gp2_fun(t)]; |
3c39dd714fb6
In Curve: Added numerical FD differentiation if derivative is not supplied. Added arc length computation based on the derivative. Added arc length parameterization (but this function is very slow.). In +util: Added fzero_vec.m and integral_vec.m, which call fzero and integral but take vector arguments.
Martin Almquist <martin.almquist@it.uu.se>
parents:
0
diff
changeset
|
265 end |
107
06c3034966b7
Added and changed some comments. Also my text editor removed a bunch of whitespace.
Jonatan Werpers <jonatan@werpers.com>
parents:
105
diff
changeset
|
266 |
0 | 267 function obj = line(p1, p2) |
268 | |
269 function v = g_fun(t) | |
270 v(1,:) = p1(1) + t.*(p2(1)-p1(1)); | |
271 v(2,:) = p1(2) + t.*(p2(2)-p1(2)); | |
272 end | |
273 | |
333
0fd4b259af9e
Fixed some bugs in parametrization.Curve.
Jonatan Werpers <jonatan@werpers.com>
parents:
248
diff
changeset
|
274 function v = g_fun_deriv(t) |
0fd4b259af9e
Fixed some bugs in parametrization.Curve.
Jonatan Werpers <jonatan@werpers.com>
parents:
248
diff
changeset
|
275 v(1,:) = t.*0 + (p2(1)-p1(1)); |
0fd4b259af9e
Fixed some bugs in parametrization.Curve.
Jonatan Werpers <jonatan@werpers.com>
parents:
248
diff
changeset
|
276 v(2,:) = t.*0 + (p2(2)-p1(2)); |
0fd4b259af9e
Fixed some bugs in parametrization.Curve.
Jonatan Werpers <jonatan@werpers.com>
parents:
248
diff
changeset
|
277 end |
0fd4b259af9e
Fixed some bugs in parametrization.Curve.
Jonatan Werpers <jonatan@werpers.com>
parents:
248
diff
changeset
|
278 |
0fd4b259af9e
Fixed some bugs in parametrization.Curve.
Jonatan Werpers <jonatan@werpers.com>
parents:
248
diff
changeset
|
279 obj = parametrization.Curve(@g_fun, @g_fun_deriv); |
0 | 280 end |
281 | |
282 function obj = circle(c,r,phi) | |
283 default_arg('phi',[0; 2*pi]) | |
284 default_arg('c',[0; 0]) | |
285 default_arg('r',1) | |
286 | |
287 function v = g_fun(t) | |
288 w = phi(1)+t*(phi(2)-phi(1)); | |
289 v(1,:) = c(1) + r*cos(w); | |
290 v(2,:) = c(2) + r*sin(w); | |
291 end | |
292 | |
293 function v = g_fun_deriv(t) | |
294 w = phi(1)+t*(phi(2)-phi(1)); | |
295 v(1,:) = -(phi(2)-phi(1))*r*sin(w); | |
296 v(2,:) = (phi(2)-phi(1))*r*cos(w); | |
297 end | |
298 | |
248
81e0ead29431
Fixed package names in +parametrization
Jonatan Werpers <jonatan@werpers.com>
parents:
151
diff
changeset
|
299 obj = parametrization.Curve(@g_fun,@g_fun_deriv); |
0 | 300 end |
301 | |
302 function obj = bezier(p0, p1, p2, p3) | |
303 function v = g_fun(t) | |
304 v(1,:) = (1-t).^3*p0(1) + 3*(1-t).^2.*t*p1(1) + 3*(1-t).*t.^2*p2(1) + t.^3*p3(1); | |
305 v(2,:) = (1-t).^3*p0(2) + 3*(1-t).^2.*t*p1(2) + 3*(1-t).*t.^2*p2(2) + t.^3*p3(2); | |
306 end | |
307 | |
308 function v = g_fun_deriv(t) | |
309 v(1,:) = 3*(1-t).^2*(p1(1)-p0(1)) + 6*(1-t).*t*(p2(1)-p1(1)) + 3*t.^2*(p3(1)-p2(1)); | |
310 v(2,:) = 3*(1-t).^2*(p1(2)-p0(2)) + 6*(1-t).*t*(p2(2)-p1(2)) + 3*t.^2*(p3(2)-p2(2)); | |
311 end | |
312 | |
248
81e0ead29431
Fixed package names in +parametrization
Jonatan Werpers <jonatan@werpers.com>
parents:
151
diff
changeset
|
313 obj = parametrization.Curve(@g_fun,@g_fun_deriv); |
0 | 314 end |
315 | |
316 | |
317 function [g_out,gp_out] = transform_g(g,gp,tr) | |
318 A = tr.A; | |
319 b = tr.b; | |
320 flip = tr.flip; | |
321 | |
322 function v = g_fun_noflip(t) | |
323 % v = A*g + b | |
324 x = g(t); | |
325 | |
326 v(1,:) = A(1,:)*x+b(1); | |
327 v(2,:) = A(2,:)*x+b(2); | |
328 end | |
329 | |
330 function v = g_fun_flip(t) | |
331 % v = A*g + b | |
332 x = g(1-t); | |
333 | |
334 v(1,:) = A(1,:)*x+b(1); | |
335 v(2,:) = A(2,:)*x+b(2); | |
336 end | |
337 | |
338 | |
339 switch flip | |
340 case 1 | |
341 g_out = @g_fun_noflip; | |
342 gp_out = @(t)A*gp(t); | |
343 case -1 | |
344 g_out = @g_fun_flip; | |
345 gp_out = @(t)-A*gp(1-t); | |
346 end | |
347 end | |
348 | |
107
06c3034966b7
Added and changed some comments. Also my text editor removed a bunch of whitespace.
Jonatan Werpers <jonatan@werpers.com>
parents:
105
diff
changeset
|
349 end |
0 | 350 end |
86
3c39dd714fb6
In Curve: Added numerical FD differentiation if derivative is not supplied. Added arc length computation based on the derivative. Added arc length parameterization (but this function is very slow.). In +util: Added fzero_vec.m and integral_vec.m, which call fzero and integral but take vector arguments.
Martin Almquist <martin.almquist@it.uu.se>
parents:
0
diff
changeset
|
351 |
3c39dd714fb6
In Curve: Added numerical FD differentiation if derivative is not supplied. Added arc length computation based on the derivative. Added arc length parameterization (but this function is very slow.). In +util: Added fzero_vec.m and integral_vec.m, which call fzero and integral but take vector arguments.
Martin Almquist <martin.almquist@it.uu.se>
parents:
0
diff
changeset
|
352 function g_norm = normalize(g0) |
107
06c3034966b7
Added and changed some comments. Also my text editor removed a bunch of whitespace.
Jonatan Werpers <jonatan@werpers.com>
parents:
105
diff
changeset
|
353 g1 = g0(1,:); |
103
bc5db54f9efd
fzero_vec, integral_vec and spline are now local functions in Curve. Renamed arcLengthStretch to arcLengthParametrization. Removed plot_derivative. Added some comments and extra lines + removed unneccesary lines. arcLength is now a method and not static. Constructor does not accept difference operator anymore.
Martin Almquist <martin.almquist@it.uu.se>
parents:
99
diff
changeset
|
354 g2 = g0(2,:); |
86
3c39dd714fb6
In Curve: Added numerical FD differentiation if derivative is not supplied. Added arc length computation based on the derivative. Added arc length parameterization (but this function is very slow.). In +util: Added fzero_vec.m and integral_vec.m, which call fzero and integral but take vector arguments.
Martin Almquist <martin.almquist@it.uu.se>
parents:
0
diff
changeset
|
355 normalization = sqrt(sum(g0.^2,1)); |
3c39dd714fb6
In Curve: Added numerical FD differentiation if derivative is not supplied. Added arc length computation based on the derivative. Added arc length parameterization (but this function is very slow.). In +util: Added fzero_vec.m and integral_vec.m, which call fzero and integral but take vector arguments.
Martin Almquist <martin.almquist@it.uu.se>
parents:
0
diff
changeset
|
356 g_norm = [g1./normalization; g2./normalization]; |
3c39dd714fb6
In Curve: Added numerical FD differentiation if derivative is not supplied. Added arc length computation based on the derivative. Added arc length parameterization (but this function is very slow.). In +util: Added fzero_vec.m and integral_vec.m, which call fzero and integral but take vector arguments.
Martin Almquist <martin.almquist@it.uu.se>
parents:
0
diff
changeset
|
357 end |
3c39dd714fb6
In Curve: Added numerical FD differentiation if derivative is not supplied. Added arc length computation based on the derivative. Added arc length parameterization (but this function is very slow.). In +util: Added fzero_vec.m and integral_vec.m, which call fzero and integral but take vector arguments.
Martin Almquist <martin.almquist@it.uu.se>
parents:
0
diff
changeset
|
358 |
103
bc5db54f9efd
fzero_vec, integral_vec and spline are now local functions in Curve. Renamed arcLengthStretch to arcLengthParametrization. Removed plot_derivative. Added some comments and extra lines + removed unneccesary lines. arcLength is now a method and not static. Constructor does not accept difference operator anymore.
Martin Almquist <martin.almquist@it.uu.se>
parents:
99
diff
changeset
|
359 function I = integral_vec(f,a,b) |
bc5db54f9efd
fzero_vec, integral_vec and spline are now local functions in Curve. Renamed arcLengthStretch to arcLengthParametrization. Removed plot_derivative. Added some comments and extra lines + removed unneccesary lines. arcLength is now a method and not static. Constructor does not accept difference operator anymore.
Martin Almquist <martin.almquist@it.uu.se>
parents:
99
diff
changeset
|
360 % Wrapper around the built-in function integral that |
bc5db54f9efd
fzero_vec, integral_vec and spline are now local functions in Curve. Renamed arcLengthStretch to arcLengthParametrization. Removed plot_derivative. Added some comments and extra lines + removed unneccesary lines. arcLength is now a method and not static. Constructor does not accept difference operator anymore.
Martin Almquist <martin.almquist@it.uu.se>
parents:
99
diff
changeset
|
361 % handles multiple limits. |
bc5db54f9efd
fzero_vec, integral_vec and spline are now local functions in Curve. Renamed arcLengthStretch to arcLengthParametrization. Removed plot_derivative. Added some comments and extra lines + removed unneccesary lines. arcLength is now a method and not static. Constructor does not accept difference operator anymore.
Martin Almquist <martin.almquist@it.uu.se>
parents:
99
diff
changeset
|
362 |
bc5db54f9efd
fzero_vec, integral_vec and spline are now local functions in Curve. Renamed arcLengthStretch to arcLengthParametrization. Removed plot_derivative. Added some comments and extra lines + removed unneccesary lines. arcLength is now a method and not static. Constructor does not accept difference operator anymore.
Martin Almquist <martin.almquist@it.uu.se>
parents:
99
diff
changeset
|
363 Na = length(a); |
bc5db54f9efd
fzero_vec, integral_vec and spline are now local functions in Curve. Renamed arcLengthStretch to arcLengthParametrization. Removed plot_derivative. Added some comments and extra lines + removed unneccesary lines. arcLength is now a method and not static. Constructor does not accept difference operator anymore.
Martin Almquist <martin.almquist@it.uu.se>
parents:
99
diff
changeset
|
364 Nb = length(b); |
bc5db54f9efd
fzero_vec, integral_vec and spline are now local functions in Curve. Renamed arcLengthStretch to arcLengthParametrization. Removed plot_derivative. Added some comments and extra lines + removed unneccesary lines. arcLength is now a method and not static. Constructor does not accept difference operator anymore.
Martin Almquist <martin.almquist@it.uu.se>
parents:
99
diff
changeset
|
365 assert(Na == 1 || Nb == 1 || Na==Nb,... |
bc5db54f9efd
fzero_vec, integral_vec and spline are now local functions in Curve. Renamed arcLengthStretch to arcLengthParametrization. Removed plot_derivative. Added some comments and extra lines + removed unneccesary lines. arcLength is now a method and not static. Constructor does not accept difference operator anymore.
Martin Almquist <martin.almquist@it.uu.se>
parents:
99
diff
changeset
|
366 'a and b must have same length, unless one is a scalar.'); |
107
06c3034966b7
Added and changed some comments. Also my text editor removed a bunch of whitespace.
Jonatan Werpers <jonatan@werpers.com>
parents:
105
diff
changeset
|
367 |
103
bc5db54f9efd
fzero_vec, integral_vec and spline are now local functions in Curve. Renamed arcLengthStretch to arcLengthParametrization. Removed plot_derivative. Added some comments and extra lines + removed unneccesary lines. arcLength is now a method and not static. Constructor does not accept difference operator anymore.
Martin Almquist <martin.almquist@it.uu.se>
parents:
99
diff
changeset
|
368 if(Na>Nb); |
bc5db54f9efd
fzero_vec, integral_vec and spline are now local functions in Curve. Renamed arcLengthStretch to arcLengthParametrization. Removed plot_derivative. Added some comments and extra lines + removed unneccesary lines. arcLength is now a method and not static. Constructor does not accept difference operator anymore.
Martin Almquist <martin.almquist@it.uu.se>
parents:
99
diff
changeset
|
369 I = zeros(size(a)); |
bc5db54f9efd
fzero_vec, integral_vec and spline are now local functions in Curve. Renamed arcLengthStretch to arcLengthParametrization. Removed plot_derivative. Added some comments and extra lines + removed unneccesary lines. arcLength is now a method and not static. Constructor does not accept difference operator anymore.
Martin Almquist <martin.almquist@it.uu.se>
parents:
99
diff
changeset
|
370 for i = 1:Na |
bc5db54f9efd
fzero_vec, integral_vec and spline are now local functions in Curve. Renamed arcLengthStretch to arcLengthParametrization. Removed plot_derivative. Added some comments and extra lines + removed unneccesary lines. arcLength is now a method and not static. Constructor does not accept difference operator anymore.
Martin Almquist <martin.almquist@it.uu.se>
parents:
99
diff
changeset
|
371 I(i) = integral(f,a(i),b); |
bc5db54f9efd
fzero_vec, integral_vec and spline are now local functions in Curve. Renamed arcLengthStretch to arcLengthParametrization. Removed plot_derivative. Added some comments and extra lines + removed unneccesary lines. arcLength is now a method and not static. Constructor does not accept difference operator anymore.
Martin Almquist <martin.almquist@it.uu.se>
parents:
99
diff
changeset
|
372 end |
bc5db54f9efd
fzero_vec, integral_vec and spline are now local functions in Curve. Renamed arcLengthStretch to arcLengthParametrization. Removed plot_derivative. Added some comments and extra lines + removed unneccesary lines. arcLength is now a method and not static. Constructor does not accept difference operator anymore.
Martin Almquist <martin.almquist@it.uu.se>
parents:
99
diff
changeset
|
373 elseif(Nb>Na) |
bc5db54f9efd
fzero_vec, integral_vec and spline are now local functions in Curve. Renamed arcLengthStretch to arcLengthParametrization. Removed plot_derivative. Added some comments and extra lines + removed unneccesary lines. arcLength is now a method and not static. Constructor does not accept difference operator anymore.
Martin Almquist <martin.almquist@it.uu.se>
parents:
99
diff
changeset
|
374 I = zeros(size(b)); |
bc5db54f9efd
fzero_vec, integral_vec and spline are now local functions in Curve. Renamed arcLengthStretch to arcLengthParametrization. Removed plot_derivative. Added some comments and extra lines + removed unneccesary lines. arcLength is now a method and not static. Constructor does not accept difference operator anymore.
Martin Almquist <martin.almquist@it.uu.se>
parents:
99
diff
changeset
|
375 for i = 1:Nb |
bc5db54f9efd
fzero_vec, integral_vec and spline are now local functions in Curve. Renamed arcLengthStretch to arcLengthParametrization. Removed plot_derivative. Added some comments and extra lines + removed unneccesary lines. arcLength is now a method and not static. Constructor does not accept difference operator anymore.
Martin Almquist <martin.almquist@it.uu.se>
parents:
99
diff
changeset
|
376 I(i) = integral(f,a,b(i)); |
bc5db54f9efd
fzero_vec, integral_vec and spline are now local functions in Curve. Renamed arcLengthStretch to arcLengthParametrization. Removed plot_derivative. Added some comments and extra lines + removed unneccesary lines. arcLength is now a method and not static. Constructor does not accept difference operator anymore.
Martin Almquist <martin.almquist@it.uu.se>
parents:
99
diff
changeset
|
377 end |
bc5db54f9efd
fzero_vec, integral_vec and spline are now local functions in Curve. Renamed arcLengthStretch to arcLengthParametrization. Removed plot_derivative. Added some comments and extra lines + removed unneccesary lines. arcLength is now a method and not static. Constructor does not accept difference operator anymore.
Martin Almquist <martin.almquist@it.uu.se>
parents:
99
diff
changeset
|
378 else |
bc5db54f9efd
fzero_vec, integral_vec and spline are now local functions in Curve. Renamed arcLengthStretch to arcLengthParametrization. Removed plot_derivative. Added some comments and extra lines + removed unneccesary lines. arcLength is now a method and not static. Constructor does not accept difference operator anymore.
Martin Almquist <martin.almquist@it.uu.se>
parents:
99
diff
changeset
|
379 I = zeros(size(b)); |
bc5db54f9efd
fzero_vec, integral_vec and spline are now local functions in Curve. Renamed arcLengthStretch to arcLengthParametrization. Removed plot_derivative. Added some comments and extra lines + removed unneccesary lines. arcLength is now a method and not static. Constructor does not accept difference operator anymore.
Martin Almquist <martin.almquist@it.uu.se>
parents:
99
diff
changeset
|
380 for i = 1:Nb |
bc5db54f9efd
fzero_vec, integral_vec and spline are now local functions in Curve. Renamed arcLengthStretch to arcLengthParametrization. Removed plot_derivative. Added some comments and extra lines + removed unneccesary lines. arcLength is now a method and not static. Constructor does not accept difference operator anymore.
Martin Almquist <martin.almquist@it.uu.se>
parents:
99
diff
changeset
|
381 I(i) = integral(f,a(i),b(i)); |
bc5db54f9efd
fzero_vec, integral_vec and spline are now local functions in Curve. Renamed arcLengthStretch to arcLengthParametrization. Removed plot_derivative. Added some comments and extra lines + removed unneccesary lines. arcLength is now a method and not static. Constructor does not accept difference operator anymore.
Martin Almquist <martin.almquist@it.uu.se>
parents:
99
diff
changeset
|
382 end |
bc5db54f9efd
fzero_vec, integral_vec and spline are now local functions in Curve. Renamed arcLengthStretch to arcLengthParametrization. Removed plot_derivative. Added some comments and extra lines + removed unneccesary lines. arcLength is now a method and not static. Constructor does not accept difference operator anymore.
Martin Almquist <martin.almquist@it.uu.se>
parents:
99
diff
changeset
|
383 end |
bc5db54f9efd
fzero_vec, integral_vec and spline are now local functions in Curve. Renamed arcLengthStretch to arcLengthParametrization. Removed plot_derivative. Added some comments and extra lines + removed unneccesary lines. arcLength is now a method and not static. Constructor does not accept difference operator anymore.
Martin Almquist <martin.almquist@it.uu.se>
parents:
99
diff
changeset
|
384 end |
1094
ad3089f04e0b
Revert changes in Curve so that spline() is not a static method.
Martin Almquist <malmquist@stanford.edu>
parents:
1090
diff
changeset
|
385 |
ad3089f04e0b
Revert changes in Curve so that spline() is not a static method.
Martin Almquist <malmquist@stanford.edu>
parents:
1090
diff
changeset
|
386 % Returns a function handle to the spline. |
ad3089f04e0b
Revert changes in Curve so that spline() is not a static method.
Martin Almquist <malmquist@stanford.edu>
parents:
1090
diff
changeset
|
387 function f = spline(tval,fval,spline_order) |
ad3089f04e0b
Revert changes in Curve so that spline() is not a static method.
Martin Almquist <malmquist@stanford.edu>
parents:
1090
diff
changeset
|
388 default_arg('spline_order',4); |
ad3089f04e0b
Revert changes in Curve so that spline() is not a static method.
Martin Almquist <malmquist@stanford.edu>
parents:
1090
diff
changeset
|
389 [m,~] = size(tval); |
ad3089f04e0b
Revert changes in Curve so that spline() is not a static method.
Martin Almquist <malmquist@stanford.edu>
parents:
1090
diff
changeset
|
390 assert(m==1,'Need row vectors.'); |
ad3089f04e0b
Revert changes in Curve so that spline() is not a static method.
Martin Almquist <malmquist@stanford.edu>
parents:
1090
diff
changeset
|
391 |
ad3089f04e0b
Revert changes in Curve so that spline() is not a static method.
Martin Almquist <malmquist@stanford.edu>
parents:
1090
diff
changeset
|
392 f_spline = spapi( optknt(tval,spline_order), tval, fval ); |
ad3089f04e0b
Revert changes in Curve so that spline() is not a static method.
Martin Almquist <malmquist@stanford.edu>
parents:
1090
diff
changeset
|
393 f = @(t) fnval(f_spline,t); |
ad3089f04e0b
Revert changes in Curve so that spline() is not a static method.
Martin Almquist <malmquist@stanford.edu>
parents:
1090
diff
changeset
|
394 end |