CMetaTriangleSelector.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // Copyright (C) 2002-2012 Nikolaus Gebhardt
  2. // This file is part of the "Irrlicht Engine".
  3. // For conditions of distribution and use, see copyright notice in irrlicht.h
  4. #include "CMetaTriangleSelector.h"
  5. namespace irr
  6. {
  7. namespace scene
  8. {
  9. //! constructor
  10. CMetaTriangleSelector::CMetaTriangleSelector()
  11. {
  12. #ifdef _DEBUG
  13. setDebugName("CMetaTriangleSelector");
  14. #endif
  15. }
  16. //! destructor
  17. CMetaTriangleSelector::~CMetaTriangleSelector()
  18. {
  19. removeAllTriangleSelectors();
  20. }
  21. //! Returns amount of all available triangles in this selector
  22. s32 CMetaTriangleSelector::getTriangleCount() const
  23. {
  24. s32 count = 0;
  25. for (u32 i=0; i<TriangleSelectors.size(); ++i)
  26. count += TriangleSelectors[i]->getTriangleCount();
  27. return count;
  28. }
  29. //! Gets all triangles.
  30. void CMetaTriangleSelector::getTriangles(core::triangle3df* triangles, s32 arraySize,
  31. s32& outTriangleCount, const core::matrix4* transform) const
  32. {
  33. s32 outWritten = 0;
  34. for (u32 i=0; i<TriangleSelectors.size(); ++i)
  35. {
  36. s32 t = 0;
  37. TriangleSelectors[i]->getTriangles(triangles + outWritten,
  38. arraySize - outWritten, t, transform);
  39. outWritten += t;
  40. if (outWritten==arraySize)
  41. break;
  42. }
  43. outTriangleCount = outWritten;
  44. }
  45. //! Gets all triangles which lie within a specific bounding box.
  46. void CMetaTriangleSelector::getTriangles(core::triangle3df* triangles, s32 arraySize,
  47. s32& outTriangleCount, const core::aabbox3d<f32>& box,
  48. const core::matrix4* transform) const
  49. {
  50. s32 outWritten = 0;
  51. for (u32 i=0; i<TriangleSelectors.size(); ++i)
  52. {
  53. s32 t = 0;
  54. TriangleSelectors[i]->getTriangles(triangles + outWritten,
  55. arraySize - outWritten, t, box, transform);
  56. outWritten += t;
  57. if (outWritten==arraySize)
  58. break;
  59. }
  60. outTriangleCount = outWritten;
  61. }
  62. //! Gets all triangles which have or may have contact with a 3d line.
  63. void CMetaTriangleSelector::getTriangles(core::triangle3df* triangles, s32 arraySize,
  64. s32& outTriangleCount, const core::line3d<f32>& line,
  65. const core::matrix4* transform) const
  66. {
  67. s32 outWritten = 0;
  68. for (u32 i=0; i<TriangleSelectors.size(); ++i)
  69. {
  70. s32 t = 0;
  71. TriangleSelectors[i]->getTriangles(triangles + outWritten,
  72. arraySize - outWritten, t, line, transform);
  73. outWritten += t;
  74. if (outWritten==arraySize)
  75. break;
  76. }
  77. outTriangleCount = outWritten;
  78. }
  79. //! Adds a triangle selector to the collection of triangle selectors
  80. //! in this metaTriangleSelector.
  81. void CMetaTriangleSelector::addTriangleSelector(ITriangleSelector* toAdd)
  82. {
  83. if (!toAdd)
  84. return;
  85. TriangleSelectors.push_back(toAdd);
  86. toAdd->grab();
  87. }
  88. //! Removes a specific triangle selector which was added before from the collection.
  89. bool CMetaTriangleSelector::removeTriangleSelector(ITriangleSelector* toRemove)
  90. {
  91. for (u32 i=0; i<TriangleSelectors.size(); ++i)
  92. {
  93. if (toRemove == TriangleSelectors[i])
  94. {
  95. TriangleSelectors[i]->drop();
  96. TriangleSelectors.erase(i);
  97. return true;
  98. }
  99. }
  100. return false;
  101. }
  102. //! Removes all triangle selectors from the collection.
  103. void CMetaTriangleSelector::removeAllTriangleSelectors()
  104. {
  105. for (u32 i=0; i<TriangleSelectors.size(); ++i)
  106. TriangleSelectors[i]->drop();
  107. TriangleSelectors.clear();
  108. }
  109. //! Return the scene node associated with a given triangle.
  110. ISceneNode* CMetaTriangleSelector::getSceneNodeForTriangle(u32 triangleIndex) const
  111. {
  112. u32 totalTriangles = 0;
  113. for (u32 i=0; i<TriangleSelectors.size(); ++i)
  114. {
  115. totalTriangles += TriangleSelectors[i]->getTriangleCount();
  116. if(totalTriangles > triangleIndex)
  117. return TriangleSelectors[i]->getSceneNodeForTriangle(0);
  118. }
  119. // For lack of anything more sensible, return the first selector.
  120. return TriangleSelectors[0]->getSceneNodeForTriangle(0);
  121. }
  122. /* Return the number of TriangleSelectors that are inside this one,
  123. Only useful for MetaTriangleSelector others return 1
  124. */
  125. u32 CMetaTriangleSelector::getSelectorCount() const
  126. {
  127. return TriangleSelectors.size();
  128. }
  129. /* Returns the TriangleSelector based on index based on getSelectorCount
  130. Only useful for MetaTriangleSelector others return 'this'
  131. */
  132. ITriangleSelector* CMetaTriangleSelector::getSelector(u32 index)
  133. {
  134. if (index >= TriangleSelectors.size())
  135. return 0;
  136. return TriangleSelectors[index];
  137. }
  138. /* Returns the TriangleSelector based on index based on getSelectorCount
  139. Only useful for MetaTriangleSelector others return 'this'
  140. */
  141. const ITriangleSelector* CMetaTriangleSelector::getSelector(u32 index) const
  142. {
  143. if (index >= TriangleSelectors.size())
  144. return 0;
  145. return TriangleSelectors[index];
  146. }
  147. } // end namespace scene
  148. } // end namespace irr