scene_points.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. // Copyright 2009-2021 Intel Corporation
  2. // SPDX-License-Identifier: Apache-2.0
  3. #pragma once
  4. #include "buffer.h"
  5. #include "default.h"
  6. #include "geometry.h"
  7. namespace embree
  8. {
  9. /*! represents an array of points */
  10. struct Points : public Geometry
  11. {
  12. /*! type of this geometry */
  13. static const Geometry::GTypeMask geom_type = Geometry::MTY_POINTS;
  14. public:
  15. /*! line segments construction */
  16. Points(Device* device, Geometry::GType gtype);
  17. public:
  18. void setMask(unsigned mask);
  19. void setNumTimeSteps(unsigned int numTimeSteps);
  20. void setVertexAttributeCount(unsigned int N);
  21. void setBuffer(RTCBufferType type,
  22. unsigned int slot,
  23. RTCFormat format,
  24. const Ref<Buffer>& buffer,
  25. size_t offset,
  26. size_t stride,
  27. unsigned int num);
  28. void* getBuffer(RTCBufferType type, unsigned int slot);
  29. void updateBuffer(RTCBufferType type, unsigned int slot);
  30. void commit();
  31. bool verify();
  32. void setMaxRadiusScale(float s);
  33. void addElementsToCount (GeometryCounts & counts) const;
  34. public:
  35. /*! returns the number of vertices */
  36. __forceinline size_t numVertices() const {
  37. return vertices[0].size();
  38. }
  39. /*! returns i'th vertex of the first time step */
  40. __forceinline Vec3ff vertex(size_t i) const {
  41. return vertices0[i];
  42. }
  43. /*! returns i'th vertex of the first time step */
  44. __forceinline const char* vertexPtr(size_t i) const {
  45. return vertices0.getPtr(i);
  46. }
  47. /*! returns i'th normal of the first time step */
  48. __forceinline Vec3fa normal(size_t i) const {
  49. return normals0[i];
  50. }
  51. /*! returns i'th radius of the first time step */
  52. __forceinline float radius(size_t i) const {
  53. return vertices0[i].w;
  54. }
  55. /*! returns i'th vertex of itime'th timestep */
  56. __forceinline Vec3ff vertex(size_t i, size_t itime) const {
  57. return vertices[itime][i];
  58. }
  59. /*! returns i'th vertex of itime'th timestep */
  60. __forceinline const char* vertexPtr(size_t i, size_t itime) const {
  61. return vertices[itime].getPtr(i);
  62. }
  63. /*! returns i'th normal of itime'th timestep */
  64. __forceinline Vec3fa normal(size_t i, size_t itime) const {
  65. return normals[itime][i];
  66. }
  67. /*! returns i'th radius of itime'th timestep */
  68. __forceinline float radius(size_t i, size_t itime) const {
  69. return vertices[itime][i].w;
  70. }
  71. /*! calculates bounding box of i'th line segment */
  72. __forceinline BBox3fa bounds(const Vec3ff& v0) const {
  73. return enlarge(BBox3fa(v0), maxRadiusScale*Vec3fa(v0.w));
  74. }
  75. /*! calculates bounding box of i'th line segment */
  76. __forceinline BBox3fa bounds(size_t i) const
  77. {
  78. const Vec3ff v0 = vertex(i);
  79. return bounds(v0);
  80. }
  81. /*! calculates bounding box of i'th line segment for the itime'th time step */
  82. __forceinline BBox3fa bounds(size_t i, size_t itime) const
  83. {
  84. const Vec3ff v0 = vertex(i, itime);
  85. return bounds(v0);
  86. }
  87. /*! calculates bounding box of i'th line segment */
  88. __forceinline BBox3fa bounds(const LinearSpace3fa& space, size_t i) const
  89. {
  90. const Vec3ff v0 = vertex(i);
  91. const Vec3ff w0(xfmVector(space, (Vec3fa)v0), v0.w);
  92. return bounds(w0);
  93. }
  94. /*! calculates bounding box of i'th line segment for the itime'th time step */
  95. __forceinline BBox3fa bounds(const LinearSpace3fa& space, size_t i, size_t itime) const
  96. {
  97. const Vec3ff v0 = vertex(i, itime);
  98. const Vec3ff w0(xfmVector(space, (Vec3fa)v0), v0.w);
  99. return bounds(w0);
  100. }
  101. /*! check if the i'th primitive is valid at the itime'th timestep */
  102. __forceinline bool valid(size_t i, size_t itime) const {
  103. return valid(i, make_range(itime, itime));
  104. }
  105. /*! check if the i'th primitive is valid between the specified time range */
  106. __forceinline bool valid(size_t i, const range<size_t>& itime_range) const
  107. {
  108. const unsigned int index = (unsigned int)i;
  109. if (index >= numVertices())
  110. return false;
  111. for (size_t itime = itime_range.begin(); itime <= itime_range.end(); itime++) {
  112. const Vec3ff v0 = vertex(index + 0, itime);
  113. if (unlikely(!isvalid4(v0)))
  114. return false;
  115. if (v0.w < 0.0f)
  116. return false;
  117. }
  118. return true;
  119. }
  120. /*! calculates the linear bounds of the i'th primitive at the itimeGlobal'th time segment */
  121. __forceinline LBBox3fa linearBounds(size_t i, size_t itime) const {
  122. return LBBox3fa(bounds(i, itime + 0), bounds(i, itime + 1));
  123. }
  124. /*! calculates the build bounds of the i'th primitive, if it's valid */
  125. __forceinline bool buildBounds(size_t i, BBox3fa* bbox) const
  126. {
  127. if (!valid(i, 0))
  128. return false;
  129. *bbox = bounds(i);
  130. return true;
  131. }
  132. /*! calculates the build bounds of the i'th primitive at the itime'th time segment, if it's valid */
  133. __forceinline bool buildBounds(size_t i, size_t itime, BBox3fa& bbox) const
  134. {
  135. if (!valid(i, itime + 0) || !valid(i, itime + 1))
  136. return false;
  137. bbox = bounds(i, itime); // use bounds of first time step in builder
  138. return true;
  139. }
  140. /*! calculates the linear bounds of the i'th primitive for the specified time range */
  141. __forceinline LBBox3fa linearBounds(size_t primID, const BBox1f& dt) const {
  142. return LBBox3fa([&](size_t itime) { return bounds(primID, itime); }, dt, time_range, fnumTimeSegments);
  143. }
  144. /*! calculates the linear bounds of the i'th primitive for the specified time range */
  145. __forceinline LBBox3fa linearBounds(const LinearSpace3fa& space, size_t primID, const BBox1f& dt) const {
  146. return LBBox3fa([&](size_t itime) { return bounds(space, primID, itime); }, dt, time_range, fnumTimeSegments);
  147. }
  148. /*! calculates the linear bounds of the i'th primitive for the specified time range */
  149. __forceinline bool linearBounds(size_t i, const BBox1f& time_range, LBBox3fa& bbox) const
  150. {
  151. if (!valid(i, timeSegmentRange(time_range))) return false;
  152. bbox = linearBounds(i, time_range);
  153. return true;
  154. }
  155. /*! get fast access to first vertex buffer */
  156. __forceinline float * getCompactVertexArray () const {
  157. return (float*) vertices0.getPtr();
  158. }
  159. public:
  160. BufferView<Vec3ff> vertices0; //!< fast access to first vertex buffer
  161. BufferView<Vec3fa> normals0; //!< fast access to first normal buffer
  162. vector<BufferView<Vec3ff>> vertices; //!< vertex array for each timestep
  163. vector<BufferView<Vec3fa>> normals; //!< normal array for each timestep
  164. vector<BufferView<char>> vertexAttribs; //!< user buffers
  165. float maxRadiusScale = 1.0; //!< maximal min-width scaling of curve radii
  166. };
  167. namespace isa
  168. {
  169. struct PointsISA : public Points
  170. {
  171. PointsISA(Device* device, Geometry::GType gtype) : Points(device, gtype) {}
  172. Vec3fa computeDirection(unsigned int primID) const
  173. {
  174. return Vec3fa(1, 0, 0);
  175. }
  176. Vec3fa computeDirection(unsigned int primID, size_t time) const
  177. {
  178. return Vec3fa(1, 0, 0);
  179. }
  180. PrimInfo createPrimRefArray(mvector<PrimRef>& prims, const range<size_t>& r, size_t k, unsigned int geomID) const
  181. {
  182. PrimInfo pinfo(empty);
  183. for (size_t j = r.begin(); j < r.end(); j++) {
  184. BBox3fa bounds = empty;
  185. if (!buildBounds(j, &bounds))
  186. continue;
  187. const PrimRef prim(bounds, geomID, unsigned(j));
  188. pinfo.add_center2(prim);
  189. prims[k++] = prim;
  190. }
  191. return pinfo;
  192. }
  193. PrimInfo createPrimRefArrayMB(mvector<PrimRef>& prims, size_t itime, const range<size_t>& r, size_t k, unsigned int geomID) const
  194. {
  195. PrimInfo pinfo(empty);
  196. for (size_t j = r.begin(); j < r.end(); j++) {
  197. BBox3fa bounds = empty;
  198. if (!buildBounds(j, itime, bounds))
  199. continue;
  200. const PrimRef prim(bounds, geomID, unsigned(j));
  201. pinfo.add_center2(prim);
  202. prims[k++] = prim;
  203. }
  204. return pinfo;
  205. }
  206. PrimInfoMB createPrimRefMBArray(mvector<PrimRefMB>& prims,
  207. const BBox1f& t0t1,
  208. const range<size_t>& r,
  209. size_t k,
  210. unsigned int geomID) const
  211. {
  212. PrimInfoMB pinfo(empty);
  213. for (size_t j = r.begin(); j < r.end(); j++) {
  214. if (!valid(j, timeSegmentRange(t0t1)))
  215. continue;
  216. const PrimRefMB prim(linearBounds(j, t0t1), this->numTimeSegments(), this->time_range, this->numTimeSegments(), geomID, unsigned(j));
  217. pinfo.add_primref(prim);
  218. prims[k++] = prim;
  219. }
  220. return pinfo;
  221. }
  222. BBox3fa vbounds(size_t i) const
  223. {
  224. return bounds(i);
  225. }
  226. BBox3fa vbounds(const LinearSpace3fa& space, size_t i) const
  227. {
  228. return bounds(space, i);
  229. }
  230. LBBox3fa vlinearBounds(size_t primID, const BBox1f& time_range) const
  231. {
  232. return linearBounds(primID, time_range);
  233. }
  234. LBBox3fa vlinearBounds(const LinearSpace3fa& space, size_t primID, const BBox1f& time_range) const
  235. {
  236. return linearBounds(space, primID, time_range);
  237. }
  238. };
  239. } // namespace isa
  240. DECLARE_ISA_FUNCTION(Points*, createPoints, Device* COMMA Geometry::GType);
  241. } // namespace embree