Panda3D
triangulator3.cxx
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file triangulator3.cxx
10  * @author drose
11  * @date 2013-01-03
12  */
13 
14 #include "triangulator3.h"
15 #include "look_at.h"
16 
17 /**
18  *
19  */
20 Triangulator3::
21 Triangulator3() {
22 }
23 
24 /**
25  * Removes all vertices and polygon specifications from the Triangulator, and
26  * prepares it to start over.
27  */
28 void Triangulator3::
29 clear() {
30  _vertices3.clear();
31  _plane = LPlaned();
33 }
34 
35 /**
36  * Adds a new vertex to the vertex pool. Returns the vertex index number.
37  */
39 add_vertex(const LPoint3d &point) {
40  int index = (int)_vertices3.size();
41  _vertices3.push_back(point);
42  return index;
43 }
44 
45 /**
46  * Does the work of triangulating the specified polygon. After this call, you
47  * may retrieve the new triangles one at a time by iterating through
48  * get_triangle_v0/1/2().
49  */
50 void Triangulator3::
52  _result.clear();
53 
54  if (_polygon.size() < 3) {
55  // Degenerate case.
56  return;
57  }
58 
59  // First, determine the polygon normal.
60  LNormald normal = LNormald::zero();
61 
62  // Project the polygon into each of the three major planes and calculate the
63  // area of each 2-d projection. This becomes the polygon normal. This
64  // works because the ratio between these different areas corresponds to the
65  // angle at which the polygon is tilted toward each plane.
66  size_t num_verts = _polygon.size();
67  for (size_t i = 0; i < num_verts; i++) {
68  int i0 = _polygon[i];
69  int i1 = _polygon[(i + 1) % num_verts];;;;
70  nassertv(i0 >= 0 && i0 < (int)_vertices3.size() &&
71  i1 >= 0 && i1 < (int)_vertices3.size());
72  const LPoint3d &p0 = _vertices3[i0];
73  const LPoint3d &p1 = _vertices3[i1];
74  normal[0] += p0[1] * p1[2] - p0[2] * p1[1];
75  normal[1] += p0[2] * p1[0] - p0[0] * p1[2];
76  normal[2] += p0[0] * p1[1] - p0[1] * p1[0];
77  }
78 
79  if (!normal.normalize()) {
80  // The polygon is degenerate: it has zero area in each plane. In this
81  // case, the triangulation result produces no triangles anyway.
82  return;
83  }
84 
85  _plane = LPlaned(normal, _vertices3[0]);
86 
87  // Now determine the matrix to project each of the vertices into this 2-d
88  // plane.
89  LMatrix4d mat;
90  heads_up(mat, _vertices3[1] - _vertices3[2], normal, CS_zup_right);
91  mat.set_row(3, _vertices3[0]);
92  mat.invert_in_place();
93 
94  _vertices.clear();
95  for (size_t i = 0; i < _vertices3.size(); i++) {
96  LPoint3d p = _vertices3[i] * mat;
97  _vertices.push_back(LPoint2d(p[0], p[1]));
98  }
100 }
Triangulator::clear
void clear()
Removes all vertices and polygon specifications from the Triangulator, and prepares it to start over.
Definition: triangulator.cxx:29
triangulator3.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
Triangulator3::clear
void clear()
Removes all vertices and polygon specifications from the Triangulator, and prepares it to start over.
Definition: triangulator3.cxx:29
Triangulator3::triangulate
void triangulate()
Does the work of triangulating the specified polygon.
Definition: triangulator3.cxx:51
Triangulator3::add_vertex
int add_vertex(const LPoint3d &point)
Adds a new vertex to the vertex pool.
Definition: triangulator3.cxx:39
look_at.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
Triangulator::triangulate
void triangulate()
Does the work of triangulating the specified polygon.
Definition: triangulator.cxx:94