2015-02-14 07:28:15 +11:00
|
|
|
/*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2019-02-18 08:08:12 +11:00
|
|
|
/** \file
|
|
|
|
* \ingroup bli
|
|
|
|
* \brief Generic array manipulation API.
|
2015-02-14 07:28:15 +11:00
|
|
|
*/
|
|
|
|
|
2016-01-19 07:00:55 +11:00
|
|
|
#include "BLI_compiler_typecheck.h"
|
2020-03-06 16:45:06 +01:00
|
|
|
#include "BLI_sys_types.h"
|
2016-01-19 07:00:55 +11:00
|
|
|
|
2020-03-02 15:04:53 +01:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2021-12-09 20:01:44 +11:00
|
|
|
/**
|
|
|
|
* In-place array reverse.
|
|
|
|
*
|
|
|
|
* Access via #BLI_array_reverse
|
|
|
|
*/
|
2021-07-09 13:37:52 +10:00
|
|
|
void _bli_array_reverse(void *arr, uint arr_len, size_t arr_stride);
|
2015-02-14 07:28:15 +11:00
|
|
|
#define BLI_array_reverse(arr, arr_len) _bli_array_reverse(arr, arr_len, sizeof(*(arr)))
|
|
|
|
|
2021-12-09 20:01:44 +11:00
|
|
|
/**
|
|
|
|
* In-place array wrap.
|
|
|
|
* (rotate the array one step forward or backwards).
|
|
|
|
*
|
|
|
|
* Access via #BLI_array_wrap
|
|
|
|
*/
|
2021-07-09 13:37:52 +10:00
|
|
|
void _bli_array_wrap(void *arr, uint arr_len, size_t arr_stride, int dir);
|
2015-02-14 07:28:15 +11:00
|
|
|
#define BLI_array_wrap(arr, arr_len, dir) _bli_array_wrap(arr, arr_len, sizeof(*(arr)), dir)
|
|
|
|
|
2021-12-09 20:01:44 +11:00
|
|
|
/**
|
|
|
|
*In-place array permute.
|
|
|
|
* (re-arrange elements based on an array of indices).
|
|
|
|
*
|
|
|
|
* Access via #BLI_array_wrap
|
|
|
|
*/
|
2021-07-09 13:37:52 +10:00
|
|
|
void _bli_array_permute(
|
BLI: Refactor vector types & functions to use templates
This patch implements the vector types (i.e:`float2`) by making heavy
usage of templating. All vector functions are now outside of the vector
classes (inside the `blender::math` namespace) and are not vector size
dependent for the most part.
In the ongoing effort to make shaders less GL centric, we are aiming
to share more code between GLSL and C++ to avoid code duplication.
####Motivations:
- We are aiming to share UBO and SSBO structures between GLSL and C++.
This means we will use many of the existing vector types and others
we currently don't have (uintX, intX). All these variations were
asking for many more code duplication.
- Deduplicate existing code which is duplicated for each vector size.
- We also want to share small functions. Which means that vector
functions should be static and not in the class namespace.
- Reduce friction to use these types in new projects due to their
incompleteness.
- The current state of the `BLI_(float|double|mpq)(2|3|4).hh` is a
bit of a let down. Most clases are incomplete, out of sync with each
others with different codestyles, and some functions that should be
static are not (i.e: `float3::reflect()`).
####Upsides:
- Still support `.x, .y, .z, .w` for readability.
- Compact, readable and easilly extendable.
- All of the vector functions are available for all the vectors types
and can be restricted to certain types. Also template specialization
let us define exception for special class (like mpq).
- With optimization ON, the compiler unroll the loops and performance
is the same.
####Downsides:
- Might impact debugability. Though I would arge that the bugs are
rarelly caused by the vector class itself (since the operations are
quite trivial) but by the type conversions.
- Might impact compile time. I did not saw a significant impact since
the usage is not really widespread.
- Functions needs to be rewritten to support arbitrary vector length.
For instance, one can't call `len_squared_v3v3` in
`math::length_squared()` and call it a day.
- Type cast does not work with the template version of the `math::`
vector functions. Meaning you need to manually cast `float *` and
`(float *)[3]` to `float3` for the function calls.
i.e: `math::distance_squared(float3(nearest.co), positions[i]);`
- Some parts might loose in readability:
`float3::dot(v1.normalized(), v2.normalized())`
becoming
`math::dot(math::normalize(v1), math::normalize(v2))`
But I propose, when appropriate, to use
`using namespace blender::math;` on function local or file scope to
increase readability.
`dot(normalize(v1), normalize(v2))`
####Consideration:
- Include back `.length()` method. It is quite handy and is more C++
oriented.
- I considered the GLM library as a candidate for replacement. It felt
like too much for what we need and would be difficult to extend / modify
to our needs.
- I used Macros to reduce code in operators declaration and potential
copy paste bugs. This could reduce debugability and could be reverted.
- This touches `delaunay_2d.cc` and the intersection code. I would like
to know @howardt opinion on the matter.
- The `noexcept` on the copy constructor of `mpq(2|3)` is being removed.
But according to @JacquesLucke it is not a real problem for now.
I would like to give a huge thanks to @JacquesLucke who helped during this
and pushed me to reduce the duplication further.
Reviewed By: brecht, sergey, JacquesLucke
Differential Revision: https://developer.blender.org/D13791
2022-01-12 12:46:52 +01:00
|
|
|
void *arr, uint arr_len, const size_t arr_stride, const uint *order, void *arr_temp);
|
2015-04-28 04:10:31 +10:00
|
|
|
#define BLI_array_permute(arr, arr_len, order) \
|
|
|
|
_bli_array_permute(arr, arr_len, sizeof(*(arr)), order, NULL)
|
2018-08-01 16:12:36 +02:00
|
|
|
#define BLI_array_permute_ex(arr, arr_len, order, arr_temp) \
|
2015-04-28 04:10:31 +10:00
|
|
|
_bli_array_permute(arr, arr_len, sizeof(*(arr)), order, arr_temp)
|
|
|
|
|
2021-12-09 20:01:44 +11:00
|
|
|
/**
|
|
|
|
* In-place array de-duplication of an ordered array.
|
|
|
|
*
|
|
|
|
* \return The new length of the array.
|
|
|
|
*
|
|
|
|
* Access via #BLI_array_deduplicate_ordered
|
|
|
|
*/
|
2021-07-09 13:37:52 +10:00
|
|
|
uint _bli_array_deduplicate_ordered(void *arr, uint arr_len, size_t arr_stride);
|
2021-07-09 13:33:36 +10:00
|
|
|
#define BLI_array_deduplicate_ordered(arr, arr_len) \
|
|
|
|
_bli_array_deduplicate_ordered(arr, arr_len, sizeof(*(arr)))
|
|
|
|
|
2021-12-09 20:01:44 +11:00
|
|
|
/**
|
|
|
|
* Find the first index of an item in an array.
|
|
|
|
*
|
|
|
|
* Access via #BLI_array_findindex
|
|
|
|
*
|
|
|
|
* \note Not efficient, use for error checks/asserts.
|
|
|
|
*/
|
2021-07-09 13:37:52 +10:00
|
|
|
int _bli_array_findindex(const void *arr, uint arr_len, size_t arr_stride, const void *p);
|
2015-02-14 07:28:15 +11:00
|
|
|
#define BLI_array_findindex(arr, arr_len, p) _bli_array_findindex(arr, arr_len, sizeof(*(arr)), p)
|
|
|
|
|
2021-12-09 20:01:44 +11:00
|
|
|
/**
|
|
|
|
* A version of #BLI_array_findindex that searches from the end of the list.
|
|
|
|
*/
|
2021-07-09 13:37:52 +10:00
|
|
|
int _bli_array_rfindindex(const void *arr, uint arr_len, size_t arr_stride, const void *p);
|
2016-06-23 11:23:31 +10:00
|
|
|
#define BLI_array_rfindindex(arr, arr_len, p) \
|
|
|
|
_bli_array_rfindindex(arr, arr_len, sizeof(*(arr)), p)
|
|
|
|
|
2016-01-19 07:00:55 +11:00
|
|
|
void _bli_array_binary_and(
|
2021-07-09 13:37:52 +10:00
|
|
|
void *arr, const void *arr_a, const void *arr_b, uint arr_len, size_t arr_stride);
|
2016-01-19 07:00:55 +11:00
|
|
|
#define BLI_array_binary_and(arr, arr_a, arr_b, arr_len) \
|
|
|
|
(CHECK_TYPE_PAIR_INLINE(*(arr), *(arr_a)), \
|
|
|
|
CHECK_TYPE_PAIR_INLINE(*(arr), *(arr_b)), \
|
|
|
|
_bli_array_binary_and(arr, arr_a, arr_b, arr_len, sizeof(*(arr))))
|
|
|
|
|
|
|
|
void _bli_array_binary_or(
|
2021-07-09 13:37:52 +10:00
|
|
|
void *arr, const void *arr_a, const void *arr_b, uint arr_len, size_t arr_stride);
|
2016-01-19 07:00:55 +11:00
|
|
|
#define BLI_array_binary_or(arr, arr_a, arr_b, arr_len) \
|
|
|
|
(CHECK_TYPE_PAIR_INLINE(*(arr), *(arr_a)), \
|
|
|
|
CHECK_TYPE_PAIR_INLINE(*(arr), *(arr_b)), \
|
|
|
|
_bli_array_binary_or(arr, arr_a, arr_b, arr_len, sizeof(*(arr))))
|
|
|
|
|
2021-12-09 20:01:44 +11:00
|
|
|
/**
|
|
|
|
* Utility function to iterate over contiguous items in an array.
|
|
|
|
*
|
|
|
|
* \param use_wrap: Detect contiguous ranges across the first/last points.
|
|
|
|
* In this case the second index of \a span_step may be lower than the first,
|
|
|
|
* which indicates the values are wrapped.
|
|
|
|
* \param use_delimit_bounds: When false,
|
|
|
|
* ranges that defined by the start/end indices are excluded.
|
|
|
|
* This option has no effect when \a use_wrap is enabled.
|
|
|
|
* \param test_fn: Function to test if the item should be included in the range.
|
|
|
|
* \param user_data: User data for \a test_fn.
|
|
|
|
* \param span_step: Indices to iterate over,
|
|
|
|
* initialize both values to the array length to initialize iteration.
|
|
|
|
* \param r_span_len: The length of the span, useful when \a use_wrap is enabled,
|
|
|
|
* where calculating the length isn't a simple subtraction.
|
|
|
|
*/
|
2016-05-02 18:01:00 +10:00
|
|
|
bool _bli_array_iter_span(const void *arr,
|
2021-07-09 13:37:52 +10:00
|
|
|
uint arr_len,
|
2016-05-02 18:01:00 +10:00
|
|
|
size_t arr_stride,
|
|
|
|
bool use_wrap,
|
|
|
|
bool use_delimit_bounds,
|
|
|
|
bool (*test_fn)(const void *arr_item, void *user_data),
|
|
|
|
void *user_data,
|
2021-07-09 13:37:52 +10:00
|
|
|
uint span_step[2],
|
|
|
|
uint *r_span_len);
|
2018-03-28 08:59:26 +02:00
|
|
|
#define BLI_array_iter_span( \
|
2016-05-02 18:01:00 +10:00
|
|
|
arr, arr_len, use_wrap, use_delimit_bounds, test_fn, user_data, span_step, r_span_len) \
|
|
|
|
_bli_array_iter_span(arr, \
|
2018-03-28 08:59:26 +02:00
|
|
|
arr_len, \
|
2016-05-02 18:01:00 +10:00
|
|
|
sizeof(*(arr)), \
|
2018-03-28 08:59:26 +02:00
|
|
|
use_wrap, \
|
|
|
|
use_delimit_bounds, \
|
|
|
|
test_fn, \
|
|
|
|
user_data, \
|
2016-05-02 18:01:00 +10:00
|
|
|
span_step, \
|
|
|
|
r_span_len)
|
|
|
|
|
2021-12-09 20:01:44 +11:00
|
|
|
/**
|
|
|
|
* Simple utility to check memory is zeroed.
|
|
|
|
*/
|
2021-07-09 13:37:52 +10:00
|
|
|
bool _bli_array_is_zeroed(const void *arr, uint arr_len, size_t arr_stride);
|
2018-03-28 11:23:38 +02:00
|
|
|
#define BLI_array_is_zeroed(arr, arr_len) _bli_array_is_zeroed(arr, arr_len, sizeof(*(arr)))
|
2018-03-28 08:59:26 +02:00
|
|
|
|
2021-12-09 20:01:44 +11:00
|
|
|
/**
|
|
|
|
* Smart function to sample a rectangle spiraling outside.
|
|
|
|
* Nice for selection ID.
|
|
|
|
*
|
|
|
|
* \param arr_shape: dimensions [w, h].
|
|
|
|
* \param center: coordinates [x, y] indicating where to start traversing.
|
|
|
|
*/
|
2021-03-09 16:02:40 -03:00
|
|
|
bool _bli_array_iter_spiral_square(const void *arr_v,
|
|
|
|
const int arr_shape[2],
|
BLI: Refactor vector types & functions to use templates
This patch implements the vector types (i.e:`float2`) by making heavy
usage of templating. All vector functions are now outside of the vector
classes (inside the `blender::math` namespace) and are not vector size
dependent for the most part.
In the ongoing effort to make shaders less GL centric, we are aiming
to share more code between GLSL and C++ to avoid code duplication.
####Motivations:
- We are aiming to share UBO and SSBO structures between GLSL and C++.
This means we will use many of the existing vector types and others
we currently don't have (uintX, intX). All these variations were
asking for many more code duplication.
- Deduplicate existing code which is duplicated for each vector size.
- We also want to share small functions. Which means that vector
functions should be static and not in the class namespace.
- Reduce friction to use these types in new projects due to their
incompleteness.
- The current state of the `BLI_(float|double|mpq)(2|3|4).hh` is a
bit of a let down. Most clases are incomplete, out of sync with each
others with different codestyles, and some functions that should be
static are not (i.e: `float3::reflect()`).
####Upsides:
- Still support `.x, .y, .z, .w` for readability.
- Compact, readable and easilly extendable.
- All of the vector functions are available for all the vectors types
and can be restricted to certain types. Also template specialization
let us define exception for special class (like mpq).
- With optimization ON, the compiler unroll the loops and performance
is the same.
####Downsides:
- Might impact debugability. Though I would arge that the bugs are
rarelly caused by the vector class itself (since the operations are
quite trivial) but by the type conversions.
- Might impact compile time. I did not saw a significant impact since
the usage is not really widespread.
- Functions needs to be rewritten to support arbitrary vector length.
For instance, one can't call `len_squared_v3v3` in
`math::length_squared()` and call it a day.
- Type cast does not work with the template version of the `math::`
vector functions. Meaning you need to manually cast `float *` and
`(float *)[3]` to `float3` for the function calls.
i.e: `math::distance_squared(float3(nearest.co), positions[i]);`
- Some parts might loose in readability:
`float3::dot(v1.normalized(), v2.normalized())`
becoming
`math::dot(math::normalize(v1), math::normalize(v2))`
But I propose, when appropriate, to use
`using namespace blender::math;` on function local or file scope to
increase readability.
`dot(normalize(v1), normalize(v2))`
####Consideration:
- Include back `.length()` method. It is quite handy and is more C++
oriented.
- I considered the GLM library as a candidate for replacement. It felt
like too much for what we need and would be difficult to extend / modify
to our needs.
- I used Macros to reduce code in operators declaration and potential
copy paste bugs. This could reduce debugability and could be reverted.
- This touches `delaunay_2d.cc` and the intersection code. I would like
to know @howardt opinion on the matter.
- The `noexcept` on the copy constructor of `mpq(2|3)` is being removed.
But according to @JacquesLucke it is not a real problem for now.
I would like to give a huge thanks to @JacquesLucke who helped during this
and pushed me to reduce the duplication further.
Reviewed By: brecht, sergey, JacquesLucke
Differential Revision: https://developer.blender.org/D13791
2022-01-12 12:46:52 +01:00
|
|
|
const size_t elem_size,
|
2021-03-09 16:02:40 -03:00
|
|
|
const int center[2],
|
2021-03-10 15:35:43 +11:00
|
|
|
bool (*test_fn)(const void *arr_item, void *user_data),
|
2021-03-09 16:02:40 -03:00
|
|
|
void *user_data);
|
|
|
|
#define BLI_array_iter_spiral_square(arr, arr_shape, center, test_fn, user_data) \
|
|
|
|
_bli_array_iter_spiral_square(arr, arr_shape, sizeof(*(arr)), center, test_fn, user_data)
|
2020-03-02 15:04:53 +01:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|