Public Group

# how to compute centroid?

This topic is 4962 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm trying to center an object in the middle of the screen (single object model in orthographic mode). I read in some points from a cad like file to render an object on screen and when I rotate it, the center of rotation is around the center of one of the far ends of the box so it rotates funny and not from the direct center of the box (this model is like a long rectangle). Someone then told me I had to compute the centroid of the model by averaging the positions of the points, and then translate that point to the origin. Can anyone tell me how computing this is done? I was thinking like ... as I read coordinates in from my file, I take the largest and smallest X, largest and smallest Y, and largest and smallest of Z, avg those and ... this is where I get lost, it sounds like I'd have to change all of the thousands of coordinates of the nodes which I use to draw all my meshing and move them to push the model in the center. Any help much appreciated.

##### Share on other sites
Using the x dimension as an example, sum all of the x components of all points then divide by the number of points. Repeat for the y and z dimensions, then the resulting xyz triplet is the centroid. Note that this assumes that each point is equally "weighted".

Here's a simple example. Given the points: (-5,0), (6,0), and (20,0), then the resulting centroid is ((-5 + 6 + 20)/3, (0 + 0 + 0)/3) = (7, 0).

Your bounding box method would look acceptable for many cases, but it would be easily fooled by outliers. Although outliers would also have a large effect on the above method, the effect would be somewhat negated at least.

Regarding the rotation issue, one method is what you mentioned -- once your centroid is found, translate all points so that they're centered about the centroid (in the above example, subtract 7 from each x coordinate). Now, you can apply the standard rotation matrices you've probably found. Finally, apply a translation matrix to translate the model somewhere in world space. To minimize computations in the future, adopt a standard for saving models such as the origin is always the model's centroid. Now, you can easily perform rotations about its local coordinate system before translating it into world space.

[Edited by - mnansgar on June 20, 2005 4:19:40 PM]

##### Share on other sites

This is of great help and I beleive I've got my centroid, but I just got lost during one point where you said apply a translation matrix to translate the model somewhere in world space.

As far as I know, a translation matrix means to use a model translation (not projection matrix because it's the model I'm positioning?). Below I do that w/ the MODELVIEW matrix, but I don't get translating to the centroid coordinates. I guess what I'm saying is I don't get adding the new coordinates to the translation matrix. What method is that done with?

--- my display method
...
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);

// reset the modelview matrix
gl.glColor3f(1.0f, 1.0f, 1.0f);

gl.glMatrixMode(GL.GL_MODELVIEW);

// viewing transformation
glu.gluLookAt(0.0, 0.0, 0.0, 0.0, 0.0, -100.0, 0.0, 1.0, 0.0);

// modeling transformation
gl.glScalef(xScale, yScale, zScale);

gl.glTranslatef(xTrans, yTrans, zTrans);

gl.glRotatef(xRot, 1.0f, 0.0f, 0.0f);
gl.glRotatef(yRot, 0.0f, 1.0f, 0.0f);
gl.glRotatef(zRot, 0.0f, 0.0f, 1.0f);

gl.glEnable(GL.GL_BLEND);
gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);

model.draw(active_id);
model.drawInfo();

gl.glDisable(GL.GL_BLEND);
---

##### Share on other sites
I think using a bounding box would be best. Find the min/max for each axis, average them together, and use that for the center. That way if one side has more verticies than the other, it doesn't pull the center towards it.

After that it's as simple as subtracting the center point, rotating or whatever else you want to do, then adding it back.

##### Share on other sites
smart_idiot has a good point. I just gave the classical physics centroid definition, but he's right that it can definitely be skewed by an area on the model which is more finely partitioned. Sorry if I confused you, but the translation steps aren't really part of finding the centroid (except for that which translated all of the points relative to the centroid). Rather, they were just part of the regular rendering pipeline. Hence, just find the centroid, make all of the points relative to that central point, then perform rotation and whatever you were doing before when it rotated about the wrong point! In the future, maybe you should think about building your 3D models where the origin is the center (although of course this has other disadvantages).

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

(You must login to your GameDev.net account.)

• 28
• 16
• 10
• 10
• 11
• ### Forum Statistics

• Total Topics
634103
• Total Posts
3015539
×