trianglev_mb.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 "primitive.h"
  18. namespace embree
  19. {
  20. /* Stores the vertices of M triangles in struct of array layout */
  21. template<int M>
  22. struct TriangleMvMB
  23. {
  24. typedef Vec3<vfloat<M>> Vec3vfM;
  25. public:
  26. struct Type : public PrimitiveType
  27. {
  28. Type();
  29. size_t size(const char* This) const;
  30. };
  31. static Type type;
  32. public:
  33. /* Returns maximal number of stored triangles */
  34. static __forceinline size_t max_size() { return M; }
  35. /* Returns required number of primitive blocks for N primitives */
  36. static __forceinline size_t blocks(size_t N) { return (N+max_size()-1)/max_size(); }
  37. public:
  38. /* Default constructor */
  39. __forceinline TriangleMvMB() {}
  40. /* Construction from vertices and IDs */
  41. __forceinline TriangleMvMB(const Vec3vfM& a0, const Vec3vfM& a1,
  42. const Vec3vfM& b0, const Vec3vfM& b1,
  43. const Vec3vfM& c0, const Vec3vfM& c1,
  44. const vint<M>& geomIDs, const vint<M>& primIDs)
  45. : v0(a0), v1(b0), v2(c0), dv0(a1-a0), dv1(b1-b0), dv2(c1-c0), geomIDs(geomIDs), primIDs(primIDs) {}
  46. /* Returns a mask that tells which triangles are valid */
  47. __forceinline vbool<M> valid() const { return geomIDs != vint<M>(-1); }
  48. /* Returns if the specified triangle is valid */
  49. __forceinline bool valid(const size_t i) const { assert(i<M); return geomIDs[i] != -1; }
  50. /* Returns the number of stored triangles */
  51. __forceinline size_t size() const { return __bsf(~movemask(valid())); }
  52. /* Returns the geometry IDs */
  53. __forceinline vint<M> geomID() const { return geomIDs; }
  54. __forceinline int geomID(const size_t i) const { assert(i<M); return geomIDs[i]; }
  55. /* Returns the primitive IDs */
  56. __forceinline vint<M> primID() const { return primIDs; }
  57. __forceinline int primID(const size_t i) const { assert(i<M); return primIDs[i]; }
  58. /* Calculate the bounds of the triangles at t0 */
  59. __forceinline BBox3fa bounds0() const
  60. {
  61. Vec3vfM lower = min(v0,v1,v2);
  62. Vec3vfM upper = max(v0,v1,v2);
  63. const vbool<M> mask = valid();
  64. lower.x = select(mask,lower.x,vfloat<M>(pos_inf));
  65. lower.y = select(mask,lower.y,vfloat<M>(pos_inf));
  66. lower.z = select(mask,lower.z,vfloat<M>(pos_inf));
  67. upper.x = select(mask,upper.x,vfloat<M>(neg_inf));
  68. upper.y = select(mask,upper.y,vfloat<M>(neg_inf));
  69. upper.z = select(mask,upper.z,vfloat<M>(neg_inf));
  70. return BBox3fa(Vec3fa(reduce_min(lower.x),reduce_min(lower.y),reduce_min(lower.z)),
  71. Vec3fa(reduce_max(upper.x),reduce_max(upper.y),reduce_max(upper.z)));
  72. }
  73. /* Calculate the bounds of the triangles at t1 */
  74. __forceinline BBox3fa bounds1() const
  75. {
  76. const Vec3vfM p0 = v0+dv0;
  77. const Vec3vfM p1 = v1+dv1;
  78. const Vec3vfM p2 = v2+dv2;
  79. Vec3vfM lower = min(p0,p1,p2);
  80. Vec3vfM upper = max(p0,p1,p2);
  81. const vbool<M> mask = valid();
  82. lower.x = select(mask,lower.x,vfloat<M>(pos_inf));
  83. lower.y = select(mask,lower.y,vfloat<M>(pos_inf));
  84. lower.z = select(mask,lower.z,vfloat<M>(pos_inf));
  85. upper.x = select(mask,upper.x,vfloat<M>(neg_inf));
  86. upper.y = select(mask,upper.y,vfloat<M>(neg_inf));
  87. upper.z = select(mask,upper.z,vfloat<M>(neg_inf));
  88. return BBox3fa(Vec3fa(reduce_min(lower.x),reduce_min(lower.y),reduce_min(lower.z)),
  89. Vec3fa(reduce_max(upper.x),reduce_max(upper.y),reduce_max(upper.z)));
  90. }
  91. /* Calculate the linear bounds of the primitive */
  92. __forceinline LBBox3fa linearBounds() const {
  93. return LBBox3fa(bounds0(),bounds1());
  94. }
  95. /* Fill triangle from triangle list */
  96. __forceinline LBBox3fa fillMB(const PrimRef* prims, size_t& begin, size_t end, Scene* scene, size_t itime, size_t numTimeSteps)
  97. {
  98. vint<M> vgeomID = -1, vprimID = -1;
  99. Vec3vfM va0 = zero, vb0 = zero, vc0 = zero;
  100. Vec3vfM va1 = zero, vb1 = zero, vc1 = zero;
  101. BBox3fa bounds0 = empty;
  102. BBox3fa bounds1 = empty;
  103. for (size_t i=0; i<M && begin<end; i++, begin++)
  104. {
  105. const PrimRef& prim = prims[begin];
  106. const unsigned geomID = prim.geomID();
  107. const unsigned primID = prim.primID();
  108. const TriangleMesh* __restrict__ const mesh = scene->get<TriangleMesh>(geomID);
  109. if (unlikely(mesh->numTimeSteps != numTimeSteps))
  110. throw_RTCError(RTC_INVALID_OPERATION, "TriangleMvMB is not supported for different number of time steps per mesh");
  111. const TriangleMesh::Triangle& tri = mesh->triangle(primID);
  112. const Vec3fa& a0 = mesh->vertex(tri.v[0],itime+0); bounds0.extend(a0);
  113. const Vec3fa& a1 = mesh->vertex(tri.v[0],itime+1); bounds1.extend(a1);
  114. const Vec3fa& b0 = mesh->vertex(tri.v[1],itime+0); bounds0.extend(b0);
  115. const Vec3fa& b1 = mesh->vertex(tri.v[1],itime+1); bounds1.extend(b1);
  116. const Vec3fa& c0 = mesh->vertex(tri.v[2],itime+0); bounds0.extend(c0);
  117. const Vec3fa& c1 = mesh->vertex(tri.v[2],itime+1); bounds1.extend(c1);
  118. vgeomID [i] = geomID;
  119. vprimID [i] = primID;
  120. va0.x[i] = a0.x; va0.y[i] = a0.y; va0.z[i] = a0.z;
  121. va1.x[i] = a1.x; va1.y[i] = a1.y; va1.z[i] = a1.z;
  122. vb0.x[i] = b0.x; vb0.y[i] = b0.y; vb0.z[i] = b0.z;
  123. vb1.x[i] = b1.x; vb1.y[i] = b1.y; vb1.z[i] = b1.z;
  124. vc0.x[i] = c0.x; vc0.y[i] = c0.y; vc0.z[i] = c0.z;
  125. vc1.x[i] = c1.x; vc1.y[i] = c1.y; vc1.z[i] = c1.z;
  126. }
  127. new (this) TriangleMvMB(va0,va1,vb0,vb1,vc0,vc1,vgeomID,vprimID);
  128. return LBBox3fa(bounds0,bounds1);
  129. }
  130. public:
  131. Vec3vfM v0; // 1st vertex of the triangles
  132. Vec3vfM v1; // 2nd vertex of the triangles
  133. Vec3vfM v2; // 3rd vertex of the triangles
  134. Vec3vfM dv0; // difference vector between time steps t0 and t1 for first vertex
  135. Vec3vfM dv1; // difference vector between time steps t0 and t1 for second vertex
  136. Vec3vfM dv2; // difference vector between time steps t0 and t1 for third vertex
  137. vint<M> geomIDs; // geometry ID
  138. vint<M> primIDs; // primitive ID
  139. };
  140. template<int M>
  141. typename TriangleMvMB<M>::Type TriangleMvMB<M>::type;
  142. typedef TriangleMvMB<4> Triangle4vMB;
  143. }