Mercurial > repos > public > sbplib
annotate +grid/Curve.m @ 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.
author | Martin Almquist <martin.almquist@it.uu.se> |
---|---|
date | Sun, 29 Nov 2015 14:28:53 +0100 |
parents | 48b6fb693025 |
children | 0a29a60e0b21 |
rev | line source |
---|---|
0 | 1 classdef Curve |
2 properties | |
3 g | |
4 gp | |
5 transformation | |
6 end | |
7 methods | |
8 %TODO: | |
9 % 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
|
10 % -semi-done |
0 | 11 |
12 % Concatenation of curves | |
13 % Subsections of curves | |
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
|
14 % Stretching of curve paramter - semi-done |
0 | 15 % Curve to cell array of linesegments |
16 | |
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
|
17 % Should supply either derivative or a difference operator 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
|
18 function obj = Curve(g,gp,transformation,D1) |
0 | 19 default_arg('gp',[]); |
20 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
|
21 default_arg('D1',[]); |
0 | 22 p_test = g(0); |
23 assert(all(size(p_test) == [2,1]), 'A curve parametrization must return a 2x1 vector.'); | |
24 | |
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
|
25 if(isempty(gp) && isempty(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
|
26 % Should be error instead of disp. |
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 disp(['You really should supply either the exact derivative ',... |
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 'or a suitable difference operator to compute it.']); |
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 elseif(isempty(gp)) |
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
|
30 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
|
31 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
|
32 |
0 | 33 if ~isempty(transformation) |
34 transformation.base_g = g; | |
35 transformation.base_gp = gp; | |
36 [g,gp] = grid.Curve.transform_g(g,gp,transformation); | |
37 end | |
38 | |
39 obj.g = g; | |
40 obj.gp = gp; | |
41 obj.transformation = transformation; | |
42 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
|
43 |
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
|
44 % Length of arc from parameter t0 to t1 (which may be vectors). |
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
|
45 % Computed using derivative. |
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
|
46 function L = arc_length(obj,t0,t1) |
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
|
47 deriv = obj.gp; |
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
|
48 speed = @(t) sp(deriv(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
|
49 |
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
|
50 function s = sp(deriv) |
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
|
51 s = sqrt(sum(deriv.^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
|
52 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
|
53 L = util.integral_vec(speed,t0,t1); |
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
|
54 end |
0 | 55 |
56 % Made up length calculation!! Science this before actual use!! | |
57 % Calculates the length of the curve. Makes sure the longet segment used is shorter than h_max. | |
58 function [L,t] = curve_length(C,h_max) | |
59 default_arg('h_max',0.001); | |
60 g = C.g; | |
61 h = 0.1; | |
62 m = 1/h+1; | |
63 t = linspace(0,1,m); | |
64 | |
65 [p,d] = get_d(t,g); | |
66 | |
67 while any(d>h_max) | |
68 I = find(d>h_max); | |
69 | |
70 % plot(p(1,:),p(2,:),'.') | |
71 % waitforbuttonpress | |
72 | |
73 new_t = []; | |
74 for i = I | |
75 new_t(end +1) = (t(i)+t(i+1))/2; | |
76 end | |
77 t = [t new_t]; | |
78 t = sort(t); | |
79 | |
80 [p,d] = get_d(t,g); | |
81 end | |
82 | |
83 L = sum(d); | |
84 | |
85 function [p,d] = get_d(t,g) | |
86 n = length(t); | |
87 | |
88 p = zeros(2,n); | |
89 for i = 1:n | |
90 p(:,i) = g(t(i)); | |
91 end | |
92 | |
93 d = zeros(1,n-1); | |
94 for i = 1:n-1 | |
95 d(i) = norm(p(:,i) - p(:,i+1)); | |
96 end | |
97 end | |
98 end | |
99 | |
100 function n = normal(obj,t) | |
101 deriv = obj.gp(t); | |
102 normalization = sqrt(sum(deriv.^2,1)); | |
103 n = [-deriv(2,:)./normalization; deriv(1,:)./normalization]; | |
104 end | |
105 | |
106 | |
107 % Plots a curve g(t) for 0<t<1, using n points. Retruns a handle h to the plotted curve. | |
108 % h = plot_curve(g,n) | |
109 function h = plot(obj,n) | |
110 default_arg('n',100); | |
111 | |
112 t = linspace(0,1,n); | |
113 | |
114 p = obj.g(t); | |
115 | |
116 h = line(p(1,:),p(2,:)); | |
117 end | |
118 | |
119 function h= plot_normals(obj,l,n,m) | |
120 default_arg('l',0.1); | |
121 default_arg('n',10); | |
122 default_arg('m',100); | |
123 t_n = linspace(0,1,n); | |
124 | |
125 normals = obj.normal(t_n)*l; | |
126 | |
127 n0 = obj.g(t_n); | |
128 n1 = n0 + normals; | |
129 | |
130 h = line([n0(1,:); n1(1,:)],[n0(2,:); n1(2,:)]); | |
131 set(h,'Color',Color.red); | |
132 obj.plot(m); | |
133 end | |
134 | |
135 function h= show(obj,name) | |
136 p = obj.g(1/2); | |
137 n = obj.normal(1/2); | |
138 p = p + n*0.1; | |
139 | |
140 % Add arrow | |
141 | |
142 h = text(p(1),p(2),name); | |
143 h.HorizontalAlignment = 'center'; | |
144 h.VerticalAlignment = 'middle'; | |
145 | |
146 obj.plot(); | |
147 end | |
148 % Shows curve with name and arrow for direction. | |
149 | |
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
|
150 |
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
|
151 % Should this be in the transformation method? |
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
|
152 function curve = stretch_parameter(obj,type) |
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
|
153 default_arg('type','arc_length'); |
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
|
154 switch type |
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
|
155 % Arc length parameterization. WARNING: slow. |
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
|
156 case 'arc_length' |
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
|
157 arcLength = @(t) obj.arc_length(0,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
|
158 arcPar = @(t) util.fzero_vec(@(s)arcLength(s) - t*arcLength(1),0); |
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
|
159 g_new = @(t)obj.g(arcPar(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
|
160 gp_old = obj.gp; |
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
|
161 gp_new = @(t) normalize(gp_old(arcPar(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
|
162 |
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
|
163 curve = grid.Curve(g_new,gp_new); |
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
|
164 |
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
|
165 otherwise |
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
|
166 error('That stretching is not implemented.'); |
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
|
167 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
|
168 |
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
|
169 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
|
170 |
0 | 171 % how to make it work for methods without returns |
172 function p = subsref(obj,S) | |
173 %Should i add error checking here? | |
174 %Maybe if you want performance you fetch obj.g and then use that | |
175 switch S(1).type | |
176 case '()' | |
177 p = obj.g(S.subs{1}); | |
178 % case '.' | |
179 | |
180 % p = obj.(S.subs); | |
181 otherwise | |
182 p = builtin('subsref',obj,S); | |
183 % error() | |
184 end | |
185 end | |
186 | |
187 | |
188 | |
189 | |
190 %% TRANSFORMATION OF A CURVE | |
191 function D = reverse(C) | |
192 % g = C.g; | |
193 % gp = C.gp; | |
194 % D = grid.Curve(@(t)g(1-t),@(t)-gp(1-t)); | |
195 D = C.transform([],[],-1); | |
196 end | |
197 | |
198 function D = transform(C,A,b,flip) | |
199 default_arg('A',[1 0; 0 1]); | |
200 default_arg('b',[0; 0]); | |
201 default_arg('flip',1); | |
202 if isempty(C.transformation) | |
203 g = C.g; | |
204 gp = C.gp; | |
205 transformation.A = A; | |
206 transformation.b = b; | |
207 transformation.flip = flip; | |
208 else | |
209 g = C.transformation.base_g; | |
210 gp = C.transformation.base_gp; | |
211 A_old = C.transformation.A; | |
212 b_old = C.transformation.b; | |
213 flip_old = C.transformation.flip; | |
214 | |
215 transformation.A = A*A_old; | |
216 transformation.b = A*b_old + b; | |
217 transformation.flip = flip*flip_old; | |
218 end | |
219 | |
220 D = grid.Curve(g,gp,transformation); | |
221 | |
222 end | |
223 | |
224 function D = translate(C,a) | |
225 g = C.g; | |
226 gp = C.gp; | |
227 | |
228 % function v = g_fun(t) | |
229 % x = g(t); | |
230 % v(1,:) = x(1,:)+a(1); | |
231 % v(2,:) = x(2,:)+a(2); | |
232 % end | |
233 | |
234 % D = grid.Curve(@g_fun,gp); | |
235 | |
236 D = C.transform([],a); | |
237 end | |
238 | |
239 function D = mirror(C, a, b) | |
240 assert_size(a,[2,1]); | |
241 assert_size(b,[2,1]); | |
242 | |
243 g = C.g; | |
244 gp = C.gp; | |
245 | |
246 l = b-a; | |
247 lx = l(1); | |
248 ly = l(2); | |
249 | |
250 | |
251 % fprintf('Singular?\n') | |
252 | |
253 A = [lx^2-ly^2 2*lx*ly; 2*lx*ly ly^2-lx^2]/(l'*l); | |
254 | |
255 % function v = g_fun(t) | |
256 % % v = a + A*(g(t)-a) | |
257 % x = g(t); | |
258 | |
259 % ax1 = x(1,:)-a(1); | |
260 % ax2 = x(2,:)-a(2); | |
261 % v(1,:) = a(1)+A(1,:)*[ax1;ax2]; | |
262 % v(2,:) = a(2)+A(2,:)*[ax1;ax2]; | |
263 % end | |
264 | |
265 % function v = gp_fun(t) | |
266 % v = A*gp(t); | |
267 % end | |
268 | |
269 % D = grid.Curve(@g_fun,@gp_fun); | |
270 | |
271 % g = A(g-a)+a = Ag - Aa + a; | |
272 b = - A*a + a; | |
273 D = C.transform(A,b); | |
274 | |
275 end | |
276 | |
277 function D = rotate(C,a,rad) | |
278 assert_size(a, [2,1]); | |
279 assert_size(rad, [1,1]); | |
280 g = C.g; | |
281 gp = C.gp; | |
282 | |
283 | |
284 A = [cos(rad) -sin(rad); sin(rad) cos(rad)]; | |
285 | |
286 % function v = g_fun(t) | |
287 % % v = a + A*(g(t)-a) | |
288 % x = g(t); | |
289 | |
290 % ax1 = x(1,:)-a(1); | |
291 % ax2 = x(2,:)-a(2); | |
292 % v(1,:) = a(1)+A(1,:)*[ax1;ax2]; | |
293 % v(2,:) = a(2)+A(2,:)*[ax1;ax2]; | |
294 % end | |
295 | |
296 % function v = gp_fun(t) | |
297 % v = A*gp(t); | |
298 % end | |
299 | |
300 % D = grid.Curve(@g_fun,@gp_fun); | |
301 | |
302 | |
303 % g = A(g-a)+a = Ag - Aa + a; | |
304 b = - A*a + a; | |
305 D = C.transform(A,b); | |
306 end | |
307 end | |
308 | |
309 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
|
310 |
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
|
311 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
|
312 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
|
313 dt = L/(m-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
|
314 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
|
315 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
|
316 gp = (D1*g)'; |
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
|
317 |
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
|
318 % Make vectors longer to make sure splines are |
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
|
319 % reasonable at 1+eps. |
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
|
320 t_l = linspace(0,L+dt,m+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
|
321 gp_l = [gp,gp(:,end)+gp(:,end)-gp(:,end-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
|
322 |
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
|
323 % 4:th order splines. |
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
|
324 spline_order = 4; |
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
|
325 gp1 = spapi( optknt(t_l,spline_order), t_l, gp_l(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
|
326 gp2 = spapi( optknt(t_l,spline_order), t_l, gp_l(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
|
327 gp1_fun = @(t) fnval(gp1,t); gp2_fun = @(t) fnval(gp2,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
|
328 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
|
329 |
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
|
330 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
|
331 |
0 | 332 function obj = line(p1, p2) |
333 | |
334 function v = g_fun(t) | |
335 v(1,:) = p1(1) + t.*(p2(1)-p1(1)); | |
336 v(2,:) = p1(2) + t.*(p2(2)-p1(2)); | |
337 end | |
338 g = @g_fun; | |
339 | |
340 obj = grid.Curve(g); | |
341 end | |
342 | |
343 function obj = circle(c,r,phi) | |
344 default_arg('phi',[0; 2*pi]) | |
345 default_arg('c',[0; 0]) | |
346 default_arg('r',1) | |
347 | |
348 function v = g_fun(t) | |
349 w = phi(1)+t*(phi(2)-phi(1)); | |
350 v(1,:) = c(1) + r*cos(w); | |
351 v(2,:) = c(2) + r*sin(w); | |
352 end | |
353 | |
354 function v = g_fun_deriv(t) | |
355 w = phi(1)+t*(phi(2)-phi(1)); | |
356 v(1,:) = -(phi(2)-phi(1))*r*sin(w); | |
357 v(2,:) = (phi(2)-phi(1))*r*cos(w); | |
358 end | |
359 | |
360 obj = grid.Curve(@g_fun,@g_fun_deriv); | |
361 end | |
362 | |
363 function obj = bezier(p0, p1, p2, p3) | |
364 function v = g_fun(t) | |
365 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); | |
366 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); | |
367 end | |
368 | |
369 function v = g_fun_deriv(t) | |
370 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)); | |
371 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)); | |
372 end | |
373 | |
374 obj = grid.Curve(@g_fun,@g_fun_deriv); | |
375 end | |
376 | |
377 | |
378 function [g_out,gp_out] = transform_g(g,gp,tr) | |
379 A = tr.A; | |
380 b = tr.b; | |
381 flip = tr.flip; | |
382 | |
383 function v = g_fun_noflip(t) | |
384 % v = A*g + b | |
385 x = g(t); | |
386 | |
387 v(1,:) = A(1,:)*x+b(1); | |
388 v(2,:) = A(2,:)*x+b(2); | |
389 end | |
390 | |
391 function v = g_fun_flip(t) | |
392 % v = A*g + b | |
393 x = g(1-t); | |
394 | |
395 v(1,:) = A(1,:)*x+b(1); | |
396 v(2,:) = A(2,:)*x+b(2); | |
397 end | |
398 | |
399 | |
400 switch flip | |
401 case 1 | |
402 g_out = @g_fun_noflip; | |
403 gp_out = @(t)A*gp(t); | |
404 case -1 | |
405 g_out = @g_fun_flip; | |
406 gp_out = @(t)-A*gp(1-t); | |
407 end | |
408 end | |
409 | |
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
|
410 end |
0 | 411 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
|
412 |
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
|
413 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
|
414 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
|
415 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
|
416 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
|
417 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
|
418 |
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
|
419 |
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
|
420 |