javascript - How can I project a cube onto an axis in a quick manner? -


i going projection of cube onto axis. basically, doing dot product of every vertex of cube axis , compare results find out smallest , greatest values. @ end of day, these 2 values tell me point of axis projected starts , ends. have written following function that; however, thing there lot of comparisons in , because going call function thousands of time whole code runs slow. looking optimization make code faster. function has lot of comparisons , wondering if there way can minimize amount of comparisons in it. ideas?

the code written in javascript considering doing on c++ , opengl open suggestions on different programming languages.

function projectcube (axis, cube) {      pro = dotprodcut(axis, cube.vertex0);     minpro = pro;     maxpro = pro;      pro = dotprodcut(axis, cube.vertex1);     if (pro < minpro) { minpro = pro; }     if (pro > maxpro) { maxpro = pro; }      pro = dotprodcut(axis, cube.vertex2);     if (pro < minpro) { minpro = pro; }     if (pro > maxpro) { maxpro = pro; }      pro = dotprodcut(axis, cube.vertex3);     if (pro < minpro) { minpro = pro; }     if (pro > maxpro) { maxpro = pro; }      pro = dotprodcut(axis, cube.vertex4);     if (pro < minpro) { minpro = pro; }     if (pro > maxpro) { maxpro = pro; }      pro = dotprodcut(axis, cube.vertex5);     if (pro < minpro) { minpro = pro; }     if (pro > maxpro) { maxpro = pro; }      pro = dotprodcut(axis, cube.vertex6);     if (pro < minpro) { minpro = pro; }     if (pro > maxpro) { maxpro = pro; }      pro = dotprodcut(axis, cube.vertex7);     if (pro < minpro) { minpro = pro; }     if (pro > maxpro) { maxpro = pro; }      return [minpro, maxpro];  } 

first of all, use local variables.

2nd. don't use enumerated properties, use arrays instead. different situations have process sth on every item of list, , way more readable if use loop this.

and faster repeat , don't extract utility-method, but sometimes!

well function-calls have overhead , therefore performace-impact, on other hand, if inline , function get's big, compiler won't try optimize (the execution of) function. , here we're at: check bottlenecks profiler.

then getting property off object (a tiny tiny bit) more expensive accessing local variable, optimizations affect code if in mathematical envoirement, code runs several thousand times/frame. on other hand, can practice bloat function, ...

try version:

function projectcube (axis, cube) {     var x = axis.x,          y = axis.y,          z = axis.z,          v, dotproduct, min = infinity, max = -infinity,          vertices = cube.vertices;      //in example doesn't matter processing vertices in reverse order,     //and don't have introduce length-var , compare index it.     for(var = vertices.length; i--; ){         v = vertices[i];         dotproduct = x*v.x + y*v.y, z*v.z;         if(dotproduct < min) min = dotproduct;         if(dotproduct > max) max = dotproduct;     }     return [min, max]; } 

Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -