~yade-dev/yade/0.80

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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
/* pygts - python package for the manipulation of triangulated surfaces
 *
 *   Copyright (C) 2009 Thomas J. Duck
 *   All rights reserved.
 *
 *   Thomas J. Duck <tom.duck@dal.ca>
 *   Department of Physics and Atmospheric Science,
 *   Dalhousie University, Halifax, Nova Scotia, Canada, B3H 3J5
 *
 * NOTICE
 *
 *   This library is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU Library 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
 *   Library General Public License for more details.
 *
 *   You should have received a copy of the GNU Library General Public
 *   License along with this library; if not, write to the
 *   Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 *   Boston, MA 02111-1307, USA.
 */

#include "pygts.h"


#if PYGTS_DEBUG
  #define SELF_CHECK if(!pygts_edge_check((PyObject*)self)) {         \
                       PyErr_SetString(PyExc_RuntimeError,            \
                       "problem with self object (internal error)");  \
		       return NULL;                                   \
                     }
#else
  #define SELF_CHECK
#endif


/*-------------------------------------------------------------------------*/
/* Methods exported to python */

static PyObject*
is_ok(PygtsEdge *self, PyObject *args)
{
  if(pygts_edge_is_ok(self)) {
    Py_INCREF(Py_True);
    return Py_True;
  }
  else {
    Py_INCREF(Py_False);
    return Py_False;
  }
}


static PyObject*
is_unattached(PygtsEdge *self, PyObject *args)
{
  guint n;

  SELF_CHECK

  /* Check for attachments other than to the gtsobj_parent */
  n = g_slist_length(PYGTS_EDGE_AS_GTS_EDGE(self)->triangles);
  if( n > 1 ) {
    Py_INCREF(Py_False);
    return Py_False;
  }
  else if( n == 1 ){
    Py_INCREF(Py_True);
    return Py_True;
  }
  else {
    PyErr_SetString(PyExc_RuntimeError,"Edge lost parent (internal error)");
    return NULL;
  }
}


/* replace() works, but can break Triangles and so is disabled */

/* static PyObject* */
/* replace(PygtsEdge *self, PyObject *args) */
/* { */
/*   PyObject *e2_; */
/*   PygtsEdge *e2; */
/*   GSList *parents=NULL, *i, *cur; */

/* #if PYGTS_DEBUG */
/*   if(!pygts_edge_check((PyObject*)self)) { */
/*     PyErr_SetString(PyExc_TypeError, */
/* 		    "problem with self object (internal error)"); */
/*     return NULL; */
/*   } */
/* #endif */

/*   /\* Parse the args *\/   */
/*   if(! PyArg_ParseTuple(args, "O", &e2_) ) { */
/*     return NULL; */
/*   } */

/*   /\* Convert to PygtsObjects *\/ */
/*   if(!pygts_edge_check(e2_)) { */
/*     PyErr_SetString(PyExc_TypeError,"expected an Edge"); */
/*     return NULL; */
/*   } */
/*   e2 = PYGTS_EDGE(e2_); */

/*   if(PYGTS_OBJECT(self)->gtsobj!=PYGTS_OBJECT(e2)->gtsobj) { */
/*     /\* (Ignore self-replacement) *\/ */

/*     /\* Detach and save any parent triangles *\/ */
/*     i = GTS_EDGE(PYGTS_OBJECT(self)->gtsobj)->triangles; */
/*     while(i!=NULL) { */
/*       cur = i; */
/*       i = i->next; */
/*       if(PYGTS_IS_PARENT_TRIANGLE(cur->data)) { */
/* 	GTS_EDGE(PYGTS_OBJECT(self)->gtsobj)->triangles =  */
/* 	  g_slist_remove_link(GTS_EDGE(PYGTS_OBJECT(self)->gtsobj)->triangles, */
/* 			      cur); */
/* 	parents = g_slist_prepend(parents,cur->data); */
/* 	g_slist_free_1(cur); */
/*       } */
/*     } */

