File: TestTriangle.cxx

package info (click to toggle)
vtk9 9.5.2%2Bdfsg3-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 205,984 kB
  • sloc: cpp: 2,336,570; ansic: 327,116; python: 111,200; yacc: 4,104; java: 3,977; sh: 3,032; xml: 2,771; perl: 2,189; lex: 1,787; makefile: 181; javascript: 165; objc: 153; tcl: 59
file content (195 lines) | stat: -rw-r--r-- 5,582 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-License-Identifier: BSD-3-Clause

// .NAME
// .SECTION Description
// this program tests the Triangle

#include "vtkMathUtilities.h"
#include "vtkNew.h"
#include "vtkPoints.h"
#include "vtkSmartPointer.h"
#include "vtkTriangle.h"
#include <limits>

int TestTriangle(int, char*[])
{
  // three vertices making a triangle
  double pnt0[3] = { 0, 2, 0 };
  double pnt1[3] = { 4, 2, 0 };
  double pnt2[3] = { 0, 6, 0 };

  // points to be tested against the triangle
  double pnts[][3] = {
    // squared error tolerance
    // = 0.0001 * 0.0001 = 0.00000001

    // outside the triangle
    { 0, 1.999, 0 },
    { -0.001, 2, 0 },

    { 4, 1.999, 0 },
    { 4, 2.001, 0 },
    { 4.001, 2, 0 },

    { 0, 6.001, 0 },
    { 0.001, 6, 0 },
    { -0.001, 6, 0 },

    { -0.001, 2.001, 0 },
    { -0.001, 1.999, 0 },
    { 0.001, 1.999, 0 },

    { 4.001, 2.001, 0 },
    { 4.001, 1.999, 0 },
    { 3.999, 1.999, 0 },

    { -0.001, 5.999, 0 },
    { -0.001, 6.001, 0 },
    { 0.001, 6.001, 0 },

    // inside the triangle
    { 0, 2.001, 0 },
    { 0.001, 2, 0 },
    { 0.001, 2.001, 0 },

    { 3.999, 2.001, 0 },
    { 3.999, 2, 0 },

    { 0, 5.999, 0 },
    { 0.001, 5.999, 0 },

    { 0, 2, 0 },
    { 4, 2, 0 },
    { 0, 6, 0 },

    { 2, 2, 0 },
    { 2, 4, 0 },
    { 0, 4, 0 },
    { 1.333, 3.333, 0 },
  };

  int inside;
  for (int i = 0; i < 31; i++)
  {
    inside = vtkTriangle::PointInTriangle(pnts[i], pnt0, pnt1, pnt2, 0.00000001);

    if (inside && i < 17)
    {
      cerr << "ERROR:  point #" << i
           << ", an outside-point, considered to be inside the triangle!!!" << endl;
      cerr << "Squared error tolerance: 0.00000001" << endl;
      return EXIT_FAILURE;
    }
    else if (!inside && i > 16)
    {
      cerr << "ERROR:  point #" << i
           << ", an inside-point, considered to be outside the triangle!!!" << endl;
      cerr << "Squared error tolerance: 0.00000001" << endl;
      return EXIT_FAILURE;
    }
  }

  cout << "Passed: 17 points outside and 14 points inside the triangle." << endl;

  vtkSmartPointer<vtkTriangle> triangle = vtkSmartPointer<vtkTriangle>::New();
  triangle->GetPoints()->SetPoint(0, 0.0, 0.0, 0.0);
  triangle->GetPoints()->SetPoint(1, 1.0, 0.0, 0.0);
  triangle->GetPoints()->SetPoint(2, 0.0, 1.0, 0.0);

  double area = triangle->ComputeArea();
  if (!vtkMathUtilities::NearlyEqual<double>(area, 0.5))
  {
    cerr << "ERROR:  triangle area is " << area << ", should be 0.5" << endl;
    return EXIT_FAILURE;
  }

  // Testing degenerated triangle
  double pntDeg0[3] = { 0, 0, -10 };
  double pntDeg1[3] = { 0, 0, 0 };
  double pntDeg2[3] = { 0, 0, 10 };
  vtkNew<vtkTriangle> triangleDeg;
  triangleDeg->GetPoints()->SetPoint(0, pntDeg0);
  triangleDeg->GetPoints()->SetPoint(1, pntDeg1);
  triangleDeg->GetPoints()->SetPoint(2, pntDeg2);

  double p1[3] = { 0, 1, 1 };
  double p2[3] = { 0, -1, 1 };
  double t;
  double x[3];
  double pcoords[3];
  int subId;
  double dEpsilon = std::numeric_limits<double>::epsilon();
  if (triangleDeg->IntersectWithLine(p1, p2, dEpsilon, t, x, pcoords, subId) != 1 ||
    !vtkMathUtilities::NearlyEqual<double>(x[0], 0.0) ||
    !vtkMathUtilities::NearlyEqual<double>(x[1], 0.0) ||
    !vtkMathUtilities::NearlyEqual<double>(x[2], 1.0) ||
    !vtkMathUtilities::NearlyEqual<double>(t, 0.5) ||
    !vtkMathUtilities::NearlyEqual<double>(pcoords[0], 1.1) ||
    !vtkMathUtilities::NearlyEqual<double>(pcoords[1], 0.55) ||
    !vtkMathUtilities::NearlyEqual<double>(pcoords[2], 0.0))
  {
    cerr << "Error while intersecting degenerated triangle" << endl;
    return EXIT_FAILURE;
  }
  double p1b[3] = { 0, 1, 10.001 };
  double p2b[3] = { 0, -1, 10.001 };
  if (triangleDeg->IntersectWithLine(p1b, p2b, dEpsilon, t, x, pcoords, subId) != 0)
  {
    cerr << "Error while intersecting degenerated triangle" << endl;
    return EXIT_FAILURE;
  }

  // Testing intersection of triangle with coplanar line

  // Build triangle
  double pt0[3] = { 0, 0, 0 };
  double pt1[3] = { 0, 10, 0 };
  double pt2[3] = { 0, 0, 10 };
  vtkNew<vtkTriangle> coplanarTriangle;
  coplanarTriangle->GetPoints()->SetPoint(0, pt0);
  coplanarTriangle->GetPoints()->SetPoint(1, pt1);
  coplanarTriangle->GetPoints()->SetPoint(2, pt2);

  // Define line extremities with first extremity inside
  double ext1[3] = { 0, 1, 5 };
  double ext2[3] = { 0, 11, 5 };

  int res = coplanarTriangle->IntersectWithLine(ext1, ext2, dEpsilon, t, x, pcoords, subId);
  // Verify correct output values
  if (res != 1)
  {
    cerr << "Line intersection with coplanar triangle not detected" << endl;
    return EXIT_FAILURE;
  }
  else if (x[0] != 0 || x[1] != 1 || x[2] != 5 || t != 0.0 || pcoords[0] != 0.1 ||
    pcoords[1] != 0.5 || pcoords[2] != 0.0)
  {
    cerr << "Output coordinates of intersecting point incorrect" << endl;
    return EXIT_FAILURE;
  }

  // Define line extremities with first extremity outside
  ext1[0] = 0;
  ext1[1] = -1;
  ext1[2] = 5;
  ext2[0] = 0;
  ext2[1] = 9;
  ext2[2] = 5;

  res = coplanarTriangle->IntersectWithLine(ext1, ext2, dEpsilon, t, x, pcoords, subId);
  // Verify correct output values
  if (res != 1)
  {
    cerr << "Line intersection with coplanar triangle not detected" << endl;
    return EXIT_FAILURE;
  }
  else if (x[0] != 0 || x[1] != 0 || x[2] != 5 || t != 0.1 || pcoords[0] != 0.0 ||
    pcoords[1] != 0.5 || pcoords[2] != 0.0)
  {
    cerr << "Output coordinates of intersecting point incorrect" << endl;
    return EXIT_FAILURE;
  }

  return EXIT_SUCCESS;
}