summaryrefslogtreecommitdiffstats
path: root/libdimension
diff options
context:
space:
mode:
Diffstat (limited to 'libdimension')
-rw-r--r--libdimension/Makefile.am2
-rw-r--r--libdimension/color_map.c74
-rw-r--r--libdimension/dimension.h1
-rw-r--r--libdimension/dimension/map.h76
-rw-r--r--libdimension/dimension/pigments.h34
-rw-r--r--libdimension/map.c126
6 files changed, 220 insertions, 93 deletions
diff --git a/libdimension/Makefile.am b/libdimension/Makefile.am
index 86caa57..e56a22b 100644
--- a/libdimension/Makefile.am
+++ b/libdimension/Makefile.am
@@ -37,6 +37,7 @@ nobase_include_HEADERS = dimension.h \
dimension/lights.h \
dimension/list.h \
dimension/malloc.h \
+ dimension/map.h \
dimension/object.h \
dimension/objects.h \
dimension/pattern.h \
@@ -75,6 +76,7 @@ libdimension_la_SOURCES = $(nobase_include_HEADERS) \
light.c \
list.c \
malloc.c \
+ map.c \
object.c \
pattern.c \
perspective.c \
diff --git a/libdimension/color_map.c b/libdimension/color_map.c
index c794374..f7d800e 100644
--- a/libdimension/color_map.c
+++ b/libdimension/color_map.c
@@ -25,71 +25,16 @@
#include "dimension.h"
-/** An [index, color] pair. */
-typedef struct dmnsn_color_map_entry {
- double n;
- dmnsn_color color;
-} dmnsn_color_map_entry;
-
-dmnsn_color_map *
-dmnsn_new_color_map(void)
-{
- return dmnsn_new_array(sizeof(dmnsn_color_map_entry));
-}
-
-void
-dmnsn_delete_color_map(dmnsn_color_map *map)
-{
- dmnsn_delete_array(map);
-}
-
-void
-dmnsn_add_color_map_entry(dmnsn_color_map *map, double n, dmnsn_color c)
-{
- dmnsn_color_map_entry entry = { .n = n, .color = c };
-
- /* Sorted insertion */
- size_t i;
- dmnsn_color_map_entry *other = dmnsn_array_last(map);
- for (i = dmnsn_array_size(map); i-- > 0;) {
- if (other->n <= n)
- break;
- }
-
- dmnsn_array_insert(map, i + 1, &entry);
-}
-
-dmnsn_color
-dmnsn_color_map_value(const dmnsn_color_map *map, double n)
+dmnsn_map *
+dmnsn_new_color_map()
{
- dmnsn_color_map_entry *entry = dmnsn_array_first(map);
-
- double n1, n2 = 0.0;
- dmnsn_color c1, c2 = entry->color;
-
- if (n < n2) {
- return c2;
- }
-
- for (; entry <= (dmnsn_color_map_entry *)dmnsn_array_last(map); ++entry) {
- n1 = n2;
- c1 = c2;
-
- n2 = entry->n;
- c2 = entry->color;
-
- if (n < n2) {
- return dmnsn_color_gradient(c1, c2, (n - n1)/(n2 - n1));
- }
- }
-
- return c2;
+ return dmnsn_new_map(sizeof(dmnsn_color));
}
/** Payload for a color_map pigment. */
typedef struct dmnsn_color_map_payload {
dmnsn_pattern *pattern;
- dmnsn_color_map *map;
+ dmnsn_map *map;
} dmnsn_color_map_payload;
/** Free a color_map payload. */
@@ -97,7 +42,7 @@ static void
dmnsn_delete_color_map_payload(void *ptr)
{
dmnsn_color_map_payload *payload = ptr;
- dmnsn_delete_color_map(payload->map);
+ dmnsn_delete_map(payload->map);
dmnsn_delete_pattern(payload->pattern);
dmnsn_free(payload);
}
@@ -107,8 +52,11 @@ static dmnsn_color
dmnsn_color_map_pigment_fn(const dmnsn_pigment *pigment, dmnsn_vector v)
{
const dmnsn_color_map_payload *payload = pigment->ptr;
- return dmnsn_color_map_value(payload->map,
- dmnsn_pattern_value(payload->pattern, v));
+ double n;
+ dmnsn_color color1, color2;
+ dmnsn_evaluate_map(payload->map, dmnsn_pattern_value(payload->pattern, v),
+ &n, &color1, &color2);
+ return dmnsn_color_gradient(color1, color2, n);
}
/** color_map initialization callback. */
@@ -122,7 +70,7 @@ dmnsn_color_map_initialize_fn(dmnsn_pigment *pigment)
}
dmnsn_pigment *
-dmnsn_new_color_map_pigment(dmnsn_pattern *pattern, dmnsn_color_map *map)
+dmnsn_new_color_map_pigment(dmnsn_pattern *pattern, dmnsn_map *map)
{
dmnsn_pigment *pigment = dmnsn_new_pigment();
diff --git a/libdimension/dimension.h b/libdimension/dimension.h
index d4ce61e..fb8efb5 100644
--- a/libdimension/dimension.h
+++ b/libdimension/dimension.h
@@ -68,6 +68,7 @@ typedef void dmnsn_free_fn(void *ptr);
#include <dimension/png.h>
#include <dimension/pattern.h>
#include <dimension/patterns.h>
+#include <dimension/map.h>
#include <dimension/texture.h>
#include <dimension/pigments.h>
#include <dimension/finishes.h>
diff --git a/libdimension/dimension/map.h b/libdimension/dimension/map.h
new file mode 100644
index 0000000..1cd0f55
--- /dev/null
+++ b/libdimension/dimension/map.h
@@ -0,0 +1,76 @@
+/*************************************************************************
+ * Copyright (C) 2009-2010 Tavian Barnes <tavianator@gmail.com> *
+ * *
+ * 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 *
+ * <http://www.gnu.org/licenses/>. *
+ *************************************************************************/
+
+
+/**
+ * @file
+ * Generic maps (backend for color_maps, pigment_maps, etc.).
+ */
+
+#ifndef DIMENSION_MAP_H
+#define DIMENSION_MAP_H
+
+/** A map. */
+typedef struct dmnsn_map {
+ dmnsn_free_fn *free_fn; /**< Destructor callback. */
+ size_t obj_size; /**< @internal The size of the mapped objects. */
+ dmnsn_array *array; /**< @internal The map entries. */
+} dmnsn_map;
+
+/**
+ * Create an empty map.
+ * @param[in] size The size of the objects to store in the map.
+ * @return A map with no entries.
+ */
+dmnsn_map *dmnsn_new_map(size_t size);
+
+/**
+ * Delete a map.
+ * @param[in,out] map The map to delete.
+ */
+void dmnsn_delete_map(dmnsn_map *map);
+
+/**
+ * Add an entry (a scalar-object pair) to a color map.
+ * @param[in,out] map The color map to add to.
+ * @param[in] n The index of the entry.
+ * @param[in] object The value of the entry.
+ */
+void dmnsn_add_map_entry(dmnsn_map *map, double n, const void *obj);
+
+/**
+ * Return the number of entries in a map.
+ * @param[in] map The map to measure.
+ * @return The size of \p map.
+ */
+size_t dmnsn_map_size(const dmnsn_map *map);
+
+/**
+ * Evaluate a map.
+ * @param[in] map The map to evaluate.
+ * @param[in] n The index to evaluate.
+ * @param[out] val The normalized distance of \p n from \p obj1.
+ * @param[out] obj1 The first object.
+ * @param[out] obj2 The second object.
+ */
+void dmnsn_evaluate_map(const dmnsn_map *map, double n,
+ double *val, void *obj1, void *obj2);
+
+#endif /* DIMENSION_MAP_H */
diff --git a/libdimension/dimension/pigments.h b/libdimension/dimension/pigments.h
index 68a9c66..687080f 100644
--- a/libdimension/dimension/pigments.h
+++ b/libdimension/dimension/pigments.h
@@ -41,37 +41,11 @@ dmnsn_pigment *dmnsn_new_solid_pigment(dmnsn_color color);
*/
dmnsn_pigment *dmnsn_new_canvas_pigment(dmnsn_canvas *canvas);
-/** Color map. */
-typedef dmnsn_array dmnsn_color_map;
-
-/**
- * Create an empty color map.
- * @return A color map with no entries.
- */
-dmnsn_color_map *dmnsn_new_color_map(void);
-
-/**
- * Delete a color map.
- * @param[in,out] map The color map to delete.
- */
-void dmnsn_delete_color_map(dmnsn_color_map *map);
-
-/**
- * Add an entry (a scalar-color pair) to a color map.
- * @param[in,out] map The color map to add to.
- * @param[in] n The index of the entry.
- * @param[in] c The value of the entry.
- */
-void dmnsn_add_color_map_entry(dmnsn_color_map *map, double n, dmnsn_color c);
-
/**
- * Evaluate a color map.
- * @param[in] map The map to evaluate.
- * @param[in] n The index to evaluate.
- * @return The value of the gradient between the the two indicies closest to
- * \p n.
+ * Construct a color map.
+ * @return An empty color map.
*/
-dmnsn_color dmnsn_color_map_value(const dmnsn_color_map *map, double n);
+dmnsn_map *dmnsn_new_color_map();
/**
* A color-mapped pigment.
@@ -80,6 +54,6 @@ dmnsn_color dmnsn_color_map_value(const dmnsn_color_map *map, double n);
* @return A pigment mapping the pattern to color values.
*/
dmnsn_pigment *dmnsn_new_color_map_pigment(dmnsn_pattern *pattern,
- dmnsn_color_map *map);
+ dmnsn_map *map);
#endif /* DIMENSION_PIGMENTS_H */
diff --git a/libdimension/map.c b/libdimension/map.c
new file mode 100644
index 0000000..3ea4b0c
--- /dev/null
+++ b/libdimension/map.c
@@ -0,0 +1,126 @@
+/*************************************************************************
+ * Copyright (C) 2010 Tavian Barnes <tavianator@gmail.com> *
+ * *
+ * 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 *
+ * <http://www.gnu.org/licenses/>. *
+ *************************************************************************/
+
+/**
+ * @file
+ * Generic maps.
+ */
+
+#include "dimension.h"
+#include <alloca.h>
+
+/** An [index, object] pair. */
+typedef struct dmnsn_map_entry {
+ double n;
+ char object[];
+} dmnsn_map_entry;
+
+dmnsn_map *
+dmnsn_new_map(size_t size)
+{
+ dmnsn_map *map = dmnsn_malloc(sizeof(dmnsn_map));
+ map->free_fn = NULL;
+ map->obj_size = size;
+ map->array = dmnsn_new_array(sizeof(dmnsn_map_entry) + size);
+ return map;
+}
+
+void
+dmnsn_delete_map(dmnsn_map *map)
+{
+ if (map) {
+ if (map->free_fn) {
+ for (size_t i = 0; i < dmnsn_array_size(map->array); ++i) {
+ dmnsn_map_entry *entry = dmnsn_array_at(map->array, i);
+ (*map->free_fn)(entry->object);
+ }
+ }
+
+ dmnsn_delete_array(map->array);
+ dmnsn_free(map);
+ }
+}
+
+void
+dmnsn_add_map_entry(dmnsn_map *map, double n, const void *obj)
+{
+ dmnsn_map_entry *entry = alloca(sizeof(dmnsn_map_entry) + map->obj_size);
+ entry->n = n;
+ memcpy(entry->object, obj, map->obj_size);
+
+ /* Sorted insertion */
+ size_t i;
+ for (i = dmnsn_array_size(map->array); i-- > 0;) {
+ dmnsn_map_entry *other = dmnsn_array_at(map->array, i);
+ if (other->n <= n)
+ break;
+ }
+
+ dmnsn_array_insert(map->array, i + 1, entry);
+}
+
+size_t
+dmnsn_map_size(const dmnsn_map *map)
+{
+ return dmnsn_array_size(map->array);
+}
+
+void
+dmnsn_evaluate_map(const dmnsn_map *map, double n,
+ double *val, void *obj1, void *obj2)
+{
+ dmnsn_assert(dmnsn_array_size(map->array) > 0,
+ "Attempt to evaluate empty map.");
+
+ const dmnsn_map_entry *entry = dmnsn_array_first(map->array);
+
+ double n1, n2 = 0.0;
+ const void *o1, *o2 = entry->object;
+
+ if (n < n2) {
+ *val = 0.0;
+ memcpy(obj1, o2, map->obj_size);
+ memcpy(obj2, o2, map->obj_size);
+ return;
+ }
+
+ const dmnsn_map_entry *last = dmnsn_array_last(map->array);
+ ptrdiff_t skip = sizeof(dmnsn_map_entry) + map->obj_size;
+ for (; entry <= last;
+ entry = (const dmnsn_map_entry *)((const char *)entry + skip))
+ {
+ n1 = n2;
+ o1 = o2;
+
+ n2 = entry->n;
+ o2 = entry->object;
+
+ if (n < n2) {
+ *val = (n - n1)/(n2 - n1);
+ memcpy(obj1, o1, map->obj_size);
+ memcpy(obj2, o2, map->obj_size);
+ return;
+ }
+ }
+
+ *val = 1.0;
+ memcpy(obj1, o2, map->obj_size);
+ memcpy(obj2, o2, map->obj_size);
+}