Chaste  Release::2018.1
CryptStatistics.cpp
1 /*
2 
3 Copyright (c) 2005-2018, University of Oxford.
4 All rights reserved.
5 
6 University of Oxford means the Chancellor, Masters and Scholars of the
7 University of Oxford, having an administrative office at Wellington
8 Square, Oxford OX1 2JD, UK.
9 
10 This file is part of Chaste.
11 
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions are met:
14  * Redistributions of source code must retain the above copyright notice,
15  this list of conditions and the following disclaimer.
16  * Redistributions in binary form must reproduce the above copyright notice,
17  this list of conditions and the following disclaimer in the documentation
18  and/or other materials provided with the distribution.
19  * Neither the name of the University of Oxford nor the names of its
20  contributors may be used to endorse or promote products derived from this
21  software without specific prior written permission.
22 
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 
34 */
35 #include "CryptStatistics.hpp"
36 #include "RandomNumberGenerator.hpp"
37 
42 bool CellsHeightComparison(const std::pair<CellPtr, double> lhs, const std::pair<CellPtr, double> rhs)
43 {
44  return lhs.second < rhs.second;
45 }
46 
48  : AbstractCryptStatistics(rCrypt)
49 {
50 }
51 
52 std::vector<CellPtr> CryptStatistics::GetCryptSection(double yTop, double xBottom, double xTop, bool periodic)
53 {
54  double crypt_width = mrCrypt.rGetMesh().GetWidth(0);
55 
56  // Fill in the default values - in a sequential manner
57  if (xBottom == DBL_MAX)
58  {
59  xBottom = RandomNumberGenerator::Instance()->ranf()*crypt_width;
60  }
61 
62  if (xTop == DBL_MAX)
63  {
64  xTop = RandomNumberGenerator::Instance()->ranf()*crypt_width;
65  }
66 
67  assert(yTop>0.0);
68  std::list<std::pair<CellPtr, double> > cells_list; // the second entry is the y value (needed for sorting)
69 
70  if (fabs(xTop-xBottom)<0.5*crypt_width)
71  {
72  // The periodic version isn't needed, ignore even if periodic was set to true
73  periodic = false;
74  }
75 
76  // Loop over cells and add to the store if they are within a cell's radius of the specified line
78  cell_iter != mrCrypt.End();
79  ++cell_iter)
80  {
81  if (periodic)
82  {
83  if (CellIsInSectionPeriodic(xBottom, xTop, yTop, mrCrypt.GetLocationOfCellCentre(*cell_iter)))
84  {
85  // Set up a pair, equal to (cell,y_val) and insert
86  std::pair<CellPtr, double> pair(*cell_iter, mrCrypt.GetLocationOfCellCentre(*cell_iter)[1]);
87  cells_list.push_back(pair);
88  }
89  }
90  else
91  {
92  if (CellIsInSection(xBottom, xTop, yTop, mrCrypt.GetLocationOfCellCentre(*cell_iter)))
93  {
94  // Set up a pair, equal to (cell,y_val) and insert
95  std::pair<CellPtr, double> pair(*cell_iter, mrCrypt.GetLocationOfCellCentre(*cell_iter)[1]);
96  cells_list.push_back(pair);
97  }
98  }
99  }
100 
101  // Sort the list
102  cells_list.sort(CellsHeightComparison);
103 
104  // Copy to a vector
105  std::vector<CellPtr> ordered_cells;
106  for (std::list<std::pair<CellPtr, double> >::iterator iter = cells_list.begin();
107  iter!=cells_list.end();
108  ++iter)
109  {
110  ordered_cells.push_back(iter->first);
111  }
112 
113  return ordered_cells;
114 }
115 
116 std::vector<CellPtr> CryptStatistics::GetCryptSectionPeriodic(double yTop, double xBottom, double xTop)
117 {
118  return GetCryptSection(yTop, xBottom, xTop, true);
119 }
120 bool CryptStatistics::CellIsInSection(double xBottom, double xTop, double yTop, const c_vector<double,2>& rCellPosition, double widthOfSection)
121 {
122  c_vector<double,2> intercept;
123 
124  if (xBottom == xTop)
125  {
126  intercept[0] = xTop;
127  intercept[1] = rCellPosition[1];
128  }
129  else
130  {
131  double m = (yTop)/(xTop-xBottom); // gradient of line
132 
133  intercept[0] = (m*m*xBottom + rCellPosition[0] + m*rCellPosition[1])/(1+m*m);
134  intercept[1] = m*(intercept[0] - xBottom);
135  }
136 
137  c_vector<double,2> vec_from_A_to_B = mrCrypt.rGetMesh().GetVectorFromAtoB(intercept, rCellPosition);
138  double dist = norm_2(vec_from_A_to_B);
139 
140  return (dist <= widthOfSection);
141 }
142 
143 bool CryptStatistics::CellIsInSectionPeriodic(double xBottom, double xTop, double yTop, const c_vector<double,2>& rCellPosition, double widthOfSection)
144 {
145  bool is_in_section = false;
146 
147  c_vector<double,2> intercept;
148  double crypt_width = mrCrypt.rGetMesh().GetWidth(0u);
149 
150  double m; // gradient of line
151  double offset;
152 
153  if (xBottom < xTop)
154  {
155  offset = -crypt_width;
156  }
157  else
158  {
159  offset = crypt_width;
160  }
161 
162  m = (yTop)/(xTop-xBottom+offset); // gradient of line
163 
164  // 1st line
165  intercept[0] = (m*m*xBottom + rCellPosition[0] + m*rCellPosition[1])/(1+m*m);
166  intercept[1] = m*(intercept[0] - xBottom);
167 
168  c_vector<double,2> vec_from_A_to_B = mrCrypt.rGetMesh().GetVectorFromAtoB(intercept, rCellPosition);
169  double dist = norm_2(vec_from_A_to_B);
170 
171  if (dist < widthOfSection)
172  {
173  is_in_section = true;
174  }
175 
176  // 2nd line
177  intercept[0] = (m*m*(xBottom-offset) + rCellPosition[0] + m*rCellPosition[1])/(1+m*m);
178  intercept[1] = m*(intercept[0] - (xBottom-offset));
179 
180  vec_from_A_to_B = mrCrypt.rGetMesh().GetVectorFromAtoB(intercept, rCellPosition);
181  dist = norm_2(vec_from_A_to_B);
182 
183  if (dist < widthOfSection)
184  {
185  is_in_section = true;
186  }
187 
188  return is_in_section;
189 }
bool CellIsInSectionPeriodic(double xBottom, double xTop, double yTop, const c_vector< double, 2 > &rCellPosition, double widthOfSection=1.0)
MutableMesh< ELEMENT_DIM, SPACE_DIM > & rGetMesh()
std::vector< CellPtr > GetCryptSection(double yTop, double xBottom=DBL_MAX, double xTop=DBL_MAX, bool periodic=false)
MeshBasedCellPopulation< 2 > & mrCrypt
static RandomNumberGenerator * Instance()
CryptStatistics(MeshBasedCellPopulation< 2 > &rCrypt)
virtual double GetWidth(const unsigned &rDimension) const
virtual c_vector< double, SPACE_DIM > GetVectorFromAtoB(const c_vector< double, SPACE_DIM > &rLocationA, const c_vector< double, SPACE_DIM > &rLocationB)
c_vector< double, SPACE_DIM > GetLocationOfCellCentre(CellPtr pCell)
std::vector< CellPtr > GetCryptSectionPeriodic(double yTop, double xBottom=DBL_MAX, double xTop=DBL_MAX)
bool CellIsInSection(double xBottom, double xTop, double yTop, const c_vector< double, 2 > &rCellPosition, double widthOfSection=0.5)