33 #ifndef vtkOpenGLContextDevice2DPrivate_h 
   34 #define vtkOpenGLContextDevice2DPrivate_h 
  101       return this->first == other.first;
 
  117     return std::find(this->
Cache.begin(), this->Cache.end(), 
key) != this->
Cache.end();
 
  135     typename std::list<CacheElement>::iterator it;
 
  136     for (it = this->
Cache.begin(); it != this->Cache.end(); ++it)
 
  138       it->second.Texture->ReleaseGraphicsResources(window);
 
  152     if (this->
Cache.size() >= this->MaxSize)
 
  154       this->
Cache.pop_back();
 
  156     this->
Cache.push_front(CacheElement(
key, cacheData));
 
  157     return this->
Cache.begin()->second;
 
  177   typename std::list<CacheElement>::iterator it =
 
  178     std::find(this->Cache.begin(), this->Cache.end(), CacheElement(
key));
 
  179   if (it != this->Cache.end())
 
  186   cacheData.Texture->SetInputData(cacheData.ImageData);
 
  187   return this->AddCacheData(
key, cacheData);
 
  193 template <
class StringType>
 
  210     vtkTypeUInt32 hash = 
static_cast<vtkTypeUInt32
>(id);
 
  214     assert(
"Hash is really a uint32" && 
static_cast<size_t>(hash) == 
id);
 
  238     this->
Color.
Set(
static_cast<unsigned char>(color[0] * 255),
 
  239       static_cast<unsigned char>(
color[1] * 255), 
static_cast<unsigned char>(
color[2] * 255),
 
  240       static_cast<unsigned char>(textProperty->
GetOpacity() * 255));
 
  337     float* texCoord = 
new float[2 * n];
 
  343     for (
int i = 0; i < n; ++i)
 
  345       minX = fptr[0] < minX ? fptr[0] : minX;
 
  346       maxX = fptr[0] > maxX ? fptr[0] : maxX;
 
  347       minY = fptr[1] < minY ? fptr[1] : minY;
 
  348       maxY = fptr[1] > maxY ? fptr[1] : maxY;
 
  356         (textureBounds[1] - textureBounds[0]) ? textureBounds[1] - textureBounds[0] : 1.;
 
  358         (textureBounds[3] - textureBounds[2]) ? textureBounds[3] - textureBounds[2] : 1.;
 
  359       for (
int i = 0; i < n; ++i)
 
  361         texCoord[i * 2] = (fptr[0] - minX) / rangeX;
 
  362         texCoord[i * 2 + 1] = (fptr[1] - minY) / rangeY;
 
  368       float rangeX = (maxX - minX) ? maxX - minX : 1.f;
 
  369       float rangeY = (maxY - minY) ? maxY - minY : 1.f;
 
  370       for (
int i = 0; i < n; ++i)
 
  372         texCoord[i * 2] = (fptr[0] - minX) / rangeX;
 
  373         texCoord[i * 2 + 1] = (fptr[1] - minY) / rangeY;
 
  383     for (
int i = 0; i < 2; ++i)
 
  385       while (pow2[i] < 
size[i])
 
  397       vtkGenericWarningMacro(
"Invalid image format: expected unsigned char.");
 
  400     int bytesPerPixel = 
image->GetNumberOfScalarComponents();
 
  405     for (
int i = 0; i < 2; ++i)
 
  407       texCoords[i] = 
size[i] / float(newImg[i]);
 
  410     unsigned char* dataPtr = 
new unsigned char[newImg[0] * newImg[1] * bytesPerPixel];
 
  411     unsigned char* origPtr = 
static_cast<unsigned char*
>(
image->GetScalarPointer());
 
  413     for (
int i = 0; i < newImg[0]; ++i)
 
  415       for (
int j = 0; j < newImg[1]; ++j)
 
  417         for (
int k = 0; k < bytesPerPixel; ++k)
 
  421             dataPtr[i * bytesPerPixel + j * newImg[0] * bytesPerPixel + k] =
 
  422               origPtr[i * bytesPerPixel + j * 
size[0] * bytesPerPixel + k];
 
  426             dataPtr[i * bytesPerPixel + j * newImg[0] * bytesPerPixel + k] = k == 3 ? 0 : 255;
 
  433     GLint glFormat = bytesPerPixel == 3 ? GL_RGB : GL_RGBA;
 
  434     GLint glInternalFormat = bytesPerPixel == 3 ? GL_RGB8 : GL_RGBA8;
 
  436     glGenTextures(1, &tmpIndex);
 
  437     glBindTexture(GL_TEXTURE_2D, tmpIndex);
 
  439     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
 
  440     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
 
  441     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
 
  442     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
 
  444     glTexImage2D(GL_TEXTURE_2D, 0, glInternalFormat, newImg[0], newImg[1], 0, glFormat,
 
  445       GL_UNSIGNED_BYTE, 
static_cast<const GLvoid*
>(dataPtr));
 
  454       cout << 
