Re: [R] rgl package and animation
-- snip -- On 11/4/2012 7:45 AM, Duncan Murdoch wrote: First, draw the new sphere at the first point and save the object id: sphereid <- sphere3d(dat[1,c("X", "Y", "Z")], col="red", radius=1) # Also save the spinner that you like: spin <- spin3d( ) #maybe with different parms # Now, the animation function: f <- function(time) { par3d(skipRedraw = TRUE) # stops intermediate redraws on.exit(par3d(skipRedraw=FALSE)) # redraw at the end rgl.pop(id=sphereid) # delete the old sphere pt <- time %% 40 + 1 # compute which one to draw pnt <- dat[pt, c("X", "Y", "Z")] # maybe interpolate instead? sphereid <<- spheres3d(pnt, radius=1, col="red") spin(time) } Duncan Murdoch Thanks so much Duncan! I probably never would have gotten there without your help. (Especially since I had to look at the help for the <<- operator, which is conceptually a level beyond where I usually work). It would be great to have an additional creative example or two for f(time) functions in the play3d() help. Your useful code comments really help me see what needs to happen in an f(time) function. I really appreciate that you took the time to get me going! I've made a small addition to the spin3d function and added an example to the ?spin3d page. This isn't on CRAN yet, but you can get the latest from R-forge. Make sure you get 0.92.898 or newer. The new example shows a rotating view of spinning cubes, using the sprites3d function within the animation function. Duncan Murdoch Thanks so much for taking the time to do this and for all the other contributions you make to the R community! I should tell you that I also looked at your rgl function writeWebGL() over the weekend, and I think it holds tremendous potential for sharing visualizations of multidimensional data with a non-technical audience. Who knew it could be so easy to move three dimensional data from R to the web? Rob Baer -- __ Robert W. Baer, Ph.D. Professor of Physiology Kirksille College of Osteopathic Medicine A. T. Still University of Health Sciences Kirksville, MO 63501 USA __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] rgl package and animation
On 12-11-03 11:40 AM, Robert Baer wrote: On 11/3/2012 6:47 AM, Duncan Murdoch wrote: On 12-11-02 7:47 PM, Robert Baer wrote: I am trying to figure out how to use rgl package for animation. It appears that this is done using the play3d() function. Below I have some sample code that plots a 3D path and puts a sphere at the point farthest from the origin (which in this case also appears to be at the end of the path). What I would like to do is animate the movement of another sphere along the length of the path while simultaneously rotating the viewport. Duncan Murdock's (wonderful) Braided Knot YouTube video: (http://www.youtube.com/watch?v=prdZWQD7L5c) makes it clear that such things can be done, but I am having trouble understanding how to construct the f(time) function that gets passed to play3d(). The demo(flag) example is a little helpful, but I still can't quite translate it to my problem. Can anyone point to some some simple f(time) function examples that I could use for reference or give me a little hint as to how to construct f(time) for movement along the path while simultaneously rotating the viewport? Thanks, Rob library(rgl) # Generate a 3D path dat <- structure(list(X = c(0, 0.06181308, 0.002235635, -0.03080658, -0.1728054, -0.372467, -0.5877065, -0.8814848, -1.103668, -1.366157, -1.625862, -1.948066, -2.265388, -2.689826, -3.095001, -3.49749, -3.946068, -4.395653, -4.772034, -5.111259, -5.410515, -5.649475, -5.73439, -5.662201, -5.567145, -5.390334, -5.081581, -4.796631, -4.496559, -4.457024, -4.459564, -4.641746, -4.849105, -5.0899430001, -5.43129, -5.763724, -6.199448, -6.517578, -6.864234, -6.907439), Y = c(0, -0.100724, -0.1694719, 0.036505999886, -0.09299519, -0.222977, -0.3557596, -0.3658229, -0.3299489, -0.2095574, -0.08041446, 0.02013388, 0.295372, 0.1388314, 0.2811047, 0.2237614, 0.1419052, 0.06029464, -0.09330875, -0.2075969, -0.3286296, -0.4385684, -0.4691093, -0.6235059, -0.5254676, -0.568444, -0.6388859, -0.727356, -1.073769, -1.0321350001, -1.203461, -1.438637, -1.6502310001, -1.861351, -2.169083, -2.4314730001, -2.6991430001, -2.961258, -3.239381, -3.466103), Z = c(0, 0.1355290002, 0.40106200024, 1.216374, 1.5539550003, 1.7308050003, 1.8116760003, 2.185124, 2.5260320004, 3.034794, 3.4265440004, 3.822512, 4.7449040002, 4.644837, 5.4184880002, 5.8586730001, 6.378356, 6.8339540001, 7.216339, 7.5941160004, 7.9559020004, 8.352936, 8.709319, 9.0166930003, 9.4855350003, 9.9000550001, 10.397003, 10.932068, 11.025726, 12.334595, 13.177887, 13.741852, 14.61142, 15.351013, 16.161255, 16.932831, 17.897186, 18.826691, 19.776001, 20.735596), time = c(0, 0.0116, 0.0196, 0.0311, 0.0391, 0.0507, 0.0623, 0.0703, 0.0818, 0.0899, 0.101, 0.109, 0.121, 0.129, 0.141, 0.152, 0.16, 0.172, 0.18, 0.191, 0.199, 0.211, 0.222, 0.23, 0.242, 0.25, 0.262, 0.27, 0.281, 0.289, 0.301, 0.312, 0.32, 0.332, 0.34, 0.351, 0.359, 0.371, 0.379, 0.391)), .Names = c("X", "Y", "Z", "time"), row.names = c("1844", "1845", "1846", "1847", "1848", "1849", "1850", "1851", "1852", "1853", "1854", "1855", "1856", "1857", "1858", "1859", "1860", "1861", "1862", "1863", "1864", "1865", "1866", "1867", "1868", "1869", "1870", "1871", "1872", "1873", "1874", "1875", "1876", "1877", "1878", "1879", "1880", "1881", "1882", "1883"), class = "data.frame") # Plot 3d path with(dat, plot3d(X,Y,Z, type = 'l', col = 'blue', lty = 1)) # get absolute distance from origin dat$r = sqrt(dat$X ^ 2 + dat$Y ^ 2 + dat$Z ^ 2) mr = max(dat$r) # yes sorry, didn't get copied to original email code mxpnt = dat[dat$r == mr,] # Coordinates of furthest point # Plot a blue sphere at max distance plot3d(mxpnt$X, mxpnt$Y, mxpnt$Z, type = 's', radius = 1, col = 'blue', add = TRUE) Your code didn't include the mr variable, but I assume it's just max(dat$r). With that assumption, I'd do the animation function as follows: First, draw the new sphere at the first point and save the object id: sphereid <- sphere3d(dat[1,c("X", "Y", "Z")], col="red", radius=1) # Also save the spinner that you like: spin <- spin3d( ) #maybe with different parms # Now, the animation function: f <- function(time) { par3d(skipRedraw = TRUE) # stops int
Re: [R] rgl package and animation
On 11/3/2012 6:47 AM, Duncan Murdoch wrote: On 12-11-02 7:47 PM, Robert Baer wrote: I am trying to figure out how to use rgl package for animation. It appears that this is done using the play3d() function. Below I have some sample code that plots a 3D path and puts a sphere at the point farthest from the origin (which in this case also appears to be at the end of the path). What I would like to do is animate the movement of another sphere along the length of the path while simultaneously rotating the viewport. Duncan Murdock's (wonderful) Braided Knot YouTube video: (http://www.youtube.com/watch?v=prdZWQD7L5c) makes it clear that such things can be done, but I am having trouble understanding how to construct the f(time) function that gets passed to play3d(). The demo(flag) example is a little helpful, but I still can't quite translate it to my problem. Can anyone point to some some simple f(time) function examples that I could use for reference or give me a little hint as to how to construct f(time) for movement along the path while simultaneously rotating the viewport? Thanks, Rob library(rgl) # Generate a 3D path dat <- structure(list(X = c(0, 0.06181308, 0.002235635, -0.03080658, -0.1728054, -0.372467, -0.5877065, -0.8814848, -1.103668, -1.366157, -1.625862, -1.948066, -2.265388, -2.689826, -3.095001, -3.49749, -3.946068, -4.395653, -4.772034, -5.111259, -5.410515, -5.649475, -5.73439, -5.662201, -5.567145, -5.390334, -5.081581, -4.796631, -4.496559, -4.457024, -4.459564, -4.641746, -4.849105, -5.0899430001, -5.43129, -5.763724, -6.199448, -6.517578, -6.864234, -6.907439), Y = c(0, -0.100724, -0.1694719, 0.036505999886, -0.09299519, -0.222977, -0.3557596, -0.3658229, -0.3299489, -0.2095574, -0.08041446, 0.02013388, 0.295372, 0.1388314, 0.2811047, 0.2237614, 0.1419052, 0.06029464, -0.09330875, -0.2075969, -0.3286296, -0.4385684, -0.4691093, -0.6235059, -0.5254676, -0.568444, -0.6388859, -0.727356, -1.073769, -1.0321350001, -1.203461, -1.438637, -1.6502310001, -1.861351, -2.169083, -2.4314730001, -2.6991430001, -2.961258, -3.239381, -3.466103), Z = c(0, 0.1355290002, 0.40106200024, 1.216374, 1.5539550003, 1.7308050003, 1.8116760003, 2.185124, 2.5260320004, 3.034794, 3.4265440004, 3.822512, 4.7449040002, 4.644837, 5.4184880002, 5.8586730001, 6.378356, 6.8339540001, 7.216339, 7.5941160004, 7.9559020004, 8.352936, 8.709319, 9.0166930003, 9.4855350003, 9.9000550001, 10.397003, 10.932068, 11.025726, 12.334595, 13.177887, 13.741852, 14.61142, 15.351013, 16.161255, 16.932831, 17.897186, 18.826691, 19.776001, 20.735596), time = c(0, 0.0116, 0.0196, 0.0311, 0.0391, 0.0507, 0.0623, 0.0703, 0.0818, 0.0899, 0.101, 0.109, 0.121, 0.129, 0.141, 0.152, 0.16, 0.172, 0.18, 0.191, 0.199, 0.211, 0.222, 0.23, 0.242, 0.25, 0.262, 0.27, 0.281, 0.289, 0.301, 0.312, 0.32, 0.332, 0.34, 0.351, 0.359, 0.371, 0.379, 0.391)), .Names = c("X", "Y", "Z", "time"), row.names = c("1844", "1845", "1846", "1847", "1848", "1849", "1850", "1851", "1852", "1853", "1854", "1855", "1856", "1857", "1858", "1859", "1860", "1861", "1862", "1863", "1864", "1865", "1866", "1867", "1868", "1869", "1870", "1871", "1872", "1873", "1874", "1875", "1876", "1877", "1878", "1879", "1880", "1881", "1882", "1883"), class = "data.frame") # Plot 3d path with(dat, plot3d(X,Y,Z, type = 'l', col = 'blue', lty = 1)) # get absolute distance from origin dat$r = sqrt(dat$X ^ 2 + dat$Y ^ 2 + dat$Z ^ 2) mr = max(dat$r) # yes sorry, didn't get copied to original email code mxpnt = dat[dat$r == mr,] # Coordinates of furthest point # Plot a blue sphere at max distance plot3d(mxpnt$X, mxpnt$Y, mxpnt$Z, type = 's', radius = 1, col = 'blue', add = TRUE) Your code didn't include the mr variable, but I assume it's just max(dat$r). With that assumption, I'd do the animation function as follows: First, draw the new sphere at the first point and save the object id: sphereid <- sphere3d(dat[1,c("X", "Y", "Z")], col="red", radius=1) # Also save the spinner that you like: spin <- spin3d( ) #maybe with different parms # Now, the animation function: f <- function(time) { par3d(skipRedraw = TRUE) # stops intermediate redraws on.exit(par3d(ski
Re: [R] rgl package and animation
On 12-11-02 7:47 PM, Robert Baer wrote: I am trying to figure out how to use rgl package for animation. It appears that this is done using the play3d() function. Below I have some sample code that plots a 3D path and puts a sphere at the point farthest from the origin (which in this case also appears to be at the end of the path). What I would like to do is animate the movement of another sphere along the length of the path while simultaneously rotating the viewport. Duncan Murdock's (wonderful) Braided Knot YouTube video: (http://www.youtube.com/watch?v=prdZWQD7L5c) makes it clear that such things can be done, but I am having trouble understanding how to construct the f(time) function that gets passed to play3d(). The demo(flag) example is a little helpful, but I still can't quite translate it to my problem. Can anyone point to some some simple f(time) function examples that I could use for reference or give me a little hint as to how to construct f(time) for movement along the path while simultaneously rotating the viewport? Thanks, Rob library(rgl) # Generate a 3D path dat <- structure(list(X = c(0, 0.06181308, 0.002235635, -0.03080658, -0.1728054, -0.372467, -0.5877065, -0.8814848, -1.103668, -1.366157, -1.625862, -1.948066, -2.265388, -2.689826, -3.095001, -3.49749, -3.946068, -4.395653, -4.772034, -5.111259, -5.410515, -5.649475, -5.73439, -5.662201, -5.567145, -5.390334, -5.081581, -4.796631, -4.496559, -4.457024, -4.459564, -4.641746, -4.849105, -5.0899430001, -5.43129, -5.763724, -6.199448, -6.517578, -6.864234, -6.907439), Y = c(0, -0.100724, -0.1694719, 0.036505999886, -0.09299519, -0.222977, -0.3557596, -0.3658229, -0.3299489, -0.2095574, -0.08041446, 0.02013388, 0.295372, 0.1388314, 0.2811047, 0.2237614, 0.1419052, 0.06029464, -0.09330875, -0.2075969, -0.3286296, -0.4385684, -0.4691093, -0.6235059, -0.5254676, -0.568444, -0.6388859, -0.727356, -1.073769, -1.0321350001, -1.203461, -1.438637, -1.6502310001, -1.861351, -2.169083, -2.4314730001, -2.6991430001, -2.961258, -3.239381, -3.466103), Z = c(0, 0.1355290002, 0.40106200024, 1.216374, 1.5539550003, 1.7308050003, 1.8116760003, 2.185124, 2.5260320004, 3.034794, 3.4265440004, 3.822512, 4.7449040002, 4.644837, 5.4184880002, 5.8586730001, 6.378356, 6.8339540001, 7.216339, 7.5941160004, 7.9559020004, 8.352936, 8.709319, 9.0166930003, 9.4855350003, 9.9000550001, 10.397003, 10.932068, 11.025726, 12.334595, 13.177887, 13.741852, 14.61142, 15.351013, 16.161255, 16.932831, 17.897186, 18.826691, 19.776001, 20.735596), time = c(0, 0.0116, 0.0196, 0.0311, 0.0391, 0.0507, 0.0623, 0.0703, 0.0818, 0.0899, 0.101, 0.109, 0.121, 0.129, 0.141, 0.152, 0.16, 0.172, 0.18, 0.191, 0.199, 0.211, 0.222, 0.23, 0.242, 0.25, 0.262, 0.27, 0.281, 0.289, 0.301, 0.312, 0.32, 0.332, 0.34, 0.351, 0.359, 0.371, 0.379, 0.391)), .Names = c("X", "Y", "Z", "time"), row.names = c("1844", "1845", "1846", "1847", "1848", "1849", "1850", "1851", "1852", "1853", "1854", "1855", "1856", "1857", "1858", "1859", "1860", "1861", "1862", "1863", "1864", "1865", "1866", "1867", "1868", "1869", "1870", "1871", "1872", "1873", "1874", "1875", "1876", "1877", "1878", "1879", "1880", "1881", "1882", "1883"), class = "data.frame") # Plot 3d path with(dat, plot3d(X,Y,Z, type = 'l', col = 'blue', lty = 1)) # get absolute distance from origin dat$r = sqrt(dat$X ^ 2 + dat$Y ^ 2 + dat$Z ^ 2) mxpnt = dat[dat$r == mr,] # Coordinates of furthest point # Plot a blue sphere at max distance plot3d(mxpnt$X, mxpnt$Y, mxpnt$Z, type = 's', radius = 1, col = 'blue', add = TRUE) Your code didn't include the mr variable, but I assume it's just max(dat$r). With that assumption, I'd do the animation function as follows: First, draw the new sphere at the first point and save the object id: sphereid <- sphere3d(dat[1,c("X", "Y", "Z")], col="red", radius=1) # Also save the spinner that you like: spin <- spin3d( ) #maybe with different parms # Now, the animation function: f <- function(time) { par3d(skipRedraw = TRUE) # stops intermediate redraws on.exit(par3d(skipRedraw=FALSE)) # redraw at the end rgl.pop(id=sphereid) # delete the old sphere pt <- time %% 40 + 1 # compute which one to dra
[R] rgl package and animation
I am trying to figure out how to use rgl package for animation. It appears that this is done using the play3d() function. Below I have some sample code that plots a 3D path and puts a sphere at the point farthest from the origin (which in this case also appears to be at the end of the path). What I would like to do is animate the movement of another sphere along the length of the path while simultaneously rotating the viewport. Duncan Murdock's (wonderful) Braided Knot YouTube video: (http://www.youtube.com/watch?v=prdZWQD7L5c) makes it clear that such things can be done, but I am having trouble understanding how to construct the f(time) function that gets passed to play3d(). The demo(flag) example is a little helpful, but I still can't quite translate it to my problem. Can anyone point to some some simple f(time) function examples that I could use for reference or give me a little hint as to how to construct f(time) for movement along the path while simultaneously rotating the viewport? Thanks, Rob library(rgl) # Generate a 3D path dat <- structure(list(X = c(0, 0.06181308, 0.002235635, -0.03080658, -0.1728054, -0.372467, -0.5877065, -0.8814848, -1.103668, -1.366157, -1.625862, -1.948066, -2.265388, -2.689826, -3.095001, -3.49749, -3.946068, -4.395653, -4.772034, -5.111259, -5.410515, -5.649475, -5.73439, -5.662201, -5.567145, -5.390334, -5.081581, -4.796631, -4.496559, -4.457024, -4.459564, -4.641746, -4.849105, -5.0899430001, -5.43129, -5.763724, -6.199448, -6.517578, -6.864234, -6.907439), Y = c(0, -0.100724, -0.1694719, 0.036505999886, -0.09299519, -0.222977, -0.3557596, -0.3658229, -0.3299489, -0.2095574, -0.08041446, 0.02013388, 0.295372, 0.1388314, 0.2811047, 0.2237614, 0.1419052, 0.06029464, -0.09330875, -0.2075969, -0.3286296, -0.4385684, -0.4691093, -0.6235059, -0.5254676, -0.568444, -0.6388859, -0.727356, -1.073769, -1.0321350001, -1.203461, -1.438637, -1.6502310001, -1.861351, -2.169083, -2.4314730001, -2.6991430001, -2.961258, -3.239381, -3.466103), Z = c(0, 0.1355290002, 0.40106200024, 1.216374, 1.5539550003, 1.7308050003, 1.8116760003, 2.185124, 2.5260320004, 3.034794, 3.4265440004, 3.822512, 4.7449040002, 4.644837, 5.4184880002, 5.8586730001, 6.378356, 6.8339540001, 7.216339, 7.5941160004, 7.9559020004, 8.352936, 8.709319, 9.0166930003, 9.4855350003, 9.9000550001, 10.397003, 10.932068, 11.025726, 12.334595, 13.177887, 13.741852, 14.61142, 15.351013, 16.161255, 16.932831, 17.897186, 18.826691, 19.776001, 20.735596), time = c(0, 0.0116, 0.0196, 0.0311, 0.0391, 0.0507, 0.0623, 0.0703, 0.0818, 0.0899, 0.101, 0.109, 0.121, 0.129, 0.141, 0.152, 0.16, 0.172, 0.18, 0.191, 0.199, 0.211, 0.222, 0.23, 0.242, 0.25, 0.262, 0.27, 0.281, 0.289, 0.301, 0.312, 0.32, 0.332, 0.34, 0.351, 0.359, 0.371, 0.379, 0.391)), .Names = c("X", "Y", "Z", "time"), row.names = c("1844", "1845", "1846", "1847", "1848", "1849", "1850", "1851", "1852", "1853", "1854", "1855", "1856", "1857", "1858", "1859", "1860", "1861", "1862", "1863", "1864", "1865", "1866", "1867", "1868", "1869", "1870", "1871", "1872", "1873", "1874", "1875", "1876", "1877", "1878", "1879", "1880", "1881", "1882", "1883"), class = "data.frame") # Plot 3d path with(dat, plot3d(X,Y,Z, type = 'l', col = 'blue', lty = 1)) # get absolute distance from origin dat$r = sqrt(dat$X ^ 2 + dat$Y ^ 2 + dat$Z ^ 2) mxpnt = dat[dat$r == mr,] # Coordinates of furthest point # Plot a blue sphere at max distance plot3d(mxpnt$X, mxpnt$Y, mxpnt$Z, type = 's', radius = 1, col = 'blue', add = TRUE) __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.