# Centering a point cloud/OBJ object

**URL:** https://discourse.processing.org/t/centering-a-point-cloud-obj-object/3493
**Category:** Coding Questions
**Created:** [September 12, 2018, 6:27pm UTC](https://discourse.processing.org/t/centering-a-point-cloud-obj-object/3493 "2018-09-12T18:27:25Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![solub](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/solub/32/333_2.png) [@solub](https://discourse.processing.org/u/solub)
#### Post date: [September 12, 2018, 6:27pm UTC](https://discourse.processing.org/t/centering-a-point-cloud-obj-object/3493/1 "2018-09-12T18:27:25Z")

</div>

Hi all,

I have a problem locating on the canvas a large point cloud file ( **.txt** / **.xyz** ) that I have created with _Meshlab_.

I’ve tried to translate the coordinates numerous times with the `translate()` function but not only I can’t seem to center the point cloud but also I’m unable to find it on the screen.

The file looks like this:

```auto
298422.156250 3.039000 -59934.984375 0.830097 1.000000 -2.509439 
298431.875000 3.039000 -59918.578125 1.011246 5.000000 -1.724044 
298434.750000 3.039000 -59921.472656 -1.559582 2.000000 -2.399486 
298442.562500 3.039000 -59922.582031 0.195280 1.000000 -2.859333 
298447.593750 3.039000 -59919.855469 1.427991 3.000000 -2.638340 
298462.968750 3.039000 -59911.554688 1.806006 5.000000 -1.242686 
298447.750000 3.039000 -59886.371094 2.543457 5.000000 1.589585 
298443.500000 3.039000 -59880.003906 2.470000 3.000000 1.701539 
298426.093750 3.039000 -59856.257812 2.401855 12.000000 1.797043 
298424.218750 3.039000 -59853.851562 1.324241 1.000000 2.196671 
298422.906250 3.039000 -59854.195312 -0.759176 2.000000 2.902353 
298401.062500 3.039000 -59859.894531 -0.761293 2.000000 2.901793

```

_the first 3 columns are the x, z and y coordinates_

Similarly, I have loaded a **.obj** object (built from the same coordinates) with the `loadShape()` function I’m facing the same issue.

What kind of translation do you think I need to operate (from looking at the snippet) ?  
Do you think it’s a problem of scaling ?

---

<div class="post-metadata">

### Author: ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)
#### Post date: [September 12, 2018, 6:32pm UTC](https://discourse.processing.org/t/centering-a-point-cloud-obj-object/3493/2 "2018-09-12T18:32:37Z")

</div>

I think you need to translate to width/2 height/2 then scale it down … a lot! Mind you, that depends if the values are around zero. What’s the minimum and maximum values for each dimension?

---

<div class="post-metadata">

### Author: ![solub](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/solub/32/333_2.png) [@solub](https://discourse.processing.org/u/solub)
#### Post date: [September 12, 2018, 6:49pm UTC](https://discourse.processing.org/t/centering-a-point-cloud-obj-object/3493/3 "2018-09-12T18:49:40Z")

</div>

Hi @neilcsmith, thanks for the reply.

`width/2, height/2` translation doesn’t work. I mean I can see a tiny point cloud (I have scaled down to .001) but I have to rotate the camera (PeasyCam) with the mouse and it’s very far.

column 1:

- min(): 298393.84375
- max(): 307484.0

column 2:

- min(): 11.891999999999999
- max(): 547.76000999999997

column 3:

- min(): 78599.117188000004
- max(): 59277.414062000003

---

<div class="post-metadata">

### Author: ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)
#### Post date: [September 12, 2018, 7:34pm UTC](https://discourse.processing.org/t/centering-a-point-cloud-obj-object/3493/4 "2018-09-12T19:34:09Z")

</div>

You could try translating by the negative of the min value in each direction? You could then scale it based on the ratio of width to the difference of max and min.

---

<div class="post-metadata">

### Author: ![solub](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/solub/32/333_2.png) [@solub](https://discourse.processing.org/u/solub)
#### Post date: [September 12, 2018, 8:24pm UTC](https://discourse.processing.org/t/centering-a-point-cloud-obj-object/3493/5 "2018-09-12T20:24:54Z")

</div>

I tried already. I also tried the negative mean of each column, didn’t work either. This thing is a real headache.

---

<div class="post-metadata">

### Author: ![solub](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/solub/32/333_2.png) [@solub](https://discourse.processing.org/u/solub)
#### Post date: [September 12, 2018, 10:05pm UTC](https://discourse.processing.org/t/centering-a-point-cloud-obj-object/3493/6 "2018-09-12T22:05:04Z")

</div>

So it seems I wasn’t using the `translate()`, `rotate()` and `scale()` functions in the correct order. The following did the trick.

```auto
scale(.01)
translate(-302938.921875, -267.93400499, 68938.265625)
rotateX(PI/2)
shape(shp)

```

The `translate()` coordinates are the negative means of each column.

However, applying that exact translation to an .obj built from the very same vertices doesn’t put that object at the center. It appears on the canvas, but far from the camera and at a different angle. Very strange.

---

<div class="post-metadata">

### Author: ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)
#### Post date: [September 13, 2018, 9:04am UTC](https://discourse.processing.org/t/centering-a-point-cloud-obj-object/3493/7 "2018-09-13T09:04:10Z")

</div>

I would expect to use translate before scale. And I’d start with negative min, not mean. The idea is to end up with the min values at zero.

---

<div class="post-metadata">

### Author: ![solub](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/solub/32/333_2.png) [@solub](https://discourse.processing.org/u/solub)
#### Post date: [September 13, 2018, 9:22am UTC](https://discourse.processing.org/t/centering-a-point-cloud-obj-object/3493/8 "2018-09-13T09:22:33Z")

</div>

Logic would have it that `scale()` goes after `translate()`, as you’re suggesting, but for some reason it’s the exact opposite that works here. Don’t know why.

I prefer putting the negative mean because it makes possible to rotate the point cloud around its centroid.

---

<div class="post-metadata">

### Author: ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)
#### Post date: [September 13, 2018, 9:36am UTC](https://discourse.processing.org/t/centering-a-point-cloud-obj-object/3493/9 "2018-09-13T09:36:21Z")

</div>

> [@solub](#):
>
> I prefer putting the negative mean because it makes possible to rotate the point cloud around its centroid.

Yes, but I’d be tempted to do those in two steps, at least at first. Get your points normalized to the window (translate then scale), then move it to the centre. By the way, are you using PeasyCam all the time? Have you tried without just in case?

Just noticed your translate line above has a positive z translation - is that intended?!

---

<div class="post-metadata">

### Author: ![solub](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/solub/32/333_2.png) [@solub](https://discourse.processing.org/u/solub)
#### Post date: [September 19, 2018, 11:32pm UTC](https://discourse.processing.org/t/centering-a-point-cloud-obj-object/3493/10 "2018-09-19T23:32:35Z")

</div>

Apologies for the late reply.

Yes, the translate line has a positive z translation and it works fine this way.  
Yes, I’m using PeasyCam and your suggestion is relevant, I’ll try adjusting the coordinates without the library first.

---

<div class="post-metadata">

### Author: ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)
#### Post date: [September 20, 2018, 7:33am UTC](https://discourse.processing.org/t/centering-a-point-cloud-obj-object/3493/11 "2018-09-20T07:33:40Z")

</div>

> [@solub](#):
>
> Yes, the translate line has a positive z translation and it works fine this way.

Really?! Why are you asking the question then? 😉 If you want to make everything around 0,0,0 then you need to negatively translate all 3 axes first.

---

<div class="post-metadata">

### Author: ![solub](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/solub/32/333_2.png) [@solub](https://discourse.processing.org/u/solub)
#### Post date: [September 22, 2018, 6:11am UTC](https://discourse.processing.org/t/centering-a-point-cloud-obj-object/3493/12 "2018-09-22T06:11:25Z")

</div>

> [@neilcsmith](#):
>
> Really?! Why are you asking the question then?

Simply because it didn’t work. Negatively translating _all_ the axes makes the object totally disappear from screen. With or without peasycam, before or after scaling…

I’ll stick with the dubious workaround aforementioned: scaling first and translating by negative means.
