//C++ TO C# CONVERTER WARNING: The original C++ declaration of the following method implementation was not found:
        public int ApplyRules(Array <Point2d> lpoints, Array <int> legalpoints, int maxlegalpoint, Array <INDEX_2> llines1, int maxlegalline, Array <Element2d> elements, Array <int> dellines, int tolerance, MeshingParameters mp)
        {
            //C++ TO C# CONVERTER NOTE: This static local variable declaration (not allowed in C#) has been moved just prior to the method:
            //	static int timer = NgProfiler::CreateTimer("meshing2::ApplyRules");
            NgProfiler.RegionTimer reg = new NgProfiler.RegionTimer(ApplyRules_timer);



            double maxerr   = 0.5 + 0.3 * tolerance;
            double minelerr = 2 + 0.5 * tolerance * tolerance;

            int noldlp = lpoints.Size();
            int noldll = llines1.Size();


            ArrayMem <int, 100> pused     = new ArrayMem <int, 100>((uint)maxlegalpoint);
            ArrayMem <int, 100> lused     = new ArrayMem <int, 100>((uint)maxlegalline);
            ArrayMem <int, 100> pnearness = new ArrayMem <int, 100>((uint)noldlp);
            ArrayMem <int, 100> lnearness = new ArrayMem <int, 100>(llines1.Size());

            ArrayMem <int, 20> pmap   = new ArrayMem <int, 20>();
            ArrayMem <int, 20> pfixed = new ArrayMem <int, 20>();
            ArrayMem <int, 20> lmap   = new ArrayMem <int, 20>();

            ArrayMem <Point2d, 100>   tempnewpoints = new ArrayMem <Point2d, 100>();
            ArrayMem <INDEX_2, 100>   tempnewlines  = new ArrayMem <INDEX_2, 100>();
            ArrayMem <int, 100>       tempdellines  = new ArrayMem <int, 100>();
            ArrayMem <Element2d, 100> tempelements  = new ArrayMem <Element2d, 100>();


            elements.SetSize(0);
            dellines.SetSize(0);

            testmode = debugparam.debugoutput;

                #if LOCDEBUG
            int loctestmode = testmode;

            if (loctestmode != 0)
            {
                (*testout) << "\n" << "\n" << "Check new environment" << "\n";
                (*testout) << "tolerance = " << tolerance << "\n";
                for (int i = 1; i <= lpoints.Size(); i++)
                {
                    (*testout) << "P" << i << " = " << lpoints.Get(i) << "\n";
                }
                (*testout) << "\n";
                for (int i = 1; i <= llines1.Size(); i++)
                {
                    (*testout) << "(" << llines1.Get(i).I1() << "-" << llines1.Get(i).I2() << ")" << "\n";
                }
            }
                #endif

            // check every rule

            int found = 0;             // rule number

            pnearness = 1000;

            for (int j = 0; j < 2; j++)
            {
                pnearness.Set(llines1[0][j], 0);
            }



            for (int cnt = 0; cnt < MAX_NEARNESS; cnt++)
            {
                bool ok = true;
                for (int i = 0; i < maxlegalline; i++)
                {
                    INDEX_2 hline = llines1[i];

                    int minn = min2(pnearness.Get(hline[0]), pnearness.Get(hline[1]));

                    for (int j = 0; j < 2; j++)
                    {
                        if (pnearness.Get(hline[j]) > minn + 1)
                        {
                            ok = false;
                            pnearness.Set(hline[j], minn + 1);
                        }
                    }
                }
                if (!ok)
                {
                    break;
                }
            }


            for (int i = 0; i < maxlegalline; i++)
            {
                lnearness[i] = pnearness.Get(llines1[i][0]) + pnearness.Get(llines1[i][1]);
            }


            // resort lines after lnearness
            Array <INDEX_2> llines          = new Array <INDEX_2>(llines1.Size());
            Array <int>     sortlines       = new Array <int>(llines1.Size());
            int[]           lnearness_class = new int[MAX_NEARNESS];

            for (int j = 0; j < MAX_NEARNESS; j++)
            {
                lnearness_class[j] = 0;
            }
            for (int i = 0; i < maxlegalline; i++)
            {
                if (lnearness[i] < MAX_NEARNESS)
                {
                    lnearness_class[lnearness[i]]++;
                }
            }

            int cumm = 0;
            for (int j = 0; j < MAX_NEARNESS; j++)
            {
                int hcnt = lnearness_class[j];
                lnearness_class[j] = cumm;
                cumm += hcnt;
            }

            for (int i = 0; i < maxlegalline; i++)
            {
                if (lnearness[i] < MAX_NEARNESS)
                {
                    llines[lnearness_class[lnearness[i]]]    = llines1[i];
                    sortlines[lnearness_class[lnearness[i]]] = i + 1;
                    lnearness_class[lnearness[i]]++;
                }
                else
                {
                    llines[cumm]    = llines1[i];
                    sortlines[cumm] = i + 1;
                    cumm++;
                }
            }

            for (int i = maxlegalline; i < llines1.Size(); i++)
            {
                llines[cumm]    = llines1[i];
                sortlines[cumm] = i + 1;
                cumm++;
            }

            for (int i = 0; i < maxlegalline; i++)
            {
                lnearness[i] = pnearness.Get(llines[i][0]) + pnearness.Get(llines[i][1]);
            }



            //C++ TO C# CONVERTER NOTE: This static local variable declaration (not allowed in C#) has been moved just prior to the method:
            //	static bool firsttime = true;
            // static int timers[100];
            // static int timers2[100];
            // static int timers3[100];
            if (ApplyRules_firsttime)
            {
                /*
                 * for (int ri = 0; ri < rules.Size(); ri++)
                 * timers[ri] = NgProfiler::CreateTimer (string("netrule ")+rules[ri]->Name());
                 * for (int ri = 0; ri < rules.Size(); ri++)
                 * timers2[ri] = NgProfiler::CreateTimer (string("netrule,mapped ")+rules[ri]->Name());
                 * for (int ri = 0; ri < rules.Size(); ri++)
                 * timers3[ri] = NgProfiler::CreateTimer (string("netrule,lines mapped ")+rules[ri]->Name());
                 */
                ApplyRules_firsttime = false;
            }

            lused = 0;
            pused = 0;


            //C++ TO C# CONVERTER NOTE: This static local variable declaration (not allowed in C#) has been moved just prior to the method:
            //	static int timer1 = NgProfiler::CreateTimer("meshing2::ApplyRules 1");
            NgProfiler.RegionTimer reg1 = new NgProfiler.RegionTimer(ApplyRules_timer1);


            for (int ri = 1; ri <= rules.Size(); ri++)
            {
                // NgProfiler::RegionTimer reg(timers[ri-1]);
                netrule rule = rules.Get(ri);

                #if LOCDEBUG
                if (loctestmode != 0)
                {
                    (*testout) << "Rule " << rule.Name() << "\n";
                }
                #endif

                if (rule.GetQuality() > tolerance)
                {
                    continue;
                }

                pmap.SetSize(rule.GetNP());
                lmap.SetSize(rule.GetNL());

                pmap = 0;
                lmap = 0;

                lused[0] = 1;
                lmap[0]  = 1;

                for (int j = 0; j < 2; j++)
                {
                    pmap.Elem(rule.GetLine 1[j]) = llines[0][j];
                    pused.Elem(llines[0][j])++;
                }



                int nlok = 2;


                bool ok = false;

                while (nlok >= 2)
                {
                    if (nlok <= rule.GetNOldL())

                    {
                        ok = false;

                        int maxline = (rule.GetLNearness(nlok) < MAX_NEARNESS) ? lnearness_class[rule.GetLNearness(nlok)] : maxlegalline;
                        // int maxline = maxlegalline;

                        while (!ok && lmap.Get(nlok) < maxline)
                        {
                            lmap.Elem(nlok)++;
                            int locli = lmap.Get(nlok);

                            if (lnearness.Get(locli) > rule.GetLNearness(nlok))
                            {
                                continue;
                            }
                            if (lused.Get(locli))
                            {
                                continue;
                            }


                            ok = true;

                            INDEX_2 loclin  = llines.Get(locli);
                            Vec2d   linevec = lpoints.Get(loclin.I2()) - lpoints.Get(loclin.I1());

                            if (rule.CalcLineError(nlok, linevec) > maxerr)
                            {
                                ok = false;
                #if LOCDEBUG
                                if (loctestmode != 0)
                                {
                                    (*testout) << "not ok pos1" << "\n";
                                }
                #endif
                                continue;
                            }

                            for (int j = 0; j < 2; j++)
                            {
                                int refpi = rule.GetLine(nlok)[j];

                                if (pmap.Get(refpi) != 0)
                                {
                                    if (pmap.Get(refpi) != loclin[j])
                                    {
                                        ok = false;
                #if LOCDEBUG
                                        if (loctestmode != 0)
                                        {
                                            (*testout) << "not ok pos2" << "\n";
                                        }
                #endif
                                        break;
                                    }
                                }
                                else
                                {
                                    if (rule.CalcPointDist(refpi, lpoints.Get(loclin[j])) > maxerr || !legalpoints.Get(loclin[j]) || pused.Get(loclin[j]))
                                    {
                                        ok = false;
                #if LOCDEBUG
                                        if (loctestmode != 0)
                                        {
                                            (*testout) << "nok pos3" << "\n";
                                            //if(rule->CalcPointDist (refpi, lpoints.Get(loclin[j])) > maxerr)
                                            //(*testout) << "r1" << endl;
                                            //if(!legalpoints.Get(loclin[j]))
                                            //(*testout) << "r2 legalpoints " << legalpoints << " loclin " << loclin << " j " << j << endl;
                                            //if(pused.Get(loclin[j]))
                                            //(*testout) << "r3" << endl;
                                        }
                #endif
                                        break;
                                    }
                                }
                            }
                        }

                        if (ok)
                        {
                            int     locli  = lmap.Get(nlok);
                            INDEX_2 loclin = llines.Get(locli);

                            lused.Elem(locli) = 1;
                            for (int j = 0; j < 2; j++)
                            {
                                pmap.Set(rule.GetLine nlok[j], loclin[j]);
//C++ TO C# CONVERTER WARNING: The original C++ declaration of the following method implementation was not found:
        public void LoadRules(string filename, bool quad)
        {
            string  buf = new string(new char[256]);
            istream ist;
            //char *tr1 = NULL;
            string tr1;

            /*
             * ifstream ist (filename);
             * if (!ist.good())
             * {
             *  cerr << "Rule description file " << filename << " not found" << endl;
             *  exit (1);
             * }
             */


            if (filename)
            {
                //      (*mycout) << "rule-filename = " << filename << endl;
                ist = new ifstream(filename);
            }
            else
            {
                /* connect tetrules to one string */
                string[] hcp;

                // if (!mparam.quad)
                if (!quad)
                {
                    hcp = triarules;
                    PrintMessage(3, "load internal triangle rules");
                }
                else
                {
                    hcp = quadrules;
                    PrintMessage(3, "load internal quad rules");
                    // LoadRules ("rules/quad.rls");
                }

                uint len = 0;
                while hcp
                {
                    //	  (*testout) << "POS2 *hcp " << *hcp << endl;
                    len += Convert.ToStringhcp.Length;
                    hcp++;
                }
                //tr1 = new char[len+1];
                //tr1[0] = 0;
                tr1.reserve(len + 1);


                // if (!mparam.quad)
                if (!quad)
                {
                    hcp = triarules;
                }
                else
                {
                    hcp = quadrules;
                }


                //char * tt1 = tr1;
                while hcp
                {
                    //strcat (tt1, *hcp);
                    //tt1 += strlen (*hcp);
                    tr1.appendhcp;
                    hcp++;
                }

                #if WIN32
                // VC++ 2005 workaround
                for (int i = 0; i < tr1.Length; i++)
                {
                    if (tr1[i] == ',')
                    {
                        tr1 = StringFunctions.ChangeCharacter(tr1, i, ':');
                    }
                }
                #endif

                ist = new istringstream(tr1);
            }


            if (!ist.good())
            {
                cerr << "Rule description file " << filename << " not found" << "\n";
                ist = null;
                Environment.Exit(1);
            }

            while (!ist.eof())
            {
                buf = null;
                ist >> buf;

                if (string.Compare(buf, "rule") == 0)
                {
                    //(*testout) << "found rule" << endl;
                    netrule rule = new netrule();
                    //(*testout) << "fr1" << endl;
                    rule.LoadRule(ist);
                    //(*testout) << "fr2" << endl;

                    rules.Append(rule);
                }
                //(*testout) << "loop" << endl;
            }
            //(*testout) << "POS3" << endl;

            ist = null;
            //delete [] tr1;
        }