Random points on a sphere

Brecht's icon

Hi again,

The following patch places points on a random location on the surface of a sphere. At least, most points end up there.
A lot end up outside the surface and I can't seem to find out why,... As far as I know, my math is right, but that doesn't mean anything.

brecht.

Max Patch
Copy patch and select New From Clipboard in Max.

yair reshef's icon

looks very nice, you could create a gl.gridshape sphere output it to a
matrix using matrixoutput and then spit out indevidual points to a gl.mesh
but a solution for your specific problem is beyond my timeknowhow.

On Mon, Feb 25, 2008 at 1:29 PM, Brecht wrote:

>
> Hi again,
>
> The following patch places points on a random location on the surface of a
> sphere. At least, most points end up there.
> A lot end up outside the surface and I can't seem to find out why,... As
> far as I know, my math is right, but that doesn't mean anything.
>
> brecht.
>
> #P window setfont "Sans Serif" 9.;
> #P window linecount 1;
> #P newex 226 328 41 196617 r pulse;
> #P newex 170 35 41 196617 r pulse;
> #P newex 170 82 36 196617 buddy;
> #P newex 67 83 41 196617 s pulse;
> #P newex 251 103 46 196617 / 1000.;
> #P newex 170 103 46 196617 / 1000.;
> #P newex 241 58 70 196617 random 6283;
> #P newex 170 58 70 196617 random 6283;
> #P message 241 280 65 196617 clear , dim 0;
> #P newex 225 353 216 196617 jit.matrix coords 3 float32 @thru 0 @adapt 1;
> #P button 225 280 15 0;
> #P newex 225 301 216 196617 jit.matrix coords 3 float32 @thru 0 @adapt 1;
> #P newex 225 257 127 196617 jit.matrix bar 3 float32 1;
> #P newex 164 328 53 196617 jit.concat;
> #P newex 225 235 64 196617 jit.fill bar 1;
> #B color 5;
> #P newex 290 235 64 196617 jit.fill bar 2;
> #B color 5;
> #P newex 160 235 64 196617 jit.fill bar 0;
> #B color 5;
> #P button 291 168 15 0;
> #P newex 264 190 72 196617 expr $f1*$f2;
> #P button 240 168 15 0;
> #P newex 160 190 101 196617 expr ($f1*-1)* $f2;
> #P newex 284 145 23 196617 cos;
> #P newex 251 145 21 196617 sin;
> #P flonum 251 124 56 9 0.001 6.28318 3 3 0 0 0 221 221 221 222 222 222 0 0
> 0;
> #P newex 203 145 23 196617 cos;
> #P newex 170 145 21 196617 sin;
> #P flonum 160 215 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
> #P flonum 170 124 56 9 0.001 6.283 3 3 0 0 0 221 221 221 222 222 222 0 0
> 0;
> #P flonum 232 215 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
> #P flonum 196 215 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
> #P newex 225 374 108 196617 jit.op @op * @val 0.5;
> #P newex 334 374 96 196617 jit.gl.handle sphere;
> #P toggle 10 24 15 0;
> #P newex 225 396 245 196617 jit.gl.mesh sphere @draw_mode points @color 1
> 1 1;
> #P newex 10 120 145 196617 jit.window sphere @floating 1;
> #P newex 10 60 58 196617 t b b erase;
> #P newex 10 41 51 196617 qmetro 5;
> #P newex 10 101 98 196617 jit.gl.render sphere;
> #P comment 308 280 48 196617 << reset;
> #P fasten 16 0 19 0 256 164 245 164;
> #P connect 16 0 18 1;
> #P fasten 16 0 21 0 256 166 296 166;
> #P fasten 16 0 20 1 256 164 331 164;
> #P fasten 10 0 23 0 237 233 295 233;
> #P connect 15 0 16 0;
> #P fasten 15 0 17 0 256 142 289 142;
> #P fasten 13 0 20 0 175 185 269 185;
> #P fasten 21 0 20 0 296 185 269 185;
> #P connect 34 0 15 0;
> #P fasten 36 1 34 0 201 101 256 101;
> #P connect 37 0 31 0;
> #P fasten 37 0 32 0 175 55 246 55;
> #P fasten 20 0 10 0 269 211 237 211;
> #P connect 8 0 5 0;
> #P fasten 7 0 5 0 339 393 230 393;
> #P connect 29 0 8 0;
> #P fasten 25 0 29 0 169 349 230 349;
> #P connect 38 0 29 0;
> #P fasten 30 0 27 0 246 298 230 298;
> #P connect 28 0 27 0;
> #P fasten 26 0 25 1 230 277 212 277;
> #P connect 26 0 28 0;
> #P fasten 23 0 26 0 295 254 230 254;
> #P connect 24 0 26 0;
> #P fasten 22 0 26 0 165 254 230 254;
> #P fasten 9 0 24 0 201 233 230 233;
> #P connect 11 0 13 0;
> #P fasten 11 0 14 0 175 142 208 142;
> #P fasten 17 0 9 0 289 209 201 209;
> #P fasten 32 0 36 1 246 78 201 78;
> #P connect 33 0 11 0;
> #P connect 36 0 33 0;
> #P connect 31 0 36 0;
> #P fasten 27 0 25 0 230 322 169 322;
> #P connect 12 0 22 0;
> #P connect 18 0 12 0;
> #P fasten 14 0 18 0 208 167 165 167;
> #P fasten 19 0 18 0 245 188 165 188;
> #P fasten 3 1 35 0 39 80 72 80;
> #P fasten 3 2 1 0 63 83 15 83;
> #P connect 3 0 1 0;
> #P connect 2 0 3 0;
> #P connect 6 0 2 0;
> #P window clipboard copycount 39;
>
>

