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// Authors: Daniel Funke, Lorenzo Moneta, Olivier Couet
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#include <map>
18#include <vector>
19#include <set>
20#include <functional>
21
22
23namespace ROOT {
24
25
26
27 namespace Math {
28
29/**
30
31 Class to generate a Delaunay triangulation of a 2D set of points.
32 Algorithm based on [CDT](https://github.com/artem-ogre/CDT), a C++ library for
33 generating constraint or conforming Delaunay triangulations.
34
35 After having found the triangles using the above library, barycentric coordinates are used
36 to test whether a point is inside a triangle (inTriangle test) and for interpolation.
37 All this below is implemented in the DoInterpolateNormalized function.
38
39 Given triangle ABC and point P, P can be expressed by
40
41 P.x = la * A.x + lb * B.x + lc * C.x
42 P.y = la * A.y + lb * B.y + lc * C.y
43
44 with lc = 1 - la - lb
45
46 P.x = la * A.x + lb * B.x + (1-la-lb) * C.x
47 P.y = la * A.y + lb * B.y + (1-la-lb) * C.y
48
49 Rearranging yields
50
51 la * (A.x - C.x) + lb * (B.x - C.x) = P.x - C.x
52 la * (A.y - C.y) + lb * (B.y - C.y) = P.y - C.y
53
54 Thus
55
56 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) )
57 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) )
58 lc = 1 - la - lb
59
60 We save the inverse denominator to speedup computation
61
62 invDenom = 1 / ( (B.y - C.y)*(A.x - C.x) + (C.x - B.x)*(A.y - C.y) )
63
64 P is in triangle (including edges if
65
66 0 <= [la, lb, lc] <= 1
67
68 The interpolation of P.z is
69
70 P.z = la * A.z + lb * B.z + lc * C.z
71
72 To speed up localisation of points (to see to which triangle belong) a grid is laid over the internal coordinate space.
73 A reference to triangle ABC is added to _all_ grid cells that include ABC's bounding box.
74 The size of the grid is defined to be 25x25
75
76 \ingroup MathCore
77 */
78
79
81
82public:
83
84 struct Triangle {
85 double x[3]; // x of triangle vertices
86 double y[3]; // y of triangle vertices
87 unsigned int idx[3]; // point corresponding to vertices
88 double invDenom; // cached inv denominator for computing barycentric coordinates (see above)
89 };
90
91 typedef std::vector<Triangle> Triangles;
92
93public:
94
95
96 Delaunay2D(int n, const double *x, const double * y, const double * z, double xmin=0, double xmax=0, double ymin=0, double ymax=0);
97
98 /// set the input points for building the graph
99 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);
100
101 /// Return the Interpolated z value corresponding to the given (x,y) point.
102 /// Note that in case no Delaunay triangles are found, for example when the
103 /// points are aligned, then a default value of zero is always return.
104 /// See the class documentation for how the interpolation is computed.
105 double Interpolate(double x, double y);
106
107 /// Find all triangles
108 void FindAllTriangles();
109
110 /// return the number of triangles
111 int NumberOfTriangles() const {return fNdt;}
112
113 double XMin() const {return fXNmin;}
114 double XMax() const {return fXNmax;}
115 double YMin() const {return fYNmin;}
116 double YMax() const {return fYNmax;}
117
118 /// set z value to be returned for points outside the region
119 void SetZOuterValue(double z=0.) { fZout = z; }
120
121 /// return the user defined Z-outer value
122 double ZOuterValue() const { return fZout; }
123
124 // iterators on the found triangles
125 Triangles::const_iterator begin() const { return fTriangles.begin(); }
126 Triangles::const_iterator end() const { return fTriangles.end(); }
127
128
129private:
130
131 // internal methods
132
133
134 inline double Linear_transform(double x, double offset, double factor){
135 return (x+offset)*factor;
136 }
137
138 /// internal function to normalize the points.
139 /// See the class documentation for the details on how it is computed.
140 void DoNormalizePoints();
141
142 /// internal function to find the triangle
143 void DoFindTriangles();
144
145 /// internal method to compute the interpolation
146 double DoInterpolateNormalized(double x, double y);
147
148
149
150private:
151 // class is not copyable
152 Delaunay2D(const Delaunay2D&); // Not implemented
153 Delaunay2D& operator=(const Delaunay2D&); // Not implemented
154
155protected:
156
157 int fNdt; ///<! Number of Delaunay triangles found
158 int fNpoints; ///<! Number of data points
159
160 const double *fX; ///<! Pointer to X array (managed externally)
161 const double *fY; ///<! Pointer to Y array
162 const double *fZ; ///<! Pointer to Z array
163
164 double fXNmin; ///<! Minimum value of fXN
165 double fXNmax; ///<! Maximum value of fXN
166 double fYNmin; ///<! Minimum value of fYN
167 double fYNmax; ///<! Maximum value of fYN
168
169 double fOffsetX; ///<! Normalization offset X
170 double fOffsetY; ///<! Normalization offset Y
171
172 double fScaleFactorX; ///<! Normalization factor X
173 double fScaleFactorY; ///<! Normalization factor Y
174
175 double fZout; ///<! Height for points lying outside the convex hull
176
177 bool fInit; ///<! True if FindAllTriangles() has been performed
178
179
180 Triangles fTriangles; ///<! Triangles of Triangulation
181
182 std::vector<double> fXN; ///<! normalized X
183 std::vector<double> fYN; ///<! normalized Y
184
185 static const int fNCells = 25; ///<! number of cells to divide the normalized space
186 double fXCellStep; ///<! inverse denominator to calculate X cell = fNCells / (fXNmax - fXNmin)
187 double fYCellStep; ///<! inverse denominator to calculate X cell = fNCells / (fYNmax - fYNmin)
188 std::set<unsigned int> fCells[(fNCells+1)*(fNCells+1)]; ///<! grid cells with containing triangles
189
190 inline unsigned int Cell(unsigned int x, unsigned int y) const {
191 return x*(fNCells+1) + y;
192 }
193
194 inline int CellX(double x) const {
195 return (x - fXNmin) * fXCellStep;
196 }
197
198 inline int CellY(double y) const {
199 return (y - fYNmin) * fYCellStep;
200 }
201
202};
203
204
205} // namespace Math
206} // namespace ROOT
207
208
209#endif
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
float xmin
float ymin
float xmax
float ymax
Class to generate a Delaunay triangulation of a 2D set of points.
Definition Delaunay2D.h:80
Triangles::const_iterator end() const
Definition Delaunay2D.h:126
int fNpoints
! Number of data points
Definition Delaunay2D.h:158
double fXNmin
! Minimum value of fXN
Definition Delaunay2D.h:164
std::set< unsigned int > fCells[(fNCells+1) *(fNCells+1)]
! grid cells with containing triangles
Definition Delaunay2D.h:188
int fNdt
! Number of Delaunay triangles found
Definition Delaunay2D.h:157
double DoInterpolateNormalized(double x, double y)
internal method to compute the interpolation
int CellY(double y) const
Definition Delaunay2D.h:198
Triangles::const_iterator begin() const
Definition Delaunay2D.h:125
Delaunay2D(const Delaunay2D &)
double Interpolate(double x, double y)
Return the Interpolated z value corresponding to the given (x,y) point.
const double * fZ
! Pointer to Z array
Definition Delaunay2D.h:162
double fXNmax
! Maximum value of fXN
Definition Delaunay2D.h:165
void SetZOuterValue(double z=0.)
set z value to be returned for points outside the region
Definition Delaunay2D.h:119
double fZout
! Height for points lying outside the convex hull
Definition Delaunay2D.h:175
double YMin() const
Definition Delaunay2D.h:115
double fOffsetY
! Normalization offset Y
Definition Delaunay2D.h:170
Delaunay2D & operator=(const Delaunay2D &)
double XMax() const
Definition Delaunay2D.h:114
double ZOuterValue() const
return the user defined Z-outer value
Definition Delaunay2D.h:122
Triangles fTriangles
! Triangles of Triangulation
Definition Delaunay2D.h:180
double Linear_transform(double x, double offset, double factor)
Definition Delaunay2D.h:134
double fYCellStep
! inverse denominator to calculate X cell = fNCells / (fYNmax - fYNmin)
Definition Delaunay2D.h:187
std::vector< double > fYN
! normalized Y
Definition Delaunay2D.h:183
Delaunay2D(int n, const double *x, const double *y, const double *z, double xmin=0, double xmax=0, double ymin=0, double ymax=0)
class constructor from array of data points
void FindAllTriangles()
Find all triangles.
static const int fNCells
! number of cells to divide the normalized space
Definition Delaunay2D.h:185
double YMax() const
Definition Delaunay2D.h:116
bool fInit
! True if FindAllTriangles() has been performed
Definition Delaunay2D.h:177
unsigned int Cell(unsigned int x, unsigned int y) const
Definition Delaunay2D.h:190
std::vector< double > fXN
! normalized X
Definition Delaunay2D.h:182
double fXCellStep
! inverse denominator to calculate X cell = fNCells / (fXNmax - fXNmin)
Definition Delaunay2D.h:186
double fYNmin
! Minimum value of fYN
Definition Delaunay2D.h:166
double fOffsetX
! Normalization offset X
Definition Delaunay2D.h:169
double XMin() const
Definition Delaunay2D.h:113
std::vector< Triangle > Triangles
Definition Delaunay2D.h:91
const double * fY
! Pointer to Y array
Definition Delaunay2D.h:161
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:160
void DoNormalizePoints()
internal function to normalize the points.
double fYNmax
! Maximum value of fYN
Definition Delaunay2D.h:167
int NumberOfTriangles() const
return the number of triangles
Definition Delaunay2D.h:111
double fScaleFactorY
! Normalization factor Y
Definition Delaunay2D.h:173
double fScaleFactorX
! Normalization factor X
Definition Delaunay2D.h:172
void DoFindTriangles()
internal function to find the triangle
int CellX(double x) const
Definition Delaunay2D.h:194
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.