NAMD
Macros | Functions
TclVec.C File Reference
#include <tcl.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

Go to the source code of this file.

Macros

#define VMD_PI   3.14159265358979323846
 
#define VMD_TWOPI   (2.0 * VMD_PI)
 
#define DEGTORAD(a)   (a*VMD_PI/180.0)
 
#define RADTODEG(a)   (a*180.0/VMD_PI)
 

Functions

static int obj_vecadd (ClientData, Tcl_Interp *interp, int argc, Tcl_Obj *const objv[])
 
static int obj_vecsub (ClientData, Tcl_Interp *interp, int argc, Tcl_Obj *const objv[])
 
static int obj_vecscale (ClientData, Tcl_Interp *interp, int argc, Tcl_Obj *const objv[])
 
int tcl_get_matrix (const char *fctn, Tcl_Interp *interp, Tcl_Obj *s, double *mat)
 Given a string with a matrix in it, return the matrix. More...
 
void tcl_append_matrix (Tcl_Interp *interp, const double *mat)
 
static int obj_vectrans (ClientData, Tcl_Interp *interp, int argc, Tcl_Obj *const objv[])
 
static int obj_transmult (ClientData, Tcl_Interp *interp, int argc, Tcl_Obj *const objv[])
 
static int obj_transvec (ClientData, Tcl_Interp *interp, int argc, Tcl_Obj *const objv[])
 
static int obj_transvecinv (ClientData, Tcl_Interp *interp, int argc, Tcl_Obj *const objv[])
 
static int obj_transabout (ClientData, Tcl_Interp *interp, int argc, Tcl_Obj *const objv[])
 
static int obj_veclength (ClientData, Tcl_Interp *interp, int argc, Tcl_Obj *const objv[])
 
static double * obj_getdoublearray (Tcl_Interp *interp, Tcl_Obj *const objv[], int *len)
 
static int obj_vecsum (ClientData, Tcl_Interp *interp, int argc, Tcl_Obj *const objv[])
 
static int obj_vecmean (ClientData, Tcl_Interp *interp, int argc, Tcl_Obj *const objv[])
 
static int obj_vecstddev (ClientData, Tcl_Interp *interp, int argc, Tcl_Obj *const objv[])
 
int Vec_Init (Tcl_Interp *interp)
 

Macro Definition Documentation

#define DEGTORAD (   a)    (a*VMD_PI/180.0)

Definition at line 34 of file TclVec.C.

Referenced by obj_transabout().

#define RADTODEG (   a)    (a*180.0/VMD_PI)

Definition at line 35 of file TclVec.C.

#define VMD_PI   3.14159265358979323846

Definition at line 32 of file TclVec.C.

Referenced by obj_transabout().

#define VMD_TWOPI   (2.0 * VMD_PI)

Definition at line 33 of file TclVec.C.

Function Documentation

static double* obj_getdoublearray ( Tcl_Interp *  interp,
Tcl_Obj *const  objv[],
int *  len 
)
static

Definition at line 537 of file TclVec.C.

Referenced by obj_vecmean(), obj_vecstddev(), and obj_vecsum().

537  {
538  int num;
539 
540  Tcl_Obj **data;
541  if (Tcl_ListObjGetElements(interp, objv[1], &num, &data) != TCL_OK)
542  return NULL;
543 
544  double *list = (double*) malloc(num*sizeof(double));
545  if (list == NULL)
546  return NULL;
547 
548  for (int i=0; i<num; i++) {
549  double tmp;
550  if (Tcl_GetDoubleFromObj(interp, data[i], &tmp) != TCL_OK) {
551  Tcl_SetResult(interp, (char *) "veclength: non-numeric in vector", TCL_STATIC);
552  free(list);
553  return NULL;
554  }
555  list[i] = tmp;
556  }
557 
558  *len = num;
559 
560  return list;
561 }
static int obj_transabout ( ClientData  ,
Tcl_Interp *  interp,
int  argc,
Tcl_Obj *const  objv[] 
)
static

Definition at line 448 of file TclVec.C.

References DEGTORAD, Matrix4::mat, Matrix4::rotate_axis(), tcl_append_matrix(), VMD_PI, x, y, and z.

Referenced by Vec_Init().

