File: mesh.h

package info (click to toggle)
xmorph 1%3A20011220
  • links: PTS
  • area: main
  • in suites: woody
  • size: 3,468 kB
  • ctags: 1,534
  • sloc: ansic: 16,401; sh: 2,651; makefile: 556; tcl: 516
file content (489 lines) | stat: -rw-r--r-- 12,986 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
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/* mesh.h -- mesh handling routines header
//

   Written and Copyright (C) 1994-1999 by Michael J. Gourlay

This file is part of Xmorph.

Xmorph 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.

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

*/

#ifndef _MESH_H__INCLUDED_
#define _MESH_H__INCLUDED_


#define MESHLABEL_T int
#define MESHPOINTSELECTED (-1)

typedef struct {
  long    nx;   /* number of mesh points in the x-direction */
  long    ny;   /* number of mesh points in the y-direction */
  double *x;    /* 2D array of mesh point x-values */
  double *y;    /* 2D array of mesh point y-values */
  MESHLABEL_T *label; /* any point may be labelled , for grouping points  etc 
	      the most important labels are 
	     0 == normal point
	     -1 == selected point */
  unsigned int changed; /*if it has been changed since the last saving */
  /* the reference count: good for porting libmorph in python (or 
     any language that needs garbage collecting)
     for easier use in GTK . read README.libmorph */
  unsigned int reference_counting;
} MeshT;












/* SWIG: Simplified Wrapper Interface Generator */
#ifdef SWIG
%module mesh
%{
#include "mesh.h"



/* mesh_tcl_result: TCL Result string for mesh functions
//
// DESCRIPTION
// This is a sneaky way to return character string results to TCL while
// not having to worry about memory leaks, and while still using SWIG's
// wrapping abilities.  I.e., I don't have to worry about writing my
// own TCL wrapper code and registering it with TCL.
//
// NOTES
// The drawback to this method is potentially that returning a pointer to
// a string could cause problems if that pointer is abused, i.e., if the
// contents of that string are assumed to remain constant by the TCL code.
*/
#define MESH_TCL_RESULT_MAX_LEN 2048
static char mesh_tcl_result[MESH_TCL_RESULT_MAX_LEN];



/* NAME
//   meshLine: return a line of mesh point coordinate pairs as a string
//
//
// ARGUMENTS
//   this (in): mesh pointer
//
//   line_index (in): index of the row or column, depending on 'direction'
//
//   direction (in): 1 for row (horizontal line), 2 for column (vertical line)
//
//
// DESCRIPTION
//   meshLine returns a list of coordinate pairs of all of the mesh
//   points along a line of the given line_index and direction.
//
//
// RETURN VALUE
//   Returns the address of the result string, which is assumed to be
//   passed back to the TCL interpretter as a TCL list.
//   The string used is the global string mesh_tcl_result.  Using a
//   constant address eliminated potential memory leak problems.
//
//
// SEE ALSO
//   mesh_tcl_result
*/
//FIXME mennucci: why is this in mesh.h instead of mesh.c?
// anyway it is protected by ifdef SWIG
char *
meshLine(const MeshT *this, const int line_index, const int direction)
{
  int vi;
  int nv;
  int str_len = 0;
  char float_string[64];
  int point_index = -10000;

  if(line_index < 0) {
    /* line_index is out of range */
    return NULL;
  }

  if(1 == direction) {
    nv = this->nx;
    if(line_index >= this->ny) {
      /* line_index is out of range */
      return NULL;
    }
  } else if (2 == direction) {
    nv = this->ny;
    if(line_index >= this->nx) {
      /* line_index is out of range */
      return NULL;
    }
  } else {
    /* Invalid value for direction */
    return NULL;
  }

  /* Empty the result string */
  mesh_tcl_result[0] = '\0';

  for(vi = 0; vi < nv; vi++) {
    if(1 == direction) {
      point_index = line_index * this->nx + vi;
    } else if (2 == direction) {
      point_index = vi * this->nx + line_index;
    }
    sprintf(float_string, " %.0f %.0f",
      this->x[point_index], this->y[point_index]);
    str_len += strlen(float_string);
    if(str_len >= MESH_TCL_RESULT_MAX_LEN) {
      fprintf(stderr, "meshLine: mesh_tcl_result length exceeded\n");
      return NULL;
    }
    strcat(mesh_tcl_result, float_string);
  }
  return mesh_tcl_result;
}
%}
#endif /* SWIG */



#define meshUnsaved(m) ((m)->changed >0)


