ccSubMesh.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. //##########################################################################
  2. //# #
  3. //# CLOUDCOMPARE #
  4. //# #
  5. //# This program is free software; you can redistribute it and/or modify #
  6. //# it under the terms of the GNU General Public License as published by #
  7. //# the Free Software Foundation; version 2 or later of the License. #
  8. //# #
  9. //# This program is distributed in the hope that it will be useful, #
  10. //# but WITHOUT ANY WARRANTY; without even the implied warranty of #
  11. //# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
  12. //# GNU General Public License for more details. #
  13. //# #
  14. //# COPYRIGHT: EDF R&D / TELECOM ParisTech (ENST-TSI) #
  15. //# #
  16. //##########################################################################
  17. #ifndef CC_SUB_MESH_HEADER
  18. #define CC_SUB_MESH_HEADER
  19. //Local
  20. #include "ccBBox.h"
  21. #include "ccGenericMesh.h"
  22. class ccMesh;
  23. //! A sub-mesh
  24. /** Equivalent to a CCCoreLib::ReferenceCloud for a mesh
  25. **/
  26. class QCC_DB_LIB_API ccSubMesh : public ccGenericMesh
  27. {
  28. public:
  29. //! Default constructor
  30. explicit ccSubMesh(ccMesh* parentMesh);
  31. //! Destructor
  32. ~ccSubMesh() override = default;
  33. //! Returns class ID
  34. CC_CLASS_ENUM getClassID() const override { return CC_TYPES::SUB_MESH; }
  35. //inherited methods (ccHObject)
  36. ccBBox getOwnBB(bool withGLFeatures = false) override;
  37. bool isSerializable() const override { return true; }
  38. //inherited methods (ccGenericMesh)
  39. ccGenericPointCloud* getAssociatedCloud() const override;
  40. void refreshBB() override;
  41. bool interpolateNormalsBC(unsigned triIndex, const CCVector3d& w, CCVector3& N) override;
  42. bool interpolateColors(unsigned triIndex, const CCVector3& P, ccColor::Rgb& color) override;
  43. bool interpolateColorsBC(unsigned triIndex, const CCVector3d& w, ccColor::Rgb& color) override;
  44. bool interpolateColors(unsigned triIndex, const CCVector3& P, ccColor::Rgba& color) override;
  45. bool interpolateColorsBC(unsigned triIndex, const CCVector3d& w, ccColor::Rgba& color) override;
  46. bool getColorFromMaterial(unsigned triIndex, const CCVector3& P, ccColor::Rgba& color, bool interpolateColorIfNoTexture) override;
  47. bool getVertexColorFromMaterial(unsigned triIndex, unsigned char vertIndex, ccColor::Rgba& color, bool returnColorIfNoTexture) override;
  48. bool hasMaterials() const override;
  49. const ccMaterialSet* getMaterialSet() const override;
  50. int getTriangleMtlIndex(unsigned triangleIndex) const override;
  51. bool hasTextures() const override;
  52. TextureCoordsContainer* getTexCoordinatesTable() const override;
  53. void getTriangleTexCoordinates(unsigned triIndex, TexCoords2D* &tx1, TexCoords2D* &tx2, TexCoords2D* &tx3) const override;
  54. bool hasPerTriangleTexCoordIndexes() const override;
  55. void getTriangleTexCoordinatesIndexes(unsigned triangleIndex, int& i1, int& i2, int& i3) const override;
  56. bool hasTriNormals() const override;
  57. void getTriangleNormalIndexes(unsigned triangleIndex, int& i1, int& i2, int& i3) const override;
  58. bool getTriangleNormals(unsigned triangleIndex, CCVector3& Na, CCVector3& Nb, CCVector3& Nc) const override;
  59. NormsIndexesTableType* getTriNormsTable() const override;
  60. unsigned capacity() const override;
  61. void setGlobalShift(const CCVector3d& shift) override { /* this method shouldn't be called on ccSubMesh instances */ assert(false); }
  62. void setGlobalScale(double scale) override { /* this method shouldn't be called on ccSubMesh instances */ assert(false); }
  63. //inherited methods (ccDrawableObject)
  64. bool hasColors() const override;
  65. bool hasNormals() const override;
  66. bool hasScalarFields() const override;
  67. bool hasDisplayedScalarField() const override;
  68. bool normalsShown() const override;
  69. //inherited methods (GenericIndexedMesh)
  70. inline unsigned size() const override { return static_cast<unsigned>(m_triIndexes.size()); }
  71. void forEach(genericTriangleAction action) override;
  72. inline void placeIteratorAtBeginning() override { m_globalIterator = 0; }
  73. CCCoreLib::GenericTriangle* _getNextTriangle() override; //temporary object
  74. CCCoreLib::GenericTriangle* _getTriangle(unsigned index) override; //temporary object
  75. CCCoreLib::VerticesIndexes* getNextTriangleVertIndexes() override;
  76. CCCoreLib::VerticesIndexes* getTriangleVertIndexes(unsigned triangleIndex) override;
  77. void getTriangleVertices(unsigned triangleIndex, CCVector3& A, CCVector3& B, CCVector3& C) const override;
  78. void getBoundingBox(CCVector3& bbMin, CCVector3& bbMax) override;
  79. bool interpolateNormals(unsigned triIndex, const CCVector3& P, CCVector3& N) override;
  80. //! Returns global index (i.e. relative to the associated mesh) of a given element
  81. /** \param localIndex local index (i.e. relative to the internal index container)
  82. **/
  83. inline unsigned getTriGlobalIndex(unsigned localIndex) const { return m_triIndexes[localIndex]; }
  84. //! Returns the global index of the triangle pointed by the current element
  85. inline unsigned getCurrentTriGlobalIndex() const { assert(m_globalIterator < size()); return m_triIndexes[m_globalIterator]; }
  86. //! Forwards the local element iterator
  87. inline void forwardIterator() { ++m_globalIterator; }
  88. //! Clears the mesh
  89. void clear(bool releaseMemory);
  90. //! Triangle global index insertion mechanism
  91. /** \param globalIndex a triangle global index
  92. \return false if not enough memory
  93. **/
  94. bool addTriangleIndex(unsigned globalIndex);
  95. //! Triangle global index insertion mechanism (range)
  96. /** \param firstIndex first triangle global index of range
  97. \param lastIndex last triangle global index of range (excluded)
  98. \return false if not enough memory
  99. **/
  100. bool addTriangleIndex(unsigned firstIndex, unsigned lastIndex);
  101. //! Sets global index for a given element
  102. /** \param localIndex local index
  103. \param globalIndex global index
  104. **/
  105. void setTriangleIndex(unsigned localIndex, unsigned globalIndex);
  106. //! Reserves some memory for hosting the triangle references
  107. /** \param n the number of triangles (references)
  108. **/
  109. bool reserve(size_t n);
  110. //! Presets the size of the vector used to store triangle references
  111. /** \param n the number of triangles (references)
  112. **/
  113. bool resize(size_t n);
  114. //! Returns the associated mesh
  115. inline ccMesh* getAssociatedMesh() { return m_associatedMesh; }
  116. //! Returns the associated mesh (const version)
  117. inline const ccMesh* getAssociatedMesh() const { return m_associatedMesh; }
  118. //! Sets the associated mesh
  119. /** \param mesh parent mesh
  120. \param unlinkPreviousOne whether to remove any dependency with the previous parent mesh (if any)
  121. **/
  122. void setAssociatedMesh(ccMesh* mesh, bool unlinkPreviousOne = true);
  123. //! Indexes map for createNewSubMeshFromSelection
  124. using IndexMap = std::vector<unsigned int>;
  125. //! Creates a new sub mesh with the visible vertices only
  126. /** \param removeSelectedTriangles specifies if the faces composed only of 'selected' vertices should be removed or not
  127. \param selectedTriangleIndexes a map of the triangles indexes that will be used in the new sub msh (or -1 if they are discarded)
  128. \param newRemainingTriangleIndexes if an index map is provided, it will be used to 'translate' global indexes of triangles remaining in the source sub-mesh
  129. \return the new sub-mesh if successful
  130. **/
  131. ccSubMesh* createNewSubMeshFromSelection( bool removeSelectedTriangles,
  132. const std::vector<int>& selectedTriangleIndexes,
  133. IndexMap* newRemainingTriangleIndexes = nullptr );
  134. //! Creates a new sub mesh with the selected vertices only
  135. /** Creates a new sub-mesh structure with the vertices that are tagged as "visible" (see ccGenericPointCloud::visibilityArray).
  136. \param removeSelectedTriangles specifies if the faces composed only of 'selected' vertices should be removed or not
  137. \param selectedTriangleIndexes a map of the triangles indexes that will be used in the new sub msh (or -1 if they are discarded)
  138. \param newRemainingTriangleIndexes if an index map is provided, it will be used to 'translate' global indexes of triangles remaining in the source sub-mesh
  139. \return the new sub-mesh if successful
  140. **/
  141. ccSubMesh* createNewSubMeshFromSelection(bool removeSelectedTriangles, IndexMap* newRemainingTriangleIndexes = nullptr);
  142. protected:
  143. //inherited from ccHObject
  144. bool toFile_MeOnly(QFile& out, short dataVersion) const override;
  145. bool fromFile_MeOnly(QFile& in, short dataVersion, int flags, LoadedIDMap& oldToNewIDMap) override;
  146. short minimumFileVersion_MeOnly() const override;
  147. void onUpdateOf(ccHObject* obj) override;
  148. //! Associated mesh
  149. ccMesh* m_associatedMesh;
  150. //! Container of 3D triangles indexes
  151. using ReferencesContainer = std::vector<unsigned int>;
  152. //! Indexes of (some of) the associated mesh triangles
  153. ReferencesContainer m_triIndexes;
  154. //! Iterator on the triangles references container
  155. unsigned m_globalIterator;
  156. //! Bounding-box
  157. ccBBox m_bBox;
  158. };
  159. #endif //CC_SUB_MESH_HEADER