Fermat
functors.h
Go to the documentation of this file.
1 /*
2  * CUGAR : Cuda Graphics Accelerator
3  *
4  * Copyright (c) 2010-2018, NVIDIA Corporation
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * * Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * * Neither the name of NVIDIA Corporation nor the
15  * names of its contributors may be used to endorse or promote products
16  * derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21  * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
22  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
34 #pragma once
35 
36 #include <cugar/basic/types.h>
37 #include <iterator>
38 
39 namespace cugar {
40 
43 
47 
51 
55 template <typename T>
57 {
59 
60  CUGAR_HOST_DEVICE bool operator() (const T t) const { return t ? true : false; }
61 };
62 
66 template <typename T, typename R>
68 {
70  typedef T argument_type;
71  typedef R result_type;
72 
76  CUGAR_HOST_DEVICE
77  constant_functor(R c) : constant(c) {}
78 
79  CUGAR_HOST_DEVICE R operator() (const T op) const { return constant; }
80 
81  R constant;
82 };
86 template <typename T, typename R>
87 struct one_fun
88 {
90  typedef T argument_type;
91  typedef R result_type;
92 
93  CUGAR_HOST_DEVICE R operator() (const T op) const { return R(1); }
94 };
100 {
102  typedef uint32 argument_type;
103  typedef uint32 result_type;
104 
105  CUGAR_HOST_DEVICE uint32 operator() (const uint32 op) const
106  {
107  return op ? 1u : 0u;
108  }
109 };
113 template <typename T>
115 {
117  typedef T argument_type;
118  typedef T result_type;
119 
120  CUGAR_HOST_DEVICE T operator() (const T op) const { return !op; }
121 };
125 template <typename T>
126 struct minus_one
127 {
129  typedef T argument_type;
130  typedef T result_type;
131 
132  CUGAR_HOST_DEVICE T operator() (const T op) const { return op - T(1); }
133 };
137 template <typename F, typename C>
138 struct binder1st
139 {
141  typedef typename F::first_argument_type argument_type;
142  typedef typename F::result_type result_type;
143 
144  CUGAR_HOST_DEVICE
145  binder1st(const F& f, const C c) : functor(f), first(c) {}
146 
147  CUGAR_HOST_DEVICE uint32 operator() (const uint32 op) const { return functor( first, op ); }
148 
149  F functor;
150  C first;
151 };
155 template <typename F, typename C>
156 struct binder2nd
157 {
159  typedef typename F::second_argument_type argument_type;
160  typedef typename F::result_type result_type;
161 
162  CUGAR_HOST_DEVICE
163  binder2nd(const F& f, const C c) : functor(f), second(c) {}
164 
165  CUGAR_HOST_DEVICE uint32 operator() (const uint32 op) const { return functor( op, second ); }
166 
167  F functor;
168  C second;
169 };
170 template <typename F, typename C> binder1st<F,C> bind1st(const F& f, const C c) { return binder1st<F,C>( f, c ); }
171 template <typename F, typename C> binder2nd<F,C> bind2nd(const F& f, const C c) { return binder2nd<F,C>( f, c ); }
172 
176 template <typename Vector_type>
178 {
180  typedef Vector_type first_argument_type;
181  typedef uint32 second_argument_type;
182  typedef typename Vector_type::value_type result_type;
183 
184  CUGAR_HOST_DEVICE result_type operator() (const first_argument_type v, const second_argument_type i) const { return v[i]; }
185 };
189 template <typename T>
191 {
193  typedef T argument_type;
194  typedef T result_type;
195 
196  CUGAR_HOST_DEVICE T operator() (const T& v) const { return v*v; }
197 };
201 template <typename T>
203 {
205  typedef T argument_type;
206  typedef bool result_type;
207 
208  CUGAR_HOST_DEVICE bool operator() (const T& v) const { return v > 0; }
209 };
213 template <typename T>
215 {
217  typedef T first_argument_type;
218  typedef T second_argument_type;
219  typedef bool result_type;
220 
221  CUGAR_HOST_DEVICE bool operator() (const T& op1, const T& op2) const { return op1 == op2; }
222 };
226 template <typename T>
228 {
230  typedef T first_argument_type;
231  typedef T second_argument_type;
232  typedef bool result_type;
233 
234  CUGAR_HOST_DEVICE bool operator() (const T& op1, const T& op2) const { return op1 != op2; }
235 };
239 template <typename T>
241 {
243  typedef T argument_type;
244  typedef bool result_type;
245 
249  CUGAR_HOST_DEVICE
250  eq_constant(const T c) : m_c(c) {}
251 
252  CUGAR_HOST_DEVICE bool operator() (const T& v) const { return v == m_c; }
253 
254 private:
255  const T m_c;
256 };
260 template <typename T>
262 {
264  typedef T argument_type;
265  typedef bool result_type;
266 
270  CUGAR_HOST_DEVICE
271  neq_constant(const T c) : m_c(c) {}
272 
273  CUGAR_HOST_DEVICE bool operator() (const T& v) const { return v != m_c; }
274 
275 private:
276  const T m_c;
277 };
278 
282 template <typename T, typename R>
284 {
286  typedef T argument_type;
287  typedef R result_type;
288 
293  CUGAR_HOST_DEVICE
294  if_true_functor(const R r0, const R r1) : m_r_true(r0), m_r_false(r1) {}
295 
296  CUGAR_HOST_DEVICE R operator() (const T& v) const { return v ? m_r_true : m_r_false; }
297 
298 private:
299  const R m_r_true;
300  const R m_r_false;
301 };
302 
306 template <typename T, typename R>
308 {
310  typedef T argument_type;
311  typedef R result_type;
312 
318  CUGAR_HOST_DEVICE
319  if_constant(const T c, const R r0, const R r1) : m_c(c), m_r_true(r0), m_r_false(r1) {}
320 
321  CUGAR_HOST_DEVICE R operator() (const T& v) const { return v == m_c ? m_r_true : m_r_false; }
322 
323 private:
324  const T m_c;
325  const R m_r_true;
326  const R m_r_false;
327 };
331 template <typename F1, typename F2>
333 {
335  typedef typename F2::first_argument_type argument_type;
336  typedef typename F1::result_type result_type;
337 
338  CUGAR_HOST_DEVICE
339  compose_unary(const F1 f1, const F2 f2) : m_fun1(f1), m_fun2(f2) {}
340 
341  CUGAR_HOST_DEVICE result_type operator() (const argument_type& op) const { return m_fun1( m_fun2( op ) ); }
342 
343 private:
344  const F1 m_fun1;
345  const F2 m_fun2;
346 };
350 template <typename F, typename G1, typename G2>
352 {
354  typedef typename G1::argument_type first_argument_type;
355  typedef typename G2::argument_type second_argument_type;
356  typedef typename F::result_type result_type;
357 
358  CUGAR_HOST_DEVICE
359  compose_binary(const F f, const G1 g1, const G2 g2) : m_f(f), m_g1(g1), m_g2(g2) {}
360 
361  CUGAR_HOST_DEVICE result_type operator() (
362  const first_argument_type& op1,
363  const second_argument_type& op2) const { return m_f( m_g1( op1 ), m_g2( op2 ) ); }
364 
365 private:
366  const F m_f;
367  const G1 m_g1;
368  const G2 m_g2;
369 };
373 template <typename F1, typename F2>
375 {
377  typedef typename F2::first_argument_type first_argument_type;
378  typedef typename F2::second_argument_type second_argument_type;
379  typedef typename F1::result_type result_type;
380 
381  CUGAR_HOST_DEVICE
382  compose_unary_after_binary(const F1 f1, const F2 f2) : m_fun1(f1), m_fun2(f2) {}
383 
384  CUGAR_HOST_DEVICE result_type operator() (
385  const first_argument_type& op1,
386  const second_argument_type& op2) const { return m_fun1( m_fun2( op1, op2 ) ); }
387 
388 private:
389  const F1 m_fun1;
390  const F2 m_fun2;
391 };
392 
393 template <typename F1, typename F2, typename T1, typename T2>
395 template <typename F1, typename F2>
397 template <typename F1, typename F2>
399 
402 template <typename F1, typename F2>
404 {
406 }
409 template <typename F, typename G1, typename G2>
410 compose_binary<F,G1,G2> compose(const F f, const G1 g1, const G2 g2)
411 {
412  return compose_binary<F,G1,G2>( f, g1, g2 );
413 }
414 
418 template <typename T>
420 {
421  typedef T first_argument_type;
422  typedef T second_argument_type;
423  typedef T result_type;
424 
425  CUGAR_HOST_DEVICE T operator() (const T a, const T b) const { return a < b ? a : b; }
426 };
430 template <typename T>
432 {
433  typedef T first_argument_type;
434  typedef T second_argument_type;
435  typedef T result_type;
436 
437  CUGAR_HOST_DEVICE T operator() (const T a, const T b) const { return a > b ? a : b; }
438 };
439 
443 template <typename T>
444 struct add
445 {
446  typedef T first_argument_type;
447  typedef T second_argument_type;
448  typedef T result_type;
449 
450  CUGAR_HOST_DEVICE T operator() (const T op1, const T op2) const { return op1 + op2; }
451 };
455 template <typename T>
456 struct binary_or
457 {
458  typedef T first_argument_type;
459  typedef T second_argument_type;
460  typedef T result_type;
461 
462  CUGAR_HOST_DEVICE T operator() (const T op1, const T op2) const { return op1 | op2; }
463 };
467 template <typename T>
469 {
470  typedef T first_argument_type;
471  typedef T second_argument_type;
472  typedef T result_type;
473 
474  CUGAR_HOST_DEVICE T operator() (const T op1, const T op2) const { return op1 & op2; }
475 };
476 
480 template <typename T>
481 struct mask_and
482 {
483  typedef T argument_type;
484  typedef T result_type;
485 
489  CUGAR_HOST_DEVICE mask_and(const T mask) : m_mask( mask ) {}
490 
491  CUGAR_HOST_DEVICE T operator() (const T op) const { return op & m_mask; }
492 
493 private:
494  const T m_mask;
495 };
496 
500 template <typename T>
501 struct mask_or
502 {
503  typedef T argument_type;
504  typedef T result_type;
505 
509  CUGAR_HOST_DEVICE mask_or(const T mask) : m_mask( mask ) {}
510 
511  CUGAR_HOST_DEVICE T operator() (const T op) const { return op | m_mask; }
512 
513 private:
514  const T m_mask;
515 };
516 
520 template <typename T>
522 {
523  typedef T argument_type;
524  typedef T result_type;
525 
529  CUGAR_HOST_DEVICE l_bit_shift(const T bits) : m_bits( bits ) {}
530 
531  CUGAR_HOST_DEVICE T operator() (const T x) const { return x << m_bits; }
532 
533 private:
534  const T m_bits;
535 };
536 
540 template <typename T>
542 {
543  typedef T argument_type;
544  typedef T result_type;
545 
549  CUGAR_HOST_DEVICE r_bit_shift(const T bits) : m_bits( bits ) {}
550 
551  CUGAR_HOST_DEVICE T operator() (const T x) const { return x >> m_bits; }
552 
553 private:
554  const T m_bits;
555 };
556 
561 template <typename Vector_type>
563 {
564  typedef Vector_type argument_type;
565  typedef float result_type;
566 
570  CUGAR_HOST_DEVICE clamped_cosine_functor(const Vector_type& normal) : m_normal( normal ) {}
571 
572  CUGAR_HOST_DEVICE float operator() (const Vector_type& dir) const { return max( dot( dir, m_normal ), 0.0f ); }
573 
574  const Vector_type m_normal;
575 };
576 
581 template <typename Vector_type>
583 {
584  typedef Vector_type argument_type;
585  typedef float result_type;
586 
590  CUGAR_HOST_DEVICE abs_cosine_functor(const Vector_type& normal) : m_normal( normal ) {}
591 
592  CUGAR_HOST_DEVICE float operator() (const Vector_type& dir) const { return fabsf( dot( dir, m_normal ) ); }
593 
594  const Vector_type m_normal;
595 };
596 
600 template <typename T>
601 struct less
602 {
604  typedef T first_argument_type;
605  typedef T second_argument_type;
606  typedef bool result_type;
607 
608  CUGAR_HOST_DEVICE bool operator() (const T& op1, const T& op2) const { return op1 < op2; }
609 };
613 template <typename T>
614 struct greater
615 {
617  typedef T first_argument_type;
618  typedef T second_argument_type;
619  typedef bool result_type;
620 
621  CUGAR_HOST_DEVICE bool operator() (const T& op1, const T& op2) const { return op1 > op2; }
622 };
623 
624 
627 template <typename word_type>
629 {
630  typedef word_type argument_type;
631  typedef word_type result_type;
632 
633  static const uint32 BITS = 8u * sizeof(word_type);
634 
637  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
638  leading_bits(const uint32 n) : n_bits(n) {}
639 
642  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
643  result_type operator() (const argument_type op) const { return op >> (BITS - n_bits); }
644 
645  const uint32 n_bits;
646 };
647 
650 template <typename word_type>
652 {
653  typedef word_type argument_type;
654  typedef word_type result_type;
655 
658  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
659  shift_left(const uint32 _shift) : shift(_shift) {}
660 
663  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
664  result_type operator() (const argument_type i) const
665  {
666  // shift i by d bits
667  return result_type(i) << shift;
668  }
669 
670  const uint32 shift;
671 };
672 
675 template <typename word_type>
677 {
678  typedef word_type argument_type;
679  typedef word_type result_type;
680 
683  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
684  shift_right(const uint32 _shift) : shift(_shift) {}
685 
688  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
689  result_type operator() (const argument_type i) const
690  {
691  // shift i by d bits
692  return result_type(i) >> shift;
693  }
694 
695  const uint32 shift;
696 };
697 
700 template <typename T, typename U>
701 struct hi_bits_functor {};
702 
705 template <>
706 struct hi_bits_functor<uint8, uint32>
707 {
708  typedef uint32 argument_type;
709  typedef uint8 result_type;
710 
711  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
712  result_type operator() (const argument_type op) const { return result_type(op >> 24u); }
713 };
714 
717 template <>
718 struct hi_bits_functor<uint16, uint32>
719 {
720  typedef uint32 argument_type;
721  typedef uint16 result_type;
722 
723  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
724  result_type operator() (const argument_type op) const { return result_type(op >> 16u); }
725 };
726 
729 template <>
730 struct hi_bits_functor<uint32, uint32>
731 {
732  typedef uint32 argument_type;
733  typedef uint32 result_type;
734 
735  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
736  result_type operator() (const argument_type op) const { return result_type(op); }
737 };
738 
741 template <>
742 struct hi_bits_functor<uint32, uint64>
743 {
744  typedef uint64 argument_type;
745  typedef uint32 result_type;
746 
747  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
748  result_type operator() (const argument_type op) const { return result_type(op >> 32); }
749 };
750 
753 template <typename T>
755 {
756  typedef T argument_type;
757  typedef bool result_type;
758 
759  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
760  result_type operator() (const T op) const { return op ? true : false; }
761 };
762 
765 template <typename T>
767 {
768  typedef T argument_type;
769  typedef bool result_type;
770 
771  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
772  result_type operator() (const T op) const { return op ? false : true; }
773 };
774 
777 template <typename Iterator, typename index_type = uint32>
779 {
780  typedef index_type argument_type;
781  typedef typename std::iterator_traits<Iterator>::value_type result_type;
782 
783  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
784  gather_functor(const Iterator perm) : m_perm(perm) {}
785 
786  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
787  result_type operator() (const argument_type op) const { return m_perm[op]; }
788 
789  Iterator m_perm;
790 };
791 template <typename Iterator>
792 gather_functor<Iterator> make_gather_functor(const Iterator perm)
793 {
794  return gather_functor<Iterator>(perm);
795 }
796 
797 
800 
801 } // namespace cugar
Definition: functors.h:48
Definition: functors.h:126
Definition: functors.h:261
Definition: functors.h:332
Definition: functors.h:521
Definition: functors.h:766
Definition: functors.h:651
Definition: functors.h:614
Definition: functors.h:456
CUGAR_HOST_DEVICE eq_constant(const T c)
Definition: functors.h:250
Definition: functors.h:501
Definition: functors.h:240
CUGAR_HOST_DEVICE clamped_cosine_functor(const Vector_type &normal)
Definition: functors.h:570
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE shift_right(const uint32 _shift)
Definition: functors.h:684
CUGAR_HOST_DEVICE if_true_functor(const R r0, const R r1)
Definition: functors.h:294
Definition: functors.h:541
Definition: functors.h:114
Definition: functors.h:419
Definition: functors.h:49
Definition: functors.h:701
Definition: functors.h:374
Definition: functors.h:99
Definition: functors.h:283
Definition: functors.h:444
CUGAR_HOST_DEVICE r_bit_shift(const T bits)
Definition: functors.h:549
Definition: functors.h:351
Definition: functors.h:468
Definition: functors.h:676
CUGAR_HOST_DEVICE mask_and(const T mask)
Definition: functors.h:489
CUGAR_HOST_DEVICE constant_functor(R c)
Definition: functors.h:77
CUGAR_HOST_DEVICE l_bit_shift(const T bits)
Definition: functors.h:529
Definition: functors.h:87
Definition: functors.h:394
Definition: functors.h:628
Definition: functors.h:227
Definition: functors.h:481
Definition: functors.h:177
Definition: functors.h:56
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE leading_bits(const uint32 n)
Definition: functors.h:638
CUGAR_HOST_DEVICE if_constant(const T c, const R r0, const R r1)
Definition: functors.h:319
CUGAR_HOST_DEVICE mask_or(const T mask)
Definition: functors.h:509
Define a vector_view POD type and plain_view() for std::vector.
Definition: diff.h:38
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE shift_left(const uint32 _shift)
Definition: functors.h:659
Definition: functors.h:431
Definition: functors.h:202
Definition: functors.h:67
Definition: functors.h:562
Definition: functors.h:778
Definition: functors.h:156
composition_type< F1, F2, typename F1::function_tag, typename F2::function_tag >::type compose(const F1 f1, const F2 f2)
Definition: functors.h:403
CUGAR_HOST_DEVICE abs_cosine_functor(const Vector_type &normal)
Definition: functors.h:590
Definition: functors.h:307
CUGAR_HOST_DEVICE neq_constant(const T c)
Definition: functors.h:271
Definition: functors.h:138
Definition: functors.h:50
Definition: functors.h:190
Definition: functors.h:601
Definition: functors.h:582
Definition: functors.h:214
Definition: functors.h:754