- don't allow n1 < 0

This commit is contained in:
2023-06-14 18:55:43 +02:00
parent 89e2cafd44
commit d8cdbc83c3
+43 -34
View File
@@ -22,59 +22,68 @@
## Author: Jens <jens@orion> ## Author: Jens <jens@orion>
## Created: 2023-06-10 ## Created: 2023-06-10
function [P1, Pa1, D] = vecfit (Pp, mask, Pt, P0, Pa0, step) function [P1, na_1, D] = vecfit (Pp, ignore_mask, Pt, P0, na_0, step)
[N_subst, N_elem] = size(Pp); [N_subst, N_elem] = size(Pp);
# Create default mask # Create default ignore_mask
if isempty(mask), if isempty(ignore_mask),
mask = ones(1,N_elem); ignore_mask = ones(1,N_elem);
end end
# A-Priory state with mask # A-Priory state with mask
P1 = P0 + sum(Pp.*Pa0.*mask); P1 = P0 + sum(Pp.*na_0.*ignore_mask);
# Calc distance to target # Calc distance to target
d = (Pt-P1); d = (Pt-P1);
D = norm(d); D = norm(d);
dn = d/D; dn = d/D;
# Find best prototype subst_list = 1:N_subst;
best_i = 0;
best_k = 10000;
best_dir = 1;
for i=1:N_subst, while 1,
p = Pp(i,:); # Find best prototype
pn = p/norm(p); best_i = 0;
best_k = 10000;
best_dir = 1;
for i=subst_list,
p = Pp(i,:);
pn = p/norm(p);
# compare d and p # compare d and p
for vecdir=[1,-1], for vecdir=[1,-1],
k = norm(vecdir*pn - dn); k = norm(vecdir*pn - dn);
if k < best_k, if k < best_k,
best_k = k; best_k = k;
best_i = i; best_i = i;
best_dir = vecdir; best_dir = vecdir;
end
end end
end end
end
# Calc next Pa # Calc next Pa
Pa1 = Pa0; na_1 = na_0;
# Calc P1 # Calc P1
P1 = P0 + sum(Pp.*Pa0); P1 = P0 + sum(Pp.*na_0);
if best_i, if best_i,
# Get value from best prototype # Get value from best prototype
delta = best_dir*step; delta = best_dir*step;
dps = delta*Pp(best_i,:); dps = delta*Pp(best_i,:);
# Update point # Check for negative n
P1 += dps; accu = na_1(best_i) + delta;
if accu < 0,
# Update Point accu subst_list(best_i) = [];
Pa1(best_i) += delta; continue;
end end
# Update point
P1 += dps;
# Update Point accu
na_1(best_i) = accu;
break;
end
end
endfunction endfunction