huangcm
2025-08-25 f350412dc55c15118d0a7925d1071877498e5e24
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
/*
 * Copyright 2018 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can
 * be found in the LICENSE file.
 *
 */
 
#include "transform_stack.h"
 
//
//
//
 
#include <stdlib.h>
#include <math.h>
 
//
//
//
 
#undef NDEBUG
#include <assert.h>
 
//
//
//
 
#define TS_TRANSFORM_SUFFIX_EVAL(a)  a
 
#define TS_TRANSFORM_SUFFIX_CONCAT(func)                                \
  TS_TRANSFORM_SUFFIX_EVAL(func)##TS_TRANSFORM_SUFFIX_EVAL(TS_TRANSFORM_FLOAT_SUFFIX)
 
//
//
//
 
#define TS_TRANSFORM_SIN(x)  TS_TRANSFORM_SUFFIX_CONCAT(sin)(x)
#define TS_TRANSFORM_COS(x)  TS_TRANSFORM_SUFFIX_CONCAT(cos)(x)
#define TS_TRANSFORM_TAN(x)  TS_TRANSFORM_SUFFIX_CONCAT(tan)(x)
 
//
//
//
#define TS_TRANSFORM_ZERO    ((ts_transform_float_t)0.0)
#define TS_TRANSFORM_ONE     ((ts_transform_float_t)1.0)
#define TS_TRANSFORM_RCP(f)  (TS_TRANSFORM_ONE / (f))
 
 
//
//
//
 
union ts_transform_stack_3x3_u
{
  ts_transform_float_t   a8[8];
 
  struct {
    ts_transform_float_t sx;
    ts_transform_float_t shx;
    ts_transform_float_t tx;
 
    ts_transform_float_t shy;
    ts_transform_float_t sy;
    ts_transform_float_t ty;
 
    ts_transform_float_t w0;
    ts_transform_float_t w1;
    // w2 is always 1.0
  };
 
  struct {
    ts_transform_float_t a;
    ts_transform_float_t b;
    ts_transform_float_t c;
 
    ts_transform_float_t d;
    ts_transform_float_t e;
    ts_transform_float_t f;
 
    ts_transform_float_t g;
    ts_transform_float_t h;
    // i is always 1.0
  };
};
 
//
//
//
 
struct ts_transform_stack
{
  uint32_t                         size;
  uint32_t                         count;
 
  ts_transform_weakref_t         * weakrefs;
  union ts_transform_stack_3x3_u * transforms;
};
 
//
//
//
 
static
void
ts_transform_stack_resize(struct ts_transform_stack * const ts, uint32_t const size)
{
  ts->size       = size;
  ts->weakrefs   = realloc(ts->weakrefs,  size * sizeof(*ts->weakrefs));
  ts->transforms = realloc(ts->transforms,size * sizeof(*ts->transforms));
}
 
static
void
ts_transform_stack_ensure(struct ts_transform_stack * const ts)
{
  if (ts->count < ts->size)
    return;
 
  // increase by 50% and by at least 8
  ts_transform_stack_resize(ts,ts->size + max(ts->size/2,8));
}
 
//
//
//
 
struct ts_transform_stack *
ts_transform_stack_create(uint32_t const size)
{
  struct ts_transform_stack * ts = malloc(sizeof(*ts));
 
  ts->size       = size;
  ts->count      = 0;
 
  ts->transforms = NULL;
  ts->weakrefs   = NULL;
 
  ts_transform_stack_resize(ts,size);
 
  return ts;
}
 
void
ts_transform_stack_release(struct ts_transform_stack * const ts)
{
  free(ts->transforms);
  free(ts->weakrefs);
 
  free(ts);
}
 
//
//
//
 
uint32_t
ts_transform_stack_save(struct ts_transform_stack * const ts)
{
  return ts->count;
}
 
void
ts_transform_stack_restore(struct ts_transform_stack * const ts,
                           uint32_t                    const restore)
{
  ts->count = restore;
}
 
//
//
//
 
static
union ts_transform_stack_3x3_u *
ts_transform_stack_tos(struct ts_transform_stack * const ts)
{
  return ts->transforms + ts->count - 1;
}
 
//
//
//
 
