From 72a8c1bb0e81f3dfc7a8a10ca11cf15ba5380e74 Mon Sep 17 00:00:00 2001 From: Daniel Egger Date: Fri, 26 Jan 2001 02:16:13 +0000 Subject: [PATCH] Added pass-by-value functions to allow code simplifications and 2001-01-26 Daniel Egger * libgimpmath/gimpvector.c: * libgimpmath/gimpvector.h: Added pass-by-value functions to allow code simplifications and functionchaining. --- ChangeLog | 6 + libgimpmath/gimpvector.c | 294 +++++++++++++++++++++++++++++++++++++++ libgimpmath/gimpvector.h | 148 +++++++++++++------- 3 files changed, 397 insertions(+), 51 deletions(-) diff --git a/ChangeLog b/ChangeLog index 38ec8db140..50fc038a6f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2001-01-26 Daniel Egger + + * libgimpmath/gimpvector.c: + * libgimpmath/gimpvector.h: Added pass-by-value functions + to allow code simplifications and functionchaining. + Thu Jan 25 10:22:39 PST 2001 Manish Singh * tools/pdbgen/pdb/channel.pdb: resurrected channel_set_color diff --git a/libgimpmath/gimpvector.c b/libgimpmath/gimpvector.c index 28e9f006e7..4cd01fbe9a 100644 --- a/libgimpmath/gimpvector.c +++ b/libgimpmath/gimpvector.c @@ -57,6 +57,13 @@ gimp_vector2_inner_product (GimpVector2 *vector1, return (vector1->x * vector2->x + vector1->y * vector2->y); } +gdouble +gimp_vector2_inner_product_val (GimpVector2 vector1, + GimpVector2 vector2) +{ + return (vector1.x * vector2.x + vector1.y * vector2.y); +} + GimpVector2 gimp_vector2_cross_product (GimpVector2 *vector1, GimpVector2 *vector2) @@ -72,6 +79,18 @@ gimp_vector2_cross_product (GimpVector2 *vector1, return normal; } +GimpVector2 +gimp_vector2_cross_product_val (GimpVector2 vector1, + GimpVector2 vector2) +{ + GimpVector2 normal; + + normal.x = vector1.x * vector2.y - vector1.y * vector2.x; + normal.y = vector1.y * vector2.x - vector1.x * vector2.y; + + return normal; +} + gdouble gimp_vector2_length (GimpVector2 *vector) { @@ -80,6 +99,12 @@ gimp_vector2_length (GimpVector2 *vector) return (sqrt (vector->x * vector->x + vector->y * vector->y)); } +gdouble +gimp_vector2_length_val (GimpVector2 vector) +{ + return (sqrt (vector.x * vector.x + vector.y * vector.y)); +} + void gimp_vector2_normalize (GimpVector2 *vector) { @@ -100,6 +125,27 @@ gimp_vector2_normalize (GimpVector2 *vector) } } +GimpVector2 +gimp_vector2_normalize_val (GimpVector2 vector) +{ + + GimpVector2 normalized; + gdouble len; + + len = gimp_vector2_length_val (vector); + if (len != 0.0) + { + len = 1.0 / len; + normalized.x = vector.x * len; + normalized.y = vector.y * len; + return normalized; + } + else + { + return gimp_vector2_zero; + } +} + void gimp_vector2_mul (GimpVector2 *vector, gdouble factor) @@ -110,6 +156,18 @@ gimp_vector2_mul (GimpVector2 *vector, vector->y *= factor; } +GimpVector2 +gimp_vector2_mul_val (GimpVector2 vector, + gdouble factor) +{ + GimpVector2 result; + + result.x = vector.x * factor; + result.y = vector.y * factor; + + return result; +} + void gimp_vector2_sub (GimpVector2 *result, GimpVector2 *vector1, @@ -123,6 +181,18 @@ gimp_vector2_sub (GimpVector2 *result, result->y = vector1->y - vector2->y; } +GimpVector2 +gimp_vector2_sub_val (GimpVector2 vector1, + GimpVector2 vector2) +{ + GimpVector2 result; + + result.x = vector1.x - vector2.x; + result.y = vector1.y - vector2.y; + + return result; +} + void gimp_vector2_set (GimpVector2 *vector, gdouble x, @@ -134,6 +204,18 @@ gimp_vector2_set (GimpVector2 *vector, vector->y = y; } +GimpVector2 +gimp_vector2_new (gdouble x, + gdouble y) +{ + GimpVector2 vector; + + vector.x = x; + vector.y = y; + + return vector; +} + void gimp_vector2_add (GimpVector2 *result, GimpVector2 *vector1, @@ -147,6 +229,18 @@ gimp_vector2_add (GimpVector2 *result, result->y = vector1->y + vector2->y; } +GimpVector2 +gimp_vector2_add_val (GimpVector2 vector1, + GimpVector2 vector2) +{ + GimpVector2 result; + + result.x = vector1.x + vector2.x; + result.y = vector1.y + vector2.y; + + return result; +} + void gimp_vector2_neg (GimpVector2 *vector) { @@ -156,6 +250,17 @@ gimp_vector2_neg (GimpVector2 *vector) vector->y *= -1.0; } +GimpVector2 +gimp_vector2_neg_val (GimpVector2 vector) +{ + GimpVector2 result; + + result.x = vector.x * -1.0; + result.y = vector.y * -1.0; + + return result; +} + void gimp_vector2_rotate (GimpVector2 *vector, gdouble alpha) @@ -170,6 +275,18 @@ gimp_vector2_rotate (GimpVector2 *vector, *vector = result; } +GimpVector2 +gimp_vector2_rotate_val (GimpVector2 vector, + gdouble alpha) +{ + GimpVector2 result; + + result.x = cos (alpha) * vector.x + sin (alpha) * vector.y; + result.y = cos (alpha) * vector.y - sin (alpha) * vector.x; + + return result; +} + /**************************************/ /* Three dimensional vector functions */ /**************************************/ @@ -186,6 +303,15 @@ gimp_vector3_inner_product (GimpVector3 *vector1, vector1->z * vector2->z); } +gdouble +gimp_vector3_inner_product_val (GimpVector3 vector1, + GimpVector3 vector2) +{ + return (vector1.x * vector2.x + + vector1.y * vector2.y + + vector1.z * vector2.z); +} + GimpVector3 gimp_vector3_cross_product (GimpVector3 *vector1, GimpVector3 *vector2) @@ -202,6 +328,19 @@ gimp_vector3_cross_product (GimpVector3 *vector1, return normal; } +GimpVector3 +gimp_vector3_cross_product_val (GimpVector3 vector1, + GimpVector3 vector2) +{ + GimpVector3 normal; + + normal.x = vector1.y * vector2.z - vector1.z * vector2.y; + normal.y = vector1.z * vector2.x - vector1.x * vector2.z; + normal.z = vector1.x * vector2.y - vector1.y * vector2.x; + + return normal; +} + gdouble gimp_vector3_length (GimpVector3 *vector) { @@ -212,6 +351,14 @@ gimp_vector3_length (GimpVector3 *vector) vector->z * vector->z)); } +gdouble +gimp_vector3_length_val (GimpVector3 vector) +{ + return (sqrt (vector.x * vector.x + + vector.y * vector.y + + vector.z * vector.z)); +} + void gimp_vector3_normalize (GimpVector3 *vector) { @@ -233,6 +380,27 @@ gimp_vector3_normalize (GimpVector3 *vector) } } +GimpVector3 +gimp_vector3_normalize_val (GimpVector3 vector) +{ + GimpVector3 result; + gdouble len; + + len = gimp_vector3_length_val (vector); + if (len != 0.0) + { + len = 1.0 / len; + result.x = vector.x * len; + result.y = vector.y * len; + result.z = vector.z * len; + return result; + } + else + { + return gimp_vector3_zero; + } +} + void gimp_vector3_mul (GimpVector3 *vector, gdouble factor) @@ -244,6 +412,19 @@ gimp_vector3_mul (GimpVector3 *vector, vector->z *= factor; } +GimpVector3 +gimp_vector3_mul_val (GimpVector3 vector, + gdouble factor) +{ + GimpVector3 result; + + result.x = vector.x * factor; + result.y = vector.y * factor; + result.z = vector.z * factor; + + return result; +} + void gimp_vector3_sub (GimpVector3 *result, GimpVector3 *vector1, @@ -258,6 +439,19 @@ gimp_vector3_sub (GimpVector3 *result, result->z = vector1->z - vector2->z; } +GimpVector3 +gimp_vector3_subval (GimpVector3 vector1, + GimpVector3 vector2) +{ + GimpVector3 result; + + result.x = vector1.x - vector2.x; + result.y = vector1.y - vector2.y; + result.z = vector1.z - vector2.z; + + return result; +} + void gimp_vector3_set (GimpVector3 *vector, gdouble x, @@ -271,6 +465,20 @@ gimp_vector3_set (GimpVector3 *vector, vector->z = z; } +GimpVector3 +gimp_vector3_new (gdouble x, + gdouble y, + gdouble z) +{ + GimpVector3 vector; + + vector.x = x; + vector.y = y; + vector.z = z; + + return vector; +} + void gimp_vector3_add (GimpVector3 *result, GimpVector3 *vector1, @@ -285,6 +493,19 @@ gimp_vector3_add (GimpVector3 *result, result->z = vector1->z + vector2->z; } +GimpVector3 +gimp_vector3_add_val (GimpVector3 vector1, + GimpVector3 vector2) +{ + GimpVector3 result; + + result.x = vector1.x + vector2.x; + result.y = vector1.y + vector2.y; + result.z = vector1.z + vector2.z; + + return result; +} + void gimp_vector3_neg (GimpVector3 *vector) { @@ -295,6 +516,18 @@ gimp_vector3_neg (GimpVector3 *vector) vector->z *= -1.0; } +GimpVector3 +gimp_vector3_neg_val (GimpVector3 vector) +{ + GimpVector3 result; + + result.x = vector.x * -1.0; + result.y = vector.y * -1.0; + result.z = vector.z * -1.0; + + return result; +} + void gimp_vector3_rotate (GimpVector3 *vector, gdouble alpha, @@ -326,6 +559,37 @@ gimp_vector3_rotate (GimpVector3 *vector, vector->z = cos (gamma) * s.z - sin (gamma) * t.y; } +GimpVector3 +gimp_vector3_rotate_val (GimpVector3 vector, + gdouble alpha, + gdouble beta, + gdouble gamma) +{ + GimpVector3 s, t, result; + + /* First we rotate it around the Z axis (XY plane).. */ + /* ================================================= */ + + s.x = cos (alpha) * vector.x + sin (alpha) * vector.y; + s.y = cos (alpha) * vector.y - sin (alpha) * vector.x; + + /* ..then around the Y axis (XZ plane).. */ + /* ===================================== */ + + t = s; + + result.x = cos (beta) *t.x + sin (beta) * vector.z; + s.z = cos (beta) *vector.z - sin (beta) * t.x; + + /* ..and at last around the X axis (YZ plane) */ + /* ========================================== */ + + result.y = cos (gamma) * t.y + sin (gamma) * s.z; + result.z = cos (gamma) * s.z - sin (gamma) * t.y; + + return result; +} + /******************************************************************/ /* Compute screen (sx,sy)-(sx+w,sy+h) to 3D unit square mapping. */ /* The plane to map to is given in the z field of p. The observer */ @@ -362,6 +626,36 @@ gimp_vector_2d_to_3d (gint sx, } } +GimpVector3 +gimp_vector_2d_to_3d_val (gint sx, + gint sy, + gint w, + gint h, + gint x, + gint y, + GimpVector3 vp, + GimpVector3 p) +{ + GimpVector3 result; + gdouble t = 0.0; + + if (vp.x != 0.0) + t = (p.z - vp.z) / vp.z; + + if (t != 0.0) + { + result.x = vp.x + t * (vp.x - ((gdouble) (x - sx) / (gdouble) w)); + result.y = vp.y + t * (vp.y - ((gdouble) (y - sy) / (gdouble) h)); + } + else + { + result.x = (gdouble) (x - sx) / (gdouble) w; + result.y = (gdouble) (y - sy) / (gdouble) h; + } + + return result; +} + /*********************************************************/ /* Convert the given 3D point to 2D (project it onto the */ /* viewing plane, (sx,sy,0)-(sx+w,sy+h,0). The input is */ diff --git a/libgimpmath/gimpvector.h b/libgimpmath/gimpvector.h index f83dc974c2..ea2ec9c706 100644 --- a/libgimpmath/gimpvector.h +++ b/libgimpmath/gimpvector.h @@ -34,65 +34,111 @@ extern "C" { /* Two dimensional vector functions */ /* ================================ */ -gdouble gimp_vector2_inner_product (GimpVector2 *vector1, - GimpVector2 *vector2); -GimpVector2 gimp_vector2_cross_product (GimpVector2 *vector1, - GimpVector2 *vector2); -gdouble gimp_vector2_length (GimpVector2 *vector); -void gimp_vector2_normalize (GimpVector2 *vector); -void gimp_vector2_mul (GimpVector2 *vector, - gdouble factor); -void gimp_vector2_sub (GimpVector2 *result, - GimpVector2 *vector1, - GimpVector2 *vector2); -void gimp_vector2_set (GimpVector2 *vector, - gdouble x, - gdouble y); -void gimp_vector2_add (GimpVector2 *result, - GimpVector2 *vector1, - GimpVector2 *vector2); -void gimp_vector2_neg (GimpVector2 *vector); -void gimp_vector2_rotate (GimpVector2 *vector, - gdouble alpha); +gdouble gimp_vector2_inner_product (GimpVector2 *vector1, + GimpVector2 *vector2); +gdouble gimp_vector2_inner_product_val (GimpVector2 vector1, + GimpVector2 vector2); +GimpVector2 gimp_vector2_cross_product (GimpVector2 *vector1, + GimpVector2 *vector2); +GimpVector2 gimp_vector2_cross_product_val (GimpVector2 vector1, + GimpVector2 vector2); +gdouble gimp_vector2_length (GimpVector2 *vector); +gdouble gimp_vector2_length_val (GimpVector2 vector); +void gimp_vector2_normalize (GimpVector2 *vector); +GimpVector2 gimp_vector2_normalize_val (GimpVector2 vector); +void gimp_vector2_mul (GimpVector2 *vector, + gdouble factor); +GimpVector2 gimp_vector2_mul_val (GimpVector2 vector, + gdouble factor); +void gimp_vector2_sub (GimpVector2 *result, + GimpVector2 *vector1, + GimpVector2 *vector2); +GimpVector2 gimp_vector2_sub_val (GimpVector2 vector1, + GimpVector2 vector2); +void gimp_vector2_set (GimpVector2 *vector, + gdouble x, + gdouble y); +GimpVector2 gimp_vector2_new_val (gdouble x, + gdouble y); +void gimp_vector2_add (GimpVector2 *result, + GimpVector2 *vector1, + GimpVector2 *vector2); +GimpVector2 gimp_vector2_add_val (GimpVector2 vector1, + GimpVector2 vector2); +void gimp_vector2_neg (GimpVector2 *vector); +GimpVector2 gimp_vector2_neg_val (GimpVector2 vector); +void gimp_vector2_rotate (GimpVector2 *vector, + gdouble alpha); +GimpVector2 gimp_vector2_rotate_val (GimpVector2 vector, + gdouble alpha); /* Three dimensional vector functions */ /* ================================== */ -gdouble gimp_vector3_inner_product (GimpVector3 *vector1, - GimpVector3 *vector2); -GimpVector3 gimp_vector3_cross_product (GimpVector3 *vector1, - GimpVector3 *vector2); -gdouble gimp_vector3_length (GimpVector3 *vector); -void gimp_vector3_normalize (GimpVector3 *vector); -void gimp_vector3_mul (GimpVector3 *vector, - gdouble factor); -void gimp_vector3_sub (GimpVector3 *result, - GimpVector3 *vector1, - GimpVector3 *vector2); -void gimp_vector3_set (GimpVector3 *vector, - gdouble x, - gdouble y, - gdouble z); -void gimp_vector3_add (GimpVector3 *result, - GimpVector3 *vector1, - GimpVector3 *vector2); -void gimp_vector3_neg (GimpVector3 *vector); -void gimp_vector3_rotate (GimpVector3 *vector, - gdouble alpha, - gdouble beta, - gdouble gamma); +gdouble gimp_vector3_inner_product (GimpVector3 *vector1, + GimpVector3 *vector2); +gdouble gimp_vector3_inner_product_val (GimpVector3 vector1, + GimpVector3 vector2); +GimpVector3 gimp_vector3_cross_product (GimpVector3 *vector1, + GimpVector3 *vector2); +GimpVector3 gimp_vector3_cross_product_val (GimpVector3 vector1, + GimpVector3 vector2); +gdouble gimp_vector3_length (GimpVector3 *vector); +gdouble gimp_vector3_length_val (GimpVector3 vector); +void gimp_vector3_normalize (GimpVector3 *vector); +GimpVector3 gimp_vector3_normalize_val (GimpVector3 vector); +void gimp_vector3_mul (GimpVector3 *vector, + gdouble factor); +GimpVector3 gimp_vector3_mul_val (GimpVector3 vector, + gdouble factor); +void gimp_vector3_sub (GimpVector3 *result, + GimpVector3 *vector1, + GimpVector3 *vector2); +GimpVector3 gimp_vector3_sub_val (GimpVector3 vector1, + GimpVector3 vector2); +void gimp_vector3_set (GimpVector3 *vector, + gdouble x, + gdouble y, + gdouble z); +GimpVector3 gimp_vector3_new (gdouble x, + gdouble y, + gdouble z); +void gimp_vector3_add (GimpVector3 *result, + GimpVector3 *vector1, + GimpVector3 *vector2); +GimpVector3 gimp_vector3_add_val (GimpVector3 vector1, + GimpVector3 vector2); +void gimp_vector3_neg (GimpVector3 *vector); +GimpVector3 gimp_vector3_neg_val (GimpVector3 vector); +void gimp_vector3_rotate (GimpVector3 *vector, + gdouble alpha, + gdouble beta, + gdouble gamma); +GimpVector3 gimp_vector3_rotate_val (GimpVector3 vector, + gdouble alpha, + gdouble beta, + gdouble gamma); /* 2d <-> 3d Vector projection functions */ /* ===================================== */ -void gimp_vector_2d_to_3d (gint sx, - gint sy, - gint w, - gint h, - gint x, - gint y, - GimpVector3 *vp, - GimpVector3 *p); +void gimp_vector_2d_to_3d (gint sx, + gint sy, + gint w, + gint h, + gint x, + gint y, + GimpVector3 *vp, + GimpVector3 *p); + +GimpVector3 gimp_vector_2d_to_3d_val (gint sx, + gint sy, + gint w, + gint h, + gint x, + gint y, + GimpVector3 vp, + GimpVector3 p); void gimp_vector_3d_to_2d (gint sx, gint sy,