Using java Syntax Highlighting
- static public float[] add(float[] v1, float[] v2) {
- float[] result = new float[3];
- result[0] = v1[0]+v2[0];
- result[1] = v1[1]+v2[1];
- result[2] = v1[2]+v2[2];
- return result;
- }
- static public float[] scale(float[] v1, float s)
- {
- float[] v = new float[3];
- v[0] = v1[0] * s;
- v[1] = v1[1] * s;
- v[2] = v1[2] * s;
- return v;
- }
- static public float[] normalize(float[] v1)
- {
- float length = length(v1);
- float[] v = new float[3];
- if (length != 0)
- {
- v[0] = v1[0] / length;
- v[1] = v1[1] / length;
- v[2] = v1[2] / length;
- }
- return v;
- }
- static public float dot_product(float[] v, float[] w) {
- float result;
- result = v[0]*w[0]+v[1]*w[1]+v[2]*w[2];
- return result;
- }
- static public float[] reflect(float[] v1, float[] v2) {
- float l = length(v1);
- float dot;
- float[] result = new float[3];
- v1 = normalize(v1);
- v2 = normalize(v2);
- dot = 2*dot_product(inverse(v1),v2);
- v2 = scale(v2,dot);
- result = add(v1,v2);
- result = scale(result,l);
- return result;
- }
Parsed in 0.035 seconds, using GeSHi 1.0.8.4
I am that novice, new to Java and smitten with the idea that you don't need to delete stuff. Funny, that as an embedded software engineer in real life, I am constantly worrying about deterministic memory allocation.
Anyway, do the above in enough places and your nice little opengl app that is running smoothly at 30+ frames a second experiences 80+ msec hickups every couple seconds when the GC runs.
The question is, what is the best way to re-write the above? Most of this is really straightforward to fix. Here are all the new functions except for reflect which is where my question lies.
Using java Syntax Highlighting
- static public void add(float[] result, float[] v1, float[] v2) {
- result[0] = v1[0]+v2[0];
- result[1] = v1[1]+v2[1];
- result[2] = v1[2]+v2[2];
- }
- static public void scale(float[] result, float[] v1, float s)
- {
- result[0] = v1[0] * s;
- result[1] = v1[1] * s;
- result[2] = v1[2] * s;
- }
- static public void normalize(float[] v, float[] v1)
- {
- float length = length(v1);
- if (length != 0)
- {
- v[0] = v1[0] / length;
- v[1] = v1[1] / length;
- v[2] = v1[2] / length;
- }
- }
- static public float dot_product(float[] v, float[] w) {
- float result;
- result = v[0]*w[0]+v[1]*w[1]+v[2]*w[2];
- return result;
- }
Parsed in 0.033 seconds, using GeSHi 1.0.8.4
Now with reflect, I need some local objects to do my work. But I don't want to new them in the function and I wasn't sure how to handle it. Since this is part of a static class, I did the following:
Using java Syntax Highlighting
- static float[] reflect_v1 = new float[3];
- static float[] reflect_v2 = new float[3];
- static float[] reflect_v3 = new float[3];
- static public void reflect(float[] result, float[] v1, float[] v2) {
- float l = length(v1);
- float ratio;
- float dot;
- normalize(reflect_v1,v1);
- normalize(reflect_v2,v2);
- dot = dot_product(inverse(reflect_v1),reflect_v2);
- scale(reflect_v2,reflect_v2,dot);
- add(reflect_v3,reflect_v1,reflect_v2);
- ratio = length(reflect_v1);
- scale(reflect_v3,reflect_v3,l*ratio);
- copy(result,reflect_v3);
- }
Parsed in 0.034 seconds, using GeSHi 1.0.8.4
I think this is really efficient and doesn't cause any GC but maybe I am wrong. Also, doing this makes the function so that it is not re-entrant. This is not an issue for my app, but it makes the class a little less generally useful.
Is there a better way to handle needing local objects but not wanting to have them collected by the GC?
Note, you might have figured out that "inverse", which is not shown but only called, has not yet been rewritten. I am rewriting stuff one at a time to make sure I am not lost trying to figure out any bugs I create in the rewrite.