Brecht's icon

That would indeed be an option, were it not that I need to have real
random points.
The gridshape points would still be structured depending on the dims
of jit.gl.gridshape.
The points need to be planes in the end, with their rotation
depending on their position, each getting a random texture.

On 25 Feb 2008, at 14:21, yair reshef wrote:

> looks very nice, you could create a gl.gridshape sphere output it
> to a matrix using matrixoutput and then spit out indevidual points
> to a gl.mesh
> but a solution for your specific problem is beyond my timeknowhow.
>
>
>
> On Mon, Feb 25, 2008 at 1:29 PM, Brecht
> wrote:
>
> Hi again,
>
> The following patch places points on a random location on the
> surface of a sphere. At least, most points end up there.
> A lot end up outside the surface and I can't seem to find out
> why,... As far as I know, my math is right, but that doesn't mean
> anything.
>
> brecht.
>
> #P window setfont "Sans Serif" 9.;
> #P window linecount 1;
> #P newex 226 328 41 196617 r pulse;
> #P newex 170 35 41 196617 r pulse;
> #P newex 170 82 36 196617 buddy;
> #P newex 67 83 41 196617 s pulse;
> #P newex 251 103 46 196617 / 1000.;
> #P newex 170 103 46 196617 / 1000.;
> #P newex 241 58 70 196617 random 6283;
> #P newex 170 58 70 196617 random 6283;
> #P message 241 280 65 196617 clear , dim 0;
> #P newex 225 353 216 196617 jit.matrix coords 3 float32 @thru 0
> @adapt 1;
> #P button 225 280 15 0;
> #P newex 225 301 216 196617 jit.matrix coords 3 float32 @thru 0
> @adapt 1;
> #P newex 225 257 127 196617 jit.matrix bar 3 float32 1;
> #P newex 164 328 53 196617 jit.concat;
> #P newex 225 235 64 196617 jit.fill bar 1;
> #B color 5;
> #P newex 290 235 64 196617 jit.fill bar 2;
> #B color 5;
> #P newex 160 235 64 196617 jit.fill bar 0;
> #B color 5;
> #P button 291 168 15 0;
> #P newex 264 190 72 196617 expr $f1*$f2;
> #P button 240 168 15 0;
> #P newex 160 190 101 196617 expr ($f1*-1)* $f2;
> #P newex 284 145 23 196617 cos;
> #P newex 251 145 21 196617 sin;
> #P flonum 251 124 56 9 0.001 6.28318 3 3 0 0 0 221 221 221 222 222
> 222 0 0 0;
> #P newex 203 145 23 196617 cos;
> #P newex 170 145 21 196617 sin;
> #P flonum 160 215 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
> #P flonum 170 124 56 9 0.001 6.283 3 3 0 0 0 221 221 221 222 222
> 222 0 0 0;
> #P flonum 232 215 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
> #P flonum 196 215 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
> #P newex 225 374 108 196617 jit.op @op * @val 0.5;
> #P newex 334 374 96 196617 jit.gl.handle sphere;
> #P toggle 10 24 15 0;
> #P newex 225 396 245 196617 jit.gl.mesh sphere @draw_mode points
> @color 1 1 1;
> #P newex 10 120 145 196617 jit.window sphere @floating 1;
> #P newex 10 60 58 196617 t b b erase;
> #P newex 10 41 51 196617 qmetro 5;
> #P newex 10 101 98 196617 jit.gl.render sphere;
> #P comment 308 280 48 196617 << reset;
> #P fasten 16 0 19 0 256 164 245 164;
> #P connect 16 0 18 1;
> #P fasten 16 0 21 0 256 166 296 166;
> #P fasten 16 0 20 1 256 164 331 164;
> #P fasten 10 0 23 0 237 233 295 233;
> #P connect 15 0 16 0;
> #P fasten 15 0 17 0 256 142 289 142;
> #P fasten 13 0 20 0 175 185 269 185;
> #P fasten 21 0 20 0 296 185 269 185;
> #P connect 34 0 15 0;
> #P fasten 36 1 34 0 201 101 256 101;
> #P connect 37 0 31 0;
> #P fasten 37 0 32 0 175 55 246 55;
> #P fasten 20 0 10 0 269 211 237 211;
> #P connect 8 0 5 0;
> #P fasten 7 0 5 0 339 393 230 393;
> #P connect 29 0 8 0;
> #P fasten 25 0 29 0 169 349 230 349;
> #P connect 38 0 29 0;
> #P fasten 30 0 27 0 246 298 230 298;
> #P connect 28 0 27 0;
> #P fasten 26 0 25 1 230 277 212 277;
> #P connect 26 0 28 0;
> #P fasten 23 0 26 0 295 254 230 254;
> #P connect 24 0 26 0;
> #P fasten 22 0 26 0 165 254 230 254;
> #P fasten 9 0 24 0 201 233 230 233;
> #P connect 11 0 13 0;
> #P fasten 11 0 14 0 175 142 208 142;
> #P fasten 17 0 9 0 289 209 201 209;
> #P fasten 32 0 36 1 246 78 201 78;
> #P connect 33 0 11 0;
> #P connect 36 0 33 0;
> #P connect 31 0 36 0;
> #P fasten 27 0 25 0 230 322 169 322;
> #P connect 12 0 22 0;
> #P connect 18 0 12 0;
> #P fasten 14 0 18 0 208 167 165 167;
> #P fasten 19 0 18 0 245 188 165 188;
> #P fasten 3 1 35 0 39 80 72 80;
> #P fasten 3 2 1 0 63 83 15 83;
> #P connect 3 0 1 0;
> #P connect 2 0 3 0;
> #P connect 6 0 2 0;
> #P window clipboard copycount 39;
>
>

