How can i calculate the volume of a fitting surface in matlab? - matlab

I have three properties of a surface (Easting, Northing and depth) in each E & N.
I want to fit a surface to these points and then calculate the volume of this fitted surface in each dx, dy and dz and then compare it with some other data.
Can you help me do that ?

To calculate volume of your surface compared to the flat plane at depth 0 just do
volume_ref = sum(sum(data)) * dx * dy * dz;
To get the volume compared to another surface (e.g. anticline) calculate the volume of the anticline with respect to the same reference (depth 0) and then subtract.

Related

Triangulate set of points on arbitrary plane in 3D space

I have a set of points in 3D space. With maximum error of 10^-5 i can place a plane through them (error is the distance from point to plane).
Is there a way to triangulate these points on this arbitrary plane? I have tried Bowyer-Watson but this only works when the error is 0. Anything else and it wont triangulate or i wont get a good triangulation(overlapping triangles).
Edit
I think i found the problem. At certain angles the bowyer watson algorithm wont work because my calculation of the circumcenter is off. How can i calculate the circumcenter of a triangle in 3D?
Since i know the points on the plane i can calculate a vector. This vector lies on the plane. Next i calculate the center of mass of the points.
Using the vector and center of mass i can create a large triangle on the plane
Vertex p1 = new Vertex(dir * 3000 + center);
Vertex p2 = new Vertex(Quaternion.AngleAxis(120, plane.normal) * dir * 3000 + center);
Vertex p3 = new Vertex(Quaternion.AngleAxis(240, plane.normal) * dir * 3000 + center);
Now that i have the enclosing triangle i can just use Bowyer-Watson.
For circumcenter in 3D i use:
Vector3 ac = p3 - p1;
Vector3 ab = p2 - p1;
Vector3 abXac = Vector3.Cross(ab, ac);
circumceter = p1 + (Vector3.Cross(abXac, ab) * ac.sqrMagnitude + Vector3.Cross(ac, abXac) * ab.sqrMagnitude) / (2 * abXac.sqrMagnitude);
And now i have a triangulated set of points on an arbitrary plane in 3D.

Centroid of Triangulated Surface in 3D

Think about the irregular 3D shape's surface (i.e stone) is triangulated.
I have:
Vertices. x,y,z coordinate of each point (pointCloud).
Faces. Contains info about, each triangle's vertex.
Area of each triangle
Volume of whole shape
With the given information, how to find exact coordinate of that whole triangulated surface's centroid?
You can compute the centroid of the surface by accumulating the centroids of each triangle weighted by each triangle mass, then in the end divide by the total mass. In algorithm, this gives:
mg : vector3d <- (0,0,0)
m : real <- 0
For each triangle t
m <- m + area(t)
mg <- mg + area(t) * centroid(t)
End for
Surfacecentroid <- mg / m
where:
centroid(t) = 1/3 (p1+p2+p3)
area(t) = 1/2 * || cross(p2-p1, p3-p1) ||
Now if what you want is the centroid of the volume enclosed by the surface, the algorithm is different, you need to decompose the volume into tetrahedra and accumulate tetrahedra centroids as follows:
mg : vector3d <- (0,0,0)
m : real <- 0
For each triangle t = (p1,p2,p3)
m <- m + signed_volume(O,p1,p2,p3)
mg <- mg + signed_volume(O,p1,p2,p3) * centroid(O,p1,p2,p3)
End for
volumeCentroid <- (1/m) * mg
where
O=(0,0,0) and
centroid(p1,p2,p3,p4) = 1/4 (p1+p2+p3+p4)
signed_volume(p1,p2,p3,p4) = 1/6 * dot(p2-p1, cross(p3-p1, p4-p1))
The formula works even when O is outside the surface because the signed volumes of the tetrahedra parts outside the surface exactly cancel-out (if you love math, another way of thinking about the algorithm is applying Stokes formula to the volume computation).

Approximating relative angle between two line segments on sphere surface

