YAC 3.21.0
Yet Another Coupler
Loading...
Searching...
No Matches
clipping.c File Reference
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "geometry.h"
#include "clipping.h"
#include "area.h"
#include "ensure_array_size.h"
#include "utils_core.h"
Include dependency graph for clipping.c:

Go to the source code of this file.

Data Structures

struct  point_list_element
 
struct  point_list
 

Functions

static void init_point_list (struct point_list *list)
 
static void reset_point_list (struct point_list *list)
 
static size_t generate_point_list (struct point_list *list, struct yac_grid_cell cell, int cell_ordering, struct yac_circle *circle_buffer)
 
static struct point_list_element * get_free_point_list_element (struct point_list *list)
 
static size_t remove_points (struct point_list *list)
 
static size_t remove_zero_length_edges (struct point_list *list)
 returns number of edges/corners
 
static void free_point_list (struct point_list *list)
 
static int get_cell_points_ordering (struct yac_grid_cell cell)
 
static void generate_cell (struct point_list *list, struct yac_grid_cell *cell)
 
static enum yac_cell_type get_cell_type (struct yac_grid_cell target_cell)
 
static struct yac_grid_cell * get_overlap_cell_buffer (size_t N)
 
void yac_compute_overlap_buf_free ()
 
static double get_edge_direction (double *ref_corner, double *corner_a, double *corner_b)
 
void yac_compute_overlap_info (size_t N, struct yac_grid_cell *source_cell, struct yac_grid_cell target_cell, double *overlap_areas, double(*overlap_barycenters)[3])
 calculates partial areas for all overlapping parts of the source cells with arbitrary target cells, this is required for conservative remapping. In addition, the barycenter of each overlap is calculated.
 
static double dotproduct (double a[], double b[])
 
void yac_compute_overlap_areas (size_t N, struct yac_grid_cell *source_cell, struct yac_grid_cell target_cell, double *partial_areas)
 calculates partial areas for all overlapping parts of the source cells with arbitrary target cells, this is required for conservative remapping.
 
int yac_circle_compare (void const *a, void const *b)
 
static void compute_norm_vector (double const a[], double const b[], double norm[])
 
void yac_circle_generate (double const *a, double const *b, enum yac_edge_type type, int edge_ordering, struct yac_circle *circle)
 
static struct yac_circle generate_lat_circle (double z, double r, int north_is_out)
 
int yac_circle_point_is_inside (double const point[3], struct yac_circle const *circle)
 
int yac_circle_compare_distances (double const a[3], double const b[3], struct yac_circle const *circle)
 
int yac_circle_contains_north_pole (struct yac_circle const *circle)
 
static void circle_clipping (struct point_list *cell, size_t num_cell_edges, struct yac_circle **clipping_circles, size_t num_clipping_circles)
 
static void point_list_clipping (struct point_list *source_list, struct point_list target_list, size_t nct)
 
static void copy_point_list (struct point_list in, struct point_list *out)
 
void yac_cell_clipping (size_t N, struct yac_grid_cell *source_cell, struct yac_grid_cell target_cell, struct yac_grid_cell *overlap_buffer)
 cell clipping to get the cells describing the intersections
 
static void yac_lon_lat_cell_lat_clipping (struct yac_grid_cell *cell, double z_upper_bound, double z_lower_bound, struct yac_grid_cell *overlap_buffer)
 
static double get_closest_pole (struct point_list *cell_list)
 
void yac_cell_lat_clipping (size_t N, struct yac_grid_cell *cells, double lat_bounds[2], struct yac_grid_cell *overlap_buffer)
 cell clipping to get the cells describing the intersections
 
void yac_correct_weights (size_t nSourceCells, double *weight)
 correct interpolation weights
 
static int is_empty_gc_cell (struct point_list *list, size_t num_edges)
 
static enum yac_edge_type circle2edge_type (enum yac_circle_type type)
 

Variables

static double const tol = 1.0e-12
 
static struct yac_grid_cell * overlap_cell_buffer = NULL
 
