Visual Computing Library  devel
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 "base/predicates.h"
27#include "base/reference_container_component.h"
28
29#include <vclib/base.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>
89 public ReferenceContainerComponent<
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{
100 using Base = ReferenceContainerComponent<
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
188
195 const Vertex* vertexMod(int i) const { return Base::elementMod(i); }
196
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
297
317
329 template<Range Rng>
331 {
333 }
334
346 template<Range Rng>
348 {
350 }
351
360 bool containsVertex(const Vertex* v) const
361 {
362 return Base::containsElement(v);
363 }
364
374
384 uint indexOfVertex(const Vertex* v) const
385 {
386 return Base::indexOfElement(v);
387 }
388
399
416 uint indexOfEdge(const Vertex* v1, const Vertex* v2) const
417 {
418 if constexpr (STORE_INDICES) {
419 return indexOfEdge(
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(
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 {
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
656
657protected:
658 // Component interface functions
659 template<typename Element>
660 void importFrom(const Element& e, bool importRefs = true);
661
662 void serialize(std::ostream& os) const
663 {
664 // regardless of the type of the container (indices or pointers), the
665 // serialization is always done using the indices
666 if constexpr (N < 0) {
667 vcl::serialize(os, vertexNumber());
668 }
669 for (uint i = 0; i < vertexNumber(); ++i) {
670 vcl::serialize(os, vertexIndex(i));
671 }
672 }
673
674 void deserialize(std::istream& is)
675 {
676 if constexpr (N < 0) {
677 uint n;
678 vcl::deserialize(is, n);
680 }
681 for (uint i = 0; i < vertexNumber(); ++i) {
682 uint vi;
683 vcl::deserialize(is, vi);
684 setVertex(i, vi);
685 }
686 }
687
688private:
689 template<typename Element>
690 void importIndicesFrom(const Element& e)
691 {
692 for (uint i = 0; i < e.vertexNumber(); ++i) {
693 setVertex(i, e.vertexIndex(i));
694 }
695 }
696};
697
698/* concept */
699
711template<typename T>
713 BTITB::IsDerivedFromSpecializationOfV<T, VertexReferences>;
714
715/* importFrom function */
716
717template<
718 bool STORE_INDICES,
719 typename Vertex,
720 int N,
721 typename ParentElemType,
722 bool VERT>
723template<typename Element>
724void VertexReferences<STORE_INDICES, Vertex, N, ParentElemType, VERT>::
725 importFrom(const Element& e, bool importRefs)
726{
727 if (importRefs) {
728 if constexpr (HasVertexReferences<Element>) {
729 if constexpr (N > 0) {
730 // same size non-polygonal faces
731 if constexpr (N == Element::VERTEX_NUMBER) {
732 importIndicesFrom(e);
733 }
734 // from polygonal to fixed size, but the polygon size == the
735 // fixed face size
736 else if constexpr (Element::VERTEX_NUMBER < 0) {
737 if (e.vertexNumber() == N) {
738 importIndicesFrom(e);
739 }
740 }
741 else {
742 // do not import in this case: cannot import from a face
743 // of different size
744 }
745 }
746 else {
747 // from fixed to polygonal size: need to resize first, then
748 // import
749 resizeVertices(e.vertexNumber());
750 importIndicesFrom(e);
751 }
752 }
753 }
754}
755
756} // namespace vcl::comp
757
758#endif // VCL_MESH_COMPONENTS_VERTEX_REFERENCES_H
A class representing a box in N-dimensional space.
Definition box.h:46
Box()
The Empty constructor of a box, initializes a null box.
Definition box.h:65
PointT size() const
Computes the size of the box.
Definition box.h:267
The Element class.
Definition element.h:75
The Vertex class represents an Vertex element of the vcl::Mesh class.
Definition vertex.h:46
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
A concept that checks whether a type T (that should be a Element) has the VertexReferences component ...
Definition vertex_references.h:712
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