0

I have a mesh model in X, Y, Z format. Lets say.

Points *P;

In first step, I want to normalize this mesh into (-1, -1, -1) to (1, 1, 1). Here normalize means to fit this mesh into a box of (-1, -1, -1) to (1, 1, 1).

then after that I do some processing to normalized mesh, finally i want to revert the dimensions to similar with the original mesh.

step-1: P = Original Mesh dimensions;

step-2: nP = Normalize(P); // from (-1, -1, -1) to (1, 1, 1)

step-3: cnP = do something with (nP), number of vertices has increased or decreased.

step-4: Original Mesh dimensions = Revert(cnP); // dimension should be same with the original mesh

how can I do that?

genpfault
  • 51,148
  • 11
  • 85
  • 139
maxpayne
  • 1,111
  • 2
  • 21
  • 41
  • 1
    @NicolBolas now I'm curious what "the obvious" is. – Drew Dormann Sep 09 '12 at 02:18
  • 1
    @DrewDormann Find largest absolute value of any coordinate for any vertex. Divide every vertex's position by that value. Later, multiply by it to scale up. Problem is, the question is underspecified, so there are several billion ways to answer it... – John Calsbeek Sep 09 '12 at 02:19
  • What is obvious ? I tried to compute it by myself but its complicated I think so. – maxpayne Sep 09 '12 at 02:20
  • 2
    @furqan: No it's not complicated at all. I can fit it in a comment: `min_{x,y,z}=max_{x,y,z}=vertex[0].{x,y,z}; foreach(v in vertices){min_{x,y,z}=min(min_{x,y,z},v.{x,y,z}); max_{x,y,z}=max(max_{x,y,z}, v.{x,y,z}); } t.{x,y,z} = (max_{x,y,z} + min_{x,y,z}); s.{x,y,z} (max_{x,y,z} - min_{x,y,z}); translate_model(-t); scale_model(2./s); modify_model(); scale_model(2.*s); translate_model(t);` that's it. – datenwolf Sep 09 '12 at 07:38
  • I think the correct one is scale_model(2./s); modify_model(); scale_model(s/2.); and as @Drew said, find the largest absolute value. it works for me. Thanks. – maxpayne Sep 09 '12 at 12:38

2 Answers2

3

It's easy - use shape functions. Here's a 1D example for two points:

-1 <= u <= +1
x(u) = x1*(1-u)/2.0 + x2*(1+u)/2.0
x(-1) = x1
x(+1) = x2

You can transform between coordinate systems using the Jacobean.

Let's see what it looks like in 2D:

-1 <= u <= =1
-1 <= v <= =1
x(u, v) = x1*(1-u)*(1-v)/4.0 + x2*(1+u)*(1-v)/4.0 + x3*(1+u)*(1+v)/4.0 + x4*(1-u)*(1+v)/4.0
y(u, v) = y1*(1-u)*(1-v)/4.0 + y2*(1+u)*(1-v)/4.0 + y3*(1+u)*(1+v)/4.0 + y4*(1-u)*(1+v)/4.0
duffymo
  • 305,152
  • 44
  • 369
  • 561
  • Do something with what I gave you first. – duffymo Sep 09 '12 at 02:34
  • What is x1, x2, x3, x4 ? I mean in mesh what are these values. You means x1 = x(u+1) , x2 = x(u+2) and so on. ? – maxpayne Sep 09 '12 at 02:41
  • The use of shape functions to merely rescale bunch of vertices to fit in a +1,-1 cube is IMHO overkill. If one already has a framework that can do it, it'd work, but then you don't write the transformation equations yourself anyway. You might as well suggest to multiply the values by some matrix, and then multiply by the inverse of that matrix -- at least that's more likely to have support by a framework of some sort, and is more common terminology. Your answer seems to be showing off. Not very useful for the asker. It was nice to read about shape functions that I never heard of, though. – Kuba hasn't forgotten Monica Sep 09 '12 at 05:32
  • Showing off? No, I'm giving you the basis for the transformation from global and local coordinates. The (x, y) values are the global coordinates and (u, v) are their counterparts in local unit space. You use the Jacobean to transform from one to the other. This is what you need to learn to solve the problem properly. People volunteer here to give answers; that's what I've done. It's too bad if you don't understand it. – duffymo Sep 09 '12 at 12:55
  • Two other points: The OP said mesh, so I'm presuming that each element has its own local coordinate system, like a finite element mesh would. And, for the life of me, I can't understand the low-rep people who don't offer answers of their own but insist on making argumentative, disparaging comments about the answers of others. They have little to offer themselves. @Kuba Ober, let's see more answers and fewer comments from the likes of you. – duffymo Sep 09 '12 at 14:41
  • FWIW this is a good answer, just more challenging mathematically. I think the core miscommunication was "presuming that each element has its own local coordinate system". Often times this is available (e.g., it's a triangle mesh and you know who the neighbors are), but it isn't always possible (e.g., a point cloud). This was under-specified in the question, but it seems they may have only had points. Shape functions rule though <3 – svenevs Nov 26 '17 at 12:44
  • No, that local coordinate system exists independent of neighbors. It's the basis for the shape functions, independent of any mesh they are embedded in. Every singe 1D element has a local coordinate system that equal -1 at one node and +1 at the other. No neighbors needed. – duffymo Nov 26 '17 at 12:58
3

I know how easy it can be to get lost in programming and completely miss the simplicity of the underlying math. But trust me, it really is simple.

The most intuitive way to go about your problem is probably this:

  1. determine the minimum and maximum value for all three coordinate axes (i.e., x, y and z). This information is contained by the eight corner vertices of your cube. Save these six values in six variables (e.g., min_x, max_x, etc.).

  2. For all points p = (x,y,z) in the mesh, compute

    q =  ( 2.0*(x-min_x)/(max_x-min_x) - 1.0
           2.0*(y-min_y)/(max_y-min_y) - 1.0
           2.0*(z-min_z)/(max_z-min_z) - 1.0 )
    

    now q equals p translated to the interval (-1,-1,-1) -- (+1,+1,+1).

  3. Do whatever you need to do on this intermediate grid.

  4. Convert all coordinates q = (xx, yy, zz) back to the original grid by doing the inverse operation:

    p =  ( (xx+1.0)*(max_x-min_x)/2.0 + min_x
           (yy+1.0)*(max_y-min_y)/2.0 + min_y
           (zz+1.0)*(max_z-min_z)/2.0 + min_z )
    
  5. Clean up any mess you've made and continue with the rest of your program.

This is so easy, it's probably a lot more work to find out which library contains these functions than it is to write them yourself.

Rody Oldenhuis
  • 37,726
  • 7
  • 50
  • 96