static
void
ts_transform_stack_3x3_u_copy(union ts_transform_stack_3x3_u       * const __restrict dst,
                              union ts_transform_stack_3x3_u const * const __restrict src)
{
  *dst = *src;
}
 
//
// C = A * B
//
// FIXME -- can save affine vs. projective flags and save a few ops
//
 
#define TS_TRANSFORM_MULTIPLY(A,B)                                    \
  A->sx  * B->sx   +  A->shx * B->shy  +  A->tx * B->w0,              \
  A->sx  * B->shx  +  A->shx * B->sy   +  A->tx * B->w1,              \
  A->sx  * B->tx   +  A->shx * B->ty   +  A->tx,                      \
  A->shy * B->sx   +  A->sy  * B->shy  +  A->ty * B->w0,              \
  A->shy * B->shx  +  A->sy  * B->sy   +  A->ty * B->w1,              \
  A->shy * B->tx   +  A->sy  * B->ty   +  A->ty,                      \
  A->w0  * B->sx   +  A->w1  * B->shy  +          B->w0,              \
  A->w0  * B->shx  +  A->w1  * B->sy   +          B->w1,              \
  A->w0  * B->tx   +  A->w1  * B->ty   +          TS_TRANSFORM_ONE
 
//
//
//
 
#define TS_IS_AFFINE(t) ((t->w0 == TS_TRANSFORM_ZERO) && (t->w1 == TS_TRANSFORM_ZERO))
 
static
ts_transform_type_e
ts_transform_stack_classify(struct ts_transform_stack * const ts)
{
  union ts_transform_stack_3x3_u const * const t = ts_transform_stack_tos(ts);
 
  if (TS_IS_AFFINE(t))
    return TS_TRANSFORM_TYPE_AFFINE;
  else
    return TS_TRANSFORM_TYPE_PROJECTIVE;
}
 
//
//
//
 
ts_transform_float_t *
ts_transform_stack_top_transform(struct ts_transform_stack * const ts)
{
  return ts_transform_stack_tos(ts)->a8;
}
 
ts_transform_weakref_t *
ts_transform_stack_top_weakref(struct ts_transform_stack * const ts)
{
  return ts->weakrefs + ts->count - 1;
}
 
//
//
//
 
void
ts_transform_stack_dup(struct ts_transform_stack * const ts)
{
  ts_transform_stack_ensure(ts);
 
  union ts_transform_stack_3x3_u * const tos = ts_transform_stack_tos(ts);
 
  ts_transform_stack_3x3_u_copy(tos+1,tos);
 
  ts->weakrefs[ts->count] = ts->weakrefs[ts->count-1];
 
  ts->count += 1;
}
 
void
ts_transform_stack_drop(struct ts_transform_stack * const ts)
{
  assert(ts->count >= 1);
 
  ts->count -= 1;
}
 
//
//
//
 
static
void
ts_transform_stack_swap_drop(struct ts_transform_stack * const ts)
{
  assert(ts->count >= 2);
 
  union ts_transform_stack_3x3_u * const tos = ts_transform_stack_tos(ts);
 
  ts_transform_stack_3x3_u_copy(tos-1,tos);
 
  ts->weakrefs[ts->count-2] = ts->weakrefs[ts->count-1];
 
  ts->count -= 1;
}
 
//
//
//
 
static
void
ts_transform_stack_store_matrix_8(struct ts_transform_stack * const ts,
                                  uint32_t                    const idx,
                                  ts_transform_float_t        const sx,
                                  ts_transform_float_t        const shx,
                                  ts_transform_float_t        const tx,
                                  ts_transform_float_t        const shy,
                                  ts_transform_float_t        const sy,
                                  ts_transform_float_t        const ty,
                                  ts_transform_float_t        const w0,
                                  ts_transform_float_t        const w1)
{
  union ts_transform_stack_3x3_u * t = ts->transforms + idx;
 
  t->sx  = sx;
  t->shx = shx;
  t->tx  = tx;
 
  t->shy = shy;
  t->sy  = sy;
  t->ty  = ty;
 
  t->w0  = w0;
  t->w1  = w1;
 
  ts->weakrefs[idx] = TS_TRANSFORM_WEAKREF_INVALID;
}
 
//
//
//
 
