MeshData.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <SceneAPI/SceneData/GraphData/MeshData.h>
  9. #include <AzCore/Casting/numeric_cast.h>
  10. #include <AzCore/Serialization/SerializeContext.h>
  11. #include <AzCore/RTTI/BehaviorContext.h>
  12. namespace AZ
  13. {
  14. AZ_TYPE_INFO_SPECIALIZE(AZ::SceneAPI::DataTypes::IMeshData::Face, "{F9F49C1A-014F-46F5-A46F-B56D8CB46C2B}");
  15. namespace SceneData
  16. {
  17. namespace GraphData
  18. {
  19. namespace DataTypes = AZ::SceneAPI::DataTypes;
  20. void MeshData::Reflect(ReflectContext* context)
  21. {
  22. SerializeContext* serializeContext = azrtti_cast<SerializeContext*>(context);
  23. if (serializeContext)
  24. {
  25. serializeContext->Class<MeshData>()->Version(1);
  26. }
  27. BehaviorContext* behaviorContext = azrtti_cast<BehaviorContext*>(context);
  28. if (behaviorContext)
  29. {
  30. behaviorContext->Class<SceneAPI::DataTypes::IMeshData>()
  31. ->Attribute(Script::Attributes::ExcludeFrom, Script::Attributes::ExcludeFlags::ListOnly)
  32. ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Common)
  33. ->Attribute(AZ::Script::Attributes::Module, "scene")
  34. ->Method("GetUnitSizeInMeters", &MeshData::GetUnitSizeInMeters)
  35. ->Method("GetOriginalUnitSizeInMeters", &MeshData::GetOriginalUnitSizeInMeters);
  36. behaviorContext->Class<MeshData>()
  37. ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Common)
  38. ->Attribute(AZ::Script::Attributes::Module, "scene")
  39. ->Method("GetControlPointIndex", &MeshData::GetControlPointIndex)
  40. ->Method("GetUsedControlPointCount", &MeshData::GetUsedControlPointCount)
  41. ->Method("GetUsedPointIndexForControlPoint", &MeshData::GetUsedPointIndexForControlPoint)
  42. ->Method("GetVertexCount", &MeshData::GetVertexCount)
  43. ->Method("HasNormalData", &MeshData::HasNormalData)
  44. ->Method("GetPosition", &MeshData::GetPosition)
  45. ->Method("GetNormal", &MeshData::GetNormal)
  46. ->Method("GetFaceCount", &MeshData::GetFaceCount)
  47. ->Method("GetFaceInfo", &MeshData::GetFaceInfo)
  48. ->Method("GetFaceMaterialId", &MeshData::GetFaceMaterialId)
  49. ->Method("GetVertexIndex", &MeshData::GetVertexIndex);
  50. behaviorContext->Class<SceneAPI::DataTypes::IMeshData::Face>()
  51. ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Common)
  52. ->Attribute(AZ::Script::Attributes::Module, "scene")
  53. ->Method("GetVertexIndex", [](const SceneAPI::DataTypes::IMeshData::Face& self, int index)
  54. {
  55. if (index >= 0 && index < 3)
  56. {
  57. return self.vertexIndex[index];
  58. }
  59. return aznumeric_cast<unsigned int>(0);
  60. });
  61. }
  62. }
  63. MeshData::~MeshData() = default;
  64. void MeshData::CloneAttributesFrom(const IGraphObject* sourceObject)
  65. {
  66. IMeshData::CloneAttributesFrom(sourceObject);
  67. }
  68. void MeshData::AddPosition(const AZ::Vector3& position)
  69. {
  70. m_positions.push_back(position);
  71. }
  72. void MeshData::AddNormal(const AZ::Vector3& normal)
  73. {
  74. m_normals.push_back(normal);
  75. }
  76. //assume consistent winding - no stripping or fanning expected (3 index per face)
  77. //indices can be used for position and normal
  78. void MeshData::AddFace(unsigned int index1, unsigned int index2, unsigned int index3, unsigned int faceMaterialId)
  79. {
  80. IMeshData::Face face;
  81. face.vertexIndex[0] = index1;
  82. face.vertexIndex[1] = index2;
  83. face.vertexIndex[2] = index3;
  84. m_faceList.push_back(face);
  85. m_faceMaterialIds.push_back(faceMaterialId);
  86. }
  87. void MeshData::AddFace(const DataTypes::IMeshData::Face& face, unsigned int faceMaterialId)
  88. {
  89. m_faceList.push_back(face);
  90. m_faceMaterialIds.push_back(faceMaterialId);
  91. }
  92. void MeshData::SetVertexIndexToControlPointIndexMap(int vertexIndex, int controlPointIndex)
  93. {
  94. m_vertexIndexToControlPointIndexMap[vertexIndex] = controlPointIndex;
  95. // The above hashmap stores the control point index (value) per vertex (key).
  96. // We construct an unordered set and fill in the control point indices in order to get access to the number of unique control points indices.
  97. if (m_controlPointToUsedVertexIndexMap.find(controlPointIndex) == m_controlPointToUsedVertexIndexMap.end())
  98. {
  99. m_controlPointToUsedVertexIndexMap[controlPointIndex] = aznumeric_cast<unsigned int>(m_controlPointToUsedVertexIndexMap.size());
  100. }
  101. }
  102. int MeshData::GetControlPointIndex(int vertexIndex) const
  103. {
  104. AZ_Assert(m_vertexIndexToControlPointIndexMap.find(vertexIndex) != m_vertexIndexToControlPointIndexMap.end(), "Vertex index %i doesn't exist", vertexIndex);
  105. // Note: AZStd::unordered_map's operator [] doesn't have const version...
  106. return m_vertexIndexToControlPointIndexMap.find(vertexIndex)->second;
  107. }
  108. size_t MeshData::GetUsedControlPointCount() const
  109. {
  110. return m_controlPointToUsedVertexIndexMap.size();
  111. }
  112. int MeshData::GetUsedPointIndexForControlPoint(int controlPointIndex) const
  113. {
  114. auto iter = m_controlPointToUsedVertexIndexMap.find(controlPointIndex);
  115. if (iter != m_controlPointToUsedVertexIndexMap.end())
  116. {
  117. return iter->second;
  118. }
  119. else
  120. {
  121. return -1; // That control point is not used in this mesh
  122. }
  123. }
  124. unsigned int MeshData::GetVertexCount() const
  125. {
  126. return static_cast<unsigned int>(m_positions.size());
  127. }
  128. bool MeshData::HasNormalData() const
  129. {
  130. return m_normals.size() > 0;
  131. }
  132. const AZ::Vector3& MeshData::GetPosition(unsigned int index) const
  133. {
  134. AZ_Assert(index < m_positions.size(), "GetPosition index not in range");
  135. return m_positions[index];
  136. }
  137. const AZ::Vector3& MeshData::GetNormal(unsigned int index) const
  138. {
  139. AZ_Assert(index < m_normals.size(), "GetNormal index not in range");
  140. return m_normals[index];
  141. }
  142. unsigned int MeshData::GetFaceCount() const
  143. {
  144. return static_cast<unsigned int>(m_faceList.size());
  145. }
  146. const DataTypes::IMeshData::Face& MeshData::GetFaceInfo(unsigned int index) const
  147. {
  148. AZ_Assert(index < m_faceList.size(), "GetFaceInfo index not in range");
  149. return m_faceList[index];
  150. }
  151. unsigned int MeshData::GetFaceMaterialId(unsigned int index) const
  152. {
  153. AZ_Assert(index < m_faceMaterialIds.size(), "GetFaceMaterialIds index not in range");
  154. return m_faceMaterialIds[index];
  155. }
  156. unsigned int MeshData::GetVertexIndex(int faceIndex, int vertexIndexInFace) const
  157. {
  158. AZ_Assert(faceIndex < m_faceList.size(), "GetFaceInfo index not in range");
  159. AZ_Assert(vertexIndexInFace < 3, "vertexIndexInFace index not in range");
  160. return m_faceList[faceIndex].vertexIndex[vertexIndexInFace];
  161. }
  162. void MeshData::GetDebugOutput(SceneAPI::Utilities::DebugOutput& output) const
  163. {
  164. output.Write("Positions", m_positions);
  165. output.Write("Normals", m_normals);
  166. output.Write("FaceList", m_faceList);
  167. output.Write("FaceMaterialIds", m_faceMaterialIds);
  168. }
  169. }
  170. }
  171. }