We have successfully segmented the failure regions from non-failure regions. Now we need to develop a method to sample near
the boundary of the failure region to continue to resolve the boundary.

Use Voronoi Diagram to Find Next Points

Voronoi diagrams add cells around each point that separates each point from the others. For this problem, the cells look
like rectangles, but thet can be any shape/sized polygon that best separates the points.

We'll use the cell polygons to find points to use that are close to our regions of interest and use them to update our sampling
of the peaks function to resolve the failure boundary.

Use Convex Hull to Identify First Failure Region

We need a way to identify the failure regions boundaries. Convex Hulls will encompase the failure points in a polygon. We
can then use the polygon to identify the Voronoi cells we can use to update our sampling of the peaks function.

% Make note of any points that are in the convex hull or on it
in = inpolygon(x,y,DT.X(ch,1),DT.X(ch,2));
% Use the points in the convex hull to find the voronoi cells
vc = R(in);
% Use the voronoi cell points as the next points to evaluate
idxNewPts = [];
for j = 1:length(vc)
idxNewPts = [idxNewPts, vc{j}];
end
idxNewPts = unique(idxNewPts);
newXY = V(idxNewPts,:);
% Remove any points in/on the convex hull
in = inpolygon(newXY(:,1),newXY(:,2),DT.X(ch,1),DT.X(ch,2));
newXY = newXY(~in,:);
% Show plot of the points we want to update
plot(newXY(:,1),newXY(:,2),'r+')

Note that Convex Hull was used to find the boundary. This assumes the failure region will be convex in shape. freeBoundary
could be used instead of Convex Hull if you expect a non-convex shape to the failure region.

Encapsulate Above Process in Function boundarySearch

Repeat the process above to find the boundary for the other failure region, and continue to add points to the boundary until
satisfied with the boundary resolution (in this case, a change in perimeter of less than 0.1 is used)

Note that the boundarySearch function takes in a function handle (@) symbol as an arguement. This allows us to use the boundarySearch for an arbitrary function. For example, we'll use this function on a Simulink® Model in statOptimDesign.

Show Boundary on Peaks

Plot the result on top of the peaks function to see how well the algorithm did.

hold off
peaks
hold on
view(2);
col = 'rb';
for i = 1:length(P)
plot(xb{i}(:,1),xb{i}(:,2),col(i),'LineWidth',2)
end