github.com/jingcheng-WU/gonum@v0.9.1-0.20210323123734-f1a2a11a8f7b/num/dualquat/dual_simple_example_test.go (about)

     1  // Copyright ©2018 The Gonum Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package dualquat_test
     6  
     7  import (
     8  	"fmt"
     9  
    10  	"github.com/jingcheng-WU/gonum/num/dualquat"
    11  	"github.com/jingcheng-WU/gonum/num/quat"
    12  )
    13  
    14  func Example_displace() {
    15  	// Displace a point [3, 4, 5] by [4, 2, 6].
    16  	// See http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/other/dualQuaternion/index.htm
    17  
    18  	// Point to be transformed in the dual imaginary vector.
    19  	p := dualquat.Number{Real: quat.Number{Real: 1}, Dual: quat.Number{Imag: 3, Jmag: 4, Kmag: 5}}
    20  
    21  	// Displacement vector, half [4, 2, 6], in the dual imaginary vector.
    22  	d := dualquat.Number{Real: quat.Number{Real: 1}, Dual: quat.Number{Imag: 2, Jmag: 1, Kmag: 3}}
    23  
    24  	fmt.Println(dualquat.Mul(dualquat.Mul(d, p), dualquat.Conj(d)).Dual)
    25  
    26  	// Output:
    27  	//
    28  	// (0+7i+6j+11k)
    29  }
    30  
    31  func Example_rotate() {
    32  	// Rotate a point [3, 4, 5] by 180° around the x axis.
    33  	// See http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/other/dualQuaternion/index.htm
    34  
    35  	// Point to be transformed in the dual imaginary vector.
    36  	p := dualquat.Number{Real: quat.Number{Real: 1}, Dual: quat.Number{Imag: 3, Jmag: 4, Kmag: 5}}
    37  
    38  	// Rotation in the real quaternion.
    39  	r := dualquat.Number{Real: quat.Number{Real: 0, Imag: 1}}
    40  
    41  	fmt.Println(dualquat.Mul(dualquat.Mul(r, p), dualquat.Conj(r)).Dual)
    42  
    43  	// Output:
    44  	//
    45  	// (0+3i-4j-5k)
    46  }
    47  
    48  func Example_displaceAndRotate() {
    49  	// Displace a point [3, 4, 5] by [4, 2, 6] and then rotate
    50  	// by 180° around the x axis.
    51  	// See http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/other/dualQuaternion/index.htm
    52  
    53  	// Point to be transformed in the dual imaginary vector.
    54  	p := dualquat.Number{Real: quat.Number{Real: 1}, Dual: quat.Number{Imag: 3, Jmag: 4, Kmag: 5}}
    55  
    56  	// Displacement vector, half [4, 2, 6], in the dual imaginary vector.
    57  	d := dualquat.Number{Real: quat.Number{Real: 1}, Dual: quat.Number{Imag: 2, Jmag: 1, Kmag: 3}}
    58  
    59  	// Rotation in the real quaternion.
    60  	r := dualquat.Number{Real: quat.Number{Real: 0, Imag: 1}}
    61  
    62  	// Combine the rotation and displacement so
    63  	// the displacement is performed first.
    64  	q := dualquat.Mul(r, d)
    65  
    66  	fmt.Println(dualquat.Mul(dualquat.Mul(q, p), dualquat.Conj(q)).Dual)
    67  
    68  	// Output:
    69  	//
    70  	// (0+7i-6j-11k)
    71  }
    72  
    73  func Example_rotateAndDisplace() {
    74  	// Rotate a point [3, 4, 5] by 180° around the x axis and then
    75  	// displace by [4, 2, 6]
    76  
    77  	// Point to be transformed in the dual imaginary vector.
    78  	p := dualquat.Number{Real: quat.Number{Real: 1}, Dual: quat.Number{Imag: 3, Jmag: 4, Kmag: 5}}
    79  
    80  	// Displacement vector, half [4, 2, 6], in the dual imaginary vector.
    81  	d := dualquat.Number{Real: quat.Number{Real: 1}, Dual: quat.Number{Imag: 2, Jmag: 1, Kmag: 3}}
    82  
    83  	// Rotation in the real quaternion.
    84  	r := dualquat.Number{Real: quat.Number{Real: 0, Imag: 1}}
    85  
    86  	// Combine the rotation and displacement so
    87  	// the rotations is performed first.
    88  	q := dualquat.Mul(d, r)
    89  
    90  	fmt.Println(dualquat.Mul(dualquat.Mul(q, p), dualquat.Conj(q)).Dual)
    91  
    92  	// Output:
    93  	//
    94  	// (0+7i-2j+1k)
    95  }