scene_triangle_mesh.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. // ======================================================================== //
  2. // Copyright 2009-2017 Intel Corporation //
  3. // //
  4. // Licensed under the Apache License, Version 2.0 (the "License"); //
  5. // you may not use this file except in compliance with the License. //
  6. // You may obtain a copy of the License at //
  7. // //
  8. // http://www.apache.org/licenses/LICENSE-2.0 //
  9. // //
  10. // Unless required by applicable law or agreed to in writing, software //
  11. // distributed under the License is distributed on an "AS IS" BASIS, //
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //
  13. // See the License for the specific language governing permissions and //
  14. // limitations under the License. //
  15. // ======================================================================== //
  16. #pragma once
  17. #include "geometry.h"
  18. #include "buffer.h"
  19. namespace embree
  20. {
  21. /*! Triangle Mesh */
  22. struct TriangleMesh : public Geometry
  23. {
  24. /*! type of this geometry */
  25. static const Geometry::Type geom_type = Geometry::TRIANGLE_MESH;
  26. /*! triangle indices */
  27. struct Triangle
  28. {
  29. uint32_t v[3];
  30. /*! outputs triangle indices */
  31. __forceinline friend std::ostream &operator<<(std::ostream& cout, const Triangle& t) {
  32. return cout << "Triangle { " << t.v[0] << ", " << t.v[1] << ", " << t.v[2] << " }";
  33. }
  34. };
  35. /*! triangle edge based on two indices */
  36. struct Edge
  37. {
  38. uint64_t e;
  39. __forceinline Edge() {}
  40. __forceinline Edge(const uint32_t& v0, const uint32_t& v1) {
  41. e = v0 < v1 ? (((uint64_t)v1 << 32) | (uint64_t)v0) : (((uint64_t)v0 << 32) | (uint64_t)v1);
  42. }
  43. __forceinline friend bool operator==( const Edge& a, const Edge& b ) {
  44. return a.e == b.e;
  45. }
  46. };
  47. /* last edge of triangle 0 is shared */
  48. static __forceinline unsigned int pair_order(const unsigned int tri0_vtx_index0,
  49. const unsigned int tri0_vtx_index1,
  50. const unsigned int tri0_vtx_index2,
  51. const unsigned int tri1_vtx_index)
  52. {
  53. return \
  54. (tri0_vtx_index0 << 0) |
  55. (tri0_vtx_index1 << 8) |
  56. (tri0_vtx_index2 << 16) |
  57. (tri1_vtx_index << 24);
  58. }
  59. /*! tests if a shared exists between two triangles, returns -1 if no shared edge exists and the opposite vertex index of the second triangle if a shared edge exists */
  60. static __forceinline int sharedEdge(const Triangle &tri0,
  61. const Triangle &tri1)
  62. {
  63. const Edge tri0_edge0(tri0.v[0],tri0.v[1]);
  64. const Edge tri0_edge1(tri0.v[1],tri0.v[2]);
  65. const Edge tri0_edge2(tri0.v[2],tri0.v[0]);
  66. const Edge tri1_edge0(tri1.v[0],tri1.v[1]);
  67. const Edge tri1_edge1(tri1.v[1],tri1.v[2]);
  68. const Edge tri1_edge2(tri1.v[2],tri1.v[0]);
  69. /* rotate triangle 0 to force shared edge between the first and last vertex */
  70. if (unlikely(tri0_edge0 == tri1_edge0)) return pair_order(1,2,0, 2);
  71. if (unlikely(tri0_edge1 == tri1_edge0)) return pair_order(2,0,1, 2);
  72. if (unlikely(tri0_edge2 == tri1_edge0)) return pair_order(0,1,2, 2);
  73. if (unlikely(tri0_edge0 == tri1_edge1)) return pair_order(1,2,0, 0);
  74. if (unlikely(tri0_edge1 == tri1_edge1)) return pair_order(2,0,1, 0);
  75. if (unlikely(tri0_edge2 == tri1_edge1)) return pair_order(0,1,2, 0);
  76. if (unlikely(tri0_edge0 == tri1_edge2)) return pair_order(1,2,0, 1);
  77. if (unlikely(tri0_edge1 == tri1_edge2)) return pair_order(2,0,1, 1);
  78. if (unlikely(tri0_edge2 == tri1_edge2)) return pair_order(0,1,2, 1);
  79. return -1;
  80. }
  81. public:
  82. /*! triangle mesh construction */
  83. TriangleMesh (Scene* parent, RTCGeometryFlags flags, size_t numTriangles, size_t numVertices, size_t numTimeSteps);
  84. /* geometry interface */
  85. public:
  86. void enabling();
  87. void disabling();
  88. void setMask (unsigned mask);
  89. void setBuffer(RTCBufferType type, void* ptr, size_t offset, size_t stride, size_t size);
  90. void* map(RTCBufferType type);
  91. void unmap(RTCBufferType type);
  92. void immutable ();
  93. bool verify ();
  94. void interpolate(unsigned primID, float u, float v, RTCBufferType buffer, float* P, float* dPdu, float* dPdv, float* ddPdudu, float* ddPdvdv, float* ddPdudv, size_t numFloats);
  95. // FIXME: implement interpolateN
  96. public:
  97. /*! returns number of triangles */
  98. __forceinline size_t size() const {
  99. return triangles.size();
  100. }
  101. /*! returns number of vertices */
  102. __forceinline size_t numVertices() const {
  103. return vertices[0].size();
  104. }
  105. /*! returns i'th triangle*/
  106. __forceinline const Triangle& triangle(size_t i) const {
  107. return triangles[i];
  108. }
  109. /*! returns i'th vertex of the first time step */
  110. __forceinline const Vec3fa vertex(size_t i) const {
  111. return vertices0[i];
  112. }
  113. /*! returns i'th vertex of the first time step */
  114. __forceinline const char* vertexPtr(size_t i) const {
  115. return vertices0.getPtr(i);
  116. }
  117. /*! returns i'th vertex of itime'th timestep */
  118. __forceinline const Vec3fa vertex(size_t i, size_t itime) const {
  119. return vertices[itime][i];
  120. }
  121. /*! returns i'th vertex of itime'th timestep */
  122. __forceinline const char* vertexPtr(size_t i, size_t itime) const {
  123. return vertices[itime].getPtr(i);
  124. }
  125. /*! calculates the bounds of the i'th triangle */
  126. __forceinline BBox3fa bounds(size_t i) const
  127. {
  128. const Triangle& tri = triangle(i);
  129. const Vec3fa v0 = vertex(tri.v[0]);
  130. const Vec3fa v1 = vertex(tri.v[1]);
  131. const Vec3fa v2 = vertex(tri.v[2]);
  132. return BBox3fa(min(v0,v1,v2),max(v0,v1,v2));
  133. }
  134. /*! calculates the bounds of the i'th triangle at the itime'th timestep */
  135. __forceinline BBox3fa bounds(size_t i, size_t itime) const
  136. {
  137. const Triangle& tri = triangle(i);
  138. const Vec3fa v0 = vertex(tri.v[0],itime);
  139. const Vec3fa v1 = vertex(tri.v[1],itime);
  140. const Vec3fa v2 = vertex(tri.v[2],itime);
  141. return BBox3fa(min(v0,v1,v2),max(v0,v1,v2));
  142. }
  143. /*! calculates the interpolated bounds of the i'th triangle at the specified time */
  144. __forceinline BBox3fa bounds(size_t i, float time) const
  145. {
  146. float ftime; size_t itime = getTimeSegment(time, fnumTimeSegments, ftime);
  147. const BBox3fa b0 = bounds(i, itime+0);
  148. const BBox3fa b1 = bounds(i, itime+1);
  149. return lerp(b0, b1, ftime);
  150. }
  151. /*! check if the i'th primitive is valid at the itime'th timestep */
  152. __forceinline bool valid(size_t i, size_t itime) const {
  153. return valid(i, make_range(itime, itime));
  154. }
  155. /*! check if the i'th primitive is valid between the specified time range */
  156. __forceinline bool valid(size_t i, const range<size_t>& itime_range) const
  157. {
  158. const Triangle& tri = triangle(i);
  159. if (unlikely(tri.v[0] >= numVertices())) return false;
  160. if (unlikely(tri.v[1] >= numVertices())) return false;
  161. if (unlikely(tri.v[2] >= numVertices())) return false;
  162. for (size_t itime = itime_range.begin(); itime <= itime_range.end(); itime++)
  163. {
  164. if (!isvalid(vertex(tri.v[0],itime))) return false;
  165. if (!isvalid(vertex(tri.v[1],itime))) return false;
  166. if (!isvalid(vertex(tri.v[2],itime))) return false;
  167. }
  168. return true;
  169. }
  170. /*! calculates the linear bounds of the i'th primitive at the itimeGlobal'th time segment */
  171. __forceinline LBBox3fa linearBounds(size_t i, size_t itimeGlobal, size_t numTimeStepsGlobal) const
  172. {
  173. return Geometry::linearBounds([&] (size_t itime) { return bounds(i, itime); },
  174. itimeGlobal, numTimeStepsGlobal, numTimeSteps);
  175. }
  176. /*! calculates the build bounds of the i'th primitive, if it's valid */
  177. __forceinline bool buildBounds(size_t i, BBox3fa* bbox = nullptr) const
  178. {
  179. const Triangle& tri = triangle(i);
  180. if (unlikely(tri.v[0] >= numVertices())) return false;
  181. if (unlikely(tri.v[1] >= numVertices())) return false;
  182. if (unlikely(tri.v[2] >= numVertices())) return false;
  183. for (size_t t=0; t<numTimeSteps; t++)
  184. {
  185. const Vec3fa v0 = vertex(tri.v[0],t);
  186. const Vec3fa v1 = vertex(tri.v[1],t);
  187. const Vec3fa v2 = vertex(tri.v[2],t);
  188. if (unlikely(!isvalid(v0) || !isvalid(v1) || !isvalid(v2)))
  189. return false;
  190. }
  191. if (likely(bbox))
  192. *bbox = bounds(i);
  193. return true;
  194. }
  195. /*! calculates the build bounds of the i'th primitive at the itime'th time segment, if it's valid */
  196. __forceinline bool buildBounds(size_t i, size_t itime, BBox3fa& bbox) const
  197. {
  198. const Triangle& tri = triangle(i);
  199. if (unlikely(tri.v[0] >= numVertices())) return false;
  200. if (unlikely(tri.v[1] >= numVertices())) return false;
  201. if (unlikely(tri.v[2] >= numVertices())) return false;
  202. assert(itime+1 < numTimeSteps);
  203. const Vec3fa a0 = vertex(tri.v[0],itime+0); if (unlikely(!isvalid(a0))) return false;
  204. const Vec3fa a1 = vertex(tri.v[1],itime+0); if (unlikely(!isvalid(a1))) return false;
  205. const Vec3fa a2 = vertex(tri.v[2],itime+0); if (unlikely(!isvalid(a2))) return false;
  206. const Vec3fa b0 = vertex(tri.v[0],itime+1); if (unlikely(!isvalid(b0))) return false;
  207. const Vec3fa b1 = vertex(tri.v[1],itime+1); if (unlikely(!isvalid(b1))) return false;
  208. const Vec3fa b2 = vertex(tri.v[2],itime+1); if (unlikely(!isvalid(b2))) return false;
  209. /* use bounds of first time step in builder */
  210. bbox = BBox3fa(min(a0,a1,a2),max(a0,a1,a2));
  211. return true;
  212. }
  213. /*! calculates the build bounds of the i'th primitive at the itimeGlobal'th time segment, if it's valid */
  214. __forceinline bool buildBounds(size_t i, size_t itimeGlobal, size_t numTimeStepsGlobal, BBox3fa& bbox) const
  215. {
  216. return Geometry::buildBounds([&] (size_t itime, BBox3fa& bbox) -> bool
  217. {
  218. if (unlikely(!valid(i, itime))) return false;
  219. bbox = bounds(i, itime);
  220. return true;
  221. },
  222. itimeGlobal, numTimeStepsGlobal, numTimeSteps, bbox);
  223. }
  224. public:
  225. APIBuffer<Triangle> triangles; //!< array of triangles
  226. BufferRefT<Vec3fa> vertices0; //!< fast access to first vertex buffer
  227. vector<APIBuffer<Vec3fa>> vertices; //!< vertex array for each timestep
  228. vector<APIBuffer<char>> userbuffers; //!< user buffers
  229. };
  230. }