def travelOut(xr=0.1, N=100): import KCore.Vector as Vector posCam = getState('posCam') posEye = getState('posEye') dirCam = getState('dirCam') d1 = Vector.sub(posEye, posCam) R = Vector.norm(d1) L = R * xr d2 = Vector.mul(L, d1) P2 = Vector.sub(posCam, d2) checkPoints = [posCam, tuple(P2)] moveCamera(checkPoints, N=N)
def travelDown(xr=0.1, N=100): import KCore.Vector as Vector posCam = getState('posCam') posEye = getState('posEye') dirCam = getState('dirCam') d1 = Vector.sub(posEye, posCam) R = Vector.norm(d1) L = 2 * 3.14 * R * xr * 1. d2 = Vector.normalize(dirCam) d2 = Vector.mul(L, d2) P2 = Vector.sub(posCam, d2) checkPoints = [posCam, tuple(P2)] moveCamera(checkPoints, N=N)
def getVectorsFromCanvas(): e1 = (1,0,0); e2 = (0,1,0) node = Internal.getNodeFromName(CTK.t, 'CANVAS') if node is None: return e1,e2 zones = Internal.getNodesFromType(node, 'Zone_t') if zones == []: return e1,e2 zone = zones[0] if (Internal.getZoneType(zone) != 1): return e1,e2 [x1,y1,z1] = C.getValue(zone, Internal.__GridCoordinates__, (1,1,1)) [x2,y2,z2] = C.getValue(zone, Internal.__GridCoordinates__, (2,1,1)) [x3,y3,z3] = C.getValue(zone, Internal.__GridCoordinates__, (1,2,1)) e1 = (x2-x1, y2-y1, z2-z1) e2 = (x3-x1, y3-y1, z3-z1) e1 = Vector.normalize(e1) e2 = Vector.normalize(e2) return e1,e2
def translate(): if CTK.t == []: return if CTK.__MAINTREE__ <= 0: CTK.TXT.insert('START', 'Fail on a temporary tree.\n') CTK.TXT.insert('START', 'Error: ', 'Error') return v = CTK.varsFromWidget(VARS[0].get(), type=1) if len(v) != 3: CTK.TXT.insert('START', 'Translation vector is incorrect.\n') CTK.TXT.insert('START', 'Error: ', 'Error') return nzs = CPlot.getSelectedZones() if nzs == []: CTK.TXT.insert('START', 'Selection is empty.\n') CTK.TXT.insert('START', 'Error: ', 'Error') return CTK.saveTree() axis = VARS[6].get() if axis == 'along view': posCam = CPlot.getState('posCam') posEye = CPlot.getState('posEye') dirCam = CPlot.getState('dirCam') axe1 = (posEye[0] - posCam[0], posEye[1] - posCam[1], posEye[2] - posCam[2]) axe2 = dirCam axe3 = (axe1[1] * axe2[2] - axe1[2] * axe2[1], axe1[2] * axe2[0] - axe1[0] * axe2[2], axe1[0] * axe2[1] - axe1[1] * axe2[0]) axe1 = Vector.normalize(axe1) axe2 = Vector.normalize(axe2) axe3 = Vector.normalize(axe3) ax = v[0] * axe1[0] + v[1] * axe2[0] + v[2] * axe3[0] ay = v[0] * axe1[1] + v[1] * axe2[1] + v[2] * axe3[1] az = v[0] * axe1[2] + v[1] * axe2[2] + v[2] * axe3[2] v[0] = ax v[1] = ay v[2] = az for nz in nzs: nob = CTK.Nb[nz] + 1 noz = CTK.Nz[nz] a = T.translate(CTK.t[2][nob][2][noz], (v[0], v[1], v[2])) CTK.replace(CTK.t, nob, noz, a) CTK.TXT.insert('START', 'Zones have been translated.\n') CTK.TKTREE.updateApp() CPlot.render()
def drawFreeHand(): global CURRENTZONE; global CURRENTPOLYLINE; global ALLZONES w = WIDGETS['draw'] prev = []; first = [] if CTK.__BUSY__ == False: CPlot.unselectAllZones() CTK.saveTree() CTK.__BUSY__ = True TTK.sunkButton(w) CPlot.setState(cursor=1) buttonState = 0 while CTK.__BUSY__ == True: if prev == []: # first point l = [] while l == []: l = CPlot.getActivePoint() if l != []: prev = l; first = l time.sleep(CPlot.__timeStep__) w.update() if CTK.__BUSY__ == False: break else: # next points diff = -1. while (diff < 1.e-10): (buttonState,x,y,z) = CPlot.getMouseState() l = (x,y,z) diff = Vector.norm2(Vector.sub(l,prev)) diff1 = Vector.norm2(Vector.sub(l,first)) if (diff1 < 1.e-10): l = first if (buttonState == 5): break time.sleep(CPlot.__timeStep__) w.update() if (CTK.__BUSY__ == False): break prev = l CPlot.unselectAllZones() if (buttonState == 5): # button released ALLZONES.append(CURRENTZONE) CURRENTZONE = None; prev = []; first = [] CURRENTPOLYLINE = [] CTK.TKTREE.updateApp() if (CTK.__BUSY__ == True and buttonState != 5): CURRENTPOLYLINE.append((l[0],l[1],l[2])) if (CURRENTZONE == None): CTK.t = C.addBase2PyTree(CTK.t, 'CONTOURS', 1) base = Internal.getNodeFromName1(CTK.t, 'CONTOURS') nob = C.getNobOfBase(base, CTK.t) a = D.polyline(CURRENTPOLYLINE) CURRENTZONE = a CTK.add(CTK.t, nob, -1, a) ret = Internal.getParentOfNode(CTK.t, CURRENTZONE) noz = ret[1] else: a = D.polyline(CURRENTPOLYLINE) CURRENTZONE = a CTK.replace(CTK.t, nob, noz, a) (CTK.Nb, CTK.Nz) = CPlot.updateCPlotNumbering(CTK.t) CPlot.render() buttonState = 0 CTK.__BUSY__ = False TTK.raiseButton(w) CPlot.setState(cursor=0) else: CTK.__BUSY__ = False surfaces = getSurfaces() if (surfaces != []): if (CURRENTZONE != None): ALLZONES += [CURRENTZONE] for s in ALLZONES: ret = Internal.getParentOfNode(CTK.t, s) nob = C.getNobOfBase(ret[0], CTK.t) a = T.projectOrthoSmooth(s, surfaces) noz = ret[1] CTK.replace(CTK.t, nob, noz, a) #C._fillMissingVariables(CTK.t) (CTK.Nb, CTK.Nz) = CPlot.updateCPlotNumbering(CTK.t) CTK.TKTREE.updateApp() CPlot.render() CURRENTZONE = None; ALLZONES = [] CURRENTPOLYLINE = [] TTK.raiseButton(w) CPlot.setState(cursor=0)
def drawRectangle(npts): CTK.t = C.addBase2PyTree(CTK.t, 'CONTOURS', 1) nodes = Internal.getNodesFromName1(CTK.t, 'CONTOURS') nob = C.getNobOfBase(nodes[0], CTK.t) CTK.TXT.insert('START', 'Click left/lower corner...\n') w = WIDGETS['draw'] prev = []; second = [] if (CTK.__BUSY__ == False): CTK.__BUSY__ = True TTK.sunkButton(w) CPlot.setState(cursor=1) while (CTK.__BUSY__ == True): CPlot.unselectAllZones() CTK.saveTree() surfaces = getSurfaces() l = [] while (l == []): l = CPlot.getActivePoint() time.sleep(CPlot.__timeStep__) w.update() if (CTK.__BUSY__ == False): break if (CTK.__BUSY__ == True): if (prev == []): prev = l CTK.TXT.insert('START', 'Click right/up corner...\n') elif (prev != l): e1,e2 = getVectorsFromCanvas() e1n = Vector.norm(e1) e2n = Vector.norm(e2) if (e2n > e1n): e1 = e2 P1 = l; P2 = prev P1P2 = Vector.sub(P2, P1) P1P2n = Vector.norm(P1P2) Q = Vector.norm(Vector.cross(e1, P1P2)) L = math.sqrt( P1P2n*P1P2n - Q*Q ) sign = Vector.dot(e1, P1P2) if (sign > 0): e1 = Vector.mul(L, e1) else: e1 = Vector.mul(-L, e1) P3 = Vector.add(P1, e1) P4 = Vector.sub(P2, e1) l1 = D.line(P1, P3, npts) l2 = D.line(P3, P2, npts) l3 = D.line(P2, P4, npts) l4 = D.line(P4, P1, npts) rect = T.join([l1,l2,l3,l4]) if (surfaces != []): rect = T.projectOrthoSmooth(rect, surfaces) CTK.add(CTK.t, nob, -1, rect) CTK.TXT.insert('START', 'Rectangle created.\n') CTK.__BUSY__ = False TTK.raiseButton(w) CPlot.setState(cursor=0) #C._fillMissingVariables(CTK.t) (CTK.Nb, CTK.Nz) = CPlot.updateCPlotNumbering(CTK.t) CTK.TKTREE.updateApp() CPlot.render() CPlot.setState(cursor=0) prev = [] return CTK.__BUSY__ = False TTK.raiseButton(w) CPlot.setState(cursor=0) else: CTK.__BUSY__ = False TTK.raiseButton(w) CPlot.setState(cursor=0)
def drawArc(npts): CTK.t = C.addBase2PyTree(CTK.t, 'CONTOURS', 1) nodes = Internal.getNodesFromName1(CTK.t, 'CONTOURS') nob = C.getNobOfBase(nodes[0], CTK.t) CTK.TXT.insert('START', 'Click first point...\n') w = WIDGETS['draw'] prev = []; second = [] if CTK.__BUSY__ == False: CTK.__BUSY__ = True TTK.sunkButton(w) CPlot.setState(cursor=1) while CTK.__BUSY__: CPlot.unselectAllZones() CTK.saveTree() surfaces = getSurfaces() l = [] while (l == []): l = CPlot.getActivePoint() time.sleep(CPlot.__timeStep__) w.update() if (CTK.__BUSY__ == False): break if (CTK.__BUSY__ == True): if (prev == []): prev = l CTK.TXT.insert('START', 'Click second point...\n') elif (second == [] and prev != l): second = l CTK.TXT.insert('START', 'Click third point...\n') elif (prev != l and second != l): x1 = l[0]; y1 = l[1]; z1 = l[2] x2 = prev[0]; y2 = prev[1]; z2 = prev[2] x3 = second[0]; y3 = second[1]; z3 = second[2] xa = x2 - x1; ya = y2 - y1; za = z2 - z1 xb = x3 - x1; yb = y3 - y1; zb = z3 - z1 xc = x3 - x2; yc = y3 - y2; zc = z3 - z2 a2 = xa*xa + ya*ya + za*za b2 = xb*xb + yb*yb + zb*zb c2 = xc*xc + yc*yc + zc*zc A = 2*b2*c2 + 2*c2*a2 + 2*a2*b2 - a2*a2 - b2*b2 - c2*c2 R = math.sqrt( a2*b2*c2 / A ) nx = ya*zb - za*yb ny = za*xb - xa*zb nz = xa*yb - ya*xb tx = ya*nz - za*ny ty = za*nx - xa*nz tz = xa*ny - ya*nx norm = tx*tx + ty*ty + tz*tz normi = 1./math.sqrt(norm) tx = tx*normi; ty = ty*normi; tz = tz*normi; alpha = R*R - (xa*xa+ya*ya+za*za)*0.25 alpha = math.sqrt(alpha) center = [0,0,0] center[0] = 0.5*(x1+x2) + alpha*tx center[1] = 0.5*(y1+y2) + alpha*ty center[2] = 0.5*(z1+z2) + alpha*tz dx3 = center[0]-x3; dy3 = center[1]-y3; dz3 = center[2]-z3 l = dx3*dx3 + dy3*dy3 + dz3*dz3 if (abs(l - R*R) > 1.e-10): center[0] = 0.5*(x1+x2) - alpha*tx center[1] = 0.5*(y1+y2) - alpha*ty center[2] = 0.5*(z1+z2) - alpha*tz dx3 = center[0]-x3; dy3 = center[1]-y3; dz3 = center[2]-z3 l = dx3*dx3 + dy3*dy3 + dz3*dz3 e1 = [x1-center[0], y1-center[1], z1-center[2]] e2 = [x2-center[0], y2-center[1], z2-center[2]] e3 = Vector.cross(e1, e2) e4 = Vector.cross(e1, e3) # Images des pts dans le plan xyz pt1 = D.point((x1,y1,z1)) pt2 = D.point((x2,y2,z2)) pt3 = D.point((x3,y3,z3)) pt1 = T.rotate(pt1, (center[0], center[1], center[2]), (e1, e4, e3), ((1,0,0), (0,1,0), (0,0,1)) ) pt2 = T.rotate(pt2, (center[0], center[1], center[2]), (e1, e4, e3), ((1,0,0), (0,1,0), (0,0,1))) pt3 = T.rotate(pt3, (center[0], center[1], center[2]), (e1, e4, e3), ((1,0,0), (0,1,0), (0,0,1))) xp1 = C.getValue(pt1, 'CoordinateX', 0) yp1 = C.getValue(pt1, 'CoordinateY', 0) zp1 = C.getValue(pt1, 'CoordinateZ', 0) xp2 = C.getValue(pt2, 'CoordinateX', 0) yp2 = C.getValue(pt2, 'CoordinateY', 0) zp2 = C.getValue(pt2, 'CoordinateZ', 0) xp3 = C.getValue(pt3, 'CoordinateX', 0) yp3 = C.getValue(pt3, 'CoordinateY', 0) zp3 = C.getValue(pt3, 'CoordinateZ', 0) dx1 = (xp1-center[0])/R; dy1 = (yp1-center[1])/R if dx1 > 1.: dx1 = 1. if dx1 < -1.: dx1 = -1. if dy1 > 0: teta1 = math.acos(dx1) else: teta1 = 2*math.pi - math.acos(dx1) teta1 = teta1*180./math.pi; teta1 = 360. dx2 = (xp2-center[0])/R; dy2 = (yp2-center[1])/R if dx2 > 1.: dx2 = 1. if dx2 < -1.: dx2 = -1. if dy2 > 0: teta2 = math.acos(dx2) else: teta2 = 2*math.pi - math.acos(dx2) teta2 = teta2*180./math.pi dx3 = (xp3-center[0])/R; dy3 = (yp3-center[1])/R if dx3 > 1.: dx3 = 1. if dx3 < -1.: dx3 = -1. if dy3 > 0: teta3 = math.acos(dx3) else: teta3 = 2*math.pi - math.acos(dx3) teta3 = teta3*180./math.pi if teta3 > teta2: teta1 = 360. else: teta1 = 0. circle = D.circle((center[0],center[1],center[2]), R, tetas=teta2, tetae=teta1, N=npts) circle = T.rotate(circle, (center[0], center[1], center[2]), ((1,0,0), (0,1,0), (0,0,1)), (e1, e4, e3)) if surfaces != []: circle = T.projectOrthoSmooth(circle, surfaces) CTK.add(CTK.t, nob, -1, circle) CTK.TXT.insert('START', 'Circle created.\n') CTK.__BUSY__ = False TTK.raiseButton(w) CPlot.setState(cursor=0) #C._fillMissingVariables(CTK.t) (CTK.Nb, CTK.Nz) = CPlot.updateCPlotNumbering(CTK.t) CTK.TKTREE.updateApp() CPlot.render() CPlot.setState(cursor=0) prev = [] return CTK.__BUSY__ = False TTK.raiseButton(w) CPlot.setState(cursor=0) else: CTK.__BUSY__ = False TTK.raiseButton(w) CPlot.setState(cursor=0)
def drawCircle(npts): CTK.t = C.addBase2PyTree(CTK.t, 'CONTOURS', 1) nodes = Internal.getNodesFromName1(CTK.t, 'CONTOURS') nob = C.getNobOfBase(nodes[0], CTK.t) CTK.TXT.insert('START', 'Click first point...\n') w = WIDGETS['draw'] prev = []; second = [] if CTK.__BUSY__ == False: CTK.__BUSY__ = True TTK.sunkButton(w) CPlot.setState(cursor=1) while CTK.__BUSY__: CPlot.unselectAllZones() CTK.saveTree() surfaces = getSurfaces() l = [] while (l == []): l = CPlot.getActivePoint() time.sleep(CPlot.__timeStep__) w.update() if (CTK.__BUSY__ == False): break if (CTK.__BUSY__ == True): if prev == []: prev = l CTK.TXT.insert('START', 'Click second point...\n') elif (second == [] and prev != l): second = l CTK.TXT.insert('START', 'Click third point...\n') elif (prev != l and second != l): x1 = l[0]; y1 = l[1]; z1 = l[2] x2 = prev[0]; y2 = prev[1]; z2 = prev[2] x3 = second[0]; y3 = second[1]; z3 = second[2] xa = x2 - x1; ya = y2 - y1; za = z2 - z1 xb = x3 - x1; yb = y3 - y1; zb = z3 - z1 xc = x3 - x2; yc = y3 - y2; zc = z3 - z2 a2 = xa*xa + ya*ya + za*za b2 = xb*xb + yb*yb + zb*zb c2 = xc*xc + yc*yc + zc*zc A = 2*b2*c2 + 2*c2*a2 + 2*a2*b2 - a2*a2 - b2*b2 - c2*c2 R = math.sqrt( a2*b2*c2 / A ) nx = ya*zb - za*yb ny = za*xb - xa*zb nz = xa*yb - ya*xb tx = ya*nz - za*ny ty = za*nx - xa*nz tz = xa*ny - ya*nx norm = tx*tx + ty*ty + tz*tz normi = 1./math.sqrt(norm) tx = tx*normi; ty = ty*normi; tz = tz*normi; alpha = R*R - (xa*xa+ya*ya+za*za)*0.25 alpha = math.sqrt(alpha) center = [0,0,0] center[0] = 0.5*(x1+x2) + alpha*tx center[1] = 0.5*(y1+y2) + alpha*ty center[2] = 0.5*(z1+z2) + alpha*tz l = (center[0]-x3)*(center[0]-x3) + \ (center[1]-y3)*(center[1]-y3) + \ (center[2]-z3)*(center[2]-z3) if (abs(l - R*R) > 1.e-10): center[0] = 0.5*(x1+x2) - alpha*tx center[1] = 0.5*(y1+y2) - alpha*ty center[2] = 0.5*(z1+z2) - alpha*tz l = (center[0]-x3)*(center[0]-x3) + \ (center[1]-y3)*(center[1]-y3) + \ (center[2]-z3)*(center[2]-z3) circle = D.circle( (center[0],center[1],center[2]), R, N=npts) e1 = [x1-center[0], y1-center[1], z1-center[2]] e2 = [x2-center[0], y2-center[1], z2-center[2]] e3 = Vector.cross(e1, e2) e4 = Vector.cross(e1, e3) circle = T.rotate(circle, (center[0], center[1], center[2]), ((1,0,0), (0,1,0), (0,0,1)), (e1, e4, e3)) if (surfaces != []): circle = T.projectOrthoSmooth(circle, surfaces) CTK.add(CTK.t, nob, -1, circle) CTK.TXT.insert('START', 'Circle created.\n') CTK.__BUSY__ = False TTK.raiseButton(w) CPlot.setState(cursor=0) #C._fillMissingVariables(CTK.t) (CTK.Nb, CTK.Nz) = CPlot.updateCPlotNumbering(CTK.t) CTK.TKTREE.updateApp() CPlot.render() CPlot.setState(cursor=0) prev = [] return CTK.__BUSY__ = False TTK.raiseButton(w) CPlot.setState(cursor=0) else: CTK.__BUSY__ = False TTK.raiseButton(w) CPlot.setState(cursor=0)
def stretch1D(h): fail = False nzs = CPlot.getSelectedZones() nz = nzs[0] nob = CTK.Nb[nz]+1 noz = CTK.Nz[nz] z = CTK.t[2][nob][2][noz] dims = Internal.getZoneDim(z) try: if dims[0] == 'Unstructured': a = C.convertBAR2Struct(z) else: a = z except Exception as e: #print 'Error: stretch1D: %s.'%str(e) Panels.displayErrors([0,str(e)], header='Error: stretch1D') return True # Fail ind = CPlot.getActivePointIndex() if ind == []: return True # Fail ind = ind[0] l = D.getLength(a) a = D.getCurvilinearAbscissa(a) zp = D.getCurvilinearAbscissa(z) distrib = C.cpVars(a, 's', a, 'CoordinateX') C._initVars(distrib, 'CoordinateY', 0.) C._initVars(distrib, 'CoordinateZ', 0.) distrib = C.rmVars(distrib, 's') N = dims[1] val = C.getValue(zp, 's', ind) Xc = CPlot.getActivePoint() valf = val Pind = C.getValue(z, 'GridCoordinates', ind) if ind < N-1: # cherche avec indp1 Pindp1 = C.getValue(z, 'GridCoordinates', ind+1) v1 = Vector.sub(Pindp1, Pind) v2 = Vector.sub(Xc, Pind) if Vector.dot(v1,v2) >= 0: val2 = C.getValue(zp, 's', ind+1) alpha = Vector.norm(v2)/Vector.norm(v1) valf = val+alpha*(val2-val) if ind > 0 and val == valf: # cherche avec indm1 Pindm1 = C.getValue(z, 'GridCoordinates', ind-1) v1 = Vector.sub(Pindm1, Pind) v2 = Vector.sub(Xc, Pind) if Vector.dot(v1,v2) >= 0: val2 = C.getValue(zp, 's', ind-1) alpha = Vector.norm(v2)/Vector.norm(v1) valf = val+alpha*(val2-val) if h < 0: # enforce point distrib = G.enforcePoint(distrib, valf) else: # enforce h if val == 0: distrib = G.enforcePlusX(distrib, h/l, N/10, 1) elif val == 1: distrib = G.enforceMoinsX(distrib, h/l, N/10, 1) else: distrib = G.enforceX(distrib, valf, h/l, N/10, 1) try: a1 = G.map(a, distrib) CTK.replace(CTK.t, nob, noz, a1) except Exception as e: fail = True Panels.displayErrors([0,str(e)], header='Error: stretch1D') return fail
def apply3D(density, npts, factor, ntype): nzs = CPlot.getSelectedZones() nz = nzs[0] nob = CTK.Nb[nz]+1 noz = CTK.Nz[nz] zone = CTK.t[2][nob][2][noz] ret = getEdges3D(zone, 0.) if ret is None: return True (m, r, f, ue, uf, ind) = ret out = [] # Applique la fonction sur m i = m[0] dims = Internal.getZoneDim(i) np = dims[1]*dims[2]*dims[3] if ntype == 0: # uniformize if density > 0: npts = D.getLength(i)*density if factor > 0: npts = np*factor[0] npts = int(max(npts, 2)) distrib = G.cart((0,0,0), (1./(npts-1.),1,1), (npts,1,1)) b = G.map(i, distrib) elif ntype == 1: # refine if factor < 0: factor = (npts-1.)/(np-1) else: npts = factor*(np-1)+1 b = G.refine(i, factor, 1) elif ntype == 2: # stretch (factor=h) h = factor l = D.getLength(i) a = D.getCurvilinearAbscissa(i) distrib = C.cpVars(a, 's', a, 'CoordinateX') C._initVars(distrib, 'CoordinateY', 0.) C._initVars(distrib, 'CoordinateZ', 0.) distrib = C.rmVars(distrib, 's') N = dims[1] val = C.getValue(a, 's', ind) Xc = CPlot.getActivePoint() valf = val Pind = C.getValue(i, 'GridCoordinates', ind) if ind < N-1: # cherche avec indp1 Pindp1 = C.getValue(i, 'GridCoordinates', ind+1) v1 = Vector.sub(Pindp1, Pind) v2 = Vector.sub(Xc, Pind) if Vector.dot(v1,v2) >= 0: val2 = C.getValue(a, 's', ind+1) alpha = Vector.norm(v2)/Vector.norm(v1) valf = val+alpha*(val2-val) if ind > 0 and val == valf: # cherche avec indm1 Pindm1 = C.getValue(i, 'GridCoordinates', ind-1) v1 = Vector.sub(Pindm1, Pind) v2 = Vector.sub(Xc, Pind) if Vector.dot(v1,v2) >= 0: val2 = C.getValue(a, 's', ind-1) alpha = Vector.norm(v2)/Vector.norm(v1) valf = val+alpha*(val2-val) if h < 0: distrib = G.enforcePoint(distrib, valf) else: if val == 0: distrib = G.enforcePlusX(distrib, h/l, N/10, 1) elif val == 1: distrib = G.enforceMoinsX(distrib, h/l, N/10, 1) else: distrib = G.enforceX(distrib, valf, h/l, N/10, 1) b = G.map(i, distrib) elif ntype == 3: source = factor b = G.map(i, source, 1) elif ntype == 4: # smooth (factor=eps, npts=niter) niter = npts eps = factor a = D.getCurvilinearAbscissa(i) distrib = C.cpVars(a, 's', a, 'CoordinateX') C._initVars(distrib, 'CoordinateY', 0.) C._initVars(distrib, 'CoordinateZ', 0.) distrib = C.rmVars(distrib, 's') bornes = P.exteriorFaces(distrib) distrib = T.smooth(distrib, eps=eps, niter=niter, fixedConstraints=[bornes]) b = G.map(i, distrib, 1) dimb = Internal.getZoneDim(b) npts = dimb[1] out.append(b) # Raffine les edges si necessaires if npts != np: ret = getEdges3D(zone, 2.) if ret is None: return True (m, r, f, ue, uf, ind) = ret for i in r: dims = Internal.getZoneDim(i) np = dims[1]*dims[2]*dims[3] factor = (npts-1.)/(np-1) # npts de m b = G.refine(i, factor, 1) out.append(b) # Garde les autres out += ue outf = [] # Rebuild les faces for i in f: # trouve les edges de la face edges = P.exteriorFacesStructured(i) match = [] for e in edges: dime = Internal.getZoneDim(e) np = dime[1]-1 P0 = C.getValue(e, Internal.__GridCoordinates__, 0) P1 = C.getValue(e, Internal.__GridCoordinates__, np) for ei in out: # retrouve les edges par leurs extremites dimei = Internal.getZoneDim(ei) npi = dimei[1]-1 Q0 = C.getValue(ei, Internal.__GridCoordinates__, 0) Q1 = C.getValue(ei, Internal.__GridCoordinates__, npi) t1 = Vector.norm2(Vector.sub(P0,Q0)) t2 = Vector.norm2(Vector.sub(P1,Q1)) if (t1 < 1.e-12 and t2 < 1.e-12): match.append(ei) if len(match) == 4: # OK fn = G.TFI(match) # Projection du patch interieur #dimsf = Internal.getZoneDim(fn) #fns = T.subzone(fn, (2,2,1), (dimsf[1]-1,dimsf[2]-1,1)) #fns = T.projectOrtho(fns, [i]) #fn = T.patch(fn, fns, position=(2,2,1)) #fn = T.projectOrtho(fn, [i]) outf.append(fn) else: return True outf += uf try: b = G.TFI(outf) CTK.replace(CTK.t, nob, noz, b) return False except Exception as e: Panels.displayErrors([0,str(e)], header='Error: apply3D') return True
def apply2D(density, npts, factor, ntype=0): nzs = CPlot.getSelectedZones() nz = nzs[0] nob = CTK.Nb[nz]+1 noz = CTK.Nz[nz] zone = CTK.t[2][nob][2][noz] ret = getEdges2D(zone, 0.) if ret is None: return True (m, r, u, ind) = ret out = [] # Applique la fonction sur m[0] (edge a modifier) i = m[0] dims = Internal.getZoneDim(i) np = dims[1]*dims[2]*dims[3] if ntype == 0: # uniformize if density > 0: npts = D.getLength(i)*density if factor > 0: npts = np*factor[0] npts = int(max(npts, 2)) distrib = G.cart((0,0,0), (1./(npts-1.),1,1), (npts,1,1)) b = G.map(i, distrib) elif ntype == 1: # refine if factor < 0: factor = (npts-1.)/(np-1) else: npts = factor*(np-1)+1 b = G.refine(i, factor, 1) elif ntype == 2: # stretch (factor=h) h = factor l = D.getLength(i) a = D.getCurvilinearAbscissa(i) distrib = C.cpVars(a, 's', a, 'CoordinateX') C._initVars(distrib, 'CoordinateY', 0.) C._initVars(distrib, 'CoordinateZ', 0.) distrib = C.rmVars(distrib, 's') N = dims[1] val = C.getValue(a, 's', ind) Xc = CPlot.getActivePoint() valf = val Pind = C.getValue(i, 'GridCoordinates', ind) if ind < N-1: # cherche avec indp1 Pindp1 = C.getValue(i, 'GridCoordinates', ind+1) v1 = Vector.sub(Pindp1, Pind) v2 = Vector.sub(Xc, Pind) if Vector.dot(v1,v2) >= 0: val2 = C.getValue(a, 's', ind+1) alpha = Vector.norm(v2)/Vector.norm(v1) valf = val+alpha*(val2-val) if ind > 0 and val == valf: # cherche avec indm1 Pindm1 = C.getValue(i, 'GridCoordinates', ind-1) v1 = Vector.sub(Pindm1, Pind) v2 = Vector.sub(Xc, Pind) if Vector.dot(v1,v2) >= 0: val2 = C.getValue(a, 's', ind-1) alpha = Vector.norm(v2)/Vector.norm(v1) valf = val+alpha*(val2-val) if h < 0: distrib = G.enforcePoint(distrib, valf) else: if val == 0: distrib = G.enforcePlusX(distrib, h/l, N/10, 1) elif val == 1: distrib = G.enforceMoinsX(distrib, h/l, N/10, 1) else: distrib = G.enforceX(distrib, valf, h/l, N/10, 1) b = G.map(i, distrib) elif ntype == 3: # copyDistrib (factor=source=edge pour l'instant) source = factor b = G.map(i, source, 1) elif ntype == 4: # smooth (factor=eps, npts=niter) niter = npts eps = factor a = D.getCurvilinearAbscissa(i) distrib = C.cpVars(a, 's', a, 'CoordinateX') C._initVars(distrib, 'CoordinateY', 0.) C._initVars(distrib, 'CoordinateZ', 0.) distrib = C.rmVars(distrib, 's') bornes = P.exteriorFaces(distrib) distrib = T.smooth(distrib, eps=eps, niter=niter, fixedConstraints=[bornes]) b = G.map(i, distrib, 1) dimb = Internal.getZoneDim(b) npts = dimb[1] out.append(b) # Raffine les edges si necessaires if npts != np: ret = getEdges2D(zone, 2.) if ret is None: return True (m, r, u, ind) = ret for i in r: dims = Internal.getZoneDim(i) np = dims[1]*dims[2]*dims[3] factor = (npts-1.)/(np-1) # npts de m b = G.refine(i, factor, 1) out.append(b) # Garde les autres out += u #tp = C.newPyTree(['Base']) #tp[2][1][2] += out #C.convertPyTree2File(tp, 'edges.cgns') # Rebuild try: b = G.TFI(out) # Projection du patch interieur #dimsb = Internal.getZoneDim(b) #bs = T.subzone(b, (2,2,1), (dimsb[1]-1,dimsb[2]-1,1)) #bs = T.projectOrtho(bs, [zone]) #b = T.patch(b, bs, position=(2,2,1)) #tp = C.newPyTree(['Base']) #tp[2][1][2] += [b, zone] #C.convertPyTree2File(tp, 'face.cgns') b = T.projectOrtho(b, [zone]) CTK.replace(CTK.t, nob, noz, b) return False except Exception as e: Panels.displayErrors([0,str(e)], header='Error: apply2D') return True
def travelLeft(xr=0.1, N=100): """Travel posCam left.""" import KCore.Vector as Vector posCam = getState('posCam') posEye = getState('posEye') dirCam = getState('dirCam') d1 = Vector.sub(posEye, posCam) R = Vector.norm(d1) L = 2 * 3.14 * R * xr * 0.5 d2 = Vector.cross(d1, dirCam) d2 = Vector.normalize(d2) d2 = Vector.mul(L, d2) P2 = Vector.add(posCam, d2) d3 = Vector.sub(posEye, P2) d4 = Vector.cross(d3, dirCam) d4 = Vector.normalize(d4) d4 = Vector.mul(L, d4) P3 = Vector.add(P2, d4) checkPoints = [posCam, tuple(P2), tuple(P3)] moveCamera(checkPoints, N=N)
def moveCamera(posCams, posEyes=None, dirCams=None, moveEye=False, N=100, speed=1., pos=-1): """Move posCam and posEye following check points.""" # Set d, array of posCams and N nbre of points import KCore import Geom import KCore.Vector as Vector if len(posCams) == 5 and isinstance(posCams[0], str): # input struct array N = posCams[2] d = posCams pinc = KCore.isNamePresent(posCams, 'incEye') if pinc >= 0: pinc = posCams[1][pinc] else: pinc = None else: # list N = max(N, 3) Np = len(posCams) pOut = [] P0 = posCams[0] pOut.append(P0) for i in range(1, Np): P1 = posCams[i] sub = Vector.sub(P1, P0) if Vector.norm(sub) > 1.e-10: pOut.append(P1) P0 = P1 if len(pOut) == 1: d = Geom.polyline(pOut * N) elif len(pOut) == 2: p = Geom.polyline(pOut) d = Geom.spline(p, 2, N) else: p = Geom.polyline(pOut) d = Geom.spline(p, 3, N) pinc = None # Set e, array of posEye of N pts if posEyes is not None: if len(posEyes) == 5 and isinstance(posEyes[0], str): # input struct array Neye = posEyes[2] if Neye != N: import Generator dis = Geom.getDistribution(d) posEyes = Generator.map(posEyes, dis, 1) e = posEyes else: # list Np = len(posEyes) pOut = [] P0 = posEyes[0] pOut.append(P0) for i in range(1, Np): P1 = posEyes[i] sub = Vector.sub(P1, P0) if Vector.norm(sub) > 1.e-10: pOut.append(P1) P0 = P1 if len(pOut) == 1: e = Geom.polyline(pOut * N) elif len(pOut) == 2: p = Geom.polyline(pOut) e = Geom.spline(p, 2, N) else: p = Geom.polyline(pOut) e = Geom.spline(p, 3, N) else: e = None # Set dc, array of dirCams of N pts if dirCams is not None: if len(dirCams) == 5 and isinstance(dirCams[0], str): # input struct array Ndc = dirCams[2] if Ndc != N: import Generator dis = Geom.getDistribution(d) dirCams = Generator.map(dirCams, dis, 1) dc = dirCams else: # list p = Geom.polyline(dirCams) if len(dirCams) == 2: dc = Geom.spline(p, 2, N) else: dc = Geom.spline(p, 3, N) else: dc = None if pos == -1: i = 0 while i < N - 1: time.sleep(__timeStep__ * speed * 0.06) if i > N - 11: inc = N - i - 1 else: inc = 10 posCam = (d[1][0, i], d[1][1, i], d[1][2, i]) if e is not None: posEye = (e[1][0, i], e[1][1, i], e[1][2, i]) if dc is not None: dirCam = (dc[1][0, i], dc[1][1, i], dc[1][2, i]) setState(posCam=posCam, posEye=posEye, dirCam=dirCam) else: setState(posCam=posCam, posEye=posEye) elif moveEye: posEye = (d[1][0, i + inc], d[1][1, i + inc], d[1][2, i + inc]) setState(posCam=posCam, posEye=posEye) else: setState(posCam=posCam) i += 1 else: i = pos i = min(pos, N - 1) if pinc is not None: inc = int(pinc[i]) else: inc = 10 inc = min(inc, N - i - 1) posCam = (d[1][0, i], d[1][1, i], d[1][2, i]) if e is not None: posEye = (e[1][0, i], e[1][1, i], e[1][2, i]) if dc is not None: dirCam = (dc[1][0, i], dc[1][1, i], dc[1][2, i]) setState(posCam=posCam, posEye=posEye, dirCam=dirCam) else: setState(posCam=posCam, posEye=posEye) elif moveEye: posEye = (d[1][0, i + inc], d[1][1, i + inc], d[1][2, i + inc]) setState(posCam=posCam, posEye=posEye) else: setState(posCam=posCam)
def savePovFile(): if CTK.t == []: return # Sauvegarde toutes les zones au format pov rep = VARS[2].get() dir = os.path.dirname(CTK.FILE) rep = os.path.join(dir, rep) os.chdir(rep) zones = Internal.getZones(CTK.t) c = 0; files = [] colors = []; materials = []; blendings = []; shader1s = [] scales = []; centers = [] for z in zones: z = C.convertArray2Tetra(z) # Scale (utlise pour scaler les textures) bb = G.bbox(z) rx = bb[3]-bb[0]; ry = bb[4]-bb[1]; rz = bb[5]-bb[2] scale = min(rx, ry, rz) scales.append(scale) centers.append([bb[0]+0.5*rx, bb[1]+0.5*ry, bb[2]+0.5*rz]) # Material/Color/Blending material = 'Solid'; color = 'White'; mode = 0; blending = 1; shader1 = 1. ri = Internal.getNodesFromName1(z, '.RenderInfo') if (ri != []): # Material mt = Internal.getNodesFromName1(ri[0], 'Material') if (mt != []): material = Internal.getValue(mt[0]) # Color co = Internal.getNodesFromName1(ri[0], 'Color') if (co != []): color = Internal.getValue(co[0]) # Blending co = Internal.getNodesFromName1(ri[0], 'Blending') if (co != []): blending = Internal.getValue(co[0]) # Shader parameter 1 co = Internal.getNodesFromName1(ri[0], 'ShaderParameters') if (co != []): shader1 = co[0][1][0] else: shader1 = 1. s = color.split(':') if (len(s) == 2 and s[0] == 'Iso'): # couleur = iso field vref = C.getVarNames(z)[0] for pos in xrange(len(vref)): if (vref[pos] == s[1]): break if (pos == len(vref)): color = 'White'; mode = 0 else: color = 'Iso'; mode = pos+1 # traduction color si #FFFFFF if (color[0] == '#'): colorR = color[1:3]; colorG = color[3:5]; colorB = color[5:] colorR = int(colorR, 16); colorR = colorR / 255. colorG = int(colorG, 16); colorG = colorG / 255. colorB = int(colorB, 16); colorB = colorB / 255. color = 'rgbf<'+str(colorR)+','+str(colorG)+','+str(colorB)+'>' colors.append(color); materials.append(material) blendings.append(blending); shader1s.append(shader1) nt = C.newPyTree(['Base']) nt[2][1][2].append(z) try: if (mode == 0): C.convertPyTree2File(nt, 'mesh_'+str(c)+'.pov') else: C.convertPyTree2File(nt, 'mesh_'+str(c)+'.pov', colormap=mode) # avec iso files.append('mesh_'+str(c)+'.pov') c += 1 except: pass # Cam position eye = CPlot.getState('posEye') cam = CPlot.getState('posCam') dir = CPlot.getState('dirCam') # Ecriture du fichier PovRay file = open('scene.pov', 'w') file.write('// POV-Ray version 3.6 scenery file written by *Cassiopee*\n') file.write('// Please render this file with :\n') file.write('// povray -W800 -H600 +a0.3 +SP16 scene.pov +P\n') file.write('#version 3.6;\n') file.write('#include "colors.inc"\n') file.write('#include "textures.inc"\n') file.write('#include "woods.inc"\n') file.write('#include "stones.inc"\n') # Brushed metal texture file.write('#declare Brushed_Depth = 10; // Bump size\n') file.write('#declare Brushed_Pigment = pigment {colour rgb 0.73} \n') file.write('#declare Brushed_Finish = finish {ambient 0 diffuse 0.95 specular 0.96 roughness 0.0005 phong 0.43 phong_size 25 brilliance 3.15 reflection 0.33 metallic metallic on }\n') file.write('// The brushed metal texture.\n') file.write('#declare Brushed_Texture = texture { average texture_map { [ pigment {Brushed_Pigment} normal {wood +Brushed_Depth ramp_wave rotate 90*x scale 50} finish {Brushed_Finish} ] [pigment {Brushed_Pigment} normal {wood -Brushed_Depth ramp_wave rotate 90*x scale 50} finish {Brushed_Finish} ] } }\n') # XRay texture file.write('#declare XRayTexture2 = texture { pigment { slope{'+'<' +str(cam[0])+' , '+str(cam[1])+' , '+str(cam[2])+'> - <' +str(eye[0])+' , '+str(eye[1])+' , '+str(eye[2])+'>}\n') file.write('pigment_map {[0 color rgbt 2*<1,1,1,0.1>] [0.75 color rgbt <0.1,0.6,2,1>*1] [1 color rgbt <1,1,1,1>] } } finish {ambient 3} }\n') # - Radiosity - #file.write('global_settings { assumed_gamma 1 radiosity { \n') #file.write('pretrace_start 0.08 \n') #file.write('pretrace_end 0.02 \n') #file.write('count 50 \n') #file.write('error_bound 0.5 \n') #file.write('recursion_limit 1 } } \n') # - Camera - file.write('#declare Cam0 = camera {angle 50 \n') file.write('#location <'+str(cam[0])+' , '+str(cam[1])+' , '+ str(cam[2])+'> \n') file.write('#look_at <'+str(eye[0])+' , '+str(eye[1])+' , '+ str(eye[2])+'>\n') file.write('#direction <-1,0,0>\n') file.write('#sky <'+str(dir[0])+','+str(dir[1])+','+str(dir[2])+'> }\n') # focal blur (experimental) #file.write('#focal_point <'+str(eye[0])+' , '+str(eye[1])+' , '+ # str(eye[2])+'>\n') #file.write('#aperture 0.4\n') #file.write('#blur_samples 20 }\n') file.write('camera{Cam0}\n') # - Lumieres: point - #d = Vector.sub(eye, cam) #n = Vector.cross(d, dir) dir = Vector.normalize(dir) #n = Vector.normalize(n) #norm = Vector.norm(d) #d = Vector.normalize(d) #n = Vector.sub(n, dir) #n = Vector.add(n, d) #n = Vector.mul(0.4*norm, n) #pos = Vector.sub(eye, n) dir = Vector.mul(0.1, dir) pos = Vector.add(cam, dir) c = 0; light = 0 for f in files: material = materials[c] if (material == 'Light'): xc = centers[c]; color = colors[c] light = 1 intensity = shader1s[c]*5. file.write('light_source{<'+str(xc[0])+' , '+str(xc[1])+' , '+ str(xc[2])+'> color '+color+'*'+str(intensity)+'}\n') c += 1 if (light == 0): # pas de lumiere dans l'arbre, on met celle par defaut file.write('light_source{<'+str(pos[0])+' , '+str(pos[1])+' , '+ str(pos[2])+'> color White*4}\n') # - Background - bckgrd = VARS[0].get() if (bckgrd == 'Blue sky'): # Ciel bleu file.write('sky_sphere { pigment { gradient <0,0,1> turbulence 0\n') file.write(' color_map { [0.00 rgb <0.6,0.7,1.0>]\n') file.write(' [0.35 rgb <0.1,0.2,0.8>]\n') file.write(' [0.65 rgb <0.1,0.2,0.8>]\n') file.write(' [1.00 rgb <0.6,0.7,1.0>]\n') file.write(' }\n') file.write(' scale 2\n') file.write(' } // end of pigment\n') file.write(' } //end of skysphere\n') elif (bckgrd == 'Cloudy sky'): # on pourrait faire beaucoup mieux file.write('sky_sphere { \n') file.write('pigment{ bozo turbulence 0.76\n') file.write(' color_map { [0.5 rgb <0.20, 0.20, 1.0>]\n') file.write(' [0.6 rgb <1,1,1>]\n') file.write(' [1.0 rgb <0.5,0.5,0.5>]\n') file.write(' }\n') file.write(' } // end of pigment\n') file.write(' } //end of skysphere\n') # Avec les macros de pov, pas satisfaisant #file.write('#include "skies.inc" \n') #file.write("object{ O_Cloud1 rotate 90*x}\n") #file.write("sphere { <0,0,0>, 100 \n") #file.write("texture {T_Cloud3} scale 100 }\n") elif (bckgrd == 'Starfield'): file.write('#include "stars.inc"\n') file.write('sphere { <0,0,0>, 1\n') file.write('texture { Starfield1 }\n') file.write('scale 10000\n') file.write(' } //end of sphere\n') elif (bckgrd == 'White'): file.write('sky_sphere { pigment { White } }\n') # Objets c = 0 for f in files: color = colors[c]; material = materials[c] blend = str(1.-blendings[c]) if (material == 'Solid' or material == 'None'): # OK file.write('#include "'+f+'"\n') file.write('object {mesh_'+str(c)+'\n') if (color != 'Iso'): file.write('texture{pigment{color '+color+' filter '+ blend+'}\n') else: file.write('texture{pigment{filter '+ blend+'}\n') file.write('finish {ambient 0.1 diffuse 0.1 reflection 0.05 phong 0.5 }}\n') file.write('}\n') elif (material == 'Flat'): file.write('#include "'+f+'"\n') file.write('object {mesh_'+str(c)+'\n') if (color != 'Iso'): file.write('texture{pigment{color '+color+' filter '+ blend+'}\n') else: file.write('texture{pigment{filter '+ blend+'}\n') file.write('finish {ambient 0.1 diffuse 0.1 reflection 0.0 phong 0.0 }}\n') file.write('}\n') elif (material == 'Glass'): # OK file.write('#include "'+f+'"\n') file.write('object {mesh_'+str(c)+'\n') if (color != 'Iso'): file.write('texture{pigment{color '+color+' filter '+ str(-0.1*blendings[c] +1.)+' }\n') else: file.write('texture{pigment{filter '+ str(-0.1*blendings[c] +1.)+' }\n') file.write('finish {reflection 0.2 phong 0.7 }}\n') file.write('interior { ior 1.3 }\n') file.write('}\n') elif (material == 'Chrome'): # OK file.write('#include "'+f+'"\n') file.write('object {mesh_'+str(c)+'\n') if (color != 'Iso'): file.write('texture{pigment{color '+color+' filter '+ blend+'}\n') else: file.write('texture{pigment{filter '+ blend+'}\n') file.write('finish {ambient 0.25 brilliance 4 diffuse 0.5 reflection 0.4 specular 0.2 metallic roughness 1/80 }}\n') file.write('}\n') elif (material == 'Metal'): file.write('#include "'+f+'"\n') file.write('object {mesh_'+str(c)+'\n') if (color == 'White'): file.write('texture {pigment{color rgb 0.73}\n') elif (color != 'Iso'): file.write('texture {pigment{color '+color+' filter '+ blend+'}\n') else: file.write('texture {pigment{filter '+ blend+'}\n') file.write('finish {ambient 0 diffuse 0.95 specular 0.26 roughness 0.0005 phong 0.33 phong_size 2 brilliance 3.15 reflection 0.33 metallic metallic on } }\n') file.write('}\n') elif (material == 'XRay'): file.write('#include "'+f+'"\n') file.write('object {mesh_'+str(c)+'\n') file.write('texture { XRayTexture2 } }\n') elif (material == 'Wood'): # OK file.write('#include "'+f+'"\n') file.write('object {mesh_'+str(c)+'\n') if (color == 'White'): file.write('texture{T_Wood3 \n') elif (color == 'Black'): file.write('texture{T_Wood2 \n') elif (color == 'Blue'): file.write('texture{T_Wood31 \n') elif (color == 'Red'): file.write('texture{T_Wood6 \n') elif (color == 'Green'): file.write('texture{T_Wood32 \n') elif (color == 'Yellow'): file.write('texture{T_Wood35 \n') elif (color == 'Orange'): file.write('texture{T_Wood7 \n') elif (color == 'Magenta'): file.write('texture{T_Wood4 \n') else: file.write('texture{T_Wood32 \n') file.write("scale "+str(scales[c])+"\n"); file.write('finish {ambient 0.7 brilliance 0.2 diffuse 0.1 reflection 0.01 specular 0.1 roughness 1/20 }}\n') file.write('}\n') elif (material == 'Marble' or material == 'Granite'): file.write('#include "'+f+'"\n') file.write('object {mesh_'+str(c)+'\n') if (color == 'White'): #file.write('texture{T_Grnt20 \n') file.write('texture{White_Marble \n') elif (color == 'Black'): file.write('texture{T_Grnt15 \n') elif (color == 'Blue'): file.write('texture{T_Wood6 \n') elif (color == 'Red'): file.write('texture{T_Wood28 \n') elif (color == 'Green'): file.write('texture{T_Grnt21 \n') elif (color == 'Yellow'): file.write('texture{T_Wood1 \n') elif (color == 'Orange'): file.write('texture{T_Wood13 \n') elif (color == 'Magenta'): file.write('texture{T_Grnt14 \n') else: file.write('texture{T_Grnt20 \n') file.write('scale '+str(scales[c])+'\n'); file.write('finish {ambient 0.3 brilliance 0.3 diffuse 0.1 reflection 0.1 specular 0.1 }}\n') file.write('}\n') elif (material == 'Smoke'): file.write('#include "'+f+'"\n') file.write('object {mesh_'+str(c)+'\n') file.write('texture{pigment{color '+color+' filter 1.}\n') #file.write('texture{pigment { rgbt 1 }\n') file.write('finish {ambient 0.1 diffuse 0.1 }}\n') file.write('hollow\n') file.write('interior{ //---------------------\n') file.write('media{ method 2 \n') file.write('emission 0. \n') file.write('scattering{ 1, // Type \n') file.write('<1,1,1>*0.2 // color of scattering haze \n') file.write('extinction 1. \n') file.write('// how fast the scattering media absorbs light \n') file.write('// useful if the media absorbs too much light \n') file.write('} // end scattering \n') file.write('density{ bozo \n') file.write('turbulence 8.0 \n') file.write(' color_map { \n') file.write(' [0.00 rgb 0] \n') file.write(' [0.05 rgb 0] \n') file.write(' [0.20 rgb 0.2] \n') file.write(' [0.30 rgb 0.6] \n') file.write(' [0.40 rgb 1] \n') file.write(' [1.00 rgb 1] \n') file.write(' } // end color_map \n') file.write('scale '+str(scales[c])+'\n'); file.write('} // end of density \n') file.write('samples 1,1 // 3,3 for adaptive sampling \n') file.write('intervals 10 // increase up to 15 \n') file.write('} // end of media --------------------------- \n') file.write('} // end of interior \n') file.write('}\n') c += 1 file.close() os.chdir('..')
def writeCassiopeeLamps(file): type = VARS[2].get() eye = CPlot.getState('posEye') cam = CPlot.getState('posCam') dir = CPlot.getState('dirCam') d = Vector.sub(eye, cam) n = Vector.cross(d, dir) dir = Vector.normalize(dir) n = Vector.normalize(n) norm = Vector.norm(d) d = Vector.normalize(d) n = Vector.sub(n, dir) n = Vector.add(n, d) n = Vector.mul(0.4 * norm, n) pos = Vector.sub(eye, n) if type == 'Interior': pass ## # distant light ## file.write('AttributeBegin\n') ## file.write('LightGroup "default"\n') ## file.write('Exterior "world"\n') ## file.write('LightSource "distant"\n') ## file.write(' "point from" ['+ ## str(pos[0])+' '+str(pos[1])+' '+str(pos[2])+ ## '] "point to" ['+ ## str(eye[0])+' '+str(eye[1])+' '+str(eye[2])+']\n') ## file.write(' "color L" [5 5 5]\n') ## file.write('AttributeEnd\n') else: # Exterior # sun/sky sundir = Vector.sub(pos, eye) sundir = Vector.normalize(sundir) file.write('AttributeBegin\n') file.write('LightGroup "default"\n') file.write('Exterior "world"\n') file.write('LightSource "sunsky"\n') file.write(' "vector sundir" [' + str(sundir[0]) + ' ' + str(sundir[1]) + ' ' + str(sundir[2]) + ']\n') file.write(' "float turbidity" [2.0]\n') file.write(' "float gain" [0.005]\n') file.write('AttributeEnd\n')
def replaceText(event=None): if CTK.t == []: return if CTK.__MAINTREE__ <= 0: CTK.TXT.insert('START', 'Fail on a temporary tree.\n') CTK.TXT.insert('START', 'Error: ', 'Error') return nzs = CPlot.getSelectedZones() if nzs == []: CTK.TXT.insert('START', 'Selection is empty.\n') CTK.TXT.insert('START', 'Error: ', 'Error') return CTK.saveTree() # Recupere l'OBB de la selection Z = [] dels = [] for nz in nzs: nob = CTK.Nb[nz] + 1 noz = CTK.Nz[nz] Z.append(CTK.t[2][nob][2][noz]) dels.append(CTK.t[2][nob][0] + Internal.SEP1 + CTK.t[2][nob][2][noz][0]) nob0 = CTK.Nb[nzs[0]] + 1 try: a = T.join(Z) except: a = Z[0] OBB = G.BB(a, method='OBB') P0 = C.getValue(OBB, 'GridCoordinates', 0) P1 = C.getValue(OBB, 'GridCoordinates', 1) P2 = C.getValue(OBB, 'GridCoordinates', 2) P3 = C.getValue(OBB, 'GridCoordinates', 4) v1 = Vector.sub(P1, P0) n1 = Vector.norm(v1) v2 = Vector.sub(P2, P0) n2 = Vector.norm(v2) v3 = Vector.sub(P3, P0) n3 = Vector.norm(v3) v1p = v1 v2p = v2 v3p = v3 if abs(n1) < 1.e-12: v1 = Vector.cross(v2, v3) v1p = (0., 0., 0.) elif abs(n2) < 1.e-12: v2 = Vector.cross(v1, v3) v2p = (0., 0., 0.) elif abs(n3) < 1.e-12: v3 = Vector.cross(v2, v3) v3p = (0., 0., 0.) # Essaie de matcher les vecteur sur la vue p1,p2,p3 # On suppose que dirCam doit etre e2, ... posCam = CPlot.getState('posCam') posEye = CPlot.getState('posEye') dirCam = CPlot.getState('dirCam') e2 = dirCam e3 = Vector.sub(posCam, posEye) e1 = Vector.cross(e2, e3) f1 = None f2 = None f3 = None Pt = P0 s1 = Vector.dot(e1, v1) s2 = Vector.dot(e1, v2) s3 = Vector.dot(e1, v3) if abs(s1) > abs(s2) and abs(s1) > abs(s3): if s1 > 0: f1 = v1 else: f1 = Vector.mul(-1., v1) Pt = Vector.add(Pt, v1p) elif abs(s2) > abs(s1) and abs(s2) > abs(s3): if s2 > 0: f1 = v2 else: f1 = Vector.mul(-1., v2) Pt = Vector.add(Pt, v2p) elif abs(s3) > abs(s1) and abs(s3) > abs(s2): if s3 > 0: f1 = v3 else: f1 = Vector.mul(-1., v3) Pt = Vector.add(Pt, v3p) s1 = Vector.dot(e2, v1) s2 = Vector.dot(e2, v2) s3 = Vector.dot(e2, v3) if abs(s1) > abs(s2) and abs(s1) > abs(s3): if s1 > 0: f2 = v1 else: f2 = Vector.mul(-1., v1) Pt = Vector.add(Pt, v1p) elif abs(s2) > abs(s1) and abs(s2) > abs(s3): if s2 > 0: f2 = v2 else: f2 = Vector.mul(-1., v2) Pt = Vector.add(Pt, v2p) elif abs(s3) > abs(s1) and abs(s3) > abs(s2): if s3 > 0: f2 = v3 else: f2 = Vector.mul(-1., v3) Pt = Vector.add(Pt, v3p) s1 = Vector.dot(e3, v1) s2 = Vector.dot(e3, v2) s3 = Vector.dot(e3, v3) if abs(s1) > abs(s2) and abs(s1) > abs(s3): if s1 > 0: f3 = v1 else: f3 = Vector.mul(-1., v1) Pt = Vector.add(Pt, v1p) elif abs(s2) > abs(s1) and abs(s2) > abs(s3): if s2 > 0: f3 = v2 else: f3 = Vector.mul(-1., v2) Pt = Vector.add(Pt, v2p) elif abs(s3) > abs(s1) and abs(s3) > abs(s2): if s3 > 0: f3 = v3 else: f3 = Vector.mul(-1., v3) Pt = Vector.add(Pt, v3p) (x0, y0, z0) = Pt n2 = Vector.norm(f2) # Cree le texte text = VARS[0].get() type = VARS[1].get() font = VARS[3].get() smoothness = VARS[2].get() smooth = 0 if smoothness == 'Regular': smooth = 0 elif smoothness == 'Smooth': smooth = 2 elif smoothness == 'Very smooth': smooth = 4 if type == '3D': a = D.text3D(text, smooth=smooth, font=font) elif type == '2D': a = D.text2D(text, smooth=smooth, font=font) elif type == '1D': a = D.text1D(text, smooth=smooth, font=font) a = C.convertArray2Tetra(a) a = T.join(a) BB = G.bbox(a) h2 = BB[4] - BB[1] # Scale, positionne le texte factor = n2 / h2 a = T.homothety(a, (BB[0], BB[1], BB[2]), factor) a = T.translate(a, (x0 - BB[0], y0 - BB[1], z0 - BB[2])) a = T.rotate(a, (x0, y0, z0), ((1, 0, 0), (0, 1, 0), (0, 0, 1)), ((f1, f2, f3))) CTK.t = CPlot.deleteSelection(CTK.t, CTK.Nb, CTK.Nz, nzs) CPlot.delete(dels) CTK.add(CTK.t, nob0, -1, a) #C._fillMissingVariables(CTK.t) CTK.TXT.insert('START', 'Text replaced.\n') (CTK.Nb, CTK.Nz) = CPlot.updateCPlotNumbering(CTK.t) CTK.TKTREE.updateApp() CPlot.render()