def orthoimage(scene: Scene, imgsize: tuple = (800, 800), zoom: float = 1, azimuth: float = 0, elevation: float = 0) -> numpy.ndarray: """ Compute an orthographic view of a scene. :param scene: The scene to render :param imgsize: The size of the image :param azimuth: The azimuth (in degrees) of view to render :param elevation: The elevation (in degrees) of view to render :return: The resulting image """ z = ZBufferEngine(imgsize[0], imgsize[1], (255, 255, 255), eColorBased) bbx = BoundingBox(scene) center = bbx.getCenter() bbx.translate(-center) v = Vector3( Vector3.Spherical(-1, radians(azimuth), radians(90 - elevation))) up = Vector3( Vector3.Spherical(-1, radians(azimuth), radians(180 - elevation))) right = cross(up, v) frame = [v, right, up] m = Matrix4(Matrix3(*frame)) bbx.transform(m.inverse()) xmin = bbx.lowerLeftCorner.x xmax = bbx.upperRightCorner.x size = bbx.getSize() msize = max(size[1], size[2]) * 1.05 dist = 1 v *= (size[0] + dist) z.setOrthographicCamera(-msize, msize, -msize, msize, dist, dist + xmax - xmin) position = center + v z.lookAt(position, center, up) z.setLight(position, (255, 255, 255)) z.process(scene) i = z.getImage() return i.to_array()
def perspectiveimage(scene: Scene, imgsize: tuple = (800, 800), zoom: float = 1, azimuth: float = 0, elevation: float = 0) -> numpy.ndarray: """ Compute an orthographic view of a scene. :param scene: The scene to render :param imgsize: The size of the image :param azimuth: The azimuth (in degrees) of view to render :param elevation: The elevation (in degrees) of view to render :return: The resulting image """ z = ZBufferEngine(imgsize[0], imgsize[1], (255, 255, 255), eColorBased) bbx = BoundingBox(scene) center = bbx.getCenter() bbx.translate(-center) head = Vector3( Vector3.Spherical(-1, radians(azimuth), radians(90 - elevation))) up = Vector3( Vector3.Spherical(-1, radians(azimuth), radians(180 - elevation))) right = cross(up, head) frame = [head, right, up] m = Matrix4(Matrix3(*frame)) bbx.transform(m.inverse()) size = bbx.getSize() msize = max(size[1], size[2]) dist = ((msize * 1.05 * zoom + size[0])) near = max(0.01, ((msize * zoom) - size[0])) z.setPerspectiveCamera(90, imgsize[1] / imgsize[0], near, near + 4 * size[0]) position = center + head * dist z.lookAt(position, center, up) z.setLight(position, (255, 255, 255)) z.process(scene) i = z.getImage() return i.to_array()
def cubic_bezier3D(ctrl_points, uniform=False, stride=60): """Construct a nurbs from a set of ctrl_points ctrl_points are control points in space that define a cubic bezier nurbs as proposed in svg norm http://www.w3.org/TR/SVG/paths.html#PathDataCurveCommands An arc `i` of the resulting curve will interpolate points 4 * i, 4 * i +1, 4 * i + 2, 4 * i + 3. :Parameters: - `ctrl_points` (list of Vector) - a list of control points - `uniform` (bool) - if True, the parameterization of the curve will be chosen such that each segment will be of length 1 in u space, wathever its real geometrical size. - `stride` (int) - number of points to discretize the curve :Returns Type: :class:NurbsCurve """ degree = 3 ctrl_points = [Vector3(*vec) for vec in ctrl_points] nb_pts = len(ctrl_points) nb_arc = (nb_pts - 1) / degree nb_knots = degree + nb_pts p = 0. param = [p] for i in xrange(nb_arc): if uniform: p += 1 else: p += norm(ctrl_points[degree * i] \ - ctrl_points[degree * (i + 1)]) param.append(p) kv = [param[0]] for p in param: for j in xrange(degree): kv.append(p) kv.append(param[-1]) #curve return NurbsCurve([Vector4(v, 1.) for v in ctrl_points], kv, degree, stride)
def grid2D(shape): """ :param `shape`: shape of the grid tuple (nb_rows,nb_columns) """ t = Topomesh(2) positions = {} NBI, NBJ = shape NBCELLS = NBI * NBJ xincr = 1. / NBI yincr = 1. / NBJ cell_grid = Grid((NBI, NBJ)) for ind in cell_grid: cid = t.add_wisp(0, ind) point_grid = Grid((NBI + 1, NBJ + 1)) for ind in point_grid: i, j = point_grid.coordinates(ind) eid = t.add_wisp(2, ind) positions[eid] = Vector3(xincr * i, yincr * j, 0) #murs verticaux for j in xrange(NBJ): for i in xrange(NBI + 1): wid = t.add_wisp(1) t.link(1, wid, point_grid.index((i, j))) t.link(1, wid, point_grid.index((i, j + 1))) if i < NBI: t.link(0, cell_grid.index((i, j)), wid) if i > 0: t.link(0, cell_grid.index((i - 1, j)), wid) #murs horizontaux for i in xrange(NBI): for j in xrange(NBJ + 1): wid = t.add_wisp(1) t.link(1, wid, point_grid.index((i, j))) t.link(1, wid, point_grid.index((i + 1, j))) if j < NBJ: t.link(0, cell_grid.index((i, j)), wid) if j > 0: t.link(0, cell_grid.index((i, j - 1)), wid) #et voila return t, positions
def cell_geometry(cid, mesh, position): """ compute the triangle set representing the geometry of this cell """ trans = {} pos_list = [] face_list = [] for pid in mesh.borders(3, cid, 3): trans[pid] = len(pos_list) pos_list.append(position[pid]) for fid in mesh.borders(3, cid): pts = set() center_ind = len(pos_list) for eid in mesh.borders(2, fid): pid1, pid2 = mesh.borders(1, eid) face_list.append((center_ind, trans[pid1], trans[pid2])) pts.add(pid1) pts.add(pid2) pos_list.append( sum((position[pid] for pid in pts), Vector3()) / len(pts)) return TriangleSet(pos_list, face_list)
def getPointAt(self, u, v, w): """ Compute point at (u,v,w) """ udeg = self.udegree uspan = sg.NurbsCurve.findSpan(u, udeg, self.uknots) Nu = sg.NurbsCurve.basisFunctions(uspan, u, udeg, self.uknots) vdeg = self.vdegree vspan = sg.NurbsCurve.findSpan(v, vdeg, self.vknots) Nv = sg.NurbsCurve.basisFunctions(vspan, v, vdeg, self.vknots) wdeg = self.wdegree wspan = sg.NurbsCurve.findSpan(w, wdeg, self.wknots) Nw = sg.NurbsCurve.basisFunctions(wspan, w, wdeg, self.wknots) tmp = [[None for i in xrange(vdeg + 1)] for j in xrange(wdeg + 1)] for i in xrange(0, wdeg + 1): for j in xrange(0, vdeg + 1): tmpVec = Vector4(0, 0, 0, 0) for k in xrange(0, udeg + 1): tmpVec += self.points[uspan - udeg + k][vspan - vdeg + j][wspan - wdeg + i] * Nu[k] tmp[i][j] = tmpVec tmp2 = [None for i in xrange(wdeg + 1)] for i in xrange(0, wdeg + 1): tmpVec = Vector4(0, 0, 0, 0) for j in xrange(0, vdeg + 1): tmpVec += tmp[i][j] * Nv[j] tmp2[i] = tmpVec res = Vector4(0, 0, 0, 0) for i in xrange(0, wdeg + 1): res += tmp2[i] * Nw[i] if res.w != 0: return res.project() else: return Vector3(res.x, res.y, res.z)
def findClosest(self, p): ctrlpolyline = interpolated_profile.sg.Polyline([Vector3(i[0], i[1], 0) \ for i in self.curve.ctrlPointList]) lp, u = ctrlpolyline.findClosest(Vector3(p, 1)) return u
def insertPoint(self, index, npoint): if self.profile is not None: pt, u = self.curve.findClosest(Vector3(*npoint)) self.profile.create_control_point(u)
def make_section(pts, distances=None, **kargs): pts = map(lambda p: Vector3(p[0], p[1], p[2]), pts) return NurbsCurve.CSpline(pts, is_linear=False, distances=None)
def make_interpolation(curves, *args, **kargs): curves = map(sg.Point3Array, ((Vector3(p[0], p[1], p[2]) for p in c) for c in curves)) return CSplineMethod.Patch(curves, *args, **kargs)
def make_section(pts, distances=None, **kargs): pts = [Vector3(p[0], p[1], p[2]) for p in pts] return NurbsCurve.CSpline(pts, is_linear=False, distances=None)