Bas van der Graaff's icon

Hey brecht,

I tried to figure out your math first, and although the calculations seemed to be correct, i couldn't quite understand why the numboxes would output the values a total of 5 times each pulse. So i redid the math (i don't know whether the distribution is uniform, don't think so, but at least all the points are definitely on the sphere).

But it still messed up, same problem as yours, so the problem is somewhere in the sending of the matrixes. Jit.fill sends a new matrix 'bar' out every time a single one gets a value, so you're out 3 matrices instead of 1, two of which have some wrong value.

Using setcell to matrix, it works correctly, only i don't think it's a very fast method. But i'll leave it up to you to work around that :/. Plugging your math back in left me with nearly a sphere, but there are still some weird shapes sticking out... Lemme know what you got!

Max Patch
Copy patch and select New From Clipboard in Max.

lists@lowfrequency.or's icon

Not true - create a gridshape with large dimensions (84 x 84 or
larger). The use matrixoutput to get the geometry into a
jit.matrix. Now, to get "random" points you use getcell/setcell.
I've done this before - I don't have time to get the code handy, but
it works well enough for those purposes.

I also have code somewhere that uses jit.expr for this...

Cheers
Evan

On Feb 25, 2008, at 1:26 PM, Brecht Debackere wrote:

> That would indeed be an option, were it not that I need to have
> real random points.
> The gridshape points would still be structured depending on the
> dims of jit.gl.gridshape.
> The points need to be planes in the end, with their rotation
> depending on their position, each getting a random texture.
>
> On 25 Feb 2008, at 14:21, yair reshef wrote:
>
>> looks very nice, you could create a gl.gridshape sphere output it
>> to a matrix using matrixoutput and then spit out indevidual points
>> to a gl.mesh
>> but a solution for your specific problem is beyond my timeknowhow.
>>
>>
>>
>> On Mon, Feb 25, 2008 at 1:29 PM, Brecht
>> wrote:
>>
>> Hi again,
>>
>> The following patch places points on a random location on the
>> surface of a sphere. At least, most points end up there.
>> A lot end up outside the surface and I can't seem to find out
>> why,... As far as I know, my math is right, but that doesn't mean
>> anything.
>>
>> brecht.
>>
>> #P window setfont "Sans Serif" 9.;
>> #P window linecount 1;
>> #P newex 226 328 41 196617 r pulse;
>> #P newex 170 35 41 196617 r pulse;
>> #P newex 170 82 36 196617 buddy;
>> #P newex 67 83 41 196617 s pulse;
>> #P newex 251 103 46 196617 / 1000.;
>> #P newex 170 103 46 196617 / 1000.;
>> #P newex 241 58 70 196617 random 6283;
>> #P newex 170 58 70 196617 random 6283;
>> #P message 241 280 65 196617 clear , dim 0;
>> #P newex 225 353 216 196617 jit.matrix coords 3 float32 @thru 0
>> @adapt 1;
>> #P button 225 280 15 0;
>> #P newex 225 301 216 196617 jit.matrix coords 3 float32 @thru 0
>> @adapt 1;
>> #P newex 225 257 127 196617 jit.matrix bar 3 float32 1;
>> #P newex 164 328 53 196617 jit.concat;
>> #P newex 225 235 64 196617 jit.fill bar 1;
>> #B color 5;
>> #P newex 290 235 64 196617 jit.fill bar 2;
>> #B color 5;
>> #P newex 160 235 64 196617 jit.fill bar 0;
>> #B color 5;
>> #P button 291 168 15 0;
>> #P newex 264 190 72 196617 expr $f1*$f2;
>> #P button 240 168 15 0;
>> #P newex 160 190 101 196617 expr ($f1*-1)* $f2;
>> #P newex 284 145 23 196617 cos;
>> #P newex 251 145 21 196617 sin;
>> #P flonum 251 124 56 9 0.001 6.28318 3 3 0 0 0 221 221 221 222 222
>> 222 0 0 0;
>> #P newex 203 145 23 196617 cos;
>> #P newex 170 145 21 196617 sin;
>> #P flonum 160 215 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
>> #P flonum 170 124 56 9 0.001 6.283 3 3 0 0 0 221 221 221 222 222
>> 222 0 0 0;
>> #P flonum 232 215 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
>> #P flonum 196 215 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
>> #P newex 225 374 108 196617 jit.op @op * @val 0.5;
>> #P newex 334 374 96 196617 jit.gl.handle sphere;
>> #P toggle 10 24 15 0;
>> #P newex 225 396 245 196617 jit.gl.mesh sphere @draw_mode points
>> @color 1 1 1;
>> #P newex 10 120 145 196617 jit.window sphere @floating 1;
>> #P newex 10 60 58 196617 t b b erase;
>> #P newex 10 41 51 196617 qmetro 5;
>> #P newex 10 101 98 196617 jit.gl.render sphere;
>> #P comment 308 280 48 196617 << reset;
>> #P fasten 16 0 19 0 256 164 245 164;
>> #P connect 16 0 18 1;
>> #P fasten 16 0 21 0 256 166 296 166;
>> #P fasten 16 0 20 1 256 164 331 164;
>> #P fasten 10 0 23 0 237 233 295 233;
>> #P connect 15 0 16 0;
>> #P fasten 15 0 17 0 256 142 289 142;
>> #P fasten 13 0 20 0 175 185 269 185;
>> #P fasten 21 0 20 0 296 185 269 185;
>> #P connect 34 0 15 0;
>> #P fasten 36 1 34 0 201 101 256 101;
>> #P connect 37 0 31 0;
>> #P fasten 37 0 32 0 175 55 246 55;
>> #P fasten 20 0 10 0 269 211 237 211;
>> #P connect 8 0 5 0;
>> #P fasten 7 0 5 0 339 393 230 393;
>> #P connect 29 0 8 0;
>> #P fasten 25 0 29 0 169 349 230 349;
>> #P connect 38 0 29 0;
>> #P fasten 30 0 27 0 246 298 230 298;
>> #P connect 28 0 27 0;
>> #P fasten 26 0 25 1 230 277 212 277;
>> #P connect 26 0 28 0;
>> #P fasten 23 0 26 0 295 254 230 254;
>> #P connect 24 0 26 0;
>> #P fasten 22 0 26 0 165 254 230 254;
>> #P fasten 9 0 24 0 201 233 230 233;
>> #P connect 11 0 13 0;
>> #P fasten 11 0 14 0 175 142 208 142;
>> #P fasten 17 0 9 0 289 209 201 209;
>> #P fasten 32 0 36 1 246 78 201 78;
>> #P connect 33 0 11 0;
>> #P connect 36 0 33 0;
>> #P connect 31 0 36 0;
>> #P fasten 27 0 25 0 230 322 169 322;
>> #P connect 12 0 22 0;
>> #P connect 18 0 12 0;
>> #P fasten 14 0 18 0 208 167 165 167;
>> #P fasten 19 0 18 0 245 188 165 188;
>> #P fasten 3 1 35 0 39 80 72 80;
>> #P fasten 3 2 1 0 63 83 15 83;
>> #P connect 3 0 1 0;
>> #P connect 2 0 3 0;
>> #P connect 6 0 2 0;
>> #P window clipboard copycount 39;
>>
>>
>

