Debian Codegraph @

skfmm/distance_marcher.cpp

raw symbols SCIP
1 #include "distance_marcher.h"
2 #include "math.h"
3 #include <stdexcept>
4
5 double distanceMarcher::updatePointOrderOne(int i)
6 {
7 double a,b,c;
8 a=b=c=0;
9 int naddr=0;
10 for (int dim=0; dim<dim_; dim++)
11 {
12 double value = maxDouble;
13 for (int j=-1; j<2; j+=2) // each direction
14 {
15 naddr = _getN(i,dim,j,Mask);
16 if (naddr!=-1 && flag_[naddr]==Frozen)
17 {
18 if (fabs(distance_[naddr])<fabs(value))
19 {
20 value = distance_[naddr];
21 }
22 }
23 }
24 if (value<maxDouble)
25 {
26 a+=idx2_[dim];
27 b-=idx2_[dim]*2*value;
28 c+=idx2_[dim]*pow(value,2);
29 }
30 }
31 try {
32 return solveQuadratic(i,a,b,c);
33 } catch(std::runtime_error& err) {
34 // if the quadratic equation can't be solved, use the
35 // position of the minimum as a more reasonable approximation
36 return -b / (2.0 * a);
37 }
38 }
39
40
41 // second order point update
42 // update the distance from the frozen points
43 const double aa = 9.0/4.0;
44 const double oneThird = 1.0/3.0;
45 double distanceMarcher::updatePointOrderTwo(int i)
46 {
47 double a,b,c;
48 a=b=c=0;
49 int naddr=0;
50 for (int dim=0; dim<dim_; dim++)
51 {
52 double value1 = maxDouble;
53 double value2 = maxDouble;
54 for (int j=-1; j<2; j+=2) // each direction
55 {
56 naddr = _getN(i,dim,j,Mask);
57 if (naddr!=-1 && flag_[naddr]==Frozen)
58 {
59 if (fabs(distance_[naddr])<fabs(value1))
60 {
61 value1 = distance_[naddr];
62 int naddr2 = _getN(i,dim,j*2,Mask);
63 if (naddr2!=-1 &&
64 flag_[naddr2]==Frozen &&
65 ((distance_[naddr2]<=value1 && value1 >=0) ||
66 (distance_[naddr2]>=value1 && value1 <=0)))
67 {
68 value2=distance_[naddr2];
69 }
70 }
71 }
72 }
73 if (value2<maxDouble)
74 {
75 double tp = oneThird*(4*value1-value2);
76 a+=idx2_[dim]*aa;
77 b-=idx2_[dim]*2*aa*tp;
78 c+=idx2_[dim]*aa*pow(tp,2);
79 }
80 else if (value1<maxDouble)
81 {
82 a+=idx2_[dim];
83 b-=idx2_[dim]*2*value1;
84 c+=idx2_[dim]*pow(value1,2);
85 }
86 }
87 try {
88 return solveQuadratic(i,a,b,c);
89 } catch(std::runtime_error& err) {
90 // if the second order method fails, try the first order method instead
91 return updatePointOrderOne(i);
92 }
93 }
94
95
96 // find and return the correct root
97 double distanceMarcher::solveQuadratic(int i, const double &a,
98 const double &b,
99 double &c)
100 {
101 c-=1;
102 double det = pow(b,2)-4*a*c;
103 if (det >= 0)
104 {
105 if (phi_[i] > doubleEpsilon) { return (-b + sqrt(det)) / 2.0 / a; }
106 else { return (-b - sqrt(det)) / 2.0 / a; }
107 }
108 else
109 {
110 throw std::runtime_error("Negative discriminant in distance marcher quadratic.");
111 }
112 }
113
114
115 void distanceMarcher::initalizeFrozen()
116 {
117 //loop over phi to find zero values
118 // and mark them as frozen
119 for (int i=0; i<size_; i++)
120 {
121 if (flag_[i] != Mask && phi_[i]==0.0)
122 {
123 flag_[i]=Frozen;
124 distance_[i]=0.0;
125 }
126 }
127 //loop over all of phi and for each point check each direction
128 // to see if we cross the zero level set
129 // if so calculate the minimum distance to the zero level set
130 // mark as frozen.
131 for (int i=0; i<size_; i++)
132 if (flag_[i] == Far)
133 {
134 double ldistance[MaximumDimension];
135 bool borders=false;
136 for (int dim=0; dim<dim_; dim++)
137 {
138 ldistance[dim]=0;
139 for (int j=-1; j<2; j+=2) // each direction
140 {
141 int naddr = _getN(i,dim,j,Mask);
142 if (naddr!=-1 && phi_[i] * phi_[naddr]<0)
143 {
144 // this cell and neighbor span the zero level set.
145 borders=true;
146 //calculate the distance to the zero level set.
147 double d = dx_[dim]*phi_[i]/(phi_[i]-phi_[naddr]);
148 if (ldistance[dim]==0 || ldistance[dim]>d)
149 {
150 ldistance[dim] = d;
151 }
152 }
153 } // for each direction
154 } // for each dimension
155 if (borders)
156 {
157 double dsum = 0;
158 for (int dim=0; dim<dim_; dim++)
159 if (ldistance[dim]>0) dsum += 1/ldistance[dim]/ldistance[dim];
160 if (phi_[i]<0)
161 distance_[i] = -sqrt(1/dsum);
162 else distance_[i] = sqrt(1/dsum);
163 flag_[i]=Frozen;
164 }
165 }// for each point in the far field
166 }