-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathVectorMath.h
1625 lines (1415 loc) · 48.6 KB
/
VectorMath.h
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
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*------------------------------------------------------------------------*/
/* VectorMath.h */
/* Joe Kniss */
/* GLUV test bed for Volume rendering */
/* Inline functions for vector mathematical manipulation man */
/* Mass props to Milan Ikits & Gordon Kindlmann for some of these */
/* THIS MUST BE MADE "C -SAFE" !!!!!!!!!!!!!!!!!!!!!!!!! */
/* */
/* ________ ____ ___ */
/* | \ / | / / */
/* +---+ \/ |/ / */
/* +---| |\ /| < */
/* | || | \ / | |\ \ */
/* | | \/ | | \ \ */
/* \_____| |__| \__\ */
/* Copyright 2001 */
/* Joe Michael Kniss */
/*------------------------------------------------------------------------*/
#ifndef __VectorMath_defined
#define __VectorMath_defined
#ifdef WIN32
#include <windows.h>
#endif
#include <GL/gl.h>
#include <math.h>
#include <stdio.h>
#include <iostream.h>
#include <limits.h>
#define EPSILON 2e-07
#define V_PI 3.1415926535897932384626433832795
#if 0 //matrix representation for mat[16], same as OpenGl matrix rep
+- -+
|0 4 8 12|
|1 5 9 13|
|2 6 10 14|
|3 7 11 15|
+- -+
#endif
/*
******** NRRD_AFFINE(i,x,I,o,O)
**
** given intervals [i,I], [o,O] and a value x which may or may not be
** inside [i,I], return the value y such that y stands in the same
** relationship to [o,O] that x does with [i,I]. Or:
**
** y - o x - i
** ------- = -------
** O - o I - i
**
** It is the callers responsibility to make sure I-i and O-o are
** both greater than zero
*/
#define CLAMP(x) ((((x)>0) ? (((x)<1) ? x : 1) : 0))
#define MAX(x,y) (((x)>(y)) ? (x) : (y))
#define MIN(x,y) (((x)<(y)) ? (x) : (y))
#define ABS(x) ((x)<0 ? (-x) : (x))
inline double CLAMP_ARB(const double c, const double x, const double C)
{
return ((((x)>c) ? (((x)<(C)) ? x : (C)) : c));
}
inline double affine(const double i, const double x, const double I,
const double o, const double O)
{
return ( ((O)-(o))*((x)-(i)) / ((I)-(i)) + (o) );
}
// make a vector all zeros -----------------------------------------
inline void zeroV3(GLfloat in[3])
{
in[0] = in[1] = in[2] = 0;
}
inline void zeroV3(GLdouble in[3])
{
in[0] = in[1] = in[2] = 0;
}
// negate all components of a vector -------------------------------
inline void negateV3(GLfloat in[3])
{
in[0] = -in[0];
in[1] = -in[1];
in[2] = -in[2];
}
inline void negateV3(GLdouble in[3])
{
in[0] = -in[0];
in[1] = -in[1];
in[2] = -in[2];
}
// copy vector 'out' = 'in';---------------------------------------
inline void copyV3(GLfloat out[3], GLfloat in[3])
{
out[0] = in[0];
out[1] = in[1];
out[2] = in[2];
}
inline void copyV3(GLdouble out[3], GLdouble in[3])
{
out[0] = in[0];
out[1] = in[1];
out[2] = in[2];
}
inline void copyV3(GLfloat out[3], GLdouble in[3])
{
out[0] = (float)in[0];
out[1] = (float)in[1];
out[2] = (float)in[2];
}
inline void copyV3(GLdouble out[3], GLfloat in[3])
{
out[0] = in[0];
out[1] = in[1];
out[2] = in[2];
}
// out = inl - inr -----------------------------------------------
inline void subV3(GLfloat out[3], GLfloat inl[3], GLfloat inr[3])
{
out[0] = inl[0] - inr[0];
out[1] = inl[1] - inr[1];
out[2] = inl[2] - inr[2];
}
inline void subV3(GLdouble out[3], GLdouble inl[3], GLdouble inr[3])
{
out[0] = inl[0] - inr[0];
out[1] = inl[1] - inr[1];
out[2] = inl[2] - inr[2];
}
// out = inl + inr ---------------------------------------------
inline void addV3(GLfloat out[3], GLfloat inl[3], GLfloat inr[3])
{
out[0] = inl[0] + inr[0];
out[1] = inl[1] + inr[1];
out[2] = inl[2] + inr[2];
}
inline void addV3(GLdouble out[3], GLdouble inl[3], GLdouble inr[3])
{
out[0] = inl[0] + inr[0];
out[1] = inl[1] + inr[1];
out[2] = inl[2] + inr[2];
}
// one *= s --------------------------------------------------
inline void scaleV3(float s, GLfloat one[3])
{
one[0] *= s;
one[1] *= s;
one[2] *= s;
}
inline void scaleV3(double s, GLdouble one[3])
{
one[0] *= s;
one[1] *= s;
one[2] *= s;
}
// out = in * s --------------------------------------------
inline void cscaleV3(GLfloat out[3], float s, GLfloat in[3])
{
out[0] = s*in[0];
out[1] = s*in[1];
out[2] = s*in[2];
}
inline void cscaleV3(GLdouble out[3], double s, GLdouble in[3])
{
out[0] = s*in[0];
out[1] = s*in[1];
out[2] = s*in[2];
}
// set a matrix 'm' to the identity -----------------------
inline void identityMatrix(GLfloat m[16])
{
m[0]=1; m[4]=0; m[8]= 0; m[12]=0;
m[1]=0; m[5]=1; m[9]= 0; m[13]=0;
m[2]=0; m[6]=0; m[10]=1; m[14]=0;
m[3]=0; m[7]=0; m[11]=0; m[15]=1;
}
inline void identityMatrix(GLdouble m[16])
{
m[0]=1; m[4]=0; m[8]= 0; m[12]=0;
m[1]=0; m[5]=1; m[9]= 0; m[13]=0;
m[2]=0; m[6]=0; m[10]=1; m[14]=0;
m[3]=0; m[7]=0; m[11]=0; m[15]=1;
}
// vector matrix multiplication [4] vector ----------------------
// out = mat * in
inline void translateV4(GLfloat out[4], GLfloat mat[16], GLfloat in[4])
{
out[0] = mat[0]*in[0] + mat[4]*in[1] + mat[8]* in[2] + mat[12]*in[3];
out[1] = mat[1]*in[0] + mat[5]*in[1] + mat[9]* in[2] + mat[13]*in[3];
out[2] = mat[2]*in[0] + mat[6]*in[1] + mat[10]*in[2] + mat[14]*in[3];
out[3] = mat[3]*in[0] + mat[7]*in[1] + mat[11]*in[2] + mat[15]*in[3];
}
//3 vector with implict w=1 mult matrix to 4 vector
inline void translateV4_3W(GLfloat out[4], GLfloat mat[16], GLfloat in[3])
{
out[0] = mat[0]*in[0] + mat[4]*in[1] + mat[8]* in[2] + mat[12];
out[1] = mat[1]*in[0] + mat[5]*in[1] + mat[9]* in[2] + mat[13];
out[2] = mat[2]*in[0] + mat[6]*in[1] + mat[10]*in[2] + mat[14];
out[3] = mat[3]*in[0] + mat[7]*in[1] + mat[11]*in[2] + mat[15];
}
inline void translateV4(GLdouble out[4], GLdouble mat[16], GLdouble in[4])
{
out[0] = mat[0]*in[0] + mat[4]*in[1] + mat[8]* in[2] + mat[12]*in[3];
out[1] = mat[1]*in[0] + mat[5]*in[1] + mat[9]* in[2] + mat[13]*in[3];
out[2] = mat[2]*in[0] + mat[6]*in[1] + mat[10]*in[2] + mat[14]*in[3];
out[3] = mat[3]*in[0] + mat[7]*in[1] + mat[11]*in[2] + mat[15]*in[3];
}
// vector matrix multiplicaiton [3] vector with no translation ---
// (only rotation) out = mat * in;
inline void translateV3(GLfloat out[3], GLfloat mat[16], GLfloat in[3])
{
out[0] = mat[0]*in[0] + mat[4]*in[1] + mat[8]* in[2];// + mat[12];
out[1] = mat[1]*in[0] + mat[5]*in[1] + mat[9]* in[2];// + mat[13];
out[2] = mat[2]*in[0] + mat[6]*in[1] + mat[10]*in[2];// + mat[14];
}
inline void translateV3(GLdouble out[3], GLdouble mat[16], GLdouble in[3])
{
out[0] = mat[0]*in[0] + mat[4]*in[1] + mat[8]* in[2];// + mat[12];
out[1] = mat[1]*in[0] + mat[5]*in[1] + mat[9]* in[2];// + mat[13];
out[2] = mat[2]*in[0] + mat[6]*in[1] + mat[10]*in[2];// + mat[14];
}
inline void translateV3(GLfloat out[3], GLdouble mat[16], GLfloat in[3])
{
out[0] = (float)(mat[0]*in[0] + mat[4]*in[1] + mat[8]* in[2]);// + mat[12];
out[1] = (float)(mat[1]*in[0] + mat[5]*in[1] + mat[9]* in[2]);// + mat[13];
out[2] = (float)(mat[2]*in[0] + mat[6]*in[1] + mat[10]*in[2]);// + mat[14];
}
// [3] vector * matrix --------------------------------------------
// out = mat * in (with translation)
inline void translateV3W(GLfloat out[3], GLfloat mat[16], GLfloat in[3])
{
out[0] = mat[0]*in[0] + mat[4]*in[1] + mat[8]* in[2] + mat[12];
out[1] = mat[1]*in[0] + mat[5]*in[1] + mat[9]* in[2] + mat[13];
out[2] = mat[2]*in[0] + mat[6]*in[1] + mat[10]*in[2] + mat[14];
}
inline void translateV3W(GLfloat out[3], GLdouble mat[16], GLfloat in[3])
{
out[0] = (float)(mat[0]*in[0] + mat[4]*in[1] + mat[8]* in[2] + mat[12]);
out[1] = (float)(mat[1]*in[0] + mat[5]*in[1] + mat[9]* in[2] + mat[13]);
out[2] = (float)(mat[2]*in[0] + mat[6]*in[1] + mat[10]*in[2] + mat[14]);
}
inline void translateV3W(GLdouble out[3], GLdouble mat[16], GLdouble in[3])
{
out[0] = mat[0]*in[0] + mat[4]*in[1] + mat[8]* in[2] + mat[12];
out[1] = mat[1]*in[0] + mat[5]*in[1] + mat[9]* in[2] + mat[13];
out[2] = mat[2]*in[0] + mat[6]*in[1] + mat[10]*in[2] + mat[14];
}
//transformation of 3 vector with implicit w=1 and homoginization
inline void translateV3WD(GLfloat out[3], GLfloat mat[16], GLfloat in[3])
{
out[0] = (float)(mat[0]*in[0] + mat[4]*in[1] + mat[8]* in[2] + mat[12]);
out[1] = (float)(mat[1]*in[0] + mat[5]*in[1] + mat[9]* in[2] + mat[13]);
out[2] = (float)(mat[2]*in[0] + mat[6]*in[1] + mat[10]*in[2] + mat[14]);
float w= (float)(mat[3]*in[0] + mat[7]*in[1] + mat[11]*in[2] + mat[15]);
out[0]/=w;
out[1]/=w;
out[2]/=w;
}
// legacy call
inline void transMatrixV3(GLfloat out[3], GLfloat mat[16], GLfloat in[3])
{
out[0] = mat[0]*in[0] + mat[4]*in[1] + mat[8]* in[2] + mat[12];
out[1] = mat[1]*in[0] + mat[5]*in[1] + mat[9]* in[2] + mat[13];
out[2] = mat[2]*in[0] + mat[6]*in[1] + mat[10]*in[2] + mat[14];
}
// dot product of two [4] vecotrs --------------------------
inline GLfloat dotV4(GLfloat one[4], GLfloat two[4])
{
return one[0]*two[0] + one[1]*two[1] + one[2]*two[2] + one[3]*two[3];
}
inline GLdouble dotV4(GLdouble one[4], GLdouble two[4])
{
return one[0]*two[0] + one[1]*two[1] + one[2]*two[2] + one[3]*two[3];
}
// dot product of two [3] vectors ------------------------
inline GLfloat dotV3(GLfloat one[4], GLfloat two[4])
{
return one[0]*two[0] + one[1]*two[1] + one[2]*two[2];
}
inline GLdouble dotV3(GLdouble one[4], GLdouble two[4])
{
return one[0]*two[0] + one[1]*two[1] + one[2]*two[2];
}
// compute the length of a [3] vector --------------------
inline GLfloat normV3(GLfloat one[3])
{
return (GLfloat)sqrt( one[0]*one[0] + one[1]*one[1] + one[2]*one[2]);
}
inline GLdouble normV3(GLdouble one[3])
{
return sqrt( one[0]*one[0] + one[1]*one[1] + one[2]*one[2]);
}
// normalize a [4] vector --------------------------------
inline void normalizeV4(GLfloat v[4])
{
GLfloat len = (float)sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2] + v[3]*v[3]);
if(len > 0){
v[0] /= len;
v[1] /= len;
v[2] /= len;
v[3] /= len;
}
}
inline void normalizeV4(GLdouble v[4])
{
GLfloat len = (float)sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2] + v[3]*v[3]);
if(len > 0){
v[0] /= len;
v[1] /= len;
v[2] /= len;
v[3] /= len;
}
}
// normalize a [3] vector ---------------------------------
inline void normalizeV3(GLfloat v[3])
{
GLfloat len = (float)sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);
if(len > 0){
v[0] /= len;
v[1] /= len;
v[2] /= len;
}
}
inline void normalizeV3(GLdouble v[3])
{
GLfloat len = (float)sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);
if(len > 0){
v[0] /= len;
v[1] /= len;
v[2] /= len;
}
}
// divide out the 'W' part of a [4] vector ----------------
inline void homogV4(GLfloat v[4])
{
v[0] /= v[3];
v[1] /= v[3];
v[2] /= v[3];
v[3] = 1;
}
inline void homogV4(GLdouble v[4])
{
v[0] /= v[3];
v[1] /= v[3];
v[2] /= v[3];
v[3] = 1;
}
// compute cross product of [3] vector -------------------
// out = one X two;
inline void crossV3(GLfloat out[3], GLfloat one[3], GLfloat two[3])
{
out[0] = one[1]*two[2] - one[2]*two[1];
out[1] = one[2]*two[0] - one[0]*two[2];
out[3] = one[0]*two[1] - one[1]*two[0];
}
inline void crossV3(GLdouble out[3], GLdouble one[3], GLdouble two[3])
{
out[0] = one[1]*two[2] - one[2]*two[1];
out[1] = one[2]*two[0] - one[0]*two[2];
out[3] = one[0]*two[1] - one[1]*two[0];
}
// copy two matricies -----------------------------------
inline void matrixEqual(GLfloat me[16], GLfloat m[16])
{
for(int i=0; i< 16; ++i) me[i] = m[i];
}
inline void matrixEqual(GLdouble me[16], GLdouble m[16])
{
for(int i=0; i< 16; ++i) me[i] = m[i];
}
// maxb = ma * mb --------------------------------------
inline void matrixMult( GLfloat maxb[16], GLfloat ma[16], GLfloat mb[16] )
{
maxb[0] = ma[0]*mb[0] + ma[4]*mb[1] + ma[8]*mb[2] + ma[12]*mb[3];
maxb[1] = ma[1]*mb[0] + ma[5]*mb[1] + ma[9]*mb[2] + ma[13]*mb[3];
maxb[2] = ma[2]*mb[0] + ma[6]*mb[1] + ma[10]*mb[2] + ma[14]*mb[3];
maxb[3] = ma[3]*mb[0] + ma[7]*mb[1] + ma[11]*mb[2] + ma[15]*mb[3];
maxb[4] = ma[0]*mb[4] + ma[4]*mb[5] + ma[8]*mb[6] + ma[12]*mb[7];
maxb[5] = ma[1]*mb[4] + ma[5]*mb[5] + ma[9]*mb[6] + ma[13]*mb[7];
maxb[6] = ma[2]*mb[4] + ma[6]*mb[5] + ma[10]*mb[6] + ma[14]*mb[7];
maxb[7] = ma[3]*mb[4] + ma[7]*mb[5] + ma[11]*mb[6] + ma[15]*mb[7];
maxb[8] = ma[0]*mb[8] + ma[4]*mb[9] + ma[8]*mb[10] + ma[12]*mb[11];
maxb[9] = ma[1]*mb[8] + ma[5]*mb[9] + ma[9]*mb[10] + ma[13]*mb[11];
maxb[10] = ma[2]*mb[8] + ma[6]*mb[9] + ma[10]*mb[10] + ma[14]*mb[11];
maxb[11] = ma[3]*mb[8] + ma[7]*mb[9] + ma[11]*mb[10] + ma[15]*mb[11];
maxb[12] = ma[0]*mb[12] + ma[4]*mb[13] + ma[8]*mb[14] + ma[12]*mb[15];
maxb[13] = ma[1]*mb[12] + ma[5]*mb[13] + ma[9]*mb[14] + ma[13]*mb[15];
maxb[14] = ma[2]*mb[12] + ma[6]*mb[13] + ma[10]*mb[14] + ma[14]*mb[15];
maxb[15] = ma[3]*mb[12] + ma[7]*mb[13] + ma[11]*mb[14] + ma[15]*mb[15];
}
inline void matrixMult( GLdouble maxb[16], GLdouble ma[16], GLdouble mb[16] )
{
maxb[0] = ma[0]*mb[0] + ma[4]*mb[1] + ma[8]*mb[2] + ma[12]*mb[3];
maxb[1] = ma[1]*mb[0] + ma[5]*mb[1] + ma[9]*mb[2] + ma[13]*mb[3];
maxb[2] = ma[2]*mb[0] + ma[6]*mb[1] + ma[10]*mb[2] + ma[14]*mb[3];
maxb[3] = ma[3]*mb[0] + ma[7]*mb[1] + ma[11]*mb[2] + ma[15]*mb[3];
maxb[4] = ma[0]*mb[4] + ma[4]*mb[5] + ma[8]*mb[6] + ma[12]*mb[7];
maxb[5] = ma[1]*mb[4] + ma[5]*mb[5] + ma[9]*mb[6] + ma[13]*mb[7];
maxb[6] = ma[2]*mb[4] + ma[6]*mb[5] + ma[10]*mb[6] + ma[14]*mb[7];
maxb[7] = ma[3]*mb[4] + ma[7]*mb[5] + ma[11]*mb[6] + ma[15]*mb[7];
maxb[8] = ma[0]*mb[8] + ma[4]*mb[9] + ma[8]*mb[10] + ma[12]*mb[11];
maxb[9] = ma[1]*mb[8] + ma[5]*mb[9] + ma[9]*mb[10] + ma[13]*mb[11];
maxb[10] = ma[2]*mb[8] + ma[6]*mb[9] + ma[10]*mb[10] + ma[14]*mb[11];
maxb[11] = ma[3]*mb[8] + ma[7]*mb[9] + ma[11]*mb[10] + ma[15]*mb[11];
maxb[12] = ma[0]*mb[12] + ma[4]*mb[13] + ma[8]*mb[14] + ma[12]*mb[15];
maxb[13] = ma[1]*mb[12] + ma[5]*mb[13] + ma[9]*mb[14] + ma[13]*mb[15];
maxb[14] = ma[2]*mb[12] + ma[6]*mb[13] + ma[10]*mb[14] + ma[14]*mb[15];
maxb[15] = ma[3]*mb[12] + ma[7]*mb[13] + ma[11]*mb[14] + ma[15]*mb[15];
}
// compute the inverse of a matrix
// invm = m^(-1)
inline void inverseMatrix( GLfloat invm[16], GLfloat m[16] )
{
GLfloat det =
m[0]*m[5]*m[10]-
m[0]*m[6]*m[9]-
m[1]*m[4]*m[10]+
m[1]*m[6]*m[8]+
m[2]*m[4]*m[9]-
m[2]*m[5]*m[8];
invm[0] = (m[5]*m[10]-m[6]*m[9])/det;
invm[1] = (-m[1]*m[10]+m[2]*m[9])/det;
invm[2] = (m[1]*m[6]-m[2]*m[5])/det;
invm[3] = 0.0;
invm[4] = (-m[4]*m[10]+m[6]*m[8])/det;
invm[5] = (m[0]*m[10]-m[2]*m[8])/det;
invm[6] = (-m[0]*m[6]+m[2]*m[4])/det;
invm[7] = 0.0;
invm[8] = (m[4]*m[9]-m[5]*m[8])/det;
invm[9] = (-m[0]*m[9]+m[1]*m[8])/det;
invm[10] = (m[0]*m[5]-m[1]*m[4])/det;
invm[11] = 0.0;
invm[12] = (-m[4]*m[9]*m[14]+m[4]*m[13]*m[10]+
m[5]*m[8]*m[14]-m[5]*m[12]*m[10]-
m[6]*m[8]*m[13]+m[6]*m[12]*m[9])/det;
invm[13] = (m[0]*m[9]*m[14]-m[0]*m[13]*m[10]-
m[1]*m[8]*m[14]+m[1]*m[12]*m[10]+
m[2]*m[8]*m[13]-m[2]*m[12]*m[9])/det;
invm[14] = (-m[0]*m[5]*m[14]+m[0]*m[13]*m[6]+
m[1]*m[4]*m[14]-m[1]*m[12]*m[6]-
m[2]*m[4]*m[13]+m[2]*m[12]*m[5])/det;
invm[15] = 1.0;
}
inline void inverseMatrix( GLdouble invm[16], GLdouble m[16] )
{
GLfloat det = (float)(
m[0]*m[5]*m[10]-
m[0]*m[6]*m[9]-
m[1]*m[4]*m[10]+
m[1]*m[6]*m[8]+
m[2]*m[4]*m[9]-
m[2]*m[5]*m[8]);
invm[0] = (m[5]*m[10]-m[6]*m[9])/det;
invm[1] = (-m[1]*m[10]+m[2]*m[9])/det;
invm[2] = (m[1]*m[6]-m[2]*m[5])/det;
invm[3] = 0.0;
invm[4] = (-m[4]*m[10]+m[6]*m[8])/det;
invm[5] = (m[0]*m[10]-m[2]*m[8])/det;
invm[6] = (-m[0]*m[6]+m[2]*m[4])/det;
invm[7] = 0.0;
invm[8] = (m[4]*m[9]-m[5]*m[8])/det;
invm[9] = (-m[0]*m[9]+m[1]*m[8])/det;
invm[10] = (m[0]*m[5]-m[1]*m[4])/det;
invm[11] = 0.0;
invm[12] = (-m[4]*m[9]*m[14]+m[4]*m[13]*m[10]+
m[5]*m[8]*m[14]-m[5]*m[12]*m[10]-
m[6]*m[8]*m[13]+m[6]*m[12]*m[9])/det;
invm[13] = (m[0]*m[9]*m[14]-m[0]*m[13]*m[10]-
m[1]*m[8]*m[14]+m[1]*m[12]*m[10]+
m[2]*m[8]*m[13]-m[2]*m[12]*m[9])/det;
invm[14] = (-m[0]*m[5]*m[14]+m[0]*m[13]*m[6]+
m[1]*m[4]*m[14]-m[1]*m[12]*m[6]-
m[2]*m[4]*m[13]+m[2]*m[12]*m[5])/det;
invm[15] = 1.0;
}
//transpose a matrix
inline void transposeMatrix(GLfloat m[16])
{
GLfloat tmp;
tmp = m[1];
m[1] = m[4];
m[4] = tmp;
tmp = m[2];
m[2] = m[8];
m[8] = tmp;
tmp = m[3];
m[3] = m[12];
m[12] = tmp;
tmp = m[6];
m[6] = m[9];
m[9] = tmp;
tmp = m[7];
m[7] = m[13];
m[13] = tmp;
tmp = m[11];
m[11] = m[14];
m[14] = tmp;
}
inline void transposeMatrix(GLdouble m[16])
{
GLdouble tmp;
tmp = m[1];
m[1] = m[4];
m[4] = tmp;
tmp = m[2];
m[2] = m[8];
m[8] = tmp;
tmp = m[3];
m[3] = m[12];
m[12] = tmp;
tmp = m[6];
m[6] = m[9];
m[9] = tmp;
tmp = m[7];
m[7] = m[13];
m[13] = tmp;
tmp = m[11];
m[11] = m[14];
m[14] = tmp;
}
//angle and axis to a rotation matrix ----------------------------
inline void axis2Rot( GLfloat m[16], GLfloat k[3], GLfloat theta )
{
float c = (float)cos(theta);
float s = (float)sin(theta);
float v = 1 - c;
m[0] = k[0]*k[0]*v + c;
m[4] = k[0]*k[1]*v - k[2]*s;
m[8] = k[0]*k[2]*v + k[1]*s;
m[1] = k[0]*k[1]*v + k[2]*s;
m[5] = k[1]*k[1]*v + c;
m[9] = k[1]*k[2]*v - k[0]*s;
m[2] = k[0]*k[2]*v - k[1]*s;
m[6] = k[1]*k[2]*v + k[0]*s;
m[10] = k[2]*k[2]*v + c;
}
inline void axis2Rot( GLdouble m[16], GLdouble k[3], GLdouble theta )
{
float c = (float)cos(theta);
float s = (float)sin(theta);
float v = 1 - c;
m[0] = k[0]*k[0]*v + c;
m[4] = k[0]*k[1]*v - k[2]*s;
m[8] = k[0]*k[2]*v + k[1]*s;
m[1] = k[0]*k[1]*v + k[2]*s;
m[5] = k[1]*k[1]*v + c;
m[9] = k[1]*k[2]*v - k[0]*s;
m[2] = k[0]*k[2]*v - k[1]*s;
m[6] = k[1]*k[2]*v + k[0]*s;
m[10] = k[2]*k[2]*v + c;
}
// Inverse angle-axis formula.-----------------------------------
inline void rot2Axis( GLfloat k[3], GLfloat *theta, GLfloat m[16] )
{
GLfloat c = (float)(0.5 * (m[0] + m[5] + m[10] - 1.0));
GLfloat r1 = m[6] - m[9];
GLfloat r2 = m[8] - m[2];
GLfloat r3 = m[1] - m[4];
GLfloat s = (float)(0.5 * sqrt(r1*r1+r2*r2+r3*r3));
*theta = (float)atan2(s, c);
if( fabs(s) > EPSILON )
{
c = (float)(2.0*s);
k[0] = r1 / c;
k[1] = r2 / c;
k[2] = r3 / c;
}
else
{
if( c > 0 ) // theta = 0
{
k[0] = 0;
k[1] = 0;
k[2] = 1;
}
else // theta = +-pi: Shepperd procedure
{
GLfloat k0_2 = (m[0] + 1)/2;
GLfloat k1_2 = (m[5] + 1)/2;
GLfloat k2_2 = (m[10] + 1)/2;
if( k0_2 > k1_2 )
{
if( k0_2 > k2_2 ) // k0 biggest
{
k[0] = (float)sqrt(k1_2);
k[1] = (m[1] + m[4])/(4*k[0]);
k[2] = (m[2] + m[8])/(4*k[0]);
}
else // k2 biggest
{
k[2] = (float)sqrt(k2_2);
k[0] = (m[2] + m[8])/(4*k[2]);
k[1] = (m[6] + m[9])/(4*k[2]);
}
}
else
{
if( k1_2 > k2_2 ) // k1 biggest
{
k[1] = (float)sqrt(k1_2);
k[0] = (m[1] + m[4])/(4*k[1]);
k[2] = (m[6] + m[9])/(4*k[1]);
}
else // k2 biggest
{
k[2] = (float)sqrt(k2_2);
k[0] = (m[2] + m[8])/(4*k[2]);
k[1] = (m[6] + m[9])/(4*k[2]);
}
}
}
}
}
//quaternion to rotation matrix
inline void q2R( GLfloat m[16], GLfloat q[4] )
{
m[0] = q[0]*q[0] + q[1]*q[1] - q[2]*q[2] - q[3]*q[3];
m[1] = 2*q[1]*q[2] + 2*q[0]*q[3];
m[2] = 2*q[1]*q[3] - 2*q[0]*q[2];
m[4] = 2*q[1]*q[2] - 2*q[0]*q[3];
m[5] = q[0]*q[0] + q[2]*q[2] - q[1]*q[1] - q[3]*q[3];
m[6] = 2*q[2]*q[3] + 2*q[0]*q[1];
m[8] = 2*q[1]*q[3] + 2*q[0]*q[2];
m[9] = 2*q[2]*q[3] - 2*q[0]*q[1];
m[10]= q[0]*q[0] + q[3]*q[3] - q[1]*q[1] - q[2]*q[2];
}
//angle axis to quaternion
inline void axis2q(GLfloat q[4], float theta, float a[3])
{
normalizeV3(a);
copyV3(q, a);
scaleV3((float)sin(theta/2.0), q);
q[3] = (float)cos(theta/2.0);
}
//normalize a quaternion
inline void normalizeQ(GLfloat q[4])
{
float mag = (q[0]*q[0] + q[1]*q[1] + q[2]*q[2] + q[3]*q[3]);
for (int i = 0; i < 4; i++) q[i] /= mag;
}
/*
* Prints matrix to stderr.
*/
inline void printMatrix( GLfloat m[16] )
{
int i, j;
for( i=0; i<4; i++ )
{
for( j=0; j<4; j++ )
fprintf(stderr, "%f ", m[i+j*4]);
fprintf(stderr, "\n");
}
fprintf(stderr, "\n");
}
inline void printMatrix( GLdouble m[16] )
{
int i, j;
for( i=0; i<4; i++ )
{
for( j=0; j<4; j++ )
fprintf(stderr, "%f ", m[i+j*4]);
fprintf(stderr, "\n");
}
fprintf(stderr, "\n");
}
inline void printV3(double v[3])
{
cerr << " " << v[0] << "," << v[1] << "," << v[2];
}
inline void printV3(float v[3])
{
cerr << " " << v[0] << "," << v[1] << "," << v[2];
}
inline void printV4(float v[3])
{
cerr << " " << v[0] << "," << v[1] << "," << v[2] << "," << v[3];
}
inline void buildLookAt(GLfloat m[16],
GLfloat eye[3], GLfloat at[3], GLfloat up[3])
{
//I am not 100% certain that my cross products are in the right order
GLfloat tmp1[3], tmp2[3], tmp3[3];
subV3(tmp1, eye, at);
GLfloat norm;
norm = normV3(tmp1);
tmp1[0] /=norm;
tmp1[1] /=norm;
tmp1[2] /=norm;
m[2] =tmp1[0];
m[6] =tmp1[1];
m[10] =tmp1[2];
crossV3(tmp2, up, tmp1);
norm = normV3(tmp2);
tmp2[0] /=norm;
tmp2[1] /=norm;
tmp2[2] /=norm;
m[0] =tmp2[0];
m[4] =tmp2[1];
m[8] =tmp2[2];
crossV3(tmp3, tmp1, tmp2);
norm = normV3(tmp3);
tmp3[0] /=norm;
tmp3[1] /=norm;
tmp3[2] /=norm;
m[1] =tmp3[0];
m[5] =tmp3[1];
m[9] =tmp3[2];
m[12]= -eye[0];
m[13]= -eye[1];
m[14]= -eye[2];
m[15]= 1;
m[3] = 0;
m[7] = 0;
m[11] = 0;
}
//======================== calculus ===================================
//standard scalar derivative measure
inline derivative3D(unsigned char *magV1,
float *gradV3,
const int sx, const int sy, const int sz,
const unsigned char *dat)
{
float *tmp = new float[sx*sy*sz];
float maxm = 0;
for(int i = 0; i < sz; ++i){
for(int j = 0; j < (sy); ++j){
for(int k = 0; k < (sx); ++k){
if((k<2)||(k>sx-3)||(j<2)||(j>sy-3)||(i<2)||(i>sz-3)){
gradV3[i*sx*sy*3 + j*sx*3 + k*3 + 0] = 0;
gradV3[i*sx*sy*3 + j*sx*3 + k*3 + 1] = 0;
gradV3[i*sx*sy*3 + j*sx*3 + k*3 + 2] = 0;
tmp[i*sx*sy + j*sx + k] = 0;
}
else {
int dx = dat[i*sx*sy + j*sx + (k+1)] - dat[i*sx*sy + j*sx + (k-1)];
int dy = dat[i*sx*sy + (j+1)*sx + k] - dat[i*sx*sy + (j-1)*sx + k];
int dz = dat[(i+1)*sx*sy + j*sx + k] - dat[(i-1)*sx*sy + j*sx + k];
gradV3[i*sx*sy*3 + j*sx*3 + k*3 + 0] = (float)dx;
gradV3[i*sx*sy*3 + j*sx*3 + k*3 + 1] = (float)dy;
gradV3[i*sx*sy*3 + j*sx*3 + k*3 + 2] = (float)dz;
tmp[i*sx*sy + j*sx + k] = (float)sqrt(dx*dx + dy*dy + dz*dz);
maxm = maxm < tmp[i*sx*sy + j*sx + k] ? tmp[i*sx*sy + j*sx + k] : maxm;
}
}
}
}
for( i = 0; i < sz; ++i){
for(int j = 0; j < sy; ++j){
for(int k = 0; k < sx; ++k){
magV1[i*sx*sy + j*sx + k] = (unsigned char)((tmp[i*sx*sy + j*sx + k]/maxm)*255);
}
}
}
delete[] tmp;
}
//standard scalar derivative measure - vgh volume (value grad hessian)
inline derivative3DVGH(float *gradV3,
const int sx, const int sy, const int sz,
const unsigned char *dat)
{
//cerr << "hi!!!!!!!!!! " << sz << " " << sy << " " << sx << endl;
for(int i = 0; i < sz; ++i){
for(int j = 0; j < (sy); ++j){
for(int k = 0; k < (sx); ++k){
if((k<1)||(k>sx-2)||(j<1)||(j>sy-2)||(i<1)||(i>sz-2)){
gradV3[i*sx*sy*3 + j*sx*3 + k*3 + 0] = 0;
gradV3[i*sx*sy*3 + j*sx*3 + k*3 + 1] = 0;
gradV3[i*sx*sy*3 + j*sx*3 + k*3 + 2] = 0;
}
else {
int dx = dat[i*sx*sy*3 + j*sx*3 +(k+1)*3] - dat[i*sx*sy*3 + j*sx*3 + (k-1)*3];
int dy = dat[i*sx*sy*3 + (j+1)*sx*3 + k*3] - dat[i*sx*sy*3 + (j-1)*sx*3 + k*3];
int dz = dat[(i+1)*sx*sy*3 + j*sx*3 + k*3] - dat[(i-1)*sx*sy*3 + j*sx*3 + k*3];
gradV3[i*sx*sy*3 + j*sx*3 + k*3 + 0] = (float)dx;
gradV3[i*sx*sy*3 + j*sx*3 + k*3 + 1] = (float)dy;
gradV3[i*sx*sy*3 + j*sx*3 + k*3 + 2] = (float)dz;
}
}
}
}
}
inline void addDer(unsigned char *magV1, float *gradV3,
const int sx, const int sy, const int sz,
const unsigned char *dat1, const unsigned char *dat2)
{
float *tmp = new float[sx*sy*sz];
float maxm = 0;
for(int i = 0; i < sz; ++i){
for(int j = 0; j < (sy); ++j){
for(int k = 0; k < (sx); ++k){
if((k<2)||(k>sx-3)||(j<2)||(j>sy-3)||(i<2)||(i>sz-3)){
gradV3[i*sx*sy*3 + j*sx*3 + k*3 + 0] = 0;
gradV3[i*sx*sy*3 + j*sx*3 + k*3 + 1] = 0;
gradV3[i*sx*sy*3 + j*sx*3 + k*3 + 2] = 0;
tmp[i*sx*sy + j*sx + k] = 0;
}
else {
int dx1 = dat1[i*sx*sy + j*sx + (k+1)] - dat1[i*sx*sy + j*sx + (k-1)];
int dy1 = dat1[i*sx*sy + (j+1)*sx + k] - dat1[i*sx*sy + (j-1)*sx + k];
int dz1 = dat1[(i+1)*sx*sy + j*sx + k] - dat1[(i-1)*sx*sy + j*sx + k];
int dx2 = dat2[i*sx*sy + j*sx + (k+1)] - dat2[i*sx*sy + j*sx + (k-1)];
int dy2 = dat2[i*sx*sy + (j+1)*sx + k] - dat2[i*sx*sy + (j-1)*sx + k];
int dz2 = dat2[(i+1)*sx*sy + j*sx + k] - dat2[(i-1)*sx*sy + j*sx + k];
gradV3[i*sx*sy*3 + j*sx*3 + k*3 + 0] = (float)(dx1 + dx2);
gradV3[i*sx*sy*3 + j*sx*3 + k*3 + 1] = (float)(dy1 + dy2);
gradV3[i*sx*sy*3 + j*sx*3 + k*3 + 2] = (float)(dz1 + dz2);
tmp[i*sx*sy + j*sx + k] = (float)sqrt((dx1+dx2)*(dx1+dx2) + (dy1+dy2)*(dy1+dy2) + (dz1+dz2)*(dz1+dz2));
maxm = maxm < tmp[i*sx*sy + j*sx + k] ? tmp[i*sx*sy + j*sx + k] : maxm;
}
}
}
}
for( i = 0; i < sz; ++i){
for(int j = 0; j < sy; ++j){
for(int k = 0; k < sx; ++k){
magV1[i*sx*sy + j*sx + k] = (unsigned char)((tmp[i*sx*sy + j*sx + k]/maxm)*255);
}
}
}
delete[] tmp;
}
template<class T> //compute an additive gradient
inline
int AGradArb(float *gradV3, //put it in here, create your data first
int sx, int sy, int sz, //size of each axis
int n, //first n elements of data to compute grad with
int elts, //number of elements in the data
T *data) //the data
{
#if 0
T **minmax = new T*[elts];
for(int e=0; e<elts; ++e){
minmax[e] = new T[2];
minmax[e][0] = (T)10000000000;
minmax[e][1] = (T)-10000000000;
}
for(int i = 0; i < sz; ++i){
for(int j = 0; j < (sy); ++j){
for(int k = 0; k < (sx); ++k){
for(int e=0; e<n; ++e){
minmax[e][0] = MIN(data[i*sx*sy*elts + j*sx*elts + (k+1)*elts + e], minmax[e][0]);
minmax[e][1] = MAX(data[i*sx*sy*elts + j*sx*elts + (k+1)*elts + e], minmax[e][1]);
}
}
}
}
for(e=0; e<elts; ++e){
cerr << "Volume " << e << " , min: " << (float)minmax[e][0] << " max: " << (float)minmax[e][1] << endl;
}
#endif
for(int i = 0; i < sz; ++i)
{
for(int j = 0; j < (sy); ++j)
{
for(int k = 0; k < (sx); ++k)
{
if((k<1)||(k>sx-2)||(j<1)||(j>sy-2)||(i<1)||(i>sz-2))
{
gradV3[i*sx*sy*3 + j*sx*3 + k*3 + 0] = 0;
gradV3[i*sx*sy*3 + j*sx*3 + k*3 + 1] = 0;
gradV3[i*sx*sy*3 + j*sx*3 + k*3 + 2] = 0;
}
else
{
gradV3[i*sx*sy*3 + j*sx*3 + k*3 + 0] = 0;
gradV3[i*sx*sy*3 + j*sx*3 + k*3 + 1] = 0;
gradV3[i*sx*sy*3 + j*sx*3 + k*3 + 2] = 0;
for(int e=0; e<n; ++e)
{
gradV3[i*sx*sy*3 + j*sx*3 + k*3 + 0] += data[i*sx*sy*elts + j*sx*elts + (k+1)*elts + e] -
data[i*sx*sy*elts + j*sx*elts + (k-1)*elts + e];
gradV3[i*sx*sy*3 + j*sx*3 + k*3 + 1] += data[i*sx*sy*elts + (j+1)*sx*elts + k*elts + e] -
data[i*sx*sy*elts + (j-1)*sx*elts + k*elts + e];
gradV3[i*sx*sy*3 + j*sx*3 + k*3 + 2] += data[(i+1)*sx*sy*elts + j*sx*elts + k*elts + e] -
data[(i-1)*sx*sy*elts + j*sx*elts + k*elts + e];
}