Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

Common/vtkTriangle.h

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Visualization Toolkit
00004   Module:    $RCSfile: vtkTriangle.h,v $
00005   Language:  C++
00006 
00007   Copyright (c) 1993-2002 Ken Martin, Will Schroeder, Bill Lorensen 
00008   All rights reserved.
00009   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
00010 
00011      This software is distributed WITHOUT ANY WARRANTY; without even 
00012      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
00013      PURPOSE.  See the above copyright notice for more information.
00014 
00015 =========================================================================*/
00046 #ifndef __vtkTriangle_h
00047 #define __vtkTriangle_h
00048 
00049 #include "vtkCell.h"
00050 
00051 #include "vtkMath.h" // Needed for inline methods
00052 
00053 class vtkLine;
00054 class vtkQuadric;
00055 
00056 class VTK_COMMON_EXPORT vtkTriangle : public vtkCell
00057 {
00058 public:
00059   static vtkTriangle *New();
00060   vtkTypeRevisionMacro(vtkTriangle,vtkCell);
00061 
00067   vtkCell *GetEdge(int edgeId);
00068 
00070 
00071   int GetCellType() {return VTK_TRIANGLE;};
00072   int GetCellDimension() {return 2;};
00073   int GetNumberOfEdges() {return 3;};
00074   int GetNumberOfFaces() {return 0;};
00075   vtkCell *GetFace(int) {return 0;};
00076   int CellBoundary(int subId, float pcoords[3], vtkIdList *pts);
00077   void Contour(float value, vtkDataArray *cellScalars, 
00078                vtkPointLocator *locator, vtkCellArray *verts,
00079                vtkCellArray *lines, vtkCellArray *polys, 
00080                vtkPointData *inPd, vtkPointData *outPd,
00081                vtkCellData *inCd, vtkIdType cellId, vtkCellData *outCd);
00082   int EvaluatePosition(float x[3], float* closestPoint,
00083                        int& subId, float pcoords[3],
00084                        float& dist2, float *weights);
00085   void EvaluateLocation(int& subId, float pcoords[3], float x[3],
00086                         float *weights);
00087   int Triangulate(int index, vtkIdList *ptIds, vtkPoints *pts);
00088   void Derivatives(int subId, float pcoords[3], float *values, 
00089                    int dim, float *derivs);
00091 
00093 
00095   void Clip(float value, vtkDataArray *cellScalars, 
00096             vtkPointLocator *locator, vtkCellArray *polys,
00097             vtkPointData *inPd, vtkPointData *outPd,
00098             vtkCellData *inCd, vtkIdType cellId, vtkCellData *outCd,
00099             int insideOut);
00101 
00103 
00105   int IntersectWithLine(float p1[3], float p2[3], float tol, float& t,
00106                         float x[3], float pcoords[3], int& subId);
00108 
00110   int GetParametricCenter(float pcoords[3]);
00111 
00113 
00114   static void TriangleCenter(float p1[3], float p2[3], float p3[3], 
00115                              float center[3]);
00117 
00119   static float TriangleArea(float p1[3], float p2[3], float p3[3]);
00120   
00122 
00126   static double Circumcircle(double  p1[2], double p2[2], double p3[2], 
00127                             double center[2]);
00129 
00131 
00142   static int BarycentricCoords(double x[2], double  x1[2], double x2[2], 
00143                                double x3[2], double bcoords[3]);
00145   
00146   
00148 
00151   static int ProjectTo2D(double x1[3], double x2[3], double x3[3],
00152                          double v1[2], double v2[2], double v3[2]);
00154 
00156 
00158   static void ComputeNormal(vtkPoints *p, int numPts, vtkIdType *pts,
00159                             float n[3]);
00161 
00163   static void ComputeNormal(float v1[3], float v2[3], float v3[3], float n[3]);
00164 
00166 
00168   static void ComputeNormalDirection(float v1[3], float v2[3], float v3[3],
00169                                      float n[3]);
00171   
00173 
00175   static void ComputeNormal(double v1[3], double v2[3], double v3[3], 
00176                             double n[3]);
00178   
00180 
00182   static void ComputeNormalDirection(double v1[3], double v2[3], double v3[3],
00183                                      double n[3]);
00185 
00187 
00192   static int PointInTriangle(float x[3], float x1[3], float x2[3], float x3[3], 
00193                              float tol2);
00195 
00197 
00200   static void ComputeQuadric(float x1[3], float x2[3], float x3[3],
00201                              float quadric[4][4]);
00202   static void ComputeQuadric(float x1[3], float x2[3], float x3[3],
00203                              vtkQuadric *quadric);
00205   
00206 
00207 protected:
00208   vtkTriangle();
00209   ~vtkTriangle();
00210 
00211   vtkLine *Line;
00212 
00213 private:
00214   vtkTriangle(const vtkTriangle&);  // Not implemented.
00215   void operator=(const vtkTriangle&);  // Not implemented.
00216 };
00217 
00218 inline int vtkTriangle::GetParametricCenter(float pcoords[3])
00219 {
00220   pcoords[0] = pcoords[1] = 0.333f; pcoords[2] = 0.0;
00221   return 0;
00222 }
00223 
00224 inline void vtkTriangle::ComputeNormalDirection(float v1[3], float v2[3], 
00225                                        float v3[3], float n[3])
00226 {
00227   float 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 inline void vtkTriangle::ComputeNormal(float v1[3], float v2[3], 
00239                                        float v3[3], float n[3])
00240 {
00241   float length;
00242   
00243   vtkTriangle::ComputeNormalDirection(v1, v2, v3, n);
00244   
00245   if ( (length = static_cast<float>(sqrt((n[0]*n[0] + n[1]*n[1] + n[2]*n[2])))) != 0.0 )
00246     {
00247     n[0] /= length;
00248     n[1] /= length;
00249     n[2] /= length;
00250     }
00251 }
00252 
00253 inline void vtkTriangle::ComputeNormalDirection(double v1[3], double v2[3], 
00254                                        double v3[3], double n[3])
00255 {
00256   double ax, ay, az, bx, by, bz;
00257 
00258   // order is important!!! maintain consistency with triangle vertex order 
00259   ax = v3[0] - v2[0]; ay = v3[1] - v2[1]; az = v3[2] - v2[2];
00260   bx = v1[0] - v2[0]; by = v1[1] - v2[1]; bz = v1[2] - v2[2];
00261 
00262   n[0] = (ay * bz - az * by);
00263   n[1] = (az * bx - ax * bz);
00264   n[2] = (ax * by - ay * bx);
00265 }
00266 
00267 inline void vtkTriangle::ComputeNormal(double v1[3], double v2[3], 
00268                                        double v3[3], double n[3])
00269 {
00270   double length;
00271 
00272   vtkTriangle::ComputeNormalDirection(v1, v2, v3, n);
00273 
00274   if ( (length = sqrt((n[0]*n[0] + n[1]*n[1] + n[2]*n[2]))) != 0.0 )
00275     {
00276     n[0] /= length;
00277     n[1] /= length;
00278     n[2] /= length;
00279     }
00280 }
00281 
00282 inline void vtkTriangle::TriangleCenter(float p1[3], float p2[3], float p3[3],
00283                                        float center[3])
00284 {
00285   center[0] = (p1[0]+p2[0]+p3[0]) / 3.0f;
00286   center[1] = (p1[1]+p2[1]+p3[1]) / 3.0f;
00287   center[2] = (p1[2]+p2[2]+p3[2]) / 3.0f;
00288 }
00289 
00290 inline float vtkTriangle::TriangleArea(float p1[3], float p2[3], float p3[3])
00291 {
00292   float a,b,c;
00293   a = vtkMath::Distance2BetweenPoints(p1,p2);
00294   b = vtkMath::Distance2BetweenPoints(p2,p3);
00295   c = vtkMath::Distance2BetweenPoints(p3,p1);
00296   return static_cast<float>(0.25* sqrt(fabs((double)4.0*a*c - (a-b+c)*(a-b+c))));
00297 } 
00298 
00299 #endif
00300 
00301