449  {
450  if (argc != 3 && argc != 4) {
451  Tcl_WrongNumArgs(interp, 1, objv, (char *)"axis amount [deg|rad|pi]");
452  return TCL_ERROR;
453  }
454 
455  int num;
456  Tcl_Obj **data;
457  // get the axis
458  if (Tcl_ListObjGetElements(interp, objv[1], &num, &data) != TCL_OK)
459  return TCL_ERROR;
460  if (num != 3) {
461  Tcl_AppendResult(interp, "transabout: vector must have three elements",NULL);
462  return TCL_ERROR;
463  }
464  double x,y,z;
465  if (Tcl_GetDoubleFromObj(interp, data[0], &x) != TCL_OK ||
466  Tcl_GetDoubleFromObj(interp, data[1], &y) != TCL_OK ||
467  Tcl_GetDoubleFromObj(interp, data[2], &z) != TCL_OK) {
468  Tcl_SetResult(interp, (char *)"transabout: non-numeric in vector", TCL_STATIC);
469  return TCL_ERROR;
470  }
471 
472  // get the amount
473  double amount;
474  if (Tcl_GetDoubleFromObj(interp, objv[2], &amount) != TCL_OK) {
475  Tcl_SetResult(interp, (char *)"transabout: non-numeric angle", TCL_STATIC);
476  return TCL_ERROR;
477  }
478 
479  // get units
480  if (argc == 4) {
481  if (!strcmp(Tcl_GetStringFromObj(objv[3], NULL), "deg")) {
482  amount = DEGTORAD(amount);
483  } else if (!strcmp(Tcl_GetStringFromObj(objv[3], NULL), "rad")) {
484  // amount = amount;
485  } else if (!strcmp(Tcl_GetStringFromObj(objv[3], NULL), "pi")) {
486  amount = amount*VMD_PI;
487  } else {
488  Tcl_AppendResult(interp, "transabout: unit must be deg|rad|pi",NULL);
489  return TCL_ERROR;
490  }
491  } else {
492  // If no unit was specified assume that we have degrees
493  amount = DEGTORAD(amount);
494  }
495 
496  double axis[3];
497  axis[0] = (double) x;
498  axis[1] = (double) y;
499  axis[2] = (double) z;
500 
501  Matrix4 mat;
502  mat.rotate_axis(axis, amount);
503  tcl_append_matrix(interp, mat.mat);
504  return TCL_OK;
505 }
void tcl_append_matrix(Tcl_Interp *interp, const double *mat)
Definition: TclVec.C:274
#define DEGTORAD(a)
Definition: TclVec.C:34
4x4 matrix class with numerous operators, conversions, etc.
Definition: Matrix4.h:26
gridSize z
#define VMD_PI
Definition: TclVec.C:32
void rotate_axis(const double axis[3], double angle)
apply a rotation around the given vector; angle in radians.
Definition: Matrix4.C:245
double mat[16]
the matrix itself
Definition: Matrix4.h:33
gridSize y
gridSize x
static int obj_transmult ( ClientData  ,
Tcl_Interp *  interp,
int  argc,
Tcl_Obj *const  objv[] 
)
static

Definition at line 351 of file TclVec.C.

References tcl_append_matrix(), and tcl_get_matrix().

Referenced by Vec_Init().

352  {
353  // make there there are at least two values
354  if (argc < 3) {
355  Tcl_WrongNumArgs(interp, 1, objv, (char *)"mx my ?m1? ?m2? ...");
356  return TCL_ERROR;
357  }
358  // Get the first matrix
359  double mult[16];
360  if (tcl_get_matrix("transmult: ", interp, objv[1], mult) != TCL_OK) {
361  return TCL_ERROR;
362  }
363  int i = 2;
364  double pre[16];
365  while (i < argc) {
366  if (tcl_get_matrix("transmult: ", interp, objv[i], pre) != TCL_OK) {
367  return TCL_ERROR;
368  }
369  // premultiply mult by tmp
370  double tmp[4];
371  for (int k=0; k<4; k++) {
372  tmp[0] = mult[k];
373  tmp[1] = mult[4+k];
374  tmp[2] = mult[8+k];
375  tmp[3] = mult[12+k];
376  for (int j=0; j<4; j++) {
377  mult[4*j+k] = pre[4*j]*tmp[0] + pre[4*j+1]*tmp[1] +
378  pre[4*j+2]*tmp[2] + pre[4*j+3]*tmp[3];
379  }
380  }
381  i++;
382  }
383  tcl_append_matrix(interp, mult);
384  return TCL_OK;
385 }
void tcl_append_matrix(Tcl_Interp *interp, const double *mat)
Definition: TclVec.C:274
int tcl_get_matrix(const char *fctn, Tcl_Interp *interp, Tcl_Obj *s, double *mat)
Given a string with a matrix in it, return the matrix.
Definition: TclVec.C:197
static int obj_transvec ( ClientData  ,
Tcl_Interp *  interp,
int  argc,
Tcl_Obj *const  objv[] 
)
static

