33 #ifndef vtkOpenGLContextDevice2DPrivate_h 34 #define vtkOpenGLContextDevice2DPrivate_h 92 return this->first == other.first;
129 typename std::list<CacheElement >::iterator it;
130 for (it = this->
Cache.begin(); it != this->
Cache.end(); ++it)
132 it->second.Texture->ReleaseGraphicsResources(window);
148 this->
Cache.pop_back();
150 this->
Cache.push_front(CacheElement(
key, cacheData));
151 return this->
Cache.begin()->second;
171 typename std::list<CacheElement>::iterator it =
172 std::find(this->Cache.begin(), this->Cache.end(), CacheElement(
key));
173 if (it != this->Cache.end())
180 cacheData.Texture->SetInputData(cacheData.ImageData);
181 return this->AddCacheData(
key, cacheData);
187 template <
class StringType>
204 vtkTypeUInt32 hash = static_cast<vtkTypeUInt32>(
id);
208 assert(
"Hash is really a uint32" && static_cast<size_t>(hash) ==
id);
234 static_cast<unsigned char>(
color[1] * 255),
235 static_cast<unsigned char>(
color[2] * 255),
236 static_cast<unsigned char>(textProperty->
GetOpacity() * 255));
336 bool colorDiffer =
false;
337 while(!colorDiffer && i < 4)
355 glEnable(capability);
359 glDisable(capability);
365 float* texCoord =
new float[2*n];
366 float minX = f[0];
float minY = f[1];
367 float maxX = f[0];
float maxY = f[1];
369 for(
int i = 0; i < n; ++i)
371 minX = fptr[0] < minX ? fptr[0] : minX;
372 maxX = fptr[0] > maxX ? fptr[0] : maxX;
373 minY = fptr[1] < minY ? fptr[1] : minY;
374 maxY = fptr[1] > maxY ? fptr[1] : maxY;
381 float rangeX = (textureBounds[1] - textureBounds[0]) ?
382 textureBounds[1] - textureBounds[0] : 1.;
383 float rangeY = (textureBounds[3] - textureBounds[2]) ?
384 textureBounds[3] - textureBounds[2] : 1.;
385 for (
int i = 0; i < n; ++i)
387 texCoord[i*2] = (fptr[0]-minX) / rangeX;
388 texCoord[i*2+1] = (fptr[1]-minY) / rangeY;
394 float rangeX = (maxX - minX)? maxX - minX : 1.f;
395 float rangeY = (maxY - minY)? maxY - minY : 1.f;
396 for (
int i = 0; i < n; ++i)
398 texCoord[i*2] = (fptr[0]-minX)/rangeX;
399 texCoord[i*2+1] = (fptr[1]-minY)/rangeY;
409 for (
int i = 0; i < 2; ++i)
411 while (pow2[i] <
size[i])
423 vtkGenericWarningMacro(
"Invalid image format: expected unsigned char.");
426 int bytesPerPixel =
image->GetNumberOfScalarComponents();
431 for (
int i = 0; i < 2; ++i)
433 texCoords[i] =
size[i] /
float(newImg[i]);
436 unsigned char *dataPtr =
437 new unsigned char[newImg[0] * newImg[1] * bytesPerPixel];
438 unsigned char *origPtr =
439 static_cast<unsigned char*>(
image->GetScalarPointer());
441 for (
int i = 0; i < newImg[0]; ++i)
443 for (
int j = 0; j < newImg[1]; ++j)
445 for (
int k = 0; k < bytesPerPixel; ++k)
449 dataPtr[i * bytesPerPixel + j * newImg[0] * bytesPerPixel + k] =
450 origPtr[i * bytesPerPixel + j *
size[0] * bytesPerPixel + k];
454 dataPtr[i * bytesPerPixel + j * newImg[0] * bytesPerPixel + k] =
462 GLint glFormat = bytesPerPixel == 3 ? GL_RGB : GL_RGBA;
463 GLint glInternalFormat = bytesPerPixel == 3 ? GL_RGB8 : GL_RGBA8;
465 glGenTextures(1, &tmpIndex);
466 glBindTexture(GL_TEXTURE_2D, tmpIndex);
468 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
469 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
470 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
472 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
475 glTexImage2D(GL_TEXTURE_2D, 0 , glInternalFormat,
476 newImg[0], newImg[1], 0, glFormat,
477 GL_UNSIGNED_BYTE, static_cast<const GLvoid *>(dataPtr));
486 cout <<
"Error = not an unsigned char..." << endl;
489 int bytesPerPixel =
image->GetNumberOfScalarComponents();
493 unsigned char *dataPtr =
494 static_cast<unsigned char*>(
image->GetScalarPointer());
496 GLint glFormat = bytesPerPixel == 3 ? GL_RGB : GL_RGBA;
497 GLint glInternalFormat = bytesPerPixel == 3 ? GL_RGB8 : GL_RGBA8;
499 glGenTextures(1, &tmpIndex);
500 glBindTexture(GL_TEXTURE_2D, tmpIndex);
502 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
503 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
504 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
506 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
509 glTexImage2D(GL_TEXTURE_2D, 0 , glInternalFormat,
511 GL_UNSIGNED_BYTE, static_cast<const GLvoid *>(dataPtr));
590 this->Colors = colors;
596 this->DrawLines(cellArray, scalarMode, x, y,
scale);
600 this->DrawPolygons(cellArray, scalarMode, x, y,
scale);
612 void MapCurrentCell (
float const posX,
float const posY,
float const scale,
615 this->CellPoints.reserve(this->NumPointsCell * 2);
617 for (
int i = 0; i < this->NumPointsCell; i++)
623 float const x = static_cast<float>(
point[0]) + posX;
624 float const y = static_cast<float>(
point[1]) + posY;
625 this->CellPoints.push_back(x *
scale);
626 this->CellPoints.push_back(y *
scale);
633 mappedColorId = this->PointIds[i];
636 mappedColorId = cellId;
639 std::cerr <<
"Scalar mode not supported!" << std::endl;
643 this->CellColors->
SetTuple(i, mappedColorId, this->Colors);
652 void DrawLines(
vtkCellArray* cellArray,
int scalarMode,
float const x,
653 float const y,
float const scale)
655 if (cellArray->
GetMTime() > this->LinesLoadingTime)
658 this->LineColors->
Reset();
662 this->Lines.reserve(numVertices * 2);
669 this->PointIds); cellId++)
671 this->MapCurrentCell(x, y,
scale, cellId, scalarMode);
674 for (
int i = 0; i < this->NumPointsCell; i++)
676 this->Lines.push_back(this->CellPoints[2 * i]);
677 this->Lines.push_back(this->CellPoints[2 * i + 1]);
679 double* color4 = this->CellColors->
GetTuple(i);
680 this->LineColors->
InsertTuple4(vertOffset + i, color4[0], color4[1], color4[2],
684 vertOffset += this->NumPointsCell;
685 this->CellColors->
Reset();
686 this->CellPoints.clear();
692 this->Device->
DrawLines(&this->Lines[0], this->Lines.size() / 2,
706 this->PointIds); cellId++)
708 numTriVert += 3 * (this->NumPointsCell - 2);
719 void DrawPolygons(
vtkCellArray* cellArray,
int scalarMode,
float const x,
720 float const y,
float const scale)
722 if (cellArray->
GetMTime() > this->PolygonsLoadingTime)
724 this->PolyTri.clear();
725 this->PolyColors->
Reset();
728 vtkIdType const totalTriVert = this->GetCountTriangleVertices(cellArray);
729 this->PolyTri.reserve(totalTriVert * 2);
738 this->PointIds); cellId++)
740 this->MapCurrentCell(x, y,
scale, cellId, scalarMode);
743 for (
int i = 0; i < this->NumPointsCell - 2; i++)
745 this->PolyTri.push_back(this->CellPoints[0]);
746 this->PolyTri.push_back(this->CellPoints[1]);
747 this->PolyTri.push_back(this->CellPoints[i * 2 + 2]);
748 this->PolyTri.push_back(this->CellPoints[i * 2 + 3]);
749 this->PolyTri.push_back(this->CellPoints[i * 2 + 4]);
750 this->PolyTri.push_back(this->CellPoints[i * 2 + 5]);
753 vtkIdType const triangOffset = vertOffset + 3 * i;
754 double* color4 = this->CellColors->
GetTuple(0);
755 this->PolyColors->
InsertTuple4(triangOffset, color4[0], color4[1],
756 color4[2], color4[3]);
758 color4 = this->CellColors->
GetTuple(i + 1);
759 this->PolyColors->
InsertTuple4(triangOffset + 1, color4[0], color4[1],
760 color4[2], color4[3]);
762 color4 = this->CellColors->
GetTuple(i + 2);
763 this->PolyColors->
InsertTuple4(triangOffset + 2, color4[0], color4[1],
764 color4[2], color4[3]);
767 vertOffset += 3 * (this->NumPointsCell - 2);
768 this->CellColors->
Reset();
769 this->CellPoints.clear();
772 this->PolygonsLoadingTime.
Modified();
776 static_cast<unsigned char*>(this->PolyColors->
GetVoidPointer(0)), 4);
790 std::vector<float> CellPoints;
798 std::vector<float> PolyTri;
807 std::vector<float> Lines;
812 #endif // VTKOPENGLCONTEXTDEVICE2DPRIVATE_H
GLuint TextureFromImage(vtkImageData *image)
void InsertTuple4(vtkIdType tupleIdx, double val0, double val1, double val2, double val3)
These methods are included as convenience for the wrappers.
void DrawLines(float *f, int n, unsigned char *colors=nullptr, int nc_comps=0) override
Draw lines using the points - memory layout is as follows: l1p1,l1p2,l2p1,l2p2...
virtual void * GetVoidPointer(vtkIdType valueIdx)=0
Return a void pointer.
virtual int GetJustification()
Set/Get the horizontal justification to left (default), centered, or right.
bool operator==(const CacheElement &other) const
void SetGLCapability(GLenum capability, GLboolean state)
double * GetBounds()
Return a pointer to the geometry bounding box in the form (xmin,xmax, ymin,ymax, zmin,...
virtual double * GetTuple(vtkIdType tupleIdx)=0
Get the data tuple at tupleIdx.
vtkTextRenderer::Metrics Metrics
record modification and/or execution time
GLfloat SavedClearColor[4]
Class for drawing 2D primitives using OpenGL 1.1+.
void SetTuple(vtkIdType dstTupleIdx, vtkIdType srcTupleIdx, vtkAbstractArray *source) override
Set the tuple at dstTupleIdx in this array to the tuple at srcTupleIdx in the source array.
void Modified()
Set this objects time to the current time.
void InitTraversal()
A cell traversal methods that is more efficient than vtkDataSet traversal methods.
GLuint TextureFromImage(vtkImageData *image, vtkVector2f &texCoords)
vtkTypeUInt32 TextPropertyId
TextPropertyKey(vtkTextProperty *textProperty, const StringType &text, int dpi)
Creates a TextPropertyKey.
int GetNumberOfComponents()
Set/Get the dimension (n) of the components.
virtual vtkIdType GetNumberOfCells()
Get the number of cells in the array.
CellArrayHelper(vtkOpenGLContextDevice2D *device)
std::list< CacheElement > Cache
List of a pair of key and cache data.
static vtkSmartPointer< T > New()
Create an instance of a VTK object.
CacheElement(const Key &key, const CacheData &cacheData)
void ReleaseGraphicsResources(vtkWindow *window)
Release all the OpenGL Pixel Buffer Object(PBO) associated with the textures of the cache list.
vtkSmartPointer< vtkTexture > Texture
float * TexCoords(float *f, int n)
TextPropertyKey< vtkStdString > UTF8TextPropertyKey
window superclass for vtkRenderWindow
size_t MaxSize
Maximum size the cache list can be.
bool operator==(const TextPropertyKey &other) const
Compares two TextPropertyKeys with each other.
#define VTK_SCALAR_MODE_USE_POINT_DATA
vtkTextureImageCache< UTF16TextPropertyKey > TextTextureCache
Cache for text images.
vtkTextureImageCache< UTF8TextPropertyKey > MathTextTextureCache
Cache for text images.
topologically and geometrically regular array of data
vtkImageData * GetInput()
Get the input as a vtkImageData object.
virtual vtkMTimeType GetMTime()
Return this object's modified time.
Some derived classes for the different vectors commonly used.
virtual int GetVerticalJustification()
Set/Get the vertical justification to bottom (default), middle, or top.
vtkTexture * SpriteTexture
CacheData & GetCacheData(const Key &key)
Return the cache associated to a key.
void Set(const T &red, const T &green, const T &blue)
Set the red, green and blue components of the color.
bool IsKeyInCache(const Key &key) const
Search the cache list to see if a given key already exists.
handles properties associated with a texture map
vtkTextureImageCache()
Construct a texture image cache with a maximum number of texture of 50.
virtual double GetOpacity()
Set/Get the text's opacity.
double * GetPoint(vtkIdType id)
Return a pointer to a double point x[3] for a specific id.
represent text properties.
void Reset()
Reset to an empty state, without freeing any memory.
dynamic, self-adjusting array of unsigned char
void CoreDrawTriangles(std::vector< float > &tverts, unsigned char *colors=nullptr, int numComp=0)
virtual void SetNumberOfComponents(int)
Set/Get the dimension (n) of the components.
object to represent cell connectivity
CacheElement(const Key &key)
virtual double * GetColor()
Set the color of the text.
#define VTK_UNSIGNED_CHAR
unsigned int TextureProperties
int GetNextCell(vtkIdType &npts, vtkIdType *&pts)
A cell traversal methods that is more efficient than vtkDataSet traversal methods.
CacheElement associates a unique key to some cache.
vtkVector2i FindPowerOfTwo(const vtkVector2i &size)
virtual int GetFontSize()
Set/Get the font size (in points).
virtual void SetNumberOfTuples(vtkIdType numTuples)=0
Set the number of tuples (a component group) in the array.
TextPropertyKey< vtkUnicodeString > UTF16TextPropertyKey
static vtkTypeUInt32 GetIdFromTextProperty(vtkTextProperty *tprop)
Transform a text property into an unsigned long.
#define VTK_SCALAR_MODE_USE_CELL_DATA
CacheData & AddCacheData(const Key &key, const CacheData &cacheData)
Add a new cache entry into the cache list.
vtkSmartPointer< vtkImageData > ImageData
GLboolean SavedStencilTest
represent and manipulate 3D points
void RestoreGLState(bool colorBuffer=false)
void Draw(int cellType, vtkCellArray *cellArray, vtkPoints *points, float x, float y, float scale, int scalarMode, vtkUnsignedCharArray *colors=nullptr)
Draw primitives as specified by cellType.
void SaveGLState(bool colorBuffer=false)
virtual void Delete()
Delete a VTK object.