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

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