OpendTect-6_4  6.4
delaunay.h
Go to the documentation of this file.
1 #ifndef delaunay_h
2 #define delaunay_h
3 
4 /*+
5 ________________________________________________________________________
6 
7  (C) dGB Beheer B.V.; (LICENSE) http://opendtect.org/OpendTect_license.txt
8  Author: Y.C. Liu
9  Date: January 2008
10  RCS: $Id$
11 ________________________________________________________________________
12 
13 -*/
14 
15 #include "algomod.h"
16 #include "coord.h"
17 #include "odmemory.h"
18 #include "typeset.h"
19 #include "thread.h"
20 #include "trigonometry.h"
21 class od_ostream;
22 
23 
24 #define mDAGTriangleForceSingleThread
25 
37 {
38 public:
39  DAGTriangleTree();
40  DAGTriangleTree(const DAGTriangleTree&);
41  virtual ~DAGTriangleTree();
42  DAGTriangleTree& operator=(const DAGTriangleTree&);
43 
44  static bool computeCoordRanges(const TypeSet<Coord>&,
46 
47  bool setCoordList(const TypeSet<Coord>* OD);
48  bool setCoordList(TypeSet<Coord>*,OD::PtrPolicy);
49  const TypeSet<Coord>& coordList() const { return *coordlist_; }
50 
51  bool setBBox(const Interval<double>& xrg,
52  const Interval<double>& yrg);
53 
54  bool isOK() const { return triangles_.size(); }
55 
56  bool init();
57 
58  bool insertPoint(int pointidx, int& dupid);
59  int insertPoint(const Coord&, int& dupid);
60 
61  const Coord getInitCoord(int vetexidx) const;
62  bool getTriangle(const Coord&,int& dupid,
63  TypeSet<int>& vertexindices) const;
65  bool getCoordIndices(TypeSet<int>&) const;
69  bool getSurroundingIndices(TypeSet<int>&) const;
72  bool getConnections(int pointidx,TypeSet<int>&) const;
73  bool getWeights(int pointidx,const TypeSet<int>& conns,
74  TypeSet<double>& weights,
75  bool normalize=true) const;
77  bool getConnectionAndWeights(int ptidx,TypeSet<int>& conns,
78  TypeSet<double>& weights,
79  bool normailze=true) const;
80  void setEpsilon(double err) { epsilon_ = err; }
81 
82  void dumpTo(od_ostream&) const;
84  void dumpTriangulationToIV(od_ostream&) const;
85 
86 
87  static int cNoVertex() { return -1; }
88 
89 protected:
90 
91  static char cIsOutside() { return 0; }
92  static char cIsInside() { return 1; }
93  static char cIsDuplicate() { return 2; }
94  static char cError() { return -1; }
95 
96  static int cNoTriangle() { return -1; }
97  static int cInitVertex0() { return -2; }
98  static int cInitVertex1() { return -3; }
99  static int cInitVertex2() { return -4; }
100 
101  char searchTriangle(const Coord& pt,int start,int& t0,
102  int& dupid) const;
103  char searchFurther(const Coord& pt,int& nti0,int& dupid) const;
104 
105  void splitTriangleInside(int ci,int ti);
107  void legalizeTriangles(TypeSet<char>& v0s,TypeSet<char>& v1s,
108  TypeSet<int>& tis);
112  int getNeighbor(int v0,int v1,int ti) const;
113  int searchChild(int v0,int v1,int ti) const;
114  char isInside(const Coord& pt,int ti,int& dupid) const;
115 
116  struct DAGTriangle
117  {
118  DAGTriangle();
119  bool operator==(const DAGTriangle&) const;
120  DAGTriangle& operator=(const DAGTriangle&);
121 
122  int coordindices_[3];
123  int childindices_[3];
124  int neighbors_[3];
125 
126  bool hasChildren() const;
127  };
128 
130 
132 
133  double epsilon_;
135 
138 
140 
141  Coord initialcoords_[3];
143 };
144 
145 
152 public:
155 
156  bool isDataRandom() { return israndom_; }
157  void dataIsRandom(bool yn) { israndom_ = yn; }
158  void setCalcScope(const Interval<int>& rg);
159 
160 protected:
161 
162 #ifdef mDAGTriangleForceSingleThread
163  int maxNrThreads() const { return 1; }
164 #endif
165  od_int64 nrIterations() const;
166  bool doWork(od_int64,od_int64, int );
167  bool doPrepare(int);
168 
170  { return tr("Points triangulated"); }
171  uiString uiMessage() const { return tr("Triangulating"); }
172 
174  bool israndom_;
177 };
178 
179 
188 {
189 public:
190  Triangle2DInterpolator(const DAGTriangleTree&);
191 
192  /*The vertices are indices from the DAGTriangleTree
193  coordlist, corresponding to the weights.*/
194  bool computeWeights(const Coord&,TypeSet<int>& vertices,
195  TypeSet<double>& weights,
196  double maxdist=mUdf(double),
197  bool dointerpolate=true);
198  /*If don't do interpolate, nearest node will be taken.*/
199 
200 protected:
201 
202  bool setFromAzimuth(const TypeSet<int>& tmpvertices,
203  const Coord&,
204  TypeSet<int>& vertices,
205  TypeSet<double>& weights);
214 
217 };
218 
219 
220 /*Simple polyon triangulation, does not work if you have holes inside it.
221  return each triangle with three indicies in order. */
222 inline bool PolygonTriangulate( const TypeSet<Coord>& knots,TypeSet<int>& res )
223 {
224  const int nrknots = knots.size();
225  if ( nrknots < 3 )
226  return false;
227 
228  /* Make sure it is a counter-clockwise polygon in ci */
229  double area=0;
230  for( int idx=nrknots-1, idy=0; idy<nrknots; idx=idy++ )
231  area += (knots[idx].x*knots[idy].y - knots[idy].x*knots[idx].y);
232  area *= 0.5;
233 
234  TypeSet<int> ci;
235  if ( 0<area )
236  {
237  for ( int idx=0; idx<nrknots; idx++ )
238  ci += idx;
239  }
240  else
241  {
242  for( int idx=0; idx<nrknots; idx++ )
243  ci += (nrknots-1-idx);
244  }
245 
246  /*Triangulate: three consecutive vertices (idx0,idx,idx1) in polygon,
247  remove cursize-2 Vertices, creating 1 triangle every time */
248  int cursize = nrknots;
249  int errcheck = 2*cursize;
250 
251  for( int idx=cursize-1; cursize>2; )
252  {
253  if ( 0 >= (errcheck--) )
254  return false;
255 
256  const int idx0 = cursize<=idx ? 0 : idx;
257  idx = cursize<=idx0+1 ? 0 : idx0+1;
258  const int idx1 = cursize<=idx+1 ? 0 : idx+1;
259 
260  const Coord& pos0 = knots[ci[idx0]];
261  const Coord& pos = knots[ci[idx]];
262  const Coord& pos1 = knots[ci[idx1]];
263  if ( (((pos.x-pos0.x)*(pos1.y-pos0.y)) -
264  ((pos.y-pos0.y)*(pos1.x-pos0.x)))<0 )
265  continue;
266 
267  bool isvalid = true;
268  for ( int idy=0; idy<cursize; idy++ )
269  {
270  if( (idy==idx0) || (idy==idx) || (idy==idx1) )
271  continue;
272 
273  if ( pointInTriangle2D(knots[ci[idy]],pos0,pos,pos1,0.0) )
274  {
275  isvalid = false;
276  break;
277  }
278  }
279 
280  if ( isvalid )
281  {
282  res += ci[idx0];
283  res += ci[idx];
284  res += ci[idx1];
285 
286  /* remove idx from remaining polygon */
287  for( int i=idx, j=idx+1; j<cursize; i++, j++ )
288  ci[i] = ci[j];
289 
290  cursize--;
291  errcheck = 2*cursize;
292  }
293  }
294 
295  return true;
296 }
297 
298 #endif
#define mExpClass(module)
Definition: commondefs.h:160
TypeSet< int > perimeter_
Definition: delaunay.h:215
Lock that permits multiple readers to lock the object at the same time, but it will not allow any rea...
Definition: thread.h:141
OD class for stream write common access to the user log file, or std::cout in batch progs...
Definition: od_ostream.h:26
Reference: "Parallel Incremental Delaunay Triangulation", by Kohout J.2005.
Definition: delaunay.h:36
TypeSet< double > cornerweights1_
Definition: delaunay.h:210
bool operator==(const ArrayNDInfo &a1, const ArrayNDInfo &a2)
Definition: arrayndinfo.h:53
bool isOK() const
Definition: delaunay.h:54
#define mODTextTranslationClass(clss)
Definition: uistring.h:38
#define od_int64
Definition: plftypes.h:36
int maxNrThreads() const
Definition: delaunay.h:163
bool init()
Interval< od_int64 > calcscope_
Definition: delaunay.h:176
TypeSet< DAGTriangle > triangles_
Definition: delaunay.h:134
Definition: uistring.h:89
Threads::ReadWriteLock coordlock_
Definition: delaunay.h:139
static int cNoTriangle()
Definition: delaunay.h:96
static char cIsOutside()
Definition: delaunay.h:91
A cartesian coordinate in 2D space.
Definition: coord.h:25
TypeSet< Coord > * coordlist_
Definition: delaunay.h:136
bool ownscoordlist_
Definition: delaunay.h:137
Generalization of a task that can be run in parallel.
Definition: paralleltask.h:66
double epsilon_
Definition: delaunay.h:133
TypeSet< double > cornerweights2_
Definition: delaunay.h:212
od_int64 * permutation_
Definition: delaunay.h:173
static char cError()
Definition: delaunay.h:94
TypeSet< double > cornerweights0_
Definition: delaunay.h:208
#define mUdf(type)
Use this macro to get the undefined for simple types.
Definition: undefval.h:272
T y
Definition: geometry.h:67
void setEpsilon(double err)
Definition: delaunay.h:80
DAGTriangleTree & tree_
Definition: delaunay.h:175
static int cNoVertex()
Definition: delaunay.h:87
bool PolygonTriangulate(const TypeSet< Coord > &knots, TypeSet< int > &res)
Definition: delaunay.h:222
static int cInitVertex1()
Definition: delaunay.h:98
void dataIsRandom(bool yn)
Definition: delaunay.h:157
The parallel triangulation works for only one processor now.
Definition: delaunay.h:150
static char cIsInside()
Definition: delaunay.h:92
const DAGTriangleTree & triangles_
Definition: delaunay.h:206
Definition: delaunay.h:116
Coord initcenter_
Definition: delaunay.h:213
size_type size() const
Definition: typeset.h:254
bool israndom_
Definition: delaunay.h:174
od_ostream & operator=(const od_ostream &s)
Definition: od_ostream.h:41
bool pointInTriangle2D(const Coord &p, const Coord &a, const Coord &b, const Coord &c, double epsilon)
Definition: trigonometry.h:187
TypeSet< int > corner0_
Definition: delaunay.h:207
T x
Definition: geometry.h:66
uiString uiMessage() const
will be message() again in 7.x
Definition: delaunay.h:171
OpendTect.
Definition: commontypes.h:29
PtrPolicy
Definition: odmemory.h:22
TypeSet< int > corner2_
Definition: delaunay.h:211
static char cIsDuplicate()
Definition: delaunay.h:93
const TypeSet< Coord > & coordList() const
Definition: delaunay.h:49
TypeSet< int > corner1_
Definition: delaunay.h:209
static int cInitVertex2()
Definition: delaunay.h:99
Threads::ReadWriteLock trianglelock_
Definition: delaunay.h:131
uiString uiNrDoneText() const
will be nrDoneText() in 7.x
Definition: delaunay.h:169
TypeSet< double > perimeterazimuth_
Definition: delaunay.h:216
bool isDataRandom()
Definition: delaunay.h:156
static int cInitVertex0()
Definition: delaunay.h:97
bool multithreadsupport_
Definition: delaunay.h:129
For a given triangulated geometry(set of points), interpolating any point located in or nearby the go...
Definition: delaunay.h:187

Generated at for the OpendTect seismic interpretation project. Copyright (C): dGB Beheer B. V. 2019