| 1 | //travel_time_marcher.cpp |
| 2 | |
| 3 | #include "travel_time_marcher.h" |
| 4 | #include "math.h" |
| 5 | #include "heap.h" |
| 6 | #include <stdexcept> |
| 7 | #include <vector> |
| 8 | #include <algorithm> // std::min_element, std::max_element |
| 9 | |
| 10 | void travelTimeMarcher::initalizeFrozen() |
| 11 | { |
| 12 | distanceMarcher::initalizeFrozen(); |
| 13 | for (int i=0; i<size_; i++) |
| 14 | { |
| 15 | if (flag_[i]==Frozen) |
| 16 | { |
| 17 | // convert distance to time |
| 18 | distance_[i]=fabs(distance_[i]/speed_[i]); |
| 19 | } |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | double travelTimeMarcher::updatePointOrderTwo(int i) |
| 24 | { |
| 25 | double res = updatePointOrderTwo(i, std::set<int>()); |
| 26 | if(res == std::numeric_limits<double>::infinity()) |
| 27 | throw std::runtime_error("Unreachable voxel"); |
| 28 | else |
| 29 | return res; |
| 30 | } |
| 31 | |
| 32 | |
| 33 | // second order point update |
| 34 | // update the distance from the frozen points |
| 35 | const double aa = 9.0/4.0; |
| 36 | const double oneThird = 1.0/3.0; |
| 37 | double travelTimeMarcher::updatePointOrderTwo(int i, std::set<int>avoid_dim) |
| 38 | { |
| 39 | double a,b,c; |
| 40 | a=b=c=0; |
| 41 | int naddr=0; |
| 42 | for (int dim=0; dim<dim_; dim++) |
| 43 | { |
| 44 | if(avoid_dim.find(dim) != avoid_dim.end()) continue; //we should avoid this dimension |
| 45 | double value1 = maxDouble; |
| 46 | double value2 = maxDouble; |
| 47 | for (int j=-1; j<2; j+=2) // each direction |
| 48 | { |
| 49 | naddr = _getN(i,dim,j,Mask); |
| 50 | if (naddr!=-1 && flag_[naddr]==Frozen) |
| 51 | { |
| 52 | if (fabs(distance_[naddr])<fabs(value1)) |
| 53 | { |
| 54 | value1 = distance_[naddr]; |
| 55 | int naddr2 = _getN(i,dim,j*2,Mask); |
| 56 | if (naddr2!=-1 && |
| 57 | flag_[naddr2]==Frozen && |
| 58 | ((distance_[naddr2]<=value1 && value1 >=0) || |
| 59 | (distance_[naddr2]>=value1 && value1 <=0))) |
| 60 | { |
| 61 | value2=distance_[naddr2]; |
| 62 | if (phi_[naddr2] * phi_[naddr] < 0 || phi_[naddr2] * phi_[i] < 0) |
| 63 | value2 *= -1; |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | if (value2<maxDouble) |
| 69 | { |
| 70 | double tp = oneThird*(4*value1-value2); |
| 71 | a+=idx2_[dim]*aa; |
| 72 | b-=idx2_[dim]*2*aa*tp; |
| 73 | c+=idx2_[dim]*aa*pow(tp,2); |
| 74 | } |
| 75 | else if (value1<maxDouble) |
| 76 | { |
| 77 | a+=idx2_[dim]; |
| 78 | b-=idx2_[dim]*2*value1; |
| 79 | c+=idx2_[dim]*pow(value1,2); |
| 80 | } |
| 81 | } |
| 82 | try { |
| 83 | double res = solveQuadratic(i,a,b,c); |
| 84 | return res; |
| 85 | } catch(std::runtime_error& err) { |
| 86 | //if the determinant is negative, we try to reach the voxel with one dimension less and take the minimum |
| 87 | if(avoid_dim.size() == dim_) return std::numeric_limits<double>::infinity(); //end of the recursion, use inf so that it is discarded selecting the minimum |
| 88 | std::vector<double> sols; |
| 89 | for (int ind=0; ind<dim_; ind++){ |
| 90 | //remove one dimension more than what we are already doing |
| 91 | std::set<int> tempset = avoid_dim; |
| 92 | std::pair<std::set<int>::iterator, bool> ret = tempset.insert(ind); |
| 93 | if(!ret.second) continue; //avoid recursive call on identical parameters (the set already had *ind* in it) |
| 94 | sols.push_back(updatePointOrderTwo(i,tempset)); |
| 95 | } |
| 96 | if(sols.size()==0) return std::numeric_limits<double>::infinity();//All the derivates with different dimensionalities are 0 |
| 97 | return *std::min_element(sols.begin(), sols.end()); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | |
| 102 | double travelTimeMarcher::solveQuadratic(int i, const double &a, |
| 103 | const double &b, |
| 104 | double &c) |
| 105 | { |
| 106 | c -= 1/pow(speed_[i],2); |
| 107 | double r0=0; |
| 108 | double det = pow(b,2)-4*a*c; |
| 109 | if (det>=0) |
| 110 | { |
| 111 | r0 = (-b+sqrt(det))/2.0/a; |
| 112 | } |
| 113 | else |
| 114 | { |
| 115 | throw std::runtime_error("Negative discriminant in time marcher quadratic."); |
| 116 | } |
| 117 | return r0; |
| 118 | } |