2009-03-29 06:05:28 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Math on rational polynomial surfaces and curves, typically in Bezier
|
|
|
|
// form. Evaluate, root-find (by Newton's methods), evaluate derivatives,
|
|
|
|
// and so on.
|
|
|
|
//-----------------------------------------------------------------------------
|
2009-01-15 03:55:42 +00:00
|
|
|
#include "../solvespace.h"
|
|
|
|
|
2009-02-27 13:04:36 +00:00
|
|
|
// Converge it to better than LENGTH_EPS; we want two points, each
|
|
|
|
// independently projected into uv and back, to end up equal with the
|
|
|
|
// LENGTH_EPS. Best case that requires LENGTH_EPS/2, but more is better
|
|
|
|
// and convergence should be fast by now.
|
2009-03-08 10:59:57 +00:00
|
|
|
#define RATPOLY_EPS (LENGTH_EPS/(1e2))
|
2009-02-27 13:04:36 +00:00
|
|
|
|
|
|
|
static double Bernstein(int k, int deg, double t)
|
2009-01-15 03:55:42 +00:00
|
|
|
{
|
2009-01-19 10:37:10 +00:00
|
|
|
if(k > deg || k < 0) return 0;
|
|
|
|
|
2009-01-15 03:55:42 +00:00
|
|
|
switch(deg) {
|
2009-01-19 10:37:10 +00:00
|
|
|
case 0:
|
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
|
2009-01-15 03:55:42 +00:00
|
|
|
case 1:
|
|
|
|
if(k == 0) {
|
|
|
|
return (1 - t);
|
|
|
|
} else if(k = 1) {
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
if(k == 0) {
|
|
|
|
return (1 - t)*(1 - t);
|
|
|
|
} else if(k == 1) {
|
|
|
|
return 2*(1 - t)*t;
|
|
|
|
} else if(k == 2) {
|
|
|
|
return t*t;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
if(k == 0) {
|
|
|
|
return (1 - t)*(1 - t)*(1 - t);
|
|
|
|
} else if(k == 1) {
|
|
|
|
return 3*(1 - t)*(1 - t)*t;
|
|
|
|
} else if(k == 2) {
|
|
|
|
return 3*(1 - t)*t*t;
|
|
|
|
} else if(k == 3) {
|
|
|
|
return t*t*t;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
oops();
|
|
|
|
}
|
|
|
|
|
2009-01-19 10:37:10 +00:00
|
|
|
double BernsteinDerivative(int k, int deg, double t)
|
|
|
|
{
|
2009-03-14 20:01:20 +00:00
|
|
|
switch(deg) {
|
|
|
|
case 0:
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
if(k == 0) {
|
|
|
|
return -1;
|
|
|
|
} else if(k = 1) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
if(k == 0) {
|
|
|
|
return -2 + 2*t;
|
|
|
|
} else if(k == 1) {
|
|
|
|
return 2 - 4*t;
|
|
|
|
} else if(k == 2) {
|
|
|
|
return 2*t;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
if(k == 0) {
|
|
|
|
return -3 + 6*t - 3*t*t;
|
|
|
|
} else if(k == 1) {
|
|
|
|
return 3 - 12*t + 9*t*t;
|
|
|
|
} else if(k == 2) {
|
|
|
|
return 6*t - 9*t*t;
|
|
|
|
} else if(k == 3) {
|
|
|
|
return 3*t*t;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
oops();
|
2009-01-19 10:37:10 +00:00
|
|
|
}
|
|
|
|
|
2009-01-19 03:51:00 +00:00
|
|
|
Vector SBezier::PointAt(double t) {
|
2009-01-15 03:55:42 +00:00
|
|
|
Vector pt = Vector::From(0, 0, 0);
|
|
|
|
double d = 0;
|
|
|
|
|
|
|
|
int i;
|
|
|
|
for(i = 0; i <= deg; i++) {
|
|
|
|
double B = Bernstein(i, deg, t);
|
|
|
|
pt = pt.Plus(ctrl[i].ScaledBy(B*weight[i]));
|
|
|
|
d += weight[i]*B;
|
|
|
|
}
|
|
|
|
pt = pt.ScaledBy(1.0/d);
|
|
|
|
return pt;
|
|
|
|
}
|
|
|
|
|
2009-04-14 04:19:23 +00:00
|
|
|
Vector SBezier::TangentAt(double t) {
|
|
|
|
Vector pt = Vector::From(0, 0, 0), pt_p = Vector::From(0, 0, 0);
|
|
|
|
double d = 0, d_p = 0;
|
|
|
|
|
|
|
|
int i;
|
|
|
|
for(i = 0; i <= deg; i++) {
|
|
|
|
double B = Bernstein(i, deg, t),
|
|
|
|
Bp = BernsteinDerivative(i, deg, t);
|
|
|
|
|
|
|
|
pt = pt.Plus(ctrl[i].ScaledBy(B*weight[i]));
|
|
|
|
d += weight[i]*B;
|
|
|
|
|
|
|
|
pt_p = pt_p.Plus(ctrl[i].ScaledBy(Bp*weight[i]));
|
|
|
|
d_p += weight[i]*Bp;
|
|
|
|
}
|
|
|
|
|
|
|
|
// quotient rule; f(t) = n(t)/d(t), so f' = (n'*d - n*d')/(d^2)
|
|
|
|
Vector ret;
|
|
|
|
ret = (pt_p.ScaledBy(d)).Minus(pt.ScaledBy(d_p));
|
|
|
|
ret = ret.ScaledBy(1.0/(d*d));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SBezier::ClosestPointTo(Vector p, double *t) {
|
|
|
|
int i;
|
|
|
|
double minDist = VERY_POSITIVE;
|
|
|
|
*t = 0;
|
|
|
|
double res = (deg <= 2) ? 7.0 : 20.0;
|
|
|
|
for(i = 0; i < (int)res; i++) {
|
|
|
|
double tryt = (i/res);
|
|
|
|
|
|
|
|
Vector tryp = PointAt(tryt);
|
|
|
|
double d = (tryp.Minus(p)).Magnitude();
|
|
|
|
if(d < minDist) {
|
|
|
|
*t = tryt;
|
|
|
|
minDist = d;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector p0;
|
|
|
|
for(i = 0; i < 15; i++) {
|
|
|
|
p0 = PointAt(*t);
|
|
|
|
if(p0.Equals(p, RATPOLY_EPS)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector dp = TangentAt(*t);
|
|
|
|
Vector pc = p.ClosestPointOnLine(p0, dp);
|
|
|
|
*t += (pc.Minus(p0)).DivPivoting(dp);
|
|
|
|
}
|
|
|
|
dbp("didn't converge (closest point on bezier curve)");
|
|
|
|
}
|
|
|
|
|
|
|
|
void SBezier::SplitAt(double t, SBezier *bef, SBezier *aft) {
|
|
|
|
Vector4 ct[4];
|
|
|
|
int i;
|
|
|
|
for(i = 0; i <= deg; i++) {
|
|
|
|
ct[i] = Vector4::From(weight[i], ctrl[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(deg) {
|
|
|
|
case 1: {
|
|
|
|
Vector4 cts = Vector4::Blend(ct[0], ct[1], t);
|
|
|
|
*bef = SBezier::From(ct[0], cts);
|
|
|
|
*aft = SBezier::From(cts, ct[1]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 2: {
|
|
|
|
Vector4 ct01 = Vector4::Blend(ct[0], ct[1], t),
|
|
|
|
ct12 = Vector4::Blend(ct[1], ct[2], t),
|
|
|
|
cts = Vector4::Blend(ct01, ct12, t);
|
|
|
|
|
|
|
|
*bef = SBezier::From(ct[0], ct01, cts);
|
|
|
|
*aft = SBezier::From(cts, ct12, ct[2]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 3: {
|
|
|
|
Vector4 ct01 = Vector4::Blend(ct[0], ct[1], t),
|
|
|
|
ct12 = Vector4::Blend(ct[1], ct[2], t),
|
|
|
|
ct23 = Vector4::Blend(ct[2], ct[3], t),
|
|
|
|
ct01_12 = Vector4::Blend(ct01, ct12, t),
|
|
|
|
ct12_23 = Vector4::Blend(ct12, ct23, t),
|
|
|
|
cts = Vector4::Blend(ct01_12, ct12_23, t);
|
|
|
|
|
|
|
|
*bef = SBezier::From(ct[0], ct01, ct01_12, cts);
|
|
|
|
*aft = SBezier::From(cts, ct12_23, ct23, ct[3]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: oops();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-18 07:26:51 +00:00
|
|
|
void SBezier::MakePwlInto(List<SCurvePt> *l) {
|
|
|
|
List<Vector> lv;
|
|
|
|
ZERO(&lv);
|
|
|
|
MakePwlInto(&lv);
|
|
|
|
int i;
|
|
|
|
for(i = 0; i < lv.n; i++) {
|
|
|
|
SCurvePt scpt;
|
|
|
|
scpt.tag = 0;
|
|
|
|
scpt.p = lv.elem[i];
|
|
|
|
scpt.vertex = (i == 0) || (i == (lv.n - 1));
|
|
|
|
l->Add(&scpt);
|
|
|
|
}
|
|
|
|
lv.Clear();
|
|
|
|
}
|
2009-01-19 03:51:00 +00:00
|
|
|
void SBezier::MakePwlInto(List<Vector> *l) {
|
2009-03-15 23:04:45 +00:00
|
|
|
l->Add(&(ctrl[0]));
|
|
|
|
MakePwlWorker(l, 0.0, 1.0);
|
2009-01-15 03:55:42 +00:00
|
|
|
}
|
2009-03-15 23:04:45 +00:00
|
|
|
void SBezier::MakePwlWorker(List<Vector> *l, double ta, double tb) {
|
2009-01-19 03:33:15 +00:00
|
|
|
Vector pa = PointAt(ta);
|
|
|
|
Vector pb = PointAt(tb);
|
2009-01-15 03:55:42 +00:00
|
|
|
|
|
|
|
// Can't test in the middle, or certain cubics would break.
|
|
|
|
double tm1 = (2*ta + tb) / 3;
|
|
|
|
double tm2 = (ta + 2*tb) / 3;
|
|
|
|
|
2009-01-19 03:33:15 +00:00
|
|
|
Vector pm1 = PointAt(tm1);
|
|
|
|
Vector pm2 = PointAt(tm2);
|
2009-01-15 03:55:42 +00:00
|
|
|
|
|
|
|
double d = max(pm1.DistanceToLine(pa, pb.Minus(pa)),
|
|
|
|
pm2.DistanceToLine(pa, pb.Minus(pa)));
|
|
|
|
|
|
|
|
double step = 1.0/SS.maxSegments;
|
2009-03-08 10:59:57 +00:00
|
|
|
if((tb - ta) < step || d < SS.ChordTolMm()) {
|
2009-01-15 03:55:42 +00:00
|
|
|
// A previous call has already added the beginning of our interval.
|
|
|
|
l->Add(&pb);
|
|
|
|
} else {
|
|
|
|
double tm = (ta + tb) / 2;
|
2009-03-15 23:04:45 +00:00
|
|
|
MakePwlWorker(l, ta, tm);
|
|
|
|
MakePwlWorker(l, tm, tb);
|
2009-01-15 03:55:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-19 10:37:10 +00:00
|
|
|
Vector SSurface::PointAt(double u, double v) {
|
|
|
|
Vector num = Vector::From(0, 0, 0);
|
|
|
|
double den = 0;
|
|
|
|
|
|
|
|
int i, j;
|
|
|
|
for(i = 0; i <= degm; i++) {
|
|
|
|
for(j = 0; j <= degn; j++) {
|
|
|
|
double Bi = Bernstein(i, degm, u),
|
|
|
|
Bj = Bernstein(j, degn, v);
|
|
|
|
|
|
|
|
num = num.Plus(ctrl[i][j].ScaledBy(Bi*Bj*weight[i][j]));
|
|
|
|
den += weight[i][j]*Bi*Bj;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
num = num.ScaledBy(1.0/den);
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SSurface::TangentsAt(double u, double v, Vector *tu, Vector *tv) {
|
|
|
|
Vector num = Vector::From(0, 0, 0),
|
|
|
|
num_u = Vector::From(0, 0, 0),
|
|
|
|
num_v = Vector::From(0, 0, 0);
|
|
|
|
double den = 0,
|
|
|
|
den_u = 0,
|
|
|
|
den_v = 0;
|
|
|
|
|
|
|
|
int i, j;
|
|
|
|
for(i = 0; i <= degm; i++) {
|
|
|
|
for(j = 0; j <= degn; j++) {
|
|
|
|
double Bi = Bernstein(i, degm, u),
|
|
|
|
Bj = Bernstein(j, degn, v),
|
|
|
|
Bip = BernsteinDerivative(i, degm, u),
|
|
|
|
Bjp = BernsteinDerivative(j, degn, v);
|
|
|
|
|
|
|
|
num = num.Plus(ctrl[i][j].ScaledBy(Bi*Bj*weight[i][j]));
|
|
|
|
den += weight[i][j]*Bi*Bj;
|
|
|
|
|
|
|
|
num_u = num_u.Plus(ctrl[i][j].ScaledBy(Bip*Bj*weight[i][j]));
|
|
|
|
den_u += weight[i][j]*Bip*Bj;
|
|
|
|
|
|
|
|
num_v = num_v.Plus(ctrl[i][j].ScaledBy(Bi*Bjp*weight[i][j]));
|
|
|
|
den_v += weight[i][j]*Bi*Bjp;
|
|
|
|
}
|
|
|
|
}
|
2009-04-14 04:19:23 +00:00
|
|
|
// quotient rule; f(t) = n(t)/d(t), so f' = (n'*d - n*d')/(d^2)
|
2009-01-19 10:37:10 +00:00
|
|
|
*tu = ((num_u.ScaledBy(den)).Minus(num.ScaledBy(den_u)));
|
|
|
|
*tu = tu->ScaledBy(1.0/(den*den));
|
|
|
|
|
|
|
|
*tv = ((num_v.ScaledBy(den)).Minus(num.ScaledBy(den_v)));
|
|
|
|
*tv = tv->ScaledBy(1.0/(den*den));
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector SSurface::NormalAt(double u, double v) {
|
|
|
|
Vector tu, tv;
|
|
|
|
TangentsAt(u, v, &tu, &tv);
|
|
|
|
return tu.Cross(tv);
|
|
|
|
}
|
|
|
|
|
2009-02-27 13:04:36 +00:00
|
|
|
void SSurface::ClosestPointTo(Vector p, double *u, double *v, bool converge) {
|
2009-01-19 10:37:10 +00:00
|
|
|
int i, j;
|
2009-02-09 12:40:48 +00:00
|
|
|
|
|
|
|
if(degm == 1 && degn == 1) {
|
|
|
|
*u = *v = 0; // a plane, perfect no matter what the initial guess
|
|
|
|
} else {
|
2009-02-27 13:04:36 +00:00
|
|
|
double minDist = VERY_POSITIVE;
|
2009-03-08 10:59:57 +00:00
|
|
|
double res = (max(degm, degn) == 2) ? 7.0 : 20.0;
|
2009-02-09 12:40:48 +00:00
|
|
|
for(i = 0; i < (int)res; i++) {
|
|
|
|
for(j = 0; j <= (int)res; j++) {
|
|
|
|
double tryu = (i/res), tryv = (j/res);
|
|
|
|
|
|
|
|
Vector tryp = PointAt(tryu, tryv);
|
|
|
|
double d = (tryp.Minus(p)).Magnitude();
|
|
|
|
if(d < minDist) {
|
|
|
|
*u = tryu;
|
|
|
|
*v = tryv;
|
|
|
|
minDist = d;
|
|
|
|
}
|
2009-01-19 10:37:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initial guess is in u, v
|
|
|
|
Vector p0;
|
2009-02-27 13:04:36 +00:00
|
|
|
for(i = 0; i < (converge ? 15 : 3); i++) {
|
2009-01-19 10:37:10 +00:00
|
|
|
p0 = PointAt(*u, *v);
|
2009-02-27 13:04:36 +00:00
|
|
|
if(converge) {
|
|
|
|
if(p0.Equals(p, RATPOLY_EPS)) {
|
|
|
|
return;
|
|
|
|
}
|
2009-01-19 10:37:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Vector tu, tv;
|
|
|
|
TangentsAt(*u, *v, &tu, &tv);
|
|
|
|
|
|
|
|
// Project the point into a plane through p0, with basis tu, tv; a
|
|
|
|
// second-order thing would converge faster but needs second
|
|
|
|
// derivatives.
|
|
|
|
Vector dp = p.Minus(p0);
|
|
|
|
double du = dp.Dot(tu), dv = dp.Dot(tv);
|
|
|
|
*u += du / (tu.MagSquared());
|
2009-01-27 09:22:18 +00:00
|
|
|
*v += dv / (tv.MagSquared());
|
2009-01-19 10:37:10 +00:00
|
|
|
}
|
2009-02-27 13:04:36 +00:00
|
|
|
|
|
|
|
if(converge) {
|
|
|
|
dbp("didn't converge");
|
|
|
|
dbp("have %.3f %.3f %.3f", CO(p0));
|
|
|
|
dbp("want %.3f %.3f %.3f", CO(p));
|
|
|
|
dbp("distance = %g", (p.Minus(p0)).Magnitude());
|
|
|
|
}
|
|
|
|
|
2009-01-19 10:37:10 +00:00
|
|
|
if(isnan(*u) || isnan(*v)) {
|
|
|
|
*u = *v = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-27 13:04:36 +00:00
|
|
|
bool SSurface::PointIntersectingLine(Vector p0, Vector p1, double *u, double *v)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for(i = 0; i < 15; i++) {
|
|
|
|
Vector pi, p, tu, tv;
|
|
|
|
p = PointAt(*u, *v);
|
|
|
|
TangentsAt(*u, *v, &tu, &tv);
|
|
|
|
|
|
|
|
Vector n = (tu.Cross(tv)).WithMagnitude(1);
|
|
|
|
double d = p.Dot(n);
|
|
|
|
|
|
|
|
bool parallel;
|
|
|
|
pi = Vector::AtIntersectionOfPlaneAndLine(n, d, p0, p1, ¶llel);
|
|
|
|
if(parallel) break;
|
|
|
|
|
|
|
|
// Check for convergence
|
|
|
|
if(pi.Equals(p, RATPOLY_EPS)) return true;
|
|
|
|
|
|
|
|
// Adjust our guess and iterate
|
|
|
|
Vector dp = pi.Minus(p);
|
|
|
|
double du = dp.Dot(tu), dv = dp.Dot(tv);
|
|
|
|
*u += du / (tu.MagSquared());
|
|
|
|
*v += dv / (tv.MagSquared());
|
|
|
|
}
|
|
|
|
// dbp("didn't converge (surface intersecting line)");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SSurface::PointOnSurfaces(SSurface *s1, SSurface *s2,
|
|
|
|
double *up, double *vp)
|
|
|
|
{
|
|
|
|
double u[3] = { *up, 0, 0 }, v[3] = { *vp, 0, 0 };
|
|
|
|
SSurface *srf[3] = { this, s1, s2 };
|
|
|
|
|
|
|
|
// Get initial guesses for (u, v) in the other surfaces
|
|
|
|
Vector p = PointAt(*u, *v);
|
|
|
|
(srf[1])->ClosestPointTo(p, &(u[1]), &(v[1]), false);
|
|
|
|
(srf[2])->ClosestPointTo(p, &(u[2]), &(v[2]), false);
|
|
|
|
|
|
|
|
int i, j;
|
|
|
|
for(i = 0; i < 15; i++) {
|
|
|
|
// Approximate each surface by a plane
|
|
|
|
Vector p[3], tu[3], tv[3], n[3];
|
|
|
|
double d[3];
|
|
|
|
for(j = 0; j < 3; j++) {
|
|
|
|
p[j] = (srf[j])->PointAt(u[j], v[j]);
|
|
|
|
(srf[j])->TangentsAt(u[j], v[j], &(tu[j]), &(tv[j]));
|
|
|
|
n[j] = ((tu[j]).Cross(tv[j])).WithMagnitude(1);
|
|
|
|
d[j] = (n[j]).Dot(p[j]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If a = b and b = c, then does a = c? No, it doesn't.
|
|
|
|
if((p[0]).Equals(p[1], RATPOLY_EPS) &&
|
|
|
|
(p[1]).Equals(p[2], RATPOLY_EPS) &&
|
|
|
|
(p[2]).Equals(p[0], RATPOLY_EPS))
|
|
|
|
{
|
|
|
|
*up = u[0];
|
|
|
|
*vp = v[0];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool parallel;
|
|
|
|
Vector pi = Vector::AtIntersectionOfPlanes(n[0], d[0],
|
|
|
|
n[1], d[1],
|
|
|
|
n[2], d[2], ¶llel);
|
|
|
|
if(parallel) break;
|
|
|
|
|
|
|
|
for(j = 0; j < 3; j++) {
|
|
|
|
Vector dp = pi.Minus(p[j]);
|
|
|
|
double du = dp.Dot(tu[j]), dv = dp.Dot(tv[j]);
|
|
|
|
u[j] += du / (tu[j]).MagSquared();
|
|
|
|
v[j] += dv / (tv[j]).MagSquared();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dbp("didn't converge (three surfaces intersecting)");
|
|
|
|
}
|
|
|
|
|