static size_t overlap_cell_buffer_size = 0
 

Function Documentation

◆ circle2edge_type()

static enum yac_edge_type circle2edge_type ( enum yac_circle_type type)
inlinestatic

Definition at line 1848 of file clipping.c.

Here is the caller graph for this function:

◆ circle_clipping()

static void circle_clipping ( struct point_list * cell,
size_t num_cell_edges,
struct yac_circle ** clipping_circles,
size_t num_clipping_circles )
static

cell clipping using Sutherland-Hodgman algorithm;

Definition at line 617 of file clipping.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ compute_norm_vector()

static void compute_norm_vector ( double const a[],
double const b[],
double norm[] )
static

Definition at line 395 of file clipping.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ copy_point_list()

static void copy_point_list ( struct point_list in,
struct point_list * out )
static

Definition at line 1100 of file clipping.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ dotproduct()

static double dotproduct ( double a[],
double b[] )
inlinestatic

Definition at line 264 of file clipping.c.

Here is the caller graph for this function:

◆ free_point_list()

static void free_point_list ( struct point_list * list)
static

Definition at line 1777 of file clipping.c.

Here is the caller graph for this function:

◆ generate_cell()

static void generate_cell ( struct point_list * list,
struct yac_grid_cell * cell )
static

Definition at line 1857 of file clipping.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ generate_lat_circle()

static struct yac_circle generate_lat_circle ( double z,
double r,
int north_is_out )
inlinestatic

generates a circle of latitude

Parameters
[in]zz coordinate of the circle of latitude
[in]rdistance of the circle of latitude from the polar axis (has to be consistent with z)
[in]north_is_outif true, the north pole is outside of the circle

Definition at line 481 of file clipping.c.

Here is the caller graph for this function:

◆ generate_point_list()

static size_t generate_point_list ( struct point_list * list,
struct yac_grid_cell cell,
int cell_ordering,
struct yac_circle * circle_buffer )
static

Definition at line 1727 of file clipping.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_cell_points_ordering()

static int get_cell_points_ordering ( struct yac_grid_cell cell)
static

Definition at line 1538 of file clipping.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_cell_type()

static enum yac_cell_type get_cell_type ( struct yac_grid_cell target_cell)
static

Definition at line 282 of file clipping.c.

Here is the caller graph for this function:

◆ get_closest_pole()

static double get_closest_pole ( struct point_list * cell_list)
static

Definition at line 1365 of file clipping.c.

Here is the caller graph for this function:

◆ get_edge_direction()

static double get_edge_direction ( double * ref_corner,
double * corner_a,
double * corner_b )
static

Definition at line 99 of file clipping.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_free_point_list_element()

static struct point_list_element * get_free_point_list_element ( struct point_list * list)
static

Definition at line 1757 of file clipping.c.

Here is the caller graph for this function:

◆ get_overlap_cell_buffer()

static struct yac_grid_cell * get_overlap_cell_buffer ( size_t N)
inlinestatic

Definition at line 69 of file clipping.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ init_point_list()

static void init_point_list ( struct point_list * list)
static

Definition at line 1592 of file clipping.c.

Here is the caller graph for this function:

◆ is_empty_gc_cell()

static int is_empty_gc_cell ( struct point_list * list,
size_t num_edges )
static

Definition at line 1799 of file clipping.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ point_list_clipping()

static void point_list_clipping ( struct point_list * source_list,
struct point_list target_list,
size_t nct )
static

Definition at line 1079 of file clipping.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ remove_points()

static size_t remove_points ( struct point_list * list)
static

Definition at line 1610 of file clipping.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ remove_zero_length_edges()

static size_t remove_zero_length_edges ( struct point_list * list)
static

returns number of edges/corners

Definition at line 1666 of file clipping.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ reset_point_list()

static void reset_point_list ( struct point_list * list)
static

Definition at line 1598 of file clipping.c.

Here is the caller graph for this function:

◆ yac_cell_clipping()

