Transforming a large number of 3D points
Members - Reputation: 130
Posted 09 October 2012 - 01:07 AM
I currently apply a matrix transform a series of 3D points, which is quite expensive in terms of processing time.
Is it possible to transform just the extent points (I create a bounding box which encapsulates the points) and do some kind of linear interpolation to shift all the points without having to apply the transform to each of them?
Moderators - Reputation: 11635
Posted 09 October 2012 - 07:45 AM
P' = M x P |a b c d| |x| P' = |e f g h| * |y| |i j k l| |z| |m n o p| P'x = ax+by+cz+d P'y = ix+jy+kz+l P'z = mx+ny+oz+p
Operations to apply a bi-linear interpolation to a 3D point:
Enclosing points (8): a, b, c, d, e, f, g, h Interpolants generated per point: Ps, Pt, Pu Lerp(t, P1, P2)=P1+t*(P2-P1) P' = Lerp(Pu, Lerp(Ps, Lerp(Pt, A, B), Lerp(Pt, C, D) ), Lerp(Ps, Lerp(Pt, E, F), Lerp(Pt, G, H))) P' = (((a+s*(b-a))+t*((c+s*(d-c))-(a+s*(b-a))))+u*(((e+s*(f-e))+t*((g+s*(h-g))-(e+s*(f-e))))-((a+s*(b-a))+t*((c+s*(d-c))-(a+s*(b-a)))))) P' = Ps*(b-a) + Pt*(c-a) + Pu*(e-a) + Ps*Pt*(d-c-b+a) + Ps*Pu*(f-e-b+a) + Pt*Pu*(g-e-c+a) + Ps*Pt*Pu*(h-g-f+e-d+c+b-a) simplify out the coefficients: P' = Ps*(A) + Pt*(B) + Pu*(C) + Ps*Pt*(D) + Ps*Pu*(E) + Pt*Pu*(F) + Ps*Pt*Pu*(G)
Granted, in the bi-linear interpolation you can simplify the terms a bit by pre-multiplying Ps*Pt, Ps*Pu, Pt*Pu and Ps*Pt*Pu and pre-calculating the coefficients from the transformed bounding box but you're still doing seven multiplies per point component, rather than the three multiplies in a matrix transform. So I don't see how this would be any kind of improvement over a simple matrix transform, especially since the matrix transform is extremely well supported on GPU, whereas the bi-linear interp is less so. In the best case, the bi-linear interp would probably simplify down to the matrix transformation anyway.
Are you doing these calcs on the GPU or CPU? Have you profiled to find out where your real bottlenecks are?
Edited by JTippetts, 09 October 2012 - 07:48 AM.
Crossbones+ - Reputation: 3129
Posted 09 October 2012 - 08:07 AM
Edited by FLeBlanc, 09 October 2012 - 08:12 AM.
Members - Reputation: 4377
Posted 09 October 2012 - 03:33 PM
First you need to identify if transforming all of the data is actually a problem. Unless you have actual data showing your program spending too much time doing the transformations then you shouldn't assume it will take too much time.
If you do find that this is a problem then you can see if there is a faster way of doing the calculations. If these transformations are for rendering geometry try to offload the calculations to the gpu if possible. Dont draw any objects that are off screen. Apply a LOD system for objects that are far away.
If these transformations are used for collision detection then see if you cant transform shapes for collision detection into the local space of the mesh instead of transforming the mesh into world space. For example, if you want to see if a ray collides with a mesh instead of doing this
tranformedMesh = transform(mesh, object.worldMatrix) if ray.intersects(tranformedMesh) \\collisionDo this
transformedRay = transform(ray, object.worldMatrix.inverse) if transformedRay.intersects(mesh) \\collisionthis way you only have to transform a ray that consists of two vectors instead of all of the mesh data
Edited by HappyCoder, 09 October 2012 - 03:36 PM.