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