Definition at line 387 of file TclVec.C.

References Matrix4::mat, tcl_append_matrix(), Matrix4::transvec(), x, y, and z.

Referenced by Vec_Init().

388  {
389  if (argc != 2) {
390  Tcl_WrongNumArgs(interp, 1, objv, (char *)"?vector?");
391  return TCL_ERROR;
392  }
393 
394  int num;
395  Tcl_Obj **data;
396  if (Tcl_ListObjGetElements(interp, objv[1], &num, &data) != TCL_OK)
397  return TCL_ERROR;
398  if (num != 3) {
399  Tcl_AppendResult(interp, "transvec: vector must have three elements",NULL);
400  return TCL_ERROR;
401  }
402  double x,y,z;
403  if (Tcl_GetDoubleFromObj(interp, data[0], &x) != TCL_OK ||
404  Tcl_GetDoubleFromObj(interp, data[1], &y) != TCL_OK ||
405  Tcl_GetDoubleFromObj(interp, data[2], &z) != TCL_OK) {
406  Tcl_SetResult(interp, (char *)"transvec: non-numeric in vector", TCL_STATIC);
407  return TCL_ERROR;
408  }
409  Matrix4 mat;
410  mat.transvec((double) x,(double) y,(double) z);
411  tcl_append_matrix(interp, mat.mat);
412  return TCL_OK;
413 }
void tcl_append_matrix(Tcl_Interp *interp, const double *mat)
Definition: TclVec.C:274
4x4 matrix class with numerous operators, conversions, etc.
Definition: Matrix4.h:26
gridSize z
double mat[16]
the matrix itself
Definition: Matrix4.h:33
void transvec(double x, double y, double z)
apply a rotation such that &#39;x&#39; is brought along the given vector.
Definition: Matrix4.C:252
gridSize y
gridSize x
static int obj_transvecinv ( ClientData  ,
Tcl_Interp *  interp,
int  argc,
Tcl_Obj *const  objv[] 
)
static

Definition at line 415 of file TclVec.C.

References Matrix4::mat, tcl_append_matrix(), Matrix4::transvecinv(), x, y, and z.

Referenced by Vec_Init().

416  {
417  if (argc != 2) {
418  Tcl_WrongNumArgs(interp, 1, objv, (char *)"?vector?");
419  return TCL_ERROR;
420  }
421 
422  int num;
423  Tcl_Obj **data;
424  if (Tcl_ListObjGetElements(interp, objv[1], &num, &data) != TCL_OK)
425  return TCL_ERROR;
426  if (num != 3) {
427  Tcl_AppendResult(interp, "transvecinv: vector must have three elements",NULL);
428  return TCL_ERROR;
429  }
430  double x,y,z;
431  if (Tcl_GetDoubleFromObj(interp, data[0], &x) != TCL_OK ||
432  Tcl_GetDoubleFromObj(interp, data[1], &y) != TCL_OK ||
433  Tcl_GetDoubleFromObj(interp, data[2], &z) != TCL_OK) {
434  Tcl_SetResult(interp, (char *)"transvecinv: non-numeric in vector", TCL_STATIC);
435  return TCL_ERROR;
436  }
437  Matrix4 mat;
438  mat.transvecinv((double) x,(double) y,(double) z);
439  tcl_append_matrix(interp, mat.mat);
440  return TCL_OK;
441 }
void transvecinv(double x, double y, double z)
apply a rotation such that the given vector is brought along &#39;x&#39;.
Definition: Matrix4.C:261
void tcl_append_matrix(Tcl_Interp *interp, const double *mat)
Definition: TclVec.C:274
4x4 matrix class with numerous operators, conversions, etc.
Definition: Matrix4.h:26
gridSize z
double mat[16]
the matrix itself
Definition: Matrix4.h:33
gridSize y
gridSize x
static int obj_vecadd ( ClientData  ,
Tcl_Interp *  interp,
int  argc,
Tcl_Obj *const  objv[] 
)
static

