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