void yac_cell_clipping ( size_t N,
struct yac_grid_cell * source_cell,
struct yac_grid_cell target_cell,
struct yac_grid_cell * overlap_buffer )

cell clipping to get the cells describing the intersections

The routine takes (a list of) source cells and a target cell. It sets the target cell data and does some further initialisation. Thus it needs to be called for each new target cell intersection calculation

The vertices of source and target cells can be either provided in a clockwise or anticlockwise sense.

Parameters
[in]Nnumber of source cells
[in]source_celllist of source cells
[in]target_celltarget cell
[in]overlap_bufferbuffer for the overlaps between the target and the source cells
Remarks
source cells can be either convex or concave
target cell has to be convex
two adjacent edges of a cell that are on the same circle of latitude have to point into the same direction (a cell whose boundary goes back and forth along a circle of latitude is not supported)
cells in overlap_buffer can be concave (even if source cell was convex)
overlap_buffer must contain valid grid_cells (have to be initialised using yac_init_grid_cell; initialisation have to be done only once, in consecutive calls, the cells can be reused with have to be reinitialised)

Definition at line 1125 of file clipping.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ yac_cell_lat_clipping()

void yac_cell_lat_clipping ( size_t N,
struct yac_grid_cell * cells,
double lat_bounds[2],
struct yac_grid_cell * overlap_buffer )

cell clipping to get the cells describing the intersections

The routine takes (a list of) cells and two latitude bounds.

Parameters
[in]Nnumber of cells
[in]cellslist of cells
[in]lat_boundslatitude bounds in radiant
[in]overlap_bufferbuffer for the overlaps between the cells and latitude band
Remarks
two adjacent edges of a cell that are on the same circle of latitude have to point into the same direction (a cell whose boundary goes back and forth along a circle of latitude is not supported)
cells in overlap_buffer can be concave
overlap_buffer must contain valid grid_cells (have to be initialised using yac_init_grid_cell; initialisation have to be done only once, in consecutive calls, the cells can be reused with have to be reinitialised)
this routine is currently not being used within YAC but potentially used within the CDOs

Definition at line 1386 of file clipping.c.

Here is the call graph for this function:

◆ yac_circle_compare()

int yac_circle_compare ( void const * a,
void const * b )

Compare routine for YAC circles (first by type and second parameters of the circle, if types are identical)

Parameters
[in]aYAC circle a
[in]bYAC circle b
Return values
<0if circle a orders before circle b
0if circle a and circle b are considered equal
>0if circle a orders after circle b
Remarks
lat circles always order after non-lat circles, regardless of their other parameters

Definition at line 339 of file clipping.c.

Here is the caller graph for this function:

◆ yac_circle_compare_distances()

int yac_circle_compare_distances ( double const a[3],
double const b[3],
struct yac_circle const * circle )

Compares the distances of two points to a given YAC circle

Parameters
[in]apoint a
[in]bpoint b
[in]circleYAC circle
Return values
<0if point a is closer to the circle than point b
0if point a and b have the same distance to the circle
>0if point b is closer to the circle than point a

Definition at line 542 of file clipping.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ yac_circle_contains_north_pole()

int yac_circle_contains_north_pole ( struct yac_circle const * circle)

Determines whether the provided YAC circle contains the north pole

Parameters
[in]circleYAC circle
Return values
Nonzeroif the circle contains the north pole
Zerootherwise

Definition at line 595 of file clipping.c.

Here is the caller graph for this function:

◆ yac_circle_generate()

void yac_circle_generate ( double const * a,
double const * b,
enum yac_edge_type type,
int edge_ordering,
struct yac_circle * circle )

Generate a YAC circle

Parameters
[in]apoint a
[in]bpoint b
[in]typetyp of edge connecting points a and b
[in]edge_orderingordering of the points a and b
[out]circleYAC circle

Definition at line 407 of file clipping.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ yac_circle_point_is_inside()

int yac_circle_point_is_inside ( double const point[3],
struct yac_circle const * circle )

Determines whether a given point is on the "inside"-side of a plane defined by the given YAC circle

