diff --git a/basis/math/quaternions/quaternions-docs.factor b/basis/math/quaternions/quaternions-docs.factor index a24011cb7c..1a381c6287 100644 --- a/basis/math/quaternions/quaternions-docs.factor +++ b/basis/math/quaternions/quaternions-docs.factor @@ -4,17 +4,17 @@ IN: math.quaternions HELP: q+ { $values { "u" "a quaternion" } { "v" "a quaternion" } { "u+v" "a quaternion" } } { $description "Add quaternions." } -{ $examples { $example "USING: math.quaternions prettyprint ;" "{ C{ 0 1 } 0 } { 0 1 } q+ ." "{ C{ 0 1 } 1 }" } } ; +{ $examples { $example "USING: math.quaternions prettyprint ;" "{ 0 1 0 0 } { 0 0 1 0 } q+ ." "{ 0 1 1 0 }" } } ; HELP: q- { $values { "u" "a quaternion" } { "v" "a quaternion" } { "u-v" "a quaternion" } } { $description "Subtract quaternions." } -{ $examples { $example "USING: math.quaternions prettyprint ;" "{ C{ 0 1 } 0 } { 0 1 } q- ." "{ C{ 0 1 } -1 }" } } ; +{ $examples { $example "USING: math.quaternions prettyprint ;" "{ 0 1 0 0 } { 0 0 1 0 } q- ." "{ 0 1 -1 0 }" } } ; HELP: q* { $values { "u" "a quaternion" } { "v" "a quaternion" } { "u*v" "a quaternion" } } { $description "Multiply quaternions." } -{ $examples { $example "USING: math.quaternions prettyprint ;" "{ C{ 0 1 } 0 } { 0 1 } q* ." "{ 0 C{ 0 1 } }" } } ; +{ $examples { $example "USING: math.quaternions prettyprint ;" "{ 0 1 0 0 } { 0 0 1 0 } q* ." "{ 0 0 0 1 }" } } ; HELP: qconjugate { $values { "u" "a quaternion" } { "u'" "a quaternion" } } @@ -27,28 +27,17 @@ HELP: qrecip HELP: q/ { $values { "u" "a quaternion" } { "v" "a quaternion" } { "u/v" "a quaternion" } } { $description "Divide quaternions." } -{ $examples { $example "USING: math.quaternions prettyprint ;" "{ 0 C{ 0 1 } } { 0 1 } q/ ." "{ C{ 0 1 } 0 }" } } ; +{ $examples { $example "USING: math.quaternions prettyprint ;" "{ 0 0 0 1 } { 0 0 1 0 } q/ ." "{ 0 1 0 0 }" } } ; HELP: q*n -{ $values { "q" "a quaternion" } { "n" number } { "q" "a quaternion" } } -{ $description "Multiplies each element of " { $snippet "q" } " by " { $snippet "n" } "." } -{ $notes "You will get the wrong result if you try to multiply a quaternion by a complex number on the right using " { $link v*n } ". Use this word instead." - $nl "Note that " { $link v*n } " with a quaternion and a real is okay." } ; +{ $values { "q" "a quaternion" } { "n" real } { "q" "a quaternion" } } +{ $description "Multiplies each element of " { $snippet "q" } " by real value " { $snippet "n" } "." } +{ $notes "To multiply a quaternion with a complex value, use " { $link c>q } " " { $link q* } "." } ; HELP: c>q { $values { "c" number } { "q" "a quaternion" } } { $description "Turn a complex number into a quaternion." } -{ $examples { $example "USING: math.quaternions prettyprint ;" "C{ 0 1 } c>q ." "{ C{ 0 1 } 0 }" } } ; - -HELP: v>q -{ $values { "v" vector } { "q" "a quaternion" } } -{ $description "Turn a 3-vector into a quaternion with real part 0." } -{ $examples { $example "USING: math.quaternions prettyprint ;" "{ 1 0 0 } v>q ." "{ C{ 0 1 } 0 }" } } ; - -HELP: q>v -{ $values { "q" "a quaternion" } { "v" vector } } -{ $description "Get the vector part of a quaternion, discarding the real part." } -{ $examples { $example "USING: math.quaternions prettyprint ;" "{ C{ 0 1 } 0 } q>v ." "{ 1 0 0 }" } } ; +{ $examples { $example "USING: math.quaternions prettyprint ;" "C{ 0 1 } c>q ." "{ 0 1 0 0 }" } } ; HELP: euler { $values { "phi" number } { "theta" number } { "psi" number } { "q" "a quaternion" } } diff --git a/basis/math/quaternions/quaternions-tests.factor b/basis/math/quaternions/quaternions-tests.factor index 3efc417e42..b049b6dbc4 100644 --- a/basis/math/quaternions/quaternions-tests.factor +++ b/basis/math/quaternions/quaternions-tests.factor @@ -2,6 +2,12 @@ IN: math.quaternions.tests USING: tools.test math.quaternions kernel math.vectors math.constants ; +CONSTANT: q0 { 0 0 0 0 } +CONSTANT: q1 { 1 0 0 0 } +CONSTANT: qi { 0 1 0 0 } +CONSTANT: qj { 0 0 1 0 } +CONSTANT: qk { 0 0 0 1 } + [ 1.0 ] [ qi norm ] unit-test [ 1.0 ] [ qj norm ] unit-test [ 1.0 ] [ qk norm ] unit-test @@ -10,18 +16,13 @@ math.constants ; [ t ] [ qi qj q* qk = ] unit-test [ t ] [ qj qk q* qi = ] unit-test [ t ] [ qk qi q* qj = ] unit-test -[ t ] [ qi qi q* q1 v+ q0 = ] unit-test -[ t ] [ qj qj q* q1 v+ q0 = ] unit-test -[ t ] [ qk qk q* q1 v+ q0 = ] unit-test -[ t ] [ qi qj qk q* q* q1 v+ q0 = ] unit-test -[ t ] [ C{ 0 1 } qj n*v qk = ] unit-test -[ t ] [ qj C{ 0 1 } q*n qk v+ q0 = ] unit-test +[ t ] [ qi qi q* q1 q+ q0 = ] unit-test +[ t ] [ qj qj q* q1 q+ q0 = ] unit-test +[ t ] [ qk qk q* q1 q+ q0 = ] unit-test +[ t ] [ qi qj qk q* q* q1 q+ q0 = ] unit-test [ t ] [ qk qj q/ qi = ] unit-test [ t ] [ qi qk q/ qj = ] unit-test [ t ] [ qj qi q/ qk = ] unit-test -[ t ] [ qi q>v v>q qi = ] unit-test -[ t ] [ qj q>v v>q qj = ] unit-test -[ t ] [ qk q>v v>q qk = ] unit-test [ t ] [ 1 c>q q1 = ] unit-test [ t ] [ C{ 0 1 } c>q qi = ] unit-test [ t ] [ qi qi q+ qi 2 q*n = ] unit-test diff --git a/basis/math/quaternions/quaternions.factor b/basis/math/quaternions/quaternions.factor index b713f44ebd..c36bf2a13d 100644 --- a/basis/math/quaternions/quaternions.factor +++ b/basis/math/quaternions/quaternions.factor @@ -1,72 +1,69 @@ ! Copyright (C) 2005, 2007 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. -USING: arrays kernel math math.functions math.vectors sequences ; +USING: arrays combinators kernel math math.functions math.libm math.vectors sequences ; IN: math.quaternions -! Everybody's favorite non-commutative skew field, the quaternions! +: q+ ( u v -- u+v ) + v+ ; inline -! Quaternions are represented as pairs of complex numbers, using the -! identity: (a+bi)+(c+di)j = a+bi+cj+dk. +: q- ( u v -- u-v ) + v- ; inline -: q+ ( u v -- u+v ) - v+ ; - -: q- ( u v -- u-v ) - v- ; - : q* ( u v -- u*v ) - [ q*a ] [ q*b ] 2bi 2array ; + { + [ [ { 1 0 0 0 } vshuffle ] [ { 1 1 2 3 } vshuffle ] bi* v* ] + [ [ { 2 1 2 3 } vshuffle ] [ { 2 0 0 0 } vshuffle ] bi* v* v+ ] + [ [ { 3 2 3 1 } vshuffle ] [ { 3 3 1 2 } vshuffle ] bi* v* v+ ] + [ [ { 0 3 1 2 } vshuffle ] [ { 0 2 3 1 } vshuffle ] bi* v* v- ] + } 2cleave (q*sign) ; inline -: qconjugate ( u -- u' ) - first2 [ conjugate ] [ neg ] bi* 2array ; +GENERIC: qconjugate ( u -- u' ) +M: object qconjugate ( u -- u' ) + { 1 -1 -1 -1 } v* ; inline : qrecip ( u -- 1/u ) - qconjugate dup norm-sq v/n ; + qconjugate dup norm-sq v/n ; inline : q/ ( u v -- u/v ) - qrecip q* ; + qrecip q* ; inline + +: n*q ( q n -- q ) + v*n ; inline : q*n ( q n -- q ) - conjugate v*n ; + v*n ; inline + +: n>q ( n -- q ) + 0 0 0 4array ; inline + +: n>q-like ( c exemplar -- q ) + [ 0 0 0 ] dip 4sequence ; inline : c>q ( c -- q ) - 0 2array ; + >rect 0 0 4array ; inline -: v>q ( v -- q ) - first3 rect> [ 0 swap rect> ] dip 2array ; - -: q>v ( q -- v ) - first2 [ imaginary-part ] dip >rect 3array ; - -! Zero -CONSTANT: q0 { 0 0 } - -! Units -CONSTANT: q1 { 1 0 } -CONSTANT: qi { C{ 0 1 } 0 } -CONSTANT: qj { 0 1 } -CONSTANT: qk { 0 C{ 0 1 } } +: c>q-like ( c exemplar -- q ) + [ >rect 0 0 ] dip 4sequence ; inline ! Euler angles q ] [ sin ] bi ] dip n*v v- ; +: (euler) ( theta exemplar shuffle -- q ) + swap + [ 0.5 * [ fcos ] [ fsin ] bi 0.0 0.0 ] [ call ] [ 4sequence ] tri* ; inline PRIVATE> +: euler-like ( phi theta psi exemplar -- q ) + [ [ ] (euler) ] [ [ swapd ] (euler) ] [ [ rot ] (euler) ] tri-curry tri* q* q* ; inline + : euler ( phi theta psi -- q ) - [ qi (euler) ] [ qj (euler) ] [ qk (euler) ] tri* q* q* ; + { } euler-like ; inline diff --git a/basis/tools/deploy/shaker/shaker.factor b/basis/tools/deploy/shaker/shaker.factor index c9485a458c..5897712a02 100755 --- a/basis/tools/deploy/shaker/shaker.factor +++ b/basis/tools/deploy/shaker/shaker.factor @@ -106,18 +106,12 @@ IN: tools.deploy.shaker : strip-word-props ( stripped-props words -- ) "Stripping word properties" show - [ - swap '[ - [ - [ drop _ member? not ] assoc-filter sift-assoc - >alist f like - ] change-props drop - ] each - ] [ - H{ } clone '[ - [ [ _ [ ] cache ] map ] change-props drop - ] each - ] bi ; + swap '[ + [ + [ drop _ member? not ] assoc-filter sift-assoc + >alist f like + ] change-props drop + ] each ; : stripped-word-props ( -- seq ) [ diff --git a/basis/tools/memory/memory.factor b/basis/tools/memory/memory.factor index 69b19899b8..0c55612466 100644 --- a/basis/tools/memory/memory.factor +++ b/basis/tools/memory/memory.factor @@ -242,8 +242,6 @@ M: code-blocks nth-unsafe [ cache>> ] [ blocks>> ] bi '[ _ nth-unsafe ] cache ; inline -M: code-blocks hashcode* 2drop 0 ; - INSTANCE: code-blocks immutable-sequence : code-blocks ( -- blocks ) diff --git a/core/classes/tuple/tuple-tests.factor b/core/classes/tuple/tuple-tests.factor index c4c2e83e95..fe55365f46 100644 --- a/core/classes/tuple/tuple-tests.factor +++ b/core/classes/tuple/tuple-tests.factor @@ -706,14 +706,6 @@ ERROR: derived-error < base-error z ; [ (( x y z -- * )) ] [ \ derived-error stack-effect ] unit-test -USE: classes.struct - -[ { } ] [ - classes - [ "prototype" word-prop ] map - [ '[ _ hashcode drop f ] [ drop t ] recover ] filter -] unit-test - ! Make sure that tuple reshaping updates code heap roots TUPLE: code-heap-ref ; diff --git a/extra/math/matrices/simd/simd-tests.factor b/extra/math/matrices/simd/simd-tests.factor index b27abcae67..a3a68b350b 100644 --- a/extra/math/matrices/simd/simd-tests.factor +++ b/extra/math/matrices/simd/simd-tests.factor @@ -238,3 +238,42 @@ IN: math.matrices.simd.tests float-4{ 0.5 0.5 0.5 1.0 } scale-matrix4 m4. float-4{ 2.0 3.0 4.0 1.0 } m4.v ] unit-test + +[ + S{ matrix4 f + float-4-array{ + float-4{ 1.0 0.0 0.0 0.0 } + float-4{ 0.0 1.0 0.0 0.0 } + float-4{ 0.0 0.0 1.0 0.0 } + float-4{ 0.0 0.0 0.0 1.0 } + } + } +] [ + float-4{ 1.0 0.0 0.0 0.0 } q>matrix4 +] unit-test + +[ t ] [ + pi 0.5 * 0.0 0.0 euler4 q>matrix4 + S{ matrix4 f + float-4-array{ + float-4{ 1.0 0.0 0.0 0.0 } + float-4{ 0.0 0.0 1.0 0.0 } + float-4{ 0.0 -1.0 0.0 0.0 } + float-4{ 0.0 0.0 0.0 1.0 } + } + } + 1.0e-7 m~ +] unit-test + +[ t ] [ + 0.0 pi 0.25 * 0.0 euler4 q>matrix4 + S{ matrix4 f + float-4-array{ + float-4{ $[ 1/2. sqrt ] 0.0 $[ 1/2. sqrt neg ] 0.0 } + float-4{ 0.0 1.0 0.0 0.0 } + float-4{ $[ 1/2. sqrt ] 0.0 $[ 1/2. sqrt ] 0.0 } + float-4{ 0.0 0.0 0.0 1.0 } + } + } + 1.0e-7 m~ +] unit-test diff --git a/extra/math/matrices/simd/simd.factor b/extra/math/matrices/simd/simd.factor index 4e1fd0e96c..01d831d6b0 100644 --- a/extra/math/matrices/simd/simd.factor +++ b/extra/math/matrices/simd/simd.factor @@ -1,8 +1,10 @@ ! (c)Joe Groff bsd license USING: accessors classes.struct fry generalizations kernel locals math math.combinatorics math.functions math.matrices.simd math.vectors -math.vectors.simd sequences sequences.private specialized-arrays +math.vectors.simd math.quaternions sequences sequences.private specialized-arrays typed ; +FROM: sequences.private => nth-unsafe ; +FROM: math.quaternions.private => (q*sign) ; QUALIFIED-WITH: alien.c-types c SPECIALIZED-ARRAY: float-4 IN: math.matrices.simd @@ -23,10 +25,7 @@ M: matrix4 new-sequence 2drop matrix4 (struct) ; inline :: set-columns ( c1 c2 c3 c4 c -- c ) c columns>> :> columns - c1 columns set-first - c2 columns set-second - c3 columns set-third - c4 columns set-fourth + c1 c2 c3 c4 columns 4 set-firstn-unsafe c ; inline : make-matrix4 ( quot: ( -- c1 c2 c3 c4 ) -- c ) @@ -151,12 +150,24 @@ TYPED: translation-matrix4 ( offset: float-4 -- matrix: matrix4 ) ] dip ] make-matrix4 ; +:: (rotation-matrix4) ( diagonal triangle-hi triangle-lo -- matrix ) + matrix4 (struct) :> triangle-m + diagonal scale-matrix4 :> diagonal-m + + triangle-hi { 3 2 1 3 } vshuffle + triangle-hi { 3 3 0 3 } vshuffle triangle-lo { 2 3 3 3 } vshuffle vbitor + triangle-lo { 1 0 3 3 } vshuffle + float-4 new + + triangle-m set-columns drop + + diagonal-m triangle-m m4+ ; inline + TYPED:: rotation-matrix4 ( axis: float-4 theta: float -- matrix: matrix4 ) ! x*x + c*(1.0 - x*x) x*y*(1.0 - c) + s*z x*z*(1.0 - c) - s*y 0 ! x*y*(1.0 - c) - s*z y*y + c*(1.0 - y*y) y*z*(1.0 - c) + s*x 0 ! x*z*(1.0 - c) + s*y y*z*(1.0 - c) - s*x z*z + c*(1.0 - z*z) 0 ! 0 0 0 1 - matrix4 (struct) :> triangle-m theta cos :> c theta sin :> s @@ -176,17 +187,8 @@ TYPED:: rotation-matrix4 ( axis: float-4 theta: float -- matrix: matrix4 ) triangle-a triangle-b v+ :> triangle-lo triangle-a triangle-b v- :> triangle-hi - diagonal scale-matrix4 :> diagonal-m - - triangle-hi { 3 2 1 3 } vshuffle - triangle-hi { 3 3 0 3 } vshuffle triangle-lo { 2 3 3 3 } vshuffle v+ - triangle-lo { 1 0 3 3 } vshuffle - float-4 new - - triangle-m set-columns drop - - diagonal-m triangle-m m4+ ; - + diagonal triangle-hi triangle-lo (rotation-matrix4) ; + TYPED:: frustum-matrix4 ( xy: float-4 near: float far: float -- matrix: matrix4 ) [ near near near far + 2 near far * * float-4-boa ! num @@ -200,3 +202,30 @@ TYPED:: frustum-matrix4 ( xy: float-4 near: float far: float -- matrix: matrix4 [ negone (vmerge) ] bi* ] make-matrix4 ; +! interface with quaternions +M: float-4 (q*sign) + float-4{ -0.0 0.0 0.0 0.0 } vbitxor ; inline +M: float-4 qconjugate + float-4{ 0.0 -0.0 -0.0 -0.0 } vbitxor ; inline + +: euler4 ( phi theta psi -- q ) + float-4{ 0 0 0 0 } euler-like ; inline + +TYPED:: q>matrix4 ( q: float-4 -- matrix: matrix4 ) + ! a*a + b*b - c*c - d*d 2*b*c - 2*a*d 2*b*d + 2*a*c 0 + ! 2*b*c + 2*a*d a*a - b*b + c*c - d*d 2*c*d - 2*a*b 0 + ! 2*b*d - 2*a*c 2*c*d + 2*a*b a*a - b*b - c*c + d*d 0 + ! 0 0 0 1 + q { 2 1 1 3 } vshuffle q { 3 3 2 3 } vshuffle v* :> triangle-a + q { 0 0 0 3 } vshuffle q { 1 2 3 3 } vshuffle v* :> triangle-b + + triangle-a float-4{ 2.0 2.0 2.0 0.0 } v* triangle-b float-4{ -2.0 2.0 -2.0 0.0 } v* + [ v- ] [ v+ ] 2bi :> ( triangle-hi triangle-lo ) + + q q v* first4 { + [ [ + ] [ - ] [ - ] tri* ] + [ [ - ] [ + ] [ - ] tri* ] + [ [ - ] [ - ] [ + ] tri* ] + } 4 ncleave 1.0 float-4-boa :> diagonal + + diagonal triangle-hi triangle-lo (rotation-matrix4) ;