CryptStatistics.cpp

00001 /*
00002 
00003 Copyright (C) University of Oxford, 2005-2009
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 
00034 bool CellsHeightComparison(const std::pair<TissueCell*, double> lhs, const std::pair<TissueCell*, double> rhs)
00035 {
00036     return lhs.second < rhs.second;
00037 }
00038 
00039 /*
00040  * PRIVATE FUNCTIONS -----------------------------------------------------------------
00041  */
00042 
00043 bool CryptStatistics::CellIsInSection(double xBottom, double xTop, double yTop, const c_vector<double,2>& rCellPosition, double widthOfSection)
00044 {
00045     c_vector<double,2> intercept;
00046 
00047     if (xBottom==xTop)
00048     {
00049         intercept[0] = xTop;
00050         intercept[1] = rCellPosition[1];
00051     }
00052     else
00053     {
00054         double m = (yTop)/(xTop-xBottom); // gradient of line
00055 
00056         intercept[0] = (m*m*xBottom + rCellPosition[0] + m*rCellPosition[1])/(1+m*m);
00057         intercept[1] = m*(intercept[0] - xBottom);
00058     }
00059 
00060     c_vector<double,2> vec_from_A_to_B = mrCrypt.rGetMesh().GetVectorFromAtoB(intercept, rCellPosition);
00061     double dist = norm_2(vec_from_A_to_B);
00062 
00063     return (dist <= widthOfSection);
00064 }
00065 
00066 bool CryptStatistics::CellIsInSectionPeriodic(double xBottom, double xTop, double yTop, const c_vector<double,2>& rCellPosition, double widthOfSection)
00067 {
00068     bool is_in_section = false;
00069 
00070     c_vector<double,2> intercept;
00071     double crypt_width = TissueConfig::Instance()->GetCryptWidth();
00072 
00073     double m; // gradient of line
00074     double offset;
00075 
00076     if (xBottom<xTop)
00077     {
00078         offset = -crypt_width;
00079     }
00080     else
00081     {
00082         offset = crypt_width;
00083     }
00084 
00085     m = (yTop)/(xTop-xBottom+offset); // gradient of line
00086 
00087     // 1st Line
00088     intercept[0] = (m*m*xBottom + rCellPosition[0] + m*rCellPosition[1])/(1+m*m);
00089     intercept[1] = m*(intercept[0] - xBottom);
00090 
00091     c_vector<double,2> vec_from_A_to_B = mrCrypt.rGetMesh().GetVectorFromAtoB(intercept, rCellPosition);
00092     double dist = norm_2(vec_from_A_to_B);
00093 
00094     if (dist < widthOfSection)
00095     {
00096         is_in_section=true;
00097     }
00098 
00099     // 2nd Line
00100     intercept[0] = (m*m*(xBottom-offset) + rCellPosition[0] + m*rCellPosition[1])/(1+m*m);
00101     intercept[1] = m*(intercept[0] - (xBottom-offset));
00102 
00103     vec_from_A_to_B = mrCrypt.rGetMesh().GetVectorFromAtoB(intercept, rCellPosition);
00104     dist = norm_2(vec_from_A_to_B);
00105 
00106     if (dist < widthOfSection)
00107     {
00108         is_in_section = true;
00109     }
00110 
00111     return is_in_section;
00112 }
00113 
00114 /*
00115  * PUBLIC FUNCTIONS -----------------------------------------------------------------
00116  */
00117 std::vector<TissueCell*> CryptStatistics::GetCryptSection(double xBottom, double xTop, double yTop, bool periodic)
00118 {
00119     //Fill in the default values - in a sequential manner
00120     if (xBottom == DBL_MAX)
00121     {
00122         xBottom = RandomNumberGenerator::Instance()->ranf()*TissueConfig::Instance()->GetCryptWidth();
00123     }
00124 
00125     if (xTop == DBL_MAX)
00126     {
00127         xTop = RandomNumberGenerator::Instance()->ranf()*TissueConfig::Instance()->GetCryptWidth();
00128     }
00129 
00130 
00131     assert(yTop>0.0);
00132     std::list<std::pair<TissueCell*, double> > cells_list; // the second entry is the y value (needed for sorting)
00133 
00134     if (fabs(xTop-xBottom)<0.5*TissueConfig::Instance()->GetCryptWidth())
00135     {
00136         // the periodic version isn't needed, ignore even if periodic was set to true
00137         periodic = false;
00138     }
00139 
00140     // loop over cells and add to the store if they are within a cell's radius of the
00141     // specified line
00142     for (AbstractTissue<2>::Iterator cell_iter = mrCrypt.Begin();
00143          cell_iter != mrCrypt.End();
00144          ++cell_iter)
00145     {
00146         if (periodic)
00147         {
00148             if (CellIsInSectionPeriodic(xBottom, xTop, yTop, mrCrypt.GetLocationOfCellCentre(&(*cell_iter))))
00149             {
00150                 // set up a pair, equal to (cell,y_val) and insert
00151                 std::pair<TissueCell*, double> pair(&(*cell_iter), mrCrypt.GetLocationOfCellCentre(&(*cell_iter))[1]);
00152                 cells_list.push_back(pair);
00153             }
00154         }
00155         else
00156         {
00157             if (CellIsInSection(xBottom, xTop, yTop, mrCrypt.GetLocationOfCellCentre(&(*cell_iter))))
00158             {
00159                 // set up a pair, equal to (cell,y_val) and insert
00160                 std::pair<TissueCell*, double> pair(&(*cell_iter), mrCrypt.GetLocationOfCellCentre(&(*cell_iter))[1]);
00161                 cells_list.push_back(pair);
00162             }
00163         }
00164     }
00165 
00166     // sort the list
00167     cells_list.sort(CellsHeightComparison);
00168 
00169     // copy to a vector
00170     std::vector<TissueCell*> ordered_cells;
00171     for (std::list<std::pair<TissueCell*, double> >::iterator iter = cells_list.begin();
00172         iter!=cells_list.end();
00173         iter++)
00174     {
00175         ordered_cells.push_back(iter->first);
00176     }
00177 
00178     return ordered_cells;
00179 }
00180 
00181 std::vector<TissueCell*> CryptStatistics::GetCryptSectionPeriodic(double xBottom, double xTop, double yTop)
00182 {
00183    return GetCryptSection(xBottom,xTop,yTop,true);
00184 }
00185 

Generated on Tue Aug 4 16:10:21 2009 for Chaste by  doxygen 1.5.5