2D ‘& 3D Surface Contour Map

xingyun86 2022-3-16 1336

2D ‘& 3D Surface Contour Map


Environment: VC6

A year and a half year ago, I published this article to the Codeguru site and got a number of requests about the Kriging algorithm contour map. Unfortunately, my project was changed shortly after that article and later I quit the company so I couldn’t find time to finish this Contour business. A week ago, I happened to need a contour map again so I decided to solve the Kriging algorithm. I searched the Internet for a commercial library but they all look ugly and hard to use. So, I made up my mind to make my own algorithm. The Kriging algorithm is easy to find, but this algorithm needs a Matrix and solver (LU-Decomposition). Again, I couldn’t find suitable code for this. I tried to use GSL first but this made my code too big and was slower. Finally, I went back to “Numerical Recipe in C”—yes, that horrible-looking C code—and changed the code there to my taste.

If you read this article before, the rendering part hasn’t been changed much. I added the Kriging algorithm and revised the codes a little bit. Following is the Kriging Algorithm:

template<class ForwardIterator>
double GetDistance(const ForwardIterator start, int i, int j)
{

  return ::sqrt(::pow(((*(start+i)).x – (*(start+j)).x), 2) +

                ::pow(((*(start+i)).y – (*(start+j)).y), 2));
}

Because of the template, this doesn’t look that clean but you can get the idea if you look at it carefully. The matrix solver is as follows:

template<class T>
void LUDecompose(TMatrix<T>& A, std::vector<int>&

                 Permutation, int& d) throw(NumericException)
{

  int n = A.GetHeight();

  vector<T> vv(n);

  Permutation.resize(n);

By using this algorithm, making a 3D grid is easy. Let’s assume we’re making a 200×200 grid and we have some scattered data. Then, what we need to do is this:


  vector<Point3D> input

If you have all the grid points with 3D data, you can make a bitmap file with it, or make a triangle strip to render with OpenGL. If you remember that the old contour map was produced from an InverseDistanced algorithm (you can switch to Inverse Distance in the Option menu), you’ll find a vast improvement over it. I compared the Kriging generated contour map with some commercial programs, and they were almost identical. I hope this helps programmers who want to make a contour map.

Downloads


Download demo project – 221 Kb


To use this project, open one of the “*.txt” files first, then click the “2D” or “3D” button in the toolbar.


上传的附件:
×
打赏作者
最新回复 (0)
只看楼主
全部楼主
返回