PageRenderTime 22ms CodeModel.GetById 11ms app.highlight 8ms RepoModel.GetById 2ms app.codeStats 0ms

/src/core/MathUtils.h

http://github.com/imageworks/OpenColorIO
C++ Header | 189 lines | 82 code | 36 blank | 71 comment | 0 complexity | 1fcc6b9cd86cba2697c47a18090773ff MD5 | raw file
  1/*
  2Copyright (c) 2003-2010 Sony Pictures Imageworks Inc., et al.
  3All Rights Reserved.
  4
  5Redistribution and use in source and binary forms, with or without
  6modification, are permitted provided that the following conditions are
  7met:
  8* Redistributions of source code must retain the above copyright
  9  notice, this list of conditions and the following disclaimer.
 10* Redistributions in binary form must reproduce the above copyright
 11  notice, this list of conditions and the following disclaimer in the
 12  documentation and/or other materials provided with the distribution.
 13* Neither the name of Sony Pictures Imageworks nor the names of its
 14  contributors may be used to endorse or promote products derived from
 15  this software without specific prior written permission.
 16THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 17"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 18LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 19A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 20OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 21SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 22LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 23DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 24THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 25(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 26OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 27*/
 28
 29
 30#ifndef INCLUDED_OCIO_MATHUTILS_H
 31#define INCLUDED_OCIO_MATHUTILS_H
 32
 33#include <OpenColorIO/OpenColorIO.h>
 34
 35#include <cmath>
 36#include <vector>
 37
 38#include "Op.h"
 39#include "Platform.h"
 40
 41#ifdef WINDOWS
 42#include <float.h>
 43#endif
 44
 45OCIO_NAMESPACE_ENTER
 46{
 47    // From Imath
 48    //--------------------------------------------------------------------------
 49    // Compare two numbers and test if they are "approximately equal":
 50    //
 51    // equalWithAbsError (x1, x2, e)
 52    //
 53    //	Returns true if x1 is the same as x2 with an absolute error of
 54    //	no more than e,
 55    //	
 56    //	abs (x1 - x2) <= e
 57    //
 58    // equalWithRelError (x1, x2, e)
 59    //
 60    //	Returns true if x1 is the same as x2 with an relative error of
 61    //	no more than e,
 62    //	
 63    //	abs (x1 - x2) <= e * x1
 64    //
 65    //--------------------------------------------------------------------------
 66    
 67    inline bool equalWithAbsError (float x1, float x2, float e)
 68    {
 69        return ((x1 > x2)? x1 - x2: x2 - x1) <= e;
 70    }
 71    
 72    inline bool equalWithRelError (float x1, float x2, float e)
 73    {
 74        return ((x1 > x2)? x1 - x2: x2 - x1) <= e * ((x1 > 0)? x1: -x1);
 75    }
 76    
 77    inline float lerpf(float a, float b, float z)
 78    {
 79        return (b - a) * z + a;
 80    }
 81    
 82#ifdef WINDOWS
 83    inline double
 84    round (float val) {
 85        return floor (val + 0.5);
 86    }
 87
 88    inline float
 89    roundf (float val) {
 90        return static_cast<float>(round (val));
 91    }
 92
 93    inline int
 94    isnan (float val) {
 95        // Windows uses a non-standard version of 'isnan'
 96        return _isnan (val);
 97    }
 98#else
 99
100#ifdef ANDROID
101// support std::isnan - needs to be tested as it might not be part of the NDK
102#define _GLIBCXX_USE_C99_MATH 1
103#endif
104
105    // This lets all platforms just use isnan, within the OCIO namespace,
106    // across all platforms. (Windows defines the function above).
107    using std::isnan;
108#endif
109    
110    // Checks within fltmin tolerance
111    bool IsScalarEqualToZero(float v);
112    bool IsScalarEqualToOne(float v);
113    bool IsScalarEqualToZeroFlt(double v);
114    bool IsScalarEqualToOneFlt(double v);
115
116    // Are all the vector components the specified value?
117    bool IsVecEqualToZero(const float* v, int size);
118    bool IsVecEqualToOne(const float* v, int size);
119    bool IsVecEqualToOneFlt(const double* v, int size);
120
121    // Is at least one of the specified components equal to 0?
122    bool VecContainsZero(const float* v, int size);
123    bool VecContainsOne(const float* v, int size);
124    
125    // Are two vectors equal? (Same size, same values?)
126    bool VecsEqualWithRelError(const float* v1, int size1,
127                               const float* v2, int size2,
128                               float e);
129    
130    inline double GetHalfMax()
131    {
132        return 65504.0;         // Largest positive half
133    }
134    
135    inline double GetHalfMin()
136    {
137        return 5.96046448e-08;  // Smallest positive half;
138    }
139    
140    inline double GetHalfNormMin()
141    {
142        return 6.10351562e-05;  // Smallest positive normalized half
143    }
144    
145    //! Clamp the specified value to the valid range of normalized half.
146    // (can be either positive or negative though
147    
148    double ClampToNormHalf(double val);
149    
150    float GetSafeScalarInverse(float v, float defaultValue = 1.0);
151    
152    
153    // All matrix / vector operations use the following sizing...
154    //
155    // m : 4x4 matrix
156    // v : 4 column vector
157    
158    // Return the 4x4 inverse, and whether the inverse has succeeded.
159    // Supports in-place operations
160    bool GetM44Inverse(float* mout, const float* m);
161    
162    // Is an identity matrix? (with fltmin tolerance)
163    bool IsM44Identity(const float* m);
164    
165    // Is this a purely diagonal matrix?
166    bool IsM44Diagonal(const float* m);
167    
168    // Extract the diagonal
169    void GetM44Diagonal(float* vout, const float* m);
170    
171    // Get the product, out = m1*m2
172    // Supports in-place operations
173    void GetM44Product(float* mout, const float* m1, const float* m2);
174    
175    // Combine two transforms in the mx+b form, into a single transform
176    // mout*x+vout == m2*(m1*x+v1)+v2
177    // Supports in-place operations
178    void GetMxbCombine(float* mout, float* vout,
179                       const float* m1, const float* v1,
180                       const float* m2, const float* v2);
181    
182    // Supports in-place operations
183    bool GetMxbInverse(float* mout, float* vout,
184                       const float* m, const float* v);
185
186}
187OCIO_NAMESPACE_EXIT
188
189#endif