bezier1v.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. #include "../subdiv/bezier_curve.h"
  19. #include "../common/primref.h"
  20. namespace embree
  21. {
  22. struct BezierPrim // FIXME: rename to BezierRef, remove!!!
  23. {
  24. public:
  25. /*! Default constructor. */
  26. __forceinline BezierPrim () {}
  27. /*! Construction from vertices and IDs. */
  28. __forceinline BezierPrim (bool hair,
  29. const Vec3fa& p0, const Vec3fa& p1, const Vec3fa& p2, const Vec3fa& p3, const int N,
  30. const unsigned int geomID, const unsigned int primID)
  31. : p0(p0), p1(p1), p2(p2), p3(p3), N(N), geom(geomID), prim(primID), hair(hair) {}
  32. /*! access hidden members */
  33. __forceinline unsigned int primID() const {
  34. return prim;
  35. }
  36. __forceinline unsigned int geomID() const {
  37. return geom;
  38. }
  39. /*! calculate the center of the curve */
  40. __forceinline const Vec3fa center2() const {
  41. return p0+p3;
  42. }
  43. /*! calculate the center of the curve */
  44. __forceinline const Vec3fa center2(const AffineSpace3fa& space) const {
  45. return xfmPoint(space,p0)+xfmPoint(space,p3);
  46. }
  47. /*! calculate the bounds of the curve */
  48. __forceinline const BBox3fa bounds() const
  49. {
  50. const Curve3fa curve(p0,p1,p2,p3);
  51. if (likely(hair)) return curve.tessellatedBounds(N);
  52. else return curve.accurateBounds();
  53. }
  54. /*! size for bin heuristic is 1 */
  55. __forceinline unsigned size() const {
  56. return 1;
  57. }
  58. /*! returns bounds and centroid used for binning */
  59. __forceinline void binBoundsAndCenter(BBox3fa& bounds_o, Vec3fa& center_o) const
  60. {
  61. bounds_o = bounds();
  62. center_o = embree::center2(bounds_o);
  63. }
  64. /*! calculate bounds in specified coordinate space */
  65. __forceinline const BBox3fa bounds(const AffineSpace3fa& space) const
  66. {
  67. Vec3fa b0 = xfmPoint(space,p0); b0.w = p0.w;
  68. Vec3fa b1 = xfmPoint(space,p1); b1.w = p1.w;
  69. Vec3fa b2 = xfmPoint(space,p2); b2.w = p2.w;
  70. Vec3fa b3 = xfmPoint(space,p3); b3.w = p3.w;
  71. const Curve3fa curve(b0,b1,b2,b3);
  72. if (likely(hair)) return curve.tessellatedBounds(N);
  73. else return curve.accurateBounds();
  74. }
  75. /*! returns bounds and centroid used for binning */
  76. __forceinline void binBoundsAndCenter(BBox3fa& bounds_o, Vec3fa& center_o, const AffineSpace3fa& space, void* user) const
  77. {
  78. bounds_o = bounds(space);
  79. center_o = embree::center2(bounds_o);
  80. }
  81. __forceinline uint64_t id64() const {
  82. return (((uint64_t)prim) << 32) + (uint64_t)geom;
  83. }
  84. friend __forceinline bool operator<(const BezierPrim& p0, const BezierPrim& p1) {
  85. return p0.id64() < p1.id64();
  86. }
  87. friend std::ostream& operator<<(std::ostream& cout, const BezierPrim& b)
  88. {
  89. return std::cout << "BezierPrim { " << std::endl <<
  90. " p0 = " << b.p0 << ", " << std::endl <<
  91. " p1 = " << b.p1 << ", " << std::endl <<
  92. " p2 = " << b.p2 << ", " << std::endl <<
  93. " p3 = " << b.p3 << ", " << std::endl <<
  94. " N = " << b.N << ", " << std::endl <<
  95. " geomID = " << b.geomID() << ", primID = " << b.primID() << std::endl <<
  96. "}";
  97. }
  98. public:
  99. Vec3fa p0; //!< 1st control point (x,y,z,r)
  100. Vec3fa p1; //!< 2nd control point (x,y,z,r)
  101. Vec3fa p2; //!< 3rd control point (x,y,z,r)
  102. Vec3fa p3; //!< 4th control point (x,y,z,r)
  103. int N; //!< tessellation rate
  104. unsigned geom; //!< geometry ID
  105. unsigned prim; //!< primitive ID
  106. int hair; //!< 0=surface, 1=hair
  107. };
  108. struct Bezier1v
  109. {
  110. struct Type : public PrimitiveType
  111. {
  112. Type ();
  113. size_t size(const char* This) const;
  114. };
  115. static Type type;
  116. public:
  117. /* Returns maximal number of stored primitives */
  118. static __forceinline size_t max_size() { return 1; }
  119. /* Returns required number of primitive blocks for N primitives */
  120. static __forceinline size_t blocks(size_t N) { return N; }
  121. public:
  122. /*! Default constructor. */
  123. __forceinline Bezier1v () {}
  124. /*! Construction from vertices and IDs. */
  125. __forceinline Bezier1v (const Vec3fa& p0, const Vec3fa& p1, const Vec3fa& p2, const Vec3fa& p3, const unsigned int geomID, const unsigned int primID)
  126. : p0(p0), p1(p1), p2(p2), p3(p3), geom(geomID), prim(primID) {}
  127. /*! return primitive ID */
  128. __forceinline unsigned int primID() const {
  129. return prim;
  130. }
  131. /*! return geometry ID */
  132. __forceinline unsigned int geomID() const {
  133. return geom;
  134. }
  135. /*! fill triangle from triangle list */
  136. __forceinline void fill(const PrimRef* prims, size_t& i, size_t end, Scene* scene)
  137. {
  138. const PrimRef& prim = prims[i];
  139. i++;
  140. const unsigned geomID = prim.geomID();
  141. const unsigned primID = prim.primID();
  142. const NativeCurves* curves = scene->get<NativeCurves>(geomID);
  143. const unsigned id = curves->curve(primID);
  144. const Vec3fa& p0 = curves->vertex(id+0);
  145. const Vec3fa& p1 = curves->vertex(id+1);
  146. const Vec3fa& p2 = curves->vertex(id+2);
  147. const Vec3fa& p3 = curves->vertex(id+3);
  148. new (this) Bezier1v(p0,p1,p2,p3,geomID,primID);
  149. }
  150. /*! fill triangle from triangle list */
  151. __forceinline void fill(const BezierPrim* prims, size_t& i, size_t end, Scene* scene)
  152. {
  153. new (this) Bezier1v(prims[i].p0,prims[i].p1,prims[i].p2,prims[i].p3,prims[i].geom,prims[i].prim);
  154. i++;
  155. }
  156. /*! calculate the center of the curve */
  157. __forceinline const Vec3fa center2() const {
  158. return p0+p3;
  159. }
  160. /*! calculate the center of the curve */
  161. __forceinline const Vec3fa center2(const AffineSpace3fa& space) const {
  162. return xfmPoint(space,p0)+xfmPoint(space,p3);
  163. }
  164. __forceinline uint64_t id64() const {
  165. return (((uint64_t)prim) << 32) + (uint64_t)geom;
  166. }
  167. friend __forceinline bool operator<(const Bezier1v& p0, const Bezier1v& p1) {
  168. return p0.id64() < p1.id64();
  169. }
  170. friend std::ostream& operator<<(std::ostream& cout, const Bezier1v& b)
  171. {
  172. return std::cout << "Bezier1v { " << std::endl <<
  173. " p0 = " << b.p0 << ", " << std::endl <<
  174. " p1 = " << b.p1 << ", " << std::endl <<
  175. " p2 = " << b.p2 << ", " << std::endl <<
  176. " p3 = " << b.p3 << ", " << std::endl <<
  177. " geomID = " << b.geomID() << ", primID = " << b.primID() << std::endl <<
  178. "}";
  179. }
  180. public:
  181. Vec3fa p0; //!< 1st control point (x,y,z,r)
  182. Vec3fa p1; //!< 2nd control point (x,y,z,r)
  183. Vec3fa p2; //!< 3rd control point (x,y,z,r)
  184. Vec3fa p3; //!< 4th control point (x,y,z,r)
  185. unsigned geom; //!< geometry ID
  186. unsigned prim; //!< primitive ID
  187. };
  188. }