1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkPoints.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkPoints.h"
#include "vtkArrayDispatch.h"
#include "vtkDataArray.h"
#include "vtkDataArrayRange.h"
#include "vtkFloatArray.h"
#include "vtkIdList.h"
#include "vtkObjectFactory.h"
//------------------------------------------------------------------------------
VTK_ABI_NAMESPACE_BEGIN
vtkPoints* vtkPoints::New(int dataType)
{
// First try to create the object from the vtkObjectFactory
vtkObject* ret = vtkObjectFactory::CreateInstance("vtkPoints");
if (ret)
{
if (dataType != VTK_FLOAT)
{
static_cast<vtkPoints*>(ret)->SetDataType(dataType);
}
return static_cast<vtkPoints*>(ret);
}
// If the factory was unable to create the object, then create it here.
vtkPoints* result = new vtkPoints(dataType);
result->InitializeObjectBase();
return result;
}
//------------------------------------------------------------------------------
vtkPoints* vtkPoints::New()
{
return vtkPoints::New(VTK_FLOAT);
}
//------------------------------------------------------------------------------
// Construct object with an initial data array of type float.
vtkPoints::vtkPoints(int dataType)
{
this->Data = vtkFloatArray::New();
this->Data->Register(this);
this->Data->Delete();
this->SetDataType(dataType);
this->Data->SetNumberOfComponents(3);
this->Data->SetName("Points");
this->Bounds[0] = this->Bounds[2] = this->Bounds[4] = VTK_DOUBLE_MAX;
this->Bounds[1] = this->Bounds[3] = this->Bounds[5] = -VTK_DOUBLE_MAX;
}
//------------------------------------------------------------------------------
vtkPoints::~vtkPoints()
{
this->Data->UnRegister(this);
}
//-----------------GetTuples (id list)------------------------------------------
// Copy from vtkDataArray with the only difference that we know the tuple size
struct GetTuplesFromListWorker
{
vtkIdList* Ids;
GetTuplesFromListWorker(vtkIdList* ids)
: Ids(ids)
{
}
template <typename Array1T, typename Array2T>
void operator()(Array1T* src, Array2T* dst) const
{
const auto srcTuples = vtk::DataArrayTupleRange<3>(src);
auto dstTuples = vtk::DataArrayTupleRange<3>(dst);
vtkIdType* srcTupleId = this->Ids->GetPointer(0);
vtkIdType* srcTupleIdEnd = this->Ids->GetPointer(this->Ids->GetNumberOfIds());
auto dstTupleIter = dstTuples.begin();
while (srcTupleId != srcTupleIdEnd)
{
*dstTupleIter++ = srcTuples[*srcTupleId++];
}
}
};
//------------------------------------------------------------------------------
// Given a list of pt ids, return an array of points.
void vtkPoints::GetPoints(vtkIdList* ptIds, vtkPoints* outPoints)
{
outPoints->Data->SetNumberOfTuples(ptIds->GetNumberOfIds());
// We will NOT use this->Data->GetTuples() for 4 reasons:
// 1) It does a check if the outPoints->Data array is a vtkDataArray, which we now it is.
// 2) It does a check if the number of components is the same, which we know it's 3 for both.
// 3) It has a really expensive Dispatch2::Execute, and every time it tries many array types.
// Points are 99% of the times floats or doubles, so we can avoid A LOT of failed FastDownCast
// operations, by utilizing this knowledge.
// 4) The Worker isn't aware of the number of components of the tuple which slows down the access.
using Dispatcher =
vtkArrayDispatch::Dispatch2ByValueType<vtkArrayDispatch::Reals, vtkArrayDispatch::Reals>;
GetTuplesFromListWorker worker(ptIds);
if (!Dispatcher::Execute(this->Data, outPoints->Data, worker))
{
// Use fallback if dispatch fails.
worker(this->Data, outPoints->Data);
}
}
//------------------------------------------------------------------------------
// Determine (xmin,xmax, ymin,ymax, zmin,zmax) bounds of points.
void vtkPoints::ComputeBounds()
{
if (this->GetMTime() > this->ComputeTime)
{
this->Data->ComputeScalarRange(this->Bounds);
this->ComputeTime.Modified();
}
}
//------------------------------------------------------------------------------
// Return the bounds of the points.
double* vtkPoints::GetBounds()
{
this->ComputeBounds();
return this->Bounds;
}
//------------------------------------------------------------------------------
// Return the bounds of the points.
void vtkPoints::GetBounds(double bounds[6])
{
this->ComputeBounds();
memcpy(bounds, this->Bounds, 6 * sizeof(double));
}
//------------------------------------------------------------------------------
vtkMTimeType vtkPoints::GetMTime()
{
vtkMTimeType doTime = this->Superclass::GetMTime();
if (this->Data->GetMTime() > doTime)
{
doTime = this->Data->GetMTime();
}
return doTime;
}
//------------------------------------------------------------------------------
vtkTypeBool vtkPoints::Allocate(vtkIdType sz, vtkIdType ext)
{
int numComp = this->Data->GetNumberOfComponents();
return this->Data->Allocate(sz * numComp, ext * numComp);
}
//------------------------------------------------------------------------------
void vtkPoints::Initialize()
{
this->Data->Initialize();
this->Modified();
}
//------------------------------------------------------------------------------
void vtkPoints::Modified()
{
this->Superclass::Modified();
if (this->Data)
{
this->Data->Modified();
}
}
//------------------------------------------------------------------------------
int vtkPoints::GetDataType() const
{
return this->Data->GetDataType();
}
//------------------------------------------------------------------------------
// Specify the underlying data type of the object.
void vtkPoints::SetDataType(int dataType)
{
if (dataType == this->Data->GetDataType())
{
return;
}
this->Data->Delete();
this->Data = vtkDataArray::CreateDataArray(dataType);
this->Data->SetNumberOfComponents(3);
this->Data->SetName("Points");
this->Modified();
}
//------------------------------------------------------------------------------
// Set the data for this object. The tuple dimension must be consistent with
// the object.
void vtkPoints::SetData(vtkDataArray* data)
{
if (data != this->Data && data != nullptr)
{
if (data->GetNumberOfComponents() != this->Data->GetNumberOfComponents())
{
vtkErrorMacro(<< "Number of components is different...can't set data");
return;
}
this->Data->UnRegister(this);
this->Data = data;
this->Data->Register(this);
if (!this->Data->GetName())
{
this->Data->SetName("Points");
}
this->Modified();
}
}
//------------------------------------------------------------------------------
// Deep copy of data. Checks consistency to make sure this operation
// makes sense.
void vtkPoints::DeepCopy(vtkPoints* da)
{
if (da == nullptr)
{
return;
}
if (da->Data != this->Data && da->Data != nullptr)
{
if (da->Data->GetNumberOfComponents() != this->Data->GetNumberOfComponents())
{
vtkErrorMacro(<< "Number of components is different...can't copy");
return;
}
this->Data->DeepCopy(da->Data);
this->Modified();
}
}
//------------------------------------------------------------------------------
// Shallow copy of data (i.e. via reference counting). Checks
// consistency to make sure this operation makes sense.
void vtkPoints::ShallowCopy(vtkPoints* da)
{
this->SetData(da->GetData());
}
//------------------------------------------------------------------------------
unsigned long vtkPoints::GetActualMemorySize()
{
return this->Data->GetActualMemorySize();
}
//------------------------------------------------------------------------------
void vtkPoints::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "Data: " << this->Data << "\n";
os << indent << "Data Array Name: ";
if (this->Data->GetName())
{
os << this->Data->GetName() << "\n";
}
else
{
os << "(none)\n";
}
os << indent << "Number Of Points: " << this->GetNumberOfPoints() << "\n";
const double* 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";
}
VTK_ABI_NAMESPACE_END
|