# Best way to avoid repeatedly computing the pixels' spherical coordinates?

**URL:** <https://forum.electromage.com/t/best-way-to-avoid-repeatedly-computing-the-pixels-spherical-coordinates/2495>\
**Category:** Patterns and Code\
**Created:** [September 12, 2022, 7:19am UTC](https://forum.electromage.com/t/best-way-to-avoid-repeatedly-computing-the-pixels-spherical-coordinates/2495 "2022-09-12T07:19:44Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![GeekMomProjects](https://forum.electromage.com/user_avatar/forum.electromage.com/geekmomprojects/32/749_2.png) [@GeekMomProjects](https://forum.electromage.com/u/GeekMomProjects)\
**Post date:** [September 12, 2022, 7:19am UTC](https://forum.electromage.com/t/best-way-to-avoid-repeatedly-computing-the-pixels-spherical-coordinates/2495/1 "2022-09-12T07:19:44Z")

</div>

Apologies if this has been discussed before. I searched for “polar” and “spherical” but didn’t see a definitive answer to my question.

I’d like to create a playlist of patterns for pixels that sit on the surface of a sphere (project described in “Show and Tell” [here](https://forum.electromage.com/t/mapping-a-cube-to-a-sphere/2483)). Some patterns use Cartesian coordinates via `render2D(index, x, y)` and others (particularly ones which use the sensor board’s accelerometer) will use each pixel’s spherical coordinates.

The Cartesian pixel map works fine. For the patterns which use spherical coordinates, I’m trying to avoid recomputing them in every single call to `render2D` because I assume the inverse trig calls are computationally expensive.

Is there a best practice for storing the spherical coordinates somewhere in the pattern code or elsewhere?

I’ve seen the trick of simply storing the spherical coordinates in the mapper instead of the Cartesian coordinates, but I’ll need to use both Cartesian AND spherical coordinates depending on the pattern.

My plan is to create global arrays for normalized spherical coordinates in the pattern code and initialize them with negative values. Then, in each call to `render2D(index, x, y)`, check to see if the values have already been computed for the specific pixel index. If they don’t exist, compute and save them in the global array before returning from render2D.

This seems workable, but I was wondering if there might be a better way within the Pixelblaze system?

Thanks,  
Debra

---

<div class="post-metadata">

**Author:** ![sorceror](https://forum.electromage.com/user_avatar/forum.electromage.com/sorceror/32/735_2.png) [@sorceror](https://forum.electromage.com/u/sorceror)\
**Post date:** [September 12, 2022, 4:05pm UTC](https://forum.electromage.com/t/best-way-to-avoid-repeatedly-computing-the-pixels-spherical-coordinates/2495/2 "2022-09-12T16:05:05Z")

</div>

> [@GeekMomProjects](#):
>
> My plan is to create global arrays for normalized spherical coordinates in the pattern code and initialize them with negative values. Then, in each call to `render2D(index, x, y)`, check to see if the values have already been computed for the specific pixel index. If they don’t exist, compute and save them in the global array before returning from render2D.

That would work, but it would probably be more efficient to initialize the whole array at startup. Also it would avoid choppy framerates at startup.

> [@GeekMomProjects](#):
>
> This seems workable, but I was wondering if there might be a better way within the Pixelblaze system?

In a 2D project, I needed x, y, and θ, so I just precalculated them in the pixelmap (that is, θ was stored in z). If you need x,y,z and r,θ,Φ then … yeah, put x,y,z in the pixelmap and calculate the other 3 at startup.

I think it would be great if we could store an arbitrary-length vector in the pixelmap 😃

---

<div class="post-metadata">

**Author:** ![jeff](https://forum.electromage.com/user_avatar/forum.electromage.com/jeff/32/3473_2.png) [@jeff](https://forum.electromage.com/u/jeff)\
**Post date:** [September 12, 2022, 5:15pm UTC](https://forum.electromage.com/t/best-way-to-avoid-repeatedly-computing-the-pixels-spherical-coordinates/2495/3 "2022-09-12T17:15:43Z")

</div>

Hi Debra!

What you describe is very close to how I would do it. In addition, I might swap out the cartesian-to-polar renderer with a polar-lookup renderer after all polar coordinates are computed and cached. This approach made it about 50% faster for me vs computing `hypot()` and `atan2()` on each render, and 7% faster than always checking for an “uncached” flag value (like the negative number)\*.

Here’s an example. Note I didn’t normalize r and phi for this demo, but you can.

```javascript
// Polar cacher - compute and cache polar coords from a cartesian map

var rs = array(pixelCount) // Polar radius from center, positive values
var phis = array(pixelCount) // Polar angle phi from center in [-PI..PI]
var cachedCoordCount = 0 // How many pixels have had their polar coordinates calculated and cached

export function beforeRender(delta) {
  resetTransform()
  translate(-.5, -.5) // Center at (0,0)
  t1 = time(.1) 
  
  // After all polar coords are cached, skip computing them
  if (cachedCoordCount >= pixelCount) render2D = render2DFromPolarCache
}

export function cacheAndRender2D(index, x, y) {
  var r = rs[index] = hypot(x, y)
  var phi = phis[index] = atan2(y, x) // Reminder that output range of atan2 is in [-PI..PI]
  cachedCoordCount++
  renderPolar(index, r, phi)
}

// Use cacheAndRender2D() as render2D() until the cache is populated (should take one frame)
export function render2D(index, x, y) {}
render2D = cacheAndRender2D 

export function render2DFromPolarCache(index, unusedX, unusedY) {
  renderPolar(index, rs[index], phis[index])
}

export function renderPolar(index, r, phi) {
  h = r + t1 + phi / PI2
  hsv(h, 1, 1)
}

```

  
Being able to redefine the function pointer for `beforeRender()`, `render()`, `render2D()`, etc is such a useful trick.

> [@sorceror](#):
>
> more efficient to initialize the whole array at startup

@sorceror I agree, except I’ve had a few cases where `pixelCount` was large and PixelBlaze ran out of time / resources (“Execution steps exhausted”) to precompute the entire cache before getting to the first run of `beforeRender()`. That’s why I sometimes do progressive caching like this.

  
_\* Benchmark conditions: simple 2D renderer, 600 pixels, preview data disabled_

---

<div class="post-metadata">

**Author:** ![wizard](https://forum.electromage.com/user_avatar/forum.electromage.com/wizard/32/4_2.png) [@wizard](https://forum.electromage.com/u/wizard)\
**Post date:** [September 12, 2022, 7:20pm UTC](https://forum.electromage.com/t/best-way-to-avoid-repeatedly-computing-the-pixels-spherical-coordinates/2495/4 "2022-09-12T19:20:56Z")

</div>

See also this thread

> [@Does Caching work?](https://forum.electromage.com/t/does-caching-work/1301):
>
> The question of caching usefulness was raised. It obviously depends on how much math/etc you are doing, but, even in the case of a simple bit of math, on a small number of LEDs, I see sufficient improvements to make it seem worthwhile, so more math, more LEDs, it would only improve. var cache = array(pixelCount) var usecache = 0 for (i = 0; i \< pixelCount; i++){ cache[i] = array(3) // to ensure it's a "array within an array" cache, // a simple array lookup might be even faster, but this…

Generally, the trig math is extremely fast, its all optimized for speed, and the rest of your pattern code will down out the cost of the trig calls.

There’s also overhead in doing the cache, unless you do a trick with swapping out render functions like @jeff mentions, you can easily erase any gains with extra `if()` checks, thats how fast they are.

You might want to use the “Performance test framework” pattern if you want to experiment with micro-optimizations. (Currently on page 5):  
[Performance test framework.epe](https://forum.electromage.com/uploads/short-url/nu7Q2NeicQV81Td5qqKEUTU1BYI.epe) (3.8 KB)

Using this I measure that calling `atan2` and `hypot` 384 times will add about 0.57ms per animation frame more than looking up a value from a cache array (assuming perfectly optimized with no checks). Given all the added complexity of a cache, I would personally opt for the much shorter simpler code.

---

<div class="post-metadata">

**Author:** ![GeekMomProjects](https://forum.electromage.com/user_avatar/forum.electromage.com/geekmomprojects/32/749_2.png) [@GeekMomProjects](https://forum.electromage.com/u/GeekMomProjects)\
**Post date:** [September 12, 2022, 8:03pm UTC](https://forum.electromage.com/t/best-way-to-avoid-repeatedly-computing-the-pixels-spherical-coordinates/2495/5 "2022-09-12T20:03:48Z")

</div>

Thanks for the feedback. I agree it makes more sense to initialize the array at startup. I just don’t know how to access the pixel map from inside the pattern mapper aside from the coordinates that get passed into `render2D`. What’s the mechanism for accessing the pixel map from inside the pattern editor?

---

<div class="post-metadata">

**Author:** ![GeekMomProjects](https://forum.electromage.com/user_avatar/forum.electromage.com/geekmomprojects/32/749_2.png) [@GeekMomProjects](https://forum.electromage.com/u/GeekMomProjects)\
**Post date:** [September 12, 2022, 8:11pm UTC](https://forum.electromage.com/t/best-way-to-avoid-repeatedly-computing-the-pixels-spherical-coordinates/2495/6 "2022-09-12T20:11:02Z")

</div>

Got it. Thank you so much, Jeff, that is extremely helpful and redefining the function pointer is a very clever way to eliminate the initialization check for each pixel!

I also hadn’t thought to put `translate(-.5, -.5)` inside `beforeRender`, but it makes so much sense. This is great - I really appreciate it!

---

<div class="post-metadata">

**Author:** ![GeekMomProjects](https://forum.electromage.com/user_avatar/forum.electromage.com/geekmomprojects/32/749_2.png) [@GeekMomProjects](https://forum.electromage.com/u/GeekMomProjects)\
**Post date:** [September 12, 2022, 8:21pm UTC](https://forum.electromage.com/t/best-way-to-avoid-repeatedly-computing-the-pixels-spherical-coordinates/2495/7 "2022-09-12T20:21:24Z")

</div>

Got it. Thank you so much! The explanation of what changes are actually useful for optimizing speed and which changes aren’t is very helpful. I had wondered how costly the array lookups are, but it seems like general coding canon that inverse trig functions are computationally expensive and to be avoided whenever possible. It’s good to have a bit of a peek behind the curtain sometimes!

I haven’t (yet!) written any patterns fancy enough that I’ll need to worry about optimizing at that level, but I do want to be prepared for the possibility. I’m sure I’m going to want to make an even bigger sphere (or something like it) in the future, in which case the optimizations may well be necessary.

I will definitely be sure to check out the test framework. I’d like to understand how my code changes affect speed on a more granular level, and that seems like the best to go. Thanks for that reference.

Thank you so much! Gonna go play with some patterns now. (Woo hoo!!!)

Best,  
Debra

---

<div class="post-metadata">

**Author:** ![GeekMomProjects](https://forum.electromage.com/user_avatar/forum.electromage.com/geekmomprojects/32/749_2.png) [@GeekMomProjects](https://forum.electromage.com/u/GeekMomProjects)\
**Post date:** [September 12, 2022, 8:27pm UTC](https://forum.electromage.com/t/best-way-to-avoid-repeatedly-computing-the-pixels-spherical-coordinates/2495/8 "2022-09-12T20:27:07Z")

</div>

One quick question about your code. Inside the `cacheAndRender2D` function, you’re just counting the number of times the function is called. I assume, then, that it is guaranteed that each of the first pixelCount calls has a different value for `index`? It would make sense, but I wasn’t sure that I could assume that.

Thanks!  
Debra

---

<div class="post-metadata">

**Author:** ![sorceror](https://forum.electromage.com/user_avatar/forum.electromage.com/sorceror/32/735_2.png) [@sorceror](https://forum.electromage.com/u/sorceror)\
**Post date:** [September 12, 2022, 8:31pm UTC](https://forum.electromage.com/t/best-way-to-avoid-repeatedly-computing-the-pixels-spherical-coordinates/2495/9 "2022-09-12T20:31:27Z")

</div>

> [@GeekMomProjects](#):
>
> What’s the mechanism for accessing the pixel map from inside the pattern editor?

The only mechanism is that the values are passed to render functions like `Render3D(index,x,y,z)`.

---

<div class="post-metadata">

**Author:** ![GeekMomProjects](https://forum.electromage.com/user_avatar/forum.electromage.com/geekmomprojects/32/749_2.png) [@GeekMomProjects](https://forum.electromage.com/u/GeekMomProjects)\
**Post date:** [September 12, 2022, 10:20pm UTC](https://forum.electromage.com/t/best-way-to-avoid-repeatedly-computing-the-pixels-spherical-coordinates/2495/10 "2022-09-12T22:20:57Z")

</div>

Oh, so you’re suggesting that I copy the pre-computed coordinates into an array in the pattern editor then. I thought you were saying there was a way to compute them from the stored Pixel Map before any of the calls to the `render` functions. Got it. Thanks!

---

<div class="post-metadata">

**Author:** ![jeff](https://forum.electromage.com/user_avatar/forum.electromage.com/jeff/32/3473_2.png) [@jeff](https://forum.electromage.com/u/jeff)\
**Post date:** [September 12, 2022, 10:35pm UTC](https://forum.electromage.com/t/best-way-to-avoid-repeatedly-computing-the-pixels-spherical-coordinates/2495/11 "2022-09-12T22:35:55Z")

</div>

> [@GeekMomProjects](#):
>
> I assume, then, that it is guaranteed that each of the first pixelCount calls has a different value for `index`?

Yeah, it happens to work out OK in the current Pixelblaze architecture, though I think you’re right that it’s not guaranteed in most shader frameworks; they reserve the right to call render with some pixel indices more frequently than others, and not necessarily in any order.

So it seems also reasonable to pre-populate the uncached values with a marker value, then only `cachedCoordCount++` when a marker is replaced.

---

<div class="post-metadata">

**Author:** ![GeekMomProjects](https://forum.electromage.com/user_avatar/forum.electromage.com/geekmomprojects/32/749_2.png) [@GeekMomProjects](https://forum.electromage.com/u/GeekMomProjects)\
**Post date:** [September 13, 2022, 1:30am UTC](https://forum.electromage.com/t/best-way-to-avoid-repeatedly-computing-the-pixels-spherical-coordinates/2495/12 "2022-09-13T01:30:31Z")

</div>

Hi All,

Just to follow up, I followed @jeff 's example and made a test using the code below, which can either compute a pixel’s spherical values in every call to `render3D` -or- use cached spherical values it computes once per pixel. Switching between the two modes requires commenting or uncommenting the line `render3D = cacheAndRender3D`

The pattern itself is just a simple moving “bullseye” with hues determined by the distance from a point which oscillates in two dimensions.

For 320 pixels (I’ve only got 5 of the 6 8x8 matrices in my build assembled ATM) the uncached version yields about 55 FPS and the cached version gives about 72 FPS. Which is significant, but I had expected a bigger difference based on my impressions of the computational expense of inverse trig functions. Either I was quite wrong about that or you’ve done some very impressive optimization, @wizard!

Anyhow, thanks again all for the excellent advice, and I learned some very useful concepts from the responses here.

-Debra

```javascript
// Try caching spherical coordinates. All points on a sphere so can ignore rho
function initVals(v,i,a) {a[i] = -1;}
var phiVals = array(pixelCount) // Normalized phi values
phiVals.forEach(initVals) // Initialize phi to -1
var thetaVals = array(pixelCount) // Normalized theta values
export var cachedCount = 0 // Number of cached pixel values

export function beforeRender(delta) {
  t1 = time(.06)
  t2 = time(.023)
  wx = wave(t1)
  wy = wave(t2)
  
  // Have cached all values. Can pull them from arrays instead of doing computation
  if (cachedCount >= pixelCount) render3D = render3DfromCache
}

export function cacheAndRender3D(index, x, y, z) {
  var phi = phiVals[index]
  var theta = thetaVals[index]
  if (phi < 0) {
    x -= .5
    y -= .5
    z -= .5
    theta = thetaVals[index] = 1 + atan2(y,x)/PI2
    phi = phiVals[index] = acos(z/hypot3(x,y,z))/PI
    cachedCount++
  }
  render2D(index, theta, phi)
}

export function render3DfromCache(index, x, y, z) {
  render2D(index, thetaVals[index], phiVals[index])
}

// This function computes spherical values with each call
export function render3D(index, x, y, z) {
  x -= 0.5
  y -= 0.5
  z -= 0.5
  render2D(index, 1 + atan2(y,x)/PI2, acos(z/hypot3(x,y,z))/PI)
}

// Comment out the next line to make the code compute pixel spherical values in 
// each call to render3D. Uncomment it to make the code use cached values instead
render3D = cacheAndRender3D

// Select hue by distance from a point oscillating in x and y
export function render2D(index, x, y) {
  hsv(2*hypot(x-wx,y-wy)+t1,1,1)
}

```
