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  void setColorAt(int i, float fvals[4]);
145  void setColorAt(int i, const UT_FRGBA &clr);
146  void setCurrentNodeColor(fpreal32 fvals[4]);
147  void setCurrentNodeColor(fpreal64 fvals[4]);
148  void setCurrentNodeColor(const UT_FRGBA &clr);
149 
150  void setCurrentNodeBasis(UT_SPLINE_BASIS basis);
151  void setNodeBasis(int i, UT_SPLINE_BASIS basis);
152 
153  void setEnable(int i, bool enable_pos, bool enable_val,
154  bool enable_interp);
155  bool getEnablePos(int i) const;
156  bool getEnableVal(int i) const;
157  bool getEnableInterp(int i) const;
158 
159  void clearSelection();
160  bool isNodeSelected(int i);
161  void selectNode(int i, bool add);
162  void selectRange(int first_i, int last_i, bool add);
163  void deselectNode(int i);
164  void toggleSelectNode(int i);
165  void getSelectionIndices(UT_IntArray &indices);
166  void setSelectionIndices(const UT_IntArray &indices);
167 
168  void addNode(fpreal pos);
169  void addNode(fpreal pos, fpreal32 fvals[4],
171  void addNode(fpreal pos, fpreal64 fvals[4],
173  void addNode(fpreal pos, const UT_FRGBA &color,
175 
176  bool deleteNode(fpreal pos);
177  bool deleteNodeAt(int i);
178  void deleteSelectedNodes();
179 
180  void moveNode(const UT_ColorNode *node, fpreal pos);
181 
182  // Move the node selection according to the delta of selected node_i's
183  // position to pos.
184  void moveNodeSelection(int node_i, fpreal pos);
185 
186  int load(const char *pathname);
187  bool load(UT_IStream &is);
188  bool load(UT_JSONParser &p);
189  int save(const char *pathname) const;
190  int save(std::ostream &os) const;
191 
192  UT_Ramp &operator=(const UT_Ramp &from);
193 
194  // Equality does not consider the selected or current node, only the ramp
195  // values themselves.
196  bool operator==(const UT_Ramp &from) const;
197 
198  void setRamp(const UT_ColorNode *r, int size);
199 
200  // For interpolated lookups, order > 0 finds the derivative wrt pos
201  void rampLookup(fpreal pos, float values[4],
202  int order = 0) const;
203  void rampLookupNoInterp(fpreal pos, float values[4]) const;
204  void rampLookup(fpreal u0, fpreal u1, float values[4],
205  int ns, int order = 0) const;
206  void rampLookupNoInterp(fpreal u0, fpreal u1, float values[4],
207  int ns) const;
208 
209  template <typename ARCHIVER_T>
210  bool serialize(ARCHIVER_T &archiver);
211 
212  /// Install an OP node resolver for ramp parameters.
213  /// Required for resolveOpRamp() to work. OP_Director calls this.
214  static void setRampOpResolver(UT_RampOpResolver *resolver);
215 
216  /// Fetch the ramp from an OP node's ramp parm. May return false
217  /// if the OP or parm is not found, if the parm is not a ramp, or if the
218  /// UT_RampOpResolver is not assigned by setRampOpResolver().
219  /// is_rgb, op_id, and op_version are optional return values for the type
220  /// of ramp, the parent node's unique ID, and the parent node's version
221  /// parm serial (as reported by getVersionParms()).
222  bool resolveOpRamp(const char *op_path,
223  const char *ramp_parm,
224  bool *is_rgb = NULL,
225  int *op_id = NULL,
226  int *op_version = NULL);
227 
228 private:
229  bool loadAsPoly(UT_IStream &is);
230  bool loadAsJSON(UT_JSONParser &p);
231 
232  UT_RGBA *sampleColors (int nsamples, int doInterp);
233  UT_BIG_RGBA *sampleColors16(int nsamples, int doInterp);
234  void convertToRaster8 (UT_Raster *raster,UT_RampOrient dir,
235  int doInterp, fpreal cx, fpreal cy, fpreal phase,
236  fpreal radius);
237  void convertToRaster16(UT_Raster *raster,UT_RampOrient dir,
238  int doInterp, fpreal cx, fpreal cy, fpreal phase,
239  fpreal radius);
240 
241  void getColorAndMultiply(fpreal pos, int do_interp,
242  UT_RGBA *pixel);
243  void getColorAndMultiply(fpreal pos, int do_interp,
244  UT_BIG_RGBA *pixel);
245 
246  void interpColor(fpreal pos, int i1, float vals[4],
247  int order = 0) const;
248 
249  void updateSplineIfNeeded() const;
250  void evaluateSpline(fpreal pos, fpreal t0, fpreal t1,
251  int i, float *val, int order = 0) const;
252 
253 private:
254 
255  typedef tbb::spin_mutex SplineLock;
256 
257  UT_Array<UT_ColorNode> myNodes;
258  UT_ColorNode *myCurrentNode;
259 
260  UT_Map<int, UT_ColorNode *> mySelectedNodes;
261 
262  UT_ColorType myColorType;
263 
264  mutable UT_Spline mySpline;
265  mutable bool mySplineUpToDate;
266  mutable SplineLock mySplineLock;
267 
268  bool myGlobalEnable;
269 };
270 
271 /// Class to create a UT_Ramp from an OP node path and parameter string.
273 {
274 public:
276  virtual ~UT_RampOpResolver();
277 
278  /// Fetch a UT_Ramp from an OP node's ramp parm. May return false
279  /// if the OP or parm is not found, or if the parm is not a ramp.
280  /// is_rgb, op_id, and op_version are optional return values for the type
281  /// of ramp, the parent node's unique ID, and the parent node's version
282  /// parm serial (as reported by getVersionParms()
283  virtual bool resolveRampParm(UT_Ramp &target_ramp,
284  const char *op_path,
285  const char *ramp_parm,
286  bool *is_rgb = NULL,
287  int *op_id = NULL,
288  int *op_version = NULL);
289 };
290 
291 #endif
UT_ColorNode * getRamp()
Definition: UT_Ramp.h:114
GLsizeiptr size
Definition: glew.h:1681
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 GLdouble u1
Definition: glew.h:3446
GLuint const GLfloat * val
Definition: glew.h:2794
GLint GLsizei const GLuint64 * values
Definition: glew.h:3612
JSON reader class which handles parsing of JSON or bJSON files.
Definition: UT_JSONParser.h:76
#define UT_API
Definition: UT_API.h:13
UT_SPLINE_BASIS
Definition: UT_Spline.h:25
float fpreal32
Definition: SYS_Types.h:200
GLuint GLdouble GLdouble GLint GLint order
Definition: glew.h:3446
float t
Definition: UT_Ramp.h:73
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:272
bool operator==(const BaseDimensions< T > &a, const BaseDimensions< Y > &b)
Definition: Dimensions.h:137
GLclampf f
Definition: glew.h:3499
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
GLuint GLuint GLsizei GLenum const void * indices
Definition: glew.h:1253
void setGlobalEnable(bool enable)
Definition: UT_Ramp.h:108
long long int64
Definition: SYS_Types.h:116
GLuint color
Definition: glew.h:7902
UT_ColorType getColorType() const
Definition: UT_Ramp.h:98
GLfloat GLfloat p
Definition: glew.h:16321
SerializableFRGBA rgba
Definition: UT_Ramp.h:74
UT_ColorType
Definition: UT_Color.h:24
UT_RampOrient
Definition: UT_Ramp.h:35
GLuint num
Definition: glew.h:2690
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 GLfloat GLfloat t1
Definition: glew.h:12681
GLdouble GLdouble GLdouble r
Definition: glew.h:1406
OIIO_API bool copy(string_view from, string_view to, std::string &err)
ImageBuf OIIO_API add(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
GLuint GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat t0
Definition: glew.h:12681
GLboolean enable
Definition: glew.h:2745
GLdouble GLdouble t
Definition: glew.h:1398
GLintptr offset
Definition: glew.h:1682