VTK
dox/Common/DataModel/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 "vtkCommonDataModelModule.h" // For export macro
00032 #include "vtkCell.h"
00033 
00034 #include "vtkMath.h" // Needed for inline methods
00035 
00036 class vtkLine;
00037 class vtkQuadric;
00038 class vtkIncrementalPointLocator;
00039 
00040 class VTKCOMMONDATAMODEL_EXPORT vtkTriangle : public vtkCell
00041 {
00042 public:
00043   static vtkTriangle *New();
00044   vtkTypeMacro(vtkTriangle,vtkCell);
00045   void PrintSelf(ostream& os, vtkIndent indent);
00046 
00049   vtkCell *GetEdge(int edgeId);
00050 
00052 
00053   int GetCellType() {return VTK_TRIANGLE;};
00054   int GetCellDimension() {return 2;};
00055   int GetNumberOfEdges() {return 3;};
00056   int GetNumberOfFaces() {return 0;};
00057   vtkCell *GetFace(int) {return 0;};
00058   int CellBoundary(int subId, double pcoords[3], vtkIdList *pts);
00059   void Contour(double value, vtkDataArray *cellScalars,
00060                vtkIncrementalPointLocator *locator, vtkCellArray *verts,
00061                vtkCellArray *lines, vtkCellArray *polys,
00062                vtkPointData *inPd, vtkPointData *outPd,
00063                vtkCellData *inCd, vtkIdType cellId, vtkCellData *outCd);
00064   int EvaluatePosition(double x[3], double* closestPoint,
00065                        int& subId, double pcoords[3],
00066                        double& dist2, double *weights);
00067   void EvaluateLocation(int& subId, double pcoords[3], double x[3],
00068                         double *weights);
00069   int Triangulate(int index, vtkIdList *ptIds, vtkPoints *pts);
00070   void Derivatives(int subId, double pcoords[3], double *values,
00071                    int dim, double *derivs);
00072   virtual double *GetParametricCoords();
00074 
00076   double ComputeArea();
00077 
00079 
00081   void Clip(double value, vtkDataArray *cellScalars,
00082             vtkIncrementalPointLocator *locator, vtkCellArray *polys,
00083             vtkPointData *inPd, vtkPointData *outPd,
00084             vtkCellData *inCd, vtkIdType cellId, vtkCellData *outCd,
00085             int insideOut);
00087 
00090   static void InterpolationFunctions(double pcoords[3], double sf[3]);
00092   static void InterpolationDerivs(double pcoords[3], double derivs[6]);
00094 
00096   virtual void InterpolateFunctions(double pcoords[3], double sf[3])
00097     {
00098     vtkTriangle::InterpolationFunctions(pcoords,sf);
00099     }
00100   virtual void InterpolateDerivs(double pcoords[3], double derivs[6])
00101     {
00102     vtkTriangle::InterpolationDerivs(pcoords,derivs);
00103     }
00105 
00107   int *GetEdgeArray(int edgeId);
00108 
00110 
00112   int IntersectWithLine(double p1[3], double p2[3], double tol, double& t,
00113                         double x[3], double pcoords[3], int& subId);
00115 
00117   int GetParametricCenter(double pcoords[3]);
00118 
00121   double GetParametricDistance(double pcoords[3]);
00122 
00124 
00125   static void TriangleCenter(double p1[3], double p2[3], double p3[3],
00126                              double center[3]);
00128 
00131   static double TriangleArea(double p1[3], double p2[3], double p3[3]);
00132 
00134 
00138   static double Circumcircle(double  p1[2], double p2[2], double p3[2],
00139                             double center[2]);
00141 
00143 
00154   static int BarycentricCoords(double x[2], double  x1[2], double x2[2],
00155                                double x3[2], double bcoords[3]);
00157 
00158 
00160 
00163   static int ProjectTo2D(double x1[3], double x2[3], double x3[3],
00164                          double v1[2], double v2[2], double v3[2]);
00166 
00168 
00170   static void ComputeNormal(vtkPoints *p, int numPts, vtkIdType *pts,
00171                             double n[3]);
00173 
00175   static void ComputeNormal(double v1[3], double v2[3], double v3[3], double n[3]);
00176 
00178 
00180   static void ComputeNormalDirection(double v1[3], double v2[3], double v3[3],
00181                                      double n[3]);
00183 
00185 
00190   static int PointInTriangle(double x[3], double x1[3],
00191                              double x2[3], double x3[3],
00192                              double tol2);
00194 
00196 
00199   static void ComputeQuadric(double x1[3], double x2[3], double x3[3],
00200                              double quadric[4][4]);
00201   static void ComputeQuadric(double x1[3], double x2[3], double x3[3],
00202                              vtkQuadric *quadric);
00204 
00205 
00206 protected:
00207   vtkTriangle();
00208   ~vtkTriangle();
00209 
00210   vtkLine *Line;
00211 
00212 private:
00213   vtkTriangle(const vtkTriangle&);  // Not implemented.
00214   void operator=(const vtkTriangle&);  // Not implemented.
00215 };
00216 
00217 //----------------------------------------------------------------------------
00218 inline int vtkTriangle::GetParametricCenter(double pcoords[3])
00219 {
00220   pcoords[0] = pcoords[1] = 1./3; pcoords[2] = 0.0;
00221   return 0;
00222 }
00223 
00224 //----------------------------------------------------------------------------
00225 inline void vtkTriangle::ComputeNormalDirection(double v1[3], double v2[3],
00226                                        double v3[3], double n[3])
00227 {
00228   double ax, ay, az, bx, by, bz;
00229 
00230   // order is important!!! maintain consistency with triangle vertex order
00231   ax = v3[0] - v2[0]; ay = v3[1] - v2[1]; az = v3[2] - v2[2];
00232   bx = v1[0] - v2[0]; by = v1[1] - v2[1]; bz = v1[2] - v2[2];
00233 
00234   n[0] = (ay * bz - az * by);
00235   n[1] = (az * bx - ax * bz);
00236   n[2] = (ax * by - ay * bx);
00237 }
00238 
00239 //----------------------------------------------------------------------------
00240 inline void vtkTriangle::ComputeNormal(double v1[3], double v2[3],
00241                                        double v3[3], double n[3])
00242 {
00243   double length;
00244 
00245   vtkTriangle::ComputeNormalDirection(v1, v2, v3, n);
00246 
00247   if ( (length = sqrt((n[0]*n[0] + n[1]*n[1] + n[2]*n[2]))) != 0.0 )
00248     {
00249     n[0] /= length;
00250     n[1] /= length;
00251     n[2] /= length;
00252     }
00253 }
00254 
00255 //----------------------------------------------------------------------------
00256 inline void vtkTriangle::TriangleCenter(double p1[3], double p2[3],
00257                                         double p3[3], double center[3])
00258 {
00259   center[0] = (p1[0]+p2[0]+p3[0]) / 3.0;
00260   center[1] = (p1[1]+p2[1]+p3[1]) / 3.0;
00261   center[2] = (p1[2]+p2[2]+p3[2]) / 3.0;
00262 }
00263 
00264 //----------------------------------------------------------------------------
00265 inline double vtkTriangle::TriangleArea(double p1[3], double p2[3], double p3[3])
00266 {
00267   double a,b,c;
00268   a = vtkMath::Distance2BetweenPoints(p1,p2);
00269   b = vtkMath::Distance2BetweenPoints(p2,p3);
00270   c = vtkMath::Distance2BetweenPoints(p3,p1);
00271   return (0.25* sqrt(fabs(4.0*a*c - (a-b+c)*(a-b+c))));
00272 }
00273 
00274 #endif
00275 
00276