static
void
ts_transform_stack_store_matrix(struct ts_transform_stack * const ts,
                                uint32_t                    const idx,
                                ts_transform_float_t        const sx,
                                ts_transform_float_t        const shx,
                                ts_transform_float_t        const tx,
                                ts_transform_float_t        const shy,
                                ts_transform_float_t        const sy,
                                ts_transform_float_t        const ty,
                                ts_transform_float_t        const w0,
                                ts_transform_float_t        const w1,
                                ts_transform_float_t        const w2)
{
  if (w2 == TS_TRANSFORM_ONE)
    {
      ts_transform_stack_store_matrix_8(ts,idx,
                                        sx, shx,tx,
                                        shy,sy, ty,
                                        w0, w1);
    }
  else
    {
      // normalize
      ts_transform_float_t d = TS_TRANSFORM_RCP(w2);
 
      ts_transform_stack_store_matrix_8(ts,idx,
                                        sx  * d, shx * d, tx * d,
                                        shy * d, sy  * d, ty * d,
                                        w0  * d, w1  * d);
    }
}
 
//
//
//
 
static
void
ts_transform_stack_push_matrix_8(struct ts_transform_stack * const ts,
                                 ts_transform_float_t        const sx,
                                 ts_transform_float_t        const shx,
                                 ts_transform_float_t        const tx,
                                 ts_transform_float_t        const shy,
                                 ts_transform_float_t        const sy,
                                 ts_transform_float_t        const ty,
                                 ts_transform_float_t        const w0,
                                 ts_transform_float_t        const w1)
{
  ts_transform_stack_ensure(ts);
 
  ts_transform_stack_store_matrix_8(ts,ts->count++,
                                    sx, shx,tx,
                                    shy,sy, ty,
                                    w0, w1);
}
 
//
//
//
 
void
ts_transform_stack_push_matrix(struct ts_transform_stack * const ts,
                               ts_transform_float_t        const sx,
                               ts_transform_float_t        const shx,
                               ts_transform_float_t        const tx,
                               ts_transform_float_t        const shy,
                               ts_transform_float_t        const sy,
                               ts_transform_float_t        const ty,
                               ts_transform_float_t        const w0,
                               ts_transform_float_t        const w1,
                               ts_transform_float_t        const w2)
{
  if (w2 == TS_TRANSFORM_ONE)
    {
      ts_transform_stack_push_matrix_8(ts,
                                       sx, shx,tx,
                                       shy,sy, ty,
                                       w0, w1);
    }
  else
    {
      // normalize
      ts_transform_float_t d = TS_TRANSFORM_RCP(w2);
 
      ts_transform_stack_push_matrix_8(ts,
                                       sx  * d, shx * d, tx * d,
                                       shy * d, sy  * d, ty * d,
                                       w0  * d, w1  * d);
    }
}
 
//
//
//
 
void
ts_transform_stack_push_identity(struct ts_transform_stack * const ts)
{
  ts_transform_stack_push_matrix_8(ts,
                                   1.0, 0.0, 0.0,
                                   0.0, 1.0, 0.0,
                                   0.0, 0.0);
}
 
void
ts_transform_stack_push_affine(struct ts_transform_stack * const ts,
                               ts_transform_float_t        const sx,
                               ts_transform_float_t        const shx,
                               ts_transform_float_t        const tx,
                               ts_transform_float_t        const shy,
                               ts_transform_float_t        const sy,
                               ts_transform_float_t        const ty)
{
  ts_transform_stack_push_matrix_8(ts,
                                   sx,  shx, tx,
                                   shy, sy,  ty,
                                   0.0, 0.0);
}
 
void
ts_transform_stack_push_translate(struct ts_transform_stack * const ts,
                                  ts_transform_float_t        const tx,
                                  ts_transform_float_t        const ty)
{
  ts_transform_stack_push_matrix_8(ts,
                                   1.0, 0.0, tx,
                                   0.0, 1.0, ty,
                                   0.0, 0.0);
}
 
void
ts_transform_stack_push_scale(struct ts_transform_stack * const ts,
                              ts_transform_float_t        const sx,
                              ts_transform_float_t        const sy)
{
  ts_transform_stack_push_matrix_8(ts,
                                   sx,  0.0, 0.0,
                                   0.0, sy,  0.0,
                                   0.0, 0.0);
}
 
