Difference between revisions of "Dev:Linking to Blines - Equations"

From Synfig Studio :: Documentation
Jump to: navigation, search
m (One spline)
(full rework)
Line 8: Line 8:
  
 
* <math>(x,y) = (1-u)^3 (x_1,y_1) + 3 u(1-u)^2  (x^{t2}_1,y^{t2}_1) + 3 u^2 (1-u)  (x^{t1}_2,y^{t1}_2) + u^3 (x_2,y_2)</math> - bline point
 
* <math>(x,y) = (1-u)^3 (x_1,y_1) + 3 u(1-u)^2  (x^{t2}_1,y^{t2}_1) + 3 u^2 (1-u)  (x^{t1}_2,y^{t1}_2) + u^3 (x_2,y_2)</math> - bline point
* <math>(x^{t1},y^{t1}) = (1-u)^2 (x_1,y_1) + 2u(1-u)(x^{t2}_1,y^{t2}_1) + u^2(x^{t1}_2,y^{t1}_2)</math> - coordinates of yellow tangent for current bline point
 
* <math>(x^{t2},y^{t2}) = (1-u)^2 (x^{t2}_1,y^{t2}_1) + 2u(1-u)(x^{t1}_2,y^{t1}_2) + u^2(x_2,y_2)</math> - red tangent of bline point
 
  
=== How I found tangents formula ===
+
==== Relative tangents ====
 
+
A,B,C,D defining spline segment. A,D - vertices, B,C - tangents:
+
 
+
<math>A = (x_1,y_1); B=(x^{t2}_1,y^{t2}_1); C=(x^{t1}_2,y^{t1}_2);  D = (x_2,y_2)</math>
+
 
+
* put a point on each line, some percentage of the way along each
+
** M on (A,B): <math>(x_1,y_1)(1-u) + (x^{t2}_1,y^{t2}_1)u</math>
+
** N on (B,C): <math>(x^{t2}_1,y^{t2}_1)(1-u) + (x^{t1}_2,y^{t1}_2)u</math>
+
** K on (C,D): <math>(x^{t1}_2,y^{t1}_2)(1-u) + (x^2,y^2)u</math>
+
* then draw new lines from the point on A-B to the point on B-C, and from the point on B-C to the point on C-D; and put new points on those 2, the same percentage of the way along each
+
** T1 on (M,N): <math>M(1-u) + N u = ((x_1,y_1)(1-u) + (x^{t2}_1,y^{t2}_1)u)(1-u) + ((x^{t2}_1,y^{t2}_1)(1-u) + (x^t_2,y^t_2)u)u = </math><math> =(1-u)^2 (x_1,y_1) + 2u(1-u)(x^{t2}_1,y^{t2}_1) + u^2(x^{t1}_2,y^{t1}_2)</math>
+
** T2 on (N,K): <math>N(1-u) + K u = ((x^{t2}_1,y^{t2}_1)(1-u) + (x^{t1}_2,y^{t1}_2)u)(1-u) + ((x^{t1}_2,y^{t1}_2)(1-u) + (x^2,y^2)u) u = </math><math> = (1-u)^2 (x^{t2}_1,y^{t2}_1) + 2u(1-u)(x^{t1}_2,y^{t1}_2) + u^2(x_2,y_2)</math>
+
 
+
==== Relative tangent coordinates ====
+
  
 
O'kay, I know, the <math>(x^{t1},y^{t1}), (x^{t2},y^{t2})</math> defining absolute position of tangents, but in synfig we have their coordinates relative to vertex. Moreover, coordinates of yellow tangent are inverted.
 
O'kay, I know, the <math>(x^{t1},y^{t1}), (x^{t2},y^{t2})</math> defining absolute position of tangents, but in synfig we have their coordinates relative to vertex. Moreover, coordinates of yellow tangent are inverted.
Line 41: Line 25:
  
 
* Bline point: <math>(x,y) = (1-(3u^2-2u^3)) (x_1,y_1) + 3u(1-u)^2 (\Delta x ^{t2}_1,\Delta y^{t2}_1) - 3u^2(1-u) (\Delta x^{t1}_2, \Delta y^{t1}_2) + (3u^2-2u^3) (x_2,y_2)</math>
 
