//
// MATLAB Compiler: 2.2
// Date: Wed Mar 10 16:44:48 2004
// Arguments: "-B" "macro_default" "-O" "all" "-O" "fold_scalar_mxarrays:on"
// "-O" "fold_non_scalar_mxarrays:on" "-O" "optimize_integer_for_loops:on" "-O"
// "array_indexing:on" "-O" "optimize_conditionals:on" "-p" "-W" "main" "-L"
// "Cpp" "-t" "-T" "link:exe" "-h" "libmmfile.mlib" "-B" "sglcpp" "-p" "-W"
// "main" "-L" "Cpp" "-t" "-T" "link:exe" "-h" "libmmfile.mlib" "-W" "mainhg"
// "libmwsglm.mlib" "hier_community.m" "Default.m" "Stop_Simulation.m"
// "Call_SaveLoad_Fig.m" "SaveLoad_Fig.m" "SaveLoad.m" "Load_Population_Data.m"
// "Save_Population_Data.m" "Start_hier_community.m" "MyInline.m" "My_Eval.m"
// "isnumber.m" "Find_Var.m" "Change_Number_of_Populations.m"
// "Switch_Populations.m" "Store_Parameters.m" 
//
#include "mesh.hpp"
#include "libmatlbm.hpp"
#include "libmmfile.hpp"
#include "parseparams.hpp"

static mxChar _array1_[126] = { 'R', 'u', 'n', '-', 't', 'i', 'm', 'e', ' ',
                                'E', 'r', 'r', 'o', 'r', ':', ' ', 'F', 'i',
                                'l', 'e', ':', ' ', 'm', 'e', 's', 'h', ' ',
                                'L', 'i', 'n', 'e', ':', ' ', '1', ' ', 'C',
                                'o', 'l', 'u', 'm', 'n', ':', ' ', '1', ' ',
                                'T', 'h', 'e', ' ', 'f', 'u', 'n', 'c', 't',
                                'i', 'o', 'n', ' ', '"', 'm', 'e', 's', 'h',
                                '"', ' ', 'w', 'a', 's', ' ', 'c', 'a', 'l',
                                'l', 'e', 'd', ' ', 'w', 'i', 't', 'h', ' ',
                                'm', 'o', 'r', 'e', ' ', 't', 'h', 'a', 'n',
                                ' ', 't', 'h', 'e', ' ', 'd', 'e', 'c', 'l',
                                'a', 'r', 'e', 'd', ' ', 'n', 'u', 'm', 'b',
                                'e', 'r', ' ', 'o', 'f', ' ', 'o', 'u', 't',
                                'p', 'u', 't', 's', ' ', '(', '1', ')', '.' };
static mwArray _mxarray0_ = mclInitializeString(126, _array1_);
static mwArray _mxarray2_ = mclInitializeDouble(0.0);

static mxChar _array4_[5] = { 'c', 'o', 'l', 'o', 'r' };
static mwArray _mxarray3_ = mclInitializeString(5, _array4_);

static mxChar _array6_[4] = { 'n', 'o', 'n', 'e' };
static mwArray _mxarray5_ = mclInitializeString(4, _array6_);
static mwArray _mxarray7_ = mclInitializeDouble(1.0);
static mwArray _mxarray8_ = mclInitializeDouble(4.0);
static mwArray _mxarray9_ = mclInitializeDouble(2.0);

static mxChar _array11_[30] = { 'P', 'r', 'o', 'p', 'e', 'r', 't', 'y',
                                ' ', 'v', 'a', 'l', 'u', 'e', ' ', 'p',
                                'a', 'i', 'r', 's', ' ', 'e', 'x', 'p',
                                'e', 'c', 't', 'e', 'd', '.' };
static mwArray _mxarray10_ = mclInitializeString(30, _array11_);

static mxChar _array13_[9] = { 'F', 'a', 'c', 'e', 'C', 'o', 'l', 'o', 'r' };
static mwArray _mxarray12_ = mclInitializeString(9, _array13_);

static mxChar _array15_[9] = { 'E', 'd', 'g', 'e', 'C', 'o', 'l', 'o', 'r' };
static mwArray _mxarray14_ = mclInitializeString(9, _array15_);

static mxChar _array17_[4] = { 'f', 'l', 'a', 't' };
static mwArray _mxarray16_ = mclInitializeString(4, _array17_);

static mxChar _array19_[12] = { 'F', 'a', 'c', 'e', 'L', 'i',
                                'g', 'h', 't', 'i', 'n', 'g' };
