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 #include "UT_SharedPtr.h"
24 
25 #include <SYS/SYS_Types.h>
26 #include <SYS/SYS_Hash.h>
27 
28 #include <tbb/spin_mutex.h>
29 
30 #include <iosfwd>
31 
32 
33 class UT_RampOpResolver;
34 class UT_Raster;
35 class UT_IStream;
36 
37 // Pass to redraw
39 {
44 };
45 
46 // Floating point RGBA values
48 {
49  struct SerializableFRGBA : public UT_FRGBA
50  {
51  using UT_FRGBA::operator=;
52 
53  template <typename ARCHIVER_T>
54  bool serialize(ARCHIVER_T &archiver)
55  {
56  return archiver.serializeUniformArray(&r, 4);
57  }
58  };
59 
60 public:
64  bool enable_pos_ = true,
65  bool enable_val_ = true,
66  bool enable_interp_ = true)
67  {
68  t = offset;
69  old_t = t;
70  basis = basis_;
71  rgba = color;
72  old_rgba = rgba;
73  enable_pos = enable_pos_;
74  enable_val = enable_val_;
75  enable_interp = enable_interp_;
76  }
77 
78  SYS_HashType hash() const;
79 
80  template <typename ARCHIVER_T>
81  bool serialize(ARCHIVER_T &archiver);
82 
83  float t;
84  float old_t;
85  SerializableFRGBA rgba;
86  SerializableFRGBA old_rgba;
88  bool enable_pos:1,
89  enable_val:1,
90  enable_interp:1;
91 };
92 
93 //==========================================================================
94 
95 /// Utility class for containing a color ramp
97 {
98 public:
99  UT_Ramp();
100  /// Do not pass by value, you should be passing by
101  /// const &. If you really need by value make the copy
102  /// internally to your system.
103  explicit UT_Ramp(const UT_Ramp &copy);
104  ~UT_Ramp();
105 
106  /// Return the amount of memory owned by this UT_Ramp in bytes
107  int64 getMemoryUsage(bool inclusive) const;
108 
109  void setColorType(UT_ColorType color_space)
110  { myColorType = color_space; }
112  { return myColorType; }
113 
114  void clearAndDestroy();
115 
116  /// Force updating of the ramp after being modified.
117  void ensureRampIsBuilt();
118 
119  bool getGlobalEnable() const
120  { return myGlobalEnable; }
121  void setGlobalEnable(bool enable)
122  { myGlobalEnable = enable; }
123 
124  int getNodeCount() const { return myNodes.entries(); }
125  const UT_ColorNode *getNode(int i) const;
126 
128  { return myNodes.entries() > 0 ? &myNodes(0)
129  : nullptr; }
130  const UT_ColorNode *getRamp() const
131  { return myNodes.entries() > 0 ? &myNodes(0)
132  : nullptr; }
133 
134  void convertToRaster( UT_Raster *raster,
135  UT_RampOrient dir,
136  bool do_interp = true,
137  // Currently only for radial and
138  // concentric
139  fpreal cx = 0.0f,
140  fpreal cy = 0.0f,
141  fpreal phase = 0.0f,
142  // For radial, this means "repeat"
143  fpreal radius = 1.0f);
144 
145  const UT_ColorNode *getClosestNode(fpreal pos) const;
146  int getClosestNodeIndex(fpreal pos) const;
147 
148  const UT_ColorNode *getClosestNode(fpreal pos, fpreal val) const;
149  int getClosestNodeIndex(fpreal pos, fpreal val) const;
150 
151  const UT_ColorNode *getCurrentNode() const;
152  int getCurrentNodeIndex() const;
153  void setCurrentNodeIndex(int i, bool add_to_selection);
154 
155  void getColor(fpreal pos, UT_FRGBA *clr,
156  bool do_interp = true) const;
157  void getColor(fpreal pos, float fvals[4],
158  bool do_interp = true) const;
159  void getColor(UT_FRGBA *values, int num,
160  bool do_interp = true) const;
161 
162  /// Warning: If setting scalar ramps, make sure all of the fvals
163  /// have the same value, as evaluators will pick a channel at whim.
164  void setColorAt(int i, float fvals[4]);
165  void setColorAt(int i, const UT_FRGBA &clr);
166  void setCurrentNodeColor(fpreal32 fvals[4]);
167  void setCurrentNodeColor(fpreal64 fvals[4]);
168  void setCurrentNodeColor(const UT_FRGBA &clr);
169 
170  void setCurrentNodeBasis(UT_SPLINE_BASIS basis);
171  void setNodeBasis(int i, UT_SPLINE_BASIS basis);
172 
173  void setEnable(int i, bool enable_pos, bool enable_val,
174  bool enable_interp);
175  bool getEnablePos(int i) const;
176  bool getEnableVal(int i) const;
177  bool getEnableInterp(int i) const;
178 
179  bool getStrictOrder() const;
180  void setStrictOrder(bool strict);
181 
182  void clearSelection();
183  bool isNodeSelected(int i) const;
184  void selectNode(int i, bool add);
185  void selectRange(int first_i, int last_i, bool add);
186  void deselectNode(int i);
187  void toggleSelectNode(int i);
188  void getSelectionIndices(UT_IntArray &indices);
189  void setSelectionIndices(const UT_IntArray &indices);
190 
191  /// Returns the index of the inserted node.
192  int addNode(fpreal pos);
193  /// WARNING: If building scalar ramps, set ALL the fvals to the same
194  /// value. Code will randomly evaluate .r or .a
195  int addNode(fpreal pos, fpreal32 fvals[4],
197  bool current = true);
198  int addNode(fpreal pos, fpreal64 fvals[4],
200  bool current = true);
201  int addNode(fpreal pos, const UT_FRGBA &color,
203  bool current = true);
204 
205  int insertNode(int i, fpreal pos, fpreal32 fvals[4],
207  bool current = true);
208  int insertNode(int i, fpreal pos, fpreal64 fvals[4],
210  bool current = true);
211 
212  void reorderNodes();
213 
214  void recordNodePositions();
215 
216  bool deleteNode(fpreal pos);
217  bool deleteNodeAt(int i);
218  void deleteSelectedNodes();
219 
220  bool isNodeHandle(int i) const;
221  bool isNodeLeftHandle(int i) const;
222  bool isNodeRightHandle(int i) const;
223 
224  void moveNode(int i, fpreal pos);
225  void moveNode(const UT_ColorNode *node, fpreal pos);
226 
227  fpreal solveSpline(fpreal pos) const;
228 
229  int load(const char *pathname);
230  bool load(UT_IStream &is);
231  bool load(UT_JSONParser &p);
232  int save(const char *pathname) const;
233  int save(std::ostream &os) const;
234 
235  UT_Ramp &operator=(const UT_Ramp &from);
236 
237  // Equality does not consider the selected or current node, only the ramp
238  // values themselves.
239  bool operator==(const UT_Ramp &from) const;
240  bool operator!=(const UT_Ramp &from) const { return !(*this == from); }
241  // Hash likewise only includes ramp values.
242  SYS_HashType hash() const;
243 
244  void setRamp(const UT_ColorNode *r, int size);
245 
246  // For interpolated lookups, order > 0 finds the derivative wrt pos
247  void rampLookup(fpreal pos, float values[4],
248  int order = 0) const;
249  void rampLookupNoInterp(fpreal pos, float values[4]) const;
250  void rampLookup(fpreal u0, fpreal u1, float values[4],
251  int ns, int order = 0) const;
252  void rampLookupNoInterp(fpreal u0, fpreal u1, float values[4],
253  int ns) const;
254 
255  template <typename ARCHIVER_T>
256  bool serialize(ARCHIVER_T &archiver);
257 
258  /// Returns a ramp constructed from the canonical default strings.
259  static UT_SharedPtr<UT_Ramp> buildFromDefault(const char *def);
260 
261  /// Install an OP node resolver for ramp parameters.
262  /// Required for resolveOpRamp() to work. OP_Director calls this.
263  static void setRampOpResolver(UT_RampOpResolver *resolver);
264 
265  /// Fetch the ramp from an OP node's ramp parm. May return false
266  /// if the OP or parm is not found, if the parm is not a ramp, or if the
267  /// UT_RampOpResolver is not assigned by setRampOpResolver().
268  /// is_rgb, op_id, and op_version are optional return values for the type
269  /// of ramp, the parent node's unique ID, and the parent node's version
270  /// parm serial (as reported by getVersionParms()).
271  bool resolveOpRamp(const char *op_path,
272  const char *ramp_parm,
273  bool *is_rgb = nullptr,
274  int *op_id = nullptr,
275  int *op_version = nullptr);
276 
277  /// Ramp registration to allow ramp to be converted
278  /// to integers across HOM boundaries.
279 
280  /// Registers & returns the handle, adding the handle to the registered
281  /// list
282  static int registerRamp(UT_SharedPtr<UT_Ramp> ramp, UT_IntArray &registered);
283  /// Unregister all layers corresponding to the provided list,
284  /// erase the list afterwards. Assertion if handle wasn't regsitered
285  static void unregisterRamps(UT_IntArray &registered);
286  /// Look up a registered layer by handle.
287  static UT_SharedPtr<UT_Ramp> lookupRamp(int handle);
288 
289 private:
290  bool loadAsPoly(UT_IStream &is);
291  bool loadAsJSON(UT_JSONParser &p);
292 
293  UT_RGBA *sampleColors(int nsamples, bool do_interp);
294  UT_BIG_RGBA *sampleColors16(int nsamples, bool do_interp);
295  void convertToRaster8(UT_Raster *raster,UT_RampOrient dir,
296  bool do_interp, fpreal cx, fpreal cy, fpreal phase,
297  fpreal radius);
298  void convertToRaster16(UT_Raster *raster,UT_RampOrient dir,
299  bool do_interp, fpreal cx, fpreal cy, fpreal phase,
300  fpreal radius);
301 
302  void getColorAndMultiply(fpreal pos, bool do_interp,
303  UT_RGBA *pixel);
304  void getColorAndMultiply(fpreal pos, bool do_interp,
305  UT_BIG_RGBA *pixel);
306 
307  void interpColor(fpreal pos, float vals[4],
308  int order = 0) const;
309 
310  void updateSplineIfNeeded() const;
311  void evaluateSpline(fpreal pos, fpreal t0, fpreal t1,
312  int i, float *val, int order = 0) const;
313 
314 private:
315  template <typename T>
316  int addNodeImpl(
317  fpreal pos,
318  T fvals[4],
319  UT_SPLINE_BASIS basis,
320  bool current);
321  template <typename T>
322  int insertNodeImpl(
323  int i,
324  fpreal pos,
325  T fvals[4],
326  UT_SPLINE_BASIS basis,
327  bool current);
328 
329  typedef tbb::spin_mutex SplineLock;
330 
332  UT_ColorNode *myCurrentNode;
333 
334  UT_Map<int, UT_ColorNode *> mySelectedNodes;
335 
336  UT_ColorType myColorType;
337 
338  mutable UT_Spline mySpline;
339  mutable bool mySplineUpToDate;
340  mutable SplineLock mySplineLock;
341 
342  bool myGlobalEnable;
343  bool myStrictOrder;
344 };
345 
346 /// Class to create a UT_Ramp from an OP node path and parameter string.
348 {
349 public:
351  virtual ~UT_RampOpResolver();
352 
353  UT_RampOpResolver(const UT_RampOpResolver &) = delete;
354  UT_RampOpResolver &operator=(const UT_RampOpResolver &) = delete;
355 
356  /// Fetch a UT_Ramp from an OP node's ramp parm. May return false
357  /// if the OP or parm is not found, or if the parm is not a ramp.
358  /// is_rgb, op_id, and op_version are optional return values for the type
359  /// of ramp, the parent node's unique ID, and the parent node's version
360  /// parm serial (as reported by getVersionParms()
361  virtual bool resolveRampParm(UT_Ramp &target_ramp,
362  const char *op_path,
363  const char *ramp_parm,
364  bool *is_rgb = nullptr,
365  int *op_id = nullptr,
366  int *op_version = nullptr);
367 };
368 
369 #endif
UT_ColorNode * getRamp()
Definition: UT_Ramp.h:127
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:62
bool getGlobalEnable() const
Definition: UT_Ramp.h:119
UT_ColorNode()
Definition: UT_Ramp.h:61
OIIO_UTIL_API bool copy(string_view from, string_view to, std::string &err)
std::size_t SYS_HashType
Define the type for hash values.
Definition: SYS_Hash.h:19
JSON reader class which handles parsing of JSON or bJSON files.
Definition: UT_JSONParser.h:87
#define UT_API
Definition: UT_API.h:14
bool operator!=(const UT_Ramp &from) const
Definition: UT_Ramp.h:240
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:83
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:347
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:109
std::shared_ptr< T > UT_SharedPtr
Wrapper around std::shared_ptr.
Definition: UT_SharedPtr.h:36
int getNodeCount() const
Definition: UT_Ramp.h:124
const UT_ColorNode * getRamp() const
Definition: UT_Ramp.h:130
float old_t
Definition: UT_Ramp.h:84
void setGlobalEnable(bool enable)
Definition: UT_Ramp.h:121
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:111
GLdouble t
Definition: glad.h:2397
GLsizeiptr size
Definition: glcorearb.h:664
SerializableFRGBA rgba
Definition: UT_Ramp.h:85
UT_ColorType
Definition: UT_Color.h:24
UT_RampOrient
Definition: UT_Ramp.h:38
GLenum GLsizei GLsizei GLint * values
Definition: glcorearb.h:1602
GLuint color
Definition: glcorearb.h:1261
fpreal64 fpreal
Definition: SYS_Types.h:278
UT_SPLINE_BASIS basis
Definition: UT_Ramp.h:87
LeafData & operator=(const LeafData &)=delete
Utility class for containing a color ramp.
Definition: UT_Ramp.h:96
GLuint GLfloat * val
Definition: glcorearb.h:1608
SerializableFRGBA old_rgba
Definition: UT_Ramp.h:86
ImageBuf OIIO_API add(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
GLboolean r
Definition: glcorearb.h:1222