From ca9cf7c00341aa9310ae6017f73604358f626e1a Mon Sep 17 00:00:00 2001 From: Diogo Andrade Date: Tue, 25 Aug 2020 19:58:25 +0100 Subject: [PATCH] Changed vector3 to Vector3, color to Color --- README.md | 12 +- camera.py | 8 +- color.py | 144 +++++----- doc/PyXYZ/camera.html | 22 +- doc/PyXYZ/color.html | 474 ++++++++++++++++---------------- doc/PyXYZ/material.html | 32 +-- doc/PyXYZ/mesh.html | 268 +++++++++--------- doc/PyXYZ/object3d.html | 94 +++---- doc/PyXYZ/sample_cubefall.html | 24 +- doc/PyXYZ/sample_game.html | 170 ++++++------ doc/PyXYZ/sample_hierarchy.html | 32 +-- doc/PyXYZ/sample_sphere.html | 24 +- doc/PyXYZ/sample_terrain.html | 68 ++--- doc/PyXYZ/vector3.html | 452 +++++++++++++++--------------- material.py | 10 +- mesh.py | 86 +++--- object3d.py | 30 +- sample_cubefall.py | 14 +- sample_game.py | 74 ++--- sample_hierarchy.py | 18 +- sample_sphere.py | 14 +- sample_terrain.py | 36 +-- vector3.py | 148 +++++----- 23 files changed, 1127 insertions(+), 1127 deletions(-) diff --git a/README.md b/README.md index 4346256..c175649 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ It provides very little functionality out-of-the- box: it allows for the program A scene is composed of 3D objects organized in an optional hierarchical fashion, and each object contains a polygonal mesh and a material that controls how the mesh is rendered. -At the most basic level, it has a few elementary helper classes, like Color, which describes a color with separate red, green, blue and alpha channels, and Vector3, a straightforward 3D vector implementation. +At the most basic level, it has a few elementary helper classes, like Color, which describes a Color with separate red, green, blue and alpha channels, and Vector3, a straightforward 3D vector implementation. The core of the engine is comprised of the Scene, Object3d, Camera and Mesh classes, which handle the rendering itself. @@ -33,7 +33,7 @@ The Camera is derived from Object3d, so that it can be treated in the same way, The Mesh contains a list of polygons, with each polygon being a list of vertex positions in local space. There are no indexing primitives as simplicity is the main driver of the engine. -The Material class stores the rendering properties like line color and width. A single material can be used by multiple meshes for rendering. +The Material class stores the rendering properties like line Color and width. A single material can be used by multiple meshes for rendering. The current implementation of PyXYZ uses Pygame for the actual render- ing. We chose Pygame for its simplicity, support for polygon rendering and full software implementation. @@ -79,13 +79,13 @@ Then, a scene can be setup: scene = Scene("TestScene") scene.camera = Camera(False , res_x , res_y) # Moves the camera back 2 units -scene.camera.position -= vector3(0,0,2) +scene.camera.position -= Vector3(0,0,2) # Create a sphere and place it in a scene, at position (0,0,0) obj1 = Object3d("TestObject") -obj1.scale = vector3(1, 1, 1) -obj1.position = vector3(0, 0, 0) +obj1.scale = Vector3(1, 1, 1) +obj1.position = Vector3(0, 0, 0) # Set the material of the sphere, in this case it is red -obj1.mesh = Mesh.create_sphere((1, 1, 1), 12, 12) obj1.material = Material(color(1,0,0,1), "TestMaterial1") scene.add_object(obj1) +obj1.mesh = Mesh.create_sphere((1, 1, 1), 12, 12) obj1.material = Material(Color(1,0,0,1), "TestMaterial1") scene.add_object(obj1) ~~~ To render the scene, the programmer just has to use: diff --git a/camera.py b/camera.py index 258617c..f721b6c 100644 --- a/camera.py +++ b/camera.py @@ -3,7 +3,7 @@ import math import numpy as np from quaternion import as_rotation_matrix -from vector3 import vector3 +from vector3 import Vector3 from object3d import Object3d class Camera(Object3d): @@ -108,11 +108,11 @@ def ray_from_ndc(self, pos): pos {2-tuple} -- Screen position in NDC (normalized device coordinates) Returns: - vector3, vector3 - Origin and direction of the ray corresponding to that screen + Vector3, Vector3 - Origin and direction of the ray corresponding to that screen positions """ - vpos = vector3(pos[0], pos[1], self.near_plane) + vpos = Vector3(pos[0], pos[1], self.near_plane) vpos.x = vpos.x * self.res_x * 0.5 vpos.y = -vpos.y * self.res_y * 0.5 @@ -120,6 +120,6 @@ def ray_from_ndc(self, pos): inv_view_proj_matrix = np.linalg.inv(inv_view_proj_matrix) direction = inv_view_proj_matrix @ vpos.to_np4(1) - direction = vector3.from_np(direction).normalized() + direction = Vector3.from_np(direction).normalized() return self.position + direction * self.near_plane, direction diff --git a/color.py b/color.py index f231afa..47d85c1 100644 --- a/color.py +++ b/color.py @@ -12,9 +12,9 @@ def __init__(self, op, type1, type2): def __str__(self): """Returns a readable version of the exception""" - return f"Invalid color operation ({self.op}) between {self.type1} and {self.type2}!" + return f"Invalid Color operation ({self.op}) between {self.type1} and {self.type2}!" -class color: +class Color: """Color class. It stores RGBA values as floats, in a range from 0 to 1. """ @@ -45,163 +45,163 @@ def __init__(self, r=0, g=0, b=0, a=1): stated otherwise, and even so it might require extra setup.""" def __str__(self): - """Converts the color to a displayable string + """Converts the Color to a displayable string Returns: String - Color in text format (r,g,b,a)""" return f"({self.r},{self.g},{self.b},{self.a})" def __add__(self, c): - """Adds this color to another. No validation of the output is done, i.e. any component - can overflow. If we try to add anything other than a color to a color, it throws the + """Adds this Color to another. No validation of the output is done, i.e. any component + can overflow. If we try to add anything other than a Color to a Color, it throws the InvalidColorOperationException. Arguments: - c {color} -- Color to add + c {Color} -- Color to add Returns: - Color - Sum of this color and the given one + Color - Sum of this Color and the given one """ - if isinstance(c, color): - return color(self.r + c.r, self.g + c.g, self.b + c.b, self.a + c.a) + if isinstance(c, Color): + return Color(self.r + c.r, self.g + c.g, self.b + c.b, self.a + c.a) else: raise InvalidColorOperationException("add", type(self), type(c)) def __sub__(self, c): - """Subtracts a color to this color. No validation of the output is done, i.e. - any component can underflow. If we try to subtract anything other than a color to - a color, it throws the InvalidColorOperationException. + """Subtracts a Color to this Color. No validation of the output is done, i.e. + any component can underflow. If we try to subtract anything other than a Color to + a Color, it throws the InvalidColorOperationException. Arguments: - c {color} -- Color to subtract + c {Color} -- Color to subtract Returns: - Color - Subraction of the given color from this color + Color - Subraction of the given Color from this Color """ - if isinstance(c, color): - return color(self.r - c.r, self.g - c.g, self.b - c.b, self.a - c.a) + if isinstance(c, Color): + return Color(self.r - c.r, self.g - c.g, self.b - c.b, self.a - c.a) else: raise InvalidColorOperationException("sub", type(self), type(c)) def __mul__(self, c): - """Multiplies this color by another color or a scalar. No validation of the output + """Multiplies this Color by another Color or a scalar. No validation of the output is done, i.e. any component can underflow. If we try to multiply anything other than a - color or a scalar, it throws the InvalidColorOperationException. + Color or a scalar, it throws the InvalidColorOperationException. Arguments: - c {color} -- Color to multiply: a component-wise multiplication is done + c {Color} -- Color to multiply: a component-wise multiplication is done or - c {number} -- Scalar to multiply: all components of the color are multiplied by + c {number} -- Scalar to multiply: all components of the Color are multiplied by this number Returns: - Color - Multiplication of the color + Color - Multiplication of the Color """ if isinstance(c, (int, float)): - return color(self.r * c, self.g * c, self.b * c, self.a * c) - elif isinstance(c, color): - return color(self.r * c.r, self.g * c.g, self.b * c.b, self.a * c.a) + return Color(self.r * c, self.g * c, self.b * c, self.a * c) + elif isinstance(c, Color): + return Color(self.r * c.r, self.g * c.g, self.b * c.b, self.a * c.a) else: raise InvalidColorOperationException("mult", type(self), type(c)) def __truediv__(self, c): - """Divides this color by a scalar. No validation of the output is done, i.e. any + """Divides this Color by a scalar. No validation of the output is done, i.e. any component can underflow. If we try to divide anything other than a scalar, it throws the InvalidColorOperationException. Arguments: - c {number} -- Scalar to divide: all components of the color are divided by this + c {number} -- Scalar to divide: all components of the Color are divided by this number Returns: Color - Color divided by the number """ if isinstance(c, (int, float)): - return color(self.r / c, self.g / c, self.b / c, self.a / c) + return Color(self.r / c, self.g / c, self.b / c, self.a / c) else: raise InvalidColorOperationException("mult", type(self), type(c)) def __eq__(self, c): - """Checks if this color is equal to the given one, with a tolerance of 0.0001. + """Checks if this Color is equal to the given one, with a tolerance of 0.0001. Exception InvalidColorOperationException is raised if we compare something other - than a color. + than a Color. Arguments: - c {color} -- Color to compare + c {Color} -- Color to compare Returns: Bool - True if the colors are the same, false otherwise """ - if isinstance(c, color): + if isinstance(c, Color): return ((self - c).magnitude()) < 0.0001 else: raise InvalidColorOperationException("eq", type(self), type(c)) def __ne__(self, c): - """Checks if this color is different to the given one, with a tolerance of 0.0001. + """Checks if this Color is different to the given one, with a tolerance of 0.0001. Exception InvalidColorOperationException is raised if we compare something other - than a color. + than a Color. Arguments: - c {color} -- Color to compare + c {Color} -- Color to compare Returns: Bool - True if the colors are different, false otherwise """ - if isinstance(c, color): + if isinstance(c, Color): return ((self - c).magnitude()) > 0.0001 else: raise InvalidColorOperationException("neq", type(self), type(c)) def __isub__(self, c): - """Subtracts a color to this color. No validation of the output is done, i.e. - any component can underflow. If we try to subtract anything other than a color to - a color, it throws the InvalidColorOperationException. + """Subtracts a Color to this Color. No validation of the output is done, i.e. + any component can underflow. If we try to subtract anything other than a Color to + a Color, it throws the InvalidColorOperationException. Arguments: - c {color} -- Color to subtract + c {Color} -- Color to subtract Returns: - Color - Subraction of the given color from this color + Color - Subraction of the given Color from this Color """ return self - c def __iadd__(self, c): - """Adds this color to another. No validation of the output is done, i.e. any component - can overflow. If we try to add anything other than a color to a color, it throws the + """Adds this Color to another. No validation of the output is done, i.e. any component + can overflow. If we try to add anything other than a Color to a Color, it throws the InvalidColorOperationException. Arguments: - c {color} -- Color to add + c {Color} -- Color to add Returns: - Color - Sum of this color and the given one + Color - Sum of this Color and the given one """ return self + c def __imul__(self, c): - """Multiplies this color by another color or a scalar. No validation of the output + """Multiplies this Color by another Color or a scalar. No validation of the output is done, i.e. any component can underflow. If we try to multiply anything other than - a color or a scalar, it throws the InvalidColorOperationException. + a Color or a scalar, it throws the InvalidColorOperationException. Arguments: - c {color} -- Color to multiply: a component-wise multiplication is done + c {Color} -- Color to multiply: a component-wise multiplication is done or - c {number} -- Scalar to multiply: all components of the color are multiplied by this + c {number} -- Scalar to multiply: all components of the Color are multiplied by this number Returns: - Color - Multiplication of the color + Color - Multiplication of the Color """ return self * c def __idiv__(self, c): - """Divides this color by a scalar. No validation of the output is done, i.e. any + """Divides this Color by a scalar. No validation of the output is done, i.e. any component can underflow. If we try to divide anything other than a scalar, it throws the InvalidColorOperationException. Arguments: - c {number} -- Scalar to divide: all components of the color are divided by this number + c {number} -- Scalar to divide: all components of the Color are divided by this number Returns: Color - Color divided by the number @@ -209,39 +209,39 @@ def __idiv__(self, c): return self / c def __neg__(self): - """Inverts this color. All components except for alpha are inverted. + """Inverts this Color. All components except for alpha are inverted. Returns: Color - Color (1-r, 1-g, 1-b, a) """ - return color(1-self.r, 1-self.g, 1-self.b, self.a) + return Color(1-self.r, 1-self.g, 1-self.b, self.a) def magnitude(self): - """Returns the magnitude of the color. + """Returns the magnitude of the Color. Returns: - Number - Magnitude of the color as a 4D vector + Number - Magnitude of the Color as a 4D vector """ return math.sqrt(self.dot(self)) def dot(self, c): - """Computes the dot product of this color with another. - If we try to do this operation with anything other than a color, it throws the + """Computes the dot product of this Color with another. + If we try to do this operation with anything other than a Color, it throws the InvalidColorOperationException. Arguments: - c {color} -- Color to do the dot product with + c {Color} -- Color to do the dot product with Returns: Number - Scalar value corresponding to the dot product of both colors """ - if isinstance(c, color): + if isinstance(c, Color): return self.r * c.r + self.g * c.g + self.b * c.b + self.a * c.a else: raise InvalidColorOperationException("dot", type(self), type(c)) def normalize(self): - """Normalizes this color, as if it was a 4D vector. + """Normalizes this Color, as if it was a 4D vector. """ d = 1.0 / self.magnitude() self.r *= d @@ -250,25 +250,25 @@ def normalize(self): self.a *= d def normalized(self): - """Returns the normalized version of this color, treating it as a 4D vector. + """Returns the normalized version of this Color, treating it as a 4D vector. Returns: - Color - Normalized color + Color - Normalized Color """ d = 1.0 / self.magnitude() - return color(self.r * d, self.g * d, self.b * d, self.a * d) + return Color(self.r * d, self.g * d, self.b * d, self.a * d) def premult_alpha(self): """Multiplies the RGB components with the alpha component, for use with pre-multiplied - alpha blend mode and returns this new color. + alpha blend mode and returns this new Color. Returns: - Color - Premultiplied color + Color - Premultiplied Color """ - return color(self.r * self.a, self.g * self.a, self.b * self.a, self.a) + return Color(self.r * self.a, self.g * self.a, self.b * self.a, self.a) def tuple3(self): - """Converts a color to a 3-tuple, to be used with Pygame + """Converts a Color to a 3-tuple, to be used with Pygame Returns: Tuple - (r * 255, g * 255, b * 255) @@ -276,7 +276,7 @@ def tuple3(self): return (self.r * 255, self.g * 255, self.b * 255) def tuple4(self): - """Converts a color to a 4-tuple, to be used with Pygame + """Converts a Color to a 4-tuple, to be used with Pygame Returns: Tuple - (r * 255, g * 255, b * 255, a * 255) @@ -284,7 +284,7 @@ def tuple4(self): return (self.r * 255, self.g * 255, self.b * 255, self.a * 255) def saturate(self): - """Clamps all the color components between the valid [0..1] range + """Clamps all the Color components between the valid [0..1] range """ self.r = min(max(self.r, 0), 1) self.g = min(max(self.g, 0), 1) @@ -292,12 +292,12 @@ def saturate(self): self.a = min(max(self.a, 0), 1) def saturated(self): - """Returns a clamped version of this color + """Returns a clamped version of this Color Returns: - Color - Clamped color + Color - Clamped Color """ - return color( + return Color( min(max(self.r, 0), 1), min(max(self.g, 0), 1), min(max(self.b, 0), 1), diff --git a/doc/PyXYZ/camera.html b/doc/PyXYZ/camera.html index a308f23..5bc3f71 100644 --- a/doc/PyXYZ/camera.html +++ b/doc/PyXYZ/camera.html @@ -32,7 +32,7 @@

Module PyXYZ.camera

import math import numpy as np from quaternion import as_rotation_matrix -from vector3 import vector3 +from vector3 import Vector3 from object3d import Object3d class Camera(Object3d): @@ -137,11 +137,11 @@

Module PyXYZ.camera

pos {2-tuple} -- Screen position in NDC (normalized device coordinates) Returns: - vector3, vector3 - Origin and direction of the ray corresponding to that screen + Vector3, Vector3 - Origin and direction of the ray corresponding to that screen positions """ - vpos = vector3(pos[0], pos[1], self.near_plane) + vpos = Vector3(pos[0], pos[1], self.near_plane) vpos.x = vpos.x * self.res_x * 0.5 vpos.y = -vpos.y * self.res_y * 0.5 @@ -149,7 +149,7 @@

Module PyXYZ.camera

inv_view_proj_matrix = np.linalg.inv(inv_view_proj_matrix) direction = inv_view_proj_matrix @ vpos.to_np4(1) - direction = vector3.from_np(direction).normalized() + direction = Vector3.from_np(direction).normalized() return self.position + direction * self.near_plane, direction @@ -282,11 +282,11 @@

Arguments

pos {2-tuple} -- Screen position in NDC (normalized device coordinates) Returns: - vector3, vector3 - Origin and direction of the ray corresponding to that screen + Vector3, Vector3 - Origin and direction of the ray corresponding to that screen positions """ - vpos = vector3(pos[0], pos[1], self.near_plane) + vpos = Vector3(pos[0], pos[1], self.near_plane) vpos.x = vpos.x * self.res_x * 0.5 vpos.y = -vpos.y * self.res_y * 0.5 @@ -294,7 +294,7 @@

Arguments

inv_view_proj_matrix = np.linalg.inv(inv_view_proj_matrix) direction = inv_view_proj_matrix @ vpos.to_np4(1) - direction = vector3.from_np(direction).normalized() + direction = Vector3.from_np(direction).normalized() return self.position + direction * self.near_plane, direction @@ -432,7 +432,7 @@

Returns

Arguments

pos {2-tuple} – Screen position in NDC (normalized device coordinates)

Returns

-

vector3, vector3 - Origin and direction of the ray corresponding to that screen +

Vector3, Vector3 - Origin and direction of the ray corresponding to that screen positions

@@ -453,11 +453,11 @@

Returns

pos {2-tuple} -- Screen position in NDC (normalized device coordinates) Returns: - vector3, vector3 - Origin and direction of the ray corresponding to that screen + Vector3, Vector3 - Origin and direction of the ray corresponding to that screen positions """ - vpos = vector3(pos[0], pos[1], self.near_plane) + vpos = Vector3(pos[0], pos[1], self.near_plane) vpos.x = vpos.x * self.res_x * 0.5 vpos.y = -vpos.y * self.res_y * 0.5 @@ -465,7 +465,7 @@

Returns

inv_view_proj_matrix = np.linalg.inv(inv_view_proj_matrix) direction = inv_view_proj_matrix @ vpos.to_np4(1) - direction = vector3.from_np(direction).normalized() + direction = Vector3.from_np(direction).normalized() return self.position + direction * self.near_plane, direction
diff --git a/doc/PyXYZ/color.html b/doc/PyXYZ/color.html index b4e9ff4..6a8124f 100644 --- a/doc/PyXYZ/color.html +++ b/doc/PyXYZ/color.html @@ -41,9 +41,9 @@

Module PyXYZ.color

def __str__(self): """Returns a readable version of the exception""" - return f"Invalid color operation ({self.op}) between {self.type1} and {self.type2}!" + return f"Invalid Color operation ({self.op}) between {self.type1} and {self.type2}!" -class color: +class Color: """Color class. It stores RGBA values as floats, in a range from 0 to 1. """ @@ -74,163 +74,163 @@

Module PyXYZ.color

stated otherwise, and even so it might require extra setup.""" def __str__(self): - """Converts the color to a displayable string + """Converts the Color to a displayable string Returns: String - Color in text format (r,g,b,a)""" return f"({self.r},{self.g},{self.b},{self.a})" def __add__(self, c): - """Adds this color to another. No validation of the output is done, i.e. any component - can overflow. If we try to add anything other than a color to a color, it throws the + """Adds this Color to another. No validation of the output is done, i.e. any component + can overflow. If we try to add anything other than a Color to a Color, it throws the InvalidColorOperationException. Arguments: - c {color} -- Color to add + c {Color} -- Color to add Returns: - Color - Sum of this color and the given one + Color - Sum of this Color and the given one """ - if isinstance(c, color): - return color(self.r + c.r, self.g + c.g, self.b + c.b, self.a + c.a) + if isinstance(c, Color): + return Color(self.r + c.r, self.g + c.g, self.b + c.b, self.a + c.a) else: raise InvalidColorOperationException("add", type(self), type(c)) def __sub__(self, c): - """Subtracts a color to this color. No validation of the output is done, i.e. - any component can underflow. If we try to subtract anything other than a color to - a color, it throws the InvalidColorOperationException. + """Subtracts a Color to this Color. No validation of the output is done, i.e. + any component can underflow. If we try to subtract anything other than a Color to + a Color, it throws the InvalidColorOperationException. Arguments: - c {color} -- Color to subtract + c {Color} -- Color to subtract Returns: - Color - Subraction of the given color from this color + Color - Subraction of the given Color from this Color """ - if isinstance(c, color): - return color(self.r - c.r, self.g - c.g, self.b - c.b, self.a - c.a) + if isinstance(c, Color): + return Color(self.r - c.r, self.g - c.g, self.b - c.b, self.a - c.a) else: raise InvalidColorOperationException("sub", type(self), type(c)) def __mul__(self, c): - """Multiplies this color by another color or a scalar. No validation of the output + """Multiplies this Color by another Color or a scalar. No validation of the output is done, i.e. any component can underflow. If we try to multiply anything other than a - color or a scalar, it throws the InvalidColorOperationException. + Color or a scalar, it throws the InvalidColorOperationException. Arguments: - c {color} -- Color to multiply: a component-wise multiplication is done + c {Color} -- Color to multiply: a component-wise multiplication is done or - c {number} -- Scalar to multiply: all components of the color are multiplied by + c {number} -- Scalar to multiply: all components of the Color are multiplied by this number Returns: - Color - Multiplication of the color + Color - Multiplication of the Color """ if isinstance(c, (int, float)): - return color(self.r * c, self.g * c, self.b * c, self.a * c) - elif isinstance(c, color): - return color(self.r * c.r, self.g * c.g, self.b * c.b, self.a * c.a) + return Color(self.r * c, self.g * c, self.b * c, self.a * c) + elif isinstance(c, Color): + return Color(self.r * c.r, self.g * c.g, self.b * c.b, self.a * c.a) else: raise InvalidColorOperationException("mult", type(self), type(c)) def __truediv__(self, c): - """Divides this color by a scalar. No validation of the output is done, i.e. any + """Divides this Color by a scalar. No validation of the output is done, i.e. any component can underflow. If we try to divide anything other than a scalar, it throws the InvalidColorOperationException. Arguments: - c {number} -- Scalar to divide: all components of the color are divided by this + c {number} -- Scalar to divide: all components of the Color are divided by this number Returns: Color - Color divided by the number """ if isinstance(c, (int, float)): - return color(self.r / c, self.g / c, self.b / c, self.a / c) + return Color(self.r / c, self.g / c, self.b / c, self.a / c) else: raise InvalidColorOperationException("mult", type(self), type(c)) def __eq__(self, c): - """Checks if this color is equal to the given one, with a tolerance of 0.0001. + """Checks if this Color is equal to the given one, with a tolerance of 0.0001. Exception InvalidColorOperationException is raised if we compare something other - than a color. + than a Color. Arguments: - c {color} -- Color to compare + c {Color} -- Color to compare Returns: Bool - True if the colors are the same, false otherwise """ - if isinstance(c, color): + if isinstance(c, Color): return ((self - c).magnitude()) < 0.0001 else: raise InvalidColorOperationException("eq", type(self), type(c)) def __ne__(self, c): - """Checks if this color is different to the given one, with a tolerance of 0.0001. + """Checks if this Color is different to the given one, with a tolerance of 0.0001. Exception InvalidColorOperationException is raised if we compare something other - than a color. + than a Color. Arguments: - c {color} -- Color to compare + c {Color} -- Color to compare Returns: Bool - True if the colors are different, false otherwise """ - if isinstance(c, color): + if isinstance(c, Color): return ((self - c).magnitude()) > 0.0001 else: raise InvalidColorOperationException("neq", type(self), type(c)) def __isub__(self, c): - """Subtracts a color to this color. No validation of the output is done, i.e. - any component can underflow. If we try to subtract anything other than a color to - a color, it throws the InvalidColorOperationException. + """Subtracts a Color to this Color. No validation of the output is done, i.e. + any component can underflow. If we try to subtract anything other than a Color to + a Color, it throws the InvalidColorOperationException. Arguments: - c {color} -- Color to subtract + c {Color} -- Color to subtract Returns: - Color - Subraction of the given color from this color + Color - Subraction of the given Color from this Color """ return self - c def __iadd__(self, c): - """Adds this color to another. No validation of the output is done, i.e. any component - can overflow. If we try to add anything other than a color to a color, it throws the + """Adds this Color to another. No validation of the output is done, i.e. any component + can overflow. If we try to add anything other than a Color to a Color, it throws the InvalidColorOperationException. Arguments: - c {color} -- Color to add + c {Color} -- Color to add Returns: - Color - Sum of this color and the given one + Color - Sum of this Color and the given one """ return self + c def __imul__(self, c): - """Multiplies this color by another color or a scalar. No validation of the output + """Multiplies this Color by another Color or a scalar. No validation of the output is done, i.e. any component can underflow. If we try to multiply anything other than - a color or a scalar, it throws the InvalidColorOperationException. + a Color or a scalar, it throws the InvalidColorOperationException. Arguments: - c {color} -- Color to multiply: a component-wise multiplication is done + c {Color} -- Color to multiply: a component-wise multiplication is done or - c {number} -- Scalar to multiply: all components of the color are multiplied by this + c {number} -- Scalar to multiply: all components of the Color are multiplied by this number Returns: - Color - Multiplication of the color + Color - Multiplication of the Color """ return self * c def __idiv__(self, c): - """Divides this color by a scalar. No validation of the output is done, i.e. any + """Divides this Color by a scalar. No validation of the output is done, i.e. any component can underflow. If we try to divide anything other than a scalar, it throws the InvalidColorOperationException. Arguments: - c {number} -- Scalar to divide: all components of the color are divided by this number + c {number} -- Scalar to divide: all components of the Color are divided by this number Returns: Color - Color divided by the number @@ -238,39 +238,39 @@