void
ts_transform_stack_push_shear(struct ts_transform_stack * const ts,
                              ts_transform_float_t        const shx,
                              ts_transform_float_t        const shy)
{
  ts_transform_stack_push_matrix_8(ts,
                                   1.0, shx, 0.0,
                                   shy, 1.0, 0.0,
                                   0.0, 0.0);
}
 
void
ts_transform_stack_push_skew_x(struct ts_transform_stack * const ts,
                               ts_transform_float_t        const theta)
{
  ts_transform_float_t        const tan_theta = TS_TRANSFORM_TAN(theta); // replace with tanpi if available
 
  ts_transform_stack_push_matrix_8(ts,
                                   1.0, tan_theta,0.0,
                                   0.0, 1.0,      0.0,
                                   0.0, 0.0);
}
 
void
ts_transform_stack_push_skew_y(struct ts_transform_stack * const ts,
                               ts_transform_float_t        const theta)
{
  ts_transform_float_t const tan_theta = TS_TRANSFORM_TAN(theta); // replace with tanpi if available
 
  ts_transform_stack_push_matrix_8(ts,
                                   1.0,       0.0, 0.0,
                                   tan_theta, 1.0, 0.0,
                                   0.0,       0.0);
}
 
void
ts_transform_stack_push_rotate(struct ts_transform_stack * const ts,
                               ts_transform_float_t        const theta)
{
  ts_transform_float_t const cos_theta = TS_TRANSFORM_COS(theta); // replace with cospi if available
  ts_transform_float_t const sin_theta = TS_TRANSFORM_SIN(theta); // replace with sinpi if available
 
  ts_transform_stack_push_matrix_8(ts,
                                   cos_theta,-sin_theta, 0.0,
                                   sin_theta, cos_theta, 0.0,
                                   0.0,       0.0);
}
 
void
ts_transform_stack_push_rotate_xy2(struct ts_transform_stack * const ts,
                                   ts_transform_float_t        const theta,
                                   ts_transform_float_t        const cx,
                                   ts_transform_float_t        const cy,
                                   ts_transform_float_t        const tx,
                                   ts_transform_float_t        const ty)
{
  ts_transform_float_t const cos_theta = TS_TRANSFORM_COS(theta); // replace with cospi if available
  ts_transform_float_t const sin_theta = TS_TRANSFORM_SIN(theta); // replace with sinpi if available
 
  ts_transform_stack_push_matrix_8(ts,
                                   cos_theta,-sin_theta, tx - (cx * cos_theta) + (cy * sin_theta),
                                   sin_theta, cos_theta, ty - (cx * sin_theta) - (cy * cos_theta),
                                   0.0,       0.0);
}
 
void
ts_transform_stack_push_rotate_xy(struct ts_transform_stack * const ts,
                                  ts_transform_float_t        const theta,
                                  ts_transform_float_t        const cx,
                                  ts_transform_float_t        const cy)
{
  ts_transform_stack_push_rotate_xy2(ts,theta,cx,cy,cx,cy);
}
 
void
ts_transform_stack_push_rotate_scale_xy(struct ts_transform_stack * const ts,
                                        ts_transform_float_t        const theta,
                                        ts_transform_float_t        const sx,
                                        ts_transform_float_t        const sy,
                                        ts_transform_float_t        const cx,
                                        ts_transform_float_t        const cy)
{
  ts_transform_float_t const cos_theta = TS_TRANSFORM_COS(theta); // replace with cospi if available
  ts_transform_float_t const sin_theta = TS_TRANSFORM_SIN(theta); // replace with sinpi if available
 
  ts_transform_stack_push_matrix_8(ts,
                                   sx*cos_theta,-sx*sin_theta, cx - cx*sx*cos_theta + cy*sy*sin_theta,
                                   sy*sin_theta, sy*cos_theta, cy - cy*sy*cos_theta - cx*sx*sin_theta,
                                   0.0,          0.0);
}
 
//
// See: "Fundamentals of Texture Mapping and Image Warping" by Paul S. Heckbert (1989)
//
 
#define DET(a,b,c,d)  (a * d - b * c)
 
#define X(v,i)        v[i*2]
#define Y(v,i)        v[i*2+1]
 
//
//
//
 
