Posts Tagged ‘patch’

Plot a bunch of edges in matlab with per-edge color data

Monday, December 19th, 2016

Suppose you have a list of vertices V (#V by dim) and a list of edges E (#E by 2) indexing V, then one hacky to draw all of these edges with per-vertex color data DV (#V by 1) is to use trisurf:

trisurf(E(:,[1 1 2]),V(:,1),V(:,2),V(:,3),'CData',DV,'EdgeColor','interp');

However, if you try to use this trick to draw per-edge color data DE (#E by 1), you’ll find that 'EdgeColor','flat' does not work. Instead you can explode you compact “mesh” of edges into individual edges and repeat you edge color data for each edge point:

trisurf(reshape([1:size(E,1),1:size(E,1)*2],[],3),V(E(:),1),V(E(:),2),V(E(:),3),'CData',repmat(DE,2,1),'EdgeColor','interp');

Two-sided material in matlab

Monday, March 23rd, 2015

Unfortunately there seems to be no builtin support for two-sided surfaces in matlab. There’s some rudimentary control over back-face lighting, but that’s all. At least you can determine the back-facing triangles for a given camera position:

N = normals(V,F);
BC = barycenter(V,F);
back_facing = sum(N.*bsxfun(@minus,BC,campos),2)<=0;

Here’s an example for an armadillo mesh:

t = tsurf(F,V,'EdgeColor','none','FaceLighting','phong');view(2);
axis equal;
camproj('persp')
t.FaceVertexCData = 1*(sum(N.*bsxfun(@minus,BC,campos),2)<=0)
apply_ambient_occlusion();

armadillo two-sided material

Of course, if you change the view, the coloring is no longer valid:

armadillo two-sided material

So you need to recompute the coloring:

armadillo two-sided material

You can also insert nans to achieve back-face culling:

t.FaceVertexCData(sum(N.*bsxfun(@minus,BC,campos),2)>0) = nan;

armadillo back-face culling matlab

Use NaNs to hide faces in matlab trisurf/patch renderings

Wednesday, February 11th, 2015

I recently (re)discovered that if you set the ‘CData’ value of a face to nan the face will be hidden. This can sometimes be useful if you want to use the wireframe of a model to give context but focus on just a few faces. Here’s an example:

% selected faces
I = sparse(125,1,1,size(F,1),1);
A = facet_adjacency_matrix(F);
while true
  % Set all but selected to nan
  tsurf(F,V,'CData',sparse(find(~I),1,nan,size(F,1),1));
  view(-48,4); set(gca,'Visible','off');apply_ambient_occlusion();
  drawnow;
  if nnz(I)==size(F,1);
    break;
  end
  I = A*I;
end

cat nan colors

Using patcht to texture map a triangle mesh in matlab

Friday, August 9th, 2013

Recently I found the patcht script which lets you texture map a triangle mesh in matlab. It unfortunately does it in a brute force way: creating a textured surface for every triangle. But it’s at least something. Here’s how I use it for 2d meshes of images using the xy positions as texture coordinates:

im = imread('woody.png');
[V,F] = load_mesh('woody.obj');
patcht(F,V,F,[max(V(:,2))-V(:,2) V(:,1)],im);
axis equal

which produces:

patcht script texture mapping triangle mesh in matlab

We thank Scott Schaefer for providing the wooden gingerbread man image from “Image Deformation Using Moving Least Squares”.

Patch for AntTweakBar to support pasting from clipboard on Mac OS X

Tuesday, January 3rd, 2012

I recently noticed I could use pbpaste and a system call to integrate the clipboard’s contents into my C++ programs. I use AntTweakBar all the time for quick prototyping UI and though it has a default internal clipboard, I found it frustrating that it doesn’t hook into my mac’s global clipboard. To make this happen I just modified TwBar.cpp in two places EditInPlaceSetClipboard and EditInPlaceGetClipboard, surrounding each edit with #elif defined ANT_OSX. Now I can copy from my AntTweakBar and paste into some other app or copy from some other app and paste into an AntTweakBar string field.

Here’s what to place in EditInPlaceSetClipboard

// PATCH BEGIN Alec Jacobson, 2012
// PATCH BEGIN Alec Jacobson, 2012
#elif defined ANT_OSX
    stringstream cmd;
    cmd << "echo \"" << _String << "\" | pbcopy";
    FILE* pipe = popen(cmd.str().c_str(), "r");
    if (!pipe)
    {
        return false;
    }
// PATCH END

And here’s what to place in EditInPlaceGetClipboard

// PATCH BEGIN Alec Jacobson, 2012
// PATCH BEGIN Alec Jacobson, 2012                                                                        
#elif defined ANT_OSX
    FILE* pipe = popen("pbpaste", "r");                                                                   
    if (!pipe)
    {   
        return false;
    }                                                                                                     
    char buffer[128];
    string result = "";                                                                                   
    while(!feof(pipe))
    {   
        if(fgets(buffer, 128, pipe) != NULL)                                                              
        {
            result += buffer;                                                                             
        }                                                                                                 
    }   
    pclose(pipe);
    *_OutString = result.c_str();                                                                         
// PATCH END

To ensure your binaries run with this patched version you might consider compiling AntTweakBar as a static library.