VTK  9.3.0
vtkTriangle.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2 // SPDX-License-Identifier: BSD-3-Clause
14 #ifndef vtkTriangle_h
15 #define vtkTriangle_h
16 
17 #include "vtkCell.h"
18 #include "vtkCommonDataModelModule.h" // For export macro
19 
20 #include "vtkMath.h" // Needed for inline methods
21 
22 VTK_ABI_NAMESPACE_BEGIN
23 class vtkLine;
24 class vtkQuadric;
26 
27 class VTKCOMMONDATAMODEL_EXPORT vtkTriangle : public vtkCell
28 {
29 public:
30  static vtkTriangle* New();
31  vtkTypeMacro(vtkTriangle, vtkCell);
32  void PrintSelf(ostream& os, vtkIndent indent) override;
33 
38  vtkCell* GetEdge(int edgeId) override;
39 
41 
44  int GetCellType() override { return VTK_TRIANGLE; }
45  int GetCellDimension() override { return 2; }
46  int GetNumberOfEdges() override { return 3; }
47  int GetNumberOfFaces() override { return 0; }
48  vtkCell* GetFace(int) override { return nullptr; }
49  int CellBoundary(int subId, const double pcoords[3], vtkIdList* pts) override;
50  void Contour(double value, vtkDataArray* cellScalars, vtkIncrementalPointLocator* locator,
51  vtkCellArray* verts, vtkCellArray* lines, vtkCellArray* polys, vtkPointData* inPd,
52  vtkPointData* outPd, vtkCellData* inCd, vtkIdType cellId, vtkCellData* outCd) override;
53  int EvaluatePosition(const double x[3], double closestPoint[3], int& subId, double pcoords[3],
54  double& dist2, double weights[]) override;
55  void EvaluateLocation(int& subId, const double pcoords[3], double x[3], double* weights) override;
56  int Triangulate(int index, vtkIdList* ptIds, vtkPoints* pts) override;
58  int subId, const double pcoords[3], const double* values, int dim, double* derivs) override;
59  double* GetParametricCoords() override;
61 
65  double ComputeArea();
66 
71  void Clip(double value, vtkDataArray* cellScalars, vtkIncrementalPointLocator* locator,
72  vtkCellArray* polys, vtkPointData* inPd, vtkPointData* outPd, vtkCellData* inCd,
73  vtkIdType cellId, vtkCellData* outCd, int insideOut) override;
74 
75  static void InterpolationFunctions(const double pcoords[3], double sf[3]);
76  static void InterpolationDerivs(const double pcoords[3], double derivs[6]);
78 
82  void InterpolateFunctions(const double pcoords[3], double sf[3]) override
83  {
85  }
86  void InterpolateDerivs(const double pcoords[3], double derivs[6]) override
87  {
88  vtkTriangle::InterpolationDerivs(pcoords, derivs);
89  }
91 
100 
107  int IntersectWithLine(const double p1[3], const double p2[3], double tol, double& t, double x[3],
108  double pcoords[3], int& subId) override;
109 
113  int GetParametricCenter(double pcoords[3]) override;
114 
119  double GetParametricDistance(const double pcoords[3]) override;
120 
124  static void TriangleCenter(
125  const double p1[3], const double p2[3], const double p3[3], double center[3]);
126 
131  static double TriangleArea(const double p1[3], const double p2[3], const double p3[3]);
132 
139  static double Circumcircle(
140  const double p1[2], const double p2[2], const double p3[2], double center[2]);
141 
154  static int BarycentricCoords(const double x[2], const double x1[2], const double x2[2],
155  const double x3[2], double bcoords[3]);
156 
162  static int ProjectTo2D(const double x1[3], const double x2[3], const double x3[3], double v1[2],
163  double v2[2], double v3[2]);
164 
169  static void ComputeNormal(vtkPoints* p, int numPts, const vtkIdType* pts, double n[3]);
170 
174  static void ComputeNormal(
175  const double v1[3], const double v2[3], const double v3[3], double n[3]);
176 
180  static void ComputeNormalDirection(
181  const double v1[3], const double v2[3], const double v3[3], double n[3]);
182 
183  // Description:
184  // Determine whether or not triangle (p1,q1,r1) intersects triangle
185  // (p2,q2,r2). This method is adapted from Olivier Devillers, Philippe Guigue.
186  // Faster Triangle-Triangle Intersection Tests. RR-4488, IN-RIA. 2002.
187  // <inria-00072100>.
188  static int TrianglesIntersect(const double p1[3], const double q1[3], const double r1[3],
189  const double p2[3], const double q2[3], const double r2[3]);
190 
191  // Description:
192  // Given a point x, determine whether it is inside (within the
193  // tolerance squared, tol2) the triangle defined by the three
194  // coordinate values p1, p2, p3. Method is via comparing dot products.
195  // (Note: in current implementation the tolerance only works in the
196  // neighborhood of the three vertices of the triangle.
197  static int PointInTriangle(
198  const double x[3], const double x1[3], const double x2[3], const double x3[3], double tol2);
199 
201 
207  static void ComputeQuadric(
208  const double x1[3], const double x2[3], const double x3[3], double quadric[4][4]);
209  static void ComputeQuadric(
210  const double x1[3], const double x2[3], const double x3[3], vtkQuadric* quadric);
212 
217  static bool ComputeCentroid(vtkPoints* points, const vtkIdType* pointIds, double centroid[3]);
218 
219 protected:
221  ~vtkTriangle() override;
222 
224 
225 private:
226  vtkTriangle(const vtkTriangle&) = delete;
227  void operator=(const vtkTriangle&) = delete;
228 };
229 
230 //----------------------------------------------------------------------------
231 inline int vtkTriangle::GetParametricCenter(double pcoords[3])
232 {
233  pcoords[0] = pcoords[1] = 1.0 / 3.0;
234  pcoords[2] = 0.0;
235  return 0;
236 }
237 
238 //----------------------------------------------------------------------------
240  const double v1[3], const double v2[3], const double v3[3], double n[3])
241 {
242  // order is important!!! maintain consistency with triangle vertex order
243  double ax = v3[0] - v2[0];
244  double ay = v3[1] - v2[1];
245  double az = v3[2] - v2[2];
246  double bx = v1[0] - v2[0];
247  double by = v1[1] - v2[1];
248  double bz = v1[2] - v2[2];
249 
250  n[0] = (ay * bz - az * by);
251  n[1] = (az * bx - ax * bz);
252  n[2] = (ax * by - ay * bx);
253 }
254 
255 //----------------------------------------------------------------------------
257  const double v1[3], const double v2[3], const double v3[3], double n[3])
258 {
260 
261  double length = sqrt(n[0] * n[0] + n[1] * n[1] + n[2] * n[2]);
262  if (length != 0.0)
263  {
264  n[0] /= length;
265  n[1] /= length;
266  n[2] /= length;
267  }
268 }
269 
270 //----------------------------------------------------------------------------
272  const double p1[3], const double p2[3], const double p3[3], double center[3])
273 {
274  center[0] = (p1[0] + p2[0] + p3[0]) / 3.0;
275  center[1] = (p1[1] + p2[1] + p3[1]) / 3.0;
276  center[2] = (p1[2] + p2[2] + p3[2]) / 3.0;
277 }
278 
279 //----------------------------------------------------------------------------
280 inline double vtkTriangle::TriangleArea(const double p1[3], const double p2[3], const double p3[3])
281 {
282  double n[3];
284 
285  return 0.5 * vtkMath::Norm(n);
286 }
287 
288 VTK_ABI_NAMESPACE_END
289 #endif
object to represent cell connectivity
Definition: vtkCellArray.h:176
represent and manipulate cell attribute data
Definition: vtkCellData.h:31
abstract class to specify cell behavior
Definition: vtkCell.h:50
virtual int GetParametricCenter(double pcoords[3])
Return center of the cell in parametric coordinates.
abstract superclass for arrays of numeric data
Definition: vtkDataArray.h:45
list of point or cell ids
Definition: vtkIdList.h:23
Abstract class in support of both point location and point insertion.
a simple class to control print indentation
Definition: vtkIndent.h:29
cell represents a 1D line
Definition: vtkLine.h:23
static float Norm(const float *x, int n)
Compute the norm of n-vector.
represent and manipulate point attribute data
Definition: vtkPointData.h:30
represent and manipulate 3D points
Definition: vtkPoints.h:29
evaluate implicit quadric function
Definition: vtkQuadric.h:23
a cell that represents a triangle
Definition: vtkTriangle.h:28
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:239
vtkCell * GetFace(int) override
See the vtkCell API for descriptions of these methods.
Definition: vtkTriangle.h:48
void EvaluateLocation(int &subId, const double pcoords[3], double x[3], double *weights) override
See the vtkCell API for descriptions of these methods.
const vtkIdType * GetEdgeArray(vtkIdType edgeId)
Return the ids of the vertices defining edge (edgeId).
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...
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:231
vtkCell * GetEdge(int edgeId) override
Get the edge specified by edgeId (range 0 to 2) and return that edge's coordinates.
static int PointInTriangle(const double x[3], const double x1[3], const double x2[3], const double x3[3], double tol2)
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:47
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:223
void InterpolateFunctions(const double pcoords[3], double sf[3]) override
Compute the interpolation functions/derivatives (aka shape functions/derivatives)
Definition: vtkTriangle.h:82
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:280
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.
static vtkTriangle * New()
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:86
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
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:45
double * GetParametricCoords() override
See the vtkCell API for descriptions of these methods.
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:271
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:46
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.
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:44
@ points
Definition: vtkX3D.h:446
@ length
Definition: vtkX3D.h:393
@ value
Definition: vtkX3D.h:220
@ center
Definition: vtkX3D.h:230
@ index
Definition: vtkX3D.h:246
@ VTK_TRIANGLE
Definition: vtkCellType.h:42
int vtkIdType
Definition: vtkType.h:315