ts_transform_type_e
ts_transform_stack_adjoint(struct ts_transform_stack * const ts)
{
  union ts_transform_stack_3x3_u * const t = ts_transform_stack_tos(ts);
 
#if 0
  // save for determinant
  ts_transform_float_t const a = t->a;
  ts_transform_float_t const b = t->b;
  ts_transform_float_t const c = t->c;
#endif
 
  ts_transform_stack_store_matrix(ts,ts->count-1,
 
                                  +DET(t->e, t->f, t->h,             TS_TRANSFORM_ONE),
                                  -DET(t->b, t->c, t->h,             TS_TRANSFORM_ONE),
                                  +DET(t->b, t->c, t->e,             t->f),
 
                                  -DET(t->d, t->f, t->g,             TS_TRANSFORM_ONE),
                                  +DET(t->a, t->c, t->g,             TS_TRANSFORM_ONE),
                                  -DET(t->a, t->c, t->d,             t->f),
 
                                  +DET(t->d, t->e, t->g,             t->h),
                                  -DET(t->a, t->b, t->g,             t->h),
                                  +DET(t->a, t->b, t->d,             t->e));
 
#if 0
  // determinant of t
  ts_transform_float_t const det = a * t->a + b * t->d + c * t->g;
#endif
 
  return ts_transform_stack_classify(ts);
}
 
//
//
//
 
ts_transform_type_e
ts_transform_stack_push_unit_to_quad(struct ts_transform_stack * const ts,
                                     ts_transform_float_t        const quad[8])
{
  ts_transform_float_t const x0  = X(quad,0);
  ts_transform_float_t const y0  = Y(quad,0);
 
  ts_transform_float_t const x1  = X(quad,1);
  ts_transform_float_t const y1  = Y(quad,1);
 
  ts_transform_float_t const x2  = X(quad,2);
  ts_transform_float_t const y2  = Y(quad,2);
 
  ts_transform_float_t const x3  = X(quad,3);
  ts_transform_float_t const y3  = Y(quad,3);
 
  ts_transform_float_t       sx  = x1 - x0;
  ts_transform_float_t       shy = y1 - y0;
 
  ts_transform_float_t const dx2 = x3 - x2;
  ts_transform_float_t const dy2 = y3 - y2;
 
  ts_transform_float_t const dx3 = -sx  - dx2;
  ts_transform_float_t const dy3 = -shy - dy2;
 
  // if both zero then quad_dst is a parallelogram and affine
  if ((dx3 == TS_TRANSFORM_ZERO) && (dy3 == TS_TRANSFORM_ZERO))
    {
      ts_transform_float_t const shx = x2 - x1;
      ts_transform_float_t const sy  = y2 - y1;
 
      ts_transform_stack_push_matrix_8(ts,
                                       sx,  shx, x0,
                                       shy, sy,  y0,
                                       0.0, 0.0);
 
      return TS_TRANSFORM_TYPE_AFFINE;
    }
  else
    {
      ts_transform_float_t const dx1    = x1 - x2;
      ts_transform_float_t const dy1    = y1 - y2;
 
      ts_transform_float_t const wx_den = dx1 * dy2 - dx2 * dy1;
 
      if (wx_den == TS_TRANSFORM_ZERO)
        return TS_TRANSFORM_TYPE_INVALID;
 
      ts_transform_float_t const w0_num = dx3 * dy2 - dx2 * dy3;
      ts_transform_float_t const w1_num = dx1 * dy3 - dx3 * dy1;
 
      ts_transform_float_t const w0     = w0_num / wx_den;
      ts_transform_float_t const w1     = w1_num / wx_den;
 
      sx                 += w0 * x1;
      ts_transform_float_t const shx    = x3 - x0 + w1 * x3;
 
      shy                += w0 * y1;
      ts_transform_float_t const sy     = y3 - y0 + w1 * y3;
 
      ts_transform_stack_push_matrix_8(ts,
                                       sx,  shx, x0,
                                       shy, sy,  y0,
                                       w0,  w1);
 
      return TS_TRANSFORM_TYPE_PROJECTIVE;
    }
}
 
//
//
//
 
