File: region.c

package info (click to toggle)
xmakemol 5.16-7
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,724 kB
  • ctags: 1,565
  • sloc: ansic: 19,032; sh: 2,977; perl: 98; makefile: 97
file content (247 lines) | stat: -rw-r--r-- 4,144 bytes parent folder | download | duplicates (6)
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*

Copyright (C) 1999, 2000, 2001, 2005 Matthew P. Hodges
This file is part of XMakemol.

XMakemol 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, or (at your option)
any later version.

XMakemol 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 XMakemol; see the file COPYING.  If not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

*/

#define __REGION_C__

/* Functions to define a region on the canvas and to determine which
   atoms lie inside it */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdio.h>
#include <stdlib.h>

#include <Xm/Xm.h>

#include "globals.h"

/* Function prototypes */

void canvas_cb (Widget, XtPointer, XtPointer);

static int *atoms_in_region, atoms_in_region_size;

static Boolean region_active;
static XPoint region_start, region_end;

XPoint
region_get_start(void)
{
  return (region_start);
}


void
region_set_start (XButtonEvent *bevent)
{

  void canvas_cb(Widget, XtPointer, XtPointer);
  void deactivate_region(void);

  region_start.x = bevent->x;
  region_start.y = bevent->y;

  deactivate_region ();

  canvas_cb (canvas, NULL, NULL);

}


XPoint
region_get_end(void)
{
 
  return(region_end);
  
}


void
region_set_end (XButtonEvent *bevent)
{

  void activate_region (void);

  region_end.x = bevent->x;
  region_end.y = bevent->y;

  if (region_active == 0)
    {
      activate_region ();
    }

  redraw = 1;
  canvas_cb (canvas, NULL, NULL);
}


void
activate_region (void)
{

  region_active = 1;

}


void
deactivate_region (void)
{

  region_active = 0;

}


Boolean
region_active_p (void)
{
  return(region_active);
}


int *
get_atoms_in_region (void)
{

  return (atoms_in_region);

}


void
update_atoms_in_region (Boolean inside_p)
{

  /* Work out which atoms are inside/outside the region depending on
     the value of inside_p */

  void allocate_atoms_in_region (void);
  void convert_to_canvas_coords (double *, double *, Boolean);

  int i;

  double atom_in[3], atom_out[2];
  double x1, y1, x2, y2, temp;

  /* Start coords of rectangle */

  x1 = region_start.x;
  y1 = region_start.y;

  /* End coords of rectangle */

  x2 = region_end.x;
  y2 = region_end.y;

  /* Ensure that top left of rectangle is the start */

  if(x1 > x2)
    {
      temp = x1;
      x1   = x2;
      x2   = temp;
    }

  if(y1 > y2)
    {
      temp = y1;
      y1   = y2;
      y2   = temp;
    }


  /* Initialize atoms_in_region if necessary */

  if(atoms_in_region == NULL)
    {
      allocate_atoms_in_region();
    }
  else
    {
      /* Check the size of atoms_in_region */
      
      if(atoms_in_region_size != no_atoms)
        {
          free(atoms_in_region);
          allocate_atoms_in_region();
        }
    }

  for(i = 0; i < no_atoms; i++)
    {

      atom_in[0] = atoms[i].x;
      atom_in[1] = atoms[i].y;
      atom_in[2] = atoms[i].z;
      
      convert_to_canvas_coords(atom_in, atom_out, 0);

      if((atom_out[0] > x1) &&
         (atom_out[0] < x2) &&
         (atom_out[1] > y1) &&
         (atom_out[1] < y2))
        {
          if(inside_p == 1)
            {
              atoms_in_region[i] = 1;
            }
          else
            {
              atoms_in_region[i] = 0;
            }
        }
      else
        {
          if(inside_p == 1)
            {
              atoms_in_region[i] = 0;
            }
          else
            {
              atoms_in_region[i] = 1;
            }
        }
      
    }

}


void
allocate_atoms_in_region(void)
{

  atoms_in_region = malloc((no_atoms * sizeof(int)));

  if(atoms_in_region == NULL)
    {
      fprintf(stderr, "Malloc failed for atoms_in_region (region.c)\n");
    }

  /* Keep track of the size */
  
  atoms_in_region_size = no_atoms;

}