There is a relatively simple type of contour tracing, that is, it involves not a very deep problem, that is, the rule-based Contour tracing grid described in this article.
First, let's talk about the formation of the contour. There are two ways to draw the contour. One is to make a complete measurement, that is, to find all the equivalent points in the actual construction, Mark X, y, value, there is also a way to use several prediction points to use some interpolation methods to form a regular grid, each grid point has coordinates and values, that is, the elevation value, then we can use the values on these grids to estimate the equivalent points. Obviously, the first method is not good and is often impractical; in this way, a series of contour tracing methods are formed. Here, there is also a gridded problem based on several observation points, there are also many areas worth studying,
When I was working on a project, I used the KNN interpolation algorithm. Of course there are many other algorithms, such as weighted Inverse Distance and minimum curvature. These algorithms are not easy to do,
You can also go to the Internet to find out what I did not do. It is also a shame that I did not do well with them. In the project, I also used the KNN algorithm by other teachers, however, we have not done a very good job. We have also done a good job in this KNN algorithm. We are doing geological software, like the srufer we have referenced, it is better in this aspect, and it is also used in geology. Its Algorithms in this aspect are better, but it also has some problems, that is, as I have said before, it has not completed the fault well. Of course, I have not been qualified to say that others are qualified. In fact, it is far from doing the same with their software. In terms of KNN algorithm, there are also some open-source code. The better thing is the open-source Fortran code developed by Stanford University. It is really interesting. It is also the highest level of open-source software. I am open-source, and you cannot understand it, when you understand it, it's out of date. What he does is I open-source. You don't understand it. It's really ugly. By the way, if you are interested, it is really worth your research and research. If you have studied well, then at least the Master's thesis is definitely not a problem, because other people's doctoral thesis in this aspect is like that. The following describes the contour tracing algorithm. The contour can be divided into non-closed and closed contour lines. The contour tracing starts from the mesh boundary or an equivalent point inside the mesh to obtain the first-class Value Point, then, search for the first-class Value Point (mainly for finding the coordinates) from this point. If a grid boundary is encountered or returns to the starting point (closed contour ), it indicates that the position of an equivalent point has been found, that is, the contour line has been found.
Pay attention to the grid. What is a grid? What does a grid mean? What are the differences between the contour lines in the mesh? As you may know, what I want to talk about now is a tracking of contour lines based on the Rule grid, that is, the rectangular mesh. Only the grids have coordinates and elevation values, in the algorithm, we need to find the coordinates. Is it because we need to find the mesh points and extend the equivalent points on the mesh points? Of course not, the point we are looking for should be on the edge of each small grid of those grids. We have to do some small processing when we encounter an equivalent point that is just good at the grid point, otherwise, we won't be able to trace it. I will discuss the reason later. You may ask, what is the value of our tracing,
This is what we need to do. Remember that we know the coordinates and elevation values of the four points on each small lattice. Then we can estimate these values. Some people will say, if it is an estimation, you won't be able to get it. Then I ask you, is your interpolation algorithm accurate? If you want to be accurate, you can make a measurement by yourself. So how can we estimate based on these values,
This is equivalent to linear interpolation. Here we can rest assured that the distance between the small cells in each grid after gridded is not very large, and there is no debate on interpolation, here we use a diagram to illustrate
Figure 1 we can see that if there is a picture like that, isn't that equivalent point found? Remind everyone, do you see that the black spot of this picture is relatively close to 10, this is the real situation of linear interpolation. In such a small cell, there are only and 4 such points. Why don't there be 1 or 3 such points? If there is only one point, how can we go out when the contour line goes in, the same is true for three. If there are more than five, it is even more difficult. Why, please note that we are doing linear interpolation. Here, you may think about how to change 5 to 10. It means you are on the road, this will be solved by adding a correction value of 10 in the program. If there are four equivalent points, we have to do some processing. Let's explain it as follows. If there are four points, it indicates that there are equal points on the top, right, and bottom, so how can we think that the 8 point is going out from that side,
Figure 2
(3) This situation is impossible. If so, the next time we trace this cell, we will have to connect the upper and lower vertices, in this way, there is a cross-line drawing. This is not allowed. Now we have to solve the problem of (1) or (2). There are also several ways to solve this problem, it is also quite troublesome. I just used a simple method based on others' methods, that is, the method of between the two vertices, if both of them are the same, we can see that the point on the 8 side is a deviation from the upper and lower sides, which is close to the top is (1); otherwise, it is (2). In this way, is (2.
There is something you have to say about algorithms, that is, the computing-defined data structure used in algorithms,
Code
Struct isopoint
{
Public int _ column; // the maximum value of the column here is 1 smaller than the number of rows and columns in the grid.
Public int _ row; // because the columns and columns here are calculated based on the rows and columns (note the rows ).
Public bool _ ishorizon; // determines whether the equivalent point is on the X axis (horizontal line) True -- X false -- y
} Code // information on each edge (with or without an equivalent point, and sometimes where (_ rate)
Struct edgeisoinfo
{
Public float _ rate; // ratio indicates the ratio position on the edge of the grid.
Public bool _ isisopoint; // whether there is an equivalence point on this edge
}
These two structures can determine whether there is an equivalent point on each cell edge of the entire grid, on which side, and on which coordinate position (Have you noticed _ rate ?).
Before we start, we first pre-process the entire grid, that is, we use a two-dimensional array to save the information on each edge.
Code
Privateedgeisoinfo [,] _ xside;
Privateedgeisoinfo [,] _ yside;
Here, _ xside and _ yside respectively refer to the equivalent points on the side of X and Y.
Preprocessing is equivalent to finding all the equivalent points of this elevation value (why, I want to). Here I am doing a loop, that is, every elevation value is doing a preprocessing loop, with the point information corresponding to this elevation value, the question now is how to classify all these points into a line of contour, so we must do some processing, then you can determine where to start, so that there are four borders on the left, top, right, and bottom sides of the grid and inside the grid (used to track closed contour lines ), we will first track the contour, and then track the closed contour. For the open contour, we take the first image above, that is, tracking the left border, we start from the bottom of the left boundary, that is, chasing _ yside [] to check whether its _ isisopoints is true. If not, we are looking for _ yside []. and so on. If we reach the position 10, 5, then the result in Figure 1 is equal to the condition tracingfromleft2right (see the code for details) tracked from left to right in the sample program, to avoid
After tracing this point, we set _ isisopoints to false so that we won't find this point again next time.
The same is true for the processing of closed contour lines.
For other details, see the sample code. In fact, I wrote my blog for the first time, only to know that writing is really not that easy. It was too slow to read other people's blog updates once a day,
It's really hard to find it on my own. Once again, what I wrote here is far from making the algorithm clear. I just wanted to introduce it all at once.
You can check the source code by yourself. You can also contact me.
The next article is smooth, and I will try to write better next time. This time, I want to talk about the smoothness of the contour line. When it comes to the smoothness of the contour line, I will mention it to you. I don't know if you still remember that there are two such functions in the class library, graphics. drawcurve method and graphics. when talking about the two functions, some people may say that smooth is not easy. It is good to pass the points in. This is also a method, it also achieves better smoothness and effectiveness. When I was doing this smoothness, I first found the method I would like to talk about, and then I found these two functions, at that time, I laughed, but after I laughed, I still had to make a choice between the two methods, because we had to choose a more suitable grammar for our project, so I have the following article. As for why, I will explain it below.
The contour is smooth. Some may have a better understanding. She is divided into two types of Fitting Curves and approximation curves, namely, over-point and over-point. Over-point curves include cubic splines and parabolic splines, however, there are some B-spline curves, as well as the betiller curves, and of course there are many others. I am not very familiar with them. As I mentioned earlier, I just achieved the degree to which my current project is used, if you want to know more about this information, you can find out more about it. There are quite a lot of algorithms. I also think it is annoying sometimes, and it is often a headache, however, I was very happy when I made the project. The biggest feeling of my project was that the architecture and algorithm were hard to do for a project, and I did a lot of work in the project, this is a headache. Of course, I am talking about development rather than development. I don't know anything about the sales of a project.
The formation of an algorithm all has its own reasons. It doesn't mean that this is a good decision, and that is definitely not good, just as I used to insert only the sorting algorithm, simple, and the speed can meet the requirements. Now I will talk about a simple over-point (fitting) smoothing algorithm and a parabolic spline algorithm. Why not use a cubic spline algorithm, the cubic spline algorithm has a requirement that every vertex has a derivative. This is a demanding requirement, if the cubic spline algorithm is used at the position such as B in the smooth state, it will be difficult to do so. Some people may say that I should swap the X and Y axes?
Then I ask you if you want to change the axis. If you don't believe it, you just need to think about it. Of course, it doesn't mean you can't. If you want to do that, you have to worry about it all the time, I want to change X to Y here, but I have a better solution.
Parabolic splines are a good method I want to talk about. Here I also want to thank clever101, because this method is made by him. I just want to talk about it with him, in the end, parabolic fitting is the fitting of three points. You can take a look at the specific original article:
Http://blog.csdn.net/clever101/archive/2006/06/03/771160.aspx
I won't talk much about it, but I still copy it here:
========================================================== ======================
If vector expressions are used to represent parameterized quadratic curves, the parabolic expressions can be written as follows:
P (t) = A1 + a2t + a3t2 (0 = <t <= 1)
The parabolic curve is over three points: P1, P2, and P3:
1. the parabolic curve starts with P1. When the variable t = 0, the curve goes through point P1;
2. the parabolic curve ends at P3. When the variable t = 0, the curve goes through P3;
3. When the variable t = 0.5, the curve goes through P2 and the tangent vector is equal to the P3-P1.
T = 0: P (0) = A1 = p1
T = 1: P (1) = A1 + A2 + A3 = P3
T = 0.5: P (0.5) = A1 + 0.5a2 + 0.25 a3 = P2
By solving the simultaneous equations, the three parameters A1, A2, and A3 are obtained respectively:
A1 = p1
A2 = 4 P2-P3-3P1
A3 = 2P1 + 2p3-4p2
The obtained values of these three coefficients are substituted into the parabolic Expression P (t) = A1 + a2t + a3t2 and obtained: p (t) = (2 T-3 t + 1) p1 + (4t-4t2) P2 + (4t2-t) P3 (0 = <t <= 1)
A discrete value column Pi (I = ,......, N), each passing through the adjacent three points for a parabolic, because there are N type value points, so can be made of N-2 parabolic segment.
In the parabolic segment of the N-2, the parabolic segment I goes through Pi, PI + 1, PI + 2 three points, so its expression should be: Si (Ti) = (2t2i-3ti + 1) PI + (4-ti-4 t2i) PI + 1 + (2t2i-ti) PI + 2 (0 = <Ti <= 1)
Similarly, the parabolic segment I + 1 is after PI + 1, PI + 2, PI + 3, so its expression should be: Si + 1 (Ti + 1) = (2t2i + 1-3ti + 1 + 1) PI + 1 + (4 Ti + 1-4 t2i + 1) PI + 2 + (2t2i + 1-ti + 1) PI + 3 (0 = <Ti + 1 <= 1)
Generally, the two parabolic curves cannot overlap in the lap intervals between the two curves. As shown in:
Obviously, for a fitting curve, the entire value point must be connected with only one smooth curve. To achieve this, we must find a way to combine the common intervals of the curve segments such as Si AND Si + 1. This method is the weighted synthesis method.
We set the common interval function to pi + 1 (t) = f (t) Si (Ti) + g (t) Si + 1 (Ti + 1 ). f (t) and g (t) are the weight functions. In a parabolic spline, we take the simple attention function as the weight function, and are complementary.
F (t) = 1-T
G (t) = T
In this way, PI + 1 (t) = (1-T) Si (Ti) + T Si + 1 (Ti + 1 ). because the function has three parameters: T, Ti, and Ti + 1, the next step is to unify the parameters.
We can unify the three parameters in the form:
T = 2 t
Ti = 0.5 + T
Ti + 1 = T
This way
PI + 1 (t) = (-2t3 + 4t2-t) PI + (12t3-410t2 + 1) PI + 1 + (-12t3 + 8t2 + T) PI + 2 + (4t3-2t2) PI + 3 (0 = <ti<= 0.5)
In a geometric sense, the PI + 1 (t) function represents a line segment between point pi + 1 and point pi + 2. But we should see that this method only gets the n-3 segment curve from N points. However, N type value points should have n-1 curve segments. A direct idea is to add two secondary points. So how to add it?
Method 1: The two auxiliary points are P0 and Pn + 1, P0 = p1, Pn + 1 = pN. The drawn curve is an unclosed free curve.
Method 2: add three auxiliary points, P0, Pn + 1 and Pn + 2, then p0 = pN, Pn + 1 = p1, Pn + 2 = P2, the drawn curve is a closed curve.
========================================================== ====================
This method is a good solution to my problem. This method I, it goes through the points, through all the observation points, second, there is no problem of X, Y axis swap, three, it is relatively simple. Unlike other program algorithms, there are a lot of things. Now I have not found any problems with it, which answers why I used this method. By the way, this is just a train of thought. You need to think more about it and see if it is not suitable for you. Modify it by yourself, at the beginning, I used him completely but did not meet my requirements. I also made some minor changes.
Two images:
Attached sample program: I will upload a complete set of contour lines I have made in the future. Thanks to clever101. Now let's continue with the last topic in my contour tracing algorithm series, and fill in the contour.
In a series of questions about the contour, strictly speaking, the only thing I want to talk about is filling. As I mentioned earlier, algorithms are a headache, the filling of the contour is also the same. The two tracking and smoothing mentioned above are more or less articles. The source code is for my reference. Although it is not good, however, there is still a reference that basically meets the project requirements, but this fill, there is very little information, let alone the source code, the example I made is actually just an example. There are still some problems that have not been solved, that is, the gradient of the color. For example, if the color is 60 in the middle, fill 55 in the above, the problem of filling 65 was not solved. After this example was completed, the teacher asked me to do three-dimensional controls, so I did not solve the problem in depth, if anyone has done well, I don't know if I can send me one point for communication.
Speaking of 3D controls, I would like to say a few more words. The information about 3D is as small as what kind of algorithms. The most annoying thing is that it is very painful to learn, remember that there are two problems: one is the translation problem, that is, the moving of objects in 3D space where the mouse is to move, this problem has not been well solved in my two months of MDX learning. I have also asked many people, but none of them answered this question. I remember that only one csdn user provided a method, I thought it was a good idea, but I still did not solve the problem. I did not think it was very simple. I just needed a piece of code. So it won't take more than two months for someone to teach us. Another problem is rotation, which is also an annoying problem. This can be done without a single code, it took a long time to complete the operation. In the future, I will also write a series. I will only write these two questions and then translate and rotate them, these two problems have been annoying for more than two months when we were studying DirectX. I always thought that the two problems in other game companies are definitely not a problem, but others just don't tell you, alas.
Back to the topic, people who work on the contour line will surely have the "Contour generation and filling algorithm". According to the paper written by Sun guiru, my filling algorithm is based on it, of course, I also used this method after comparing some materials in this area, but it is good. In this way, my ability to access materials has also improved a lot, the less information, the higher the requirement on the ability to access people, and the higher the requirement on the ability to learn by others. In that paper, her algorithm IDEA is probably:
The contour can only contain the Class A), Class B), Class C), and Class D. Their overwrites are as follows:
Zone B will never appear inside Zone,
Zone B will never appear in Zone C.
Therefore, when filling, as long as the third (c), the second (B), the first contour (A) and the grid boundary are filled in order, then fill the surrounding area of the fourth contour line (d) in the order from the outer layer to the inner layer to fill the entire area.
The basic description of the entire algorithm is as follows:
1) sort the contour lines of the start point in the left world from the bottom to the top of the vertical coordinate of the start point;
2) sort the contour of the starting point on the upper boundary from left to right by the abscissa of the starting point;
3) sort the contour lines of the start point in the right world from top to bottom by the vertical coordinate of the start point;
4) sort the contours of the starting point on the bottom boundary from right to left by the abscissa of the starting point;
5) sort the internal closed contour lines from left to right by the abscissa of the starting point;
6) Fill in the area of the third contour line and the bottom or left boundary of the grid, and the boundary of the start and end points. For the last contour, you also need to fill the area surrounding the upper or right boundary of the grid;
7) Fill in the border of the second contour with the start and end points, and the area surrounding the vertex that intersection the two boundaries;
8) Fill in the area enclosed by the vertex of the first contour and the boundary of the start and end points;
9) Fill in the area enclosed by the internal closed contour.
If you do this, you will be able to achieve your goal. No matter what other people say about algorithms, you will not be able to learn them. Only when you have realized it can you truly learn it. I will not post code here. However, I 'd like to tell you a better way to find an equivalent graph to be filled in. Based on the method given above, we can find out the rules in them on paper, then write the program on your own. I am not clear about this because after the teacher came back two days, I will do it more deeply. As mentioned above, I just made an example, later, I did not do this, but the teacher took over and wanted to integrate it into our project. However, the time was too tight and others kept urging the teacher to summarize the report, the teacher had no choice but to postpone this function. Now the teacher is back, and it is time to go deeper. When I have done it, I will upload the complete set of programs, also, rewrite this article.