summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@gmail.com>2011-07-28 21:34:45 -0600
committerTavian Barnes <tavianator@gmail.com>2011-07-28 21:34:45 -0600
commit250c28ccdef5f238677b34f21ec7fab06588a127 (patch)
tree7aa115cf5d7c4119bedafc31703cd4a0a11d9d64
parent275c2542c4e414ffe01558cfca2445dcad6bbd29 (diff)
downloaddimension-250c28ccdef5f238677b34f21ec7fab06588a127.tar.xz
Make the checker pattern a singleton.
-rw-r--r--libdimension-python/dimension.pxd2
-rw-r--r--libdimension-python/dimension.pyx8
-rw-r--r--libdimension/checker.c11
-rw-r--r--libdimension/dimension/pattern.h12
-rw-r--r--libdimension/dimension/refcount.h9
-rw-r--r--libdimension/pattern.c9
-rw-r--r--libdimension/pigment_map.c1
7 files changed, 17 insertions, 35 deletions
diff --git a/libdimension-python/dimension.pxd b/libdimension-python/dimension.pxd
index da4ac12..cda2c59 100644
--- a/libdimension-python/dimension.pxd
+++ b/libdimension-python/dimension.pxd
@@ -198,7 +198,7 @@ cdef extern from "../libdimension/dimension.h":
############
ctypedef struct dmnsn_pattern:
- dmnsn_matrix trans
+ pass
void dmnsn_delete_pattern(dmnsn_pattern *pattern)
diff --git a/libdimension-python/dimension.pyx b/libdimension-python/dimension.pyx
index 2cde396..3fd64d1 100644
--- a/libdimension-python/dimension.pyx
+++ b/libdimension-python/dimension.pyx
@@ -543,14 +543,6 @@ cdef class Pattern:
def __dealloc__(self):
dmnsn_delete_pattern(self._pattern)
- def transform(self, Matrix trans not None):
- """Transform a pattern."""
- if self._pattern == NULL:
- raise TypeError("attempt to transform base Pattern")
-
- self._pattern.trans = dmnsn_matrix_mul(trans._m, self._pattern.trans)
- return self
-
cdef class Checker(Pattern):
"""A checkerboard pattern."""
def __init__(self):
diff --git a/libdimension/checker.c b/libdimension/checker.c
index 84b13bd..aa663ed 100644
--- a/libdimension/checker.c
+++ b/libdimension/checker.c
@@ -51,10 +51,15 @@ dmnsn_checker_pattern_fn(const dmnsn_pattern *checker, dmnsn_vector v)
return (n%2 == 0) ? 0.0 : 1.0;
}
+/** The singleton instance. */
+static dmnsn_pattern dmnsn_checker_instance = {
+ .pattern_fn = dmnsn_checker_pattern_fn,
+ .refcount = 1,
+};
+
dmnsn_pattern *
dmnsn_new_checker_pattern(void)
{
- dmnsn_pattern *checker = dmnsn_new_pattern();
- checker->pattern_fn = dmnsn_checker_pattern_fn;
- return checker;
+ DMNSN_INCREF(&dmnsn_checker_instance);
+ return &dmnsn_checker_instance;
}
diff --git a/libdimension/dimension/pattern.h b/libdimension/dimension/pattern.h
index de37286..596ed35 100644
--- a/libdimension/dimension/pattern.h
+++ b/libdimension/dimension/pattern.h
@@ -40,9 +40,6 @@ struct dmnsn_pattern {
dmnsn_pattern_fn *pattern_fn; /**< The pattern callback. */
dmnsn_free_fn *free_fn; /**< The destructor callback. */
- dmnsn_matrix trans; /**< The transformation matrix of the pattern. */
- dmnsn_matrix trans_inv; /**< The inverse of the transformation matrix. */
-
void *ptr; /**< Generic pointer. */
dmnsn_refcount refcount; /**< @internal Reference count. */
@@ -61,15 +58,6 @@ dmnsn_pattern *dmnsn_new_pattern(void);
void dmnsn_delete_pattern(dmnsn_pattern *pattern);
/**
- * Initialize a pattern. This precomputes some values that are used during
- * ray-tracing; the pattern will not work until it has been initialized, but
- * should not be modified after it has been initialized. Patterns are generally
- * initialized for you.
- * @param[in,out] pattern The pattern to initialize.
- */
-void dmnsn_initialize_pattern(dmnsn_pattern *pattern);
-
-/**
* Invoke the pattern callback with the right transformation.
* @param[in] pattern The pattern to evaluate.
* @param[in] v The point to get the pattern value for.
diff --git a/libdimension/dimension/refcount.h b/libdimension/dimension/refcount.h
index 5ffde4c..a0f5b6e 100644
--- a/libdimension/dimension/refcount.h
+++ b/libdimension/dimension/refcount.h
@@ -32,7 +32,14 @@ typedef unsigned int dmnsn_refcount;
* Increment a reference count.
* @param[in,out] object The reference-counted object to acquire.
*/
-#define DMNSN_INCREF(object) ((void)((object) && ++(object)->refcount))
+#define DMNSN_INCREF(object) \
+ do { \
+ /* Suppress "address will always evaluate to true" warning */ \
+ void *testptr = (object); \
+ if (testptr) { \
+ ++(object)->refcount; \
+ } \
+ } while (0)
/**
* @internal
diff --git a/libdimension/pattern.c b/libdimension/pattern.c
index 368755a..1db2244 100644
--- a/libdimension/pattern.c
+++ b/libdimension/pattern.c
@@ -30,7 +30,6 @@ dmnsn_pattern *
dmnsn_new_pattern(void)
{
dmnsn_pattern *pattern = dmnsn_malloc(sizeof(dmnsn_pattern));
- pattern->trans = dmnsn_identity_matrix();
pattern->free_fn = NULL;
pattern->refcount = 1;
return pattern;
@@ -48,17 +47,9 @@ dmnsn_delete_pattern(dmnsn_pattern *pattern)
}
}
-/* Precompute the transformation matrix inverse */
-void
-dmnsn_initialize_pattern(dmnsn_pattern *pattern)
-{
- pattern->trans_inv = dmnsn_matrix_inverse(pattern->trans);
-}
-
/* Invoke the pattern callback with the right transformation */
double
dmnsn_pattern_value(const dmnsn_pattern *pattern, dmnsn_vector v)
{
- v = dmnsn_transform_vector(pattern->trans_inv, v);
return pattern->pattern_fn(pattern, v);
}
diff --git a/libdimension/pigment_map.c b/libdimension/pigment_map.c
index 4ffb129..e87584e 100644
--- a/libdimension/pigment_map.c
+++ b/libdimension/pigment_map.c
@@ -102,7 +102,6 @@ static void
dmnsn_pigment_map_initialize_fn(dmnsn_pigment *pigment)
{
dmnsn_pigment_map_payload *payload = pigment->ptr;
- dmnsn_initialize_pattern(payload->pattern);
dmnsn_map_apply(payload->map, dmnsn_initialize_mapped_pigment);
}