VTK  9.1.0
vtkTriangle.h
Go to the documentation of this file.
1/*=========================================================================
2
3 Program: Visualization Toolkit
4 Module: vtkTriangle.h
5
6 Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7 All rights reserved.
8 See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9
10 This software is distributed WITHOUT ANY WARRANTY; without even
11 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12 PURPOSE. See the above copyright notice for more information.
13
14=========================================================================*/
132#ifndef vtkTriangle_h
133#define vtkTriangle_h
134
135#include "vtkCell.h"
136#include "vtkCommonDataModelModule.h" // For export macro
137
138#include "vtkMath.h" // Needed for inline methods
139
140class vtkLine;
141class vtkQuadric;
143
144class VTKCOMMONDATAMODEL_EXPORT vtkTriangle : public vtkCell
145{
146public:
147 static vtkTriangle* New();
148 vtkTypeMacro(vtkTriangle, vtkCell);
149 void PrintSelf(ostream& os, vtkIndent indent) override;
150
155 vtkCell* GetEdge(int edgeId) override;
156
158
161 int GetCellType() override { return VTK_TRIANGLE; }
162 int GetCellDimension() override { return 2; }
163 int GetNumberOfEdges() override { return 3; }
164 int GetNumberOfFaces() override { return 0; }
165 vtkCell* GetFace(int) override { return nullptr; }
166 int CellBoundary(int subId, const double pcoords[3], vtkIdList* pts) override;
167 void Contour(double value, vtkDataArray* cellScalars, vtkIncrementalPointLocator* locator,
168 vtkCellArray* verts, vtkCellArray* lines, vtkCellArray* polys, vtkPointData* inPd,
169 vtkPointData* outPd, vtkCellData* inCd, vtkIdType cellId, vtkCellData* outCd) override;
170 int EvaluatePosition(const double x[3], double closestPoint[3], int& subId, double pcoords[3],
171 double& dist2, double weights[]) override;
172 void EvaluateLocation(int& subId, const double pcoords[3], double x[3], double* weights) override;
173 int Triangulate(int index, vtkIdList* ptIds, vtkPoints* pts) override;
175 int subId, const double pcoords[3], const double* values, int dim, double* derivs) override;
176 double* GetParametricCoords() override;
178
182 double ComputeArea();
183
188 void Clip(double value, vtkDataArray* cellScalars, vtkIncrementalPointLocator* locator,
189 vtkCellArray* polys, vtkPointData* inPd, vtkPointData* outPd, vtkCellData* inCd,
190 vtkIdType cellId, vtkCellData* outCd, int insideOut) override;
191
192 static void InterpolationFunctions(const double pcoords[3], double sf[3]);
193 static void InterpolationDerivs(const double pcoords[3], double derivs[6]);
195
199 void InterpolateFunctions(const double pcoords[3], double sf[3]) override
200 {
202 }
203 void InterpolateDerivs(const double pcoords[3], double derivs[6]) override
204 {
205 vtkTriangle::InterpolationDerivs(pcoords, derivs);
206 }
208
217
224 int IntersectWithLine(const double p1[3], const double p2[3], double tol, double& t, double x[3],
225 double pcoords[3], int& subId) override;
226
230 int GetParametricCenter(double pcoords[3]) override;
231
236 double GetParametricDistance(const double pcoords[3]) override;
237
241 static void TriangleCenter(
242 const double p1[3], const double p2[3], const double p3[3], double center[3]);
243
248 static double TriangleArea(const double p1[3], const double p2[3], const double p3[3]);
249
256 static double Circumcircle(
257 const double p1[2], const double p2[2], const double p3[2], double center[2]);
258
271 static int BarycentricCoords(const double x[2], const double x1[2], const double x2[2],
272 const double x3[2], double bcoords[3]);
273
279 static int ProjectTo2D(const double x1[3], const double x2[3], const double x3[3], double v1[2],
280 double v2[2], double v3[2]);
281
286 static void ComputeNormal(vtkPoints* p, int numPts, const vtkIdType* pts, double n[3]);
287
291 static void ComputeNormal(
292 const double v1[3], const double v2[3], const double v3[3], double n[3]);
293
297 static void ComputeNormalDirection(
298 const double v1[3], const double v2[3], const double v3[3], double n[3]);
299
300 // Description:
301 // Determine whether or not triangle (p1,q1,r1) intersects triangle
302 // (p2,q2,r2). This method is adapted from Olivier Devillers, Philippe Guigue.
303 // Faster Triangle-Triangle Intersection Tests. RR-4488, IN-RIA. 2002.
304 // <inria-00072100>.
305 static int TrianglesIntersect(const double p1[3], const double q1[3], const double r1[3],
306 const double p2[3], const double q2[3], const double r2[3]);
307
308 // Description:
309 // Given a point x, determine whether it is inside (within the
310 // tolerance squared, tol2) the triangle defined by the three
311 // coordinate values p1, p2, p3. Method is via comparing dot products.
312 // (Note: in current implementation the tolerance only works in the
313 // neighborhood of the three vertices of the triangle.
314 static int PointInTriangle(const double x[3], const double x1[3], const double x2[3],
315 const double x3[3], const double tol2);
316
318
324 static void ComputeQuadric(
325 const double x1[3], const double x2[3], const double x3[3], double quadric[4][4]);
326 static void ComputeQuadric(
327 const double x1[3], const double x2[3], const double x3[3], vtkQuadric* quadric);
329
334 static bool ComputeCentroid(vtkPoints* points, const vtkIdType* pointIds, double centroid[3]);
335
336protected:
338 ~vtkTriangle() override;
339
341
342private:
343 vtkTriangle(const vtkTriangle&) = delete;
344 void operator=(const vtkTriangle&) = delete;
345};
346
347//----------------------------------------------------------------------------
348inline int vtkTriangle::GetParametricCenter(double pcoords[3])
349{
350 pcoords[0] = pcoords[1] = 1. / 3;
351 pcoords[2] = 0.0;
352 return 0;
353}
354
355//----------------------------------------------------------------------------
357 const double v1[3], const double v2[3], const double v3[3], double n[3])
358{
359 double ax, ay, az, bx, by, bz;
360
361 // order is important!!! maintain consistency with triangle vertex order
362 ax = v3[0] - v2[0];
363 ay = v3[1] - v2[1];
364 az = v3[2] - v2[2];
365 bx = v1[0] - v2[0];
366 by = v1[1] - v2[1];
367 bz = v1[2] - v2[2];
368
369 n[0] = (ay * bz - az * by);
370 n[1] = (az * bx - ax * bz);
371 n[2] = (ax * by - ay * bx);
372}
373
374//----------------------------------------------------------------------------
376 const double v1[3], const double v2[3], const double v3[3], double n[3])
377{
378 double length;
379
381
382 if ((length = sqrt((n[0] * n[0] + n[1] * n[1] + n[2] * n[2]))) != 0.0)
383 {
384 n[0] /= length;
385 n[1] /= length;
386 n[2] /= length;
387 }
388}
389
390//----------------------------------------------------------------------------
392 const double p1[3], const double p2[3], const double p3[3], double center[3])
393{
394 center[0] = (p1[0] + p2[0] + p3[0]) / 3.0;
395 center[1] = (p1[1] + p2[1] + p3[1]) / 3.0;
396 center[2] = (p1[2] + p2[2] + p3[2]) / 3.0;
397}
398
399//----------------------------------------------------------------------------
400inline double vtkTriangle::TriangleArea(const double p1[3], const double p2[3], const double p3[3])
401{
402 double n[3];
404
405 return 0.5 * vtkMath::Norm(n);
406}
407
408#endif
object to represent cell connectivity
Definition: vtkCellArray.h:290
represent and manipulate cell attribute data
Definition: vtkCellData.h:142
abstract class to specify cell behavior
Definition: vtkCell.h:147
virtual int GetParametricCenter(double pcoords[3])
Return center of the cell in parametric coordinates.
abstract superclass for arrays of numeric data
Definition: vtkDataArray.h:159
list of point or cell ids
Definition: vtkIdList.h:140
Abstract class in support of both point location and point insertion.
a simple class to control print indentation
Definition: vtkIndent.h:113
cell represents a 1D line
Definition: vtkLine.h:140
static float Norm(const float *x, int n)
Compute the norm of n-vector.
represent and manipulate point attribute data
Definition: vtkPointData.h:142
represent and manipulate 3D points
Definition: vtkPoints.h:143
evaluate implicit quadric function
Definition: vtkQuadric.h:120
a cell that represents a triangle
Definition: vtkTriangle.h:145
static int PointInTriangle(const double x[3], const double x1[3], const double x2[3], const double x3[3], const double tol2)
static void ComputeNormalDirection(const double v1[3], const double v2[3], const double v3[3], double n[3])
Compute the (unnormalized) triangle normal direction from three points.
Definition: vtkTriangle.h:356
void EvaluateLocation(int &subId, const double pcoords[3], double x[3], double *weights) override
See the vtkCell API for descriptions of these methods.
static int TrianglesIntersect(const double p1[3], const double q1[3], const double r1[3], const double p2[3], const double q2[3], const double r2[3])
static void ComputeQuadric(const double x1[3], const double x2[3], const double x3[3], vtkQuadric *quadric)
Calculate the error quadric for this triangle.
static void ComputeNormal(vtkPoints *p, int numPts, const vtkIdType *pts, double n[3])
Compute the triangle normal from a points list, and a list of point ids that index into the points li...
static vtkTriangle * New()
int EvaluatePosition(const double x[3], double closestPoint[3], int &subId, double pcoords[3], double &dist2, double weights[]) override
See the vtkCell API for descriptions of these methods.
int GetParametricCenter(double pcoords[3]) override
Return the center of the triangle in parametric coordinates.
Definition: vtkTriangle.h:348
void Contour(double value, vtkDataArray *cellScalars, vtkIncrementalPointLocator *locator, vtkCellArray *verts, vtkCellArray *lines, vtkCellArray *polys, vtkPointData *inPd, vtkPointData *outPd, vtkCellData *inCd, vtkIdType cellId, vtkCellData *outCd) override
See the vtkCell API for descriptions of these methods.
int GetNumberOfFaces() override
See the vtkCell API for descriptions of these methods.
Definition: vtkTriangle.h:164
void Clip(double value, vtkDataArray *cellScalars, vtkIncrementalPointLocator *locator, vtkCellArray *polys, vtkPointData *inPd, vtkPointData *outPd, vtkCellData *inCd, vtkIdType cellId, vtkCellData *outCd, int insideOut) override
Clip this triangle using scalar value provided.
int CellBoundary(int subId, const double pcoords[3], vtkIdList *pts) override
See the vtkCell API for descriptions of these methods.
vtkLine * Line
Definition: vtkTriangle.h:340
void InterpolateFunctions(const double pcoords[3], double sf[3]) override
Compute the interpolation functions/derivatives (aka shape functions/derivatives)
Definition: vtkTriangle.h:199
static double TriangleArea(const double p1[3], const double p2[3], const double p3[3])
Compute the area of a triangle in 3D.
Definition: vtkTriangle.h:400
static int ProjectTo2D(const double x1[3], const double x2[3], const double x3[3], double v1[2], double v2[2], double v3[2])
Project triangle defined in 3D to 2D coordinates.
static bool ComputeCentroid(vtkPoints *points, const vtkIdType *pointIds, double centroid[3])
Get the centroid of the triangle.
double GetParametricDistance(const double pcoords[3]) override
Return the distance of the parametric coordinate provided to the cell.
~vtkTriangle() override
int IntersectWithLine(const double p1[3], const double p2[3], double tol, double &t, double x[3], double pcoords[3], int &subId) override
Given a line defined by two points p1 and p2, determine whether it intersects the triangle.
int Triangulate(int index, vtkIdList *ptIds, vtkPoints *pts) override
See the vtkCell API for descriptions of these methods.
void InterpolateDerivs(const double pcoords[3], double derivs[6]) override
Compute the interpolation functions/derivatives (aka shape functions/derivatives)
Definition: vtkTriangle.h:203
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
double * GetParametricCoords() override
See the vtkCell API for descriptions of these methods.
static double Circumcircle(const double p1[2], const double p2[2], const double p3[2], double center[2])
Compute the circumcenter (center[3]) and radius squared (method return value) of a triangle defined b...
static void InterpolationDerivs(const double pcoords[3], double derivs[6])
static void ComputeQuadric(const double x1[3], const double x2[3], const double x3[3], double quadric[4][4])
Calculate the error quadric for this triangle.
int GetCellDimension() override
See the vtkCell API for descriptions of these methods.
Definition: vtkTriangle.h:162
double ComputeArea()
A convenience function to compute the area of a vtkTriangle.
static void TriangleCenter(const double p1[3], const double p2[3], const double p3[3], double center[3])
Compute the center of the triangle.
Definition: vtkTriangle.h:391
const vtkIdType * GetEdgeArray(vtkIdType edgeId)
Return the ids of the vertices defining edge (edgeId).
static int BarycentricCoords(const double x[2], const double x1[2], const double x2[2], const double x3[2], double bcoords[3])
Given a 2D point x[2], determine the barycentric coordinates of the point.
int GetNumberOfEdges() override
See the vtkCell API for descriptions of these methods.
Definition: vtkTriangle.h:163
void Derivatives(int subId, const double pcoords[3], const double *values, int dim, double *derivs) override
See the vtkCell API for descriptions of these methods.
vtkCell * GetFace(int) override
See the vtkCell API for descriptions of these methods.
Definition: vtkTriangle.h:165
vtkCell * GetEdge(int edgeId) override
Get the edge specified by edgeId (range 0 to 2) and return that edge's coordinates.
static void InterpolationFunctions(const double pcoords[3], double sf[3])
int GetCellType() override
See the vtkCell API for descriptions of these methods.
Definition: vtkTriangle.h:161
@ points
Definition: vtkX3D.h:452
@ length
Definition: vtkX3D.h:399
@ value
Definition: vtkX3D.h:226
@ center
Definition: vtkX3D.h:236
@ index
Definition: vtkX3D.h:252
@ VTK_TRIANGLE
Definition: vtkCellType.h:90
int vtkIdType
Definition: vtkType.h:332