@@ -680,37 +680,25 @@ unsigned Segment::virtualHeight() const {
680680
681681// Constants for mapping mode "Pinwheel"
682682#ifndef WLED_DISABLE_2D
683- constexpr int Pinwheel_Steps_Small = 72 ; // no holes up to 16x16
684- constexpr int Pinwheel_Size_Small = 16 ; // larger than this -> use "Medium"
685- constexpr int Pinwheel_Steps_Medium = 192 ; // no holes up to 32x32
686- constexpr int Pinwheel_Size_Medium = 32 ; // larger than this -> use "Big"
687- constexpr int Pinwheel_Steps_Big = 304 ; // no holes up to 50x50
688- constexpr int Pinwheel_Size_Big = 50 ; // larger than this -> use "XL"
689- constexpr int Pinwheel_Steps_XL = 368 ;
690- constexpr float Int_to_Rad_Small = (DEG_TO_RAD * 360 ) / Pinwheel_Steps_Small; // conversion: from 0...72 to Radians
691- constexpr float Int_to_Rad_Med = (DEG_TO_RAD * 360 ) / Pinwheel_Steps_Medium; // conversion: from 0...192 to Radians
692- constexpr float Int_to_Rad_Big = (DEG_TO_RAD * 360 ) / Pinwheel_Steps_Big; // conversion: from 0...304 to Radians
693- constexpr float Int_to_Rad_XL = (DEG_TO_RAD * 360 ) / Pinwheel_Steps_XL; // conversion: from 0...368 to Radians
694-
695- constexpr int Fixed_Scale = 512 ; // fixpoint scaling factor (9bit for fraction)
696-
697- // Pinwheel helper function: pixel index to radians
698- static float getPinwheelAngle (int i, int vW, int vH) {
699- int maxXY = max (vW, vH);
700- if (maxXY <= Pinwheel_Size_Small) return float (i) * Int_to_Rad_Small;
701- if (maxXY <= Pinwheel_Size_Medium) return float (i) * Int_to_Rad_Med;
702- if (maxXY <= Pinwheel_Size_Big) return float (i) * Int_to_Rad_Big;
703- // else
704- return float (i) * Int_to_Rad_XL;
705- }
683+ constexpr int Fixed_Scale = 16384 ; // fixpoint scaling factor (14bit for fraction)
706684// Pinwheel helper function: matrix dimensions to number of rays
707685static int getPinwheelLength (int vW, int vH) {
708- int maxXY = max (vW, vH);
709- if (maxXY <= Pinwheel_Size_Small) return Pinwheel_Steps_Small;
710- if (maxXY <= Pinwheel_Size_Medium) return Pinwheel_Steps_Medium;
711- if (maxXY <= Pinwheel_Size_Big) return Pinwheel_Steps_Big;
712- // else
713- return Pinwheel_Steps_XL;
686+ // Returns multiple of 8, prevents over drawing
687+ return (max (vW, vH) + 15 ) & ~7 ;
688+ }
689+ static void setPinwheelParameters (int i, int vW, int vH, int & startx, int & starty, int * cosVal, int * sinVal, bool getPixel = false ) {
690+ int steps = getPinwheelLength (vW, vH);
691+ int baseAngle = ((0xFFFF + steps / 2 ) / steps); // 360° / steps, in 16 bit scale round to nearest integer
692+ int rotate = 0 ;
693+ if (getPixel) rotate = baseAngle / 2 ; // rotate by half a ray width when reading pixel color
694+ for (int k = 0 ; k < 2 ; k++) // angular steps for two consecutive rays
695+ {
696+ int angle = (i + k) * baseAngle + rotate;
697+ cosVal[k] = (cos16 (angle) * Fixed_Scale) >> 15 ; // step per pixel in fixed point, cos16 output is -0x7FFF to +0x7FFF
698+ sinVal[k] = (sin16 (angle) * Fixed_Scale) >> 15 ; // using explicit bit shifts as dividing negative numbers is not equivalent (rounding error is acceptable)
699+ }
700+ startx = (vW * Fixed_Scale) / 2 ; // + cosVal[0] / 4; // starting position = center + 1/4 pixel (in fixed point)
701+ starty = (vH * Fixed_Scale) / 2 ; // + sinVal[0] / 4;
714702}
715703#endif
716704
@@ -845,55 +833,103 @@ void IRAM_ATTR_YN Segment::setPixelColor(int i, uint32_t col) const
845833 for (int x = 0 ; x <= i; x++) setPixelColorXY (x, i, col);
846834 for (int y = 0 ; y < i; y++) setPixelColorXY (i, y, col);
847835 break ;
848- case M12_sPinwheel: {
849- // i = angle --> 0 - 296 (Big), 0 - 192 (Medium), 0 - 72 (Small)
850- float centerX = roundf ((vW-1 ) / 2 .0f );
851- float centerY = roundf ((vH-1 ) / 2 .0f );
852- float angleRad = getPinwheelAngle (i, vW, vH); // angle in radians
853- float cosVal = cos_t (angleRad);
854- float sinVal = sin_t (angleRad);
855-
856- // avoid re-painting the same pixel
857- int lastX = INT_MIN; // impossible position
858- int lastY = INT_MIN; // impossible position
859- // draw line at angle, starting at center and ending at the segment edge
860- // we use fixed point math for better speed. Starting distance is 0.5 for better rounding
861- // int_fast16_t and int_fast32_t types changed to int, minimum bits commented
862- int posx = (centerX + 0 .5f * cosVal) * Fixed_Scale; // X starting position in fixed point 18 bit
863- int posy = (centerY + 0 .5f * sinVal) * Fixed_Scale; // Y starting position in fixed point 18 bit
864- int inc_x = cosVal * Fixed_Scale; // X increment per step (fixed point) 10 bit
865- int inc_y = sinVal * Fixed_Scale; // Y increment per step (fixed point) 10 bit
866-
867- int32_t maxX = vW * Fixed_Scale; // X edge in fixedpoint
868- int32_t maxY = vH * Fixed_Scale; // Y edge in fixedpoint
869-
870- // Odd rays start further from center if prevRay started at center.
871- static int prevRay = INT_MIN; // previous ray number
872- if ((i % 2 == 1 ) && (i - 1 == prevRay || i + 1 == prevRay)) {
873- int jump = min (vW/3 , vH/3 ); // can add 2 if using medium pinwheel
874- posx += inc_x * jump;
875- posy += inc_y * jump;
876- }
877- prevRay = i;
878-
879- // draw ray until we hit any edge
880- while ((posx >= 0 ) && (posy >= 0 ) && (posx < maxX) && (posy < maxY)) {
881- // scale down to integer (compiler will replace division with appropriate bitshift)
882- int x = posx / Fixed_Scale;
883- int y = posy / Fixed_Scale;
884- // set pixel
885- if (x != lastX || y != lastY) setPixelColorXY (x, y, col); // only paint if pixel position is different
886- lastX = x;
887- lastY = y;
888- // advance to next position
889- posx += inc_x;
890- posy += inc_y;
836+ case M12_sPinwheel: {
837+ // Uses Bresenham's algorithm to place coordinates of two lines in arrays then draws between them
838+ int startX, startY, cosVal[2 ], sinVal[2 ]; // in fixed point scale
839+ setPinwheelParameters (i, vW, vH, startX, startY, cosVal, sinVal);
840+
841+ unsigned maxLineLength = max (vW, vH) + 2 ; // pixels drawn is always smaller than dx or dy, +1 pair for rounding errors
842+ uint16_t lineCoords[2 ][maxLineLength]; // uint16_t to save ram
843+ int lineLength[2 ] = {0 };
844+
845+ static int prevRays[2 ] = {INT_MAX, INT_MAX}; // previous two ray numbers
846+ int closestEdgeIdx = INT_MAX; // index of the closest edge pixel
847+
848+ for (int lineNr = 0 ; lineNr < 2 ; lineNr++) {
849+ int x0 = startX; // x, y coordinates in fixed scale
850+ int y0 = startY;
851+ int x1 = (startX + (cosVal[lineNr] << 9 )); // outside of grid
852+ int y1 = (startY + (sinVal[lineNr] << 9 )); // outside of grid
853+ const int dx = abs (x1-x0), sx = x0<x1 ? 1 : -1 ; // x distance & step
854+ const int dy = -abs (y1-y0), sy = y0<y1 ? 1 : -1 ; // y distance & step
855+ uint16_t * coordinates = lineCoords[lineNr]; // 1D access is faster
856+ int * length = &lineLength[lineNr]; // faster access
857+ x0 /= Fixed_Scale; // convert to pixel coordinates
858+ y0 /= Fixed_Scale;
859+
860+ // Bresenham's algorithm
861+ int idx = 0 ;
862+ int err = dx + dy;
863+ while (true ) {
864+ if (unsigned (x0) >= vW || unsigned (y0) >= vH) {
865+ closestEdgeIdx = min (closestEdgeIdx, idx-2 );
866+ break ; // stop if outside of grid (exploit unsigned int overflow)
867+ }
868+ coordinates[idx++] = x0;
869+ coordinates[idx++] = y0;
870+ (*length)++;
871+ // note: since endpoint is out of grid, no need to check if endpoint is reached
872+ int e2 = 2 * err;
873+ if (e2 >= dy) { err += dy; x0 += sx; }
874+ if (e2 <= dx) { err += dx; y0 += sy; }
875+ }
876+ }
877+
878+ // fill up the shorter line with missing coordinates, so block filling works correctly and efficiently
879+ int diff = lineLength[0 ] - lineLength[1 ];
880+ int longLineIdx = (diff > 0 ) ? 0 : 1 ;
881+ int shortLineIdx = longLineIdx ? 0 : 1 ;
882+ if (diff != 0 ) {
883+ int idx = (lineLength[shortLineIdx] - 1 ) * 2 ; // last valid coordinate index
884+ int lastX = lineCoords[shortLineIdx][idx++];
885+ int lastY = lineCoords[shortLineIdx][idx++];
886+ bool keepX = lastX == 0 || lastX == vW - 1 ;
887+ for (int d = 0 ; d < abs (diff); d++) {
888+ lineCoords[shortLineIdx][idx] = keepX ? lastX :lineCoords[longLineIdx][idx];
889+ idx++;
890+ lineCoords[shortLineIdx][idx] = keepX ? lineCoords[longLineIdx][idx] : lastY;
891+ idx++;
892+ }
893+ }
894+
895+ // draw and block-fill the line coordinates. Note: block filling only efficient if angle between lines is small
896+ closestEdgeIdx += 2 ;
897+ int max_i = getPinwheelLength (vW, vH) - 1 ;
898+ bool drawFirst = !(prevRays[0 ] == i - 1 || (i == 0 && prevRays[0 ] == max_i)); // draw first line if previous ray was not adjacent including wrap
899+ bool drawLast = !(prevRays[0 ] == i + 1 || (i == max_i && prevRays[0 ] == 0 )); // same as above for last line
900+ for (int idx = 0 ; idx < lineLength[longLineIdx] * 2 ;) { // !! should be long line idx!
901+ int x1 = lineCoords[0 ][idx];
902+ int x2 = lineCoords[1 ][idx++];
903+ int y1 = lineCoords[0 ][idx];
904+ int y2 = lineCoords[1 ][idx++];
905+ int minX, maxX, minY, maxY;
906+ (x1 < x2) ? (minX = x1, maxX = x2) : (minX = x2, maxX = x1);
907+ (y1 < y2) ? (minY = y1, maxY = y2) : (minY = y2, maxY = y1);
908+
909+ // fill the block between the two x,y points
910+ bool alwaysDraw = (drawFirst && drawLast) || // No adjacent rays, draw all pixels
911+ (idx > closestEdgeIdx) || // Edge pixels on uneven lines are always drawn
912+ (i == 0 && idx == 2 ) || // Center pixel special case
913+ (i == prevRays[1 ]); // Effect drawing twice in 1 frame
914+ for (int x = minX; x <= maxX; x++) {
915+ for (int y = minY; y <= maxY; y++) {
916+ bool onLine1 = x == x1 && y == y1;
917+ bool onLine2 = x == x2 && y == y2;
918+ if ((alwaysDraw) ||
919+ (!onLine1 && (!onLine2 || drawLast)) || // Middle pixels and line2 if drawLast
920+ (!onLine2 && (!onLine1 || drawFirst)) // Middle pixels and line1 if drawFirst
921+ ) {
922+ setPixelColorXY (x, y, col);
923+ }
924+ }
925+ }
926+ }
927+ prevRays[1 ] = prevRays[0 ];
928+ prevRays[0 ] = i;
929+ break ;
891930 }
892- break ;
893931 }
894- }
895- _colorScaled = false ;
896- return ;
932+ return ;
897933 } else if (Segment::maxHeight != 1 && (width () == 1 || height () == 1 )) {
898934 if (start < Segment::maxWidth*Segment::maxHeight) {
899935 // we have a vertical or horizontal 1D segment (WARNING: virtual...() may be transposed)
@@ -1025,31 +1061,17 @@ uint32_t IRAM_ATTR_YN Segment::getPixelColor(int i) const
10251061 break ;
10261062 case M12_sPinwheel:
10271063 // not 100% accurate, returns pixel at outer edge
1028- // i = angle --> 0 - 296 (Big), 0 - 192 (Medium), 0 - 72 (Small)
1029- float centerX = roundf ((vW-1 ) / 2 .0f );
1030- float centerY = roundf ((vH-1 ) / 2 .0f );
1031- float angleRad = getPinwheelAngle (i, vW, vH); // angle in radians
1032- float cosVal = cos_t (angleRad);
1033- float sinVal = sin_t (angleRad);
1034-
1035- int posx = (centerX + 0 .5f * cosVal) * Fixed_Scale; // X starting position in fixed point 18 bit
1036- int posy = (centerY + 0 .5f * sinVal) * Fixed_Scale; // Y starting position in fixed point 18 bit
1037- int inc_x = cosVal * Fixed_Scale; // X increment per step (fixed point) 10 bit
1038- int inc_y = sinVal * Fixed_Scale; // Y increment per step (fixed point) 10 bit
1039- int32_t maxX = vW * Fixed_Scale; // X edge in fixedpoint
1040- int32_t maxY = vH * Fixed_Scale; // Y edge in fixedpoint
1041-
1042- // trace ray from center until we hit any edge - to avoid rounding problems, we use the same method as in setPixelColor
1043- int x = INT_MIN;
1044- int y = INT_MIN;
1045- while ((posx >= 0 ) && (posy >= 0 ) && (posx < maxX) && (posy < maxY)) {
1046- // scale down to integer (compiler will replace division with appropriate bitshift)
1047- x = posx / Fixed_Scale;
1048- y = posy / Fixed_Scale;
1049- // advance to next position
1050- posx += inc_x;
1051- posy += inc_y;
1064+ int x, y, cosVal[2 ], sinVal[2 ];
1065+ setPinwheelParameters (i, vW, vH, x, y, cosVal, sinVal, true );
1066+ int maxX = (vW-1 ) * Fixed_Scale;
1067+ int maxY = (vH-1 ) * Fixed_Scale;
1068+ // trace ray from center until we hit any edge - to avoid rounding problems, we use fixed point coordinates
1069+ while ((x < maxX) && (y < maxY) && (x > Fixed_Scale) && (y > Fixed_Scale)) {
1070+ x += cosVal[0 ]; // advance to next position
1071+ y += sinVal[0 ];
10521072 }
1073+ x /= Fixed_Scale;
1074+ y /= Fixed_Scale;
10531075 return getPixelColorXY (x, y);
10541076 break ;
10551077 }
0 commit comments