/*     /\* Perform the replace operation *\/ */
/*     gts_edge_replace(GTS_EDGE(PYGTS_OBJECT(self)->gtsobj), */
/* 		     GTS_EDGE(PYGTS_OBJECT(e2)->gtsobj)); */

/*     /\* Reattach the parent segments *\/ */
/*     i = parents; */
/*     while(i!=NULL) { */
/*       GTS_EDGE(PYGTS_OBJECT(self)->gtsobj)->triangles =  */
/* 	g_slist_prepend(GTS_EDGE(PYGTS_OBJECT(self)->gtsobj)->triangles, */
/* 			i->data); */
/*       i = i->next; */
/*     } */
/*     g_slist_free(parents); */
/*   } */

/* #if PYGTS_DEBUG */
/*   if(!pygts_edge_check((PyObject*)self)) { */
/*     PyErr_SetString(PyExc_TypeError, */
/* 		    "problem with self object (internal error)"); */
/*     return NULL; */
/*   } */
/* #endif */

/*   Py_INCREF(Py_None); */
/*   return Py_None; */
/* } */


static PyObject*
face_number(PygtsEdge *self, PyObject *args)
{
  PyObject *s_;
  GtsSurface *s;

  SELF_CHECK

  /* Parse the args */
  if(! PyArg_ParseTuple(args, "O", &s_) ) {
    return NULL;
  }

  /* Convert to PygtsObjects */
  if(!pygts_surface_check(s_)) {
    PyErr_SetString(PyExc_TypeError,"expected a Surface");
    return NULL;
  }
  s = PYGTS_SURFACE_AS_GTS_SURFACE(s_);

  return Py_BuildValue("i",
		       gts_edge_face_number(PYGTS_EDGE_AS_GTS_EDGE(self),s));
}


static PyObject*
belongs_to_tetrahedron(PygtsEdge *self, PyObject *args)
{
  SELF_CHECK

  if(gts_edge_belongs_to_tetrahedron(PYGTS_EDGE_AS_GTS_EDGE(self))) {
    Py_INCREF(Py_True);
    return Py_True;
  }
  else {
    Py_INCREF(Py_False);
    return Py_False;
  }
}


static PyObject*
is_boundary(PygtsEdge *self, PyObject *args)
{
  PyObject *s_;
  GtsSurface *s;

  SELF_CHECK

  /* Parse the args */
  if(! PyArg_ParseTuple(args, "O", &s_) ) {
    return NULL;
  }

  /* Convert to PygtsObjects */
  if(!pygts_surface_check(s_)) {
    PyErr_SetString(PyExc_TypeError,"expected a Surface");
    return NULL;
  }
  s = PYGTS_SURFACE_AS_GTS_SURFACE(s_);

  /* Make the call and return */
  if(gts_edge_is_boundary(PYGTS_EDGE_AS_GTS_EDGE(self),s)!=NULL) {
    Py_INCREF(Py_True);
    return Py_True;
  }
  else {
    Py_INCREF(Py_False);
    return Py_False;
  }
}


static PyObject*
contacts(PygtsEdge *self, PyObject *args)
{
  SELF_CHECK

  return Py_BuildValue("i",
		       gts_edge_is_contact(PYGTS_EDGE_AS_GTS_EDGE(self)));
}


