Commit eba11f32 authored by Philip Trettner's avatar Philip Trettner
Browse files

attribute map

parent be3a0c84
......@@ -1983,12 +1983,14 @@ void halfedge_attribute<AttrT>::clear()
template <class AttrT>
template <class FuncT>
auto vertex_attribute<AttrT>::map(FuncT f) const -> tmp::result_type_of<FuncT, AttrT>
auto vertex_attribute<AttrT>::map(FuncT f) const -> vertex_attribute<tmp::result_type_of<FuncT, AttrT>>
{
auto attr = mMesh->vertices().make_attribute<tmp::result_type_of<FuncT, AttrT>>();
auto s = size();
auto d_in = data();
auto d_out = attr.data();
for (auto i = 0; i < s; ++i)
attr.mData[i] = f(mData[i]);
d_out[i] = f(d_in[i]);
return attr; // copy elison
}
}
......@@ -33,8 +33,8 @@ public:
AttrT& operator[](vertex_index v) { return mData[v.value]; }
AttrT const& operator[](vertex_index v) const { return mData[v.value]; }
AttrT* data() { return mData; }
AttrT const* data() const { return mData; }
AttrT* data() { return mData.data; }
AttrT const* data() const { return mData.data; }
int size() const;
// methods
......@@ -44,7 +44,7 @@ public:
/// returns a new attribute where the given function was applied to each entry
template <class FuncT>
auto map(FuncT f) const -> tmp::result_type_of<FuncT, AttrT>;
auto map(FuncT f) const -> vertex_attribute<tmp::result_type_of<FuncT, AttrT>>;
/// applies to given function to each attribute entry
template <class FuncT>
void apply(FuncT f);
......@@ -61,6 +61,7 @@ private:
private:
vertex_attribute(Mesh const* mesh, AttrT const& def_value);
friend struct vertex_collection;
friend struct const_vertex_collection;
// move & copy
public:
......@@ -579,5 +580,4 @@ AttrT const& halfedge_handle::operator[](halfedge_attribute<AttrT> const* attr)
{
return (*attr)[*this];
}
}
......@@ -5,6 +5,16 @@
using namespace polymesh;
void polymesh::write_obj(const std::string &filename,
const Mesh &mesh,
const vertex_attribute<glm::vec3> &position,
const vertex_attribute<glm::vec2> *tex_coord,
const vertex_attribute<glm::vec3> *normal)
{
obj_writer obj(filename);
obj.write_mesh(mesh, position, tex_coord, normal);
}
obj_writer::obj_writer(const std::string &filename)
{
tmp_out = new std::ofstream(filename);
......@@ -80,16 +90,6 @@ void obj_writer::write_mesh(const Mesh &mesh,
}
}
void write_obj(const std::string &filename,
const Mesh &mesh,
const vertex_attribute<glm::vec3> &position,
const vertex_attribute<glm::vec2> *tex_coord,
const vertex_attribute<glm::vec3> *normal)
{
obj_writer obj(filename);
obj.write_mesh(mesh, position, tex_coord, normal);
}
obj_reader::obj_reader(const std::string &filename, Mesh &mesh)
: positions(mesh.vertices().make_attribute<glm::vec4>()),
tex_coords(mesh.halfedges().make_attribute<glm::vec3>()),
......@@ -110,6 +110,26 @@ obj_reader::obj_reader(std::istream &in, Mesh &mesh)
parse(in, mesh);
}
vertex_attribute<glm::vec4> obj_reader::positions_vec4() const
{
return positions;
}
vertex_attribute<glm::vec3> obj_reader::positions_vec3() const
{
return positions.map([](glm::vec4 const &v) { return glm::vec3(v); });
}
halfedge_attribute<glm::vec3> obj_reader::tex_coords_vec3() const
{
return tex_coords;
}
halfedge_attribute<glm::vec3> obj_reader::normals_vec3() const
{
return tex_coords;
}
void obj_reader::parse(std::istream &in, Mesh &mesh)
{
mesh.clear();
......
......@@ -49,9 +49,10 @@ struct obj_reader
// get properties of the obj
// NOTE: these always return fresh copies of the attribute!
public:
vertex_attribute<glm::vec4> positions_vec4() const { return positions; }
halfedge_attribute<glm::vec3> tex_coords_vec3() const { return tex_coords; }
halfedge_attribute<glm::vec3> normals_vec3() const { return tex_coords; }
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::vec3> normals_vec3() const;
private:
void parse(std::istream& in, Mesh& mesh);
......
......@@ -10,7 +10,7 @@ namespace tmp
template <class FuncT, class ArgT>
struct result_of
{
using type = typename std::decay<decltype((std::declval<FuncT>())(std::declval<ArgT>()))>::type;
using type = typename std::decay<decltype(std::declval<FuncT>()(std::declval<ArgT>()))>::type;
};
template <class FuncT, class ArgT>
......
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