dmurdoch / rgl

rgl is a 3D visualization system based on OpenGL. It provides a medium to high level interface for use in R, currently modelled on classic R graphics, with extensions to allow for interaction.
https://dmurdoch.github.io/rgl/
GNU General Public License v2.0
84 stars 20 forks source link

[Optimization] Compute sin/cos outside for-loop #385

Closed discoleo closed 10 months ago

discoleo commented 10 months ago

Optimization of turn3d

The following code appears in function turn3d:

theta <- seq(0, 2*pi, length.out = n + 1)[-(n + 1)]
for (i in inds) {
    vb <- cbind(vb, rbind(x[i], sin(theta)*y[i], cos(theta)*y[i], 1))

The sin(theta) and cos(theta) are computed during each iteration inside the for-loop. Furthermore, theta is a vector with 12 values: the repeated evaluation may become sufficiently expensive.

dmurdoch commented 10 months ago

Show me the timings for this and your proposed improvement.

discoleo commented 10 months ago

I was looking for a simple solution to rotate a protein (e.g. a ion channel monomer) around its central pore and generate the surface around the pore. The monomer may have hundreds of amino acids.

Using turn3d seemed a sufficiently simple hack. I do not have yet a functional code: but I have to think if it will work with 100 * (10 to 20 atoms) > 1000 atoms.

dmurdoch commented 10 months ago

Don't worry about optimization yet. Keep your code simple, and get it to work. If it's fast enough, move on to the next task. If not, then worry about optimization.