Chaste Release::3.1
CryptStatistics.cpp
00001 /*
00002 
00003 Copyright (c) 2005-2012, University of Oxford.
00004 All rights reserved.
00005 
00006 University of Oxford means the Chancellor, Masters and Scholars of the
00007 University of Oxford, having an administrative office at Wellington
00008 Square, Oxford OX1 2JD, UK.
00009 
00010 This file is part of Chaste.
00011 
00012 Redistribution and use in source and binary forms, with or without
00013 modification, are permitted provided that the following conditions are met:
00014  * Redistributions of source code must retain the above copyright notice,
00015    this list of conditions and the following disclaimer.
00016  * Redistributions in binary form must reproduce the above copyright notice,
00017    this list of conditions and the following disclaimer in the documentation
00018    and/or other materials provided with the distribution.
00019  * Neither the name of the University of Oxford nor the names of its
00020    contributors may be used to endorse or promote products derived from this
00021    software without specific prior written permission.
00022 
00023 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00024 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00025 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00026 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00027 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00028 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
00029 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00030 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
00032 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033 
00034 */
00035 #include "CryptStatistics.hpp"
00036 #include "RandomNumberGenerator.hpp"
00037 
00042 bool CellsHeightComparison(const std::pair<CellPtr, double> lhs, const std::pair<CellPtr, double> rhs)
00043 {
00044     return lhs.second < rhs.second;
00045 }
00046 
00047 CryptStatistics::CryptStatistics(MeshBasedCellPopulation<2>& rCrypt)
00048     : AbstractCryptStatistics(rCrypt)
00049 {
00050 }
00051 
00052 std::vector<CellPtr> CryptStatistics::GetCryptSection(double yTop, double xBottom, double xTop, bool periodic)
00053 {
00054 
00055     double crypt_width = mrCrypt.rGetMesh().GetWidth(0u);
00056     // Fill in the default values - in a sequential manner
00057     if (xBottom == DBL_MAX)
00058     {
00059         xBottom = RandomNumberGenerator::Instance()->ranf()*crypt_width;
00060     }
00061 
00062     if (xTop == DBL_MAX)
00063     {
00064         xTop = RandomNumberGenerator::Instance()->ranf()*crypt_width;
00065     }
00066 
00067     assert(yTop>0.0);
00068     std::list<std::pair<CellPtr, double> > cells_list; // the second entry is the y value (needed for sorting)
00069 
00070     if (fabs(xTop-xBottom)<0.5*crypt_width)
00071     {
00072         // The periodic version isn't needed, ignore even if periodic was set to true
00073         periodic = false;
00074     }
00075 
00076     // Loop over cells and add to the store if they are within a cell's radius of the
00077     // specified line
00078     for (AbstractCellPopulation<2>::Iterator cell_iter = mrCrypt.Begin();
00079          cell_iter != mrCrypt.End();
00080          ++cell_iter)
00081     {
00082         if (periodic)
00083         {
00084             if (CellIsInSectionPeriodic(xBottom, xTop, yTop, mrCrypt.GetLocationOfCellCentre(*cell_iter)))
00085             {
00086                 // Set up a pair, equal to (cell,y_val) and insert
00087                 std::pair<CellPtr, double> pair(*cell_iter, mrCrypt.GetLocationOfCellCentre(*cell_iter)[1]);
00088                 cells_list.push_back(pair);
00089             }
00090         }
00091         else
00092         {
00093             if (CellIsInSection(xBottom, xTop, yTop, mrCrypt.GetLocationOfCellCentre(*cell_iter)))
00094             {
00095                 // Set up a pair, equal to (cell,y_val) and insert
00096                 std::pair<CellPtr, double> pair(*cell_iter, mrCrypt.GetLocationOfCellCentre(*cell_iter)[1]);
00097                 cells_list.push_back(pair);
00098             }
00099         }
00100     }
00101 
00102     // Sort the list
00103     cells_list.sort(CellsHeightComparison);
00104 
00105     // Copy to a vector
00106     std::vector<CellPtr> ordered_cells;
00107     for (std::list<std::pair<CellPtr, double> >::iterator iter = cells_list.begin();
00108          iter!=cells_list.end();
00109          ++iter)
00110     {
00111         ordered_cells.push_back(iter->first);
00112     }
00113 
00114     return ordered_cells;
00115 }
00116 
00117 std::vector<CellPtr> CryptStatistics::GetCryptSectionPeriodic(double yTop, double xBottom, double xTop)
00118 {
00119    return GetCryptSection(yTop, xBottom, xTop, true);
00120 }
00121 bool CryptStatistics::CellIsInSection(double xBottom, double xTop, double yTop, const c_vector<double,2>& rCellPosition, double widthOfSection)
00122 {
00123     c_vector<double,2> intercept;
00124 
00125     if (xBottom == xTop)
00126     {
00127         intercept[0] = xTop;
00128         intercept[1] = rCellPosition[1];
00129     }
00130     else
00131     {
00132         double m = (yTop)/(xTop-xBottom); // gradient of line
00133 
00134         intercept[0] = (m*m*xBottom + rCellPosition[0] + m*rCellPosition[1])/(1+m*m);
00135         intercept[1] = m*(intercept[0] - xBottom);
00136     }
00137 
00138     c_vector<double,2> vec_from_A_to_B = mrCrypt.rGetMesh().GetVectorFromAtoB(intercept, rCellPosition);
00139     double dist = norm_2(vec_from_A_to_B);
00140 
00141     return (dist <= widthOfSection);
00142 }
00143 
00144 bool CryptStatistics::CellIsInSectionPeriodic(double xBottom, double xTop, double yTop, const c_vector<double,2>& rCellPosition, double widthOfSection)
00145 {
00146     bool is_in_section = false;
00147 
00148     c_vector<double,2> intercept;
00149     double crypt_width = mrCrypt.rGetMesh().GetWidth(0u);
00150 
00151     double m; // gradient of line
00152     double offset;
00153 
00154     if (xBottom < xTop)
00155     {
00156         offset = -crypt_width;
00157     }
00158     else
00159     {
00160         offset = crypt_width;
00161     }
00162 
00163     m = (yTop)/(xTop-xBottom+offset); // gradient of line
00164 
00165     // 1st line
00166     intercept[0] = (m*m*xBottom + rCellPosition[0] + m*rCellPosition[1])/(1+m*m);
00167     intercept[1] = m*(intercept[0] - xBottom);
00168 
00169     c_vector<double,2> vec_from_A_to_B = mrCrypt.rGetMesh().GetVectorFromAtoB(intercept, rCellPosition);
00170     double dist = norm_2(vec_from_A_to_B);
00171 
00172     if (dist < widthOfSection)
00173     {
00174         is_in_section = true;
00175     }
00176 
00177     // 2nd line
00178     intercept[0] = (m*m*(xBottom-offset) + rCellPosition[0] + m*rCellPosition[1])/(1+m*m);
00179     intercept[1] = m*(intercept[0] - (xBottom-offset));
00180 
00181     vec_from_A_to_B = mrCrypt.rGetMesh().GetVectorFromAtoB(intercept, rCellPosition);
00182     dist = norm_2(vec_from_A_to_B);
00183 
00184     if (dist < widthOfSection)
00185     {
00186         is_in_section = true;
00187     }
00188 
00189     return is_in_section;
00190 }