function [figRegionCoords,figDragEnd,figDragStart, maxX, minX, maxY, minY]   = getROIInFigCoordinates(this, varargin)
% This internal helper function may change in a future release.

% getROIInFigCoordinates returns the ROI vertices in figure coordinates

%  Copyright 2019 The MathWorks, Inc.

fig = this.Figure;
ax = this.Axes;

% It is possible for windowFocusLostFcn to clear the brushing.select3d
% object when the renderer is changed to openGL by the brushdown function
% during a brushing gesture (g654964). This will cause the Axes and Figure
% properties to be cleared. Quick return to prevent that causing an error.
if isempty(ax) || isempty(fig)
    [figRegionCoords,figDragEnd,figDragStart, maxX, minX, maxY, minY] = deal([]);
    return
end

% Get ROI vertices in figure coords
figDragEnd = get(fig,'CurrentPoint');
figDragStart = this.ScribeStartPoint;

%Since all coputations are done in pixels , make sure that the units are
%consistent
figDragEnd = hgconvertunits(fig,[figDragEnd 0 0],fig.Units,'pixels',fig);
figDragStart = hgconvertunits(fig,[figDragStart 0 0],fig.Units,'pixels',fig);


% Find the cubic hull of the axes vertices

xlim = this.AxesXLim;
ylim = this.AxesYLim;
zlim = this.AxesZLim;

iter = matlab.graphics.axis.dataspace.XYZPointsIterator;
iter.XData = [xlim(1) xlim(2) xlim(2) xlim(1) xlim(1) xlim(2) xlim(2) xlim(1)];
iter.YData = [ylim(1) ylim(1) ylim(1) ylim(1) ylim(2) ylim(2) ylim(2) ylim(2)];
iter.ZData = [zlim(1) zlim(1) zlim(2) zlim(2) zlim(1) zlim(1) zlim(2) zlim(2)];
camAxesVertices = TransformPoints(ax.DataSpace,[],iter);
figAxesVertices = brushing.select.transformCameraToFigCoord(ax,camAxesVertices);
minX = min(figAxesVertices(1,:));
maxX = max(figAxesVertices(1,:));
minY = min(figAxesVertices(2,:));
maxY = max(figAxesVertices(2,:));

% Clip ROI in figure space to axes vertices
figDragEnd(1) = max(min(figDragEnd(1),maxX),minX);
figDragEnd(2) = max(min(figDragEnd(2),maxY),minY);
figDragStart(1) = max(min(figDragStart(1),maxX),minX);
figDragStart(2) = max(min(figDragStart(2),maxY),minY);
figRegionCoords = [figDragStart(1) figDragStart(2);...
    figDragEnd(1) figDragStart(2);...
    figDragEnd(1) figDragEnd(2);...
    figDragStart(1) figDragEnd(2)
    ]';
end