c - Calculate surface area of a 3D mesh -
i have 3d mesh defined verteces , triangles. have normals of mesh. i'd calculate area of mesh, assuming it's closed. found interesting implementation of calculation of 3d volume in this question, , applied in c code build function called r. code:
double signedvolumeoftriangle(double p1x, double p1y, double p1z, double p2x, double p2y, double p2z, double p3x, double p3y, double p3z) { double v321 = p3x*p2y*p1z; double v231 = p2x*p3y*p1z; double v312 = p3x*p1y*p2z; double v132 = p1x*p3y*p2z; double v213 = p2x*p1y*p3z; double v123 = p1x*p2y*p3z; homecoming (double)(1.0/6.0)*(-v321 + v231 + v312 - v132 - v213 + v123); } void meshvolume(double *x, double *y, double *z, int *numt, int *v1, int *v2, int *v3, double *volume) { int n; *volume=0; (n=0; n<*numt; n++) { *volume = *volume + signedvolumeoftriangle(x[v1[n]], y[v1[n]], z[v1[n]], x[v2[n]], y[v2[n]], z[v2[n]], x[v3[n]], y[v3[n]], z[v3[n]]); } *volume = fabs(*volume); }
neither in question nor in article linked found algorithm calculating area of mesh. there can help me please?
you have closed volume surface made triangles. , triangles contribute outer surface. right?
the surface of triangle between points p
, q
, r
can obtained by:
a = 0.5 * |pq × pr| = 0.5 * |pq| * |pr| * sin(Ɵ)
where
pq = q - p pr = r - p
and ×
denotes cross product , Ɵ
angle between vectors. (the magnitude of resulting vector of cross product area of parallelogramme between 2 original vectors. half of area of triangle.)
sum aeras of triangles. there's no need take absolute value, because area can 0 or positive. so:
double areaoftriangle(double p1x, double p1y, double p1z, double p2x, double p2y, double p2z, double p3x, double p3y, double p3z) { double ax = p2x - p1x; double ay = p2y - p1y; double az = p2z - p1z; double bx = p3x - p1x; double = p3y - p1y; double bz = p3z - p1z; double cx = ay*bz - az*by; double cy = az*bx - ax*bz; double cz = ax*by - ay*bx; homecoming 0.5 * sqrt(cx*cx + cy*cy + cz*cz); } void meshsurface(double *x, double *y, double *z, int *numt, int *v1, int *v2, int *v3, double *area) { int n; *area = 0.0; (n=0; n<*numt; n++) { *area += areaoftriangle(x[v1[n]], y[v1[n]], z[v1[n]], x[v2[n]], y[v2[n]], z[v2[n]], x[v3[n]], y[v3[n]], z[v3[n]]); } }
c r 3d
No comments:
Post a Comment