Rocstar  1.0
Rocstar multiphysics simulation application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Triangulation.h
Go to the documentation of this file.
1 /* *******************************************************************
2  * Rocstar Simulation Suite *
3  * Copyright@2015, Illinois Rocstar LLC. All rights reserved. *
4  * *
5  * Illinois Rocstar LLC *
6  * Champaign, IL *
7  * www.illinoisrocstar.com *
8  * sales@illinoisrocstar.com *
9  * *
10  * License: See LICENSE file in top level of distribution package or *
11  * http://opensource.org/licenses/NCSA *
12  *********************************************************************/
13 /* *******************************************************************
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, *
15  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES *
16  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND *
17  * NONINFRINGEMENT. IN NO EVENT SHALL THE CONTRIBUTORS OR *
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
20  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE *
21  * USE OR OTHER DEALINGS WITH THE SOFTWARE. *
22  *********************************************************************/
23 // $Id: Triangulation.h,v 1.9 2008/12/06 08:43:27 mtcampbe Exp $
24 
25 // This file implements the ear-removal algorithm for triangulating a
26 // convex polygon described in "Computational Geometry in C".
27 
28 #ifndef TRIANGULATION_H
29 #define TRIANGULATION_H
30 
31 #include "rfc_basic.h"
32 #include "Tuple.h"
36 #include <vector>
37 #include <list>
38 
40 
42 
51 public:
52  typedef CGAL::PointS2<Real> PointS2;
53  typedef CGAL::Simple_cartesian<Real>::Segment_2 SegmentS2;
54 
55 private:
56  // A helper class for tracking information about a node of the polygon.
57  class Node {
58  short int _id;
59  bool _ear;
60  public:
62  explicit Node( short int i) : _id(i), _ear(false) {}
63 
64  int id() const { return _id; }
65  bool is_ear() const { return _ear; }
66  void set_ear( bool b) { _ear = b; }
67  };
68 
69  typedef std::list< Node> Node_list;
70  typedef Node_list::iterator Node_iterator;
71  typedef Node_list::const_iterator Node_const_iterator;
72 public:
75  typedef std::vector< Triangle> Connectivity;
77 
80 
82 
87  void triangulate( const Point_2 *ps, int n, Connectivity *tri) {
88  _pnts = ps;
89  _nodes.clear();
90  for ( int i=0; i<n; ++i) _nodes.push_back( Node(i));
91 
92  // Initialize ears
93  for ( Node_iterator it=_nodes.begin(); it!=_nodes.end(); ++it) {
94  it->set_ear( is_diagonal( get_prev(it), get_next(it)));
95  }
96 
97  tri->resize(0); tri->reserve( n-2);
98  // Loop through to remove ears.
99  for ( ; n>3; --n) {
100  // The inner loop searches for an ear
101  for ( Node_iterator it=_nodes.begin(); it!=_nodes.end(); ++it) {
102  if ( it->is_ear()) {
103  Node_iterator v1=get_prev( it), v0=get_prev( v1);
104  Node_iterator v3=get_next( it), v4=get_next( v3);
105 
106  // Output the ear
107  tri->push_back( Triangle( v1->id(), it->id(), v3->id()));
108 
109  // update neighbor vertices
110  v1->set_ear( is_diagonal( v0, v3));
111  v3->set_ear( is_diagonal( v1, v4));
112 
113  // Cut off the ear
114  _nodes.erase( it);
115  break;
116  }
117  }
118  }
119  RFC_assertion( n==3);
120 
121  tri->push_back( Triangle( _nodes.front().id(), (++_nodes.begin())->id(),
122  _nodes.back().id()));
123  _nodes.clear();
124  }
125 
126 private:
127  //============= Helper subroutines
128  const PointS2 &get_point(const Node &v) const { return (PointS2&)_pnts[v.id()]; }
129 
131  return ( ++i==_nodes.end()) ? _nodes.begin() : i;
132  }
134  if ( i==_nodes.begin()) i=_nodes.end();
135  return --i;
136  }
137 
139  return ( ++i==_nodes.end()) ? _nodes.begin() : i;
140  }
142  if ( i==_nodes.begin()) i=_nodes.end();
143  return --i;
144  }
145 
147  return in_cone( u, v) && in_cone( v, u) && is_diagonalie( u, v);
148  }
149 
151  Node_const_iterator a0 = get_prev( u);
152  Node_const_iterator a1 = get_next( u);
153 
154  if ( !CGAL::rightturn( get_point(*u), get_point(*a1), get_point(*a0))) {
155  return ( CGAL::leftturn(get_point(*u), get_point(*v), get_point(*a0)) &&
156  CGAL::leftturn(get_point(*v), get_point(*u), get_point(*a1)));
157  }
158 
159  return ( ! CGAL::rightturn(get_point(*v), get_point(*u), get_point(*a1)) &&
160  ! CGAL::rightturn(get_point(*a0), get_point(*u), get_point(*v)));
161  }
162 
164  for ( Node_const_iterator i=_nodes.begin(); i!=_nodes.end(); ++i) {
166 
167  if ( u!=i && u!=j && v!=i && v!=j &&
169  SegmentS2( get_point(*i), get_point(*j))))
170  return false;
171  }
172  return true;
173  }
174 
175 private: // Data members
176  const Point_2 *_pnts;
178 
179 };
180 
182 
183 #endif
184 
185 
186 
187 
188 
189 
std::list< Node > Node_list
Definition: Triangulation.h:69
Triangulation()
A constructor.
Definition: Triangulation.h:79
Node_list _nodes
bool is_diagonalie(Node_const_iterator u, Node_const_iterator v) const
Node_const_iterator get_next(Node_const_iterator i) const
bool in_cone(Node_const_iterator u, Node_const_iterator v) const
void triangulate(const Point_2 *ps, int n, Connectivity *tri)
Main entry for triangulation.
Definition: Triangulation.h:87
Node_list::const_iterator Node_const_iterator
Definition: Triangulation.h:71
CGAL::Simple_cartesian< Real >::Segment_2 SegmentS2
Definition: Triangulation.h:53
bool do_intersect(const Segment_2< R > &seg1, const Segment_2< R > &seg2)
bool is_ear() const
Definition: Triangulation.h:65
*********************************************************************Illinois Open Source License ****University of Illinois NCSA **Open Source License University of Illinois All rights reserved ****Developed free of to any person **obtaining a copy of this software and associated documentation to deal with the Software without including without limitation the rights to and or **sell copies of the and to permit persons to whom the **Software is furnished to do subject to the following this list of conditions and the following disclaimers ****Redistributions in binary form must reproduce the above **copyright this list of conditions and the following **disclaimers in the documentation and or other materials **provided with the distribution ****Neither the names of the Center for Simulation of Advanced the University of nor the names of its **contributors may be used to endorse or promote products derived **from this Software without specific prior written permission ****THE SOFTWARE IS PROVIDED AS WITHOUT WARRANTY OF ANY **EXPRESS OR INCLUDING BUT NOT LIMITED TO THE WARRANTIES **OF FITNESS FOR A PARTICULAR PURPOSE AND **NONINFRINGEMENT IN NO EVENT SHALL THE CONTRIBUTORS OR **COPYRIGHT HOLDERS BE LIABLE FOR ANY DAMAGES OR OTHER WHETHER IN AN ACTION OF TORT OR **ARISING OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE **USE OR OTHER DEALINGS WITH THE SOFTWARE v
Definition: roccomf90.h:20
const PointS2 & get_point(const Node &v) const
#define RFC_END_NAME_SPACE
Definition: rfc_basic.h:29
bool rightturn(const Point_2< R > &p, const Point_2< R > &q, const Point_2< R > &r)
const Point_2 * _pnts
Node_const_iterator get_prev(Node_const_iterator i) const
Three_tuple< int > Triangle
Connectivity for a triangle.
Definition: Triangulation.h:73
blockLoc i
Definition: read.cpp:79
#define RFC_BEGIN_NAME_SPACE
Definition: rfc_basic.h:28
void set_ear(bool b)
Definition: Triangulation.h:66
Triangulating a convex polygon by ear-removal.
Definition: Triangulation.h:50
const NT & n
bool is_diagonal(Node_const_iterator u, Node_const_iterator v) const
CGAL::PointS2< Real > PointS2
Definition: Triangulation.h:52
Node_iterator get_prev(Node_iterator i)
j indices j
Definition: Indexing.h:6
Node_iterator get_next(Node_iterator i)
bool leftturn(const Point_2< R > &p, const Point_2< R > &q, const Point_2< R > &r)
Node_list::iterator Node_iterator
Definition: Triangulation.h:70
std::vector< Triangle > Connectivity
Element connectivity table.
Definition: Triangulation.h:75
#define RFC_assertion
Definition: rfc_basic.h:65
Node(short int i)
A constructor.
Definition: Triangulation.h:62