21#ifndef vtkOpenGLContextDevice2DPrivate_h
22#define vtkOpenGLContextDevice2DPrivate_h
47VTK_ABI_NAMESPACE_BEGIN
90 return this->first == other.first;
106 return std::find(this->
Cache.begin(), this->Cache.end(), key) != this->
Cache.end();
124 typename std::list<CacheElement>::iterator it;
125 for (it = this->
Cache.begin(); it != this->Cache.end(); ++it)
127 it->second.Texture->ReleaseGraphicsResources(window);
141 if (this->
Cache.size() >= this->MaxSize)
143 this->
Cache.pop_back();
146 return this->
Cache.begin()->second;
166 typename std::list<CacheElement>::iterator it =
168 if (it != this->
Cache.end())
182template <
class StringType>
199 vtkTypeUInt32 hash =
static_cast<vtkTypeUInt32
>(id);
203 assert(
"Hash is really a uint32" &&
static_cast<size_t>(hash) ==
id);
236 this->
Color.Set(
static_cast<unsigned char>(color[0] * 255),
237 static_cast<unsigned char>(color[1] * 255),
static_cast<unsigned char>(color[2] * 255),
238 static_cast<unsigned char>(textProperty->
GetOpacity() * 255));
330 ::glDrawBuffers(1, bufs);
340 float* texCoord =
new float[2 * n];
346 for (
int i = 0; i < n; ++i)
348 minX = fptr[0] < minX ? fptr[0] : minX;
349 maxX = fptr[0] > maxX ? fptr[0] : maxX;
350 minY = fptr[1] < minY ? fptr[1] : minY;
351 maxY = fptr[1] > maxY ? fptr[1] : maxY;
357 const double* textureBounds = this->
Texture->GetInput()->GetBounds();
359 (textureBounds[1] - textureBounds[0]) ? textureBounds[1] - textureBounds[0] : 1.;
361 (textureBounds[3] - textureBounds[2]) ? textureBounds[3] - textureBounds[2] : 1.;
362 for (
int i = 0; i < n; ++i)
364 texCoord[i * 2] = (fptr[0] - minX) / rangeX;
365 texCoord[i * 2 + 1] = (fptr[1] - minY) / rangeY;
371 float rangeX = (maxX - minX) ? maxX - minX : 1.f;
372 float rangeY = (maxY - minY) ? maxY - minY : 1.f;
373 for (
int i = 0; i < n; ++i)
375 texCoord[i * 2] = (fptr[0] - minX) / rangeX;
376 texCoord[i * 2 + 1] = (fptr[1] - minY) / rangeY;
386 for (
int i = 0; i < 2; ++i)
388 while (pow2[i] < size[i])
400 vtkGenericWarningMacro(
"Invalid image format: expected unsigned char.");
403 int bytesPerPixel = image->GetNumberOfScalarComponents();
405 image->GetDimensions(size);
408 for (
int i = 0; i < 2; ++i)
410 texCoords[i] = size[i] / float(newImg[i]);
413 unsigned char* dataPtr =
new unsigned char[newImg[0] * newImg[1] * bytesPerPixel];
414 unsigned char* origPtr =
static_cast<unsigned char*
>(image->GetScalarPointer());
416 for (
int i = 0; i < newImg[0]; ++i)
418 for (
int j = 0; j < newImg[1]; ++j)
420 for (
int k = 0; k < bytesPerPixel; ++k)
422 if (i < size[0] && j < size[1])
424 dataPtr[i * bytesPerPixel + j * newImg[0] * bytesPerPixel + k] =
425 origPtr[i * bytesPerPixel + j * size[0] * bytesPerPixel + k];
429 dataPtr[i * bytesPerPixel + j * newImg[0] * bytesPerPixel + k] = k == 3 ? 0 : 255;
436 GLint glFormat = bytesPerPixel == 3 ? GL_RGB : GL_RGBA;
437 GLint glInternalFormat = bytesPerPixel == 3 ? GL_RGB8 : GL_RGBA8;
439 glGenTextures(1, &tmpIndex);
440 glBindTexture(GL_TEXTURE_2D, tmpIndex);
442 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_NEAREST);
443 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
GL_NEAREST);
447 glTexImage2D(GL_TEXTURE_2D, 0, glInternalFormat, newImg[0], newImg[1], 0, glFormat,
457 std::cout <<
"Error = not an unsigned char..." << std::endl;
460 int bytesPerPixel = image->GetNumberOfScalarComponents();
462 image->GetDimensions(size);
464 unsigned char* dataPtr =
static_cast<unsigned char*
>(image->GetScalarPointer());
466 GLint glFormat = bytesPerPixel == 3 ? GL_RGB : GL_RGBA;
467 GLint glInternalFormat = bytesPerPixel == 3 ? GL_RGB8 : GL_RGBA8;
469 glGenTextures(1, &tmpIndex);
470 glBindTexture(GL_TEXTURE_2D, tmpIndex);
472 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_NEAREST);
473 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
GL_NEAREST);
477 glTexImage2D(GL_TEXTURE_2D, 0, glInternalFormat, size[0], size[1], 0, glFormat,
547 this->cache =
new PolyDataCache();
558 this->Points = points;
559 this->Colors = colors;
560 this->CellColors->SetNumberOfComponents(colors->GetNumberOfComponents());
565 this->DrawLines(polyData, scalarMode, x, y, scale);
569 this->DrawPolygons(polyData, scalarMode, x, y, scale);
580 struct PolyDataCacheItem
584 std::vector<float> PolyTri;
588 std::vector<float> Lines;
597 std::map<vtkPolyData*, PolyDataCacheItem*>::iterator itPrev = this->PrevFrameCache.begin();
598 for (; itPrev != this->PrevFrameCache.end(); ++itPrev)
600 delete itPrev->second;
603 std::map<vtkPolyData*, PolyDataCacheItem*>::iterator it = this->CurrentFrameCache.begin();
604 for (; it != this->CurrentFrameCache.end(); ++it)
610 PolyDataCacheItem* GetCacheEntry(vtkPolyData* key)
612 PolyDataCacheItem* cacheItem = this->CurrentFrameCache[
key];
613 if (cacheItem ==
nullptr)
615 cacheItem = this->PrevFrameCache[
key];
616 if (cacheItem ==
nullptr)
618 cacheItem =
new PolyDataCacheItem();
625 this->PrevFrameCache.erase(key);
629 this->CurrentFrameCache[
key] = cacheItem;
640 std::map<vtkPolyData*, PolyDataCacheItem*>::iterator itPrev = this->PrevFrameCache.begin();
641 for (; itPrev != this->PrevFrameCache.end(); ++itPrev)
643 delete itPrev->second;
647 this->PrevFrameCache.clear();
650 std::swap(this->PrevFrameCache, this->CurrentFrameCache);
655 std::map<vtkPolyData*, PolyDataCacheItem*> PrevFrameCache;
656 std::map<vtkPolyData*, PolyDataCacheItem*> CurrentFrameCache;
663 float const posX,
float const posY,
float const scale,
vtkIdType cellId,
int scalarMode)
665 this->CellPoints.reserve(this->NumPointsCell * 2);
666 this->CellColors->SetNumberOfTuples(this->NumPointsCell);
667 for (
int i = 0; i < this->NumPointsCell; i++)
670 this->Points->GetPoint(this->PointIds[i], point);
673 float const x =
static_cast<float>(
point[0]) + posX;
674 float const y =
static_cast<float>(
point[1]) + posY;
675 this->CellPoints.push_back(x * scale);
676 this->CellPoints.push_back(y * scale);
683 mappedColorId = this->PointIds[i];
686 mappedColorId = cellId;
689 std::cerr <<
"Scalar mode not supported!" << std::endl;
693 this->CellColors->SetTuple(i, mappedColorId, this->Colors);
703 vtkPolyData* polyData,
int scalarMode,
float const x,
float const y,
float const scale)
705 PolyDataCacheItem* cacheItem = this->cache->GetCacheEntry(polyData);
707 if (polyData->
GetMTime() > cacheItem->LinesLoadingTime)
709 vtkNew<vtkGenericCell> genericCell;
710 cacheItem->Lines.clear();
711 cacheItem->LineColors->Reset();
715 cacheItem->Lines.reserve(numVertices * 2);
716 cacheItem->LineColors->SetNumberOfComponents(this->Colors->GetNumberOfComponents());
717 cacheItem->LineColors->SetNumberOfTuples(numVertices);
721 vtkCellIterator* cellIter =
nullptr;
729 vtkIdType actualNumPointsCell = genericCell->GetNumberOfPoints();
731 for (
int i = 0; i < actualNumPointsCell - 1; ++i)
733 this->NumPointsCell = 2;
734 this->PointIds = genericCell->GetPointIds()->
GetPointer(i);
736 this->MapCurrentCell(x, y, scale, cellId, scalarMode);
739 for (
int j = 0; j < this->NumPointsCell; j++)
741 cacheItem->Lines.push_back(this->CellPoints[2 * j]);
742 cacheItem->Lines.push_back(this->CellPoints[2 * j + 1]);
744 double* color4 = this->CellColors->GetTuple(j);
745 cacheItem->LineColors->InsertTuple4(
746 vertOffset + j, color4[0], color4[1], color4[2], color4[3]);
749 vertOffset += this->NumPointsCell;
750 this->CellColors->Reset();
751 this->CellPoints.clear();
756 cacheItem->LinesLoadingTime.Modified();
760 if (!cacheItem->Lines.empty())
762 this->Device->DrawLines(cacheItem->Lines.data(),
763 static_cast<int>(cacheItem->Lines.size() / 2), cacheItem->LineColors->GetPointer(0),
764 cacheItem->LineColors->GetNumberOfComponents());
772 vtkIdType GetCountTriangleVertices(vtkPolyData* polyData)
776 vtkNew<vtkGenericCell> genericCell;
777 vtkCellIterator* cellIter =
nullptr;
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);
810 cacheItem->PolyColors->SetNumberOfComponents(this->Colors->GetNumberOfComponents());
811 cacheItem->PolyColors->SetNumberOfTuples(totalTriVert);
816 cacheItem->PolyColors->SetNumberOfComponents(this->Colors->GetNumberOfComponents());
818 vtkNew<vtkGenericCell> genericCell;
819 vtkCellIterator* cellIter =
nullptr;
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())
870 this->Device->CoreDrawTriangles(cacheItem->PolyTri, cacheItem->PolyColors->GetPointer(0), 4);
878 vtkUnsignedCharArray* Colors;
885 std::vector<float> CellPoints;
886 vtkNew<vtkUnsignedCharArray> CellColors;
889 PolyDataCache* cache;
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 SetInputData(vtkDataObject *)
Assign a data object as input.
topologically and geometrically regular array of data
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)
vtkOpenGLContextDevice2D()
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
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
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