Commit c673d89a authored by Will Schroeder's avatar Will Schroeder
Browse files

ENH:Improved SetValue() interface

parent c700a858
......@@ -47,7 +47,7 @@ vtkScalars *vtkAGraymap::MakeObject(int sze, int ext)
float vtkAGraymap::GetScalar(int i)
{
return (float)(this->S[2*i]);
return (float)(this->S.GetValue(2*i));
}
// Description:
......@@ -55,8 +55,8 @@ float vtkAGraymap::GetScalar(int i)
unsigned char *vtkAGraymap::GetColor(int id)
{
static unsigned char rgba[4];
rgba[0] = rgba[1] = rgba[2] = this->S[2*id];
rgba[3] = this->S[2*id+1];
rgba[0] = rgba[1] = rgba[2] = this->S.GetValue(2*id);
rgba[3] = this->S.GetValue(2*id+1);
return rgba;
}
......@@ -66,8 +66,8 @@ unsigned char *vtkAGraymap::GetColor(int id)
// point id.
void vtkAGraymap::GetColor(int id, unsigned char rgba[4])
{
rgba[0] = rgba[1] = rgba[2] = this->S[2*id];
rgba[3] = this->S[2*id+1];
rgba[0] = rgba[1] = rgba[2] = this->S.GetValue(2*id);
rgba[3] = this->S.GetValue(2*id+1);
}
// Description:
......@@ -83,8 +83,8 @@ vtkAGraymap& vtkAGraymap::operator=(const vtkAGraymap& fs)
unsigned char *vtkAGraymap::GetAGrayValue(int id)
{
static unsigned char ga[2];
ga[0] = this->S[2*id];
ga[1] = this->S[2*id+1];
ga[0] = this->S.GetValue(2*id);
ga[1] = this->S.GetValue(2*id+1);
return ga;
}
......@@ -94,18 +94,25 @@ unsigned char *vtkAGraymap::GetAGrayValue(int id)
// point id.
void vtkAGraymap::GetAGrayValue(int id, unsigned char ga[2])
{
ga[0] = this->S[2*id];
ga[1] = this->S[2*id+1];
id *= 2;
ga[0] = this->S.GetValue(id);
ga[1] = this->S.GetValue(id+1);
}
void vtkAGraymap::SetNumberOfColors(int number)
{
this->S.SetNumberOfValues(number*2);
}
// Description:
// Set a gray-alpha value at a particular array location. Does not do
// range checking.
// range checking. Make sure you use SetNumberOfColors() to allocate
// memory prior to using this method.
void vtkAGraymap::SetAGrayValue(int i, unsigned char ga[2])
{
i *= 2;
this->S[i] = ga[0];
this->S[i+1] = ga[1];
this->S.SetValue(i, ga[0]);
this->S.SetValue(i+1, ga[1]);
}
// Description:
......@@ -113,8 +120,9 @@ void vtkAGraymap::SetAGrayValue(int i, unsigned char ga[2])
// checking and will allocate additional memory if necessary.
void vtkAGraymap::InsertAGrayValue(int i, unsigned char ga[2])
{
this->S.InsertValue(2*i+1, ga[1]);
this->S[2*i] = ga[0];
i *= 2;
this->S.InsertValue(i+1, ga[1]);
this->S.SetValue(i, ga[0]);
}
// Description:
......
......@@ -80,11 +80,11 @@ public:
void Reset() {this->S.Reset();};
unsigned char *GetPtr(const int id);
unsigned char *WritePtr(const int id, const int number);
void WrotePtr();
// vtkColorScalar interface.
unsigned char *GetColor(int id);
void GetColor(int id, unsigned char rgba[4]);
void SetNumberOfColors(int number);
void SetColor(int id, unsigned char rgba[4]);
void InsertColor(int id, unsigned char rgba[4]);
int InsertNextColor(unsigned char rgba[4]);
......@@ -102,15 +102,16 @@ protected:
// Description:
// Set a rgba color value at a particular array location. Does not do
// range checking.
// range checking. Make sure you use SetNumberOfColors() to allocate
// memory prior to using SetColor().
inline void vtkAGraymap::SetColor(int i, unsigned char rgba[4])
{
float g = 0.30*rgba[0] + 0.59*rgba[1] + 0.11*rgba[2];
g = (g > 255.0 ? 255.0 : g);
i *= 2;
this->S[i] = (unsigned char)g;
this->S[i+1] = rgba[3];
this->S.SetValue(i, (unsigned char)g);
this->S.SetValue(i+1, rgba[3]);
}
// Description:
......@@ -122,7 +123,7 @@ inline void vtkAGraymap::InsertColor(int i, unsigned char rgba[4])
g = (g > 255.0 ? 255.0 : g);
this->S.InsertValue(2*i+1, rgba[3]);
this->S[2*i] = (unsigned char)g;
this->S.SetValue(2*i, (unsigned char)g);
}
// Description:
......@@ -152,16 +153,10 @@ inline unsigned char *vtkAGraymap::GetPtr(const int id)
// Get pointer to data array. Useful for direct writes of data. MaxId is
// bumped by number (and memory allocated if necessary). Id is the
// location you wish to write into; number is the number of scalars to
// write. Use the method WrotePtr() to mark completion of write.
// write.
inline unsigned char *vtkAGraymap::WritePtr(const int id, const int number)
{
return this->S.WritePtr(2*id,2*number);
}
// Description:
// Terminate direct write of data. Although dummy routine now, reserved for
// future use.
inline void vtkAGraymap::WrotePtr() {}
#endif
......@@ -67,12 +67,17 @@ void vtkAPixmap::GetColor(int id, unsigned char rgba[4])
rgba[3] = _rgba[3];
}
void vtkAPixmap::SetNumberOfColors(int number)
{
this->S.SetNumberOfValues(number*4);
}
// Description:
// Insert color into object. No range checking performed (fast!).
void vtkAPixmap::SetColor(int id, unsigned char rgba[4])
{
id *= 4;
memcpy (&(this->S[id]), rgba, 4);
memcpy (this->S.GetPtr(id), rgba, 4);
}
// Description:
......
......@@ -75,11 +75,11 @@ public:
void Reset() {this->S.Reset();};
unsigned char *GetPtr(const int id);
unsigned char *WritePtr(const int id, const int number);
void WrotePtr();
// vtkColorScalar interface.
unsigned char *GetColor(int id);
void GetColor(int id, unsigned char rgba[4]);
void SetNumberOfColors(int number);
void SetColor(int id, unsigned char rgba[4]);
void InsertColor(int id, unsigned char rgba[4]);
int InsertNextColor(unsigned char rgba[4]);
......@@ -106,15 +106,10 @@ inline unsigned char *vtkAPixmap::GetPtr(const int id)
// Get pointer to data array. Useful for direct writes of data. MaxId is
// bumped by number (and memory allocated if necessary). Id is the
// location you wish to write into; number is the number of scalars to
// write. Use the method WrotePtr() to mark completion of write.
// write.
inline unsigned char *vtkAPixmap::WritePtr(const int id, const int number)
{
return this->S.WritePtr(4*id,4*number);
}
// Description:
// Terminate direct write of data. Although dummy routine now, reserved for
// future use.
inline void vtkAPixmap::WrotePtr() {}
#endif
......@@ -49,13 +49,17 @@ int vtkBitArray::GetValue(const int id)
};
// Description:
// Allocate memory for this array. Delete old storage if present.
// Allocate memory for this array. Delete old storage only if necessary.
int vtkBitArray::Allocate(const int sz, const int ext)
{
if ( this->Array != NULL ) delete [] this->Array;
if ( sz > this->Size || this->Array == NULL )
{
delete [] this->Array;
this->Size = ( sz > 0 ? sz : 1);
if ( (this->Array = new unsigned char[(this->Size+7)/8]) == NULL ) return 0;
}
this->Size = ( (sz/8) > 0 ? sz : 1);
if ( (this->Array = new unsigned char[(this->Size+7)/8]) == NULL ) return 0;
this->Extend = ( ext > 0 ? ext : 1);
this->MaxId = -1;
......
......@@ -54,7 +54,7 @@ class VTK_EXPORT vtkBitArray : public vtkObject
{
public:
vtkBitArray():Array(NULL),Size(0),MaxId(-1),Extend(1000) {};
int Allocate(const int sz, const int ext);
int Allocate(const int sz, const int ext=1000);
void Initialize();
vtkBitArray(const int sz, const int ext=1000);
vtkBitArray(const vtkBitArray& ia);
......@@ -64,6 +64,8 @@ public:
// access/insertion methods
int GetValue(const int id);
void SetNumberOfValues(const int number);
void SetValue(const int id, const int value);
vtkBitArray &InsertValue(const int id, const int i);
int InsertNextValue(const int i);
unsigned char *GetPtr(const int id);
......@@ -73,7 +75,6 @@ public:
vtkBitArray &operator=(const vtkBitArray& ia);
vtkBitArray &operator+=(const vtkBitArray& ia);
void operator+=(const char i);
vtkBitArray &SetValue(const int id, const int i);
// miscellaneous methods
void Squeeze();
......@@ -109,15 +110,22 @@ inline unsigned char *vtkBitArray::WritePtr(const int id, const int number)
}
// Description:
// Insert data at a specified position in the array. Does not perform
// range checking.
inline vtkBitArray& vtkBitArray::SetValue(const int id, const int i)
// Specify the number of values for this object to hold. Does an
// allocation as well as setting the MaxId ivar. Used in conjunction with
// SetValue() method for fast insertion.
inline void vtkBitArray::SetNumberOfValues(const int number)
{
if (i) this->Array[id/8] |= (0x80 >> id%8);
this->Allocate(number);
this->MaxId = number - 1;
}
// Description:
// Set the data at a particular index. Does not do range checking. Make sure
// you use the method SetNumberOfValues() before inserting data.
inline void vtkBitArray::SetValue(const int id, const int value)
{
if (value) this->Array[id/8] |= (0x80 >> id%8);
else this->Array[id/8] &= (~(0x80 >> id%8));
if ( id > this->MaxId ) this->MaxId = id;
return *this;
}
// Description:
......
......@@ -66,6 +66,7 @@ public:
int GetNumberOfScalars() {return (this->S.GetMaxId()+1);};
void Squeeze() {this->S.Squeeze();};
float GetScalar(int i) {return (float)this->S.GetValue(i);};
void SetNumberOfScalars(int number);
void SetScalar(int i, int s) {this->S.SetValue(i,s);};
void SetScalar(int i, float s) {this->S.SetValue(i,(int)s);};
void InsertScalar(int i, float s) {S.InsertValue(i,(int)s);};
......@@ -77,7 +78,6 @@ public:
// miscellaneous
unsigned char *GetPtr(const int id);
unsigned char *WritePtr(const int id, const int number);
void WrotePtr();
vtkBitScalars &operator=(const vtkBitScalars& cs);
void operator+=(const vtkBitScalars& cs) {this->S += cs.S;};
void Reset() {this->S.Reset();};
......@@ -86,6 +86,11 @@ protected:
vtkBitArray S;
};
inline void vtkBitScalars::SetNumberOfScalars(int number)
{
this->S.SetNumberOfValues(number);
}
// Description:
// Get pointer to array of data starting at data position "id".
inline unsigned char *vtkBitScalars::GetPtr(const int id)
......@@ -97,15 +102,10 @@ inline unsigned char *vtkBitScalars::GetPtr(const int id)
// Get pointer to data array. Useful for direct writes of data. MaxId is
// bumped by number (and memory allocated if necessary). Id is the
// location you wish to write into; number is the number of scalars to
// write. Use the method WrotePtr() to mark completion of write.
// write.
inline unsigned char *vtkBitScalars::WritePtr(const int id, const int number)
{
return this->S.WritePtr(id,number);
}
// Description:
// Terminate direct write of data. Although dummy routine now, reserved for
// future use.
inline void vtkBitScalars::WrotePtr() {}
#endif
......@@ -79,6 +79,7 @@ public:
// vtkColorScalar interface.
unsigned char *GetColor(int id);
void GetColor(int id, unsigned char rgba[4]);
void SetNumberOfColors(int number);
void SetColor(int id, unsigned char rgba[4]);
void InsertColor(int id, unsigned char rgba[4]);
int InsertNextColor(unsigned char rgba[4]);
......@@ -87,6 +88,11 @@ protected:
vtkBitArray S;
};
inline void vtkBitmap::SetNumberOfColors(int number)
{
this->S.SetNumberOfValues(number);
}
// Description:
// Get pointer to byte containing bit in question. You will have to decompose
// byte to obtain appropriate bit value.
......
......@@ -59,7 +59,7 @@ void vtkCell::Initialize(int npts, int *pts, vtkPoints *p)
for (int i=0; i<npts; i++)
{
this->PointIds.InsertId(i,pts[i]);
this->Points.SetPoint(i,p->GetPoint(pts[i]));
this->Points.InsertPoint(i,p->GetPoint(pts[i]));
}
}
......@@ -216,23 +216,28 @@ float vtkCell::GetLength2 ()
void vtkCell::PrintSelf(ostream& os, vtkIndent indent)
{
float *bounds;
int i;
int numIds=this->PointIds.GetNumberOfIds();
vtkObject::PrintSelf(os,indent);
os << indent << "Number Of Points: " << this->PointIds.GetNumberOfIds() << "\n";
bounds = this->GetBounds();
os << indent << "Bounds: \n";
os << indent << " Xmin,Xmax: (" << bounds[0] << ", " << bounds[1] << ")\n";
os << indent << " Ymin,Ymax: (" << bounds[2] << ", " << bounds[3] << ")\n";
os << indent << " Zmin,Zmax: (" << bounds[4] << ", " << bounds[5] << ")\n";
os << indent << "Number Of Points: " << numIds << "\n";
os << indent << " Point ids are: ";
for (i=0; this->PointIds.GetNumberOfIds(); i++)
if ( numIds > 0 )
{
os << ", " << this->PointIds.GetId(i);
if ( i && !(i % 12) ) os << "\n\t";
float *bounds=this->GetBounds();
os << indent << "Bounds: \n";
os << indent << " Xmin,Xmax: (" << bounds[0] << ", " << bounds[1] << ")\n";
os << indent << " Ymin,Ymax: (" << bounds[2] << ", " << bounds[3] << ")\n";
os << indent << " Zmin,Zmax: (" << bounds[4] << ", " << bounds[5] << ")\n";
os << indent << " Point ids are: ";
for (int i=0; i < numIds; i++)
{
os << this->PointIds.GetId(i);
if ( i && !(i % 12) ) os << "\n\t";
else if ( i != (numIds-1) ) os << ", ";
}
os << indent << "\n";
}
os << indent << "\n";
}
......@@ -101,7 +101,6 @@ public:
// miscellaneous pointer type operations (for fast read/write operations)
int *GetPtr();
int *WritePtr(const int ncells, const int size);
void WrotePtr();
// reuse memory
void Reset();
......@@ -173,7 +172,7 @@ inline void vtkCellArray::InsertCellPoint(int id)
// update the number of points defining the cell.
inline void vtkCellArray::UpdateCellCount(int npts)
{
this->Ia[this->Location-npts-1] = npts;
this->Ia.SetValue(this->Location-npts-1, npts);
}
// Description:
......@@ -304,7 +303,6 @@ inline int *vtkCellArray::GetPtr()
// Get pointer to data array for purpose of direct writes of data. Size is the
// total storage consumed by the cell array. ncells is the number of cells
// represented in the array.
// Use the method WrotePtr() to mark completion of write.
inline int *vtkCellArray::WritePtr(const int ncells, const int size)
{
this->NumberOfCells = ncells;
......@@ -312,9 +310,4 @@ inline int *vtkCellArray::WritePtr(const int ncells, const int size)
return this->Ia.WritePtr(0,size);
}
// Description:
// Terminate direct write of data. Although dummy routine now, reserved for
// future use.
inline void vtkCellArray::WrotePtr() {}
#endif
......@@ -56,7 +56,8 @@ float vtkColorScalars::GetScalar(int i)
}
// Description:
// Map through lookup table to set the color.
// Map through lookup table to set the color. Make sure that you've
// used the method SetNumberOfScalars() to allocate storage.
void vtkColorScalars::SetScalar(int i, float s)
{
if ( this->LookupTable == NULL ) this->CreateDefaultLookupTable();
......
......@@ -74,6 +74,7 @@ public:
char *GetScalarType() {return "ColorScalar";};
char *GetDataType() {return "unsigned char";};
float GetScalar(int i);
void SetNumberOfScalars(int number) {this->SetNumberOfColors(number);};
void SetScalar(int i, float s);
void InsertScalar(int i, float s);
int InsertNextScalar(float s);
......@@ -99,8 +100,16 @@ public:
// class must convert it to rgba form.
virtual void GetColor(int id, unsigned char rgba[4]) = 0;
// Description:
// Specify the number of colors for this object to hold. Does an
// allocation as well as setting the MaxId ivar. Used in conjunction with
// SetColor() method for fast insertion.
virtual void SetNumberOfColors(int number) = 0;
// Description:
// Insert color into object. No range checking performed (fast!).
// Make sure you use SetNumberOfColors() to allocate memory prior
// to using SetColor().
virtual void SetColor(int id, unsigned char rgba[4]) = 0;
// Description:
......
......@@ -41,13 +41,17 @@ MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
#include "vtkDoubleArray.h"
// Description:
// Allocate memory for this array. Delete old storage if present.
// Allocate memory for this array. Delete old storage only if necessary.
int vtkDoubleArray::Allocate(const int sz, const int ext)
{
if ( this->Array ) delete [] this->Array;
if ( sz > this->Size || this->Array == NULL )
{
delete [] this->Array;
this->Size = ( sz > 0 ? sz : 1);
if ( (this->Array = new double[this->Size]) == NULL ) return 0;
}
this->Size = ( sz > 0 ? sz : 1);
if ( (this->Array = new double[this->Size]) == NULL ) return 0;
this->Extend = ( ext > 0 ? ext : 1);
this->MaxId = -1;
......
......@@ -63,6 +63,8 @@ public:
// access/insertion methods
double GetValue(const int id);
void SetNumberOfValues(const int number);
void SetValue(const int id, const double value);
vtkDoubleArray &InsertValue(const int id, const double f);
int InsertNextValue(const double f);
double *GetPtr(const int id);
......@@ -72,7 +74,6 @@ public:
vtkDoubleArray &operator=(const vtkDoubleArray& fa);
void operator+=(const vtkDoubleArray& fa);
void operator+=(const double f);
double& operator[](const int i);
// miscellaneous methods
void Squeeze();
......@@ -92,6 +93,24 @@ private:
// Get the data at a particular index.
inline double vtkDoubleArray::GetValue(const int id) {return this->Array[id];};
// Description:
// Specify the number of values for this object to hold. Does an
// allocation as well as setting the MaxId ivar. Used in conjunction with
// SetValue() method for fast insertion.
inline void vtkDoubleArray::SetNumberOfValues(const int number)
{
this->Allocate(number);
this->MaxId = number - 1;
}
// Description:
// Set the data at a particular index. Does not do range checking. Make sure
// you use the method SetNumberOfValues() before inserting data.
inline void vtkDoubleArray::SetValue(const int id, const double value)
{
this->Array[id] = value;
}
// Description:
// Get the address of a particular data index.
inline double *vtkDoubleArray::GetPtr(const int id) {return this->Array + id;};
......@@ -130,15 +149,6 @@ inline void vtkDoubleArray::operator+=(const double f)
this->InsertNextValue(f);
}
// Description:
// Does insert or get (depending on location on lhs or rhs of statement). Does
// not do automatic resizing - user's responsibility to range check.
inline double& vtkDoubleArray::operator[](const int i)
{
if (i > this->MaxId) this->MaxId = i;
return this->Array[i];
}
// Description:
// Resize object to just fit data requirement. Reclaims extra memory.
inline void vtkDoubleArray::Squeeze() {this->Resize (this->MaxId+1);};
......
......@@ -41,13 +41,17 @@ MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
#include "vtkFloatArray.h"
// Description:
// Allocate memory for this array. Delete old storage if present.
// Allocate memory for this array. Delete old storage only if necessary.
int vtkFloatArray::Allocate(const int sz, const int ext)
{
if ( this->Array ) delete [] this->Array;
if ( sz > this->Size || this->Array == NULL )
{
delete [] this->Array;
this->Size = ( sz > 0 ? sz : 1);
if ( (this->Array = new float[this->Size]) == NULL ) return 0;
}
this->Size = ( sz > 0 ? sz : 1);
if ( (this->Array = new float[this->Size]) == NULL ) return 0;
this->Extend = ( ext > 0 ? ext : 1);
this->MaxId = -1;
......
......@@ -63,6 +63,8 @@ public:
// access/insertion methods
float GetValue(const int id);
void SetNumberOfValues(const int number);
void SetValue(const int id, const float value);
vtkFloatArray &InsertValue(const int id, const float f);
int InsertNextValue(const float f);
float *GetPtr(const int id);
......@@ -72,7 +74,6 @@ public:
vtkFloatArray &operator=(const vtkFloatArray& fa);
void operator+=(const vtkFloatArray& fa);
void operator+=(const float f);
float& operator[](const int i);
// miscellaneous methods
void Squeeze();
......@@ -81,10 +82,10 @@ public:
void Reset();
private:
float *Array; // pointer to data
int Size; // allocated size of data
float *Array; // pointer to data
int Size; // allocated size of data
int MaxId; // maximum index inserted thus far
int Extend; // grow array by this point
int Extend; // grow array by this point
float *Resize(const int sz); // function to resize data
};
......@@ -92,6 +93,24 @@ private:
// Get the data at a particular index.
inline float vtkFloatArray::GetValue(const int id) {return this->Array[id];};
// Description:
// Specify the number of values for this object to hold. Does an
// allocation as well as setting the MaxId ivar. Used in conjunction with
// SetValue() method for fast insertion.
inline void vtkFloatArray::SetNumberOfValues(const int number)
{
this->Allocate(number);
this->MaxId = number - 1;
}
// Description:
// Set the data at a particular index. Does not do range checking. Make sure
// you use the method SetNumberOfValues() before inserting data.
inline void vtkFloatArray::SetValue(const int id, const float value)
{
this->Array[id] = value;
}