function I = regionpicker(obj,crossX)
% This internal helper function may change in a future release.               

% I  = REGIONPICKER(OBJ,CROSSX) 
% OBJ is an axes child.
% CROSSX is a matrix which defines an region of interest cross-section 
%        in normlized figure units. It comprises a 2-by-2 matrix, where the
%        first column identifies one corner of the ROI and the second
%        column the other. Thix matrix is usually generated by a call to
%        brushing3d.draw, which draws the ROI.
% I is a logical array the same size as either the zdata of OBJ, if it 
%        exists, or the xdata if is does not. The entries of I indicate if
%        the corresponding vertex of OBJ lies within the prism defined
%        by CROSSX.

%   Copyright 2008-2009 The MathWorks, Inc.

% Get the xdata,ydata,zdata for the current object
xdata = get(obj,'XData');
ydata = get(obj,'YData');

% Some objects (ex. stairseries) do not have zdata but can be brushed in
% 3d views after rotating axes. Wrap the call to get zdata in a try 
% to trap this situation, and if found assume all zdata is zero.
try % 
    zdata = get(obj,'ZData');
catch mException
   if strcmp(mException.identifier,'MATLAB:class:InvalidProperty')
       zdata = [];
   else
       rethrow(mException);
   end
end
if isempty(zdata)
    zdata = zeros(size(xdata));
end
if ~isequal(size(xdata),size(zdata)) && isvector(xdata)
    xdata = repmat(xdata,[size(zdata,1) 1]);
end
if ~isequal(size(ydata),size(zdata)) && isvector(ydata)
    ydata = repmat(ydata,[1 size(zdata,2)]);
end 

% Get the cross-section bounds.
minX = min(crossX(1,1),crossX(2,1));
maxX = max(crossX(1,1),crossX(2,1));
minY = min(crossX(1,2),crossX(2,2));
maxY = max(crossX(1,2),crossX(2,2));

% Project the (x,y,z) values onto the figure normalized units and 
% determine which points lie within the ROI.
figurePixelVectorNormalized = brushing.select3d.convertToFigNormalized(...
    ancestor(obj,'axes'),ancestor(obj,'figure'),[xdata(:)'; ydata(:)'; zdata(:)']);
I = figurePixelVectorNormalized(1,:)>=minX & figurePixelVectorNormalized(1,:)<=maxX & ...
    figurePixelVectorNormalized(2,:)>=minY & figurePixelVectorNormalized(2,:)<=maxY ;
I = reshape(I,size(zdata));

