aboutsummaryrefslogtreecommitdiff
path: root/tools/CurveEditor/source/Curve.h
blob: a070fb54fd467fecb17b64addab4411c523d21c2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#ifndef CURVE_H
#define CURVE_H
#include <QtCore/QPointF>
#include <QtGui/QColor>
#include <vector>
#include "Common.h"

namespace nvidia {
namespace CurveEditor {

enum InterpolateMode
{
    eDiscret,
    eLinear,
    eBezierSpline,
    eCatmullRomSpline,
};

struct BezierSplineData
{
    float    inTan;
    float    inLen;
    float    outTan;
    float    outLen;
};

struct ControlPoint
{
    ControlPoint()
        : value(0, 0)
        , mode (eDiscret)
    {
        memset( &splineData, 0, sizeof(BezierSplineData));
    }

    ControlPoint(float inX, float inY, InterpolateMode inMode)
        : value(inX, inY)
        , mode(inMode)
    {
        memset( &splineData, 0, sizeof(BezierSplineData));
    }

    ControlPoint(float inX, float inY, InterpolateMode inMode, BezierSplineData& inSplineData)
        : value(inX, inY)
        , mode(inMode)
        , splineData(inSplineData)
    {

    }

    QPointF value;
    InterpolateMode mode;
    BezierSplineData splineData;
};

class CURVEEDITOR_EXPORT Curve
{
    friend class CurveWidget;
    friend class CurveEntity;
public:
    Curve(long segmentCount = 100);

    void initValueRange(const QPointF& min, const QPointF& max);
    QPointF getMinValue();
    QPointF getMaxValue();

    int getControlPointCount()  { return (int) _controlPoints.size(); }
    const ControlPoint& getControlPoint(int index)    { return _controlPoints[index]; }
    void appendControlPoint(ControlPoint& controlPoint, bool asDefaultToo = true);
    int appendControlPoint(float x);
    void setControlPoint(int index, const ControlPoint& point);
    void reset(void);
    void insertControlPointAt(int index);
    void removeControlPoint(int index);
    void removeControlPoints(std::vector<int>& indexes);

    QPointF getPointByX(float x);
    std::vector<QPointF> getSamplePoints();
    Curve resampleCurve(int resamplePnts, long segmentCount = 100);

    Curve& operator = (const Curve& right);

private:
    void _sample();
    ControlPoint& _getNearCtrlPnt(float x);

private:
    bool                    _needSample;
    bool                    _initValueRange;
    QPointF                 _minValue;
    QPointF                 _maxValue;
    long                    _segmentCount;
    std::vector<QPointF>    _samplePoints;
    std::vector<ControlPoint>    _controlPoints;
    std::vector<ControlPoint>    _defaultControlPoints;
};

struct ColorControlPoint
{
    ColorControlPoint()
        : x(0.0)
        , color()
        , mode (eDiscret)
        , weight(0.5f)
        , fallOff(0.5f)
        , texturePath()
    {
        memset( &splineData, 0, sizeof(BezierSplineData));
    }

    ColorControlPoint(float inX, const QColor& inColor, InterpolateMode inMode, float inWeight = 0.5f, float inFallOff = 0.5f)
        : x(inX)
        , color(inColor)
        , mode(inMode)
        , weight(inWeight)
        , fallOff(inFallOff)
        , texturePath()
    {
        memset( &splineData, 0, sizeof(BezierSplineData));
    }

    ColorControlPoint(float inX, const QColor& inColor, InterpolateMode inMode, const BezierSplineData& inSplineData, float inWeight = 0.5f, float inFallOff = 0.5f)
        : x(inX)
        , color(inColor)
        , mode(inMode)
        , splineData(inSplineData)
        , weight(inWeight)
        , fallOff(inFallOff)
        , texturePath()
    {

    }

    float               x;
    QColor              color;
    InterpolateMode     mode;
    BezierSplineData    splineData;
    float               weight;         // if it's less than 0, the segememnt between this control point and next control point won't use weight algorithm
    float               fallOff;
    std::string         texturePath;
    QColor              textureAverageColor;
};

struct ColorPoint
{
    ColorPoint()
        : x(0.0)
        , color()
    {
    }

    ColorPoint(float inX, const QColor& inColor)
        : x(inX)
        , color(inColor)
    {
    }

    float               x;
    QColor              color;
};

class ColorWidget;
class CURVEEDITOR_EXPORT ColorCurve
{
    friend class ColorWidget;
public:
    ColorCurve(long segmentCount = 100);

    void initValueRange(float min, float max);
    float getMinValue();
    float getMaxValue();

    int getControlPointCount()  { return (int) _controlPoints.size(); }
    const ColorControlPoint& getControlPoint(int index)    { return _controlPoints[index]; }
    void appendControlPoint(ColorControlPoint& controlPoint, bool asDefaultToo = true);
    void setControlPoint(int index, const ColorControlPoint& point);
    void reset(void);
    void insertControlPointAt(int index);
    void removeControlPoint(int index);

    QColor getColorByX(float x);

    ColorCurve& operator = (const ColorCurve& right);

private:
    void _doSamplePoints();
    void _reOrderControlPoints(int& pickedPoint);
    QColor _calcTextureAverageColor(const char* texturePath);

private:
    bool                        _needSample;
    bool                        _initValueRange;
    float                       _minValue;
    float                       _maxValue;
    long                        _segmentCount;
    std::vector<ColorPoint>          _colorSamplePnts;
    std::vector<ColorControlPoint>   _controlPoints;
    std::vector<ColorControlPoint>   _defaultControlPoints;
};

} // namespace CurveEditor
} // namespace nvidia

#endif // CURVE_H