classdef OpenCylinder < shape.Shape3D
%SHAPE.OPENCYLINDER Creates an Open Cylinder on the X-Y-Z plane
% 
%   h = shape.OpenCylinder returns a cylinder shape open on top cap and
%   centered at the origin in 3D space.
%
%   h = shape.OpenCylinder('Property1', value1,'Property2', value2,...)
%   returns a open cylinder shape, based on the specified properties.
%   Properties you do not specify retain their default values.
%
%   OpenCylinder properties:
%
%   Name         - Name of the cylinder shape
%   Center       - Cartesian coordinates of the center of the cylinder in
%                  meters specified as a 3-element vector. The default value
%                  is [0 0 0].
%   Radius       - Radius of the cylinder shape in meters. The default value is 1
%   Height       - Height of the cylinder shape in meters . The default value is
%                  2.
%   Metal       - Metal assigned for the shape. The default object is
%                 'PEC' with infinite conductivity and zero
%                 thickness.
%   Dielectric  - Dielectric assigned for the shape. The default object is
%                 'Air'.
%   Color        - Face Color of the shape. The default value is 'y'.
%   Transparency - Transparency of the shape. The default value is 1.
%   EdgeColor    - Edge Color of the shape. The default value is 'k'.
%
%   OpenCylinder methods:
%
%   add          - Boolean unite operation on two shapes
%   subtract     - Boolean subtraction operation on two shapes
%   intersect    - Boolean intersection operation on two shapes
%   plus         - r1 + r2, boolean unite, equivalent to add
%   minus        - r1 - r2, boolean subtraction, equivalent to subtract
%   and          - r1&r2, boolean intersection, equivalent to intersect
%   show         - Visualize the shape as a filled patch
%   mesh         - Compute/visualize the mesh of the shape
%   rotate       - Rotate the shape about an axis and an angle
%   rotateX      - Rotate the shape about the X-axis with an angle
%   rotateY      - Rotate the shape about the Y-axis with an angle
%   rotateZ      - Rotate the shape about the Z-axis with an angle
%   translate    - Translate the shape to a new location
%   scale        - Scale the size of the shape
%   imprintShape - Imprint a 2d shape onto a custom 2-D or 3-D shape
%   createHole   - Create a hole of 2d shape on custom 2-D or 3-D shape
%   extrude      - Extrude a 2d shape on custom shape
%   removeFaces  - Remove the selected faces of shape interactively
%   addSubstrate - Add Substrate to the custom shape
%
%   % Examples
%
%   % Example 1: 
%   % Create a open cylinder shape, rotate it by 45 degrees about
%   % the z-axis and visualize it.
%   
%   r = shape.OpenCylinder;
%   r = rotateZ(r,45);
%   show(r);
%
%   % Example 2: 
%   % Create a open cylinder shape with radius 2cm and height 2cm, scale it by 
%   % 2 and visualize it
%
%   r = shape.OpenCylinder(Radius = .02,Height = 0.02);
%   [~] = scale(r,2);
%   show(r);
%
%   See also shape.OpenBox, shape.OpenCylinder

