public Vector3Param(Vector3Param v) { s = v.s; t = v.t; u = v.u; p = v.p; p0 = v.p0; }
void OnDestroy() { f1 = null; f2 = null; f3 = null; f4 = null; f5 = null; farr = null; flua = null; ie = null; add = null; luaenv.Dispose(); }
/// <summary> /// Calculate the trivariate bernstein polynomial as described by [1] /// /// My method adapts [1] slightly by precalculating the BP coefficients and storing /// them in Vector3Param. When it comes time to extract a world coordinate, /// it's just a matter of summing up multiplications through each polynomial from eq (2). /// </summary> /// <links> /// [1] - Method based on: http://pages.cpsc.ucalgary.ca/~blob/papers/others/ffd.pdf /// </links> /// <param name="p0">Origin of our coordinate system (where STU meet)</param> void calculateTrivariateBernsteinPolynomial(Vector3 p0) { Vector3 TcU = Vector3.Cross(T, U); Vector3 ScU = Vector3.Cross(S, U); Vector3 ScT = Vector3.Cross(S, T); float TcUdS = Vector3.Dot(TcU, S); float ScUdT = Vector3.Dot(ScU, T); float ScTdU = Vector3.Dot(ScT, U); for (int v = 0; v < originalVertices.Length; v++) { Vector3 diff = originalVertices[v] - p0; Vector3Param tmp = new Vector3Param(); tmp.s = Vector3.Dot(TcU, diff / TcUdS); tmp.t = Vector3.Dot(ScU, diff / ScUdT); tmp.u = Vector3.Dot(ScT, diff / ScTdU); tmp.p = p0 + (tmp.s * S) + (tmp.t * T) + (tmp.u * U); tmp.p0 = p0; tmp.bernPolyPack = new List <List <float> >(); { // Reserve room for each bernstein polynomial pack. tmp.bernPolyPack.Add(new List <float>(L)); //outer bernstein poly tmp.bernPolyPack.Add(new List <float>(M)); //middle bernstein poly tmp.bernPolyPack.Add(new List <float>(N)); //inner bernstein poly } { // Pre-calculate bernstein polynomial expansion. It only needs to be done once per parameterization for (int i = 0; i <= L; i++) { for (int j = 0; j <= M; j++) { for (int k = 0; k <= N; k++) { tmp.bernPolyPack[2].Add(bernsteinPoly(N, k, tmp.u)); } tmp.bernPolyPack[1].Add(bernsteinPoly(M, j, tmp.t)); } tmp.bernPolyPack[0].Add(bernsteinPoly(L, i, tmp.s)); } } vertexParams.Add(tmp); if (Vector3.Distance(tmp.p, originalVertices[v]) > 0.001f) { //Debug.Log("Warning, mismatched parameterization"); } } }
/// <summary> /// Convert parameterized vertex in to a world coordinate /// </summary> Vector3 getWorldVector3(Vector3Param r) { int l = L; int m = M; int n = N; Vector3 tS = Vector3.zero; for (int i = 0; i <= l; i++) { Vector3 tM = Vector3.zero; for (int j = 0; j <= m; j++) { Vector3 tK = Vector3.zero; for (int k = 0; k <= n; k++) { tK += r.bernPolyPack[2][k] * controlPoints[i, j, k].transform.localPosition; } tM += r.bernPolyPack[1][j] * tK; } tS += r.bernPolyPack[0][i] * tM; } return(tS); }
// Use this for initialization void Start() { XLuaEnv.onDispose += () => { f1 = null; f2 = null; f3 = null; f4 = null; f5 = null; farr = null; flua = null; ie = null; add = null; }; XLuaEnv.DoString(@" function id(...) return ... end function add(a, b) return a + b end function array_exchange(arr) arr[0], arr[1] = arr[1], arr[0] end local v3 = CS.UnityEngine.Vector3(7, 8, 9) local vt = CS.IFramework.Hotfix.Lua.MyStruct(5, 6) function lua_access_csharp() monoBehaviour:FloatParamMethod(123) --primitive monoBehaviour:Vector3ParamMethod(v3) --vector3 local rnd = math.random(1, 100) local r = monoBehaviour:Vector3ParamMethod({x = 1, y = 2, z = rnd}) --vector3 assert(r.x == 1 and r.y == 2 and r.z == rnd) monoBehaviour:StructParamMethod(vt) --custom struct r = monoBehaviour:StructParamMethod({a = 1, b = rnd, e = {c = rnd}}) assert(r.b == rnd and r.e.c == rnd) monoBehaviour:EnumParamMethod(CS.IFramework.Hotfix.Lua.MyEnum.E2) --enum monoBehaviour:DecimalParamMethod(monoBehaviour.a5[0]) monoBehaviour.a1[0], monoBehaviour.a1[1] = monoBehaviour.a1[1], monoBehaviour.a1[0] -- field end exchanger = { exchange = function(self, arr) array_exchange(arr) end } A = { B = { C = 789}} GDATA = 1234; "); XLuaEnv.gtable.Set("monoBehaviour", this); XLuaEnv.gtable.Get("id", out f1); XLuaEnv.gtable.Get("id", out f2); XLuaEnv.gtable.Get("id", out f3); XLuaEnv.gtable.Get("id", out f4); XLuaEnv.gtable.Get("id", out f5); XLuaEnv.gtable.Get("array_exchange", out farr); XLuaEnv.gtable.Get("lua_access_csharp", out flua); XLuaEnv.gtable.Get("exchanger", out ie); XLuaEnv.gtable.Get("add", out add); XLuaEnv.gtable.Set("g_int", 123); XLuaEnv.gtable.Set(123, 456); int i; XLuaEnv.gtable.Get("g_int", out i); Debug.Log("g_int:" + i); XLuaEnv.gtable.Get(123, out i); Debug.Log("123:" + i); }
public Vector3Param(Vector3Param v) { s = v.s; t = v.t; u = v.u; p = v.p; p0 = v.p0; }
/// <summary> /// Convert parameterized vertex in to a world coordinate /// </summary> Vector3 getWorldVector3(Vector3Param r) { int l = L; int m = M; int n = N; Vector3 tS = Vector3.zero; for(int i = 0; i <= l; i++){ Vector3 tM = Vector3.zero; for(int j = 0; j <= m; j++){ Vector3 tK = Vector3.zero; for(int k = 0; k <= n; k++){ tK += r.bernPolyPack[2][k] * controlPoints[i,j,k].transform.position; } tM += r.bernPolyPack[1][j] * tK; } tS += r.bernPolyPack[0][i] * tM; } return tS; }
/// <summary> /// Calculate the trivariate bernstein polynomial as described by [1] /// /// My method adapts [1] slightly by precalculating the BP coefficients and storing /// them in Vector3Param. When it comes time to extract a world coordinate, /// it's just a matter of summing up multiplications through each polynomial from eq (2). /// </summary> /// <links> /// [1] - Method based on: http://pages.cpsc.ucalgary.ca/~blob/papers/others/ffd.pdf /// </links> /// <param name="p0">Origin of our coordinate system (where STU meet)</param> void calculateTrivariateBernsteinPolynomial(Vector3 p0) { Vector3 TcU = Vector3.Cross(T, U); Vector3 ScU = Vector3.Cross(S, U); Vector3 ScT = Vector3.Cross(S, T); float TcUdS = Vector3.Dot(TcU, S); float ScUdT = Vector3.Dot(ScU, T); float ScTdU = Vector3.Dot(ScT, U); for (int v = 0; v < originalVertices.Length; v++) { Vector3 diff = originalVertices[v] - p0; Vector3Param tmp = new Vector3Param(); tmp.s = Vector3.Dot(TcU, diff / TcUdS); tmp.t = Vector3.Dot(ScU, diff / ScUdT); tmp.u = Vector3.Dot(ScT, diff / ScTdU); tmp.p = p0 + (tmp.s * S) + (tmp.t * T) + (tmp.u * U); tmp.p0 = p0; tmp.bernPolyPack = new List<List<float>>(); { // Reserve room for each bernstein polynomial pack. tmp.bernPolyPack.Add(new List<float>(L)); //outer bernstein poly tmp.bernPolyPack.Add(new List<float>(M)); //middle bernstein poly tmp.bernPolyPack.Add(new List<float>(N)); //inner bernstein poly } { // Pre-calculate bernstein polynomial expansion. It only needs to be done once per parameterization for (int i = 0; i <= L; i++) { for (int j = 0; j <= M; j++) { for (int k = 0; k <= N; k++) { tmp.bernPolyPack[2].Add(bernsteinPoly(N, k, tmp.u)); } tmp.bernPolyPack[1].Add(bernsteinPoly(M, j, tmp.t)); } tmp.bernPolyPack[0].Add(bernsteinPoly(L, i, tmp.s)); } } vertexParams.Add(tmp); if (Vector3.Distance(tmp.p, originalVertices[v]) > 0.001f) { //Debug.Log("Warning, mismatched parameterization"); } } }