summaryrefslogtreecommitdiff
path: root/src/lib/eina/eina_quaternion.c
blob: 3c268457ea245fbb8df7da86b518b7c7ae27a721 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
/* EINA - Drawing Library
 * Copyright (C) 2007-2014 Jorge Luis Zapata
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.
 * If not, see <http://www.gnu.org/licenses/>.
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include "eina_private.h"

#include <math.h>
#include <float.h>

#include "eina_fp.h"
#include "eina_matrix.h"
#include "eina_quaternion.h"

EAPI void
eina_quaternion_f16p16_set(Eina_Quaternion *out,
                           Eina_F16p16 x, Eina_F16p16 y,
                           Eina_F16p16 z, Eina_F16p16 w)
{
   out->w = w;
   out->x = x;
   out->y = y;
   out->z = z;
}

EAPI Eina_F16p16
eina_quaternion_f16p16_norm(const Eina_Quaternion_F16p16 *q)
{
   Eina_F16p16 s;

   s = eina_f16p16_add(eina_f16p16_add(eina_f16p16_mul(q->w, q->w),
                                       eina_f16p16_mul(q->x, q->x)),
                       eina_f16p16_add(eina_f16p16_mul(q->y, q->y),
                                       eina_f16p16_mul(q->z, q->z)));

   return eina_f16p16_sqrt(s);
}

EAPI void
eina_quaternion_f16p16_negative(Eina_Quaternion_F16p16 *out,
                                const Eina_Quaternion_F16p16 *in)
{
   out->w = eina_f16p16_sub(0, in->w);
   out->x = eina_f16p16_sub(0, in->x);
   out->y = eina_f16p16_sub(0, in->y);
   out->z = eina_f16p16_sub(0, in->z);
}

EAPI void
eina_quaternion_f16p16_add(Eina_Quaternion_F16p16 *out,
                           const Eina_Quaternion_F16p16 *a,
                           const Eina_Quaternion_F16p16 *b)
{
   out->w = eina_f16p16_add(a->w, b->w);
   out->x = eina_f16p16_add(a->x, b->x);
   out->y = eina_f16p16_add(a->y, b->y);
   out->z = eina_f16p16_add(a->z, b->z);
}

EAPI void
eina_quaternion_f16p16_mul(Eina_Quaternion_F16p16 *out,
                           const Eina_Quaternion_F16p16 *a,
                           const Eina_Quaternion_F16p16 *b)
{
   out->w = eina_f16p16_sub(eina_f16p16_sub(eina_f16p16_mul(a->w, b->w),
                                            eina_f16p16_mul(a->x, b->x)),
                            eina_f16p16_sub(eina_f16p16_mul(a->y, b->y),
                                            eina_f16p16_mul(a->z, b->z)));
   out->x = eina_f16p16_add(eina_f16p16_add(eina_f16p16_mul(a->w, b->x),
                                            eina_f16p16_mul(a->x, b->w)),
                            eina_f16p16_sub(eina_f16p16_mul(a->y, b->z),
                                            eina_f16p16_mul(a->z, b->y)));
   out->y = eina_f16p16_add(eina_f16p16_sub(eina_f16p16_mul(a->w, b->y),
                                            eina_f16p16_mul(a->x, b->z)),
                            eina_f16p16_add(eina_f16p16_mul(a->y, b->w),
                                            eina_f16p16_mul(a->z, b->x)));
   out->z = eina_f16p16_add(eina_f16p16_add(eina_f16p16_mul(a->w, b->z),
                                            eina_f16p16_mul(a->x, b->y)),
                            eina_f16p16_sub(eina_f16p16_mul(a->z, b->w),
                                            eina_f16p16_mul(a->y, b->x)));
}

EAPI void
eina_quaternion_f16p16_scale(Eina_Quaternion_F16p16 *out,
                             const Eina_Quaternion_F16p16 *a,
                             Eina_F16p16 b)
{
   out->w = eina_f16p16_scale(a->w, b);
   out->x = eina_f16p16_scale(a->x, b);
   out->y = eina_f16p16_scale(a->y, b);
   out->z = eina_f16p16_scale(a->z, b);
}

EAPI void
eina_quaternion_f16p16_conjugate(Eina_Quaternion_F16p16 *out,
                                 const Eina_Quaternion_F16p16 *in)
{
   out->w = in->w;
   out->x = eina_f16p16_sub(0, in->x);
   out->y = eina_f16p16_sub(0, in->y);
   out->z = eina_f16p16_sub(0, in->z);
}

EAPI Eina_F16p16
eina_quaternion_f16p16_dot(const Eina_Quaternion_F16p16 *a,
                           const Eina_Quaternion_F16p16 *b)
{
   return eina_f16p16_add(eina_f16p16_add(eina_f16p16_mul(a->w, b->w),
                                          eina_f16p16_mul(a->x, b->x)),
                          eina_f16p16_add(eina_f16p16_mul(a->y, b->y),
                                          eina_f16p16_mul(a->z, b->z)));
}

EAPI void
eina_quaternion_f16p16_normalized(Eina_Quaternion_F16p16 *out,
                                  const Eina_Quaternion_F16p16 *in)
{
   Eina_F16p16 norm;

   norm = eina_quaternion_f16p16_norm(in);
   eina_quaternion_f16p16_scale(out, in,
                                eina_f16p16_div(eina_f16p16_int_from(1),
                                                norm));
}

EAPI void
eina_quaternion_f16p16_lerp(Eina_Quaternion_F16p16 *out,
                            const Eina_Quaternion_F16p16 *a,
                            const Eina_Quaternion_F16p16 *b,
                            Eina_F16p16 pos)
{
   if (pos == 0)
     {
        *out = *a;
        return ;
     }
   else if (pos == eina_f16p16_int_from(1))
     {
        *out = *b;
        return ;
     }

   out->w = eina_f16p16_add(a->w,
                            eina_f16p16_mul(eina_f16p16_sub(b->w, a->w),
                                            pos));
   out->x = eina_f16p16_add(a->x,
                            eina_f16p16_mul(eina_f16p16_sub(b->x, a->x),
                                            pos));
   out->y = eina_f16p16_add(a->y,
                            eina_f16p16_mul(eina_f16p16_sub(b->y, a->y),
                                            pos));
   out->z = eina_f16p16_add(a->z,
                            eina_f16p16_mul(eina_f16p16_sub(b->z, a->z),
                                            pos));
}

EAPI void
eina_quaternion_f16p16_slerp(Eina_Quaternion_F16p16 *out,
                             const Eina_Quaternion_F16p16 *a,
                             const Eina_Quaternion_F16p16 *b,
                             Eina_F16p16 pos)
{
   Eina_Quaternion_F16p16 bp;
   Eina_Quaternion_F16p16 left, right;
   Eina_F16p16 dot;
   Eina_F16p16 pos1, pos2;

   if (pos == 0)
     {
        *out = *a;
        return ;
     }
   else if (pos == eina_f16p16_int_from(1))
     {
        *out = *b;
        return ;
     }

   dot = eina_quaternion_f16p16_dot(a, b);
   if (dot >= eina_f16p16_int_from(0))
     {
        bp = *b;
     }
   else
     {
        eina_quaternion_f16p16_negative(&bp, b);
        dot = eina_f16p16_sub(0, dot);
     }

   pos1 = eina_f16p16_sub(eina_f16p16_int_from(1), pos);
   pos2 = pos;
   if (eina_f16p16_sub(eina_f16p16_int_from(1), dot) > eina_f16p16_float_from(0.0000001))
     {
        Eina_F32p32 angle;
        Eina_F16p16 sangle;

        angle = eina_f32p32_double_from(acos(eina_f16p16_double_to(dot)));
        sangle = eina_f32p32_to_f16p16(eina_f32p32_sin(angle));
        if (sangle > eina_f16p16_float_from(0.0000001))
          {
             pos1 = eina_f32p32_to_f16p16(eina_f32p32_div(eina_f32p32_sin(eina_f32p32_mul(eina_f16p16_to_f32p32(pos1), angle)),
                                                          eina_f16p16_to_f32p32(sangle)));
             pos2 = eina_f32p32_to_f16p16(eina_f32p32_div(eina_f32p32_sin(eina_f32p32_mul(eina_f16p16_to_f32p32(pos2), angle)),
                                                          eina_f16p16_to_f32p32(sangle)));
          }
     }

   eina_quaternion_f16p16_scale(&left, a, pos1);
   eina_quaternion_f16p16_scale(&right, b, pos2);
   eina_quaternion_f16p16_add(out, &left, &right);
}

EAPI void
eina_quaternion_f16p16_nlerp(Eina_Quaternion_F16p16 *out,
                             const Eina_Quaternion_F16p16 *a,
                             const Eina_Quaternion_F16p16 *b,
                             Eina_F16p16 pos)
{
   Eina_Quaternion_F16p16 bp, left, right;
   Eina_Quaternion_F16p16 not_normalize;
   double dot;

   if (pos == 0)
     {
        *out = *a;
        return ;
     }
   else if (pos == eina_f16p16_int_from(1))
     {
        *out = *b;
        return ;
     }

   dot = eina_quaternion_f16p16_dot(a, b);
   if (dot >= eina_f16p16_int_from(0))
     {
        bp = *b;
     }
   else
     {
        eina_quaternion_f16p16_negative(&bp, b);
        dot = -dot;
     }

   eina_quaternion_f16p16_scale(&left, a,
                                eina_f16p16_sub(eina_f16p16_int_from(1), pos));
   eina_quaternion_f16p16_scale(&right, b, pos);
   eina_quaternion_f16p16_add(&not_normalize, &left, &right);
   eina_quaternion_f16p16_normalized(out, &not_normalize);
}

EAPI void
eina_quaternion_f16p16_rotate(Eina_Point_3D_F16p16 *p,
                              const Eina_Point_3D_F16p16 *center,
                              const Eina_Quaternion_F16p16 *q)
{
   const Eina_Point_3D_F16p16 zero = { 0, 0, 0 };
   Eina_F16p16 x, y, z;
   Eina_F16p16 uvx, uvy, uvz;
   Eina_F16p16 uuvx, uuvy, uuvz;

   if (!center) center = &zero;

   x = eina_f16p16_sub(p->x, center->x);
   y = eina_f16p16_sub(p->y, center->y);
   z = eina_f16p16_sub(p->z, center->z);

   uvx = eina_f16p16_sub(eina_f16p16_mul(q->y, z),
                         eina_f16p16_mul(q->z, y));
   uvy = eina_f16p16_sub(eina_f16p16_mul(q->z, x),
                         eina_f16p16_mul(q->x, z));
   uvz = eina_f16p16_sub(eina_f16p16_mul(q->x, y),
                         eina_f16p16_mul(q->y, x));

   uuvx = eina_f16p16_sub(eina_f16p16_mul(q->y, uvz),
                          eina_f16p16_mul(q->z, uvy));
   uuvy = eina_f16p16_sub(eina_f16p16_mul(q->z, uvx),
                          eina_f16p16_mul(q->x, uvz));
   uuvz = eina_f16p16_sub(eina_f16p16_mul(q->x, uvy),
                          eina_f16p16_mul(q->y, uvx));

   uvx = eina_f16p16_mul(eina_f16p16_scale(q->w, 2), uvx);
   uvy = eina_f16p16_mul(eina_f16p16_scale(q->w, 2), uvy);
   uvz = eina_f16p16_mul(eina_f16p16_scale(q->w, 2), uvz);

   uuvx = eina_f16p16_scale(uuvx, 2);
   uuvy = eina_f16p16_scale(uuvy, 2);
   uuvz = eina_f16p16_scale(uuvz, 2);

   p->x = eina_f16p16_add(eina_f16p16_add(center->x, x),
                          eina_f16p16_add(uvx, uuvx));
   p->y = eina_f16p16_add(eina_f16p16_add(center->y, y),
                          eina_f16p16_add(uvy, uuvy));
   p->z = eina_f16p16_add(eina_f16p16_add(center->z, z),
                          eina_f16p16_add(uvz, uuvz));
}

EAPI void
eina_quaternion_f16p16_rotation_matrix3_get(Eina_Matrix3_F16p16 *m,
                                            const Eina_Quaternion_F16p16 *q)
{
   Eina_F16p16 x, y, z;
   Eina_F16p16 xx, xy, xz;
   Eina_F16p16 yy, yz;
   Eina_F16p16 zz;
   Eina_F16p16 wx, wy, wz;

   x = eina_f16p16_scale(q->x, 2);
   y = eina_f16p16_scale(q->y, 2);
   z = eina_f16p16_scale(q->z, 2);

   xx = eina_f16p16_mul(q->x, x);
   xy = eina_f16p16_mul(q->x, y);
   xz = eina_f16p16_mul(q->x, z);
   yy = eina_f16p16_mul(q->y, y);
   yz = eina_f16p16_mul(q->y, z);
   zz = eina_f16p16_mul(q->z, z);
   wx = eina_f16p16_mul(q->w, x);
   wy = eina_f16p16_mul(q->w, y);
   wz = eina_f16p16_mul(q->w, z);

   m->xx = eina_f16p16_sub(eina_f16p16_int_to(1),
                           eina_f16p16_add(yy, zz));
   m->xy = eina_f16p16_add(xy, wz);
   m->xz = eina_f16p16_sub(xz, wy);
   m->yx = eina_f16p16_sub(xy, wz);
   m->yy = eina_f16p16_sub(eina_f16p16_int_to(1),
                           eina_f16p16_add(xx, zz));
   m->yz = eina_f16p16_add(yz, wx);
   m->zx = eina_f16p16_add(xz, wy);
   m->zy = eina_f16p16_add(yz, wx);
   m->zz = eina_f16p16_sub(eina_f16p16_int_to(1),
                           eina_f16p16_add(xx, yy));
}

EAPI void
eina_quaternion_set(Eina_Quaternion *out, double x,
                    double y, double z, double w)
{
   out->w = w;
   out->x = x;
   out->y = y;
   out->z = z;
}

EAPI double
eina_quaternion_norm(const Eina_Quaternion *q)
{
   double s;

   s = q->w * q->w + q->x * q->x + q->y * q->y + q->z * q->z;

   return sqrt(s);
}

EAPI void
eina_quaternion_negative(Eina_Quaternion *out,
                         const Eina_Quaternion *in)
{
   out->w = - in->w;
   out->x = - in->x;
   out->y = - in->y;
   out->z = - in->z;
}

EAPI void
eina_quaternion_add(Eina_Quaternion *out,
                    const Eina_Quaternion *a,
                    const Eina_Quaternion *b)
{
   out->w = a->w + b->w;
   out->x = a->x + b->x;
   out->y = a->y + b->y;
   out->z = a->z + b->z;
}

EAPI void
eina_quaternion_mul(Eina_Quaternion *out,
                    const Eina_Quaternion *a,
                    const Eina_Quaternion *b)
{
   out->w = a->w * b->w - a->x * b->x - a->y * b->y - a->z * b->z;
   out->x = a->w * b->x + a->x * b->w + a->y * b->z - a->z * b->y;
   out->y = a->w * b->y - a->x * b->z + a->y * b->w + a->z * b->x;
   out->z = a->w * b->z + a->x * b->y - a->y * b->x + a->z * b->w;
}

EAPI void
eina_quaternion_scale(Eina_Quaternion *out,
                      const Eina_Quaternion *a,
                      double b)
{
   out->w = a->w * b;
   out->x = a->x * b;
   out->y = a->y * b;
   out->z = a->z * b;
}

EAPI void
eina_quaternion_conjugate(Eina_Quaternion *out,
                          const Eina_Quaternion *in)
{
   out->w = in->w;
   out->x = - in->x;
   out->y = - in->y;
   out->z = - in->z;
}

EAPI double
eina_quaternion_dot(const Eina_Quaternion *a,
                    const Eina_Quaternion *b)
{
   return a->w * b->w + a->x * b->x + a->y * b->y + a->z * b->z;
}

EAPI void
eina_quaternion_normalized(Eina_Quaternion *out,
                           const Eina_Quaternion *in)
{
   double norm;

   norm = eina_quaternion_norm(in);
   eina_quaternion_scale(out, in, 1.0 / norm);
}

EAPI void
eina_quaternion_lerp(Eina_Quaternion *out,
                     const Eina_Quaternion *a,
                     const Eina_Quaternion *b,
                     double pos)
{
   if (pos == 0)
     {
        *out = *a;
        return ;
     }
   else if (pos == 1.0)
     {
        *out = *b;
        return ;
     }

   out->w = a->w + pos * (b->w - a->w);
   out->x = a->x + pos * (b->x - a->x);
   out->y = a->y + pos * (b->y - a->y);
   out->z = a->z + pos * (b->z - a->z);
}

EAPI void
eina_quaternion_slerp(Eina_Quaternion *out,
                      const Eina_Quaternion *a,
                      const Eina_Quaternion *b,
                      double pos)
{
   Eina_Quaternion bp;
   Eina_Quaternion left, right;
   double dot;
   double pos1, pos2;

   if (pos == 0)
     {
        *out = *a;
        return ;
     }
   else if (pos == 1.0)
     {
        *out = *b;
        return ;
     }

   dot = eina_quaternion_dot(a, b);
   if (dot >= 0)
     {
        bp = *b;
     }
   else
     {
        eina_quaternion_negative(&bp, b);
        dot = -dot;
     }

   pos1 = 1.0 - pos;
   pos2 = pos;
   if ((1.0 - dot) > 0.0000001)
     {
        double angle;
        double sangle;

        angle = acos(dot);
        sangle = sin(angle);
        if (sangle > 0.0000001)
          {
             pos1 = sin(pos1 * angle) / sangle;
             pos2 = sin(pos2 * angle) / sangle;
          }
     }

   eina_quaternion_scale(&left, a, pos1);
   eina_quaternion_scale(&right, b, pos2);
   eina_quaternion_add(out, &left, &right);
}

EAPI void
eina_quaternion_nlerp(Eina_Quaternion *out,
                      const Eina_Quaternion *a,
                      const Eina_Quaternion *b,
                      double pos)
{
   Eina_Quaternion bp, left, right;
   Eina_Quaternion not_normalize;
   double dot;

   if (pos == 0)
     {
        *out = *a;
        return ;
     }
   else if (pos == 1.0)
     {
        *out = *b;
        return ;
     }

   dot = eina_quaternion_dot(a, b);
   if (dot >= 0)
     {
        bp = *b;
     }
   else
     {
        eina_quaternion_negative(&bp, b);
        dot = -dot;
     }

   eina_quaternion_scale(&left, a, 1.0 - pos);
   eina_quaternion_scale(&right, b, pos);
   eina_quaternion_add(&not_normalize, &left, &right);
   eina_quaternion_normalized(out, &not_normalize);
}

EAPI void
eina_quaternion_rotate(Eina_Point_3D *p,
                       const Eina_Point_3D *center,
                       const Eina_Quaternion *q)
{
   double x, y, z;
   double uvx, uvy, uvz;
   double uuvx, uuvy, uuvz;

   x = p->x - center->x;
   y = p->y - center->y;
   z = p->z - center->z;

   uvx = q->y * z - q->z * y;
   uvy = q->z * x - q->x * z;
   uvz = q->x * y - q->y * x;

   uuvx = q->y * uvz - q->z * uvy;
   uuvy = q->z * uvx - q->x * uvz;
   uuvz = q->x * uvy - q->y * uvx;

   uvx *= (2.0f * q->w);
   uvy *= (2.0f * q->w);
   uvz *= (2.0f * q->w);

   uuvx *= 2.0f;
   uuvy *= 2.0f;
   uuvz *= 2.0f;

   p->x = center->x + x + uvx + uuvx;
   p->y = center->y + y + uvy + uuvy;
   p->z = center->z + z + uvz + uuvz;
}

EAPI void
eina_quaternion_rotation_matrix3_get(Eina_Matrix3 *m,
                                     const Eina_Quaternion *q)
{
   double x, y, z;
   double xx, xy, xz;
   double yy, yz;
   double zz;
   double wx, wy, wz;

   x = 2.0 * q->x;
   y = 2.0 * q->y;
   z = 2.0 * q->z;

   xx = q->x * x;
   xy = q->x * y;
   xz = q->x * z;
   yy = q->y * y;
   yz = q->y * z;
   zz = q->z * z;
   wx = q->w * x;
   wy = q->w * y;
   wz = q->w * z;

   m->xx = 1.0 - yy - zz;
   m->xy = xy + wz;
   m->xz = xz - wy;
   m->yx = xy - wz;
   m->yy = 1.0 - xx - zz;
   m->yz = yz + wx;
   m->zx = xz + wy;
   m->zy = yz - wx;
   m->zz = 1.0 - xx - yy;
}

static inline double
_max(double a, double b)
{
   return a > b ? a : b;
}

static inline double
eina_point_3d_norm(Eina_Point_3D *p)
{
   return sqrt(p->x * p->x + p->y * p->y + p->z * p->z);
}

static inline void
eina_point_3d_normalize(Eina_Point_3D *p, double norm)
{
   double tmp = 1 / norm;

   p->x *= tmp;
   p->y *= tmp;
   p->z *= tmp;
}

static inline double
eina_point_3d_dot(const Eina_Point_3D *a, const Eina_Point_3D *b)
{
   return a->x * b->x + a->y * b->y + a->z * b->z;
}

static inline void
eina_point_3d_combine(Eina_Point_3D *out,
                      const Eina_Point_3D *a, const Eina_Point_3D *b,
                      double scale1, double scale2)
{
   out->x = a->x * scale1 + b->x * scale2;
   out->y = a->y * scale1 + b->y * scale2;
   out->z = a->z * scale1 + b->z * scale2;
}

static inline void
eina_point3d_cross(Eina_Point_3D *out,
                   const Eina_Point_3D *a, const Eina_Point_3D *b)
{
   out->x = a->y * b->z - a->z * b->y;
   out->y = a->z * b->x - a->x * b->z;
   out->z = a->x * b->y - a->y * b->x;
}

static inline void
eina_point3d_neg(Eina_Point_3D *out, const Eina_Point_3D *in)
{
   out->x = - in->x;
   out->y = - in->y;
   out->z = - in->z;
}

/* http://www.w3.org/TR/css3-transforms/#decomposing-a-3d-matrix */
EAPI Eina_Bool
eina_matrix4_quaternion_to(Eina_Quaternion *rotation,
                           Eina_Quaternion *perspective,
                           Eina_Point_3D *translation,
                           Eina_Point_3D *scale,
                           Eina_Point_3D *skew,
                           const Eina_Matrix4 *m)
{
   Eina_Matrix4 n, pm;
   double det, factor;

   if (m->ww == 0) return EINA_FALSE;

   // Normalize the matrix.
   factor = 1 / m->ww;

   n.xx = m->xx * factor;
   n.xy = m->xy * factor;
   n.xz = m->xz * factor;
   n.xw = m->xw * factor;
   n.yx = m->yx * factor;
   n.yy = m->yy * factor;
   n.yz = m->yz * factor;
   n.yw = m->yw * factor;
   n.zx = m->zx * factor;
   n.zy = m->zy * factor;
   n.zz = m->zz * factor;
   n.zw = m->zw * factor;
   n.wx = m->wx * factor;
   n.wy = m->wy * factor;
   n.wz = m->wz * factor;
   n.ww = m->ww * factor;

   pm = n;
   pm.xw = 0;
   pm.yw = 0;
   pm.zw = 0;
   pm.ww = 1;

   // If the perspective matrix is not invertible, we are also unable to
   // decompose, so we'll bail early.
   det = eina_matrix4_determinant(&pm);
   if (fabs(det) < DBL_EPSILON) return EINA_FALSE;

   // First, isolate perspective.
   if (perspective)
     {
        if (fabs(n.xw) < DBL_EPSILON ||
            fabs(n.yw) < DBL_EPSILON ||
            fabs(n.zw) < DBL_EPSILON)
          {
             Eina_Quaternion tmp;
             Eina_Matrix4 ipm, tipm;

             tmp.x = n.wx;
             tmp.y = n.wy;
             tmp.z = n.wz;
             tmp.w = n.ww;

             if (!eina_matrix4_inverse(&ipm, &pm))
               return EINA_FALSE;

             eina_matrix4_transpose(&tipm, &ipm);

             perspective->x = tipm.xx * tmp.x + tipm.yx * tmp.y + tipm.zx * tmp.z + tipm.wx * tmp.w;
             perspective->y = tipm.xy * tmp.x + tipm.yy * tmp.y + tipm.zy * tmp.z + tipm.wy * tmp.w;
             perspective->z = tipm.xz * tmp.x + tipm.yz * tmp.y + tipm.zz * tmp.z + tipm.wz * tmp.w;
             perspective->w = tipm.xw * tmp.x + tipm.yw * tmp.y + tipm.zw * tmp.z + tipm.ww * tmp.w;
          }
        else
          {
             perspective->x = perspective->y = perspective->z = 0;
             perspective->w = 1;
          }
     }

   if (translation)
     {
        translation->x = n.xw;
        translation->y = n.yw;
        translation->z = n.zw;
     }

   if (skew || scale || rotation)
     {
        Eina_Point_3D tsc, tsk, row0, row1, row2, cross;
        double tmp;

        // Make sure all pointer are defined
        if (!scale) scale = &tsc;
        if (!skew) skew = &tsk;

        row0.x = n.xx; row1.x = n.yx; row2.x = n.zx;
        row0.y = n.xy; row1.y = n.yy; row2.y = n.zy;
        row0.z = n.xz; row1.z = n.yz; row2.z = n.zz;

        // Compute X scale factor and normalize first row.
        scale->x = eina_point_3d_norm(&row0);
        eina_point_3d_normalize(&row0, scale->x);

        skew->x = eina_point_3d_dot(&row0, &row1);
        eina_point_3d_combine(&row1, &row1, &row0, 1.0, -skew->x);

        // Now, compute Y scale and normalize 2nd row.
        scale->y = eina_point_3d_norm(&row1);
        eina_point_3d_normalize(&row1, scale->y);
        skew->x /= scale->y;

        // Compute XZ and YZ shears, orthogonalize 3rd row
        skew->y = eina_point_3d_dot(&row0, &row2);
        eina_point_3d_combine(&row2, &row2, &row0, 1.0, -skew->y);
        skew->z = eina_point_3d_dot(&row1, &row2);
        eina_point_3d_combine(&row2, &row2, &row1, 1.0, -skew->z);

        // Next, get Z scale and normalize 3rd row.
        scale->z = eina_point_3d_norm(&row2);
        eina_point_3d_normalize(&row2, scale->z);

        tmp = 1 / scale->z;
        skew->y *= tmp;
        skew->z *= tmp;

        // At this point, the matrix (in rows) is orthonormal.
        // Check for a coordinate system flip.  If the determinant
        // is -1, then negate the matrix and the scaling factors.
        eina_point3d_cross(&cross, &row1, &row2);
        if (eina_point_3d_dot(&row0, &cross) < 0)
          {
             eina_point3d_neg(scale, scale);
             eina_point3d_neg(&row0, &row0);
             eina_point3d_neg(&row1, &row1);
             eina_point3d_neg(&row2, &row2);
          }

        if (rotation)
          {
             // Now, get the rotations out
             rotation->x = 0.5 * sqrt(_max(1 + row0.x - row1.y - row2.z, 0));
             rotation->y = 0.5 * sqrt(_max(1 - row0.x + row1.y - row2.z, 0));
             rotation->z = 0.5 * sqrt(_max(1 - row0.x - row1.y + row2.z, 0));
             rotation->w = 0.5 * sqrt(_max(1 + row0.x + row1.y + row2.z, 0));

             if (row2.y > row1.z) rotation->x = - rotation->x;
             if (row0.z > row2.x) rotation->y = - rotation->y;
             if (row1.x > row0.y) rotation->z = - rotation->z;
          }
     }

   return EINA_TRUE;
}