ts_transform_type_e
ts_transform_stack_push_quad_to_unit(struct ts_transform_stack * const ts,
                                     float                        const quad[8])
{
  if (ts_transform_stack_push_unit_to_quad(ts,quad) == TS_TRANSFORM_TYPE_INVALID)
    return TS_TRANSFORM_TYPE_INVALID;
 
  return ts_transform_stack_adjoint(ts);
}
 
//
//
//
 
ts_transform_type_e
ts_transform_stack_push_quad_to_quad(struct ts_transform_stack * const ts,
                                     ts_transform_float_t        const quad_src[8],
                                     ts_transform_float_t        const quad_dst[8])
{
  if (ts_transform_stack_push_unit_to_quad(ts,quad_dst) == TS_TRANSFORM_TYPE_INVALID)
    return TS_TRANSFORM_TYPE_INVALID;
 
  if (ts_transform_stack_push_quad_to_unit(ts,quad_src) == TS_TRANSFORM_TYPE_INVALID)
    return TS_TRANSFORM_TYPE_INVALID;
 
  ts_transform_stack_multiply(ts);
 
  return ts_transform_stack_classify(ts);
}
 
//
//
//
 
ts_transform_type_e
ts_transform_stack_push_rect_to_quad(struct ts_transform_stack * const ts,
                                     ts_transform_float_t        const x0,
                                     ts_transform_float_t        const y0,
                                     ts_transform_float_t        const x1,
                                     ts_transform_float_t        const y1,
                                     ts_transform_float_t        const quad_dst[8])
{
  if (ts_transform_stack_push_unit_to_quad(ts,quad_dst) == TS_TRANSFORM_TYPE_INVALID)
    return TS_TRANSFORM_TYPE_INVALID;
 
  ts_transform_stack_push_matrix_8(ts,
                                   TS_TRANSFORM_RCP(x1-x0),
                                   0.0,
                                   -x0,
                                   0.0,
                                   TS_TRANSFORM_RCP(y1-y0),
                                   -y0,
                                   0.0,
                                   0.0);
 
  ts_transform_stack_multiply(ts);
 
  return ts_transform_stack_classify(ts);
}
 
//
// The second matrix on the stack (TOS[-1]) is post-multiplied by the
// top matrix on the stack (TOS[0]).
//
// The result replaces TOS[0] and TOS[-1] is unmodified.
//
// The stack effect of concat is:
//
//   | B |    | A*B |
//   | A |    |  A  |
//   | . | => |  .  |
//   | . |    |  .  |
//   | . |    |  .  |
//
void
ts_transform_stack_concat(struct ts_transform_stack * const ts)
{
  assert(ts->count >= 2);
 
  // get A and B
  union ts_transform_stack_3x3_u const * const B = ts_transform_stack_tos(ts);
  union ts_transform_stack_3x3_u const * const A = B - 1;
 
  ts_transform_stack_store_matrix(ts,ts->count-1,TS_TRANSFORM_MULTIPLY(A,B));
}
 
//
// The second matrix on the stack (TOS[-1]) is post-multiplied by the
// top matrix on the stack (TOS[0]).
//
// The result replaces both matrices.
//
// The stack effect of multiply is:
//
//   | B |    | A*B |
//   | A |    |  .  |
//   | . | => |  .  |
//   | . |    |  .  |
//   | . |    |  .  |
//
void
ts_transform_stack_multiply(struct ts_transform_stack * const ts)
{
  assert(ts->count >= 2);
 
  // get A and B
  union ts_transform_stack_3x3_u const * const B = ts_transform_stack_tos(ts);
  union ts_transform_stack_3x3_u const * const A = B - 1;
 
  ts_transform_stack_store_matrix(ts,ts->count-- - 2,TS_TRANSFORM_MULTIPLY(A,B));
}
 
//
//
//
 
void
ts_transform_stack_transform_xy(struct ts_transform_stack * const ts,
                                ts_transform_float_t        const x,
                                ts_transform_float_t        const y,
                                ts_transform_float_t      * const xp,
                                ts_transform_float_t      * const yp)
{
  union ts_transform_stack_3x3_u const * const t = ts_transform_stack_tos(ts);
 
  *xp = x * t->sx  + y * t->shx + t->tx;
  *yp = x * t->shy + y * t->sy  + t->ty;
 
  if (!TS_IS_AFFINE(t))
    {
      ts_transform_float_t const d = TS_TRANSFORM_RCP(x * t->w0 + y * t->w1 + TS_TRANSFORM_ONE);
 
      *xp *= d;
      *yp *= d;
    }
}
 
