aboutsummaryrefslogtreecommitdiff
path: root/geometry
diff options
context:
space:
mode:
Diffstat (limited to 'geometry')
-rw-r--r--geometry/box.py58
-rw-r--r--geometry/cylinder.py72
-rw-r--r--geometry/sphere.py36
3 files changed, 166 insertions, 0 deletions
diff --git a/geometry/box.py b/geometry/box.py
new file mode 100644
index 0000000..5538819
--- /dev/null
+++ b/geometry/box.py
@@ -0,0 +1,58 @@
+from OpenGL.GL import *
+
+class Box:
+ def __init__(self, x0, x1, y0, y1, z0, z1):
+ self.x0 = x0
+ self.x1 = x1
+ self.y0 = y0
+ self.y1 = y1
+ self.z0 = z0
+ self.z1 = z1
+
+ def indicator(self):
+ return lambda x, y, z: x >= self.x0 and x <= self.x1 and y >= self.y0 and y <= self.y1 and z >= self.z0 and z <= self.z1
+
+ def draw(self):
+ glBegin(GL_POLYGON)
+ glNormal(-1,0,0)
+ glVertex(self.x0, self.y0, self.z0)
+ glVertex(self.x0, self.y1, self.z0)
+ glVertex(self.x0, self.y1, self.z1)
+ glVertex(self.x0, self.y0, self.z1)
+ glEnd()
+ glBegin(GL_POLYGON)
+ glNormal(1,0,0)
+ glVertex(self.x1, self.y0, self.z0)
+ glVertex(self.x1, self.y1, self.z0)
+ glVertex(self.x1, self.y1, self.z1)
+ glVertex(self.x1, self.y0, self.z1)
+ glEnd()
+ glBegin(GL_POLYGON)
+ glNormal(0,0,1)
+ glVertex(self.x0, self.y0, self.z1)
+ glVertex(self.x1, self.y0, self.z1)
+ glVertex(self.x1, self.y1, self.z1)
+ glVertex(self.x0, self.y1, self.z1)
+ glEnd()
+ glBegin(GL_POLYGON)
+ glNormal(0,0,-1)
+ glVertex(self.x0, self.y0, self.z0)
+ glVertex(self.x1, self.y0, self.z0)
+ glVertex(self.x1, self.y1, self.z0)
+ glVertex(self.x0, self.y1, self.z0)
+ glEnd()
+ glBegin(GL_POLYGON)
+ glNormal(0,-1,0)
+ glVertex(self.x0, self.y0, self.z0)
+ glVertex(self.x1, self.y0, self.z0)
+ glVertex(self.x1, self.y0, self.z1)
+ glVertex(self.x0, self.y0, self.z1)
+ glEnd()
+ glBegin(GL_POLYGON)
+ glNormal(0,1,0)
+ glVertex(self.x0,self.y1,self.z0)
+ glVertex(self.x1,self.y1,self.z0)
+ glVertex(self.x1,self.y1,self.z1)
+ glVertex(self.x0,self.y1,self.z1)
+ glEnd()
+
diff --git a/geometry/cylinder.py b/geometry/cylinder.py
new file mode 100644
index 0000000..b576a62
--- /dev/null
+++ b/geometry/cylinder.py
@@ -0,0 +1,72 @@
+import numpy
+
+from OpenGL.GL import *
+
+class Cylinder:
+ def __init__(self, x, y, z, r, h = 0, l = 0):
+ self.x = x
+ self.y = y
+ self.z = z
+ self.r = r
+ self.h = h
+ self.l = l
+
+ self.circle = []
+ for i in range(33):
+ alpha = 2 * numpy.pi * (i/32)
+ c0 = self.r * numpy.cos(alpha)
+ c1 = self.r * numpy.sin(alpha)
+ self.circle.append((c0, c1))
+
+ def indicator(self):
+ if self.h == 0:
+ return lambda x, y, z: (x - self.x)**2 + (z - self.z)**2 < self.r*self.r and y >= self.y and y <= self.y+self.l
+ else:
+ return lambda x, y, z: (x - self.x)**2 + (y - self.y)**2 < self.r*self.r and z >= self.z and z <= self.z+self.h
+
+ def draw(self):
+ glBegin(GL_TRIANGLE_FAN)
+
+ if self.h == 0:
+ glNormal(0,-1, 0)
+ glVertex(self.x, self.y, self.z)
+ for (c0, c1) in self.circle:
+ glNormal(0, -1, 0)
+ glVertex(self.x+c0, self.y, self.z+c1)
+ else:
+ glNormal(0, 0, -1)
+ glVertex(self.x, self.y, self.z)
+ for (c0, c1) in self.circle:
+ glVertex(self.x+c0, self.y+c1, self.z)
+
+ glEnd()
+
+ glBegin(GL_TRIANGLE_FAN)
+ if self.h == 0:
+ glNormal(0, 1, 0)
+ glVertex(self.x, self.y+self.l, self.z)
+ for (c0, c1) in self.circle:
+ glVertex(self.x+c0, self.y+self.l, self.z+c1)
+ else:
+ glNormal(0, 0, 1)
+ glVertex(self.x, self.y, self.z+self.h)
+ for (c0, c1) in self.circle:
+ glVertex(self.x+c0, self.y+c1, self.z+self.h)
+
+ glEnd()
+
+ glBegin(GL_TRIANGLE_STRIP)
+
+ if self.h == 0:
+ for (c0, c1) in self.circle:
+ glNormal(c0, 0, c1)
+ glVertex(self.x+c0, self.y, self.z+c1)
+ glVertex(self.x+c0, self.y + self.l, self.z+c1)
+ else:
+ for (c0, c1) in self.circle:
+ glNormal(c0, c1, 0)
+ glVertex(self.x+c0, self.y+c1, self.z)
+ glVertex(self.x+c0, self.y+c1, self.z+self.h)
+
+ glEnd()
+
diff --git a/geometry/sphere.py b/geometry/sphere.py
new file mode 100644
index 0000000..9bc245c
--- /dev/null
+++ b/geometry/sphere.py
@@ -0,0 +1,36 @@
+import numpy
+
+from OpenGL.GL import *
+
+class Sphere:
+ def __init__(self, x, y, z, r):
+ self.x = x
+ self.y = y
+ self.z = z
+ self.r = r
+
+ def indicator(self):
+ return lambda x, y, z: (x - self.x)**2 + (y - self.y)**2 + (z - self.z)**2 < self.r*self.r
+
+ def draw(self, resolution = 32):
+ for i in range(0,resolution+1):
+ lat0 = numpy.pi * (-0.5 + (i - 1) / resolution)
+ z0 = numpy.sin(lat0)
+ zr0 = numpy.cos(lat0)
+
+ lat1 = numpy.pi * (-0.5 + i / resolution)
+ z1 = numpy.sin(lat1)
+ zr1 = numpy.cos(lat1)
+
+ glBegin(GL_QUAD_STRIP)
+ for j in range(0,resolution+1):
+ lng = 2 * numpy.pi * (j - 1) / resolution
+ x = numpy.cos(lng)
+ y = numpy.sin(lng)
+
+ glNormal(x * zr0, y * zr0, z0)
+ glVertex(self.x + self.r * x * zr0, self.y + self.r * y * zr0, self.z + self.r * z0)
+ glNormal(x * zr1, y * zr1, z1)
+ glVertex(self.x + self.r * x * zr1, self.y + self.r * y * zr1, self.z + self.r * z1)
+
+ glEnd()