Commit Graph

30 Commits

Author SHA1 Message Date
Omar Emara 12d34fed91 BLI: Add step function to math library
This patch adds a step function that is equivalent to the GLSL step
function to the BLI math library.

Needed by #119414.

Pull Request: https://projects.blender.org/blender/blender/pulls/119731
2024-03-21 10:54:17 +01:00
Hans Goudey 9704d5e468 BLI: Add "numbers" math header, decouple C API
Adds a header that defines the same constants as the C++ 20
<numbers> header.

Benefits:
- Decouple our C++ and C math APIs
- Avoid using macros everywhere, nicer syntax
- Less header parsing during compilation
- Can be replaced by `std::numbers` with C++ 20

Downsides:
- There are fewer numbers defined in the C++ standard header
- Maybe we should just wait until we can use C++ 20

Pull Request: https://projects.blender.org/blender/blender/pulls/116805
2024-01-09 18:05:12 +01:00
Jeroen Bakker 37d04f030d Cleanup: Make format 2023-11-13 08:12:39 +01:00
Ray Molenkamp 3816fcd861 Fix: build error with MSVC
BLI_math_base.h is still required to get defines like M_PI
2023-11-12 08:36:39 -07:00
Hans Goudey 583bd9662e Cleanup: Remove C header from C++ math header
Remove some redundancy between headers, more fully
separate the C++ headers from the older C headers.

Pull Request: https://projects.blender.org/blender/blender/pulls/114705
2023-11-11 21:09:54 +01:00
Hans Goudey 70074a7d46 Cleanup: Follow style guide in new math function, use std:: namespace 2023-11-10 14:21:43 +01:00
Campbell Barton 58ea0e051f Cleanup: spelling in comments 2023-11-09 09:54:28 +11:00
Aras Pranckevicius b1c7b573c0 mesh: use faster acos() variant in normals calculation
Function                         Module   Inclusive Time  Exclusive Time
--------------------------------------------------------------------------
  mesh_render_data_update_normals  blender  297.51          0.00

  315 -> 297

acos() usage in all places related to normal calculations shows up in the
profiler. Given that "angle between faces" is only additional heuristic
weight in there (the effect of it at all is very subtle), approximate but
faster version of acos() might be just fine. Especially since some other
parts of Blender (e.g. mikktspace) use approximate acos in a conceptually
the same part.

- Adds safe_acos_approx() to BLI_math_base.hh. Implementation the same
  as already exists in Cycles; max error 0.00258 degrees. Between 2x and 4x
  faster in my tests.
- Changes all normals related calculations to use the function above instead
  of saacos.

Computing normals on a Stanford Lucy (14m verts) mesh:
- Mac (arm64, M1 Max): 247ms -> 229ms
- Win (x64, Ryzen 5950X): 276ms -> 250ms

All places that are about "normal calculation" were changed, including e.g.
Corrective Smooth modifier. Applying that one to the same 14m vertices mesh,
Mac M1 Max: 9.96s -> 9.76s

Tiny changes in several test output expectations w.r.t. normals are
observed, these were reviewed and updated expectations checked in svn.

Pull Request: https://projects.blender.org/blender/blender/pulls/114501
2023-11-07 18:22:17 +01:00
Omar Emara 110cb5d0d5 BLI: Add square function
This patch adds the square function for both float and vector math BLI
libraries. It also renames the corresponding GLSL function for
compatibility.

Pull Request: https://projects.blender.org/blender/blender/pulls/111217
2023-08-17 15:05:52 +02:00
Campbell Barton e955c94ed3 License Headers: Set copyright to "Blender Authors", add AUTHORS
Listing the "Blender Foundation" as copyright holder implied the Blender
Foundation holds copyright to files which may include work from many
developers.

While keeping copyright on headers makes sense for isolated libraries,
Blender's own code may be refactored or moved between files in a way
that makes the per file copyright holders less meaningful.

Copyright references to the "Blender Foundation" have been replaced with
"Blender Authors", with the exception of `./extern/` since these this
contains libraries which are more isolated, any changed to license
headers there can be handled on a case-by-case basis.

Some directories in `./intern/` have also been excluded:

- `./intern/cycles/` it's own `AUTHORS` file is planned.
- `./intern/opensubdiv/`.

An "AUTHORS" file has been added, using the chromium projects authors
file as a template.

Design task: #110784

Ref !110783.
2023-08-16 00:20:26 +10:00
Campbell Barton 65f99397ec License headers: use SPDX-FileCopyrightText in all sources 2023-06-15 13:35:34 +10:00
Sergey Sharybin 884b1e8cc3 BLI: Add math::exp() function
Covers both arithmetic and vectorized types.

