Mercurial > repos > public > sbplib
annotate +parametrization/Curve.m @ 1299:73e52c74baac feature/boundary_optimized_grids
Closed branch feature/boundary_optimized_grids
author | Vidar Stiernström <vidar.stiernstrom@it.uu.se> |
---|---|
date | Wed, 08 Jul 2020 18:20:57 +0200 |
parents | 7f4aae76e06a |
children | 60c875c18de3 |
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); |
1098
36d092a00040
Bugfix Curve.arcLengthParametrization() by using linear interpolation instead of higher-order spline for parameter as a funciton of arclength. Generalize dataSpline() to curves in higher dimensions.
Martin Almquist <malmquist@stanford.edu>
parents:
1094
diff
changeset
|
106 |
1103
7f4aae76e06a
In Curve.arcLengthParametrization(), upgrade linear interpolation to monotonicity-preserving cubic splines.
Martin Almquist <malmquist@stanford.edu>
parents:
1098
diff
changeset
|
107 % t as a function of arcLength. Monotonicity-preserving cubic splines. |
7f4aae76e06a
In Curve.arcLengthParametrization(), upgrade linear interpolation to monotonicity-preserving cubic splines.
Martin Almquist <malmquist@stanford.edu>
parents:
1098
diff
changeset
|
108 tFunc = @(arcLen) pchip(arcVec,tvec,arcLen); |
1098
36d092a00040
Bugfix Curve.arcLengthParametrization() by using linear interpolation instead of higher-order spline for parameter as a funciton of arclength. Generalize dataSpline() to curves in higher dimensions.
Martin Almquist <malmquist@stanford.edu>
parents:
1094
diff
changeset
|
109 |
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
|
110 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
|
111 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
|
112 |
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
|
113 % 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
|
114 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
|
115 gp_new = @(t) normalize(obj.gp(arcPar(t))); |
248
81e0ead29431
Fixed package names in +parametrization
Jonatan Werpers <jonatan@werpers.com>
parents:
151
diff
changeset
|
116 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
|
117 |
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
|
118 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
|
119 |
0 | 120 % how to make it work for methods without returns |
121 function p = subsref(obj,S) | |
122 %Should i add error checking here? | |
123 %Maybe if you want performance you fetch obj.g and then use that | |
124 switch S(1).type | |
125 case '()' | |
126 p = obj.g(S.subs{1}); | |
127 % case '.' | |
128 | |
129 % p = obj.(S.subs); | |
130 otherwise | |
131 p = builtin('subsref',obj,S); | |
132 % error() | |
133 end | |
134 end | |
135 | |
136 | |
137 %% TRANSFORMATION OF A CURVE | |
138 function D = reverse(C) | |
139 % g = C.g; | |
140 % gp = C.gp; | |
248
81e0ead29431
Fixed package names in +parametrization
Jonatan Werpers <jonatan@werpers.com>
parents:
151
diff
changeset
|
141 % D = parametrization.Curve(@(t)g(1-t),@(t)-gp(1-t)); |
0 | 142 D = C.transform([],[],-1); |
143 end | |
144 | |
145 function D = transform(C,A,b,flip) | |
146 default_arg('A',[1 0; 0 1]); | |
147 default_arg('b',[0; 0]); | |
148 default_arg('flip',1); | |
149 if isempty(C.transformation) | |
150 g = C.g; | |
151 gp = C.gp; | |
152 transformation.A = A; | |
153 transformation.b = b; | |
154 transformation.flip = flip; | |
155 else | |
156 g = C.transformation.base_g; | |
157 gp = C.transformation.base_gp; | |
158 A_old = C.transformation.A; | |
159 b_old = C.transformation.b; | |
160 flip_old = C.transformation.flip; | |
161 | |
162 transformation.A = A*A_old; | |
163 transformation.b = A*b_old + b; | |
164 transformation.flip = flip*flip_old; | |
165 end | |
166 | |
248
81e0ead29431
Fixed package names in +parametrization
Jonatan Werpers <jonatan@werpers.com>
parents:
151
diff
changeset
|
167 D = parametrization.Curve(g,gp,transformation); |
0 | 168 |
169 end | |
170 | |
171 function D = translate(C,a) | |
172 g = C.g; | |
173 gp = C.gp; | |
174 | |
175 % function v = g_fun(t) | |
176 % x = g(t); | |
177 % v(1,:) = x(1,:)+a(1); | |
178 % v(2,:) = x(2,:)+a(2); | |
179 % end | |
180 | |
248
81e0ead29431
Fixed package names in +parametrization
Jonatan Werpers <jonatan@werpers.com>
parents:
151
diff
changeset
|
181 % D = parametrization.Curve(@g_fun,gp); |
0 | 182 |
183 D = C.transform([],a); | |
184 end | |
185 | |
186 function D = mirror(C, a, b) | |
777
0776fa4754ff
Switch to assertSize from assert_size
Jonatan Werpers <jonatan@werpers.com>
parents:
333
diff
changeset
|
187 assertSize(a,[2,1]); |
0776fa4754ff
Switch to assertSize from assert_size
Jonatan Werpers <jonatan@werpers.com>
parents:
333
diff
changeset
|
188 assertSize(b,[2,1]); |
0 | 189 |
190 g = C.g; | |
191 gp = C.gp; | |
192 | |
193 l = b-a; | |
194 lx = l(1); | |
195 ly = l(2); | |
196 | |
197 | |
198 % fprintf('Singular?\n') | |
199 | |
200 A = [lx^2-ly^2 2*lx*ly; 2*lx*ly ly^2-lx^2]/(l'*l); | |
201 | |
202 % function v = g_fun(t) | |
203 % % v = a + A*(g(t)-a) | |
204 % x = g(t); | |
205 | |
206 % ax1 = x(1,:)-a(1); | |
207 % ax2 = x(2,:)-a(2); | |
208 % v(1,:) = a(1)+A(1,:)*[ax1;ax2]; | |
209 % v(2,:) = a(2)+A(2,:)*[ax1;ax2]; | |
210 % end | |
211 | |
212 % function v = gp_fun(t) | |
213 % v = A*gp(t); | |
214 % end | |
215 | |
248
81e0ead29431
Fixed package names in +parametrization
Jonatan Werpers <jonatan@werpers.com>
parents:
151
diff
changeset
|
216 % D = parametrization.Curve(@g_fun,@gp_fun); |
0 | 217 |
218 % g = A(g-a)+a = Ag - Aa + a; | |
219 b = - A*a + a; | |
220 D = C.transform(A,b); | |
221 | |
222 end | |
223 | |
224 function D = rotate(C,a,rad) | |
777
0776fa4754ff
Switch to assertSize from assert_size
Jonatan Werpers <jonatan@werpers.com>
parents:
333
diff
changeset
|
225 assertSize(a, [2,1]); |
0776fa4754ff
Switch to assertSize from assert_size
Jonatan Werpers <jonatan@werpers.com>
parents:
333
diff
changeset
|
226 assertSize(rad, [1,1]); |
0 | 227 g = C.g; |
228 gp = C.gp; | |
229 | |
230 | |
231 A = [cos(rad) -sin(rad); sin(rad) cos(rad)]; | |
232 | |
233 % function v = g_fun(t) | |
234 % % v = a + A*(g(t)-a) | |
235 % x = g(t); | |
236 | |
237 % ax1 = x(1,:)-a(1); | |
238 % ax2 = x(2,:)-a(2); | |
239 % v(1,:) = a(1)+A(1,:)*[ax1;ax2]; | |
240 % v(2,:) = a(2)+A(2,:)*[ax1;ax2]; | |
241 % end | |
242 | |
243 % function v = gp_fun(t) | |
244 % v = A*gp(t); | |
245 % end | |
246 | |
248
81e0ead29431
Fixed package names in +parametrization
Jonatan Werpers <jonatan@werpers.com>
parents:
151
diff
changeset
|
247 % D = parametrization.Curve(@g_fun,@gp_fun); |
0 | 248 |
249 | |
250 % g = A(g-a)+a = Ag - Aa + a; | |
251 b = - A*a + a; | |
252 D = C.transform(A,b); | |
253 end | |
254 end | |
255 | |
256 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
|
257 |
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 % 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
|
259 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
|
260 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
|
261 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
|
262 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
|
263 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
|
264 |
1094
ad3089f04e0b
Revert changes in Curve so that spline() is not a static method.
Martin Almquist <malmquist@stanford.edu>
parents:
1090
diff
changeset
|
265 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
|
266 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
|
267 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
|
268 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
|
269 |
0 | 270 function obj = line(p1, p2) |
271 | |
272 function v = g_fun(t) | |
273 v(1,:) = p1(1) + t.*(p2(1)-p1(1)); | |
274 v(2,:) = p1(2) + t.*(p2(2)-p1(2)); | |
275 end | |
276 | |
333
0fd4b259af9e
Fixed some bugs in parametrization.Curve.
Jonatan Werpers <jonatan@werpers.com>
parents:
248
diff
changeset
|
277 function v = g_fun_deriv(t) |
0fd4b259af9e
Fixed some bugs in parametrization.Curve.
Jonatan Werpers <jonatan@werpers.com>
parents:
248
diff
changeset
|
278 v(1,:) = t.*0 + (p2(1)-p1(1)); |
0fd4b259af9e
Fixed some bugs in parametrization.Curve.
Jonatan Werpers <jonatan@werpers.com>
parents:
248
diff
changeset
|
279 v(2,:) = t.*0 + (p2(2)-p1(2)); |
0fd4b259af9e
Fixed some bugs in parametrization.Curve.
Jonatan Werpers <jonatan@werpers.com>
parents:
248
diff
changeset
|
280 end |
0fd4b259af9e
Fixed some bugs in parametrization.Curve.
Jonatan Werpers <jonatan@werpers.com>
parents:
248
diff
changeset
|
281 |
0fd4b259af9e
Fixed some bugs in parametrization.Curve.
Jonatan Werpers <jonatan@werpers.com>
parents:
248
diff
changeset
|
282 obj = parametrization.Curve(@g_fun, @g_fun_deriv); |
0 | 283 end |
284 | |
285 function obj = circle(c,r,phi) | |
286 default_arg('phi',[0; 2*pi]) | |
287 default_arg('c',[0; 0]) | |
288 default_arg('r',1) | |
289 | |
290 function v = g_fun(t) | |
291 w = phi(1)+t*(phi(2)-phi(1)); | |
292 v(1,:) = c(1) + r*cos(w); | |
293 v(2,:) = c(2) + r*sin(w); | |
294 end | |
295 | |
296 function v = g_fun_deriv(t) | |
297 w = phi(1)+t*(phi(2)-phi(1)); | |
298 v(1,:) = -(phi(2)-phi(1))*r*sin(w); | |
299 v(2,:) = (phi(2)-phi(1))*r*cos(w); | |
300 end | |
301 | |
248
81e0ead29431
Fixed package names in +parametrization
Jonatan Werpers <jonatan@werpers.com>
parents:
151
diff
changeset
|
302 obj = parametrization.Curve(@g_fun,@g_fun_deriv); |
0 | 303 end |
304 | |
305 function obj = bezier(p0, p1, p2, p3) | |
306 function v = g_fun(t) | |
307 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); | |
308 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); | |
309 end | |
310 | |
311 function v = g_fun_deriv(t) | |
312 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)); | |
313 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)); | |
314 end | |
315 | |
248
81e0ead29431
Fixed package names in +parametrization
Jonatan Werpers <jonatan@werpers.com>
parents:
151
diff
changeset
|
316 obj = parametrization.Curve(@g_fun,@g_fun_deriv); |
0 | 317 end |
318 | |
319 | |
320 function [g_out,gp_out] = transform_g(g,gp,tr) | |
321 A = tr.A; | |
322 b = tr.b; | |
323 flip = tr.flip; | |
324 | |
325 function v = g_fun_noflip(t) | |
326 % v = A*g + b | |
327 x = g(t); | |
328 | |
329 v(1,:) = A(1,:)*x+b(1); | |
330 v(2,:) = A(2,:)*x+b(2); | |
331 end | |
332 | |
333 function v = g_fun_flip(t) | |
334 % v = A*g + b | |
335 x = g(1-t); | |
336 | |
337 v(1,:) = A(1,:)*x+b(1); | |
338 v(2,:) = A(2,:)*x+b(2); | |
339 end | |
340 | |
341 | |
342 switch flip | |
343 case 1 | |
344 g_out = @g_fun_noflip; | |
345 gp_out = @(t)A*gp(t); | |
346 case -1 | |
347 g_out = @g_fun_flip; | |
348 gp_out = @(t)-A*gp(1-t); | |
349 end | |
350 end | |
351 | |
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
|
352 end |
0 | 353 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
|
354 |
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 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
|
356 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
|
357 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
|
358 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
|
359 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
|
360 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
|
361 |
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
|
362 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
|
363 % 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
|
364 % 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
|
365 |
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 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
|
367 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
|
368 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
|
369 '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
|
370 |
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
|
371 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
|
372 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
|
373 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
|
374 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
|
375 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
|
376 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
|
377 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
|
378 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
|
379 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
|
380 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
|
381 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
|
382 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
|
383 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
|
384 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
|
385 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
|
386 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
|
387 end |
1094
ad3089f04e0b
Revert changes in Curve so that spline() is not a static method.
Martin Almquist <malmquist@stanford.edu>
parents:
1090
diff
changeset
|
388 |
ad3089f04e0b
Revert changes in Curve so that spline() is not a static method.
Martin Almquist <malmquist@stanford.edu>
parents:
1090
diff
changeset
|
389 % 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
|
390 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
|
391 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
|
392 [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
|
393 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
|
394 |
ad3089f04e0b
Revert changes in Curve so that spline() is not a static method.
Martin Almquist <malmquist@stanford.edu>
parents:
1090
diff
changeset
|
395 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
|
396 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
|
397 end |