From 200c86b91ea7063d35be3bffc11c5da53c054653 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Mon, 18 Aug 2014 18:17:11 -0400 Subject: Move some headers around and make new ones. --- libdimension/dimension.h | 22 +++------ libdimension/dimension/common.h | 30 ++++++++++++ libdimension/dimension/geometry.h | 74 ----------------------------- libdimension/dimension/math.h | 99 +++++++++++++++++++++++++++++++++++++++ libdimension/polynomial.c | 8 ++-- 5 files changed, 139 insertions(+), 94 deletions(-) create mode 100644 libdimension/dimension/common.h create mode 100644 libdimension/dimension/math.h diff --git a/libdimension/dimension.h b/libdimension/dimension.h index 71c68fa..fc3a681 100644 --- a/libdimension/dimension.h +++ b/libdimension/dimension.h @@ -20,25 +20,21 @@ /** * @file - * The main \#include file for libdimension. This file declares all public - * functions and types used by the Dimension library. You should never attempt - * to include any of the component headers in the dimension/ subdirectory - * directly; only this file. + * The main \#include file for the Dimension library. This file declares all + * of its public functions and types. */ /** * @mainpage libdimension - A library for photo-realistic 3-D rendering * * The Dimension library (libdimension) is the ray-tracing library that handles - * all rendering-related tasks for Dimension. It is written in C and designed - * with performance and concurrency in mind. It is also generic enough to be - * used for applications other than Dimension, though that is its primary - * purpose. + * all rendering-related tasks for Dimension. */ #ifndef DIMENSION_H #define DIMENSION_H +/* Include compiler.h first for DMNSN_CXX */ #include #if DMNSN_CXX @@ -46,15 +42,8 @@ extern "C" { #endif -/* Common types */ - -/** - * Generic callback type. - * @param[in,out] ptr A pointer to an object to act on. - */ -typedef void dmnsn_callback_fn(void *ptr); - /* Include all the libdimension headers */ +#include #include #include #include @@ -62,6 +51,7 @@ typedef void dmnsn_callback_fn(void *ptr); #include #include #include +#include #include #include #include diff --git a/libdimension/dimension/common.h b/libdimension/dimension/common.h new file mode 100644 index 0000000..15bafd8 --- /dev/null +++ b/libdimension/dimension/common.h @@ -0,0 +1,30 @@ +/************************************************************************* + * Copyright (C) 2014 Tavian Barnes * + * * + * This file is part of The Dimension Library. * + * * + * The Dimension Library is free software; you can redistribute it and/ * + * or modify it under the terms of the GNU Lesser General Public License * + * as published by the Free Software Foundation; either version 3 of the * + * License, or (at your option) any later version. * + * * + * The Dimension Library 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 * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with this program. If not, see * + * . * + *************************************************************************/ + +/** + * @file + * Common types and utilities. + */ + +/** + * Generic callback type. + * @param[in,out] ptr A pointer to an object to act on. + */ +typedef void dmnsn_callback_fn(void *ptr); diff --git a/libdimension/dimension/geometry.h b/libdimension/dimension/geometry.h index 811f591..2ea10ca 100644 --- a/libdimension/dimension/geometry.h +++ b/libdimension/dimension/geometry.h @@ -82,9 +82,6 @@ typedef struct dmnsn_bounding_box { /* Constants */ -/** The smallest value considered non-zero by some numerical algorithms. */ -#define dmnsn_epsilon 1.0e-10 - /** The zero vector. */ static const dmnsn_vector dmnsn_zero = { 0.0, 0.0, 0.0 }; /** The x vector. */ @@ -94,66 +91,6 @@ static const dmnsn_vector dmnsn_y = { 0.0, 1.0, 0.0 }; /** The z vector. */ static const dmnsn_vector dmnsn_z = { 0.0, 0.0, 1.0 }; -/** - * @def DMNSN_INFINITY - * Expands to floating-point infinity. - */ -#if defined(INFINITY) || DMNSN_C99 - #define DMNSN_INFINITY INFINITY -#else - #define DMNSN_INFINITY HUGE_VAL -#endif - -/* Scalar functions */ - -/** Find the minimum of two scalars. */ -DMNSN_INLINE double -dmnsn_min(double a, double b) -{ - return a < b ? a : b; -} - -/** Find the maximum of two scalars. */ -DMNSN_INLINE double -dmnsn_max(double a, double b) -{ - return a > b ? a : b; -} - -/** Clamp a value to an interval. */ -DMNSN_INLINE double -dmnsn_clamp(double n, double min, double max) -{ - return dmnsn_min(dmnsn_max(n, min), max); -} - -/** Convert degrees to radians. */ -DMNSN_INLINE double -dmnsn_radians(double degrees) -{ - return degrees*atan(1.0)/45.0; -} - -/** Convert radians to degrees. */ -DMNSN_INLINE double -dmnsn_degrees(double radians) -{ - return radians*45.0/atan(1.0); -} - -/** Return the sign of a scalar. */ -DMNSN_INLINE int -dmnsn_sign(double n) -{ - if (n > 0.0) { - return 1; - } else if (n < 0.0) { - return -1; - } else { - return 0; - } -} - /* Shorthand for vector/matrix construction */ /** Construct a new vector. */ @@ -526,17 +463,6 @@ dmnsn_bounding_box_swallow(dmnsn_bounding_box box, dmnsn_vector point) return ret; } -/** Return whether a scalar is NaN. */ -DMNSN_INLINE bool -dmnsn_isnan(double n) -{ -#if DMNSN_C99 - return isnan(n); -#else - return n != n; -#endif -} - /** Return whether a vector contains any NaN components. */ DMNSN_INLINE bool dmnsn_vector_isnan(dmnsn_vector v) diff --git a/libdimension/dimension/math.h b/libdimension/dimension/math.h new file mode 100644 index 0000000..597be36 --- /dev/null +++ b/libdimension/dimension/math.h @@ -0,0 +1,99 @@ +/************************************************************************* + * Copyright (C) 2014 Tavian Barnes * + * * + * This file is part of The Dimension Library. * + * * + * The Dimension Library is free software; you can redistribute it and/ * + * or modify it under the terms of the GNU Lesser General Public License * + * as published by the Free Software Foundation; either version 3 of the * + * License, or (at your option) any later version. * + * * + * The Dimension Library 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 * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with this program. If not, see * + * . * + *************************************************************************/ + +/** + * @file + * Useful math functions. + */ + +#include +#include + +/** The smallest value considered non-zero by some numerical algorithms. */ +#define dmnsn_epsilon 1.0e-10 + +/** + * @def DMNSN_INFINITY + * Expands to floating-point infinity. + */ +#if defined(INFINITY) || DMNSN_C99 + #define DMNSN_INFINITY INFINITY +#else + #define DMNSN_INFINITY HUGE_VAL +#endif + +/** Find the minimum of two values. */ +DMNSN_INLINE double +dmnsn_min(double a, double b) +{ + return a < b ? a : b; +} + +/** Find the maximum of two values. */ +DMNSN_INLINE double +dmnsn_max(double a, double b) +{ + return a > b ? a : b; +} + +/** Clamp a value to an interval. */ +DMNSN_INLINE double +dmnsn_clamp(double n, double min, double max) +{ + return dmnsn_min(dmnsn_max(n, min), max); +} + +/** Convert degrees to radians. */ +DMNSN_INLINE double +dmnsn_radians(double degrees) +{ + return degrees*(atan(1.0)/45.0); +} + +/** Convert radians to degrees. */ +DMNSN_INLINE double +dmnsn_degrees(double radians) +{ + return radians*(45.0/atan(1.0)); +} + +/** Signum function: return the sign of a value. */ +DMNSN_INLINE int +dmnsn_sgn(double n) +{ + if (n > 0.0) { + return 1; + } else if (n < 0.0) { + return -1; + } else { + return 0; + } +} + +/** Return whether a value is NaN. */ +DMNSN_INLINE bool +dmnsn_isnan(double n) +{ +#if DMNSN_C99 + return isnan(n); +#else + return n != n; +#endif +} diff --git a/libdimension/polynomial.c b/libdimension/polynomial.c index 23b96d1..609364a 100644 --- a/libdimension/polynomial.c +++ b/libdimension/polynomial.c @@ -125,9 +125,9 @@ dmnsn_descartes_bound(const double poly[], size_t degree) // Find the number of sign changes in p[] size_t changes = 0; - int lastsign = dmnsn_sign(p[0]); + int lastsign = dmnsn_sgn(p[0]); for (size_t i = 1; changes <= 1 && i <= degree; ++i) { - int sign = dmnsn_sign(p[i]); + int sign = dmnsn_sgn(p[i]); if (sign != 0 && sign != lastsign) { ++changes; lastsign = sign; @@ -227,7 +227,7 @@ dmnsn_bisect_root(const double poly[], size_t degree, double min, double max) for (size_t i = 0; i < DMNSN_BISECT_ITERATIONS; ++i) { mid = (min*evmax - max*evmin)/(evmax - evmin); evmid = dmnsn_polynomial_evaluate(poly, degree, mid); - int sign = dmnsn_sign(evmid); + int sign = dmnsn_sgn(evmid); if ((fabs(evmid) < fabs(mid)*dmnsn_epsilon // This condition improves stability when one of the bounds is close to @@ -250,7 +250,7 @@ dmnsn_bisect_root(const double poly[], size_t degree, double min, double max) evmin = evmax; max = mid; evmax = evmid; - } else if (sign == dmnsn_sign(evmax)) { + } else if (sign == dmnsn_sgn(evmax)) { max = mid; evmax = evmid; if (sign == lastsign) { -- cgit v1.2.3