Commit 8b89bacb authored by Philip Trettner's avatar Philip Trettner
Browse files

more convenience

parent 6e1920b9
......@@ -114,6 +114,11 @@ Scalar angle_sum(vertex_handle v, vertex_attribute<Vec3> const& position);
template <class Vec3, class Scalar = typename field_3d<Vec3>::Scalar>
Scalar angle_defect(vertex_handle v, vertex_attribute<Vec3> const& position);
/// efficiently computes the voronoi areas of all vertices
/// assumes triangle meshes for now
template <class Vec3, class Scalar = typename field_3d<Vec3>::Scalar>
vertex_attribute<Scalar> vertex_voronoi_areas(Mesh const& m, vertex_attribute<Vec3> const& position);
/// ======== IMPLEMENTATION ========
inline bool is_boundary(vertex_handle v) { return v.is_boundary(); }
......@@ -340,4 +345,20 @@ Vec3 bary_interpolate(face_handle f, Vec3 bary, vertex_attribute<Vec3> const& po
auto v2 = h.next().next().vertex_to()[position];
return v0 * bary[0] + v1 * bary[1] + v2 * bary[2];
}
template <class Vec3, class Scalar>
vertex_attribute<Scalar> vertex_voronoi_areas(Mesh const& m, vertex_attribute<Vec3> const& position)
{
vertex_attribute<Scalar> areas = m.vertices().make_attribute_with_default(Scalar(0));
for (auto f : m.faces())
{
Scalar a = face_area(f, position);
int v_cnt = f.vertices().size();
for (auto v : f.vertices())
areas[v] += a / v_cnt;
}
return areas;
}
}
......@@ -63,6 +63,9 @@ public:
/// applies to given function to each attribute entry (calls f(e))
template <class FuncT>
void apply(FuncT&& f);
/// sets each attribute to f(primitive)
template <class FuncT>
void compute(FuncT&& f);
/// copies as much data as possible from the given vector
void copy_from(std::vector<AttrT> const& data);
......@@ -74,6 +77,10 @@ public:
/// Saves ALL data into a vector (includes possibly removed ones)
std::vector<AttrT> to_vector() const;
// public ctor
public:
primitive_attribute(Mesh const& mesh, AttrT const& def_value = AttrT());
// data
protected:
attribute_data<AttrT> mData;
......
......@@ -283,6 +283,11 @@ void primitive_attribute_base<tag>::deregister_attr()
}
}
template <class tag, class AttrT>
primitive_attribute<tag, AttrT>::primitive_attribute(Mesh const &mesh, const AttrT &def_value) : primitive_attribute(&mesh, def_value)
{
}
template <class tag, class AttrT>
primitive_attribute<tag, AttrT>::primitive_attribute(const Mesh *mesh, const AttrT &def_value)
: primitive_attribute_base<tag>(mesh), mDefaultValue(def_value)
......@@ -330,4 +335,14 @@ void primitive_attribute<tag, AttrT>::apply(FuncT &&f)
for (auto i = 0; i < s; ++i)
f(d[i]);
}
template <class tag, class AttrT>
template <class FuncT>
void primitive_attribute<tag, AttrT>::compute(FuncT &&f)
{
auto s = size();
auto d = data();
for (auto h : primitive<tag>::valid_collection_of(*this->mMesh))
d[(int)h] = f(h);
}
}
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