[Pkg-octave-devel] Bug#365757: plotcollapse

Francesco Potorti` Potorti at isti.cnr.it
Tue May 16 13:54:55 UTC 2006


I sent this message before, but it may have got lost in the mailing list
reorganisation.  I resend it with an improved and much faster version of
the same program.

This function solves a problem of mine when plotting data.  It often
happens to me to have a lot of redundant points for measured data,
points that are mostly invisible on paper or screen because they are
very near one another.  Moreover, when plotting lines, often a straight
line is composed by a great number of small segments, which makes a huge
.eps file.

This function can be called on data to be plotted to optimise them by
removing unnecessary points before generating the plot.  Data that I use
are typically reduced by ten times.  You will appreciate this if you
happen to plot data series of simulated or measured data with hundreds
or more samples.

Segment optimisation can be disabled for point plots.  Point plot
optimisation is done as a special case of general segment optimisation,
but it could probably be rewritten with a separate vectorialised
algorithm that makes it much faster.

Only the common case of points with increasing abscissae is optimised.
Optimisation in the general case is more complex and probably not worth
doing for plotting.

If you try it on your data please let me know how it works.

## Copyright © 2006  Francesco Potortì
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software Foundation,
## Inc., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301, USA.

## -*- texinfo -*-
## @deftypefn {Function File} {} plotcollapse (@var{P})
## @deftypefnx {Function File} {} plotcollapse (@var{P}, @var{so})
## @deftypefnx {Function File} {} plotcollapse (@var{P}, @var{so}, @var{res})
##
## Optimise plot data by removing redundant points and segments
##
## The first parameter @var{P} is a two-column matrix to be plotted as X and
## Y coordinates.   The second optional argument @var{so} disables segment
## optimisation when set to @var{false} (default is @var{true}). The third
## optional argument @var{res} is the size of the largest error on the plot:
## if it is a scalar, it is meant relative to the range of X and Y values
## (default 1e-3); if it is a 2x1 array, it contains the absolute errors for
## X and Y.  Returns a two-column matrix containing a subset of the rows of
## @var{P}. A line plot of @var{P} has the same appearance as a line plot of
## the output, with errors smaller than @var{res}.  When creating point
## plots, set @var{so} to @var{false}.
## @end deftypefn

## Author: Francesco Potortì <Potorti at isti.cnr.it>
## $Revision: 2.7 $
## Usage: plotcollapse(P[, so[, res]])
## Description: Optimise plot data by removing redundant points and segments

function C = plotcollapse (P, so, res)

  if (!ismatrix(P) || columns(P) != 2)
    error("P must be a matrix with two columns");
  endif
  if (nargin < 2)
    so = true;			# do segment optimisation
  endif
  if (nargin < 3)
    res = 1e-3;			# default resolution is 1000 dots/axis
  endif

  ## Slack: admissible error on coordinates on the output plot
  if (isscalar(res))
    if (res <= 0)
      error("res must be positive");
    endif
    E = range(P)' * res;	# build error vector using range of data
  elseif (ismatrix(res))
    if (!all(size(res) == [2 1]) || any(res <= 0))
      error("res must be a 2x1 matrix with positive values");
    endif
    E = res;			# take error vector as it is
  else
    error("res should be a scalar or matrix");
  endif

  if (rows(P) < 3)
    C = P;
    return;			# nothing to do
  endif
  P ./= repmat(E',rows(P),1);	# normalize P
  rot = [0,-1;1,0];		# rotate a vector pi/4 anticlockwise

  ## Iteratively remove points too near to the previous point
  while (1)
    V = [true; sumsq(diff(P),2) > 1]; # points far from the previous ones
    if (all(V)) break; endif
    V = [true; diff(V) >= 0];	# identify the sequence leaders
    P = P(V,:);			# remove them
  endwhile

  ## Remove points laying near to a segment: for each segment R->S, build a
  ## unitary-lenght projection vector D perpendicular to R->S, and project
  ## R->T over D to compute the distance ot T from R->S.
  if (so)			# segment optimisation
    ## For each segment, r and s are its extremes
    r = 1; R = P(1,:)';		# start of segment
    s = 2; S = P(2,:)';		# end of the segment
    rebuild = true;		# build first projection vector

    for t = 3:rows(P)
      if (rebuild)		# build projection vector
	D = rot*(S-R)/sqrt(sumsq(S-R)); # projection vector for distance
	rebuild = false;	# keep current projection vector
      endif

      T = P(t,:)';		# next point

      if (abs(sum((T-R).*D)) < 1 # T is aligned
	  && sum((T-R).*(S-R)) > 0) # going forward
	V(s) = false;		# do not plot s
      else			# set a new segment
	r = s; R = S;		# new start of segment
	rebuild = true;		# rebuild projection vector
      endif
      s = t; S = T;		# new end of segment
    endfor
  endif

  C = P(V,:) .* repmat(E',sum(V),1); # denormalize P
endfunction




More information about the Pkg-octave-devel mailing list