2011-04-27 11:58:34 +00:00
|
|
|
/*
|
2013-08-18 14:16:15 +00:00
|
|
|
* Copyright 2011-2013 Blender Foundation
|
2011-04-27 11:58:34 +00:00
|
|
|
*
|
2013-08-18 14:16:15 +00:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2011-04-27 11:58:34 +00:00
|
|
|
*
|
2013-08-18 14:16:15 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-04-27 11:58:34 +00:00
|
|
|
*
|
2013-08-18 14:16:15 +00:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License
|
2011-04-27 11:58:34 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __UTIL_TRANSFORM_H__
|
|
|
|
#define __UTIL_TRANSFORM_H__
|
|
|
|
|
|
|
|
#ifndef __KERNEL_GPU__
|
|
|
|
#include <string.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "util_math.h"
|
|
|
|
#include "util_types.h"
|
|
|
|
|
|
|
|
CCL_NAMESPACE_BEGIN
|
|
|
|
|
2012-04-30 12:49:26 +00:00
|
|
|
/* Data Types */
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
typedef struct Transform {
|
|
|
|
float4 x, y, z, w; /* rows */
|
2011-09-19 11:57:31 +00:00
|
|
|
|
|
|
|
#ifndef __KERNEL_GPU__
|
|
|
|
float4 operator[](int i) const { return *(&x + i); }
|
|
|
|
float4& operator[](int i) { return *(&x + i); }
|
|
|
|
#endif
|
2011-04-27 11:58:34 +00:00
|
|
|
} Transform;
|
|
|
|
|
2012-11-11 15:02:05 +00:00
|
|
|
/* transform decomposed in rotation/translation/scale. we use the same data
|
|
|
|
* structure as Transform, and tightly pack decomposition into it. first the
|
2012-11-29 00:43:50 +00:00
|
|
|
* rotation (4), then translation (3), then 3x3 scale matrix (9).
|
|
|
|
*
|
|
|
|
* For the DecompMotionTransform we drop scale from pre/post. */
|
2012-11-11 15:02:05 +00:00
|
|
|
|
2013-11-15 23:17:10 +00:00
|
|
|
typedef struct ccl_may_alias MotionTransform {
|
2012-04-30 12:49:26 +00:00
|
|
|
Transform pre;
|
2012-10-17 12:55:23 +00:00
|
|
|
Transform mid;
|
2012-04-30 12:49:26 +00:00
|
|
|
Transform post;
|
|
|
|
} MotionTransform;
|
|
|
|
|
2012-11-29 00:43:50 +00:00
|
|
|
typedef struct DecompMotionTransform {
|
|
|
|
Transform mid;
|
|
|
|
float4 pre_x, pre_y;
|
|
|
|
float4 post_x, post_y;
|
|
|
|
} DecompMotionTransform;
|
|
|
|
|
2012-04-30 12:49:26 +00:00
|
|
|
/* Functions */
|
|
|
|
|
2013-11-15 23:17:10 +00:00
|
|
|
ccl_device_inline float3 transform_perspective(const Transform *t, const float3 a)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
2011-05-31 11:31:00 +00:00
|
|
|
float4 b = make_float4(a.x, a.y, a.z, 1.0f);
|
|
|
|
float3 c = make_float3(dot(t->x, b), dot(t->y, b), dot(t->z, b));
|
2011-12-13 00:00:26 +00:00
|
|
|
float w = dot(t->w, b);
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2011-12-13 00:00:26 +00:00
|
|
|
return (w != 0.0f)? c/w: make_float3(0.0f, 0.0f, 0.0f);
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
2013-11-15 23:17:10 +00:00
|
|
|
ccl_device_inline float3 transform_point(const Transform *t, const float3 a)
|
2012-04-16 08:35:21 +00:00
|
|
|
{
|
2012-05-28 19:21:13 +00:00
|
|
|
float3 c = make_float3(
|
|
|
|
a.x*t->x.x + a.y*t->x.y + a.z*t->x.z + t->x.w,
|
|
|
|
a.x*t->y.x + a.y*t->y.y + a.z*t->y.z + t->y.w,
|
|
|
|
a.x*t->z.x + a.y*t->z.y + a.z*t->z.z + t->z.w);
|
2012-04-16 08:35:21 +00:00
|
|
|
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2013-11-15 23:17:10 +00:00
|
|
|
ccl_device_inline float3 transform_direction(const Transform *t, const float3 a)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
2012-05-28 19:21:13 +00:00
|
|
|
float3 c = make_float3(
|
|
|
|
a.x*t->x.x + a.y*t->x.y + a.z*t->x.z,
|
|
|
|
a.x*t->y.x + a.y*t->y.y + a.z*t->y.z,
|
|
|
|
a.x*t->z.x + a.y*t->z.y + a.z*t->z.z);
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2013-11-15 23:17:10 +00:00
|
|
|
ccl_device_inline float3 transform_direction_transposed(const Transform *t, const float3 a)
|
2012-04-30 12:49:26 +00:00
|
|
|
{
|
|
|
|
float3 x = make_float3(t->x.x, t->y.x, t->z.x);
|
|
|
|
float3 y = make_float3(t->x.y, t->y.y, t->z.y);
|
|
|
|
float3 z = make_float3(t->x.z, t->y.z, t->z.z);
|
|
|
|
|
|
|
|
return make_float3(dot(x, a), dot(y, a), dot(z, a));
|
|
|
|
}
|
|
|
|
|
2013-11-15 23:17:10 +00:00
|
|
|
ccl_device_inline Transform transform_transpose(const Transform a)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
|
|
|
Transform t;
|
|
|
|
|
|
|
|
t.x.x = a.x.x; t.x.y = a.y.x; t.x.z = a.z.x; t.x.w = a.w.x;
|
|
|
|
t.y.x = a.x.y; t.y.y = a.y.y; t.y.z = a.z.y; t.y.w = a.w.y;
|
|
|
|
t.z.x = a.x.z; t.z.y = a.y.z; t.z.z = a.z.z; t.z.w = a.w.z;
|
|
|
|
t.w.x = a.x.w; t.w.y = a.y.w; t.w.z = a.z.w; t.w.w = a.w.w;
|
|
|
|
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2013-11-15 23:17:10 +00:00
|
|
|
ccl_device_inline Transform make_transform(float a, float b, float c, float d,
|
2014-05-04 16:19:08 +00:00
|
|
|
float e, float f, float g, float h,
|
|
|
|
float i, float j, float k, float l,
|
|
|
|
float m, float n, float o, float p)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
|
|
|
Transform t;
|
|
|
|
|
|
|
|
t.x.x = a; t.x.y = b; t.x.z = c; t.x.w = d;
|
|
|
|
t.y.x = e; t.y.y = f; t.y.z = g; t.y.w = h;
|
|
|
|
t.z.x = i; t.z.y = j; t.z.z = k; t.z.w = l;
|
|
|
|
t.w.x = m; t.w.y = n; t.w.z = o; t.w.w = p;
|
|
|
|
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2013-05-08 17:33:25 +00:00
|
|
|
#ifndef __KERNEL_GPU__
|
|
|
|
|
2013-11-15 23:17:10 +00:00
|
|
|
ccl_device_inline Transform operator*(const Transform a, const Transform b)
|
2013-05-09 14:05:40 +00:00
|
|
|
{
|
|
|
|
Transform c = transform_transpose(b);
|
|
|
|
Transform t;
|
|
|
|
|
|
|
|
t.x = make_float4(dot(a.x, c.x), dot(a.x, c.y), dot(a.x, c.z), dot(a.x, c.w));
|
|
|
|
t.y = make_float4(dot(a.y, c.x), dot(a.y, c.y), dot(a.y, c.z), dot(a.y, c.w));
|
|
|
|
t.z = make_float4(dot(a.z, c.x), dot(a.z, c.y), dot(a.z, c.z), dot(a.z, c.w));
|
|
|
|
t.w = make_float4(dot(a.w, c.x), dot(a.w, c.y), dot(a.w, c.z), dot(a.w, c.w));
|
|
|
|
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2013-11-15 23:17:10 +00:00
|
|
|
ccl_device_inline void print_transform(const char *label, const Transform& t)
|
2013-05-08 17:33:25 +00:00
|
|
|
{
|
|
|
|
print_float4(label, t.x);
|
|
|
|
print_float4(label, t.y);
|
|
|
|
print_float4(label, t.z);
|
|
|
|
print_float4(label, t.w);
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
2013-11-15 23:17:10 +00:00
|
|
|
ccl_device_inline Transform transform_translate(float3 t)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
|
|
|
return make_transform(
|
|
|
|
1, 0, 0, t.x,
|
|
|
|
0, 1, 0, t.y,
|
|
|
|
0, 0, 1, t.z,
|
|
|
|
0, 0, 0, 1);
|
|
|
|
}
|
|
|
|
|
2013-11-15 23:17:10 +00:00
|
|
|
ccl_device_inline Transform transform_translate(float x, float y, float z)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
|
|
|
return transform_translate(make_float3(x, y, z));
|
|
|
|
}
|
|
|
|
|
2013-11-15 23:17:10 +00:00
|
|
|
ccl_device_inline Transform transform_scale(float3 s)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
|
|
|
return make_transform(
|
|
|
|
s.x, 0, 0, 0,
|
|
|
|
0, s.y, 0, 0,
|
|
|
|
0, 0, s.z, 0,
|
|
|
|
0, 0, 0, 1);
|
|
|
|
}
|
|
|
|
|
2013-11-15 23:17:10 +00:00
|
|
|
ccl_device_inline Transform transform_scale(float x, float y, float z)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
|
|
|
return transform_scale(make_float3(x, y, z));
|
|
|
|
}
|
|
|
|
|
2013-11-15 23:17:10 +00:00
|
|
|
ccl_device_inline Transform transform_perspective(float fov, float n, float f)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
|
|
|
Transform persp = make_transform(
|
|
|
|
1, 0, 0, 0,
|
|
|
|
0, 1, 0, 0,
|
|
|
|
0, 0, f / (f - n), -f*n / (f - n),
|
|
|
|
0, 0, 1, 0);
|
|
|
|
|
|
|
|
float inv_angle = 1.0f/tanf(0.5f*fov);
|
|
|
|
|
|
|
|
Transform scale = transform_scale(inv_angle, inv_angle, 1);
|
|
|
|
|
|
|
|
return scale * persp;
|
|
|
|
}
|
|
|
|
|
2013-11-15 23:17:10 +00:00
|
|
|
ccl_device_inline Transform transform_rotate(float angle, float3 axis)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
|
|
|
float s = sinf(angle);
|
|
|
|
float c = cosf(angle);
|
2013-05-18 11:04:29 +00:00
|
|
|
float t = 1.0f - c;
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
axis = normalize(axis);
|
|
|
|
|
|
|
|
return make_transform(
|
|
|
|
axis.x*axis.x*t + c,
|
|
|
|
axis.x*axis.y*t - s*axis.z,
|
|
|
|
axis.x*axis.z*t + s*axis.y,
|
|
|
|
0.0f,
|
|
|
|
|
|
|
|
axis.y*axis.x*t + s*axis.z,
|
|
|
|
axis.y*axis.y*t + c,
|
|
|
|
axis.y*axis.z*t - s*axis.x,
|
|
|
|
0.0f,
|
|
|
|
|
|
|
|
axis.z*axis.x*t - s*axis.y,
|
|
|
|
axis.z*axis.y*t + s*axis.x,
|
|
|
|
axis.z*axis.z*t + c,
|
|
|
|
0.0f,
|
|
|
|
|
|
|
|
0.0f, 0.0f, 0.0f, 1.0f);
|
|
|
|
}
|
|
|
|
|
2013-11-15 23:17:10 +00:00
|
|
|
ccl_device_inline Transform transform_euler(float3 euler)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
|
|
|
return
|
|
|
|
transform_rotate(euler.x, make_float3(1.0f, 0.0f, 0.0f)) *
|
2011-05-12 11:34:16 +00:00
|
|
|
transform_rotate(euler.y, make_float3(0.0f, 1.0f, 0.0f)) *
|
|
|
|
transform_rotate(euler.z, make_float3(0.0f, 0.0f, 1.0f));
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
2013-11-15 23:17:10 +00:00
|
|
|
ccl_device_inline Transform transform_orthographic(float znear, float zfar)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
|
|
|
return transform_scale(1.0f, 1.0f, 1.0f / (zfar-znear)) *
|
|
|
|
transform_translate(0.0f, 0.0f, -znear);
|
|
|
|
}
|
|
|
|
|
2013-11-15 23:17:10 +00:00
|
|
|
ccl_device_inline Transform transform_identity()
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
|
|
|
return transform_scale(1.0f, 1.0f, 1.0f);
|
|
|
|
}
|
|
|
|
|
2013-11-15 23:17:10 +00:00
|
|
|
ccl_device_inline bool operator==(const Transform& A, const Transform& B)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
|
|
|
return memcmp(&A, &B, sizeof(Transform)) == 0;
|
|
|
|
}
|
|
|
|
|
2013-11-15 23:17:10 +00:00
|
|
|
ccl_device_inline bool operator!=(const Transform& A, const Transform& B)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
|
|
|
return !(A == B);
|
|
|
|
}
|
|
|
|
|
2013-11-15 23:17:10 +00:00
|
|
|
ccl_device_inline float3 transform_get_column(const Transform *t, int column)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
|
|
|
return make_float3(t->x[column], t->y[column], t->z[column]);
|
|
|
|
}
|
|
|
|
|
2013-11-15 23:17:10 +00:00
|
|
|
ccl_device_inline void transform_set_column(Transform *t, int column, float3 value)
|
2011-12-19 14:23:24 +00:00
|
|
|
{
|
|
|
|
t->x[column] = value.x;
|
|
|
|
t->y[column] = value.y;
|
|
|
|
t->z[column] = value.z;
|
|
|
|
}
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
Transform transform_inverse(const Transform& a);
|
|
|
|
|
2013-11-15 23:17:10 +00:00
|
|
|
ccl_device_inline bool transform_uniform_scale(const Transform& tfm, float& scale)
|
2011-09-02 16:15:18 +00:00
|
|
|
{
|
|
|
|
/* the epsilon here is quite arbitrary, but this function is only used for
|
2012-06-09 17:22:52 +00:00
|
|
|
* surface area and bump, where we except it to not be so sensitive */
|
2011-09-02 16:15:18 +00:00
|
|
|
Transform ttfm = transform_transpose(tfm);
|
2014-02-03 02:55:26 +00:00
|
|
|
float eps = 1e-6f;
|
2011-09-02 16:15:18 +00:00
|
|
|
|
2013-06-04 17:20:00 +00:00
|
|
|
float sx = len_squared(float4_to_float3(tfm.x));
|
|
|
|
float sy = len_squared(float4_to_float3(tfm.y));
|
|
|
|
float sz = len_squared(float4_to_float3(tfm.z));
|
|
|
|
float stx = len_squared(float4_to_float3(ttfm.x));
|
|
|
|
float sty = len_squared(float4_to_float3(ttfm.y));
|
|
|
|
float stz = len_squared(float4_to_float3(ttfm.z));
|
2012-05-07 10:53:09 +00:00
|
|
|
|
2011-09-02 16:15:18 +00:00
|
|
|
if(fabsf(sx - sy) < eps && fabsf(sx - sz) < eps &&
|
|
|
|
fabsf(sx - stx) < eps && fabsf(sx - sty) < eps &&
|
2014-02-03 02:55:26 +00:00
|
|
|
fabsf(sx - stz) < eps)
|
|
|
|
{
|
2011-09-02 16:15:18 +00:00
|
|
|
scale = sx;
|
|
|
|
return true;
|
|
|
|
}
|
2014-02-03 02:55:26 +00:00
|
|
|
|
|
|
|
return false;
|
2011-09-02 16:15:18 +00:00
|
|
|
}
|
|
|
|
|
2013-11-15 23:17:10 +00:00
|
|
|
ccl_device_inline bool transform_negative_scale(const Transform& tfm)
|
2011-11-12 14:29:52 +00:00
|
|
|
{
|
|
|
|
float3 c0 = transform_get_column(&tfm, 0);
|
|
|
|
float3 c1 = transform_get_column(&tfm, 1);
|
|
|
|
float3 c2 = transform_get_column(&tfm, 2);
|
|
|
|
|
|
|
|
return (dot(cross(c0, c1), c2) < 0.0f);
|
|
|
|
}
|
|
|
|
|
2013-11-15 23:17:10 +00:00
|
|
|
ccl_device_inline Transform transform_clear_scale(const Transform& tfm)
|
2011-12-19 14:23:24 +00:00
|
|
|
{
|
|
|
|
Transform ntfm = tfm;
|
|
|
|
|
|
|
|
transform_set_column(&ntfm, 0, normalize(transform_get_column(&ntfm, 0)));
|
|
|
|
transform_set_column(&ntfm, 1, normalize(transform_get_column(&ntfm, 1)));
|
|
|
|
transform_set_column(&ntfm, 2, normalize(transform_get_column(&ntfm, 2)));
|
|
|
|
|
|
|
|
return ntfm;
|
|
|
|
}
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
#endif
|
|
|
|
|
2012-04-30 12:49:26 +00:00
|
|
|
/* Motion Transform */
|
|
|
|
|
2013-11-15 23:17:10 +00:00
|
|
|
ccl_device_inline float4 quat_interpolate(float4 q1, float4 q2, float t)
|
2012-04-30 12:49:26 +00:00
|
|
|
{
|
2012-11-29 13:07:45 +00:00
|
|
|
/* use simpe nlerp instead of slerp. it's faster and almost the same */
|
|
|
|
return normalize((1.0f - t)*q1 + t*q2);
|
|
|
|
|
|
|
|
#if 0
|
2012-11-11 15:02:05 +00:00
|
|
|
/* note: this does not ensure rotation around shortest angle, q1 and q2
|
|
|
|
* are assumed to be matched already in transform_motion_decompose */
|
2012-04-30 12:49:26 +00:00
|
|
|
float costheta = dot(q1, q2);
|
|
|
|
|
2012-11-11 15:02:05 +00:00
|
|
|
/* possible optimization: it might be possible to precompute theta/qperp */
|
2012-10-15 17:56:40 +00:00
|
|
|
|
2012-04-30 12:49:26 +00:00
|
|
|
if(costheta > 0.9995f) {
|
2012-10-15 17:56:40 +00:00
|
|
|
/* linear interpolation in degenerate case */
|
2012-04-30 12:49:26 +00:00
|
|
|
return normalize((1.0f - t)*q1 + t*q2);
|
|
|
|
}
|
|
|
|
else {
|
2012-10-15 17:56:40 +00:00
|
|
|
/* slerp */
|
2012-04-30 12:49:26 +00:00
|
|
|
float theta = acosf(clamp(costheta, -1.0f, 1.0f));
|
|
|
|
float4 qperp = normalize(q2 - q1 * costheta);
|
2012-11-11 15:02:05 +00:00
|
|
|
float thetap = theta * t;
|
2012-04-30 12:49:26 +00:00
|
|
|
return q1 * cosf(thetap) + qperp * sinf(thetap);
|
|
|
|
}
|
2012-11-29 13:07:45 +00:00
|
|
|
#endif
|
2012-04-30 12:49:26 +00:00
|
|
|
}
|
|
|
|
|
2013-11-15 23:17:10 +00:00
|
|
|
ccl_device_inline Transform transform_quick_inverse(Transform M)
|
2012-04-30 12:49:26 +00:00
|
|
|
{
|
2012-11-11 15:02:05 +00:00
|
|
|
/* possible optimization: can we avoid doing this altogether and construct
|
|
|
|
* the inverse matrix directly from negated translation, transposed rotation,
|
|
|
|
* scale can be inverted but what about shearing? */
|
2012-04-30 12:49:26 +00:00
|
|
|
Transform R;
|
|
|
|
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) + M.z.x*(M.y.z*M.x.y - M.y.y*M.x.z);
|
|
|
|
|
|
|
|
det = (det != 0.0f)? 1.0f/det: 0.0f;
|
|
|
|
|
|
|
|
float3 Rx = det*make_float3(M.z.z*M.y.y - M.z.y*M.y.z, M.z.y*M.x.z - M.z.z*M.x.y, M.y.z*M.x.y - M.y.y*M.x.z);
|
|
|
|
float3 Ry = det*make_float3(M.z.x*M.y.z - M.z.z*M.y.x, M.z.z*M.x.x - M.z.x*M.x.z, M.y.x*M.x.z - M.y.z*M.x.x);
|
|
|
|
float3 Rz = det*make_float3(M.z.y*M.y.x - M.z.x*M.y.y, M.z.x*M.x.y - M.z.y*M.x.x, M.y.y*M.x.x - M.y.x*M.x.y);
|
|
|
|
float3 T = -make_float3(M.x.w, M.y.w, M.z.w);
|
|
|
|
|
|
|
|
R.x = make_float4(Rx.x, Rx.y, Rx.z, dot(Rx, T));
|
|
|
|
R.y = make_float4(Ry.x, Ry.y, Ry.z, dot(Ry, T));
|
|
|
|
R.z = make_float4(Rz.x, Rz.y, Rz.z, dot(Rz, T));
|
|
|
|
R.w = make_float4(0.0f, 0.0f, 0.0f, 1.0f);
|
|
|
|
|
|
|
|
return R;
|
|
|
|
}
|
|
|
|
|
2013-11-15 23:17:10 +00:00
|
|
|
ccl_device_inline void transform_compose(Transform *tfm, const Transform *decomp)
|
2012-04-30 12:49:26 +00:00
|
|
|
{
|
|
|
|
/* rotation */
|
|
|
|
float q0, q1, q2, q3, qda, qdb, qdc, qaa, qab, qac, qbb, qbc, qcc;
|
|
|
|
|
|
|
|
q0 = M_SQRT2_F * decomp->x.w;
|
|
|
|
q1 = M_SQRT2_F * decomp->x.x;
|
|
|
|
q2 = M_SQRT2_F * decomp->x.y;
|
|
|
|
q3 = M_SQRT2_F * decomp->x.z;
|
|
|
|
|
|
|
|
qda = q0*q1;
|
|
|
|
qdb = q0*q2;
|
|
|
|
qdc = q0*q3;
|
|
|
|
qaa = q1*q1;
|
|
|
|
qab = q1*q2;
|
|
|
|
qac = q1*q3;
|
|
|
|
qbb = q2*q2;
|
|
|
|
qbc = q2*q3;
|
|
|
|
qcc = q3*q3;
|
|
|
|
|
|
|
|
float3 rotation_x = make_float3(1.0f-qbb-qcc, -qdc+qab, qdb+qac);
|
|
|
|
float3 rotation_y = make_float3(qdc+qab, 1.0f-qaa-qcc, -qda+qbc);
|
|
|
|
float3 rotation_z = make_float3(-qdb+qac, qda+qbc, 1.0f-qaa-qbb);
|
|
|
|
|
|
|
|
/* scale */
|
|
|
|
float3 scale_x = make_float3(decomp->y.w, decomp->z.z, decomp->w.y);
|
|
|
|
float3 scale_y = make_float3(decomp->z.x, decomp->z.w, decomp->w.z);
|
|
|
|
float3 scale_z = make_float3(decomp->z.y, decomp->w.x, decomp->w.w);
|
|
|
|
|
|
|
|
/* compose with translation */
|
|
|
|
tfm->x = make_float4(dot(rotation_x, scale_x), dot(rotation_x, scale_y), dot(rotation_x, scale_z), decomp->y.x);
|
|
|
|
tfm->y = make_float4(dot(rotation_y, scale_x), dot(rotation_y, scale_y), dot(rotation_y, scale_z), decomp->y.y);
|
|
|
|
tfm->z = make_float4(dot(rotation_z, scale_x), dot(rotation_z, scale_y), dot(rotation_z, scale_z), decomp->y.z);
|
|
|
|
tfm->w = make_float4(0.0f, 0.0f, 0.0f, 1.0f);
|
|
|
|
}
|
|
|
|
|
2012-11-11 15:02:05 +00:00
|
|
|
/* Disabled for now, need arc-length parametrization for constant speed motion.
|
|
|
|
* #define CURVED_MOTION_INTERPOLATE */
|
|
|
|
|
2013-11-15 23:17:10 +00:00
|
|
|
ccl_device void transform_motion_interpolate(Transform *tfm, const DecompMotionTransform *motion, float t)
|
2012-04-30 12:49:26 +00:00
|
|
|
{
|
2012-11-11 15:02:05 +00:00
|
|
|
/* possible optimization: is it worth it adding a check to skip scaling?
|
|
|
|
* it's probably quite uncommon to have scaling objects. or can we skip
|
|
|
|
* just shearing perhaps? */
|
2012-04-30 12:49:26 +00:00
|
|
|
Transform decomp;
|
|
|
|
|
2012-11-11 15:02:05 +00:00
|
|
|
#ifdef CURVED_MOTION_INTERPOLATE
|
2012-10-17 12:55:23 +00:00
|
|
|
/* 3 point bezier curve interpolation for position */
|
2012-11-29 00:43:50 +00:00
|
|
|
float3 Ppre = float4_to_float3(motion->pre_y);
|
2012-10-17 12:55:23 +00:00
|
|
|
float3 Pmid = float4_to_float3(motion->mid.y);
|
2012-11-29 00:43:50 +00:00
|
|
|
float3 Ppost = float4_to_float3(motion->post_y);
|
2012-04-30 12:49:26 +00:00
|
|
|
|
2012-10-17 12:55:23 +00:00
|
|
|
float3 Pcontrol = 2.0f*Pmid - 0.5f*(Ppre + Ppost);
|
|
|
|
float3 P = Ppre*t*t + Pcontrol*2.0f*t*(1.0f - t) + Ppost*(1.0f - t)*(1.0f - t);
|
|
|
|
|
|
|
|
decomp.y.x = P.x;
|
|
|
|
decomp.y.y = P.y;
|
|
|
|
decomp.y.z = P.z;
|
2012-11-11 15:02:05 +00:00
|
|
|
#endif
|
2012-10-17 12:55:23 +00:00
|
|
|
|
|
|
|
/* linear interpolation for rotation and scale */
|
|
|
|
if(t < 0.5f) {
|
|
|
|
t *= 2.0f;
|
|
|
|
|
2012-11-29 00:43:50 +00:00
|
|
|
decomp.x = quat_interpolate(motion->pre_x, motion->mid.x, t);
|
2012-11-11 15:02:05 +00:00
|
|
|
#ifdef CURVED_MOTION_INTERPOLATE
|
2012-11-29 00:43:50 +00:00
|
|
|
decomp.y.w = (1.0f - t)*motion->pre_y.w + t*motion->mid.y.w;
|
2012-11-11 15:02:05 +00:00
|
|
|
#else
|
2012-11-29 00:43:50 +00:00
|
|
|
decomp.y = (1.0f - t)*motion->pre_y + t*motion->mid.y;
|
2012-11-11 15:02:05 +00:00
|
|
|
#endif
|
2012-10-17 12:55:23 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
t = (t - 0.5f)*2.0f;
|
|
|
|
|
2012-11-29 00:43:50 +00:00
|
|
|
decomp.x = quat_interpolate(motion->mid.x, motion->post_x, t);
|
2012-11-11 15:02:05 +00:00
|
|
|
#ifdef CURVED_MOTION_INTERPOLATE
|
2012-11-29 00:43:50 +00:00
|
|
|
decomp.y.w = (1.0f - t)*motion->mid.y.w + t*motion->post_y.w;
|
2012-11-11 15:02:05 +00:00
|
|
|
#else
|
2012-11-29 00:43:50 +00:00
|
|
|
decomp.y = (1.0f - t)*motion->mid.y + t*motion->post_y;
|
2012-11-11 15:02:05 +00:00
|
|
|
#endif
|
2012-10-17 12:55:23 +00:00
|
|
|
}
|
|
|
|
|
2012-11-29 00:43:50 +00:00
|
|
|
decomp.z = motion->mid.z;
|
|
|
|
decomp.w = motion->mid.w;
|
|
|
|
|
2012-10-17 12:55:23 +00:00
|
|
|
/* compose rotation, translation, scale into matrix */
|
2012-04-30 12:49:26 +00:00
|
|
|
transform_compose(tfm, &decomp);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef __KERNEL_GPU__
|
|
|
|
|
2013-11-15 23:17:10 +00:00
|
|
|
ccl_device_inline bool operator==(const MotionTransform& A, const MotionTransform& B)
|
2012-04-30 12:49:26 +00:00
|
|
|
{
|
|
|
|
return (A.pre == B.pre && A.post == B.post);
|
|
|
|
}
|
|
|
|
|
2013-04-05 23:03:10 +00:00
|
|
|
float4 transform_to_quat(const Transform& tfm);
|
2012-11-29 00:43:50 +00:00
|
|
|
void transform_motion_decompose(DecompMotionTransform *decomp, const MotionTransform *motion, const Transform *mid);
|
2012-04-30 12:49:26 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
CCL_NAMESPACE_END
|
|
|
|
|
|
|
|
#endif /* __UTIL_TRANSFORM_H__ */
|
|
|
|
|