Skip to content

Instantly share code, notes, and snippets.

@RKursatV
Created December 12, 2021 22:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RKursatV/8c27e9aca8adec21ad97c091b62c85e4 to your computer and use it in GitHub Desktop.
Save RKursatV/8c27e9aca8adec21ad97c091b62c85e4 to your computer and use it in GitHub Desktop.
477 study for midterm
import os
import numpy as np
import math
def unit(arr):
return 1.0 * arr / np.linalg.norm(arr)
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
class Solution:
available_options = [] # function, description, paramlist
def __init__(self):
self.available_options = []
self.addCalc(self.luminanceCalculator, "Find output luminance", "contrast, voltage, gamma, brightness")
self.addCalc(self.pixelCalc, "Sample 3 Pixel Calculation: Given the following parameters, what will be the world coordinates of the top-left pixel of the image on the image plane?",
"Ex, Ey, Ez, Wx, Wy, Wz, Vx, Vy, Vz, distance, lBoundary, rBoundary, tBoundary, bBoundary, Nx, Ny")
# self.addCalc(self.worldCoordinates, "Find world coordinates of the top left pixel", "contrast, voltage, gamma, brightness")
self.addCalc(self.rotateSquare, "Find transformation matrix to rotate the square", "Centerx, Centery, EdgeLength, Degree")
self.addCalc(self.rayFind, "Find ray at t=X", "Ox, Oy, Oz, Dx, Dy, Dz, t")
self.addCalc(self.spherediffuse, "Find the RED channel value of the diffuse color component of P",
"CoefficientX, CoefficientY, CoefficientZ, "
"CenteredX, CenteredY, CenteredZ, "
"LSourceX, LSourceY, LSourceZ, "
"LightIntensityX, LightIntensityY, LightIntensityZ,"
"PointX, PointY, PointZ")
self.addCalc(self.unitreflectionofnonunitvector, "Find the unit reflection of a non-unit vector",
"Wx, Wy, Wz, Ndx, Ndy, Ndz")
self.addCalc(self.transformationmatrix, "Find transformation matrix to transform the normals", "v1x, v1y, v1z, "
"v2x, v2y, v2z,"
" v3x, v3y, v3z")
self.addCalc(self.textureBarytencric, "Find texture coords by Barycentric Coordinates",
"alpha, beta, alpha_assos_u1, alpha_assos_v1, beta_assos_u1, beta_assos_v1, o_u, o_v")
self.addCalc(self.calcIntersectionRayPlane, "Find Intersection point of ray with norm and point of surface",
"o_x, o_y, o_z, d_x, d_y, d_z, n_x, n_y, n_z, p_x, p_y, p_z ")
self.addCalc(self.calcBaryXYZ, "Find x y z for given Barycentric ON EDGE", "v1x, v1y, v1z, v2x, v2y, v2z, v3x, "
"v3y, v3z, beta")
self.addCalc(self.camtransmatrix, "Compute the camera transformation matrix",
"Ux, Uy, Uz, Vx, Vy, Vz, Wx, Wy, Wz, Ex, Ey, Ez")
self.addCalc(self.calcRes, "Compute Resolution width or Hieght by image's width and height",
"dup_x, dup_y, other ")
self.prepare_screen()
self.show_message("What you wanna do?", bcolors.WARNING, 0, 0, 1, 0)
self.listOptions()
self.getCommand()
def getCommand(self):
req = input(bcolors.HEADER + "\n\n\tCommand: " + bcolors.ENDC)
if not req.isnumeric():
quit(31)
req = int(req)
if req > len(self.available_options):
self.__init__()
else:
self.getParameters(req - 1)
def getParameters(self, optId):
self.prepare_screen()
self.show_message(self.available_options[optId][1], bcolors.WARNING, 0, 0, 1, 0)
reqParams = map(str.strip, self.available_options[optId][2].split(","))
funcParams = []
for i in reqParams:
self.show_message(i + ": ", bcolors.WARNING, 0, 0, 2, 0, 0)
newParam = input(" ")
funcParams.append(newParam)
self.show_message("Result: " + "\n" + self.available_options[optId][0](funcParams),
bcolors.BOLD + bcolors.OKGREEN, 0,
0, 2, 0)
self.show_message("Press Enter to continue...", bcolors.BOLD, 0, 0, 1, 0, 0)
input()
self.__init__()
def listOptions(self):
for index, i in enumerate(self.available_options):
self.show_message(str(index + 1) + ") " + i[1] + " by using " + i[2], bcolors.BOLD, 0, 0, 1, 0)
def addCalc(self, function, description: str, paramList: str):
self.available_options.append((function, description, paramList))
def unitreflectionofnonunitvector(self, params):
Wx, Wy, Wz, Ndx, Ndy, Ndz = map(float, params)
w = np.array([Wx, Wy, Wz])
n = np.array([Ndx, Ndy, Ndz])
w /= np.linalg.norm(w)
n /= np.linalg.norm(n)
return str(-w + 2 * n * sum(n * w))
def transformationmatrix(self, params):
v1x, v1y, v1z, v2x, v2y, v2z, v3x, v3y, v3z = map(float, params)
T = np.array([[v1x, v1y, v1z],
[v2x, v2y, v2z],
[v3x, v3y, v3z]])
return str(np.linalg.inv(T))
def rotateSquare(self, params):
Centerx, Centery, EdgeLength, Degree = map(float, params)
cos = math.cos(math.radians(Degree))
sin = math.sin(math.radians(Degree))
T = np.array([[1,0,Centerx], [0,1,Centery], [0,0,1]])
R = np.array([[cos, -sin, 0],
[sin, cos, 0],
[0, 0, 1]])
T2 = np.array([[1,0,Centerx], [0,1,Centery], [0,0,1]])
return str((T*R*T2))
def pixelCalc(self, params):
Ex, Ey, Ez, Wx, Wy, Wz, Vx, Vy, Vz, distance, lBoundary, rBoundary, tBoundary, bBoundary, Nx, Ny = map(float, params)
e = np.array([Ex,Ey,Ez])
w = np.array([Wx,Wy,Wz])
v = np.array([Vx,Vy,Vz])
u = np.cross(v,w)
d=distance
l = lBoundary
r = rBoundary
t = tBoundary
b = bBoundary
nx= Nx
ny = Ny
m = e + w*d
q = m + l*u + t*v
su = 0.5*(r-l)/nx
sv = 0.5*(t-b)/ny
s = q+su*u-sv*v
return str(s)
def rayFind(self, params):
Ox, Oy, Oz, Dx, Dy, Dz, t = map(float, params)
o = np.array([Ox, Oy, Oz])
d = np.array([Dx, Dy, Dz])
return str((o + t*d).sum())
def spherediffuse(self, params):
CoefficientX, CoefficientY, CoefficientZ, CenteredX, CenteredY, CenteredZ, LSourceX, LSourceY, LSourceZ, LightIntensityX, LightIntensityY, LightIntensityZ, PointX, PointY, PointZ = map(
float, params)
kd = np.array([CoefficientX, CoefficientY, CoefficientZ])
I = np.array([LightIntensityX, LightIntensityY, LightIntensityZ])
p = np.array([PointX, PointY, PointZ])
lp = np.array([LSourceX, LSourceY, LSourceZ])
center = np.array([CenteredX, CenteredY, CenteredZ])
r = ((p - lp) ** 2).sum() ** 0.5
wi = lp - p
n = p - center
cosT = max(0, (unit(wi) * unit(n)).sum())
return str(kd * cosT * I)
def calcRes(self, params):
dup_x, dup_y, other = map(float, params)
return str(dup_x * other / dup_y)
def camtransmatrix(self, params):
Ux, Uy, Uz, Vx, Vy, Vz, Wx, Wy, Wz, Ex, Ey, Ez = map(float, params)
u = np.array([Ux, Uy, Uz])
v = np.array([Vx, Vy, Vz])
w = np.array([Wx, Wy, Wz])
e = np.array([Ex, Ey, Ez])
T = np.array([[u[0], u[1], u[2], -sum(u * e)],
[v[0], v[1], v[2], -sum(v * e)],
[w[0], w[1], w[2], -sum(w * e)],
[0, 0, 0, 1]])
return "\n" + str(T)
def textureBarytencric(self, params):
alpha, beta, alpha_assos_u1, alpha_assos_v1, beta_assos_u1, beta_assos_v1, o_u, o_v = map(float, params)
uP = o_u + beta * (beta_assos_u1 - o_u) + alpha * (alpha_assos_u1 - o_u)
vP = o_v + beta * (beta_assos_v1 - o_v) + alpha * (alpha_assos_v1 - o_v)
return "uP = " + str(uP) + " " + " vP = " + str(vP)
def calcIntersectionRayPlane(self, params):
o_x, o_y, o_z, d_x, d_y, d_z, n_x, n_y, n_z, p_x, p_y, p_z = map(float, params)
t_mult = d_x * n_x + d_y * n_y + d_z * n_z
t_cons = (o_x - p_x) * n_x + (o_y - p_y) * n_y + (o_z - p_z) * n_z
t = - t_cons / t_mult
return "x == " + str(o_x + t * d_x) + " y == " + str(o_y + t * d_y) + " z == " + str(o_z + t * d_z)
def luminanceCalculator(self, params):
contrast, voltage, gamma, brightness = params
res = float(contrast) * (float(voltage) ** float(gamma)) + float(brightness)
return str(res)
def calcBaryXYZ(self, params):
v1x, v1y, v1z, v2x, v2y, v2z, v3x, v3y, v3z, beta = map(float, params)
a = np.array([v1x, v1y, v1z])
b = np.array([v2x, v2y, v2z])
c = np.array([v3x, v3y, v3z])
alpha = 0
gamma = 0
P = a + beta * (b - a) + gamma * (c - a)
return str(P)
def prepare_screen(self):
os.system("cls" if os.name == 'nt' else "clear")
print(bcolors.OKGREEN +
"""
██ ██ ███████ ███████ ███████ ██████ ██ ██ ██ ███████ ██████
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
███████ ██ ██ ███████ ██ ██ ██ ██ ██ █████ ██████
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ██ ███████ ██████ ███████ ████ ███████ ██ ██
""" + bcolors.ENDC)
def show_message(self, message, text_type=bcolors.ENDC, clear_before=0, line=0, tab=1, space_before=0,
newLineAfter=1):
if clear_before:
print(bcolors.ENDC)
self.prepare_screen()
message = message.replace("\n", "\n" + (tab + 1) * "\t")
print("\n" * space_before + text_type + tab * "\t" + message + "\n" * line + bcolors.ENDC,
end="\n" if newLineAfter else "")
if __name__ == '__main__':
a = Solution()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment