Skip to content

Instantly share code, notes, and snippets.

@d3x0r
Last active June 11, 2020 04:30
Show Gist options
  • Select an option

  • Save d3x0r/e23fc4509070a96fe4e08e23601f2695 to your computer and use it in GitHub Desktop.

Select an option

Save d3x0r/e23fc4509070a96fe4e08e23601f2695 to your computer and use it in GitHub Desktop.

Revisions

  1. d3x0r revised this gist Jun 11, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion lnQuat.js
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    // theta - angle in radians, d = {x, y, z } direction/unnormalized
    function lnQuat( theta, d ){
    // if no rotation, then nothing.
    const dl = 1/Math.sqrt( d.x*d.x + d.y*d.y + d.z*d.z );
    const dl = 1/Math.sqrt( d.x*d.x + d.y*d.y + d.z*d.z ); // 1/ d length = normalize d

    const t = theta/2;
    const ct2 = Math.cos( t ); // sqrt( 1/2(1 + cos theta)) - half angle subst
  2. d3x0r revised this gist Jun 11, 2020. 1 changed file with 13 additions and 6 deletions.
    19 changes: 13 additions & 6 deletions lnQuat.js
    Original file line number Diff line number Diff line change
    @@ -4,15 +4,22 @@ function lnQuat( theta, d ){
    const dl = 1/Math.sqrt( d.x*d.x + d.y*d.y + d.z*d.z );

    const t = theta/2;
    const ct2 = Math.cos( t ); // sqrt( 1/2(1 + cos theta))
    const st2 = Math.sin( t ); // sqrt( 1/2(1 - cos theta))
    const w = ct2; // sqrt( 1/2(1 + cos theta))
    const x = dl * d.x * st2; // sqrt( 1/2(1 - cos theta))
    const y = dl * d.y * st2; //
    const ct2 = Math.cos( t ); // sqrt( 1/2(1 + cos theta)) - half angle subst
    const st2 = Math.sin( t ); // sqrt( 1/2(1 - cos theta)) - half angle subst
    const w = ct2;
    const x = dl * d.x * st2;
    const y = dl * d.y * st2;
    const z = dl * d.z * st2;

    // sqrt( 1/2(1 + cos theta)) * sqrt( 1/2(1 + cos theta))
    // + sqrt( 1/2(1 - cos theta) )*sqrt( 1/2(1 - cos theta)) * x*x
    // + sqrt( 1/2(1 - cos theta) )*sqrt( 1/2(1 - cos theta)) * y*y
    // + sqrt( 1/2(1 - cos theta) )*sqrt( 1/2(1 - cos theta)) * z*z )

    // for R below, substitute terms... (also factor x,y,z from sin value)
    // sqrt( 1/2(1 + cos theta)) * sqrt( 1/2(1 + cos theta)) + sqrt( 1/2(1 - cos theta))*sqrt( 1/2(1 - cos theta)) * ( x*x+y*y+z*z )
    // sqrt( 1/2(1 + cos theta)) * sqrt( 1/2(1 + cos theta))
    // + sqrt( 1/2(1 - cos theta))*sqrt( 1/2(1 - cos theta))
    // * ( x*x+y*y+z*z )

    // collapse sqrt(n)*sqrt(n) to n
    // 1/2(1 + cos theta) + 1/2(1 - cos theta) * ( x*x+y*y+z*z )
  3. d3x0r revised this gist Jun 11, 2020. 1 changed file with 12 additions and 9 deletions.
    21 changes: 12 additions & 9 deletions lnQuat.js
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,5 @@
    function lnQuat( theta, d, a, b ){
    if( "undefined" !== typeof theta ) {
    if( "undefined" !== typeof a ) {
    // create with 4 raw coordinates
    this.w = theta;
    this.x = d;
    this.y = a;
    this.z = b;
    }else {
    // theta - angle in radians, d = {x, y, z } direction/unnormalized
    function lnQuat( theta, d ){
    // if no rotation, then nothing.
    const dl = 1/Math.sqrt( d.x*d.x + d.y*d.y + d.z*d.z );

    @@ -17,10 +10,20 @@ function lnQuat( theta, d, a, b ){
    const x = dl * d.x * st2; // sqrt( 1/2(1 - cos theta))
    const y = dl * d.y * st2; //
    const z = dl * d.z * st2;

    // for R below, substitute terms... (also factor x,y,z from sin value)
    // sqrt( 1/2(1 + cos theta)) * sqrt( 1/2(1 + cos theta)) + sqrt( 1/2(1 - cos theta))*sqrt( 1/2(1 - cos theta)) * ( x*x+y*y+z*z )

    // collapse sqrt(n)*sqrt(n) to n
    // 1/2(1 + cos theta) + 1/2(1 - cos theta) * ( x*x+y*y+z*z )

    // the length of the direction part is normalized and is always 1
    // 1/2(1 + cos theta) + 1/2(1 - cos theta) * (1)

    // 1/2 ( 1 + cos theta + 1 - cos theta)

    // so this should always be 1??
    // 1
    const r = w*w + x*x+y*y+z*z ;

    }
  4. d3x0r created this gist Jun 11, 2020.
    26 changes: 26 additions & 0 deletions lnQuat.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    function lnQuat( theta, d, a, b ){
    if( "undefined" !== typeof theta ) {
    if( "undefined" !== typeof a ) {
    // create with 4 raw coordinates
    this.w = theta;
    this.x = d;
    this.y = a;
    this.z = b;
    }else {
    // if no rotation, then nothing.
    const dl = 1/Math.sqrt( d.x*d.x + d.y*d.y + d.z*d.z );

    const t = theta/2;
    const ct2 = Math.cos( t ); // sqrt( 1/2(1 + cos theta))
    const st2 = Math.sin( t ); // sqrt( 1/2(1 - cos theta))
    const w = ct2; // sqrt( 1/2(1 + cos theta))
    const x = dl * d.x * st2; // sqrt( 1/2(1 - cos theta))
    const y = dl * d.y * st2; //
    const z = dl * d.z * st2;
    // sqrt( 1/2(1 + cos theta)) * sqrt( 1/2(1 + cos theta)) + sqrt( 1/2(1 - cos theta))*sqrt( 1/2(1 - cos theta)) * ( x*x+y*y+z*z )

    // 1/2(1 + cos theta) + 1/2(1 - cos theta) * ( x*x+y*y+z*z )
    // 1/2(1 + cos theta) + 1/2(1 - cos theta) * (1)
    // 1/2 ( 1 + cos theta + 1 - cos theta)
    // 1
    const r = w*w + x*x+y*y+z*z ;