[go: up one dir, main page]

File: rtk_region.c

package info (click to toggle)
stage 2.0.3-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 2,676 kB
  • ctags: 1,725
  • sloc: ansic: 10,192; sh: 8,357; cpp: 3,676; makefile: 199
file content (150 lines) | stat: -rw-r--r-- 3,375 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
 *  RTK2 : A GUI toolkit for robotics
 *  Copyright (C) 2001  Andrew Howard  ahoward@usc.edu
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

/*
 * Desc: Stk region manipulation
 * Author: Andrew Howard
 * CVS: $Id: rtk_region.c,v 1.4 2005/03/11 21:56:57 rtv Exp $
 *
 * TODO:
 *  - Use GdkRegions instead of GdkRectangles.
 */

#include <stdlib.h>
#include "rtk.h"


// Create a new region.
stg_rtk_region_t *stg_rtk_region_create()
{
  stg_rtk_region_t *region;

  region = calloc(1, sizeof(stg_rtk_region_t));
  stg_rtk_region_set_empty(region);

  return region;
}


// Destroy a region.
void stg_rtk_region_destroy(stg_rtk_region_t *region)
{
  free(region);
  return;
}


// Set a region to empty.
void stg_rtk_region_set_empty(stg_rtk_region_t *region)
{
  region->rect.x = region->rect.y = 0;
  region->rect.width = region->rect.height = 0;
  return;
}


// Set the region to the union of the two given regions.
void stg_rtk_region_set_union(stg_rtk_region_t *regiona, stg_rtk_region_t *regionb)
{
  GdkRectangle rectc;

  if (!stg_rtk_region_test_empty(regionb))
  {
    if (stg_rtk_region_test_empty(regiona))
      regiona->rect = regionb->rect;
    else 
    {
      gdk_rectangle_union(&regiona->rect, &regionb->rect, &rectc);
      regiona->rect = rectc;
    }
  }
  return;
}


// Set the region to the union of the region with a rectangle
void stg_rtk_region_set_union_rect(stg_rtk_region_t *region, int ax, int ay, int bx, int by)
{
  GdkRectangle rectb, rectc;

  if (ax <= 0)
    rectb.x = 0;
  else
    rectb.x = ax;

  if (ay <= 0)
    rectb.y = 0;
  else
    rectb.y = ay;

  if (bx - ax + 1 <= 0)
    rectb.width = 0;
  else
    rectb.width = bx - ax + 1;
  
  if (by - ay + 1 <= 0)
    rectb.height = 0;
  else
    rectb.height = by - ay + 1;

  if (rectb.width * rectb.height > 0)
  {
    if (stg_rtk_region_test_empty(region))
      region->rect = rectb;
    else
    {
      gdk_rectangle_union(&region->rect, &rectb, &rectc);
      region->rect = rectc;
    }
  }
  return;
}


// Get the bounding rectangle for the region.
void stg_rtk_region_get_brect(stg_rtk_region_t *region, GdkRectangle *rect)
{
  *rect = region->rect;
  return;
}


// Test to see if a region is empty.
int stg_rtk_region_test_empty(stg_rtk_region_t *region)
{
  if (region->rect.width * region->rect.height == 0)
    return 1;
  return 0;
}


// Test for intersection betweenr regions.
int stg_rtk_region_test_intersect(stg_rtk_region_t *regiona, stg_rtk_region_t *regionb)
{
  GdkRectangle recta, rectb, rectc;

  recta = regiona->rect;
  recta.width += 1;
  rectb = regionb->rect;
  rectb.width += 1;
  
  return gdk_rectangle_intersect(&regiona->rect, &regionb->rect, &rectc);
}