(core): mesh iterator performs badly
Description
Newly introduced mesh iterator does not perform well. It holds a structure containing all possible attributes (position, normals, texture coordinates, RGB color, RGBA color). This leads to the introdution of many dynamic branches in the methods of the iterator to check if the attributes are present. These checks are made for each point, for instance for operator*
or operator++
. This is a real shame since the user knows right at the construction which attributes he wants to use.
Proposal
Sadly, we must deprecate this new iterator. We must propose a new one that allows to specify only the attributes needed. This is a bit more work but this should be achievable using variadic templates and new C++17 fold expressions. Here is basically the principle:
#include <iostream>
struct Position {
float x;
float y;
float z;
Position() {printf("Position ctor\n");}
void display() {printf("Position display\n");}
Position& operator++() {printf("Position++\n"); return *this; }
};
struct Normal {
float nx;
float ny;
float nz;
Normal() {printf("Normal ctor\n");}
void display() {printf("Normal display\n");}
Normal& operator++() {printf("Normal++\n"); return *this; }
};
/// Used to iterate through a RGBA image of type 'uint8'
struct RGBA {
std::uint8_t r;
std::uint8_t g;
std::uint8_t b;
std::uint8_t a;
RGBA() {printf("RGBA ctor\n");}
void display() {printf("RGBA display\n");}
RGBA& operator++() {printf("RGBA++\n"); return *this; }
};
template<class...A> struct iterator : public A...{
iterator(){
printf("container ctor\n");
}
void display()
{
(static_cast<void>(A::display()), ...);
}
iterator& operator++()
{
(static_cast<void>(A::operator++()), ...);
return *this;
}
};
typedef iterator<Position,Normal> PointNormal;
typedef iterator<Position,RGBA> PointRGBA;
typedef iterator<Position,Normal,RGBA> PointNormalRGBA;
int main(void)
{
PointNormal test;
PointRGBA test1;
PointNormalRGBA test2;
test.display();
++test1;
++test2;
return 0;
}
Outcomes
This is gonna be much more elegant and more performant.