Skip to content

Instantly share code, notes, and snippets.

@roelj
Created September 6, 2014 15:20
Show Gist options
  • Save roelj/5b9cdbaf4db159655871 to your computer and use it in GitHub Desktop.
Save roelj/5b9cdbaf4db159655871 to your computer and use it in GitHub Desktop.
Point reduction filter for InklingReader
#include "point-reduction.h"
#include "../datatypes/coordinate.h"
#include "../datatypes/element.h"
static int
opt_in_between (dt_coordinate* first, dt_coordinate* second, dt_coordinate* third)
{
int crossproduct =
(third->y - first->y) * (second->x - first->x) -
(third->x - first->x) * (second->y - first->y);
if (crossproduct < 100 && crossproduct > -100) return 1;
int dotproduct =
(third->x - first->x) * (second->x - first->x) +
(third->y - first->y) * (second->y - first->y);
if (dotproduct < 0) return 1;
int squaredlength =
(second->x - first->x) * (second->x - first->x) +
(second->y - first->y) * (second->y - first->y);
if (dotproduct > squaredlength) return 1;
return 0;
}
int
opt_point_reduction_apply (GSList* data)
{
GSList* head = data;
dt_coordinate* first = NULL;
dt_coordinate* second = NULL;
dt_coordinate* third = NULL;
while (data != NULL)
{
dt_element* e = (dt_element *)data->data;
if (e->type == TYPE_COORDINATE)
{
if (third == NULL)
{
if (first == NULL) first = (dt_coordinate *)data->data;
else if (second == NULL) second = (dt_coordinate *)data->data;
else if (third == NULL) third = (dt_coordinate *)data->data;
}
else
{
if (opt_in_between (first, second, third))
head = g_slist_remove (head, second);
first = second;
second = third;
third = NULL;
}
}
/* Reset the optimization when it's the beginning or end of a stroke. */
if (e->type == TYPE_STROKE)
{
first = NULL;
second = NULL;
third = NULL;
}
data = data->next;
}
return 1;
}
@xuv
Copy link

xuv commented Sep 6, 2014

Sorry, can't really say what's best here. I just can "forward" a discussion that followed @n8willis's lightning talk at LGM about the Inklingreader. About point reduction and drawing optimization, Chris Lilley (if I remember well) suggested to look for works or algos done by the mapping community. They have really thought about optimization and efficiency with these kind of problems. So instead of trying to reinvent the wheel, maybe there is some research to do there. But sorry, I can't be much more help here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment