ctkTransferFunction.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.commontk.org/LICENSE
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. #ifndef __ctkTransferFunction_h
  15. #define __ctkTransferFunction_h
  16. /// Qt includes
  17. #include <QObject>
  18. #include <QtGlobal>
  19. #include <QSharedPointer>
  20. #include <QVariant>
  21. /// CTK includes
  22. #include "ctkPimpl.h"
  23. #include "ctkCoreExport.h"
  24. class ctkTransferFunctionPrivate;
  25. class ctkTransferFunctionRepresentation;
  26. //-----------------------------------------------------------------------------
  27. /// \ingroup Core
  28. struct CTK_CORE_EXPORT ctkPoint
  29. {
  30. ctkPoint(){}
  31. ctkPoint(qreal x, const QVariant& v)
  32. :X(x),Value(v)
  33. {
  34. }
  35. qreal X;
  36. // QColor or qreal.
  37. QVariant Value;
  38. };
  39. //-----------------------------------------------------------------------------
  40. /// \ingroup Core
  41. /// assumes the mapping is linear by default, if not, then subclasses must be
  42. /// used
  43. struct CTK_CORE_EXPORT ctkControlPoint
  44. {
  45. virtual ~ctkControlPoint();
  46. inline const qreal& x()const {return this->P.X;}
  47. inline const QVariant& value()const {return this->P.Value;}
  48. ctkPoint P;
  49. };
  50. //-----------------------------------------------------------------------------
  51. /// \ingroup Core
  52. struct CTK_CORE_EXPORT ctkBezierControlPoint : public ctkControlPoint
  53. {
  54. virtual ~ctkBezierControlPoint();
  55. ctkPoint P1;
  56. ctkPoint P2;
  57. };
  58. //-----------------------------------------------------------------------------
  59. /// \ingroup Core
  60. struct CTK_CORE_EXPORT ctkNonLinearControlPoint : public ctkControlPoint
  61. {
  62. virtual ~ctkNonLinearControlPoint();
  63. QList<ctkPoint> SubPoints;
  64. };
  65. //-----------------------------------------------------------------------------
  66. /// \ingroup Core
  67. class CTK_CORE_EXPORT ctkTransferFunction: public QObject
  68. {
  69. Q_OBJECT
  70. public:
  71. ctkTransferFunction(QObject* parent = 0);
  72. virtual ~ctkTransferFunction();
  73. virtual ctkControlPoint* controlPoint(int index)const = 0;
  74. inline QVariant value(int index)const;
  75. virtual QVariant value(qreal pos)const = 0;
  76. virtual int count()const = 0;
  77. virtual bool isDiscrete()const = 0;
  78. virtual bool isEditable()const = 0;
  79. inline void range(qreal rangeValues[2])const;
  80. virtual void range(qreal& minRange, qreal& maxRange)const=0;
  81. virtual QVariant minValue()const = 0;
  82. virtual QVariant maxValue()const = 0;
  83. inline void valueRange(QVariant range[2])const;
  84. ///
  85. virtual int insertControlPoint(const ctkControlPoint& cp) = 0;
  86. virtual int insertControlPoint(qreal pos) = 0;
  87. virtual void removeControlPoint( qreal pos ) = 0;
  88. ///
  89. /// be careful with it, as changing the value might require
  90. /// more changes to ctkControlPoint.
  91. virtual void setControlPointPos(int index, qreal pos)=0;
  92. ///
  93. /// be careful with it, as changing the value might require
  94. /// more changes to ctkControlPoint.
  95. virtual void setControlPointValue(int index, const QVariant& value)=0;
  96. ctkTransferFunctionRepresentation* representation()const;
  97. signals:
  98. void changed();
  99. protected:
  100. QScopedPointer<ctkTransferFunctionPrivate> d_ptr;
  101. private:
  102. Q_DECLARE_PRIVATE(ctkTransferFunction);
  103. Q_DISABLE_COPY(ctkTransferFunction);
  104. };
  105. //-----------------------------------------------------------------------------
  106. QVariant ctkTransferFunction::value(int index)const
  107. {
  108. QSharedPointer<ctkControlPoint> cp(this->controlPoint(index));
  109. return cp->P.Value;
  110. }
  111. //-----------------------------------------------------------------------------
  112. void ctkTransferFunction::range(qreal rangeValues[2])const
  113. {
  114. this->range(rangeValues[0], rangeValues[1]);
  115. }
  116. //-----------------------------------------------------------------------------
  117. void ctkTransferFunction::valueRange(QVariant rangeValues[2])const
  118. {
  119. rangeValues[0] = this->minValue();
  120. rangeValues[1] = this->maxValue();
  121. }
  122. #endif