/* Methods table */
static PyMethodDef methods[] = {
  {"is_ok", (PyCFunction)is_ok,
   METH_NOARGS,
   "True if this Edge e is not degenerate or duplicate.\n"
   "False otherwise.  Degeneracy implies e.v1.id == e.v2.id.\n"
   "\n"
   "Signature: e.is_ok()\n"
  },  

  {"is_unattached", (PyCFunction)is_unattached,
   METH_NOARGS,
   "True if this Edge e is not part of any Triangle.\n"
   "\n"
   "Signature: e.is_unattached()\n"
  },

/* Edge replace() method works but results in Triangles that are "not ok";
 * i.e., they have edges that don't connect.  We don't want that problem
 * and so the method has been disabled.
 */
/*
  {"replace", (PyCFunction)replace,
   METH_VARARGS,
   "Replaces this Edge e1 with Edge e2 in all Triangles that have e1.\n"
   "Edge e1 itself is left unchanged.\n"
   "\n"
   "Signature: e1.replace(e2).\n"
  },
*/

  {"face_number", (PyCFunction)face_number,
   METH_VARARGS,
   "Returns number of faces using this Edge e on Surface s.\n"
   "\n"
   "Signature: e.face_number(s)\n"
  },

  {"is_boundary", (PyCFunction)is_boundary,
   METH_VARARGS,
   "Returns True if this Edge e is a boundary on Surface s.\n"
   "Otherwise False.\n"
   "\n"
   "Signature: e.is_boundary(s)\n"
  },

  {"belongs_to_tetrahedron", (PyCFunction)belongs_to_tetrahedron,
   METH_NOARGS,
   "Returns True if this Edge e belongs to a tetrahedron.\n"
   "Otherwise False.\n"
   "\n"
   "Signature: e.belongs_to_tetrahedron()\n"
  },

  {"contacts", (PyCFunction)contacts,
   METH_NOARGS,
   "Returns number of sets of connected triangles share this Edge e\n"
   "as a contact Edge.\n"
   "\n"
   "Signature: e.contacts()\n"
  },

  {NULL}  /* Sentinel */
};


/*-------------------------------------------------------------------------*/
/* Python type methods */

static GtsObject* parent(GtsEdge *e1);

static PyObject *
new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
  PyObject *o;
  PygtsObject *obj;
  GtsEdge *tmp;
  GtsObject *edge=NULL;
  PyObject *v1_,*v2_;
  PygtsVertex *v1,*v2;
  guint alloc_gtsobj = TRUE;
  guint N;

  /* Parse the args */
  if(kwds) {
    o = PyDict_GetItemString(kwds,"alloc_gtsobj");
    if(o==Py_False) {
      alloc_gtsobj = FALSE;
    }
    if(o!=NULL) {
      PyDict_DelItemString(kwds, "alloc_gtsobj");
    }
  }
  if(kwds) {
    Py_INCREF(Py_False);
    PyDict_SetItemString(kwds,"alloc_gtsobj", Py_False);
  }

  /* Allocate the gtsobj (if needed) */
  if( alloc_gtsobj ) {

    /* Parse the args */
    if( (N = PyTuple_Size(args)) < 2 ) {
      PyErr_SetString(PyExc_TypeError,"expected two Vertices");
      return NULL;
    }
    v1_ = PyTuple_GET_ITEM(args,0);
    v2_ = PyTuple_GET_ITEM(args,1);

    /* Convert to PygtsObjects */
    if(!pygts_vertex_check(v1_)) {
      PyErr_SetString(PyExc_TypeError,"expected two Vertices");
      return NULL;
    }
    if(!pygts_vertex_check(v2_)) {
      PyErr_SetString(PyExc_TypeError,"expected two Vertices");
      return NULL;
    }
    v1 = PYGTS_VERTEX(v1_);
    v2 = PYGTS_VERTEX(v2_);

    /* Error check */
    if(PYGTS_OBJECT(v1)->gtsobj == PYGTS_OBJECT(v2)->gtsobj) {
      PyErr_SetString(PyExc_ValueError,"Vertices given are the same");
      return NULL;
    }

    /* Create the GtsEdge */
    edge = GTS_OBJECT(gts_edge_new(gts_edge_class(),
				   GTS_VERTEX(v1->gtsobj),
				   GTS_VERTEX(v2->gtsobj)));
    if( edge == NULL )  {
      PyErr_SetString(PyExc_MemoryError, "could not create Edge");
      return NULL;
    }

    /* Check for duplicate */
    tmp = gts_edge_is_duplicate(GTS_EDGE(edge));
    if( tmp != NULL ) {
      gts_object_destroy(edge);
      edge = GTS_OBJECT(tmp);
    }

    /* If corresponding PyObject found in object table, we are done */
    if( (obj=g_hash_table_lookup(obj_table,edge)) != NULL ) {
      Py_INCREF(obj);
      return (PyObject*)obj;
    }
  }

  /* Chain up */
  obj = PYGTS_OBJECT(PygtsSegmentType.tp_new(type,args,kwds));

  if( alloc_gtsobj ) {
    obj->gtsobj = edge;

    /* Create a parent GtsTriangle */
    if( (obj->gtsobj_parent = parent(GTS_EDGE(obj->gtsobj))) == NULL ) {
      gts_object_destroy(obj->gtsobj);
      obj->gtsobj = NULL;
      return NULL;
    }

    pygts_object_register(PYGTS_OBJECT(obj));
  }

  return (PyObject*)obj;
}