Definition at line 44 of file TclVec.C.

Referenced by Vec_Init().

45  {
46  if (argc < 3) {
47  Tcl_WrongNumArgs(interp, 1, objv, (char *)"vec1 vec2 ?vec3? ?vec4? ...");
48  return TCL_ERROR;
49  }
50  int num;
51  Tcl_Obj **data;
52  if (Tcl_ListObjGetElements(interp, objv[1], &num, &data) != TCL_OK) {
53  return TCL_ERROR;
54  }
55  double *sum = new double[num];
56  int i;
57  for (i=0; i<num; i++) {
58  if (Tcl_GetDoubleFromObj(interp, data[i], sum+i) != TCL_OK) {
59  delete [] sum;
60  return TCL_ERROR;
61  }
62  }
63  // do the sums on the rest
64  int num2;
65  for (int term=2; term < argc; term++) {
66  if (Tcl_ListObjGetElements(interp, objv[term], &num2, &data) != TCL_OK) {
67  delete [] sum;
68  return TCL_ERROR;
69  }
70  if (num != num2) {
71  Tcl_SetResult(interp, (char *) "vecadd: two vectors don't have the same size", TCL_STATIC);
72  delete [] sum;
73  return TCL_ERROR;
74  }
75  for (i=0; i<num; i++) {
76  double df;
77  if (Tcl_GetDoubleFromObj(interp, data[i], &df) != TCL_OK) {
78  delete [] sum;
79  return TCL_ERROR;
80  }
81  sum[i] += df;
82  }
83  }
84 
85 
86  // and return the result
87  Tcl_Obj *tcl_result = Tcl_NewListObj(0, NULL);
88  for (i=0; i<num; i++) {
89  Tcl_ListObjAppendElement(interp, tcl_result, Tcl_NewDoubleObj(sum[i]));
90  }
91  Tcl_SetObjResult(interp, tcl_result);
92  delete [] sum;
93  return TCL_OK;
94 }
static int obj_veclength ( ClientData  ,
Tcl_Interp *  interp,
int  argc,
Tcl_Obj *const  objv[] 
)
static

Definition at line 507 of file TclVec.C.

Referenced by Vec_Init().

507  {
508 
509  if (argc != 2) {
510  Tcl_WrongNumArgs(interp, 1, objv, (char *)"?vector?");
511  return TCL_ERROR;
512  }
513 
514  int num;
515  Tcl_Obj **data;
516  if (Tcl_ListObjGetElements(interp, objv[1], &num, &data) != TCL_OK)
517  return TCL_ERROR;
518 
519  double length = 0.;
520  for (int i=0; i<num; i++) {
521  double tmp;
522  if (Tcl_GetDoubleFromObj(interp, data[i], &tmp) != TCL_OK) {
523  Tcl_SetResult(interp, (char *) "veclength: non-numeric in vector", TCL_STATIC);
524  return TCL_ERROR;
525  } else {
526  length += tmp*tmp;
527  }
528  }
529 
530  length = sqrt(length);
531  Tcl_Obj *tcl_result = Tcl_GetObjResult(interp);
532  Tcl_SetDoubleObj(tcl_result, length);
533  return TCL_OK;
534 }
static int obj_vecmean ( ClientData  ,
Tcl_Interp *  interp,
int  argc,
Tcl_Obj *const  objv[] 
)
static

Definition at line 587 of file TclVec.C.

References obj_getdoublearray().

Referenced by Vec_Init().

