OperationArrayFunctor 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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_OPERATIONARRAYFUNCTOR
  14. #define OSGUTIL_OPERATIONARRAYFUNCTOR 1
  15. #include <osg/Array>
  16. #include <osgUtil/ConvertVec>
  17. // ** template ArrayVisitor to handle all method in one template.
  18. // ** Only use when process done on each array could be templated
  19. namespace osgUtil {
  20. template <class T>
  21. class OperationArrayFunctor : public osg::ArrayVisitor, public T
  22. {
  23. public:
  24. virtual void apply(osg::Array&) {}
  25. // virtual void apply(osg::ByteArray& array) { T::process<osg::ByteArray>(array); }
  26. // virtual void apply(osg::ShortArray& array) { T::process(array); }
  27. // virtual void apply(osg::IntArray& array) { T::process<osg::IntArray>(array); }
  28. // virtual void apply(osg::UByteArray& array) { T::process<osg::UByteArray>(array); }
  29. // virtual void apply(osg::UShortArray& array) { T::process<osg::UShortArray>(array); }
  30. // virtual void apply(osg::UIntArray& array) { T::process<osg::UIntArray>(array); }
  31. // virtual void apply(osg::FloatArray& array) { T::process<osg::FloatArray>(array); }
  32. // virtual void apply(osg::DoubleArray& array) { T::process<osg::DoubleArray>(array); }
  33. virtual void apply(osg::Vec2Array & array) { T::process(array); }
  34. virtual void apply(osg::Vec3Array& array) { T::process(array); }
  35. virtual void apply(osg::Vec4Array& array) { T::process(array); }
  36. virtual void apply(osg::Vec4ubArray& array) { T::process(array); }
  37. virtual void apply(osg::Vec2bArray& array) { T::process(array); }
  38. virtual void apply(osg::Vec3bArray& array) { T::process(array); }
  39. virtual void apply(osg::Vec4bArray& array) { T::process(array); }
  40. virtual void apply(osg::Vec2sArray& array) { T::process(array); }
  41. virtual void apply(osg::Vec3sArray& array) { T::process(array); }
  42. virtual void apply(osg::Vec4sArray& array) { T::process(array); }
  43. virtual void apply(osg::Vec2dArray& array) { T::process(array); }
  44. virtual void apply(osg::Vec3dArray& array) { T::process(array); }
  45. virtual void apply(osg::Vec4dArray& array) { T::process(array); }
  46. };
  47. struct AddRangeOperator
  48. {
  49. template <typename ArrayType>
  50. void process(ArrayType & array)
  51. {
  52. typedef typename ArrayType::ElementDataType ElementDataType;
  53. ElementDataType convertedVector;
  54. osgUtil::ConvertVec<osg::Vec3d, ElementDataType>::convert(_vector, convertedVector);
  55. typename ArrayType::iterator it = array.begin();
  56. std::advance(it, _begin);
  57. typename ArrayType::iterator end = it;
  58. std::advance(end, _count);
  59. for (; it < end; ++it)
  60. (*it) += convertedVector;
  61. }
  62. unsigned int _begin;
  63. unsigned int _count;
  64. osg::Vec3d _vector;
  65. };
  66. typedef OperationArrayFunctor<AddRangeOperator> AddRangeFunctor;
  67. struct MultiplyRangeOperator
  68. {
  69. template <typename ArrayType>
  70. void process(ArrayType & array)
  71. {
  72. typedef typename ArrayType::ElementDataType ElementDataType;
  73. ElementDataType convertedVector;
  74. osgUtil::ConvertVec<osg::Vec3d, ElementDataType>::convert(_vector, convertedVector);
  75. typename ArrayType::iterator it = array.begin();
  76. std::advance(it, _begin);
  77. typename ArrayType::iterator end = it;
  78. std::advance(end, _count);
  79. for (; it < end; ++it)
  80. (*it) *= convertedVector;
  81. }
  82. unsigned int _begin;
  83. unsigned int _count;
  84. osg::Vec3d _vector;
  85. };
  86. typedef OperationArrayFunctor<MultiplyRangeOperator> MultiplyRangeFunctor;
  87. } // end osgUtil namespace
  88. #endif // ** OPERATIONARRAYFUNCTOR ** //