int r = -1;
int a = p;
int b = p + 1;
int cind = 1;
float ua = u[0];
cph[0] = new Vec4D(cp[0]);
for (int i = 0; i <= ph; i++) {
uh[i] = ua;
}
for (int i = 0; i <= p; i++) {
bpts[i] = new Vec4D(cp[i]);
}
while (b < m) {
int i = b;
while (b < m && u[b] == u[b + 1]) {
b++;
}
int mul = b - i + 1;
mh += mul + t;
float ub = u[b];
int oldr = r;
r = p - mul;
int lbz = 1;
if (oldr > 0) {
lbz = (oldr + 2) / 2;
}
int rbz = ph;
if (r > 0) {
rbz = ph - (r + 1) / 2;
float numer = ub - ua;
for (int k = p; k > mul; k--) {
alfs[k - mul - 1] = numer / (u[a + k] - ua);
}
for (int j = 1; j <= r; j++) {
int save = r - j;
int s = mul + j;
for (int k = p; k >= s; k--) {
bpts[k].interpolateToSelf(bpts[k - 1], 1 - alfs[k - s]);
}
nextbpts[save] = bpts[p];
}
}
for (i = lbz; i <= ph; i++) {
ebpts[i] = new Vec4D();
int mpi = MathUtils.min(p, i);
for (int j = MathUtils.max(0, i - t); j <= mpi; j++) {
ebpts[i].addScaledSelf(bpts[j], bezalfs[i][j]);
}
}
if (oldr > 1) {
int first = kind - 2;
int last = kind;
float den = ua - ub;
for (int tr = 1; tr < oldr; tr++) {
i = first;
int j = last;
int kj = j - kind + 1;
while (j - i > tr) {
if (i < cind) {
float alf = (ub - uh[i]) / (ua - uh[i]);
cph[i].interpolateToSelf(cph[i - 1], 1 - alf);
}
if (j >= lbz) {
if (j - tr <= kind - ph + oldr) {
float gam = (ub - uh[j - tr]) / den;
ebpts[kj].interpolateToSelf(ebpts[kj + 1],
1 - gam);
} else {
float bet = (ub - uh[kind - 1]) / den;
ebpts[kj].interpolateToSelf(ebpts[kj + 1],
1 - bet);
}
}
i++;
j--;
kj--;
}
first--;
last++;
}
}
if (a != p) {
for (i = 0; i < ph - oldr; i++) {
uh[kind] = ua;
kind++;
}
}
for (int j = lbz; j <= rbz; j++) {
cph[cind] = new Vec4D(ebpts[j]);
cind++;
}
if (b < m) {
for (int j = 0; j < r; j++) {
bpts[j].set(nextbpts[j]);
}
for (int j = r; j <= p; j++) {
bpts[j].set(cp[b - p + j]);
}
a = b;
b++;
ua = ub;
} else {
for (i = 0; i <= ph; i++) {
uh[kind + i] = ub;
}
}
}
int nh = mh - ph - 1;
float[] uNew = new float[mh + 1];
for (int i = 0; i < uNew.length; i++) {
uNew[i] = uh[i];
}
Vec4D[] cpNew = new Vec4D[nh + 1];
for (int i = 0; i < cpNew.length; i++) {
cpNew[i] = new Vec4D(cph[i]);
}
return new BasicNurbsCurve(cpNew, uNew, p + t);
}