initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / OpenFOAM / graph / curve / curveTools.C
blob82e6ff7feb78d75302a775b9a12d76a182ac3f2b
1 #include "scalar.H"
2 #include "vector.H"
3 #include "curveTools.H"
5 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
7 namespace Foam
10 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
12 scalar distance(const vector& p1, const vector& p2)
14     return mag(p2 - p1);
18 bool stepForwardsToNextPoint
20     const vector& o,
21     vector& n,
22     label& i,
23     label& ip1,
24     scalar l,
25     const curve& Curve
28     label ip1n = ip1-1;
29     while (++ip1n < Curve.size() && distance(o, Curve[ip1n]) < l);
30     label in = ip1n - 1;
32     bool eoc = true;
34     if (ip1n < Curve.size() && in >= 0)
35     {
36         eoc = interpolate(Curve[in], Curve[ip1n], o, n, l);
38         i = in;
39         ip1 = ip1n;
40     }
42     return eoc;
46 bool stepBackwardsToNextPoint
48     const vector& o,
49     vector& n,
50     label& i,
51     label& ip1,
52     scalar l,
53     const curve& Curve
56     label ip1n = ip1+1;
57     while (--ip1n >= 0 && distance(o, Curve[ip1n]) < l);
58     label in = ip1n + 1;
60     bool eoc = true;
62     if (ip1n >= 0 && in < Curve.size())
63     {
64         eoc = interpolate(Curve[in], Curve[ip1n], o, n, l);
66         i = in;
67         ip1 = ip1n;
68     }
70     return eoc;
74 bool interpolate
76     const vector& p1,
77     const vector& p2,
78     const vector& o,
79     vector& n,
80     scalar l
83     vector D = p1 - p2;
84     scalar a = magSqr(D);
85     scalar b = 2.0*(D&(p2 - o));
86     scalar c = magSqr(p2) + (o&(o - 2.0*p2)) - l*l;
88     scalar b2m4ac = b*b - 4.0*a*c;
90     if (b2m4ac >= 0.0)
91     {
92         scalar srb2m4ac = sqrt(b2m4ac);
94         scalar lamda = (-b - srb2m4ac)/(2.0*a);
96         if (lamda > 1.0+curveSmall || lamda < -curveSmall)
97         {
98             lamda = (-b + srb2m4ac)/(2.0*a);
99         }
101         if (lamda < 1.0+curveSmall && lamda > -curveSmall)
102         {
103             n = p2 + lamda*(p1 - p2);
105             return false;
106         }
107         else
108         {
109             return true;
110         }
111     }
112     else
113     {
114         return true;
115     }
120 bool XstepForwardsToNextPoint
122     const vector& o,
123     vector& n,
124     label& i,
125     label& ip1,
126     scalar l,
127     const curve& Curve
130     label ip1n = ip1-1;
131     while (++ip1n < Curve.size() && mag(o.x() - Curve[ip1n].x()) < l);
132     label in = ip1n - 1;
134     bool eoc = true;
136     if (ip1n < Curve.size() && in >= 0)
137     {
138         eoc = Xinterpolate(Curve[in], Curve[ip1n], o, n, l);
140         i = in;
141         ip1 = ip1n;
142     }
144     return eoc;
149 bool Xinterpolate
151     const vector& p1,
152     const vector& p2,
153     const vector& o,
154     vector& n,
155     scalar l
158     n.x() = o.x() + l;
160     if (p2.x() < o.x() + l - curveSmall && p2.x() > o.x() - l + curveSmall)
161     {
162         return true;
163     }
165     if (p2.x() < o.x() + l)
166     {
167         n.x() = o.x() - l;
168     }
170     vector D = p2 - p1;
171     scalar lamda = (n.x() - p1.x())/D.x();
172     n.y() = p1.y() + lamda*D.y();
173     return false;
178 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
180 } // End namespace Foam
182 // ************************************************************************* //