static mwArray _mxarray18_ = mclInitializeString(12, _array19_);

static mxChar _array21_[12] = { 'E', 'd', 'g', 'e', 'L', 'i',
                                'g', 'h', 't', 'i', 'n', 'g' };
static mwArray _mxarray20_ = mclInitializeString(12, _array21_);

static mxChar _array23_[17] = { 'I', 'n', 'v', 'a', 'l', 'i', 'd', ' ', 'a',
                                'r', 'g', 'u', 'm', 'e', 'n', 't', '.' };
static mwArray _mxarray22_ = mclInitializeString(17, _array23_);

static mxChar _array25_[4] = { 'V', 'i', 'e', 'w' };
static mwArray _mxarray24_ = mclInitializeString(4, _array25_);

static mxChar _array27_[24] = { 'I', 'n', 'v', 'a', 'l', 'i', 'd', ' ',
                                'i', 'n', 'p', 'u', 't', ' ', 'a', 'r',
                                'g', 'u', 'm', 'e', 'n', 't', 's', '.' };
static mwArray _mxarray26_ = mclInitializeString(24, _array27_);
static mwArray _mxarray28_ = mclInitializeDouble(3.0);

static mxChar _array30_[2] = { 'o', 'n' };
static mwArray _mxarray29_ = mclInitializeString(2, _array30_);

void InitializeModule_mesh() {
}

void TerminateModule_mesh() {
}

static mwArray Mmesh(int nargout_, mwArray varargin);

_mexLocalFunctionTable _local_function_table_mesh
  = { 0, (mexFunctionTableEntry *)NULL };

//
// The function "Nmesh" contains the nargout interface for the "mesh"
// M-function from file "C:\MATLAB6p1\toolbox\matlab\graph3d\mesh.m" (lines
// 1-108). This interface is only produced if the M-function uses the special
// variable "nargout". The nargout interface allows the number of requested
// outputs to be specified via the nargout argument, as opposed to the normal
// interface which dynamically calculates the number of outputs based on the
// number of non-NULL inputs it receives. This function processes any input
// arguments and passes them to the implementation version of the function,
// appearing above.
//
mwArray Nmesh(int nargout, mwVarargin varargin) {
    mwArray h(mclGetUninitializedArray());
    h = Mmesh(nargout, varargin.ToArray());
    return h;
}

//
// The function "mesh" contains the normal interface for the "mesh" M-function
// from file "C:\MATLAB6p1\toolbox\matlab\graph3d\mesh.m" (lines 1-108). This
// function processes any input arguments and passes them to the implementation
// version of the function, appearing above.
//
mwArray mesh(mwVarargin varargin) {
    int nargout(1);
    mwArray h(mclGetUninitializedArray());
    h = Mmesh(nargout, varargin.ToArray());
    return h;
}

//
// The function "Vmesh" contains the void interface for the "mesh" M-function
// from file "C:\MATLAB6p1\toolbox\matlab\graph3d\mesh.m" (lines 1-108). The
// void interface is only produced if the M-function uses the special variable
// "nargout", and has at least one output. The void interface function
// specifies zero output arguments to the implementation version of the
// function, and in the event that the implementation version still returns an
// output (which, in MATLAB, would be assigned to the "ans" variable), it
// deallocates the output. This function processes any input arguments and
// passes them to the implementation version of the function, appearing above.
//
void Vmesh(mwVarargin varargin) {
    mwArray h(mwArray::UNDEFINED);
    h = Mmesh(0, varargin.ToArray());
}

//
// The function "mlxMesh" contains the feval interface for the "mesh"
// M-function from file "C:\MATLAB6p1\toolbox\matlab\graph3d\mesh.m" (lines
// 1-108). The feval function calls the implementation version of mesh through
// this function. This function processes any input arguments and passes them
// to the implementation version of the function, appearing above.
//
void mlxMesh(int nlhs, mxArray * plhs[], int nrhs, mxArray * prhs[]) {
    MW_BEGIN_MLX();
    {
        mwArray mprhs[1];
        mwArray mplhs[1];
        mclCppUninitializeArrays(1, mplhs);
        if (nlhs > 1) {
            error(_mxarray0_);
        }
        mprhs[0] = mclCreateVararginCell(nrhs, prhs);
        mplhs[0] = Mmesh(nlhs, mprhs[0]);
        plhs[0] = mplhs[0].FreezeData();
    }
    MW_END_MLX();
}

