Mercurial > repos > public > sbplib
annotate +grid/Curve.m @ 105:1df4f3704b76 feature/arclen-param
Added error checking for all methods that use the derivative gp. Removed that from the to do list. Updated some comments.
author | Martin Almquist <martin.almquist@it.uu.se> |
---|---|
date | Mon, 07 Dec 2015 18:51:41 +0100 |
parents | ffd9e68f63cc |
children | 06c3034966b7 |
rev | line source |
---|---|
0 | 1 classdef Curve |
2 properties | |
3 g | |
4 gp | |
5 transformation | |
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
|
6 |
0 | 7 end |
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 | |
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
|
15 % The curve parameter t must be in [0,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
|
16 % Can supply 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
|
17 function obj = Curve(g,gp,transformation) |
0 | 18 default_arg('gp',[]); |
19 default_arg('transformation',[]); | |
20 p_test = g(0); | |
21 assert(all(size(p_test) == [2,1]), 'A curve parametrization must return a 2x1 vector.'); | |
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
|
22 |
0 | 23 if ~isempty(transformation) |
24 transformation.base_g = g; | |
25 transformation.base_gp = gp; | |
26 [g,gp] = grid.Curve.transform_g(g,gp,transformation); | |
27 end | |
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
|
28 |
0 | 29 obj.g = g; |
30 obj.gp = gp; | |
31 obj.transformation = transformation; | |
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
|
32 |
0 | 33 end |
34 | |
35 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
|
36 assert(~isempty(obj.gp),'Curve has no derivative!'); |
0 | 37 deriv = obj.gp(t); |
38 normalization = sqrt(sum(deriv.^2,1)); | |
39 n = [-deriv(2,:)./normalization; deriv(1,:)./normalization]; | |
40 end | |
41 | |
42 | |
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
|
43 % Plots a curve g(t) for 0<t<1, using n points. Returns a handle h to the plotted curve. |
0 | 44 % 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
|
45 function h = plot(obj,n) |
0 | 46 default_arg('n',100); |
47 | |
48 t = linspace(0,1,n); | |
49 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
|
50 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
|
51 end |
0 | 52 |
53 function h= plot_normals(obj,l,n,m) | |
54 default_arg('l',0.1); | |
55 default_arg('n',10); | |
56 default_arg('m',100); | |
57 t_n = linspace(0,1,n); | |
58 | |
59 normals = obj.normal(t_n)*l; | |
60 | |
61 n0 = obj.g(t_n); | |
62 n1 = n0 + normals; | |
63 | |
64 h = line([n0(1,:); n1(1,:)],[n0(2,:); n1(2,:)]); | |
65 set(h,'Color',Color.red); | |
66 obj.plot(m); | |
67 end | |
68 | |
69 function h= show(obj,name) | |
70 p = obj.g(1/2); | |
71 n = obj.normal(1/2); | |
72 p = p + n*0.1; | |
73 | |
74 % Add arrow | |
75 | |
76 h = text(p(1),p(2),name); | |
77 h.HorizontalAlignment = 'center'; | |
78 h.VerticalAlignment = 'middle'; | |
79 | |
80 obj.plot(); | |
81 end | |
82 % Shows curve with name and arrow for direction. | |
83 | |
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
|
84 % 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
|
85 % 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
|
86 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
|
87 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
|
88 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
|
89 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
|
90 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
|
91 |
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
|
92 % Arc length parameterization. |
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
|
93 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
|
94 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
|
95 assert(~isempty(obj.gp),'Curve has no derivative!'); |
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
|
96 |
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
|
97 % 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
|
98 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
|
99 arcVec = obj.arcLength(0,tvec); |
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
|
100 tFunc = spline(arcVec,tvec); % t as a function of arcLength |
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
|
101 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
|
102 arcPar = @(s) tFunc(s*L); |
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 |
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 % 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
|
105 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
|
106 gp_new = @(t) normalize(obj.gp(arcPar(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
|
107 curve = grid.Curve(g_new,gp_new); |
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
|
108 |
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
|
109 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
|
110 |
0 | 111 % how to make it work for methods without returns |
112 function p = subsref(obj,S) | |
113 %Should i add error checking here? | |
114 %Maybe if you want performance you fetch obj.g and then use that | |
115 switch S(1).type | |
116 case '()' | |
117 p = obj.g(S.subs{1}); | |
118 % case '.' | |
119 | |
120 % p = obj.(S.subs); | |
121 otherwise | |
122 p = builtin('subsref',obj,S); | |
123 % error() | |
124 end | |
125 end | |
126 | |
127 | |
128 %% TRANSFORMATION OF A CURVE | |
129 function D = reverse(C) | |
130 % g = C.g; | |
131 % gp = C.gp; | |
132 % D = grid.Curve(@(t)g(1-t),@(t)-gp(1-t)); | |
133 D = C.transform([],[],-1); | |
134 end | |
135 | |
136 function D = transform(C,A,b,flip) | |
137 default_arg('A',[1 0; 0 1]); | |
138 default_arg('b',[0; 0]); | |
139 default_arg('flip',1); | |
140 if isempty(C.transformation) | |
141 g = C.g; | |
142 gp = C.gp; | |
143 transformation.A = A; | |
144 transformation.b = b; | |
145 transformation.flip = flip; | |
146 else | |
147 g = C.transformation.base_g; | |
148 gp = C.transformation.base_gp; | |
149 A_old = C.transformation.A; | |
150 b_old = C.transformation.b; | |
151 flip_old = C.transformation.flip; | |
152 | |
153 transformation.A = A*A_old; | |
154 transformation.b = A*b_old + b; | |
155 transformation.flip = flip*flip_old; | |
156 end | |
157 | |
158 D = grid.Curve(g,gp,transformation); | |
159 | |
160 end | |
161 | |
162 function D = translate(C,a) | |
163 g = C.g; | |
164 gp = C.gp; | |
165 | |
166 % function v = g_fun(t) | |
167 % x = g(t); | |
168 % v(1,:) = x(1,:)+a(1); | |
169 % v(2,:) = x(2,:)+a(2); | |
170 % end | |
171 | |
172 % D = grid.Curve(@g_fun,gp); | |
173 | |
174 D = C.transform([],a); | |
175 end | |
176 | |
177 function D = mirror(C, a, b) | |
178 assert_size(a,[2,1]); | |
179 assert_size(b,[2,1]); | |
180 | |
181 g = C.g; | |
182 gp = C.gp; | |
183 | |
184 l = b-a; | |
185 lx = l(1); | |
186 ly = l(2); | |
187 | |
188 | |
189 % fprintf('Singular?\n') | |
190 | |
191 A = [lx^2-ly^2 2*lx*ly; 2*lx*ly ly^2-lx^2]/(l'*l); | |
192 | |
193 % function v = g_fun(t) | |
194 % % v = a + A*(g(t)-a) | |
195 % x = g(t); | |
196 | |
197 % ax1 = x(1,:)-a(1); | |
198 % ax2 = x(2,:)-a(2); | |
199 % v(1,:) = a(1)+A(1,:)*[ax1;ax2]; | |
200 % v(2,:) = a(2)+A(2,:)*[ax1;ax2]; | |
201 % end | |
202 | |
203 % function v = gp_fun(t) | |
204 % v = A*gp(t); | |
205 % end | |
206 | |
207 % D = grid.Curve(@g_fun,@gp_fun); | |
208 | |
209 % g = A(g-a)+a = Ag - Aa + a; | |
210 b = - A*a + a; | |
211 D = C.transform(A,b); | |
212 | |
213 end | |
214 | |
215 function D = rotate(C,a,rad) | |
216 assert_size(a, [2,1]); | |
217 assert_size(rad, [1,1]); | |
218 g = C.g; | |
219 gp = C.gp; | |
220 | |
221 | |
222 A = [cos(rad) -sin(rad); sin(rad) cos(rad)]; | |
223 | |
224 % function v = g_fun(t) | |
225 % % v = a + A*(g(t)-a) | |
226 % x = g(t); | |
227 | |
228 % ax1 = x(1,:)-a(1); | |
229 % ax2 = x(2,:)-a(2); | |
230 % v(1,:) = a(1)+A(1,:)*[ax1;ax2]; | |
231 % v(2,:) = a(2)+A(2,:)*[ax1;ax2]; | |
232 % end | |
233 | |
234 % function v = gp_fun(t) | |
235 % v = A*gp(t); | |
236 % end | |
237 | |
238 % D = grid.Curve(@g_fun,@gp_fun); | |
239 | |
240 | |
241 % g = A(g-a)+a = Ag - Aa + a; | |
242 b = - A*a + a; | |
243 D = C.transform(A,b); | |
244 end | |
245 end | |
246 | |
247 methods (Static) | |
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
|
248 |
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
|
249 |
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
|
250 function gp_out = numericalDerivative(g,D1) |
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
|
251 m = length(D1); L = 1; % Assume curve parameter from 0 to 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
|
252 t = linspace(0,L,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
|
253 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
|
254 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
|
255 |
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
|
256 gp1_fun = spline(t,gpVec(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
|
257 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
|
258 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
|
259 |
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
|
260 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
|
261 |
0 | 262 function obj = line(p1, p2) |
263 | |
264 function v = g_fun(t) | |
265 v(1,:) = p1(1) + t.*(p2(1)-p1(1)); | |
266 v(2,:) = p1(2) + t.*(p2(2)-p1(2)); | |
267 end | |
268 g = @g_fun; | |
269 | |
270 obj = grid.Curve(g); | |
271 end | |
272 | |
273 function obj = circle(c,r,phi) | |
274 default_arg('phi',[0; 2*pi]) | |
275 default_arg('c',[0; 0]) | |
276 default_arg('r',1) | |
277 | |
278 function v = g_fun(t) | |
279 w = phi(1)+t*(phi(2)-phi(1)); | |
280 v(1,:) = c(1) + r*cos(w); | |
281 v(2,:) = c(2) + r*sin(w); | |
282 end | |
283 | |
284 function v = g_fun_deriv(t) | |
285 w = phi(1)+t*(phi(2)-phi(1)); | |
286 v(1,:) = -(phi(2)-phi(1))*r*sin(w); | |
287 v(2,:) = (phi(2)-phi(1))*r*cos(w); | |
288 end | |
289 | |
290 obj = grid.Curve(@g_fun,@g_fun_deriv); | |
291 end | |
292 | |
293 function obj = bezier(p0, p1, p2, p3) | |
294 function v = g_fun(t) | |
295 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); | |
296 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); | |
297 end | |
298 | |
299 function v = g_fun_deriv(t) | |
300 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)); | |
301 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)); | |
302 end | |
303 | |
304 obj = grid.Curve(@g_fun,@g_fun_deriv); | |
305 end | |
306 | |
307 | |
308 function [g_out,gp_out] = transform_g(g,gp,tr) | |
309 A = tr.A; | |
310 b = tr.b; | |
311 flip = tr.flip; | |
312 | |
313 function v = g_fun_noflip(t) | |
314 % v = A*g + b | |
315 x = g(t); | |
316 | |
317 v(1,:) = A(1,:)*x+b(1); | |
318 v(2,:) = A(2,:)*x+b(2); | |
319 end | |
320 | |
321 function v = g_fun_flip(t) | |
322 % v = A*g + b | |
323 x = g(1-t); | |
324 | |
325 v(1,:) = A(1,:)*x+b(1); | |
326 v(2,:) = A(2,:)*x+b(2); | |
327 end | |
328 | |
329 | |
330 switch flip | |
331 case 1 | |
332 g_out = @g_fun_noflip; | |
333 gp_out = @(t)A*gp(t); | |
334 case -1 | |
335 g_out = @g_fun_flip; | |
336 gp_out = @(t)-A*gp(1-t); | |
337 end | |
338 end | |
339 | |
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
|
340 end |
0 | 341 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
|
342 |
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
|
343 function g_norm = normalize(g0) |
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
|
344 g1 = g0(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
|
345 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
|
346 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
|
347 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
|
348 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
|
349 |
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
|
350 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
|
351 % 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
|
352 % 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
|
353 |
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 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
|
355 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
|
356 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
|
357 'a and b must have same length, unless one is a scalar.'); |
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
|
358 |
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 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
|
360 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
|
361 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
|
362 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
|
363 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
|
364 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
|
365 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
|
366 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
|
367 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
|
368 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
|
369 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
|
370 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
|
371 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
|
372 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
|
373 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
|
374 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
|
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 |
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 % Returns a function handle to the spline. |
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 function f = spline(tval,fval,spline_order) |
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 default_arg('spline_order',4); |
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 [m,~] = size(tval); |
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 assert(m==1,'Need row 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
|
382 |
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 f_spline = spapi( optknt(tval,spline_order), tval, fval ); |
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 f = @(t) fnval(f_spline,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
|
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 |
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
|
387 |
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
|
388 |