587  {
588  if (argc != 2) {
589  Tcl_WrongNumArgs(interp, 1, objv, (char *)"?vector?");
590  return TCL_ERROR;
591  }
592 
593  int num;
594  double *list = obj_getdoublearray(interp, objv, &num);
595  if (list == NULL)
596  return TCL_ERROR;
597 
598  double sum = 0.;
599  for (int i=0; i<num; i++) {
600  sum += list[i];
601  }
602  sum /= (double) num;
603  free(list);
604 
605  Tcl_Obj *tcl_result = Tcl_GetObjResult(interp);
606  Tcl_SetDoubleObj(tcl_result, sum);
607  return TCL_OK;
608 }
static double * obj_getdoublearray(Tcl_Interp *interp, Tcl_Obj *const objv[], int *len)
Definition: TclVec.C:537
static int obj_vecscale ( ClientData  ,
Tcl_Interp *  interp,
int  argc,
Tcl_Obj *const  objv[] 
)
static

Definition at line 139 of file TclVec.C.

Referenced by Vec_Init().

140  {
141  if (argc != 3) {
142  Tcl_WrongNumArgs(interp, 1, objv, (char *)"?c? ?v?");
143  return TCL_ERROR;
144  }
145 
146  int num1, num2;
147  Tcl_Obj **data1, **data2;
148  if (Tcl_ListObjGetElements(interp, objv[1], &num1, &data1) != TCL_OK) {
149  return TCL_ERROR;
150  }
151  if (Tcl_ListObjGetElements(interp, objv[2], &num2, &data2) != TCL_OK) {
152  return TCL_ERROR;
153  }
154  if (num1 == 0 || num2 == 0) {
155  Tcl_SetResult(interp, (char *) "vecscale: parameters must have data", TCL_STATIC);
156  return TCL_ERROR;
157  } else if (num1 != 1 && num2 != 1) {
158  Tcl_SetResult(interp, (char *) "vecscale: one parameter must be a scalar value", TCL_STATIC);
159  return TCL_ERROR;
160  }
161 
162  int num;
163  Tcl_Obj *scalarobj, **vector;
164  if (num1 == 1) {
165  scalarobj = data1[0];
166  vector = data2;
167  num = num2;
168  } else {
169  scalarobj = data2[0];
170  vector = data1;
171  num = num1;
172  }
173 
174  double scalar;
175  if (Tcl_GetDoubleFromObj(interp, scalarobj, &scalar) != TCL_OK)
176  return TCL_ERROR;
177 
178  Tcl_Obj *tcl_result = Tcl_NewListObj(0, NULL);
179  for (int i=0; i<num; i++) {
180  double val;
181  if (Tcl_GetDoubleFromObj(interp, vector[i], &val) != TCL_OK) {
182  Tcl_SetResult(interp, (char *) "vecscale: non-numeric in vector", TCL_STATIC);
183  return TCL_ERROR;
184  }
185  val *= scalar;
186  Tcl_ListObjAppendElement(interp, tcl_result, Tcl_NewDoubleObj(val));
187  }
188  Tcl_SetObjResult(interp, tcl_result);
189  return TCL_OK;
190 }
static int obj_vecstddev ( ClientData  ,
Tcl_Interp *  interp,
int  argc,
Tcl_Obj *const  objv[] 
)
static

Definition at line 611 of file TclVec.C.

References obj_getdoublearray().

Referenced by Vec_Init().

611  {
612  if (argc != 2) {
613  Tcl_WrongNumArgs(interp, 1, objv, (char *)"?vector?");
614  return TCL_ERROR;
615  }
616 
617  int i, num;
618  double* list = obj_getdoublearray(interp, objv, &num);
619  if (list == NULL)
620  return TCL_ERROR;
621 
622  double mean = 0.;
623  for (i=0; i<num; i++) {
624  mean += list[i];
625  }
626  mean /= (double) num;
627 
628  double stddev = 0.;
629  for (i=0; i<num; i++) {
630  double tmp = list[i] - mean;
631  stddev += tmp * tmp;
632  }
633  stddev /= (double) num;
634  stddev = sqrt(stddev);
635  free(list);
636 
637  Tcl_Obj *tcl_result = Tcl_GetObjResult(interp);
638  Tcl_SetDoubleObj(tcl_result, stddev);
639  return TCL_OK;
640 }
static double * obj_getdoublearray(Tcl_Interp *interp, Tcl_Obj *const objv[], int *len)
Definition: TclVec.C:537
static int obj_vecsub ( ClientData  ,
Tcl_Interp *  interp,
int  argc,
Tcl_Obj *const  objv[] 
)
static