Pull Request: https://projects.blender.org/blender/blender/pulls/108793
2023-06-09 10:33:29 +02:00
Sergey Sharybin 97197bd53e BLI: Add more per-element functions for vectors
This includes square root and reciprocal, and their safe versions.

For the reciprocal use name rcp, which matches Cycles and allows
to implement the same function for per-element operation on matrices.

Pull Request: https://projects.blender.org/blender/blender/pulls/108705
2023-06-07 15:15:37 +02:00
Sergey Sharybin db3a96492f BLI: Use math:: functions for per-element vector operations
Use math functions from blender::math for those operations, which
allows to device vectors of non-standard types.
2023-06-07 15:15:35 +02:00
Sergey Sharybin c1bc70b711 Cleanup: Add a copyright notice to files and use SPDX format
A lot of files were missing copyright field in the header and
the Blender Foundation contributed to them in a sense of bug
fixing and general maintenance.

This change makes it explicit that those files are at least
partially copyrighted by the Blender Foundation.

Note that this does not make it so the Blender Foundation is
the only holder of the copyright in those files, and developers
who do not have a signed contract with the foundation still
hold the copyright as well.

Another aspect of this change is using SPDX format for the
header. We already used it for the license specification,
and now we state it for the copyright as well, following the
FAQ:

    https://reuse.software/faq/
2023-05-31 16:19:06 +02:00
Hans Goudey 988f23cec3 Attributes: Add 2D integer vector attribute type
This type will be used to store mesh edges in #106638, but it could
be used for anything else too. This commit adds support for:
- The new type in the Python API
- Editing the type in the edit mode "Attribute Set" operator
- Rendering the type in EEVEE and Cycles for all geometry types
- Geometry nodes attribute interpolation and mixing
- Viewing the type in the spreadsheet and using row filters

The attribute uses the `blender::int2` type in most code, and
the `vec2i` DNA type in C code when necessary. The enum names
are based on `INT32_2D` for consistency with `INT8` and `INT32`.

Pull Request: https://projects.blender.org/blender/blender/pulls/106677
2023-04-14 16:08:05 +02:00
Campbell Barton d64dfc4333 Cleanup: spelling in comments 2023-03-10 12:58:10 +11:00
Clément Foucault 28a581d6cb BLI: Rotation C++ API
This patch re-implement the whole C rotation API into a more type
oriented C++ API. See the #104444 design task for more details about
the goals.

The list of C to C++ equivalent syntax can be found attached.

This adds `AngleRadian`, `AngleCartesian` and `AngleFraction` as
different angle types with the same interface. Each of them have
specific benefits / cons. See inline documentation for detail.

This adds `Axis` and `AxisSigned` class wrapped enums to increase type
safety with axes selection.

This adds `CartesianBasis` to represent orthonormal orientations.

Added a weight accumulation to dual-quaternions to make normalization
error proof. Creates the overhead of summing the total weight twice
(which I think is negligible) and add an extra float.

Named the dual-quaternion `DualQuaternion` to avoid naming ambiguity
with `DualQuat` which come up quite often (even with namespace).

Pull Request: https://projects.blender.org/blender/blender/pulls/104941
2023-03-09 18:15:22 +01:00
Clément Foucault 8b5d5cbf06 BLI: Math: Add sign() function
This implement the sign function as simple as possible while giving the
the same result as `signum`.
2023-01-15 14:15:39 +01:00
Clément Foucault 5fa694ffe1 Cleanup: BLI: Remove BLI_ENABLE_IF((is_math_float_type)) from vector API
This was limiting the use of the templates with other non internal types.
2023-01-06 21:59:14 +01:00
Clément Foucault 8a16523bf1 BLI: Refactor matrix types & functions to use templates
This patch implements the matrix types (i.e:float4x4) by making heavy
usage of templating. All matrix functions are now outside of the vector
classes (inside the blender::math namespace) and are not vector size
dependent for the most part.

###Motivations
The goal/motivations of this rewrite are the same as the Vector C++ API (D13791):
- Template everything for making it work with any types and avoid code duplication.
- Use functional style instead of Object Oriented function call to allow a simple compatibility layer with GLSL syntax (see T103026 for more details).
- Allow most convenient constructor syntax and accessors (array subscript `matrix[c][r]`, or component alias `matrix.y.z`).
- Make it cover all features the current C API supports for adoption.
- Keep compilation time and debug performance somehow acceptable.

