classdef Circle < shape.Shape2D
%SHAPE.CIRCLE Create a circle centered at the origin on the X-Y plane
% 
%   h = shape.Circle returns a circle shape on the X-Y plane
%   centered at the origin.
%
%   h = shape.Circle(Name1=Value1,...,NameN=ValueN) creates a circular shape 
%   with specified properties. Name is the property name and 
%   Value is the corresponding value. Properties that you do 
%   not specify retain their default values.
%
%   Circle properties:
%
%   Name         - Name of the circle. The default name is mycircle.
%   Center       - Cartesian coordinates of the circle center in meters, 
%                  specified as a 2-element vector. The default value 
%                  is [0 0].
%   Radius       - Radius of the circle in meters. The default value is
%                  1.
%   NumPoints    - Number of discretization points on the circumference.
%                  The default value is 30.
%   Metal       - Metal assigned for the shape. The default object is
%                 'PEC' with infinite conductivity and zero
%                 thickness.
%   Color        - Face color of the shape. The default color is yellow ('y').
%   Transparency - Transparency of the shape. The default value is 1.
%   EdgeColor    - Edge Color of the shape. The default color is black ('k').
%
%   Circle 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
%   area          - Area of shape in units of sq.m
%   show          - Visualize the shape as a filled patch
%   plot          - Visualize the shape boundary
%   mesh          - Compute/visualize the mesh of the shape
%   rotate        - Rotate the shape about an axis and an angle
%   rotateX       - Rotate the shape about X-axis with an angle
%   rotateY       - Rotate the shape about Y-axis with an angle
%   rotateZ       - Rotate the shape about Z-axis with an angle
%   translate     - Move the shape to a new location
%   scale         - Scale the size of the shape
%   imprintShape  - Imprint a 2-D shape onto a custom 2-D or 3-D shape
%   createHole    - Create a hole of 2-D shape on custom 2-D or 3-D shape
%   extrude       - Extrude a 2D shape on custom shape
%   extrudeLinear - Extrude a 2D shape linearly
%   extrudeRotate - Extrude a 2D shape by rotation
%   removeFaces   - Remove the selected faces of shape interactively
%
%   % Examples
%
%   % Example 1: 
%   % Create a circle shape of 1 m radius, make a square hole
%   % of side 10 cm at the center and  mesh it at a wavelength of 1m.
%
%   c = shape.Circle;
%   r = shape.Rectangle(Length = 0.1,Width = 0.1);
%   s = c-r;
%   mesh(s,1)
%
%   % Example 2: 
%   % Create a circle shape of 1 m radius, make a circular hole
%   % of radius 10 cm at the center and  mesh it with a maximum edge length
%   % of 20 cm.
%
%   c = shape.Circle;
%   r = shape.Circle(Radius = 0.1);
%   s = c-r;
%   mesh(s,MaxEdgeLength = 2e-1)
%
%   See also shape.Rectangle, shape.Polygon
    
%   Copyright 2017-2023 The MathWorks, Inc.

    properties
        Name
        Center
        Radius
        NumPoints
    end
    
    methods
        function obj = Circle(varargin)
            parserObj = inputParser;
            addParameter(parserObj,'Name','mycircle');
            addParameter(parserObj,'Center',[0,0]);
            addParameter(parserObj,'Radius',1);
            addParameter(parserObj,'NumPoints',30);
            addParameter(parserObj,'Metal','PEC');
            addParameter(parserObj,'Color','y');
            addParameter(parserObj,'Transparency',1);
            addParameter(parserObj,'EdgeColor','k');
            parse(parserObj,varargin{:});
            p =  [1.0000         0         0
                0.5000    0.8660         0
                -0.5000    0.8660         0
                -1.0000         0         0
                -0.5000   -0.8660         0
                0.5000   -0.8660         0]';   
            obj             = obj@shape.Shape2D(p');
            obj.Name        = parserObj.Results.Name;
            obj.Center      = parserObj.Results.Center;
            obj.Radius      = parserObj.Results.Radius;
            obj.NumPoints   = parserObj.Results.NumPoints;
            obj.Metal = parserObj.Results.Metal;
            obj.Color = parserObj.Results.Color;
            obj.Transparency = parserObj.Results.Transparency;
            obj.EdgeColor = parserObj.Results.EdgeColor;
        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',2},class(obj),'Center');
            if isPropertyChanged(obj,obj.Center,propVal)
                if ~isrow(propVal)
                    propVal = propVal';
                end
                obj.Center = propVal;
            end
        end
        
        function set.NumPoints(obj,propVal)
            validateattributes(propVal,{'numeric'},{'nonempty','scalar',...
                'nonnan','finite', 'real','integer','positive','>=',3},class(obj),'NumPoints');
            if isPropertyChanged(obj,obj.NumPoints,propVal)
                obj.NumPoints = propVal;
            end
        end        
    end
    
    methods(Hidden)
        function createGeometry(obj)
            p = shape.generateCirclePoints(obj.Center,obj.Radius,   ...
                obj.NumPoints);
            [~]=em.internal.authoring.getMultiplier(obj,[obj.Radius,obj.Center],1);
            p = orientGeom(obj,p);
            updateShape(obj,p')
            obj.MesherStruct.Geometry.BorderVertices = p';
            resetHasStructureChanged(obj);
        end
        
        function rObj = superLoad(obj,s)
            if nargin>1
                obj.Name            = s.Name;
                obj.Center          = s.Center;
                obj.Radius          = s.Radius;
                obj.NumPoints       = s.NumPoints;
                rObj = superLoadShape(obj,s);
            end            
        end
    end
    
    methods(Access=protected)
        function group = getPropertyGroups(obj)      %#ok<MANU>
            title = '';
            propertyList = {'Name'; 'Center'; 'Radius';...
                'NumPoints';
                 'Metal';...
               'Color';'Transparency';'EdgeColor'};
            group = matlab.mixin.util.PropertyGroup(propertyList,title);
        end
        function updateShapeDimension(obj, scalingFactor)
            obj.Radius = obj.Radius.*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
                newcircle = shape.Circle;
                % Populate the properties
                r = superLoad(newcircle,obj);
            end
            r = loadobj@shape.Shape2D(r);
        end
    end
        
end
