VTK
dox/Filtering/vtkTriangle.h
Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Visualization Toolkit
00004   Module:    vtkTriangle.h
00005 
00006   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
00007   All rights reserved.
00008   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
00009 
00010      This software is distributed WITHOUT ANY WARRANTY; without even
00011      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00012      PURPOSE.  See the above copyright notice for more information.
00013 
00014 =========================================================================*/
00028 #ifndef __vtkTriangle_h
00029 #define __vtkTriangle_h
00030 
00031 #include "vtkCell.h"
00032 
00033 #include "vtkMath.h" // Needed for inline methods
00034 
00035 class vtkLine;
00036 class vtkQuadric;
00037 class vtkIncrementalPointLocator;
00038 
00039 class VTK_FILTERING_EXPORT vtkTriangle : public vtkCell
00040 {
00041 public:
00042   static vtkTriangle *New();
00043   vtkTypeMacro(vtkTriangle,vtkCell);
00044   void PrintSelf(ostream& os, vtkIndent indent);
00045 
00048   vtkCell *GetEdge(int edgeId);
00049 
00051 
00052   int GetCellType() {return VTK_TRIANGLE;};
00053   int GetCellDimension() {return 2;};
00054   int GetNumberOfEdges() {return 3;};
00055   int GetNumberOfFaces() {return 0;};
00056   vtkCell *GetFace(int) {return 0;};
00057   int CellBoundary(int subId, double pcoords[3], vtkIdList *pts);
00058   void Contour(double value, vtkDataArray *cellScalars,
00059                vtkIncrementalPointLocator *locator, vtkCellArray *verts,
00060                vtkCellArray *lines, vtkCellArray *polys,
00061                vtkPointData *inPd, vtkPointData *outPd,
00062                vtkCellData *inCd, vtkIdType cellId, vtkCellData *outCd);
00063   int EvaluatePosition(double x[3], double* closestPoint,
00064                        int& subId, double pcoords[3],
00065                        double& dist2, double *weights);
00066   void EvaluateLocation(int& subId, double pcoords[3], double x[3],
00067                         double *weights);
00068   int Triangulate(int index, vtkIdList *ptIds, vtkPoints *pts);
00069   void Derivatives(int subId, double pcoords[3], double *values,
00070                    int dim, double *derivs);
00071   virtual double *GetParametricCoords();
00073 
00075   double ComputeArea();
00076 
00078 
00080   void Clip(double value, vtkDataArray *cellScalars,
00081             vtkIncrementalPointLocator *locator, vtkCellArray *polys,
00082             vtkPointData *inPd, vtkPointData *outPd,
00083             vtkCellData *inCd, vtkIdType cellId, vtkCellData *outCd,
00084             int insideOut);
00086 
00089   static void InterpolationFunctions(double pcoords[3], double sf[3]);
00091   static void InterpolationDerivs(double pcoords[3], double derivs[6]);
00093 
00095   virtual void InterpolateFunctions(double pcoords[3], double sf[3])
00096     {
00097     vtkTriangle::InterpolationFunctions(pcoords,sf);
00098     }
00099   virtual void InterpolateDerivs(double pcoords[3], double derivs[6])
00100     {
00101     vtkTriangle::InterpolationDerivs(pcoords,derivs);
00102     }
00104 
00106   int *GetEdgeArray(int edgeId);
00107 
00109 
00111   int IntersectWithLine(double p1[3], double p2[3], double tol, double& t,
00112                         double x[3], double pcoords[3], int& subId);
00114 
00116   int GetParametricCenter(double pcoords[3]);
00117 
00120   double GetParametricDistance(double pcoords[3]);
00121 
00123 
00124   static void TriangleCenter(double p1[3], double p2[3], double p3[3],
00125                              double center[3]);
00127 
00130   static double TriangleArea(double p1[3], double p2[3], double p3[3]);
00131 
00133 
00137   static double Circumcircle(double  p1[2], double p2[2], double p3[2],
00138                             double center[2]);
00140 
00142 
00153   static int BarycentricCoords(double x[2], double  x1[2], double x2[2],
00154                                double x3[2], double bcoords[3]);
00156 
00157 
00159 
00162   static int ProjectTo2D(double x1[3], double x2[3], double x3[3],
00163                          double v1[2], double v2[2], double v3[2]);
00165 
00167 
00169   static void ComputeNormal(vtkPoints *p, int numPts, vtkIdType *pts,
00170                             double n[3]);
00172 
00174   static void ComputeNormal(double v1[3], double v2[3], double v3[3], double n[3]);
00175 
00177 
00179   static void ComputeNormalDirection(double v1[3], double v2[3], double v3[3],
00180                                      double n[3]);
00182 
00184 
00189   static int PointInTriangle(double x[3], double x1[3],
00190                              double x2[3], double x3[3],
00191                              double tol2);
00193 
00195 
00198   static void ComputeQuadric(double x1[3], double x2[3], double x3[3],
00199                              double quadric[4][4]);
00200   static void ComputeQuadric(double x1[3], double x2[3], double x3[3],
00201                              vtkQuadric *quadric);
00203 
00204 
00205 protected:
00206   vtkTriangle();
00207   ~vtkTriangle();
00208 
00209   vtkLine *Line;
00210 
00211 private:
00212   vtkTriangle(const vtkTriangle&);  // Not implemented.
00213   void operator=(const vtkTriangle&);  // Not implemented.
00214 };
00215 
00216 //----------------------------------------------------------------------------
00217 inline int vtkTriangle::GetParametricCenter(double pcoords[3])
00218 {
00219   pcoords[0] = pcoords[1] = 1./3; pcoords[2] = 0.0;
00220   return 0;
00221 }
00222 
00223 //----------------------------------------------------------------------------
00224 inline void vtkTriangle::ComputeNormalDirection(double v1[3], double v2[3],
00225                                        double v3[3], double n[3])
00226 {
00227   double ax, ay, az, bx, by, bz;
00228 
00229   // order is important!!! maintain consistency with triangle vertex order
00230   ax = v3[0] - v2[0]; ay = v3[1] - v2[1]; az = v3[2] - v2[2];
00231   bx = v1[0] - v2[0]; by = v1[1] - v2[1]; bz = v1[2] - v2[2];
00232 
00233   n[0] = (ay * bz - az * by);
00234   n[1] = (az * bx - ax * bz);
00235   n[2] = (ax * by - ay * bx);
00236 }
00237 
00238 //----------------------------------------------------------------------------
00239 inline void vtkTriangle::ComputeNormal(double v1[3], double v2[3],
00240                                        double v3[3], double n[3])
00241 {
00242   double length;
00243 
00244   vtkTriangle::ComputeNormalDirection(v1, v2, v3, n);
00245 
00246   if ( (length = sqrt((n[0]*n[0] + n[1]*n[1] + n[2]*n[2]))) != 0.0 )
00247     {
00248     n[0] /= length;
00249     n[1] /= length;
00250     n[2] /= length;
00251     }
00252 }
00253 
00254 //----------------------------------------------------------------------------
00255 inline void vtkTriangle::TriangleCenter(double p1[3], double p2[3],
00256                                         double p3[3], double center[3])
00257 {
00258   center[0] = (p1[0]+p2[0]+p3[0]) / 3.0;
00259   center[1] = (p1[1]+p2[1]+p3[1]) / 3.0;
00260   center[2] = (p1[2]+p2[2]+p3[2]) / 3.0;
00261 }
00262 
00263 //----------------------------------------------------------------------------
00264 inline double vtkTriangle::TriangleArea(double p1[3], double p2[3], double p3[3])
00265 {
00266   double a,b,c;
00267   a = vtkMath::Distance2BetweenPoints(p1,p2);
00268   b = vtkMath::Distance2BetweenPoints(p2,p3);
00269   c = vtkMath::Distance2BetweenPoints(p3,p1);
00270   return (0.25* sqrt(fabs(4.0*a*c - (a-b+c)*(a-b+c))));
00271 }
00272 
00273 #endif
00274 
00275