Commit 644f1cbc authored by Philip Trettner's avatar Philip Trettner
Browse files

improved attribute interface

parent ab1c7279
......@@ -11,7 +11,6 @@ Best used with glm and glow.
* Tests
* std::less and std::hash for _index (and maybe _handle)
* attribute transformations (also between different types)
* lambda to attribute (from attribute to attribute or from make_attribute to attribute)
* Debug: store compactify generation in handles to check for invalidation
* Debug: insert is_removed assertions into handle access
* Switch primitives and valid_primitives, check if compact flag is inlined
......
......@@ -110,6 +110,11 @@ vertex_attribute<glm::vec3> obj_reader::positions_vec3() const
halfedge_attribute<glm::vec3> obj_reader::tex_coords_vec3() const { return tex_coords; }
halfedge_attribute<glm::vec2> obj_reader::tex_coords_vec2() const
{
return tex_coords.map([](glm::vec3 const &v) { return glm::vec2(v); });
}
halfedge_attribute<glm::vec3> obj_reader::normals_vec3() const { return tex_coords; }
void obj_reader::parse(std::istream &in, Mesh &mesh)
......
......@@ -52,6 +52,7 @@ public:
vertex_attribute<glm::vec4> positions_vec4() const;
vertex_attribute<glm::vec3> positions_vec3() const;
halfedge_attribute<glm::vec3> tex_coords_vec3() const;
halfedge_attribute<glm::vec2> tex_coords_vec2() const;
halfedge_attribute<glm::vec3> normals_vec3() const;
private:
......
......@@ -31,4 +31,14 @@ inline all_edge_collection primitive<edge_tag>::all_collection_of(Mesh &m) { ret
inline all_edge_const_collection primitive<edge_tag>::all_collection_of(const Mesh &m) { return m.all_edges(); }
inline all_halfedge_collection primitive<halfedge_tag>::all_collection_of(Mesh &m) { return m.all_halfedges(); }
inline all_halfedge_const_collection primitive<halfedge_tag>::all_collection_of(const Mesh &m) { return m.all_halfedges(); }
// primitive::valid_collection_of
inline valid_vertex_collection primitive<vertex_tag>::valid_collection_of(Mesh &m) { return m.vertices(); }
inline valid_vertex_const_collection primitive<vertex_tag>::valid_collection_of(const Mesh &m) { return m.vertices(); }
inline valid_face_collection primitive<face_tag>::valid_collection_of(Mesh &m) { return m.faces(); }
inline valid_face_const_collection primitive<face_tag>::valid_collection_of(const Mesh &m) { return m.faces(); }
inline valid_edge_collection primitive<edge_tag>::valid_collection_of(Mesh &m) { return m.edges(); }
inline valid_edge_const_collection primitive<edge_tag>::valid_collection_of(const Mesh &m) { return m.edges(); }
inline valid_halfedge_collection primitive<halfedge_tag>::valid_collection_of(Mesh &m) { return m.halfedges(); }
inline valid_halfedge_const_collection primitive<halfedge_tag>::valid_collection_of(const Mesh &m) { return m.halfedges(); }
}
......@@ -259,11 +259,27 @@ void smart_collection<mesh_ptr, tag, iterator>::reserve(int capacity) const
template <class mesh_ptr, class tag, class iterator>
template <class PropT>
typename primitive<tag>::template attribute<PropT> smart_collection<mesh_ptr, tag, iterator>::make_attribute(PropT const &def_value)
typename primitive<tag>::template attribute<PropT> smart_collection<mesh_ptr, tag, iterator>::make_attribute() const
{
return typename primitive<tag>::template attribute<PropT>(mesh, PropT());
}
template <class mesh_ptr, class tag, class iterator>
template <class PropT>
typename primitive<tag>::template attribute<PropT> smart_collection<mesh_ptr, tag, iterator>::make_attribute_default(PropT const &def_value) const
{
return typename primitive<tag>::template attribute<PropT>(mesh, def_value);
}
template <class mesh_ptr, class tag, class iterator>
template <class FuncT, class PropT>
typename primitive<tag>::template attribute<PropT> smart_collection<mesh_ptr, tag, iterator>::make_attribute(FuncT &&f, PropT const& def_value) const
{
auto attr = make_attribute_default<PropT>(def_value);
for (auto h : *this)
attr[h] = f(h);
return attr; // copy elison
}
template <class mesh_ptr, class tag, class iterator>
iterator smart_collection<mesh_ptr, tag, iterator>::begin() const
{
......
......@@ -31,6 +31,8 @@ struct primitive<vertex_tag>
static void reserve(Mesh& m, int capacity);
static all_collection all_collection_of(Mesh& m);
static all_const_collection all_collection_of(Mesh const& m);
static valid_collection valid_collection_of(Mesh& m);
static valid_const_collection valid_collection_of(Mesh const& m);
};
template <>
......@@ -57,6 +59,8 @@ struct primitive<face_tag>
static void reserve(Mesh& m, int capacity);
static all_collection all_collection_of(Mesh& m);
static all_const_collection all_collection_of(Mesh const& m);
static valid_collection valid_collection_of(Mesh& m);
static valid_const_collection valid_collection_of(Mesh const& m);
};
template <>
......@@ -83,6 +87,8 @@ struct primitive<edge_tag>
static void reserve(Mesh& m, int capacity);
static all_collection all_collection_of(Mesh& m);
static all_const_collection all_collection_of(Mesh const& m);
static valid_collection valid_collection_of(Mesh& m);
static valid_const_collection valid_collection_of(Mesh const& m);
};
template <>
......@@ -109,5 +115,7 @@ struct primitive<halfedge_tag>
static void reserve(Mesh& m, int capacity);
static all_collection all_collection_of(Mesh& m);
static all_const_collection all_collection_of(Mesh const& m);
static valid_collection valid_collection_of(Mesh& m);
static valid_const_collection valid_collection_of(Mesh const& m);
};
}
......@@ -115,6 +115,7 @@ struct smart_collection : smart_range<smart_collection<mesh_ptr, tag, iterator>,
{
template <class AttrT>
using attribute = typename primitive<tag>::template attribute<AttrT>;
using handle = typename primitive<tag>::handle;
/// Number of primitives, INCLUDING those marked for deletion
/// O(1) computation
......@@ -123,9 +124,15 @@ struct smart_collection : smart_range<smart_collection<mesh_ptr, tag, iterator>,
/// Ensures that a given number of primitives can be stored without reallocation
void reserve(int capacity) const;
/// Creates a new vertex attribute
/// Creates a new primitive attribute
template <class PropT>
attribute<PropT> make_attribute() const;
/// Creates a new primitive attribute with a given default value
template <class PropT>
attribute<PropT> make_attribute(PropT const& def_value = PropT());
attribute<PropT> make_attribute_default(PropT const& def_value) const;
/// Creates a new primitive attribute and initializes it with f(h) for each handle h
template <class FuncT, class PropT = tmp::decayed_result_type_of<FuncT, handle>>
attribute<PropT> make_attribute(FuncT&& f, PropT const& def_value = PropT()) const;
// Iteration:
iterator begin() const;
......@@ -398,4 +405,5 @@ bool primitive_ring<this_t, tag>::contains(handle v) const
return true;
return false;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment