00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef _UT_Ramp_
00022 #define _UT_Ramp_
00023
00024 #include "UT_API.h"
00025
00026
00027
00028
00029 #include <iostream.h>
00030 #include <SYS/SYS_Types.h>
00031 #include "UT_AssocArray.h"
00032 #include "UT_RefArray.h"
00033 #include "UT_Color.h"
00034 #include "UT_Spline.h"
00035 #include "UT_VectorTypes.h"
00036
00037 class UT_Raster;
00038 class UT_RGBA;
00039 class UT_BIG_RGBA;
00040 class UT_IStream;
00041
00042 enum UT_RampOrient {
00043 UT_RAMP_HORIZONTAL,
00044 UT_RAMP_VERTICAL,
00045 UT_RAMP_RADIAL,
00046 UT_RAMP_CONCENTRIC
00047 };
00048
00049 class UT_API UT_FRGBA
00050 {
00051 public:
00052 UT_FRGBA() {};
00053 UT_FRGBA(fpreal32 red, fpreal32 green, fpreal32 blue, fpreal32 alpha)
00054 { r = red; g = green; b = blue; a = alpha; }
00055
00056 template <typename ARCHIVER_T>
00057 bool serialize(ARCHIVER_T &archiver)
00058 {
00059 return archiver.serializeUniformArray(&r, 4);
00060 }
00061
00062 fpreal32 r, g, b, a;
00063 };
00064
00065 class UT_API UT_ColorNode {
00066 public:
00067 UT_ColorNode() {}
00068 UT_ColorNode(fpreal offset, const UT_FRGBA &color,
00069 UT_SPLINE_BASIS basis_ = UT_SPLINE_LINEAR,
00070 bool enable_pos_ = true,
00071 bool enable_val_ = true,
00072 bool enable_interp_ = true)
00073 {
00074 t = offset;
00075 rgba = color;
00076 basis = basis_;
00077 enable_pos = enable_pos_;
00078 enable_val = enable_val_;
00079 enable_interp = enable_interp_;
00080 }
00081
00082 template <typename ARCHIVER_T>
00083 bool serialize(ARCHIVER_T &archiver);
00084
00085 float t;
00086 UT_FRGBA rgba;
00087 UT_SPLINE_BASIS basis;
00088 bool enable_pos:1,
00089 enable_val:1,
00090 enable_interp:1;
00091 };
00092
00093
00094
00095 class UT_API UT_Ramp {
00096 public:
00097 UT_Ramp();
00098 UT_Ramp(const UT_Ramp ©);
00099 ~UT_Ramp();
00100
00101 void setColorType(UT_ColorType color_space)
00102 { myColorType = color_space; }
00103 UT_ColorType getColorType() const
00104 { return myColorType; }
00105
00106 void clearAndDestroy();
00107
00108
00109
00110
00111 void ensureRampIsBuilt() { updateSpline(); }
00112
00113 bool getGlobalEnable() const
00114 { return myGlobalEnable; }
00115 void setGlobalEnable(bool enable)
00116 { myGlobalEnable = enable; }
00117
00118 int getNodeCount() const { return myNodes.entries(); }
00119 const UT_ColorNode *getNode(int i) const;
00120
00121 UT_ColorNode *getRamp()
00122 { return myNodes.entries() > 0 ? &myNodes(0) : 0; }
00123 const UT_ColorNode *getRamp() const
00124 { return myNodes.entries() > 0 ? &myNodes(0) : 0; }
00125
00126 void convertToRaster( UT_Raster *raster,
00127 UT_RampOrient dir,
00128 int doInterping = 1,
00129
00130
00131 fpreal cx = 0.0f,
00132 fpreal cy = 0.0f,
00133 fpreal phase = 0.0f,
00134
00135 fpreal radius = 1.0f);
00136
00137 const UT_ColorNode *getClosestNode(fpreal pos) const;
00138 int getClosestNodeIndex(fpreal pos) const;
00139
00140 const UT_ColorNode *getCurrentNode() const;
00141 int getCurrentNodeIndex() const;
00142 void setCurrentNodeIndex(int i, bool add_to_selection);
00143
00144 void getColor(fpreal pos, UT_FRGBA *clr,
00145 int doInterping = 1) const;
00146 void getColor(fpreal pos, float fvals[4],
00147 int doInterping = 1) const;
00148 void getColor(UT_FRGBA *values, int num,
00149 int doInterping = 1) const;
00150
00151 void setColorAt(int i, float fvals[4]);
00152 void setColorAt(int i, const UT_FRGBA &clr);
00153 void setCurrentNodeColor(fpreal32 fvals[4]);
00154 void setCurrentNodeColor(fpreal64 fvals[4]);
00155 void setCurrentNodeColor(const UT_FRGBA &clr);
00156
00157 void setCurrentNodeBasis(UT_SPLINE_BASIS basis);
00158 void setNodeBasis(int i, UT_SPLINE_BASIS basis);
00159
00160 void setEnable(int i, bool enable_pos, bool enable_val,
00161 bool enable_interp);
00162 bool getEnablePos(int i) const;
00163 bool getEnableVal(int i) const;
00164 bool getEnableInterp(int i) const;
00165
00166 void clearSelection();
00167 bool isNodeSelected(int i);
00168 void selectNode(int i, bool add);
00169 void selectRange(int first_i, int last_i, bool add);
00170 void deselectNode(int i);
00171 void toggleSelectNode(int i);
00172 void getSelectionIndices(UT_IntArray &indices);
00173 void setSelectionIndices(const UT_IntArray &indices);
00174
00175 void addNode(fpreal pos);
00176 void addNode(fpreal pos, fpreal32 fvals[4],
00177 UT_SPLINE_BASIS basis = UT_SPLINE_LINEAR);
00178 void addNode(fpreal pos, fpreal64 fvals[4],
00179 UT_SPLINE_BASIS basis = UT_SPLINE_LINEAR);
00180 void addNode(fpreal pos, const UT_FRGBA &color,
00181 UT_SPLINE_BASIS basis = UT_SPLINE_LINEAR);
00182
00183 bool deleteNode(fpreal pos);
00184 bool deleteNodeAt(int i);
00185 void deleteSelectedNodes();
00186
00187 void moveNode(const UT_ColorNode *node, fpreal pos);
00188
00189
00190
00191 void moveNodeSelection(int node_i, fpreal pos);
00192
00193 int load(const char *pathname);
00194 bool load(UT_IStream &is);
00195 int save(const char *pathname) const;
00196 int save(ostream &os) const;
00197
00198 UT_Ramp &operator=(const UT_Ramp &from);
00199
00200
00201
00202 bool operator==(const UT_Ramp &from) const;
00203
00204 void setRamp(const UT_ColorNode *r, int size);
00205
00206 void rampLookup(fpreal pos, float values[4]) const;
00207 void rampLookupNoInterp(fpreal pos, float values[4]) const;
00208 void rampLookup(fpreal u0, fpreal u1, float values[4],
00209 int ns) const;
00210 void rampLookupNoInterp(fpreal u0, fpreal u1, float values[4],
00211 int ns) const;
00212
00213 template <typename ARCHIVER_T>
00214 bool serialize(ARCHIVER_T &archiver);
00215
00216 private:
00217 bool loadAsPoly(UT_IStream &is);
00218 bool loadAsJSON(UT_IStream &is);
00219
00220 UT_RGBA *sampleColors (int nsamples, int doInterp);
00221 UT_BIG_RGBA *sampleColors16(int nsamples, int doInterp);
00222 void convertToRaster8 (UT_Raster *raster,UT_RampOrient dir,
00223 int doInterp, fpreal cx, fpreal cy, fpreal phase,
00224 fpreal radius);
00225 void convertToRaster16(UT_Raster *raster,UT_RampOrient dir,
00226 int doInterp, fpreal cx, fpreal cy, fpreal phase,
00227 fpreal radius);
00228
00229 void getColorAndMultiply(fpreal pos, int do_interp,
00230 UT_RGBA *pixel);
00231 void getColorAndMultiply(fpreal pos, int do_interp,
00232 UT_BIG_RGBA *pixel);
00233
00234 void interpColor(fpreal pos, int i1, float vals[4]) const;
00235
00236 void updateSpline() const;
00237 void evaluateSpline(fpreal pos, fpreal t0, fpreal t1,
00238 int i, float *val) const;
00239
00240 private:
00241 UT_RefArray<UT_ColorNode> myNodes;
00242 UT_ColorNode *myCurrentNode;
00243
00244 UT_AssocArray<int, UT_ColorNode *> mySelectedNodes;
00245
00246 UT_ColorType myColorType;
00247
00248 mutable UT_Spline mySpline;
00249 mutable bool mySplineDirty;
00250
00251 bool myGlobalEnable;
00252 };
00253
00254 #endif