00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 
00034 
00035 
00036 
00037 
00038 
00039 
00053 #ifndef __vtkTriangle_h
00054 #define __vtkTriangle_h
00055 
00056 #include "vtkCell.h"
00057 #include "vtkMath.h"
00058 #include "vtkLine.h"
00059 #include "vtkQuadric.h"
00060 
00061 class VTK_EXPORT vtkTriangle : public vtkCell
00062 {
00063 public:
00064   static vtkTriangle *New();
00065   vtkTypeMacro(vtkTriangle,vtkCell);
00066 
00069   vtkCell *MakeObject();
00070 
00073   vtkCell *GetEdge(int edgeId);
00074 
00076   int GetCellType() {return VTK_TRIANGLE;};
00077   int GetCellDimension() {return 2;};
00078   int GetNumberOfEdges() {return 3;};
00079   int GetNumberOfFaces() {return 0;};
00080   vtkCell *GetFace(int) {return 0;};
00081   int CellBoundary(int subId, float pcoords[3], vtkIdList *pts);
00082   void Contour(float value, vtkScalars *cellScalars, 
00083                vtkPointLocator *locator, vtkCellArray *verts,
00084                vtkCellArray *lines, vtkCellArray *polys, 
00085                vtkPointData *inPd, vtkPointData *outPd,
00086                vtkCellData *inCd, int cellId, vtkCellData *outCd);
00087   int EvaluatePosition(float x[3], float* closestPoint,
00088                        int& subId, float pcoords[3],
00089                        float& dist2, float *weights);
00090   void EvaluateLocation(int& subId, float pcoords[3], float x[3],
00091                         float *weights);
00092   int Triangulate(int index, vtkIdList *ptIds, vtkPoints *pts);
00093   void Derivatives(int subId, float pcoords[3], float *values, 
00094                    int dim, float *derivs);
00095 
00098   void Clip(float value, vtkScalars *cellScalars, 
00099             vtkPointLocator *locator, vtkCellArray *polys,
00100             vtkPointData *inPd, vtkPointData *outPd,
00101             vtkCellData *inCd, int cellId, vtkCellData *outCd, int insideOut);
00102 
00105   int IntersectWithLine(float p1[3], float p2[3], float tol, float& t,
00106                         float x[3], float pcoords[3], int& subId);
00107 
00109   int GetParametricCenter(float pcoords[3]);
00110 
00112   static void TriangleCenter(float p1[3], float p2[3], float p3[3], 
00113                              float center[3]);
00114 
00116   static float TriangleArea(float p1[3], float p2[3], float p3[3]);
00117   
00122   static double Circumcircle(double  p1[2], double p2[2], double p3[2], 
00123                             double center[2]);
00124 
00136   static int BarycentricCoords(double x[2], double  x1[2], double x2[2], 
00137                                double x3[2], double bcoords[3]);
00138   
00139   
00143   static int ProjectTo2D(double x1[3], double x2[3], double x3[3],
00144                          double v1[2], double v2[2], double v3[2]);
00145 
00148   static void ComputeNormal(vtkPoints *p, int numPts, int *pts, float n[3]);
00149 
00151   static void ComputeNormal(float v1[3], float v2[3], float v3[3], float n[3]);
00152 
00155   static void ComputeNormalDirection(float v1[3], float v2[3], float v3[3],
00156                  float n[3]);
00157   
00160   static void ComputeNormal(double v1[3], double v2[3], double v3[3], 
00161                             double n[3]);
00162   
00165   static void ComputeNormalDirection(double v1[3], double v2[3], double v3[3],
00166                  double n[3]);
00167 
00173   static int PointInTriangle(float x[3], float x1[3], float x2[3], float x3[3], 
00174                              float tol2);
00175 
00179   static void ComputeQuadric(float x1[3], float x2[3], float x3[3],
00180               float quadric[4][4]);
00181   static void ComputeQuadric(float x1[3], float x2[3], float x3[3],
00182               vtkQuadric *quadric);
00183   
00184 #ifndef VTK_REMOVE_LEGACY_CODE
00185 
00186   int CellBoundary(int subId, float pcoords[3], vtkIdList &pts)
00187     {VTK_LEGACY_METHOD(CellBoundary,"3.2"); return this->CellBoundary(subId, pcoords, &pts);}
00188   int Triangulate(int index, vtkIdList &ptIds, vtkPoints &pts)
00189     {VTK_LEGACY_METHOD(Triangulate,"3.2"); return this->Triangulate(index, &ptIds, &pts);}
00190 #endif
00191   
00192 
00193 protected:
00194   vtkTriangle();
00195   ~vtkTriangle();
00196   vtkTriangle(const vtkTriangle&) {};
00197   void operator=(const vtkTriangle&) {};
00198 
00199   vtkLine *Line;
00200 
00201 };
00202 
00203 inline int vtkTriangle::GetParametricCenter(float pcoords[3])
00204 {
00205   pcoords[0] = pcoords[1] = 0.333; pcoords[2] = 0.0;
00206   return 0;
00207 }
00208 
00209 inline void vtkTriangle::ComputeNormalDirection(float v1[3], float v2[3], 
00210                                        float v3[3], float n[3])
00211 {
00212   float ax, ay, az, bx, by, bz;
00213 
00214   
00215   ax = v3[0] - v2[0]; ay = v3[1] - v2[1]; az = v3[2] - v2[2];
00216   bx = v1[0] - v2[0]; by = v1[1] - v2[1]; bz = v1[2] - v2[2];
00217 
00218   n[0] = (ay * bz - az * by);
00219   n[1] = (az * bx - ax * bz);
00220   n[2] = (ax * by - ay * bx);
00221 }
00222 
00223 inline void vtkTriangle::ComputeNormal(float v1[3], float v2[3], 
00224                                        float v3[3], float n[3])
00225 {
00226   float length;
00227   
00228   vtkTriangle::ComputeNormalDirection(v1, v2, v3, n);
00229   
00230   if ( (length = sqrt((n[0]*n[0] + n[1]*n[1] + n[2]*n[2]))) != 0.0 )
00231     {
00232     n[0] /= length;
00233     n[1] /= length;
00234     n[2] /= length;
00235     }
00236 }
00237 
00238 inline void vtkTriangle::ComputeNormalDirection(double v1[3], double v2[3], 
00239                                        double v3[3], double n[3])
00240 {
00241   double ax, ay, az, bx, by, bz;
00242 
00243   
00244   ax = v3[0] - v2[0]; ay = v3[1] - v2[1]; az = v3[2] - v2[2];
00245   bx = v1[0] - v2[0]; by = v1[1] - v2[1]; bz = v1[2] - v2[2];
00246 
00247   n[0] = (ay * bz - az * by);
00248   n[1] = (az * bx - ax * bz);
00249   n[2] = (ax * by - ay * bx);
00250 }
00251 
00252 inline void vtkTriangle::ComputeNormal(double v1[3], double v2[3], 
00253                                        double v3[3], double n[3])
00254 {
00255   double length;
00256 
00257   vtkTriangle::ComputeNormalDirection(v1, v2, v3, n);
00258 
00259   if ( (length = sqrt((n[0]*n[0] + n[1]*n[1] + n[2]*n[2]))) != 0.0 )
00260     {
00261     n[0] /= length;
00262     n[1] /= length;
00263     n[2] /= length;
00264     }
00265 }
00266 
00267 inline void vtkTriangle::TriangleCenter(float p1[3], float p2[3], float p3[3],
00268                                        float center[3])
00269 {
00270   center[0] = (p1[0]+p2[0]+p3[0]) / 3.0;
00271   center[1] = (p1[1]+p2[1]+p3[1]) / 3.0;
00272   center[2] = (p1[2]+p2[2]+p3[2]) / 3.0;
00273 }
00274 
00275 inline float vtkTriangle::TriangleArea(float p1[3], float p2[3], float p3[3])
00276 {
00277   float a,b,c;
00278   a = vtkMath::Distance2BetweenPoints(p1,p2);
00279   b = vtkMath::Distance2BetweenPoints(p2,p3);
00280   c = vtkMath::Distance2BetweenPoints(p3,p1);
00281   return (0.25* sqrt(fabs((double)4.0*a*c - (a-b+c)*(a-b+c))));
00282 } 
00283 
00284 #endif
00285 
00286