* Bline point: <math>(x,y) = (1-(3u^2-2u^3)) (x_1,y_1) + 3u(1-u)^2 (\Delta x ^{t2}_1,\Delta y^{t2}_1) - 3u^2(1-u) (\Delta x^{t1}_2, \Delta y^{t1}_2) + (3u^2-2u^3) (x_2,y_2)</math>
* Yellow tangent: <math>(\Delta x ^{t1}, \Delta y ^{t1}) = (x,y) - (1-u^2) (x_1,y_1) - 2u(1-u)(\Delta x^{t2}_1, \Delta y^{t2}_1) + u^2 (\Delta x^{t1}_2, \Delta y^{t1}_2) - u^2(x_2,y_2)</math>
+
 
* Red tangent: <math>(\Delta x ^{t2}, \Delta y ^{t2}) = (1-u)^2(x_1,y_1) + (1-u)^2(\Delta x^{t2}_1, \Delta y^{t2}_1) - 2u(1-u)( \Delta x^{t1}_2, \Delta y^{t1}_2) + (2u-u^2)(x_2,y_2) - (x,y)</math>
+
=== Tangent coordinates ===
 +
 
 +
<pre>
 +
<dooglus> look at etl/_calculus.h
 +
<dooglus> class derivative<hermite<T> >
 +
<dooglus>  T a = func[0], b = func[1], c = func[2], d = func[3];
 +
<dooglus>  typename hermite<T>::argument_type y(1-x);
 +
<dooglus>  return ((b-a)*y*y + (c-b)*x*y*2 + (d-c)*x*x) * 3;
 +
</pre>
 +
 
 +
: Can't understand, why *3? --[[User:Zelgadis|Zelgadis]] 05:31, 8 March 2008 (EST)
 +
 
 +
For x:
 +
* <math>\Delta x^{t1} = \Delta x^{t2} = (1-u)^2 \Delta x^{t2}_1 + 2u(1-u) ((x_2 + \Delta x^{t1}_2) - (x_1 + \Delta x^{t2}_1)) + u^2 \Delta x^{t1}_2   = </math> <math>= -2u(1-u)x_1 + (1-u^2)\Delta x^{t2}_1 + u(2-u)\Delta x^{t1}_2 + u(1-u)x_2</math>
  
 
== One spline ==
 
== One spline ==
Line 53: Line 50:
 
* <math>x^{t2}_{A1} = c_2</math>
 
* <math>x^{t2}_{A1} = c_2</math>
 
* <math>x_{A2} = (1-(3u^2-2u^3)) x_{A1} + 3u(1-u)^2 \Delta x ^{t2}_{A1} - 3u^2(1-u) \Delta x^{t1}_{A2} + (3u^2-2u^3) x_{A2}</math>
 
* <math>x_{A2} = (1-(3u^2-2u^3)) x_{A1} + 3u(1-u)^2 \Delta x ^{t2}_{A1} - 3u^2(1-u) \Delta x^{t1}_{A2} + (3u^2-2u^3) x_{A2}</math>
*  
+
* <math>\Delta x^{t1}_{A2} = -2u(1-u)x_{A1} + (1-u^2)\Delta x^{t2}_{A1} + u(2-u)\Delta x^{t1}_{A2} + u(1-u)x_{A2}</math>
 +
 
 +
Transform:
 +
 
 +
* <math>x_{A1} = c_1</math>
 +
* <math>x^{t2}_{A1} = c_2 </math>
 +
* <math>(1-(3u^2-2u^3)) x_{A1} + 3u(1-u)^2 \Delta x ^{t2}_{A1} - 3u^2(1-u) \Delta x^{t1}_{A2} + (3u^2-2u^3-1) x_{A2} = 0</math>
 +
* <math>-2u(1-u)x_{A1} + (1-u^2)\Delta x^{t2}_{A1} + (u(2-u)-1)\Delta x^{t1}_{A2} + u(1-u)x_{A2} = 0</math>
 +
 
 +
So, we must solve the 4x4 matrix equation:
 +
