1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
|
/* GIMP - The GNU Image Manipulation Program
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* gimp-3d-transform-utils.c
* Copyright (C) 2019 Ell
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <glib-object.h>
#include "libgimpmath/gimpmath.h"
#include "core-types.h"
#include "gimp-transform-3d-utils.h"
#define MIN_FOCAL_LENGTH 0.01
gdouble
gimp_transform_3d_angle_of_view_to_focal_length (gdouble angle_of_view,
gdouble width,
gdouble height)
{
return MAX (width, height) / (2.0 * tan (angle_of_view / 2.0));
}
gdouble
gimp_transform_3d_focal_length_to_angle_of_view (gdouble focal_length,
gdouble width,
gdouble height)
{
return 2.0 * atan (MAX (width, height) / (2.0 * focal_length));
}
gint
gimp_transform_3d_permutation_to_rotation_order (const gint permutation[3])
{
if (permutation[1] == (permutation[0] + 1) % 3)
return permutation[0] << 1;
else
return (permutation[2] << 1) + 1;
}
void
gimp_transform_3d_rotation_order_to_permutation (gint rotation_order,
gint permutation[3])
{
gboolean reverse = rotation_order & 1;
gint shift = rotation_order >> 1;
gint i;
for (i = 0; i < 3; i++)
permutation[reverse ? 2 - i : i] = (i + shift) % 3;
}
gint
gimp_transform_3d_rotation_order_reverse (gint rotation_order)
{
return rotation_order ^ 1;
}
void
gimp_transform_3d_vector3_rotate (GimpVector3 *vector,
const GimpVector3 *axis)
{
GimpVector3 normal;
GimpVector3 proj;
GimpVector3 u, v;
gdouble angle;
angle = gimp_vector3_length (axis);
if (angle == 0.0)
return;
normal = gimp_vector3_mul_val (*axis, 1.0 / angle);
proj = gimp_vector3_mul_val (normal,
gimp_vector3_inner_product_val (*vector,
normal));
u = gimp_vector3_sub_val (*vector, proj);
v = gimp_vector3_cross_product_val (u, normal);
gimp_vector3_mul (&u, cos (angle));
gimp_vector3_mul (&v, sin (angle));
*vector = proj;
gimp_vector3_add (vector, vector, &u);
gimp_vector3_add (vector, vector, &v);
}
GimpVector3
gimp_transform_3d_vector3_rotate_val (GimpVector3 vector,
GimpVector3 axis)
{
gimp_transform_3d_vector3_rotate (&vector, &axis);
return vector;
}
void
gimp_transform_3d_matrix3_to_matrix4 (const GimpMatrix3 *matrix3,
GimpMatrix4 *matrix4,
gint axis)
{
gint i, j;
gint k, l;
for (i = 0; i < 4; i++)
{
if (i == axis)
{
matrix4->coeff[i][i] = 1.0;
}
else
{
matrix4->coeff[axis][i] = 0.0;
matrix4->coeff[i][axis] = 0.0;
}
}
for (i = 0; i < 3; i++)
{
k = i + (i >= axis);
for (j = 0; j < 3; j++)
{
l = j + (j >= axis);
matrix4->coeff[k][l] = matrix3->coeff[i][j];
}
}
}
void
gimp_transform_3d_matrix4_to_matrix3 (const GimpMatrix4 *matrix4,
GimpMatrix3 *matrix3,
gint axis)
{
gint i, j;
gint k, l;
for (i = 0; i < 3; i++)
{
k = i + (i >= axis);
for (j = 0; j < 3; j++)
{
l = j + (j >= axis);
matrix3->coeff[i][j] = matrix4->coeff[k][l];
}
}
}
void
gimp_transform_3d_matrix4_translate (GimpMatrix4 *matrix,
gdouble x,
gdouble y,
gdouble z)
{
gint i;
for (i = 0; i < 4; i++)
matrix->coeff[0][i] += x * matrix->coeff[3][i];
for (i = 0; i < 4; i++)
matrix->coeff[1][i] += y * matrix->coeff[3][i];
for (i = 0; i < 4; i++)
matrix->coeff[2][i] += z * matrix->coeff[3][i];
}
void
gimp_transform_3d_matrix4_rotate (GimpMatrix4 *matrix,
const GimpVector3 *axis)
{
GimpMatrix4 rotation;
GimpVector3 v;
v = gimp_transform_3d_vector3_rotate_val ((GimpVector3) {1.0, 0.0, 0.0},
*axis);
rotation.coeff[0][0] = v.x;
rotation.coeff[1][0] = v.y;
rotation.coeff[2][0] = v.z;
rotation.coeff[3][0] = 0.0;
v = gimp_transform_3d_vector3_rotate_val ((GimpVector3) {0.0, 1.0, 0.0},
*axis);
rotation.coeff[0][1] = v.x;
rotation.coeff[1][1] = v.y;
rotation.coeff[2][1] = v.z;
rotation.coeff[3][1] = 0.0;
v = gimp_transform_3d_vector3_rotate_val ((GimpVector3) {0.0, 0.0, 1.0},
*axis);
rotation.coeff[0][2] = v.x;
rotation.coeff[1][2] = v.y;
rotation.coeff[2][2] = v.z;
rotation.coeff[3][2] = 0.0;
rotation.coeff[0][3] = 0.0;
rotation.coeff[1][3] = 0.0;
rotation.coeff[2][3] = 0.0;
rotation.coeff[3][3] = 1.0;
gimp_matrix4_mult (&rotation, matrix);
}
void
gimp_transform_3d_matrix4_rotate_standard (GimpMatrix4 *matrix,
gint axis,
gdouble angle)
{
gdouble v[3] = {};
v[axis] = angle;
gimp_transform_3d_matrix4_rotate (matrix, &(GimpVector3) {v[0], v[1], v[2]});
}
void
gimp_transform_3d_matrix4_rotate_euler (GimpMatrix4 *matrix,
gint rotation_order,
gdouble angle_x,
gdouble angle_y,
gdouble angle_z,
gdouble pivot_x,
gdouble pivot_y,
gdouble pivot_z)
{
const gdouble angles[3] = {angle_x, angle_y, angle_z};
gint permutation[3];
gint i;
gimp_transform_3d_rotation_order_to_permutation (rotation_order, permutation);
gimp_transform_3d_matrix4_translate (matrix, -pivot_x, -pivot_y, -pivot_z);
for (i = 0; i < 3; i++)
{
gimp_transform_3d_matrix4_rotate_standard (matrix,
permutation[i],
angles[permutation[i]]);
}
gimp_transform_3d_matrix4_translate (matrix, +pivot_x, +pivot_y, +pivot_z);
}
void
gimp_transform_3d_matrix4_rotate_euler_decompose (GimpMatrix4 *matrix,
gint rotation_order,
gdouble *angle_x,
gdouble *angle_y,
gdouble *angle_z)
{
GimpMatrix4 m = *matrix;
gdouble * const angles[3] = {angle_x, angle_y, angle_z};
gint permutation[3];
gboolean forward;
gimp_transform_3d_rotation_order_to_permutation (rotation_order, permutation);
forward = permutation[1] == (permutation[0] + 1) % 3;
*angles[permutation[2]] = atan2 (m.coeff[permutation[1]][permutation[0]],
m.coeff[permutation[0]][permutation[0]]);
if (forward)
*angles[permutation[2]] *= -1.0;
gimp_transform_3d_matrix4_rotate_standard (&m,
permutation[2],
-*angles[permutation[2]]);
*angles[permutation[1]] = atan2 (m.coeff[permutation[2]][permutation[0]],
m.coeff[permutation[0]][permutation[0]]);
if (! forward)
*angles[permutation[1]] *= -1.0;
gimp_transform_3d_matrix4_rotate_standard (&m,
permutation[1],
-*angles[permutation[1]]);
*angles[permutation[0]] = atan2 (m.coeff[permutation[2]][permutation[1]],
m.coeff[permutation[1]][permutation[1]]);
if (forward)
*angles[permutation[0]] *= -1.0;
}
void
gimp_transform_3d_matrix4_perspective (GimpMatrix4 *matrix,
gdouble camera_x,
gdouble camera_y,
gdouble camera_z)
{
gint i;
camera_z = MIN (camera_z, -MIN_FOCAL_LENGTH);
gimp_transform_3d_matrix4_translate (matrix, -camera_x, -camera_y, 0.0);
for (i = 0; i < 4; i++)
matrix->coeff[3][i] += matrix->coeff[2][i] / -camera_z;
gimp_transform_3d_matrix4_translate (matrix, +camera_x, +camera_y, 0.0);
}
void
gimp_transform_3d_matrix (GimpMatrix3 *matrix,
gdouble camera_x,
gdouble camera_y,
gdouble camera_z,
gdouble offset_x,
gdouble offset_y,
gdouble offset_z,
gint rotation_order,
gdouble angle_x,
gdouble angle_y,
gdouble angle_z,
gdouble pivot_x,
gdouble pivot_y,
gdouble pivot_z)
{
GimpMatrix4 m;
gimp_matrix4_identity (&m);
gimp_transform_3d_matrix4_rotate_euler (&m,
rotation_order,
angle_x, angle_y, angle_z,
pivot_x, pivot_y, pivot_z);
gimp_transform_3d_matrix4_translate (&m, offset_x, offset_y, offset_z);
gimp_transform_3d_matrix4_perspective (&m, camera_x, camera_y, camera_z);
gimp_transform_3d_matrix4_to_matrix3 (&m, matrix, 2);
}
|