我试图将这两个函数从Lua移植到Java(bspline2D和bspline3D),但它们无法正常工作。输出完全不同,我无法弄清楚原因。
我已经尝试过搞乱数组编号,细分值,for循环以及我头脑中的所有内容。
所提供的测试应该给出彼此完全不同的数字,但它们不是。
这些是Java结果:
bspline2D
-2.0, 0.0, 0.0
-1.0, 0.0, 0.0
-1.0, 0.0, 0.0
0.0, 0.0, 0.0
0.0, 0.0, 0.0
bspline3D
0, 0
-0.75, 0
-0.5, 0
-0.25, 0
0, 0
0.24739583, 0.0026041667
0.47916666, 0.020833334
0.6796875, 0.0703125
0.8333333, 0.16666667
0.9270833, 0.31770834
0.9583333, 0.5
0.9270833, 0.6822917
0.8333333, 0.8333333
0.6796875, 0.9296875
0.47916666, 0.9791667
0.24739583, 0.9973958
0, 1
......这显然是错误的。
这些是Lua版本的输出,正确的。
bspline2D
0, 0, 0
0.24739583, 0.0026041667, 0
0.47916666, 0.020833334, 0
0.6796875, 0.0703125, 0
0.8333333, 0.16666667, 0
0.9270833, 0.31770834, 0
0.9583333, 0.5, 0
0.9270833, 0.6822917, 0
0.8333333, 0.8333333, 0
0.6796875, 0.9296875, 0
0.47916666, 0.9791667, 0
0.24739583, 0.9973958, 0
0, 1, 0
-0.25, 1, 0
-0.5, 1, 0
-0.75, 1, 0
-1, 1, 0
-1.25, 1, 0
-1.5, 1, 0
-1.75, 1, 0
-2, 1, 0
bspline3D
public static List<Vector3> bspline2D(Vector3[] pointz, int subdivision) {
ArrayList spline = new ArrayList();
int pointsNumber = pointz.length;
Vector3[] points = new Vector3[pointsNumber + 4];
System.arraycopy(pointz, 0, points, 2, pointsNumber);
points[1] = points[2].add(points[2].minus(points[3]));
points[0] = points[1].add(points[1].minus(points[2]));
points[pointsNumber + 2] = points[pointsNumber - 3].add(points[pointsNumber - 3].minus(points[pointsNumber - 4]));
points[pointsNumber + 3] = points[pointsNumber].add(points[pointsNumber].minus(points[pointsNumber - 3]));
spline.add(pointz[0]);
for(int i = 1; i < pointsNumber - 1; ++i) {
double[] a = new double[4];
double[] b = new double[4];
a[0] = (-points[i - 1].x + 3.0D * points[i].x - 3.0D * points[i + 1].x + points[i + 2].x) / 6.0D;
a[1] = (3.0D * points[i - 1].x - 6.0D * points[i].x + 3.0D * points[i + 1].x) / 6.0D;
a[2] = (-3.0D * points[i - 1].x + 3.0D * points[i + 1].x) / 6.0D;
a[3] = (points[i - 1].x + 4.0D * points[i].x + points[i + 1].x) / 6.0D;
b[0] = (-points[i - 1].y + 3.0D * points[i].y - 3.0D * points[i + 1].y + points[i + 2].y) / 6.0D;
b[1] = (3.0D * points[i - 1].y - 6.0D * points[i].y + 3.0D * points[i + 1].y) / 6.0D;
b[2] = (-3.0D * points[i - 1].y + 3.0D * points[i + 1].y) / 6.0D;
b[3] = (points[i - 1].y + 4.0D * points[i].y + points[i + 1].y) / 6.0D;
for(int j = 0; j < subdivision; ++j) {
double t = j / subdivision;
spline.add(new Vector3((a[2] + t * (a[1] + t * a[0])) * t + a[3], (b[2] + t * (b[1] + t * b[0])) * t + b[3]));
}
}
return spline;
}
public static List<Vector3> bspline3D(Vector3[] pointz, int subdivision) {
ArrayList spline = new ArrayList();
int pointsNumber = pointz.length;
Vector3[] points = new Vector3[pointsNumber + 4];
System.arraycopy(pointz, 0, points, 2, pointsNumber);
points[1] = points[2].add(points[2].minus(points[3]));
points[0] = points[1].add(points[1].minus(points[2]));
points[pointsNumber + 2] = pointz[pointsNumber - 1].add(pointz[pointsNumber - 1].minus(pointz[pointsNumber - 2]));
points[pointsNumber + 3] = points[pointsNumber].add(points[pointsNumber].minus(pointz[pointsNumber - 1]));
spline.add(points[0]);
for(int i = 1; i < pointsNumber - 1; ++i) {
double[] a = new double[4];
double[] b = new double[4];
double[] c = new double[4];
a[0] = (-points[i - 1].x + 3.0D * points[i].x - 3.0D * points[i + 1].x + points[i + 2].x) / 6.0D;
a[1] = (3.0D * points[i - 1].x - 6.0D * points[i].x + 3.0D * points[i + 1].x) / 6.0D;
a[2] = (-3.0D * points[i - 1].x + 3.0D * points[i + 1].x) / 6.0D;
a[3] = (points[i - 1].x + 4.0D * points[i].x + points[i + 1].x) / 6.0D;
b[0] = (-points[i - 1].y + 3.0D * points[i].y - 3.0D * points[i + 1].y + points[i + 2].y) / 6.0D;
b[1] = (3.0D * points[i - 1].y - 6.0D * points[i].y + 3.0D * points[i + 1].y) / 6.0D;
b[2] = (-3.0D * points[i - 1].y + 3.0D * points[i + 1].y) / 6.0D;
b[3] = (points[i - 1].y + 4.0D * points[i].y + points[i + 1].y) / 6.0D;
c[0] = (-points[i - 1].z + 3.0D * points[i].z - 3.0D * points[i + 1].z + points[i + 2].z) / 6.0D;
c[1] = (3.0D * points[i - 1].z - 6.0D * points[i].z + 3.0D * points[i + 1].z) / 6.0D;
c[2] = (-3.0D * points[i - 1].z + 3.0D * points[i + 1].z) / 6.0D;
c[3] = (points[i - 1].z + 4.0D * points[i].z + points[i + 1].z) / 6.0D;
for(int j = 1; j < subdivision; ++j) {
double t = j / subdivision;
Vector3 sp = new Vector3();
sp.x = (a[2] + t * (a[1] + t * a[0])) * t + a[3];
sp.y = (b[2] + t * (b[1] + t * b[0])) * t + b[3];
sp.z = (c[2] + t * (c[1] + t * c[0])) * t + c[3];
spline.add(sp);
}
}
return spline;
}
public static void main(String[] args) {
Vector3[] v = new Vector3[] {new Vector3(0, 0, 0), new Vector3(1, 0, 0), new Vector3(1, 1, 0), new Vector3(0, 1, 0)};
System.out.println("\nbspline2D");
for(Vector3 v3 : bspline2D(v, 4)) {
System.out.println(v3.x + ", " + v3.y + ", " + v3.z);
}
System.out.println("\nbspline3D");
for(Vector3 v3 : bspline3D(v, 4)) {
System.out.println(v3.x + ", " + v3.y + ", " + v3.z);
}
}
我已经在这上面唠叨了好几天,我没有任何想法,所以我想也许有人在StackOverflow会这么做。
这是需要修复的Java版本:
function M.bspline2D(points, subdivision)
local spline = {}
local wbvector2 = dofile('wbvector3.lua')
local pointsNumber = #points
-- extend the points array
points[0] = wbvector2.add(points[1], wbvector2.minus(points[1], points[2]))
points[-1] = wbvector2.add(points[0], wbvector2.minus(points[0], points[1]))
points[pointsNumber + 1] = wbvector2.add(points[pointsNumber], wbvector2.minus(points[pointsNumber], points[pointsNumber - 1]))
points[pointsNumber + 2] = wbvector2.add(points[pointsNumber + 1], wbvector2.minus(points[pointsNumber + 1], points[pointsNumber]))
-- Interpolation
local index = 1
spline[index] = points[1] -- first point
for i = 0, pointsNumber - 1 do
local a = {} -- compute the third order coefficients for x
local b = {} -- compute the third order coefficients for y
a[1] = (-points[i-1].x + 3 * points[i].x - 3 * points[i+1].x + points[i+2].x) / 6.0;
a[2] = (3 * points[i-1].x - 6 * points[i].x + 3 * points[i+1].x) / 6.0;
a[3] = (-3 * points[i-1].x + 3 * points[i+1].x) / 6.0;
a[4] = (points[i-1].x + 4 * points[i].x + points[i+1].x) / 6.0;
b[1] = (-points[i-1].y + 3 * points[i].y - 3 * points[i+1].y + points[i+2].y) / 6.0;
b[2] = (3 * points[i-1].y - 6 * points[i].y + 3 * points[i+1].y) / 6.0;
b[3] = (-3 * points[i-1].y + 3 * points[i+1].y) / 6.0;
b[4] = (points[i-1].y + 4 * points[i].y + points[i+1].y) / 6.0;
for j = 1, subdivision do
index = index + 1
spline[index] = {}
local t = j / subdivision
spline[index].x = (a[3] + t * (a[2] + t * a[1])) * t + a[4]
spline[index].y = (b[3] + t * (b[2] + t * b[1])) * t + b[4]
end
end
return spline
end
function M.bspline3D(points, subdivision)
local spline = {}
local wbcore = require('wbcore')
local wbvector3 = require('wbvector3')
local pointsNumber = wbcore.tablelength(points)
-- extend the points array
points[0] = wbvector3.add(points[1], wbvector3.minus(points[1], points[2]))
points[-1] = wbvector3.add(points[0], wbvector3.minus(points[0], points[1]))
points[pointsNumber + 1] = wbvector3.add(points[pointsNumber], wbvector3.minus(points[pointsNumber], points[pointsNumber - 1]))
points[pointsNumber + 2] = wbvector3.add(points[pointsNumber + 1], wbvector3.minus(points[pointsNumber + 1], points[pointsNumber]))
-- Interpolation
local index = 1
spline[index] = points[1] -- first point
for i = 1, pointsNumber - 1 do
local a = {} -- compute the third order coefficients for x
local b = {} -- compute the third order coefficients for y
local c = {} -- compute the third order coefficients for z
a[1] = (-points[i-1].x + 3 * points[i].x - 3 * points[i+1].x + points[i+2].x) / 6.0;
a[2] = (3 * points[i-1].x - 6 * points[i].x + 3 * points[i+1].x) / 6.0;
a[3] = (-3 * points[i-1].x + 3 * points[i+1].x) / 6.0;
a[4] = (points[i-1].x + 4 * points[i].x + points[i+1].x) / 6.0;
b[1] = (-points[i-1].y + 3 * points[i].y - 3 * points[i+1].y + points[i+2].y) / 6.0;
b[2] = (3 * points[i-1].y - 6 * points[i].y + 3 * points[i+1].y) / 6.0;
b[3] = (-3 * points[i-1].y + 3 * points[i+1].y) / 6.0;
b[4] = (points[i-1].y + 4 * points[i].y + points[i+1].y) / 6.0;
c[1] = (-points[i-1].z + 3 * points[i].z - 3 * points[i+1].z + points[i+2].z) / 6.0;
c[2] = (3 * points[i-1].z - 6 * points[i].z + 3 * points[i+1].z) / 6.0;
c[3] = (-3 * points[i-1].z + 3 * points[i+1].z) / 6.0;
c[4] = (points[i-1].z + 4 * points[i].z + points[i+1].z) / 6.0;
for j = 1, subdivision do
index = index + 1
spline[index] = {}
local t = j / subdivision
spline[index].x = (a[3] + t * (a[2] + t * a[1])) * t + a[4]
spline[index].y = (b[3] + t * (b[2] + t * b[1])) * t + b[4]
spline[index].z = (c[3] + t * (c[2] + t * c[1])) * t + c[4]
end
end
return spline
end
local v = {{x = 0, y = 0, z = 0}, {x = 1, y = 0, z = 0}, {x = 1, y = 1, z = 0}, {x = 0, y = 1, z = 0}}
print()
print("bspline2D");
for _, v3 in ipairs(M.bspline2D(v, 4)) do
print(v3.x .. ", " .. v3.y .. ", " .. v3.z)
end
print()
print("bspline3D");
for _, v3 in ipairs(M.bspline3D(v, 4)) do
print(v3.x .. ", " .. v3.y .. ", " .. v3.z)
end
这是Lua版本:
paper-dialog