static int
init(PygtsEdge *self, PyObject *args, PyObject *kwds)
{
  gint ret;

  /* Chain up */
  if( (ret=PygtsSegmentType.tp_init((PyObject*)self,args,kwds)) != 0 ){
    return ret;
  }

  return 0;
}


/* Methods table */
PyTypeObject PygtsEdgeType = {
    PyObject_HEAD_INIT(NULL)
    0,                       /* ob_size */
    "gts.Edge",              /* tp_name */
    sizeof(PygtsEdge),       /* tp_basicsize */
    0,                       /* tp_itemsize */
    0,                       /* tp_dealloc */
    0,                       /* tp_print */
    0,                       /* tp_getattr */
    0,                       /* tp_setattr */
    0,                       /* tp_compare */
    0,                       /* tp_repr */
    0,                       /* tp_as_number */
    0,                       /* tp_as_sequence */
    0,                       /* tp_as_mapping */
    0,                       /* tp_hash */
    0,                       /* tp_call */
    0,                       /* tp_str */
    0,                       /* tp_getattro */
    0,                       /* tp_setattro */
    0,                       /* tp_as_buffer */
    Py_TPFLAGS_DEFAULT |
      Py_TPFLAGS_BASETYPE,   /* tp_flags */
    "Edge object",           /* tp_doc */
    0,                       /* tp_traverse */
    0,                       /* tp_clear */
    0,                       /* tp_richcompare */
    0,                       /* tp_weaklistoffset */
    0,                       /* tp_iter */
    0,                       /* tp_iternext */
    methods,                 /* tp_methods */
    0,                       /* tp_members */
    0,                       /* tp_getset */
    0,                       /* tp_base */
    0,                       /* tp_dict */
    0,                       /* tp_descr_get */
    0,                       /* tp_descr_set */
    0,                       /* tp_dictoffset */
    (initproc)init,          /* tp_init */
    0,                       /* tp_alloc */
    (newfunc)new             /* tp_new */
};


/*-------------------------------------------------------------------------*/
/* Pygts functions */

gboolean 
pygts_edge_check(PyObject* o)
{
  if(! PyObject_TypeCheck(o, &PygtsEdgeType)) {
    return FALSE;
  }
  else {
#if PYGTS_DEBUG
    return pygts_edge_is_ok(PYGTS_EDGE(o));
#else
    return TRUE;
#endif
  }
}

gboolean
pygts_edge_is_ok(PygtsEdge *e)
{
  GSList *parent;
  PygtsObject *obj;

  obj = PYGTS_OBJECT(e);

  if(!pygts_segment_is_ok(PYGTS_SEGMENT(e))) return FALSE;

  /* Check for a valid parent */
  g_return_val_if_fail(obj->gtsobj_parent!=NULL,FALSE);
  g_return_val_if_fail(PYGTS_IS_PARENT_TRIANGLE(obj->gtsobj_parent),FALSE);
  parent = g_slist_find(GTS_EDGE(obj->gtsobj)->triangles,
			obj->gtsobj_parent);
  g_return_val_if_fail(parent!=NULL,FALSE);

  return TRUE;
}