Module PyXYZ.color

return self / c def __neg__(self): - """Inverts this color. All components except for alpha are inverted. + """Inverts this Color. All components except for alpha are inverted. Returns: Color - Color (1-r, 1-g, 1-b, a) """ - return color(1-self.r, 1-self.g, 1-self.b, self.a) + return Color(1-self.r, 1-self.g, 1-self.b, self.a) def magnitude(self): - """Returns the magnitude of the color. + """Returns the magnitude of the Color. Returns: - Number - Magnitude of the color as a 4D vector + Number - Magnitude of the Color as a 4D vector """ return math.sqrt(self.dot(self)) def dot(self, c): - """Computes the dot product of this color with another. - If we try to do this operation with anything other than a color, it throws the + """Computes the dot product of this Color with another. + If we try to do this operation with anything other than a Color, it throws the InvalidColorOperationException. Arguments: - c {color} -- Color to do the dot product with + c {Color} -- Color to do the dot product with Returns: Number - Scalar value corresponding to the dot product of both colors """ - if isinstance(c, color): + if isinstance(c, Color): return self.r * c.r + self.g * c.g + self.b * c.b + self.a * c.a else: raise InvalidColorOperationException("dot", type(self), type(c)) def normalize(self): - """Normalizes this color, as if it was a 4D vector. + """Normalizes this Color, as if it was a 4D vector. """ d = 1.0 / self.magnitude() self.r *= d @@ -279,25 +279,25 @@

Module PyXYZ.color

self.a *= d def normalized(self): - """Returns the normalized version of this color, treating it as a 4D vector. + """Returns the normalized version of this Color, treating it as a 4D vector. Returns: - Color - Normalized color + Color - Normalized Color """ d = 1.0 / self.magnitude() - return color(self.r * d, self.g * d, self.b * d, self.a * d) + return Color(self.r * d, self.g * d, self.b * d, self.a * d) def premult_alpha(self): """Multiplies the RGB components with the alpha component, for use with pre-multiplied - alpha blend mode and returns this new color. + alpha blend mode and returns this new Color. Returns: - Color - Premultiplied color + Color - Premultiplied Color """ - return color(self.r * self.a, self.g * self.a, self.b * self.a, self.a) + return Color(self.r * self.a, self.g * self.a, self.b * self.a, self.a) def tuple3(self): - """Converts a color to a 3-tuple, to be used with Pygame + """Converts a Color to a 3-tuple, to be used with Pygame Returns: Tuple - (r * 255, g * 255, b * 255) @@ -305,7 +305,7 @@

Module PyXYZ.color

return (self.r * 255, self.g * 255, self.b * 255) def tuple4(self): - """Converts a color to a 4-tuple, to be used with Pygame + """Converts a Color to a 4-tuple, to be used with Pygame Returns: Tuple - (r * 255, g * 255, b * 255, a * 255) @@ -313,7 +313,7 @@

Module PyXYZ.color

return (self.r * 255, self.g * 255, self.b * 255, self.a * 255) def saturate(self): - """Clamps all the color components between the valid [0..1] range + """Clamps all the Color components between the valid [0..1] range """ self.r = min(max(self.r, 0), 1) self.g = min(max(self.g, 0), 1) @@ -321,12 +321,12 @@

Module PyXYZ.color

self.a = min(max(self.a, 0), 1) def saturated(self): - """Returns a clamped version of this color + """Returns a clamped version of this Color Returns: - Color - Clamped color + Color - Clamped Color """ - return color( + return Color( min(max(self.r, 0), 1), min(max(self.g, 0), 1), min(max(self.b, 0), 1), @@ -343,36 +343,8 @@

Module PyXYZ.color

Classes

-
-class InvalidColorOperationException -(op, type1, type2) -
-
-

Exception thrown when there's an invalid operation with colors

-
- -Expand source code - -
class InvalidColorOperationException(Exception):
-    """Exception thrown when there's an invalid operation with colors"""
-    def __init__(self, op, type1, type2):
-        super().__init__(self)
-        self.op = op
-        self.type1 = type1
-        self.type2 = type2
-
-    def __str__(self):
-        """Returns a readable version of the exception"""
-        return f"Invalid color operation ({self.op}) between {self.type1} and {self.type2}!"
-
-

Ancestors

-
    -
  • builtins.Exception
  • -
  • builtins.BaseException
  • -
-
-
-class color +
+class Color (r=0, g=0, b=0, a=1)
@@ -387,7 +359,7 @@

Arguments

Expand source code -
class color:
+
class Color:
     """Color class.
     It stores RGBA values as floats, in a range from 0 to 1.
     """
@@ -418,163 +390,163 @@ 

Arguments

stated otherwise, and even so it might require extra setup.""" def __str__(self): - """Converts the color to a displayable string + """Converts the Color to a displayable string Returns: String - Color in text format (r,g,b,a)""" return f"({self.r},{self.g},{self.b},{self.a})" def __add__(self, c): - """Adds this color to another. No validation of the output is done, i.e. any component - can overflow. If we try to add anything other than a color to a color, it throws the + """Adds this Color to another. No validation of the output is done, i.e. any component + can overflow. If we try to add anything other than a Color to a Color, it throws the InvalidColorOperationException. Arguments: - c {color} -- Color to add + c {Color} -- Color to add Returns: - Color - Sum of this color and the given one + Color - Sum of this Color and the given one """ - if isinstance(c, color): - return color(self.r + c.r, self.g + c.g, self.b + c.b, self.a + c.a) + if isinstance(c, Color): + return Color(self.r + c.r, self.g + c.g, self.b + c.b, self.a + c.a) else: raise InvalidColorOperationException("add", type(self), type(c)) def __sub__(self, c): - """Subtracts a color to this color. No validation of the output is done, i.e. - any component can underflow. If we try to subtract anything other than a color to - a color, it throws the InvalidColorOperationException. + """Subtracts a Color to this Color. No validation of the output is done, i.e. + any component can underflow. If we try to subtract anything other than a Color to + a Color, it throws the InvalidColorOperationException. Arguments: - c {color} -- Color to subtract + c {Color} -- Color to subtract Returns: - Color - Subraction of the given color from this color + Color - Subraction of the given Color from this Color """ - if isinstance(c, color): - return color(self.r - c.r, self.g - c.g, self.b - c.b, self.a - c.a) + if isinstance(c, Color): + return Color(self.r - c.r, self.g - c.g, self.b - c.b, self.a - c.a) else: raise InvalidColorOperationException("sub", type(self), type(c)) def __mul__(self, c): - """Multiplies this color by another color or a scalar. No validation of the output + """Multiplies this Color by another Color or a scalar. No validation of the output is done, i.e. any component can underflow. If we try to multiply anything other than a - color or a scalar, it throws the InvalidColorOperationException. + Color or a scalar, it throws the InvalidColorOperationException. Arguments: - c {color} -- Color to multiply: a component-wise multiplication is done + c {Color} -- Color to multiply: a component-wise multiplication is done or - c {number} -- Scalar to multiply: all components of the color are multiplied by + c {number} -- Scalar to multiply: all components of the Color are multiplied by this number Returns: - Color - Multiplication of the color + Color - Multiplication of the Color """ if isinstance(c, (int, float)): - return color(self.r * c, self.g * c, self.b * c, self.a * c) - elif isinstance(c, color): - return color(self.r * c.r, self.g * c.g, self.b * c.b, self.a * c.a) + return Color(self.r * c, self.g * c, self.b * c, self.a * c) + elif isinstance(c, Color): + return Color(self.r * c.r, self.g * c.g, self.b * c.b, self.a * c.a) else: raise InvalidColorOperationException("mult", type(self), type(c)) def __truediv__(self, c): - """Divides this color by a scalar. No validation of the output is done, i.e. any + """Divides this Color by a scalar. No validation of the output is done, i.e. any component can underflow. If we try to divide anything other than a scalar, it throws the InvalidColorOperationException. Arguments: - c {number} -- Scalar to divide: all components of the color are divided by this + c {number} -- Scalar to divide: all components of the Color are divided by this number Returns: Color - Color divided by the number """ if isinstance(c, (int, float)): - return color(self.r / c, self.g / c, self.b / c, self.a / c) + return Color(self.r / c, self.g / c, self.b / c, self.a / c) else: raise InvalidColorOperationException("mult", type(self), type(c)) def __eq__(self, c): - """Checks if this color is equal to the given one, with a tolerance of 0.0001. + """Checks if this Color is equal to the given one, with a tolerance of 0.0001. Exception InvalidColorOperationException is raised if we compare something other - than a color. + than a Color. Arguments: - c {color} -- Color to compare + c {Color} -- Color to compare Returns: Bool - True if the colors are the same, false otherwise """ - if isinstance(c, color): + if isinstance(c, Color): return ((self - c).magnitude()) < 0.0001 else: raise InvalidColorOperationException("eq", type(self), type(c)) def __ne__(self, c): - """Checks if this color is different to the given one, with a tolerance of 0.0001. + """Checks if this Color is different to the given one, with a tolerance of 0.0001. Exception InvalidColorOperationException is raised if we compare something other - than a color. + than a Color. Arguments: - c {color} -- Color to compare + c {Color} -- Color to compare Returns: Bool - True if the colors are different, false otherwise """ - if isinstance(c, color): + if isinstance(c, Color): return ((self - c).magnitude()) > 0.0001 else: raise InvalidColorOperationException("neq", type(self), type(c)) def __isub__(self, c): - """Subtracts a color to this color. No validation of the output is done, i.e. - any component can underflow. If we try to subtract anything other than a color to - a color, it throws the InvalidColorOperationException. + """Subtracts a Color to this Color. No validation of the output is done, i.e. + any component can underflow. If we try to subtract anything other than a Color to + a Color, it throws the InvalidColorOperationException. Arguments: - c {color} -- Color to subtract + c {Color} -- Color to subtract Returns: - Color - Subraction of the given color from this color + Color - Subraction of the given Color from this Color """ return self - c def __iadd__(self, c): - """Adds this color to another. No validation of the output is done, i.e. any component - can overflow. If we try to add anything other than a color to a color, it throws the + """Adds this Color to another. No validation of the output is done, i.e. any component + can overflow. If we try to add anything other than a Color to a Color, it throws the InvalidColorOperationException. Arguments: - c {color} -- Color to add + c {Color} -- Color to add Returns: - Color - Sum of this color and the given one + Color - Sum of this Color and the given one """ return self + c def __imul__(self, c): - """Multiplies this color by another color or a scalar. No validation of the output + """Multiplies this Color by another Color or a scalar. No validation of the output is done, i.e. any component can underflow. If we try to multiply anything other than - a color or a scalar, it throws the InvalidColorOperationException. + a Color or a scalar, it throws the InvalidColorOperationException. Arguments: - c {color} -- Color to multiply: a component-wise multiplication is done + c {Color} -- Color to multiply: a component-wise multiplication is done or - c {number} -- Scalar to multiply: all components of the color are multiplied by this + c {number} -- Scalar to multiply: all components of the Color are multiplied by this number Returns: - Color - Multiplication of the color + Color - Multiplication of the Color """ return self * c def __idiv__(self, c): - """Divides this color by a scalar. No validation of the output is done, i.e. any + """Divides this Color by a scalar. No validation of the output is done, i.e. any component can underflow. If we try to divide anything other than a scalar, it throws the InvalidColorOperationException. Arguments: - c {number} -- Scalar to divide: all components of the color are divided by this number + c {number} -- Scalar to divide: all components of the Color are divided by this number Returns: Color - Color divided by the number @@ -582,39 +554,39 @@

Arguments

return self / c def __neg__(self): - """Inverts this color. All components except for alpha are inverted. + """Inverts this Color. All components except for alpha are inverted. Returns: Color - Color (1-r, 1-g, 1-b, a) """ - return color(1-self.r, 1-self.g, 1-self.b, self.a) + return Color(1-self.r, 1-self.g, 1-self.b, self.a) def magnitude(self): - """Returns the magnitude of the color. + """Returns the magnitude of the Color. Returns: - Number - Magnitude of the color as a 4D vector + Number - Magnitude of the Color as a 4D vector """ return math.sqrt(self.dot(self)) def dot(self, c): - """Computes the dot product of this color with another. - If we try to do this operation with anything other than a color, it throws the + """Computes the dot product of this Color with another. + If we try to do this operation with anything other than a Color, it throws the InvalidColorOperationException. Arguments: - c {color} -- Color to do the dot product with + c {Color} -- Color to do the dot product with Returns: Number - Scalar value corresponding to the dot product of both colors """ - if isinstance(c, color): + if isinstance(c, Color): return self.r * c.r + self.g * c.g + self.b * c.b + self.a * c.a else: raise InvalidColorOperationException("dot", type(self), type(c)) def normalize(self): - """Normalizes this color, as if it was a 4D vector. + """Normalizes this Color, as if it was a 4D vector. """ d = 1.0 / self.magnitude() self.r *= d @@ -623,25 +595,25 @@

Arguments

self.a *= d def normalized(self): - """Returns the normalized version of this color, treating it as a 4D vector. + """Returns the normalized version of this Color, treating it as a 4D vector. Returns: - Color - Normalized color + Color - Normalized Color """ d = 1.0 / self.magnitude() - return color(self.r * d, self.g * d, self.b * d, self.a * d) + return Color(self.r * d, self.g * d, self.b * d, self.a * d) def premult_alpha(self): """Multiplies the RGB components with the alpha component, for use with pre-multiplied - alpha blend mode and returns this new color. + alpha blend mode and returns this new Color. Returns: - Color - Premultiplied color + Color - Premultiplied Color """ - return color(self.r * self.a, self.g * self.a, self.b * self.a, self.a) + return Color(self.r * self.a, self.g * self.a, self.b * self.a, self.a) def tuple3(self): - """Converts a color to a 3-tuple, to be used with Pygame + """Converts a Color to a 3-tuple, to be used with Pygame Returns: Tuple - (r * 255, g * 255, b * 255) @@ -649,7 +621,7 @@

Arguments

return (self.r * 255, self.g * 255, self.b * 255) def tuple4(self): - """Converts a color to a 4-tuple, to be used with Pygame + """Converts a Color to a 4-tuple, to be used with Pygame Returns: Tuple - (r * 255, g * 255, b * 255, a * 255) @@ -657,7 +629,7 @@

Arguments

