boolInVec.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. Copyright (C) 2009 Sony Computer Entertainment Inc.
  3. All rights reserved.
  4. This software is provided 'as-is', without any express or implied warranty.
  5. In no event will the authors be held liable for any damages arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it freely,
  8. subject to the following restrictions:
  9. 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
  10. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  11. 3. This notice may not be removed or altered from any source distribution.
  12. */
  13. #ifndef _BOOLINVEC_H
  14. #define _BOOLINVEC_H
  15. #include <math.h>
  16. namespace Vectormath {
  17. class floatInVec;
  18. //--------------------------------------------------------------------------------------------------
  19. // boolInVec class
  20. //
  21. class boolInVec
  22. {
  23. private:
  24. unsigned int mData;
  25. public:
  26. // Default constructor; does no initialization
  27. //
  28. inline boolInVec( ) { };
  29. // Construct from a value converted from float
  30. //
  31. inline boolInVec(floatInVec vec);
  32. // Explicit cast from bool
  33. //
  34. explicit inline boolInVec(bool scalar);
  35. // Explicit cast to bool
  36. //
  37. inline bool getAsBool() const;
  38. #ifndef _VECTORMATH_NO_SCALAR_CAST
  39. // Implicit cast to bool
  40. //
  41. inline operator bool() const;
  42. #endif
  43. // Boolean negation operator
  44. //
  45. inline const boolInVec operator ! () const;
  46. // Assignment operator
  47. //
  48. inline boolInVec& operator = (boolInVec vec);
  49. // Boolean and assignment operator
  50. //
  51. inline boolInVec& operator &= (boolInVec vec);
  52. // Boolean exclusive or assignment operator
  53. //
  54. inline boolInVec& operator ^= (boolInVec vec);
  55. // Boolean or assignment operator
  56. //
  57. inline boolInVec& operator |= (boolInVec vec);
  58. };
  59. // Equal operator
  60. //
  61. inline const boolInVec operator == (boolInVec vec0, boolInVec vec1);
  62. // Not equal operator
  63. //
  64. inline const boolInVec operator != (boolInVec vec0, boolInVec vec1);
  65. // And operator
  66. //
  67. inline const boolInVec operator & (boolInVec vec0, boolInVec vec1);
  68. // Exclusive or operator
  69. //
  70. inline const boolInVec operator ^ (boolInVec vec0, boolInVec vec1);
  71. // Or operator
  72. //
  73. inline const boolInVec operator | (boolInVec vec0, boolInVec vec1);
  74. // Conditionally select between two values
  75. //
  76. inline const boolInVec select(boolInVec vec0, boolInVec vec1, boolInVec select_vec1);
  77. } // namespace Vectormath
  78. //--------------------------------------------------------------------------------------------------
  79. // boolInVec implementation
  80. //
  81. #include "floatInVec.h"
  82. namespace Vectormath {
  83. inline
  84. boolInVec::boolInVec(floatInVec vec)
  85. {
  86. *this = (vec != floatInVec(0.0f));
  87. }
  88. inline
  89. boolInVec::boolInVec(bool scalar)
  90. {
  91. mData = -(int)scalar;
  92. }
  93. inline
  94. bool
  95. boolInVec::getAsBool() const
  96. {
  97. return (mData > 0);
  98. }
  99. #ifndef _VECTORMATH_NO_SCALAR_CAST
  100. inline
  101. boolInVec::operator bool() const
  102. {
  103. return getAsBool();
  104. }
  105. #endif
  106. inline
  107. const boolInVec
  108. boolInVec::operator ! () const
  109. {
  110. return boolInVec(!mData);
  111. }
  112. inline
  113. boolInVec&
  114. boolInVec::operator = (boolInVec vec)
  115. {
  116. mData = vec.mData;
  117. return *this;
  118. }
  119. inline
  120. boolInVec&
  121. boolInVec::operator &= (boolInVec vec)
  122. {
  123. *this = *this & vec;
  124. return *this;
  125. }
  126. inline
  127. boolInVec&
  128. boolInVec::operator ^= (boolInVec vec)
  129. {
  130. *this = *this ^ vec;
  131. return *this;
  132. }
  133. inline
  134. boolInVec&
  135. boolInVec::operator |= (boolInVec vec)
  136. {
  137. *this = *this | vec;
  138. return *this;
  139. }
  140. inline
  141. const boolInVec
  142. operator == (boolInVec vec0, boolInVec vec1)
  143. {
  144. return boolInVec(vec0.getAsBool() == vec1.getAsBool());
  145. }
  146. inline
  147. const boolInVec
  148. operator != (boolInVec vec0, boolInVec vec1)
  149. {
  150. return !(vec0 == vec1);
  151. }
  152. inline
  153. const boolInVec
  154. operator & (boolInVec vec0, boolInVec vec1)
  155. {
  156. return boolInVec(vec0.getAsBool() & vec1.getAsBool());
  157. }
  158. inline
  159. const boolInVec
  160. operator | (boolInVec vec0, boolInVec vec1)
  161. {
  162. return boolInVec(vec0.getAsBool() | vec1.getAsBool());
  163. }
  164. inline
  165. const boolInVec
  166. operator ^ (boolInVec vec0, boolInVec vec1)
  167. {
  168. return boolInVec(vec0.getAsBool() ^ vec1.getAsBool());
  169. }
  170. inline
  171. const boolInVec
  172. select(boolInVec vec0, boolInVec vec1, boolInVec select_vec1)
  173. {
  174. return (select_vec1.getAsBool() == 0) ? vec0 : vec1;
  175. }
  176. } // namespace Vectormath
  177. #endif // boolInVec_h