diff options
Diffstat (limited to 'converter/other')
-rw-r--r-- | converter/other/pamtosvg/vector.c | 271 | ||||
-rw-r--r-- | converter/other/pamtosvg/vector.h | 66 |
2 files changed, 195 insertions, 142 deletions
diff --git a/converter/other/pamtosvg/vector.c b/converter/other/pamtosvg/vector.c index 559163fc..43e1872e 100644 --- a/converter/other/pamtosvg/vector.c +++ b/converter/other/pamtosvg/vector.c @@ -17,136 +17,149 @@ static float acos_d (float, at_exception_type * excep); /* Given the point COORD, return the corresponding vector. */ vector_type -make_vector (const float_coord c) -{ - vector_type v; +make_vector(float_coord const c) { - v.dx = c.x; - v.dy = c.y; - v.dz = c.z; + vector_type v; + + v.dx = c.x; + v.dy = c.y; + v.dz = c.z; - return v; + return v; } + /* And the converse: given a vector, return the corresponding point. */ float_coord -vector_to_point (const vector_type v) -{ - float_coord coord; +vector_to_point(vector_type const v) { + + float_coord coord; - coord.x = v.dx; - coord.y = v.dy; + coord.x = v.dx; + coord.y = v.dy; - return coord; + return coord; } -float -magnitude (const vector_type v) -{ - return (float) sqrt (v.dx * v.dx + v.dy * v.dy + v.dz * v.dz); -} +float +magnitude(vector_type const v) { -vector_type -normalize (const vector_type v) -{ - vector_type new_v; - float m = magnitude (v); + return sqrt(SQR(v.dx) + SQR(v.dy) + SQR(v.dz)); +} - /* assert (m > 0.0); */ - if (m > 0.0) - { - new_v.dx = v.dx / m; - new_v.dy = v.dy / m; - new_v.dz = v.dz / m; - } - else - { - new_v.dx = v.dx; - new_v.dy = v.dy; - new_v.dz = v.dz; - } - return new_v; +vector_type +normalize(vector_type const v) { + + vector_type new_v; + float const m = magnitude(v); + + if (m > 0.0) { + new_v.dx = v.dx / m; + new_v.dy = v.dy / m; + new_v.dz = v.dz / m; + } else { + new_v.dx = v.dx; + new_v.dy = v.dy; + new_v.dz = v.dz; + } + + return new_v; } + vector_type -Vadd (const vector_type v1, const vector_type v2) -{ - vector_type new_v; +Vadd(vector_type const v1, + vector_type const v2) { + + vector_type new_v; - new_v.dx = v1.dx + v2.dx; - new_v.dy = v1.dy + v2.dy; - new_v.dz = v1.dz + v2.dz; + new_v.dx = v1.dx + v2.dx; + new_v.dy = v1.dy + v2.dy; + new_v.dz = v1.dz + v2.dz; - return new_v; + return new_v; } + float -Vdot (const vector_type v1, const vector_type v2) -{ - return v1.dx * v2.dx + v1.dy * v2.dy + v1.dz * v2.dz; +Vdot(vector_type const v1, + vector_type const v2) { + + return v1.dx * v2.dx + v1.dy * v2.dy + v1.dz * v2.dz; } + vector_type -Vmult_scalar (const vector_type v, const float r) -{ - vector_type new_v; +Vmult_scalar(vector_type const v, + float const r) { - new_v.dx = v.dx * r; - new_v.dy = v.dy * r; - new_v.dz = v.dz * r; + vector_type new_v; - return new_v; + new_v.dx = v.dx * r; + new_v.dy = v.dy * r; + new_v.dz = v.dz * r; + + return new_v; } + /* Given the IN_VECTOR and OUT_VECTOR, return the angle between them in - degrees, in the range zero to 180. */ + degrees, in the range zero to 180. +*/ float -Vangle (const vector_type in_vector, - const vector_type out_vector, - at_exception_type * exp) -{ - vector_type v1 = normalize (in_vector); - vector_type v2 = normalize (out_vector); +Vangle(vector_type const in_vector, + vector_type const out_vector, + at_exception_type * const exP) { - return acos_d (Vdot (v2, v1), exp); + vector_type const v1 = normalize(in_vector); + vector_type const v2 = normalize(out_vector); + + return acos_d(Vdot(v2, v1), exP); } + float_coord -Vadd_point (const float_coord c, const vector_type v) -{ - float_coord new_c; +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; + new_c.x = c.x + v.dx; + new_c.y = c.y + v.dy; + new_c.z = c.z + v.dz; + + return new_c; } + float_coord -Vsubtract_point (const float_coord c, const vector_type v) -{ - float_coord new_c; +Vsubtract_point(float_coord const c, + vector_type const v) { - new_c.x = c.x - v.dx; - new_c.y = c.y - v.dy; - new_c.z = c.z - v.dz; - return new_c; + 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; } + pm_pixelcoord Vadd_int_point(pm_pixelcoord const c, vector_type const v) { @@ -160,56 +173,64 @@ Vadd_int_point(pm_pixelcoord const c, } + vector_type -Vabs (const vector_type v) -{ - vector_type new_v; +Vabs(vector_type const v) { + + vector_type new_v; - new_v.dx = (float) fabs (v.dx); - new_v.dy = (float) fabs (v.dy); - new_v.dz = (float) fabs (v.dz); - return new_v; + new_v.dx = (float) fabs (v.dx); + new_v.dy = (float) fabs (v.dy); + new_v.dz = (float) fabs (v.dz); + + return new_v; } + /* Operations on points. */ float_coord -Padd (const float_coord coord1, const float_coord coord2) -{ - float_coord sum; +Padd(float_coord const coord1, + float_coord const coord2) { + + float_coord sum; - sum.x = coord1.x + coord2.x; - sum.y = coord1.y + coord2.y; - sum.z = coord1.z + coord2.z; + sum.x = coord1.x + coord2.x; + sum.y = coord1.y + coord2.y; + sum.z = coord1.z + coord2.z; - return sum; + return sum; } + float_coord -Pmult_scalar (const float_coord coord, const float r) -{ - float_coord answer; +Pmult_scalar(float_coord const coord, + float const r) { + + float_coord answer; - answer.x = coord.x * r; - answer.y = coord.y * r; - answer.z = coord.z * r; + answer.x = coord.x * r; + answer.y = coord.y * r; + answer.z = coord.z * r; - return answer; + return answer; } + vector_type -Psubtract (const float_coord c1, const float_coord c2) -{ - vector_type v; +Psubtract(float_coord const c1, + float_coord const c2) { + + vector_type v; - v.dx = c1.x - c2.x; - v.dy = c1.y - c2.y; - v.dz = c1.z - c2.z; + v.dx = c1.x - c2.x; + v.dy = c1.y - c2.y; + v.dz = c1.z - c2.z; - return v; + return v; } @@ -232,23 +253,27 @@ IPsubtract(pm_pixelcoord const coord1, static float -acos_d (float v, at_exception_type * excep) -{ - float a; - - if (epsilon_equal (v, 1.0)) - v = 1.0; - else if (epsilon_equal (v, -1.0)) - v = -1.0; - - errno = 0; - a = (float) acos (v); - if (errno == ERANGE || errno == EDOM) - { - at_exception_fatal(excep, strerror(errno)); - return 0.0; - } - - - return a * (float) 180.0 / (float) M_PI; +acos_d(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; + + errno = 0; + a = acos(v); + if (errno == ERANGE || errno == EDOM) { + at_exception_fatal(excepP, strerror(errno)); + retval = 0.0; + } else + retval = a * 180.0 / M_PI; + + return retval; } diff --git a/converter/other/pamtosvg/vector.h b/converter/other/pamtosvg/vector.h index 74fb2fd2..b3a5454e 100644 --- a/converter/other/pamtosvg/vector.h +++ b/converter/other/pamtosvg/vector.h @@ -15,32 +15,54 @@ typedef struct /* Consider a point as a vector from the origin. */ -extern vector_type make_vector (const float_coord); +vector_type +make_vector(float_coord const); /* And a vector as a point, i.e., a displacement from the origin. */ -extern float_coord vector_to_point (const vector_type); +float_coord +vector_to_point(vector_type const); /* Definitions for these common operations can be found in any decent linear algebra book, and most calculus books. */ -extern float magnitude (const vector_type); -extern vector_type normalize (const vector_type); +float +magnitude(vector_type const); + +vector_type +normalize(vector_type const); + +vector_type +Vadd(vector_type const, + vector_type const); + +float +Vdot(vector_type const, + vector_type const); + +vector_type +Vmult_scalar(vector_type const, + float const); -extern vector_type Vadd (const vector_type, const vector_type); -extern float Vdot (const vector_type, const vector_type); -extern vector_type Vmult_scalar (const vector_type, const float); -extern float Vangle (const vector_type in, const vector_type out, at_exception_type * exp); +float +Vangle(vector_type const in, + vector_type const out, + at_exception_type * const exP); /* These operations could have been named `P..._vector' just as well as V..._point, so we may as well allow both names. */ + #define Padd_vector Vadd_point -extern float_coord Vadd_point - (const float_coord, const vector_type); + +float_coord +Vadd_point(float_coord const, + vector_type const); #define Psubtract_vector Vsubtract_point -extern float_coord Vsubtract_point - (const float_coord, const vector_type); + +float_coord +Vsubtract_point(float_coord const, + vector_type const); /* This returns the rounded sum. */ #define IPadd_vector Vadd_int_point @@ -50,22 +72,28 @@ Vadd_int_point(pm_pixelcoord const c, vector_type const v); /* Take the absolute value of both components. */ -extern vector_type Vabs (const vector_type); +vector_type +Vabs(vector_type const); /* Operations on points with real coordinates. It is not orthogonal, but more convenient, to have the subtraction operator return a vector, and the addition operator return a point. */ -extern vector_type Psubtract - (const float_coord, const float_coord); +vector_type +Psubtract(float_coord const, + float_coord const); vector_type IPsubtract(pm_pixelcoord const coord1, pm_pixelcoord const coord2); /* These are heavily used in spline fitting. */ -extern float_coord Padd (const float_coord, - const float_coord); -extern float_coord Pmult_scalar (const float_coord, const float); -#endif +float_coord +Padd(float_coord const, + float_coord const); +float_coord +Pmult_scalar(float_coord const, + float const); + +#endif |