|
| 1 | +////////////////////////////////////////////////////////////////////////// |
| 2 | +// |
| 3 | +// Copyright (c) 2025, Image Engine Design Inc. All rights reserved. |
| 4 | +// |
| 5 | +// Redistribution and use in source and binary forms, with or without |
| 6 | +// modification, are permitted provided that the following conditions are |
| 7 | +// met: |
| 8 | +// |
| 9 | +// * Redistributions of source code must retain the above copyright |
| 10 | +// notice, this list of conditions and the following disclaimer. |
| 11 | +// |
| 12 | +// * Redistributions in binary form must reproduce the above copyright |
| 13 | +// notice, this list of conditions and the following disclaimer in the |
| 14 | +// documentation and/or other materials provided with the distribution. |
| 15 | +// |
| 16 | +// * Neither the name of Image Engine Design nor the names of any |
| 17 | +// other contributors to this software may be used to endorse or |
| 18 | +// promote products derived from this software without specific prior |
| 19 | +// written permission. |
| 20 | +// |
| 21 | +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
| 22 | +// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 23 | +// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 24 | +// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 25 | +// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 26 | +// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 27 | +// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 28 | +// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 29 | +// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 30 | +// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 31 | +// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 | +// |
| 33 | +////////////////////////////////////////////////////////////////////////// |
| 34 | + |
| 35 | +#ifndef IECORE_RAMP_H |
| 36 | +#define IECORE_RAMP_H |
| 37 | + |
| 38 | +#include "IECore/Export.h" |
| 39 | +#include "IECore/Spline.h" |
| 40 | +#include "IECore/MessageHandler.h" |
| 41 | + |
| 42 | +IECORE_PUSH_DEFAULT_VISIBILITY |
| 43 | +#include "Imath/ImathColor.h" |
| 44 | +IECORE_POP_DEFAULT_VISIBILITY |
| 45 | + |
| 46 | +#include <map> |
| 47 | + |
| 48 | +namespace IECore |
| 49 | +{ |
| 50 | + |
| 51 | +// This lives outside the class because we don't want multiple incompatible templated versions of |
| 52 | +// the same enum floating around |
| 53 | +enum class RampInterpolation |
| 54 | +{ |
| 55 | + Linear = 0, |
| 56 | + CatmullRom = 1, |
| 57 | + BSpline = 2, |
| 58 | + MonotoneCubic = 3, |
| 59 | + Constant = 4, |
| 60 | +}; |
| 61 | + |
| 62 | +/// A Ramp represents a spline-like curve as it is represented in a simple UI: with a set of independent |
| 63 | +/// control points, and an interpolation type selected from RampInterpolation. |
| 64 | +/// |
| 65 | +/// Rather than storing the lower level IECore::Spline*, we now store this Ramp type in shader networks, |
| 66 | +/// and only convert to the lower level class with the evaluator() function when evaluation is needed. |
| 67 | +/// |
| 68 | +/// This was chosen as superior to IECore::Spline* because IECoreS::spline* requires duplicating the |
| 69 | +/// end points in order to make the curve reach the first and last control point. |
| 70 | +template<typename X, typename Y> |
| 71 | +class IECORE_EXPORT Ramp |
| 72 | +{ |
| 73 | + |
| 74 | + public : |
| 75 | + |
| 76 | + using XType = X; |
| 77 | + using YType = Y; |
| 78 | + |
| 79 | + using PointContainer = std::multimap<X, Y>; |
| 80 | + using Point = typename PointContainer::value_type; |
| 81 | + |
| 82 | + Ramp() : interpolation( RampInterpolation::CatmullRom ) |
| 83 | + { |
| 84 | + } |
| 85 | + |
| 86 | + Ramp( const PointContainer &p, RampInterpolation i ) |
| 87 | + : points( p ), interpolation( i ) |
| 88 | + { |
| 89 | + } |
| 90 | + |
| 91 | + PointContainer points; |
| 92 | + RampInterpolation interpolation; |
| 93 | + |
| 94 | + |
| 95 | + // Convert to Cortex Spline |
| 96 | + // In the future, IECore::Spline may be replaced with IECore::SplineEvaluator, and this |
| 97 | + // function would be the only way to setup one. |
| 98 | + IECore::Spline<X, Y> evaluator() const; |
| 99 | + |
| 100 | + // Convert to and from a set of arguments that could be passed to a pair of spline() and |
| 101 | + // splineinverse() functions in OSL. This can be useful in converting ramps to parameters |
| 102 | + // for OSL shaders. |
| 103 | + // |
| 104 | + // Some shader libraries use these arguments directly as shader parameters ( i.e. Gaffer ). |
| 105 | + // Some shader libraries preprocess shader parameters before passing them to spline(), |
| 106 | + // so they don't need some aspects of this conversion ( like endpoint duplication ), but |
| 107 | + // the extra endpoint duplication doesn't cause problems ( i.e. PRMan ). |
| 108 | + // Some shader libraries are doing their own thing, implementing their own custom math, |
| 109 | + // but convention is still similar enough that these function can be a useful building |
| 110 | + // block in converting to something that mostly works ( i.e. 3delight ). |
| 111 | + void fromOSL( const std::string &basis, const std::vector<X> &positions, const std::vector<Y> &values, const std::string &identifier ); |
| 112 | + void toOSL( std::string &basis, std::vector<X> &positions, std::vector<Y> &values ) const; |
| 113 | + |
| 114 | + /// The number of times `toOSL()` repeats the initial point. |
| 115 | + int oslStartPointMultiplicity() const; |
| 116 | + |
| 117 | + // In Cortex 10.6 and earlier, shader parameters were represented uing IECore::Spline*Data instead of |
| 118 | + // IECore::Ramp*Data. This is used in converting SCC files to the new standard. |
| 119 | + // \todo : This can probably be removed in the next major version - we're not actually aware of any |
| 120 | + // significant users of Cortex who both use SCC files, and cache shaders, so this compatibility shim |
| 121 | + // is only needed theoretically. |
| 122 | + void fromDeprecatedSpline( const IECore::Spline<X, Y> &deprecated ); |
| 123 | + |
| 124 | + bool operator==( const Ramp<X,Y> &rhs ) const; |
| 125 | + bool operator!=( const Ramp<X,Y> &rhs ) const; |
| 126 | + |
| 127 | +}; |
| 128 | + |
| 129 | +using Rampff = Ramp<float, float>; |
| 130 | +using RampfColor3f = Ramp<float, Imath::Color3f>; |
| 131 | +using RampfColor4f = Ramp<float, Imath::Color4f>; |
| 132 | + |
| 133 | +template<typename X, typename Y> |
| 134 | +inline void murmurHashAppend( IECore::MurmurHash &h, const Ramp<X,Y> &data ) |
| 135 | +{ |
| 136 | + h.append( data.interpolation ); |
| 137 | + for ( auto &p : data.points ) |
| 138 | + { |
| 139 | + h.append( p.first ); |
| 140 | + h.append( p.second ); |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +} // namespace IECore |
| 145 | + |
| 146 | +#endif // IECORE_RAMP_H |
0 commit comments