subd_patch.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright 2011-2013 Blender Foundation
  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. /* Parts adapted from code in the public domain in NVidia Mesh Tools. */
  17. #include "render/mesh.h"
  18. #include "subd/subd_patch.h"
  19. #include "util/util_math.h"
  20. #include "util/util_types.h"
  21. CCL_NAMESPACE_BEGIN
  22. /* De Casteljau Evaluation */
  23. static void decasteljau_cubic(float3 *P, float3 *dt, float t, const float3 cp[4])
  24. {
  25. float3 d0 = cp[0] + t * (cp[1] - cp[0]);
  26. float3 d1 = cp[1] + t * (cp[2] - cp[1]);
  27. float3 d2 = cp[2] + t * (cp[3] - cp[2]);
  28. d0 += t * (d1 - d0);
  29. d1 += t * (d2 - d1);
  30. *P = d0 + t * (d1 - d0);
  31. if (dt)
  32. *dt = d1 - d0;
  33. }
  34. static void decasteljau_bicubic(
  35. float3 *P, float3 *du, float3 *dv, const float3 cp[16], float u, float v)
  36. {
  37. float3 ucp[4], utn[4];
  38. /* interpolate over u */
  39. decasteljau_cubic(ucp + 0, utn + 0, u, cp);
  40. decasteljau_cubic(ucp + 1, utn + 1, u, cp + 4);
  41. decasteljau_cubic(ucp + 2, utn + 2, u, cp + 8);
  42. decasteljau_cubic(ucp + 3, utn + 3, u, cp + 12);
  43. /* interpolate over v */
  44. decasteljau_cubic(P, dv, v, ucp);
  45. if (du)
  46. decasteljau_cubic(du, NULL, v, utn);
  47. }
  48. /* Linear Quad Patch */
  49. void LinearQuadPatch::eval(float3 *P, float3 *dPdu, float3 *dPdv, float3 *N, float u, float v)
  50. {
  51. float3 d0 = interp(hull[0], hull[1], u);
  52. float3 d1 = interp(hull[2], hull[3], u);
  53. *P = interp(d0, d1, v);
  54. if (dPdu && dPdv) {
  55. *dPdu = interp(hull[1] - hull[0], hull[3] - hull[2], v);
  56. *dPdv = interp(hull[2] - hull[0], hull[3] - hull[1], u);
  57. }
  58. if (N) {
  59. *N = normalize(
  60. interp(interp(normals[0], normals[1], u), interp(normals[2], normals[3], u), v));
  61. }
  62. }
  63. BoundBox LinearQuadPatch::bound()
  64. {
  65. BoundBox bbox = BoundBox::empty;
  66. for (int i = 0; i < 4; i++)
  67. bbox.grow(hull[i]);
  68. return bbox;
  69. }
  70. /* Bicubic Patch */
  71. void BicubicPatch::eval(float3 *P, float3 *dPdu, float3 *dPdv, float3 *N, float u, float v)
  72. {
  73. if (N) {
  74. float3 dPdu_, dPdv_;
  75. decasteljau_bicubic(P, &dPdu_, &dPdv_, hull, u, v);
  76. if (dPdu && dPdv) {
  77. *dPdu = dPdu_;
  78. *dPdv = dPdv_;
  79. }
  80. *N = normalize(cross(dPdu_, dPdv_));
  81. }
  82. else {
  83. decasteljau_bicubic(P, dPdu, dPdv, hull, u, v);
  84. }
  85. }
  86. BoundBox BicubicPatch::bound()
  87. {
  88. BoundBox bbox = BoundBox::empty;
  89. for (int i = 0; i < 16; i++)
  90. bbox.grow(hull[i]);
  91. return bbox;
  92. }
  93. CCL_NAMESPACE_END