triangle.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. /* Precalculated representation for M triangles. Stores for each
  21. triangle a base vertex, two edges, and the geometry normal to
  22. speed up intersection calculations */
  23. template<int M>
  24. struct TriangleM
  25. {
  26. typedef Vec3<vfloat<M>> Vec3vfM;
  27. public:
  28. struct Type : public PrimitiveType
  29. {
  30. Type();
  31. size_t size(const char* This) const;
  32. };
  33. static Type type;
  34. public:
  35. /* Returns maximal number of stored triangles */
  36. static __forceinline size_t max_size() { return M; }
  37. /* Returns required number of primitive blocks for N primitives */
  38. static __forceinline size_t blocks(size_t N) { return (N+max_size()-1)/max_size(); }
  39. public:
  40. /* Default constructor */
  41. __forceinline TriangleM() {}
  42. /* Construction from vertices and IDs */
  43. __forceinline TriangleM(const Vec3vfM& v0, const Vec3vfM& v1, const Vec3vfM& v2, const vint<M>& geomIDs, const vint<M>& primIDs)
  44. : v0(v0), e1(v0-v1), e2(v2-v0), Ng(cross(e1,e2)), geomIDs(geomIDs), primIDs(primIDs) {}
  45. /* Returns a mask that tells which triangles are valid */
  46. __forceinline vbool<M> valid() const { return geomIDs != vint<M>(-1); }
  47. /* Returns true if the specified triangle is valid */
  48. __forceinline bool valid(const size_t i) const { assert(i<M); return geomIDs[i] != -1; }
  49. /* Returns the number of stored triangles */
  50. __forceinline size_t size() const { return __bsf(~movemask(valid())); }
  51. /* Returns the geometry IDs */
  52. __forceinline vint<M> geomID() const { return geomIDs; }
  53. __forceinline int geomID(const size_t i) const { assert(i<M); return geomIDs[i]; }
  54. /* Returns the primitive IDs */
  55. __forceinline vint<M> primID() const { return primIDs; }
  56. __forceinline int primID(const size_t i) const { assert(i<M); return primIDs[i]; }
  57. /* Calculate the bounds of the triangle */
  58. __forceinline BBox3fa bounds() const
  59. {
  60. Vec3vfM p0 = v0;
  61. Vec3vfM p1 = v0-e1;
  62. Vec3vfM p2 = v0+e2;
  63. Vec3vfM lower = min(p0,p1,p2);
  64. Vec3vfM upper = max(p0,p1,p2);
  65. vbool<M> mask = valid();
  66. lower.x = select(mask,lower.x,vfloat<M>(pos_inf));
  67. lower.y = select(mask,lower.y,vfloat<M>(pos_inf));
  68. lower.z = select(mask,lower.z,vfloat<M>(pos_inf));
  69. upper.x = select(mask,upper.x,vfloat<M>(neg_inf));
  70. upper.y = select(mask,upper.y,vfloat<M>(neg_inf));
  71. upper.z = select(mask,upper.z,vfloat<M>(neg_inf));
  72. return BBox3fa(Vec3fa(reduce_min(lower.x),reduce_min(lower.y),reduce_min(lower.z)),
  73. Vec3fa(reduce_max(upper.x),reduce_max(upper.y),reduce_max(upper.z)));
  74. }
  75. /* Non temporal store */
  76. __forceinline static void store_nt(TriangleM* dst, const TriangleM& src)
  77. {
  78. vfloat<M>::store_nt(&dst->v0.x,src.v0.x);
  79. vfloat<M>::store_nt(&dst->v0.y,src.v0.y);
  80. vfloat<M>::store_nt(&dst->v0.z,src.v0.z);
  81. vfloat<M>::store_nt(&dst->e1.x,src.e1.x);
  82. vfloat<M>::store_nt(&dst->e1.y,src.e1.y);
  83. vfloat<M>::store_nt(&dst->e1.z,src.e1.z);
  84. vfloat<M>::store_nt(&dst->e2.x,src.e2.x);
  85. vfloat<M>::store_nt(&dst->e2.y,src.e2.y);
  86. vfloat<M>::store_nt(&dst->e2.z,src.e2.z);
  87. vfloat<M>::store_nt(&dst->Ng.x,src.Ng.x);
  88. vfloat<M>::store_nt(&dst->Ng.y,src.Ng.y);
  89. vfloat<M>::store_nt(&dst->Ng.z,src.Ng.z);
  90. vint<M>::store_nt(&dst->geomIDs,src.geomIDs);
  91. vint<M>::store_nt(&dst->primIDs,src.primIDs);
  92. }
  93. /* Fill triangle from triangle list */
  94. __forceinline void fill(const PrimRef* prims, size_t& begin, size_t end, Scene* scene)
  95. {
  96. vint<M> vgeomID = -1, vprimID = -1;
  97. Vec3vfM v0 = zero, v1 = zero, v2 = zero;
  98. for (size_t i=0; i<M && begin<end; i++, begin++)
  99. {
  100. const PrimRef& prim = prims[begin];
  101. const unsigned geomID = prim.geomID();
  102. const unsigned primID = prim.primID();
  103. const TriangleMesh* __restrict__ const mesh = scene->get<TriangleMesh>(geomID);
  104. const TriangleMesh::Triangle& tri = mesh->triangle(primID);
  105. const Vec3fa& p0 = mesh->vertex(tri.v[0]);
  106. const Vec3fa& p1 = mesh->vertex(tri.v[1]);
  107. const Vec3fa& p2 = mesh->vertex(tri.v[2]);
  108. vgeomID [i] = geomID;
  109. vprimID [i] = primID;
  110. v0.x[i] = p0.x; v0.y[i] = p0.y; v0.z[i] = p0.z;
  111. v1.x[i] = p1.x; v1.y[i] = p1.y; v1.z[i] = p1.z;
  112. v2.x[i] = p2.x; v2.y[i] = p2.y; v2.z[i] = p2.z;
  113. }
  114. TriangleM::store_nt(this,TriangleM(v0,v1,v2,vgeomID,vprimID));
  115. }
  116. /* Updates the primitive */
  117. __forceinline BBox3fa update(TriangleMesh* mesh)
  118. {
  119. BBox3fa bounds = empty;
  120. vint<M> vgeomID = -1, vprimID = -1;
  121. Vec3vfM v0 = zero, v1 = zero, v2 = zero;
  122. for (size_t i=0; i<M; i++)
  123. {
  124. if (unlikely(geomID(i) == -1)) break;
  125. const unsigned geomId = geomID(i);
  126. const unsigned primId = primID(i);
  127. const TriangleMesh::Triangle& tri = mesh->triangle(primId);
  128. const Vec3fa p0 = mesh->vertex(tri.v[0]);
  129. const Vec3fa p1 = mesh->vertex(tri.v[1]);
  130. const Vec3fa p2 = mesh->vertex(tri.v[2]);
  131. bounds.extend(merge(BBox3fa(p0),BBox3fa(p1),BBox3fa(p2)));
  132. vgeomID [i] = geomId;
  133. vprimID [i] = primId;
  134. v0.x[i] = p0.x; v0.y[i] = p0.y; v0.z[i] = p0.z;
  135. v1.x[i] = p1.x; v1.y[i] = p1.y; v1.z[i] = p1.z;
  136. v2.x[i] = p2.x; v2.y[i] = p2.y; v2.z[i] = p2.z;
  137. }
  138. TriangleM::store_nt(this,TriangleM(v0,v1,v2,vgeomID,vprimID));
  139. return bounds;
  140. }
  141. public:
  142. Vec3vfM v0; // base vertex of the triangles
  143. Vec3vfM e1; // 1st edge of the triangles (v0-v1)
  144. Vec3vfM e2; // 2nd edge of the triangles (v2-v0)
  145. Vec3vfM Ng; // geometry normal of the triangles (cross(e1,e2))
  146. vint<M> geomIDs; // geometry IDs
  147. vint<M> primIDs; // primitive IDs
  148. };
  149. template<int M>
  150. typename TriangleM<M>::Type TriangleM<M>::type;
  151. typedef TriangleM<4> Triangle4;
  152. }