Guest User

Untitled

a guest
Jan 15th, 2019
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.09 KB | None | 0 0
  1. #imports
  2. import matplotlib.pyplot as plt
  3. import matplotlib.patches as patches
  4. from math import pi , sin
  5.  
  6. #raw data
  7. connections = ['1', '0,2,5,7,8', '1,3', '2,4', '3', '1,6', '5', '1', '1']
  8. indices = ['0', '1', '2', '3', '4', '5', '6', '7', '8']
  9. spaceTags = ['entry ', 'living area', 'dining area', 'Kitchen', 'Utility',
  10.              'Bedroom1', 'Toilet attached', 'Toilet common', 'Bedroom2']
  11. area = ['1', '40', '20', '15', '6', '20', '6', '6', '20']
  12. minimumDimension = ['1', '5', '4', '3', '2', '4', '2', '2', '4']
  13.  
  14. #define the space class
  15. class Space(object):
  16.     def __init__(self,index,spaceName,connections,area,minimumDimension):
  17.         self.index = index
  18.         self.spaceName = spaceName
  19.         self.connections = connections
  20.         self.area = area
  21.         self.ydim = minimumDimension
  22.         self.xdim = (self.area / self.ydim)
  23.         self.x = 0
  24.         self.y = 0
  25.         self.isExplored = False
  26.         self.polarAngle = 0
  27.         self.spaceType = 0
  28.         # 0= not assigned ; 1 = entry; 2 = intermediate; 3 = termination
  29.    
  30.     def ObjectAttributes(self):
  31.         return (self.index,
  32.                 self.spaceName,
  33.                 self.connections,
  34.                 self.area,
  35.                 self.y,self.x,
  36.                 self.xdim,self.ydim,
  37.                 self.isExplored,
  38.                 self.spaceType)
  39.  
  40. #definations
  41. #process integers
  42. def convert_to_int_vals (input_list):
  43.     output_list = []
  44.     i = 0
  45.     while i < len(input_list):
  46.         output_list.append(int(input_list[i]))
  47.         i += 1
  48.     return output_list
  49.  
  50. #process floats
  51. def convert_to_float_vals (input_list):
  52.     output_list = []
  53.     i = 0
  54.     while i < len(input_list):
  55.         output_list.append(float(input_list[i]))
  56.         i += 1
  57.     return output_list
  58.  
  59. #process 2D lists for connections
  60. def process_2d_connections (input_list):
  61.     output_list = []
  62.     for item in input_list:
  63.         if len(item) <= 1:
  64.             lst = []
  65.             lst.append(int(item))
  66.             output_list.append(lst)
  67.         else:
  68.             var = item
  69.             var = (var.split(','))
  70.             var = convert_to_int_vals(var)
  71.             output_list.append(var)
  72.     return output_list
  73.  
  74. #make data into objects i.e. spaces
  75. def convertDataToSpaces(index,spaceTag,connections,area,minimumDimension):
  76.     print('Processing data...')
  77.     if (len(index)==len(spaceTag)==len(connections)==len(area)==len(minimumDimension)):
  78.         i = 0
  79.         output_list = []
  80.         while i < len(spaceTag):
  81.             space = Space(index[i],spaceTag[i],connections[i],area[i],minimumDimension[i])
  82.             output_list.append(space)
  83.             i += 1
  84.         print('Done.')
  85.         return output_list
  86.     else:
  87.         print('Number of lists dont match')
  88.  
  89. #find first node
  90. def FindFirstNode(spaceList):
  91.     output = 'null'
  92.     for item in spaceList:
  93.         if item.spaceName == 'entry ' or item.spaceName =='entry':
  94.             item.spaceType = 1
  95.             output = item
  96.    
  97.     if output == 'null':
  98.         print('No entry defined. Please define entry!')
  99.        
  100.     return output
  101.  
  102. #Calculate hypotenuse
  103. def calculate_hypotenuse(arg1,arg2):
  104.     val = ((arg1**2)+(arg2**2))**(0.5)
  105.     return val
  106.  
  107. #Calculate max hypotenuse
  108. def calculate_max_hypotenuse (spaceList):
  109.     outputval = 0
  110.     for item in spaceList:
  111.         var = calculate_hypotenuse(item.xdim,item.ydim)
  112.         if var > outputval:
  113.             outputval = var
  114.         else:
  115.             outputval
  116.     return outputval
  117.  
  118. # Note this is a FIFO list
  119. def FindAndQueueNextNode(spaceList,searchNode,queue):
  120.     searchNode.isExplored = True
  121.  
  122.     if len(searchNode.connections) == 1:
  123.         if searchNode.spaceName == 'entry ' or searchNode.spaceName =='entry':
  124.             searchNode.spaceType = 1
  125.         else:
  126.             searchNode.spaceType = 3
  127.     elif len(searchNode.connections) > 1:
  128.         searchNode.spaceType = 2
  129.     else:
  130.         searchNode.spaceType = 0
  131.  
  132.     for item in spaceList:
  133.         if ( item.index in searchNode.connections) and (item.isExplored == False) :
  134.             queue.append(item)
  135.  
  136. # Calculate the position based on the dimension (inputs are the object dimensions and current floating dim)
  137. def CalculatePosition(currentx, currenty, space):
  138.     spaceXdimadjusted = (space.xdim / 2)* -1
  139.     spaceYdimadjusted = (space.ydim / 2)* -1
  140.     adjustedx = currentx + spaceXdimadjusted
  141.     adjustedy = currenty + spaceYdimadjusted
  142.  
  143.     return (adjustedx,adjustedy)
  144.  
  145. #core algorithm
  146. def coreAlgorithm(spacesList):
  147.     ## variable holding max hypotenuse distance
  148.     grid_dimension = int((calculate_max_hypotenuse(spacesList))*(1.5))
  149.     print('Grid dimensions are : ' + str(grid_dimension) + (' units'))
  150.  
  151.     ## create empty processing variables
  152.     processingQueue = []
  153.     orderedListOfSpacesInBFS = []
  154.     maxTreeWidth = 0
  155.  
  156.     ## find the first space
  157.     firstSpace = FindFirstNode(spacesList)
  158.     orderedListOfSpacesInBFS.append(firstSpace)
  159.     print('The first node is : ' + str(firstSpace.spaceName) +
  160.           '; Index being : ' + str(firstSpace.index))
  161.      
  162.     ## queue the next space
  163.     FindAndQueueNextNode(spacesList,firstSpace,processingQueue)
  164.  
  165.     ##start while loop (while queue length loop  > 0)
  166.     while len(processingQueue) > 0 :
  167.         if len(processingQueue) > maxTreeWidth:
  168.             maxTreeWidth = len(processingQueue)
  169.         else:
  170.             maxTreeWidth = maxTreeWidth
  171.         item = processingQueue.pop(0)
  172.         orderedListOfSpacesInBFS.append(item)
  173.         FindAndQueueNextNode(spacesList,item,processingQueue)
  174.  
  175.     ## second loop to loop through spaces and draw them
  176.     maxXDepthDimension = grid_dimension * maxTreeWidth
  177.     ypos = grid_dimension
  178.     counter = 0
  179.  
  180.     while len(orderedListOfSpacesInBFS) > 0:
  181.         item = orderedListOfSpacesInBFS.pop(0)
  182.  
  183.         if item.spaceType == 1:
  184.             xpos = maxXDepthDimension / 2
  185.             (item.x , item.y) = CalculatePosition(xpos,ypos, item)
  186.             ypos += grid_dimension
  187.             counter = len(item.connections)
  188.        
  189.         elif counter == 1:
  190.             xpos = maxXDepthDimension / 2
  191.             (item.x , item.y) = CalculatePosition(xpos,ypos, item)
  192.             ypos += grid_dimension
  193.             counter = len(item.connections) - 1
  194.        
  195.         elif counter > 1:
  196.             xpos = (maxXDepthDimension / counter)
  197.             (item.x, item.y) = CalculatePosition(xpos, ypos, item)
  198.             counter -= 1
  199.  
  200.  
  201.  
  202. #draw lines as a separete method
  203.  
  204. #core algorithm preprocess
  205. def coreAlgorithmLoop (spaces_list):
  206.    
  207.     #check object feasibility and if the algorithm can run.
  208.     print('Starting algorithm...')
  209.     startrun = False
  210.     floatingvartoggle = 1
  211.  
  212.     for item in spaces_list:
  213.         if type(item) == Space:
  214.             floatingvartoggle = floatingvartoggle * 1
  215.         else:
  216.             floatingvartoggle = floatingvartoggle * 0
  217.    
  218.     if floatingvartoggle == 1:
  219.         startrun = True
  220.     else:
  221.         print('Objects should be spaces.')
  222.    
  223.     #start of  core-algorithm.
  224.     if startrun == True:
  225.         coreAlgorithm(spaces_list)
  226.  
  227. #implementation
  228. #pre-process data
  229. indices = convert_to_int_vals(indices)
  230. spaceTags = spaceTags
  231. connections = process_2d_connections(connections)
  232. area = convert_to_float_vals(area)
  233. minimumDimension = convert_to_float_vals(minimumDimension)
  234.  
  235. #initialize processing
  236. listOfSpaces = convertDataToSpaces(indices,
  237.                                    spaceTags,
  238.                                    connections,
  239.                                    area,
  240.                                    minimumDimension)
  241. coreAlgorithmLoop(listOfSpaces)
  242.  
  243. #matplotlibtester - start
  244. fig, ax = plt.subplots()
  245. ax.set_xlim((0, 100))
  246. ax.set_ylim((0, 70))
  247.  
  248.  
  249. for space in listOfSpaces:
  250.     var = space.area
  251.     print(space.ObjectAttributes())
  252.     rect = patches.Rectangle((space.x,space.y),
  253.                               space.xdim,space.ydim,0,
  254.                               linewidth=1,
  255.                               edgecolor='r',
  256.                               facecolor='none')
  257.     ax.add_patch(rect)
  258.  
  259. plt.show()
  260. #matplotlibtester - end
Add Comment
Please, Sign In to add comment