Definition at line 100 of file TclVec.C.

Referenced by Vec_Init().

101 {
102  if (argc != 3) {
103  Tcl_WrongNumArgs(interp, 1, objv, (char *)"?x? ?y?");
104  return TCL_ERROR;
105  }
106  int num1=0, num2=0;
107  Tcl_Obj **data1, **data2;
108  if (Tcl_ListObjGetElements(interp, objv[1], &num1, &data1) != TCL_OK)
109  return TCL_ERROR;
110  if (Tcl_ListObjGetElements(interp, objv[2], &num2, &data2) != TCL_OK)
111  return TCL_ERROR;
112 
113  if (num1 != num2) {
114  Tcl_SetResult(interp, (char *)"vecsub: two vectors don't have the same size", TCL_STATIC);
115  return TCL_ERROR;
116  }
117 
118  Tcl_Obj *tcl_result = Tcl_NewListObj(0, NULL);
119  for (int i=0; i<num1; i++) {
120  double d1=0, d2=0;
121  if (Tcl_GetDoubleFromObj(interp, data1[i], &d1) != TCL_OK) {
122  Tcl_SetResult(interp, (char *)"vecsub: non-numeric in first argument", TCL_STATIC);
123  return TCL_ERROR;
124  }
125  if (Tcl_GetDoubleFromObj(interp, data2[i], &d2) != TCL_OK) {
126  Tcl_SetResult(interp, (char *)"vecsub: non-numeric in second argument", TCL_STATIC);
127  return TCL_ERROR;
128  }
129  Tcl_ListObjAppendElement(interp, tcl_result, Tcl_NewDoubleObj(d1-d2));
130  }
131  Tcl_SetObjResult(interp, tcl_result);
132  return TCL_OK;
133 }
static int obj_vecsum ( ClientData  ,
Tcl_Interp *  interp,
int  argc,
Tcl_Obj *const  objv[] 
)
static

Definition at line 564 of file TclVec.C.

References obj_getdoublearray().

Referenced by Vec_Init().

564  {
565  if (argc != 2) {
566  Tcl_WrongNumArgs(interp, 1, objv, (char *)"?vector?");
567  return TCL_ERROR;
568  }
569 
570  int num;
571  double *list = obj_getdoublearray(interp, objv, &num);
572  if (list == NULL)
573  return TCL_ERROR;
574 
575  double sum = 0.;
576  for (int i=0; i<num; i++) {
577  sum += list[i];
578  }
579  free(list);
580 
581  Tcl_Obj *tcl_result = Tcl_GetObjResult(interp);
582  Tcl_SetDoubleObj(tcl_result, sum);
583  return TCL_OK;
584 }
static double * obj_getdoublearray(Tcl_Interp *interp, Tcl_Obj *const objv[], int *len)
Definition: TclVec.C:537
static int obj_vectrans ( ClientData  ,
Tcl_Interp *  interp,
int  argc,
Tcl_Obj *const  objv[] 
)
static

Definition at line 288 of file TclVec.C.

References tcl_get_matrix().

Referenced by Vec_Init().

