- 
          
 - 
                Notifications
    
You must be signed in to change notification settings  - Fork 210
 
          invlerp and remap implementation
          #2654
        
          New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 17 commits
1975b07
              3e6fe92
              4f9ca04
              1b196b8
              7e20fb8
              66c817f
              cf51bf6
              a291038
              f5524ef
              f557983
              5f41e95
              0f552cd
              3f83b7f
              d2b053b
              969b2b0
              3868b04
              386ab80
              99d0682
              93aa1a9
              43dc68f
              849b739
              8960d02
              a542c8f
              3078c72
              dad6326
              5bcdf35
              1ce7019
              cbf66ef
              bbcdeef
              64461ed
              a87b274
              a27e44d
              fce1693
              ff7dc3d
              85e64e3
              964fdbd
              9d483ea
              File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
                              
      
                  bilhox marked this conversation as resolved.
               
          
            Show resolved
            Hide resolved
         | 
            
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| 
          
            
          
           | 
    @@ -4192,6 +4192,18 @@ vector_elementwise(pgVector *vec, PyObject *_null) | |
| return (PyObject *)proxy; | ||
| } | ||
| 
     | 
||
| inline double | ||
| lerp(double a, double b, double v) | ||
| { | ||
| return a + (b - a) * v; | ||
| } | ||
                
      
                  bilhox marked this conversation as resolved.
               
          
            Show resolved
            Hide resolved
         | 
||
| 
     | 
||
| inline double | ||
| invlerp(double a, double b, double v) | ||
| { | ||
| return (v - a) / (b - a); | ||
| } | ||
| 
     | 
||
| static PyObject * | ||
| math_clamp(PyObject *self, PyObject *const *args, Py_ssize_t nargs) | ||
| { | ||
| 
          
            
          
           | 
    @@ -4233,6 +4245,81 @@ math_clamp(PyObject *self, PyObject *const *args, Py_ssize_t nargs) | |
| return value; | ||
| } | ||
| 
     | 
||
| static PyObject * | ||
| math_invlerp(PyObject *self, PyObject *const *args, Py_ssize_t nargs) | ||
| { | ||
| if (nargs != 3) | ||
| return RAISE(PyExc_TypeError, | ||
| "invlerp requires exactly 3 numeric arguments"); | ||
| 
     | 
||
| PyObject *min = args[0]; | ||
| PyObject *max = args[1]; | ||
| PyObject *value = args[2]; | ||
| 
     | 
||
| if (!PyNumber_Check(min) || !PyNumber_Check(max) || !PyNumber_Check(value)) | ||
| return RAISE(PyExc_TypeError, | ||
| "invlerp requires all the arguments to be numbers"); | ||
                
       | 
||
| 
     | 
||
| double t = PyFloat_AsDouble(value); | ||
                
      
                  bilhox marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| 
     | 
||
| if (t < 0) | ||
| t = 0.0; | ||
| else if (t > 1) | ||
| t = 1.0; | ||
                
      
                  bilhox marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| 
     | 
||
| double a = PyFloat_AsDouble(min); | ||
| double b = PyFloat_AsDouble(max); | ||
| 
     | 
||
| if (PyErr_Occurred()) | ||
| return RAISE(PyExc_ValueError, | ||
| "invalid argument values passed to invlerp, numbers " | ||
| "might be too small or too big"); | ||
                
      
                  bilhox marked this conversation as resolved.
               
          
            Show resolved
            Hide resolved
         | 
||
| 
     | 
||
| if (b - a == 0) | ||
| return RAISE(PyExc_ZeroDivisionError, | ||
| "the result of b - a needs to be different from zero"); | ||
                
      
                  bilhox marked this conversation as resolved.
               
          
            Show resolved
            Hide resolved
         | 
||
| 
     | 
||
| return PyFloat_FromDouble(invlerp(a, b, t)); | ||
| } | ||
| 
     | 
||
| static PyObject * | ||
| math_remap(PyObject *self, PyObject *const *args, Py_ssize_t nargs) | ||
| { | ||
| if (nargs != 5) | ||
| return RAISE(PyExc_TypeError, | ||
| "remap requires exactly 5 numeric arguments"); | ||
| 
     | 
||
| PyObject *i_min = args[0]; | ||
| PyObject *i_max = args[1]; | ||
| PyObject *o_min = args[2]; | ||
| PyObject *o_max = args[3]; | ||
| PyObject *value = args[4]; | ||
| 
     | 
||
| if (!PyNumber_Check(value) || !PyNumber_Check(i_min) || | ||
| !PyNumber_Check(i_max) || !PyNumber_Check(o_min) || | ||
| !PyNumber_Check(o_max)) | ||
| return RAISE(PyExc_TypeError, | ||
| "remap requires all the arguments to be numbers"); | ||
| 
     | 
||
| double v = PyFloat_AsDouble(value); | ||
                
      
                  bilhox marked this conversation as resolved.
               
          
            Show resolved
            Hide resolved
         | 
||
| double a = PyFloat_AsDouble(i_min); | ||
| double b = PyFloat_AsDouble(i_max); | ||
| double c = PyFloat_AsDouble(o_min); | ||
| double d = PyFloat_AsDouble(o_max); | ||
                
      
                  bilhox marked this conversation as resolved.
               
          
            Show resolved
            Hide resolved
         | 
||
| 
     | 
||
| if (PyErr_Occurred()) | ||
| return RAISE(PyExc_ValueError, | ||
| "invalid argument values passed to remap, numbers might " | ||
| "be too small or too big"); | ||
| 
     | 
||
| if (b - a == 0) | ||
| return RAISE( | ||
| PyExc_ZeroDivisionError, | ||
| "the result of i_max - i_min needs to be different from zero"); | ||
| 
     | 
||
| return PyFloat_FromDouble(lerp(c, d, invlerp(a, b, v))); | ||
| } | ||
| 
     | 
||
| static PyObject * | ||
| math_lerp(PyObject *self, PyObject *const *args, Py_ssize_t nargs) | ||
| { | ||
| 
          
            
          
           | 
    @@ -4343,6 +4430,8 @@ math_disable_swizzling(pgVector *self, PyObject *_null) | |
| static PyMethodDef _math_methods[] = { | ||
| {"clamp", (PyCFunction)math_clamp, METH_FASTCALL, DOC_MATH_CLAMP}, | ||
| {"lerp", (PyCFunction)math_lerp, METH_FASTCALL, DOC_MATH_LERP}, | ||
| {"invlerp", (PyCFunction)math_invlerp, METH_FASTCALL, DOC_MATH_INVLERP}, | ||
| {"remap", (PyCFunction)math_remap, METH_FASTCALL, DOC_MATH_REMAP}, | ||
| {"smoothstep", (PyCFunction)math_smoothstep, METH_FASTCALL, | ||
| DOC_MATH_SMOOTHSTEP}, | ||
| {"enable_swizzling", (PyCFunction)math_enable_swizzling, METH_NOARGS, | ||
| 
          
            
          
           | 
    ||
Uh oh!
There was an error while loading. Please reload this page.