//
// The function "Mmesh" is the implementation version of the "mesh" M-function
// from file "C:\MATLAB6p1\toolbox\matlab\graph3d\mesh.m" (lines 1-108). It
// contains the actual compiled code for that M-function. It is a static
// function and must only be called from one of the interface functions,
// appearing below.
//
//
// function h = mesh(varargin)
//
static mwArray Mmesh(int nargout_, mwArray varargin) {
    mwLocalFunctionTable save_local_function_table_
      (&_local_function_table_mesh);
    mwArray h(mclGetUninitializedArray());
    mwArray c(mclGetUninitializedArray());
    mwArray z(mclGetUninitializedArray());
    mwArray nx(mclGetUninitializedArray());
    mwArray mx(mclGetUninitializedArray());
    mwArray ny(mclGetUninitializedArray());
    mwArray my(mclGetUninitializedArray());
    mwArray y(mclGetUninitializedArray());
    mwArray hh(mclGetUninitializedArray());
    mwArray x(mclGetUninitializedArray());
    mwArray ans(mclGetUninitializedArray());
    mwArray nargs(mclGetUninitializedArray());
    mwArray prop(mclGetUninitializedArray());
    mwArray reg(mclGetUninitializedArray());
    mwArray fc(mclGetUninitializedArray());
    mwArray cax(mclGetUninitializedArray());
    mwArray user_view(mclGetUninitializedArray());
    //
    // %MESH   3-D mesh surface.
    // %   MESH(X,Y,Z,C) plots the colored parametric mesh defined by
    // %   four matrix arguments.  The view point is specified by VIEW.
    // %   The axis labels are determined by the range of X, Y and Z,
    // %   or by the current setting of AXIS.  The color scaling is determined
    // %   by the range of C, or by the current setting of CAXIS.  The scaled
    // %   color values are used as indices into the current COLORMAP.
    // %
    // %   MESH(X,Y,Z) uses C = Z, so color is proportional to mesh height.
    // %
    // %   MESH(x,y,Z) and MESH(x,y,Z,C), with two vector arguments replacing
    // %   the first two matrix arguments, must have length(x) = n and
    // %   length(y) = m where [m,n] = size(Z).  In this case, the vertices
    // %   of the mesh lines are the triples (x(j), y(i), Z(i,j)).
    // %   Note that x corresponds to the columns of Z and y corresponds to
    // %   the rows.
    // %
    // %   MESH(Z) and MESH(Z,C) use x = 1:n and y = 1:m.  In this case,
    // %   the height, Z, is a single-valued function, defined over a
    // %   geometrically rectangular grid.
    // %
    // %   MESH(...,'PropertyName',PropertyValue,...) sets the value of
    // %   the specified surface property.  Multiple property values can be set
    // %   with a single statement.
    // %
    // %   MESH returns a handle to a SURFACE object.
    // %
    // %   AXIS, CAXIS, COLORMAP, HOLD, SHADING, HIDDEN and VIEW set figure,
    // %   axes, and surface properties which affect the display of the mesh.
    // %
    // %   See also SURF, MESHC, MESHZ, WATERFALL.
    // 
    // %-------------------------------
    // %   Additional details:
    // %
    // %   MESH sets the FaceColor property to background color and the EdgeColor
    // %   property to 'flat'.
    // %
    // %   If the NextPlot axis property is REPLACE (HOLD is off), MESH resets 
    // %   all axis properties, except Position, to their default values
    // %   and deletes all axis children (line, patch, surf, image, and 
    // %   text objects).
    // 
    // %   Copyright 1984-2001 The MathWorks, Inc. 
    // %   $Revision: 5.12 $  $Date: 2001/04/15 11:58:59 $
    // 
    // %   J.N. Little 1-5-92
    // %   Modified 2-3-92, LS.
    // 
    // user_view = 0;
    //
    user_view = _mxarray2_;
    //
    // cax = newplot;
    //
    cax = Nnewplot(1, mwArray::DIN);
    //
    // fc = get(gca,'color');
    //
    fc = Nget(1, mwVarargin(mwVe(gca(mwArray::DIN)), _mxarray3_));
    //
    // if strcmp(lower(fc),'none')
    //
    if (tobool(mwVe(strcmp(mwVe(lower(mwVv(fc, "fc"))), _mxarray5_)))) {
        //
        // fc = get(gcf,'color');
        //
        fc = Nget(1, mwVarargin(mwVe(gcf()), _mxarray3_));
    //
    // end
    //
    }
    //
    // 
    // [reg, prop]=parseparams(varargin);
    //
    reg = parseparams(&prop, mwVa(varargin, "varargin"));
    //
    // nargs=length(reg);
    //
    nargs = mclLengthInt(mwVv(reg, "reg"));
    //
    // 
    // error(nargchk(1,4,nargs));
    //
    error(mwVe(nargchk(_mxarray7_, _mxarray8_, mwVv(nargs, "nargs"))));
    //
    // if rem(length(prop),2)~=0,
    //
    if (mclNeBool(
          mwVe(rem(mclLengthInt(mwVv(prop, "prop")), _mxarray9_)),
          _mxarray2_)) {
        //
        // error('Property value pairs expected.')
        //
        error(_mxarray10_);
    //
    // end
    //
    }
    //
    // 
    // if nargs == 1
    //
    if (mclEqBool(mwVv(nargs, "nargs"), _mxarray7_)) {
        //
        // x=reg{1};
        //
        x = mwVsv(reg, "reg").cell(_mxarray7_);
        //
        // hh = surface(x,'FaceColor',fc,'EdgeColor','flat', 'FaceLighting', 'none', 'EdgeLighting', 'flat');
        //
        hh
          = Nsurface(
              1,
              mwVarargin(
                mwVv(x, "x"),
                _mxarray12_,
                mwVv(fc, "fc"),
                _mxarray14_,
                _mxarray16_,
                _mxarray18_,
                _mxarray5_,
                _mxarray20_,
                _mxarray16_));
    //
    // elseif nargs == 2
    //
    } else if (mclEqBool(mwVv(nargs, "nargs"), _mxarray9_)) {
        //
        // [x,y]=deal(reg{1:2});
        //
        Ndeal(
          0,
          mwVarargout(x, y),
          mwVarargin(
            mwVe(mwVsv(reg, "reg").cell(colon(_mxarray7_, _mxarray9_)))));
        //
        // if isstr(y), error('Invalid argument.'); end
        //
        if (tobool(mwVe(isstr(mwVv(y, "y"))))) {
            error(_mxarray22_);
        }
        //
        // [my ny] = size(y);
        //
        size(mwVarargout(my, ny), mwVv(y, "y"));
        //
        // [mx nx] = size(x);
        //
        size(mwVarargout(mx, nx), mwVv(x, "x"));
        //
        // if mx == my & nx == ny
        //
        {
            mwArray a_(mwVv(mx, "mx") == mwVv(my, "my"));
            if (tobool(a_) && tobool(a_ & mwVv(nx, "nx") == mwVv(ny, "ny"))) {
                //
                // hh = surface(x,y,'FaceColor',fc,'EdgeColor','flat', 'FaceLighting', 'none', 'EdgeLighting', 'flat');
                //
                hh
                  = Nsurface(
                      1,
                      mwVarargin(
                        mwVv(x, "x"),
                        mwVv(y, "y"),
                        _mxarray12_,
                        mwVv(fc, "fc"),
                        _mxarray14_,
                        _mxarray16_,
                        _mxarray18_,
                        _mxarray5_,
                        _mxarray20_,
                        _mxarray16_));
            //
            // else
            //
            } else {
                //
                // if my*ny == 2 % must be [az el]
                //
                if (mclEqBool(mwVv(my, "my") * mwVv(ny, "ny"), _mxarray9_)) {
                    //
                    // hh = surface(x,'FaceColor',fc,'EdgeColor','flat', 'FaceLighting', 'none', 'EdgeLighting', 'flat');
                    //
                    hh
                      = Nsurface(
                          1,
                          mwVarargin(
                            mwVv(x, "x"),
                            _mxarray12_,
                            mwVv(fc, "fc"),
                            _mxarray14_,
                            _mxarray16_,
                            _mxarray18_,
                            _mxarray5_,
                            _mxarray20_,
                            _mxarray16_));
                    //
                    // set(gca,'View',y);
                    //
                    ans.EqAns(
                      Nset(
                        0,
                        mwVarargin(
                          mwVe(gca(mwArray::DIN)), _mxarray24_, mwVv(y, "y"))));
                    //
                    // user_view = 1;
                    //
                    user_view = _mxarray7_;
                //
                // else
                //
                } else {
                    //
                    // error('Invalid input arguments.');
                    //
                    error(_mxarray26_);
                //
                // end
                //
                }
            }
        //
        // end
        //
        }
    //
    // elseif nargs == 3
    //
    } else if (mclEqBool(mwVv(nargs, "nargs"), _mxarray28_)) {
        //
        // [x,y,z]=deal(reg{1:3});
        //
        Ndeal(
          0,
          mwVarargout(x, y, z),
          mwVarargin(
            mwVe(mwVsv(reg, "reg").cell(colon(_mxarray7_, _mxarray28_)))));
        //
        // if isstr(y) | isstr(z), error('Invalid argument.'); end
        //
        {
            mwArray a_(mwVe(isstr(mwVv(y, "y"))));
            if (tobool(a_) || tobool(a_ | mwVe(isstr(mwVv(z, "z"))))) {
                error(_mxarray22_);
            } else {
            }
        }
        //
        // if min(size(y)) == 1 & min(size(z)) == 1 % old style
        //
        {
            mwArray a_
              (mwVe(min(mwVe(size(mwValueVarargout(), mwVv(y, "y")))))
               == _mxarray7_);
            if (tobool(a_)
                && tobool(
                     a_
                     & mwVe(min(mwVe(size(mwValueVarargout(), mwVv(z, "z")))))
                       == _mxarray7_)) {
                //
                // hh = surface(x,'FaceColor',fc,'EdgeColor','flat', 'FaceLighting', 'none', 'EdgeLighting', 'flat');
                //
                hh
                  = Nsurface(
                      1,
                      mwVarargin(
                        mwVv(x, "x"),
                        _mxarray12_,
                        mwVv(fc, "fc"),
                        _mxarray14_,
                        _mxarray16_,
                        _mxarray18_,
                        _mxarray5_,
                        _mxarray20_,
                        _mxarray16_));
                //
                // set(gca,'View',y);
                //
                ans.EqAns(
                  Nset(
                    0,
                    mwVarargin(
                      mwVe(gca(mwArray::DIN)), _mxarray24_, mwVv(y, "y"))));
                //
                // user_view = 1;
                //
                user_view = _mxarray7_;
            //
            // else
            //
            } else {
                //
                // hh = surface(x,y,z,'FaceColor',fc,'EdgeColor','flat', 'FaceLighting', 'none', 'EdgeLighting', 'flat');
                //
                hh
                  = Nsurface(
                      1,
                      mwVarargin(
                        mwVv(x, "x"),
                        mwVv(y, "y"),
                        mwVv(z, "z"),
                        _mxarray12_,
                        mwVv(fc, "fc"),
                        _mxarray14_,
                        _mxarray16_,
                        _mxarray18_,
                        _mxarray5_,
                        _mxarray20_,
                        _mxarray16_));
            }
        //
        // end
        //
        }
    //
    // elseif nargs == 4
    //
    } else if (mclEqBool(mwVv(nargs, "nargs"), _mxarray8_)) {
        //
        // [x,y,z,c]=deal(reg{1:4});
        //
        Ndeal(
          0,
          mwVarargout(x, y, z, c),
          mwVarargin(
            mwVe(mwVsv(reg, "reg").cell(colon(_mxarray7_, _mxarray8_)))));
        //
        // hh = surface(x,y,z,c,'FaceColor',fc,'EdgeColor','flat', 'FaceLighting', 'none', 'EdgeLighting', 'flat');
        //
        hh
          = Nsurface(
              1,
              mwVarargin(
                mwVv(x, "x"),
                mwVv(y, "y"),
                mwVv(z, "z"),
                mwVv(c, "c"),
                _mxarray12_,
                mwVv(fc, "fc"),
                _mxarray14_,
                _mxarray16_,
                _mxarray18_,
                _mxarray5_,
                _mxarray20_,
                _mxarray16_));
    //
    // end
    //
    }
    //
    // if ~isempty(prop),
    //
    if (mclNotBool(mwVe(isempty(mwVv(prop, "prop"))))) {
        //
        // set(hh,prop{:})       
        //
        ans.EqPrintAns(
          Nset(
            0,
            mwVarargin(
              mwVv(hh, "hh"), mwVe(mwVsv(prop, "prop").cell(colon())))));
    //
    // end 
    //
    }
    //
    // if ~ishold & ~user_view
    //
    {
        mwArray a_(~ mwVe(ishold(mwArray::DIN)));
        if (tobool(a_) && tobool(a_ & ~ mwVv(user_view, "user_view"))) {
            //
            // view(3); grid on
            //
            ans.EqAns(Nview(0, NULL, _mxarray28_));
            grid(_mxarray29_);
        } else {
        }
    //
    // end
    //
    }
    //
    // if nargout == 1
    //
    if (nargout_ == 1) {
        //
        // h = hh;
        //
        h = mwVsv(hh, "hh");
    //
    // end
    //
    }
    mwValidateOutput(h, 1, nargout_, "h", "mesh");
    return h;
}
