Rotate nxn matrix / array / TImage


I have a nxn matrix of values that I want to rotate by a number of arbitrary angles around its center in order to finally sum up all the matrices. Of course, I’d have to resize to Sqrt(2)*n x Sqrt(2)*n matrices and fill up some zeros to make it work.

Now I’m looking for a fast way to do it. Is there anything available in ROOT for such a task? I have not found anything in TMatrix…

My second thought was: Hey, it’s like rotating an image, so why not feed the matrix into a TImage, rotate and then read out the pixel array again. Unfortunately, there is no such thing as image->Rotate(x, y, anyangle), only image->Flip(…) will do multiples of 90 deg. Or have I overlooked something?

Edit: Actually, the flip isn’t even an option at all:
“Warning in TASImage::Flip: flip does not work for data images”

Just for reference and closure of thread.

Since in the end I’m only interested in a TH2D histogram with the superposition of the rotated matrices, the quickest way seems to be rotating each each entry and then filling the histo directly, roughly like that:

// put coordinate origin in center of matrix for easy rotation handling
TMatrixD values( -nrows/2, nrows/2,  -ncols/2, ncols/2, data );

TH2D histo( "his", "superposition histo", ncols+1, -ncols/2, ncols/2, nrows+1,-nrows/2, nrows/2 );

angleStep = 360. / 10.;   // etc.
for( double angle = 0.; angle < 360.; angle += angleStep){
    for( int x = -ncols/2; x < ncols/2 + 1; x++){
        for( int y = -nrows/2; y < nrows/2 + 1; y++){
            TVector3 pixVal( x, y,  values[y][x] );
            pixVal.RotateZ( angle * TMath::DegToRad() );  // z-Value not affected
            histo->Fill( pixVal.X(), pixVal.Y(), pixVal.Z() );

Of course, one can make it infinitely more complicated, also for an image one might have to correct for partial volume effect of overlapping pixels etc., but this is good enough for me…