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 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
|
/* EXTRAITS DE LA LICENCE
Copyright CEA, contributeurs : Damien CALISTE, laboratoire L_Sim, (2001-2008)
Adresse m�l :
CALISTE, damien P caliste AT cea P fr.
Ce logiciel est un programme informatique servant � visualiser des
structures atomiques dans un rendu pseudo-3D.
Ce logiciel est r�gi par la licence CeCILL soumise au droit fran�ais et
respectant les principes de diffusion des logiciels libres. Vous pouvez
utiliser, modifier et/ou redistribuer ce programme sous les conditions
de la licence CeCILL telle que diffus�e par le CEA, le CNRS et l'INRIA
sur le site "http://www.cecill.info".
Le fait que vous puissiez acc�der � cet en-t�te signifie que vous avez
pris connaissance de la licence CeCILL, et que vous en avez accept� les
termes (cf. le fichier Documentation/licence.fr.txt fourni avec ce logiciel).
*/
/* LICENCE SUM UP
Copyright CEA, contributors: Damien CALISTE, laboratoire L_Sim, (2001-2008)
E-mail address:
CALISTE, damien P caliste AT cea P fr.
This software is a computer program whose purpose is to visualize atomic
configurations in 3D.
This software is governed by the CeCILL license under French law and
abiding by the rules of distribution of free software. You can use,
modify and/ or redistribute the software under the terms of the CeCILL
license as circulated by CEA, CNRS and INRIA at the following URL
"http://www.cecill.info".
The fact that you are presently reading this means that you have had
knowledge of the CeCILL license and that you accept its terms. You can
find a copy of this licence shipped with this software at
Documentation/licence.en.txt.
*/
#include <Python.h>
#include <gtk/gtk.h>
#include <pygobject.h>
#include <pygtk/pygtk.h>
#include <visu_basic.h>
#include <visu_gtk.h>
#include <visu_elements.h>
#include <visu_data.h>
#include <visu_object.h>
#include <visu_extension.h>
#include <gtk_renderingWindowWidget.h>
#include <coreTools/toolPhysic.h>
#include <openGLFunctions/objectList.h>
#include "plane_py.h"
typedef struct _VisuElementPy
{
PyObject_HEAD
VisuElement *obj;
gboolean ownByVSim;
} VisuElementPy;
static int visuElementPy_init(VisuElementPy *self, PyObject *args, PyObject *kwds);
static PyObject* visuElementPy_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
static void visuElementPy_free(VisuElementPy* self);
static PyObject* visuElementPy_set(PyObject *obj, PyObject *args,
PyObject *kwds);
/* We define here all the methods of the dtsets class. */
static PyMethodDef VisuElementPyMethods[] =
{
{"set", (PyCFunction)visuElementPy_set, METH_VARARGS | METH_KEYWORDS,
"Change values for a VisuElement."},
/* {"get", (PyCFunction)dtsets_get, METH_VARARGS, */
/* "Return the valus of an attribute."}, */
{NULL, NULL, 0, NULL}
};
/* static PyMemberDef VisuElementPyMembers[] = */
/* { */
/* {"dt", T_OBJECT_EX, offsetof(Dtsets, dt), READONLY, */
/* "Internal identifier for Fortran calls."}, */
/* {NULL, 0, 0, 0, NULL} */
/* }; */
static PyTypeObject VisuElementPyType = {
PyObject_HEAD_INIT(NULL)
0, /*ob_size*/
"v_sim.VisuElementPy", /*tp_name*/
sizeof(VisuElementPy), /*tp_basicsize*/
0, /*tp_itemsize*/
(destructor)visuElementPy_free, /*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*/
"Ploum", /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
VisuElementPyMethods, /* 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)visuElementPy_init, /* tp_init */
0, /* tp_alloc */
visuElementPy_new, /* tp_new */
/* 0, */
/* 0, */
/* 0, */
/* 0, */
/* 0, */
/* 0, */
/* 0, */
/* 0 */
};
typedef struct _VisuDataPy VisuDataPy;
typedef struct _VisuNodePy
{
PyObject_HEAD
guint nodeId;
VisuDataPy *parent;
} VisuNodePy;
static int visuNodePy_init(VisuNodePy *self, PyObject *args, PyObject *kwds);
static PyObject* visuNodePy_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
static void visuNodePy_free(VisuNodePy* self);
static PyObject* visuNodePy_isValid(PyObject *obj);
static PyObject* visuNodePy_set(PyObject *obj, PyObject *args, PyObject *kwds);
/* We define here all the methods of the dtsets class. */
static PyMethodDef VisuNodePyMethods[] =
{
{"set", (PyCFunction)visuNodePy_set, METH_VARARGS | METH_KEYWORDS,
"Change values for a VisuNode."},
{"isValid", (PyCFunction)visuNodePy_isValid, METH_NOARGS,
"Test if the given node still exist or has been deleted."},
/* {"get", (PyCFunction)dtsets_get, METH_VARARGS, */
/* "Return the valus of an attribute."}, */
{NULL, NULL, 0, NULL}
};
/* static PyMemberDef VisuNodePyMembers[] = */
/* { */
/* {"dt", T_OBJECT_EX, offsetof(Dtsets, dt), READONLY, */
/* "Internal identifier for Fortran calls."}, */
/* {NULL, 0, 0, 0, NULL} */
/* }; */
static PyTypeObject VisuNodePyType = {
PyObject_HEAD_INIT(NULL)
0, /*ob_size*/
"v_sim.VisuNodePy", /*tp_name*/
sizeof(VisuNodePy), /*tp_basicsize*/
0, /*tp_itemsize*/
(destructor)visuNodePy_free, /*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*/
"Ploum", /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
VisuNodePyMethods, /* 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)visuNodePy_init, /* tp_init */
0, /* tp_alloc */
visuNodePy_new, /* tp_new */
/* 0, */
/* 0, */
/* 0, */
/* 0, */
/* 0, */
/* 0, */
/* 0, */
/* 0 */
};
struct _VisuDataPy
{
PyObject_HEAD
VisuData *obj;
PyObject *planes;
int planesId;
};
static int visuDataPy_init(VisuDataPy *self, PyObject *args, PyObject *kwds);
static PyObject* visuDataPy_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
static void visuDataPy_free(VisuDataPy* self);
static PyObject* visuDataPy_addFile(PyObject *self, PyObject *args, PyObject *kwds);
static PyObject* visuDataPy_setPopulation(PyObject *self, PyObject *args, PyObject *kwds);
static PyObject* visuDataPy_getPopulation(PyObject *self);
static PyObject* visuDataPy_createAllNodes(PyObject *self);
static PyObject* visuDataPy_setBox(PyObject *self, PyObject *args, PyObject *kwds);
static PyObject* visuDataPy_addNode(PyObject *self, PyObject *args, PyObject *kwds);
static PyObject* visuDataPy_delNodes(PyObject *self, PyObject *args);
static PyObject* visuDataPy_setPlanes(PyObject *self, PyObject *planes);
/* We define here all the methods of the dtsets class. */
static PyMethodDef VisuDataPyMethods[] =
{
{"addFile", (PyCFunction)visuDataPy_addFile, METH_VARARGS | METH_KEYWORDS,
"Associate a file name to a file type."},
{"setPopulation", (PyCFunction)visuDataPy_setPopulation, METH_VARARGS | METH_KEYWORDS,
"Allocate the size to store nodes."},
{"getPopulation", (PyCFunction)visuDataPy_getPopulation, METH_NOARGS,
"get a dictionnary with the description of the population."},
{"createAllNodes", (PyCFunction)visuDataPy_createAllNodes, METH_NOARGS,
"Create the OpenGL rendering for all the nodes."},
{"setBox", (PyCFunction)visuDataPy_setBox, METH_VARARGS | METH_KEYWORDS,
"Set the box size."},
{"addNode", (PyCFunction)visuDataPy_addNode, METH_VARARGS | METH_KEYWORDS,
"Set the coordinates of a new node for the given element."},
{"delNodes", (PyCFunction)visuDataPy_delNodes, METH_VARARGS,
"Delete the given nodes."},
{"setPlanes", (PyCFunction)visuDataPy_setPlanes, METH_O,
"Draw the given planes inside the box of the given data."},
{NULL, NULL, 0, NULL}
};
/* static PyMemberDef VisuDataPyMembers[] = */
/* { */
/* {"dt", T_OBJECT_EX, offsetof(Dtsets, dt), READONLY, */
/* "Internal identifier for Fortran calls."}, */
/* {NULL, 0, 0, 0, NULL} */
/* }; */
static PyTypeObject VisuDataPyType = {
PyObject_HEAD_INIT(NULL)
0, /*ob_size*/
"v_sim.VisuDataPy", /*tp_name*/
sizeof(VisuDataPy), /*tp_basicsize*/
0, /*tp_itemsize*/
(destructor)visuDataPy_free, /*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*/
"Ploum", /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
VisuDataPyMethods, /* 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)visuDataPy_init, /* tp_init */
0, /* tp_alloc */
visuDataPy_new, /* tp_new */
/* 0, */
/* 0, */
/* 0, */
/* 0, */
/* 0, */
/* 0, */
/* 0, */
/* 0 */
};
static int visuElementPy_init(VisuElementPy *self _U_, PyObject *args _U_,
PyObject *kwds _U_)
{
return 0;
}
static PyObject* visuElementPy_new(PyTypeObject *type, PyObject *args,
PyObject *kwds)
{
VisuElementPy *self;
char *name;
gchar *eleName;
PyObject *argsPy = NULL, *namePy;
name = (char*)0;
eleName = (gchar*)0;
namePy = PyTuple_GetItem(args, 0);
if (! PyObject_TypeCheck(namePy, &PyString_Type) &&
! PyObject_TypeCheck(namePy, &PyInt_Type))
{
PyErr_SetString(PyExc_TypeError, "first argument must be a string or a Z number.");
return NULL;
}
else
{
if (PyObject_TypeCheck(namePy, &PyString_Type))
name = PyString_AsString(namePy);
else if (PyObject_TypeCheck(namePy, &PyInt_Type))
{
if (!tool_physic_getElementFromZ(&eleName, (float*)0,
((PyIntObject*)namePy)->ob_ival))
PyErr_SetString(PyExc_ValueError, "Unknown element.");
name = eleName;
}
}
if (!name)
return NULL;
argsPy = PyTuple_GetSlice(args, 1, PyTuple_Size(args));
self = (VisuElementPy*)type->tp_alloc(type, 0);
if (self != NULL)
{
DBG_fprintf(stderr, "Visu Python: new VisuElement object %p.\n", self);
self->obj = visu_element_getFromName(name);
if (self->obj)
self->ownByVSim = TRUE;
else
{
self->obj = visu_element_new(name);
self->ownByVSim = FALSE;
}
}
else
{
Py_DECREF(argsPy);
return NULL;
}
if (! visuElementPy_set((PyObject*)self, argsPy, kwds))
{
Py_DECREF(argsPy);
return NULL;
}
Py_DECREF(argsPy);
return (PyObject *)self;
}
static void visuElementPy_free(VisuElementPy* self)
{
DBG_fprintf(stderr, "Visu Python: deallocate a VisuElement object %p.\n", self);
if (!self->ownByVSim)
visuElementFree(self->obj);
self->ob_type->tp_free((PyObject*)self);
}
static PyObject* visuElementPy_set(PyObject *obj, PyObject *args,
PyObject *kwds)
{
VisuElementPy *self;
static char *kwlist[] = {"rgba", "material", "rendered", "hidden", NULL};
PyObject *rgbPy = NULL, *matPy = NULL, *rendered = Py_True, *hidden = Py_True;
float rgb[4] = {-1.f, -1.f, -1.f, -1.f};
float mat[5] = {-0.2f, -1.f, -0.5f, -0.5f, -0.f};
DBG_fprintf(stderr, "Visu Python: set values for VisuElement '%s' object %p.\n",
((VisuElementPy*)obj)->obj->name, obj);
if (! PyArg_ParseTupleAndKeywords(args, kwds, "|O!O!O!O!", kwlist,
&PyTuple_Type, &rgbPy,
&PyTuple_Type, &matPy,
&PyBool_Type, &rendered,
&PyBool_Type, &hidden))
return NULL;
if (rgbPy)
{
if (! PyArg_ParseTuple(rgbPy, "ffff", rgb, rgb + 1, rgb + 2, rgb + 3))
return NULL;
}
if (matPy)
{
if (! PyArg_ParseTuple(matPy, "fffff", mat, mat + 1, mat + 2, mat + 3, mat + 4))
return NULL;
}
self = (VisuElementPy*)obj;
if (rgb[0] >= 0.f && mat[0] >= 0.f)
visuElementSet_allColorValues(self->obj, rgb, mat);
else if (rgb[0] >= 0.f)
visuElementSet_allRGBValues(self->obj, rgb);
else if (mat[0] >= 0.f)
visuElementSet_allMaterialValues(self->obj, mat);
visuElementSet_rendered(self->obj, (rendered == Py_True));
visuElementSet_sensitiveToPlanes(self->obj, (hidden == Py_True));
Py_INCREF(Py_None);
return Py_None;
}
static int visuNodePy_init(VisuNodePy *self _U_, PyObject *args _U_,
PyObject *kwds _U_)
{
return 0;
}
static PyObject* visuNodePy_new(PyTypeObject *type, PyObject *args, PyObject *kwds _U_)
{
VisuNodePy *self;
guint id;
PyObject *parentPy;
if (! PyArg_ParseTuple(args, "O!I", &VisuDataPyType, &parentPy, &id))
return NULL;
self = (VisuNodePy*)type->tp_alloc(type, 0);
if (self == NULL)
return NULL;
DBG_fprintf(stderr, "Visu Python: new VisuNode object %p.\n", self);
Py_INCREF(parentPy);
self->nodeId = id;
self->parent = (VisuDataPy*)parentPy;
return (PyObject *)self;
}
static void visuNodePy_free(VisuNodePy* self)
{
DBG_fprintf(stderr, "Visu Python: deallocate a VisuNode object %p.\n", self);
Py_DECREF(self->parent);
self->ob_type->tp_free((PyObject*)self);
}
static PyObject* visuNodePy_set(PyObject *obj, PyObject *args,
PyObject *kwds)
{
VisuNodePy *self;
static char *kwlist[] = {"coord", "translation", "rendered", "emit", NULL};
PyObject *coordPy = NULL, *transPy = NULL, *rendered = Py_None, *emit = Py_False;
gboolean coordSet = FALSE, transSet = FALSE;
float coord[3] = {-1.f, -1.f, -1.f};
float trans[3] = {-1.f, -1.f, -1.f};
VisuNode *node;
gboolean emitPos;
DBG_fprintf(stderr, "Visu Python: set values for VisuNode %d object %p.\n",
((VisuNodePy*)obj)->nodeId, obj);
if (! PyArg_ParseTupleAndKeywords(args, kwds, "|O!O!O!O!", kwlist,
&PyTuple_Type, &coordPy,
&PyTuple_Type, &transPy,
&PyBool_Type, &rendered,
&PyBool_Type, &emit))
return NULL;
if (coordPy)
{
if (! PyArg_ParseTuple(coordPy, "fff", coord, coord + 1, coord + 2))
return NULL;
coordSet = TRUE;
}
if (transPy)
{
if (! PyArg_ParseTuple(transPy, "fff", trans, trans + 1, trans + 2))
return NULL;
transSet = TRUE;
}
self = (VisuNodePy*)obj;
node = visuDataGet_nodeFromNumber(self->parent->obj, self->nodeId);
if (!node)
{
PyErr_SetString(PyExc_RuntimeError, "not a VisuNode anymore.");
return NULL;
}
emitPos = FALSE;
if (coordSet)
{
node->xyz[0] = coord[0];
node->xyz[1] = coord[1];
node->xyz[2] = coord[2];
emitPos = (emit == Py_True);
}
if (transSet)
{
node->translation[0] = coord[0];
node->translation[1] = coord[1];
node->translation[2] = coord[2];
emitPos = (emit == Py_True);
}
if (rendered != Py_None &&
visuNodeSet_visibility(node, (rendered == Py_True)) && emit == Py_True)
visuDataEmit_nodeRenderedChange(self->parent->obj);
if (emitPos)
visuDataEmit_nodePositionChanged(self->parent->obj);
Py_INCREF(Py_None);
return Py_None;
}
static PyObject* visuNodePy_isValid(PyObject *obj)
{
VisuNodePy *self;
VisuNode *node;
self = (VisuNodePy*)obj;
node = visuDataGet_nodeFromNumber(self->parent->obj, self->nodeId);
if (node)
{
Py_INCREF(Py_True);
return Py_True;
}
else
{
Py_INCREF(Py_False);
return Py_False;
}
}
static void _onAskForHide(VisuData *visuData, gboolean *redraw, VisuDataPy *self);
static void _onNodePositionChanged(VisuData *dataObj, VisuDataPy *self);
static int visuDataPy_init(VisuDataPy *self, PyObject *args _U_, PyObject *kwds _U_)
{
self->planes = (PyObject*)0;
self->planesId = 0;
return 0;
}
static PyObject* visuDataPy_new(PyTypeObject *type, PyObject *args, PyObject *kwds _U_)
{
VisuDataPy *self;
PyObject *parentPy = NULL;
self = (VisuDataPy*)type->tp_alloc(type, 0);
if (self != NULL)
DBG_fprintf(stderr, "Visu Python: new VisuData object %p.\n", self);
if (!PyArg_ParseTuple(args, "|O!", &VisuDataPyType, &parentPy))
return NULL;
if (parentPy)
self->obj = visuDataNew_withVisuOpenGLView(visuDataGet_openGLView(((VisuDataPy*)parentPy)->obj));
else
self->obj = visuDataNew();
g_object_ref(G_OBJECT(self->obj));
g_object_set_data(G_OBJECT(self->obj), "selfPython", self);
g_signal_connect(G_OBJECT(self->obj), "NodeAskForShowHide",
G_CALLBACK(_onAskForHide), self);
g_signal_connect(G_OBJECT(self->obj), "NodePositionChanged",
G_CALLBACK(_onNodePositionChanged), self);
return (PyObject *)self;
}
static void visuDataPy_free(VisuDataPy* self)
{
DBG_fprintf(stderr, "Visu Python: deallocate a VisuData object %p.\n", self);
g_object_unref(G_OBJECT(self->obj));
self->ob_type->tp_free((PyObject*)self);
}
static PyObject* visuDataPy_addFile(PyObject *self, PyObject *args, PyObject *kwds)
{
char *filename;
int type;
static char *kwlist[] = {"filename", "type", NULL};
if (! PyArg_ParseTupleAndKeywords(args, kwds, "si", kwlist,
&filename, &type))
return NULL;
visuDataAdd_file(((VisuDataPy*)self)->obj, filename, type, (ToolFileFormat*)0);
Py_INCREF(Py_None);
return Py_None;
}
static PyObject* visuDataPy_getPopulation(PyObject *self)
{
PyObject* dict, *nb;
const char *ele;
VisuNodeArray *nodes;
guint i;
dict = PyDict_New();
if (!dict)
return NULL;
nodes = visu_data_getNodeArray(((VisuDataPy*)self)->obj);
if (nodes)
for (i = 0; i < nodes->ntype; i++)
{
nb = PyInt_FromLong((long)nodes->numberOfStoredNodes[i]);
ele = ((VisuDataPy*)self)->obj->fromIntToVisuElement[i]->name;
PyDict_SetItemString(dict, ele, nb);
}
Py_INCREF(dict);
return dict;
}
static PyObject* visuDataPy_setPopulation(PyObject *self, PyObject *args, PyObject *kwds)
{
static char *kwlist[] = {"population", "iSet", NULL};
unsigned int ntypes, i, iSet;
VisuElement **visuElementUsed;
unsigned int *nbOfNodesPerVisuElement;
PyObject *dict, *key, *value;
#if PY_MINOR_VERSION > 4
Py_ssize_t pos = 0;
#else
int pos = 0;
#endif
iSet = 0;
if (! PyArg_ParseTupleAndKeywords(args, kwds, "O!|I", kwlist,
&PyDict_Type, &dict, &iSet))
return NULL;
ntypes = (unsigned int)PyDict_Size(dict);
if (ntypes == 0)
{
PyErr_SetString(PyExc_ValueError, "dictionary of elements must not be empty.");
return NULL;
}
visuElementUsed = g_malloc(sizeof(VisuElement*) * ntypes);
nbOfNodesPerVisuElement = g_malloc(sizeof(unsigned int) * ntypes);
visuDataFree_population(((VisuDataPy*)self)->obj);
pos = 0;
i = 0;
while (PyDict_Next(dict, &pos, &key, &value))
{
if (! PyObject_TypeCheck(key, &VisuElementPyType))
{
PyErr_SetString(PyExc_TypeError, "keys must be VisuElementPy.");
g_free(visuElementUsed);
g_free(nbOfNodesPerVisuElement);
return NULL;
}
if (! PyInt_Check(value) || ((PyIntObject*)value)->ob_ival <= 0)
{
PyErr_SetString(PyExc_TypeError, "values must be positive integer.");
g_free(visuElementUsed);
g_free(nbOfNodesPerVisuElement);
return NULL;
}
Py_INCREF(key);
visuElementUsed[i] = ((VisuElementPy*)key)->obj;
nbOfNodesPerVisuElement[i] = (int) ((PyIntObject*)value)->ob_ival;
i += 1;
}
if (i != ntypes)
{
PyErr_SetString(PyExc_RuntimeError, "can't browse the population dictionnary.");
g_free(visuElementUsed);
g_free(nbOfNodesPerVisuElement);
return NULL;
}
visu_data_setPopulation(((VisuDataPy*)self)->obj, ntypes,
nbOfNodesPerVisuElement, visuElementUsed);
visuDataSet_setId(((VisuDataPy*)self)->obj, iSet);
g_free(visuElementUsed);
g_free(nbOfNodesPerVisuElement);
Py_INCREF(Py_None);
return Py_None;
}
static PyObject* visuDataPy_createAllNodes(PyObject *self)
{
visuData_createAllNodes(((VisuDataPy*)self)->obj);
Py_INCREF(Py_None);
return Py_None;
}
static PyObject* visuDataPy_setBox(PyObject *self, PyObject *args, PyObject *kwds)
{
static char *kwlist[] = {"box", "periodic", "scale", NULL};
float box[6], size;
double rprimd[3][3];
PyObject *bool, *boxPy;
size = 0.f;
if (! PyArg_ParseTupleAndKeywords(args, kwds, "O!|O!f", kwlist,
&PyTuple_Type, &boxPy, &PyBool_Type, &bool, &size))
return NULL;
if (PyTuple_Size(boxPy) == 6 &&
! PyArg_ParseTuple(boxPy, "ffffff", box, box + 1, box + 2,
box + 3, box + 4, box + 5))
return NULL;
if (PyTuple_Size(boxPy) == 3 &&
! PyArg_ParseTuple(boxPy, "(ddd)(ddd)(ddd)",
rprimd[0], rprimd[0] + 1, rprimd[0] + 2,
rprimd[1], rprimd[1] + 1, rprimd[1] + 2,
rprimd[2], rprimd[2] + 1, rprimd[2] + 2))
return NULL;
if (PyTuple_Size(boxPy) == 3)
tool_matrix_reducePrimitiveVectors(box, rprimd);
visu_data_setBoxGeometry(((VisuDataPy*)self)->obj, box, (bool == Py_True));
visu_data_applyBoxGeometry(((VisuDataPy*)self)->obj);
Py_INCREF(Py_None);
return Py_None;
}
static PyObject* visuDataPy_addNode(PyObject *self, PyObject *args, PyObject *kwds)
{
float xyz[3];
static char *kwlist[] = {"element", "coord", "emit", NULL};
PyObject *ele, *emitPy = Py_False, *xyzPy;
VisuNode *node;
VisuNodePy *nodePy;
if (! PyArg_ParseTupleAndKeywords(args, kwds, "O!O!|O!", kwlist,
&VisuElementPyType, &ele,
&PyTuple_Type, &xyzPy,
&PyBool_Type, &emitPy))
return NULL;
if (! PyArg_ParseTuple(xyzPy, "fff", xyz, xyz + 1, xyz + 2))
return NULL;
node = visu_data_addNodeFromElement(((VisuDataPy*)self)->obj,
((VisuElementPy*)ele)->obj,
xyz, (emitPy == Py_True));
if (!node)
{
PyErr_SetString(PyExc_ValueError, "can't add new node.");
return NULL;
}
DBG_fprintf(stderr, "Visu Python: create a new object for node %d %p.\n",
node->number, node);
nodePy = (VisuNodePy*)PyObject_New(VisuNodePy, &VisuNodePyType);
if (nodePy == NULL)
return NULL;
Py_INCREF(self);
nodePy->nodeId = node->number;
nodePy->parent = (VisuDataPy*)self;
Py_INCREF(nodePy);
return (PyObject *)nodePy;
}
static PyObject* visuDataPy_delNodes(PyObject *self, PyObject *args)
{
PyObject *nodesPy, *nodePy;
int *nodes, n, i;
if (! PyArg_ParseTuple(args, "O", &nodesPy))
return NULL;
nodes = (int*)0;
if (PyObject_TypeCheck(nodesPy, &PyInt_Type))
{
nodes = g_malloc(sizeof(int) * 2);
nodes[0] = (int) ((PyIntObject*)nodesPy)->ob_ival;
if (nodes[0] < 0)
{
PyErr_SetString(PyExc_ValueError, "Only positive numbers identify nodes.");
g_free(nodes);
return NULL;
}
nodes[1] = -1;
}
else if (PyObject_TypeCheck(nodesPy, &VisuNodePyType))
{
nodes = g_malloc(sizeof(int) * 2);
nodes[0] = ((VisuNodePy*)nodesPy)->nodeId;
nodes[1] = -1;
}
else if (PyObject_TypeCheck(nodesPy, &PyTuple_Type))
{
n = PyTuple_GET_SIZE(nodesPy);
nodes = g_malloc(sizeof(int) * (n + 1));
for (i = 0; i < n; i++)
{
nodePy = PyTuple_GET_ITEM(nodesPy, i);
if (! PyObject_TypeCheck(nodePy, &PyInt_Type) &&
! PyObject_TypeCheck(nodePy, &VisuNodePyType))
{
PyErr_SetString(PyExc_TypeError,
"Arguments should be integers or VisuNodePy.");
g_free(nodes);
return NULL;
}
if (PyObject_TypeCheck(nodePy, &PyInt_Type))
{
nodes[i] = (int) ((PyIntObject*)nodePy)->ob_ival;
if (nodes[i] < 0)
{
PyErr_SetString(PyExc_ValueError,
"Only positive numbers identify nodes.");
g_free(nodes);
return NULL;
}
}
else
nodes[i] = ((VisuNodePy*)nodePy)->nodeId;
}
nodes[n] = -1;
}
else
{
PyErr_SetString(PyExc_TypeError,
"Argument must be tuple, integer or VisuNodePy.");
return NULL;
}
visuDataRemove_nodes(((VisuDataPy*)self)->obj, nodes);
g_free(nodes);
Py_INCREF(Py_None);
return Py_None;
}
static Plane** _buildArrayOfPlanes(VisuDataPy *self)
{
int i, n;
Plane **planes;
n = PyTuple_GET_SIZE(self->planes);
planes = g_malloc(sizeof(Plane*) * (n + 1));
for (i = 0; i < n; i++)
planes[i] = ((PlanePy*)PyTuple_GET_ITEM(self->planes, i))->obj;
planes[n] = (Plane*)0;
return planes;
}
static void _rebuildPlanes(VisuData *dataObj)
{
Plane **planes;
VisuDataPy *self;
self = (VisuDataPy*)g_object_get_data(G_OBJECT(dataObj), "selfPython");
if (!self)
return;
planes = _buildArrayOfPlanes(self);
DBG_fprintf(stderr, "Visu Python: create OpenGL list for planes.\n");
planesDraw_list(planes, self->planesId);
DBG_fprintf(stderr, " | OK\n");
g_free(planes);
}
static void _onPlaneMoved(Plane *plane _U_, VisuDataPy *self)
{
_rebuildPlanes(self->obj);
}
static void _onAskForHide(VisuData *visuData, gboolean *redraw, VisuDataPy *self)
{
Plane **planes;
if (!self->planes)
return;
DBG_fprintf(stderr, "Visu Python: caught the 'NodeAskForShowHide' signal for"
" VisuData %p.\n", (gpointer)visuData);
planes = _buildArrayOfPlanes(self);
*redraw = planeShowHide_all(visuData, planes) || *redraw;
g_free(planes);
}
static void _onNodePositionChanged(VisuData *dataObj, VisuDataPy *self)
{
gboolean redraw;
DBG_fprintf(stderr, "Visu Python: caught the 'NodePositionChanged' signal,"
" recalculating masking properties.\n");
if (self->planes)
{
visuDataEmit_askForShowHideNodes(dataObj, &redraw);
if (redraw)
visuDataEmit_nodeRenderedChange(dataObj);
}
}
static PyObject* visuDataPy_setPlanes(PyObject *obj, PyObject *planes)
{
VisuDataPy *self = (VisuDataPy*)obj;
int i;
PyObject *plane;
float vertices[8][3];
Plane *planeObj;
VisuExtension *planeExt;
if (!PyObject_TypeCheck(planes, &PyTuple_Type))
{
PyErr_SetString(PyExc_TypeError,
"Argument must be tuple of v_sim.plane.");
return NULL;
}
for (i = 0; i < PyTuple_GET_SIZE(planes); i++)
{
plane = PyTuple_GET_ITEM(planes, i);
if (! PyObject_TypeCheck(plane, PLANEPY_TYPE))
{
PyErr_SetString(PyExc_TypeError,
"Tuple argument must contain v_sim.plane objects.");
return NULL;
}
}
Py_XDECREF(self->planes);
Py_INCREF(planes);
self->planes = planes;
/* We compute the plane intersections with this VisuData box,
and connect the change signals. */
visuDataGet_boxVertices(self->obj, vertices, TRUE);
for (i = 0; i < PyTuple_GET_SIZE(self->planes); i++)
{
planeObj = ((PlanePy*)PyTuple_GET_ITEM(self->planes, i))->obj;
g_signal_connect(G_OBJECT(planeObj), "moved",
G_CALLBACK(_onPlaneMoved), (gpointer)self);
planeSet_box(planeObj, vertices);
}
/* We create an OpenGL extension to draw the planes if not already
done. */
if (!self->planesId)
{
self->planesId = visu_openGL_objectList_new(1);
planeExt = visu_extension_new("Planes", "Planes", (char*)0,
self->planesId, _rebuildPlanes);
planeExt->used = 1;
visu_extension_setSensitiveToRenderingMode(planeExt, TRUE);
visu_extension_setPriority(planeExt, VISU_EXTENSION_PRIORITY_LOW + 1);
visuExtensions_add(planeExt);
}
_rebuildPlanes(self->obj);
Py_INCREF(Py_None);
return Py_None;
}
static int width, height;
static void _createMain(GtkWindow **panel, GtkWindow **render)
{
GtkWidget *renderingWindow;
/* Force the creation of the Scale class. */
DBG_fprintf(stderr,"Visu Python: create a rendering window (%dx%d).\n",
width, height);
renderingWindow =
visu_rendering_window_new(width, height, TRUE, FALSE);
visuVisuRenderingWindowSet_default(renderingWindow);
*panel = (GtkWindow*)0;
*render = (GtkWindow*)0;
return;
}
static gboolean _parseFiles(gpointer data _U_)
{
GString *message;
message = visu_basic_parseConfigFiles();
if (message)
{
g_string_free(message, TRUE);
}
}
static PyObject* visuPyMain(PyObject *self _U_, PyObject *args, PyObject *kwds)
{
PyObject *me;
static char *kwlist[] = {"width", "height", NULL};
GtkWidget *renderingWindow;
width = 600;
height = 600;
/* Try to get the width and the height. */
if (! PyArg_ParseTupleAndKeywords(args, kwds, "|ii", kwlist,
&width, &height))
return NULL;
/* Force the creation of the Scale class. */
DBG_fprintf(stderr,"Visu Python: create a rendering window (%dx%d).\n",
width, height);
renderingWindow =
visu_rendering_window_new(width, height, TRUE, FALSE);
visuVisuRenderingWindowSet_default(renderingWindow);
g_idle_add(_parseFiles, (gpointer)0);
me = pygobject_new(G_OBJECT(renderingWindow));
Py_INCREF(me);
return me;
}
static PyObject* visuPyLoad(PyObject *self _U_, PyObject *args, PyObject *kwds)
{
PyObject *obj;
int iSet;
PyObject *bool;
static char *kwlist[] = {"data", "iSet", "internal", NULL};
GError *error;
gboolean res;
/* Try to get the width and the height. */
iSet = 0;
if (! PyArg_ParseTupleAndKeywords(args, kwds, "O!|iO!", kwlist,
&VisuDataPyType, &obj, &iSet, &PyBool_Type, &bool))
return NULL;
DBG_fprintf(stderr,"Visu Python: load VisuData from %p (%p).\n",
obj, ((VisuDataPy*)obj)->obj);
if ((bool == Py_False))
visuGtkLoad_file(((VisuDataPy*)obj)->obj, iSet);
else
{
error = (GError*)0;
res = visu_basic_loadData(((VisuDataPy*)obj)->obj,
(ToolFileFormat*)0, iSet, &error);
if (error)
{
PyErr_SetString(PyExc_TypeError, error->message);
g_error_free(error);
return NULL;
}
visuVisuRenderingWindowSet_visuData
(RENDERING_WINDOW(visuVisuRenderingWindowGet_current()), ((VisuDataPy*)obj)->obj);
/* We release a ref on obj, since
visu_rendering_window_setData has increased it. */
g_object_unref(G_OBJECT(((VisuDataPy*)obj)->obj));
visuData_createAllNodes(((VisuDataPy*)obj)->obj);
}
Py_INCREF(Py_None);
return Py_None;
}
static PyObject* visuPySet(PyObject *self _U_, PyObject *data)
{
if (! PyObject_TypeCheck(data, &VisuDataPyType) && data != Py_None)
{
PyErr_SetString(PyExc_TypeError, "argument must be of type VisuDataPy or None.");
return NULL;
}
if (data != Py_None)
{
visu_rendering_window_setData(RENDERING_WINDOW(visuVisuRenderingWindowGet_current()),
((VisuDataPy*)data)->obj);
rebuildAllExtensionsLists(((VisuDataPy*)data)->obj);
}
else
visu_rendering_window_setData(RENDERING_WINDOW(visuVisuRenderingWindowGet_current()),
(VisuData*)0);
g_idle_add(visu_object_redraw, (gpointer)0);
Py_INCREF(Py_None);
return Py_None;
}
static PyObject* visuPyRedraw(PyObject *self _U_)
{
g_idle_add(visu_object_redraw, (gpointer)0);
Py_INCREF(Py_None);
return Py_None;
}
/* We define here all the methods of the module. */
static PyMethodDef ModuleMethods[] =
{
{"main", (PyCFunction)visuPyMain, METH_VARARGS | METH_KEYWORDS,
"Start a V_Sim run and open an empty rendering window."},
{"load", (PyCFunction)visuPyLoad, METH_VARARGS | METH_KEYWORDS,
"Load the given data."},
{"set", (PyCFunction)visuPySet, METH_O,
"Associate a VisuData to a rendering area."},
{"redraw", (PyCFunction)visuPyRedraw, METH_NOARGS,
"Redraw the rendering area."},
{NULL, NULL, 0, NULL}
};
/* The name initv_sim is mandatory, do not change it! */
PyMODINIT_FUNC initv_sim(void)
{
PyObject *module;
if (PyType_Ready(&VisuDataPyType) < 0)
return;
if (PyType_Ready(&VisuElementPyType) < 0)
return;
if (PyType_Ready(&VisuNodePyType) < 0)
return;
if (PyType_Ready(PLANEPY_TYPE) < 0)
return;
module = Py_InitModule("v_sim", ModuleMethods);
if (module == NULL)
return;
Py_INCREF(&VisuDataPyType);
PyModule_AddObject(module, "data", (PyObject*)&VisuDataPyType);
Py_INCREF(&VisuElementPyType);
PyModule_AddObject(module, "element", (PyObject*)&VisuElementPyType);
Py_INCREF(&VisuNodePyType);
PyModule_AddObject(module, "node", (PyObject*)&VisuNodePyType);
Py_INCREF(PLANEPY_TYPE);
PyModule_AddObject(module, "plane", (PyObject*)PLANEPY_TYPE);
init_pygobject();
init_pygtk();
/* Set the static paths for V_Sim. */
visuBasicSet_paths((const gchar*)0);
initVisuBasic();
}
|