Brecht's icon

I'm not sure if I understand the random points from a gridshape fully, but I'll give it a go.

As for the speed using get/setcell, I'm not really concerned with this since it is for a non-realtime project anyway...
I might eventually port the code to after effects for easier rendering etc.

Brecht's icon

Looks like the solution was simple.
Now all I need is to get rid of the initial first point...

Max Patch
Copy patch and select New From Clipboard in Max.


Bas van der Graaff's icon

That last one you posted still draws a lot of points that are not on the sphere for me, am i missing something? I don't see how this one differs from your first one.

Wesley Smith's icon

Couldn't you do this by taking the input if jit.noise as though it
were normalized phi/theta angles for spherical coordinates and
performing the appropriate transformation?

wes

On Tue, Feb 26, 2008 at 1:36 AM, Bas van der Graaff wrote:
>
> That last one you posted still draws a lot of points that are not on the sphere for me, am i missing something? I don't see how this one differs from your first one.
>
> --
> SmadSteck - http://www.smadsteck.nl
> Hard- and software for interactive audiovisual sampling
>
>
>

Brecht's icon

Aargh. I'm so sick of this "copy/pasting" not working. It's like my system needs confirmation by doing 'copy' twice.
This should be the correct one.

What you say Wesley will probably work, but I don't understand it. :)

