
function print_geom_objects (GOBJ, taglist)
% print geometry object's object info, traversing its children.
% Displays selected fields of each object in the input geometric
% object "GOBJ" or those objects whose tags are included in an optional 
% call array parameter "objlist".


% place constant array for names of transformation fields
global trans_param_names;
trans_param_names = {'disp' 'scale' 'rotnAng' 'rotnCntr' 'rotnAxis' 'p6(?)' };

% place constant array for names of cutplane fields
global brep_param_names;
brep_param_names = {'cutplane pt' 'cutplane norm'};

if( nargin ~= 1 )
    if ~iscell(taglist)
        disp ("INFO: Optional 'objtags' must be cell array such as {'S1' B1'}");
        return;
    end
end


% disp ("Objects (by tag):");

for i=1:1:length(GOBJ.objects)
    if( nargin == 1 )  
        %% || ismember (GOBJ.objects{i}.tag, objtags)
        %% (if I could count on short circuit processing of 'if A || B'
        %% but for now just make two tests and duplicate calls :(
        print_children ("", GOBJ.objects{i}, i, 0);
    elseif  ismember (GOBJ.objects{i}.tag, taglist)
        print_children ("", GOBJ.objects{i}, i, 0);
    end
end

end

function print_param (par, n)
% print triad, typically a 3D point or Vector
for j=1:1:length(par{n})
    if (isinteger(par{n}(j)))
        %  integer or logical
        fprintf (" %2d", par{n}(j));
    else
        %  floating point
        fprintf (" %9f", par{n}(j));
    end
end
end


function print_info (space, obj, n)
% Print selected fields from objects of the specific object type

    global trans_param_names;
    global brep_param_names;
    
    % first print the info contained in all objects
    fprintf ("%s%2d:  '%s', '%s'\n", space, n, obj.tag, obj.type);
    
    % then 'case/switch' on each type of interest and print its info
    if isequal(obj.type,'transformed')
        for i=1:1:length(obj.param)
            fprintf ("%s   %8s: ", space, trans_param_names{i});
            print_param (obj.param, i);
            fprintf ("\n");
        end
        
    elseif  isequal(obj.type,'point')
        fprintf ("   %scenter: %9f  %9f  %9f\n", space, obj.center(1:3));
        fprintf ("   %s     v: %9f  %9f  %9f  (?)\n", space, obj.v(1:3));
        
    elseif  isequal(obj.type,'block')
        fprintf ("   %s        min        max\n", space);
        fprintf ("   %s x: %9f  %9f\n", space, obj.x_min, obj.x_max);
        fprintf ("   %s y: %9f  %9f\n", space, obj.y_min, obj.y_max);
        fprintf ("   %s z: %9f  %9f\n", space, obj.z_min, obj.z_max);
              
    elseif  isequal(obj.type,'ellipsoid')
        fprintf ("   %scenter: %9f  %9f  %9f\n", space, obj.center(1:3));
        fprintf ("   %s radii: %9f  %9f  %9f\n", space, obj.radius_x, obj.radius_y, obj.radius_z);
        fprintf ("   %s  axis: %9f  %9f  %9f\n", space, obj.axis(1:3));
        
    elseif  isequal(obj.type,'sphere')
        fprintf ("   %s  center: %9f  %9f  %9f\n", space, obj.center(1:3));
        fprintf ("   %s  radius: %9f\n", space, obj.radius);
        fprintf ("   %s    axis: %2d     (1/2/3 ==> x/y/z axis direction)\n", space, obj.axis);

    elseif  isequal(obj.type,'cylinder')
        fprintf ("   %scenter: %9f  %9f  %9f\n", space, obj.center(1:3));
        fprintf ("   %sradius: %9f\n", space, obj.radius);
        fprintf ("   %slength: %9f\n", space, obj.length);
        fprintf ("   %s  axis: %9f  %9f  %9f\n", space, obj.axis(1:3));

    elseif  isequal(obj.type,'brep')
        for i=1:1:length(obj.param)
            fprintf ("%s   %13s: ", space, brep_param_names{i});
            print_param (obj.param, i);
            fprintf ("\n");
        end
        
    elseif  isequal(obj.type,'cone')
        fprintf ("   %scenter: %9f  %9f  %9f\n", space, obj.center(1:3));
        fprintf ("   %s rad_1: %9f\n", space, obj.radius_1);
        fprintf ("   %s rad_2: %9f\n", space, obj.radius_2);
        fprintf ("   %slength: %9f\n", space, obj.length);
        fprintf ("   %s  axis: %9f  %9f  %9f\n", space, obj.axis(1:3));
        
    elseif  isequal(obj.type,'extrusion')
        fprintf ("   %sdistance: %9f\n", space, obj.param{1});
        fprintf ("   %s  vector: %9f  %9f  %9f\n", space, obj.param{2});
        
        if isstruct(obj.param{3}) && ...
           isequal(fieldnames(obj.param{3}),[{'p'}; {'n'}; {'t'}])
            %  extrusion from 2D Workplane at: 
            fprintf ("   %sWkPpoint: %9f  %9f  %9f\n", space, obj.param{3}.p);
            fprintf ("   %sWkPVnorm: %9f  %9f  %9f\n", space, obj.param{3}.n);
            fprintf ("   %sWkPVtang: %9f  %9f  %9f\n", space, obj.param{3}.t);
            
        else  % if isinteger(obj.param{3}) but it is a FLOAT?
            fprintf ("   %sobj face:  %d\n", space, obj.param{3});

        end
    end   
end

function print_children (space, obj, m, gen)

if (m == 1)
    %  Print a header for each generation
    fprintf ("%s --> Gen %d", space, gen);
    if (gen == 0)
        fprintf (" (Active Objects)");
    end
    % fprintf ("\n");
end
fprintf ("\n");

% print the salient info for this object
print_info(space, obj, m);

if (isfield(obj,'children') && ~isempty(obj.children))
    % if there are children, print its info and traverse its descendents
    for k=1:1:length(obj.children) 
        print_children(space+"   ", obj.children{k}, k, gen+1);
    end
end
end



