33 #ifndef vtkOpenGLContextDevice2DPrivate_h 34 #define vtkOpenGLContextDevice2DPrivate_h 87 : std::pair<Key,
CacheData>(key, cacheData){}
94 return this->first == other.first;
131 typename std::list<CacheElement >::iterator it;
132 for (it = this->
Cache.begin(); it != this->
Cache.end(); ++it)
134 it->second.Texture->ReleaseGraphicsResources(window);
150 this->
Cache.pop_back();
152 this->
Cache.push_front(CacheElement(key, cacheData));
153 return this->
Cache.begin()->second;
173 typename std::list<CacheElement>::iterator it =
175 if (it != this->
Cache.end())
189 template <
class StringType>
206 vtkTypeUInt32 hash =
static_cast<vtkTypeUInt32
>(id);
210 assert(
"Hash is really a uint32" && static_cast<size_t>(hash) ==
id);
231 this->TextPropertyId = GetIdFromTextProperty(textProperty);
235 this->
Color.Set(static_cast<unsigned char>(color[0] * 255),
236 static_cast<unsigned char>(color[1] * 255),
237 static_cast<unsigned char>(color[2] * 255),
238 static_cast<unsigned char>(textProperty->
GetOpacity() * 255));
257 this->DPI == other.
DPI;
279 this->SpriteTexture =
nullptr;
280 this->SavedDepthTest = GL_TRUE;
281 this->SavedStencilTest = GL_TRUE;
282 this->SavedBlend = GL_TRUE;
283 this->SavedDrawBuffer = 0;
284 this->SavedClearColor[0] = this->SavedClearColor[1] =
285 this->SavedClearColor[2] =
286 this->SavedClearColor[3] = 0.0f;
287 this->TextCounter = 0;
288 this->GLExtensionsLoaded =
true;
290 this->PowerOfTwoTextures =
false;
300 if (this->SpriteTexture)
302 this->SpriteTexture->
Delete();
303 this->SpriteTexture =
nullptr;
309 this->SavedDepthTest = ostate->
GetEnumState(GL_DEPTH_TEST);
313 this->SavedStencilTest = ostate->
GetEnumState(GL_STENCIL_TEST);
315 ostate->
vtkglGetFloatv(GL_COLOR_CLEAR_VALUE, this->SavedClearColor);
322 ostate->
SetEnumState(GL_DEPTH_TEST, this->SavedDepthTest);
326 ostate->
SetEnumState(GL_STENCIL_TEST, this->SavedStencilTest);
329 if(this->SavedDrawBuffer != GL_BACK_LEFT)
331 glDrawBuffer(this->SavedDrawBuffer);
335 this->SavedClearColor[1],
336 this->SavedClearColor[2],
337 this->SavedClearColor[3]);
343 float* texCoord =
new float[2*n];
344 float minX = f[0];
float minY = f[1];
345 float maxX = f[0];
float maxY = f[1];
347 for(
int i = 0; i < n; ++i)
349 minX = fptr[0] < minX ? fptr[0] : minX;
350 maxX = fptr[0] > maxX ? fptr[0] : maxX;
351 minY = fptr[1] < minY ? fptr[1] : minY;
352 maxY = fptr[1] > maxY ? fptr[1] : maxY;
359 float rangeX = (textureBounds[1] - textureBounds[0]) ?
360 textureBounds[1] - textureBounds[0] : 1.;
361 float rangeY = (textureBounds[3] - textureBounds[2]) ?
362 textureBounds[3] - textureBounds[2] : 1.;
363 for (
int i = 0; i < n; ++i)
365 texCoord[i*2] = (fptr[0]-minX) / rangeX;
366 texCoord[i*2+1] = (fptr[1]-minY) / rangeY;
372 float rangeX = (maxX - minX)? maxX - minX : 1.f;
373 float rangeY = (maxY - minY)? maxY - minY : 1.f;
374 for (
int i = 0; i < n; ++i)
376 texCoord[i*2] = (fptr[0]-minX)/rangeX;
377 texCoord[i*2+1] = (fptr[1]-minY)/rangeY;
387 for (
int i = 0; i < 2; ++i)
389 while (pow2[i] < size[i])
401 vtkGenericWarningMacro(
"Invalid image format: expected unsigned char.");
409 for (
int i = 0; i < 2; ++i)
411 texCoords[i] = size[i] /
float(newImg[i]);
414 unsigned char *dataPtr =
415 new unsigned char[newImg[0] * newImg[1] * bytesPerPixel];
416 unsigned char *origPtr =
419 for (
int i = 0; i < newImg[0]; ++i)
421 for (
int j = 0; j < newImg[1]; ++j)
423 for (
int k = 0; k < bytesPerPixel; ++k)
425 if (i < size[0] && j < size[1])
427 dataPtr[i * bytesPerPixel + j * newImg[0] * bytesPerPixel + k] =
428 origPtr[i * bytesPerPixel + j * size[0] * bytesPerPixel + k];
432 dataPtr[i * bytesPerPixel + j * newImg[0] * bytesPerPixel + k] =
440 GLint glFormat = bytesPerPixel == 3 ? GL_RGB : GL_RGBA;
441 GLint glInternalFormat = bytesPerPixel == 3 ? GL_RGB8 : GL_RGBA8;
443 glGenTextures(1, &tmpIndex);
444 glBindTexture(GL_TEXTURE_2D, tmpIndex);
446 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
447 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
448 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
450 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
453 glTexImage2D(GL_TEXTURE_2D, 0 , glInternalFormat,
454 newImg[0], newImg[1], 0, glFormat,
455 GL_UNSIGNED_BYTE, static_cast<const GLvoid *>(dataPtr));
464 cout <<
"Error = not an unsigned char..." << endl;
471 unsigned char *dataPtr =
474 GLint glFormat = bytesPerPixel == 3 ? GL_RGB : GL_RGBA;
475 GLint glInternalFormat = bytesPerPixel == 3 ? GL_RGB8 : GL_RGBA8;
477 glGenTextures(1, &tmpIndex);
478 glBindTexture(GL_TEXTURE_2D, tmpIndex);
480 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
481 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
482 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
484 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
487 glTexImage2D(GL_TEXTURE_2D, 0 , glInternalFormat,
488 size[0], size[1], 0, glFormat,
489 GL_UNSIGNED_BYTE, static_cast<const GLvoid *>(dataPtr));
501 GLfloat SavedClearColor[4];
558 this->cache =
new PolyDataCache();
573 this->Colors = colors;
574 this->CellColors->SetNumberOfComponents(colors->GetNumberOfComponents());
579 this->DrawLines(polyData, scalarMode, x, y, scale);
583 this->DrawPolygons(polyData, scalarMode, x, y, scale);
590 this->cache->SwapCaches();
597 struct PolyDataCacheItem
601 std::vector<float> PolyTri;
605 std::vector<float> Lines;
614 std::map<vtkPolyData*, PolyDataCacheItem*>::iterator itPrev =
615 this->PrevFrameCache.begin();
616 for (; itPrev != this->PrevFrameCache.end(); ++itPrev)
618 delete itPrev->second;
621 std::map<vtkPolyData*, PolyDataCacheItem*>::iterator it =
622 this->CurrentFrameCache.begin();
623 for (; it != this->CurrentFrameCache.end(); ++it)
631 PolyDataCacheItem* cacheItem = this->CurrentFrameCache[
key];
632 if (cacheItem ==
nullptr)
634 cacheItem = this->PrevFrameCache[
key];
635 if (cacheItem ==
nullptr)
637 cacheItem =
new PolyDataCacheItem();
644 this->PrevFrameCache.erase(key);
648 this->CurrentFrameCache[
key] = cacheItem;
659 std::map<vtkPolyData*, PolyDataCacheItem*>::iterator itPrev =
660 this->PrevFrameCache.begin();
661 for (; itPrev != this->PrevFrameCache.end(); ++itPrev)
663 delete itPrev->second;
667 this->PrevFrameCache.clear();
670 std::swap(this->PrevFrameCache, this->CurrentFrameCache);
675 std::map<vtkPolyData*, PolyDataCacheItem*> PrevFrameCache;
676 std::map<vtkPolyData*, PolyDataCacheItem*> CurrentFrameCache;
682 void MapCurrentCell (
float const posX,
float const posY,
float const scale,
685 this->CellPoints.reserve(this->NumPointsCell * 2);
686 this->CellColors->SetNumberOfTuples(this->NumPointsCell);
687 for (
int i = 0; i < this->NumPointsCell; i++)
690 this->Points->GetPoint(this->PointIds[i], point);
693 float const x =
static_cast<float>(point[0]) + posX;
694 float const y =
static_cast<float>(point[1]) + posY;
695 this->CellPoints.push_back(x * scale);
696 this->CellPoints.push_back(y * scale);
703 mappedColorId = this->PointIds[i];
706 mappedColorId = cellId;
709 std::cerr <<
"Scalar mode not supported!" << std::endl;
713 this->CellColors->SetTuple(i, mappedColorId, this->Colors);
722 void DrawLines(
vtkPolyData* polyData,
int scalarMode,
float const x,
723 float const y,
float const scale)
725 PolyDataCacheItem* cacheItem = this->cache->GetCacheEntry(polyData);
727 if (polyData->
GetMTime() > cacheItem->LinesLoadingTime)
730 cacheItem->Lines.clear();
731 cacheItem->LineColors->Reset();
735 cacheItem->Lines.reserve(numVertices * 2);
736 cacheItem->LineColors->SetNumberOfComponents(this->Colors->GetNumberOfComponents());
737 cacheItem->LineColors->SetNumberOfTuples(numVertices);
753 for (
int i = 0; i < actualNumPointsCell - 1; ++i)
755 this->NumPointsCell = 2;
758 this->MapCurrentCell(x, y, scale, cellId, scalarMode);
761 for (
int j = 0; j < this->NumPointsCell; j++)
763 cacheItem->Lines.push_back(this->CellPoints[2 * j]);
764 cacheItem->Lines.push_back(this->CellPoints[2 * j + 1]);
766 double* color4 = this->CellColors->GetTuple(j);
767 cacheItem->LineColors->InsertTuple4(vertOffset + j, color4[0], color4[1], color4[2],
771 vertOffset += this->NumPointsCell;
772 this->CellColors->Reset();
773 this->CellPoints.clear();
778 cacheItem->LinesLoadingTime.Modified();
782 if (cacheItem->Lines.size() > 0)
784 this->Device->DrawLines(&cacheItem->Lines[0], cacheItem->Lines.size() / 2,
785 static_cast<unsigned char*
>(cacheItem->LineColors->GetVoidPointer(0)),
786 cacheItem->LineColors->GetNumberOfComponents());
808 numTriVert += 3 * (this->NumPointsCell - 2);
820 void DrawPolygons(
vtkPolyData* polyData,
int scalarMode,
float const x,
821 float const y,
float const scale)
823 PolyDataCacheItem* cacheItem = this->cache->GetCacheEntry(polyData);
825 if (polyData->
GetMTime() > cacheItem->PolygonsLoadingTime)
827 cacheItem->PolyTri.clear();
828 cacheItem->PolyColors->Reset();
831 vtkIdType const totalTriVert = this->GetCountTriangleVertices(polyData);
832 cacheItem->PolyTri.reserve(totalTriVert * 2);
833 cacheItem->PolyColors->SetNumberOfComponents(this->Colors->GetNumberOfComponents());
834 cacheItem->PolyColors->SetNumberOfTuples(totalTriVert);
839 cacheItem->PolyColors->SetNumberOfComponents(this->Colors->GetNumberOfComponents());
856 this->MapCurrentCell(x, y, scale, cellId, scalarMode);
859 for (
int i = 0; i < this->NumPointsCell - 2; i++)
861 cacheItem->PolyTri.push_back(this->CellPoints[0]);
862 cacheItem->PolyTri.push_back(this->CellPoints[1]);
863 cacheItem->PolyTri.push_back(this->CellPoints[i * 2 + 2]);
864 cacheItem->PolyTri.push_back(this->CellPoints[i * 2 + 3]);
865 cacheItem->PolyTri.push_back(this->CellPoints[i * 2 + 4]);
866 cacheItem->PolyTri.push_back(this->CellPoints[i * 2 + 5]);
869 vtkIdType const triangOffset = vertOffset + 3 * i;
870 double* color4 = this->CellColors->GetTuple(0);
871 cacheItem->PolyColors->InsertTuple4(triangOffset, color4[0], color4[1],
872 color4[2], color4[3]);
874 color4 = this->CellColors->GetTuple(i + 1);
875 cacheItem->PolyColors->InsertTuple4(triangOffset + 1, color4[0], color4[1],
876 color4[2], color4[3]);
878 color4 = this->CellColors->GetTuple(i + 2);
879 cacheItem->PolyColors->InsertTuple4(triangOffset + 2, color4[0], color4[1],
880 color4[2], color4[3]);
883 vertOffset += 3 * (this->NumPointsCell - 2);
884 this->CellColors->Reset();
885 this->CellPoints.clear();
889 cacheItem->PolygonsLoadingTime.Modified();
893 if (cacheItem->PolyTri.size() > 0)
895 this->Device->CoreDrawTriangles(cacheItem->PolyTri,
896 static_cast<unsigned char*>(cacheItem->PolyColors->GetVoidPointer(0)), 4);
911 std::vector<float> CellPoints;
915 PolyDataCache* cache;
917 #endif // VTKOPENGLCONTEXTDEVICE2DPRIVATE_H
GLuint TextureFromImage(vtkImageData *image)
bool operator==(const CacheElement &other) const
vtkIdType GetNumberOfPoints()
Return the number of points in the cell.
virtual int GetJustification()
Set/Get the horizontal justification to left (default), centered, or right.
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
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)
std::list< CacheElement > Cache
List of a pair of key and cache data.
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)
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.
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.
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
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).
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.
virtual bool IsDoneWithTraversal()=0
Returns false while the iterator is valid.
vtkSmartPointer< vtkImageData > ImageData
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.