"Error = not an unsigned char..." << endl;
 
  457     int bytesPerPixel = 
image->GetNumberOfScalarComponents();
 
  461     unsigned char* dataPtr = 
static_cast<unsigned char*
>(
image->GetScalarPointer());
 
  463     GLint glFormat = bytesPerPixel == 3 ? GL_RGB : GL_RGBA;
 
  464     GLint glInternalFormat = bytesPerPixel == 3 ? GL_RGB8 : GL_RGBA8;
 
  466     glGenTextures(1, &tmpIndex);
 
  467     glBindTexture(GL_TEXTURE_2D, tmpIndex);
 
  469     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
 
  470     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
 
  471     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
 
  472     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
 
  474     glTexImage2D(GL_TEXTURE_2D, 0, glInternalFormat, 
size[0], 
size[1], 0, glFormat,
 
  475       GL_UNSIGNED_BYTE, 
static_cast<const GLvoid*
>(dataPtr));
 
  544     this->cache = 
new PolyDataCache();
 
  556     this->Colors = colors;
 
  557     this->CellColors->SetNumberOfComponents(colors->GetNumberOfComponents());
 
  562         this->DrawLines(polyData, scalarMode, x, y, 
scale);
 
  566         this->DrawPolygons(polyData, scalarMode, x, y, 
scale);
 
  577   struct PolyDataCacheItem
 
  581     std::vector<float> PolyTri;
 
  585     std::vector<float> Lines;
 
  594       std::map<vtkPolyData*, PolyDataCacheItem*>::iterator itPrev = this->PrevFrameCache.begin();
 
  595       for (; itPrev != this->PrevFrameCache.end(); ++itPrev)
 
  597         delete itPrev->second;
 
  600       std::map<vtkPolyData*, PolyDataCacheItem*>::iterator it = this->CurrentFrameCache.begin();
 
  601       for (; it != this->CurrentFrameCache.end(); ++it)
 
  609       PolyDataCacheItem* cacheItem = this->CurrentFrameCache[
key];
 
  610       if (cacheItem == 
nullptr)
 
  612         cacheItem = this->PrevFrameCache[
key];
 
  613         if (cacheItem == 
nullptr)
 
  615           cacheItem = 
new PolyDataCacheItem();
 
  622           this->PrevFrameCache.erase(
key);
 
  626         this->CurrentFrameCache[
key] = cacheItem;
 
  637       std::map<vtkPolyData*, PolyDataCacheItem*>::iterator itPrev = this->PrevFrameCache.begin();
 
  638       for (; itPrev != this->PrevFrameCache.end(); ++itPrev)
 
  640         delete itPrev->second;
 
  644       this->PrevFrameCache.clear();
 
  647       std::swap(this->PrevFrameCache, this->CurrentFrameCache);
 
  652     std::map<vtkPolyData*, PolyDataCacheItem*> PrevFrameCache;
 
  653     std::map<vtkPolyData*, PolyDataCacheItem*> CurrentFrameCache;
 
  660     float const posX, 
float const posY, 
float const scale, 
vtkIdType cellId, 
int scalarMode)
 
  662     this->CellPoints.reserve(this->NumPointsCell * 2);        
 
  663     this->CellColors->SetNumberOfTuples(this->NumPointsCell); 
 
  664     for (
int i = 0; i < this->NumPointsCell; i++)
 
  670       float const x = 
static_cast<float>(
point[0]) + posX;
 
  671       float const y = 
static_cast<float>(
point[1]) + posY;
 
  672       this->CellPoints.push_back(x * 
scale);
 
  673       this->CellPoints.push_back(y * 
scale);
 
  680           mappedColorId = this->PointIds[i];
 
  683           mappedColorId = cellId;
 
  686           std::cerr << 
"Scalar mode not supported!" << std::endl;
 
  690       this->CellColors->SetTuple(i, mappedColorId, this->Colors);
 
  700     vtkPolyData* polyData, 
int scalarMode, 
float const x, 
float const y, 
float const scale)
 
  702     PolyDataCacheItem* cacheItem = this->cache->GetCacheEntry(polyData);
 
  704     if (polyData->
GetMTime() > cacheItem->LinesLoadingTime)
 
  707       cacheItem->Lines.clear();
 
  708       cacheItem->LineColors->Reset();
 
  712       cacheItem->Lines.reserve(numVertices * 2);                      
 
  714       cacheItem->LineColors->SetNumberOfTuples(numVertices);
 
  726           vtkIdType actualNumPointsCell = genericCell->GetNumberOfPoints();
 
  728           for (
int i = 0; i < actualNumPointsCell - 1; ++i)
 
  730             this->NumPointsCell = 2;
 
  731             this->PointIds = genericCell->GetPointIds()->
