DASD 8 Posted October 11, 2018 (edited) Hi, I making a small VEX code library and I was wondering if there are any good techniques to reduce the amount of code duplication. For example I have a function that works with or without inputs and with different variables. For simplified example: vector pointPos(const int pointid){ return point(0,"P",pointid); } vector pointPos(const int input; const int pointid){ return point(input,"P",pointid); } Could this be reduced to something like: vector pointPos(const int input = 0; const int pointid){ return point(input,"P",pointid); } Is there anything like auto type recognition, or decorators, or anything else (aside from structs) that has improved your vex code? Edited October 11, 2018 by DASD Share this post Link to post Share on other sites

anim 810 Posted October 11, 2018 you can use preprocessor macros for that https://www.sidefx.com/docs/houdini/vex/vcc.html#pre-processor for an example you can look at houdini/vex/include/subdcurve.h file Share this post Link to post Share on other sites

DASD 8 Posted October 11, 2018 Thank you very much! So I am looking at something like this and trying to make sense of it: #define SUBD_BBOX(TYPE,VEC_SIZE) \ void subd_curve_bbox_general(const float t0; const float t1; const TYPE C0; const TYPE p0; const TYPE diff; const TYPE C1; TYPE boxmin; TYPE boxmax) \ { \ /* Start with endpoints */ \ TYPE v0 = subd_curve_evaluate_general(t0, C0, p0, diff, C1); \ boxmin = min(boxmin,v0); \ boxmax = max(boxmax,v0); \ TYPE v1 = subd_curve_evaluate_general(t1, C0, p0, diff, C1); \ boxmin = min(boxmin,v1); \ boxmax = max(boxmax,v1); \ for (int i = 0; i < VEC_SIZE; ++i) { \ subd_curve_bbox_between(t0, t1, C0[i], p0[i], diff[i], C1[i], boxmin[i], boxmax[i]); \ } \ } \ void subd_curve_bbox(const float t0; const float t1; const TYPE pn1; const TYPE p0; const TYPE p1; const TYPE p2; TYPE boxmin; TYPE boxmax) \ { \ TYPE diff = (p1-p0); /* Vector from p0 to p1 */ \ TYPE C0 = 0.5*(diff + (pn1-p0));/* Average of neighbours of p0, minus p0 */ \ TYPE C1 = 0.5*((p2-p1) - diff); /* Average of neighbours of p1, minus p1 */ \ subd_curve_bbox_general(t0, t1, C0, p0, diff, C1, boxmin, boxmax); \ } \ void subd_curve_bbox_first(const float t0; const float t1; const TYPE p0; const TYPE p1; const TYPE p2; TYPE boxmin; TYPE boxmax) \ { \ TYPE diff = (p1-p0); /* Vector from p0 to p1 */ \ TYPE C1 = 0.5*((p2-p1) - diff); /* Average of neighbours of p1, minus p1 */ \ subd_curve_bbox_general(t0, t1, (TYPE)0, p0, diff, C1, boxmin, boxmax); \ } \ void subd_curve_bbox_last(const float t0; const float t1; const TYPE pn1; const TYPE p0; const TYPE p1; TYPE boxmin; TYPE boxmax) \ { \ subd_curve_bbox_first(1-t1, 1-t0, p1, p0, pn1, boxmin, boxmax); \ } \ /*empty line at end of macro*/ SUBD_BBOX(vector2,2) SUBD_BBOX(vector,3) SUBD_BBOX(vector4,4) #undef SUBD_BBOX #define SUBD_BBOX(TYPE,VEC_SIZE) \ seems to define some kind of block named "SUBD_BBOX" the "\" at the end of each line seem to be part of the definition of that block - but I am not sure about what it does TYPE and VEC_SIZE are arbitrarily named variables/placeholders/whateverYouCallThose, which are substituted with the given permutations: SUBD_BBOX(vector2,2) SUBD_BBOX(vector,3) SUBD_BBOX(vector4,4) The block is then truly concluded with: #undef SUBD_BBOX Do I get this right? Another thing I would like to do is, to create variations where certain parameters are optional, because they have "default values". I guess I can do this with the -D Preprocessor option? Do you know where there might be an example for something like this? Is this even the right command? Share this post Link to post Share on other sites