0

# Help Calculating Normals - Sample Code?

Anonymous 10 years ago
This discussion was imported from CodePlex

GilbertF wrote at 2012-10-03 22:10:

first thanks for this amaizing tool , i am using mesh builder to create an object made of quads i am passing to the function four points in counter clock  order, this is a laser scanner and i am reading contour to recreated an object, now i have data and an object but it looks bumpy or facet not smooth i need to calculate normals to make it look better,  any help wil be appreciated.

thanks

Gilbert figueroa

orlando, fl

objo wrote at 2012-10-03 22:41:

You don't have to calculate normals, just share vertices for adjacent panels where you want a smooth surface. You will not share vertices when you use the AddQuad method, try to use AddRectangularMesh (if your surface is a 'rectangular mesh') or set the positions and triangle indices yourself!

GilbertF wrote at 2012-10-13 07:49:

Hi Objo

thanks for your last replay i still stock trying to get  a smooth surface im trying to use the addrectangularmesh but no results i was wondering where i can find sample code of the type of data i need to pass to the method it saids it is IList<Point3D>? sample code or sample will be appreciated.

thanks

gilbert

objo wrote at 2012-10-14 15:54:

Use "Find usages" function (Shift-F12?) in Visual Studio to find two examples of AddRectangularMesh. The points should be specified 'row by row' and you have to specify the number of 'columns'. Note that the front side of the geometry is defined by the order of the points (reversing the order should reverse the normals).

GilbertF wrote at 2012-10-14 22:54:

Hi Objo and thanks for the replay

I'm building an application that needs to detect the contour or shape of a hand or foot. This application uses a laser to draw a line on a surface, a hand in this case. We move the laser to a position, then take a snapshot with a camara, analyze the image to detect the X,Y coordinates of the detected red laser line. Then we command the laser to advance a certain distance to a new 'row' (Y coordinate).

What we end up with is a List of rows, technically a List<List<Point3D>>. Our image might be 380 cols by 480 rows. But this list does not contains all the 380x480 points, it just contains the X,Y,Z coordinates of the 'red' laser pixels it detected. As we are advancing the laser 5 or 10 rows at a time we most rows are missing in this collection. Also there can be undetected pixels in each of the rows. So what we have is not a complete collection of X,Y coordinates. And each row might be a different size, etc.

In order to display a more realistic image of the scanned object I'm trying to take this collection of points and plot a smooth surface that interpolates all the missing points from the pixel points we collected from the images.

Below is an excerpt of the different approaches I have taken so far.

First I create the meshBuilder:
MeshBuilder meshBuilder = new MeshBuilder(true, false);

Then we create our points collection and fill it with the data vaules:
List<List<Point3D>> pointsList = new List<List<Point3D>>();
[some code here to fill in the collection]

A- This approach plots a series of small cubes that resemble our scaned hand:
foreach (List<Point3D> pointsRow in pointsList)
foreach (Point3D point in pointsRow)

So that indicates that our points collection is not that far off.

So I'm trying to create a mesh with this points collection by doing this:

Approach #1: (where matrix.maxCols is the width of our image)
meshBuilder.CreateNormals = true;
foreach (List<Point3D> pointsRow in pointsList) {
}

Approach #2: (this will feed a maxCols x maxRows points array with the undertemined values = (0,0,0)
Point3D[,] pointsArray = new Point3D[matrix.maxCols, matrix.maxRows];
foreach (List<Point3D> pointsRow in pointsList)
foreach (Point3D point in pointsRow)
pointsArray[(int)point.X, (int)point.Y] = point;