HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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; }
108  void setGlobalEnable(bool enable)
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  int save(const char *pathname) const;
189  int save(std::ostream &os) const;
190 
191  UT_Ramp &operator=(const UT_Ramp &from);
192 
193  // Equality does not consider the selected or current node, only the ramp
194  // values themselves.
195  bool operator==(const UT_Ramp &from) const;
196 
197  void setRamp(const UT_ColorNode *r, int size);
198 
199  void rampLookup(fpreal pos, float values[4]) const;
200  void rampLookupNoInterp(fpreal pos, float values[4]) const;
201  void rampLookup(fpreal u0, fpreal u1, float values[4],
202  int ns) const;
203  void rampLookupNoInterp(fpreal u0, fpreal u1, float values[4],
204  int ns) const;
205 
206  template <typename ARCHIVER_T>
207  bool serialize(ARCHIVER_T &archiver);
208 
209  /// Install an OP node resolver for ramp parameters.
210  /// Required for resolveOpRamp() to work. OP_Director calls this.
211  static void setRampOpResolver(UT_RampOpResolver *resolver);
212 
213  /// Fetch the ramp from an OP node's ramp parm. May return false
214  /// if the OP or parm is not found, if the parm is not a ramp, or if the
215  /// UT_RampOpResolver is not assigned by setRampOpResolver().
216  /// is_rgb, op_id, and op_version are optional return values for the type
217  /// of ramp, the parent node's unique ID, and the parent node's version
218  /// parm serial (as reported by getVersionParms()).
219  bool resolveOpRamp(const char *op_path,
220  const char *ramp_parm,
221  bool *is_rgb = NULL,
222  int *op_id = NULL,
223  int *op_version = NULL);
224 
225 private:
226  bool loadAsPoly(UT_IStream &is);
227  bool loadAsJSON(UT_IStream &is);
228 
229  UT_RGBA *sampleColors (int nsamples, int doInterp);
230  UT_BIG_RGBA *sampleColors16(int nsamples, int doInterp);
231  void convertToRaster8 (UT_Raster *raster,UT_RampOrient dir,
232  int doInterp, fpreal cx, fpreal cy, fpreal phase,
233  fpreal radius);
234  void convertToRaster16(UT_Raster *raster,UT_RampOrient dir,
235  int doInterp, fpreal cx, fpreal cy, fpreal phase,
236  fpreal radius);
237 
238  void getColorAndMultiply(fpreal pos, int do_interp,
239  UT_RGBA *pixel);
240  void getColorAndMultiply(fpreal pos, int do_interp,
241  UT_BIG_RGBA *pixel);
242 
243  void interpColor(fpreal pos, int i1, float vals[4]) const;
244 
245  void updateSplineIfNeeded() const;
246  void evaluateSpline(fpreal pos, fpreal t0, fpreal t1,
247  int i, float *val) const;
248 
249 private:
250 
251  typedef tbb::spin_mutex SplineLock;
252 
253  UT_Array<UT_ColorNode> myNodes;
254  UT_ColorNode *myCurrentNode;
255 
256  UT_Map<int, UT_ColorNode *> mySelectedNodes;
257 
258  UT_ColorType myColorType;
259 
260  mutable UT_Spline mySpline;
261  mutable bool mySplineUpToDate;
262  mutable SplineLock mySplineLock;
263 
264  bool myGlobalEnable;
265 };
266 
267 /// Class to create a UT_Ramp from an OP node path and parameter string.
269 {
270 public:
272  virtual ~UT_RampOpResolver();
273 
274  /// Fetch a UT_Ramp from an OP node's ramp parm. May return false
275  /// if the OP or parm is not found, or if the parm is not a ramp.
276  /// is_rgb, op_id, and op_version are optional return values for the type
277  /// of ramp, the parent node's unique ID, and the parent node's version
278  /// parm serial (as reported by getVersionParms()
279  virtual bool resolveRampParm(UT_Ramp &target_ramp,
280  const char *op_path,
281  const char *ramp_parm,
282  bool *is_rgb = NULL,
283  int *op_id = NULL,
284  int *op_version = NULL);
285 };
286 
287 #endif
UT_ColorNode * getRamp()
Definition: UT_Ramp.h:114
GLsizei GLenum const void * indices
Definition: glcorearb.h:405
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
#define UT_API
Definition: UT_API.h:12
UT_SPLINE_BASIS
Definition: UT_Spline.h:25
png_uint_32 i
Definition: png.h:2877
GLsizeiptr size
Definition: glcorearb.h:663
float t
Definition: UT_Ramp.h:73
Class to create a UT_Ramp from an OP node path and parameter string.
Definition: UT_Ramp.h:268
bool operator==(const BaseDimensions< T > &a, const BaseDimensions< Y > &b)
Definition: Dimensions.h:137
long long int64
Definition: SYS_Types.h:106
GLfloat f
Definition: glcorearb.h:1925
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
double fpreal64
Definition: SYS_Types.h:191
GLintptr offset
Definition: glcorearb.h:664
void setGlobalEnable(bool enable)
Definition: UT_Ramp.h:108
GLenum GLsizei GLsizei GLint * values
Definition: glcorearb.h:1601
UT_ColorType getColorType() const
Definition: UT_Ramp.h:98
SerializableFRGBA rgba
Definition: UT_Ramp.h:74
double fpreal
Definition: SYS_Types.h:269
UT_ColorType
Definition: UT_Color.h:24
UT_RampOrient
Definition: UT_Ramp.h:35
UT_SPLINE_BASIS basis
Definition: UT_Ramp.h:75
Utility class for containing a color ramp.
Definition: UT_Ramp.h:84
GLuint GLfloat * val
Definition: glcorearb.h:1607
png_infop png_uint_32 int num
Definition: png.h:2158
GLboolean r
Definition: glcorearb.h:1221
float fpreal32
Definition: SYS_Types.h:190