Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
Delaunay2D.h
Go to the documentation of this file.
1// @(#)root/mathcore:$Id: Delaunay2D.h,v 1.00
2// Author: Daniel Funke, Lorenzo Moneta
3
4/*************************************************************************
5 * Copyright (C) 2015 ROOT Math Team *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12// Header file for class Delaunay2D
13
14#ifndef ROOT_Math_Delaunay2D
15#define ROOT_Math_Delaunay2D
16
17//for testing purposes HAS_CGAL can be [un]defined here
18//#define HAS_CGAL
19
20//for testing purposes THREAD_SAFE can [un]defined here
21//#define THREAD_SAFE
22
23
24#include "RtypesCore.h"
25
26#include <map>
27#include <vector>
28#include <set>
29#include <functional>
30
31#ifdef HAS_CGAL
32 /* CGAL uses the name PTR as member name in its Handle class
33 * but its a macro defined in mmalloc.h of ROOT
34 * Safe it, disable it and then re-enable it later on*/
35 #pragma push_macro("PTR")
36 #undef PTR
37
38 #include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
39 #include <CGAL/Delaunay_triangulation_2.h>
40 #include <CGAL/Triangulation_vertex_base_with_info_2.h>
41 #include <CGAL/Interpolation_traits_2.h>
42 #include <CGAL/natural_neighbor_coordinates_2.h>
43 #include <CGAL/interpolation_functions.h>
44
45 #pragma pop_macro("PTR")
46#endif
47
48#ifdef THREAD_SAFE
49 #include<atomic> //atomic operations for thread safety
50#endif
51
52
53namespace ROOT {
54
55
56
57 namespace Math {
58
59/**
60
61 Class to generate a Delaunay triangulation of a 2D set of points.
62 Algorithm based on **Triangle**, a two-dimensional quality mesh generator and
63 Delaunay triangulator from Jonathan Richard Shewchuk.
64
65 See [http://www.cs.cmu.edu/~quake/triangle.html]
66
67 \ingroup MathCore
68 */
69
70
72
73public:
74
75 struct Triangle {
76 double x[3];
77 double y[3];
79
80 #ifndef HAS_CGAL
81 double invDenom; //see comment below in CGAL fall back section
82 #endif
83 };
84
85 typedef std::vector<Triangle> Triangles;
86
87public:
88
89
90 Delaunay2D(int n, const double *x, const double * y, const double * z, double xmin=0, double xmax=0, double ymin=0, double ymax=0);
91
92 /// set the input points for building the graph
93 void SetInputPoints(int n, const double *x, const double * y, const double * z, double xmin=0, double xmax=0, double ymin=0, double ymax=0);
94
95 /// Return the Interpolated z value corresponding to the given (x,y) point
96 /// Note that in case no Delaunay triangles are found, for example when the
97 /// points are aligned, then a default value of zero is always return
98 double Interpolate(double x, double y);
99
100 /// Find all triangles
101 void FindAllTriangles();
102
103 /// return the number of triangles
104 Int_t NumberOfTriangles() const {return fNdt;}
105
106 double XMin() const {return fXNmin;}
107 double XMax() const {return fXNmax;}
108 double YMin() const {return fYNmin;}
109 double YMax() const {return fYNmax;}
110
111 /// set z value to be returned for points outside the region
112 void SetZOuterValue(double z=0.) { fZout = z; }
113
114 /// return the user defined Z-outer value
115 double ZOuterValue() const { return fZout; }
116
117 // iterators on the found triangles
118 Triangles::const_iterator begin() const { return fTriangles.begin(); }
119 Triangles::const_iterator end() const { return fTriangles.end(); }
120
121
122private:
123
124 // internal methods
125
126
127 inline double Linear_transform(double x, double offset, double factor){
128 return (x+offset)*factor;
129 }
130
131 /// internal function to normalize the points
132 void DoNormalizePoints();
133
134 /// internal function to find the triangle
135 /// use Triangle or CGAL if flag is set
136 void DoFindTriangles();
137
138 /// internal method to compute the interpolation
139 double DoInterpolateNormalized(double x, double y);
140
141
142
143private:
144 // class is not copyable
145 Delaunay2D(const Delaunay2D&); // Not implemented
146 Delaunay2D& operator=(const Delaunay2D&); // Not implemented
147
148protected:
149
150 Int_t fNdt; ///<! Number of Delaunay triangles found
151 Int_t fNpoints; ///<! Number of data points
152
153 const double *fX; ///<! Pointer to X array (managed externally)
154 const double *fY; ///<! Pointer to Y array
155 const double *fZ; ///<! Pointer to Z array
156
157 double fXNmin; ///<! Minimum value of fXN
158 double fXNmax; ///<! Maximum value of fXN
159 double fYNmin; ///<! Minimum value of fYN
160 double fYNmax; ///<! Maximum value of fYN
161
162 double fOffsetX; ///<! Normalization offset X
163 double fOffsetY; ///<! Normalization offset Y
164
165 double fScaleFactorX; ///<! Normalization factor X
166 double fScaleFactorY; ///<! Normalization factor Y
167
168 double fZout; ///<! Height for points lying outside the convex hull
169
170#ifdef THREAD_SAFE
171
172 enum class Initialization : char {UNINITIALIZED, INITIALIZING, INITIALIZED};
173 std::atomic<Initialization> fInit; ///<! Indicate initialization state
174
175#else
176 Bool_t fInit; ///<! True if FindAllTriangles() has been performed
177#endif
178
179
180 Triangles fTriangles; ///<! Triangles of Triangulation
181
182#ifdef HAS_CGAL
183
184 //Functor class for accessing the function values/gradients
185 template< class PointWithInfoMap, typename ValueType >
186 struct Data_access : public std::unary_function< typename PointWithInfoMap::key_type,
187 std::pair<ValueType, bool> >
188 {
189
190 Data_access(const PointWithInfoMap& points, const ValueType * values)
191 : _points(points), _values(values){};
192
193 std::pair< ValueType, bool>
194 operator()(const typename PointWithInfoMap::key_type& p) const {
195 typename PointWithInfoMap::const_iterator mit = _points.find(p);
196 if(mit!= _points.end())
197 return std::make_pair(_values[mit->second], true);
198 return std::make_pair(ValueType(), false);
199 };
200
201 const PointWithInfoMap& _points;
202 const ValueType * _values;
203 };
204
205 typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
206 typedef CGAL::Triangulation_vertex_base_with_info_2<uint, K> Vb;
207 typedef CGAL::Triangulation_data_structure_2<Vb> Tds;
208 typedef CGAL::Delaunay_triangulation_2<K, Tds> Delaunay;
209 typedef CGAL::Interpolation_traits_2<K> Traits;
210 typedef K::FT Coord_type;
211 typedef K::Point_2 Point;
212 typedef std::map<Point, Vb::Info, K::Less_xy_2> PointWithInfoMap;
213 typedef Data_access< PointWithInfoMap, double > Value_access;
214
215 Delaunay fCGALdelaunay; //! CGAL delaunay triangulation object
216 PointWithInfoMap fNormalizedPoints; //! Normalized function values
217
218#else // HAS_CGAL
219 //fallback to triangle library
220
221 /* Using barycentric coordinates for inTriangle test and interpolation
222 *
223 * Given triangle ABC and point P, P can be expressed by
224 *
225 * P.x = la * A.x + lb * B.x + lc * C.x
226 * P.y = la * A.y + lb * B.y + lc * C.y
227 *
228 * with lc = 1 - la - lb
229 *
230 * P.x = la * A.x + lb * B.x + (1-la-lb) * C.x
231 * P.y = la * A.y + lb * B.y + (1-la-lb) * C.y
232 *
233 * Rearranging yields
234 *
235 * la * (A.x - C.x) + lb * (B.x - C.x) = P.x - C.x
236 * la * (A.y - C.y) + lb * (B.y - C.y) = P.y - C.y
237 *
238 * Thus
239 *
240 * la = ( (B.y - C.y)*(P.x - C.x) + (C.x - B.x)*(P.y - C.y) ) / ( (B.y - C.y)*(A.x - C.x) + (C.x - B.x)*(A.y - C.y) )
241 * lb = ( (C.y - A.y)*(P.x - C.x) + (A.x - C.x)*(P.y - C.y) ) / ( (B.y - C.y)*(A.x - C.x) + (C.x - B.x)*(A.y - C.y) )
242 * lc = 1 - la - lb
243 *
244 * We save the inverse denominator to speedup computation
245 *
246 * invDenom = 1 / ( (B.y - C.y)*(A.x - C.x) + (C.x - B.x)*(A.y - C.y) )
247 *
248 * P is in triangle (including edges if
249 *
250 * 0 <= [la, lb, lc] <= 1
251 *
252 * The interpolation of P.z is
253 *
254 * P.z = la * A.z + lb * B.z + lc * C.z
255 *
256 */
257
258 std::vector<double> fXN; ///<! normalized X
259 std::vector<double> fYN; ///<! normalized Y
260
261 /* To speed up localisation of points a grid is layed over normalized space
262 *
263 * A reference to triangle ABC is added to _all_ grid cells that include ABC's bounding box
264 */
265
266 static const int fNCells = 25; ///<! number of cells to divide the normalized space
267 double fXCellStep; ///<! inverse denominator to calculate X cell = fNCells / (fXNmax - fXNmin)
268 double fYCellStep; ///<! inverse denominator to calculate X cell = fNCells / (fYNmax - fYNmin)
269 std::set<UInt_t> fCells[(fNCells+1)*(fNCells+1)]; ///<! grid cells with containing triangles
270
271 inline unsigned int Cell(UInt_t x, UInt_t y) const {
272 return x*(fNCells+1) + y;
273 }
274
275 inline int CellX(double x) const {
276 return (x - fXNmin) * fXCellStep;
277 }
278
279 inline int CellY(double y) const {
280 return (y - fYNmin) * fYCellStep;
281 }
282
283#endif //HAS_CGAL
284
285
286};
287
288
289} // namespace Math
290} // namespace ROOT
291
292
293#endif
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h offset
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t points
float xmin
float ymin
float xmax
float ymax
TRObject operator()(const T1 &t1) const
Class to generate a Delaunay triangulation of a 2D set of points.
Definition Delaunay2D.h:71
Triangles::const_iterator end() const
Definition Delaunay2D.h:119
double fXNmin
! Minimum value of fXN
Definition Delaunay2D.h:157
double DoInterpolateNormalized(double x, double y)
internal method to compute the interpolation
int CellY(double y) const
Definition Delaunay2D.h:279
Triangles::const_iterator begin() const
Definition Delaunay2D.h:118
Delaunay2D(const Delaunay2D &)
const double * fZ
! Pointer to Z array
Definition Delaunay2D.h:155
double fXNmax
! Maximum value of fXN
Definition Delaunay2D.h:158
void SetZOuterValue(double z=0.)
set z value to be returned for points outside the region
Definition Delaunay2D.h:112
double fZout
! Height for points lying outside the convex hull
Definition Delaunay2D.h:168
Int_t NumberOfTriangles() const
return the number of triangles
Definition Delaunay2D.h:104
double YMin() const
Definition Delaunay2D.h:108
double fOffsetY
! Normalization offset Y
Definition Delaunay2D.h:163
Delaunay2D & operator=(const Delaunay2D &)
double XMax() const
Definition Delaunay2D.h:107
Int_t fNdt
! Number of Delaunay triangles found
Definition Delaunay2D.h:150
double ZOuterValue() const
return the user defined Z-outer value
Definition Delaunay2D.h:115
Triangles fTriangles
! Triangles of Triangulation
Definition Delaunay2D.h:180
unsigned int Cell(UInt_t x, UInt_t y) const
Definition Delaunay2D.h:271
double Linear_transform(double x, double offset, double factor)
Definition Delaunay2D.h:127
double fYCellStep
! inverse denominator to calculate X cell = fNCells / (fYNmax - fYNmin)
Definition Delaunay2D.h:268
std::vector< double > fYN
! normalized Y
Definition Delaunay2D.h:259
void FindAllTriangles()
Find all triangles.
static const int fNCells
! number of cells to divide the normalized space
Definition Delaunay2D.h:266
double YMax() const
Definition Delaunay2D.h:109
std::vector< double > fXN
! normalized X
Definition Delaunay2D.h:258
double fXCellStep
! inverse denominator to calculate X cell = fNCells / (fXNmax - fXNmin)
Definition Delaunay2D.h:267
std::set< UInt_t > fCells[(fNCells+1) *(fNCells+1)]
! grid cells with containing triangles
Definition Delaunay2D.h:269
double fYNmin
! Minimum value of fYN
Definition Delaunay2D.h:159
double fOffsetX
! Normalization offset X
Definition Delaunay2D.h:162
Int_t fNpoints
! Number of data points
Definition Delaunay2D.h:151
double XMin() const
Definition Delaunay2D.h:106
std::vector< Triangle > Triangles
Definition Delaunay2D.h:85
Bool_t fInit
! True if FindAllTriangles() has been performed
Definition Delaunay2D.h:176
const double * fY
! Pointer to Y array
Definition Delaunay2D.h:154
void SetInputPoints(int n, const double *x, const double *y, const double *z, double xmin=0, double xmax=0, double ymin=0, double ymax=0)
set the input points for building the graph
const double * fX
! Pointer to X array (managed externally)
Definition Delaunay2D.h:153
void DoNormalizePoints()
internal function to normalize the points
double fYNmax
! Maximum value of fYN
Definition Delaunay2D.h:160
double fScaleFactorY
! Normalization factor Y
Definition Delaunay2D.h:166
double fScaleFactorX
! Normalization factor X
Definition Delaunay2D.h:165
void DoFindTriangles()
internal function to find the triangle use Triangle or CGAL if flag is set
int CellX(double x) const
Definition Delaunay2D.h:275
#define Interpolate(a, x, b, y)
Definition geom.c:179
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
const Int_t n
Definition legend1.C:16
Namespace for new Math classes and functions.
This file contains a specialised ROOT message handler to test for diagnostic in unit tests.
static const Int_t UNINITIALIZED
Definition TNeuron.cxx:41