classdef Sphere < shape.Shape3D
%SHAPE.SPHERE Creates a Sphere on the X-Y-Z plane
% 
%   h = shape.Sphere returns a Sphere shape centered at the origin
%   in 3D space.
%
%   h = shape.Sphere('Property1', value1,'Property2', value2,...)
%   returns a Sphere shape, based on the specified properties.
%   Properties you do not specify retain their default values.
%
%   Sphere properties:
%
%   Name        - Name of the Sphere shape
%   Center      - Cartesian coordinates of the center of the Sphere in
%                 meters specified as a 3-element vector. The default value
%                 is [0 0 0].
%   Radius      - Radius of the Sphere shape in meters. The default value is 1
%   Height      - Height of the Sphere 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'.
%
%   Sphere 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 2D or 3D shape
%   createHole    - Create a 2D hole on the custom 2D or 3D 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 Sphere shape, rotate it by 45 degrees about
%   % the z-axis and visualize it.
%   
%   r = shape.Sphere;
%   r = rotateZ(r,45);
%   show(r);
%
%   See also shape.OpenBox, shape.OpenCylinder

%   Copyright 2022-2023 The MathWorks, Inc.

    properties
        % Name of the sphere
        Name
        % Center of the sphere
        Center
        % Radius of the sphere
        Radius
    end

  

    methods
        function obj = Sphere(varargin)
            parserObj = inputParser;
            addParameter(parserObj,'Name','mySphere');
            addParameter(parserObj,'Center',[0,0,0]);
            addParameter(parserObj,'Radius',1);
            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.Metal = parserObj.Results.Metal;
            obj.Dielectric = parserObj.Results.Dielectric;
            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',3},class(obj),'Center');
            if isPropertyChanged(obj,obj.Center,propVal)
                if ~isrow(propVal)
                    propVal = propVal';
                end
                obj.Center = propVal;
            end
        end

    end

    methods(Hidden)
        function createGeometry(obj,varargin)

            % Generate sphere from Geometry Library
            sph_mesh = Geometry.sphere(obj.Radius,string(1),20);
            [pp,tt] = Geometry.extractMesh(sph_mesh);
            tt = tt(:,1:3)+1;
            for i=1:size(tt,1)
                if numel(unique(tt(i,:)))<3
                    error(message('antenna:shapeerrors:SmallDimensions','Dimensions of sphere'));
                end
            end
            % Assign points and triangles
            p = pp';t =tt';
            be = find(p(3,:)==0);
            % Translate the shape based on the center
            p=em.internal.translateshape(p,obj.Center);
            % Orient the geometry
            p=orientGeom(obj,p);
            % Get multiplier
            [~]=em.internal.authoring.getMultiplier(obj,[obj.Radius,obj.Center],1);
            obj.Points=p';obj.Triangles=t(1:3,:)';
            % Create a triangulation and recalculate normals
            trnorm = triangulation(obj.Triangles,obj.Points);
            trnorm = em.internal.authoring.recalculateNormals(trnorm);
            if ~isempty(obj.Thickness)
                trnorm=em.internal.authoring.thicken(trnorm,obj.Thickness);
                obj.Points=trnorm.Points;obj.Triangles=trnorm.ConnectivityList;
            end
            % Maintain the points and triangles internally
            obj.Points=trnorm.Points;obj.Triangles=trnorm.ConnectivityList;
            obj.MesherStruct.Geometry.BorderVertices = obj.Points;
            geom = getGeometry(obj);
            % Save geometry
            saveGeometry(obj,geom.BorderVertices,geom.polygons, ...
                geom.doNotPlot,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;
                rObj = superLoadShape(obj,s);
            end
        end
    end

    methods(Access=protected)
        function group = getPropertyGroups(obj)      %#ok<MANU>
            title = '';
            propertyList = {'Name'; 'Center'; 'Radius';...
                              'Metal';'Dielectric';...
                              '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
                newSphere = shape.Sphere;
                % Populate the properties
                r = superLoad(newSphere,obj);
            end
            r = loadobj@shape.Shape3D(r);
        end
    end

end