//
// test it!
//
 
#ifdef TS_DEBUG
 
#include <stdio.h>
 
#define TS_DEBUG_SCALE 32.0
 
//
//
//
 
void
ts_transform_stack_tos_debug(struct ts_transform_stack * const ts)
{
  union ts_transform_stack_3x3_u const * const t = ts_transform_stack_tos(ts);
 
  printf("{ { %13.5f, %13.5f, %13.5f },\n"
         "  { %13.5f, %13.5f, %13.5f },\n"
         "  { %13.5f, %13.5f, %13.5f } }\n",
         t->a8[0],
         t->a8[1],
         t->a8[2],
         t->a8[3],
         t->a8[4],
         t->a8[5],
         t->a8[6],
         t->a8[7],
         TS_TRANSFORM_ONE);
}
 
//
//
//
 
void
ts_debug(struct ts_transform_stack * const ts,
         ts_transform_float_t        const quad[8])
{
  ts_transform_stack_tos_debug(ts);
 
  for (int ii=0; ii<8; ii+=2)
    {
      ts_transform_float_t xp,yp;
 
      ts_transform_stack_transform_xy(ts,
                                      quad[ii],quad[ii+1],
                                      &xp,&yp);
 
      printf("( %13.2f, %13.2f ) \t-> ( %13.2f, %13.2f )\n",
             xp,yp,xp/TS_DEBUG_SCALE,yp/TS_DEBUG_SCALE);
    }
}
 
//
//
//
 
int
main(int argc, char * argv[])
{
  struct ts_transform_stack * const ts = ts_transform_stack_create(32);
 
  ts_transform_float_t const w = 1000;
  ts_transform_float_t const h = 1000;
 
#if 1
  ts_transform_stack_push_scale(ts,TS_DEBUG_SCALE,TS_DEBUG_SCALE);
 
  // OpenGL'ism
  ts_transform_stack_push_affine(ts,
                                 1.0f, 0.0f,0.0f,
                                 0.0f,-1.0f,h);
  // multiply
  ts_transform_stack_concat(ts);
#else
  ts_transform_stack_push_identity(ts);
#endif
 
  uint32_t const restore = ts_transform_stack_save(ts);
 
  //
  //
  //
  ts_transform_float_t const quad_src[8] = { 0.0f,0.0f,
                                             w,   0.0f,
                                             w,   h,
                                             0.0f,h };
 
  ts_transform_float_t const quad_dst[8] = { 300.0f,   0.0f,
                                             w-300.0f, 0.0f,
                                             w,        h,
                                             0.0f,     h };
 
  ts_transform_float_t const quad_tst[8] = { 50,   50,
                                             1550, 50,
                                             1550, 1550,
                                             50,   1550 };
  //
  // RECT TO QUAD
  //
  printf("type = %d\n",
         ts_transform_stack_push_rect_to_quad(ts,
                                              0.0, 0.0,
                                              w,   h,
                                              quad_dst));
  ts_transform_stack_concat(ts);
 
  ts_debug(ts,quad_src);
 
  //
  // QUAD TO QUAD
  //
  ts_transform_stack_restore(ts,restore);
 
  printf("type = %d\n",
         ts_transform_stack_push_quad_to_quad(ts,
                                              quad_src,
                                              quad_dst));
  ts_transform_stack_concat(ts);
 
  ts_debug(ts,quad_src);
 
  //
  // DIRECT
  //
  ts_transform_stack_restore(ts,restore);
 
  ts_transform_stack_push_matrix(ts,
                                 0.87004626f, -0.35519487f,   72.14745f,
                                 0.0f,         0.2600208f,    86.16314f,
                                 0.0f,        -0.0029599573f, 1.0f);
 
  ts_transform_stack_concat(ts);
 
  ts_transform_float_t const quad_foo[8] = { -10,  10,
                                             130,  10,
                                             130,  110,
                                             -10,  110 };
 
  ts_debug(ts,quad_foo);
 
  return EXIT_SUCCESS;
}
 
#endif
 
//
//
//