I am in need of an idea! I want to model the vascular network on the eye in 3D. I have made statistics on the branching behaviour in relation to vessel diameter, length etc. What I am stuck at right now is the visualization:
The eye is approximated as a sphere E with center in origo C = [0, 0, 0] and a radius r.
What I want to achieve is that based on the following input parameters, it should be able to draw a segment on the surface/perimeter of E:
Input:
Cartesian position of previous segment ending: P_0 = [x_0, y_0, z_0]
Segment length: L
Segment diameter: d
Desired angle relative to the previous segment: a (1)
Output:
Cartesian position of resulting segment ending: P_1 = [x_1, y_1, z_1]
What I do now, is the following:
From P_0, generate a sphere with radius L, representing all the points we could possibly draw to with the correct length. This set is called pool.
Limit pool to only include points with a distance to C between r*0.95 and r, so only the points around the perimeter of the eye are included.
Select only the point that would generate a relative angle (2) closest to the desired angle a.
The problem is, that whatever angle a I desire, is actually not what is measured by the dot product. Say I want an angle at 0 (i.e. that the new segment is following the same direction as the previous`, what I actually get is an angle around 30 degrees because of the curvature of the sphere. I guess what I want is more the 2D angle when looking from an angle orthogonal from the sphere to the branching point. Please take a look at the screenshots below for a visualization.
Any ideas?
(1) The reason for this is, that the child node with the greatest diameter is usually follows the path of the previous segment, whereas smaller child nodes tend to angle differently.
(2) Calculated by acos(dot(v1/norm(v1), v2/norm(v2)))
Screenshots explaining the problem:
Yellow line: previous segment
Red line: "new" segment to one of the points (not neccesarily the correct one)
Blue x'es: Pool (text=angle in radians)
I will restate the problem with my own notation:
Given two points P and Q on the surface of a sphere centered at C with radius r, find a new point T such that the angle of the turn from PQ to QT is A and the length of QT is L.
Because the segments are small in relation to the sphere, we will use a locally-planar approximation of the sphere at the pivot point Q. (If this isn't an okay assumption, you need to be more explicit in your question.)
You can then compute T as follows.
// First compute an aligned orthonormal basis {U,V,W}.
// - {U,V} should be a basis for the plane tangent at Q.
// - W should be normal to the plane tangent at Q.
// - U should be in the direction PQ in the plane tangent at Q
W = normalize(Q - C)
U = normalize(Q - P)
U = normalize(U - W * dotprod(W, U))
V = normalize(crossprod(W, U))
// Next compute the next point S in the plane tangent at Q.
// In a regular plane, the parametric equation of a unit circle
// centered at the origin is:
// f(A) = (cos A, sin A) = (1,0) cos A + (0,1) sin A
// We just do the same thing, but with the {U,V} basis instead
// of the standard basis {(1,0),(0,1)}.
S = Q + L * (U cos A + V sin A)
// Finally project S onto the sphere, obtaining the segment QT.
T = C + r * normalize(S - C)

Converting 3D point clouds to range image

I have many 3D point clouds gathered by velodyne sensor. eg(x, y, z) in meter.
I'd like to convert 3D point clouds to range image.
Firstly, I've got transformtation from Catesian to spherical coordinate.
r = sqrt(x*x + y*y + z*z)
azimuth angle = atan2(x, z)
elevation angle = asin(y/r)
Now. How can I convert 3D point to Range image using these transformation in matlab?
Whole points are about 180,000 and I want 870*64 range image.
azimuth angle range(-180 ~ 180), elevation angle range(-15 ~ 15)
Divide up your azimuth and elevation into M and N ranges respectively. Now you have M*N "bins" (M = 870, N = 64).
Then (per bin) accumulate a histogram of points that project into that bin.
Finally, pick a representative value from each bin for the final range image. You could pick the average value (noisy, fast) or fit some distribution and then use that to pick the value (more precise, slow).
The pointcloud2image code available from Matlab File Exchange can help you to directly convert point cloud (in x,y,z format) to 2D raster image.

direction angle computation from surface normal(nx, ny, nz)

I'd like to calculate direction angle from suface normal vector it has nx, ny, nz.
I've got 3D point clouds normals using princomp() on Matlab.
Each point has a normal vector(nx, ny, nz).
How can I compute the direction angle of normal vector?
My final goal is to plot range image colors are mapped in directional angle of surface normal.
If I am not wrong, the direction angle means the angle of the surface normal w.r.t the surface. (Please clarify if I am interpreting it wrongly) In that case, you can take a point on the surface (or the point corresponding to the surface normal). Let the point be P (px, py, pz)'. The normal is N (nx, ny, nz)'
Let theta be the angle b/w the surface normal and the point.
cos(theta) = P.N / |P|.|N|
so in matlab,
cos_theta = P' * N / (norm(P) * norm(N))
theta = acosd(cos_theta)