visualization - How do I visualize the intersection of spheres in MATLAB? -


it seems question has been asked in few places (including on so). came across need when visualizing results of trilateration problem.

in every case, answer directs inquiry @ wolfram math excludes code. math great reference, if i'm asking question on programming, code might well. (it appreciated when answers code question avoid pithy comments "writing code trivial").

so how can 1 visualize intersection of spheres in matlab? have simple solution below.

i wrote small script this. feel free make suggestions , edits. works checking if surface of each sphere falls within volume of of other spheres.

for sphere intersection, it's better (but slower) use larger number of faces in sphere() function call. should give denser results in visualization. sphere-alone visualization, smaller number (~50) should suffice. see comments how visualize each.

close clear clc  % centers   : 3 x n matrix of [x;y;z] coordinates % dist      : 1 x n vector of sphere radii  %% plot spheres (fewer faces) figure, hold on % 1 figure rule them [x,y,z] = sphere(50); % 50x50-face sphere = 1 : size(centers,2)     h = surfl(dist(i) * x + centers(1,i), dist(i) * y + centers(2,i), dist(i) * z + centers(3,i));     set(h, 'facealpha', 0.15)     shading interp end  %% plot intersection (more faces) % create 1000x1000-face sphere (bigger number = better visualization) [x,y,z] = sphere(1000);  % allocate space xt = zeros([size(x), size(centers,2)]); yt = zeros([size(y), size(centers,2)]); zt = zeros([size(z), size(centers,2)]); xm = zeros([size(x), size(centers,2), size(centers,2)]); ym = zeros([size(y), size(centers,2), size(centers,2)]); zm = zeros([size(z), size(centers,2), size(centers,2)]);  % calculate each sphere = 1 : size(centers, 2)     xt(:,:,i) = dist(i) * x + centers(1,i);     yt(:,:,i) = dist(i) * y + centers(2,i);     zt(:,:,i) = dist(i) * z + centers(3,i); end  % determine whether points of each sphere fall within sphere % returns booleans = 1 : size(centers, 2)     [xm(:,:,:,i), ym(:,:,:,i), zm(:,:,:,i)] = insphere(xt, yt, zt, centers(1,i), centers(2,i), centers(3,i), dist(i)+0.001); end  % exclude values of x,y,z don't fall in every sphere xmsum = sum(xm,4); ymsum = sum(ym,4); zmsum = sum(zm,4); xt(xmsum < size(centers,2)) = 0; yt(ymsum < size(centers,2)) = 0; zt(zmsum < size(centers,2)) = 0;  % plot intersection = 1 : size(centers,2)     xp = xt(:,:,i);     yp = yt(:,:,i);     zp = zt(:,:,i);     zp(~(xp & yp & zp)) = nan;     surf(xt(:,:,i), yt(:,:,i), zp, 'edgecolor', 'none'); end 

and here insphere function

function [x_new,y_new,z_new] = insphere(x,y,z, x0, y0, z0, r)     x_new = (x - x0).^2 + (y - y0).^2 + (z - z0).^2 <= r^2;     y_new = (x - x0).^2 + (y - y0).^2 + (z - z0).^2 <= r^2;     z_new = (x - x0).^2 + (y - y0).^2 + (z - z0).^2 <= r^2; end 

sample visualizations

for 6 spheres used in these examples, took average of 1.934 seconds run combined visualization on laptop.

intersection of 6 spheres: intersection of 6 spheres

actual 6 spheres: not intersection

below, i've combined 2 can see intersection in view of spheres. both

for these examples:

centers =     -0.0065   -0.3383   -0.1738   -0.2513   -0.2268   -0.3115     1.6521   -5.7721   -1.7783   -3.5578   -2.9894   -5.1412     1.2947   -0.2749    0.6781    0.2438    0.4235   -0.1483  dist =      5.8871    2.5280    2.7109    1.6833    1.9164    2.1231 

i hope helps else may desire visualize effect.


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -