function tree = GeoMLConvertToTree(model,visible)
% GEOMLCONVERTTOTREE Converts a tree containing a model.
%
% function tree = GeoMLConvertToTree(model,visible)
%
% This function generates a tree containing a model hierarchy using
% structs as group containers. In this tree only the geometrical entites
% are inserted, in particular each geometrical entity became a leaf of the
% struct containing a cell with a first element (position 1) containing a
% string that defines the type of the entity, and the following elements
% describes the entity. The following geometrical entities are managed:
%
% groups: Are expanded in structs with theri content.
% points: With tag 'point' are described with a 2x1 double vector.
% lines: With tag 'line' are described in omogeneous coordinates with a
% 3x1 double vector.
% circles: With tag 'circle' are described with a center point (2x1 double
% vector) and a radius (a double).
% paths: With tag 'path' are described with a matrix of integer (column)
% points.
%
% Params
% ------
% IN:
% model = The GeoML model.
% visible = Only visible entities must be inserted? (def=true)
% OUT:
% tree = The model tree.
%
% Pre
% ---
% - The model must be a GeoML valid model.
%
% Post
% - The tree contains all the model entities except the hidden ones if not
% required.
% - The tree order isn't the same of the XML file.
% Check params:
if nargin<1 error('A model must be given!'); end
if nargin<2 visible=true; end
% Only geometry:
if ~isa(model,'com.dsi.libs.geoml.Geometry') || ...
(visible && ~model.isVisible)
tree = false;
return;
end
% Looking the type of the model:
if isa(model,'com.dsi.libs.geoml.Group') % This is a group:
% Preparing the struct:
tree = struct;
% Iterating on it's content:
i = model.iterator;
while i.hasNext
% Getting an element:
e = i.next;
% Converting:
c = GeoMLConvertToTree(e,visible);
% Adding:
if ~isa(c,'logical')
% All ok, adding:
tree = setfield(tree,char(e.getName),c);
end
end
elseif isa(model,'com.dsi.libs.geoml.Line') % This is a line:
% Creating the line descriptor:
tree = {'line',reshape(model.getLineEquation,[3,1])};
elseif isa(model,'com.dsi.libs.geoml.Circle') % This is a circle:
% Creating the circle descriptor:
tree = {'circle',reshape(model.getCenter,[2,1]),model.getRadius};
elseif isa(model,'com.dsi.libs.geoml.IterablePath') % This is a path:
% Creating the path descriptor:
tree = {'path',GeoMLIterate(model)};
else % This is a point:
% Creating the point descriptor:
tree = {'point',reshape(model.getCoords,[2,1])};
end