diff options
Diffstat (limited to 'converter/other/pamtosvg/vector.c')
-rw-r--r-- | converter/other/pamtosvg/vector.c | 295 |
1 files changed, 154 insertions, 141 deletions
diff --git a/converter/other/pamtosvg/vector.c b/converter/other/pamtosvg/vector.c index 771e5f27..a02b933b 100644 --- a/converter/other/pamtosvg/vector.c +++ b/converter/other/pamtosvg/vector.c @@ -10,17 +10,41 @@ #include "vector.h" #include "message.h" -#include "epsilon-equal.h" +#include "epsilon.h" -static float acos_d (float, at_exception_type * excep); +static float +acosD(float const v, + at_exception_type * const excepP) { + + float vAdj; + float a; + float retval; + if (epsilon_equal(v, 1.0)) + vAdj = 1.0; + else if (epsilon_equal(v, -1.0)) + vAdj = -1.0; + else + vAdj = v; -/* Given the point COORD, return the corresponding vector. */ + errno = 0; + a = acos(vAdj); + if (errno == ERANGE || errno == EDOM) { + at_exception_fatal(excepP, strerror(errno)); + retval = 0.0; + } else + retval = a * 180.0 / M_PI; + + return retval; +} -vector_type -make_vector(float_coord const c) { - vector_type v; + +Vector +vector_fromPoint(Point const c) { +/* Vector corresponding to point 'c', taken as a vector from the origin. */ + + Vector v; v.dx = c.x; v.dy = c.y; @@ -31,12 +55,28 @@ make_vector(float_coord const c) { +Vector +vector_fromTwoPoints(Point const c1, + Point const c2) { + + Vector retval; + + retval.dx = c1.x - c2.x; + retval.dy = c1.y - c2.y; + retval.dz = c1.z - c2.z; + + return retval; +} + + + /* And the converse: given a vector, return the corresponding point. */ -float_coord -vector_to_point(vector_type const v) { +Point +vector_toPoint_point(Vector const v) { +/* vector as a point, i.e., a displacement from the origin. */ - float_coord coord; + Point coord; coord.x = v.dx; coord.y = v.dy; @@ -48,18 +88,18 @@ vector_to_point(vector_type const v) { float -magnitude(vector_type const v) { +vector_magnitude(Vector const v) { return sqrt(SQR(v.dx) + SQR(v.dy) + SQR(v.dz)); } -vector_type -normalize(vector_type const v) { +Vector +vector_normalized(Vector const v) { - vector_type new_v; - float const m = magnitude(v); + Vector new_v; + float const m = vector_magnitude(v); if (m > 0.0) { new_v.dx = v.dx / m; @@ -70,221 +110,194 @@ normalize(vector_type const v) { new_v.dy = v.dy; new_v.dz = v.dz; } - + return new_v; } -vector_type -Vadd(vector_type const v1, - vector_type const v2) { +Vector +vector_sum(Vector const addend, + Vector const adder) { - vector_type new_v; + Vector retval; - new_v.dx = v1.dx + v2.dx; - new_v.dy = v1.dy + v2.dy; - new_v.dz = v1.dz + v2.dz; + retval.dx = addend.dx + adder.dx; + retval.dy = addend.dy + adder.dy; + retval.dz = addend.dz + adder.dz; - return new_v; + return retval; } float -Vdot(vector_type const v1, - vector_type const v2) { +vector_dotProduct(Vector const v1, + Vector const v2) { return v1.dx * v2.dx + v1.dy * v2.dy + v1.dz * v2.dz; } -vector_type -Vmult_scalar(vector_type const v, - float const r) { +Vector +vector_scaled(Vector const v, + float const r) { - vector_type new_v; + Vector retval; - new_v.dx = v.dx * r; - new_v.dy = v.dy * r; - new_v.dz = v.dz * r; + retval.dx = v.dx * r; + retval.dy = v.dy * r; + retval.dz = v.dz * r; - return new_v; + return retval; } -/* Given the IN_VECTOR and OUT_VECTOR, return the angle between them in - degrees, in the range zero to 180. -*/ - float -Vangle(vector_type const in_vector, - vector_type const out_vector, - at_exception_type * const exP) { - - vector_type const v1 = normalize(in_vector); - vector_type const v2 = normalize(out_vector); - - return acos_d(Vdot(v2, v1), exP); -} +vector_angle(Vector const inVector, + Vector const outVector, + at_exception_type * const exP) { +/* The angle between 'inVector' and 'outVector' in degrees, in the range zero + to 180. +*/ + Vector const v1 = vector_normalized(inVector); + Vector const v2 = vector_normalized(outVector); -float_coord -Vadd_point(float_coord const c, - vector_type const v) { - - float_coord new_c; - - new_c.x = c.x + v.dx; - new_c.y = c.y + v.dy; - new_c.z = c.z + v.dz; - - return new_c; + return acosD(vector_dotProduct(v2, v1), exP); } -float_coord -Vsubtract_point(float_coord const c, - vector_type const v) { +Point +vector_sumPoint(Point const c, + Vector const v) { - float_coord new_c; + Point retval; - new_c.x = c.x - v.dx; - new_c.y = c.y - v.dy; - new_c.z = c.z - v.dz; + retval.x = c.x + v.dx; + retval.y = c.y + v.dy; + retval.z = c.z + v.dz; - return new_c; + return retval; } -pm_pixelcoord -Vadd_int_point(pm_pixelcoord const c, - vector_type const v) { +Point +vector_diffPoint(Point const c, + Vector const v) { + + Point retval; - pm_pixelcoord a; + retval.x = c.x - v.dx; + retval.y = c.y - v.dy; + retval.z = c.z - v.dz; - a.col = ROUND ((float) c.col + v.dx); - a.row = ROUND ((float) c.row + v.dy); - - return a; + return retval; } -vector_type -Vabs(vector_type const v) { +Vector +vector_IPointDiff(pm_pixelcoord const coord1, + pm_pixelcoord const coord2) { - vector_type new_v; + Vector retval; - new_v.dx = (float) fabs (v.dx); - new_v.dy = (float) fabs (v.dy); - new_v.dz = (float) fabs (v.dz); + retval.dx = (int) (coord1.col - coord2.col); + retval.dy = (int) (coord1.row - coord2.row); + retval.dz = 0.0; - return new_v; + return retval; } -/* Operations on points. */ +pm_pixelcoord +vector_sumIntPoint(pm_pixelcoord const c, + Vector const v) { -float_coord -Padd(float_coord const coord1, - float_coord const coord2) { +/* This returns the rounded sum. */ - float_coord sum; + pm_pixelcoord retval; - sum.x = coord1.x + coord2.x; - sum.y = coord1.y + coord2.y; - sum.z = coord1.z + coord2.z; + retval.col = ROUND ((float) c.col + v.dx); + retval.row = ROUND ((float) c.row + v.dy); - return sum; + return retval; } -float_coord -Pmult_scalar(float_coord const coord, - float const r) { +Vector +vector_abs(Vector const v) { + +/* First-quadrant mirror of 'v' (both components unsigned) */ - float_coord answer; + Vector retval; - answer.x = coord.x * r; - answer.y = coord.y * r; - answer.z = coord.z * r; + retval.dx = (float) fabs (v.dx); + retval.dy = (float) fabs (v.dy); + retval.dz = (float) fabs (v.dz); - return answer; + return retval; } -vector_type -Psubtract(float_coord const c1, - float_coord const c2) { +Vector +vector_pointDirection(Point const final, + Point const initial) { - vector_type v; + return vector_normalized(vector_fromTwoPoints(final, initial)); +} - v.dx = c1.x - c2.x; - v.dy = c1.y - c2.y; - v.dz = c1.z - c2.z; - return v; -} +Vector +vector_horizontal(void) { + Vector retval; -vector_type -Pdirection(float_coord const final, - float_coord const initial) { + retval.dx = 1.0; + retval.dy = 0.0; + retval.dz = 0.0; - return normalize(Psubtract(final, initial)); + return retval; } -/* Operations on integer points. */ +Vector +vector_zero(void) { -vector_type -IPsubtract(pm_pixelcoord const coord1, - pm_pixelcoord const coord2) { + Vector retval; - vector_type v; + retval.dx = 0.0; + retval.dy = 0.0; + retval.dz = 0.0; - v.dx = (int) (coord1.col - coord2.col); - v.dy = (int) (coord1.row - coord2.row); - v.dz = 0.0; - - return v; + return retval; } -static float -acos_d(float const v, - at_exception_type * const excepP) { +bool +vector_equal(Vector const comparand, + Vector const comparator) { - float vAdj; - float a; - float retval; + return + epsilon_equal(comparand.dx, comparator.dx) + && + epsilon_equal(comparand.dy, comparator.dy) + && + epsilon_equal(comparand.dz, comparator.dz) + ; +} - if (epsilon_equal(v, 1.0)) - vAdj = 1.0; - else if (epsilon_equal(v, -1.0)) - vAdj = -1.0; - else - vAdj = v; - errno = 0; - a = acos(vAdj); - if (errno == ERANGE || errno == EDOM) { - at_exception_fatal(excepP, strerror(errno)); - retval = 0.0; - } else - retval = a * 180.0 / M_PI; - return retval; -} |