Panda3D
 All Classes Functions Variables Enumerations
triangulator3.cxx
1 // Filename: triangulator3.cxx
2 // Created by: drose (03Jan13)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "triangulator3.h"
16 #include "look_at.h"
17 
18 ////////////////////////////////////////////////////////////////////
19 // Function: Triangulator3::Constructor
20 // Access: Published
21 // Description:
22 ////////////////////////////////////////////////////////////////////
23 Triangulator3::
24 Triangulator3() {
25 }
26 
27 ////////////////////////////////////////////////////////////////////
28 // Function: Triangulator3::clear
29 // Access: Published
30 // Description: Removes all vertices and polygon specifications from
31 // the Triangulator, and prepares it to start over.
32 ////////////////////////////////////////////////////////////////////
33 void Triangulator3::
34 clear() {
35  _vertices3.clear();
36  _plane = LPlaned();
38 }
39 
40 ////////////////////////////////////////////////////////////////////
41 // Function: Triangulator3::add_vertex
42 // Access: Published
43 // Description: Adds a new vertex to the vertex pool. Returns the
44 // vertex index number.
45 ////////////////////////////////////////////////////////////////////
47 add_vertex(const LPoint3d &point) {
48  int index = (int)_vertices3.size();
49  _vertices3.push_back(point);
50  return index;
51 }
52 
53 ////////////////////////////////////////////////////////////////////
54 // Function: Triangulator3::triangulate
55 // Access: Published
56 // Description: Does the work of triangulating the specified polygon.
57 // After this call, you may retrieve the new triangles
58 // one at a time by iterating through
59 // get_triangle_v0/1/2().
60 ////////////////////////////////////////////////////////////////////
61 void Triangulator3::
63  _result.clear();
64 
65  if (_polygon.size() < 3) {
66  // Degenerate case.
67  return;
68  }
69 
70  // First, determine the polygon normal.
71  LNormald normal = LNormald::zero();
72 
73  // Project the polygon into each of the three major planes and
74  // calculate the area of each 2-d projection. This becomes the
75  // polygon normal. This works because the ratio between these
76  // different areas corresponds to the angle at which the polygon is
77  // tilted toward each plane.
78  size_t num_verts = _polygon.size();
79  for (size_t i = 0; i < num_verts; i++) {
80  int i0 = _polygon[i];
81  int i1 = _polygon[(i + 1) % num_verts];;;;
82  nassertv(i0 >= 0 && i0 < (int)_vertices3.size() &&
83  i1 >= 0 && i1 < (int)_vertices3.size());
84  const LPoint3d &p0 = _vertices3[i0];
85  const LPoint3d &p1 = _vertices3[i1];
86  normal[0] += p0[1] * p1[2] - p0[2] * p1[1];
87  normal[1] += p0[2] * p1[0] - p0[0] * p1[2];
88  normal[2] += p0[0] * p1[1] - p0[1] * p1[0];
89  }
90 
91  if (!normal.normalize()) {
92  // The polygon is degenerate: it has zero area in each plane. In
93  // this case, the triangulation result produces no triangles
94  // anyway.
95  return;
96  }
97 
98  _plane = LPlaned(normal, _vertices3[0]);
99 
100  // Now determine the matrix to project each of the vertices into
101  // this 2-d plane.
102  LMatrix4d mat;
103  heads_up(mat, _vertices3[1] - _vertices3[2], normal, CS_zup_right);
104  mat.set_row(3, _vertices3[0]);
105  mat.invert_in_place();
106 
107  _vertices.clear();
108  for (size_t i = 0; i < _vertices3.size(); i++) {
109  LPoint3d p = _vertices3[i] * mat;
110  _vertices.push_back(LPoint2d(p[0], p[1]));
111  }
113 }
This is a 4-by-4 transform matrix.
Definition: lmatrix.h:4716
void triangulate()
Does the work of triangulating the specified polygon.
void clear()
Removes all vertices and polygon specifications from the Triangulator, and prepares it to start over...
This is a two-component point in space.
Definition: lpoint2.h:411
void triangulate()
Does the work of triangulating the specified polygon.
static const LVector3d & zero()
Returns a zero-length vector.
Definition: lvector3.h:915
void set_row(int row, const LVecBase4d &v)
Replaces the indicated row of the matrix.
Definition: lmatrix.h:5452
bool normalize()
Normalizes the vector in place.
Definition: lvecBase3.h:2132
int add_vertex(const LPoint3d &point)
Adds a new vertex to the vertex pool.
bool invert_in_place()
Inverts the current matrix.
Definition: lmatrix.h:6553
void clear()
Removes all vertices and polygon specifications from the Triangulator, and prepares it to start over...
This is a three-component vector distance (as opposed to a three-component point, which represents a ...
Definition: lvector3.h:746
This is a three-component point in space (as opposed to a three-component vector, which represents a ...
Definition: lpoint3.h:531