Go Down

Topic: a pointer to a multidimensional array (Read 1 time) previous topic - next topic

PaulS

Code: [Select]
ColourBase::ColourBase()
{ float wheelHSBColor[3];
}

What do you think this is doing? You've declared a local variable that immediately goes out of scope. You are not assigning any values to it.

Code: [Select]
class ColourBase
{
  public:
    ColourBase(); 
  int baseHue;           // hue value on the circle 
float *wheelHSBColor;
};

The variable ColourBase::wheelHSBColor is a pointer that points nowhere. This is likely NOT what you want, since the constructor doesn't point it anywhere, either.

Code: [Select]
ColourAtRYBWheel(cArray[i].baseHue, cArray[i].wheelHSBColor);
You are calling this function with a pointer (cArray.wheelHSBColor) that points nowhere.

Code: [Select]
void ColourPalette::ColourAtRYBWheel(int hue, float c[3]) {
 
// Snipped some code

  c[0] = lerp(rybHueWheel[rybWheelIndex], rybHueWheel[rybWheelIndex+1], rybWheelAmount);
  c[1] = lerp(rybHueWheel[4], rybHueWheel[5], rybWheelAmount);
  c[2] = lerp(255, 255, rybWheelAmount); 

And, even though c is not a real array (you have allocated no space for it), you try to write to that space.

As you have figured out, you can't do that (successfully).

kasperkamperman.com

Paul thanks for the great help you provide here in the forum helping to solve some problems.

I think I've solved part of the problem. Stupid indeed not to assign values to the arrays and the ColourBase object.

Although I have the idea that I did everything good now, there still goes something wrong with the c array in combination with the ColourAtRYBWheel function.

My .h file
Code: [Select]

#ifndef ColorLight_h
#define ColorLight_h

#include "WProgram.h"

class ColourBase
{
  public:
    ColourBase(); 
   
  int baseHue;           // hue value on the circle 
int *wheelHSBColor;
};

class ColourPalette
{
  public:
    ColourPalette();
 
int mainHue;
int complementOffset;
int hueAngle;
int complementAngle;

ColourBase *cArray;
void updatePalette();
void ColourAtRYBWheel(int, int*);

int lerpInt(int, int, int, int);
};
#endif


My .cpp file
Code: [Select]

##include "WProgram.h"
#include "ColorLight.h"

const int rybHueWheel[25] =
{ 0  , 12 , 24 , 30 , 36 , 42 , 48 , 54 , 60 , 72 , 84 , 108, 120, 154, 180, 206, 225, 240, 260, 265, 280, 300, 315, 333, 360 };

ColourBase::ColourBase()
{ int wheelHSBColor[] = {0, 0, 0};
}

ColourPalette::ColourPalette()
{
ColourBase cArray[6];

for (int i=0;i<6; i++)
    { cArray[i] = ColourBase();
    }

mainHue = 5;
complementOffset = 0;
hueAngle = 30;
complementAngle = 30;   

updatePalette();
}

void ColourPalette::updatePalette()
{
  Serial.println("update palette");
  cArray[0].baseHue = mainHue;
  cArray[1].baseHue = mainHue - hueAngle;
  cArray[2].baseHue = mainHue + hueAngle;
  cArray[3].baseHue = mainHue + 180 + complementOffset;
  cArray[4].baseHue = cArray[3].baseHue - complementAngle;
  cArray[5].baseHue = cArray[3].baseHue + complementAngle;

  // convert to new hue numbers with the new ColorWheel (HueToRYB)
  for (int i=0; i<6;i++)
  { ColourAtRYBWheel(cArray[i].baseHue, cArray[i].wheelHSBColor);
  }       
}

void ColourPalette::ColourAtRYBWheel(int hue, int c[3]) {
 
  if (hue<0)    hue = 360+hue;     
  hue = hue%360; // stick to 0 - 359

  int rybWheelIndex  = hue/15;      // colourWheel has 24 base colours
  int rybWheelAmount = hue%15;
 
  c[0] = lerpInt(rybHueWheel[rybWheelIndex], rybHueWheel[rybWheelIndex+1], rybWheelAmount,15);

  Serial.print("hue : ");
  Serial.println(hue, DEC);
 
  Serial.print("wheelindex: ");
  Serial.println(rybWheelIndex, DEC);
 
  Serial.print("val in wheelindex: ");
  Serial.println(rybHueWheel[rybWheelIndex], DEC); 
  Serial.print("val in wheelindex +1 : ");
  Serial.println(rybHueWheel[rybWheelIndex+1], DEC);
 
  Serial.print("wheelamount: ");
  Serial.println(rybWheelAmount,DEC);
 
  Serial.print("result in array: ");
 
  Serial.println(c[0],DEC); 
  Serial.print("result direct: ");
  Serial.println(lerpInt(rybHueWheel[rybWheelIndex], rybHueWheel[rybWheelIndex+1], rybWheelAmount,15));

  Serial.println("===================");
}

int ColourPalette::lerpInt(int a, int b, int f, int max)
{ // f is a number between 0 - max
return a + ( (b-a) * f/max );
}


I think something goes wrong with writing values to the c array in the function ColourAtRYBWheel. Probably its still not declared ok, but how can I solve that.

The results I print. In all cases the "result in array" should be the same as "result direct". I two cases thats not the case, so the number changes somewhere in between.
Code: [Select]

update palette
hue : 5
wheelindex: 0
val in wheelindex: 0
val in wheelindex +1 : 12
wheelamount: 5
result in array: 198
result direct: 4
===================
hue : 335
wheelindex: 22
val in wheelindex: 315
val in wheelindex +1 : 333
wheelamount: 5
result in array: 321
result direct: 321
===================
hue : 35
wheelindex: 2
val in wheelindex: 24
val in wheelindex +1 : 30
wheelamount: 5
result in array: 0
result direct: 26
===================
hue : 185
wheelindex: 12
val in wheelindex: 120
val in wheelindex +1 : 154
wheelamount: 5
result in array: 131
result direct: 131
===================
hue : 16
wheelindex: 1
val in wheelindex: 12
val in wheelindex +1 : 0
wheelamount: 1
result in array: 12
result direct: 12
===================
hue : 10
wheelindex: 0
val in wheelindex: 0
val in wheelindex +1 : 12
wheelamount: 10
result in array: 8
result direct: 8
===================?


I've implemented a lerp with only integers, so that part of the problem is solved.

AWOL

Code: [Select]
ColourPalette::ColourPalette()
{
ColourBase cArray[6]; // GET RID OF THIS!

for (int i=0;i<6; i++)
    { cArray[i] = ColourBase();
    }

mainHue = 5;
complementOffset = 0;
hueAngle = 30;
complementAngle = 30;   

updatePalette();
}
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

PaulS

Code: [Select]
int *wheelHSBColor;
Changing the type from float to int doesn't change the fact that this points nowhere.

Code: [Select]
ColourBase::ColourBase()
{ int wheelHSBColor[] = {0, 0, 0};
}

Changing the type from float to int doesn't change the fact that this is a local variable that goes out of scope as soon as the constructor ends. The pointer still points nowhere.

Code: [Select]
  c[0] = lerpInt(rybHueWheel[rybWheelIndex], rybHueWheel[rybWheelIndex+1], rybWheelAmount,15);

The c pointer still points to no reserved space, so you still can't write there.

kasperkamperman.com

I'm puzzled. Its clear for me that changing the type doesn't change anything.
I don't get that I'm pointing to nowhere.

Let me explain how I think and what I want.

I've made a ColourBase object to store different variables and arrays.
I this case I have only the wheelHSBColor array and the baseHue integer, but I'd like to use more arrays and variables.

So I make 6 ColourBase objects in the constructor of the ColourPalette
Code: [Select]

ColourBase cArray[6]; // calling this I allocated the space for an array with 6 ColourBase objects?

for (int i=0;i<6; i++)
{ cArray[i] = ColourBase(); // here I will "fill up" the array with new created ColourBase objects.
}


By calling ColourBase(); I assume that I create 6 ColourBase() objects and also allocate memory space for the wheelHSBColor[] array since I create an array by doing this in the ColourBase constructor:
Code: [Select]

int wheelHSBColor[] = {0, 0, 0};


In the updatePalette() function called from the ColourPalette constructor I will set the value of each baseHue variable in each ColourBase object
Code: [Select]

  cArray[0].baseHue = mainHue;
  cArray[1].baseHue = mainHue - hueAngle;
  cArray[2].baseHue = mainHue + hueAngle;
  cArray[3].baseHue = mainHue + 180 + complementOffset;
  cArray[4].baseHue = cArray[3].baseHue - complementAngle;
  cArray[5].baseHue = cArray[3].baseHue + complementAngle;


Then I'll call the ColourAtRYBWheel function to fill the wheelHSBColor array in each ColourBase object (stored in the cArray)
Code: [Select]

for (int i=0; i<6;i++) {
   ColourAtRYBWheel(cArray[i].baseHue, cArray[i].wheelHSBColor);
}


In my assumption does c[0] point to index[0] of the wheelHSBColor array as index c[1] will point to wheelHSBColor[1].
Since I use a for-loop I think I'll fill each wheelHSBColor array for each ColorBase object.

I hope I cleared up my way of thinking. Hope you can point out what goes wrong. Thank you in advance.

Go Up