bilinear_patch.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // Copyright 2009-2021 Intel Corporation
  2. // SPDX-License-Identifier: Apache-2.0
  3. #pragma once
  4. #include "catmullclark_patch.h"
  5. #include "bezier_curve.h"
  6. namespace embree
  7. {
  8. template<typename Vertex, typename Vertex_t = Vertex>
  9. class __aligned(64) BilinearPatchT
  10. {
  11. typedef CatmullClark1RingT<Vertex,Vertex_t> CatmullClarkRing;
  12. typedef CatmullClarkPatchT<Vertex,Vertex_t> CatmullClarkPatch;
  13. public:
  14. Vertex v[4];
  15. public:
  16. __forceinline BilinearPatchT () {}
  17. __forceinline BilinearPatchT (const HalfEdge* edge, const BufferView<Vertex>& vertices) {
  18. init(edge,vertices.getPtr(),vertices.getStride());
  19. }
  20. __forceinline BilinearPatchT (const HalfEdge* edge, const char* vertices, size_t stride) {
  21. init(edge,vertices,stride);
  22. }
  23. __forceinline void init (const HalfEdge* edge, const char* vertices, size_t stride)
  24. {
  25. v[0] = Vertex::loadu(vertices+edge->getStartVertexIndex()*stride); edge = edge->next();
  26. v[1] = Vertex::loadu(vertices+edge->getStartVertexIndex()*stride); edge = edge->next();
  27. v[2] = Vertex::loadu(vertices+edge->getStartVertexIndex()*stride); edge = edge->next();
  28. v[3] = Vertex::loadu(vertices+edge->getStartVertexIndex()*stride); edge = edge->next();
  29. }
  30. __forceinline BilinearPatchT (const CatmullClarkPatch& patch)
  31. {
  32. v[0] = patch.ring[0].getLimitVertex();
  33. v[1] = patch.ring[1].getLimitVertex();
  34. v[2] = patch.ring[2].getLimitVertex();
  35. v[3] = patch.ring[3].getLimitVertex();
  36. }
  37. __forceinline BBox<Vertex> bounds() const
  38. {
  39. BBox<Vertex> bounds (v[0]);
  40. bounds.extend(v[1]);
  41. bounds.extend(v[2]);
  42. bounds.extend(v[3]);
  43. return bounds;
  44. }
  45. __forceinline Vertex eval(const float uu, const float vv) const {
  46. return lerp(lerp(v[0],v[1],uu),lerp(v[3],v[2],uu),vv);
  47. }
  48. __forceinline Vertex eval_du(const float uu, const float vv) const {
  49. return lerp(v[1]-v[0],v[2]-v[3],vv);
  50. }
  51. __forceinline Vertex eval_dv(const float uu, const float vv) const {
  52. return lerp(v[3]-v[0],v[2]-v[1],uu);
  53. }
  54. __forceinline Vertex eval_dudu(const float uu, const float vv) const {
  55. return Vertex(zero);
  56. }
  57. __forceinline Vertex eval_dvdv(const float uu, const float vv) const {
  58. return Vertex(zero);
  59. }
  60. __forceinline Vertex eval_dudv(const float uu, const float vv) const {
  61. return (v[2]-v[3]) - (v[1]-v[0]);
  62. }
  63. __forceinline Vertex normal(const float uu, const float vv) const {
  64. return cross(eval_du(uu,vv),eval_dv(uu,vv));
  65. }
  66. __forceinline void eval(const float u, const float v,
  67. Vertex* P, Vertex* dPdu, Vertex* dPdv, Vertex* ddPdudu, Vertex* ddPdvdv, Vertex* ddPdudv,
  68. const float dscale = 1.0f) const
  69. {
  70. if (P) {
  71. *P = eval(u,v);
  72. }
  73. if (dPdu) {
  74. assert(dPdu); *dPdu = eval_du(u,v)*dscale;
  75. assert(dPdv); *dPdv = eval_dv(u,v)*dscale;
  76. }
  77. if (ddPdudu) {
  78. assert(ddPdudu); *ddPdudu = eval_dudu(u,v)*sqr(dscale);
  79. assert(ddPdvdv); *ddPdvdv = eval_dvdv(u,v)*sqr(dscale);
  80. assert(ddPdudv); *ddPdudv = eval_dudv(u,v)*sqr(dscale);
  81. }
  82. }
  83. template<class vfloat>
  84. __forceinline Vec3<vfloat> eval(const vfloat& uu, const vfloat& vv) const
  85. {
  86. const vfloat x = lerp(lerp(v[0].x,v[1].x,uu),lerp(v[3].x,v[2].x,uu),vv);
  87. const vfloat y = lerp(lerp(v[0].y,v[1].y,uu),lerp(v[3].y,v[2].y,uu),vv);
  88. const vfloat z = lerp(lerp(v[0].z,v[1].z,uu),lerp(v[3].z,v[2].z,uu),vv);
  89. return Vec3<vfloat>(x,y,z);
  90. }
  91. template<class vfloat>
  92. __forceinline Vec3<vfloat> eval_du(const vfloat& uu, const vfloat& vv) const
  93. {
  94. const vfloat x = lerp(v[1].x-v[0].x,v[2].x-v[3].x,vv);
  95. const vfloat y = lerp(v[1].y-v[0].y,v[2].y-v[3].y,vv);
  96. const vfloat z = lerp(v[1].z-v[0].z,v[2].z-v[3].z,vv);
  97. return Vec3<vfloat>(x,y,z);
  98. }
  99. template<class vfloat>
  100. __forceinline Vec3<vfloat> eval_dv(const vfloat& uu, const vfloat& vv) const
  101. {
  102. const vfloat x = lerp(v[3].x-v[0].x,v[2].x-v[1].x,uu);
  103. const vfloat y = lerp(v[3].y-v[0].y,v[2].y-v[1].y,uu);
  104. const vfloat z = lerp(v[3].z-v[0].z,v[2].z-v[1].z,uu);
  105. return Vec3<vfloat>(x,y,z);
  106. }
  107. template<typename vfloat>
  108. __forceinline Vec3<vfloat> normal(const vfloat& uu, const vfloat& vv) const {
  109. return cross(eval_du(uu,vv),eval_dv(uu,vv));
  110. }
  111. template<class vfloat>
  112. __forceinline vfloat eval(const size_t i, const vfloat& uu, const vfloat& vv) const {
  113. return lerp(lerp(v[0][i],v[1][i],uu),lerp(v[3][i],v[2][i],uu),vv);
  114. }
  115. template<class vfloat>
  116. __forceinline vfloat eval_du(const size_t i, const vfloat& uu, const vfloat& vv) const {
  117. return lerp(v[1][i]-v[0][i],v[2][i]-v[3][i],vv);
  118. }
  119. template<class vfloat>
  120. __forceinline vfloat eval_dv(const size_t i, const vfloat& uu, const vfloat& vv) const {
  121. return lerp(v[3][i]-v[0][i],v[2][i]-v[1][i],uu);
  122. }
  123. template<class vfloat>
  124. __forceinline vfloat eval_dudu(const size_t i, const vfloat& uu, const vfloat& vv) const {
  125. return vfloat(zero);
  126. }
  127. template<class vfloat>
  128. __forceinline vfloat eval_dvdv(const size_t i, const vfloat& uu, const vfloat& vv) const {
  129. return vfloat(zero);
  130. }
  131. template<class vfloat>
  132. __forceinline vfloat eval_dudv(const size_t i, const vfloat& uu, const vfloat& vv) const {
  133. return (v[2][i]-v[3][i]) - (v[1][i]-v[0][i]);
  134. }
  135. template<typename vbool, typename vfloat>
  136. __forceinline void eval(const vbool& valid, const vfloat& uu, const vfloat& vv,
  137. float* P, float* dPdu, float* dPdv, float* ddPdudu, float* ddPdvdv, float* ddPdudv,
  138. const float dscale, const size_t dstride, const size_t N) const
  139. {
  140. if (P) {
  141. for (size_t i=0; i<N; i++) vfloat::store(valid,P+i*dstride,eval(i,uu,vv));
  142. }
  143. if (dPdu) {
  144. for (size_t i=0; i<N; i++) {
  145. assert(dPdu); vfloat::store(valid,dPdu+i*dstride,eval_du(i,uu,vv)*dscale);
  146. assert(dPdv); vfloat::store(valid,dPdv+i*dstride,eval_dv(i,uu,vv)*dscale);
  147. }
  148. }
  149. if (ddPdudu) {
  150. for (size_t i=0; i<N; i++) {
  151. assert(ddPdudu); vfloat::store(valid,ddPdudu+i*dstride,eval_dudu(i,uu,vv)*sqr(dscale));
  152. assert(ddPdvdv); vfloat::store(valid,ddPdvdv+i*dstride,eval_dvdv(i,uu,vv)*sqr(dscale));
  153. assert(ddPdudv); vfloat::store(valid,ddPdudv+i*dstride,eval_dudv(i,uu,vv)*sqr(dscale));
  154. }
  155. }
  156. }
  157. };
  158. typedef BilinearPatchT<Vec3fa,Vec3fa_t> BilinearPatch3fa;
  159. }