CryptStatistics.cpp

00001 /*
00002 
00003 Copyright (c) 2005-2015, 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     double crypt_width = mrCrypt.rGetMesh().GetWidth(0);
00055 
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 specified line
00077     for (AbstractCellPopulation<2>::Iterator cell_iter = mrCrypt.Begin();
00078          cell_iter != mrCrypt.End();
00079          ++cell_iter)
00080     {
00081         if (periodic)
00082         {
00083             if (CellIsInSectionPeriodic(xBottom, xTop, yTop, mrCrypt.GetLocationOfCellCentre(*cell_iter)))
00084             {
00085                 // Set up a pair, equal to (cell,y_val) and insert
00086                 std::pair<CellPtr, double> pair(*cell_iter, mrCrypt.GetLocationOfCellCentre(*cell_iter)[1]);
00087                 cells_list.push_back(pair);
00088             }
00089         }
00090         else
00091         {
00092             if (CellIsInSection(xBottom, xTop, yTop, mrCrypt.GetLocationOfCellCentre(*cell_iter)))
00093             {
00094                 // Set up a pair, equal to (cell,y_val) and insert
00095                 std::pair<CellPtr, double> pair(*cell_iter, mrCrypt.GetLocationOfCellCentre(*cell_iter)[1]);
00096                 cells_list.push_back(pair);
00097             }
00098         }
00099     }
00100 
00101     // Sort the list
00102     cells_list.sort(CellsHeightComparison);
00103 
00104     // Copy to a vector
00105     std::vector<CellPtr> ordered_cells;
00106     for (std::list<std::pair<CellPtr, double> >::iterator iter = cells_list.begin();
00107          iter!=cells_list.end();
00108          ++iter)
00109     {
00110         ordered_cells.push_back(iter->first);
00111     }
00112 
00113     return ordered_cells;
00114 }
00115 
00116 std::vector<CellPtr> CryptStatistics::GetCryptSectionPeriodic(double yTop, double xBottom, double xTop)
00117 {
00118    return GetCryptSection(yTop, xBottom, xTop, true);
00119 }
00120 bool CryptStatistics::CellIsInSection(double xBottom, double xTop, double yTop, const c_vector<double,2>& rCellPosition, double widthOfSection)
00121 {
00122     c_vector<double,2> intercept;
00123 
00124     if (xBottom == xTop)
00125     {
00126         intercept[0] = xTop;
00127         intercept[1] = rCellPosition[1];
00128     }
00129     else
00130     {
00131         double m = (yTop)/(xTop-xBottom); // gradient of line
00132 
00133         intercept[0] = (m*m*xBottom + rCellPosition[0] + m*rCellPosition[1])/(1+m*m);
00134         intercept[1] = m*(intercept[0] - xBottom);
00135     }
00136 
00137     c_vector<double,2> vec_from_A_to_B = mrCrypt.rGetMesh().GetVectorFromAtoB(intercept, rCellPosition);
00138     double dist = norm_2(vec_from_A_to_B);
00139 
00140     return (dist <= widthOfSection);
00141 }
00142 
00143 bool CryptStatistics::CellIsInSectionPeriodic(double xBottom, double xTop, double yTop, const c_vector<double,2>& rCellPosition, double widthOfSection)
00144 {
00145     bool is_in_section = false;
00146 
00147     c_vector<double,2> intercept;
00148     double crypt_width = mrCrypt.rGetMesh().GetWidth(0u);
00149 
00150     double m; // gradient of line
00151     double offset;
00152 
00153     if (xBottom < xTop)
00154     {
00155         offset = -crypt_width;
00156     }
00157     else
00158     {
00159         offset = crypt_width;
00160     }
00161 
00162     m = (yTop)/(xTop-xBottom+offset); // gradient of line
00163 
00164     // 1st line
00165     intercept[0] = (m*m*xBottom + rCellPosition[0] + m*rCellPosition[1])/(1+m*m);
00166     intercept[1] = m*(intercept[0] - xBottom);
00167 
00168     c_vector<double,2> vec_from_A_to_B = mrCrypt.rGetMesh().GetVectorFromAtoB(intercept, rCellPosition);
00169     double dist = norm_2(vec_from_A_to_B);
00170 
00171     if (dist < widthOfSection)
00172     {
00173         is_in_section = true;
00174     }
00175 
00176     // 2nd line
00177     intercept[0] = (m*m*(xBottom-offset) + rCellPosition[0] + m*rCellPosition[1])/(1+m*m);
00178     intercept[1] = m*(intercept[0] - (xBottom-offset));
00179 
00180     vec_from_A_to_B = mrCrypt.rGetMesh().GetVectorFromAtoB(intercept, rCellPosition);
00181     dist = norm_2(vec_from_A_to_B);
00182 
00183     if (dist < widthOfSection)
00184     {
00185         is_in_section = true;
00186     }
00187 
00188     return is_in_section;
00189 }

Generated by  doxygen 1.6.2