From c29e4ecb7de4cb10f48b2526bc1abae847c718e2 Mon Sep 17 00:00:00 2001 From: pacien Date: Thu, 28 Dec 2017 01:19:45 +0100 Subject: Add new geometry common types and functions Signed-off-by: pacien --- include/common/geom.h | 93 +++++++++++++++++++++++++++++++++++++++++++++++---- include/common/time.h | 4 ++- 2 files changed, 89 insertions(+), 8 deletions(-) (limited to 'include') diff --git a/include/common/geom.h b/include/common/geom.h index b3564a5..334e95c 100644 --- a/include/common/geom.h +++ b/include/common/geom.h @@ -14,6 +14,12 @@ */ typedef int32_t IntVector; +/** + * Type: RealVector + * An abstract 1-D real vector. + */ +typedef double RealVector; + /** * Struct: CartesianVector * An abstract 2-D vector in cartesian coordinates. @@ -26,6 +32,19 @@ typedef struct { IntVector x, y; } CartesianVector; +/** + * Struct: BarycentricVector + * An abstract barycentric coordinate tuple relative to a triangle. + * The third barycentric coordinate is deduced from the first two ones. + * + * Fields: + * a - the first barycentric coordinate + * b - the second barycentric coordinate + */ +typedef struct { + RealVector a, b; +} BarycentricVector; + /** * Struct: CartesianMapping * A tuple of cartesian vectors representing a mapping. @@ -38,6 +57,16 @@ typedef struct { CartesianVector origin, target; } CartesianMapping; +/** + * Struct: Triangle + * Represents a simple triangle with three vertices. + * + * Fields: + * v[] - array of vertices + */ +typedef struct { + CartesianVector v[3]; +} Triangle; /** * Function: m @@ -65,6 +94,19 @@ CartesianMapping m(int x, int y); */ CartesianVector v(int x, int y); +/** + * Function: b + * Shorthand for a barycentric vector. + * + * Parameters: + * a - the a-coordinate + * b - the b-coordinate + * + * Returns: + * A barycentric vector + */ +BarycentricVector b(double a, double b); + /** * Function: mappings_equals * Compares two cartesian mappings. @@ -92,17 +134,54 @@ bool mappings_equals(CartesianMapping m1, CartesianMapping m2); bool vector_equals(CartesianVector v1, CartesianVector v2); /** - * Function: triangle_area - * Computes the area of a triangle. + * Function: barycentric_vector_equals + * Compares two barycentric vectors. + * + * Parameters: + * v1 - the first vector + * v2 - the second vector + * + * Returns: + * T(v1 is equal to v2) + */ +bool barycentric_vector_equals(BarycentricVector b1, BarycentricVector b2); + +/** + * Function: square_area + * Computes the area of a square spawned by three positively oriented vertices. + * + * Parameters: + * vi - vertices + * + * Returns: + * The area of the square + */ +IntVector square_area(CartesianVector v1, CartesianVector v2, CartesianVector v3); + +/** + * Function: cartesian_to_barycentric + * Computes and returns the barycentric coordinates of a given point in the given reference triangle. + * + * Parameters: + * t - reference triangle + * p - the vector to convert + * + * Returns: + * The barycentric coordinates vector + */ +BarycentricVector cartesian_to_barycentric(Triangle t, CartesianVector p); + +/** + * Function: barycentric_to_cartesian + * Computes and returns the cartesian coordinates of a given point in the given reference triangle. * * Parameters: - * v1 - first vertex - * v2 - second vertex - * v3 - third vertex + * t - reference triangle + * p - the vector to convert * * Returns: - * The area of the triangle spawned by the three supplied vertices + * The cartesian coordinate vector */ -IntVector triangle_area(CartesianVector v1, CartesianVector v2, CartesianVector v3); +CartesianVector barycentric_to_cartesian(Triangle t, BarycentricVector p); #endif diff --git a/include/common/time.h b/include/common/time.h index 54a7bb2..e207ad7 100644 --- a/include/common/time.h +++ b/include/common/time.h @@ -5,6 +5,8 @@ * File: time.h */ +#include "geom.h" + /** * Constants: Time vectors * @@ -18,6 +20,6 @@ * Type: TimeVector * An abstract time vector. */ -typedef float TimeVector; +typedef RealVector TimeVector; #endif -- cgit v1.2.3 From c970da3f5830fae5b4d98dcdcc8d34d678ec0434 Mon Sep 17 00:00:00 2001 From: pacien Date: Thu, 28 Dec 2017 01:22:03 +0100 Subject: Refactor canvas Signed-off-by: pacien --- include/painter/canvas.h | 76 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 include/painter/canvas.h (limited to 'include') diff --git a/include/painter/canvas.h b/include/painter/canvas.h new file mode 100644 index 0000000..e354938 --- /dev/null +++ b/include/painter/canvas.h @@ -0,0 +1,76 @@ +#ifndef UPEM_MORPHING_CANVAS +#define UPEM_MORPHING_CANVAS + +/** + * File: canvas.h + * "Everyday is a good day when you paint" – Bob Ross + */ + +#include +#include "common/geom.h" +#include "painter/color.h" + +/** + * Type: Canvas + * Represents a fixed size RGBa pixel matrix. + */ +typedef struct { + MLV_Image *mlv; +} Canvas; + +/** + * Function: canvas_create + * Initialises a canvas of the given size + * + * Parameters: + * width - the width in pixels + * height - the height in pixels + */ +Canvas *canvas_create(IntVector width, IntVector height); + +/** + * Function: canvas_destroy + * Frees all memory allocated to a canvas. + * + * Parameters: + * *c - the canvas to destroy + */ +void canvas_destroy(Canvas *c); + +/** + * Function: canvas_set_pixel + * Sets the pixel colour at the given coordinates. + * + * Parameters: + * *c - the canvas to alter + * pos - the coordinate of the pixel to set + * color - the new colour to set + */ +void canvas_set_pixel(Canvas *c, CartesianVector pos, Color color); + +/** + * Function: canvas_get_pixel + * Returns the colour of the pixel at the given position. + * + * Parameters: + * *c - the base canvas + * pos - the coordinate of the pixel to get + * + * Returns: + * The colour of the requested pixel + */ +Color canvas_get_pixel(Canvas *c, CartesianVector pos); + +/** + * Function: canvas_get_dim + * Returns the size (in pixels) of the given canvas. + * + * Parameters: + * *c - the canvas + * + * Returns: + * The size of the canvas + */ +CartesianVector canvas_get_dim(Canvas *c); + +#endif -- cgit v1.2.3 From 190449ee18bec69b2e385dccd9bd42ddc83dd418 Mon Sep 17 00:00:00 2001 From: pacien Date: Thu, 28 Dec 2017 01:22:41 +0100 Subject: Refactor and test color Signed-off-by: pacien --- include/painter/color.h | 61 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 include/painter/color.h (limited to 'include') diff --git a/include/painter/color.h b/include/painter/color.h new file mode 100644 index 0000000..2aeee3e --- /dev/null +++ b/include/painter/color.h @@ -0,0 +1,61 @@ +#ifndef UPEM_MORPHING_COLOR +#define UPEM_MORPHING_COLOR + +/** + * File: color.h + * + * See also: + * A rainbow + */ + +#include +#include +#include "common/time.h" + +/** + * Type: ColorComponent + * Represents a single colour component of 32-bits RGBa tuple. + */ +typedef uint8_t ColorComponent; + +/** + * Type: ColorPixel + * Represents a single RGBa coloured pixel. + * Compatible with the libMLV representation. + */ +typedef union { + struct { + ColorComponent a, b, g, r; + } rgba; + + MLV_Color mlv; +} Color; + +/** + * Function: color_equals + * Compares the supplied colors. + * + * Parameters: + * c1 - the first color + * c2 - the second color + * + * Returns: + * T(c1 is the same color as c2) + */ +bool color_equals(Color c1, Color c2); + +/** + * Function: color_blend + * Blends two colors. + * + * Parameters: + * origin - the first color + * target - the second color + * distance - the distance from the first color + * + * Returns: + * The blended color + */ +Color color_blend(Color origin, Color target, TimeVector distance); + +#endif -- cgit v1.2.3 From 330fd85db8c89c178621d978929d911bbe93fec7 Mon Sep 17 00:00:00 2001 From: pacien Date: Thu, 28 Dec 2017 01:23:08 +0100 Subject: Refactor canvas blender into rasterizer Signed-off-by: pacien --- include/blender/blender.h | 26 -------------------------- include/painter/rasterizer.h | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 26 deletions(-) delete mode 100644 include/blender/blender.h create mode 100644 include/painter/rasterizer.h (limited to 'include') diff --git a/include/blender/blender.h b/include/blender/blender.h deleted file mode 100644 index 26ff802..0000000 --- a/include/blender/blender.h +++ /dev/null @@ -1,26 +0,0 @@ -#ifndef UPEM_MORPHING_BLENDER -#define UPEM_MORPHING_BLENDER - -/** - * File: blender.h - * Will it blend? That is the question. - */ - -#include "common/time.h" -#include "blender/canvas.h" -#include "morpher/morphing.h" - -/** - * Function: blender_blend_canvas - * Blends two canvas by applying the given morphing at the requested time frame. - * - * Parameters: - * *canvas - pointer to the canvas to paint - * *source - source image - * *target - target image - * *morphing - morphing transform to apply - * frame - the interpolation distance from the origin canvas [0;1] - */ -void blender_blend_canvas(Canvas *canvas, Canvas *source, Canvas *target, Morphing *morphing, TimeVector frame); - -#endif diff --git a/include/painter/rasterizer.h b/include/painter/rasterizer.h new file mode 100644 index 0000000..204d616 --- /dev/null +++ b/include/painter/rasterizer.h @@ -0,0 +1,41 @@ +#ifndef UPEM_MORPHING_RASTERIZER +#define UPEM_MORPHING_RASTERIZER + +/** + * File: rasterizer.h + */ + +#include "painter/canvas.h" +#include "morpher/morphing.h" + +/** + * Struct: RasterizationContext + */ +typedef struct { + Canvas *result, *source, *target; + TimeVector frame; +} RasterizationContext; + +/** + * Struct: TriangleContext + */ +typedef struct { + Triangle current, source, target; +} TriangleContext; + +/** + * Function: rasterize + * Rasterises a morphing from a source and a target image at the given time frame. + * + * Parameters: + * *source - source image canvas + * *target - target image canvas + * *m - reference morphing + * frame - time frame + * + * Returns: + * The drawn canvas, dynamically allocated + */ +Canvas *rasterize(Canvas *source, Canvas *target, Morphing *m, TimeVector frame); + +#endif -- cgit v1.2.3 From fa154af5a72964931bae432dbb96abf45465e6cb Mon Sep 17 00:00:00 2001 From: pacien Date: Thu, 28 Dec 2017 01:37:38 +0100 Subject: Add canvas from image init. func. Signed-off-by: pacien --- include/painter/canvas.h | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'include') diff --git a/include/painter/canvas.h b/include/painter/canvas.h index e354938..ba6d4da 100644 --- a/include/painter/canvas.h +++ b/include/painter/canvas.h @@ -25,9 +25,24 @@ typedef struct { * Parameters: * width - the width in pixels * height - the height in pixels + * + * Returns: + * The initialised canvas */ Canvas *canvas_create(IntVector width, IntVector height); +/** + * Function: canvas_create_from_image + * Initialises a canvas with an image loaded from the given path. + * + * Parameters: + * *fpath - path to the base image file + * + * Returns: + * The initialised canvas + */ +Canvas *canvas_create_from_image(const char *fpath); + /** * Function: canvas_destroy * Frees all memory allocated to a canvas. -- cgit v1.2.3 From 0b3608c819b809ae599844bb9188c59564620868 Mon Sep 17 00:00:00 2001 From: pacien Date: Thu, 28 Dec 2017 01:41:24 +0100 Subject: Remove previous impl. files Signed-off-by: pacien --- include/blender/canvas.h | 79 ------------------------------------------------ include/blender/color.h | 46 ---------------------------- 2 files changed, 125 deletions(-) delete mode 100644 include/blender/canvas.h delete mode 100644 include/blender/color.h (limited to 'include') diff --git a/include/blender/canvas.h b/include/blender/canvas.h deleted file mode 100644 index aae217d..0000000 --- a/include/blender/canvas.h +++ /dev/null @@ -1,79 +0,0 @@ -#ifndef UPEM_MORPHING_CANVAS -#define UPEM_MORPHING_CANVAS - -/** - * File: canvas.h - * - * See also: - * Freedom, according to Bob Ross - */ - -#include -#include "common/geom.h" -#include "color.h" - -/** - * Type: Canvas - * Represents a fixed size RGBa pixel matrix. - */ -typedef struct { - MLV_Image *mlv; -} Canvas; - -/** - * Function: canvas_init - * Initialises a canvas of the given size - * - * Parameters: - * *canvas - the canvas to initialise - * width - the width in pixels - * height - the height in pixels - */ -void canvas_init(Canvas *canvas, IntVector width, IntVector height); - -/** - * Function: canvas_free - * Frees all memory allocated to a canvas. - * - * Parameters: - * *canvas - the canvas to destroy - */ -void canvas_free(Canvas *canvas); - -/** - * Function: canvas_set_pixel - * Sets the pixel colour at the given coordinates. - * - * Parameters: - * *canvas - the canvas to alter - * position - the cartesian coordinates of the pixel to set - * color - the new colour to set - */ -void canvas_set_pixel(Canvas *canvas, CartesianVector position, Color color); - -/** - * Function: canvas_get_pixel - * Returns the colour of the pixel at the given position. - * - * Parameters: - * *canvas - the base canvas - * position - the position in cartesian coordinates - * - * Returns: - * The colour of the requested pixel - */ -Color canvas_get_pixel(Canvas *canvas, CartesianVector position); - -/** - * Function: canvas_get_dim - * Returns the size (in pixels) of the given canvas. - * - * Parameters: - * *canvas - the canvas - * - * Returns: - * The size of the canvas - */ -CartesianVector canvas_get_dim(Canvas *canvas); - -#endif diff --git a/include/blender/color.h b/include/blender/color.h deleted file mode 100644 index 1fef2cf..0000000 --- a/include/blender/color.h +++ /dev/null @@ -1,46 +0,0 @@ -#ifndef UPEM_MORPHING_COLOR -#define UPEM_MORPHING_COLOR - -/** - * File: color.h - * - * See also: - * A rainbow - */ - -#include -#include - -/** - * Type: ColorComponent - * Represents a single colour component of 32-bits RGBa tuple. - */ -typedef uint8_t ColorComponent; - -/** - * Type: ColorPixel - * Represents a single RGBa coloured pixel. - * Compatible with the libMLV representation. - */ -typedef union { - struct { - ColorComponent r, g, b, a; - } rgba; - - MLV_Color mlv; -} Color; - -/** - * Function: color_equals - * Compares the supplied colors. - * - * Parameters: - * c1 - the first color - * c2 - the second color - * - * Returns: - * T(c1 is the same color as c2) - */ -bool color_equals(Color c1, Color c2); - -#endif -- cgit v1.2.3 From ca8d91787e297b69d3d34a136b60df19b207a4f4 Mon Sep 17 00:00:00 2001 From: pacien Date: Thu, 28 Dec 2017 01:52:09 +0100 Subject: Adding inspiration Signed-off-by: pacien --- include/common/time.h | 3 +++ include/painter/canvas.h | 3 ++- 2 files changed, 5 insertions(+), 1 deletion(-) (limited to 'include') diff --git a/include/common/time.h b/include/common/time.h index e207ad7..54a81af 100644 --- a/include/common/time.h +++ b/include/common/time.h @@ -3,6 +3,9 @@ /** * File: time.h + * > Who can say where the road goes... + * > Where the day flows, only time... + * -- Enya */ #include "geom.h" diff --git a/include/painter/canvas.h b/include/painter/canvas.h index ba6d4da..928121a 100644 --- a/include/painter/canvas.h +++ b/include/painter/canvas.h @@ -3,7 +3,8 @@ /** * File: canvas.h - * "Everyday is a good day when you paint" – Bob Ross + * > Everyday is a good day when you paint. + * -- Bob Ross */ #include -- cgit v1.2.3