mirror of
https://github.com/c-sooyoung/fold_slice.git
synced 2026-09-18 00:59:11 +09:00
initial commit
This commit is contained in:
@@ -0,0 +1,202 @@
|
||||
% IMAGESC_HSV for plotting complex valued arrays , similar to imagesc3D but with more options
|
||||
% imagesc_hsv(varargin)
|
||||
%
|
||||
% ** varargin see the code
|
||||
|
||||
%
|
||||
% Academic License Agreement
|
||||
%
|
||||
% Source Code
|
||||
%
|
||||
% Introduction
|
||||
% • This license agreement sets forth the terms and conditions under which the PAUL SCHERRER INSTITUT (PSI), CH-5232 Villigen-PSI, Switzerland (hereafter "LICENSOR")
|
||||
% will grant you (hereafter "LICENSEE") a royalty-free, non-exclusive license for academic, non-commercial purposes only (hereafter "LICENSE") to use the cSAXS
|
||||
% ptychography MATLAB package computer software program and associated documentation furnished hereunder (hereafter "PROGRAM").
|
||||
%
|
||||
% Terms and Conditions of the LICENSE
|
||||
% 1. LICENSOR grants to LICENSEE a royalty-free, non-exclusive license to use the PROGRAM for academic, non-commercial purposes, upon the terms and conditions
|
||||
% hereinafter set out and until termination of this license as set forth below.
|
||||
% 2. LICENSEE acknowledges that the PROGRAM is a research tool still in the development stage. The PROGRAM is provided without any related services, improvements
|
||||
% or warranties from LICENSOR and that the LICENSE is entered into in order to enable others to utilize the PROGRAM in their academic activities. It is the
|
||||
% LICENSEE’s responsibility to ensure its proper use and the correctness of the results.”
|
||||
% 3. THE PROGRAM IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR
|
||||
% A PARTICULAR PURPOSE AND NONINFRINGEMENT OF ANY PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS. IN NO EVENT SHALL THE LICENSOR, THE AUTHORS OR THE COPYRIGHT
|
||||
% HOLDERS BE LIABLE FOR ANY CLAIM, DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES OR OTHER LIABILITY ARISING FROM, OUT OF OR IN CONNECTION WITH THE PROGRAM OR THE USE
|
||||
% OF THE PROGRAM OR OTHER DEALINGS IN THE PROGRAM.
|
||||
% 4. LICENSEE agrees that it will use the PROGRAM and any modifications, improvements, or derivatives of PROGRAM that LICENSEE may create (collectively,
|
||||
% "IMPROVEMENTS") solely for academic, non-commercial purposes and that any copy of PROGRAM or derivatives thereof shall be distributed only under the same
|
||||
% license as PROGRAM. The terms "academic, non-commercial", as used in this Agreement, mean academic or other scholarly research which (a) is not undertaken for
|
||||
% profit, or (b) is not intended to produce works, services, or data for commercial use, or (c) is neither conducted, nor funded, by a person or an entity engaged
|
||||
% in the commercial use, application or exploitation of works similar to the PROGRAM.
|
||||
% 5. LICENSEE agrees that it shall make the following acknowledgement in any publication resulting from the use of the PROGRAM or any translation of the code into
|
||||
% another computing language:
|
||||
% "Data processing was carried out using the cSAXS ptychography MATLAB package developed by the Science IT and the coherent X-ray scattering (CXS) groups, Paul
|
||||
% Scherrer Institut, Switzerland."
|
||||
%
|
||||
% Additionally, any publication using the package, or any translation of the code into another computing language should cite for difference map:
|
||||
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379–382 (2008).
|
||||
% (doi: 10.1126/science.1158573),
|
||||
% for mixed coherent modes:
|
||||
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 68–71 (2013). (doi: 10.1038/nature11806),
|
||||
% for LSQ-ML method
|
||||
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
|
||||
% for OPRP method
|
||||
% M. Odstrcil, P. Baksh, S. A. Boden, R. Card, J. E. Chad, J. G. Frey, W. S. Brocklesby, "Ptychographic coherent diffractive imaging with orthogonal probe relaxation." Optics express 24.8 (2016): 8360-8369
|
||||
% and/or for multislice:
|
||||
% E. H. R. Tsai, I. Usov, A. Diaz, A. Menzel, and M. Guizar-Sicairos, X-ray ptychography with extended depth of field, Opt. Express 24, 29089–29108 (2016).
|
||||
% 6. Except for the above-mentioned acknowledgment, LICENSEE shall not use the PROGRAM title or the names or logos of LICENSOR, nor any adaptation thereof, nor the
|
||||
% names of any of its employees or laboratories, in any advertising, promotional or sales material without prior written consent obtained from LICENSOR in each case.
|
||||
% 7. Ownership of all rights, including copyright in the PROGRAM and in any material associated therewith, shall at all times remain with LICENSOR, and LICENSEE
|
||||
% agrees to preserve same. LICENSEE agrees not to use any portion of the PROGRAM or of any IMPROVEMENTS in any machine-readable form outside the PROGRAM, nor to
|
||||
% make any copies except for its internal use, without prior written consent of LICENSOR. LICENSEE agrees to place the following copyright notice on any such copies:
|
||||
% © All rights reserved. PAUL SCHERRER INSTITUT, Switzerland, Laboratory for Macromolecules and Bioimaging, 2017.
|
||||
% 8. The LICENSE shall not be construed to confer any rights upon LICENSEE by implication or otherwise except as specifically set forth herein.
|
||||
% 9. DISCLAIMER: LICENSEE shall be aware that Phase Focus Limited of Sheffield, UK has an international portfolio of patents and pending applications which relate
|
||||
% to ptychography and that the PROGRAM may be capable of being used in circumstances which may fall within the claims of one or more of the Phase Focus patents,
|
||||
% in particular of patent with international application number PCT/GB2005/001464. The LICENSOR explicitly declares not to indemnify the users of the software
|
||||
% in case Phase Focus or any other third party will open a legal action against the LICENSEE due to the use of the program.
|
||||
% 10. This Agreement shall be governed by the material laws of Switzerland and any dispute arising out of this Agreement or use of the PROGRAM shall be brought before
|
||||
% the courts of Zürich, Switzerland.
|
||||
|
||||
|
||||
function imagesc_hsv(varargin)
|
||||
|
||||
import utils.*
|
||||
import math.*
|
||||
|
||||
par = inputParser;
|
||||
par.addOptional('data', [])
|
||||
par.addParameter('scale', nan , @isnumeric )
|
||||
par.addParameter('clim', [] , @isnumeric )
|
||||
par.addParameter('inverse', false , @islogical ) % use white background
|
||||
par.addParameter('show_ROI', false , @islogical ) % show only intersting area
|
||||
par.addParameter('points', [] , @isnumeric ) % plot dots
|
||||
par.addParameter('enhance_contrast', false , @islogical ) % plot dots
|
||||
par.addParameter('axis', [] , @isnumeric ) % plot dots
|
||||
par.addParameter('stabilize_phase', true , @islogical ) % plot dots
|
||||
par.addParameter('show', true , @islogical ) % plot dots
|
||||
|
||||
par.parse(varargin{:})
|
||||
r = par.Results;
|
||||
data = r.data;
|
||||
clim = r.clim;
|
||||
|
||||
if all(data(:) == 0)
|
||||
warning('Empty data to plot')
|
||||
return
|
||||
end
|
||||
|
||||
|
||||
|
||||
[W,H] = size(data);
|
||||
|
||||
if ~isempty(r.axis)
|
||||
X = linspace(r.axis(1),r.axis(2),W)*1e6;
|
||||
Y = linspace(r.axis(3),r.axis(4),H)*1e6;
|
||||
else
|
||||
if ~isnan(r.scale)
|
||||
scale = ones(2,1).*r.scale(:);
|
||||
X = [-W/2:W/2-1]* scale(1)*1e6;
|
||||
Y = [-H/2:H/2-1]* scale(2)*1e6;
|
||||
else
|
||||
X = 1:W; Y = 1:H;
|
||||
end
|
||||
end
|
||||
if r.show_ROI
|
||||
asum = abs(sum(data,3));
|
||||
try
|
||||
T1 = (graythresh_new((sum(asum,1))));
|
||||
T2 = (graythresh_new((sum(asum,2))));
|
||||
asum(:,sum(asum,1) < T1) = 0;
|
||||
asum(sum(asum,2) < T2,:) = 0;
|
||||
[ROI] = get_ROI(asum > 0.01*quantile(asum(:), 0.99), 0);
|
||||
data = data(ROI{:});
|
||||
X = X(ROI{1});
|
||||
Y = Y(ROI{2});
|
||||
catch
|
||||
warning('ROI estimation failed')
|
||||
end
|
||||
end
|
||||
[W,H] = size(data);
|
||||
|
||||
if ~isempty(clim)
|
||||
ind_min = abs(data) < clim(1);
|
||||
ind_max = abs(data) > clim(2);
|
||||
data(ind_min) = data(ind_min) ./ abs(data(ind_min)) * clim(1);
|
||||
data(ind_max) = data(ind_max) ./ abs(data(ind_max)) * clim(2);
|
||||
end
|
||||
|
||||
adata = abs(data);
|
||||
|
||||
|
||||
|
||||
alpha = 1e-3;
|
||||
tmp= sort(adata(:));
|
||||
MAX = tmp(ceil(end*(1-alpha)));
|
||||
ind = adata > MAX;
|
||||
data(ind) = MAX * data(ind) ./ abs(data(ind));
|
||||
if r.enhance_contrast
|
||||
data = data ./ sqrt(alpha+abs(data));
|
||||
clim = sqrt(clim);
|
||||
end
|
||||
if r.stabilize_phase
|
||||
data = stabilize_phase(data, abs(data), abs(data), 'remove_ramp', false);
|
||||
end
|
||||
|
||||
adata = abs(data);
|
||||
|
||||
if isempty(clim)
|
||||
range = sp_quantile(adata(:), [1e-2, 1-1e-2],10);
|
||||
else
|
||||
range = clim;
|
||||
end
|
||||
|
||||
adata = (adata - range(1) ) ./ ( range(2) - range(1) );
|
||||
ang_data = angle(data);
|
||||
|
||||
if r.enhance_contrast && r.stabilize_phase
|
||||
ang_range = max(abs(sp_quantile(ang_data(:), [1e-2, 1-1e-2],10)));
|
||||
ang_range = max(1e-3, ang_range);
|
||||
ang_data = 2*pi*ang_data ./ (2* ang_range);
|
||||
end
|
||||
|
||||
|
||||
if r.inverse
|
||||
hue = mod(ang_data+1.5*pi, 2*pi)/(2*pi);
|
||||
hsv_data = [ hue(:) , adata(:), ones(W*H,1) ];
|
||||
else
|
||||
hue = mod(ang_data+2.5*pi, 2*pi)/(2*pi);
|
||||
hsv_data = [ hue(:) , ones(W*H,1), adata(:) ];
|
||||
end
|
||||
hsv_data = min(max(0, hsv_data),1);
|
||||
|
||||
|
||||
rgb_data = hsv2rgb(hsv_data);
|
||||
|
||||
rgb_data = reshape(rgb_data, W,H,3);
|
||||
rgb_data = min(1,rgb_data);
|
||||
|
||||
|
||||
if r.show
|
||||
hh = imagesc(Y,X, rgb_data );
|
||||
axis image
|
||||
end
|
||||
|
||||
|
||||
if r.show
|
||||
% Get the parent Axes of the image
|
||||
axis image
|
||||
|
||||
if ~isempty(r.points) && ~any(isnan(r.scale))
|
||||
hold on
|
||||
points = r.scale.*1e6.*r.points;
|
||||
plot( points(:,1),points(:,2), '.w')
|
||||
hold off
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,369 @@
|
||||
function [x0,y0,iout,jout] = intersections(x1,y1,x2,y2,robust)
|
||||
%INTERSECTIONS Intersections of curves.
|
||||
% Computes the (x,y) locations where two curves intersect. The curves
|
||||
% can be broken with NaNs or have vertical segments.
|
||||
%
|
||||
% Example:
|
||||
% [X0,Y0] = intersections(X1,Y1,X2,Y2,ROBUST);
|
||||
%
|
||||
% where X1 and Y1 are equal-length vectors of at least two points and
|
||||
% represent curve 1. Similarly, X2 and Y2 represent curve 2.
|
||||
% X0 and Y0 are column vectors containing the points at which the two
|
||||
% curves intersect.
|
||||
%
|
||||
% ROBUST (optional) set to 1 or true means to use a slight variation of the
|
||||
% algorithm that might return duplicates of some intersection points, and
|
||||
% then remove those duplicates. The default is true, but since the
|
||||
% algorithm is slightly slower you can set it to false if you know that
|
||||
% your curves don't intersect at any segment boundaries. Also, the robust
|
||||
% version properly handles parallel and overlapping segments.
|
||||
%
|
||||
% The algorithm can return two additional vectors that indicate which
|
||||
% segment pairs contain intersections and where they are:
|
||||
%
|
||||
% [X0,Y0,I,J] = intersections(X1,Y1,X2,Y2,ROBUST);
|
||||
%
|
||||
% For each element of the vector I, I(k) = (segment number of (X1,Y1)) +
|
||||
% (how far along this segment the intersection is). For example, if I(k) =
|
||||
% 45.25 then the intersection lies a quarter of the way between the line
|
||||
% segment connecting (X1(45),Y1(45)) and (X1(46),Y1(46)). Similarly for
|
||||
% the vector J and the segments in (X2,Y2).
|
||||
%
|
||||
% You can also get intersections of a curve with itself. Simply pass in
|
||||
% only one curve, i.e.,
|
||||
%
|
||||
% [X0,Y0] = intersections(X1,Y1,ROBUST);
|
||||
%
|
||||
% where, as before, ROBUST is optional.
|
||||
|
||||
% Version: 2.0, 25 May 2017
|
||||
% Author: Douglas M. Schwarz
|
||||
% Email: dmschwarz=ieee*org, dmschwarz=urgrad*rochester*edu
|
||||
% Real_email = regexprep(Email,{'=','*'},{'@','.'})
|
||||
|
||||
|
||||
% Theory of operation:
|
||||
%
|
||||
% Given two line segments, L1 and L2,
|
||||
%
|
||||
% L1 endpoints: (x1(1),y1(1)) and (x1(2),y1(2))
|
||||
% L2 endpoints: (x2(1),y2(1)) and (x2(2),y2(2))
|
||||
%
|
||||
% we can write four equations with four unknowns and then solve them. The
|
||||
% four unknowns are t1, t2, x0 and y0, where (x0,y0) is the intersection of
|
||||
% L1 and L2, t1 is the distance from the starting point of L1 to the
|
||||
% intersection relative to the length of L1 and t2 is the distance from the
|
||||
% starting point of L2 to the intersection relative to the length of L2.
|
||||
%
|
||||
% So, the four equations are
|
||||
%
|
||||
% (x1(2) - x1(1))*t1 = x0 - x1(1)
|
||||
% (x2(2) - x2(1))*t2 = x0 - x2(1)
|
||||
% (y1(2) - y1(1))*t1 = y0 - y1(1)
|
||||
% (y2(2) - y2(1))*t2 = y0 - y2(1)
|
||||
%
|
||||
% Rearranging and writing in matrix form,
|
||||
%
|
||||
% [x1(2)-x1(1) 0 -1 0; [t1; [-x1(1);
|
||||
% 0 x2(2)-x2(1) -1 0; * t2; = -x2(1);
|
||||
% y1(2)-y1(1) 0 0 -1; x0; -y1(1);
|
||||
% 0 y2(2)-y2(1) 0 -1] y0] -y2(1)]
|
||||
%
|
||||
% Let's call that A*T = B. We can solve for T with T = A\B.
|
||||
%
|
||||
% Once we have our solution we just have to look at t1 and t2 to determine
|
||||
% whether L1 and L2 intersect. If 0 <= t1 < 1 and 0 <= t2 < 1 then the two
|
||||
% line segments cross and we can include (x0,y0) in the output.
|
||||
%
|
||||
% In principle, we have to perform this computation on every pair of line
|
||||
% segments in the input data. This can be quite a large number of pairs so
|
||||
% we will reduce it by doing a simple preliminary check to eliminate line
|
||||
% segment pairs that could not possibly cross. The check is to look at the
|
||||
% smallest enclosing rectangles (with sides parallel to the axes) for each
|
||||
% line segment pair and see if they overlap. If they do then we have to
|
||||
% compute t1 and t2 (via the A\B computation) to see if the line segments
|
||||
% cross, but if they don't then the line segments cannot cross. In a
|
||||
% typical application, this technique will eliminate most of the potential
|
||||
% line segment pairs.
|
||||
|
||||
%
|
||||
%
|
||||
% Copyright (c) 2017, Douglas M. Schwarz
|
||||
% All rights reserved.
|
||||
%
|
||||
% Redistribution and use in source and binary forms, with or without
|
||||
% modification, are permitted provided that the following conditions are
|
||||
% met:
|
||||
%
|
||||
% * Redistributions of source code must retain the above copyright
|
||||
% notice, this list of conditions and the following disclaimer.
|
||||
% * Redistributions in binary form must reproduce the above copyright
|
||||
% notice, this list of conditions and the following disclaimer in
|
||||
% the documentation and/or other materials provided with the distribution
|
||||
%
|
||||
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
% ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||
% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
% CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
% ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
% POSSIBILITY OF SUCH DAMAGE.
|
||||
%
|
||||
%
|
||||
|
||||
|
||||
% Input checks.
|
||||
if verLessThan('matlab','7.13')
|
||||
error(nargchk(2,5,nargin)) %#ok<NCHKN>
|
||||
else
|
||||
narginchk(2,5)
|
||||
end
|
||||
|
||||
% Adjustments based on number of arguments.
|
||||
switch nargin
|
||||
case 2
|
||||
robust = true;
|
||||
x2 = x1;
|
||||
y2 = y1;
|
||||
self_intersect = true;
|
||||
case 3
|
||||
robust = x2;
|
||||
x2 = x1;
|
||||
y2 = y1;
|
||||
self_intersect = true;
|
||||
case 4
|
||||
robust = true;
|
||||
self_intersect = false;
|
||||
case 5
|
||||
self_intersect = false;
|
||||
end
|
||||
|
||||
% x1 and y1 must be vectors with same number of points (at least 2).
|
||||
if sum(size(x1) > 1) ~= 1 || sum(size(y1) > 1) ~= 1 || ...
|
||||
length(x1) ~= length(y1)
|
||||
error('X1 and Y1 must be equal-length vectors of at least 2 points.')
|
||||
end
|
||||
% x2 and y2 must be vectors with same number of points (at least 2).
|
||||
if sum(size(x2) > 1) ~= 1 || sum(size(y2) > 1) ~= 1 || ...
|
||||
length(x2) ~= length(y2)
|
||||
error('X2 and Y2 must be equal-length vectors of at least 2 points.')
|
||||
end
|
||||
|
||||
|
||||
% Force all inputs to be column vectors.
|
||||
x1 = x1(:);
|
||||
y1 = y1(:);
|
||||
x2 = x2(:);
|
||||
y2 = y2(:);
|
||||
|
||||
% Compute number of line segments in each curve and some differences we'll
|
||||
% need later.
|
||||
n1 = length(x1) - 1;
|
||||
n2 = length(x2) - 1;
|
||||
xy1 = [x1 y1];
|
||||
xy2 = [x2 y2];
|
||||
dxy1 = diff(xy1);
|
||||
dxy2 = diff(xy2);
|
||||
|
||||
|
||||
% Determine the combinations of i and j where the rectangle enclosing the
|
||||
% i'th line segment of curve 1 overlaps with the rectangle enclosing the
|
||||
% j'th line segment of curve 2.
|
||||
|
||||
% Original method that works in old MATLAB versions, but is slower than
|
||||
% using binary singleton expansion (explicit or implicit).
|
||||
% [i,j] = find( ...
|
||||
% repmat(mvmin(x1),1,n2) <= repmat(mvmax(x2).',n1,1) & ...
|
||||
% repmat(mvmax(x1),1,n2) >= repmat(mvmin(x2).',n1,1) & ...
|
||||
% repmat(mvmin(y1),1,n2) <= repmat(mvmax(y2).',n1,1) & ...
|
||||
% repmat(mvmax(y1),1,n2) >= repmat(mvmin(y2).',n1,1));
|
||||
|
||||
% Select an algorithm based on MATLAB version and number of line
|
||||
% segments in each curve. We want to avoid forming large matrices for
|
||||
% large numbers of line segments. If the matrices are not too large,
|
||||
% choose the best method available for the MATLAB version.
|
||||
if n1 > 1000 || n2 > 1000 || verLessThan('matlab','7.4')
|
||||
% Determine which curve has the most line segments.
|
||||
if n1 >= n2
|
||||
% Curve 1 has more segments, loop over segments of curve 2.
|
||||
ijc = cell(1,n2);
|
||||
min_x1 = mvmin(x1);
|
||||
max_x1 = mvmax(x1);
|
||||
min_y1 = mvmin(y1);
|
||||
max_y1 = mvmax(y1);
|
||||
for k = 1:n2
|
||||
k1 = k + 1;
|
||||
ijc{k} = find( ...
|
||||
min_x1 <= max(x2(k),x2(k1)) & max_x1 >= min(x2(k),x2(k1)) & ...
|
||||
min_y1 <= max(y2(k),y2(k1)) & max_y1 >= min(y2(k),y2(k1)));
|
||||
ijc{k}(:,2) = k;
|
||||
end
|
||||
ij = vertcat(ijc{:});
|
||||
i = ij(:,1);
|
||||
j = ij(:,2);
|
||||
else
|
||||
% Curve 2 has more segments, loop over segments of curve 1.
|
||||
ijc = cell(1,n1);
|
||||
min_x2 = mvmin(x2);
|
||||
max_x2 = mvmax(x2);
|
||||
min_y2 = mvmin(y2);
|
||||
max_y2 = mvmax(y2);
|
||||
for k = 1:n1
|
||||
k1 = k + 1;
|
||||
ijc{k}(:,2) = find( ...
|
||||
min_x2 <= max(x1(k),x1(k1)) & max_x2 >= min(x1(k),x1(k1)) & ...
|
||||
min_y2 <= max(y1(k),y1(k1)) & max_y2 >= min(y1(k),y1(k1)));
|
||||
ijc{k}(:,1) = k;
|
||||
end
|
||||
ij = vertcat(ijc{:});
|
||||
i = ij(:,1);
|
||||
j = ij(:,2);
|
||||
end
|
||||
|
||||
elseif verLessThan('matlab','9.1')
|
||||
% Use bsxfun.
|
||||
[i,j] = find( ...
|
||||
bsxfun(@le,mvmin(x1),mvmax(x2).') & ...
|
||||
bsxfun(@ge,mvmax(x1),mvmin(x2).') & ...
|
||||
bsxfun(@le,mvmin(y1),mvmax(y2).') & ...
|
||||
bsxfun(@ge,mvmax(y1),mvmin(y2).'));
|
||||
|
||||
else
|
||||
% Use implicit expansion.
|
||||
[i,j] = find( ...
|
||||
mvmin(x1) <= mvmax(x2).' & mvmax(x1) >= mvmin(x2).' & ...
|
||||
mvmin(y1) <= mvmax(y2).' & mvmax(y1) >= mvmin(y2).');
|
||||
|
||||
end
|
||||
|
||||
|
||||
% Find segments pairs which have at least one vertex = NaN and remove them.
|
||||
% This line is a fast way of finding such segment pairs. We take
|
||||
% advantage of the fact that NaNs propagate through calculations, in
|
||||
% particular subtraction (in the calculation of dxy1 and dxy2, which we
|
||||
% need anyway) and addition.
|
||||
% At the same time we can remove redundant combinations of i and j in the
|
||||
% case of finding intersections of a line with itself.
|
||||
if self_intersect
|
||||
remove = isnan(sum(dxy1(i,:) + dxy2(j,:),2)) | j <= i + 1;
|
||||
else
|
||||
remove = isnan(sum(dxy1(i,:) + dxy2(j,:),2));
|
||||
end
|
||||
i(remove) = [];
|
||||
j(remove) = [];
|
||||
|
||||
% Initialize matrices. We'll put the T's and B's in matrices and use them
|
||||
% one column at a time. AA is a 3-D extension of A where we'll use one
|
||||
% plane at a time.
|
||||
n = length(i);
|
||||
T = zeros(4,n);
|
||||
AA = zeros(4,4,n);
|
||||
AA([1 2],3,:) = -1;
|
||||
AA([3 4],4,:) = -1;
|
||||
AA([1 3],1,:) = dxy1(i,:).';
|
||||
AA([2 4],2,:) = dxy2(j,:).';
|
||||
B = -[x1(i) x2(j) y1(i) y2(j)].';
|
||||
|
||||
% Loop through possibilities. Trap singularity warning and then use
|
||||
% lastwarn to see if that plane of AA is near singular. Process any such
|
||||
% segment pairs to determine if they are colinear (overlap) or merely
|
||||
% parallel. That test consists of checking to see if one of the endpoints
|
||||
% of the curve 2 segment lies on the curve 1 segment. This is done by
|
||||
% checking the cross product
|
||||
%
|
||||
% (x1(2),y1(2)) - (x1(1),y1(1)) x (x2(2),y2(2)) - (x1(1),y1(1)).
|
||||
%
|
||||
% If this is close to zero then the segments overlap.
|
||||
|
||||
% If the robust option is false then we assume no two segment pairs are
|
||||
% parallel and just go ahead and do the computation. If A is ever singular
|
||||
% a warning will appear. This is faster and obviously you should use it
|
||||
% only when you know you will never have overlapping or parallel segment
|
||||
% pairs.
|
||||
|
||||
if robust
|
||||
overlap = false(n,1);
|
||||
warning_state = warning('off','MATLAB:singularMatrix');
|
||||
% Use try-catch to guarantee original warning state is restored.
|
||||
try
|
||||
lastwarn('')
|
||||
for k = 1:n
|
||||
T(:,k) = AA(:,:,k)\B(:,k);
|
||||
[unused,last_warn] = lastwarn; %#ok<ASGLU>
|
||||
lastwarn('')
|
||||
if strcmp(last_warn,'MATLAB:singularMatrix')
|
||||
% Force in_range(k) to be false.
|
||||
T(1,k) = NaN;
|
||||
% Determine if these segments overlap or are just parallel.
|
||||
overlap(k) = rcond([dxy1(i(k),:);xy2(j(k),:) - xy1(i(k),:)]) < eps;
|
||||
end
|
||||
end
|
||||
warning(warning_state)
|
||||
catch err
|
||||
warning(warning_state)
|
||||
rethrow(err)
|
||||
end
|
||||
% Find where t1 and t2 are between 0 and 1 and return the corresponding
|
||||
% x0 and y0 values.
|
||||
in_range = (T(1,:) >= 0 & T(2,:) >= 0 & T(1,:) <= 1 & T(2,:) <= 1).';
|
||||
% For overlapping segment pairs the algorithm will return an
|
||||
% intersection point that is at the center of the overlapping region.
|
||||
if any(overlap)
|
||||
ia = i(overlap);
|
||||
ja = j(overlap);
|
||||
% set x0 and y0 to middle of overlapping region.
|
||||
T(3,overlap) = (max(min(x1(ia),x1(ia+1)),min(x2(ja),x2(ja+1))) + ...
|
||||
min(max(x1(ia),x1(ia+1)),max(x2(ja),x2(ja+1)))).'/2;
|
||||
T(4,overlap) = (max(min(y1(ia),y1(ia+1)),min(y2(ja),y2(ja+1))) + ...
|
||||
min(max(y1(ia),y1(ia+1)),max(y2(ja),y2(ja+1)))).'/2;
|
||||
selected = in_range | overlap;
|
||||
else
|
||||
selected = in_range;
|
||||
end
|
||||
xy0 = T(3:4,selected).';
|
||||
|
||||
% Remove duplicate intersection points.
|
||||
[xy0,index] = unique(xy0,'rows');
|
||||
x0 = xy0(:,1);
|
||||
y0 = xy0(:,2);
|
||||
|
||||
% Compute how far along each line segment the intersections are.
|
||||
if nargout > 2
|
||||
sel_index = find(selected);
|
||||
sel = sel_index(index);
|
||||
iout = i(sel) + T(1,sel).';
|
||||
jout = j(sel) + T(2,sel).';
|
||||
end
|
||||
else % non-robust option
|
||||
for k = 1:n
|
||||
[L,U] = lu(AA(:,:,k));
|
||||
T(:,k) = U\(L\B(:,k));
|
||||
end
|
||||
|
||||
% Find where t1 and t2 are between 0 and 1 and return the corresponding
|
||||
% x0 and y0 values.
|
||||
in_range = (T(1,:) >= 0 & T(2,:) >= 0 & T(1,:) < 1 & T(2,:) < 1).';
|
||||
x0 = T(3,in_range).';
|
||||
y0 = T(4,in_range).';
|
||||
|
||||
% Compute how far along each line segment the intersections are.
|
||||
if nargout > 2
|
||||
iout = i(in_range) + T(1,in_range).';
|
||||
jout = j(in_range) + T(2,in_range).';
|
||||
end
|
||||
end
|
||||
|
||||
% Plot the results (useful for debugging).
|
||||
% plot(x1,y1,x2,y2,x0,y0,'ok');
|
||||
|
||||
function y = mvmin(x)
|
||||
% Faster implementation of movmin(x,k) when k = 1.
|
||||
y = min(x(1:end-1),x(2:end));
|
||||
|
||||
function y = mvmax(x)
|
||||
% Faster implementation of movmax(x,k) when k = 1.
|
||||
y = max(x(1:end-1),x(2:end));
|
||||
@@ -0,0 +1,115 @@
|
||||
% SHOW_SPATIAL_DISTRIBUTION plot distribution of a variable, you can also use scatter or scatter_hsv
|
||||
%
|
||||
% show_spatial_distribution(pos, values, symmetrize, plot_points, range, px_scale )
|
||||
%
|
||||
% ** pos positions for each value
|
||||
% ** val plotted values
|
||||
% ** symmetrize (bool) if true make the caxis symmetric around 0
|
||||
% ** plot_points (bool) if true plot the positions where are provided values located
|
||||
% ** range array 2x1 of min / max range
|
||||
% ** px_scale size of a single pixel
|
||||
|
||||
%
|
||||
%
|
||||
% Academic License Agreement
|
||||
%
|
||||
% Source Code
|
||||
%
|
||||
% Introduction
|
||||
% • This license agreement sets forth the terms and conditions under which the PAUL SCHERRER INSTITUT (PSI), CH-5232 Villigen-PSI, Switzerland (hereafter "LICENSOR")
|
||||
% will grant you (hereafter "LICENSEE") a royalty-free, non-exclusive license for academic, non-commercial purposes only (hereafter "LICENSE") to use the cSAXS
|
||||
% ptychography MATLAB package computer software program and associated documentation furnished hereunder (hereafter "PROGRAM").
|
||||
%
|
||||
% Terms and Conditions of the LICENSE
|
||||
% 1. LICENSOR grants to LICENSEE a royalty-free, non-exclusive license to use the PROGRAM for academic, non-commercial purposes, upon the terms and conditions
|
||||
% hereinafter set out and until termination of this license as set forth below.
|
||||
% 2. LICENSEE acknowledges that the PROGRAM is a research tool still in the development stage. The PROGRAM is provided without any related services, improvements
|
||||
% or warranties from LICENSOR and that the LICENSE is entered into in order to enable others to utilize the PROGRAM in their academic activities. It is the
|
||||
% LICENSEE’s responsibility to ensure its proper use and the correctness of the results.”
|
||||
% 3. THE PROGRAM IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR
|
||||
% A PARTICULAR PURPOSE AND NONINFRINGEMENT OF ANY PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS. IN NO EVENT SHALL THE LICENSOR, THE AUTHORS OR THE COPYRIGHT
|
||||
% HOLDERS BE LIABLE FOR ANY CLAIM, DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES OR OTHER LIABILITY ARISING FROM, OUT OF OR IN CONNECTION WITH THE PROGRAM OR THE USE
|
||||
% OF THE PROGRAM OR OTHER DEALINGS IN THE PROGRAM.
|
||||
% 4. LICENSEE agrees that it will use the PROGRAM and any modifications, improvements, or derivatives of PROGRAM that LICENSEE may create (collectively,
|
||||
% "IMPROVEMENTS") solely for academic, non-commercial purposes and that any copy of PROGRAM or derivatives thereof shall be distributed only under the same
|
||||
% license as PROGRAM. The terms "academic, non-commercial", as used in this Agreement, mean academic or other scholarly research which (a) is not undertaken for
|
||||
% profit, or (b) is not intended to produce works, services, or data for commercial use, or (c) is neither conducted, nor funded, by a person or an entity engaged
|
||||
% in the commercial use, application or exploitation of works similar to the PROGRAM.
|
||||
% 5. LICENSEE agrees that it shall make the following acknowledgement in any publication resulting from the use of the PROGRAM or any translation of the code into
|
||||
% another computing language:
|
||||
% "Data processing was carried out using the cSAXS ptychography MATLAB package developed by the Science IT and the coherent X-ray scattering (CXS) groups, Paul
|
||||
% Scherrer Institut, Switzerland."
|
||||
%
|
||||
% Additionally, any publication using the package, or any translation of the code into another computing language should cite for difference map:
|
||||
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379–382 (2008).
|
||||
% (doi: 10.1126/science.1158573),
|
||||
% for mixed coherent modes:
|
||||
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 68–71 (2013). (doi: 10.1038/nature11806),
|
||||
% for LSQ-ML method
|
||||
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
|
||||
% for OPRP method
|
||||
% M. Odstrcil, P. Baksh, S. A. Boden, R. Card, J. E. Chad, J. G. Frey, W. S. Brocklesby, "Ptychographic coherent diffractive imaging with orthogonal probe relaxation." Optics express 24.8 (2016): 8360-8369
|
||||
% and/or for multislice:
|
||||
% E. H. R. Tsai, I. Usov, A. Diaz, A. Menzel, and M. Guizar-Sicairos, X-ray ptychography with extended depth of field, Opt. Express 24, 29089–29108 (2016).
|
||||
% 6. Except for the above-mentioned acknowledgment, LICENSEE shall not use the PROGRAM title or the names or logos of LICENSOR, nor any adaptation thereof, nor the
|
||||
% names of any of its employees or laboratories, in any advertising, promotional or sales material without prior written consent obtained from LICENSOR in each case.
|
||||
% 7. Ownership of all rights, including copyright in the PROGRAM and in any material associated therewith, shall at all times remain with LICENSOR, and LICENSEE
|
||||
% agrees to preserve same. LICENSEE agrees not to use any portion of the PROGRAM or of any IMPROVEMENTS in any machine-readable form outside the PROGRAM, nor to
|
||||
% make any copies except for its internal use, without prior written consent of LICENSOR. LICENSEE agrees to place the following copyright notice on any such copies:
|
||||
% © All rights reserved. PAUL SCHERRER INSTITUT, Switzerland, Laboratory for Macromolecules and Bioimaging, 2017.
|
||||
% 8. The LICENSE shall not be construed to confer any rights upon LICENSEE by implication or otherwise except as specifically set forth herein.
|
||||
% 9. DISCLAIMER: LICENSEE shall be aware that Phase Focus Limited of Sheffield, UK has an international portfolio of patents and pending applications which relate
|
||||
% to ptychography and that the PROGRAM may be capable of being used in circumstances which may fall within the claims of one or more of the Phase Focus patents,
|
||||
% in particular of patent with international application number PCT/GB2005/001464. The LICENSOR explicitly declares not to indemnify the users of the software
|
||||
% in case Phase Focus or any other third party will open a legal action against the LICENSEE due to the use of the program.
|
||||
% 10. This Agreement shall be governed by the material laws of Switzerland and any dispute arising out of this Agreement or use of the PROGRAM shall be brought before
|
||||
% the courts of Zürich, Switzerland.
|
||||
|
||||
|
||||
|
||||
function show_spatial_distribution(pos, values, symmetrize, plot_points, range, px_scale )
|
||||
|
||||
|
||||
pos = double(pos);
|
||||
values = squeeze(double(values));
|
||||
if nargin < 3; symmetrize = false; end
|
||||
if nargin < 4; plot_points = true; end
|
||||
if nargin < 5 || isempty(range); range = [min(values(:)), max(values(:))]; end
|
||||
if nargin < 6; px_scale = 1; end
|
||||
|
||||
if range(1) == range(2)
|
||||
range(1) = 0;
|
||||
range(2) = max(range(1),1);
|
||||
range = sort(range);
|
||||
end
|
||||
|
||||
% remove missing data
|
||||
missing = isnan(values);
|
||||
pos(missing,:) = [];
|
||||
values(missing) = [];
|
||||
|
||||
ax = [min(pos(:,1)), max(pos(:,1)), min(pos(:,2)), max(pos(:,2))];
|
||||
N = max(100, 4*sqrt(length(pos)));
|
||||
XI = linspace(ax(1), ax(2), N);
|
||||
YI = linspace(ax(3), ax(4), N)';
|
||||
warning('off','all')
|
||||
Z = griddata(pos(:,1),pos(:,2),real(values),XI,YI,'linear');
|
||||
if ~isreal(values)
|
||||
Z = Z + 1i*griddata(pos(:,1),pos(:,2),imag(values),XI,YI,'linear');
|
||||
end
|
||||
warning('on','all')
|
||||
|
||||
|
||||
if isreal(Z)
|
||||
imagesc(px_scale*XI, px_scale*YI, Z, range)
|
||||
colormap gray
|
||||
else
|
||||
imagesc_hsv(Z)
|
||||
end
|
||||
if plot_points
|
||||
hold on
|
||||
plot(px_scale*pos(:,1), px_scale*pos(:,2), 'wo')
|
||||
hold off
|
||||
axis equal tight
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user