EAPI void
eina_matrix3_quaternion_get(Eina_Quaternion *q,
                            const Eina_Matrix3 *m)
{
   double tval;
   double w, x, y, z;

   tval = m->xx + m->yy + m->zz;

   if (tval > 0)
     {

        double s = 0.5 / sqrtf(tval + 1.0);

        w = (0.25 / s);
        x = ((m->zy - m->yz) * s);
        y = ((m->xz - m->zx) * s);
        z = ((m->yx - m->xy) * s);
     }
   else if ((m->xx > m->yy) && (m->xx > m->zz))
     {
        double s = 2.0 * sqrtf(1.0 + m->xx - m->yy - m->zz);

        w = ((m->zy - m->yz) / s);
        x = (0.25 * s);
        y = ((m->xy + m->yx) / s);
        z = ((m->xz + m->zx) / s);
     }
   else if (m->yy > m->zz)
     {
        double s = 2.0 * sqrtf(1.0 + m->yy - m->xx - m->zz);

        w = ((m->xz - m->zx) / s);
        x = ((m->xy + m->yx) / s);
        y = (0.25 * s);
        z = ((m->yz + m->zy) / s);
     }
   else
     {
        double s = 2.0 * sqrtf(1.0 + m->zz - m->xx - m->yy);

        w = ((m->yx - m->xy) / s);
        x = ((m->xz + m->zx) / s);
        y = ((m->yz + m->zy) / s);
        z = (0.25 * s);
     }
   q->w = w;
   q->x = x;
   q->y = y;
   q->z = z;
}

