scene_points.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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 for specified time */
  60. __forceinline Vec3ff vertex(size_t i, float time) const
  61. {
  62. float ftime;
  63. const size_t itime = timeSegment(time, ftime);
  64. const float t0 = 1.0f - ftime;
  65. const float t1 = ftime;
  66. Vec3ff v0 = vertex(i, itime+0);
  67. Vec3ff v1 = vertex(i, itime+1);
  68. return madd(Vec3ff(t0),v0,t1*v1);
  69. }
  70. /*! returns i'th vertex of for specified time */
  71. __forceinline Vec3ff vertex_safe(size_t i, float time) const
  72. {
  73. if (hasMotionBlur()) return vertex(i,time);
  74. else return vertex(i);
  75. }
  76. /*! returns i'th vertex of itime'th timestep */
  77. __forceinline const char* vertexPtr(size_t i, size_t itime) const {
  78. return vertices[itime].getPtr(i);
  79. }
  80. /*! returns i'th normal of itime'th timestep */
  81. __forceinline Vec3fa normal(size_t i, size_t itime) const {
  82. return normals[itime][i];
  83. }
  84. /*! returns i'th normal of for specified time */
  85. __forceinline Vec3fa normal(size_t i, float time) const
  86. {
  87. float ftime;
  88. const size_t itime = timeSegment(time, ftime);
  89. const float t0 = 1.0f - ftime;
  90. const float t1 = ftime;
  91. Vec3fa n0 = normal(i, itime+0);
  92. Vec3fa n1 = normal(i, itime+1);
  93. return madd(Vec3fa(t0),n0,t1*n1);
  94. }
  95. /*! returns i'th normal of for specified time */
  96. __forceinline Vec3fa normal_safe(size_t i, float time) const
  97. {
  98. if (hasMotionBlur()) return normal(i,time);
  99. else return normal(i);
  100. }
  101. /*! returns i'th radius of itime'th timestep */
  102. __forceinline float radius(size_t i, size_t itime) const {
  103. return vertices[itime][i].w;
  104. }
  105. /*! returns i'th radius of for specified time */
  106. __forceinline float radius(size_t i, float time) const
  107. {
  108. float ftime;
  109. const size_t itime = timeSegment(time, ftime);
  110. const float t0 = 1.0f - ftime;
  111. const float t1 = ftime;
  112. float r0 = radius(i, itime+0);
  113. float r1 = radius(i, itime+1);
  114. return madd(t0,r0,t1*r1);
  115. }
  116. /*! returns i'th radius of for specified time */
  117. __forceinline float radius_safe(size_t i, float time) const
  118. {
  119. if (hasMotionBlur()) return radius(i,time);
  120. else return radius(i);
  121. }
  122. /*! calculates bounding box of i'th line segment */
  123. __forceinline BBox3fa bounds(const Vec3ff& v0) const {
  124. return enlarge(BBox3fa(v0), maxRadiusScale*Vec3fa(v0.w));
  125. }
  126. /*! calculates bounding box of i'th line segment */
  127. __forceinline BBox3fa bounds(size_t i) const
  128. {
  129. const Vec3ff v0 = vertex(i);
  130. return bounds(v0);
  131. }
  132. /*! calculates bounding box of i'th line segment for the itime'th time step */
  133. __forceinline BBox3fa bounds(size_t i, size_t itime) const
  134. {
  135. const Vec3ff v0 = vertex(i, itime);
  136. return bounds(v0);
  137. }
  138. /*! calculates bounding box of i'th line segment */
  139. __forceinline BBox3fa bounds(const LinearSpace3fa& space, size_t i) const
  140. {
  141. const Vec3ff v0 = vertex(i);
  142. const Vec3ff w0(xfmVector(space, (Vec3fa)v0), v0.w);
  143. return bounds(w0);
  144. }
  145. /*! calculates bounding box of i'th line segment for the itime'th time step */
  146. __forceinline BBox3fa bounds(const LinearSpace3fa& space, size_t i, size_t itime) const
  147. {
  148. const Vec3ff v0 = vertex(i, itime);
  149. const Vec3ff w0(xfmVector(space, (Vec3fa)v0), v0.w);
  150. return bounds(w0);
  151. }
  152. /*! check if the i'th primitive is valid at the itime'th timestep */
  153. __forceinline bool valid(size_t i, size_t itime) const {
  154. return valid(i, make_range(itime, itime));
  155. }
  156. /*! check if the i'th primitive is valid between the specified time range */
  157. __forceinline bool valid(size_t i, const range<size_t>& itime_range) const
  158. {
  159. const unsigned int index = (unsigned int)i;
  160. if (index >= numVertices())
  161. return false;
  162. for (size_t itime = itime_range.begin(); itime <= itime_range.end(); itime++) {
  163. const Vec3ff v0 = vertex(index + 0, itime);
  164. if (unlikely(!isvalid4(v0)))
  165. return false;
  166. if (v0.w < 0.0f)
  167. return false;
  168. }
  169. return true;
  170. }
  171. /*! calculates the linear bounds of the i'th primitive at the itimeGlobal'th time segment */
  172. __forceinline LBBox3fa linearBounds(size_t i, size_t itime) const {
  173. return LBBox3fa(bounds(i, itime + 0), bounds(i, itime + 1));
  174. }
  175. /*! calculates the build bounds of the i'th primitive, if it's valid */
  176. __forceinline bool buildBounds(size_t i, BBox3fa* bbox) const
  177. {
  178. if (!valid(i, 0))
  179. return false;
  180. *bbox = bounds(i);
  181. return true;
  182. }
  183. /*! calculates the build bounds of the i'th primitive at the itime'th time segment, if it's valid */
  184. __forceinline bool buildBounds(size_t i, size_t itime, BBox3fa& bbox) const
  185. {
  186. if (!valid(i, itime + 0) || !valid(i, itime + 1))
  187. return false;
  188. bbox = bounds(i, itime); // use bounds of first time step in builder
  189. return true;
  190. }
  191. /*! calculates the linear bounds of the i'th primitive for the specified time range */
  192. __forceinline LBBox3fa linearBounds(size_t primID, const BBox1f& dt) const {
  193. return LBBox3fa([&](size_t itime) { return bounds(primID, itime); }, dt, time_range, fnumTimeSegments);
  194. }
  195. /*! calculates the linear bounds of the i'th primitive for the specified time range */
  196. __forceinline LBBox3fa linearBounds(const LinearSpace3fa& space, size_t primID, const BBox1f& dt) const {
  197. return LBBox3fa([&](size_t itime) { return bounds(space, primID, itime); }, dt, time_range, fnumTimeSegments);
  198. }
  199. /*! calculates the linear bounds of the i'th primitive for the specified time range */
  200. __forceinline bool linearBounds(size_t i, const BBox1f& time_range, LBBox3fa& bbox) const
  201. {
  202. if (!valid(i, timeSegmentRange(time_range))) return false;
  203. bbox = linearBounds(i, time_range);
  204. return true;
  205. }
  206. /*! get fast access to first vertex buffer */
  207. __forceinline float * getCompactVertexArray () const {
  208. return (float*) vertices0.getPtr();
  209. }
  210. __forceinline float projectedPrimitiveArea(const size_t i) const {
  211. const float R = radius(i);
  212. return 1 + 2*M_PI*R*R;
  213. }
  214. public:
  215. BufferView<Vec3ff> vertices0; //!< fast access to first vertex buffer
  216. BufferView<Vec3fa> normals0; //!< fast access to first normal buffer
  217. Device::vector<BufferView<Vec3ff>> vertices = device; //!< vertex array for each timestep
  218. Device::vector<BufferView<Vec3fa>> normals = device; //!< normal array for each timestep
  219. Device::vector<BufferView<char>> vertexAttribs = device; //!< user buffers
  220. float maxRadiusScale = 1.0; //!< maximal min-width scaling of curve radii
  221. };
  222. namespace isa
  223. {
  224. struct PointsISA : public Points
  225. {
  226. PointsISA(Device* device, Geometry::GType gtype) : Points(device, gtype) {}
  227. Vec3fa computeDirection(unsigned int primID) const
  228. {
  229. return Vec3fa(1, 0, 0);
  230. }
  231. Vec3fa computeDirection(unsigned int primID, size_t time) const
  232. {
  233. return Vec3fa(1, 0, 0);
  234. }
  235. PrimInfo createPrimRefArray(PrimRef* prims, const range<size_t>& r, size_t k, unsigned int geomID) const
  236. {
  237. PrimInfo pinfo(empty);
  238. for (size_t j = r.begin(); j < r.end(); j++) {
  239. BBox3fa bounds = empty;
  240. if (!buildBounds(j, &bounds))
  241. continue;
  242. const PrimRef prim(bounds, geomID, unsigned(j));
  243. pinfo.add_center2(prim);
  244. prims[k++] = prim;
  245. }
  246. return pinfo;
  247. }
  248. PrimInfo createPrimRefArrayMB(mvector<PrimRef>& prims, size_t itime, const range<size_t>& r, size_t k, unsigned int geomID) const
  249. {
  250. PrimInfo pinfo(empty);
  251. for (size_t j = r.begin(); j < r.end(); j++) {
  252. BBox3fa bounds = empty;
  253. if (!buildBounds(j, itime, bounds))
  254. continue;
  255. const PrimRef prim(bounds, geomID, unsigned(j));
  256. pinfo.add_center2(prim);
  257. prims[k++] = prim;
  258. }
  259. return pinfo;
  260. }
  261. PrimInfo createPrimRefArrayMB(PrimRef* prims, const BBox1f& time_range, const range<size_t>& r, size_t k, unsigned int geomID) const
  262. {
  263. PrimInfo pinfo(empty);
  264. const BBox1f t0t1 = BBox1f::intersect(getTimeRange(), time_range);
  265. if (t0t1.empty()) return pinfo;
  266. for (size_t j = r.begin(); j < r.end(); j++) {
  267. LBBox3fa lbounds = empty;
  268. if (!linearBounds(j, t0t1, lbounds))
  269. continue;
  270. const PrimRef prim(lbounds.bounds(), geomID, unsigned(j));
  271. pinfo.add_center2(prim);
  272. prims[k++] = prim;
  273. }
  274. return pinfo;
  275. }
  276. PrimInfoMB createPrimRefMBArray(mvector<PrimRefMB>& prims,
  277. const BBox1f& t0t1,
  278. const range<size_t>& r,
  279. size_t k,
  280. unsigned int geomID) const
  281. {
  282. PrimInfoMB pinfo(empty);
  283. for (size_t j = r.begin(); j < r.end(); j++) {
  284. if (!valid(j, timeSegmentRange(t0t1)))
  285. continue;
  286. const PrimRefMB prim(linearBounds(j, t0t1), this->numTimeSegments(), this->time_range, this->numTimeSegments(), geomID, unsigned(j));
  287. pinfo.add_primref(prim);
  288. prims[k++] = prim;
  289. }
  290. return pinfo;
  291. }
  292. BBox3fa vbounds(size_t i) const
  293. {
  294. return bounds(i);
  295. }
  296. BBox3fa vbounds(const LinearSpace3fa& space, size_t i) const
  297. {
  298. return bounds(space, i);
  299. }
  300. LBBox3fa vlinearBounds(size_t primID, const BBox1f& time_range) const
  301. {
  302. return linearBounds(primID, time_range);
  303. }
  304. LBBox3fa vlinearBounds(const LinearSpace3fa& space, size_t primID, const BBox1f& time_range) const
  305. {
  306. return linearBounds(space, primID, time_range);
  307. }
  308. };
  309. } // namespace isa
  310. DECLARE_ISA_FUNCTION(Points*, createPoints, Device* COMMA Geometry::GType);
  311. } // namespace embree