return (self.r * 255, self.g * 255, self.b * 255, self.a * 255) def saturate(self): - """Clamps all the color components between the valid [0..1] range + """Clamps all the Color components between the valid [0..1] range """ self.r = min(max(self.r, 0), 1) self.g = min(max(self.g, 0), 1) @@ -665,12 +637,12 @@

Arguments

self.a = min(max(self.a, 0), 1) def saturated(self): - """Returns a clamped version of this color + """Returns a clamped version of this Color Returns: - Color - Clamped color + Color - Clamped Color """ - return color( + return Color( min(max(self.r, 0), 1), min(max(self.g, 0), 1), min(max(self.b, 0), 1), @@ -679,23 +651,23 @@

Arguments

Instance variables

-
var a
+
var a

{number} Alpha component. Should be in the [0..1] range, although there is no guarantees. Note that most Pygame functions don't use the alpha component, unless stated otherwise, and even so it might require extra setup.

-
var b
+
var b

{number} Blue component. Should be in the [0..1] range, although there is no guarantees

-
var g
+
var g

{number} Green component. Should be in the [0..1] range, although there is no guarantees

-
var r
+
var r

{number} Red component. Should be in the [0..1] range, although there is no guarantees

@@ -703,15 +675,15 @@

Instance variables

Methods

-
+
def dot(self, c)
-

Computes the dot product of this color with another. -If we try to do this operation with anything other than a color, it throws the +

Computes the dot product of this Color with another. +If we try to do this operation with anything other than a Color, it throws the InvalidColorOperationException.

Arguments

-

c {color} – Color to do the dot product with

+

c {Color} – Color to do the dot product with

Returns

Number - Scalar value corresponding to the dot product of both colors

@@ -719,53 +691,53 @@

Returns

Expand source code
def dot(self, c):
-    """Computes the dot product of this color with another.
-    If we try to do this operation with anything other than a color, it throws the
+    """Computes the dot product of this Color with another.
+    If we try to do this operation with anything other than a Color, it throws the
     InvalidColorOperationException.
 
     Arguments:
-        c {color} -- Color to do the dot product with
+        c {Color} -- Color to do the dot product with
 
     Returns:
         Number - Scalar value corresponding to the dot product of both colors
     """
-    if isinstance(c, color):
+    if isinstance(c, Color):
         return self.r * c.r + self.g * c.g + self.b * c.b + self.a * c.a
     else:
         raise InvalidColorOperationException("dot", type(self), type(c))
-
+
def magnitude(self)
-

Returns the magnitude of the color.

+

Returns the magnitude of the Color.

Returns

-

Number - Magnitude of the color as a 4D vector

+

Number - Magnitude of the Color as a 4D vector

Expand source code
def magnitude(self):
-    """Returns the magnitude of the color.
+    """Returns the magnitude of the Color.
 
     Returns:
-        Number - Magnitude of the color as a 4D vector
+        Number - Magnitude of the Color as a 4D vector
     """
     return math.sqrt(self.dot(self))
-
+
def normalize(self)
-

Normalizes this color, as if it was a 4D vector.

+

Normalizes this Color, as if it was a 4D vector.

Expand source code
def normalize(self):
-    """Normalizes this color, as if it was a 4D vector.
+    """Normalizes this Color, as if it was a 4D vector.
     """
     d = 1.0 / self.magnitude()
     self.r *= d
@@ -774,60 +746,60 @@ 

Returns

self.a *= d
-
+
def normalized(self)
-

Returns the normalized version of this color, treating it as a 4D vector.

+

Returns the normalized version of this Color, treating it as a 4D vector.

Returns

-

Color - Normalized color

+

Color - Normalized Color

Expand source code
def normalized(self):
-    """Returns the normalized version of this color, treating it as a 4D vector.
+    """Returns the normalized version of this Color, treating it as a 4D vector.
 
     Returns:
-        Color - Normalized color
+        Color - Normalized Color
     """
     d = 1.0 / self.magnitude()
-    return color(self.r * d, self.g * d, self.b * d, self.a * d)
+ return Color(self.r * d, self.g * d, self.b * d, self.a * d)
-
+
def premult_alpha(self)

Multiplies the RGB components with the alpha component, for use with pre-multiplied -alpha blend mode and returns this new color.

+alpha blend mode and returns this new Color.

Returns

-

Color - Premultiplied color

+

Color - Premultiplied Color

Expand source code
def premult_alpha(self):
     """Multiplies the RGB components with the alpha component, for use with pre-multiplied
-    alpha blend mode and returns this new color.
+    alpha blend mode and returns this new Color.
 
     Returns:
-        Color - Premultiplied color
+        Color - Premultiplied Color
     """
-    return color(self.r * self.a, self.g * self.a, self.b * self.a, self.a)
+ return Color(self.r * self.a, self.g * self.a, self.b * self.a, self.a)
-
+
def saturate(self)
-

Clamps all the color components between the valid [0..1] range

+

Clamps all the Color components between the valid [0..1] range

Expand source code
def saturate(self):
-    """Clamps all the color components between the valid [0..1] range
+    """Clamps all the Color components between the valid [0..1] range
     """
     self.r = min(max(self.r, 0), 1)
     self.g = min(max(self.g, 0), 1)
@@ -835,24 +807,24 @@ 

Returns

self.a = min(max(self.a, 0), 1)
-
+
def saturated(self)
-

Returns a clamped version of this color

+

Returns a clamped version of this Color

Returns

-

Color - Clamped color

+

Color - Clamped Color

Expand source code
def saturated(self):
-    """Returns a clamped version of this color
+    """Returns a clamped version of this Color
 
     Returns:
-        Color - Clamped color
+        Color - Clamped Color
     """
-    return color(
+    return Color(
         min(max(self.r, 0), 1),
         min(max(self.g, 0), 1),
         min(max(self.b, 0), 1),
@@ -860,11 +832,11 @@ 

Returns

)
-
+
def tuple3(self)
-

Converts a color to a 3-tuple, to be used with Pygame

+

Converts a Color to a 3-tuple, to be used with Pygame

Returns

Tuple - (r * 255, g * 255, b * 255)

@@ -872,7 +844,7 @@

Returns

Expand source code
def tuple3(self):
-    """Converts a color to a 3-tuple, to be used with Pygame
+    """Converts a Color to a 3-tuple, to be used with Pygame
 
     Returns:
         Tuple - (r * 255, g * 255, b * 255)
@@ -880,11 +852,11 @@ 

Returns

return (self.r * 255, self.g * 255, self.b * 255)
-
+
def tuple4(self)
-

Converts a color to a 4-tuple, to be used with Pygame

+

Converts a Color to a 4-tuple, to be used with Pygame

Returns

Tuple - (r * 255, g * 255, b * 255, a * 255)

@@ -892,7 +864,7 @@

Returns

Expand source code
def tuple4(self):
-    """Converts a color to a 4-tuple, to be used with Pygame
+    """Converts a Color to a 4-tuple, to be used with Pygame
 
     Returns:
         Tuple - (r * 255, g * 255, b * 255, a * 255)
@@ -902,6 +874,34 @@ 

Returns

+
+class InvalidColorOperationException +(op, type1, type2) +
+
+

Exception thrown when there's an invalid operation with colors

+
+ +Expand source code + +
class InvalidColorOperationException(Exception):
+    """Exception thrown when there's an invalid operation with colors"""
+    def __init__(self, op, type1, type2):
+        super().__init__(self)
+        self.op = op
+        self.type1 = type1
+        self.type2 = type2
+
+    def __str__(self):
+        """Returns a readable version of the exception"""
+        return f"Invalid Color operation ({self.op}) between {self.type1} and {self.type2}!"
+
+

Ancestors

+
    +
  • builtins.Exception
  • +
  • builtins.BaseException
  • +
+
@@ -919,26 +919,26 @@

Index

  • Classes

  • diff --git a/doc/PyXYZ/material.html b/doc/PyXYZ/material.html index 3aaf3d7..f1d9e88 100644 --- a/doc/PyXYZ/material.html +++ b/doc/PyXYZ/material.html @@ -32,18 +32,18 @@

    Module PyXYZ.material

    class Material: """Material class. Describe the properties of the mesh being drawn. Currently it only supports a - color and a line width. + Color and a line width. """ - def __init__(self, color, name="UnknownMaterial"): + def __init__(self, Color, name="UnknownMaterial"): """ Arguments: - color {color} -- Color of the line + Color {Color} -- Color of the line name {str} -- Name of the material, defaults to 'UnknownMaterial' """ - self.color = color - """{color} Color of the lines on the mesh""" + self.Color = Color + """{Color} Color of the lines on the mesh""" self.name = name """{str} Name of this material""" self.line_width = 2 @@ -61,14 +61,14 @@

    Classes

    class Material -(color, name='UnknownMaterial') +(Color, name='UnknownMaterial')

    Material class. Describe the properties of the mesh being drawn. Currently it only supports a -color and a line width.

    +Color and a line width.

    Arguments

    -

    color {color} – Color of the line

    +

    Color {Color} – Color of the line

    name {str} – Name of the material, defaults to 'UnknownMaterial'

    @@ -77,18 +77,18 @@

    Arguments

    class Material:
         """Material class.
         Describe the properties of the mesh being drawn. Currently it only supports a
    -    color and a line width.
    +    Color and a line width.
         """
    -    def __init__(self, color, name="UnknownMaterial"):
    +    def __init__(self, Color, name="UnknownMaterial"):
             """
             Arguments:
     
    -            color {color} -- Color of the line
    +            Color {Color} -- Color of the line
     
                 name {str} -- Name of the material, defaults to 'UnknownMaterial'
             """
    -        self.color = color
    -        """{color} Color of the lines on the mesh"""
    +        self.Color = Color
    +        """{Color} Color of the lines on the mesh"""
             self.name = name
             """{str} Name of this material"""
             self.line_width = 2
    @@ -96,9 +96,9 @@ 

    Arguments

    Instance variables

    -
    var color
    +
    var Color
    -

    {color} Color of the lines on the mesh

    +

    {Color} Color of the lines on the mesh

    var line_width
    @@ -129,7 +129,7 @@

    Index

  • Material

    diff --git a/doc/PyXYZ/mesh.html b/doc/PyXYZ/mesh.html index ad45ea8..386f0c8 100644 --- a/doc/PyXYZ/mesh.html +++ b/doc/PyXYZ/mesh.html @@ -30,7 +30,7 @@

    Module PyXYZ.mesh

    """Mesh class definition"""
     import math
     import pygame
    -from vector3 import vector3
    +from vector3 import Vector3
     
     class Mesh:
         """Mesh class.
    @@ -55,7 +55,7 @@ 

    Module PyXYZ.mesh

    self.name = name """ {str} Name of the mesh""" self.polygons = [] - """ {list[list[vector3]]} List of lists of polygons. A polygon is a closed shape, + """ {list[list[Vector3]]} List of lists of polygons. A polygon is a closed shape, hence the need for a list of lists, if we want more complex shapes.""" def offset(self, v): @@ -64,7 +64,7 @@

    Module PyXYZ.mesh

    Arguments: - v {vector3} -- Ammount to displace the mesh + v {Vector3} -- Ammount to displace the mesh """ new_polys = [] for poly in self.polygons: @@ -92,8 +92,8 @@

    Module PyXYZ.mesh

    render times, but it is normally commented out, for performance reasons. If you want to use the statistics, uncomment the code on this funtion. """ - # Convert color to the pygame format - c = material.color.tuple3() + # Convert Color to the pygame format + c = material.Color.tuple3() # For all polygons for poly in self.polygons: @@ -146,26 +146,26 @@

    Module PyXYZ.mesh

    mesh = Mesh("UnknownCube") # Add the 6 quads that create a cube - Mesh.create_quad(vector3(size[0] * 0.5, 0, 0), - vector3(0, -size[1] * 0.5, 0), - vector3(0, 0, size[2] * 0.5), mesh) - Mesh.create_quad(vector3(-size[0] * 0.5, 0, 0), - vector3(0, size[1] * 0.5, 0), - vector3(0, 0, size[2] * 0.5), mesh) - - Mesh.create_quad(vector3(0, size[1] * 0.5, 0), - vector3(size[0] * 0.5, 0), - vector3(0, 0, size[2] * 0.5), mesh) - Mesh.create_quad(vector3(0, -size[1] * 0.5, 0), - vector3(-size[0] * 0.5, 0), - vector3(0, 0, size[2] * 0.5), mesh) - - Mesh.create_quad(vector3(0, 0, size[2] * 0.5), - vector3(-size[0] * 0.5, 0), - vector3(0, size[1] * 0.5, 0), mesh) - Mesh.create_quad(vector3(0, 0, -size[2] * 0.5), - vector3(size[0] * 0.5, 0), - vector3(0, size[1] * 0.5, 0), mesh) + Mesh.create_quad(Vector3(size[0] * 0.5, 0, 0), + Vector3(0, -size[1] * 0.5, 0), + Vector3(0, 0, size[2] * 0.5), mesh) + Mesh.create_quad(Vector3(-size[0] * 0.5, 0, 0), + Vector3(0, size[1] * 0.5, 0), + Vector3(0, 0, size[2] * 0.5), mesh) + + Mesh.create_quad(Vector3(0, size[1] * 0.5, 0), + Vector3(size[0] * 0.5, 0), + Vector3(0, 0, size[2] * 0.5), mesh) + Mesh.create_quad(Vector3(0, -size[1] * 0.5, 0), + Vector3(-size[0] * 0.5, 0), + Vector3(0, 0, size[2] * 0.5), mesh) + + Mesh.create_quad(Vector3(0, 0, size[2] * 0.5), + Vector3(-size[0] * 0.5, 0), + Vector3(0, size[1] * 0.5, 0), mesh) + Mesh.create_quad(Vector3(0, 0, -size[2] * 0.5), + Vector3(size[0] * 0.5, 0), + Vector3(0, size[1] * 0.5, 0), mesh) return mesh @@ -196,15 +196,15 @@

    Module PyXYZ.mesh

    mesh = Mesh("UnknownSphere") # Compute half-size - if isinstance(size, vector3): + if isinstance(size, Vector3): hs = size * 0.5 else: - hs = vector3(size[0], size[1], size[2]) * 0.5 + hs = Vector3(size[0], size[1], size[2]) * 0.5 # Sphere is going to be composed by quads in most of the surface, but triangles near the # poles, so compute the bottom and top vertex - bottom_vertex = vector3(0, -hs.y, 0) - top_vertex = vector3(0, hs.y, 0) + bottom_vertex = Vector3(0, -hs.y, 0) + top_vertex = Vector3(0, hs.y, 0) lat_inc = math.pi / res_lat lon_inc = math.pi * 2 / res_lon @@ -215,8 +215,8 @@

    Module PyXYZ.mesh

    y = hs.y * math.sin(lat + lat_inc) c = math.cos(lat + lat_inc) for _ in range(0, res_lon): - p1 = vector3(c * math.cos(lon) * hs.x, y, c * math.sin(lon) * hs.z) - p2 = vector3(c * math.cos(lon + lon_inc) * hs.x, y, c * math.sin(lon + lon_inc) * hs.z) + p1 = Vector3(c * math.cos(lon) * hs.x, y, c * math.sin(lon) * hs.z) + p2 = Vector3(c * math.cos(lon + lon_inc) * hs.x, y, c * math.sin(lon + lon_inc) * hs.z) Mesh.create_tri(bottom_vertex, p1, p2, mesh) @@ -233,16 +233,16 @@

    Module PyXYZ.mesh

    lon = 0 for _ in range(0, res_lon): - p1 = vector3(c1 * math.cos(lon) * hs.x, + p1 = Vector3(c1 * math.cos(lon) * hs.x, y1, c1 * math.sin(lon) * hs.z) - p2 = vector3(c1 * math.cos(lon + lon_inc) * hs.x, + p2 = Vector3(c1 * math.cos(lon + lon_inc) * hs.x, y1, c1 * math.sin(lon + lon_inc) * hs.z) - p3 = vector3(c2 * math.cos(lon) * hs.x, + p3 = Vector3(c2 * math.cos(lon) * hs.x, y2, c2 * math.sin(lon) * hs.z) - p4 = vector3(c2 * math.cos(lon + lon_inc) * hs.x, + p4 = Vector3(c2 * math.cos(lon + lon_inc) * hs.x, y2, c2 * math.sin(lon + lon_inc) * hs.z) @@ -261,8 +261,8 @@

    Module PyXYZ.mesh

    y = hs.y * math.sin(lat) c = math.cos(lat) for _ in range(0, res_lon): - p1 = vector3(c * math.cos(lon) * hs.x, y, c * math.sin(lon) * hs.z) - p2 = vector3(c * math.cos(lon + lon_inc) * hs.x, y, c * math.sin(lon + lon_inc) * hs.z) + p1 = Vector3(c * math.cos(lon) * hs.x, y, c * math.sin(lon) * hs.z) + p2 = Vector3(c * math.cos(lon + lon_inc) * hs.x, y, c * math.sin(lon + lon_inc) * hs.z) Mesh.create_tri(top_vertex, p1, p2, mesh) @@ -279,12 +279,12 @@

    Module PyXYZ.mesh

    Arguments: - origin {vector3} -- Center of the quad + origin {Vector3} -- Center of the quad - axis0 {vector3} -- One of the axis of the quad. This is not normalized, since the + axis0 {Vector3} -- One of the axis of the quad. This is not normalized, since the length specifies half the length of that side along that axis - axis1 {vector3} -- One of the axis of the quad. This is not normalized, since the + axis1 {Vector3} -- One of the axis of the quad. This is not normalized, since the length specifies half the length of that side along that axis mesh {Mesh} -- Mesh to add the polygons. If not given, create a new mesh @@ -313,11 +313,11 @@

    Module PyXYZ.mesh

    Arguments: - p1 {vector3} -- First vertex of the triangle + p1 {Vector3} -- First vertex of the triangle - p2 {vector3} -- Second vertex of the triangle + p2 {Vector3} -- Second vertex of the triangle - p3 {vector3} -- Third vertex of the triangle + p3 {Vector3} -- Third vertex of the triangle mesh {Mesh} -- Mesh to add the polygons. If not given, create a new mesh @@ -382,7 +382,7 @@

    Arguments

    self.name = name """ {str} Name of the mesh""" self.polygons = [] - """ {list[list[vector3]]} List of lists of polygons. A polygon is a closed shape, + """ {list[list[Vector3]]} List of lists of polygons. A polygon is a closed shape, hence the need for a list of lists, if we want more complex shapes.""" def offset(self, v): @@ -391,7 +391,7 @@

    Arguments

    Arguments: - v {vector3} -- Ammount to displace the mesh + v {Vector3} -- Ammount to displace the mesh """ new_polys = [] for poly in self.polygons: @@ -419,8 +419,8 @@

    Arguments

    render times, but it is normally commented out, for performance reasons. If you want to use the statistics, uncomment the code on this funtion. """ - # Convert color to the pygame format - c = material.color.tuple3() + # Convert Color to the pygame format + c = material.Color.tuple3() # For all polygons for poly in self.polygons: @@ -473,26 +473,26 @@

    Arguments

    mesh = Mesh("UnknownCube") # Add the 6 quads that create a cube - Mesh.create_quad(vector3(size[0] * 0.5, 0, 0), - vector3(0, -size[1] * 0.5, 0), - vector3(0, 0, size[2] * 0.5), mesh) - Mesh.create_quad(vector3(-size[0] * 0.5, 0, 0), - vector3(0, size[1] * 0.5, 0), - vector3(0, 0, size[2] * 0.5), mesh) - - Mesh.create_quad(vector3(0, size[1] * 0.5, 0), - vector3(size[0] * 0.5, 0), - vector3(0, 0, size[2] * 0.5), mesh) - Mesh.create_quad(vector3(0, -size[1] * 0.5, 0), - vector3(-size[0] * 0.5, 0), - vector3(0, 0, size[2] * 0.5), mesh) - - Mesh.create_quad(vector3(0, 0, size[2] * 0.5), - vector3(-size[0] * 0.5, 0), - vector3(0, size[1] * 0.5, 0), mesh) - Mesh.create_quad(vector3(0, 0, -size[2] * 0.5), - vector3(size[0] * 0.5, 0), - vector3(0, size[1] * 0.5, 0), mesh) + Mesh.create_quad(Vector3(size[0] * 0.5, 0, 0), + Vector3(0, -size[1] * 0.5, 0), + Vector3(0, 0, size[2] * 0.5), mesh) + Mesh.create_quad(Vector3(-size[0] * 0.5, 0, 0), + Vector3(0, size[1] * 0.5, 0), + Vector3(0, 0, size[2] * 0.5), mesh) + + Mesh.create_quad(Vector3(0, size[1] * 0.5, 0), + Vector3(size[0] * 0.5, 0), + Vector3(0, 0, size[2] * 0.5), mesh) + Mesh.create_quad(Vector3(0, -size[1] * 0.5, 0), + Vector3(-size[0] * 0.5, 0), + Vector3(0, 0, size[2] * 0.5), mesh) + + Mesh.create_quad(Vector3(0, 0, size[2] * 0.5), + Vector3(-size[0] * 0.5, 0), + Vector3(0, size[1] * 0.5, 0), mesh) + Mesh.create_quad(Vector3(0, 0, -size[2] * 0.5), + Vector3(size[0] * 0.5, 0), + Vector3(0, size[1] * 0.5, 0), mesh) return mesh @@ -523,15 +523,15 @@

    Arguments

    mesh = Mesh("UnknownSphere") # Compute half-size - if isinstance(size, vector3): + if isinstance(size, Vector3): hs = size * 0.5 else: - hs = vector3(size[0], size[1], size[2]) * 0.5 + hs = Vector3(size[0], size[1], size[2]) * 0.5 # Sphere is going to be composed by quads in most of the surface, but triangles near the # poles, so compute the bottom and top vertex - bottom_vertex = vector3(0, -hs.y, 0) - top_vertex = vector3(0, hs.y, 0) + bottom_vertex = Vector3(0, -hs.y, 0) + top_vertex = Vector3(0, hs.y, 0) lat_inc = math.pi / res_lat lon_inc = math.pi * 2 / res_lon @@ -542,8 +542,8 @@

    Arguments

    y = hs.y * math.sin(lat + lat_inc) c = math.cos(lat + lat_inc) for _ in range(0, res_lon): - p1 = vector3(c * math.cos(lon) * hs.x, y, c * math.sin(lon) * hs.z) - p2 = vector3(c * math.cos(lon + lon_inc) * hs.x, y, c * math.sin(lon + lon_inc) * hs.z) + p1 = Vector3(c * math.cos(lon) * hs.x, y, c * math.sin(lon) * hs.z) + p2 = Vector3(c * math.cos(lon + lon_inc) * hs.x, y, c * math.sin(lon + lon_inc) * hs.z) Mesh.create_tri(bottom_vertex, p1, p2, mesh) @@ -560,16 +560,16 @@

    Arguments

    lon = 0 for _ in range(0, res_lon): - p1 = vector3(c1 * math.cos(lon) * hs.x, + p1 = Vector3(c1 * math.cos(lon) * hs.x, y1, c1 * math.sin(lon) * hs.z) - p2 = vector3(c1 * math.cos(lon + lon_inc) * hs.x, + p2 = Vector3(c1 * math.cos(lon + lon_inc) * hs.x, y1, c1 * math.sin(lon + lon_inc) * hs.z) - p3 = vector3(c2 * math.cos(lon) * hs.x, + p3 = Vector3(c2 * math.cos(lon) * hs.x, y2, c2 * math.sin(lon) * hs.z) - p4 = vector3(c2 * math.cos(lon + lon_inc) * hs.x, + p4 = Vector3(c2 * math.cos(lon + lon_inc) * hs.x, y2, c2 * math.sin(lon + lon_inc) * hs.z) @@ -588,8 +588,8 @@

    Arguments

    y = hs.y * math.sin(lat) c = math.cos(lat) for _ in range(0, res_lon): - p1 = vector3(c * math.cos(lon) * hs.x, y, c * math.sin(lon) * hs.z) - p2 = vector3(c * math.cos(lon + lon_inc) * hs.x, y, c * math.sin(lon + lon_inc) * hs.z) + p1 = Vector3(c * math.cos(lon) * hs.x, y, c * math.sin(lon) * hs.z) + p2 = Vector3(c * math.cos(lon + lon_inc) * hs.x, y, c * math.sin(lon + lon_inc) * hs.z) Mesh.create_tri(top_vertex, p1, p2, mesh) @@ -606,12 +606,12 @@

    Arguments

    Arguments: - origin {vector3} -- Center of the quad + origin {Vector3} -- Center of the quad - axis0 {vector3} -- One of the axis of the quad. This is not normalized, since the + axis0 {Vector3} -- One of the axis of the quad. This is not normalized, since the length specifies half the length of that side along that axis - axis1 {vector3} -- One of the axis of the quad. This is not normalized, since the + axis1 {Vector3} -- One of the axis of the quad. This is not normalized, since the length specifies half the length of that side along that axis mesh {Mesh} -- Mesh to add the polygons. If not given, create a new mesh @@ -640,11 +640,11 @@

    Arguments

    Arguments: - p1 {vector3} -- First vertex of the triangle + p1 {Vector3} -- First vertex of the triangle - p2 {vector3} -- Second vertex of the triangle + p2 {Vector3} -- Second vertex of the triangle - p3 {vector3} -- Third vertex of the triangle + p3 {Vector3} -- Third vertex of the triangle mesh {Mesh} -- Mesh to add the polygons. If not given, create a new mesh @@ -721,26 +721,26 @@

    Returns

    mesh = Mesh("UnknownCube") # Add the 6 quads that create a cube - Mesh.create_quad(vector3(size[0] * 0.5, 0, 0), - vector3(0, -size[1] * 0.5, 0), - vector3(0, 0, size[2] * 0.5), mesh) - Mesh.create_quad(vector3(-size[0] * 0.5, 0, 0), - vector3(0, size[1] * 0.5, 0), - vector3(0, 0, size[2] * 0.5), mesh) - - Mesh.create_quad(vector3(0, size[1] * 0.5, 0), - vector3(size[0] * 0.5, 0), - vector3(0, 0, size[2] * 0.5), mesh) - Mesh.create_quad(vector3(0, -size[1] * 0.5, 0), - vector3(-size[0] * 0.5, 0), - vector3(0, 0, size[2] * 0.5), mesh) - - Mesh.create_quad(vector3(0, 0, size[2] * 0.5), - vector3(-size[0] * 0.5, 0), - vector3(0, size[1] * 0.5, 0), mesh) - Mesh.create_quad(vector3(0, 0, -size[2] * 0.5), - vector3(size[0] * 0.5, 0), - vector3(0, size[1] * 0.5, 0), mesh) + Mesh.create_quad(Vector3(size[0] * 0.5, 0, 0), + Vector3(0, -size[1] * 0.5, 0), + Vector3(0, 0, size[2] * 0.5), mesh) + Mesh.create_quad(Vector3(-size[0] * 0.5, 0, 0), + Vector3(0, size[1] * 0.5, 0), + Vector3(0, 0, size[2] * 0.5), mesh) + + Mesh.create_quad(Vector3(0, size[1] * 0.5, 0), + Vector3(size[0] * 0.5, 0), + Vector3(0, 0, size[2] * 0.5), mesh) + Mesh.create_quad(Vector3(0, -size[1] * 0.5, 0), + Vector3(-size[0] * 0.5, 0), + Vector3(0, 0, size[2] * 0.5), mesh) + + Mesh.create_quad(Vector3(0, 0, size[2] * 0.5), + Vector3(-size[0] * 0.5, 0), + Vector3(0, size[1] * 0.5, 0), mesh) + Mesh.create_quad(Vector3(0, 0, -size[2] * 0.5), + Vector3(size[0] * 0.5, 0), + Vector3(0, size[1] * 0.5, 0), mesh) return mesh
    @@ -752,10 +752,10 @@

    Returns

    Adds the vertices necessary to create a quad (4 sided coplanar rectangle). If a source mesh is not given, a new mesh is created.

    Arguments

    -

    origin {vector3} – Center of the quad

    -

    axis0 {vector3} – One of the axis of the quad. This is not normalized, since the +

    origin {Vector3} – Center of the quad

    +

    axis0 {Vector3} – One of the axis of the quad. This is not normalized, since the length specifies half the length of that side along that axis

    -

    axis1 {vector3} – One of the axis of the quad. This is not normalized, since the +

    axis1 {Vector3} – One of the axis of the quad. This is not normalized, since the length specifies half the length of that side along that axis

    mesh {Mesh} – Mesh to add the polygons. If not given, create a new mesh

    Returns

    @@ -772,12 +772,12 @@

    Returns

    Arguments: - origin {vector3} -- Center of the quad + origin {Vector3} -- Center of the quad - axis0 {vector3} -- One of the axis of the quad. This is not normalized, since the + axis0 {Vector3} -- One of the axis of the quad. This is not normalized, since the length specifies half the length of that side along that axis - axis1 {vector3} -- One of the axis of the quad. This is not normalized, since the + axis1 {Vector3} -- One of the axis of the quad. This is not normalized, since the length specifies half the length of that side along that axis mesh {Mesh} -- Mesh to add the polygons. If not given, create a new mesh @@ -846,15 +846,15 @@

    Returns

    mesh = Mesh("UnknownSphere") # Compute half-size - if isinstance(size, vector3): + if isinstance(size, Vector3): hs = size * 0.5 else: - hs = vector3(size[0], size[1], size[2]) * 0.5 + hs = Vector3(size[0], size[1], size[2]) * 0.5 # Sphere is going to be composed by quads in most of the surface, but triangles near the # poles, so compute the bottom and top vertex - bottom_vertex = vector3(0, -hs.y, 0) - top_vertex = vector3(0, hs.y, 0) + bottom_vertex = Vector3(0, -hs.y, 0) + top_vertex = Vector3(0, hs.y, 0) lat_inc = math.pi / res_lat lon_inc = math.pi * 2 / res_lon @@ -865,8 +865,8 @@

    Returns

    y = hs.y * math.sin(lat + lat_inc) c = math.cos(lat + lat_inc) for _ in range(0, res_lon): - p1 = vector3(c * math.cos(lon) * hs.x, y, c * math.sin(lon) * hs.z) - p2 = vector3(c * math.cos(lon + lon_inc) * hs.x, y, c * math.sin(lon + lon_inc) * hs.z) + p1 = Vector3(c * math.cos(lon) * hs.x, y, c * math.sin(lon) * hs.z) + p2 = Vector3(c * math.cos(lon + lon_inc) * hs.x, y, c * math.sin(lon + lon_inc) * hs.z) Mesh.create_tri(bottom_vertex, p1, p2, mesh) @@ -883,16 +883,16 @@

    Returns

    lon = 0 for _ in range(0, res_lon): - p1 = vector3(c1 * math.cos(lon) * hs.x, + p1 = Vector3(c1 * math.cos(lon) * hs.x, y1, c1 * math.sin(lon) * hs.z) - p2 = vector3(c1 * math.cos(lon + lon_inc) * hs.x, + p2 = Vector3(c1 * math.cos(lon + lon_inc) * hs.x, y1, c1 * math.sin(lon + lon_inc) * hs.z) - p3 = vector3(c2 * math.cos(lon) * hs.x, + p3 = Vector3(c2 * math.cos(lon) * hs.x, y2, c2 * math.sin(lon) * hs.z) - p4 = vector3(c2 * math.cos(lon + lon_inc) * hs.x, + p4 = Vector3(c2 * math.cos(lon + lon_inc) * hs.x, y2, c2 * math.sin(lon + lon_inc) * hs.z) @@ -911,8 +911,8 @@

    Returns

    y = hs.y * math.sin(lat) c = math.cos(lat) for _ in range(0, res_lon): - p1 = vector3(c * math.cos(lon) * hs.x, y, c * math.sin(lon) * hs.z) - p2 = vector3(c * math.cos(lon + lon_inc) * hs.x, y, c * math.sin(lon + lon_inc) * hs.z) + p1 = Vector3(c * math.cos(lon) * hs.x, y, c * math.sin(lon) * hs.z) + p2 = Vector3(c * math.cos(lon + lon_inc) * hs.x, y, c * math.sin(lon + lon_inc) * hs.z) Mesh.create_tri(top_vertex, p1, p2, mesh) @@ -928,9 +928,9 @@

    Returns

    Adds the vertices necessary to create a triangle If a source mesh is not given, a new mesh is created.

    Arguments

    -

    p1 {vector3} – First vertex of the triangle

    -

    p2 {vector3} – Second vertex of the triangle

    -

    p3 {vector3} – Third vertex of the triangle

    +

    p1 {Vector3} – First vertex of the triangle

    +

    p2 {Vector3} – Second vertex of the triangle

    +

    p3 {Vector3} – Third vertex of the triangle

    mesh {Mesh} – Mesh to add the polygons. If not given, create a new mesh

    Returns

    {Mesh} - Mesh where the polygons were added

    @@ -946,11 +946,11 @@

    Returns

    Arguments: - p1 {vector3} -- First vertex of the triangle + p1 {Vector3} -- First vertex of the triangle - p2 {vector3} -- Second vertex of the triangle + p2 {Vector3} -- Second vertex of the triangle - p3 {vector3} -- Third vertex of the triangle + p3 {Vector3} -- Third vertex of the triangle mesh {Mesh} -- Mesh to add the polygons. If not given, create a new mesh @@ -979,7 +979,7 @@

    Instance variables

  • var polygons
    -

    {list[list[vector3]]} List of lists of polygons. A polygon is a closed shape, +

    {list[list[Vector3]]} List of lists of polygons. A polygon is a closed shape, hence the need for a list of lists, if we want more complex shapes.

    @@ -991,7 +991,7 @@

    Methods

    Offsets this mesh by a given vector. In practice, adds v to all vertex in all polygons

    Arguments

    -

    v {vector3} – Ammount to displace the mesh

    +

    v {Vector3} – Ammount to displace the mesh

    Expand source code @@ -1002,7 +1002,7 @@

    Arguments

    Arguments: - v {vector3} -- Ammount to displace the mesh + v {Vector3} -- Ammount to displace the mesh """ new_polys = [] for poly in self.polygons: @@ -1048,8 +1048,8 @@

    Arguments

    render times, but it is normally commented out, for performance reasons. If you want to use the statistics, uncomment the code on this funtion. """ - # Convert color to the pygame format - c = material.color.tuple3() + # Convert Color to the pygame format + c = material.Color.tuple3() # For all polygons for poly in self.polygons: diff --git a/doc/PyXYZ/object3d.html b/doc/PyXYZ/object3d.html index 1306dbe..06c18a4 100644 --- a/doc/PyXYZ/object3d.html +++ b/doc/PyXYZ/object3d.html @@ -31,7 +31,7 @@

    Module PyXYZ.object3d

    import numpy as np from quaternion import quaternion, as_rotation_matrix -from vector3 import vector3 +from vector3 import Vector3 class Object3d: """3d object class. @@ -45,12 +45,12 @@

    Module PyXYZ.object3d

    """ self.name = name """ {str} Name of the object""" - self.position = vector3() - """ {vector3} Local position of the object (relative to parent)""" + self.position = Vector3() + """ {Vector3} Local position of the object (relative to parent)""" self.rotation = quaternion(1, 0, 0, 0) """ {quaternion} Local rotation of the object {relative to parent)""" - self.scale = vector3(1, 1, 1) - """ {vector3} Local scale of the object (relative to parent)""" + self.scale = Vector3(1, 1, 1) + """ {Vector3} Local scale of the object (relative to parent)""" self.mesh = None """ {Mesh} Mesh to be rendered in this object""" self.material = None @@ -124,9 +124,9 @@

    Module PyXYZ.object3d

    Returns: - {vector3} - Local position of the object + {Vector3} - Local position of the object """ - return vector3.from_np(vector3(0, 0, 0).to_np4(1) @ self.get_matrix()) + return Vector3.from_np(Vector3(0, 0, 0).to_np4(1) @ self.get_matrix()) def forward(self): """ @@ -135,9 +135,9 @@

    Module PyXYZ.object3d

    Returns: - {vector3} - Local forward vector of the object + {Vector3} - Local forward vector of the object """ - return vector3.from_np(vector3(0, 0, 1).to_np4(0) @ self.get_matrix()) + return Vector3.from_np(Vector3(0, 0, 1).to_np4(0) @ self.get_matrix()) def up(self): """ @@ -146,9 +146,9 @@

    Module PyXYZ.object3d

    Returns: - {vector3} - Local up vector of the object + {Vector3} - Local up vector of the object """ - return vector3.from_np(vector3(0, 1, 0).to_np4(0) @ self.get_matrix()) + return Vector3.from_np(Vector3(0, 1, 0).to_np4(0) @ self.get_matrix()) def right(self): """ @@ -157,9 +157,9 @@

    Module PyXYZ.object3d

    Returns: - {vector3} - Local right vector of the object + {Vector3} - Local right vector of the object """ - return vector3.from_np(vector3(1, 0, 0).to_np4(0) @ self.get_matrix()) + return Vector3.from_np(Vector3(1, 0, 0).to_np4(0) @ self.get_matrix()) @staticmethod def get_prs_matrix(position, rotation, scale): @@ -168,11 +168,11 @@

    Module PyXYZ.object3d

    Arguments: - position {vector3} - Position + position {Vector3} - Position rotation {quaternion} - Rotation - scale {vector3} - Scale + scale {Vector3} - Scale Returns: @@ -238,12 +238,12 @@

    Arguments

    """ self.name = name """ {str} Name of the object""" - self.position = vector3() - """ {vector3} Local position of the object (relative to parent)""" + self.position = Vector3() + """ {Vector3} Local position of the object (relative to parent)""" self.rotation = quaternion(1, 0, 0, 0) """ {quaternion} Local rotation of the object {relative to parent)""" - self.scale = vector3(1, 1, 1) - """ {vector3} Local scale of the object (relative to parent)""" + self.scale = Vector3(1, 1, 1) + """ {Vector3} Local scale of the object (relative to parent)""" self.mesh = None """ {Mesh} Mesh to be rendered in this object""" self.material = None @@ -317,9 +317,9 @@

    Arguments

    Returns: - {vector3} - Local position of the object + {Vector3} - Local position of the object """ - return vector3.from_np(vector3(0, 0, 0).to_np4(1) @ self.get_matrix()) + return Vector3.from_np(Vector3(0, 0, 0).to_np4(1) @ self.get_matrix()) def forward(self): """ @@ -328,9 +328,9 @@

    Arguments

    Returns: - {vector3} - Local forward vector of the object + {Vector3} - Local forward vector of the object """ - return vector3.from_np(vector3(0, 0, 1).to_np4(0) @ self.get_matrix()) + return Vector3.from_np(Vector3(0, 0, 1).to_np4(0) @ self.get_matrix()) def up(self): """ @@ -339,9 +339,9 @@

    Arguments

    Returns: - {vector3} - Local up vector of the object + {Vector3} - Local up vector of the object """ - return vector3.from_np(vector3(0, 1, 0).to_np4(0) @ self.get_matrix()) + return Vector3.from_np(Vector3(0, 1, 0).to_np4(0) @ self.get_matrix()) def right(self): """ @@ -350,9 +350,9 @@

    Arguments

    Returns: - {vector3} - Local right vector of the object + {Vector3} - Local right vector of the object """ - return vector3.from_np(vector3(1, 0, 0).to_np4(0) @ self.get_matrix()) + return Vector3.from_np(Vector3(1, 0, 0).to_np4(0) @ self.get_matrix()) @staticmethod def get_prs_matrix(position, rotation, scale): @@ -361,11 +361,11 @@

    Arguments

    Arguments: - position {vector3} - Position + position {Vector3} - Position rotation {quaternion} - Rotation - scale {vector3} - Scale + scale {Vector3} - Scale Returns: @@ -404,9 +404,9 @@

    Static methods

    Creates a PRS matrix from the given position, rotation and scale

    Arguments

    -

    position {vector3} - Position

    +

    position {Vector3} - Position

    rotation {quaternion} - Rotation

    -

    scale {vector3} - Scale

    +

    scale {Vector3} - Scale

    Returns

    {np.array} - PRS matrix

    @@ -420,11 +420,11 @@

    Returns

    Arguments: - position {vector3} - Position + position {Vector3} - Position rotation {quaternion} - Rotation - scale {vector3} - Scale + scale {Vector3} - Scale Returns: @@ -477,7 +477,7 @@

    Instance variables

    var position
    -

    {vector3} Local position of the object (relative to parent)

    +

    {Vector3} Local position of the object (relative to parent)

    var rotation
    @@ -485,7 +485,7 @@

    Instance variables

    var scale
    -

    {vector3} Local scale of the object (relative to parent)

    +

    {Vector3} Local scale of the object (relative to parent)

    Methods

    @@ -519,7 +519,7 @@

    Arguments

    Retrieves the local forward vector of this object. The forward vector is defined as being the z-positive vector multiplied with the local transformation matrix

    Returns

    -

    {vector3} - Local forward vector of the object

    +

    {Vector3} - Local forward vector of the object

    Expand source code @@ -531,9 +531,9 @@

    Returns

    Returns: - {vector3} - Local forward vector of the object + {Vector3} - Local forward vector of the object """ - return vector3.from_np(vector3(0, 0, 1).to_np4(0) @ self.get_matrix())
    + return Vector3.from_np(Vector3(0, 0, 1).to_np4(0) @ self.get_matrix())
    @@ -566,7 +566,7 @@

    Returns

    method actually computes the transfomation matrix and multiplies the 4d vector (0,0,0,1) by it. Results should be very similar.

    Returns

    -

    {vector3} - Local position of the object

    +

    {Vector3} - Local position of the object

    Expand source code @@ -579,9 +579,9 @@

    Returns

    Returns: - {vector3} - Local position of the object + {Vector3} - Local position of the object """ - return vector3.from_np(vector3(0, 0, 0).to_np4(1) @ self.get_matrix())
    + return Vector3.from_np(Vector3(0, 0, 0).to_np4(1) @ self.get_matrix())
    @@ -656,7 +656,7 @@

    Arguments

    Retrieves the local right vector of this object. The right vector is defined as being the x-positive vector multiplied with the local transformation matrix

    Returns

    -

    {vector3} - Local right vector of the object

    +

    {Vector3} - Local right vector of the object

    Expand source code @@ -668,9 +668,9 @@

    Returns

    Returns: - {vector3} - Local right vector of the object + {Vector3} - Local right vector of the object """ - return vector3.from_np(vector3(1, 0, 0).to_np4(0) @ self.get_matrix())
    + return Vector3.from_np(Vector3(1, 0, 0).to_np4(0) @ self.get_matrix())
    @@ -680,7 +680,7 @@

    Returns

    Retrieves the local up vector of this object. The up vector is defined as being the y-positive vector multiplied with the local transformation matrix

    Returns

    -

    {vector3} - Local up vector of the object

    +

    {Vector3} - Local up vector of the object

    Expand source code @@ -692,9 +692,9 @@

    Returns

    Returns: - {vector3} - Local up vector of the object + {Vector3} - Local up vector of the object """ - return vector3.from_np(vector3(0, 1, 0).to_np4(0) @ self.get_matrix())
    + return Vector3.from_np(Vector3(0, 1, 0).to_np4(0) @ self.get_matrix())
    diff --git a/doc/PyXYZ/sample_cubefall.html b/doc/PyXYZ/sample_cubefall.html index b46855c..7410f24 100644 --- a/doc/PyXYZ/sample_cubefall.html +++ b/doc/PyXYZ/sample_cubefall.html @@ -39,8 +39,8 @@

    Module PyXYZ.sample_cubefall

    from camera import Camera from mesh import Mesh from material import Material -from color import color -from vector3 import vector3 +from color import Color +from vector3 import Vector3 GRAVITY = -9.8 @@ -49,17 +49,17 @@

    Module PyXYZ.sample_cubefall

    def __init__(self, mesh): super().__init__("FallingCube") # Create a cube on a random positions - self.position = vector3(random.uniform(-6, 6), random.uniform(6, 10), random.uniform(3, 10)) + self.position = Vector3(random.uniform(-6, 6), random.uniform(6, 10), random.uniform(3, 10)) self.mesh = mesh - # Pick a random color for the cube - self.material = Material(color(random.uniform(0.1, 1), + # Pick a random Color for the cube + self.material = Material(Color(random.uniform(0.1, 1), random.uniform(0.1, 1), random.uniform(0.1, 1), 1), "FallingCubeMaterial") # Starting velocity is zero self.velocity = 0 # Select a random rotation axis - self.rotation_axis = vector3(random.uniform(-1, 1), + self.rotation_axis = Vector3(random.uniform(-1, 1), random.uniform(-1, 1), random.uniform(-1, 1)).normalized() # Select a random rotation speed @@ -90,7 +90,7 @@

    Module PyXYZ.sample_cubefall

    scene.camera = Camera(False, res_x, res_y) # Moves the camera back 2 units - scene.camera.position -= vector3(0, 0, 2) + scene.camera.position -= Vector3(0, 0, 2) # Create the cube mesh we're going to use for every single object cube_mesh = Mesh.create_cube((1, 1, 1)) @@ -199,7 +199,7 @@

    Functions

    scene.camera = Camera(False, res_x, res_y) # Moves the camera back 2 units - scene.camera.position -= vector3(0, 0, 2) + scene.camera.position -= Vector3(0, 0, 2) # Create the cube mesh we're going to use for every single object cube_mesh = Mesh.create_cube((1, 1, 1)) @@ -293,17 +293,17 @@

    Arguments

    def __init__(self, mesh): super().__init__("FallingCube") # Create a cube on a random positions - self.position = vector3(random.uniform(-6, 6), random.uniform(6, 10), random.uniform(3, 10)) + self.position = Vector3(random.uniform(-6, 6), random.uniform(6, 10), random.uniform(3, 10)) self.mesh = mesh - # Pick a random color for the cube - self.material = Material(color(random.uniform(0.1, 1), + # Pick a random Color for the cube + self.material = Material(Color(random.uniform(0.1, 1), random.uniform(0.1, 1), random.uniform(0.1, 1), 1), "FallingCubeMaterial") # Starting velocity is zero self.velocity = 0 # Select a random rotation axis - self.rotation_axis = vector3(random.uniform(-1, 1), + self.rotation_axis = Vector3(random.uniform(-1, 1), random.uniform(-1, 1), random.uniform(-1, 1)).normalized() # Select a random rotation speed diff --git a/doc/PyXYZ/sample_game.html b/doc/PyXYZ/sample_game.html index c1f093a..81eaa0a 100644 --- a/doc/PyXYZ/sample_game.html +++ b/doc/PyXYZ/sample_game.html @@ -41,8 +41,8 @@

    Module PyXYZ.sample_game

    from camera import Camera from mesh import Mesh from material import Material -from color import color -from vector3 import vector3, dot_product, cross_product +from color import Color +from vector3 import Vector3, dot_product, cross_product from perlin import noise2d class Missile(Object3d): @@ -58,24 +58,24 @@

    Module PyXYZ.sample_game

    def __init__(self): if Missile.missile_mesh is None: Missile.missile_mesh = Missile.create_missile_mesh() - Missile.missile_material = Material(color(1, 0, 0, 1), "MissileMaterial") + Missile.missile_material = Material(Color(1, 0, 0, 1), "MissileMaterial") super().__init__("Missile") # Determine the spawn position. There's 33% chance it comes from the right, 33% that it # comes from behind the mountains, and 34% chance it comes from the left - self.position = vector3(0, random.uniform(0, 3), 12) + self.position = Vector3(0, random.uniform(0, 3), 12) r = random.uniform(0, 100) if r > 66: self.position.x = 7 - self.rotation = from_rotation_vector((vector3(0, 1, 0) * math.radians(90)).to_np3()) + self.rotation = from_rotation_vector((Vector3(0, 1, 0) * math.radians(90)).to_np3()) elif r > 33: self.position.y = -2 self.position.x = random.uniform(-4, 4) - self.rotation = from_rotation_vector((vector3(1, 0, 0) * math.radians(90)).to_np3()) + self.rotation = from_rotation_vector((Vector3(1, 0, 0) * math.radians(90)).to_np3()) else: self.position.x = -7 - self.rotation = from_rotation_vector((vector3(0, 1, 0) * math.radians(-90)).to_np3()) + self.rotation = from_rotation_vector((Vector3(0, 1, 0) * math.radians(-90)).to_np3()) # Set the mesh and material of the missile self.mesh = Missile.missile_mesh @@ -94,7 +94,7 @@

    Module PyXYZ.sample_game

    # Rotate missile towards the player - Figure out where it is pointing and # where do we want to point it current_dir = self.forward() - desired_dir = (vector3(0, 0, 0) - self.position).normalized() + desired_dir = (Vector3(0, 0, 0) - self.position).normalized() # Find the angle between these two directions dp = np.clip(dot_product(current_dir, desired_dir), -1, 1) angle = math.acos(dp) @@ -119,18 +119,18 @@

    Module PyXYZ.sample_game

    length = 0.075 cone = 0.05 missile_mesh = Mesh.create_cube((radius * 2, radius * 2, length * 2)) - missile_mesh = Mesh.create_tri(vector3(radius, radius, length), - vector3(radius, -radius, length), - vector3(0, 0, length + cone), missile_mesh) - missile_mesh = Mesh.create_tri(vector3(radius, -radius, length), - vector3(-radius, -radius, length), - vector3(0, 0, length + cone), missile_mesh) - missile_mesh = Mesh.create_tri(vector3(-radius, -radius, length), - vector3(-radius, radius, length), - vector3(0, 0, length + cone), missile_mesh) - missile_mesh = Mesh.create_tri(vector3(-radius, radius, length), - vector3(radius, radius, length), - vector3(0, 0, length + cone), missile_mesh) + missile_mesh = Mesh.create_tri(Vector3(radius, radius, length), + Vector3(radius, -radius, length), + Vector3(0, 0, length + cone), missile_mesh) + missile_mesh = Mesh.create_tri(Vector3(radius, -radius, length), + Vector3(-radius, -radius, length), + Vector3(0, 0, length + cone), missile_mesh) + missile_mesh = Mesh.create_tri(Vector3(-radius, -radius, length), + Vector3(-radius, radius, length), + Vector3(0, 0, length + cone), missile_mesh) + missile_mesh = Mesh.create_tri(Vector3(-radius, radius, length), + Vector3(radius, radius, length), + Vector3(0, 0, length + cone), missile_mesh) return missile_mesh @@ -146,16 +146,16 @@

    Module PyXYZ.sample_game

    def __init__(self): if Shot.shot_mesh is None: Shot.shot_mesh = Mesh.create_sphere((0.1, 0.1, 0.1), 4, 4) - Shot.shot_material = Material(color(1, 1, 0, 1), "ShotMaterial") + Shot.shot_material = Material(Color(1, 1, 0, 1), "ShotMaterial") super().__init__("Shot") # The position and direction will be overwritten by the code that spawns the shot - self.position = vector3(0, 0, 0) + self.position = Vector3(0, 0, 0) self.mesh = Shot.shot_mesh self.material = Shot.shot_material self.shot_speed = 6 - self.direction = vector3(0, 0, 0) + self.direction = Vector3(0, 0, 0) def update(self, delta_time): """Animates the missile.""" @@ -191,17 +191,17 @@

    Module PyXYZ.sample_game

    pz = size_z / div # For centering the terrain on the object center - origin = vector3(-size_x * 0.5, 0, 0) + origin = Vector3(-size_x * 0.5, 0, 0) terrain_mesh = Mesh("Terrain") # Create the geometry of the terrain and add it to the mesh for dz in range(0, div): for dx in range(0, div): - p1 = vector3(dx * px, 0, dz * pz) + origin - p2 = vector3((dx + 1) * px, 0, dz * pz) + origin - p3 = vector3((dx + 1) * px, 0, (dz + 1) * pz) + origin - p4 = vector3(dx * px, 0, (dz + 1) * pz) + origin + p1 = Vector3(dx * px, 0, dz * pz) + origin + p2 = Vector3((dx + 1) * px, 0, dz * pz) + origin + p3 = Vector3((dx + 1) * px, 0, (dz + 1) * pz) + origin + p4 = Vector3(dx * px, 0, (dz + 1) * pz) + origin p1.y = sample_height(p1.x, p1.z) p2.y = sample_height(p2.x, p2.z) @@ -217,12 +217,12 @@

    Module PyXYZ.sample_game

    terrain_mesh.polygons.append(poly) # Create materials for the terrain - terrain_material = Material(color(0.1, 0.6, 0.1, 1), "TerrainMaterial") + terrain_material = Material(Color(0.1, 0.6, 0.1, 1), "TerrainMaterial") # Create object to display the terrain obj = Object3d("TerrainObject") - obj.scale = vector3(1, 1, 1) - obj.position = vector3(0, -1, 1) + obj.scale = Vector3(1, 1, 1) + obj.position = Vector3(0, -1, 1) obj.mesh = terrain_mesh obj.material = terrain_material @@ -246,7 +246,7 @@

    Module PyXYZ.sample_game

    scene.camera = Camera(False, res_x, res_y) # Moves the camera back 2 units - scene.camera.position -= vector3(0, 0, 0) + scene.camera.position -= Vector3(0, 0, 0) # Creates the terrain meshes and materials terrain_object = create_terrain() @@ -261,8 +261,8 @@

    Module PyXYZ.sample_game

    # Storage for all missiles active in the game missiles = [] - # Flashing effect color - flash_color = color(0, 0, 0, 0) + # Flashing effect Color + flash_color = Color(0, 0, 0, 0) # Total time of the current flash total_flash_time = 0 # Time elapsed for the current flash @@ -356,7 +356,7 @@

    Module PyXYZ.sample_game

    # Check if the missile is close enough to the player to hit him if missile.position.magnitude() < 0.25: # It has hit the player, flash the screen red for one second - flash_color = color(1, 0, 1, 1) + flash_color = Color(1, 0, 1, 1) total_flash_time = flash_timer = 1 # Destroy missile (remove it from the scene and add it to the destruction # list so we can remove it in a bit) @@ -387,7 +387,7 @@

    Module PyXYZ.sample_game

    for missile in missiles: # Check the distance between the shot and the missile, it it is below # a threshould (in this case 0.5), destroy the missile and the shot - distance = vector3.distance(shot.position, missile.position) + distance = Vector3.distance(shot.position, missile.position) if distance < 0.5: # Add it to the missile destruction list, and remove it from the scene missiles_to_destroy.append(missile) @@ -396,7 +396,7 @@

    Module PyXYZ.sample_game

    shots_to_destroy.append(shot) scene.remove_object(shot) # Flash the screen cyan for 0.5 seconds - flash_color = color(0, 1, 1, 1) + flash_color = Color(0, 1, 1, 1) total_flash_time = flash_timer = 0.5 # Actually delete objects @@ -453,17 +453,17 @@

    Functions

    pz = size_z / div # For centering the terrain on the object center - origin = vector3(-size_x * 0.5, 0, 0) + origin = Vector3(-size_x * 0.5, 0, 0) terrain_mesh = Mesh("Terrain") # Create the geometry of the terrain and add it to the mesh for dz in range(0, div): for dx in range(0, div): - p1 = vector3(dx * px, 0, dz * pz) + origin - p2 = vector3((dx + 1) * px, 0, dz * pz) + origin - p3 = vector3((dx + 1) * px, 0, (dz + 1) * pz) + origin - p4 = vector3(dx * px, 0, (dz + 1) * pz) + origin + p1 = Vector3(dx * px, 0, dz * pz) + origin + p2 = Vector3((dx + 1) * px, 0, dz * pz) + origin + p3 = Vector3((dx + 1) * px, 0, (dz + 1) * pz) + origin + p4 = Vector3(dx * px, 0, (dz + 1) * pz) + origin p1.y = sample_height(p1.x, p1.z) p2.y = sample_height(p2.x, p2.z) @@ -479,12 +479,12 @@

    Functions

    terrain_mesh.polygons.append(poly) # Create materials for the terrain - terrain_material = Material(color(0.1, 0.6, 0.1, 1), "TerrainMaterial") + terrain_material = Material(Color(0.1, 0.6, 0.1, 1), "TerrainMaterial") # Create object to display the terrain obj = Object3d("TerrainObject") - obj.scale = vector3(1, 1, 1) - obj.position = vector3(0, -1, 1) + obj.scale = Vector3(1, 1, 1) + obj.position = Vector3(0, -1, 1) obj.mesh = terrain_mesh obj.material = terrain_material @@ -518,7 +518,7 @@

    Functions

    scene.camera = Camera(False, res_x, res_y) # Moves the camera back 2 units - scene.camera.position -= vector3(0, 0, 0) + scene.camera.position -= Vector3(0, 0, 0) # Creates the terrain meshes and materials terrain_object = create_terrain() @@ -533,8 +533,8 @@

    Functions

    # Storage for all missiles active in the game missiles = [] - # Flashing effect color - flash_color = color(0, 0, 0, 0) + # Flashing effect Color + flash_color = Color(0, 0, 0, 0) # Total time of the current flash total_flash_time = 0 # Time elapsed for the current flash @@ -628,7 +628,7 @@

    Functions

    # Check if the missile is close enough to the player to hit him if missile.position.magnitude() < 0.25: # It has hit the player, flash the screen red for one second - flash_color = color(1, 0, 1, 1) + flash_color = Color(1, 0, 1, 1) total_flash_time = flash_timer = 1 # Destroy missile (remove it from the scene and add it to the destruction # list so we can remove it in a bit) @@ -659,7 +659,7 @@

    Functions

    for missile in missiles: # Check the distance between the shot and the missile, it it is below # a threshould (in this case 0.5), destroy the missile and the shot - distance = vector3.distance(shot.position, missile.position) + distance = Vector3.distance(shot.position, missile.position) if distance < 0.5: # Add it to the missile destruction list, and remove it from the scene missiles_to_destroy.append(missile) @@ -668,7 +668,7 @@

    Functions

    shots_to_destroy.append(shot) scene.remove_object(shot) # Flash the screen cyan for 0.5 seconds - flash_color = color(0, 1, 1, 1) + flash_color = Color(0, 1, 1, 1) total_flash_time = flash_timer = 0.5 # Actually delete objects @@ -752,24 +752,24 @@

    Arguments

    def __init__(self): if Missile.missile_mesh is None: Missile.missile_mesh = Missile.create_missile_mesh() - Missile.missile_material = Material(color(1, 0, 0, 1), "MissileMaterial") + Missile.missile_material = Material(Color(1, 0, 0, 1), "MissileMaterial") super().__init__("Missile") # Determine the spawn position. There's 33% chance it comes from the right, 33% that it # comes from behind the mountains, and 34% chance it comes from the left - self.position = vector3(0, random.uniform(0, 3), 12) + self.position = Vector3(0, random.uniform(0, 3), 12) r = random.uniform(0, 100) if r > 66: self.position.x = 7 - self.rotation = from_rotation_vector((vector3(0, 1, 0) * math.radians(90)).to_np3()) + self.rotation = from_rotation_vector((Vector3(0, 1, 0) * math.radians(90)).to_np3()) elif r > 33: self.position.y = -2 self.position.x = random.uniform(-4, 4) - self.rotation = from_rotation_vector((vector3(1, 0, 0) * math.radians(90)).to_np3()) + self.rotation = from_rotation_vector((Vector3(1, 0, 0) * math.radians(90)).to_np3()) else: self.position.x = -7 - self.rotation = from_rotation_vector((vector3(0, 1, 0) * math.radians(-90)).to_np3()) + self.rotation = from_rotation_vector((Vector3(0, 1, 0) * math.radians(-90)).to_np3()) # Set the mesh and material of the missile self.mesh = Missile.missile_mesh @@ -788,7 +788,7 @@

    Arguments

    # Rotate missile towards the player - Figure out where it is pointing and # where do we want to point it current_dir = self.forward() - desired_dir = (vector3(0, 0, 0) - self.position).normalized() + desired_dir = (Vector3(0, 0, 0) - self.position).normalized() # Find the angle between these two directions dp = np.clip(dot_product(current_dir, desired_dir), -1, 1) angle = math.acos(dp) @@ -813,18 +813,18 @@

    Arguments

    length = 0.075 cone = 0.05 missile_mesh = Mesh.create_cube((radius * 2, radius * 2, length * 2)) - missile_mesh = Mesh.create_tri(vector3(radius, radius, length), - vector3(radius, -radius, length), - vector3(0, 0, length + cone), missile_mesh) - missile_mesh = Mesh.create_tri(vector3(radius, -radius, length), - vector3(-radius, -radius, length), - vector3(0, 0, length + cone), missile_mesh) - missile_mesh = Mesh.create_tri(vector3(-radius, -radius, length), - vector3(-radius, radius, length), - vector3(0, 0, length + cone), missile_mesh) - missile_mesh = Mesh.create_tri(vector3(-radius, radius, length), - vector3(radius, radius, length), - vector3(0, 0, length + cone), missile_mesh) + missile_mesh = Mesh.create_tri(Vector3(radius, radius, length), + Vector3(radius, -radius, length), + Vector3(0, 0, length + cone), missile_mesh) + missile_mesh = Mesh.create_tri(Vector3(radius, -radius, length), + Vector3(-radius, -radius, length), + Vector3(0, 0, length + cone), missile_mesh) + missile_mesh = Mesh.create_tri(Vector3(-radius, -radius, length), + Vector3(-radius, radius, length), + Vector3(0, 0, length + cone), missile_mesh) + missile_mesh = Mesh.create_tri(Vector3(-radius, radius, length), + Vector3(radius, radius, length), + Vector3(0, 0, length + cone), missile_mesh) return missile_mesh @@ -861,18 +861,18 @@

    Static methods

    length = 0.075 cone = 0.05 missile_mesh = Mesh.create_cube((radius * 2, radius * 2, length * 2)) - missile_mesh = Mesh.create_tri(vector3(radius, radius, length), - vector3(radius, -radius, length), - vector3(0, 0, length + cone), missile_mesh) - missile_mesh = Mesh.create_tri(vector3(radius, -radius, length), - vector3(-radius, -radius, length), - vector3(0, 0, length + cone), missile_mesh) - missile_mesh = Mesh.create_tri(vector3(-radius, -radius, length), - vector3(-radius, radius, length), - vector3(0, 0, length + cone), missile_mesh) - missile_mesh = Mesh.create_tri(vector3(-radius, radius, length), - vector3(radius, radius, length), - vector3(0, 0, length + cone), missile_mesh) + missile_mesh = Mesh.create_tri(Vector3(radius, radius, length), + Vector3(radius, -radius, length), + Vector3(0, 0, length + cone), missile_mesh) + missile_mesh = Mesh.create_tri(Vector3(radius, -radius, length), + Vector3(-radius, -radius, length), + Vector3(0, 0, length + cone), missile_mesh) + missile_mesh = Mesh.create_tri(Vector3(-radius, -radius, length), + Vector3(-radius, radius, length), + Vector3(0, 0, length + cone), missile_mesh) + missile_mesh = Mesh.create_tri(Vector3(-radius, radius, length), + Vector3(radius, radius, length), + Vector3(0, 0, length + cone), missile_mesh) return missile_mesh @@ -898,7 +898,7 @@

    Methods

    # Rotate missile towards the player - Figure out where it is pointing and # where do we want to point it current_dir = self.forward() - desired_dir = (vector3(0, 0, 0) - self.position).normalized() + desired_dir = (Vector3(0, 0, 0) - self.position).normalized() # Find the angle between these two directions dp = np.clip(dot_product(current_dir, desired_dir), -1, 1) angle = math.acos(dp) @@ -943,16 +943,16 @@

    Arguments

    def __init__(self): if Shot.shot_mesh is None: Shot.shot_mesh = Mesh.create_sphere((0.1, 0.1, 0.1), 4, 4) - Shot.shot_material = Material(color(1, 1, 0, 1), "ShotMaterial") + Shot.shot_material = Material(Color(1, 1, 0, 1), "ShotMaterial") super().__init__("Shot") # The position and direction will be overwritten by the code that spawns the shot - self.position = vector3(0, 0, 0) + self.position = Vector3(0, 0, 0) self.mesh = Shot.shot_mesh self.material = Shot.shot_material self.shot_speed = 6 - self.direction = vector3(0, 0, 0) + self.direction = Vector3(0, 0, 0) def update(self, delta_time): """Animates the missile.""" diff --git a/doc/PyXYZ/sample_hierarchy.html b/doc/PyXYZ/sample_hierarchy.html index 2fb3357..938dd2c 100644 --- a/doc/PyXYZ/sample_hierarchy.html +++ b/doc/PyXYZ/sample_hierarchy.html @@ -40,8 +40,8 @@

    Module PyXYZ.sample_hierarchy

    from camera import Camera from mesh import Mesh from material import Material -from color import color -from vector3 import vector3 +from color import Color +from vector3 import Vector3 # Define a main function, just to keep things nice and tidy def main(): @@ -61,28 +61,28 @@

    Module PyXYZ.sample_hierarchy

    scene.camera = Camera(False, res_x, res_y) # Moves the camera back 2 units - scene.camera.position -= vector3(0, 0, 2) + scene.camera.position -= Vector3(0, 0, 2) # Create a cube and place it in a scene, at position (0,0,0) obj1 = Object3d("TestObject") - obj1.scale = vector3(1, 1, 1) - obj1.position = vector3(0, -1, 0) + obj1.scale = Vector3(1, 1, 1) + obj1.position = Vector3(0, -1, 0) obj1.mesh = Mesh.create_cube((1, 1, 1)) - obj1.material = Material(color(1, 0, 0, 1), "TestMaterial1") + obj1.material = Material(Color(1, 0, 0, 1), "TestMaterial1") scene.add_object(obj1) # Create a second object, and add it as a child of the first object # When the first object rotates, this one will also mimic the transform obj2 = Object3d("ChildObject") - obj2.position += vector3(0, 0.75, 0) + obj2.position += Vector3(0, 0.75, 0) obj2.mesh = Mesh.create_cube((0.5, 0.5, 0.5)) - obj2.material = Material(color(0, 1, 0, 1), "TestMaterial2") + obj2.material = Material(Color(0, 1, 0, 1), "TestMaterial2") obj1.add_child(obj2) # Specify the rotation of the object. It will rotate 15 degrees around the axis given, # every second angle = 15 - axis = vector3(1, 0.7, 0.2) + axis = Vector3(1, 0.7, 0.2) axis.normalize() # Timer @@ -159,28 +159,28 @@

    Functions

    scene.camera = Camera(False, res_x, res_y) # Moves the camera back 2 units - scene.camera.position -= vector3(0, 0, 2) + scene.camera.position -= Vector3(0, 0, 2) # Create a cube and place it in a scene, at position (0,0,0) obj1 = Object3d("TestObject") - obj1.scale = vector3(1, 1, 1) - obj1.position = vector3(0, -1, 0) + obj1.scale = Vector3(1, 1, 1) + obj1.position = Vector3(0, -1, 0) obj1.mesh = Mesh.create_cube((1, 1, 1)) - obj1.material = Material(color(1, 0, 0, 1), "TestMaterial1") + obj1.material = Material(Color(1, 0, 0, 1), "TestMaterial1") scene.add_object(obj1) # Create a second object, and add it as a child of the first object # When the first object rotates, this one will also mimic the transform obj2 = Object3d("ChildObject") - obj2.position += vector3(0, 0.75, 0) + obj2.position += Vector3(0, 0.75, 0) obj2.mesh = Mesh.create_cube((0.5, 0.5, 0.5)) - obj2.material = Material(color(0, 1, 0, 1), "TestMaterial2") + obj2.material = Material(Color(0, 1, 0, 1), "TestMaterial2") obj1.add_child(obj2) # Specify the rotation of the object. It will rotate 15 degrees around the axis given, # every second angle = 15 - axis = vector3(1, 0.7, 0.2) + axis = Vector3(1, 0.7, 0.2) axis.normalize() # Timer diff --git a/doc/PyXYZ/sample_sphere.html b/doc/PyXYZ/sample_sphere.html index 4399b35..9aa6f50 100644 --- a/doc/PyXYZ/sample_sphere.html +++ b/doc/PyXYZ/sample_sphere.html @@ -39,8 +39,8 @@

    Module PyXYZ.sample_sphere

    from camera import Camera from mesh import Mesh from material import Material -from color import color -from vector3 import vector3 +from color import Color +from vector3 import Vector3 # Define a main function, just to keep things nice and tidy def main(): @@ -60,20 +60,20 @@

    Module PyXYZ.sample_sphere

    scene.camera = Camera(False, res_x, res_y) # Moves the camera back 2 units - scene.camera.position -= vector3(0, 0, 2) + scene.camera.position -= Vector3(0, 0, 2) # Create a sphere and place it in a scene, at position (0,0,0) obj1 = Object3d("TestObject") - obj1.scale = vector3(1, 1, 1) - obj1.position = vector3(0, 0, 0) + obj1.scale = Vector3(1, 1, 1) + obj1.position = Vector3(0, 0, 0) obj1.mesh = Mesh.create_sphere((1, 1, 1), 12, 12) - obj1.material = Material(color(1, 0, 0, 1), "TestMaterial1") + obj1.material = Material(Color(1, 0, 0, 1), "TestMaterial1") scene.add_object(obj1) # Specify the rotation of the object. It will rotate 15 degrees around the axis given, # every second angle = 15 - axis = vector3(1, 0.7, 0.2) + axis = Vector3(1, 0.7, 0.2) axis.normalize() # Timer @@ -149,20 +149,20 @@

    Functions

    scene.camera = Camera(False, res_x, res_y) # Moves the camera back 2 units - scene.camera.position -= vector3(0, 0, 2) + scene.camera.position -= Vector3(0, 0, 2) # Create a sphere and place it in a scene, at position (0,0,0) obj1 = Object3d("TestObject") - obj1.scale = vector3(1, 1, 1) - obj1.position = vector3(0, 0, 0) + obj1.scale = Vector3(1, 1, 1) + obj1.position = Vector3(0, 0, 0) obj1.mesh = Mesh.create_sphere((1, 1, 1), 12, 12) - obj1.material = Material(color(1, 0, 0, 1), "TestMaterial1") + obj1.material = Material(Color(1, 0, 0, 1), "TestMaterial1") scene.add_object(obj1) # Specify the rotation of the object. It will rotate 15 degrees around the axis given, # every second angle = 15 - axis = vector3(1, 0.7, 0.2) + axis = Vector3(1, 0.7, 0.2) axis.normalize() # Timer diff --git a/doc/PyXYZ/sample_terrain.html b/doc/PyXYZ/sample_terrain.html index ddfa74b..fa5e7e7 100644 --- a/doc/PyXYZ/sample_terrain.html +++ b/doc/PyXYZ/sample_terrain.html @@ -39,8 +39,8 @@

    Module PyXYZ.sample_terrain

    from camera import Camera from mesh import Mesh from material import Material -from color import color -from vector3 import vector3, dot_product, cross_product +from color import Color +from vector3 import Vector3, dot_product, cross_product from perlin import noise2d def sample_height(x, y): @@ -78,7 +78,7 @@

    Module PyXYZ.sample_terrain

    pz = size_z / div # For centering the terrain on the object center - origin = vector3(-size_x * 0.5, 0, -size_z * 0.5) + origin = Vector3(-size_x * 0.5, 0, -size_z * 0.5) # Create the meshes for each type of terrain grass_mesh = Mesh("Terrain_Grass") @@ -88,10 +88,10 @@

    Module PyXYZ.sample_terrain

    for dz in range(0, div): for dx in range(0, div): - p1 = vector3(dx * px, 0, dz * pz) + origin - p2 = vector3((dx + 1) * px, 0, dz * pz) + origin - p3 = vector3((dx + 1) * px, 0, (dz + 1) * pz) + origin - p4 = vector3(dx * px, 0, (dz + 1) * pz) + origin + p1 = Vector3(dx * px, 0, dz * pz) + origin + p2 = Vector3((dx + 1) * px, 0, dz * pz) + origin + p3 = Vector3((dx + 1) * px, 0, (dz + 1) * pz) + origin + p4 = Vector3(dx * px, 0, (dz + 1) * pz) + origin p1.y = sample_height(p1.x, p1.z) p2.y = sample_height(p2.x, p2.z) @@ -123,16 +123,16 @@

    Module PyXYZ.sample_terrain

    else: # Check for cliff face, check the normal normal = cross_product((p3 - p1).normalized(), (p2 - p1).normalized()) - if dot_product(normal, vector3(0, 1, 0)) < 0.5: + if dot_product(normal, Vector3(0, 1, 0)) < 0.5: cliff_mesh.polygons.append(poly) else: grass_mesh.polygons.append(poly) # Create materials for the terrain - grass_material = Material(color(0.1, 0.6, 0.1, 1), "GrassMaterial") - snow_material = Material(color(0.8, 0.8, 0.8, 1), "SnowMaterial") - cliff_material = Material(color(0.4, 0.4, 0.4, 1), "CliffMaterial") - water_material = Material(color(0, 0.5, 0.7, 1), "WaterMaterial") + grass_material = Material(Color(0.1, 0.6, 0.1, 1), "GrassMaterial") + snow_material = Material(Color(0.8, 0.8, 0.8, 1), "SnowMaterial") + cliff_material = Material(Color(0.4, 0.4, 0.4, 1), "CliffMaterial") + water_material = Material(Color(0, 0.5, 0.7, 1), "WaterMaterial") # Return meshes and materials meshes = [grass_mesh, snow_mesh, cliff_mesh, water_mesh] @@ -158,22 +158,22 @@

    Module PyXYZ.sample_terrain

    scene.camera = Camera(False, res_x, res_y) # Moves the camera back 2 units - scene.camera.position -= vector3(0, 0, 4) - scene.camera.rotation = from_rotation_vector((vector3(1, 0, 0) * math.radians(-15)).to_np3()) + scene.camera.position -= Vector3(0, 0, 4) + scene.camera.rotation = from_rotation_vector((Vector3(1, 0, 0) * math.radians(-15)).to_np3()) # Creates the terrain meshes and materials terrain_meshes, terrain_materials = create_terrain() # Create container object for all the terrain sub-objects master_object = Object3d("TerrainObject") - master_object.position = vector3(0, -1, 0) + master_object.position = Vector3(0, -1, 0) scene.add_object(master_object) # Create the terrain objects and place it in a scene, at position (0,0,0) for _, (mesh, material) in enumerate(zip(terrain_meshes, terrain_materials)): obj = Object3d("TerrainObject") - obj.scale = vector3(1, 1, 1) - obj.position = vector3(0, 0, 0) + obj.scale = Vector3(1, 1, 1) + obj.position = Vector3(0, 0, 0) obj.mesh = mesh obj.material = material master_object.add_child(obj) @@ -181,7 +181,7 @@

    Module PyXYZ.sample_terrain

    # Specify the rotation of the object. It will rotate 15 degrees around the axis given, # every second angle = 15 - axis = vector3(0, 1, 0) + axis = Vector3(0, 1, 0) axis.normalize() # Timer @@ -292,7 +292,7 @@

    Functions

    pz = size_z / div # For centering the terrain on the object center - origin = vector3(-size_x * 0.5, 0, -size_z * 0.5) + origin = Vector3(-size_x * 0.5, 0, -size_z * 0.5) # Create the meshes for each type of terrain grass_mesh = Mesh("Terrain_Grass") @@ -302,10 +302,10 @@

    Functions

    for dz in range(0, div): for dx in range(0, div): - p1 = vector3(dx * px, 0, dz * pz) + origin - p2 = vector3((dx + 1) * px, 0, dz * pz) + origin - p3 = vector3((dx + 1) * px, 0, (dz + 1) * pz) + origin - p4 = vector3(dx * px, 0, (dz + 1) * pz) + origin + p1 = Vector3(dx * px, 0, dz * pz) + origin + p2 = Vector3((dx + 1) * px, 0, dz * pz) + origin + p3 = Vector3((dx + 1) * px, 0, (dz + 1) * pz) + origin + p4 = Vector3(dx * px, 0, (dz + 1) * pz) + origin p1.y = sample_height(p1.x, p1.z) p2.y = sample_height(p2.x, p2.z) @@ -337,16 +337,16 @@

    Functions

    else: # Check for cliff face, check the normal normal = cross_product((p3 - p1).normalized(), (p2 - p1).normalized()) - if dot_product(normal, vector3(0, 1, 0)) < 0.5: + if dot_product(normal, Vector3(0, 1, 0)) < 0.5: cliff_mesh.polygons.append(poly) else: grass_mesh.polygons.append(poly) # Create materials for the terrain - grass_material = Material(color(0.1, 0.6, 0.1, 1), "GrassMaterial") - snow_material = Material(color(0.8, 0.8, 0.8, 1), "SnowMaterial") - cliff_material = Material(color(0.4, 0.4, 0.4, 1), "CliffMaterial") - water_material = Material(color(0, 0.5, 0.7, 1), "WaterMaterial") + grass_material = Material(Color(0.1, 0.6, 0.1, 1), "GrassMaterial") + snow_material = Material(Color(0.8, 0.8, 0.8, 1), "SnowMaterial") + cliff_material = Material(Color(0.4, 0.4, 0.4, 1), "CliffMaterial") + water_material = Material(Color(0, 0.5, 0.7, 1), "WaterMaterial") # Return meshes and materials meshes = [grass_mesh, snow_mesh, cliff_mesh, water_mesh] @@ -381,22 +381,22 @@

    Functions

    scene.camera = Camera(False, res_x, res_y) # Moves the camera back 2 units - scene.camera.position -= vector3(0, 0, 4) - scene.camera.rotation = from_rotation_vector((vector3(1, 0, 0) * math.radians(-15)).to_np3()) + scene.camera.position -= Vector3(0, 0, 4) + scene.camera.rotation = from_rotation_vector((Vector3(1, 0, 0) * math.radians(-15)).to_np3()) # Creates the terrain meshes and materials terrain_meshes, terrain_materials = create_terrain() # Create container object for all the terrain sub-objects master_object = Object3d("TerrainObject") - master_object.position = vector3(0, -1, 0) + master_object.position = Vector3(0, -1, 0) scene.add_object(master_object) # Create the terrain objects and place it in a scene, at position (0,0,0) for _, (mesh, material) in enumerate(zip(terrain_meshes, terrain_materials)): obj = Object3d("TerrainObject") - obj.scale = vector3(1, 1, 1) - obj.position = vector3(0, 0, 0) + obj.scale = Vector3(1, 1, 1) + obj.position = Vector3(0, 0, 0) obj.mesh = mesh obj.material = material master_object.add_child(obj) @@ -404,7 +404,7 @@

    Functions

    # Specify the rotation of the object. It will rotate 15 degrees around the axis given, # every second angle = 15 - axis = vector3(0, 1, 0) + axis = Vector3(0, 1, 0) axis.normalize() # Timer diff --git a/doc/PyXYZ/vector3.html b/doc/PyXYZ/vector3.html index c436273..a6d8530 100644 --- a/doc/PyXYZ/vector3.html +++ b/doc/PyXYZ/vector3.html @@ -43,7 +43,7 @@

    Module PyXYZ.vector3

    """Returns a readable version of the exception""" return f"Invalid operation ({self.op}) between {self.type1} and {self.type2}!" -class vector3: +class Vector3: """3d vector class. It stores XYZ values as floats.""" @@ -71,39 +71,39 @@

    Module PyXYZ.vector3

    return "(" + str(self.x) + "," + str(self.y) + "," + str(self.z) + ")" def __add__(self, v): - """Adds this vector3 to another. - If we try to add anything other than a vector3 to it, it throws the + """Adds this Vector3 to another. + If we try to add anything other than a Vector3 to it, it throws the InvalidOperationException. Arguments: - v {vector3} -- Vector to add + v {Vector3} -- Vector to add Returns: - vector3 - Sum of this vector3 and the given one + Vector3 - Sum of this Vector3 and the given one """ - if isinstance(v, vector3): - return vector3(self.x + v.x, self.y + v.y, self.z + v.z) + if isinstance(v, Vector3): + return Vector3(self.x + v.x, self.y + v.y, self.z + v.z) else: raise InvalidOperationException("add", type(self), type(v)) def __sub__(self, v): - """Subtracts a vector3 from this one. - If we try to subtract anything other than a vector3, it throws the + """Subtracts a Vector3 from this one. + If we try to subtract anything other than a Vector3, it throws the InvalidOperationException. Arguments: - v {vector3} -- Vector to subtract + v {Vector3} -- Vector to subtract Returns: - vector3 - Subraction of the given vector from this one + Vector3 - Subraction of the given vector from this one """ - if isinstance(v, vector3): - return vector3(self.x - v.x, self.y - v.y, self.z - v.z) + if isinstance(v, Vector3): + return Vector3(self.x - v.x, self.y - v.y, self.z - v.z) else: raise InvalidOperationException("sub", type(self), type(v)) def __mul__(self, v): - """Multiplies this vector3 by a scalar. + """Multiplies this Vector3 by a scalar. If we try to multiply anything other than a scalar, it throws the InvalidOperationException. @@ -112,15 +112,15 @@

    Module PyXYZ.vector3

    multiplied by this number Returns: - vector3 - Multiplication of the vector3 + Vector3 - Multiplication of the Vector3 """ if isinstance(v, (int, float)): - return vector3(self.x * v, self.y * v, self.z * v) + return Vector3(self.x * v, self.y * v, self.z * v) else: raise InvalidOperationException("mult", type(self), type(v)) def __rmul__(self, v): - """Multiplies this vector3 by a scalar. + """Multiplies this Vector3 by a scalar. If we try to multiply anything other than a scalar, it throws the InvalidOperationException Arguments: @@ -128,88 +128,88 @@

    Module PyXYZ.vector3

    this number Returns: - vector3 - Multiplication of the vector3 + Vector3 - Multiplication of the Vector3 """ if isinstance(v, (int, float)): - return vector3(self.x * v, self.y * v, self.z * v) + return Vector3(self.x * v, self.y * v, self.z * v) else: raise InvalidOperationException("mult", type(self), type(v)) def __truediv__(self, v): - """Divides this vector3 by a scalar. + """Divides this Vector3 by a scalar. If we try to divide anything other than a scalar, it throws the InvalidOperationException Arguments: v {number} -- Scalar to divide: all components of the vector are divided by this number Returns: - vector3 - Division of the vector3 + Vector3 - Division of the Vector3 """ if isinstance(v, (int, float)): - return vector3(self.x / v, self.y / v, self.z / v) + return Vector3(self.x / v, self.y / v, self.z / v) else: raise InvalidOperationException("mult", type(self), type(v)) def __eq__(self, v): - """Checks if this vector3 is equal to the given one, with a tolerance of 0.0001. + """Checks if this Vector3 is equal to the given one, with a tolerance of 0.0001. Exception InvalidOperationException is thrown if we compare something other than a - vector3. + Vector3. Arguments: - v {vector3} -- Vector to compare + v {Vector3} -- Vector to compare Returns: Bool - True if the vectors are the same, false otherwise """ - if isinstance(v, vector3): + if isinstance(v, Vector3): return ((self - v).magnitude()) < 0.0001 else: raise InvalidOperationException("eq", type(self), type(v)) def __ne__(self, v): - """Checks if this vector3 is different to the given one, with a tolerance of 0.0001. + """Checks if this Vector3 is different to the given one, with a tolerance of 0.0001. Exception InvalidOperationException is thrown if we compare something other than a - vector3. + Vector3. Arguments: - v {vector3} -- Vector to compare + v {Vector3} -- Vector to compare Returns: Bool - True if the vectors are different, false otherwise """ - if isinstance(v, vector3): + if isinstance(v, Vector3): return ((self - v).magnitude()) > 0.0001 else: raise InvalidOperationException("neq", type(self), type(v)) def __isub__(self, v): - """Subtracts a vector3 from this one. - If we try to subtract anything other than a vector3, it throws the + """Subtracts a Vector3 from this one. + If we try to subtract anything other than a Vector3, it throws the InvalidOperationException. Arguments: - v {vector3} -- Vector to subtract + v {Vector3} -- Vector to subtract Returns: - vector3 - Subraction of the given vector from this one + Vector3 - Subraction of the given vector from this one """ return self - v def __iadd__(self, v): - """Adds this vector3 to another. - If we try to add anything other than a vector3 to it, it throws the + """Adds this Vector3 to another. + If we try to add anything other than a Vector3 to it, it throws the InvalidOperationException. Arguments: - v {vector3} -- Vector to add + v {Vector3} -- Vector to add Returns: - vector3 - Sum of this vector3 and the given one + Vector3 - Sum of this Vector3 and the given one """ return self + v def __imul__(self, v): - """Multiplies this vector3 by a scalar. + """Multiplies this Vector3 by a scalar. If we try to multiply anything other than a scalar, it throws the InvalidOperationException. @@ -218,32 +218,32 @@

    Module PyXYZ.vector3

    multiplied by this number. Returns: - vector3 - Multiplication of the vector3 + Vector3 - Multiplication of the Vector3 """ return self * v def __idiv__(self, v): - """Divides this vector3 by a scalar. + """Divides this Vector3 by a scalar. If we try to divide anything other than a scalar, it throws the InvalidOperationException Arguments: v {number} -- Scalar to divide: all components of the vector are divided by this number Returns: - vector3 - Division of the vector3 + Vector3 - Division of the Vector3 """ return self / v def __neg__(self): - """Negates this vector3, component-wise. Equivelent to multiplying by (-1) + """Negates this Vector3, component-wise. Equivelent to multiplying by (-1) Returns: - vector3 - Negated vector3 + Vector3 - Negated Vector3 """ - return vector3(-self.x, -self.y, -self.z) + return Vector3(-self.x, -self.y, -self.z) def magnitude(self): - """Returns the magnitude of the vector3. + """Returns the magnitude of the Vector3. Returns: Number - Magnitude of the vector @@ -251,7 +251,7 @@

    Module PyXYZ.vector3

    return math.sqrt(self.dot(self)) def magnitude_squared(self): - """Returns the squared magnitude of the vector3. + """Returns the squared magnitude of the Vector3. Returns: Number - Magnitude of the vector @@ -259,34 +259,34 @@

    Module PyXYZ.vector3

    return self.dot(self) def dot(self, v): - """Computes the dot product of this vector3 with another. - If we try to do this operation with anything other than a vector3, it throws + """Computes the dot product of this Vector3 with another. + If we try to do this operation with anything other than a Vector3, it throws the InvalidOperationException. Arguments: - v {vector3} -- Vector3 to do the dot product with + v {Vector3} -- Vector3 to do the dot product with Returns: Number - Scalar value corresponding to the dot product of both vectors """ - if isinstance(v, vector3): + if isinstance(v, Vector3): return self.x * v.x + self.y * v.y + self.z * v.z else: raise InvalidOperationException("dot", type(self), type(v)) def cross(self, v): - """Computes the cross product of this vector3 with another. - If we try to do this operation with anything other than a vector3, it throws + """Computes the cross product of this Vector3 with another. + If we try to do this operation with anything other than a Vector3, it throws the InvalidOperationException. Arguments: - v {vector3} -- Vector3 to do the cross product with + v {Vector3} -- Vector3 to do the cross product with Returns: - vector3 - Cross product of both vectors + Vector3 - Cross product of both vectors """ - if isinstance(v, vector3): - return vector3(self.y * v.z - self.z * v.y, + if isinstance(v, Vector3): + return Vector3(self.y * v.z - self.z * v.y, self.z * v.x - self.x * v.z, self.x * v.y - self.y * v.x) else: @@ -300,24 +300,24 @@

    Module PyXYZ.vector3

    self.z *= d def normalized(self): - """Returns the normalized version of this vector3 + """Returns the normalized version of this Vector3 Returns: - vector3 - Normalized vector + Vector3 - Normalized vector """ d = 1.0 / self.magnitude() - return vector3(self.x * d, self.y * d, self.z * d) + return Vector3(self.x * d, self.y * d, self.z * d) def x0z(self): """Returns this vector, but with the y component zeroed. Returns: - vector3 - (x,0,z) + Vector3 - (x,0,z) """ - return vector3(self.x, 0, self.z) + return Vector3(self.x, 0, self.z) def to_np3(self): - """Converts a vector3 to a 3-component numpy array + """Converts a Vector3 to a 3-component numpy array Returns: np.array - [x,y,z] @@ -325,7 +325,7 @@

    Module PyXYZ.vector3

    return np.array([self.x, self.y, self.z]) def to_np4(self, w=1): - """Converts a vector3 to a 4-component numpy array, with the given w as the 4th component. + """Converts a Vector3 to a 4-component numpy array, with the given w as the 4th component. Arguments: w {number} - Value of the w component on the np.array. @@ -337,24 +337,24 @@

    Module PyXYZ.vector3

    @staticmethod def from_np(np_array): - """Converts a np.array to a vector3. No validation is done on the array to confirm it + """Converts a np.array to a Vector3. No validation is done on the array to confirm it has 3 components. Arguments: np_array {np.array} - np.array with 3-components (rows or columns) Returns: - vector3 - A vector3 + Vector3 - A Vector3 """ - return vector3(np_array[0], np_array[1], np_array[2]) + return Vector3(np_array[0], np_array[1], np_array[2]) @staticmethod def distance(v1, v2): """Returns the distance between two positions/vectors Arguments: - v1 {vector3} - First vector - v2 {vector3} - Second vector + v1 {Vector3} - First vector + v2 {Vector3} - Second vector Returns: number - Distance between the two positions/vectors @@ -365,8 +365,8 @@

    Module PyXYZ.vector3

    """Returns the dot product between two vectors Arguments: - v1 {vector3} - First vector - v2 {vector3} - Second vector + v1 {Vector3} - First vector + v2 {Vector3} - Second vector Returns: number - Dot product between the vectors @@ -377,11 +377,11 @@

    Module PyXYZ.vector3

    """Returns the cross product between two vectors Arguments: - v1 {vector3} - First vector - v2 {vector3} - Second vector + v1 {Vector3} - First vector + v2 {Vector3} - Second vector Returns: - vector3 - Cross product between the vectors + Vector3 - Cross product between the vectors """ return v1.cross(v2) @@ -399,10 +399,10 @@

    Functions

    Returns the cross product between two vectors

    Arguments

    -

    v1 {vector3} - First vector -v2 {vector3} - Second vector

    +

    v1 {Vector3} - First vector +v2 {Vector3} - Second vector

    Returns

    -

    vector3 - Cross product between the vectors

    +

    Vector3 - Cross product between the vectors

    Expand source code @@ -411,11 +411,11 @@

    Returns

    """Returns the cross product between two vectors Arguments: - v1 {vector3} - First vector - v2 {vector3} - Second vector + v1 {Vector3} - First vector + v2 {Vector3} - Second vector Returns: - vector3 - Cross product between the vectors + Vector3 - Cross product between the vectors """ return v1.cross(v2)
    @@ -426,8 +426,8 @@

    Returns

    Returns the dot product between two vectors

    Arguments

    -

    v1 {vector3} - First vector -v2 {vector3} - Second vector

    +

    v1 {Vector3} - First vector +v2 {Vector3} - Second vector

    Returns

    number - Dot product between the vectors

    @@ -438,8 +438,8 @@

    Returns

    """Returns the dot product between two vectors Arguments: - v1 {vector3} - First vector - v2 {vector3} - Second vector + v1 {Vector3} - First vector + v2 {Vector3} - Second vector Returns: number - Dot product between the vectors @@ -480,8 +480,8 @@

    Ancestors

  • builtins.BaseException
  • -
    -class vector3 +
    +class Vector3 (x=0, y=0, z=0)
    @@ -495,7 +495,7 @@

    Arguments

    Expand source code -
    class vector3:
    +
    class Vector3:
         """3d vector class.
         It stores XYZ values as floats."""
     
    @@ -523,39 +523,39 @@ 

    Arguments

    return "(" + str(self.x) + "," + str(self.y) + "," + str(self.z) + ")" def __add__(self, v): - """Adds this vector3 to another. - If we try to add anything other than a vector3 to it, it throws the + """Adds this Vector3 to another. + If we try to add anything other than a Vector3 to it, it throws the InvalidOperationException. Arguments: - v {vector3} -- Vector to add + v {Vector3} -- Vector to add Returns: - vector3 - Sum of this vector3 and the given one + Vector3 - Sum of this Vector3 and the given one """ - if isinstance(v, vector3): - return vector3(self.x + v.x, self.y + v.y, self.z + v.z) + if isinstance(v, Vector3): + return Vector3(self.x + v.x, self.y + v.y, self.z + v.z) else: raise InvalidOperationException("add", type(self), type(v)) def __sub__(self, v): - """Subtracts a vector3 from this one. - If we try to subtract anything other than a vector3, it throws the + """Subtracts a Vector3 from this one. + If we try to subtract anything other than a Vector3, it throws the InvalidOperationException. Arguments: - v {vector3} -- Vector to subtract + v {Vector3} -- Vector to subtract Returns: - vector3 - Subraction of the given vector from this one + Vector3 - Subraction of the given vector from this one """ - if isinstance(v, vector3): - return vector3(self.x - v.x, self.y - v.y, self.z - v.z) + if isinstance(v, Vector3): + return Vector3(self.x - v.x, self.y - v.y, self.z - v.z) else: raise InvalidOperationException("sub", type(self), type(v)) def __mul__(self, v): - """Multiplies this vector3 by a scalar. + """Multiplies this Vector3 by a scalar. If we try to multiply anything other than a scalar, it throws the InvalidOperationException. @@ -564,15 +564,15 @@

    Arguments

    multiplied by this number Returns: - vector3 - Multiplication of the vector3 + Vector3 - Multiplication of the Vector3 """ if isinstance(v, (int, float)): - return vector3(self.x * v, self.y * v, self.z * v) + return Vector3(self.x * v, self.y * v, self.z * v) else: raise InvalidOperationException("mult", type(self), type(v)) def __rmul__(self, v): - """Multiplies this vector3 by a scalar. + """Multiplies this Vector3 by a scalar. If we try to multiply anything other than a scalar, it throws the InvalidOperationException Arguments: @@ -580,88 +580,88 @@

    Arguments

    this number Returns: - vector3 - Multiplication of the vector3 + Vector3 - Multiplication of the Vector3 """ if isinstance(v, (int, float)): - return vector3(self.x * v, self.y * v, self.z * v) + return Vector3(self.x * v, self.y * v, self.z * v) else: raise InvalidOperationException("mult", type(self), type(v)) def __truediv__(self, v): - """Divides this vector3 by a scalar. + """Divides this Vector3 by a scalar. If we try to divide anything other than a scalar, it throws the InvalidOperationException Arguments: v {number} -- Scalar to divide: all components of the vector are divided by this number Returns: - vector3 - Division of the vector3 + Vector3 - Division of the Vector3 """ if isinstance(v, (int, float)): - return vector3(self.x / v, self.y / v, self.z / v) + return Vector3(self.x / v, self.y / v, self.z / v) else: raise InvalidOperationException("mult", type(self), type(v)) def __eq__(self, v): - """Checks if this vector3 is equal to the given one, with a tolerance of 0.0001. + """Checks if this Vector3 is equal to the given one, with a tolerance of 0.0001. Exception InvalidOperationException is thrown if we compare something other than a - vector3. + Vector3. Arguments: - v {vector3} -- Vector to compare + v {Vector3} -- Vector to compare Returns: Bool - True if the vectors are the same, false otherwise """ - if isinstance(v, vector3): + if isinstance(v, Vector3): return ((self - v).magnitude()) < 0.0001 else: raise InvalidOperationException("eq", type(self), type(v)) def __ne__(self, v): - """Checks if this vector3 is different to the given one, with a tolerance of 0.0001. + """Checks if this Vector3 is different to the given one, with a tolerance of 0.0001. Exception InvalidOperationException is thrown if we compare something other than a - vector3. + Vector3. Arguments: - v {vector3} -- Vector to compare + v {Vector3} -- Vector to compare Returns: Bool - True if the vectors are different, false otherwise """ - if isinstance(v, vector3): + if isinstance(v, Vector3): return ((self - v).magnitude()) > 0.0001 else: raise InvalidOperationException("neq", type(self), type(v)) def __isub__(self, v): - """Subtracts a vector3 from this one. - If we try to subtract anything other than a vector3, it throws the + """Subtracts a Vector3 from this one. + If we try to subtract anything other than a Vector3, it throws the InvalidOperationException. Arguments: - v {vector3} -- Vector to subtract + v {Vector3} -- Vector to subtract Returns: - vector3 - Subraction of the given vector from this one + Vector3 - Subraction of the given vector from this one """ return self - v def __iadd__(self, v): - """Adds this vector3 to another. - If we try to add anything other than a vector3 to it, it throws the + """Adds this Vector3 to another. + If we try to add anything other than a Vector3 to it, it throws the InvalidOperationException. Arguments: - v {vector3} -- Vector to add + v {Vector3} -- Vector to add Returns: - vector3 - Sum of this vector3 and the given one + Vector3 - Sum of this Vector3 and the given one """ return self + v def __imul__(self, v): - """Multiplies this vector3 by a scalar. + """Multiplies this Vector3 by a scalar. If we try to multiply anything other than a scalar, it throws the InvalidOperationException. @@ -670,32 +670,32 @@

    Arguments

    multiplied by this number. Returns: - vector3 - Multiplication of the vector3 + Vector3 - Multiplication of the Vector3 """ return self * v def __idiv__(self, v): - """Divides this vector3 by a scalar. + """Divides this Vector3 by a scalar. If we try to divide anything other than a scalar, it throws the InvalidOperationException Arguments: v {number} -- Scalar to divide: all components of the vector are divided by this number Returns: - vector3 - Division of the vector3 + Vector3 - Division of the Vector3 """ return self / v def __neg__(self): - """Negates this vector3, component-wise. Equivelent to multiplying by (-1) + """Negates this Vector3, component-wise. Equivelent to multiplying by (-1) Returns: - vector3 - Negated vector3 + Vector3 - Negated Vector3 """ - return vector3(-self.x, -self.y, -self.z) + return Vector3(-self.x, -self.y, -self.z) def magnitude(self): - """Returns the magnitude of the vector3. + """Returns the magnitude of the Vector3. Returns: Number - Magnitude of the vector @@ -703,7 +703,7 @@

    Arguments

    return math.sqrt(self.dot(self)) def magnitude_squared(self): - """Returns the squared magnitude of the vector3. + """Returns the squared magnitude of the Vector3. Returns: Number - Magnitude of the vector @@ -711,34 +711,34 @@

    Arguments

    return self.dot(self) def dot(self, v): - """Computes the dot product of this vector3 with another. - If we try to do this operation with anything other than a vector3, it throws + """Computes the dot product of this Vector3 with another. + If we try to do this operation with anything other than a Vector3, it throws the InvalidOperationException. Arguments: - v {vector3} -- Vector3 to do the dot product with + v {Vector3} -- Vector3 to do the dot product with Returns: Number - Scalar value corresponding to the dot product of both vectors """ - if isinstance(v, vector3): + if isinstance(v, Vector3): return self.x * v.x + self.y * v.y + self.z * v.z else: raise InvalidOperationException("dot", type(self), type(v)) def cross(self, v): - """Computes the cross product of this vector3 with another. - If we try to do this operation with anything other than a vector3, it throws + """Computes the cross product of this Vector3 with another. + If we try to do this operation with anything other than a Vector3, it throws the InvalidOperationException. Arguments: - v {vector3} -- Vector3 to do the cross product with + v {Vector3} -- Vector3 to do the cross product with Returns: - vector3 - Cross product of both vectors + Vector3 - Cross product of both vectors """ - if isinstance(v, vector3): - return vector3(self.y * v.z - self.z * v.y, + if isinstance(v, Vector3): + return Vector3(self.y * v.z - self.z * v.y, self.z * v.x - self.x * v.z, self.x * v.y - self.y * v.x) else: @@ -752,24 +752,24 @@

    Arguments

    self.z *= d def normalized(self): - """Returns the normalized version of this vector3 + """Returns the normalized version of this Vector3 Returns: - vector3 - Normalized vector + Vector3 - Normalized vector """ d = 1.0 / self.magnitude() - return vector3(self.x * d, self.y * d, self.z * d) + return Vector3(self.x * d, self.y * d, self.z * d) def x0z(self): """Returns this vector, but with the y component zeroed. Returns: - vector3 - (x,0,z) + Vector3 - (x,0,z) """ - return vector3(self.x, 0, self.z) + return Vector3(self.x, 0, self.z) def to_np3(self): - """Converts a vector3 to a 3-component numpy array + """Converts a Vector3 to a 3-component numpy array Returns: np.array - [x,y,z] @@ -777,7 +777,7 @@

    Arguments

    return np.array([self.x, self.y, self.z]) def to_np4(self, w=1): - """Converts a vector3 to a 4-component numpy array, with the given w as the 4th component. + """Converts a Vector3 to a 4-component numpy array, with the given w as the 4th component. Arguments: w {number} - Value of the w component on the np.array. @@ -789,24 +789,24 @@

    Arguments

    @staticmethod def from_np(np_array): - """Converts a np.array to a vector3. No validation is done on the array to confirm it + """Converts a np.array to a Vector3. No validation is done on the array to confirm it has 3 components. Arguments: np_array {np.array} - np.array with 3-components (rows or columns) Returns: - vector3 - A vector3 + Vector3 - A Vector3 """ - return vector3(np_array[0], np_array[1], np_array[2]) + return Vector3(np_array[0], np_array[1], np_array[2]) @staticmethod def distance(v1, v2): """Returns the distance between two positions/vectors Arguments: - v1 {vector3} - First vector - v2 {vector3} - Second vector + v1 {Vector3} - First vector + v2 {Vector3} - Second vector Returns: number - Distance between the two positions/vectors @@ -815,14 +815,14 @@

    Arguments

    Static methods

    -
    +
    def distance(v1, v2)

    Returns the distance between two positions/vectors

    Arguments

    -

    v1 {vector3} - First vector -v2 {vector3} - Second vector

    +

    v1 {Vector3} - First vector +v2 {Vector3} - Second vector

    Returns

    number - Distance between the two positions/vectors

    @@ -834,8 +834,8 @@

    Returns

    """Returns the distance between two positions/vectors Arguments: - v1 {vector3} - First vector - v2 {vector3} - Second vector + v1 {Vector3} - First vector + v2 {Vector3} - Second vector Returns: number - Distance between the two positions/vectors @@ -843,95 +843,95 @@

    Returns

    return (v1 - v2).magnitude()
    -
    +
    def from_np(np_array)
    -

    Converts a np.array to a vector3. No validation is done on the array to confirm it +

    Converts a np.array to a Vector3. No validation is done on the array to confirm it has 3 components.

    Arguments

    np_array {np.array} - np.array with 3-components (rows or columns)

    Returns

    -

    vector3 - A vector3

    +

    Vector3 - A Vector3

    Expand source code
    @staticmethod
     def from_np(np_array):
    -    """Converts a np.array to a vector3. No validation is done on the array to confirm it
    +    """Converts a np.array to a Vector3. No validation is done on the array to confirm it
         has 3 components.
     
         Arguments:
             np_array {np.array} - np.array with 3-components (rows or columns)
     
         Returns:
    -        vector3 - A vector3
    +        Vector3 - A Vector3
         """
    -    return vector3(np_array[0], np_array[1], np_array[2])
    + return Vector3(np_array[0], np_array[1], np_array[2])

    Instance variables

    -
    var x
    +
    var x

    {number} - X component

    -
    var y
    +
    var y

    {number} - Y component

    -
    var z
    +
    var z

    {number} - Z component

    Methods

    -
    +
    def cross(self, v)
    -

    Computes the cross product of this vector3 with another. -If we try to do this operation with anything other than a vector3, it throws +

    Computes the cross product of this Vector3 with another. +If we try to do this operation with anything other than a Vector3, it throws the InvalidOperationException.

    Arguments

    -

    v {vector3} – Vector3 to do the cross product with

    +

    v {Vector3} – Vector3 to do the cross product with

    Returns

    -

    vector3 - Cross product of both vectors

    +

    Vector3 - Cross product of both vectors

    Expand source code
    def cross(self, v):
    -    """Computes the cross product of this vector3 with another.
    -    If we try to do this operation with anything other than a vector3, it throws
    +    """Computes the cross product of this Vector3 with another.
    +    If we try to do this operation with anything other than a Vector3, it throws
         the InvalidOperationException.
     
         Arguments:
    -        v {vector3} -- Vector3 to do the cross product with
    +        v {Vector3} -- Vector3 to do the cross product with
     
         Returns:
    -        vector3 - Cross product of both vectors
    +        Vector3 - Cross product of both vectors
         """
    -    if isinstance(v, vector3):
    -        return vector3(self.y * v.z - self.z * v.y,
    +    if isinstance(v, Vector3):
    +        return Vector3(self.y * v.z - self.z * v.y,
                            self.z * v.x - self.x * v.z,
                            self.x * v.y - self.y * v.x)
         else:
             raise InvalidOperationException("dot", type(self), type(v))
    -
    +
    def dot(self, v)
    -

    Computes the dot product of this vector3 with another. -If we try to do this operation with anything other than a vector3, it throws +

    Computes the dot product of this Vector3 with another. +If we try to do this operation with anything other than a Vector3, it throws the InvalidOperationException.

    Arguments

    -

    v {vector3} – Vector3 to do the dot product with

    +

    v {Vector3} – Vector3 to do the dot product with

    Returns

    Number - Scalar value corresponding to the dot product of both vectors

    @@ -939,27 +939,27 @@

    Returns

    Expand source code
    def dot(self, v):
    -    """Computes the dot product of this vector3 with another.
    -    If we try to do this operation with anything other than a vector3, it throws
    +    """Computes the dot product of this Vector3 with another.
    +    If we try to do this operation with anything other than a Vector3, it throws
         the InvalidOperationException.
     
         Arguments:
    -        v {vector3} -- Vector3 to do the dot product with
    +        v {Vector3} -- Vector3 to do the dot product with
     
         Returns:
             Number - Scalar value corresponding to the dot product of both vectors
         """
    -    if isinstance(v, vector3):
    +    if isinstance(v, Vector3):
             return self.x * v.x + self.y * v.y + self.z * v.z
         else:
             raise InvalidOperationException("dot", type(self), type(v))
    -
    +
    def magnitude(self)
    -

    Returns the magnitude of the vector3.

    +

    Returns the magnitude of the Vector3.

    Returns

    Number - Magnitude of the vector

    @@ -967,7 +967,7 @@

    Returns

    Expand source code
    def magnitude(self):
    -    """Returns the magnitude of the vector3.
    +    """Returns the magnitude of the Vector3.
     
         Returns:
             Number - Magnitude of the vector
    @@ -975,11 +975,11 @@ 

    Returns

    return math.sqrt(self.dot(self))
    -
    +
    def magnitude_squared(self)
    -

    Returns the squared magnitude of the vector3.

    +

    Returns the squared magnitude of the Vector3.

    Returns

    Number - Magnitude of the vector

    @@ -987,7 +987,7 @@

    Returns

    Expand source code
    def magnitude_squared(self):
    -    """Returns the squared magnitude of the vector3.
    +    """Returns the squared magnitude of the Vector3.
     
         Returns:
             Number - Magnitude of the vector
    @@ -995,7 +995,7 @@ 

    Returns

    return self.dot(self)
    -
    +
    def normalize(self)
    @@ -1012,32 +1012,32 @@

    Returns

    self.z *= d
    -
    +
    def normalized(self)
    -

    Returns the normalized version of this vector3

    +

    Returns the normalized version of this Vector3

    Returns

    -

    vector3 - Normalized vector

    +

    Vector3 - Normalized vector

    Expand source code
    def normalized(self):
    -    """Returns the normalized version of this vector3
    +    """Returns the normalized version of this Vector3
     
         Returns:
    -        vector3 - Normalized vector
    +        Vector3 - Normalized vector
         """
         d = 1.0 / self.magnitude()
    -    return vector3(self.x * d, self.y * d, self.z * d)
    + return Vector3(self.x * d, self.y * d, self.z * d)
    -
    +
    def to_np3(self)
    -

    Converts a vector3 to a 3-component numpy array

    +

    Converts a Vector3 to a 3-component numpy array

    Returns

    np.array - [x,y,z]

    @@ -1045,7 +1045,7 @@

    Returns

    Expand source code
    def to_np3(self):
    -    """Converts a vector3 to a 3-component numpy array
    +    """Converts a Vector3 to a 3-component numpy array
     
         Returns:
             np.array - [x,y,z]
    @@ -1053,11 +1053,11 @@ 

    Returns

    return np.array([self.x, self.y, self.z])
    -
    +
    def to_np4(self, w=1)
    -

    Converts a vector3 to a 4-component numpy array, with the given w as the 4th component.

    +

    Converts a Vector3 to a 4-component numpy array, with the given w as the 4th component.

    Arguments

    w {number} - Value of the w component on the np.array.

    Returns

    @@ -1067,7 +1067,7 @@

    Returns

    Expand source code
    def to_np4(self, w=1):
    -    """Converts a vector3 to a 4-component numpy array, with the given w as the 4th component.
    +    """Converts a Vector3 to a 4-component numpy array, with the given w as the 4th component.
     
         Arguments:
             w {number} - Value of the w component on the np.array.
    @@ -1078,13 +1078,13 @@ 

    Returns

    return np.array([self.x, self.y, self.z, w])
    -
    +
    def x0z(self)

    Returns this vector, but with the y component zeroed.

    Returns

    -

    vector3 - (x,0,z)

    +

    Vector3 - (x,0,z)

    Expand source code @@ -1093,9 +1093,9 @@

    Returns

    """Returns this vector, but with the y component zeroed. Returns: - vector3 - (x,0,z) + Vector3 - (x,0,z) """ - return vector3(self.x, 0, self.z)
    + return Vector3(self.x, 0, self.z)
    @@ -1126,22 +1126,22 @@

    Index

    InvalidOperationException

  • -

    vector3

    +

    Vector3

  • diff --git a/material.py b/material.py index 9a0b806..05808ec 100644 --- a/material.py +++ b/material.py @@ -3,18 +3,18 @@ class Material: """Material class. Describe the properties of the mesh being drawn. Currently it only supports a - color and a line width. + Color and a line width. """ - def __init__(self, color, name="UnknownMaterial"): + def __init__(self, Color, name="UnknownMaterial"): """ Arguments: - color {color} -- Color of the line + Color {Color} -- Color of the line name {str} -- Name of the material, defaults to 'UnknownMaterial' """ - self.color = color - """{color} Color of the lines on the mesh""" + self.Color = Color + """{Color} Color of the lines on the mesh""" self.name = name """{str} Name of this material""" self.line_width = 2 diff --git a/mesh.py b/mesh.py index e6cbc70..0231534 100644 --- a/mesh.py +++ b/mesh.py @@ -1,7 +1,7 @@ """Mesh class definition""" import math import pygame -from vector3 import vector3 +from vector3 import Vector3 class Mesh: """Mesh class. @@ -26,7 +26,7 @@ def __init__(self, name="UnknownMesh"): self.name = name """ {str} Name of the mesh""" self.polygons = [] - """ {list[list[vector3]]} List of lists of polygons. A polygon is a closed shape, + """ {list[list[Vector3]]} List of lists of polygons. A polygon is a closed shape, hence the need for a list of lists, if we want more complex shapes.""" def offset(self, v): @@ -35,7 +35,7 @@ def offset(self, v): Arguments: - v {vector3} -- Ammount to displace the mesh + v {Vector3} -- Ammount to displace the mesh """ new_polys = [] for poly in self.polygons: @@ -63,8 +63,8 @@ def render(self, screen, clip_matrix, material): render times, but it is normally commented out, for performance reasons. If you want to use the statistics, uncomment the code on this funtion. """ - # Convert color to the pygame format - c = material.color.tuple3() + # Convert Color to the pygame format + c = material.Color.tuple3() # For all polygons for poly in self.polygons: @@ -117,26 +117,26 @@ def create_cube(size, mesh=None): mesh = Mesh("UnknownCube") # Add the 6 quads that create a cube - Mesh.create_quad(vector3(size[0] * 0.5, 0, 0), - vector3(0, -size[1] * 0.5, 0), - vector3(0, 0, size[2] * 0.5), mesh) - Mesh.create_quad(vector3(-size[0] * 0.5, 0, 0), - vector3(0, size[1] * 0.5, 0), - vector3(0, 0, size[2] * 0.5), mesh) - - Mesh.create_quad(vector3(0, size[1] * 0.5, 0), - vector3(size[0] * 0.5, 0), - vector3(0, 0, size[2] * 0.5), mesh) - Mesh.create_quad(vector3(0, -size[1] * 0.5, 0), - vector3(-size[0] * 0.5, 0), - vector3(0, 0, size[2] * 0.5), mesh) - - Mesh.create_quad(vector3(0, 0, size[2] * 0.5), - vector3(-size[0] * 0.5, 0), - vector3(0, size[1] * 0.5, 0), mesh) - Mesh.create_quad(vector3(0, 0, -size[2] * 0.5), - vector3(size[0] * 0.5, 0), - vector3(0, size[1] * 0.5, 0), mesh) + Mesh.create_quad(Vector3(size[0] * 0.5, 0, 0), + Vector3(0, -size[1] * 0.5, 0), + Vector3(0, 0, size[2] * 0.5), mesh) + Mesh.create_quad(Vector3(-size[0] * 0.5, 0, 0), + Vector3(0, size[1] * 0.5, 0), + Vector3(0, 0, size[2] * 0.5), mesh) + + Mesh.create_quad(Vector3(0, size[1] * 0.5, 0), + Vector3(size[0] * 0.5, 0), + Vector3(0, 0, size[2] * 0.5), mesh) + Mesh.create_quad(Vector3(0, -size[1] * 0.5, 0), + Vector3(-size[0] * 0.5, 0), + Vector3(0, 0, size[2] * 0.5), mesh) + + Mesh.create_quad(Vector3(0, 0, size[2] * 0.5), + Vector3(-size[0] * 0.5, 0), + Vector3(0, size[1] * 0.5, 0), mesh) + Mesh.create_quad(Vector3(0, 0, -size[2] * 0.5), + Vector3(size[0] * 0.5, 0), + Vector3(0, size[1] * 0.5, 0), mesh) return mesh @@ -167,15 +167,15 @@ def create_sphere(size, res_lat, res_lon, mesh=None): mesh = Mesh("UnknownSphere") # Compute half-size - if isinstance(size, vector3): + if isinstance(size, Vector3): hs = size * 0.5 else: - hs = vector3(size[0], size[1], size[2]) * 0.5 + hs = Vector3(size[0], size[1], size[2]) * 0.5 # Sphere is going to be composed by quads in most of the surface, but triangles near the # poles, so compute the bottom and top vertex - bottom_vertex = vector3(0, -hs.y, 0) - top_vertex = vector3(0, hs.y, 0) + bottom_vertex = Vector3(0, -hs.y, 0) + top_vertex = Vector3(0, hs.y, 0) lat_inc = math.pi / res_lat lon_inc = math.pi * 2 / res_lon @@ -186,8 +186,8 @@ def create_sphere(size, res_lat, res_lon, mesh=None): y = hs.y * math.sin(lat + lat_inc) c = math.cos(lat + lat_inc) for _ in range(0, res_lon): - p1 = vector3(c * math.cos(lon) * hs.x, y, c * math.sin(lon) * hs.z) - p2 = vector3(c * math.cos(lon + lon_inc) * hs.x, y, c * math.sin(lon + lon_inc) * hs.z) + p1 = Vector3(c * math.cos(lon) * hs.x, y, c * math.sin(lon) * hs.z) + p2 = Vector3(c * math.cos(lon + lon_inc) * hs.x, y, c * math.sin(lon + lon_inc) * hs.z) Mesh.create_tri(bottom_vertex, p1, p2, mesh) @@ -204,16 +204,16 @@ def create_sphere(size, res_lat, res_lon, mesh=None): lon = 0 for _ in range(0, res_lon): - p1 = vector3(c1 * math.cos(lon) * hs.x, + p1 = Vector3(c1 * math.cos(lon) * hs.x, y1, c1 * math.sin(lon) * hs.z) - p2 = vector3(c1 * math.cos(lon + lon_inc) * hs.x, + p2 = Vector3(c1 * math.cos(lon + lon_inc) * hs.x, y1, c1 * math.sin(lon + lon_inc) * hs.z) - p3 = vector3(c2 * math.cos(lon) * hs.x, + p3 = Vector3(c2 * math.cos(lon) * hs.x, y2, c2 * math.sin(lon) * hs.z) - p4 = vector3(c2 * math.cos(lon + lon_inc) * hs.x, + p4 = Vector3(c2 * math.cos(lon + lon_inc) * hs.x, y2, c2 * math.sin(lon + lon_inc) * hs.z) @@ -232,8 +232,8 @@ def create_sphere(size, res_lat, res_lon, mesh=None): y = hs.y * math.sin(lat) c = math.cos(lat) for _ in range(0, res_lon): - p1 = vector3(c * math.cos(lon) * hs.x, y, c * math.sin(lon) * hs.z) - p2 = vector3(c * math.cos(lon + lon_inc) * hs.x, y, c * math.sin(lon + lon_inc) * hs.z) + p1 = Vector3(c * math.cos(lon) * hs.x, y, c * math.sin(lon) * hs.z) + p2 = Vector3(c * math.cos(lon + lon_inc) * hs.x, y, c * math.sin(lon + lon_inc) * hs.z) Mesh.create_tri(top_vertex, p1, p2, mesh) @@ -250,12 +250,12 @@ def create_quad(origin, axis0, axis1, mesh): Arguments: - origin {vector3} -- Center of the quad + origin {Vector3} -- Center of the quad - axis0 {vector3} -- One of the axis of the quad. This is not normalized, since the + axis0 {Vector3} -- One of the axis of the quad. This is not normalized, since the length specifies half the length of that side along that axis - axis1 {vector3} -- One of the axis of the quad. This is not normalized, since the + axis1 {Vector3} -- One of the axis of the quad. This is not normalized, since the length specifies half the length of that side along that axis mesh {Mesh} -- Mesh to add the polygons. If not given, create a new mesh @@ -284,11 +284,11 @@ def create_tri(p1, p2, p3, mesh): Arguments: - p1 {vector3} -- First vertex of the triangle + p1 {Vector3} -- First vertex of the triangle - p2 {vector3} -- Second vertex of the triangle + p2 {Vector3} -- Second vertex of the triangle - p3 {vector3} -- Third vertex of the triangle + p3 {Vector3} -- Third vertex of the triangle mesh {Mesh} -- Mesh to add the polygons. If not given, create a new mesh diff --git a/object3d.py b/object3d.py index 4865721..1e2b167 100644 --- a/object3d.py +++ b/object3d.py @@ -2,7 +2,7 @@ import numpy as np from quaternion import quaternion, as_rotation_matrix -from vector3 import vector3 +from vector3 import Vector3 class Object3d: """3d object class. @@ -16,12 +16,12 @@ def __init__(self, name): """ self.name = name """ {str} Name of the object""" - self.position = vector3() - """ {vector3} Local position of the object (relative to parent)""" + self.position = Vector3() + """ {Vector3} Local position of the object (relative to parent)""" self.rotation = quaternion(1, 0, 0, 0) """ {quaternion} Local rotation of the object {relative to parent)""" - self.scale = vector3(1, 1, 1) - """ {vector3} Local scale of the object (relative to parent)""" + self.scale = Vector3(1, 1, 1) + """ {Vector3} Local scale of the object (relative to parent)""" self.mesh = None """ {Mesh} Mesh to be rendered in this object""" self.material = None @@ -95,9 +95,9 @@ def get_position(self): Returns: - {vector3} - Local position of the object + {Vector3} - Local position of the object """ - return vector3.from_np(vector3(0, 0, 0).to_np4(1) @ self.get_matrix()) + return Vector3.from_np(Vector3(0, 0, 0).to_np4(1) @ self.get_matrix()) def forward(self): """ @@ -106,9 +106,9 @@ def forward(self): Returns: - {vector3} - Local forward vector of the object + {Vector3} - Local forward vector of the object """ - return vector3.from_np(vector3(0, 0, 1).to_np4(0) @ self.get_matrix()) + return Vector3.from_np(Vector3(0, 0, 1).to_np4(0) @ self.get_matrix()) def up(self): """ @@ -117,9 +117,9 @@ def up(self): Returns: - {vector3} - Local up vector of the object + {Vector3} - Local up vector of the object """ - return vector3.from_np(vector3(0, 1, 0).to_np4(0) @ self.get_matrix()) + return Vector3.from_np(Vector3(0, 1, 0).to_np4(0) @ self.get_matrix()) def right(self): """ @@ -128,9 +128,9 @@ def right(self): Returns: - {vector3} - Local right vector of the object + {Vector3} - Local right vector of the object """ - return vector3.from_np(vector3(1, 0, 0).to_np4(0) @ self.get_matrix()) + return Vector3.from_np(Vector3(1, 0, 0).to_np4(0) @ self.get_matrix()) @staticmethod def get_prs_matrix(position, rotation, scale): @@ -139,11 +139,11 @@ def get_prs_matrix(position, rotation, scale): Arguments: - position {vector3} - Position + position {Vector3} - Position rotation {quaternion} - Rotation - scale {vector3} - Scale + scale {Vector3} - Scale Returns: diff --git a/sample_cubefall.py b/sample_cubefall.py index 83ef104..d10eaa1 100644 --- a/sample_cubefall.py +++ b/sample_cubefall.py @@ -10,8 +10,8 @@ from camera import Camera from mesh import Mesh from material import Material -from color import color -from vector3 import vector3 +from color import Color +from vector3 import Vector3 GRAVITY = -9.8 @@ -20,17 +20,17 @@ class FallingCube(Object3d): def __init__(self, mesh): super().__init__("FallingCube") # Create a cube on a random positions - self.position = vector3(random.uniform(-6, 6), random.uniform(6, 10), random.uniform(3, 10)) + self.position = Vector3(random.uniform(-6, 6), random.uniform(6, 10), random.uniform(3, 10)) self.mesh = mesh - # Pick a random color for the cube - self.material = Material(color(random.uniform(0.1, 1), + # Pick a random Color for the cube + self.material = Material(Color(random.uniform(0.1, 1), random.uniform(0.1, 1), random.uniform(0.1, 1), 1), "FallingCubeMaterial") # Starting velocity is zero self.velocity = 0 # Select a random rotation axis - self.rotation_axis = vector3(random.uniform(-1, 1), + self.rotation_axis = Vector3(random.uniform(-1, 1), random.uniform(-1, 1), random.uniform(-1, 1)).normalized() # Select a random rotation speed @@ -61,7 +61,7 @@ def main(): scene.camera = Camera(False, res_x, res_y) # Moves the camera back 2 units - scene.camera.position -= vector3(0, 0, 2) + scene.camera.position -= Vector3(0, 0, 2) # Create the cube mesh we're going to use for every single object cube_mesh = Mesh.create_cube((1, 1, 1)) diff --git a/sample_game.py b/sample_game.py index b258c26..785ddf2 100644 --- a/sample_game.py +++ b/sample_game.py @@ -12,8 +12,8 @@ from camera import Camera from mesh import Mesh from material import Material -from color import color -from vector3 import vector3, dot_product, cross_product +from color import Color +from vector3 import Vector3, dot_product, cross_product from perlin import noise2d class Missile(Object3d): @@ -29,24 +29,24 @@ class Missile(Object3d): def __init__(self): if Missile.missile_mesh is None: Missile.missile_mesh = Missile.create_missile_mesh() - Missile.missile_material = Material(color(1, 0, 0, 1), "MissileMaterial") + Missile.missile_material = Material(Color(1, 0, 0, 1), "MissileMaterial") super().__init__("Missile") # Determine the spawn position. There's 33% chance it comes from the right, 33% that it # comes from behind the mountains, and 34% chance it comes from the left - self.position = vector3(0, random.uniform(0, 3), 12) + self.position = Vector3(0, random.uniform(0, 3), 12) r = random.uniform(0, 100) if r > 66: self.position.x = 7 - self.rotation = from_rotation_vector((vector3(0, 1, 0) * math.radians(90)).to_np3()) + self.rotation = from_rotation_vector((Vector3(0, 1, 0) * math.radians(90)).to_np3()) elif r > 33: self.position.y = -2 self.position.x = random.uniform(-4, 4) - self.rotation = from_rotation_vector((vector3(1, 0, 0) * math.radians(90)).to_np3()) + self.rotation = from_rotation_vector((Vector3(1, 0, 0) * math.radians(90)).to_np3()) else: self.position.x = -7 - self.rotation = from_rotation_vector((vector3(0, 1, 0) * math.radians(-90)).to_np3()) + self.rotation = from_rotation_vector((Vector3(0, 1, 0) * math.radians(-90)).to_np3()) # Set the mesh and material of the missile self.mesh = Missile.missile_mesh @@ -65,7 +65,7 @@ def update(self, delta_time): # Rotate missile towards the player - Figure out where it is pointing and # where do we want to point it current_dir = self.forward() - desired_dir = (vector3(0, 0, 0) - self.position).normalized() + desired_dir = (Vector3(0, 0, 0) - self.position).normalized() # Find the angle between these two directions dp = np.clip(dot_product(current_dir, desired_dir), -1, 1) angle = math.acos(dp) @@ -90,18 +90,18 @@ def create_missile_mesh(): length = 0.075 cone = 0.05 missile_mesh = Mesh.create_cube((radius * 2, radius * 2, length * 2)) - missile_mesh = Mesh.create_tri(vector3(radius, radius, length), - vector3(radius, -radius, length), - vector3(0, 0, length + cone), missile_mesh) - missile_mesh = Mesh.create_tri(vector3(radius, -radius, length), - vector3(-radius, -radius, length), - vector3(0, 0, length + cone), missile_mesh) - missile_mesh = Mesh.create_tri(vector3(-radius, -radius, length), - vector3(-radius, radius, length), - vector3(0, 0, length + cone), missile_mesh) - missile_mesh = Mesh.create_tri(vector3(-radius, radius, length), - vector3(radius, radius, length), - vector3(0, 0, length + cone), missile_mesh) + missile_mesh = Mesh.create_tri(Vector3(radius, radius, length), + Vector3(radius, -radius, length), + Vector3(0, 0, length + cone), missile_mesh) + missile_mesh = Mesh.create_tri(Vector3(radius, -radius, length), + Vector3(-radius, -radius, length), + Vector3(0, 0, length + cone), missile_mesh) + missile_mesh = Mesh.create_tri(Vector3(-radius, -radius, length), + Vector3(-radius, radius, length), + Vector3(0, 0, length + cone), missile_mesh) + missile_mesh = Mesh.create_tri(Vector3(-radius, radius, length), + Vector3(radius, radius, length), + Vector3(0, 0, length + cone), missile_mesh) return missile_mesh @@ -117,16 +117,16 @@ class Shot(Object3d): def __init__(self): if Shot.shot_mesh is None: Shot.shot_mesh = Mesh.create_sphere((0.1, 0.1, 0.1), 4, 4) - Shot.shot_material = Material(color(1, 1, 0, 1), "ShotMaterial") + Shot.shot_material = Material(Color(1, 1, 0, 1), "ShotMaterial") super().__init__("Shot") # The position and direction will be overwritten by the code that spawns the shot - self.position = vector3(0, 0, 0) + self.position = Vector3(0, 0, 0) self.mesh = Shot.shot_mesh self.material = Shot.shot_material self.shot_speed = 6 - self.direction = vector3(0, 0, 0) + self.direction = Vector3(0, 0, 0) def update(self, delta_time): """Animates the missile.""" @@ -162,17 +162,17 @@ def create_terrain(): pz = size_z / div # For centering the terrain on the object center - origin = vector3(-size_x * 0.5, 0, 0) + origin = Vector3(-size_x * 0.5, 0, 0) terrain_mesh = Mesh("Terrain") # Create the geometry of the terrain and add it to the mesh for dz in range(0, div): for dx in range(0, div): - p1 = vector3(dx * px, 0, dz * pz) + origin - p2 = vector3((dx + 1) * px, 0, dz * pz) + origin - p3 = vector3((dx + 1) * px, 0, (dz + 1) * pz) + origin - p4 = vector3(dx * px, 0, (dz + 1) * pz) + origin + p1 = Vector3(dx * px, 0, dz * pz) + origin + p2 = Vector3((dx + 1) * px, 0, dz * pz) + origin + p3 = Vector3((dx + 1) * px, 0, (dz + 1) * pz) + origin + p4 = Vector3(dx * px, 0, (dz + 1) * pz) + origin p1.y = sample_height(p1.x, p1.z) p2.y = sample_height(p2.x, p2.z) @@ -188,12 +188,12 @@ def create_terrain(): terrain_mesh.polygons.append(poly) # Create materials for the terrain - terrain_material = Material(color(0.1, 0.6, 0.1, 1), "TerrainMaterial") + terrain_material = Material(Color(0.1, 0.6, 0.1, 1), "TerrainMaterial") # Create object to display the terrain obj = Object3d("TerrainObject") - obj.scale = vector3(1, 1, 1) - obj.position = vector3(0, -1, 1) + obj.scale = Vector3(1, 1, 1) + obj.position = Vector3(0, -1, 1) obj.mesh = terrain_mesh obj.material = terrain_material @@ -217,7 +217,7 @@ def main(): scene.camera = Camera(False, res_x, res_y) # Moves the camera back 2 units - scene.camera.position -= vector3(0, 0, 0) + scene.camera.position -= Vector3(0, 0, 0) # Creates the terrain meshes and materials terrain_object = create_terrain() @@ -232,8 +232,8 @@ def main(): # Storage for all missiles active in the game missiles = [] - # Flashing effect color - flash_color = color(0, 0, 0, 0) + # Flashing effect Color + flash_color = Color(0, 0, 0, 0) # Total time of the current flash total_flash_time = 0 # Time elapsed for the current flash @@ -327,7 +327,7 @@ def main(): # Check if the missile is close enough to the player to hit him if missile.position.magnitude() < 0.25: # It has hit the player, flash the screen red for one second - flash_color = color(1, 0, 1, 1) + flash_color = Color(1, 0, 1, 1) total_flash_time = flash_timer = 1 # Destroy missile (remove it from the scene and add it to the destruction # list so we can remove it in a bit) @@ -358,7 +358,7 @@ def main(): for missile in missiles: # Check the distance between the shot and the missile, it it is below # a threshould (in this case 0.5), destroy the missile and the shot - distance = vector3.distance(shot.position, missile.position) + distance = Vector3.distance(shot.position, missile.position) if distance < 0.5: # Add it to the missile destruction list, and remove it from the scene missiles_to_destroy.append(missile) @@ -367,7 +367,7 @@ def main(): shots_to_destroy.append(shot) scene.remove_object(shot) # Flash the screen cyan for 0.5 seconds - flash_color = color(0, 1, 1, 1) + flash_color = Color(0, 1, 1, 1) total_flash_time = flash_timer = 0.5 # Actually delete objects diff --git a/sample_hierarchy.py b/sample_hierarchy.py index 2a92741..fdb28b2 100644 --- a/sample_hierarchy.py +++ b/sample_hierarchy.py @@ -11,8 +11,8 @@ from camera import Camera from mesh import Mesh from material import Material -from color import color -from vector3 import vector3 +from color import Color +from vector3 import Vector3 # Define a main function, just to keep things nice and tidy def main(): @@ -32,28 +32,28 @@ def main(): scene.camera = Camera(False, res_x, res_y) # Moves the camera back 2 units - scene.camera.position -= vector3(0, 0, 2) + scene.camera.position -= Vector3(0, 0, 2) # Create a cube and place it in a scene, at position (0,0,0) obj1 = Object3d("TestObject") - obj1.scale = vector3(1, 1, 1) - obj1.position = vector3(0, -1, 0) + obj1.scale = Vector3(1, 1, 1) + obj1.position = Vector3(0, -1, 0) obj1.mesh = Mesh.create_cube((1, 1, 1)) - obj1.material = Material(color(1, 0, 0, 1), "TestMaterial1") + obj1.material = Material(Color(1, 0, 0, 1), "TestMaterial1") scene.add_object(obj1) # Create a second object, and add it as a child of the first object # When the first object rotates, this one will also mimic the transform obj2 = Object3d("ChildObject") - obj2.position += vector3(0, 0.75, 0) + obj2.position += Vector3(0, 0.75, 0) obj2.mesh = Mesh.create_cube((0.5, 0.5, 0.5)) - obj2.material = Material(color(0, 1, 0, 1), "TestMaterial2") + obj2.material = Material(Color(0, 1, 0, 1), "TestMaterial2") obj1.add_child(obj2) # Specify the rotation of the object. It will rotate 15 degrees around the axis given, # every second angle = 15 - axis = vector3(1, 0.7, 0.2) + axis = Vector3(1, 0.7, 0.2) axis.normalize() # Timer diff --git a/sample_sphere.py b/sample_sphere.py index cfc1e47..d95d957 100644 --- a/sample_sphere.py +++ b/sample_sphere.py @@ -10,8 +10,8 @@ from camera import Camera from mesh import Mesh from material import Material -from color import color -from vector3 import vector3 +from color import Color +from vector3 import Vector3 # Define a main function, just to keep things nice and tidy def main(): @@ -31,20 +31,20 @@ def main(): scene.camera = Camera(False, res_x, res_y) # Moves the camera back 2 units - scene.camera.position -= vector3(0, 0, 2) + scene.camera.position -= Vector3(0, 0, 2) # Create a sphere and place it in a scene, at position (0,0,0) obj1 = Object3d("TestObject") - obj1.scale = vector3(1, 1, 1) - obj1.position = vector3(0, 0, 0) + obj1.scale = Vector3(1, 1, 1) + obj1.position = Vector3(0, 0, 0) obj1.mesh = Mesh.create_sphere((1, 1, 1), 12, 12) - obj1.material = Material(color(1, 0, 0, 1), "TestMaterial1") + obj1.material = Material(Color(1, 0, 0, 1), "TestMaterial1") scene.add_object(obj1) # Specify the rotation of the object. It will rotate 15 degrees around the axis given, # every second angle = 15 - axis = vector3(1, 0.7, 0.2) + axis = Vector3(1, 0.7, 0.2) axis.normalize() # Timer diff --git a/sample_terrain.py b/sample_terrain.py index 90d39fd..1d0598b 100644 --- a/sample_terrain.py +++ b/sample_terrain.py @@ -10,8 +10,8 @@ from camera import Camera from mesh import Mesh from material import Material -from color import color -from vector3 import vector3, dot_product, cross_product +from color import Color +from vector3 import Vector3, dot_product, cross_product from perlin import noise2d def sample_height(x, y): @@ -49,7 +49,7 @@ def create_terrain(): pz = size_z / div # For centering the terrain on the object center - origin = vector3(-size_x * 0.5, 0, -size_z * 0.5) + origin = Vector3(-size_x * 0.5, 0, -size_z * 0.5) # Create the meshes for each type of terrain grass_mesh = Mesh("Terrain_Grass") @@ -59,10 +59,10 @@ def create_terrain(): for dz in range(0, div): for dx in range(0, div): - p1 = vector3(dx * px, 0, dz * pz) + origin - p2 = vector3((dx + 1) * px, 0, dz * pz) + origin - p3 = vector3((dx + 1) * px, 0, (dz + 1) * pz) + origin - p4 = vector3(dx * px, 0, (dz + 1) * pz) + origin + p1 = Vector3(dx * px, 0, dz * pz) + origin + p2 = Vector3((dx + 1) * px, 0, dz * pz) + origin + p3 = Vector3((dx + 1) * px, 0, (dz + 1) * pz) + origin + p4 = Vector3(dx * px, 0, (dz + 1) * pz) + origin p1.y = sample_height(p1.x, p1.z) p2.y = sample_height(p2.x, p2.z) @@ -94,16 +94,16 @@ def create_terrain(): else: # Check for cliff face, check the normal normal = cross_product((p3 - p1).normalized(), (p2 - p1).normalized()) - if dot_product(normal, vector3(0, 1, 0)) < 0.5: + if dot_product(normal, Vector3(0, 1, 0)) < 0.5: cliff_mesh.polygons.append(poly) else: grass_mesh.polygons.append(poly) # Create materials for the terrain - grass_material = Material(color(0.1, 0.6, 0.1, 1), "GrassMaterial") - snow_material = Material(color(0.8, 0.8, 0.8, 1), "SnowMaterial") - cliff_material = Material(color(0.4, 0.4, 0.4, 1), "CliffMaterial") - water_material = Material(color(0, 0.5, 0.7, 1), "WaterMaterial") + grass_material = Material(Color(0.1, 0.6, 0.1, 1), "GrassMaterial") + snow_material = Material(Color(0.8, 0.8, 0.8, 1), "SnowMaterial") + cliff_material = Material(Color(0.4, 0.4, 0.4, 1), "CliffMaterial") + water_material = Material(Color(0, 0.5, 0.7, 1), "WaterMaterial") # Return meshes and materials meshes = [grass_mesh, snow_mesh, cliff_mesh, water_mesh] @@ -129,22 +129,22 @@ def main(): scene.camera = Camera(False, res_x, res_y) # Moves the camera back 2 units - scene.camera.position -= vector3(0, 0, 4) - scene.camera.rotation = from_rotation_vector((vector3(1, 0, 0) * math.radians(-15)).to_np3()) + scene.camera.position -= Vector3(0, 0, 4) + scene.camera.rotation = from_rotation_vector((Vector3(1, 0, 0) * math.radians(-15)).to_np3()) # Creates the terrain meshes and materials terrain_meshes, terrain_materials = create_terrain() # Create container object for all the terrain sub-objects master_object = Object3d("TerrainObject") - master_object.position = vector3(0, -1, 0) + master_object.position = Vector3(0, -1, 0) scene.add_object(master_object) # Create the terrain objects and place it in a scene, at position (0,0,0) for _, (mesh, material) in enumerate(zip(terrain_meshes, terrain_materials)): obj = Object3d("TerrainObject") - obj.scale = vector3(1, 1, 1) - obj.position = vector3(0, 0, 0) + obj.scale = Vector3(1, 1, 1) + obj.position = Vector3(0, 0, 0) obj.mesh = mesh obj.material = material master_object.add_child(obj) @@ -152,7 +152,7 @@ def main(): # Specify the rotation of the object. It will rotate 15 degrees around the axis given, # every second angle = 15 - axis = vector3(0, 1, 0) + axis = Vector3(0, 1, 0) axis.normalize() # Timer diff --git a/vector3.py b/vector3.py index 64439a4..519e15b 100644 --- a/vector3.py +++ b/vector3.py @@ -14,7 +14,7 @@ def __str__(self): """Returns a readable version of the exception""" return f"Invalid operation ({self.op}) between {self.type1} and {self.type2}!" -class vector3: +class Vector3: """3d vector class. It stores XYZ values as floats.""" @@ -42,39 +42,39 @@ def __str__(self): return "(" + str(self.x) + "," + str(self.y) + "," + str(self.z) + ")" def __add__(self, v): - """Adds this vector3 to another. - If we try to add anything other than a vector3 to it, it throws the + """Adds this Vector3 to another. + If we try to add anything other than a Vector3 to it, it throws the InvalidOperationException. Arguments: - v {vector3} -- Vector to add + v {Vector3} -- Vector to add Returns: - vector3 - Sum of this vector3 and the given one + Vector3 - Sum of this Vector3 and the given one """ - if isinstance(v, vector3): - return vector3(self.x + v.x, self.y + v.y, self.z + v.z) + if isinstance(v, Vector3): + return Vector3(self.x + v.x, self.y + v.y, self.z + v.z) else: raise InvalidOperationException("add", type(self), type(v)) def __sub__(self, v): - """Subtracts a vector3 from this one. - If we try to subtract anything other than a vector3, it throws the + """Subtracts a Vector3 from this one. + If we try to subtract anything other than a Vector3, it throws the InvalidOperationException. Arguments: - v {vector3} -- Vector to subtract + v {Vector3} -- Vector to subtract Returns: - vector3 - Subraction of the given vector from this one + Vector3 - Subraction of the given vector from this one """ - if isinstance(v, vector3): - return vector3(self.x - v.x, self.y - v.y, self.z - v.z) + if isinstance(v, Vector3): + return Vector3(self.x - v.x, self.y - v.y, self.z - v.z) else: raise InvalidOperationException("sub", type(self), type(v)) def __mul__(self, v): - """Multiplies this vector3 by a scalar. + """Multiplies this Vector3 by a scalar. If we try to multiply anything other than a scalar, it throws the InvalidOperationException. @@ -83,15 +83,15 @@ def __mul__(self, v): multiplied by this number Returns: - vector3 - Multiplication of the vector3 + Vector3 - Multiplication of the Vector3 """ if isinstance(v, (int, float)): - return vector3(self.x * v, self.y * v, self.z * v) + return Vector3(self.x * v, self.y * v, self.z * v) else: raise InvalidOperationException("mult", type(self), type(v)) def __rmul__(self, v): - """Multiplies this vector3 by a scalar. + """Multiplies this Vector3 by a scalar. If we try to multiply anything other than a scalar, it throws the InvalidOperationException Arguments: @@ -99,88 +99,88 @@ def __rmul__(self, v): this number Returns: - vector3 - Multiplication of the vector3 + Vector3 - Multiplication of the Vector3 """ if isinstance(v, (int, float)): - return vector3(self.x * v, self.y * v, self.z * v) + return Vector3(self.x * v, self.y * v, self.z * v) else: raise InvalidOperationException("mult", type(self), type(v)) def __truediv__(self, v): - """Divides this vector3 by a scalar. + """Divides this Vector3 by a scalar. If we try to divide anything other than a scalar, it throws the InvalidOperationException Arguments: v {number} -- Scalar to divide: all components of the vector are divided by this number Returns: - vector3 - Division of the vector3 + Vector3 - Division of the Vector3 """ if isinstance(v, (int, float)): - return vector3(self.x / v, self.y / v, self.z / v) + return Vector3(self.x / v, self.y / v, self.z / v) else: raise InvalidOperationException("mult", type(self), type(v)) def __eq__(self, v): - """Checks if this vector3 is equal to the given one, with a tolerance of 0.0001. + """Checks if this Vector3 is equal to the given one, with a tolerance of 0.0001. Exception InvalidOperationException is thrown if we compare something other than a - vector3. + Vector3. Arguments: - v {vector3} -- Vector to compare + v {Vector3} -- Vector to compare Returns: Bool - True if the vectors are the same, false otherwise """ - if isinstance(v, vector3): + if isinstance(v, Vector3): return ((self - v).magnitude()) < 0.0001 else: raise InvalidOperationException("eq", type(self), type(v)) def __ne__(self, v): - """Checks if this vector3 is different to the given one, with a tolerance of 0.0001. + """Checks if this Vector3 is different to the given one, with a tolerance of 0.0001. Exception InvalidOperationException is thrown if we compare something other than a - vector3. + Vector3. Arguments: - v {vector3} -- Vector to compare + v {Vector3} -- Vector to compare Returns: Bool - True if the vectors are different, false otherwise """ - if isinstance(v, vector3): + if isinstance(v, Vector3): return ((self - v).magnitude()) > 0.0001 else: raise InvalidOperationException("neq", type(self), type(v)) def __isub__(self, v): - """Subtracts a vector3 from this one. - If we try to subtract anything other than a vector3, it throws the + """Subtracts a Vector3 from this one. + If we try to subtract anything other than a Vector3, it throws the InvalidOperationException. Arguments: - v {vector3} -- Vector to subtract + v {Vector3} -- Vector to subtract Returns: - vector3 - Subraction of the given vector from this one + Vector3 - Subraction of the given vector from this one """ return self - v def __iadd__(self, v): - """Adds this vector3 to another. - If we try to add anything other than a vector3 to it, it throws the + """Adds this Vector3 to another. + If we try to add anything other than a Vector3 to it, it throws the InvalidOperationException. Arguments: - v {vector3} -- Vector to add + v {Vector3} -- Vector to add Returns: - vector3 - Sum of this vector3 and the given one + Vector3 - Sum of this Vector3 and the given one """ return self + v def __imul__(self, v): - """Multiplies this vector3 by a scalar. + """Multiplies this Vector3 by a scalar. If we try to multiply anything other than a scalar, it throws the InvalidOperationException. @@ -189,32 +189,32 @@ def __imul__(self, v): multiplied by this number. Returns: - vector3 - Multiplication of the vector3 + Vector3 - Multiplication of the Vector3 """ return self * v def __idiv__(self, v): - """Divides this vector3 by a scalar. + """Divides this Vector3 by a scalar. If we try to divide anything other than a scalar, it throws the InvalidOperationException Arguments: v {number} -- Scalar to divide: all components of the vector are divided by this number Returns: - vector3 - Division of the vector3 + Vector3 - Division of the Vector3 """ return self / v def __neg__(self): - """Negates this vector3, component-wise. Equivelent to multiplying by (-1) + """Negates this Vector3, component-wise. Equivelent to multiplying by (-1) Returns: - vector3 - Negated vector3 + Vector3 - Negated Vector3 """ - return vector3(-self.x, -self.y, -self.z) + return Vector3(-self.x, -self.y, -self.z) def magnitude(self): - """Returns the magnitude of the vector3. + """Returns the magnitude of the Vector3. Returns: Number - Magnitude of the vector @@ -222,7 +222,7 @@ def magnitude(self): return math.sqrt(self.dot(self)) def magnitude_squared(self): - """Returns the squared magnitude of the vector3. + """Returns the squared magnitude of the Vector3. Returns: Number - Magnitude of the vector @@ -230,34 +230,34 @@ def magnitude_squared(self): return self.dot(self) def dot(self, v): - """Computes the dot product of this vector3 with another. - If we try to do this operation with anything other than a vector3, it throws + """Computes the dot product of this Vector3 with another. + If we try to do this operation with anything other than a Vector3, it throws the InvalidOperationException. Arguments: - v {vector3} -- Vector3 to do the dot product with + v {Vector3} -- Vector3 to do the dot product with Returns: Number - Scalar value corresponding to the dot product of both vectors """ - if isinstance(v, vector3): + if isinstance(v, Vector3): return self.x * v.x + self.y * v.y + self.z * v.z else: raise InvalidOperationException("dot", type(self), type(v)) def cross(self, v): - """Computes the cross product of this vector3 with another. - If we try to do this operation with anything other than a vector3, it throws + """Computes the cross product of this Vector3 with another. + If we try to do this operation with anything other than a Vector3, it throws the InvalidOperationException. Arguments: - v {vector3} -- Vector3 to do the cross product with + v {Vector3} -- Vector3 to do the cross product with Returns: - vector3 - Cross product of both vectors + Vector3 - Cross product of both vectors """ - if isinstance(v, vector3): - return vector3(self.y * v.z - self.z * v.y, + if isinstance(v, Vector3): + return Vector3(self.y * v.z - self.z * v.y, self.z * v.x - self.x * v.z, self.x * v.y - self.y * v.x) else: @@ -271,24 +271,24 @@ def normalize(self): self.z *= d def normalized(self): - """Returns the normalized version of this vector3 + """Returns the normalized version of this Vector3 Returns: - vector3 - Normalized vector + Vector3 - Normalized vector """ d = 1.0 / self.magnitude() - return vector3(self.x * d, self.y * d, self.z * d) + return Vector3(self.x * d, self.y * d, self.z * d) def x0z(self): """Returns this vector, but with the y component zeroed. Returns: - vector3 - (x,0,z) + Vector3 - (x,0,z) """ - return vector3(self.x, 0, self.z) + return Vector3(self.x, 0, self.z) def to_np3(self): - """Converts a vector3 to a 3-component numpy array + """Converts a Vector3 to a 3-component numpy array Returns: np.array - [x,y,z] @@ -296,7 +296,7 @@ def to_np3(self): return np.array([self.x, self.y, self.z]) def to_np4(self, w=1): - """Converts a vector3 to a 4-component numpy array, with the given w as the 4th component. + """Converts a Vector3 to a 4-component numpy array, with the given w as the 4th component. Arguments: w {number} - Value of the w component on the np.array. @@ -308,24 +308,24 @@ def to_np4(self, w=1): @staticmethod def from_np(np_array): - """Converts a np.array to a vector3. No validation is done on the array to confirm it + """Converts a np.array to a Vector3. No validation is done on the array to confirm it has 3 components. Arguments: np_array {np.array} - np.array with 3-components (rows or columns) Returns: - vector3 - A vector3 + Vector3 - A Vector3 """ - return vector3(np_array[0], np_array[1], np_array[2]) + return Vector3(np_array[0], np_array[1], np_array[2]) @staticmethod def distance(v1, v2): """Returns the distance between two positions/vectors Arguments: - v1 {vector3} - First vector - v2 {vector3} - Second vector + v1 {Vector3} - First vector + v2 {Vector3} - Second vector Returns: number - Distance between the two positions/vectors @@ -336,8 +336,8 @@ def dot_product(v1, v2): """Returns the dot product between two vectors Arguments: - v1 {vector3} - First vector - v2 {vector3} - Second vector + v1 {Vector3} - First vector + v2 {Vector3} - Second vector Returns: number - Dot product between the vectors @@ -348,10 +348,10 @@ def cross_product(v1, v2): """Returns the cross product between two vectors Arguments: - v1 {vector3} - First vector - v2 {vector3} - Second vector + v1 {Vector3} - First vector + v2 {Vector3} - Second vector Returns: - vector3 - Cross product between the vectors + Vector3 - Cross product between the vectors """ return v1.cross(v2)