#ifdef SWIG
%addmethods MeshT {
  MeshT(void)           { return meshNew(0, 0); }

  ~MeshT()              { meshDelete(self); }

  int alloc(int nx, int ny)
                        { return meshAlloc(self, nx, ny); }

  void free()           { meshFree(self); }

  void print()          { meshPrint(self); }

  void interpolate(const MeshT *m1P, const MeshT *m2P, float tween_param)
                        {meshInterpolate(self, m1P, m2P, tween_param);}

  void reset(int img_width, int img_height)
                        { meshReset(self, img_width, img_height); }

  void scale(int img_width, int img_height)
                        { meshScale(self, img_width, img_height); }

  char *row(int line_index)
                        { return meshLine(self, line_index, 1); }

  char *col(int line_index)
                        { return meshLine(self, line_index, 2); }

  int pick(int mouse_x, int mouse_y, int component, float proximity)
        { return meshPick(self, mouse_x, mouse_y, component, proximity); }

  void store()          { meshStore(self); }

  void recover()        { meshRetrieve(self); }

  int lineModify(MeshT *other, int mouse_x, int mouse_y, char line_type, char action)
        { return meshLineMouseModify(self, other, mouse_x, mouse_y,
                                     line_type, action);
        }

  int read(const char *filename)
                        { return meshRead(self, filename); }

  int write(const char *filename)
                        { return meshWrite(self, filename); }

  void match(const MeshT *other)
                        { meshMatch(self, other); }

  float pointGet(int xi, int yi, int component)
        { if(0 == component) return self->x[yi * self->nx + xi];
          else if(1 == component) return self->y[yi * self->nx + xi];
          /* Invalid value for component */
          else return -1.0;
        }

  void set(int xi, int yi, float new_x, float new_y)
        {  meshSet(self, xi, yi, new_x, new_y); }
};
#endif




/* MP_PICK_DIST: farthest you may be from a Mesh point to pick it */
#define MP_PICK_DIST 8




void meshInit(MeshT *this);

int meshAlloc(MeshT *this, int nx, int ny);

MeshT * meshNew(const int nx, const int ny);

static inline void meshFree(MeshT *this);

void meshDelete(MeshT *this);

void meshPrint(const MeshT *this);

int meshCompatibilityCheck(const MeshT *this, const MeshT *other);

void meshInterpolate(MeshT *moP, const MeshT *m1P, const MeshT *m2P, float tween_param);

void meshReset(MeshT *this, const int img_width, const int img_height);

void meshScale(MeshT *this, const int img_width, const int img_height) ;

int meshFunctionalize(MeshT *this, int img_width, int img_height);

long int meshPointNearest(const MeshT *this, int px, int py, int *mi, int *mj, int *dx, int *dy);

int meshPick(const MeshT *this, int mouse_x, int mouse_y, int component, float proximity);

void meshSet(MeshT *this, int xi, int yi, float new_x, float new_y);

//FIXME the situation is messy: sometimes the values for the point
// coordinates are float, but the values in the arrays are double
static inline
void meshSetNoundo(MeshT *this, int xi, int yi, double new_x, double new_y);

void meshSetLabel(MeshT *this, int xi, int yi, MESHLABEL_T new_label);

void meshCopy(MeshT *this, const MeshT *source) ;

void meshBackupIndexSet(int backup_index) ;

void meshBackupFree(void) ;

void meshStore(const MeshT *this) ;

void meshRetrieve(MeshT *this) ;

int meshLineAdd(MeshT *this, const int mi, const float mt, const int type);

int meshLineDelete(MeshT *this, int mi, int type);

int meshLineMouseModify(MeshT *this, MeshT *other, int mouse_x, int mouse_y, char line_type, char action);

int meshRead(MeshT *this, const char *filename);

int meshWrite(MeshT *this, char *filename);

void meshMatch(MeshT *this, const MeshT *other);

/*****************************************************************************
 * functions for accessing points in meshes
 *****************************************************************************/



/* reflect the coordinate in the mesh to have it inside
 *(this must be a macro) 
 */
#define  reflect_coord_mesh(N,X) { if((X)<0){(X)=-(X);}\
                                   while((X)>=(N)){(X)-=2*(N)-2;}\
                                   if((X)<0){(X)=-(X);} }
/* controls if the coordinates in the mesh are inside */

#define  check_coord_mesh_np(THIS,XI,YI) ( \
 ((XI) >= (THIS)->nx)  || ((XI) < 0) || ((YI) >= (THIS)->ny)  || ((YI) < 0))

#ifndef NDEBUG 
#ifdef g_warning
#define  check_coord_mesh_return0(THIS,XI,YI) {\
 if(check_coord_mesh_np(THIS,XI,YI)) {\
    g_warning("coord out of mesh, in %s at line %d\n",__FILE__,__LINE__);\
abort();\
 return 0;}}
