2016-04-25 13:06:09 -03:00
|
|
|
# Copyright (C) 2016 Intel Corporation. All rights reserved.
|
|
|
|
#
|
|
|
|
# This file is free software: you can redistribute it and/or modify it
|
|
|
|
# under the terms of the GNU General Public License as published by the
|
|
|
|
# Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This file is distributed in the hope that it will be useful, but
|
|
|
|
# WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
# See the GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License along
|
|
|
|
# with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
import matplotlib.pyplot as plt
|
|
|
|
import matplotlib.patches as mpatches
|
|
|
|
|
|
|
|
from mpl_toolkits.mplot3d import Axes3D
|
|
|
|
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
|
|
|
|
|
|
|
|
import icosahedron as ico
|
2016-05-12 20:17:49 -03:00
|
|
|
import grid
|
2016-04-25 13:06:09 -03:00
|
|
|
|
|
|
|
fig = plt.figure()
|
|
|
|
ax = fig.add_subplot(111, projection='3d')
|
|
|
|
ax.set_xlim3d(-2, 2)
|
|
|
|
ax.set_ylim3d(-2, 2)
|
|
|
|
ax.set_zlim3d(-2, 2)
|
|
|
|
|
|
|
|
ax.set_xlabel('x')
|
|
|
|
ax.set_ylabel('y')
|
|
|
|
ax.set_zlabel('z')
|
|
|
|
|
|
|
|
ax.invert_zaxis()
|
|
|
|
ax.invert_xaxis()
|
|
|
|
|
|
|
|
ax.set_aspect('equal')
|
|
|
|
|
|
|
|
added_polygons = set()
|
2016-05-12 20:17:49 -03:00
|
|
|
added_sections = set()
|
2016-04-25 13:06:09 -03:00
|
|
|
|
|
|
|
def polygons(polygons):
|
|
|
|
for p in polygons:
|
|
|
|
polygon(p)
|
|
|
|
|
|
|
|
def polygon(polygon):
|
|
|
|
added_polygons.add(polygon)
|
|
|
|
|
2016-05-12 20:17:49 -03:00
|
|
|
def section(s):
|
|
|
|
added_sections.add(s)
|
|
|
|
|
|
|
|
def sections(sections):
|
|
|
|
for s in sections:
|
|
|
|
section(s)
|
|
|
|
|
2016-04-25 13:06:09 -03:00
|
|
|
def show(subtriangles=False):
|
|
|
|
polygons = []
|
|
|
|
facecolors = []
|
2016-05-12 20:27:24 -03:00
|
|
|
triangles_indexes = set()
|
2016-04-25 13:06:09 -03:00
|
|
|
|
2016-05-12 20:17:49 -03:00
|
|
|
subtriangle_facecolors = (
|
|
|
|
'#CCCCCC',
|
|
|
|
'#CCE5FF',
|
|
|
|
'#E5FFCC',
|
|
|
|
'#FFCCCC',
|
|
|
|
)
|
|
|
|
|
|
|
|
if added_sections:
|
|
|
|
subtriangles = True
|
|
|
|
|
2016-04-25 13:06:09 -03:00
|
|
|
for p in added_polygons:
|
|
|
|
try:
|
|
|
|
i = ico.triangles.index(p)
|
|
|
|
except ValueError:
|
|
|
|
polygons.append(p)
|
|
|
|
continue
|
|
|
|
|
|
|
|
if subtriangles:
|
2016-05-12 20:17:49 -03:00
|
|
|
sections(range(i * 4, i * 4 + 4))
|
2016-04-25 13:06:09 -03:00
|
|
|
else:
|
2016-05-12 20:27:24 -03:00
|
|
|
triangles_indexes.add(i)
|
2016-04-25 13:06:09 -03:00
|
|
|
polygons.append(p)
|
|
|
|
facecolors.append('#DDDDDD')
|
|
|
|
|
2016-05-12 20:17:49 -03:00
|
|
|
for s in added_sections:
|
2016-05-12 20:27:24 -03:00
|
|
|
triangles_indexes.add(int(s / 4))
|
2016-05-12 20:17:49 -03:00
|
|
|
subtriangle_index = s % 4
|
|
|
|
polygons.append(grid.section_triangle(s))
|
|
|
|
facecolors.append(subtriangle_facecolors[subtriangle_index])
|
|
|
|
|
2016-04-25 13:06:09 -03:00
|
|
|
ax.add_collection3d(Poly3DCollection(
|
|
|
|
polygons,
|
|
|
|
facecolors=facecolors,
|
|
|
|
edgecolors="#777777",
|
|
|
|
))
|
|
|
|
|
2016-05-12 20:27:24 -03:00
|
|
|
for i in triangles_indexes:
|
|
|
|
t = ico.triangles[i]
|
|
|
|
mx = my = mz = 0
|
|
|
|
for x, y, z in t:
|
|
|
|
mx += x
|
|
|
|
my += y
|
|
|
|
mz += z
|
|
|
|
ax.text(mx / 2.6, my / 2.6, mz / 2.6, i, color='#444444')
|
|
|
|
|
2016-04-25 13:06:09 -03:00
|
|
|
if subtriangles:
|
|
|
|
ax.legend(
|
2016-05-12 20:17:49 -03:00
|
|
|
handles=tuple(
|
|
|
|
mpatches.Patch(color=c, label='Sub-triangle #%d' % i)
|
|
|
|
for i, c in enumerate(subtriangle_facecolors)
|
2016-04-25 13:06:09 -03:00
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
plt.show()
|