EAPI void
eina_quaternion_matrix4_to(Eina_Matrix4 *m,
                           const Eina_Quaternion *rotation,
                           const Eina_Quaternion *perspective,
                           const Eina_Point_3D *translation,
                           const Eina_Point_3D *scale,
                           const Eina_Point_3D *skew)
{
   Eina_Matrix4 rm, tmp;
   double x, y, z, w;

   eina_matrix4_identity(m);

   // apply perspective
   m->wx = perspective->x;
   m->wy = perspective->y;
   m->wz = perspective->z;
   m->ww = perspective->w;

   // apply translation
   m->xw = translation->x * m->xx + translation->y * m->xy + translation->z * m->xz;
   m->yw = translation->x * m->yx + translation->y * m->yy + translation->z * m->yz;
   m->zw = translation->x * m->zx + translation->y * m->zy + translation->z * m->zz;

   // apply rotation
   x = rotation->x;
   y = rotation->y;
   z = rotation->z;
   w = rotation->w;

   // Construct a composite rotation matrix from the quaternion values
   // rotationMatrix is a identity 4x4 matrix initially
   eina_matrix4_identity(&rm);

   rm.xx = 1 - 2 * (y * y + z * z);
   rm.xy = 2 * (x * y - z * w);
   rm.xz = 2 * (x * z + y * w);
   rm.yx = 2 * (x * y + z * w);
   rm.yy = 1 - 2 * (x * x + z * z);
   rm.yz = 2 * (y * z - x * w);
   rm.zx = 2 * (x * z - y * w);
   rm.zy = 2 * (y * z + x * w);
   rm.zz = 1 - 2 * (x * x + y * y);

   eina_matrix4_multiply(&tmp, m, &rm);

   // apply skew
   // rm is a identity 4x4 matrix initially
   if (skew->z)
     {
        Eina_Matrix4 cp;

        eina_matrix4_identity(&rm);
        rm.zx = skew->z;

        eina_matrix4_multiply(&cp, &tmp, &rm);
        tmp = cp;
     }

   if (skew->y)
     {
        Eina_Matrix4 cp;

        eina_matrix4_identity(&rm);
        rm.zy = 0;
        rm.zx = skew->y;

        eina_matrix4_multiply(&cp, &tmp, &rm);
        tmp = cp;
     }

   if (skew->x)
     {
        Eina_Matrix4 cp;

        eina_matrix4_identity(&rm);
        rm.zx = 0;
        rm.yx = skew->x;

        eina_matrix4_multiply(&cp, &tmp, &rm);
        tmp = cp;
     }

   // apply scale
   m->xx = tmp.xx * scale->x;
   m->xy = tmp.xy * scale->x;
   m->xz = tmp.xz * scale->x;
   m->xw = tmp.xw;
   m->yx = tmp.yx * scale->y;
   m->yy = tmp.yy * scale->y;
   m->yz = tmp.yz * scale->y;
   m->yw = tmp.yw;
   m->zx = tmp.zx * scale->z;
   m->zy = tmp.zy * scale->z;
   m->zz = tmp.zz * scale->z;
   m->zw = tmp.zw;
   m->wx = tmp.wx;
   m->wy = tmp.wy;
   m->wz = tmp.wz;
   m->ww = tmp.ww;
}