<math>\begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 &0 \\ (1-(3u^2-2u^3)) & 3u(1-u)^2 & -3u^2(1-u) & (3u^2-2u^3)-1 \\ (-2u(1-u) & (1-u^2) & u(2-u)-1 & u(1-u) \end{pmatrix} \cdot \begin{pmatrix} x_{A1} \\ x^{t2}_{A1} \\ x^{t1}_{A2} \\ x_{A2} \end{pmatrix} = \begin{pmatrix} c_1 \\ c_2 \\ 0 \\ 0 \end{pmatrix}</math>
 +
 
  
 
dooglus, can you check how this formula works in code? Something like:
 
dooglus, can you check how this formula works in code? Something like:
Line 60: Line 68:
 
* Place (not link!) at the position calculated by this formula. If we'll have A2 on bline after that then it's ok for now, if not - something wrong.
 
* Place (not link!) at the position calculated by this formula. If we'll have A2 on bline after that then it's ok for now, if not - something wrong.
 
I know we need a general formula but if this won't work then I definitely not right.
 
I know we need a general formula but if this won't work then I definitely not right.
 +
 +
 +
Case 2: Bline A. A2 linked to A without tangent.
 +
 +
The 4th line just replaced with:
 +
* <math>\Delta x^{t1}_{A2} = c_3</math>
  
 
== Two splines ==
 
== Two splines ==
...
+
 
 +
If we got circular reference with two blines, then we must solve equation with matrix 8x8.
 +
 
 +
== Conclusion ==
 +
 
 +
Maybe formulas are not correct, but I think you've got the idea:
 +
For N Bline segments in loop we have 4N equations. If vertex is linked then we got appropriate equation, depending on what is linked. If vertex is not linked - just assigning constants.

Revision as of 12:31, 8 March 2008

Main equations

  • Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://api.formulasearchengine.com/v1/":): {\displaystyle (x_1,y_1), (x_2,y_2)} - vertices, defining bline segment
  • Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://api.formulasearchengine.com/v1/":): {\displaystyle (x,y) } - current bline point
  • Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://api.formulasearchengine.com/v1/":): {\displaystyle (x^{t2}_1,y^{t2}_1), (x^{t1}_2,y^{t1}_2)} - tangent points defining bline segment
  • Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://api.formulasearchengine.com/v1/":): {\displaystyle (x^{t1},y^{t1}), (x^{t2},y^{t2})} - coordinates of yellow (t1) and red (t2) tangents (absolute)
  • u - Amount of current segment, [0,1]
  • Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://api.formulasearchengine.com/v1/":): {\displaystyle (x,y) = (1-u)^3 (x_1,y_1) + 3 u(1-u)^2 (x^{t2}_1,y^{t2}_1) + 3 u^2 (1-u) (x^{t1}_2,y^{t1}_2) + u^3 (x_2,y_2)} - bline point

Relative tangents

O'kay, I know, the Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://api.formulasearchengine.com/v1/":): {\displaystyle (x^{t1},y^{t1}), (x^{t2},y^{t2})} defining absolute position of tangents, but in synfig we have their coordinates relative to vertex. Moreover, coordinates of yellow tangent are inverted.

Let's say:

  • Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://api.formulasearchengine.com/v1/":): {\displaystyle (\Delta x^{t1},\Delta y^{t1})} - relative coordinates of yellow tangent
  • Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://api.formulasearchengine.com/v1/":): {\displaystyle (\Delta x^{t2},\Delta y^{t2})} - relative coordinates of red tangent

Then:

  • Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://api.formulasearchengine.com/v1/":): {\displaystyle (x^{t1},y^{t1}) = (x,y) - (\Delta x^{t1},\Delta y^{t1})}
  • Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://api.formulasearchengine.com/v1/":): {\displaystyle (x^{t2},y^{t2}) = (x,y) + (\Delta x^{t2},\Delta y^{t2})}

Make substitution:

  • Bline point: Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://api.formulasearchengine.com/v1/":): {\displaystyle (x,y) = (1-(3u^2-2u^3)) (x_1,y_1) + 3u(1-u)^2 (\Delta x ^{t2}_1,\Delta y^{t2}_1) - 3u^2(1-u) (\Delta x^{t1}_2, \Delta y^{t1}_2) + (3u^2-2u^3) (x_2,y_2)}

Tangent coordinates

<dooglus> look at etl/_calculus.h
<dooglus> class derivative<hermite<T> >
<dooglus>   T a = func[0], b = func[1], c = func[2], d = func[3];
<dooglus>   typename hermite<T>::argument_type y(1-x);
<dooglus>   return ((b-a)*y*y + (c-b)*x*y*2 + (d-c)*x*x) * 3;
Can't understand, why *3? --Zelgadis 05:31, 8 March 2008 (EST)

For x:

  • Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://api.formulasearchengine.com/v1/":): {\displaystyle \Delta x^{t1} = \Delta x^{t2} = (1-u)^2 \Delta x^{t2}_1 + 2u(1-u) ((x_2 + \Delta x^{t1}_2) - (x_1 + \Delta x^{t2}_1)) + u^2 \Delta x^{t1}_2 = } Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://api.formulasearchengine.com/v1/":): {\displaystyle = -2u(1-u)x_1 + (1-u^2)\Delta x^{t2}_1 + u(2-u)\Delta x^{t1}_2 + u(1-u)x_2}

One spline

Case 1: Bline A. A2 linked to A with tangent.

For x coordinates we have following system of equations:

  • Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://api.formulasearchengine.com/v1/":): {\displaystyle x_{A1} = c_1}
  • Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://api.formulasearchengine.com/v1/":): {\displaystyle x^{t2}_{A1} = c_2}
  • Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://api.formulasearchengine.com/v1/":): {\displaystyle x_{A2} = (1-(3u^2-2u^3)) x_{A1} + 3u(1-u)^2 \Delta x ^{t2}_{A1} - 3u^2(1-u) \Delta x^{t1}_{A2} + (3u^2-2u^3) x_{A2}}
  • Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://api.formulasearchengine.com/v1/":): {\displaystyle \Delta x^{t1}_{A2} = -2u(1-u)x_{A1} + (1-u^2)\Delta x^{t2}_{A1} + u(2-u)\Delta x^{t1}_{A2} + u(1-u)x_{A2}}

Transform:

  • Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://api.formulasearchengine.com/v1/":): {\displaystyle x_{A1} = c_1}
  • Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://api.formulasearchengine.com/v1/":): {\displaystyle x^{t2}_{A1} = c_2 }
  • Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://api.formulasearchengine.com/v1/":): {\displaystyle (1-(3u^2-2u^3)) x_{A1} + 3u(1-u)^2 \Delta x ^{t2}_{A1} - 3u^2(1-u) \Delta x^{t1}_{A2} + (3u^2-2u^3-1) x_{A2} = 0}
  • Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://api.formulasearchengine.com/v1/":): {\displaystyle -2u(1-u)x_{A1} + (1-u^2)\Delta x^{t2}_{A1} + (u(2-u)-1)\Delta x^{t1}_{A2} + u(1-u)x_{A2} = 0}

So, we must solve the 4x4 matrix equation: Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://api.formulasearchengine.com/v1/":): {\displaystyle \begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 &0 \\ (1-(3u^2-2u^3)) & 3u(1-u)^2 & -3u^2(1-u) & (3u^2-2u^3)-1 \\ (-2u(1-u) & (1-u^2) & u(2-u)-1 & u(1-u) \end{pmatrix} \cdot \begin{pmatrix} x_{A1} \\ x^{t2}_{A1} \\ x^{t1}_{A2} \\ x_{A2} \end{pmatrix} = \begin{pmatrix} c_1 \\ c_2 \\ 0 \\ 0 \end{pmatrix}}


dooglus, can you check how this formula works in code? Something like:

  • Create bline with 2 points - A1,A2
  • Select point A2, right click on bline -> "Link to bline"
  • Place (not link!) at the position calculated by this formula. If we'll have A2 on bline after that then it's ok for now, if not - something wrong.

I know we need a general formula but if this won't work then I definitely not right.


Case 2: Bline A. A2 linked to A without tangent.

The 4th line just replaced with:

  • Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://api.formulasearchengine.com/v1/":): {\displaystyle \Delta x^{t1}_{A2} = c_3}

Two splines

If we got circular reference with two blines, then we must solve equation with matrix 8x8.

Conclusion

Maybe formulas are not correct, but I think you've got the idea: For N Bline segments in loop we have 4N equations. If vertex is linked then we got appropriate equation, depending on what is linked. If vertex is not linked - just assigning constants.