CryptStatistics.cpp

00001 /*
00002 
00003 Copyright (C) University of Oxford, 2005-2010
00004 
00005 University of Oxford means the Chancellor, Masters and Scholars of the
00006 University of Oxford, having an administrative office at Wellington
00007 Square, Oxford OX1 2JD, UK.
00008 
00009 This file is part of Chaste.
00010 
00011 Chaste is free software: you can redistribute it and/or modify it
00012 under the terms of the GNU Lesser General Public License as published
00013 by the Free Software Foundation, either version 2.1 of the License, or
00014 (at your option) any later version.
00015 
00016 Chaste is distributed in the hope that it will be useful, but WITHOUT
00017 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
00019 License for more details. The offer of Chaste under the terms of the
00020 License is subject to the License being interpreted in accordance with
00021 English Law and subject to any action against the University of Oxford
00022 being under the jurisdiction of the English Courts.
00023 
00024 You should have received a copy of the GNU Lesser General Public License
00025 along with Chaste. If not, see <http://www.gnu.org/licenses/>.
00026 
00027 */
00028 #include "CryptStatistics.hpp"
00029 #include "RandomNumberGenerator.hpp"
00030 
00035 bool CellsHeightComparison(const std::pair<CellPtr, double> lhs, const std::pair<CellPtr, double> rhs)
00036 {
00037     return lhs.second < rhs.second;
00038 }
00039 
00040 CryptStatistics::CryptStatistics(MeshBasedCellPopulation<2>& rCrypt)
00041     : AbstractCryptStatistics(rCrypt)
00042 {
00043 }
00044 
00045 std::vector<CellPtr> CryptStatistics::GetCryptSection(double yTop, double xBottom, double xTop, bool periodic)
00046 {
00047 
00048     double crypt_width = mrCrypt.rGetMesh().GetWidth(0u);
00049     // Fill in the default values - in a sequential manner
00050     if (xBottom == DBL_MAX)
00051     {
00052         xBottom = RandomNumberGenerator::Instance()->ranf()*crypt_width;
00053     }
00054 
00055     if (xTop == DBL_MAX)
00056     {
00057         xTop = RandomNumberGenerator::Instance()->ranf()*crypt_width;
00058     }
00059 
00060     assert(yTop>0.0);
00061     std::list<std::pair<CellPtr, double> > cells_list; // the second entry is the y value (needed for sorting)
00062 
00063     if (fabs(xTop-xBottom)<0.5*crypt_width)
00064     {
00065         // The periodic version isn't needed, ignore even if periodic was set to true
00066         periodic = false;
00067     }
00068 
00069     // Loop over cells and add to the store if they are within a cell's radius of the
00070     // specified line
00071     for (AbstractCellPopulation<2>::Iterator cell_iter = mrCrypt.Begin();
00072          cell_iter != mrCrypt.End();
00073          ++cell_iter)
00074     {
00075         if (periodic)
00076         {
00077             if (CellIsInSectionPeriodic(xBottom, xTop, yTop, mrCrypt.GetLocationOfCellCentre(*cell_iter)))
00078             {
00079                 // Set up a pair, equal to (cell,y_val) and insert
00080                 std::pair<CellPtr, double> pair(*cell_iter, mrCrypt.GetLocationOfCellCentre(*cell_iter)[1]);
00081                 cells_list.push_back(pair);
00082             }
00083         }
00084         else
00085         {
00086             if (CellIsInSection(xBottom, xTop, yTop, mrCrypt.GetLocationOfCellCentre(*cell_iter)))
00087             {
00088                 // Set up a pair, equal to (cell,y_val) and insert
00089                 std::pair<CellPtr, double> pair(*cell_iter, mrCrypt.GetLocationOfCellCentre(*cell_iter)[1]);
00090                 cells_list.push_back(pair);
00091             }
00092         }
00093     }
00094 
00095     // Sort the list
00096     cells_list.sort(CellsHeightComparison);
00097 
00098     // Copy to a vector
00099     std::vector<CellPtr> ordered_cells;
00100     for (std::list<std::pair<CellPtr, double> >::iterator iter = cells_list.begin();
00101          iter!=cells_list.end();
00102          ++iter)
00103     {
00104         ordered_cells.push_back(iter->first);
00105     }
00106 
00107     return ordered_cells;
00108 }
00109 
00110 std::vector<CellPtr> CryptStatistics::GetCryptSectionPeriodic(double yTop, double xBottom, double xTop)
00111 {
00112    return GetCryptSection(yTop, xBottom, xTop, true);
00113 }
00114 bool CryptStatistics::CellIsInSection(double xBottom, double xTop, double yTop, const c_vector<double,2>& rCellPosition, double widthOfSection)
00115 {
00116     c_vector<double,2> intercept;
00117 
00118     if (xBottom == xTop)
00119     {
00120         intercept[0] = xTop;
00121         intercept[1] = rCellPosition[1];
00122     }
00123     else
00124     {
00125         double m = (yTop)/(xTop-xBottom); // gradient of line
00126 
00127         intercept[0] = (m*m*xBottom + rCellPosition[0] + m*rCellPosition[1])/(1+m*m);
00128         intercept[1] = m*(intercept[0] - xBottom);
00129     }
00130 
00131     c_vector<double,2> vec_from_A_to_B = mrCrypt.rGetMesh().GetVectorFromAtoB(intercept, rCellPosition);
00132     double dist = norm_2(vec_from_A_to_B);
00133 
00134     return (dist <= widthOfSection);
00135 }
00136 
00137 bool CryptStatistics::CellIsInSectionPeriodic(double xBottom, double xTop, double yTop, const c_vector<double,2>& rCellPosition, double widthOfSection)
00138 {
00139     bool is_in_section = false;
00140 
00141     c_vector<double,2> intercept;
00142     double crypt_width = mrCrypt.rGetMesh().GetWidth(0u);
00143 
00144     double m; // gradient of line
00145     double offset;
00146 
00147     if (xBottom < xTop)
00148     {
00149         offset = -crypt_width;
00150     }
00151     else
00152     {
00153         offset = crypt_width;
00154     }
00155 
00156     m = (yTop)/(xTop-xBottom+offset); // gradient of line
00157 
00158     // 1st line
00159     intercept[0] = (m*m*xBottom + rCellPosition[0] + m*rCellPosition[1])/(1+m*m);
00160     intercept[1] = m*(intercept[0] - xBottom);
00161 
00162     c_vector<double,2> vec_from_A_to_B = mrCrypt.rGetMesh().GetVectorFromAtoB(intercept, rCellPosition);
00163     double dist = norm_2(vec_from_A_to_B);
00164 
00165     if (dist < widthOfSection)
00166     {
00167         is_in_section = true;
00168     }
00169 
00170     // 2nd line
00171     intercept[0] = (m*m*(xBottom-offset) + rCellPosition[0] + m*rCellPosition[1])/(1+m*m);
00172     intercept[1] = m*(intercept[0] - (xBottom-offset));
00173 
00174     vec_from_A_to_B = mrCrypt.rGetMesh().GetVectorFromAtoB(intercept, rCellPosition);
00175     dist = norm_2(vec_from_A_to_B);
00176 
00177     if (dist < widthOfSection)
00178     {
00179         is_in_section = true;
00180     }
00181 
00182     return is_in_section;
00183 }

Generated on Mon Nov 1 12:35:16 2010 for Chaste by  doxygen 1.5.5