33 #ifndef vtkOpenGLContextDevice2DPrivate_h
34 #define vtkOpenGLContextDevice2DPrivate_h
91 : std::pair<Key,
CacheData>(key, cacheData)
100 return this->first == other.first;
134 typename std::list<CacheElement>::iterator it;
135 for (it = this->
Cache.begin(); it != this->
Cache.end(); ++it)
137 it->second.Texture->ReleaseGraphicsResources(window);
153 this->
Cache.pop_back();
155 this->
Cache.push_front(CacheElement(key, cacheData));
156 return this->
Cache.begin()->second;
176 typename std::list<CacheElement>::iterator it =
177 std::find(this->Cache.begin(), this->Cache.end(),
CacheElement(key));
178 if (it != this->Cache.end())
186 return this->AddCacheData(key, cacheData);
192 template <
class StringType>
209 vtkTypeUInt32 hash =
static_cast<vtkTypeUInt32
>(id);
213 assert(
"Hash is really a uint32" && static_cast<size_t>(hash) ==
id);
246 this->
Color.
Set(static_cast<unsigned char>(color[0] * 255),
247 static_cast<unsigned char>(color[1] * 255), static_cast<unsigned char>(color[2] * 255),
248 static_cast<unsigned char>(textProperty->
GetOpacity() * 255));
335 ::glDrawBuffers(1, bufs);
345 float* texCoord =
new float[2 * n];
351 for (
int i = 0; i < n; ++i)
353 minX = fptr[0] < minX ? fptr[0] : minX;
354 maxX = fptr[0] > maxX ? fptr[0] : maxX;
355 minY = fptr[1] < minY ? fptr[1] : minY;
356 maxY = fptr[1] > maxY ? fptr[1] : maxY;
364 (textureBounds[1] - textureBounds[0]) ? textureBounds[1] - textureBounds[0] : 1.;
366 (textureBounds[3] - textureBounds[2]) ? textureBounds[3] - textureBounds[2] : 1.;
367 for (
int i = 0; i < n; ++i)
369 texCoord[i * 2] = (fptr[0] - minX) / rangeX;
370 texCoord[i * 2 + 1] = (fptr[1] - minY) / rangeY;
376 float rangeX = (maxX - minX) ? maxX - minX : 1.f;
377 float rangeY = (maxY - minY) ? maxY - minY : 1.f;
378 for (
int i = 0; i < n; ++i)
380 texCoord[i * 2] = (fptr[0] - minX) / rangeX;
381 texCoord[i * 2 + 1] = (fptr[1] - minY) / rangeY;
391 for (
int i = 0; i < 2; ++i)
393 while (pow2[i] < size[i])
405 vtkGenericWarningMacro(
"Invalid image format: expected unsigned char.");
413 for (
int i = 0; i < 2; ++i)
415 texCoords[i] = size[i] / float(newImg[i]);
418 unsigned char* dataPtr =
new unsigned char[newImg[0] * newImg[1] * bytesPerPixel];
419 unsigned char* origPtr =
static_cast<unsigned char*
>(image->
GetScalarPointer());
421 for (
int i = 0; i < newImg[0]; ++i)
423 for (
int j = 0; j < newImg[1]; ++j)
425 for (
int k = 0; k < bytesPerPixel; ++k)
427 if (i < size[0] && j < size[1])
429 dataPtr[i * bytesPerPixel + j * newImg[0] * bytesPerPixel + k] =
430 origPtr[i * bytesPerPixel + j * size[0] * bytesPerPixel + k];
434 dataPtr[i * bytesPerPixel + j * newImg[0] * bytesPerPixel + k] = k == 3 ? 0 : 255;
441 GLint glFormat = bytesPerPixel == 3 ? GL_RGB : GL_RGBA;
442 GLint glInternalFormat = bytesPerPixel == 3 ? GL_RGB8 : GL_RGBA8;
444 glGenTextures(1, &tmpIndex);
445 glBindTexture(GL_TEXTURE_2D, tmpIndex);
447 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_NEAREST);
448 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
GL_NEAREST);
452 glTexImage2D(GL_TEXTURE_2D, 0, glInternalFormat, newImg[0], newImg[1], 0, glFormat,
462 cout <<
"Error = not an unsigned char..." << endl;
469 unsigned char* dataPtr =
static_cast<unsigned char*
>(image->
GetScalarPointer());
471 GLint glFormat = bytesPerPixel == 3 ? GL_RGB : GL_RGBA;
472 GLint glInternalFormat = bytesPerPixel == 3 ? GL_RGB8 : GL_RGBA8;
474 glGenTextures(1, &tmpIndex);
475 glBindTexture(GL_TEXTURE_2D, tmpIndex);
477 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_NEAREST);
478 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
GL_NEAREST);
482 glTexImage2D(GL_TEXTURE_2D, 0, glInternalFormat, size[0], size[1], 0, glFormat,
551 this->cache =
new PolyDataCache();
563 this->Colors = colors;
564 this->CellColors->SetNumberOfComponents(colors->GetNumberOfComponents());
569 this->
DrawLines(polyData, scalarMode, x, y, scale);
573 this->DrawPolygons(polyData, scalarMode, x, y, scale);
584 struct PolyDataCacheItem
588 std::vector<float> PolyTri;
592 std::vector<float> Lines;
601 std::map<vtkPolyData*, PolyDataCacheItem*>::iterator itPrev = this->PrevFrameCache.begin();
602 for (; itPrev != this->PrevFrameCache.end(); ++itPrev)
604 delete itPrev->second;
607 std::map<vtkPolyData*, PolyDataCacheItem*>::iterator it = this->CurrentFrameCache.begin();
608 for (; it != this->CurrentFrameCache.end(); ++it)
616 PolyDataCacheItem* cacheItem = this->CurrentFrameCache[
key];
617 if (cacheItem ==
nullptr)
619 cacheItem = this->PrevFrameCache[
key];
620 if (cacheItem ==
nullptr)
622 cacheItem =
new PolyDataCacheItem();
629 this->PrevFrameCache.erase(key);
633 this->CurrentFrameCache[
key] = cacheItem;
644 std::map<vtkPolyData*, PolyDataCacheItem*>::iterator itPrev = this->PrevFrameCache.begin();
645 for (; itPrev != this->PrevFrameCache.end(); ++itPrev)
647 delete itPrev->second;
651 this->PrevFrameCache.clear();
654 std::swap(this->PrevFrameCache, this->CurrentFrameCache);
659 std::map<vtkPolyData*, PolyDataCacheItem*> PrevFrameCache;
660 std::map<vtkPolyData*, PolyDataCacheItem*> CurrentFrameCache;
667 float const posX,
float const posY,
float const scale,
vtkIdType cellId,
int scalarMode)
669 this->CellPoints.reserve(this->NumPointsCell * 2);
670 this->CellColors->SetNumberOfTuples(this->NumPointsCell);
671 for (
int i = 0; i < this->NumPointsCell; i++)
674 this->
Points->GetPoint(this->PointIds[i], point);
677 float const x =
static_cast<float>(point[0]) + posX;
678 float const y =
static_cast<float>(point[1]) + posY;
679 this->CellPoints.push_back(x * scale);
680 this->CellPoints.push_back(y * scale);
687 mappedColorId = this->PointIds[i];
690 mappedColorId = cellId;
693 std::cerr <<
"Scalar mode not supported!" << std::endl;
697 this->CellColors->SetTuple(i, mappedColorId, this->Colors);
707 vtkPolyData* polyData,
int scalarMode,
float const x,
float const y,
float const scale)
709 PolyDataCacheItem* cacheItem = this->cache->GetCacheEntry(polyData);
711 if (polyData->
GetMTime() > cacheItem->LinesLoadingTime)
714 cacheItem->Lines.clear();
715 cacheItem->LineColors->Reset();
719 cacheItem->Lines.reserve(numVertices * 2);
720 cacheItem->LineColors->SetNumberOfComponents(this->Colors->GetNumberOfComponents());
721 cacheItem->LineColors->SetNumberOfTuples(numVertices);
735 for (
int i = 0; i < actualNumPointsCell - 1; ++i)
737 this->NumPointsCell = 2;
740 this->MapCurrentCell(x, y, scale, cellId, scalarMode);
743 for (
int j = 0; j < this->NumPointsCell; j++)
745 cacheItem->Lines.push_back(this->CellPoints[2 * j]);
746 cacheItem->Lines.push_back(this->CellPoints[2 * j + 1]);
748 double* color4 = this->CellColors->GetTuple(j);
749 cacheItem->LineColors->InsertTuple4(
750 vertOffset + j, color4[0], color4[1], color4[2], color4[3]);
753 vertOffset += this->NumPointsCell;
754 this->CellColors->Reset();
755 this->CellPoints.clear();
760 cacheItem->LinesLoadingTime.Modified();
764 if (!cacheItem->Lines.empty())
766 this->Device->DrawLines(&cacheItem->Lines[0], static_cast<int>(cacheItem->Lines.size() / 2),
767 static_cast<unsigned char*>(cacheItem->LineColors->GetVoidPointer(0)),
768 cacheItem->LineColors->GetNumberOfComponents());
789 numTriVert += 3 * (this->NumPointsCell - 2);
802 vtkPolyData* polyData,
int scalarMode,
float const x,
float const y,
float const scale)
804 PolyDataCacheItem* cacheItem = this->cache->GetCacheEntry(polyData);
806 if (polyData->
GetMTime() > cacheItem->PolygonsLoadingTime)
808 cacheItem->PolyTri.clear();
809 cacheItem->PolyColors->Reset();
812 vtkIdType const totalTriVert = this->GetCountTriangleVertices(polyData);
813 cacheItem->PolyTri.reserve(totalTriVert * 2);
814 cacheItem->PolyColors->SetNumberOfComponents(this->Colors->GetNumberOfComponents());
815 cacheItem->PolyColors->SetNumberOfTuples(totalTriVert);
820 cacheItem->PolyColors->SetNumberOfComponents(this->Colors->GetNumberOfComponents());
835 this->MapCurrentCell(x, y, scale, cellId, scalarMode);
838 for (
int i = 0; i < this->NumPointsCell - 2; i++)
840 cacheItem->PolyTri.push_back(this->CellPoints[0]);
841 cacheItem->PolyTri.push_back(this->CellPoints[1]);
842 cacheItem->PolyTri.push_back(this->CellPoints[i * 2 + 2]);
843 cacheItem->PolyTri.push_back(this->CellPoints[i * 2 + 3]);
844 cacheItem->PolyTri.push_back(this->CellPoints[i * 2 + 4]);
845 cacheItem->PolyTri.push_back(this->CellPoints[i * 2 + 5]);
848 vtkIdType const triangOffset = vertOffset + 3 * i;
849 double* color4 = this->CellColors->GetTuple(0);
850 cacheItem->PolyColors->InsertTuple4(
851 triangOffset, color4[0], color4[1], color4[2], color4[3]);
853 color4 = this->CellColors->GetTuple(i + 1);
854 cacheItem->PolyColors->InsertTuple4(
855 triangOffset + 1, color4[0], color4[1], color4[2], color4[3]);
857 color4 = this->CellColors->GetTuple(i + 2);
858 cacheItem->PolyColors->InsertTuple4(
859 triangOffset + 2, color4[0], color4[1], color4[2], color4[3]);
862 vertOffset += 3 * (this->NumPointsCell - 2);
863 this->CellColors->Reset();
864 this->CellPoints.clear();
868 cacheItem->PolygonsLoadingTime.Modified();
872 if (!cacheItem->PolyTri.empty())
874 this->Device->CoreDrawTriangles(cacheItem->PolyTri,
875 static_cast<unsigned char*>(cacheItem->PolyColors->GetVoidPointer(0)), 4);
890 std::vector<float> CellPoints;
894 PolyDataCache* cache;
896 #endif // VTKOPENGLCONTEXTDEVICE2DPRIVATE_H
virtual double * GetInteriorLinesColor()
Set the color of the interior lines between cells.
GLuint TextureFromImage(vtkImageData *image)
bool operator==(const CacheElement &other) const
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... ...
std::list< CacheElement > Cache
List of a pair of key and cache data.
virtual int GetJustification()
Set/Get the horizontal justification to left (default), centered, or right.
vtkIdType GetNumberOfPoints() const
Return the number of points in the cell.
double * GetBounds()
Return a pointer to the geometry bounding box in the form (xmin,xmax, ymin,ymax, zmin,zmax).
void SetEnumState(unsigned int name, bool value)
vtkTextRenderer::Metrics Metrics
record modification and/or execution time
GLfloat SavedClearColor[4]
Class for drawing 2D primitives using OpenGL 1.1+.
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.
void vtkglGetIntegerv(unsigned int pname, int *params)
static int GetNumberOfScalarComponents(vtkInformation *meta_data)
Set/Get the number of scalar components for points.
GLuint TextureFromImage(vtkImageData *image, vtkVector2f &texCoords)
bool IsKeyInCache(const Key &key) const
Search the cache list to see if a given key already exists.
vtkTypeUInt32 TextPropertyId
void SaveGLState(vtkOpenGLState *ostate, bool colorBuffer=false)
TextPropertyKey(vtkTextProperty *textProperty, const StringType &text, int dpi)
Creates a TextPropertyKey.
concrete dataset represents vertices, lines, polygons, and triangle strips
CellArrayHelper(vtkOpenGLContextDevice2D *device)
bool GetEnumState(unsigned int name)
static vtkSmartPointer< T > New()
Create an instance of a VTK object.
CacheElement(const Key &key, const CacheData &cacheData)
void SetInputData(vtkDataObject *)
Assign a data object as input.
void ReleaseGraphicsResources(vtkWindow *window)
Release all the OpenGL Pixel Buffer Object(PBO) associated with the textures of the cache list...
bool operator==(const TextPropertyKey &other) const
Compares two TextPropertyKeys with each other.
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.
virtual vtkIdType GetCellId()=0
Get the id of the current cell.
void vtkglGetFloatv(unsigned int pname, float *params)
void GoToNextCell()
Increment to next cell.
vtkCell * GetCell(vtkIdType cellId) override
Standard vtkDataSet interface.
#define VTK_SCALAR_MODE_USE_POINT_DATA
virtual int * GetDimensions()
Get dimensions of this structured points dataset.
static int GetScalarType(vtkInformation *meta_data)
topologically and geometrically regular array of data
vtkImageData * GetInput()
Get the input as a vtkImageData object.
Some derived classes for the different vectors commonly used.
int GetCellType() override
See the vtkCell API for descriptions of these methods.
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.
vtkIdType GetNumberOfCells() override
Standard vtkDataSet interface.
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.
vtkMTimeType GetMTime() override
Get MTime which also considers its cell array MTime.
represent text properties.
dynamic, self-adjusting array of unsigned char
virtual bool GetInteriorLinesVisibility()
Set/Get the visibility of the interior lines between cells.
CacheElement(const Key &key)
virtual double * GetColor()
Set the color of the text.
#define VTK_UNSIGNED_CHAR
vtkCellIterator * NewCellIterator() override
Return an iterator that traverses the cells in this data set.
virtual void * GetScalarPointer(int coordinates[3])
Access the native pointer for the scalar data.
unsigned int TextureProperties
void RestoreGLState(vtkOpenGLState *ostate, bool colorBuffer=false)
void vtkglClearColor(float red, float green, float blue, float alpha)
Efficient cell iterator for vtkDataSet topologies.
vtkIdList * GetPointIds()
Return the list of point ids defining the cell.
CacheElement associates a unique key to some cache.
vtkVector2i FindPowerOfTwo(const vtkVector2i &size)
virtual int GetFontSize()
Set/Get the font size (in points).
static vtkTypeUInt32 GetIdFromTextProperty(vtkTextProperty *tprop)
Transform a text property into an unsigned long.
#define VTK_SCALAR_MODE_USE_CELL_DATA
virtual double GetCellOffset()
Set/Get the horizontal offset between cells.
CacheData & AddCacheData(const Key &key, const CacheData &cacheData)
Add a new cache entry into the cache list.
virtual bool IsDoneWithTraversal()=0
Returns false while the iterator is valid.
vtkSmartPointer< vtkImageData > ImageData
virtual int GetInteriorLinesWidth()
Set the width (in pixels) of the interior lines between cells.
vtkTextureImageCache< UTF8TextPropertyKey > TextTextureCache
Cache for text images.
vtkIdType * GetPointer(const vtkIdType i)
Get a pointer to a particular data index.
represent and manipulate 3D points
virtual void Delete()
Delete a VTK object.