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  basis = basis_;
64  rgba = color;
65  enable_pos = enable_pos_;
66  enable_val = enable_val_;
67  enable_interp = enable_interp_;
68  }
69 
70  template <typename ARCHIVER_T>
71  bool serialize(ARCHIVER_T &archiver);
72 
73  float t;
74  SerializableFRGBA rgba;
76  bool enable_pos:1,
77  enable_val:1,
78  enable_interp:1;
79 };
80 
81 //==========================================================================
82 
83 /// Utility class for containing a color ramp
84 class UT_API UT_Ramp {
85 public:
86  UT_Ramp();
87  /// Do not pass by value, you should be passing by
88  /// const &. If you really need by value make the copy
89  /// internally to your system.
90  explicit UT_Ramp(const UT_Ramp &copy);
91  ~UT_Ramp();
92 
93  /// Return the amount of memory owned by this UT_Ramp in bytes
94  int64 getMemoryUsage(bool inclusive) const;
95 
96  void setColorType(UT_ColorType color_space)
97  { myColorType = color_space; }
99  { return myColorType; }
100 
101  void clearAndDestroy();
102 
103  /// Force updating of the ramp after being modified.
104  void ensureRampIsBuilt();
105 
106  bool getGlobalEnable() const
107  { return myGlobalEnable; }
109  { myGlobalEnable = enable; }
110 
111  int getNodeCount() const { return myNodes.entries(); }
112  const UT_ColorNode *getNode(int i) const;
113 
115  { return myNodes.entries() > 0 ? &myNodes(0) : 0; }
116  const UT_ColorNode *getRamp() const
117  { return myNodes.entries() > 0 ? &myNodes(0) : 0; }
118 
119  void convertToRaster( UT_Raster *raster,
120  UT_RampOrient dir,
121  int doInterping = 1,
122  // Currently only for radial and
123  // concentric
124  fpreal cx = 0.0f,
125  fpreal cy = 0.0f,
126  fpreal phase = 0.0f,
127  // For radial, this means "repeat"
128  fpreal radius = 1.0f);
129 
130  const UT_ColorNode *getClosestNode(fpreal pos) const;
131  int getClosestNodeIndex(fpreal pos) const;
132 
133  const UT_ColorNode *getCurrentNode() const;
134  int getCurrentNodeIndex() const;
135  void setCurrentNodeIndex(int i, bool add_to_selection);
136 
137  void getColor(fpreal pos, UT_FRGBA *clr,
138  int doInterping = 1) const;
139  void getColor(fpreal pos, float fvals[4],
140  int doInterping = 1) const;
141  void getColor(UT_FRGBA *values, int num,
142  int doInterping = 1) const;
143 
144  /// Warning: If setting scalar ramps, make sure all of the fvals
145  /// have the same value, as evaluators will pick a channel at whim.
146  void setColorAt(int i, float fvals[4]);
147  void setColorAt(int i, const UT_FRGBA &clr);
148  void setCurrentNodeColor(fpreal32 fvals[4]);
149  void setCurrentNodeColor(fpreal64 fvals[4]);
150  void setCurrentNodeColor(const UT_FRGBA &clr);
151 
152  void setCurrentNodeBasis(UT_SPLINE_BASIS basis);
153  void setNodeBasis(int i, UT_SPLINE_BASIS basis);
154 
155  void setEnable(int i, bool enable_pos, bool enable_val,
156  bool enable_interp);
157  bool getEnablePos(int i) const;
158  bool getEnableVal(int i) const;
159  bool getEnableInterp(int i) const;
160 
161  void clearSelection();
162  bool isNodeSelected(int i);
163  void selectNode(int i, bool add);
164  void selectRange(int first_i, int last_i, bool add);
165  void deselectNode(int i);
166  void toggleSelectNode(int i);
167  void getSelectionIndices(UT_IntArray &indices);
168  void setSelectionIndices(const UT_IntArray &indices);
169 
170  void addNode(fpreal pos);
171  /// WARNING: If building scalar ramps, set ALL the fvals to the same
172  /// value. Code will randomly evaluate .r or .a
173  void addNode(fpreal pos, fpreal32 fvals[4],
175  void addNode(fpreal pos, fpreal64 fvals[4],
177  void addNode(fpreal pos, const UT_FRGBA &color,
179 
180  bool deleteNode(fpreal pos);
181  bool deleteNodeAt(int i);
182  void deleteSelectedNodes();
183 
184  void moveNode(const UT_ColorNode *node, fpreal pos);
185 
186  // Move the node selection according to the delta of selected node_i's
187  // position to pos.
188  void moveNodeSelection(int node_i, fpreal pos);
189 
190  int load(const char *pathname);
191  bool load(UT_IStream &is);
192  bool load(UT_JSONParser &p);
193  int save(const char *pathname) const;
194  int save(std::ostream &os) const;
195 
196  UT_Ramp &operator=(const UT_Ramp &from);
197 
198  // Equality does not consider the selected or current node, only the ramp
199  // values themselves.
200  bool operator==(const UT_Ramp &from) const;
201 
202  void setRamp(const UT_ColorNode *r, int size);
203 
204  // For interpolated lookups, order > 0 finds the derivative wrt pos
205  void rampLookup(fpreal pos, float values[4],
206  int order = 0) const;
207  void rampLookupNoInterp(fpreal pos, float values[4]) const;
208  void rampLookup(fpreal u0, fpreal u1, float values[4],
209  int ns, int order = 0) const;
210  void rampLookupNoInterp(fpreal u0, fpreal u1, float values[4],
211  int ns) const;
212 
213  template <typename ARCHIVER_T>
214  bool serialize(ARCHIVER_T &archiver);
215 
216  /// Install an OP node resolver for ramp parameters.
217  /// Required for resolveOpRamp() to work. OP_Director calls this.
218  static void setRampOpResolver(UT_RampOpResolver *resolver);
219 
220  /// Fetch the ramp from an OP node's ramp parm. May return false
221  /// if the OP or parm is not found, if the parm is not a ramp, or if the
222  /// UT_RampOpResolver is not assigned by setRampOpResolver().
223  /// is_rgb, op_id, and op_version are optional return values for the type
224  /// of ramp, the parent node's unique ID, and the parent node's version
225  /// parm serial (as reported by getVersionParms()).
226  bool resolveOpRamp(const char *op_path,
227  const char *ramp_parm,
228  bool *is_rgb = NULL,
229  int *op_id = NULL,
230  int *op_version = NULL);
231 
232 private:
233  bool loadAsPoly(UT_IStream &is);
234  bool loadAsJSON(UT_JSONParser &p);
235 
236  UT_RGBA *sampleColors (int nsamples, int doInterp);
237  UT_BIG_RGBA *sampleColors16(int nsamples, int doInterp);
238  void convertToRaster8 (UT_Raster *raster,UT_RampOrient dir,
239  int doInterp, fpreal cx, fpreal cy, fpreal phase,
240  fpreal radius);
241  void convertToRaster16(UT_Raster *raster,UT_RampOrient dir,
242  int doInterp, fpreal cx, fpreal cy, fpreal phase,
243  fpreal radius);
244 
245  void getColorAndMultiply(fpreal pos, int do_interp,
246  UT_RGBA *pixel);
247  void getColorAndMultiply(fpreal pos, int do_interp,
248  UT_BIG_RGBA *pixel);
249 
250  void interpColor(fpreal pos, int i1, float vals[4],
251  int order = 0) const;
252 
253  void updateSplineIfNeeded() const;
254  void evaluateSpline(fpreal pos, fpreal t0, fpreal t1,
255  int i, float *val, int order = 0) const;
256 
257 private:
258 
259  typedef tbb::spin_mutex SplineLock;
260 
261  UT_Array<UT_ColorNode> myNodes;
262  UT_ColorNode *myCurrentNode;
263 
264  UT_Map<int, UT_ColorNode *> mySelectedNodes;
265 
266  UT_ColorType myColorType;
267 
268  mutable UT_Spline mySpline;
269  mutable bool mySplineUpToDate;
270  mutable SplineLock mySplineLock;
271 
272  bool myGlobalEnable;
273 };
274 
275 /// Class to create a UT_Ramp from an OP node path and parameter string.
277 {
278 public:
280  virtual ~UT_RampOpResolver();
281 
282  /// Fetch a UT_Ramp from an OP node's ramp parm. May return false
283  /// if the OP or parm is not found, or if the parm is not a ramp.
284  /// is_rgb, op_id, and op_version are optional return values for the type
285  /// of ramp, the parent node's unique ID, and the parent node's version
286  /// parm serial (as reported by getVersionParms()
287  virtual bool resolveRampParm(UT_Ramp &target_ramp,
288  const char *op_path,
289  const char *ramp_parm,
290  bool *is_rgb = NULL,
291  int *op_id = NULL,
292  int *op_version = NULL);
293 };
294 
295 #endif
UT_ColorNode * getRamp()
Definition: UT_Ramp.h:114
GLboolean enable
Definition: glew.h:2750
GLuint GLdouble GLdouble GLint GLint order
Definition: glew.h:3460
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:106
UT_ColorNode()
Definition: UT_Ramp.h:55
GLuint color
Definition: glcorearb.h:1260
JSON reader class which handles parsing of JSON or bJSON files.
Definition: UT_JSONParser.h:76
#define UT_API
Definition: UT_API.h:14
GLuint GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat t1
Definition: glew.h:12900
GLuint GLdouble u1
Definition: glew.h:3460
UT_SPLINE_BASIS
Definition: UT_Spline.h:31
float fpreal32
Definition: SYS_Types.h:200
GLdouble GLdouble t
Definition: glew.h:1403
float t
Definition: UT_Ramp.h:73
GLenum GLsizei GLsizei GLint * values
Definition: glcorearb.h:1601
GLsizeiptr size
Definition: glcorearb.h:663
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:276
bool operator==(const BaseDimensions< T > &a, const BaseDimensions< Y > &b)
Definition: Dimensions.h:137
void setColorType(UT_ColorType color_space)
Definition: UT_Ramp.h:96
int getNodeCount() const
Definition: UT_Ramp.h:111
const UT_ColorNode * getRamp() const
Definition: UT_Ramp.h:116
GLsizei GLenum const void * indices
Definition: glcorearb.h:405
GLuint num
Definition: glew.h:2695
void setGlobalEnable(bool enable)
Definition: UT_Ramp.h:108
HUSD_API const char * raster()
long long int64
Definition: SYS_Types.h:116
GLfloat GLfloat p
Definition: glew.h:16656
UT_ColorType getColorType() const
Definition: UT_Ramp.h:98
GLuint GLfloat * val
Definition: glcorearb.h:1607
SerializableFRGBA rgba
Definition: UT_Ramp.h:74
UT_ColorType
Definition: UT_Color.h:24
UT_RampOrient
Definition: UT_Ramp.h:35
fpreal64 fpreal
Definition: SYS_Types.h:277
UT_SPLINE_BASIS basis
Definition: UT_Ramp.h:75
Utility class for containing a color ramp.
Definition: UT_Ramp.h:84
GLuint GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat t0
Definition: glew.h:12900
OIIO_API bool copy(string_view from, string_view to, std::string &err)
GLfloat f
Definition: glcorearb.h:1925
GLintptr offset
Definition: glcorearb.h:664
ImageBuf OIIO_API add(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
GLboolean r
Definition: glcorearb.h:1221