GetPointer(i);
 
  733             this->MapCurrentCell(x, y, 
scale, cellId, scalarMode);
 
  736             for (
int j = 0; j < this->NumPointsCell; j++)
 
  738               cacheItem->Lines.push_back(this->CellPoints[2 * j]);
 
  739               cacheItem->Lines.push_back(this->CellPoints[2 * j + 1]);
 
  741               double* color4 = this->CellColors->GetTuple(j);
 
  742               cacheItem->LineColors->InsertTuple4(
 
  743                 vertOffset + j, color4[0], color4[1], color4[2], color4[3]);
 
  746             vertOffset += this->NumPointsCell;
 
  747             this->CellColors->
Reset();
 
  748             this->CellPoints.clear();
 
  753       cacheItem->LinesLoadingTime.Modified();
 
  757     if (cacheItem->Lines.size() > 0)
 
  759       this->Device->
DrawLines(&cacheItem->Lines[0], 
static_cast<int>(cacheItem->Lines.size() / 2),
 
  760         static_cast<unsigned char*
>(cacheItem->LineColors->GetVoidPointer(0)),
 
  761         cacheItem->LineColors->GetNumberOfComponents());
 
  780       this->NumPointsCell = genericCell->GetNumberOfPoints();
 
  781       this->PointIds = genericCell->GetPointIds()->
GetPointer(0);
 
  782       numTriVert += 3 * (this->NumPointsCell - 2);
 
  795     vtkPolyData* polyData, 
int scalarMode, 
float const x, 
float const y, 
float const scale)
 
  797     PolyDataCacheItem* cacheItem = this->cache->GetCacheEntry(polyData);
 
  799     if (polyData->
GetMTime() > cacheItem->PolygonsLoadingTime)
 
  801       cacheItem->PolyTri.clear();
 
  802       cacheItem->PolyColors->Reset();
 
  805       vtkIdType const totalTriVert = this->GetCountTriangleVertices(polyData);
 
  806       cacheItem->PolyTri.reserve(totalTriVert * 2); 
 
  808       cacheItem->PolyColors->SetNumberOfTuples(totalTriVert);
 
  825           this->NumPointsCell = genericCell->GetNumberOfPoints();
 
  826           this->PointIds = genericCell->GetPointIds()->
GetPointer(0);
 
  828           this->MapCurrentCell(x, y, 
scale, cellId, scalarMode);
 
  831           for (
int i = 0; i < this->NumPointsCell - 2; i++)
 
  833             cacheItem->PolyTri.push_back(this->CellPoints[0]);
 
  834             cacheItem->PolyTri.push_back(this->CellPoints[1]);
 
  835             cacheItem->PolyTri.push_back(this->CellPoints[i * 2 + 2]);
 
  836             cacheItem->PolyTri.push_back(this->CellPoints[i * 2 + 3]);
 
  837             cacheItem->PolyTri.push_back(this->CellPoints[i * 2 + 4]);
 
  838             cacheItem->PolyTri.push_back(this->CellPoints[i * 2 + 5]);
 
  841             vtkIdType const triangOffset = vertOffset + 3 * i;
 
  842             double* color4 = this->CellColors->GetTuple(0);
 
  843             cacheItem->PolyColors->InsertTuple4(
 
  844               triangOffset, color4[0], color4[1], color4[2], color4[3]);
 
  846             color4 = this->CellColors->GetTuple(i + 1);
 
  847             cacheItem->PolyColors->InsertTuple4(
 
  848               triangOffset + 1, color4[0], color4[1], color4[2], color4[3]);
 
  850             color4 = this->CellColors->GetTuple(i + 2);
 
  851             cacheItem->PolyColors->InsertTuple4(
 
  852               triangOffset + 2, color4[0], color4[1], color4[2], color4[3]);
 
  855           vertOffset += 3 * (this->NumPointsCell - 2); 
 
  856           this->CellColors->
Reset();
 
  857           this->CellPoints.clear();
 
  861       cacheItem->PolygonsLoadingTime.Modified();
 
  865     if (cacheItem->PolyTri.size() > 0)
 
  868         static_cast<unsigned char*
>(cacheItem->PolyColors->GetVoidPointer(0)), 4);
 
  883   std::vector<float> CellPoints;
 
  887   PolyDataCache* cache;
 
  889 #endif // VTKOPENGLCONTEXTDEVICE2DPRIVATE_H