home *** CD-ROM | disk | FTP | other *** search
- // GptCell.cpp - pcGPlot cell array stuff
- // copyright 1992 Pittsburgh Supercomputing Center
- #include "gpt.h"
- #include "cgm.h" // basic display classes
- #include "cgmdisp.h" // basic display classes
- #include "gptDisp.h"
-
- // *** pcDisplay::cells
- int
- pcDisplay::cells(const cellArray *inCarray)
- {
- HBITMAP hOldBitmap, hBitmap;
- int *scaledPt;
- POINT *scaledPtr;
- int ret = 0;
- int xMin, yMin, xMax, yMax, i;
- unsigned long myWidth, myHeight;
- unsigned long cellnx, cellny;
- BOOL isRect = FALSE;
- RECT rr, srcRect;
-
- if ( !inCarray || !inCarray->corners()) return 1;
-
- const vdcPts *corners = inCarray->corners();
- getPts(corners, scaledPt); // get the scaled corner pts
- scaledPtr = (POINT *)scaledPt;
- // #define DEBUG
- #ifdef DEBUG
- // Draw Green Rectangle on Display which cell array will cover
- COLORREF RGB = RGB(0,255,0);
- SetBackBrush(RGB);
- SetHollowPen(RGB);
- Rectangle(hdc,scaledPtr[0].x,scaledPtr[0].y,scaledPtr[1].x,scaledPtr[1].x);
- Rectangle(hdcMeta,scaledPtr[0].x,scaledPtr[0].y,scaledPtr[1].x,scaledPtr[1].x) ;
- #endif
-
- cellnx = inCarray->nx();
- cellny = inCarray->ny();
- ////
- // figure out the upper left corner, width, height, y increases down
- // use the full enclosing rectangle
- // start out at the fourth point (so as to cover all 4)
-
- xMin = xMax = scaledPtr[0].x + scaledPtr[1].x - scaledPtr[2].x;
- yMin = yMax = scaledPtr[0].y + scaledPtr[1].y - scaledPtr[2].y;
- POINT ClipVert[5];
- ClipVert[0] = scaledPtr[0];
- ClipVert[1] = scaledPtr[2];
- ClipVert[2] = scaledPtr[1];
- ClipVert[3].x = xMax;
- ClipVert[3].y = yMax;
- ClipVert[4] = scaledPtr[0];
-
- HRGN clip = CreatePolygonRgn(ClipVert, 5, ALTERNATE);
- for (i=0; i<3; ++i)
- {
- if (scaledPtr[i].x < xMin) xMin = scaledPtr[i].x;
- if (scaledPtr[i].x > xMax) xMax = scaledPtr[i].x;
- if (scaledPtr[i].y < yMin) yMin = scaledPtr[i].y;
- if (scaledPtr[i].y > yMax) yMax = scaledPtr[i].y;
- }
-
- // let's make sure things fit
- if (xMin < 0) xMin = 0;
- if (yMin < 0) yMin = 0;
- if (xMax > width()) xMax = width();
- if (yMax > height()) yMax = height();
-
- myWidth = xMax - xMin;
- myHeight = yMax - yMin;
- if (myWidth == abs((int)(scaledPtr[1].x - scaledPtr[0].x)) &&
- myHeight == abs((int)(scaledPtr[1].y - scaledPtr[0].y)) )
- isRect = TRUE;
- // shift the scaled Ptr for the cellarray
- for (i=0; i<3; ++i) { scaledPtr[i].x -= xMin; scaledPtr[i].y -= yMin; }
-
- srcRect.top = 0;
- srcRect.left = 0;
-
- if (isRect && ( cellnx*cellny < myWidth *myHeight ) )
- {
- hBitmap = CreateCompatibleBitmap(hdc, cellnx, cellny);
- srcRect.right = cellnx;
- srcRect.bottom = cellny;
- }
- else
- {
- hBitmap = CreateCompatibleBitmap(hdc, myWidth, myHeight);
- srcRect.right = myWidth;
- srcRect.bottom = myHeight;
- }
-
- HDC hMemoryDC = CreateCompatibleDC(hdc);
- hOldBitmap = SelectObject( hMemoryDC, hBitmap);
- if (cgmWin->hPal)
- {
- HPALETTE foo = SelectPalette(hMemoryDC,cgmWin->hPal,0);
- int snow = RealizePalette(hMemoryDC);
- }
-
- // Fill Bitmap in with background color
- LOGBRUSH lBrush;
- lBrush.lbStyle = BS_SOLID;
- lBrush.lbColor = RGBbackground;
- HBRUSH hBrush = CreateBrushIndirect((LOGBRUSH FAR *)&lBrush);
- //HBRUSH hOldBrush = SelectObject(hdc, (HANDLE)hBrush);
- //SelectObject(hdcMeta, (HANDLE)hBrush);
- //PatBlt(hMemoryDC,0,0,srcRect.right, srcRect.bottom,PATCOPY);
- //SelectObject(hMemoryDC, (HANDLE)hOldBrush);
- //DeleteObject( hBrush);
-
- HBRUSH hOldBrush = SelectObject(hMemoryDC, (HANDLE)hBrush);
- SelectObject(hMemoryDC, (HANDLE)hBrush);
- PatBlt(hMemoryDC,0,0,srcRect.right, srcRect.bottom,PATCOPY);
- SelectObject(hMemoryDC, (HANDLE)hOldBrush);
- DeleteObject( hBrush);
-
-
- // do fill for pc
- inCarray->fill( scaledPtr, myWidth, myHeight, hMemoryDC );
- //for (int xInd = 0; xInd < srcRect.right; xInd +=3)
- // for (int yInd = 0; yInd < srcRect.bottom; yInd +=3)
- // SetPixel(hMemoryDC,xInd, yInd, RGB(255,0,0));
-
- SelectObject( hdc, clip);
- //SelectObject(hdcMeta,clip);
- DeleteObject( clip);
- StretchBlt(hdc, xMin, yMin,xMax -xMin, yMax-yMin, hMemoryDC,0,0, srcRect.right,
- srcRect.bottom, SRCCOPY);
- StretchBlt(hdcMeta, xMin, yMin,xMax -xMin, yMax-yMin, hMemoryDC,0,0, srcRect.right,
- srcRect.bottom, SRCCOPY);
-
- SelectObject(hMemoryDC, hOldBitmap);
- DeleteDC(hMemoryDC);
- DeleteObject(hBitmap);
-
-
- // Restore original clip
- ClipVert[0].x = clipr.left;
- ClipVert[0].y = clipr.top;
- ClipVert[1].x = clipr.right;
- ClipVert[1].y = clipr.top;
- ClipVert[2].x = clipr.right;
- ClipVert[2].y = clipr.bottom;
- ClipVert[3].x = clipr.left;
- ClipVert[3].y = clipr.bottom;
- ClipVert[4] = ClipVert[0];
- clip = CreatePolygonRgn(ClipVert, 5, ALTERNATE);
- SelectClipRgn(hdc, clip);
- //SelectClipRgn(hdcMeta, clip);
- DeleteObject(clip);
-
- delete scaledPt;
- return 0;
- }
-
-
-
-
- // cell array routine to fill out cell array bitmap
- // *** cellArray::fill
- void
- cellArray::fill(POINT *inPts, unsigned int inWidth, unsigned int inHeight
- , HDC hdcMem) const
- {
- const unsigned long PAL = 0X2000000 ;
-
- long ix, iy, newX, newY, oldX, oldY, cellX, cellY;
- COLORREF myPixelColor;
- int topX, topY;
- float *myColrs, rx, ry;
- double axx, axy, ayx, ayy; // xform matrix from X space to cell array space
- long Qx, Qy, Rx,Ry;
- long det;
- double ddet;
- DWORD stat;
- BOOL isRect1, isRect2;
- BOOL GotColor = FALSE;
- BOOL SetColor = FALSE;
-
- cellX = this->myNx;
- cellY = this->myNy;
- isRect1 = isRect2 = FALSE;
- // get the offsets from the first co-ord, P
- Qx = inPts[1].x - inPts[0].x;
- Qy = inPts[1].y - inPts[0].y;
- Rx = inPts[2].x - inPts[0].x;
- Ry = inPts[2].y - inPts[0].y;
-
- // See if rectangular and no floating point cell array - memory xformation needed
- if ( (Rx == 0 && Ry == Qy) || ( Ry == 0 && Rx == Qx))
- {
- if ( cellX*cellY <= (long)((long)inHeight*inWidth))
- isRect1 = TRUE; // filling in cellX by cellY memory size
- else isRect2 = TRUE; // filling in inWidth by inHeight memory size
- }
- else
- {
- // now the determinant of the transformation
- det = Rx * Qy - Ry * Qx;
- ddet = (double) det; // take care of ULTRIX C bug
-
- if (!det) return; // P, Q, and R are co-linear ( gads!!)
- // now the actual matrix components
- axx = (cellX * (Qy - Ry) )/ ddet;
- axy = (cellX * (Rx - Qx) ) / ddet;
- ayx = - (cellY * Ry )/ ddet;
- ayy = (cellY * Rx )/ ddet;
- }
-
- if (isRect1) { topY = cellY; topX = cellX; }
- else { topY = inHeight; topX = inWidth; }
- //if (topY > 100) topY =100;
- //if (topX > 100) topX = 100;
- for (iy=0; iy < topY; ++iy)
- {
- if (isRect1)
- {
- newY = iy;
- if (Qy < 0) newY = cellY - newY -1;
- }
- else if (isRect2)
- {
- oldY = iy - inPts[0].y;
- newY = (oldY * cellY)/Qy;
- }
- else
- oldY = iy - inPts[0].y;
-
- for (ix=0; ix < topX; ++ix)
- {
- if (isRect1)
- {
- newX = ix;
- if (Qx < 0) newX = cellX - newX -1;
- }
- else if (isRect2)
- {
- oldX = ix - inPts[0].x;
- newX = (oldX * cellX)/Qx;
- }
- else
- {
- oldX = ix - inPts[0].x;
- rx = axx * oldX + axy * oldY;
- ry = ayx * oldX + ayy * oldY;
- newX = (int) rx;
- newY = (int) ry;
- }
-
- // is rectangular or the transformed pt in the affected area ?
- if ( isRect1 || isRect2 || ((rx >= 0)
- && (rx < cellX) && (ry >= 0) && (ry < cellY)))
- {
- myColrs = getFloats(newX, newY);
- myPixelColor = RGB( (BYTE)(myColrs[0] * 0XFF),
- (BYTE)(myColrs[1] * 0XFF),
- (BYTE)(myColrs[2] * 0XFF)
- ) | PAL;
- if (myPixelColor & 0XFFFFFF)
- {
- GotColor = TRUE;
- }
- stat = SetPixel(hdcMem,ix, iy, myPixelColor);
- if (stat)
- {
- SetColor = TRUE;;
- }
- }
- }
- }
- int moo = 0; // for debug stop
- }
-
-
- // *********************** set_rect() **************************
- RECT *set_rect(RECT *rectangle, short top, short left,
- short bottom, short right)
- {
- rectangle->top = top;
- rectangle->left =left;
- rectangle->bottom = bottom;
- rectangle->right = right;
- return rectangle;
- }
-