Parameters
[in]pointpoint to be checked
[in]circleYAC circle
Return values
0if the point is not inside
1if the point is inside
2if the point is on the plane

Definition at line 492 of file clipping.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ yac_compute_overlap_areas()

void yac_compute_overlap_areas ( size_t N,
struct yac_grid_cell * source_cell,
struct yac_grid_cell target_cell,
double * partial_areas )

calculates partial areas for all overlapping parts of the source cells with arbitrary target cells, this is required for conservative remapping.

Some of the underlying concepts can be found in

See e.g. Joseph O'Rourke, Computational Geometry in C, 2nd Ed., 1998 Sec. 7.6 Intersections of Convex Polygons, page 253.

The routine takes (a list of) source cells and a target cell. It determines the clipping points of the intersection between a source and the target cells using cell_clipping internally. In a second step areas are calculated for each intersection described in the overlap cells. If a target cell is fully covered by N source cells the N partial areas should add up to the area of the target cell.

Parameters
[in]Nnumber of source cells
[in]source_celllist of source cells
[in]target_celltarget cell
[out]partial_areaslist of N partial weights, one weight for each source-target intersection

Definition at line 271 of file clipping.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ yac_compute_overlap_buf_free()

void yac_compute_overlap_buf_free ( )

Frees buffer allocated by yac_compute_overlap_info

Definition at line 88 of file clipping.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ yac_compute_overlap_info()

void yac_compute_overlap_info ( size_t N,
struct yac_grid_cell * source_cell,
struct yac_grid_cell target_cell,
double * overlap_areas,
double(*) overlap_barycenters[3] )

calculates partial areas for all overlapping parts of the source cells with arbitrary target cells, this is required for conservative remapping. In addition, the barycenter of each overlap is calculated.

Some of the underlying concepts can be found in

See e.g. Joseph O'Rourke, Computational Geometry in C, 2nd Ed., 1998 Sec. 7.6 Intersections of Convex Polygons, page 253.

The routine takes (a list of) source cells and a target cell. It determines the clipping points of the intersection between a source and the target cells using cell_clipping internally. In a second step areas are calculated for each intersection described in the overlap cells. If a target cell is fully covered by N source cells the N partial areas should add up to the area of the target cell.

Parameters
[in]Nnumber of source cells
[in]source_celllist of source cells
[in]target_celltarget cell
[out]overlap_areaslist of N partial weights, one weight for each source-target intersection
[out]overlap_barycenterscoordinates of the barycenters of the overlap cell
Remarks
for efficiency the overlap computation keeps an internal buffer, which can be freed by a call to yac_compute_overlap_buf_free

Definition at line 119 of file clipping.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ yac_correct_weights()

void yac_correct_weights ( size_t N,
double * weight )

correct interpolation weights

Returns weights with a sum close to 1.0

Parameters
[in]Nnumber of source cells
[out]weightlist of N partial weights
Remarks
if the weights add up to (nearly) zero, they cannot be scaled to a sum of 1.0 and are left unchanged

Definition at line 1513 of file clipping.c.

Here is the caller graph for this function:

◆ yac_lon_lat_cell_lat_clipping()

static void yac_lon_lat_cell_lat_clipping ( struct yac_grid_cell * cell,
double z_upper_bound,
double z_lower_bound,
struct yac_grid_cell * overlap_buffer )
static

clips a lon-lat cell with a latitude band

Remarks
the cell has to have exactly four corners (a lon-lat cell whose lat edge collapsed at a pole is not supported)

Definition at line 1245 of file clipping.c.

Here is the caller graph for this function:

Variable Documentation

◆ overlap_cell_buffer

struct yac_grid_cell* overlap_cell_buffer = NULL
static

Definition at line 66 of file clipping.c.

◆ overlap_cell_buffer_size

size_t overlap_cell_buffer_size = 0
static

Definition at line 67 of file clipping.c.

◆ tol

double const tol = 1.0e-12
static

Definition at line 23 of file clipping.c.