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 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkEllipticalButtonSource.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 "vtkEllipticalButtonSource.h"
#include "vtkCellArray.h"
#include "vtkFloatArray.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkMath.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkPoints.h"
#include "vtkPolyData.h"
#include "vtkTransform.h"
vtkStandardNewMacro(vtkEllipticalButtonSource);
//----------------------------------------------------------------------------
// Construct
vtkEllipticalButtonSource::vtkEllipticalButtonSource()
{
this->Width = 0.5;
this->Height = 0.5;
this->Depth = 0.05;
this->CircumferentialResolution = 4;
this->TextureResolution = 2;
this->ShoulderResolution = 2;
this->OutputPointsPrecision = SINGLE_PRECISION;
this->RadialRatio = 1.1;
}
//----------------------------------------------------------------------------
int vtkEllipticalButtonSource::RequestData(
vtkInformation *vtkNotUsed(request),
vtkInformationVector **vtkNotUsed(inputVector),
vtkInformationVector *outputVector)
{
vtkInformation *outInfo = outputVector->GetInformationObject(0);
int i, j;
vtkPolyData *output = vtkPolyData::SafeDownCast(
outInfo->Get(vtkDataObject::DATA_OBJECT()));
vtkDebugMacro(<<"Generating elliptical button");
// Check input
if ( this->Width <= 0.0 || this->Height <= 0.0 )
{
vtkErrorMacro(<<"Button must have non-zero height and width");
return 1;
}
// Create the button in several steps. First, create the button in
// the x-y plane, this requires creating the texture region and then
// the shoulder region. After this, the z-depth is created. And if
// it is a two-sided button, then a mirror reflection of the button
// in the negative z-direction is created.
int numPts = 1 + this->CircumferentialResolution *
(this->TextureResolution + this->ShoulderResolution + 1);
if ( this->TwoSided ) { numPts *= 2; }
vtkPoints *newPts = vtkPoints::New();
// Set the desired precision for the points in the output.
if(this->OutputPointsPrecision == vtkAlgorithm::DOUBLE_PRECISION)
{
newPts->SetDataType(VTK_DOUBLE);
}
else
{
newPts->SetDataType(VTK_FLOAT);
}
newPts->SetNumberOfPoints(numPts);
vtkFloatArray *normals = vtkFloatArray::New();
normals->SetNumberOfComponents(3);
normals->SetNumberOfTuples(numPts);
vtkFloatArray *tcoords = vtkFloatArray::New();
tcoords->SetNumberOfComponents(2);
tcoords->SetNumberOfTuples(numPts);
vtkCellArray *newPolys = vtkCellArray::New();
newPolys->Allocate(this->CircumferentialResolution*
(this->TextureResolution*this->ShoulderResolution));
// Create the texture region. --------------------------------------------
// Start by determining the resolution in the width and height directions.
// Setup the ellipsoid.
double x[3], x0[3], x1[3], x2[3], x3[3], n[3];
this->A = this->Width / 2.0;
this->A2 = this->A * this->A;
this->B = this->Height / 2.0;
this->B2 = this->B*this->B;
this->C = this->Depth;
this->C2 = this->C*this->C;
double xP[3], dX, dY;
if ( this->TextureStyle == VTK_TEXTURE_STYLE_FIT_IMAGE )
{
dX = static_cast<double>(this->TextureDimensions[0]);
dY = static_cast<double>(this->TextureDimensions[1]);
}
else
{
dX = this->A;
dY = this->B;
}
int hRes = static_cast<int>(
ceil(this->CircumferentialResolution * (dY/(dY+dX)))) / 2;
hRes = (hRes <= 0 ? 1 : hRes);
int wRes = (this->CircumferentialResolution - 2*hRes) / 2;
// Create the center point
newPts->SetPoint(0,this->Center[0],this->Center[1],
this->Center[2]+this->Depth);
normals->SetTuple3(0, 0.0,0.0,1.0);
tcoords->SetTuple2(0, 0.5, 0.5);
// Set up for points interior to the texture
int offset = 1 + (this->TextureResolution-1)*this->CircumferentialResolution;
// Determine the lower-left corner of the texture region
double xe, ye;
double a = this->A/this->RadialRatio;
double b = this->B/this->RadialRatio;
this->IntersectEllipseWithLine(a*a,b*b,dX,dY,xe,ye);
x0[0] = this->Center[0] - xe;
x0[1] = this->Center[1] - ye;
x0[2] = this->ComputeDepth(1,x0[0],x0[1],n);
newPts->SetPoint(offset, x0);
normals->SetTuple(offset, n);
tcoords->SetTuple2(offset, 0.0, 0.0);
// Create the lower right point
x1[0] = this->Center[0] + xe;
x1[1] = this->Center[1] - ye;
x1[2] = this->ComputeDepth(1,x1[0],x1[1],n);
newPts->SetPoint(offset + wRes, x1);
normals->SetTuple(offset + wRes, n);
tcoords->SetTuple2(offset + wRes, 1.0, 0.0);
// Create the upper right point
x2[0] = this->Center[0] + xe;
x2[1] = this->Center[1] + ye;
x2[2] = this->ComputeDepth(1,x2[0],x2[1],n);
newPts->SetPoint(offset + wRes + hRes, x2);
normals->SetTuple(offset + wRes + hRes, n);
tcoords->SetTuple2(offset + wRes + hRes, 1.0, 1.0);
// Create the upper left point
x3[0] = this->Center[0] - xe;
x3[1] = this->Center[1] + ye;
x3[2] = this->ComputeDepth(1,x3[0],x3[1],n);
newPts->SetPoint(offset + 2*wRes + hRes, x3);
normals->SetTuple(offset + 2*wRes + hRes, n);
tcoords->SetTuple2(offset + 2*wRes + hRes, 0.0, 1.0);
// Okay, now fill in the points along the edges
double t;
for (i=1; i < wRes; i++) //x0 -> x1
{
t = static_cast<double>(i)/wRes;
x[0] = x0[0] + t * (x1[0]-x0[0]);
x[1] = x0[1];
x[2] = this->ComputeDepth(1,x[0],x[1],n);
newPts->SetPoint(offset+i,x);
normals->SetTuple(offset+i,n);
tcoords->SetTuple2(offset+i, t,0.0);
}
for (i=1; i < hRes; i++) //x1 -> x2
{
t = static_cast<double>(i)/hRes;
x[0] = x1[0];
x[1] = x1[1] + t * (x2[1]-x1[1]);
x[2] = this->ComputeDepth(1,x[0],x[1],n);
newPts->SetPoint(offset+wRes+i,x);
normals->SetTuple(offset+wRes+i,n);
tcoords->SetTuple2(offset+wRes+i, 1.0,t);
}
for (i=1; i < wRes; i++) //x2 -> x3
{
t = static_cast<double>(i)/wRes;
x[0] = x2[0] + t * (x3[0]-x2[0]);
x[1] = x2[1];
x[2] = this->ComputeDepth(1,x[0],x[1],n);
newPts->SetPoint(offset+wRes+hRes+i,x);
normals->SetTuple(offset+wRes+hRes+i,n);
tcoords->SetTuple2(offset+wRes+hRes+i, (1.0-t),1.0);
}
for (i=1; i < hRes; i++) //x3 -> x0
{
t = static_cast<double>(i)/hRes;
x[0] = x3[0];
x[1] = x3[1] + t * (x0[1]-x3[1]);
x[2] = this->ComputeDepth(1,x[0],x[1],n);
newPts->SetPoint(offset+2*wRes+hRes+i,x);
normals->SetTuple(offset+2*wRes+hRes+i,n);
tcoords->SetTuple2(offset+2*wRes+hRes+i, 0.0,(1.0-t));
}
// Fill in the inside of the texture region
vtkIdType pts[3];
pts[0] = 0;
for (i=0; i<(this->CircumferentialResolution-1); i++)
{
pts[1] = i + 1;
pts[2] = i + 2;
newPolys->InsertNextCell(3,pts);
}
pts[1] = this->CircumferentialResolution;
pts[2] = 1;
newPolys->InsertNextCell(3,pts);
if ( this->TextureResolution >= 1 )
{
this->InterpolateCurve(1, newPts, this->CircumferentialResolution,
normals, tcoords, this->TextureResolution,
0, 0, offset, 1, 1, 1);
this->CreatePolygons(newPolys, this->CircumferentialResolution,
this->TextureResolution-1, 1);
}
// Create the shoulder region. --------------------------------------------
// Start by duplicating points around the texture region. These are
// copied to avoid texture interpolation pollution.
int c1Start = offset + this->CircumferentialResolution;
for ( i=0; i < this->CircumferentialResolution; i++)
{
newPts->SetPoint(c1Start+i, newPts->GetPoint(offset+i));
normals->SetTuple(c1Start+i, normals->GetTuple(offset+i));
tcoords->SetTuple(c1Start+i, this->ShoulderTextureCoordinate);
}
// Now create points around the perimeter of the button. The locations
// of the points (i.e., angles) are taken from the texture region.
int c2Start = offset + (this->ShoulderResolution+1) *
this->CircumferentialResolution;
for ( i=0; i < this->CircumferentialResolution; i++)
{
//compute the angle
newPts->GetPoint(offset+i, xP);
dX = xP[0] - this->Center[0];
dY = xP[1] - this->Center[1];
this->IntersectEllipseWithLine(this->A2,this->B2,dX,dY,xe,ye);
x[0] = this->Center[0] + xe;
x[1] = this->Center[1] + ye;
x[2] = this->ComputeDepth(0,x[0],x[1],n);
newPts->SetPoint(c2Start+i, x);
normals->SetTuple(c2Start+i, n);
tcoords->SetTuple(c2Start+i, this->ShoulderTextureCoordinate);
}
// Interpolate points between the curves. Create polygons.
this->InterpolateCurve(0, newPts, this->CircumferentialResolution,
normals, tcoords, this->ShoulderResolution,
c1Start, 1, c2Start, 1,
c1Start+this->CircumferentialResolution, 1);
this->CreatePolygons(newPolys, this->CircumferentialResolution,
this->ShoulderResolution, c1Start);
// Create the other side of the button if requested.
if ( this->TwoSided > 0.0 )
{
//do the points
numPts /= 2;
for (i=0; i<numPts; i++)
{
newPts->GetPoint(i, x);
x[0] = -(x[0]-this->Center[0]) + this->Center[0];
x[2] = -(x[2]-this->Center[2]) + this->Center[2];
newPts->SetPoint(i+numPts, x);
normals->GetTuple(i, x);
x[0] = -x[0];
x[2] = -x[2];
normals->SetTuple(i+numPts, x);
tcoords->SetTuple(i+numPts, tcoords->GetTuple(i));
}
//do the polygons
vtkIdType *ipts = 0;
vtkIdType opts[4];
vtkIdType npts = 0;
vtkIdType numPolys=newPolys->GetNumberOfCells();
for ( j=0, newPolys->InitTraversal(); j < numPolys; j++ )
{
newPolys->GetNextCell(npts,ipts);
for (i=0; i<npts; i++)
{
opts[i] = ipts[i] + numPts;
}
newPolys->InsertNextCell(npts,opts);
}
}
// Clean up and get out
output->SetPoints(newPts);
output->GetPointData()->SetNormals(normals);
output->GetPointData()->SetTCoords(tcoords);
output->SetPolys(newPolys);
newPts->Delete();
tcoords->Delete();
normals->Delete();
newPolys->Delete();
return 1;
}
//----------------------------------------------------------------------------
void vtkEllipticalButtonSource::InterpolateCurve(int inTextureRegion,
vtkPoints *newPts, int numPts,
vtkFloatArray *normals,
vtkFloatArray *tcoords, int res,
int c1StartPt, int c1Incr,
int c2StartPt, int c2Incr,
int startPt, int incr)
{
int i, j, idx;
double x0[3], x1[3], tc0[3], tc1[3], t, x[3], tc[2], n[3];
//walk around the curves interpolating new points between them
for ( i=0; i < numPts;
i++, c1StartPt+=c1Incr, c2StartPt+=c2Incr, startPt+=incr)
{
newPts->GetPoint(c1StartPt, x0);
newPts->GetPoint(c2StartPt, x1);
tcoords->GetTuple(c1StartPt, tc0);
tcoords->GetTuple(c2StartPt, tc1);
//do the interpolations along this radius
for ( j=1; j < res; j++ )
{
idx = startPt+(j-1)*numPts;
t = static_cast<double>(j) / res;
x[0] = x0[0] + t * (x1[0] - x0[0]);
x[1] = x0[1] + t * (x1[1] - x0[1]);
x[2] = this->ComputeDepth(inTextureRegion,x[0],x[1],n);
newPts->SetPoint(idx, x);
normals->SetTuple(idx, n);
tc[0] = tc0[0] + t * (tc1[0] - tc0[0]);
tc[1] = tc0[1] + t * (tc1[1] - tc0[1]);
tcoords->SetTuple(idx, tc);
}
}//for all points
}
//----------------------------------------------------------------------------
void vtkEllipticalButtonSource::CreatePolygons(vtkCellArray *newPolys,
int num, int res, int startIdx)
{
int i, j;
vtkIdType idx, pts[4];
for (i=0; i < res; i++, startIdx+=num)
{
idx = startIdx;
for (j=0; j < num; j++, idx++)
{
pts[0] = idx;
pts[1] = idx + num;
if ( j == (num-1) )
{
pts[2] = startIdx + num;
pts[3] = startIdx;
}
else
{
pts[2] = idx + num + 1;
pts[3] = idx + 1;
}
newPolys->InsertNextCell(4,pts);
}
}
}
//----------------------------------------------------------------------------
void vtkEllipticalButtonSource::IntersectEllipseWithLine(double a2, double b2, double dX,
double dY, double& xe, double& ye)
{
double m;
if ( fabs(dY) <= fabs(dX) )
{
m = dY/dX;
xe = sqrt( a2*b2/(b2 + m*m*a2) );
if ( dX < 0.0 ) xe = -xe;
ye = m*xe;
}
else
{
m = dX/dY;
ye = sqrt( a2*b2/(m*m*b2 + a2) );
if ( dY < 0.0 ) ye = -ye;
xe = m*ye;
}
}
//----------------------------------------------------------------------------
double vtkEllipticalButtonSource::ComputeDepth(int vtkNotUsed(inTextureRegion),
double x, double y, double n[3])
{
double z;
x -= this->Center[0];
y -= this->Center[1];
z = 1.0 - (x*x)/this->A2 - (y*y)/this->B2;
if ( z < 0.0 ) n[2] = z = 0.0;
else n[2] = z = this->Depth * sqrt(z);
n[0] = 2.0*x / this->A2;
n[1] = 2.0*y / this->B2;
n[2] = 2.0*z / this->C2;
vtkMath::Normalize(n);
return (z + this->Center[2]);
}
//----------------------------------------------------------------------------
void vtkEllipticalButtonSource::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
os << indent << "Width: " << this->Width << "\n";
os << indent << "Height: " << this->Height << "\n";
os << indent << "Depth: " << this->Depth << "\n";
os << indent << "Circumferential Resolution: "
<< this->CircumferentialResolution << "\n";
os << indent << "Texture Resolution: " << this->TextureResolution << "\n";
os << indent << "Shoulder Resolution: " << this->ShoulderResolution << "\n";
os << indent << "Radial Ratio: " << this->RadialRatio << "\n";
os << indent << "Output Points Precision: " << this->OutputPointsPrecision
<< "\n";
}
|