1 /*=========================================================================
3 Program: Visualization Toolkit
4 Module: $RCSfile: vtkPolygon.h,v $
6 Date: $Date: 2002/12/26 18:24:21 $
7 Version: $Revision: 1.74 $
9 Copyright (c) 1993-2002 Ken Martin, Will Schroeder, Bill Lorensen
11 See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
13 This software is distributed WITHOUT ANY WARRANTY; without even
14 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 PURPOSE. See the above copyright notice for more information.
17 =========================================================================*/
18 // .NAME vtkPolygon - a cell that represents an n-sided polygon
19 // .SECTION Description
20 // vtkPolygon is a concrete implementation of vtkCell to represent a 2D
21 // n-sided polygon. The polygons cannot have any internal holes, and cannot
22 // self-intersect. Define the polygon with n-points ordered in the clockwise
23 // direction; do not repeat the last point.
25 #ifndef __vtkPolygon_h
26 #define __vtkPolygon_h
36 class VTK_COMMON_EXPORT vtkPolygon : public vtkCell
39 static vtkPolygon *New();
40 vtkTypeRevisionMacro(vtkPolygon,vtkCell);
43 // See the vtkCell API for descriptions of these methods.
44 int GetCellType() {return VTK_POLYGON;};
45 int GetCellDimension() {return 2;};
46 int GetNumberOfEdges() {return this->GetNumberOfPoints();};
47 int GetNumberOfFaces() {return 0;};
48 vtkCell *GetEdge(int edgeId);
49 vtkCell *GetFace(int) {return 0;};
50 int CellBoundary(int subId, float pcoords[3], vtkIdList *pts);
51 void Contour(float value, vtkDataArray *cellScalars,
52 vtkPointLocator *locator,vtkCellArray *verts,
53 vtkCellArray *lines, vtkCellArray *polys,
54 vtkPointData *inPd, vtkPointData *outPd,
55 vtkCellData *inCd, vtkIdType cellId, vtkCellData *outCd);
56 void Clip(float value, vtkDataArray *cellScalars,
57 vtkPointLocator *locator, vtkCellArray *tris,
58 vtkPointData *inPd, vtkPointData *outPd,
59 vtkCellData *inCd, vtkIdType cellId, vtkCellData *outCd,
61 int EvaluatePosition(float x[3], float* closestPoint,
62 int& subId, float pcoords[3],
63 float& dist2, float *weights);
64 void EvaluateLocation(int& subId, float pcoords[3], float x[3],
66 int IntersectWithLine(float p1[3], float p2[3], float tol, float& t,
67 float x[3], float pcoords[3], int& subId);
68 int Triangulate(int index, vtkIdList *ptIds, vtkPoints *pts);
69 void Derivatives(int subId, float pcoords[3], float *values,
70 int dim, float *derivs);
73 // Polygon specific methods.
74 static void ComputeNormal(vtkPoints *p, int numPts, vtkIdType *pts,
76 static void ComputeNormal(vtkPoints *p, float n[3]);
79 // Compute the polygon normal from an array of points. This version assumes
80 // that the polygon is convex, and looks for the first valid normal.
81 static void ComputeNormal(int numPts, float *pts, float n[3]);
84 // Compute interpolation weights using 1/r**2 normalized sum.
85 void ComputeWeights(float x[3], float *weights);
89 // Create a local s-t coordinate system for a polygon. The point p0 is
90 // the origin of the local system, p10 is s-axis vector, and p20 is the
91 // t-axis vector. (These are expressed in the modeling coordinate system and
92 // are vectors of dimension [3].) The values l20 and l20 are the lengths of
93 // the vectors p10 and p20, and n is the polygon normal.
94 int ParameterizePolygon(float p0[3], float p10[3], float &l10,
95 float p20[3], float &l20, float n[3]);
98 // Determine whether point is inside polygon. Function uses ray-casting
99 // to determine if point is inside polygon. Works for arbitrary polygon shape
100 // (e.g., non-convex). Returns 0 if point is not in polygon; 1 if it is.
101 // Can also return -1 to indicate degenerate polygon.
102 static int PointInPolygon(float x[3], int numPts, float *pts,
103 float bounds[6], float n[3]);
106 // Triangulate this polygon. The user must provide the vtkIdList outTris.
107 // On output, the outTris list contains the ids of the points defining
108 // the triangulation. The ids are ordered into groups of three: each
109 // three-group defines one triangle.
110 int Triangulate(vtkIdList *outTris);
113 // Method intersects two polygons. You must supply the number of points and
114 // point coordinates (npts, *pts) and the bounding box (bounds) of the two
115 // polygons. Also supply a tolerance squared for controlling
116 // error. The method returns 1 if there is an intersection, and 0 if
117 // not. A single point of intersection x[3] is also returned if there
118 // is an intersection.
119 static int IntersectPolygonWithPolygon(int npts, float *pts, float bounds[6],
120 int npts2, float *pts2,
121 float bounds2[3], float tol,
128 // variables used by instances of this class
129 float Tolerance; // Intersection tolerance
130 int SuccessfulTriangulation; // Stops recursive tri. if necessary
131 float Normal[3]; //polygon normal
133 vtkTriangle *Triangle;
135 vtkFloatArray *TriScalars;
138 // Helper methods for triangulation------------------------------
140 // A fast triangulation method. Uses recursive divide and
141 // conquer based on plane splitting to reduce loop into triangles.
142 // The cell (e.g., triangle) is presumed properly initialized (i.e.,
143 // Points and PointIds).
144 int EarCutTriangulation();
147 vtkPolygon(const vtkPolygon&); // Not implemented.
148 void operator=(const vtkPolygon&); // Not implemented.