ConvertVec 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
  2. *
  3. * This library is open source and may be redistributed and/or modified under
  4. * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
  5. * (at your option) any later version. The full license is in LICENSE file
  6. * included with this distribution, and on the openscenegraph.org website.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * OpenSceneGraph Public License for more details.
  12. */
  13. #ifndef OSGUTIL_CONVERTVEC
  14. #define OSGUTIL_CONVERTVEC 1
  15. namespace osgUtil {
  16. template <typename InType, typename OutType,
  17. unsigned int InSize = InType::num_components,
  18. unsigned int OutSize = OutType::num_components>
  19. struct ConvertVec
  20. {
  21. static void convert(InType & in, OutType & out)
  22. {}
  23. };
  24. template <typename InType, typename OutType>
  25. struct ConvertVec<InType, OutType, 2, 2>
  26. {
  27. static void convert(InType & in, OutType & out)
  28. {
  29. out.set(static_cast<typename OutType::value_type>(in.x()),
  30. static_cast<typename OutType::value_type>(in.y()));
  31. }
  32. };
  33. template <typename InType, typename OutType>
  34. struct ConvertVec<InType, OutType, 2, 3>
  35. {
  36. static void convert(InType & in, OutType & out)
  37. {
  38. out.set(static_cast<typename OutType::value_type>(in.x()),
  39. static_cast<typename OutType::value_type>(in.y()),
  40. static_cast<typename OutType::value_type>(0.0));
  41. }
  42. };
  43. template <typename InType, typename OutType>
  44. struct ConvertVec<InType, OutType, 2, 4>
  45. {
  46. static void convert(InType & in, OutType & out)
  47. {
  48. out.set(static_cast<typename OutType::value_type>(in.x()),
  49. static_cast<typename OutType::value_type>(in.y()),
  50. static_cast<typename OutType::value_type>(0.0),
  51. static_cast<typename OutType::value_type>(1.0));
  52. }
  53. };
  54. template <typename InType, typename OutType>
  55. struct ConvertVec<InType, OutType, 3, 2>
  56. {
  57. static void convert(InType & in, OutType & out)
  58. {
  59. out.set(static_cast<typename OutType::value_type>(in.x()),
  60. static_cast<typename OutType::value_type>(in.y()));
  61. }
  62. };
  63. template <typename InType, typename OutType>
  64. struct ConvertVec<InType, OutType, 3, 3>
  65. {
  66. static void convert(InType & in, OutType & out)
  67. {
  68. out.set(static_cast<typename OutType::value_type>(in.x()),
  69. static_cast<typename OutType::value_type>(in.y()),
  70. static_cast<typename OutType::value_type>(in.z()));
  71. }
  72. };
  73. template <typename InType, typename OutType>
  74. struct ConvertVec<InType, OutType, 3, 4>
  75. {
  76. static void convert(InType & in, OutType & out)
  77. {
  78. out.set(static_cast<typename OutType::value_type>(in.x()),
  79. static_cast<typename OutType::value_type>(in.y()),
  80. static_cast<typename OutType::value_type>(in.z()),
  81. static_cast<typename OutType::value_type>(1.0));
  82. }
  83. };
  84. template <typename InType, typename OutType>
  85. struct ConvertVec<InType, OutType, 4, 2>
  86. {
  87. static void convert(InType & in, OutType & out)
  88. {
  89. out.set(static_cast<typename OutType::value_type>(in.x()/in.w()),
  90. static_cast<typename OutType::value_type>(in.y()/in.w()));
  91. }
  92. };
  93. template <typename InType, typename OutType>
  94. struct ConvertVec<InType, OutType, 4, 3>
  95. {
  96. static void convert(InType & in, OutType & out)
  97. {
  98. out.set(static_cast<typename OutType::value_type>(in.x()/in.w()),
  99. static_cast<typename OutType::value_type>(in.y()/in.w()),
  100. static_cast<typename OutType::value_type>(in.z()/in.w()));
  101. }
  102. };
  103. template <typename InType, typename OutType>
  104. struct ConvertVec<InType, OutType, 4, 4>
  105. {
  106. static void convert(InType & in, OutType & out)
  107. {
  108. out.set(static_cast<typename OutType::value_type>(in.x()),
  109. static_cast<typename OutType::value_type>(in.y()),
  110. static_cast<typename OutType::value_type>(in.z()),
  111. static_cast<typename OutType::value_type>(in.w()));
  112. }
  113. };
  114. } // end of osg namespace
  115. #endif // ** OSG_CONVERTVEC ** //