STLEngine

This commit is contained in:
Laura Klünder 2017-11-09 21:00:20 +01:00
parent 8eab7cca96
commit b18ea92a9b
4 changed files with 29 additions and 1 deletions

View file

@ -2,6 +2,7 @@ from django.conf import settings
from django.core import checks from django.core import checks
from c3nav.mapdata.render.engines.svg import SVGEngine # noqa from c3nav.mapdata.render.engines.svg import SVGEngine # noqa
from c3nav.mapdata.render.engines.stl import STLEngine # noqa
@checks.register() @checks.register()

View file

@ -16,6 +16,9 @@ class Base3DEngine(RenderEngine):
self.np_scale = np.array((self.scale, self.scale, self.scale)) self.np_scale = np.array((self.scale, self.scale, self.scale))
self.np_offset = np.array((-self.minx * self.scale, -self.maxy * self.scale, 0)) self.np_offset = np.array((-self.minx * self.scale, -self.maxy * self.scale, 0))
def _add_geometry(self, geometry, fill: Optional[FillAttribs], stroke: Optional[StrokeAttribs], **kwargs):
if fill is not None:
self.vertices.append(self._place_geometry(geometry))
def _append_to_vertices(self, vertices, append=None): def _append_to_vertices(self, vertices, append=None):
if append is not None: if append is not None:

View file

@ -0,0 +1,24 @@
import numpy as np
from c3nav.mapdata.render.engines.base3d import Base3DEngine
class STLEngine(Base3DEngine):
facet_template = (b' facet normal %f %f %f\n'
b' outer loop\n'
b' vertex %f %f %f\n'
b' vertex %f %f %f\n'
b' vertex %f %f %f\n'
b' endloop\n'
b' endfacet')
def _create_facet(self, facet) -> bytes:
return self.facet_template % tuple(facet.flatten())
def render(self) -> bytes:
facets = np.vstack(self.vertices)
facets = np.hstack((np.cross(facets[:, 1]-facets[:, 0], facets[:, 2]-facets[:, 1]).reshape((-1, 1, 3))*1e10,
facets))
return (b'solid c3nav_export\n' +
b'\n'.join((self._create_facet(facet) for facet in facets)) +
b'\nendsolid c3nav_export\n')

View file

@ -4,7 +4,7 @@ from shapely import prepared
from shapely.geometry import box from shapely.geometry import box
from c3nav.mapdata.cache import MapHistory from c3nav.mapdata.cache import MapHistory
from c3nav.mapdata.models import MapUpdate, Level from c3nav.mapdata.models import Level, MapUpdate
from c3nav.mapdata.render.data import LevelRenderData, hybrid_union from c3nav.mapdata.render.data import LevelRenderData, hybrid_union
from c3nav.mapdata.render.engines.base import FillAttribs, StrokeAttribs from c3nav.mapdata.render.engines.base import FillAttribs, StrokeAttribs