2012-08-05 23:29:43 +00: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
|
2020-05-09 17:14:35 +10:00
|
|
|
* along with this program; if not, write to the Free Software Foundation,
|
2012-08-05 23:29:43 +00:00
|
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2019-02-18 08:08:12 +11:00
|
|
|
/** \file
|
|
|
|
* \ingroup bli
|
2012-08-06 08:01:20 +00:00
|
|
|
*/
|
|
|
|
|
2014-06-30 11:44:28 +10:00
|
|
|
#include "BLI_compiler_attrs.h"
|
|
|
|
|
2017-06-06 12:13:45 +02:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2012-08-05 23:29:43 +00:00
|
|
|
typedef struct BLI_Stack BLI_Stack;
|
|
|
|
|
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 @Howard Trickey (howardt) opinion on the matter.
- The noexcept on the copy constructor of mpq(2|3) is being removed.
But according to @Jacques Lucke (JacquesLucke) it is not a real problem
for now.
I would like to give a huge thanks to @Jacques Lucke (JacquesLucke) who
helped during this and pushed me to reduce the duplication further.
Reviewed By: brecht, sergey, JacquesLucke
Differential Revision: http://developer.blender.org/D13791
2022-01-12 12:19:00 +01:00
|
|
|
BLI_Stack *BLI_stack_new_ex(const size_t elem_size,
|
2014-06-30 11:44:28 +10:00
|
|
|
const char *description,
|
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 @Howard Trickey (howardt) opinion on the matter.
- The noexcept on the copy constructor of mpq(2|3) is being removed.
But according to @Jacques Lucke (JacquesLucke) it is not a real problem
for now.
I would like to give a huge thanks to @Jacques Lucke (JacquesLucke) who
helped during this and pushed me to reduce the duplication further.
Reviewed By: brecht, sergey, JacquesLucke
Differential Revision: http://developer.blender.org/D13791
2022-01-12 12:19:00 +01:00
|
|
|
const size_t chunk_size) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
|
2021-12-09 20:01:44 +11:00
|
|
|
/**
|
|
|
|
* Create a new homogeneous stack with elements of 'elem_size' bytes.
|
|
|
|
*/
|
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 @Howard Trickey (howardt) opinion on the matter.
- The noexcept on the copy constructor of mpq(2|3) is being removed.
But according to @Jacques Lucke (JacquesLucke) it is not a real problem
for now.
I would like to give a huge thanks to @Jacques Lucke (JacquesLucke) who
helped during this and pushed me to reduce the duplication further.
Reviewed By: brecht, sergey, JacquesLucke
Differential Revision: http://developer.blender.org/D13791
2022-01-12 12:19:00 +01:00
|
|
|
BLI_Stack *BLI_stack_new(const size_t elem_size, const char *description) ATTR_WARN_UNUSED_RESULT
|
2014-07-15 20:37:06 +10:00
|
|
|
ATTR_NONNULL();
|
2012-08-05 23:29:43 +00:00
|
|
|
|
2021-12-09 20:01:44 +11:00
|
|
|
/**
|
|
|
|
* Free the stack's data and the stack itself
|
|
|
|
*/
|
2014-06-30 11:44:28 +10:00
|
|
|
void BLI_stack_free(BLI_Stack *stack) ATTR_NONNULL();
|
2012-08-05 23:29:43 +00:00
|
|
|
|
2021-12-09 20:01:44 +11:00
|
|
|
/**
|
|
|
|
* Push a new item onto the stack.
|
|
|
|
*
|
|
|
|
* \return a pointer #BLI_Stack.elem_size
|
|
|
|
* bytes of uninitialized memory (caller must fill in).
|
|
|
|
*/
|
2014-07-15 20:37:06 +10:00
|
|
|
void *BLI_stack_push_r(BLI_Stack *stack) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
|
2021-12-09 20:01:44 +11:00
|
|
|
/**
|
|
|
|
* Copies the source value onto the stack
|
|
|
|
*
|
|
|
|
* \note This copies #BLI_Stack.elem_size bytes from \a src,
|
|
|
|
* (the pointer itself is not stored).
|
|
|
|
*
|
|
|
|
* \param src: source data to be copied to the stack.
|
|
|
|
*/
|
2014-07-15 20:37:06 +10:00
|
|
|
void BLI_stack_push(BLI_Stack *stack, const void *src) ATTR_NONNULL();
|
2012-08-05 23:29:43 +00:00
|
|
|
|
2021-12-09 20:01:44 +11:00
|
|
|
/**
|
|
|
|
* A version of #BLI_stack_pop which fills in an array.
|
|
|
|
*
|
|
|
|
* \param dst: The destination array,
|
|
|
|
* must be at least (#BLI_Stack.elem_size * \a n) bytes long.
|
|
|
|
* \param n: The number of items to pop.
|
|
|
|
*
|
|
|
|
* \note The first item in the array will be last item added to the stack.
|
|
|
|
*/
|
2014-07-15 20:37:06 +10:00
|
|
|
void BLI_stack_pop_n(BLI_Stack *stack, void *dst, unsigned int n) ATTR_NONNULL();
|
2021-12-09 20:01:44 +11:00
|
|
|
/**
|
|
|
|
* A version of #BLI_stack_pop_n which fills in an array (in the reverse order).
|
|
|
|
*
|
|
|
|
* \note The first item in the array will be first item added to the stack.
|
|
|
|
*/
|
2015-06-19 17:40:35 +10:00
|
|
|
void BLI_stack_pop_n_reverse(BLI_Stack *stack, void *dst, unsigned int n) ATTR_NONNULL();
|
2021-12-09 20:01:44 +11:00
|
|
|
/**
|
|
|
|
* Retrieves and removes the top element from the stack.
|
|
|
|
* The value is copies to \a dst, which must be at least \a elem_size bytes.
|
|
|
|
*
|
|
|
|
* Does not reduce amount of allocated memory.
|
|
|
|
*/
|
2014-06-30 11:44:28 +10:00
|
|
|
void BLI_stack_pop(BLI_Stack *stack, void *dst) ATTR_NONNULL();
|
2012-08-05 23:29:43 +00:00
|
|
|
|
2014-09-28 13:24:01 +10:00
|
|
|
void *BLI_stack_peek(BLI_Stack *stack) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
|
2021-12-09 20:01:44 +11:00
|
|
|
/**
|
|
|
|
* Removes the top element from the stack.
|
|
|
|
*/
|
2014-09-28 13:24:01 +10:00
|
|
|
void BLI_stack_discard(BLI_Stack *stack) ATTR_NONNULL();
|
2021-12-09 20:01:44 +11:00
|
|
|
/**
|
|
|
|
* Discards all elements without freeing.
|
|
|
|
*/
|
2015-06-03 18:06:47 +10:00
|
|
|
void BLI_stack_clear(BLI_Stack *stack) ATTR_NONNULL();
|
2014-09-28 13:24:01 +10:00
|
|
|
|
2014-07-15 20:37:06 +10:00
|
|
|
size_t BLI_stack_count(const BLI_Stack *stack) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
|
2012-08-05 23:29:43 +00:00
|
|
|
|
2021-12-09 20:01:44 +11:00
|
|
|
/**
|
|
|
|
* Returns true if the stack is empty, false otherwise
|
|
|
|
*/
|
2014-07-15 20:37:06 +10:00
|
|
|
bool BLI_stack_is_empty(const BLI_Stack *stack) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
|
|
|
|
|
2017-06-06 12:13:45 +02:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|