util_transform.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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. #ifndef __UTIL_TRANSFORM_H__
  17. #define __UTIL_TRANSFORM_H__
  18. #ifndef __KERNEL_GPU__
  19. # include <string.h>
  20. #endif
  21. #include "util/util_math.h"
  22. #include "util/util_types.h"
  23. CCL_NAMESPACE_BEGIN
  24. /* Affine transformation, stored as 4x3 matrix. */
  25. typedef struct Transform {
  26. float4 x, y, z;
  27. #ifndef __KERNEL_GPU__
  28. float4 operator[](int i) const
  29. {
  30. return *(&x + i);
  31. }
  32. float4 &operator[](int i)
  33. {
  34. return *(&x + i);
  35. }
  36. #endif
  37. } Transform;
  38. /* Transform decomposed in rotation/translation/scale. we use the same data
  39. * structure as Transform, and tightly pack decomposition into it. first the
  40. * rotation (4), then translation (3), then 3x3 scale matrix (9). */
  41. typedef struct DecomposedTransform {
  42. float4 x, y, z, w;
  43. } DecomposedTransform;
  44. /* Functions */
  45. ccl_device_inline float3 transform_point(const Transform *t, const float3 a)
  46. {
  47. /* TODO(sergey): Disabled for now, causes crashes in certain cases. */
  48. #if defined(__KERNEL_SSE__) && defined(__KERNEL_SSE2__)
  49. ssef x, y, z, w, aa;
  50. aa = a.m128;
  51. x = _mm_loadu_ps(&t->x.x);
  52. y = _mm_loadu_ps(&t->y.x);
  53. z = _mm_loadu_ps(&t->z.x);
  54. w = _mm_set_ps(1.0f, 0.0f, 0.0f, 0.0f);
  55. _MM_TRANSPOSE4_PS(x, y, z, w);
  56. ssef tmp = shuffle<0>(aa) * x;
  57. tmp = madd(shuffle<1>(aa), y, tmp);
  58. tmp = madd(shuffle<2>(aa), z, tmp);
  59. tmp += w;
  60. return float3(tmp.m128);
  61. #else
  62. float3 c = make_float3(a.x * t->x.x + a.y * t->x.y + a.z * t->x.z + t->x.w,
  63. a.x * t->y.x + a.y * t->y.y + a.z * t->y.z + t->y.w,
  64. a.x * t->z.x + a.y * t->z.y + a.z * t->z.z + t->z.w);
  65. return c;
  66. #endif
  67. }
  68. ccl_device_inline float3 transform_direction(const Transform *t, const float3 a)
  69. {
  70. #if defined(__KERNEL_SSE__) && defined(__KERNEL_SSE2__)
  71. ssef x, y, z, w, aa;
  72. aa = a.m128;
  73. x = _mm_loadu_ps(&t->x.x);
  74. y = _mm_loadu_ps(&t->y.x);
  75. z = _mm_loadu_ps(&t->z.x);
  76. w = _mm_setzero_ps();
  77. _MM_TRANSPOSE4_PS(x, y, z, w);
  78. ssef tmp = shuffle<0>(aa) * x;
  79. tmp = madd(shuffle<1>(aa), y, tmp);
  80. tmp = madd(shuffle<2>(aa), z, tmp);
  81. return float3(tmp.m128);
  82. #else
  83. float3 c = make_float3(a.x * t->x.x + a.y * t->x.y + a.z * t->x.z,
  84. a.x * t->y.x + a.y * t->y.y + a.z * t->y.z,
  85. a.x * t->z.x + a.y * t->z.y + a.z * t->z.z);
  86. return c;
  87. #endif
  88. }
  89. ccl_device_inline float3 transform_direction_transposed(const Transform *t, const float3 a)
  90. {
  91. float3 x = make_float3(t->x.x, t->y.x, t->z.x);
  92. float3 y = make_float3(t->x.y, t->y.y, t->z.y);
  93. float3 z = make_float3(t->x.z, t->y.z, t->z.z);
  94. return make_float3(dot(x, a), dot(y, a), dot(z, a));
  95. }
  96. ccl_device_inline Transform make_transform(float a,
  97. float b,
  98. float c,
  99. float d,
  100. float e,
  101. float f,
  102. float g,
  103. float h,
  104. float i,
  105. float j,
  106. float k,
  107. float l)
  108. {
  109. Transform t;
  110. t.x.x = a;
  111. t.x.y = b;
  112. t.x.z = c;
  113. t.x.w = d;
  114. t.y.x = e;
  115. t.y.y = f;
  116. t.y.z = g;
  117. t.y.w = h;
  118. t.z.x = i;
  119. t.z.y = j;
  120. t.z.z = k;
  121. t.z.w = l;
  122. return t;
  123. }
  124. /* Constructs a coordinate frame from a normalized normal. */
  125. ccl_device_inline Transform make_transform_frame(float3 N)
  126. {
  127. const float3 dx0 = cross(make_float3(1.0f, 0.0f, 0.0f), N);
  128. const float3 dx1 = cross(make_float3(0.0f, 1.0f, 0.0f), N);
  129. const float3 dx = normalize((dot(dx0, dx0) > dot(dx1, dx1)) ? dx0 : dx1);
  130. const float3 dy = normalize(cross(N, dx));
  131. return make_transform(dx.x, dx.y, dx.z, 0.0f, dy.x, dy.y, dy.z, 0.0f, N.x, N.y, N.z, 0.0f);
  132. }
  133. #ifndef __KERNEL_GPU__
  134. ccl_device_inline Transform operator*(const Transform a, const Transform b)
  135. {
  136. float4 c_x = make_float4(b.x.x, b.y.x, b.z.x, 0.0f);
  137. float4 c_y = make_float4(b.x.y, b.y.y, b.z.y, 0.0f);
  138. float4 c_z = make_float4(b.x.z, b.y.z, b.z.z, 0.0f);
  139. float4 c_w = make_float4(b.x.w, b.y.w, b.z.w, 1.0f);
  140. Transform t;
  141. t.x = make_float4(dot(a.x, c_x), dot(a.x, c_y), dot(a.x, c_z), dot(a.x, c_w));
  142. t.y = make_float4(dot(a.y, c_x), dot(a.y, c_y), dot(a.y, c_z), dot(a.y, c_w));
  143. t.z = make_float4(dot(a.z, c_x), dot(a.z, c_y), dot(a.z, c_z), dot(a.z, c_w));
  144. return t;
  145. }
  146. ccl_device_inline void print_transform(const char *label, const Transform &t)
  147. {
  148. print_float4(label, t.x);
  149. print_float4(label, t.y);
  150. print_float4(label, t.z);
  151. printf("\n");
  152. }
  153. ccl_device_inline Transform transform_translate(float3 t)
  154. {
  155. return make_transform(1, 0, 0, t.x, 0, 1, 0, t.y, 0, 0, 1, t.z);
  156. }
  157. ccl_device_inline Transform transform_translate(float x, float y, float z)
  158. {
  159. return transform_translate(make_float3(x, y, z));
  160. }
  161. ccl_device_inline Transform transform_scale(float3 s)
  162. {
  163. return make_transform(s.x, 0, 0, 0, 0, s.y, 0, 0, 0, 0, s.z, 0);
  164. }
  165. ccl_device_inline Transform transform_scale(float x, float y, float z)
  166. {
  167. return transform_scale(make_float3(x, y, z));
  168. }
  169. ccl_device_inline Transform transform_rotate(float angle, float3 axis)
  170. {
  171. float s = sinf(angle);
  172. float c = cosf(angle);
  173. float t = 1.0f - c;
  174. axis = normalize(axis);
  175. return make_transform(axis.x * axis.x * t + c,
  176. axis.x * axis.y * t - s * axis.z,
  177. axis.x * axis.z * t + s * axis.y,
  178. 0.0f,
  179. axis.y * axis.x * t + s * axis.z,
  180. axis.y * axis.y * t + c,
  181. axis.y * axis.z * t - s * axis.x,
  182. 0.0f,
  183. axis.z * axis.x * t - s * axis.y,
  184. axis.z * axis.y * t + s * axis.x,
  185. axis.z * axis.z * t + c,
  186. 0.0f);
  187. }
  188. /* Euler is assumed to be in XYZ order. */
  189. ccl_device_inline Transform transform_euler(float3 euler)
  190. {
  191. return transform_rotate(euler.z, make_float3(0.0f, 0.0f, 1.0f)) *
  192. transform_rotate(euler.y, make_float3(0.0f, 1.0f, 0.0f)) *
  193. transform_rotate(euler.x, make_float3(1.0f, 0.0f, 0.0f));
  194. }
  195. ccl_device_inline Transform transform_identity()
  196. {
  197. return transform_scale(1.0f, 1.0f, 1.0f);
  198. }
  199. ccl_device_inline bool operator==(const Transform &A, const Transform &B)
  200. {
  201. return memcmp(&A, &B, sizeof(Transform)) == 0;
  202. }
  203. ccl_device_inline bool operator!=(const Transform &A, const Transform &B)
  204. {
  205. return !(A == B);
  206. }
  207. ccl_device_inline float3 transform_get_column(const Transform *t, int column)
  208. {
  209. return make_float3(t->x[column], t->y[column], t->z[column]);
  210. }
  211. ccl_device_inline void transform_set_column(Transform *t, int column, float3 value)
  212. {
  213. t->x[column] = value.x;
  214. t->y[column] = value.y;
  215. t->z[column] = value.z;
  216. }
  217. Transform transform_inverse(const Transform &a);
  218. Transform transform_transposed_inverse(const Transform &a);
  219. ccl_device_inline bool transform_uniform_scale(const Transform &tfm, float &scale)
  220. {
  221. /* the epsilon here is quite arbitrary, but this function is only used for
  222. * surface area and bump, where we expect it to not be so sensitive */
  223. float eps = 1e-6f;
  224. float sx = len_squared(float4_to_float3(tfm.x));
  225. float sy = len_squared(float4_to_float3(tfm.y));
  226. float sz = len_squared(float4_to_float3(tfm.z));
  227. float stx = len_squared(transform_get_column(&tfm, 0));
  228. float sty = len_squared(transform_get_column(&tfm, 1));
  229. float stz = len_squared(transform_get_column(&tfm, 2));
  230. if (fabsf(sx - sy) < eps && fabsf(sx - sz) < eps && fabsf(sx - stx) < eps &&
  231. fabsf(sx - sty) < eps && fabsf(sx - stz) < eps) {
  232. scale = sx;
  233. return true;
  234. }
  235. return false;
  236. }
  237. ccl_device_inline bool transform_negative_scale(const Transform &tfm)
  238. {
  239. float3 c0 = transform_get_column(&tfm, 0);
  240. float3 c1 = transform_get_column(&tfm, 1);
  241. float3 c2 = transform_get_column(&tfm, 2);
  242. return (dot(cross(c0, c1), c2) < 0.0f);
  243. }
  244. ccl_device_inline Transform transform_clear_scale(const Transform &tfm)
  245. {
  246. Transform ntfm = tfm;
  247. transform_set_column(&ntfm, 0, normalize(transform_get_column(&ntfm, 0)));
  248. transform_set_column(&ntfm, 1, normalize(transform_get_column(&ntfm, 1)));
  249. transform_set_column(&ntfm, 2, normalize(transform_get_column(&ntfm, 2)));
  250. return ntfm;
  251. }
  252. ccl_device_inline Transform transform_empty()
  253. {
  254. return make_transform(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
  255. }
  256. #endif
  257. /* Motion Transform */
  258. ccl_device_inline float4 quat_interpolate(float4 q1, float4 q2, float t)
  259. {
  260. /* use simpe nlerp instead of slerp. it's faster and almost the same */
  261. return normalize((1.0f - t) * q1 + t * q2);
  262. #if 0
  263. /* note: this does not ensure rotation around shortest angle, q1 and q2
  264. * are assumed to be matched already in transform_motion_decompose */
  265. float costheta = dot(q1, q2);
  266. /* possible optimization: it might be possible to precompute theta/qperp */
  267. if (costheta > 0.9995f) {
  268. /* linear interpolation in degenerate case */
  269. return normalize((1.0f - t) * q1 + t * q2);
  270. }
  271. else {
  272. /* slerp */
  273. float theta = acosf(clamp(costheta, -1.0f, 1.0f));
  274. float4 qperp = normalize(q2 - q1 * costheta);
  275. float thetap = theta * t;
  276. return q1 * cosf(thetap) + qperp * sinf(thetap);
  277. }
  278. #endif
  279. }
  280. ccl_device_inline Transform transform_quick_inverse(Transform M)
  281. {
  282. /* possible optimization: can we avoid doing this altogether and construct
  283. * the inverse matrix directly from negated translation, transposed rotation,
  284. * scale can be inverted but what about shearing? */
  285. Transform R;
  286. float det = M.x.x * (M.z.z * M.y.y - M.z.y * M.y.z) - M.y.x * (M.z.z * M.x.y - M.z.y * M.x.z) +
  287. M.z.x * (M.y.z * M.x.y - M.y.y * M.x.z);
  288. if (det == 0.0f) {
  289. M.x.x += 1e-8f;
  290. M.y.y += 1e-8f;
  291. M.z.z += 1e-8f;
  292. det = M.x.x * (M.z.z * M.y.y - M.z.y * M.y.z) - M.y.x * (M.z.z * M.x.y - M.z.y * M.x.z) +
  293. M.z.x * (M.y.z * M.x.y - M.y.y * M.x.z);
  294. }
  295. det = (det != 0.0f) ? 1.0f / det : 0.0f;
  296. float3 Rx = det * make_float3(M.z.z * M.y.y - M.z.y * M.y.z,
  297. M.z.y * M.x.z - M.z.z * M.x.y,
  298. M.y.z * M.x.y - M.y.y * M.x.z);
  299. float3 Ry = det * make_float3(M.z.x * M.y.z - M.z.z * M.y.x,
  300. M.z.z * M.x.x - M.z.x * M.x.z,
  301. M.y.x * M.x.z - M.y.z * M.x.x);
  302. float3 Rz = det * make_float3(M.z.y * M.y.x - M.z.x * M.y.y,
  303. M.z.x * M.x.y - M.z.y * M.x.x,
  304. M.y.y * M.x.x - M.y.x * M.x.y);
  305. float3 T = -make_float3(M.x.w, M.y.w, M.z.w);
  306. R.x = make_float4(Rx.x, Rx.y, Rx.z, dot(Rx, T));
  307. R.y = make_float4(Ry.x, Ry.y, Ry.z, dot(Ry, T));
  308. R.z = make_float4(Rz.x, Rz.y, Rz.z, dot(Rz, T));
  309. return R;
  310. }
  311. ccl_device_inline void transform_compose(Transform *tfm, const DecomposedTransform *decomp)
  312. {
  313. /* rotation */
  314. float q0, q1, q2, q3, qda, qdb, qdc, qaa, qab, qac, qbb, qbc, qcc;
  315. q0 = M_SQRT2_F * decomp->x.w;
  316. q1 = M_SQRT2_F * decomp->x.x;
  317. q2 = M_SQRT2_F * decomp->x.y;
  318. q3 = M_SQRT2_F * decomp->x.z;
  319. qda = q0 * q1;
  320. qdb = q0 * q2;
  321. qdc = q0 * q3;
  322. qaa = q1 * q1;
  323. qab = q1 * q2;
  324. qac = q1 * q3;
  325. qbb = q2 * q2;
  326. qbc = q2 * q3;
  327. qcc = q3 * q3;
  328. float3 rotation_x = make_float3(1.0f - qbb - qcc, -qdc + qab, qdb + qac);
  329. float3 rotation_y = make_float3(qdc + qab, 1.0f - qaa - qcc, -qda + qbc);
  330. float3 rotation_z = make_float3(-qdb + qac, qda + qbc, 1.0f - qaa - qbb);
  331. /* scale */
  332. float3 scale_x = make_float3(decomp->y.w, decomp->z.z, decomp->w.y);
  333. float3 scale_y = make_float3(decomp->z.x, decomp->z.w, decomp->w.z);
  334. float3 scale_z = make_float3(decomp->z.y, decomp->w.x, decomp->w.w);
  335. /* compose with translation */
  336. tfm->x = make_float4(
  337. dot(rotation_x, scale_x), dot(rotation_x, scale_y), dot(rotation_x, scale_z), decomp->y.x);
  338. tfm->y = make_float4(
  339. dot(rotation_y, scale_x), dot(rotation_y, scale_y), dot(rotation_y, scale_z), decomp->y.y);
  340. tfm->z = make_float4(
  341. dot(rotation_z, scale_x), dot(rotation_z, scale_y), dot(rotation_z, scale_z), decomp->y.z);
  342. }
  343. /* Interpolate from array of decomposed transforms. */
  344. ccl_device void transform_motion_array_interpolate(Transform *tfm,
  345. const ccl_global DecomposedTransform *motion,
  346. uint numsteps,
  347. float time)
  348. {
  349. /* Figure out which steps we need to interpolate. */
  350. int maxstep = numsteps - 1;
  351. int step = min((int)(time * maxstep), maxstep - 1);
  352. float t = time * maxstep - step;
  353. const ccl_global DecomposedTransform *a = motion + step;
  354. const ccl_global DecomposedTransform *b = motion + step + 1;
  355. /* Interpolate rotation, translation and scale. */
  356. DecomposedTransform decomp;
  357. decomp.x = quat_interpolate(a->x, b->x, t);
  358. decomp.y = (1.0f - t) * a->y + t * b->y;
  359. decomp.z = (1.0f - t) * a->z + t * b->z;
  360. decomp.w = (1.0f - t) * a->w + t * b->w;
  361. /* Compose rotation, translation, scale into matrix. */
  362. transform_compose(tfm, &decomp);
  363. }
  364. #ifndef __KERNEL_GPU__
  365. # ifdef WITH_EMBREE
  366. ccl_device void transform_motion_array_interpolate_straight(
  367. Transform *tfm, const ccl_global DecomposedTransform *motion, uint numsteps, float time)
  368. {
  369. /* Figure out which steps we need to interpolate. */
  370. int maxstep = numsteps - 1;
  371. int step = min((int)(time * maxstep), maxstep - 1);
  372. float t = time * maxstep - step;
  373. const ccl_global DecomposedTransform *a = motion + step;
  374. const ccl_global DecomposedTransform *b = motion + step + 1;
  375. Transform step1, step2;
  376. transform_compose(&step1, a);
  377. transform_compose(&step2, b);
  378. /* matrix lerp */
  379. tfm->x = (1.0f - t) * step1.x + t * step2.x;
  380. tfm->y = (1.0f - t) * step1.y + t * step2.y;
  381. tfm->z = (1.0f - t) * step1.z + t * step2.z;
  382. }
  383. # endif
  384. class BoundBox2D;
  385. ccl_device_inline bool operator==(const DecomposedTransform &A, const DecomposedTransform &B)
  386. {
  387. return memcmp(&A, &B, sizeof(DecomposedTransform)) == 0;
  388. }
  389. float4 transform_to_quat(const Transform &tfm);
  390. void transform_motion_decompose(DecomposedTransform *decomp, const Transform *motion, size_t size);
  391. Transform transform_from_viewplane(BoundBox2D &viewplane);
  392. #endif
  393. /* TODO(sergey): This is only for until we've got OpenCL 2.0
  394. * on all devices we consider supported. It'll be replaced with
  395. * generic address space.
  396. */
  397. #ifdef __KERNEL_OPENCL__
  398. # define OPENCL_TRANSFORM_ADDRSPACE_GLUE(a, b) a##b
  399. # define OPENCL_TRANSFORM_ADDRSPACE_DECLARE(function) \
  400. ccl_device_inline float3 OPENCL_TRANSFORM_ADDRSPACE_GLUE(function, _addrspace)( \
  401. ccl_addr_space const Transform *t, const float3 a) \
  402. { \
  403. Transform private_tfm = *t; \
  404. return function(&private_tfm, a); \
  405. }
  406. OPENCL_TRANSFORM_ADDRSPACE_DECLARE(transform_point)
  407. OPENCL_TRANSFORM_ADDRSPACE_DECLARE(transform_direction)
  408. OPENCL_TRANSFORM_ADDRSPACE_DECLARE(transform_direction_transposed)
  409. # undef OPENCL_TRANSFORM_ADDRSPACE_DECLARE
  410. # undef OPENCL_TRANSFORM_ADDRSPACE_GLUE
  411. # define transform_point_auto transform_point_addrspace
  412. # define transform_direction_auto transform_direction_addrspace
  413. # define transform_direction_transposed_auto transform_direction_transposed_addrspace
  414. #else
  415. # define transform_point_auto transform_point
  416. # define transform_direction_auto transform_direction
  417. # define transform_direction_transposed_auto transform_direction_transposed
  418. #endif
  419. CCL_NAMESPACE_END
  420. #endif /* __UTIL_TRANSFORM_H__ */