Skip to content

Instantly share code, notes, and snippets.

@mattnewport
Last active August 29, 2015 14:09
Show Gist options
  • Save mattnewport/3a46f68a7d2a87ea02bb to your computer and use it in GitHub Desktop.
Save mattnewport/3a46f68a7d2a87ea02bb to your computer and use it in GitHub Desktop.
Mesh with joint allocation using C++11 allocators
// Usage - with library support for joint allocation, usage might look something like this:
struct Vector3 {
float x, y, z;
};
struct Mesh {
private:
JointPool pool;
public:
template <typename ItPs, typename ItIs>
Mesh(ItPs firstP, ItPs lastP, ItIs firstI, ItIs lastI)
: pool(GetJointPoolSize<decltype(positions), decltype(indices)>(distance(firstP, lastP),
distance(firstI, lastI))),
positions(firstP, lastP, &pool),
indices(firstI, lastI, &pool) {}
Mesh(initializer_list<Vector3> ps, initializer_list<int> is)
: Mesh(begin(ps), end(ps), begin(is), end(is)) {}
JointVector<Vector3> positions;
JointVector<int> indices;
};
ostream& operator<<(ostream& os, const Mesh& m) {
for (const auto& pos : m.positions)
os << "(" << pos.x << ", " << pos.y << ", " << pos.z << "), ";
for (const auto& index : m.indices) os << index << ", ";
return os << endl;
}
int main() {
// initialize a Mesh with explicit verts and indices
Mesh myMesh{{{0.f, 0.f, 0.f}, {1.0f, 0.0f, 0.0f}, {0.0f, 1.0f, 0.0f}}, {0, 1, 2}};
// initialize a Mesh with iterator pairs
Mesh mySecondMesh{begin(myMesh.positions), end(myMesh.positions), begin(myMesh.indices),
end(myMesh.indices)};
cout << myMesh << mySecondMesh;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment