Chaste Release::3.1
CryptProjectionStatistics.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 "CryptProjectionStatistics.hpp"
00036 #include "RandomNumberGenerator.hpp"
00037 
00042 bool CellsRadiusComparison(const std::pair<CellPtr, double> lhs, const std::pair<CellPtr, double> rhs)
00043 {
00044     return lhs.second < rhs.second;
00045 }
00046 
00047 CryptProjectionStatistics::CryptProjectionStatistics(MeshBasedCellPopulation<2>& rCrypt)
00048     : AbstractCryptStatistics(rCrypt)
00049 {
00050 }
00051 
00052 bool CryptProjectionStatistics::CellIsInSection(double angle, const c_vector<double,2>& rCellPosition, double widthOfSection)
00053 {
00054     // Get corresponding 3D position of closest point on line
00055     c_vector<double,2> line_position;
00056     line_position[0] = norm_2(rCellPosition)*cos(angle);
00057     line_position[1] = norm_2(rCellPosition)*sin(angle);
00058 
00059     double distance_between_cell_and_line = norm_2(rCellPosition - line_position);
00060 
00061     return (distance_between_cell_and_line <= widthOfSection);
00062 }
00063 
00064 std::vector<CellPtr> CryptProjectionStatistics::GetCryptSection(double angle)
00065 {
00066     if (angle == DBL_MAX)
00067     {
00068         angle = M_PI - 2*M_PI*RandomNumberGenerator::Instance()->ranf();
00069     }
00070 
00071     assert(angle>=-M_PI && angle<=M_PI);
00072 
00073     std::list<std::pair<CellPtr, double> > cells_list; // the second entry is the radius (needed for sorting)
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 
00083         if (CellIsInSection(angle, mrCrypt.GetLocationOfCellCentre(*cell_iter)))
00084         {
00085             // Set up a pair, equal to (cell,r) and insert
00086             std::pair<CellPtr, double> pair(*cell_iter, norm_2(mrCrypt.GetLocationOfCellCentre(*cell_iter)));
00087             cells_list.push_back(pair);
00088         }
00089     }
00090 
00091     // Sort the list
00092     cells_list.sort(CellsRadiusComparison);
00093 
00094     // Copy to a vector
00095     std::vector<CellPtr> ordered_cells;
00096     for (std::list<std::pair<CellPtr, double> >::iterator iter = cells_list.begin();
00097          iter != cells_list.end();
00098          ++iter)
00099     {
00100         ordered_cells.push_back(iter->first);
00101     }
00102 
00103     return ordered_cells;
00104 }