#else  g_warning
#define  check_coord_mesh_return0(THIS,XI,YI) {\
 if(check_coord_mesh_np(THIS,XI,YI)) {\
 fprintf(stderr,"coords out of mesh, in %s at line %d\n",__FILE__,__LINE__);\
abort();\
 return 0;}}
#endif
#else NDEBUG 
#define  check_coord_mesh_return0(THIS,XI,YI) {}
#endif NDEBUG 

/**** inline functions... 
      when you would need a macro with local variables!*/


static inline double //__attribute__ weak 
meshGetx(MeshT *this, int xi, int yi)
{
  check_coord_mesh_return0(this,xi,yi);
  return this->x[yi * this->nx + xi] ;
}
static inline double //__attribute__ weak 
meshGetxRefl(MeshT *this, int xi, int yi)
{
  reflect_coord_mesh(this->nx,xi);
  reflect_coord_mesh(this->ny,yi);
  check_coord_mesh_return0(this,xi,yi);
  return this->x[yi * this->nx + xi] ;
}


static inline double   //__attribute__ weak 
meshGety(MeshT *this, int xi, int yi)
{
  check_coord_mesh_return0(this,xi,yi);
  return this->y[yi * this->nx + xi] ;  
}

static inline double   //__attribute__ weak 
meshGetyRefl(MeshT *this, int xi, int yi)
{
  reflect_coord_mesh(this->nx,xi);
  reflect_coord_mesh(this->ny,yi);
  check_coord_mesh_return0(this,xi,yi);
  return this->y[yi * this->nx + xi] ;  
}


/** 
    maximum values for x,y in the mesh to be inside the mesh
**/
static inline double
meshMaxx(MeshT *mesh)
{
  return   meshGetx(mesh,mesh->nx-1,mesh->ny-1);
}

static inline double
meshMaxy(MeshT *mesh)
{
  return   meshGety(mesh,mesh->nx-1,mesh->ny-1);
}



static inline MESHLABEL_T   //__attribute__ weak 
meshGetLabel(MeshT *this, int xi, int yi)
{
  check_coord_mesh_return0(this,xi,yi);
  return this->label[yi * this->nx + xi] ;
}


static inline void  //__attribute__ weak 
meshSetNoundo(MeshT *this, int xi, int yi, double new_x, double new_y)
{
#ifndef NDEBUG
  if(check_coord_mesh_np(this,xi,yi)) {
#ifdef g_warning
    g_warning("set coord out of mesh, in %s at line %d\n",__FILE__,__LINE__);
#else
    fprintf(stderr,"\
set coord out of mesh, in %s at line %d\n",__FILE__,__LINE__);
#endif
  } else 
#endif
    {
    this->x[yi * this->nx + xi] = new_x;
    this->y[yi * this->nx + xi] = new_y;
    this->changed ++;
  }
}






/* NAME
//   meshFree: free memory of internal arrays of a MeshT
//             THIS FUNCTION IS DEPRECATED: USE meshUnref
//
// ARGUMENTS
//   this: pointer to mesh
//
//
// NOTES
//   The memory of the MeshT instance is not freed here.
//
//   mennucci: since this is influencede by NDEBUG and the presence of glib,
//     I put it in mesh.h, so it is recompiled inside the applications using
//     libmorph
*/
static inline void
meshFree(MeshT *this)
{
  void  meshFreeReally(MeshT *this);

#ifndef NDEBUG_LIBMORPH_REF_COUNT
#ifndef NDEBUG 
#ifdef g_warning
  g_warning("meshFree is not to be used in applications! use meshUnref\n");
#else //g_warning
  fprintf(stderr,"meshFree is not to be used in applications! use meshUnref\n");
#endif //g_warning
  if(this->reference_counting>0) {
#ifdef g_warning
    g_warning("meshFree: mesh has positive reference counting!\n");
#else //g_warning
    fprintf(stderr,"meshFree: mesh has positive reference counting!\n");
#endif //g_warning
#ifdef LIBMORPH_STRICTLY_CHECKS_REFERENCE_COUNTING
    abort();
#endif 
  }
#endif NDEBUG
#endif NDEBUG_LIBMORPH_REF_COUNT
  meshFreeReally(this);
}


static inline void
meshRef(MeshT *this)
{
  this->reference_counting++;
}
static inline void
meshUnref(MeshT *this)
{
  void  meshFreeReally(MeshT *this);
  this->reference_counting--;
  if(this->reference_counting==0)
    meshFreeReally(this);
}


#ifdef GIMP
void set_src_mesh_name (char *fname);
char *get_src_mesh_name (void);

void set_dst_mesh_name (char *fname);
char *get_dst_mesh_name (void);
#endif


#endif /* _MESH_H__INCLUDED_ */