1
0
Fork 0

cogl: Adds _cogl_util_point_in_polygon API

This adds a utility function that can determine if a given point
intersects an arbitrary polygon, by counting how many edges a
"semi-infinite" horizontal ray crosses from that point. The plan is to
use this for a software based read-pixel fast path that avoids using the
GPU to rasterize journaled primitives and can instead intersect a point
being read with quads in the journal to determine the correct color.
This commit is contained in:
Robert Bragg 2011-01-13 14:29:46 +00:00
parent 60fd6ab708
commit 1f0826543e
3 changed files with 111 additions and 0 deletions

View file

@ -269,6 +269,8 @@ cogl_sources_c = \
$(srcdir)/cogl-callback-list.h \
$(srcdir)/cogl-callback-list.c \
$(srcdir)/cogl-gtype-private.h \
$(srcdir)/cogl-point-in-poly-private.h \
$(srcdir)/cogl-point-in-poly.c \
$(NULL)
if SUPPORT_XLIB

View file

@ -0,0 +1,40 @@
/*
* Cogl
*
* An object oriented GL/GLES Abstraction/Utility Layer
*
* Copyright (C) 2011 Intel Corporation.
*
* This 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 2 of the License, or (at your option) any later version.
*
* This 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 library. If not, see
* <http://www.gnu.org/licenses/>.
*/
#ifndef __COGL_POINT_INT_POLYGON_PRIVATE_H
#define __COGL_POINT_INT_POLYGON_PRIVATE_H
#include <glib.h>
G_BEGIN_DECLS
int
_cogl_util_point_in_poly (float point_x,
float point_y,
void *vertices,
size_t stride,
int n_vertices);
G_END_DECLS
#endif /* __COGL_POINT_INT_POLYGON_PRIVATE_H */

69
cogl/cogl-point-in-poly.c Normal file
View file

@ -0,0 +1,69 @@
/*
* Point Inclusion in Polygon Test
*
* Copyright (c) 1970-2003, Wm. Randolph Franklin
* Copyright (C) 2011 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimers.
* 2. Redistributions in binary form must reproduce the above
* copyright notice in the documentation and/or other materials
* provided with the distribution.
* 3. The name of W. Randolph Franklin may not be used to endorse or
* promote products derived from this Software without specific
* prior written permission.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* Note:
* The algorithm for this point_in_poly() function was learnt from:
* http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <glib.h>
int
_cogl_util_point_in_poly (float point_x,
float point_y,
void *vertices,
size_t stride,
int n_vertices)
{
int i, j, c = 0;
for (i = 0, j = n_vertices - 1; i < n_vertices; j = i++)
{
float vert_xi = *(float *)((guint8 *)vertices + i * stride);
float vert_xj = *(float *)((guint8 *)vertices + j * stride);
float vert_yi = *(float *)((guint8 *)vertices + i * stride +
sizeof (float));
float vert_yj = *(float *)((guint8 *)vertices + j * stride +
sizeof (float));
if (((vert_yi > point_y) != (vert_yj > point_y)) &&
(point_x < (vert_xj - vert_xi) * (point_y - vert_yi) /
(vert_yj - vert_yi) + vert_xi) )
c = !c;
}
return c;
}