290 {
291  if (argc != 3) {
292  Tcl_WrongNumArgs(interp, 1, objv, (char *)"?matrix? ?vector?");
293  return TCL_ERROR;
294  }
295 
296  // get the matrix data
297  double mat[16];
298  if (tcl_get_matrix(
299  Tcl_GetStringFromObj(objv[0],NULL), interp, objv[1], mat) != TCL_OK) {
300  return TCL_ERROR;
301  }
302 
303  // for the vector
304  Tcl_Obj **vec;
305  int vec_size;
306  if (Tcl_ListObjGetElements(interp, objv[2], &vec_size, &vec) != TCL_OK)
307  return TCL_ERROR;
308 
309  if (vec_size != 3 && vec_size != 4) {
310  Tcl_SetResult(interp, (char *) "vectrans: vector must be of size 3 or 4",
311  TCL_STATIC);
312  return TCL_ERROR;
313  }
314 
315  double opoint[4];
316  opoint[3] = 0;
317  for (int i=0; i<vec_size; i++) {
318  double tmp;
319  if (Tcl_GetDoubleFromObj(interp, vec[i], &tmp) != TCL_OK) {
320  Tcl_SetResult(interp, (char *) "vectrans: non-numeric in vector", TCL_STATIC);
321  return TCL_ERROR;
322  }
323  opoint[i] = (double)tmp;
324  }
325  // vector data is in vec_data
326  double npoint[4];
327 
328  npoint[0]=opoint[0]*mat[0]+opoint[1]*mat[4]+opoint[2]*mat[8]+opoint[3]*mat[12]
329 ;
330  npoint[1]=opoint[0]*mat[1]+opoint[1]*mat[5]+opoint[2]*mat[9]+opoint[3]*mat[13]
331 ;
332  npoint[2]=opoint[0]*mat[2]+opoint[1]*mat[6]+opoint[2]*mat[10]+opoint[3]*mat[14
333 ];
334  npoint[3]=opoint[0]*mat[3]+opoint[1]*mat[7]+opoint[2]*mat[11]+opoint[3]*mat[15
335 ];
336  // return it
337 
338  {
339  Tcl_Obj *tcl_result = Tcl_NewListObj(0, NULL);
340  for (int i=0; i<vec_size; i++)
341  Tcl_ListObjAppendElement(interp, tcl_result, Tcl_NewDoubleObj(npoint[i]));
342  Tcl_SetObjResult(interp, tcl_result);
343  }
344  return TCL_OK;
345 }
int tcl_get_matrix(const char *fctn, Tcl_Interp *interp, Tcl_Obj *s, double *mat)
Given a string with a matrix in it, return the matrix.
Definition: TclVec.C:197
void tcl_append_matrix ( Tcl_Interp *  interp,
const double *  mat 
)

Definition at line 274 of file TclVec.C.

Referenced by obj_transabout(), obj_transmult(), obj_transvec(), and obj_transvecinv().

274  {
275  Tcl_Obj *tcl_result = Tcl_NewListObj(0, NULL);
276  for (int i=0; i<4; i++) {
277  Tcl_Obj *m = Tcl_NewListObj(0, NULL);
278  for (int j=0; j<4; j++)
279  Tcl_ListObjAppendElement(interp, m, Tcl_NewDoubleObj(mat[4*j+i]));
280  Tcl_ListObjAppendElement(interp, tcl_result, m);
281  }
282  Tcl_SetObjResult(interp, tcl_result);
283 }
int tcl_get_matrix ( const char *  fctn,
Tcl_Interp *  interp,
Tcl_Obj *  s,
double *  mat 
)

Given a string with a matrix in it, return the matrix.

Definition at line 197 of file TclVec.C.

Referenced by obj_transmult(), and obj_vectrans().

199 {
200  int num_rows;
201  Tcl_Obj **data_rows;
202  if (Tcl_ListObjGetElements(interp, s, &num_rows, &data_rows) != TCL_OK) {
203  char tmpstring[1024];
204  sprintf(tmpstring, "%s: badly formed matrix", fctn);
205  Tcl_SetResult(interp, tmpstring, TCL_VOLATILE);
206  return TCL_ERROR;
207  }
208  if (num_rows != 4) {
209  char tmpstring[1024];
210  sprintf(tmpstring, "%s: need a 4x4 matrix", fctn);
211  Tcl_SetResult(interp, tmpstring, TCL_VOLATILE);
212  return TCL_ERROR;
213  }
214  int num_row[4];
215  Tcl_Obj **data_row[4];
216  if (Tcl_ListObjGetElements(interp, data_rows[0], num_row+0, data_row+0) != TCL_OK ||
217  num_row[0] != 4 ||
218  Tcl_ListObjGetElements(interp, data_rows[1], num_row+1, data_row+1) != TCL_OK ||
219  num_row[1] != 4 ||
220  Tcl_ListObjGetElements(interp, data_rows[2], num_row+2, data_row+2) != TCL_OK ||
221  num_row[2] != 4 ||
222  Tcl_ListObjGetElements(interp, data_rows[3], num_row+3, data_row+3) != TCL_OK ||
223  num_row[3] != 4) {
224  Tcl_AppendResult(interp, fctn, ": poorly formed matrix", NULL);
225  return TCL_ERROR;
226  }
227  // now get the numbers
228  for (int i=0; i<4; i++) {
229  for (int j=0; j<4; j++) {
230  double tmp;
231  if (Tcl_GetDoubleFromObj(interp, data_row[i][j], &tmp) != TCL_OK) {
232  char tmpstring[1024];
233  sprintf(tmpstring, "%s: non-numeric in matrix", fctn);
234  Tcl_SetResult(interp, tmpstring, TCL_VOLATILE);
235  return TCL_ERROR;
236  } else {
237  mat[4*j+i] = (double) tmp; // Matrix4 is transpose of Tcl's matrix
238  }
239  }
240  }
241  return TCL_OK;
242 }
int Vec_Init ( Tcl_Interp *  interp)

