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

Generated on Wed Mar 18 12:51:50 2009 for Chaste by  doxygen 1.5.5