Visual Computing Library
Loading...
Searching...
No Matches
vertex_references.h
1/*****************************************************************************
2 * VCLib *
3 * Visual Computing Library *
4 * *
5 * Copyright(C) 2021-2025 *
6 * Visual Computing Lab *
7 * ISTI - Italian National Research Council *
8 * *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the Mozilla Public License Version 2.0 as published *
13 * by the Mozilla Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * Mozilla Public License Version 2.0 *
20 * (https://www.mozilla.org/en-US/MPL/2.0/) for more details. *
21 ****************************************************************************/
22
23#ifndef VCL_MESH_COMPONENTS_VERTEX_REFERENCES_H
24#define VCL_MESH_COMPONENTS_VERTEX_REFERENCES_H
25
26#include "bases/reference_container_component.h"
27
28#include <vclib/concepts/mesh/components/vertex_references.h>
29#include <vclib/types/view.h>
30
31namespace vcl::comp {
32
82template<
83 bool STORE_INDICES,
84 typename Vertex,
85 int N,
86 typename ParentElemType = void,
87 bool VERT = false>
90 STORE_INDICES,
91 VertexReferences<STORE_INDICES, Vertex, N, ParentElemType, VERT>,
92 CompId::VERTEX_REFERENCES,
93 Vertex,
94 N,
95 ParentElemType,
96 VERT,
97 false,
98 false>
99{
103 CompId::VERTEX_REFERENCES,
104 Vertex,
105 N,
107 VERT,
108 false,
109 false>;
110
111public:
116
117 /* Iterator Types declaration */
118
119 using VertexIterator = Base::Iterator;
120 using ConstVertexIterator = Base::ConstIterator;
121 using ConstVertexIndexIterator = Base::ConstIndexIterator;
122
123 static const bool INDEXED = STORE_INDICES;
124 static const int VERTEX_NUMBER = Base::SIZE;
125
126 /* Constructors */
127
135 VertexReferences() = default;
136
137 /* Member functions */
138
143 uint vertexNumber() const { return Base::size(); }
144
151 Vertex* vertex(uint i) { return Base::element(i); }
152
159 const Vertex* vertex(uint i) const { return Base::element(i); }
160
167 uint vertexIndex(uint i) const { return Base::elementIndex(i); }
168
187 Vertex* vertexMod(int i) { return Base::elementMod(i); }
188
195 const Vertex* vertexMod(int i) const { return Base::elementMod(i); }
196
216 uint vertexIndexMod(int i) const { return Base::elementIndexMod(i); }
217
224 void setVertex(uint i, Vertex* v) { Base::setElement(i, v); }
225
232 void setVertex(uint i, uint vi) { Base::setElement(i, vi); }
233
240 void setVertex(ConstVertexIterator it, Vertex* v)
241 {
242 Base::setElement(it, v);
243 }
244
251 void setVertex(ConstVertexIterator it, uint vi)
252 {
253 Base::setElement(it, vi);
254 }
255
262 void setVertex(ConstVertexIndexIterator it, Vertex* v)
263 {
264 Base::setElement(it, v);
265 }
266
273 void setVertex(ConstVertexIndexIterator it, uint vi)
274 {
275 Base::setElement(it, vi);
276 }
277
296 void setVertexMod(int i, Vertex* v) { Base::setElementMod(i, v); }
297
316 void setVertexMod(int i, uint vi) { Base::setElementMod(i, vi); }
317
329 template<Range Rng>
331 {
332 Base::setElements(r);
333 }
334
346 template<Range Rng>
348 {
349 Base::setElements(r);
350 }
351
360 bool containsVertex(const Vertex* v) const
361 {
362 return Base::containsElement(v);
363 }
364
373 bool containsVertex(uint vi) const { return Base::containsElement(vi); }
374
384 uint indexOfVertex(const Vertex* v) const
385 {
386 return Base::indexOfElement(v);
387 }
388
398 uint indexOfVertex(uint vi) const { return Base::indexOfElement(vi); }
399
416 uint indexOfEdge(const Vertex* v1, const Vertex* v2) const
417 {
418 if constexpr (STORE_INDICES) {
419 return indexOfEdge(
420 Base::indexFromPointer(v1), Base::indexFromPointer(v2));
421 }
422 else {
423 uint vid = indexOfVertex(v1);
424 if (vid == UINT_NULL) {
425 return UINT_NULL;
426 }
427 else if (vertexMod(vid + 1) == v2) {
428 return vid;
429 }
430 else if (vertexMod((int) vid - 1) == v2) {
431 int n = vertexNumber(); // n must be int to avoid unwanted casts
432 return (((int) vid - 1) % n + n) % n;
433 }
434 else {
435 return UINT_NULL;
436 }
437 }
438 }
439
457 {
458 if constexpr (STORE_INDICES) {
460 if (vid == UINT_NULL) {
461 return UINT_NULL;
462 }
463 else if (vertexIndexMod(vid + 1) == vi2) {
464 return vid;
465 }
466 else if (vertexIndexMod((int) vid - 1) == vi2) {
467 int n = vertexNumber(); // n must be int to avoid unwanted casts
468 return (((int) vid - 1) % n + n) % n;
469 }
470 else {
471 return UINT_NULL;
472 }
473 }
474 else {
475 return indexOfEdge(
476 Base::elemFromParent(vi1), Base::elemFromParent(vi2));
477 }
478 }
479
480 /* Member functions specific for dynamic vector of refs */
481
488 void resizeVertices(uint n) requires (N < 0) { Base::resize(n); }
489
497 void pushVertex(Vertex* v) requires (N < 0) { Base::pushBack(v); }
498
506 void pushVertex(uint vi) requires (N < 0) { Base::pushBack(vi); }
507
515 void insertVertex(uint i, Vertex* v) requires (N < 0)
516 {
517 Base::insert(i, v);
518 }
519
528 void insertVertex(uint i, uint vi) requires (N < 0) { Base::insert(i, vi); }
529
536 void eraseVertex(uint i) requires (N < 0) { Base::erase(i); }
537
543 void clearVertices() requires (N < 0) { Base::clear(); }
544
545 /* Iterator Member functions */
546
553 VertexIterator vertexBegin() { return Base::elementBegin(); }
554
561 VertexIterator vertexEnd() { return Base::elementEnd(); }
562
569 ConstVertexIterator vertexBegin() const { return Base::elementBegin(); }
570
577 ConstVertexIterator vertexEnd() const { return Base::elementEnd(); }
578
585 ConstVertexIndexIterator vertexIndexBegin() const
586 {
587 return Base::elementIndexBegin();
588 }
589
595 ConstVertexIndexIterator vertexIndexEnd() const
596 {
597 return Base::elementIndexEnd();
598 }
599
616 View<VertexIterator> vertices() { return Base::elements(); }
617
634 View<ConstVertexIterator> vertices() const { return Base::elements(); }
635
653 {
654 return Base::elementIndices();
655 }
656
657protected:
658 // Component interface functions
659 template<typename Element>
660 void importFrom(const Element& e, bool importRefs = true)
661 {
662 if (importRefs) {
663 if constexpr (HasVertexReferences<Element>) {
664 if constexpr (N > 0) {
665 // same size non-polygonal faces
666 if constexpr (N == Element::VERTEX_NUMBER) {
667 importIndicesFrom(e);
668 }
669 // from polygonal to fixed size, but the polygon size == the
670 // fixed face size
671 else if constexpr (Element::VERTEX_NUMBER < 0) {
672 if (e.vertexNumber() == N) {
673 importIndicesFrom(e);
674 }
675 }
676 else {
677 // do not import in this case: cannot import from a face
678 // of different size
679 }
680 }
681 else {
682 // from fixed to polygonal size: need to resize first, then
683 // import
684 resizeVertices(e.vertexNumber());
685 importIndicesFrom(e);
686 }
687 }
688 }
689 }
690
691 void serialize(std::ostream& os) const
692 {
693 // regardless of the type of the container (indices or pointers), the
694 // serialization is always done using the indices
695 if constexpr (N < 0) {
696 vcl::serialize(os, vertexNumber());
697 }
698 for (uint i = 0; i < vertexNumber(); ++i) {
699 vcl::serialize(os, vertexIndex(i));
700 }
701 }
702
703 void deserialize(std::istream& is)
704 {
705 if constexpr (N < 0) {
706 uint n;
707 vcl::deserialize(is, n);
709 }
710 for (uint i = 0; i < vertexNumber(); ++i) {
711 uint vi;
712 vcl::deserialize(is, vi);
713 setVertex(i, vi);
714 }
715 }
716
717private:
718 template<typename Element>
719 void importIndicesFrom(const Element& e)
720 {
721 for (uint i = 0; i < e.vertexNumber(); ++i) {
722 setVertex(i, e.vertexIndex(i));
723 }
724 }
725};
726
727} // namespace vcl::comp
728
729#endif // VCL_MESH_COMPONENTS_VERTEX_REFERENCES_H
The Element class.
Definition element.h:57
A class representing a line segment in n-dimensional space. The class is parameterized by a PointConc...
Definition segment.h:43
The Vertex class represents an Vertex element of the vcl::Mesh class.
Definition vertex.h:47
The ReferenceContainerComponent is a class that inherits from the IndexContainerComponent or PointerC...
Definition reference_container_component.h:99
The VertexReferences class represents a component that stores a container of indices or pointers to v...
Definition vertex_references.h:99
void setVertex(ConstVertexIterator it, uint vi)
Sets the vertex pointed by the iterator.
Definition vertex_references.h:251
Vertex * vertexMod(int i)
Returns a reference of the pointer to the i-th vertex of the element, but using as index the module b...
Definition vertex_references.h:187
void setVertex(uint i, uint vi)
Sets the i-th vertex of the element.
Definition vertex_references.h:232
void pushVertex(uint vi)
Pushes in the back of the container the given vertex.
Definition vertex_references.h:506
void setVertex(uint i, Vertex *v)
Sets the i-th vertex of the element.
Definition vertex_references.h:224
View< ConstVertexIterator > vertices() const
Returns a lightweight const view object that stores the begin and end iterators of the container of v...
Definition vertex_references.h:634
bool containsVertex(uint vi) const
Returns true if the container of vertices contains the vertex with the given index,...
Definition vertex_references.h:373
void setVertexMod(int i, Vertex *v)
Sets the i-th vertex of the element, but using as index the module between i and the number of vertic...
Definition vertex_references.h:296
void setVertex(ConstVertexIterator it, Vertex *v)
Sets the vertex pointed by the iterator.
Definition vertex_references.h:240
VertexReferences()=default
Empty constructor.
ConstVertexIterator vertexEnd() const
Returns a const iterator to the end of the container of this component.
Definition vertex_references.h:577
ConstVertexIndexIterator vertexIndexEnd() const
Returns an iterator to the end of the container of this component.
Definition vertex_references.h:595
void setVertices(Rng &&r)
Sets all the vertex pointers of the element.
Definition vertex_references.h:330
uint indexOfVertex(const Vertex *v) const
Returns the index of the given vertex in the container of the element. If the given vertex is not in ...
Definition vertex_references.h:384
uint indexOfEdge(const Vertex *v1, const Vertex *v2) const
Returns the index of the given edge composed of the two vertices v1 and v2 in the container of the el...
Definition vertex_references.h:416
ConstVertexIndexIterator vertexIndexBegin() const
Returns an iterator to the first vertex index in the container of this component.
Definition vertex_references.h:585
View< VertexIterator > vertices()
Returns a lightweight view object that stores the begin and end iterators of the container of vertice...
Definition vertex_references.h:616
void clearVertices()
Clears the container of vertices, making it empty.
Definition vertex_references.h:543
uint indexOfEdge(uint vi1, uint vi2) const
Returns the index of the edge composed of the two vertices with the given indices in the container of...
Definition vertex_references.h:456
void pushVertex(Vertex *v)
Pushes in the back of the container the given vertex.
Definition vertex_references.h:497
uint vertexNumber() const
Returns the number of vertices of the element.
Definition vertex_references.h:143
uint indexOfVertex(uint vi) const
Returns the index of the vertex with the given index in the container of the element....
Definition vertex_references.h:398
uint vertexIndex(uint i) const
Returns the index in the vertex container of the i-th vertex of the element.
Definition vertex_references.h:167
void resizeVertices(uint n)
Resize the container of the vertices to the given size.
Definition vertex_references.h:488
const Vertex * vertexMod(int i) const
Same of vertexMod, but returns a const pointer to the vertex.
Definition vertex_references.h:195
VertexIterator vertexBegin()
Returns an iterator to the first vertex in the container of this component.
Definition vertex_references.h:553
void eraseVertex(uint i)
Removes the vertex at the given position from the container.
Definition vertex_references.h:536
void setVertexMod(int i, uint vi)
Sets the i-th vertex of the element, but using as index the module between i and the number of vertic...
Definition vertex_references.h:316
void insertVertex(uint i, Vertex *v)
Inserts the given vertex in the container at the given position.
Definition vertex_references.h:515
ConstVertexIterator vertexBegin() const
Returns a const iterator to the first vertex in the container of this component.
Definition vertex_references.h:569
VertexIterator vertexEnd()
Returns an iterator to the end of the container of this component.
Definition vertex_references.h:561
View< ConstVertexIndexIterator > vertexIndices() const
Returns a lightweight view object that stores the begin and end iterators of the container of vertex ...
Definition vertex_references.h:652
void insertVertex(uint i, uint vi)
Inserts the vertex with the given index in the container at the given position.
Definition vertex_references.h:528
void setVertex(ConstVertexIndexIterator it, uint vi)
Sets the vertex pointed by the iterator.
Definition vertex_references.h:273
uint vertexIndexMod(int i) const
Returns the index in the vertex container of the i-th vertex of the element, but using as index the m...
Definition vertex_references.h:216
const Vertex * vertex(uint i) const
Returns a const pointer to the i-th vertex of the element.
Definition vertex_references.h:159
void setVertices(Rng &&r)
Sets all the vertex pointers of the element.
Definition vertex_references.h:347
void setVertex(ConstVertexIndexIterator it, Vertex *v)
Sets the vertex pointed by the iterator.
Definition vertex_references.h:262
bool containsVertex(const Vertex *v) const
Returns true if the container of vertices contains the given vertex, false otherwise.
Definition vertex_references.h:360
Vertex * vertex(uint i)
Returns the pointer to the i-th vertex of the element.
Definition vertex_references.h:151
Utility concept that is evaluated true the Range R is an Input Range and has a value_type that is con...
Definition range.h:89
HasVertexReferences concept is satisfied only if a Element class provides the types and member functi...
Definition vertex_references.h:43
constexpr uint UINT_NULL
The UINT_NULL value represent a null value of uint that is the maximum value that can be represented ...
Definition base.h:48