Definition at line 643 of file TclVec.C.

References obj_transabout(), obj_transmult(), obj_transvec(), obj_transvecinv(), obj_vecadd(), obj_veclength(), obj_vecmean(), obj_vecscale(), obj_vecstddev(), obj_vecsub(), obj_vecsum(), and obj_vectrans().

Referenced by tcl_vector_math_init().

643  {
644  Tcl_CreateObjCommand(interp, "vecadd", obj_vecadd,
645  (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
646  Tcl_CreateObjCommand(interp, "vecsub", obj_vecsub,
647  (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
648  Tcl_CreateObjCommand(interp, "vecscale", obj_vecscale,
649  (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
650  Tcl_CreateObjCommand(interp, "transmult", obj_transmult,
651  (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
652  Tcl_CreateObjCommand(interp, "vectrans", obj_vectrans,
653  (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
654  Tcl_CreateObjCommand(interp, "veclength", obj_veclength,
655  (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
656  Tcl_CreateObjCommand(interp, "vecmean", obj_vecmean,
657  (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
658  Tcl_CreateObjCommand(interp, "vecstddev", obj_vecstddev,
659  (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
660  Tcl_CreateObjCommand(interp, "vecsum", obj_vecsum,
661  (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
662  Tcl_CreateObjCommand(interp, "transvec", obj_transvec,
663  (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
664  Tcl_CreateObjCommand(interp, "transvecinv", obj_transvecinv,
665  (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
666  Tcl_CreateObjCommand(interp, "transabout", obj_transabout,
667  (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
668  return TCL_OK;
669 }
static int obj_vectrans(ClientData, Tcl_Interp *interp, int argc, Tcl_Obj *const objv[])
Definition: TclVec.C:288
static int obj_vecmean(ClientData, Tcl_Interp *interp, int argc, Tcl_Obj *const objv[])
Definition: TclVec.C:587
static int obj_transvecinv(ClientData, Tcl_Interp *interp, int argc, Tcl_Obj *const objv[])
Definition: TclVec.C:415
static int obj_transmult(ClientData, Tcl_Interp *interp, int argc, Tcl_Obj *const objv[])
Definition: TclVec.C:351
static int obj_vecscale(ClientData, Tcl_Interp *interp, int argc, Tcl_Obj *const objv[])
Definition: TclVec.C:139
static int obj_vecsum(ClientData, Tcl_Interp *interp, int argc, Tcl_Obj *const objv[])
Definition: TclVec.C:564
static int obj_vecadd(ClientData, Tcl_Interp *interp, int argc, Tcl_Obj *const objv[])
Definition: TclVec.C:44
static int obj_vecstddev(ClientData, Tcl_Interp *interp, int argc, Tcl_Obj *const objv[])
Definition: TclVec.C:611
static int obj_transvec(ClientData, Tcl_Interp *interp, int argc, Tcl_Obj *const objv[])
Definition: TclVec.C:387
static int obj_transabout(ClientData, Tcl_Interp *interp, int argc, Tcl_Obj *const objv[])
Definition: TclVec.C:448
static int obj_vecsub(ClientData, Tcl_Interp *interp, int argc, Tcl_Obj *const objv[])
Definition: TclVec.C:100
static int obj_veclength(ClientData, Tcl_Interp *interp, int argc, Tcl_Obj *const objv[])
Definition: TclVec.C:507