Max Patch
Copy patch and select New From Clipboard in Max.


Wesley Smith's icon
Brecht's icon

The points seem indeed to be bunched near the poles.
This is not such a problem for now. I tried implementing the formulas
on the wolfram page but without succes.
If someone else has managed to get it working properly, I'd be
interested to see the result, but I give up.
I need a math course which doesn't assume that one knows what that S
stands for or what the d means, or the omega.
I got as far as

theta = 2pi*u
and
phi = acos(2*v-1)
(with u and v between 0 and 1)

and then I got x,y,z coordinates from those using the top formulas
form http://ozviz.wasp.uwa.edu.au/~pbourke/texture_colour/spheremap/
but that's not right.

short-circuited-ly yours,

B.

On 26 Feb 2008, at 11:19, Wesley Smith wrote:

Zachary Seldess's icon

Here's how I've done it in the past. Using a table to reduce probability of points near the poles.

Zachary

Max Patch
Copy patch and select New From Clipboard in Max.

Andrew Benson's icon

Hello,
If you don't mind using a vertex shader to do the work, you can use this slightly modified version of the spherical harmonics patch I posted previously to get it working. This also has the problem of distributing more points near the poles, but you might try to work around this by using other types of noise generation (jit.bfg?) or doing some math to manipulate the distribution of theta/phi coordinates. You might also play around with using audio waves to generate a matrix of point locations with jit.poke~.

The vertex shader uses a 3D poltocar equation, so you might be able to extract the necessary math from there and make use of jit.op or jit.expr to apply it.

Hope that helps.

Andrew B.