NCBI C++ ToolKit
globals.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef GUI_MATH___GLOBALS__HPP
2 #define GUI_MATH___GLOBALS__HPP
3 
4 /* $Id: globals.hpp 14973 2007-09-14 12:11:32Z dicuccio $
5  * ===========================================================================
6  *
7  * PUBLIC DOMAIN NOTICE
8  * National Center for Biotechnology Information
9  *
10  * This software / database is a "United States Government Work" under the
11  * terms of the United States Copyright Act. It was written as part of
12  * the author's official duties as a United States Government employee and
13  * thus cannot be copyrighted. This software / database is freely available
14  * to the public for use. The National Library of Medicine and the U.S.
15  * Government have not placed any restriction on its use or reproduction.
16  *
17  * Although all reasonable efforts have been taken to ensure the accuracy
18  * and reliability of the software and data, the NLM and the U.S.
19  * Government do not and cannot warrant the performance or results that
20  * may be obtained by using this software or data. The NLM and the U.S.
21  * Government disclaim all warranties, express or implied, including
22  * warranties of performance, merchantability or fitness for any particular
23  * purpose.
24  *
25  * Please cite the author in any work or product based on this material.
26  *
27  * ===========================================================================
28  *
29  * Authors: Mike DiCuccio
30  *
31  * File Description:
32  *
33  */
34 
35 #include <corelib/ncbistd.hpp>
36 #include <vector>
37 
38 /** @addtogroup GUI_MATH
39  *
40  * @{
41  */
42 
44 
45 // predeclarations of the types we use
46 template <class T> class CVect2;
47 template <class T> class CVect3;
48 template <class T> class CVect4;
49 template <class T> class CMatrix3;
50 template <class T> class CMatrix4;
51 
53 
54 // Promotion rules
55 // we keep these in a separate file because they're long
56 #include <gui/utils/promote.hpp>
57 
58 #include <gui/utils/vect2.hpp>
59 #include <gui/utils/vect3.hpp>
60 #include <gui/utils/vect4.hpp>
61 #include <gui/utils/matrix3.hpp>
62 #include <gui/utils/matrix4.hpp>
63 
64 // general utilities
65 #include <math.h>
66 #include <gui/utils/math.hpp>
67 
68 
70 
71 //
72 //
73 // Global operators
74 //
75 //
76 
77 
78 
79 //
80 //
81 // CVect<> templates
82 //
83 //
84 
85 //
86 // global addition: CVect*<> + CVect*<>
87 //
88 template <class T, class U>
89 inline CVect2< NCBI_PROMOTE(T,U) >
90 operator+ (const CVect2<T>& v1, const CVect2<U>& v2)
91 {
92  return
94  (v1[0]+v2[0], v1[1]+v2[1]);
95 }
96 
97 
98 template <class T, class U>
99 inline CVect3< NCBI_PROMOTE(T,U) >
100 operator+ (const CVect3<T>& v1, const CVect3<U>& v2)
101 {
102  return
104  (v1[0]+v2[0], v1[1]+v2[1], v1[2]+v2[2]);
105 }
106 
107 
108 template <class T, class U>
109 inline CVect4< NCBI_PROMOTE(T,U) >
110 operator+ (const CVect4<T>& v1, const CVect4<U>& v2)
111 {
112  return
114  (v1[0]+v2[0], v1[1]+v2[1], v1[2]+v2[2], v1[3]+v2[3]);
115 }
116 
117 
118 //
119 // global addition: const + CVect*<>
120 // we do this with macros because not all compilers support partial template
121 // specialization
122 //
123 
124 #define NCBI_ADD_VECT2(type) \
125 template <class U> \
126 inline CVect2< NCBI_PROMOTE(type,U) > \
127 operator+ (type v1, const CVect2<U>& v2) \
128 { \
129  return \
130  CVect2< NCBI_PROMOTE(type,U) > \
131  (v1 + v2[0], v1 + v2[1]); \
132 } \
133 template <class T> \
134 inline CVect2< NCBI_PROMOTE(T,type) > \
135 operator+ (const CVect2<T>& v1, type v2) \
136 { \
137  return \
138  CVect2< NCBI_PROMOTE(T,type) > \
139  (v1[0] + v2, v1[1] + v2); \
140 }
141 
142 
146 
147 #undef NCBI_ADD_VECT2
148 
149 
150 #define NCBI_ADD_VECT3(type) \
151 template <class U> \
152 inline CVect3< NCBI_PROMOTE(type,U) > \
153 operator+ (type v1, const CVect3<U>& v2) \
154 { \
155  return \
156  CVect3< NCBI_PROMOTE(type,U) > \
157  (v1 + v2[0], v1 + v2[1], v1 + v2[2]); \
158 } \
159 template <class T> \
160 inline CVect3< NCBI_PROMOTE(T,type) > \
161 operator+ (const CVect3<T>& v1, type v2) \
162 { \
163  return \
164  CVect3< NCBI_PROMOTE(T,type) > \
165  (v1[0] + v2, v1[1] + v2, v1[2] + v2); \
166 }
167 
168 
172 
173 #undef NCBI_ADD_VECT3
174 
175 
176 #define NCBI_ADD_VECT4(type) \
177 template <class U> \
178 inline CVect4< NCBI_PROMOTE(type,U) > \
179 operator+ (type v1, const CVect4<U>& v2) \
180 { \
181  return \
182  CVect4< NCBI_PROMOTE(type,U) > \
183  (v1 + v2[0], v1 + v2[1], v1 + v2[2], v1 + v2[3]); \
184 } \
185 template <class T> \
186 inline CVect4< NCBI_PROMOTE(T,type) > \
187 operator+ (const CVect4<T>& v1, type v2) \
188 { \
189  return \
190  CVect4< NCBI_PROMOTE(T,type) > \
191  (v1[0] + v2, v1[1] + v2, v1[2] + v2, v1[3] + v2); \
192 }
193 
194 
198 
199 #undef NCBI_ADD_VECT4
200 
201 
202 //
203 // global unary negation
204 //
205 template <class T>
206 inline CVect2<T>
208 {
209  return CVect2<T> (-v[0], -v[1]);
210 }
211 
212 
213 template <class T>
214 inline CVect3<T>
216 {
217  return CVect3<T> (-v[0], -v[1], -v[2]);
218 }
219 
220 
221 template <class T>
222 inline CVect4<T>
224 {
225  return CVect4<T> (-v[0], -v[1], -v[2], -v[3]);
226 }
227 
228 //
229 // global subtraction: CVect*<> - CVect*<>
230 //
231 template <class T, class U>
232 inline CVect2< NCBI_PROMOTE(T,U) >
233 operator- (const CVect2<T>& v1, const CVect2<U>& v2)
234 {
235  return
237  (v1[0]-v2[0], v1[1]-v2[1]);
238 }
239 
240 
241 template <class T, class U>
242 inline CVect3< NCBI_PROMOTE(T,U) >
243 operator- (const CVect3<T>& v1, const CVect3<U>& v2)
244 {
245  return
247  (v1[0]-v2[0], v1[1]-v2[1], v1[2]-v2[2]);
248 }
249 
250 
251 template <class T, class U>
252 inline CVect4< NCBI_PROMOTE(T,U) >
253 operator- (const CVect4<T>& v1, const CVect4<U>& v2)
254 {
255  return
257  (v1[0]-v2[0], v1[1]-v2[1], v1[2]-v2[2], v1[3]-v2[3]);
258 }
259 
260 
261 //
262 // global subtraction: const + CVect*<>
263 // we do this with macros because not all compilers support partial template
264 // specialization
265 //
266 
267 #define NCBI_SUBTRACT_VECT2(type) \
268 template <class U> \
269 inline CVect2< NCBI_PROMOTE(type,U) > \
270 operator- (type v1, const CVect2<U>& v2) \
271 { \
272  return \
273  CVect2< NCBI_PROMOTE(type,U) > \
274  (v1 - v2[0], v1 - v2[1]); \
275 } \
276 template <class T> \
277 inline CVect2< NCBI_PROMOTE(T,type) > \
278 operator- (const CVect2<T>& v1, type v2) \
279 { \
280  return \
281  CVect2< NCBI_PROMOTE(T,type) > \
282  (v1[0] - v2, v1[1] - v2); \
283 }
284 
285 
289 
290 #undef NCBI_SUBTRACT_VECT2
291 
292 
293 #define NCBI_SUBTRACT_VECT3(type) \
294 template <class U> \
295 inline CVect3< NCBI_PROMOTE(type,U) > \
296 operator- (type v1, const CVect3<U>& v2) \
297 { \
298  return \
299  CVect3< NCBI_PROMOTE(type,U) > \
300  (v1 - v2[0], v1 - v2[1], v1 - v2[2]); \
301 } \
302 template <class T> \
303 inline CVect3< NCBI_PROMOTE(T,type) > \
304 operator- (const CVect3<T>& v1, type v2) \
305 { \
306  return \
307  CVect3< NCBI_PROMOTE(T,type) > \
308  (v1[0] - v2, v1[1] - v2, v1[2] - v2); \
309 }
310 
311 
315 
316 #undef NCBI_SUBTRACT_VECT3
317 
318 
319 #define NCBI_SUBTRACT_VECT4(type) \
320 template <class U> \
321 inline CVect4< NCBI_PROMOTE(type,U) > \
322 operator- (type v1, const CVect4<U>& v2) \
323 { \
324  return \
325  CVect4< NCBI_PROMOTE(type,U) > \
326  (v1 - v2[0], v1 - v2[1], v1 - v2[2], v1 - v2[3]); \
327 } \
328 template <class T> \
329 inline CVect4< NCBI_PROMOTE(T,type) > \
330 operator- (const CVect4<T>& v1, type v2) \
331 { \
332  return \
333  CVect4< NCBI_PROMOTE(T,type) > \
334  (v1[0] - v2, v1[1] - v2, v1[2] - v2, v1[3] - v2); \
335 }
336 
337 
341 
342 #undef NCBI_SUBTRACT_VECT4
343 
344 
345 
346 //
347 // global subtraction: const + CVect*<>
348 // we do this with macros because not all compilers support partial template
349 // specialization
350 //
351 
352 #define NCBI_MULTIPLY_VECT2(type) \
353 template <class U> \
354 inline CVect2< NCBI_PROMOTE(type,U) > \
355 operator* (type v1, const CVect2<U>& v2) \
356 { \
357  return \
358  CVect2< NCBI_PROMOTE(type,U) > \
359  (v1 * v2[0], v1 * v2[1]); \
360 } \
361 template <class T> \
362 inline CVect2< NCBI_PROMOTE(T,type) > \
363 operator* (const CVect2<T>& v1, type v2) \
364 { \
365  return \
366  CVect2< NCBI_PROMOTE(T,type) > \
367  (v1[0] * v2, v1[1] * v2); \
368 }
369 
370 
374 
375 #undef NCBI_MULTIPLY_VECT2
376 
377 
378 #define NCBI_MULTIPLY_VECT3(type) \
379 template <class U> \
380 inline CVect3< NCBI_PROMOTE(type,U) > \
381 operator* (type v1, const CVect3<U>& v2) \
382 { \
383  return \
384  CVect3< NCBI_PROMOTE(type,U) > \
385  (v1 * v2[0], v1 * v2[1], v1 * v2[2]); \
386 } \
387 template <class T> \
388 inline CVect3< NCBI_PROMOTE(T,type) > \
389 operator* (const CVect3<T>& v1, type v2) \
390 { \
391  return \
392  CVect3< NCBI_PROMOTE(T,type) > \
393  (v1[0] * v2, v1[1] * v2, v1[2] * v2); \
394 }
395 
396 
400 
401 #undef NCBI_MULTIPLY_VECT3
402 
403 
404 #define NCBI_MULTIPLY_VECT4(type) \
405 template <class U> \
406 inline CVect4< NCBI_PROMOTE(type,U) > \
407 operator* (type v1, const CVect4<U>& v2) \
408 { \
409  return \
410  CVect4< NCBI_PROMOTE(type,U) > \
411  (v1 * v2[0], v1 * v2[1], v1 * v2[2], v1 * v2[3]); \
412 } \
413 template <class T> \
414 inline CVect4< NCBI_PROMOTE(T,type) > \
415 operator* (const CVect4<T>& v1, type v2) \
416 { \
417  return \
418  CVect4< NCBI_PROMOTE(T,type) > \
419  (v1[0] * v2, v1[1] * v2, v1[2] * v2, v1[3] * v2); \
420 }
421 
422 
426 
427 #undef NCBI_MULTIPLY_VECT4
428 
429 
430 
431 
432 
433 //
434 // global multiplication: CVect*<> * CVect*<>
435 // we define this as dot!
436 //
437 template <class T, class U>
438 inline NCBI_PROMOTE(T,U)
439 operator* (const CVect2<T>& v1, const CVect2<U>& v2)
440 {
441  return (v1[0] * v2[0] +
442  v1[1] * v2[1]);
443 }
444 
445 
446 template <class T, class U>
447 inline NCBI_PROMOTE(T,U)
448 operator* (const CVect3<T>& v1, const CVect3<U>& v2)
449 {
450  return (v1[0] * v2[0] +
451  v1[1] * v2[1] +
452  v1[2] * v2[2]);
453 }
454 
455 
456 template <class T, class U>
457 inline NCBI_PROMOTE(T,U)
458 operator* (const CVect4<T>& v1, const CVect4<U>& v2)
459 {
460  return (v1[0] * v2[0] +
461  v1[1] * v2[1] +
462  v1[2] * v2[2] +
463  v1[3] * v2[3]);
464 }
465 
466 
467 
468 //
469 // global division: CVect*<> / const
470 //
471 template <class T, class U>
472 inline CVect2< NCBI_PROMOTE(T,U) >
474 {
475  v2 = T(1) / v2;
476  return
478  (v1[0]*v2, v1[1]*v2);
479 }
480 
481 
482 template <class T, class U>
483 inline CVect3< NCBI_PROMOTE(T,U) >
485 {
486  v2 = T(1) / v2;
487  return
489  (v1[0]*v2, v1[1]*v2, v1[2]*v2);
490 }
491 
492 
493 template <class T, class U>
494 inline CVect4< NCBI_PROMOTE(T,U) >
496 {
497  v2 = T(1) / v2;
498  return
500  (v1[0]*v2, v1[1]*v2, v1[2]*v2, v1[3]*v2);
501 }
502 
503 
504 //
505 // global comparison: equals
506 //
507 template <class T, class U>
508 inline bool
509 operator== (const CVect2<T>& v1, const CVect2<U>& v2)
510 {
511  return (v1[0] == v2[0] &&
512  v1[1] == v2[1]);
513 }
514 
515 
516 template <class T, class U>
517 inline bool
518 operator== (const CVect3<T>& v1, const CVect3<U>& v2)
519 {
520  return (v1[0] == v2[0] &&
521  v1[1] == v2[1] &&
522  v1[2] == v2[2]);
523 }
524 
525 
526 template <class T, class U>
527 inline bool
528 operator== (const CVect4<T>& v1, const CVect4<U>& v2)
529 {
530  return (v1[0] == v2[0] &&
531  v1[1] == v2[1] &&
532  v1[2] == v2[2] &&
533  v1[3] == v2[3]);
534 }
535 
536 
537 //
538 // global comparison: less than
539 //
540 template <class T, class U>
541 inline bool
542 operator< (const CVect2<T>& v1, const CVect2<U>& v2)
543 {
544  if (v1[0] < v2[0]) {
545  return true;
546  } else if (v1[0] > v2[0]) {
547  return false;
548  }
549 
550  if (v1[1] < v2[1]) {
551  return true;
552  }
553 
554  return false;
555 }
556 
557 
558 template <class T, class U>
559 inline bool
560 operator< (const CVect3<T>& v1, const CVect3<U>& v2)
561 {
562  if (v1[0] < v2[0]) {
563  return true;
564  } else if (v1[0] > v2[0]) {
565  return false;
566  }
567 
568  if (v1[1] < v2[1]) {
569  return true;
570  } else if (v1[1] > v2[1]) {
571  return false;
572  }
573 
574  if (v1[2] < v2[2]) {
575  return true;
576  }
577 
578  return false;
579 }
580 
581 
582 template <class T, class U>
583 inline bool
584 operator< (const CVect4<T>& v1, const CVect4<U>& v2)
585 {
586  if (v1[0] < v2[0]) {
587  return true;
588  } else if (v1[0] > v2[0]) {
589  return false;
590  }
591 
592  if (v1[1] < v2[1]) {
593  return true;
594  } else if (v1[1] > v2[1]) {
595  return false;
596  }
597 
598  if (v1[2] < v2[2]) {
599  return true;
600  } else if (v1[2] > v2[2]) {
601  return false;
602  }
603 
604  if (v1[3] < v2[3]) {
605  return true;
606  }
607 
608  return false;
609 }
610 
611 
612 //
613 //
614 //
615 // Matrices
616 //
617 //
618 //
619 
620 
621 //
622 // global addition: matrix + matrix
623 //
624 template <class T, class U>
625 inline CMatrix3< NCBI_PROMOTE(T,U) >
626 operator+ (const CMatrix3<T>& m1, const CMatrix3<U>& m2)
627 {
628  return
630  (m1[0]+m2[0], m1[1]+m2[1], m1[2]+m2[2],
631  m1[3]+m2[3], m1[4]+m2[4], m1[5]+m2[5],
632  m1[6]+m2[6], m1[7]+m2[7], m1[8]+m2[8]);
633 }
634 
635 
636 template <class T, class U>
637 inline CMatrix4< NCBI_PROMOTE(T,U) >
638 operator+ (const CMatrix4<T>& m1, const CMatrix4<U>& m2)
639 {
640  return
642  (m1[ 0]+m2[ 0], m1[ 1]+m2[ 1], m1[ 2]+m2[ 2], m1[ 3]+m2[ 3],
643  m1[ 4]+m2[ 4], m1[ 5]+m2[ 5], m1[ 6]+m2[ 6], m1[ 7]+m2[ 7],
644  m1[ 8]+m2[ 8], m1[ 9]+m2[ 9], m1[10]+m2[10], m1[11]+m2[11],
645  m1[12]+m2[12], m1[13]+m2[13], m1[14]+m2[14], m1[15]+m2[15]);
646 }
647 
648 
649 //
650 // global addition: scalar + matrix
651 //
652 #define NCBI_ADD_MATRIX3(type) \
653 template <class U> \
654 inline CMatrix3< NCBI_PROMOTE(type,U) > \
655 operator+ (type s, const CMatrix3<U>& m) \
656 { \
657  return \
658  CMatrix3< NCBI_PROMOTE(type,U) > \
659  (m[0]+s, m[1]+s, m[2]+s, \
660  m[3]+s, m[4]+s, m[5]+s, \
661  m[6]+s, m[7]+s, m[8]+s); \
662 } \
663 template <class T> \
664 inline CMatrix3< NCBI_PROMOTE(T,type) > \
665 operator+ (const CMatrix3<T>& m, type s) \
666 { \
667  return \
668  CMatrix3< NCBI_PROMOTE(T,type) > \
669  (m[0]+s, m[1]+s, m[2]+s, \
670  m[3]+s, m[4]+s, m[5]+s, \
671  m[6]+s, m[7]+s, m[8]+s); \
672 }
673 
674 
678 
679 #undef NCBI_ADD_MATRIX3
680 
681 
682 #define NCBI_ADD_MATRIX4(type) \
683 template <class U> \
684 inline CMatrix4< NCBI_PROMOTE(type,U) > \
685 operator+ (type s, const CMatrix4<U>& m) \
686 { \
687  return \
688  CMatrix4< NCBI_PROMOTE(type,U) > \
689  (m[ 0]+s, m[ 1]+s, m[ 2]+s, m[ 3]+s, \
690  m[ 4]+s, m[ 5]+s, m[ 6]+s, m[ 7]+s, \
691  m[ 8]+s, m[ 9]+s, m[10]+s, m[11]+s, \
692  m[12]+s, m[13]+s, m[14]+s, m[15]+s); \
693 } \
694 template <class T> \
695 inline CMatrix4< NCBI_PROMOTE(T,type) > \
696 operator+ (const CMatrix4<T>& m, type s) \
697 { \
698  return \
699  CMatrix4< NCBI_PROMOTE(T,type) > \
700  (m[ 0]+s, m[ 1]+s, m[ 2]+s, m[ 3]+s, \
701  m[ 4]+s, m[ 5]+s, m[ 6]+s, m[ 7]+s, \
702  m[ 8]+s, m[ 9]+s, m[10]+s, m[11]+s, \
703  m[12]+s, m[13]+s, m[14]+s, m[15]+s); \
704 }
705 
709 
710 #undef NCBI_ADD_MATRIX3
711 
712 
713 //
714 // global addition: scalar - matrix
715 //
716 #define NCBI_SUBTRACT_MATRIX3(type) \
717 template <class U> \
718 inline CMatrix3< NCBI_PROMOTE(type,U) > \
719 operator- (type s, const CMatrix3<U>& m) \
720 { \
721  return \
722  CMatrix3< NCBI_PROMOTE(type,U) > \
723  (m[0]-s, m[1]-s, m[2]-s, \
724  m[3]-s, m[4]-s, m[5]-s, \
725  m[6]-s, m[7]-s, m[8]-s); \
726 } \
727 template <class T> \
728 inline CMatrix3< NCBI_PROMOTE(T,type) > \
729 operator- (const CMatrix3<T>& m, type s) \
730 { \
731  return \
732  CMatrix3< NCBI_PROMOTE(T,type) > \
733  (m[0]-s, m[1]-s, m[2]-s, \
734  m[3]-s, m[4]-s, m[5]-s, \
735  m[6]-s, m[7]-s, m[8]-s); \
736 }
737 
738 
742 
743 #undef NCBI_SUBTRACT_MATRIX3
744 
745 
746 #define NCBI_SUBTRACT_MATRIX4(type) \
747 template <class U> \
748 inline CMatrix4< NCBI_PROMOTE(type,U) > \
749 operator- (type s, const CMatrix4<U>& m) \
750 { \
751  return \
752  CMatrix4< NCBI_PROMOTE(type,U) > \
753  (m[ 0]-s, m[ 1]-s, m[ 2]-s, m[ 3]-s, \
754  m[ 4]-s, m[ 5]-s, m[ 6]-s, m[ 7]-s, \
755  m[ 8]-s, m[ 9]-s, m[10]-s, m[11]-s, \
756  m[12]-s, m[13]-s, m[14]-s, m[15]-s); \
757 } \
758 template <class T> \
759 inline CMatrix4< NCBI_PROMOTE(T,type) > \
760 operator- (const CMatrix4<T>& m, type s) \
761 { \
762  return \
763  CMatrix4< NCBI_PROMOTE(T,type) > \
764  (m[ 0]-s, m[ 1]-s, m[ 2]-s, m[ 3]-s, \
765  m[ 4]-s, m[ 5]-s, m[ 6]-s, m[ 7]-s, \
766  m[ 8]-s, m[ 9]-s, m[10]-s, m[11]-s, \
767  m[12]-s, m[13]-s, m[14]-s, m[15]-s); \
768 }
769 
773 
774 #undef NCBI_SUBTRACT_MATRIX3
775 
776 
777 //
778 // global addition: scalar * matrix
779 //
780 #define NCBI_MULTIPLY_MATRIX3(type) \
781 template <class U> \
782 inline CMatrix3< NCBI_PROMOTE(type,U) > \
783 operator* (type s, const CMatrix3<U>& m) \
784 { \
785  return \
786  CMatrix3< NCBI_PROMOTE(type,U) > \
787  (m[0]*s, m[1]*s, m[2]*s, \
788  m[3]*s, m[4]*s, m[5]*s, \
789  m[6]*s, m[7]*s, m[8]*s); \
790 } \
791 template <class T> \
792 inline CMatrix3< NCBI_PROMOTE(T,type) > \
793 operator* (const CMatrix3<T>& m, type s) \
794 { \
795  return \
796  CMatrix3< NCBI_PROMOTE(T,type) > \
797  (m[0]*s, m[1]*s, m[2]*s, \
798  m[3]*s, m[4]*s, m[5]*s, \
799  m[6]*s, m[7]*s, m[8]*s); \
800 }
801 
802 
806 
807 #undef NCBI_MULTIPLY_MATRIX3
808 
809 
810 #define NCBI_MULTIPLY_MATRIX4(type) \
811 template <class U> \
812 inline CMatrix4< NCBI_PROMOTE(type,U) > \
813 operator* (type s, const CMatrix4<U>& m) \
814 { \
815  return \
816  CMatrix4< NCBI_PROMOTE(type,U) > \
817  (m[ 0]*s, m[ 1]*s, m[ 2]*s, m[ 3]*s, \
818  m[ 4]*s, m[ 5]*s, m[ 6]*s, m[ 7]*s, \
819  m[ 8]*s, m[ 9]*s, m[10]*s, m[11]*s, \
820  m[12]*s, m[13]*s, m[14]*s, m[15]*s); \
821 } \
822 template <class T> \
823 inline CMatrix4< NCBI_PROMOTE(T,type) > \
824 operator* (const CMatrix4<T>& m, type s) \
825 { \
826  return \
827  CMatrix4< NCBI_PROMOTE(T,type) > \
828  (m[ 0]*s, m[ 1]*s, m[ 2]*s, m[ 3]*s, \
829  m[ 4]*s, m[ 5]*s, m[ 6]*s, m[ 7]*s, \
830  m[ 8]*s, m[ 9]*s, m[10]*s, m[11]*s, \
831  m[12]*s, m[13]*s, m[14]*s, m[15]*s); \
832 }
833 
837 
838 #undef NCBI_MULTIPLY_MATRIX3
839 
840 
841 //
842 // global subtraction: matrix - matrix
843 //
844 template <class T, class U>
845 inline CMatrix3< NCBI_PROMOTE(T,U) >
846 operator- (const CMatrix3<T>& m1, const CMatrix3<U>& m2)
847 {
848  return
850  (m1[0]-m2[0], m1[1]-m2[1], m1[2]-m2[2],
851  m1[3]-m2[3], m1[4]-m2[4], m1[5]-m2[5],
852  m1[6]-m2[6], m1[7]-m2[7], m1[8]-m2[8]);
853 }
854 
855 
856 template <class T, class U>
857 inline CMatrix4< NCBI_PROMOTE(T,U) >
858 operator- (const CMatrix4<T>& m1, const CMatrix4<U>& m2)
859 {
860  return
862  (m1[ 0]-m2[ 0], m1[ 1]-m2[ 1], m1[ 2]-m2[ 2], m1[ 3]-m2[ 3],
863  m1[ 4]-m2[ 4], m1[ 5]-m2[ 5], m1[ 6]-m2[ 6], m1[ 7]-m2[ 7],
864  m1[ 8]-m2[ 8], m1[ 9]-m2[ 9], m1[10]-m2[10], m1[11]-m2[11],
865  m1[12]-m2[12], m1[13]-m2[13], m1[14]-m2[14], m1[15]-m2[15]);
866 }
867 
868 
869 //
870 // global multiplication: matrix * CVect*<>
871 // vector assumed to be a column vector!
872 //
873 template <class T, class U>
874 inline CVect3< NCBI_PROMOTE(T,U) >
875 operator* (const CMatrix3<T>& m, const CVect3<U>& v)
876 {
877  return
879  (m[0]*v[0] + m[1]*v[1] + m[2]*v[2],
880  m[3]*v[0] + m[4]*v[1] + m[5]*v[2],
881  m[6]*v[0] + m[7]*v[1] + m[8]*v[2] );
882 }
883 
884 
885 template <class T, class U>
886 inline CVect4< NCBI_PROMOTE(T,U) >
887 operator* (const CMatrix4<T>& m, const CVect4<U>& v)
888 {
889  return
891  (m[ 0]*v[0] + m[ 1]*v[1] + m[ 2]*v[2] + m[ 3]*v[3],
892  m[ 4]*v[0] + m[ 5]*v[1] + m[ 6]*v[2] + m[ 7]*v[3],
893  m[ 8]*v[0] + m[ 9]*v[1] + m[10]*v[2] + m[11]*v[3],
894  m[12]*v[0] + m[13]*v[1] + m[14]*v[2] + m[15]*v[3] );
895 }
896 
897 //
898 // global multiplication: CVect*<> * matrix
899 // vector assumed to be a row vector!
900 //
901 template <class T, class U>
902 inline CVect3< NCBI_PROMOTE(T,U) >
903 operator* (const CVect3<T>& v, const CMatrix3<U>& m)
904 {
905  return
907  (v[0]*m[0] + v[1]*m[3] + v[2]*m[6],
908  v[0]*m[1] + v[1]*m[4] + v[2]*m[7],
909  v[0]*m[2] + v[1]*m[5] + v[2]*m[8] );
910 }
911 
912 
913 template <class T, class U>
914 inline CVect4< NCBI_PROMOTE(T,U) >
915 operator* (const CVect4<T>& v, const CMatrix4<U>& m)
916 {
917  return
919  (v[0]*m[ 0] + v[1]*m[ 4] + v[2]*m[ 8] + m[12]*v[3],
920  v[0]*m[ 1] + v[1]*m[ 5] + v[2]*m[ 9] + m[13]*v[3],
921  v[0]*m[ 2] + v[1]*m[ 6] + v[2]*m[10] + m[14]*v[3],
922  v[0]*m[ 3] + v[1]*m[ 7] + v[2]*m[11] + m[15]*v[3] );
923 }
924 
925 
926 //
927 // global multiplication: matrix + matrix
928 //
929 template <class T, class U>
930 inline CMatrix3< NCBI_PROMOTE(T,U) >
931 operator* (const CMatrix3<T>& m1, const CMatrix3<U>& m2)
932 {
933  return
935  (m1[0]*m2[0] + m1[1]*m2[3] + m1[2]*m2[6],
936  m1[0]*m2[1] + m1[1]*m2[4] + m1[2]*m2[7],
937  m1[0]*m2[2] + m1[1]*m2[5] + m1[2]*m2[8],
938 
939  m1[3]*m2[0] + m1[4]*m2[3] + m1[5]*m2[6],
940  m1[3]*m2[1] + m1[4]*m2[4] + m1[5]*m2[7],
941  m1[3]*m2[2] + m1[4]*m2[5] + m1[5]*m2[8],
942 
943  m1[6]*m2[0] + m1[7]*m2[3] + m1[8]*m2[6],
944  m1[6]*m2[1] + m1[7]*m2[4] + m1[8]*m2[7],
945  m1[6]*m2[2] + m1[7]*m2[5] + m1[8]*m2[8]);
946 }
947 
948 
949 template <class T, class U>
950 inline CMatrix4< NCBI_PROMOTE(T,U) >
951 operator* (const CMatrix4<T>& m1, const CMatrix4<U>& m2)
952 {
953  return
955  (m1[ 0]*m2[ 0] + m1[ 1]*m2[ 4] + m1[ 2]*m2[ 8] + m1[ 3]*m2[12],
956  m1[ 0]*m2[ 1] + m1[ 1]*m2[ 5] + m1[ 2]*m2[ 9] + m1[ 3]*m2[13],
957  m1[ 0]*m2[ 2] + m1[ 1]*m2[ 6] + m1[ 2]*m2[10] + m1[ 3]*m2[14],
958  m1[ 0]*m2[ 3] + m1[ 1]*m2[ 7] + m1[ 2]*m2[11] + m1[ 3]*m2[15],
959 
960  m1[ 4]*m2[ 0] + m1[ 5]*m2[ 4] + m1[ 6]*m2[ 8] + m1[ 7]*m2[12],
961  m1[ 4]*m2[ 1] + m1[ 5]*m2[ 5] + m1[ 6]*m2[ 9] + m1[ 7]*m2[13],
962  m1[ 4]*m2[ 2] + m1[ 5]*m2[ 6] + m1[ 6]*m2[10] + m1[ 7]*m2[14],
963  m1[ 4]*m2[ 3] + m1[ 5]*m2[ 7] + m1[ 6]*m2[11] + m1[ 7]*m2[15],
964 
965  m1[ 8]*m2[ 0] + m1[ 9]*m2[ 4] + m1[10]*m2[ 8] + m1[11]*m2[12],
966  m1[ 8]*m2[ 1] + m1[ 9]*m2[ 5] + m1[10]*m2[ 9] + m1[11]*m2[13],
967  m1[ 8]*m2[ 2] + m1[ 9]*m2[ 6] + m1[10]*m2[10] + m1[11]*m2[14],
968  m1[ 8]*m2[ 3] + m1[ 9]*m2[ 7] + m1[10]*m2[11] + m1[11]*m2[15],
969 
970  m1[12]*m2[ 0] + m1[13]*m2[ 4] + m1[14]*m2[ 8] + m1[15]*m2[12],
971  m1[12]*m2[ 1] + m1[13]*m2[ 5] + m1[14]*m2[ 9] + m1[15]*m2[13],
972  m1[12]*m2[ 2] + m1[13]*m2[ 6] + m1[14]*m2[10] + m1[15]*m2[14],
973  m1[12]*m2[ 3] + m1[13]*m2[ 7] + m1[14]*m2[11] + m1[15]*m2[15]);
974 }
975 
976 //
977 // global division: matrix / scalar
978 //
979 template <class T, class U>
980 inline CMatrix3< NCBI_PROMOTE(T,U) >
981 operator/ (const CMatrix3<T>& m1, U s)
982 {
983  s = T(1) / s;
984  return
986  (m1[0]*s, m1[1]*s, m1[2]*s,
987  m1[3]*s, m1[4]*s, m1[5]*s,
988  m1[6]*s, m1[7]*s, m1[8]*s);
989 }
990 
991 
992 template <class T, class U>
993 inline CMatrix4< NCBI_PROMOTE(T,U) >
994 operator/ (const CMatrix4<T>& m1, U s)
995 {
996  s = T(1) / s;
997  return
999  (m1[ 0]*s, m1[ 1]*s, m1[ 2]*s, m1[ 3]*s,
1000  m1[ 4]*s, m1[ 5]*s, m1[ 6]*s, m1[ 7]*s,
1001  m1[ 8]*s, m1[ 9]*s, m1[10]*s, m1[11]*s,
1002  m1[12]*s, m1[13]*s, m1[14]*s, m1[15]*s);
1003 }
1004 
1005 
1006 
1008 
1009 /* @} */
1010 
1011 #endif // GUI_MATH___GLOBALS__HPP
Definition: vect2.hpp:48
Definition: vect3.hpp:48
Definition: vect4.hpp:49
Include a standard set of the NCBI C++ Toolkit most basic headers.
#define T(s)
Definition: common.h:230
CVect2< T > operator-(const CVect2< T > &v)
Definition: globals.hpp:207
bool operator==(const CVect2< T > &v1, const CVect2< U > &v2)
Definition: globals.hpp:509
#define NCBI_ADD_MATRIX3(type)
Definition: globals.hpp:652
#define NCBI_SUBTRACT_MATRIX3(type)
Definition: globals.hpp:716
#define NCBI_ADD_VECT2(type)
Definition: globals.hpp:124
#define NCBI_MULTIPLY_VECT2(type)
Definition: globals.hpp:352
#define NCBI_MULTIPLY_VECT4(type)
Definition: globals.hpp:404
#define NCBI_MULTIPLY_MATRIX3(type)
Definition: globals.hpp:780
#define NCBI_ADD_VECT3(type)
Definition: globals.hpp:150
bool operator<(const CVect2< T > &v1, const CVect2< U > &v2)
Definition: globals.hpp:542
#define NCBI_ADD_VECT4(type)
Definition: globals.hpp:176
#define NCBI_MULTIPLY_MATRIX4(type)
Definition: globals.hpp:810
CVect2< NCBI_PROMOTE(T, U) > operator+(const CVect2< T > &v1, const CVect2< U > &v2)
Definition: globals.hpp:90
#define NCBI_SUBTRACT_VECT4(type)
Definition: globals.hpp:319
CVect2< NCBI_PROMOTE(T, U) > operator/(const CVect2< T > &v1, U v2)
Definition: globals.hpp:473
const CVect2< U > & v2
Definition: globals.hpp:440
#define NCBI_SUBTRACT_VECT3(type)
Definition: globals.hpp:293
#define NCBI_SUBTRACT_MATRIX4(type)
Definition: globals.hpp:746
#define NCBI_ADD_MATRIX4(type)
Definition: globals.hpp:682
#define NCBI_SUBTRACT_VECT2(type)
Definition: globals.hpp:267
NCBI_PROMOTE(T, U) operator*(const CVect2< T > &v1
#define NCBI_MULTIPLY_VECT3(type)
Definition: globals.hpp:378
CVect2< NCBI_PROMOTE(int,U) > operator*(int v1, const CVect2< U > &v2)
Definition: globals.hpp:371
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
#define U
#define const
Definition: zconf.h:232
Modified on Fri Sep 20 14:57:59 2024 by modify_doxy.py rev. 669887