23#ifndef VCL_ALGORITHMS_MESH_UPDATE_COLOR_H
24#define VCL_ALGORITHMS_MESH_UPDATE_COLOR_H
26#include <vclib/algorithms/mesh/stat.h>
28#include <vclib/mesh.h>
29#include <vclib/space/core.h>
43template<u
int ELEM_ID, MeshConcept MeshType>
44void setPerElemColorFromVertexColor(MeshType& m)
46 requirePerVertexColor(m);
47 requirePerElementComponent<ELEM_ID, CompId::VERTEX_REFERENCES>(m);
48 requirePerElementComponent<ELEM_ID, CompId::COLOR>(m);
50 for (
auto& e : m.template elements<ELEM_ID>()) {
51 Point4<uint> avg(0, 0, 0, 0);
53 avg += v->color().template cast<uint>();
55 avg /= e.vertexNumber();
56 e.color() = avg.template cast<uint8_t>();
60template<u
int ELEM_ID, MeshConcept MeshType>
61void setPerVertexColorFromElemColor(MeshType& m)
63 requirePerVertexColor(m);
64 requirePerElementComponent<ELEM_ID, CompId::VERTEX_REFERENCES>(m);
65 requirePerElementComponent<ELEM_ID, CompId::COLOR>(m);
67 std::vector<ColorAvgInfo> avgColors(m.vertexContainerSize());
69 for (
const auto& e : m.template elements<ELEM_ID>()) {
71 avgColors[v->index()].c += e.color().template cast<uint>();
72 avgColors[v->index()].cnt++;
77 if (avgColors[v.index()].cnt > 0) {
78 avgColors[v.index()].c /= avgColors[v.index()].cnt;
79 v.color() = avgColors[v.index()].c.template cast<uint8_t>();
84template<u
int ELEM_ID, MeshConcept MeshType>
85void setPerElemColorFromMaterial(MeshType& m)
88 HasMaterials<MeshType>,
89 "The input Mesh must have the Materials component.");
90 requirePerElementComponent<ELEM_ID, CompId::COLOR>(m);
91 requirePerElementComponent<ELEM_ID, CompId::MATERIAL_INDEX>(m);
93 for (
auto& e : m.template elements<ELEM_ID>()) {
94 uint matIndex = e.materialIndex();
95 if (matIndex < m.materialsNumber()) {
96 e.color() = m.materials()[matIndex].baseColor();
120template<MeshConcept MeshType>
123 Color c = Color::White,
126 requirePerVertexColor(
m);
129 std::ranges::fill(
m.vertices() | views::selected | views::colors, c);
132 std::ranges::fill(
m.vertices() | views::colors, c);
154template<FaceMeshConcept MeshType>
157 Color c = Color::White,
163 std::ranges::fill(
m.faces() | views::selected | views::colors, c);
166 std::ranges::fill(
m.faces() | views::colors, c);
188template<EdgeMeshConcept MeshType>
191 Color c = Color::White,
197 std::ranges::fill(
m.edges() | views::selected | views::colors, c);
200 std::ranges::fill(
m.edges() | views::colors, c);
220template<FaceMeshConcept MeshType>
223 detail::setPerVertexColorFromElemColor<ElemId::FACE>(
m);
242template<FaceMeshConcept MeshType>
245 detail::setPerElemColorFromVertexColor<ElemId::FACE>(
m);
264template<EdgeMeshConcept MeshType>
267 detail::setPerElemColorFromVertexColor<ElemId::EDGE>(
m);
294template<MeshConcept MeshType>
298 typename MeshType::VertexType::QualityType
minQuality = 0,
299 typename MeshType::VertexType::QualityType
maxQuality = 0)
301 requirePerVertexColor(
m);
302 requirePerVertexQuality(
m);
304 using VertexType = MeshType::VertexType;
305 using QualityType = VertexType::QualityType;
308 std::pair<QualityType, QualityType>
pair = vertexQualityMinMax(
m);
312 for (VertexType& v :
m.vertices()) {
342template<FaceMeshConcept MeshType>
346 typename MeshType::FaceType::QualityType
minQuality = 0,
347 typename MeshType::FaceType::QualityType
maxQuality = 0)
352 using FaceType = MeshType::FaceType;
353 using QualityType = FaceType::QualityType;
356 std::pair<QualityType, QualityType>
pair = faceQualityMinMax(
m);
361 for (FaceType& f :
m.faces()) {
383template<MeshConcept MeshType>
386 detail::setPerElemColorFromMaterial<ElemId::VERTEX>(
m);
405template<FaceMeshConcept MeshType>
408 detail::setPerElemColorFromMaterial<ElemId::FACE>(
m);
436template<FaceMeshConcept MeshType>
443 requirePerVertexColor(
m);
445 using FaceType = MeshType::FaceType;
447 const Color baseColor = Color::Green;
451 for (FaceType& f :
m.faces()) {
452 for (uint
i = 0;
i < f.vertexNumber(); ++
i) {
453 if (f.edgeOnBorder(
i)) {
454 if (f.vertex(
i)->color() == baseColor)
458 if (f.vertexMod(
i + 1)->color() == baseColor)
464 if (f.vertex(
i)->color() == baseColor)
468 if (f.vertexMod(
i + 1)->color() == baseColor)
495template<FaceMeshConcept MeshType>
528template<FaceMeshConcept MeshType>
562template<FaceMeshConcept MeshType>
570 using FaceType = MeshType::FaceType;
572 Color baseColor = Color::Black;
575 std::vector<Color> vc = colorScattering(
nColors);
577 for (FaceType& f :
m.faces()) {
578 if (f.color() == baseColor) {
579 f.color() = vc[
m.index(f) %
nColors];
583 for (uint
i = 0;
i < f.vertexNumber(); ++
i) {
585 assert(f.adjFace(
i) !=
nullptr);
586 f.adjFace(
i)->color() = f.color();
615template<MeshConcept MeshType, Po
int3Concept Po
intType>
619 const PointType& offset = PointType(0, 0, 0),
622 requirePerVertexColor(
m);
624 using VertexType = MeshType::VertexType;
628 for (VertexType& v :
m.vertices()) {
630 p[0] = (v.position() /
period[0]) + offset;
631 p[1] = (v.position() /
period[1]) + offset;
632 p[2] = (v.position() /
period[2]) + offset;
634 127 + 128.0 * perlinNoise(
p[0][0],
p[0][1],
p[0][2]),
635 127 + 128.0 * perlinNoise(
p[1][0],
p[1][1],
p[1][2]),
636 127 + 128.0 * perlinNoise(
p[2][0],
p[2][1],
p[2][2]),
660template<MeshConcept MeshType, Po
int3Concept Po
intType>
664 const PointType& offset = PointType(0, 0, 0),
669 requirePerVertexColor(
m);
671 using VertexType = MeshType::VertexType;
673 for (VertexType& v :
m.vertices()) {
675 PointType
p = v.position() /
period + offset;
677 double factor = (perlinNoise(
p[0],
p[1],
p[2]) + 1.0) / 2.0;
A class representing a box in N-dimensional space.
Definition box.h:46
The Color class represents a 32 bit color.
Definition color.h:48
ColorMap
List of Color Maps supported by the vcl::Color.
Definition color.h:126
Concept that checks if a Mesh has the per Face AdjacentFaces component.
Definition face_requirements.h:109
std::vector< std::set< uint > > connectedComponents(const MeshType &m)
Computes the connected components of the input mesh based on its topology.
Definition topology.h:791
void requirePerEdgeColor(const MeshType &m)
This function asserts that a Mesh has a EdgeContainer, the Edge has a Color Component,...
Definition edge_requirements.h:704
bool isPerFaceAdjacentFacesAvailable(const MeshType &m)
Returns true if the AdjacentFaces component is available (enabled) in the Face element of the input m...
Definition face_requirements.h:431
void requirePerFaceQuality(const MeshType &m)
This function asserts that a Mesh has a FaceContainer, the Face has a Quality Component,...
Definition face_requirements.h:1250
void requirePerFaceColor(const MeshType &m)
This function asserts that a Mesh has a FaceContainer, the Face has a Color Component,...
Definition face_requirements.h:1073
void setPerVertexColorFromFaceColor(MeshType &m)
Sets the vertex colors from its incident face colors, computing a plain average of the face colors.
Definition color.h:221
void setPerVertexColorFromMaterial(MeshType &m)
Sets the per-vertex color of a mesh according to a material assigned to each vertex.
Definition color.h:384
void setPerFaceColorScattering(MeshType &m, uint nColors=50, bool checkFauxEdges=true)
This function colors each face of the mesh using a given number of scattering colors (default number ...
Definition color.h:563
void setPerFaceColorFromMaterial(MeshType &m)
Sets the per-face color of a mesh according to a material assigned to each face.
Definition color.h:406
void setPerFaceColor(MeshType &m, Color c=Color::White, bool onlySelected=false)
Sets the color of the faces. If the onlySelected flag is set to true, only the color of the selected ...
Definition color.h:155
void setPerVertexColorFromQuality(MeshType &m, Color::ColorMap colorMap=Color::ColorMap::RedBlue, typename MeshType::VertexType::QualityType minQuality=0, typename MeshType::VertexType::QualityType maxQuality=0)
Sets the vertex colors from the quality values by computing a shading in the given color map (default...
Definition color.h:295
void setPerFaceColorFromQuality(MeshType &m, Color::ColorMap colorMap=Color::ColorMap::RedBlue, typename MeshType::FaceType::QualityType minQuality=0, typename MeshType::FaceType::QualityType maxQuality=0)
Sets the face colors from the quality values by computing a shading in the given color map (default i...
Definition color.h:343
void setPerFaceColorFromVertexColor(MeshType &m)
Sets the face colors from its incident vertex colors, computing a plain average of the vertex colors.
Definition color.h:243
void setPerEdgeColorFromVertexColor(MeshType &m)
Sets the edge colors from its incident vertex colors, computing a plain average of the vertex colors.
Definition color.h:265
void setPerVertexColor(MeshType &m, Color c=Color::White, bool onlySelected=false)
Sets the color of the vertices. If the onlySelected flag is set to true, only the color of the select...
Definition color.h:121
void setPerVertexPerlinColor(MeshType &m, double period, const PointType &offset=PointType(0, 0, 0), const Color &color1=Color::Black, const Color &color2=Color::White, bool onSelected=false)
Simple Perlin color mixing. color1 and color2 are mixed according the perlin noise function,...
Definition color.h:661
void setPerVertexColorPerlinNoise(MeshType &m, const PointType &period, const PointType &offset=PointType(0, 0, 0), bool onSelected=false)
Set the vertex color according to a perlin noise computed on the vertex positions.
Definition color.h:616
void setPerVertexColorFromFaceBorderFlag(MeshType &m, Color borderColor=Color::Blue, Color internalColor=Color::White, Color mixColor=Color::Cyan)
Color the vertices of the mesh that are on border, using the border flags of the faces.
Definition color.h:437
void setPerFaceColorFromConnectedComponents(MeshType &m, const std::vector< std::set< uint > > &connectedComponents)
Given an already computed vector of sets of connected components (see vcl::connectedComponents(m) in ...
Definition color.h:496
void setPerEdgeColor(MeshType &m, Color c=Color::White, bool onlySelected=false)
Sets the color of the edges. If the onlySelected flag is set to true, only the color of the selected ...
Definition color.h:189
constexpr detail::VerticesView vertices
A view that allows to iterate over the Vertex elements of an object.
Definition vertex.h:92