%   Copyright 2022-2023 The MathWorks, Inc.

    properties
        % Name of the shape
        Name
        % Center of the open cylinder
        Center
        % Radius of the open cylinder
        Radius
        % Height of the open cylinder
        Height
    end
    
    properties(Hidden)        
        NumSegments
    end
    
    methods
        function obj = OpenCylinder(varargin)
            parserObj = inputParser;
            addParameter(parserObj,'Name','myOpenCylinder');
            addParameter(parserObj,'Center',[0,0,0]);
            addParameter(parserObj,'Radius',1);
            addParameter(parserObj,'Height',2);
           % addParameter(parserObj,'Material',shape.Material);
            addParameter(parserObj,'Metal','PEC');
            addParameter(parserObj,'Dielectric','Air');
            addParameter(parserObj,'Color','y');
            addParameter(parserObj,'Transparency',1);
            addParameter(parserObj,'EdgeColor','k');   
            parse(parserObj,varargin{:});
            obj             = obj@shape.Shape3D();
            obj.Name        = parserObj.Results.Name;
            obj.Center      = parserObj.Results.Center;
            obj.Radius      = parserObj.Results.Radius;
            obj.Height   = parserObj.Results.Height;
            % obj.Material = parserObj.Results.Material;
            obj.Metal = parserObj.Results.Metal;
            obj.Dielectric = parserObj.Results.Dielectric;
            obj.Color = parserObj.Results.Color;
            obj.Transparency = parserObj.Results.Transparency;
            obj.EdgeColor = parserObj.Results.EdgeColor;  
            obj.NumSegments=1;
        end        
    end    
    
    methods
        function set.Name(obj,propVal)
            validateattributes(propVal,{'char','string'},{'nonempty',     ...
                },class(obj),'Name') ;
            if isPropertyChanged(obj,obj.Name,propVal)
                obj.Name = propVal;
            end
        end
        
        function set.Radius(obj,propVal)
            validateattributes(propVal,{'double'},{'nonempty','scalar',     ...
                'nonnan','finite','real','positive'},class(obj),'Radius')
            if isPropertyChanged(obj,obj.Radius,propVal)
                obj.Radius = propVal;
            end
        end
        
        function set.Center(obj,propVal)
            validateattributes(propVal,{'double'},{'nonempty',      ...
                'nonnan','finite','real','numel',3},class(obj),'Center');
            if isPropertyChanged(obj,obj.Center,propVal)
                if ~isrow(propVal)
                    propVal = propVal';
                end
                obj.Center = propVal;
            end
        end
        
        function set.Height(obj,propVal)
            validateattributes(propVal,{'numeric'},{'nonempty','scalar',...
                'nonnan','finite', 'positive'},class(obj),'Height');
            if isPropertyChanged(obj,obj.Height,propVal)
                obj.Height = propVal;
            end
        end

    end
    
    methods(Hidden)
        function createGeometry(obj,varargin)
            % Create a open cylinder by using the cap [1 0]
            opencyl=shape.Cylinder('Cap',[1 0],'Radius',obj.Radius,...
                'Height',obj.Height,'Center',obj.Center);
            createGeometry(opencyl);
            pt = opencyl.Points;
            % Orient the geometry
            pt=orientGeom(obj,pt');
            % Get multiplier
            [~]=em.internal.authoring.getMultiplier(obj,[obj.Radius,obj.Height,obj.Center],1);
            % Assign the points,triangles to the existing object
            obj.Points=pt';obj.Triangles=opencyl.Triangles;
            % generate the triangulation
            warnState = warning('off','MATLAB:triangulation:PtsNotInTriWarnId');
            trnorm=triangulation(obj.Triangles(:,1:3),obj.Points);
            warning(warnState);
            if ~isempty(obj.Thickness)
                trnorm=em.internal.authoring.thicken(trnorm,obj.Thickness);
                obj.Points=trnorm.Points;obj.Triangles=trnorm.ConnectivityList;
            end
            % Get the feature edges
            be=featureEdges(trnorm,pi/3);
            obj.MesherStruct.Geometry.BorderVertices = obj.Points;
            geom = getGeometry(obj);
            saveGeometry(obj,geom.BorderVertices,geom.polygons, ...
                geom.MaxFeatureSize,...
                be);
            resetHasStructureChanged(obj);
        end
        
        function rObj = superLoad(obj,s)
            if nargin>1
                obj.Name            = s.Name;
                obj.Center          = s.Center;
                obj.Radius          = s.Radius;
                obj.Height          = s.Height;
                rObj = superLoadShape(obj,s);
            end            
        end
    end
    
    methods(Access=protected)
        function group = getPropertyGroups(obj)      %#ok<MANU>
            title = '';
            propertyList = {'Name'; 'Center'; 'Radius';
                'Height';...
                 'Metal';'Dielectric';...
             'Color';'Transparency';'EdgeColor'};
            group = matlab.mixin.util.PropertyGroup(propertyList,title);
        end
        function updateShapeDimension(obj, scalingFactor)
            obj.Radius = obj.Radius.*scalingFactor;
            obj.Height = obj.Height.*scalingFactor;
        end
    end 
    
    methods (Static = true,Hidden) % ----LOADOBJ only IMPLEMENTATION ---%
        function r = loadobj(obj)
            if isobject(obj)&& isObjectFromCurrentVersion(obj)
                r = obj;
            else
                % Create default microstrip patch
                newOpenCylinder = shape.OpenCylinder;
                % Populate the properties
                r = superLoad(newOpenCylinder,obj);
            end
            r = loadobj@shape.Shape3D(r);
        end
    end
        
end
