Advertisement
Guest User

Untitled

a guest
Jun 4th, 2018
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.03 KB | None | 0 0
  1. import csv
  2. import colorsys
  3. import numpy as np
  4.  
  5. # Compute the color of a star from its color index:
  6. # see https://stackoverflow.com/questions/21977786/star-b-v-color-index-to-apparent-rgb-color
  7. def bv2rgb(bv):
  8.     r,g,b = 0,0,0
  9.     #extrema and out of range values
  10.     if (bv<-0.4):
  11.         bv=-0.4
  12.     if (bv> 2.0):
  13.         bv= 2.0
  14.     #red
  15.     if bv>=-0.40 and bv<0.00:
  16.         t=(bv+0.40)/(0.00+0.40)
  17.         r=0.61+(0.11*t)+(0.1*t*t)
  18.     elif bv>= 0.00 and bv<0.40:
  19.         t=(bv-0.00)/(0.40-0.00)
  20.         r=0.83+(0.17*t)
  21.     elif bv>= 0.40 and bv<2.10:
  22.         t=(bv-0.40)/(2.10-0.40)
  23.         r=1.00
  24.     #green
  25.     if bv>=-0.40 and bv<0.00:
  26.         t=(bv+0.40)/(0.00+0.40)
  27.         g=0.70+(0.07*t)+(0.1*t*t)
  28.     elif bv>= 0.00 and bv<0.40:
  29.         t=(bv-0.00)/(0.40-0.00)
  30.         g=0.87+(0.11*t)
  31.     elif bv>= 0.40 and bv<1.60:
  32.         t=(bv-0.40)/(1.60-0.40)
  33.         g=0.98-(0.16*t)
  34.     elif bv>= 1.60 and bv<2.00:
  35.         t=(bv-1.60)/(2.00-1.60)
  36.         g=0.82-(0.5*t*t)
  37.     #blue
  38.     if bv>=-0.40 and bv<0.40:
  39.         t=(bv+0.40)/(0.40+0.40)
  40.         b=1.00
  41.     elif bv>= 0.40 and bv<1.50:
  42.         t=(bv-0.40)/(1.50-0.40)
  43.         b=1.00-(0.47*t)+(0.1*t*t)
  44.     elif bv>= 1.50 and bv<1.94:
  45.         t=(bv-1.50)/(1.94-1.50)
  46.         b=0.63-(0.6*t*t)
  47.     return [r,g,b]
  48.  
  49. # Darken the rgb accordingly to magnitude
  50. # see https://blender.stackexchange.com/questions/80034/fix-hsv-to-rgb-conversion/80047
  51. """
  52. def linerRGBToHSV(r,g,b)
  53. def linear_to_srgb(r, g, b):
  54.    def linear(c):
  55.        a = .055
  56.        if c <= .0031308:
  57.            return 12.92 * c
  58.        else:
  59.            return (1+a) * c**(1/2.4) - a
  60.    return [linear(c) for c in (r, g, b)]
  61.  
  62. def srgb_to_linear(r, g, b):
  63.    def srgb(c):
  64.        a = .055
  65.        if c <= .04045:
  66.            return c / 12.92
  67.        else:
  68.            return ((c+a) / (1+a)) ** 2.4
  69.    return tuple(srgb(c) for c in (r, g, b))
  70. """
  71.  
  72. starFile = "/home/loic/Downloads/hygdata_v3_full.csv"
  73.  
  74. reader = csv.DictReader(open(starFile))
  75. verts, tris, cols = [], [], []
  76. ind = 0
  77. offsetY = 0.25*np.sqrt(3)/4 #offset to make an equilateral triangle
  78. for i, row in enumerate(reader):
  79.     if i>0 and i<300000:
  80.         #Get the data
  81.         x = float(row["x"])
  82.         y = float(row["y"])
  83.         z = float(row["z"])
  84.         #mag = float(row["absmag"])
  85.         col = [int(255*w) for w in bv2rgb(float(row["ci"]))] if row["ci"]!="" else [200,200,200]
  86.         if 1:#(x**2 + y**2 + z**2)**0.5 < 1000:
  87.             verts.append([x,y,z])
  88.             #tris.append(tri)
  89.             cols.append(col)
  90.             #iterate and info
  91.             print i
  92.  
  93. with open("out.ply", "w") as f:
  94.     f.write("ply\nformat ascii 1.0\n")
  95.     f.write("element vertex " + str(len(verts)) + "\n")
  96.     f.write("property float x\nproperty float y\nproperty float z\n")
  97.     f.write("property uchar red\nproperty uchar green\nproperty uchar blue\n")
  98.     f.write("end_header\n")
  99.     for v,c in zip(verts, cols):
  100.         f.write(" ".join([str(x) for x in [v[0], v[1], v[2], c[0], c[1], c[2]] ]) + "\n")
  101.     f.write("\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement