Blender  V3.3
patch.cpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: Apache-2.0
2  * Copyright 2011-2022 Blender Foundation */
3 
4 /* Parts adapted from code in the public domain in NVidia Mesh Tools. */
5 
6 #include "scene/mesh.h"
7 
8 #include "subd/patch.h"
9 
10 #include "util/math.h"
11 #include "util/types.h"
12 
14 
15 /* De Casteljau Evaluation */
16 
17 static void decasteljau_cubic(float3 *P, float3 *dt, float t, const float3 cp[4])
18 {
19  float3 d0 = cp[0] + t * (cp[1] - cp[0]);
20  float3 d1 = cp[1] + t * (cp[2] - cp[1]);
21  float3 d2 = cp[2] + t * (cp[3] - cp[2]);
22 
23  d0 += t * (d1 - d0);
24  d1 += t * (d2 - d1);
25 
26  *P = d0 + t * (d1 - d0);
27  if (dt)
28  *dt = d1 - d0;
29 }
30 
31 static void decasteljau_bicubic(
32  float3 *P, float3 *du, float3 *dv, const float3 cp[16], float u, float v)
33 {
34  float3 ucp[4], utn[4];
35 
36  /* interpolate over u */
37  decasteljau_cubic(ucp + 0, utn + 0, u, cp);
38  decasteljau_cubic(ucp + 1, utn + 1, u, cp + 4);
39  decasteljau_cubic(ucp + 2, utn + 2, u, cp + 8);
40  decasteljau_cubic(ucp + 3, utn + 3, u, cp + 12);
41 
42  /* interpolate over v */
43  decasteljau_cubic(P, dv, v, ucp);
44  if (du)
45  decasteljau_cubic(du, NULL, v, utn);
46 }
47 
48 /* Linear Quad Patch */
49 
50 void LinearQuadPatch::eval(float3 *P, float3 *dPdu, float3 *dPdv, float3 *N, float u, float v)
51 {
52  float3 d0 = interp(hull[0], hull[1], u);
53  float3 d1 = interp(hull[2], hull[3], u);
54 
55  *P = interp(d0, d1, v);
56 
57  if (dPdu && dPdv) {
58  *dPdu = interp(hull[1] - hull[0], hull[3] - hull[2], v);
59  *dPdv = interp(hull[2] - hull[0], hull[3] - hull[1], u);
60  }
61 
62  if (N) {
63  *N = normalize(
64  interp(interp(normals[0], normals[1], u), interp(normals[2], normals[3], u), v));
65  }
66 }
67 
69 {
71 
72  for (int i = 0; i < 4; i++)
73  bbox.grow(hull[i]);
74 
75  return bbox;
76 }
77 
78 /* Bicubic Patch */
79 
80 void BicubicPatch::eval(float3 *P, float3 *dPdu, float3 *dPdv, float3 *N, float u, float v)
81 {
82  if (N) {
83  float3 dPdu_, dPdv_;
84  decasteljau_bicubic(P, &dPdu_, &dPdv_, hull, u, v);
85 
86  if (dPdu && dPdv) {
87  *dPdu = dPdu_;
88  *dPdv = dPdv_;
89  }
90 
91  *N = normalize(cross(dPdu_, dPdv_));
92  }
93  else {
94  decasteljau_bicubic(P, dPdu, dPdv, hull, u, v);
95  }
96 }
97 
99 {
100  BoundBox bbox = BoundBox::empty;
101 
102  for (int i = 0; i < 16; i++)
103  bbox.grow(hull[i]);
104 
105  return bbox;
106 }
107 
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble u2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLdouble GLdouble v2 _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLdouble GLdouble nz _GL_VOID_RET _GL_VOID GLfloat GLfloat nz _GL_VOID_RET _GL_VOID GLint GLint nz _GL_VOID_RET _GL_VOID GLshort GLshort nz _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const GLfloat *values _GL_VOID_RET _GL_VOID GLsizei const GLushort *values _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID const GLuint const GLclampf *priorities _GL_VOID_RET _GL_VOID GLdouble y _GL_VOID_RET _GL_VOID GLfloat y _GL_VOID_RET _GL_VOID GLint y _GL_VOID_RET _GL_VOID GLshort y _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLfloat GLfloat z _GL_VOID_RET _GL_VOID GLint GLint z _GL_VOID_RET _GL_VOID GLshort GLshort z _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble w _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat w _GL_VOID_RET _GL_VOID GLint GLint GLint w _GL_VOID_RET _GL_VOID GLshort GLshort GLshort w _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble y2 _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat y2 _GL_VOID_RET _GL_VOID GLint GLint GLint y2 _GL_VOID_RET _GL_VOID GLshort GLshort GLshort y2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLuint *buffer _GL_VOID_RET _GL_VOID GLdouble t _GL_VOID_RET _GL_VOID GLfloat t _GL_VOID_RET _GL_VOID GLint t _GL_VOID_RET _GL_VOID GLshort t _GL_VOID_RET _GL_VOID GLdouble t
ATTR_WARN_UNUSED_RESULT const BMVert * v
BoundBox bound()
Definition: patch.cpp:98
float3 hull[16]
Definition: subd/patch.h:42
void eval(float3 *P, float3 *dPdu, float3 *dPdv, float3 *N, float u, float v)
Definition: patch.cpp:80
float3 hull[4]
Definition: subd/patch.h:31
BoundBox bound()
Definition: patch.cpp:68
float3 normals[4]
Definition: subd/patch.h:32
void eval(float3 *P, float3 *dPdu, float3 *dPdv, float3 *N, float u, float v)
Definition: patch.cpp:50
#define CCL_NAMESPACE_END
Definition: cuda/compat.h:9
ccl_device_inline float2 interp(const float2 &a, const float2 &b, float t)
Definition: math_float2.h:232
static float P(float k)
Definition: math_interp.c:25
#define N
vec_base< T, 3 > cross(const vec_base< T, 3 > &a, const vec_base< T, 3 > &b)
vec_base< T, Size > normalize(const vec_base< T, Size > &v)
static void decasteljau_bicubic(float3 *P, float3 *du, float3 *dv, const float3 cp[16], float u, float v)
Definition: patch.cpp:31
static CCL_NAMESPACE_BEGIN void decasteljau_cubic(float3 *P, float3 *dt, float t, const float3 cp[4])
Definition: patch.cpp:17
@ empty
Definition: boundbox.h:35
__forceinline void grow(const float3 &pt)
Definition: boundbox.h:42