Class to convert object coordinates

This is the place for queries that don't fit in any of the other categories.

Class to convert object coordinates

Postby esuyb » Thu Jan 23, 2014 11:24 pm

I have a number of objects whose centres are described in coordinates local to a room:
table = [2400,600,600]
lamp = [3600,900,300]
rug = [3000,5,2400]

I want to convert them into coordinates local to the house. The room relative to the house is at [12000,0,8000]

I want to keep the objects' names, but, so to speak, suffix them with .house to indicate house based coordinates, i.e.
table.house = [14400,600,8600]
lamp.house = [15400,900,8300]
etc.
- this being the sum of the coordinates of the table and the room ([2400,600,600] + [12000,0,8000]).

Using the following class, I can convert to house based coordinates:

Code: Select all
class room_to_house_coords:
    def __init__(self,room_coords):
        self.house = room_coords + house

However, the entry must be in the form:
table = room_to_house_coords(table)

Is there any way of avoiding the repetition of the object, in this case 'table'?

Lastly, if I have a long list of room-coordinate based objects and want to convert them all to house-based ones, what is the best way of setting up such a loop? Would this involve extracting keys from the class dictionary?

Many thanks for your help.

Angus
esuyb
 
Posts: 3
Joined: Thu Jan 23, 2014 11:09 pm

Re: Class to convert object coordinates

Postby micseydel » Fri Jan 24, 2014 1:30 am

I'd like to see more of the code you're using that exemplifies what you want. A class doesn't seem like a good solution here from what I see.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1507
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Class to convert object coordinates

Postby esuyb » Fri Jan 24, 2014 2:02 am

micseydel

You may be right that a class isn't the best way to achieve what I want.

I'm very new to Python, having migrated from Octave. In Octave it was possible to create useful hierarchical 'structure arrays', and it may be that I am trying to imitate this structure when there is an more efficient form to contain my data in Python.

It's important to me to be able to clearly identify objects by name, rather than indexing the data from a list or a dictionary, so that the variables can be easily identified in equations to be used later on.

Setting up objects and room-based coordinates is simple:
table = [2400,600,600]
lamp = [3600,900,300]
rug = [3000,5,2400]

I want to translate all these object coordinates over to a new reference frame and still retain a very clear naming system for the new objects. I had thought that appending a '.' suffix with, for example, '.house' (so that the table object in house based coordinates would be table.house = [14400,600,8600] ), to indicate the new house reference frame, would be easy to achieve in Python.

I hope that this is clear enough. Any further pointers in the right direction would be appreciated.
esuyb
 
Posts: 3
Joined: Thu Jan 23, 2014 11:09 pm

Re: Class to convert object coordinates

Postby micseydel » Fri Jan 24, 2014 2:21 am

Unfortunately I do not know Octave. I have learned many languages (besides Python, C, C++, Java, Prolog, assembly, Lisp) and I've learned that although it's useful to incorporate knowledge you have from a language already, you definitely shouldn't try to force a language to act like another. I wouldn't write Python-like C code nor C-like Python code. Namely, a dictionary is the best way I know to accomplish what you want outside of some special Python OOP stuff that is likely too advanced for you right now. Although, the internet does things backwards from everywhere else where you have "google.com" and "google" is inside of the "com" domain. In programming we consistently start with the highest level and go to the right to go deeper.

I'm not familiar with the kind of arrays you're referring to, but you should probably check this out. It looks like what you're looking for and would give you a Pythonic way of doing what you want.

Again though, I believe I could help better if you provide code, if you still need help after looking at that link.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1507
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

[SOLVED] Re: Class to convert object coordinates

Postby esuyb » Fri Jan 24, 2014 3:56 pm

I have managed to achieve what I was looking for by using the 'defaultdict' datatype with a tree data structure in a process known as 'autovivification'. More about this can be found here:
https://gist.github.com/hrldcpr/2012250
http://blog.yjl.im/2013/08/autovivification-in-python.html

The code is:
Code: Select all
from collections import defaultdict
import numpy as np
import pprint

def tree(): return defaultdict(tree)

room_relative_to_house = [12000,0,8000] # position of room relative to house

# Define items
furniture = tree()
furniture['table']['room_based_coords'] = np.array([2400,600,600])
furniture['lamp']['room_based_coords'] = np.array([3600,900,300])
furniture['rug']['room_based_coords'] = np.array([3000,5,2400])

# To convert to house based coords
for item in range(len(list(furniture.keys()))):
    furniture[list(furniture.keys())[item]]['house_based_coords'] = furniture[list(furniture.keys())[item]]['room_based_coords'] + room_relative_to_house

# Print out nicely
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(furniture)


It produces the following output:
Image
esuyb
 
Posts: 3
Joined: Thu Jan 23, 2014 11:09 pm

Re: Class to convert object coordinates

Postby micseydel » Fri Jan 24, 2014 6:29 pm

Thanks for posting back your solution!
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1507
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 3 guests