static GtsObject* 
parent(GtsEdge *e1) {
  GtsVertex *v1,*v2,*v3;
  GtsPoint *p1,*p2;
  GtsEdge *e2, *e3;
  GtsTriangle *p;

  /* Create a third vertex for the triangle */
  v1 = GTS_SEGMENT(e1)->v1;
  v2 = GTS_SEGMENT(e1)->v2;
  p1 = GTS_POINT(v1);
  p2 = GTS_POINT(v2);
  v3 = gts_vertex_new(pygts_parent_vertex_class(),
		      p1->x+p2->x,p1->y+p2->y,p1->z+p2->z);
  if( v3 == NULL ) {
    PyErr_SetString(PyExc_MemoryError, "could not create Vertex");
    return NULL;
  }

  /* Create another two edges */
  if( (e2 = gts_edge_new(pygts_parent_edge_class(),v2,v3)) == NULL ) {
    PyErr_SetString(PyExc_MemoryError, "could not create Edge");
    return NULL;
  }
  if( (e3 = gts_edge_new(pygts_parent_edge_class(),v3,v1)) == NULL ) {
    PyErr_SetString(PyExc_MemoryError, "could not create Edge");
    gts_object_destroy(GTS_OBJECT(e2));
    return NULL;
  }

  /* Create and return the parent */
  if( (p = gts_triangle_new(pygts_parent_triangle_class(),e1,e2,e3)) 
      == NULL ) {
    gts_object_destroy(GTS_OBJECT(e2));
    gts_object_destroy(GTS_OBJECT(e3));
    PyErr_SetString(PyExc_MemoryError, "could not create Triangle");
    return NULL;
  }

  return GTS_OBJECT(p);
}


PygtsEdge *
pygts_edge_new(GtsEdge *e)
{
  PyObject *args, *kwds;
  PygtsObject *edge;

  /* Check for Edge in the object table */
  if( (edge = PYGTS_OBJECT(g_hash_table_lookup(obj_table,GTS_OBJECT(e)))) 
      !=NULL ) {
    Py_INCREF(edge);
    return PYGTS_EDGE(edge);
  }

  /* Build a new Edge */
  args = Py_BuildValue("OO",Py_None,Py_None);
  kwds = Py_BuildValue("{s:O}","alloc_gtsobj",Py_False);
  edge = PYGTS_EDGE(PygtsEdgeType.tp_new(&PygtsEdgeType, args, kwds));
  Py_DECREF(args);
  Py_DECREF(kwds);
  if( edge == NULL ) {
    PyErr_SetString(PyExc_MemoryError,"could not create Edge");
    return NULL;
  }
  edge->gtsobj = GTS_OBJECT(e);

  /* Attach the parent */
  if( (edge->gtsobj_parent = parent(e)) == NULL ) {
    Py_DECREF(edge);
    return NULL;
  }

  /* Register and return */
  pygts_object_register(edge);
  return PYGTS_EDGE(edge);
}


GtsTriangleClass*
pygts_parent_triangle_class(void)
{
  static GtsTriangleClass *klass = NULL;
  GtsObjectClass *super = NULL;

  if (klass == NULL) {

    super = GTS_OBJECT_CLASS(gts_triangle_class());

    GtsObjectClassInfo pygts_parent_triangle_info = {
      "PygtsParentTriangle",
      sizeof(PygtsParentTriangle),
      sizeof(GtsTriangleClass),
      (GtsObjectClassInitFunc)(super->info.class_init_func),
      (GtsObjectInitFunc)(super->info.object_init_func),
      (GtsArgSetFunc) NULL,
      (GtsArgGetFunc) NULL
    };
    klass = gts_object_class_new(gts_object_class(),
				 &pygts_parent_triangle_info);
  }

  return klass;
}


GtsEdgeClass*
pygts_parent_edge_class(void)
{
  static GtsEdgeClass *klass = NULL;
  GtsObjectClass *super = NULL;

  if (klass == NULL) {

    super = GTS_OBJECT_CLASS(pygts_parent_segment_class());

    GtsObjectClassInfo pygts_parent_edge_info = {
      "PygtsParentEdge",
      sizeof(PygtsParentEdge),
      sizeof(GtsEdgeClass),
      (GtsObjectClassInitFunc)(super->info.class_init_func),
      (GtsObjectInitFunc)(super->info.object_init_func),
      (GtsArgSetFunc) NULL,
      (GtsArgGetFunc) NULL
    };
    klass = gts_object_class_new(gts_object_class(),
				 &pygts_parent_edge_info);
  }

  return klass;
}