VTK
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 =========================================================================*/
28 #ifndef __vtkTriangle_h
29 #define __vtkTriangle_h
30 
31 #include "vtkCell.h"
32 
33 #include "vtkMath.h" // Needed for inline methods
34 
35 class vtkLine;
36 class vtkQuadric;
38 
40 {
41 public:
42  static vtkTriangle *New();
43  vtkTypeMacro(vtkTriangle,vtkCell);
44  void PrintSelf(ostream& os, vtkIndent indent);
45 
48  vtkCell *GetEdge(int edgeId);
49 
51 
52  int GetCellType() {return VTK_TRIANGLE;};
53  int GetCellDimension() {return 2;};
54  int GetNumberOfEdges() {return 3;};
55  int GetNumberOfFaces() {return 0;};
56  vtkCell *GetFace(int) {return 0;};
57  int CellBoundary(int subId, double pcoords[3], vtkIdList *pts);
58  void Contour(double value, vtkDataArray *cellScalars,
60  vtkCellArray *lines, vtkCellArray *polys,
61  vtkPointData *inPd, vtkPointData *outPd,
62  vtkCellData *inCd, vtkIdType cellId, vtkCellData *outCd);
63  int EvaluatePosition(double x[3], double* closestPoint,
64  int& subId, double pcoords[3],
65  double& dist2, double *weights);
66  void EvaluateLocation(int& subId, double pcoords[3], double x[3],
67  double *weights);
68  int Triangulate(int index, vtkIdList *ptIds, vtkPoints *pts);
69  void Derivatives(int subId, double pcoords[3], double *values,
70  int dim, double *derivs);
71  virtual double *GetParametricCoords();
73 
75  double ComputeArea();
76 
78 
80  void Clip(double value, vtkDataArray *cellScalars,
82  vtkPointData *inPd, vtkPointData *outPd,
83  vtkCellData *inCd, vtkIdType cellId, vtkCellData *outCd,
84  int insideOut);
86 
89  static void InterpolationFunctions(double pcoords[3], double sf[3]);
91  static void InterpolationDerivs(double pcoords[3], double derivs[6]);
93 
95  virtual void InterpolateFunctions(double pcoords[3], double sf[3])
96  {
98  }
99  virtual void InterpolateDerivs(double pcoords[3], double derivs[6])
100  {
101  vtkTriangle::InterpolationDerivs(pcoords,derivs);
102  }
104 
106  int *GetEdgeArray(int edgeId);
107 
109 
111  int IntersectWithLine(double p1[3], double p2[3], double tol, double& t,
112  double x[3], double pcoords[3], int& subId);
114 
116  int GetParametricCenter(double pcoords[3]);
117 
120  double GetParametricDistance(double pcoords[3]);
121 
123 
124  static void TriangleCenter(double p1[3], double p2[3], double p3[3],
125  double center[3]);
127 
130  static double TriangleArea(double p1[3], double p2[3], double p3[3]);
131 
133 
137  static double Circumcircle(double p1[2], double p2[2], double p3[2],
138  double center[2]);
140 
142 
153  static int BarycentricCoords(double x[2], double x1[2], double x2[2],
154  double x3[2], double bcoords[3]);
156 
157 
159 
162  static int ProjectTo2D(double x1[3], double x2[3], double x3[3],
163  double v1[2], double v2[2], double v3[2]);
165 
167 
169  static void ComputeNormal(vtkPoints *p, int numPts, vtkIdType *pts,
170  double n[3]);
172 
174  static void ComputeNormal(double v1[3], double v2[3], double v3[3], double n[3]);
175 
177 
179  static void ComputeNormalDirection(double v1[3], double v2[3], double v3[3],
180  double n[3]);
182 
184 
189  static int PointInTriangle(double x[3], double x1[3],
190  double x2[3], double x3[3],
191  double tol2);
193 
195 
198  static void ComputeQuadric(double x1[3], double x2[3], double x3[3],
199  double quadric[4][4]);
200  static void ComputeQuadric(double x1[3], double x2[3], double x3[3],
201  vtkQuadric *quadric);
203 
204 
205 protected:
206  vtkTriangle();
207  ~vtkTriangle();
208 
210 
211 private:
212  vtkTriangle(const vtkTriangle&); // Not implemented.
213  void operator=(const vtkTriangle&); // Not implemented.
214 };
215 
216 //----------------------------------------------------------------------------
217 inline int vtkTriangle::GetParametricCenter(double pcoords[3])
218 {
219  pcoords[0] = pcoords[1] = 1./3; pcoords[2] = 0.0;
220  return 0;
221 }
222 
223 //----------------------------------------------------------------------------
224 inline void vtkTriangle::ComputeNormalDirection(double v1[3], double v2[3],
225  double v3[3], double n[3])
226 {
227  double ax, ay, az, bx, by, bz;
228 
229  // order is important!!! maintain consistency with triangle vertex order
230  ax = v3[0] - v2[0]; ay = v3[1] - v2[1]; az = v3[2] - v2[2];
231  bx = v1[0] - v2[0]; by = v1[1] - v2[1]; bz = v1[2] - v2[2];
232 
233  n[0] = (ay * bz - az * by);
234  n[1] = (az * bx - ax * bz);
235  n[2] = (ax * by - ay * bx);
236 }
237 
238 //----------------------------------------------------------------------------
239 inline void vtkTriangle::ComputeNormal(double v1[3], double v2[3],
240  double v3[3], double n[3])
241 {
242  double length;
243 
245 
246  if ( (length = sqrt((n[0]*n[0] + n[1]*n[1] + n[2]*n[2]))) != 0.0 )
247  {
248  n[0] /= length;
249  n[1] /= length;
250  n[2] /= length;
251  }
252 }
253 
254 //----------------------------------------------------------------------------
255 inline void vtkTriangle::TriangleCenter(double p1[3], double p2[3],
256  double p3[3], double center[3])
257 {
258  center[0] = (p1[0]+p2[0]+p3[0]) / 3.0;
259  center[1] = (p1[1]+p2[1]+p3[1]) / 3.0;
260  center[2] = (p1[2]+p2[2]+p3[2]) / 3.0;
261 }
262 
263 //----------------------------------------------------------------------------
264 inline double vtkTriangle::TriangleArea(double p1[3], double p2[3], double p3[3])
265 {
266  double a,b,c;
270  return (0.25* sqrt(fabs(4.0*a*c - (a-b+c)*(a-b+c))));
271 }
272 
273 #endif
274 
275