21#ifndef vtkOpenGLContextDevice2DPrivate_h
22#define vtkOpenGLContextDevice2DPrivate_h
46VTK_ABI_NAMESPACE_BEGIN
89 return this->first == other.first;
105 return std::find(this->
Cache.begin(), this->Cache.end(), key) != this->
Cache.end();
123 typename std::list<CacheElement>::iterator it;
124 for (it = this->
Cache.begin(); it != this->
Cache.end(); ++it)
126 it->second.Texture->ReleaseGraphicsResources(window);
140 if (this->
Cache.size() >= this->MaxSize)
142 this->
Cache.pop_back();
145 return this->
Cache.begin()->second;
165 typename std::list<CacheElement>::iterator it =
166 std::find(this->Cache.begin(), this->Cache.end(),
CacheElement(key));
167 if (it != this->Cache.end())
175 return this->AddCacheData(key, cacheData);
181template <
class StringType>
198 vtkTypeUInt32 hash =
static_cast<vtkTypeUInt32
>(id);
202 assert(
"Hash is really a uint32" &&
static_cast<size_t>(hash) ==
id);
235 this->
Color.
Set(
static_cast<unsigned char>(color[0] * 255),
236 static_cast<unsigned char>(color[1] * 255),
static_cast<unsigned char>(color[2] * 255),
237 static_cast<unsigned char>(textProperty->
GetOpacity() * 255));
329 ::glDrawBuffers(1, bufs);
339 float* texCoord =
new float[2 * n];
345 for (
int i = 0; i < n; ++i)
347 minX = fptr[0] < minX ? fptr[0] : minX;
348 maxX = fptr[0] > maxX ? fptr[0] : maxX;
349 minY = fptr[1] < minY ? fptr[1] : minY;
350 maxY = fptr[1] > maxY ? fptr[1] : maxY;
358 (textureBounds[1] - textureBounds[0]) ? textureBounds[1] - textureBounds[0] : 1.;
360 (textureBounds[3] - textureBounds[2]) ? textureBounds[3] - textureBounds[2] : 1.;
361 for (
int i = 0; i < n; ++i)
363 texCoord[i * 2] = (fptr[0] - minX) / rangeX;
364 texCoord[i * 2 + 1] = (fptr[1] - minY) / rangeY;
370 float rangeX = (maxX - minX) ? maxX - minX : 1.f;
371 float rangeY = (maxY - minY) ? maxY - minY : 1.f;
372 for (
int i = 0; i < n; ++i)
374 texCoord[i * 2] = (fptr[0] - minX) / rangeX;
375 texCoord[i * 2 + 1] = (fptr[1] - minY) / rangeY;
385 for (
int i = 0; i < 2; ++i)
387 while (pow2[i] < size[i])
399 vtkGenericWarningMacro(
"Invalid image format: expected unsigned char.");
402 int bytesPerPixel = image->GetNumberOfScalarComponents();
404 image->GetDimensions(size);
407 for (
int i = 0; i < 2; ++i)
409 texCoords[i] = size[i] / float(newImg[i]);
412 unsigned char* dataPtr =
new unsigned char[newImg[0] * newImg[1] * bytesPerPixel];
413 unsigned char* origPtr =
static_cast<unsigned char*
>(image->GetScalarPointer());
415 for (
int i = 0; i < newImg[0]; ++i)
417 for (
int j = 0; j < newImg[1]; ++j)
419 for (
int k = 0; k < bytesPerPixel; ++k)
421 if (i < size[0] && j < size[1])
423 dataPtr[i * bytesPerPixel + j * newImg[0] * bytesPerPixel + k] =
424 origPtr[i * bytesPerPixel + j * size[0] * bytesPerPixel + k];
428 dataPtr[i * bytesPerPixel + j * newImg[0] * bytesPerPixel + k] = k == 3 ? 0 : 255;
435 GLint glFormat = bytesPerPixel == 3 ? GL_RGB : GL_RGBA;
436 GLint glInternalFormat = bytesPerPixel == 3 ? GL_RGB8 : GL_RGBA8;
438 glGenTextures(1, &tmpIndex);
439 glBindTexture(GL_TEXTURE_2D, tmpIndex);
441 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_NEAREST);
442 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
GL_NEAREST);
446 glTexImage2D(GL_TEXTURE_2D, 0, glInternalFormat, newImg[0], newImg[1], 0, glFormat,
456 cout <<
"Error = not an unsigned char..." << endl;
459 int bytesPerPixel = image->GetNumberOfScalarComponents();
461 image->GetDimensions(size);
463 unsigned char* dataPtr =
static_cast<unsigned char*
>(image->GetScalarPointer());
465 GLint glFormat = bytesPerPixel == 3 ? GL_RGB : GL_RGBA;
466 GLint glInternalFormat = bytesPerPixel == 3 ? GL_RGB8 : GL_RGBA8;
468 glGenTextures(1, &tmpIndex);
469 glBindTexture(GL_TEXTURE_2D, tmpIndex);
471 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_NEAREST);
472 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
GL_NEAREST);
476 glTexImage2D(GL_TEXTURE_2D, 0, glInternalFormat, size[0], size[1], 0, glFormat,
546 this->cache =
new PolyDataCache();
557 this->Points = points;
558 this->Colors = colors;
564 this->DrawLines(polyData, scalarMode, x, y, scale);
568 this->DrawPolygons(polyData, scalarMode, x, y, scale);
579 struct PolyDataCacheItem
583 std::vector<float> PolyTri;
587 std::vector<float> Lines;
596 std::map<vtkPolyData*, PolyDataCacheItem*>::iterator itPrev = this->PrevFrameCache.begin();
597 for (; itPrev != this->PrevFrameCache.end(); ++itPrev)
599 delete itPrev->second;
602 std::map<vtkPolyData*, PolyDataCacheItem*>::iterator it = this->CurrentFrameCache.begin();
603 for (; it != this->CurrentFrameCache.end(); ++it)
611 PolyDataCacheItem* cacheItem = this->CurrentFrameCache[
key];
612 if (cacheItem ==
nullptr)
614 cacheItem = this->PrevFrameCache[
key];
615 if (cacheItem ==
nullptr)
617 cacheItem =
new PolyDataCacheItem();
624 this->PrevFrameCache.erase(key);
628 this->CurrentFrameCache[
key] = cacheItem;
639 std::map<vtkPolyData*, PolyDataCacheItem*>::iterator itPrev = this->PrevFrameCache.begin();
640 for (; itPrev != this->PrevFrameCache.end(); ++itPrev)
642 delete itPrev->second;
646 this->PrevFrameCache.clear();
649 std::swap(this->PrevFrameCache, this->CurrentFrameCache);
654 std::map<vtkPolyData*, PolyDataCacheItem*> PrevFrameCache;
655 std::map<vtkPolyData*, PolyDataCacheItem*> CurrentFrameCache;
662 float const posX,
float const posY,
float const scale,
vtkIdType cellId,
int scalarMode)
664 this->CellPoints.reserve(this->NumPointsCell * 2);
666 for (
int i = 0; i < this->NumPointsCell; i++)
669 this->Points->
GetPoint(this->PointIds[i], point);
672 float const x =
static_cast<float>(
point[0]) + posX;
673 float const y =
static_cast<float>(
point[1]) + posY;
674 this->CellPoints.push_back(x * scale);
675 this->CellPoints.push_back(y * scale);
682 mappedColorId = this->PointIds[i];
685 mappedColorId = cellId;
688 std::cerr <<
"Scalar mode not supported!" << std::endl;
692 this->CellColors->
SetTuple(i, mappedColorId, this->Colors);
702 vtkPolyData* polyData,
int scalarMode,
float const x,
float const y,
float const scale)
704 PolyDataCacheItem* cacheItem = this->cache->GetCacheEntry(polyData);
706 if (polyData->
GetMTime() > cacheItem->LinesLoadingTime)
709 cacheItem->Lines.clear();
710 cacheItem->LineColors->Reset();
714 cacheItem->Lines.reserve(numVertices * 2);
716 cacheItem->LineColors->SetNumberOfTuples(numVertices);
728 vtkIdType actualNumPointsCell = genericCell->GetNumberOfPoints();
730 for (
int i = 0; i < actualNumPointsCell - 1; ++i)
732 this->NumPointsCell = 2;
733 this->PointIds = genericCell->GetPointIds()->
GetPointer(i);
735 this->MapCurrentCell(x, y, scale, cellId, scalarMode);
738 for (
int j = 0; j < this->NumPointsCell; j++)
740 cacheItem->Lines.push_back(this->CellPoints[2 * j]);
741 cacheItem->Lines.push_back(this->CellPoints[2 * j + 1]);
743 double* color4 = this->CellColors->
GetTuple(j);
744 cacheItem->LineColors->InsertTuple4(
745 vertOffset + j, color4[0], color4[1], color4[2], color4[3]);
748 vertOffset += this->NumPointsCell;
749 this->CellColors->
Reset();
750 this->CellPoints.clear();
755 cacheItem->LinesLoadingTime.Modified();
759 if (!cacheItem->Lines.empty())
761 this->Device->
DrawLines(cacheItem->Lines.data(),
762 static_cast<int>(cacheItem->Lines.size() / 2),
763 static_cast<unsigned char*
>(cacheItem->LineColors->GetVoidPointer(0)),
764 cacheItem->LineColors->GetNumberOfComponents());
783 this->NumPointsCell = genericCell->GetNumberOfPoints();
784 this->PointIds = genericCell->GetPointIds()->
GetPointer(0);
785 numTriVert += 3 * (this->NumPointsCell - 2);
798 vtkPolyData* polyData,
int scalarMode,
float const x,
float const y,
float const scale)
800 PolyDataCacheItem* cacheItem = this->cache->GetCacheEntry(polyData);
802 if (polyData->
GetMTime() > cacheItem->PolygonsLoadingTime)
804 cacheItem->PolyTri.clear();
805 cacheItem->PolyColors->Reset();
808 vtkIdType const totalTriVert = this->GetCountTriangleVertices(polyData);
809 cacheItem->PolyTri.reserve(totalTriVert * 2);
811 cacheItem->PolyColors->SetNumberOfTuples(totalTriVert);
828 this->NumPointsCell = genericCell->GetNumberOfPoints();
829 this->PointIds = genericCell->GetPointIds()->
GetPointer(0);
831 this->MapCurrentCell(x, y, scale, cellId, scalarMode);
834 for (
int i = 0; i < this->NumPointsCell - 2; i++)
836 cacheItem->PolyTri.push_back(this->CellPoints[0]);
837 cacheItem->PolyTri.push_back(this->CellPoints[1]);
838 cacheItem->PolyTri.push_back(this->CellPoints[i * 2 + 2]);
839 cacheItem->PolyTri.push_back(this->CellPoints[i * 2 + 3]);
840 cacheItem->PolyTri.push_back(this->CellPoints[i * 2 + 4]);
841 cacheItem->PolyTri.push_back(this->CellPoints[i * 2 + 5]);
844 vtkIdType const triangOffset = vertOffset + 3 * i;
845 double* color4 = this->CellColors->
GetTuple(0);
846 cacheItem->PolyColors->InsertTuple4(
847 triangOffset, color4[0], color4[1], color4[2], color4[3]);
849 color4 = this->CellColors->
GetTuple(i + 1);
850 cacheItem->PolyColors->InsertTuple4(
851 triangOffset + 1, color4[0], color4[1], color4[2], color4[3]);
853 color4 = this->CellColors->
GetTuple(i + 2);
854 cacheItem->PolyColors->InsertTuple4(
855 triangOffset + 2, color4[0], color4[1], color4[2], color4[3]);
858 vertOffset += 3 * (this->NumPointsCell - 2);
859 this->CellColors->
Reset();
860 this->CellPoints.clear();
864 cacheItem->PolygonsLoadingTime.Modified();
868 if (!cacheItem->PolyTri.empty())
871 static_cast<unsigned char*
>(cacheItem->PolyColors->GetVoidPointer(0)), 4);
886 std::vector<float> CellPoints;
890 PolyDataCache* cache;
void SetTuple(vtkIdType tupleIdx, const float *tuple) override
Set the data tuple at tupleIdx.
void GetTuple(vtkIdType tupleIdx, double *tuple) override
Get the data tuple at tupleIdx by filling in a user-provided array, Make sure that your array is larg...
int GetNumberOfComponents() const
Set/Get the dimension (n) of the components.
void Reset()
Reset to an empty state, without freeing any memory.
Efficient cell iterator for vtkDataSet topologies.
virtual vtkIdType GetCellId()=0
Get the id of the current cell.
void GoToNextCell()
Increment to next cell.
virtual bool IsDoneWithTraversal()=0
Returns false while the iterator is valid.
void Set(const T &red, const T &green, const T &blue)
Set the red, green and blue components of the color.
double * GetBounds()
Return a pointer to the geometry bounding box in the form (xmin,xmax, ymin,ymax, zmin,...
void SetNumberOfTuples(vtkIdType number) override
Set the number of tuples (a component group) in the array.
void SetNumberOfComponents(int num) override
Set/Get the dimension (n) of the components.
void SetInputData(vtkDataObject *)
Assign a data object as input.
topologically and geometrically regular array of data
Allocate and hold a VTK object.
T * GetPointer() const noexcept
Get a raw pointer to the contained object.
virtual void Delete()
Delete a VTK object.
void Draw(int cellType, vtkPolyData *polyData, vtkPoints *points, float x, float y, float scale, int scalarMode, vtkUnsignedCharArray *colors=nullptr)
Draw primitives as specified by cellType.
CellArrayHelper(vtkOpenGLContextDevice2D *device)
vtkVector2i FindPowerOfTwo(const vtkVector2i &size)
void SaveGLState(vtkOpenGLState *ostate, bool colorBuffer=false)
unsigned int TextureProperties
void RestoreGLState(vtkOpenGLState *ostate, bool colorBuffer=false)
vtkTexture * SpriteTexture
GLuint TextureFromImage(vtkImageData *image, vtkVector2f &texCoords)
GLuint TextureFromImage(vtkImageData *image)
GLfloat SavedClearColor[4]
vtkOpenGLContextDeviceBufferObjectBuilder BufferObjectBuilder
vtkTextureImageCache< UTF8TextPropertyKey > TextTextureCache
Cache for text images.
float * TexCoords(float *f, int n)
Class for drawing 2D primitives using OpenGL 1.1+.
void CoreDrawTriangles(std::vector< float > &tverts, unsigned char *colors=nullptr, int numComp=0)
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... The lines will be c...
Internal buffer object builder that maintains a cache of VBO groups.
void vtkglGetIntegerv(unsigned int pname, int *params)
bool GetEnumState(unsigned int name)
void vtkglGetFloatv(unsigned int pname, float *params)
void vtkglClearColor(float red, float green, float blue, float alpha)
void SetEnumState(unsigned int name, bool value)
vtkCellIterator * NewCellIterator() override
Return an iterator that traverses the cells in this data set.
represent and manipulate 3D points
double * GetPoint(vtkIdType id)
Return a pointer to a double point x[3] for a specific id.
concrete dataset represents vertices, lines, polygons, and triangle strips
vtkCell * GetCell(vtkIdType cellId) override
Standard vtkDataSet interface.
vtkMTimeType GetMTime() override
Get MTime which also considers its cell array MTime.
vtkIdType GetNumberOfCells() override
Standard vtkDataSet interface.
Hold a reference to a vtkObjectBase instance.
static vtkSmartPointer< T > New()
Create an instance of a VTK object.
represent text properties.
virtual double GetCellOffset()
Set/Get the horizontal offset between cells.
virtual int GetVerticalJustification()
Set/Get the vertical justification to bottom (default), middle, or top.
virtual double GetOpacity()
Set/Get the text's opacity.
virtual int GetFontSize()
Set/Get the font size (in points).
virtual double * GetColor()
Set the color of the text.
virtual double * GetInteriorLinesColor()
Set the color of the interior lines between cells.
virtual bool GetInteriorLinesVisibility()
Set/Get the visibility of the interior lines between cells.
virtual int GetInteriorLinesWidth()
Set the width (in pixels) of the interior lines between cells.
virtual int GetJustification()
Set/Get the horizontal justification to left (default), centered, or right.
bool IsKeyInCache(const Key &key) const
Search the cache list to see if a given key already exists.
std::list< CacheElement > Cache
List of a pair of key and cache data.
CacheData & AddCacheData(const Key &key, const CacheData &cacheData)
Add a new cache entry into the cache list.
void ReleaseGraphicsResources(vtkWindow *window)
Release all the OpenGL Pixel Buffer Object(PBO) associated with the textures of the cache list.
vtkTextureImageCache()
Construct a texture image cache with a maximum number of texture of 50.
size_t MaxSize
Maximum size the cache list can be.
CacheData & GetCacheData(const Key &key)
Return the cache associated to a key.
handles properties associated with a texture map
vtkImageData * GetInput()
Get the input as a vtkImageData object.
record modification and/or execution time
dynamic, self-adjusting array of unsigned char
Some derived classes for the different vectors commonly used.
window superclass for vtkRenderWindow
TextPropertyKey(vtkTextProperty *textProperty, const StringType &text, int dpi)
Creates a TextPropertyKey.
vtkTypeUInt32 TextPropertyId
static vtkTypeUInt32 GetIdFromTextProperty(vtkTextProperty *tprop)
Transform a text property into an unsigned long.
bool operator==(const TextPropertyKey &other) const
Compares two TextPropertyKeys with each other.
vtkSmartPointer< vtkImageData > ImageData
vtkTextRenderer::Metrics Metrics
vtkSmartPointer< vtkTexture > Texture
CacheElement associates a unique key to some cache.
CacheElement(const Key &key, const CacheData &cacheData)
CacheElement(const Key &key)
bool operator==(const CacheElement &other) const
#define VTK_SCALAR_MODE_USE_POINT_DATA
#define VTK_SCALAR_MODE_USE_CELL_DATA
TextPropertyKey< std::string > UTF8TextPropertyKey
#define VTK_UNSIGNED_CHAR