###Consideration:
- The new `MatView` class can be generated by `my_float.view<NumCol, NumRow, StartCol, StartRow>()` (with the last 2 being optionnal). This one allows modifying parts of the source matrix in place. It isn't pretty and duplicates a lot of code, but it is needed mainly to replace `normalize_m4`. At least I think it is a good starting point that can refined further.
- An exhaustive list of missing `BLI_math_matrix.h` functions from the new API can be found here P3373.
- This adds new Rotation types in order to have a clean API. This will be extended when we port the full Rotation API. The types are made so that they don't allow implicit down-casting to their vector representation.
- Some functions make direct use of the Eigen library, bypassing the Eigen C API defined in `intern/eigen`. Its use is contained inside `math_matrix.cc`. There is conflicting opinion wether we should use it more so I contained its usage to almost the tasks as in the C API for now.

Reviewed By: sergey, JacquesLucke, HooglyBoogly, Severin, brecht
Differential Revision: https://developer.blender.org/D16625
2023-01-06 17:03:32 +01:00
Clément Foucault 2466b2e43c Cleanup: BLI: Rename arguments of math::atan2 2022-12-01 21:46:06 +01:00
Clément Foucault 730fd0a257 BLI: Add math::sqrt
Allows other number types to overload this function without poluting std
namespace.
2022-12-01 21:46:06 +01:00
Clément Foucault 19dc2157cd BLI: Add trigonometric functions to BLI_math_base.hh``
This is needed for the upcomming matrix library.
2022-11-30 12:59:47 +01:00
Jacques Lucke af983a3eef BLI: add min_inplace and max_inplace functions 2022-06-20 16:27:57 +02:00
Hans Goudey 3060b98842 Cleanup: Simplify dependencies for GMP math header
Previously, the base math headers included GMP headers in all cases.
This was problematic because we don't want all modules that use the
math headers to depend on GMP, and the unnecessary includes could
theoretically have detrimental effects to compile times.

Now `BLI_math_mpq.hh` depends on `BLI_math_base.hh`, so if a file
needs to use exact arithmatic, it can just include the former.

Differential Revision: https://developer.blender.org/D15079
2022-05-31 16:55:14 +02:00
Jacques Lucke 2e70af5cd5 Fix T97761: incorrect mixing of integers
Sometimes integers are mixed using float weights. In those cases
the mixed result has to be converted from into float again.
Previously, this was done using a simple cast, which was unexpected
because e.g. 14.999 would be cast to 14 instead of 15.
Now, the values are rounded properly.

This can affect existing files unfortunately without a good option
for versioning. Gladly, very few files seem to depend on the details
of the old behavior.

Differential Revision: https://developer.blender.org/D14892
2022-05-18 17:00:38 +02:00
Jacques Lucke b9799dfb8a Geometry Nodes: better support for byte color attributes
Since {rBeae36be372a6b16ee3e76eff0485a47da4f3c230} the distinction
between float and byte colors is more explicit in the ui. So far, geometry
nodes couldn't really deal with byte colors in general. This patch fixes that.
There is still only one color socket, which contains float colors. Conversion
to and from byte colors is done when read from or writing to attributes.

* Support writing to byte color attributes in Store Named Attribute node.
* Support converting to/from byte color in attribute conversion operator.
* Support propagating byte color attributes.
* Add all the implicit conversions from byte colors to the other types.
* Display byte colors as integers in spreadsheet.

Differential Revision: https://developer.blender.org/D14705
2022-04-21 16:11:26 +02:00
Hans Goudey 378022c797 BLI: Adjust interpolation to support integers, other tweaks
In order to allow interpolation of integers with a float, add a separate
template parameter for the factor and multiplication types.
Also move some helper constexpr variables to the "base" header
(reversing the dependency to "base" -> "vector").

This also adds a distance function for scalar types, which is
helpful to allow sharing code between vectors and basic types.

Differential Revision: https://developer.blender.org/D14446
2022-03-25 09:57:10 -05:00
Hans Goudey 399168f3c1 BLI: Implement templated math functions for basic types
This is meant to complement the `blender::math` functions recently
added by D13791. It's sometimes desired to template an operation to work
on vector types, but also basic types like `float` and `int`. This patch
adds that ability with a new `BLI_math_base.hh` header.

The existing vector math header is changed to use the `vec_base` type
more explicitly, to allow the compiler's generic function overload resolution
to determine which implementation of each math function to use.
This is a relatively large change, but it also makes the file significantly
easier to understand by reducing the use of macros.

Differential Revision: https://developer.blender.org/D14113
2022-02-16 10:28:26 -06:00