HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_Ramp.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: UT library (C++)
7  *
8  * COMMENTS: Ramp class
9  *
10  */
11 
12 
13 #ifndef _UT_Ramp_
14 #define _UT_Ramp_
15 
16 #include "UT_API.h"
17 #include "UT_Array.h"
18 #include "UT_Color.h"
19 #include "UT_Map.h"
20 #include "UT_Pixel.h"
21 #include "UT_Spline.h"
22 #include "UT_VectorTypes.h"
23 
24 #include <SYS/SYS_Types.h>
25 
26 #include <tbb/spin_mutex.h>
27 
28 #include <iosfwd>
29 
30 
31 class UT_RampOpResolver;
32 class UT_Raster;
33 class UT_IStream;
34 
35 enum UT_RampOrient { // Pass to redraw
40 };
41 
42 class UT_API UT_ColorNode { // Floating point RGBA values
43  struct SerializableFRGBA : public UT_FRGBA
44  {
45  using UT_FRGBA::operator=;
46 
47  template <typename ARCHIVER_T>
48  bool serialize(ARCHIVER_T &archiver)
49  {
50  return archiver.serializeUniformArray(&r, 4);
51  }
52  };
53 
54 public:
58  bool enable_pos_ = true,
59  bool enable_val_ = true,
60  bool enable_interp_ = true)
61  {
62  t = offset;
63  old_t = t;
64  basis = basis_;
65  rgba = color;
66  old_rgba = rgba;
67  enable_pos = enable_pos_;
68  enable_val = enable_val_;
69  enable_interp = enable_interp_;
70  }
71 
72  template <typename ARCHIVER_T>
73  bool serialize(ARCHIVER_T &archiver);
74 
75  float t;
76  float old_t;
77  SerializableFRGBA rgba;
78  SerializableFRGBA old_rgba;
80  bool enable_pos:1,
81  enable_val:1,
82  enable_interp:1;
83 };
84 
85 //==========================================================================
86 
87 /// Utility class for containing a color ramp
88 class UT_API UT_Ramp {
89 public:
90  UT_Ramp();
91  /// Do not pass by value, you should be passing by
92  /// const &. If you really need by value make the copy
93  /// internally to your system.
94  explicit UT_Ramp(const UT_Ramp &copy);
95  ~UT_Ramp();
96 
97  /// Return the amount of memory owned by this UT_Ramp in bytes
98  int64 getMemoryUsage(bool inclusive) const;
99 
100  void setColorType(UT_ColorType color_space)
101  { myColorType = color_space; }
103  { return myColorType; }
104 
105  void clearAndDestroy();
106 
107  /// Force updating of the ramp after being modified.
108  void ensureRampIsBuilt();
109 
110  bool getGlobalEnable() const
111  { return myGlobalEnable; }
112  void setGlobalEnable(bool enable)
113  { myGlobalEnable = enable; }
114 
115  int getNodeCount() const { return myNodes.entries(); }
116  const UT_ColorNode *getNode(int i) const;
117 
119  { return myNodes.entries() > 0 ? &myNodes(0)
120  : nullptr; }
121  const UT_ColorNode *getRamp() const
122  { return myNodes.entries() > 0 ? &myNodes(0)
123  : nullptr; }
124 
125  void convertToRaster( UT_Raster *raster,
126  UT_RampOrient dir,
127  bool do_interp = true,
128  // Currently only for radial and
129  // concentric
130  fpreal cx = 0.0f,
131  fpreal cy = 0.0f,
132  fpreal phase = 0.0f,
133  // For radial, this means "repeat"
134  fpreal radius = 1.0f);
135 
136  const UT_ColorNode *getClosestNode(fpreal pos) const;
137  int getClosestNodeIndex(fpreal pos) const;
138 
139  const UT_ColorNode *getClosestNode(fpreal pos, fpreal val) const;
140  int getClosestNodeIndex(fpreal pos, fpreal val) const;
141 
142  const UT_ColorNode *getCurrentNode() const;
143  int getCurrentNodeIndex() const;
144  void setCurrentNodeIndex(int i, bool add_to_selection);
145 
146  void getColor(fpreal pos, UT_FRGBA *clr,
147  bool do_interp = true) const;
148  void getColor(fpreal pos, float fvals[4],
149  bool do_interp = true) const;
150  void getColor(UT_FRGBA *values, int num,
151  bool do_interp = true) const;
152 
153  /// Warning: If setting scalar ramps, make sure all of the fvals
154  /// have the same value, as evaluators will pick a channel at whim.
155  void setColorAt(int i, float fvals[4]);
156  void setColorAt(int i, const UT_FRGBA &clr);
157  void setCurrentNodeColor(fpreal32 fvals[4]);
158  void setCurrentNodeColor(fpreal64 fvals[4]);
159  void setCurrentNodeColor(const UT_FRGBA &clr);
160 
161  void setCurrentNodeBasis(UT_SPLINE_BASIS basis);
162  void setNodeBasis(int i, UT_SPLINE_BASIS basis);
163 
164  void setEnable(int i, bool enable_pos, bool enable_val,
165  bool enable_interp);
166  bool getEnablePos(int i) const;
167  bool getEnableVal(int i) const;
168  bool getEnableInterp(int i) const;
169 
170  bool getStrictOrder() const;
171  void setStrictOrder(bool strict);
172 
173  void clearSelection();
174  bool isNodeSelected(int i) const;
175  void selectNode(int i, bool add);
176  void selectRange(int first_i, int last_i, bool add);
177  void deselectNode(int i);
178  void toggleSelectNode(int i);
179  void getSelectionIndices(UT_IntArray &indices);
180  void setSelectionIndices(const UT_IntArray &indices);
181 
182  /// Returns the index of the inserted node.
183  int addNode(fpreal pos);
184  /// WARNING: If building scalar ramps, set ALL the fvals to the same
185  /// value. Code will randomly evaluate .r or .a
186  int addNode(fpreal pos, fpreal32 fvals[4],
188  bool current = true);
189  int addNode(fpreal pos, fpreal64 fvals[4],
191  bool current = true);
192  int addNode(fpreal pos, const UT_FRGBA &color,
194  bool current = true);
195 
196  int insertNode(int i, fpreal pos, fpreal32 fvals[4],
198  bool current = true);
199  int insertNode(int i, fpreal pos, fpreal64 fvals[4],
201  bool current = true);
202 
203  void reorderNodes();
204 
205  void recordNodePositions();
206 
207  bool deleteNode(fpreal pos);
208  bool deleteNodeAt(int i);
209  void deleteSelectedNodes();
210 
211  bool isNodeHandle(int i) const;
212  bool isNodeLeftHandle(int i) const;
213  bool isNodeRightHandle(int i) const;
214 
215  void moveNode(int i, fpreal pos);
216  void moveNode(const UT_ColorNode *node, fpreal pos);
217 
218  fpreal solveSpline(fpreal pos) const;
219 
220  int load(const char *pathname);
221  bool load(UT_IStream &is);
222  bool load(UT_JSONParser &p);
223  int save(const char *pathname) const;
224  int save(std::ostream &os) const;
225 
226  UT_Ramp &operator=(const UT_Ramp &from);
227 
228  // Equality does not consider the selected or current node, only the ramp
229  // values themselves.
230  bool operator==(const UT_Ramp &from) const;
231 
232  void setRamp(const UT_ColorNode *r, int size);
233 
234  // For interpolated lookups, order > 0 finds the derivative wrt pos
235  void rampLookup(fpreal pos, float values[4],
236  int order = 0) const;
237  void rampLookupNoInterp(fpreal pos, float values[4]) const;
238  void rampLookup(fpreal u0, fpreal u1, float values[4],
239  int ns, int order = 0) const;
240  void rampLookupNoInterp(fpreal u0, fpreal u1, float values[4],
241  int ns) const;
242 
243  template <typename ARCHIVER_T>
244  bool serialize(ARCHIVER_T &archiver);
245 
246  /// Install an OP node resolver for ramp parameters.
247  /// Required for resolveOpRamp() to work. OP_Director calls this.
248  static void setRampOpResolver(UT_RampOpResolver *resolver);
249 
250  /// Fetch the ramp from an OP node's ramp parm. May return false
251  /// if the OP or parm is not found, if the parm is not a ramp, or if the
252  /// UT_RampOpResolver is not assigned by setRampOpResolver().
253  /// is_rgb, op_id, and op_version are optional return values for the type
254  /// of ramp, the parent node's unique ID, and the parent node's version
255  /// parm serial (as reported by getVersionParms()).
256  bool resolveOpRamp(const char *op_path,
257  const char *ramp_parm,
258  bool *is_rgb = nullptr,
259  int *op_id = nullptr,
260  int *op_version = nullptr);
261 
262 private:
263  bool loadAsPoly(UT_IStream &is);
264  bool loadAsJSON(UT_JSONParser &p);
265 
266  UT_RGBA *sampleColors(int nsamples, bool do_interp);
267  UT_BIG_RGBA *sampleColors16(int nsamples, bool do_interp);
268  void convertToRaster8(UT_Raster *raster,UT_RampOrient dir,
269  bool do_interp, fpreal cx, fpreal cy, fpreal phase,
270  fpreal radius);
271  void convertToRaster16(UT_Raster *raster,UT_RampOrient dir,
272  bool do_interp, fpreal cx, fpreal cy, fpreal phase,
273  fpreal radius);
274 
275  void getColorAndMultiply(fpreal pos, bool do_interp,
276  UT_RGBA *pixel);
277  void getColorAndMultiply(fpreal pos, bool do_interp,
278  UT_BIG_RGBA *pixel);
279 
280  void interpColor(fpreal pos, float vals[4],
281  int order = 0) const;
282 
283  void updateSplineIfNeeded() const;
284  void evaluateSpline(fpreal pos, fpreal t0, fpreal t1,
285  int i, float *val, int order = 0) const;
286 
287 private:
288  template <typename T>
289  int addNodeImpl(
290  fpreal pos,
291  T fvals[4],
292  UT_SPLINE_BASIS basis,
293  bool current);
294  template <typename T>
295  int insertNodeImpl(
296  int i,
297  fpreal pos,
298  T fvals[4],
299  UT_SPLINE_BASIS basis,
300  bool current);
301 
302  typedef tbb::spin_mutex SplineLock;
303 
305  UT_ColorNode *myCurrentNode;
306 
307  UT_Map<int, UT_ColorNode *> mySelectedNodes;
308 
309  UT_ColorType myColorType;
310 
311  mutable UT_Spline mySpline;
312  mutable bool mySplineUpToDate;
313  mutable SplineLock mySplineLock;
314 
315  bool myGlobalEnable;
316  bool myStrictOrder;
317 };
318 
319 /// Class to create a UT_Ramp from an OP node path and parameter string.
321 {
322 public:
324  virtual ~UT_RampOpResolver();
325 
326  UT_RampOpResolver(const UT_RampOpResolver &) = delete;
327  UT_RampOpResolver &operator=(const UT_RampOpResolver &) = delete;
328 
329  /// Fetch a UT_Ramp from an OP node's ramp parm. May return false
330  /// if the OP or parm is not found, or if the parm is not a ramp.
331  /// is_rgb, op_id, and op_version are optional return values for the type
332  /// of ramp, the parent node's unique ID, and the parent node's version
333  /// parm serial (as reported by getVersionParms()
334  virtual bool resolveRampParm(UT_Ramp &target_ramp,
335  const char *op_path,
336  const char *ramp_parm,
337  bool *is_rgb = nullptr,
338  int *op_id = nullptr,
339  int *op_version = nullptr);
340 };
341 
342 #endif
UT_ColorNode * getRamp()
Definition: UT_Ramp.h:118
GLsizei GLenum const void * indices
Definition: glcorearb.h:406
myNodes
Definition: UT_RTreeImpl.h:708
UT_ColorNode(fpreal offset, const UT_FRGBA &color, UT_SPLINE_BASIS basis_=UT_SPLINE_LINEAR, bool enable_pos_=true, bool enable_val_=true, bool enable_interp_=true)
Definition: UT_Ramp.h:56
bool getGlobalEnable() const
Definition: UT_Ramp.h:110
UT_ColorNode()
Definition: UT_Ramp.h:55
OIIO_UTIL_API bool copy(string_view from, string_view to, std::string &err)
JSON reader class which handles parsing of JSON or bJSON files.
Definition: UT_JSONParser.h:87
#define UT_API
Definition: UT_API.h:14
UT_SPLINE_BASIS
Definition: UT_Spline.h:31
float fpreal32
Definition: SYS_Types.h:200
GLdouble u1
Definition: glad.h:2676
float t
Definition: UT_Ramp.h:75
double fpreal64
Definition: SYS_Types.h:201
Class to create a UT_Ramp from an OP node path and parameter string.
Definition: UT_Ramp.h:320
bool operator==(const BaseDimensions< T > &a, const BaseDimensions< Y > &b)
Definition: Dimensions.h:137
GLfloat f
Definition: glcorearb.h:1926
GLintptr offset
Definition: glcorearb.h:665
void setColorType(UT_ColorType color_space)
Definition: UT_Ramp.h:100
int getNodeCount() const
Definition: UT_Ramp.h:115
const UT_ColorNode * getRamp() const
Definition: UT_Ramp.h:121
float old_t
Definition: UT_Ramp.h:76
void setGlobalEnable(bool enable)
Definition: UT_Ramp.h:112
HUSD_API const char * raster()
GLdouble GLdouble GLint GLint order
Definition: glad.h:2676
long long int64
Definition: SYS_Types.h:116
UT_ColorType getColorType() const
Definition: UT_Ramp.h:102
GLdouble t
Definition: glad.h:2397
GLsizeiptr size
Definition: glcorearb.h:664
SerializableFRGBA rgba
Definition: UT_Ramp.h:77
UT_ColorType
Definition: UT_Color.h:24
UT_RampOrient
Definition: UT_Ramp.h:35
GLenum GLsizei GLsizei GLint * values
Definition: glcorearb.h:1602
GLuint color
Definition: glcorearb.h:1261
fpreal64 fpreal
Definition: SYS_Types.h:277
UT_SPLINE_BASIS basis
Definition: UT_Ramp.h:79
Utility class for containing a color ramp.
Definition: UT_Ramp.h:88
GLuint GLfloat * val
Definition: glcorearb.h:1608
SerializableFRGBA old_rgba
Definition: UT_Ramp.h:78
ImageBuf OIIO_API add(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
GLboolean r
Definition: glcorearb.h:1222